This commit is contained in:
Timothy Jaeryang Baek committed 2026-07-06 18:17:36 -05:00
1 parent ac1e56aa41
commit 380addd664
3 files changed
+24 -50

No files matched your search

+1 -11
View File
@@ -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
+21 -39
View File
@@ -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
+2
View File
@@ -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.