From 380addd664584dccfed50c9ad6d3f74d3d96ac7a Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 6 Jul 2026 18:17:36 -0500 Subject: [PATCH] refac --- cptr/routers/chat.py | 12 +-------- cptr/utils/chat_task.py | 60 +++++++++++++++-------------------------- cptr/utils/tools.py | 2 ++ 3 files changed, 24 insertions(+), 50 deletions(-) diff --git a/cptr/routers/chat.py b/cptr/routers/chat.py index e81ee1a..c03087c 100644 --- a/cptr/routers/chat.py +++ b/cptr/routers/chat.py @@ -680,7 +680,7 @@ async def approve_tool(chat_id: str, message_id: str, body: ApproveRequest, requ ) # Emit artifact card if the tool produced an artifact - from cptr.utils.chat_task import build_artifact_item, build_image_item + from cptr.utils.chat_task import build_artifact_item artifact_item = build_artifact_item(call["name"], call.get("arguments", {}), result) if artifact_item: @@ -706,16 +706,6 @@ async def approve_tool(chat_id: str, message_id: str, body: ApproveRequest, requ {"chat_id": chat_id, "message_id": message_id, "output": file_item}, ) - image_item = build_image_item(call["name"], result) - if image_item: - output.append(image_item) - from cptr.socket.main import emit_to_user - - await emit_to_user( - user_id, - {"chat_id": chat_id, "message_id": message_id, "output": image_item}, - ) - await ChatMessage.update(message_id, output=output, done=False) # Resolve model target and continue diff --git a/cptr/utils/chat_task.py b/cptr/utils/chat_task.py index 46c12b3..5ca4957 100644 --- a/cptr/utils/chat_task.py +++ b/cptr/utils/chat_task.py @@ -876,21 +876,33 @@ def _tool_result_for_model(tool_name: str, result: str) -> str: if not isinstance(images, list): return result + image_files = [ + { + "path": image.get("path"), + "name": image.get("name"), + "content_type": image.get("content_type"), + } + for image in images + if isinstance(image, dict) and image.get("path") + ] + return json.dumps( { "status": meta.get("status", "success"), - "displayed_to_user": True, - "note": "The generated image is already displayed in the chat. Do not include markdown image links for it.", - "images": [ + "displayed_to_user": False, + "must_call": "display_file", + "instruction": ( + "Do not answer the user yet. The image file is saved but not rendered. " + "Call display_file once for each path below." + ), + "display_file_calls": [ { - "id": image.get("id"), - "path": image.get("path"), - "name": image.get("name"), - "mime_type": image.get("mime_type"), + "name": "display_file", + "arguments": {"path": image["path"]}, } - for image in images - if isinstance(image, dict) + for image in image_files ], + "images": image_files, } ) @@ -1199,30 +1211,6 @@ def build_artifact_item(tool_name: str, arguments: dict, result: str) -> dict | } -def build_image_item(tool_name: str, result: str) -> dict | None: - """Build a renderable image output item for image-generation tools.""" - if tool_name != "image_generate": - return None - - try: - meta = json.loads(result) - except (json.JSONDecodeError, TypeError): - return None - - images = meta.get("images") - if not isinstance(images, list) or not images: - return None - - return { - "type": "image", - "images": [ - image - for image in images - if isinstance(image, dict) and isinstance(image.get("url"), str) - ], - } - - def _default_base_url(provider: str) -> str: return { "anthropic": "https://api.anthropic.com/v1", @@ -2068,12 +2056,6 @@ async def run_chat_task( await emit(output=file_item) _sync_state() - image_item = build_image_item(tc["name"], result) - if image_item: - output_items.append(image_item) - await emit(output=image_item) - _sync_state() - sequential_results.append((tc, result)) # Build a single combined assistant message for all sequential calls diff --git a/cptr/utils/tools.py b/cptr/utils/tools.py index 2281943..e2268bc 100644 --- a/cptr/utils/tools.py +++ b/cptr/utils/tools.py @@ -1700,6 +1700,8 @@ async def image_generate( __context__: dict, ) -> str: """Generate or edit image files from a prompt. + Returns saved image file paths. You must call display_file next for each returned path + before responding to the user. :param prompt: Detailed description of the image to create or the edits to make. :param image: Optional source image file id, /api/files/... URL, or workspace path for edit mode. :param images: Optional source image file ids, /api/files/... URLs, or workspace paths for edit mode.