From 759ffd1b2dafa997d300cd93d37eb3850382b1d5 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Fri, 19 Jun 2026 23:58:06 +0200 Subject: [PATCH] refac --- .../components/chat/AssistantMessage.svelte | 2 + .../chat/ToolCallCollapsible.svelte | 2 +- cptr/utils/chat_task.py | 4 +- cptr/utils/tools.py | 103 +++++++++--------- 4 files changed, 54 insertions(+), 57 deletions(-) diff --git a/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte b/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte index eea6b0d..17b5658 100644 --- a/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte +++ b/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte @@ -177,6 +177,8 @@ return _t('chat.tool.checkTask', { id: args.task_id || '?' }); case 'kill_task': return _t('chat.tool.killTask', { id: args.task_id || '?' }); + case 'image_generate': + return args.image || args.images?.length ? 'Edit image' : 'Generate image'; case 'web_search': return _t('chat.tool.webSearch', { query: args.query || '?' }); case 'read_url': { diff --git a/cptr/frontend/src/lib/components/chat/ToolCallCollapsible.svelte b/cptr/frontend/src/lib/components/chat/ToolCallCollapsible.svelte index c66e2f7..cb90c6e 100644 --- a/cptr/frontend/src/lib/components/chat/ToolCallCollapsible.svelte +++ b/cptr/frontend/src/lib/components/chat/ToolCallCollapsible.svelte @@ -27,7 +27,7 @@ const isRejected = $derived(item.status === 'rejected'); const isPending = $derived(item.status === 'pending'); const imageToolOutput = $derived.by(() => { - if (!['generate_image', 'edit_image'].includes(toolName) || !pairedOutput?.output) { + if (toolName !== 'image_generate' || !pairedOutput?.output) { return null; } try { diff --git a/cptr/utils/chat_task.py b/cptr/utils/chat_task.py index 2065e86..9e5f7a7 100644 --- a/cptr/utils/chat_task.py +++ b/cptr/utils/chat_task.py @@ -674,7 +674,7 @@ def _parse_image_data_uri(result: str) -> tuple[str, str] | None: def _tool_result_for_model(tool_name: str, result: str) -> str: """Return the tool result text to send back to the model.""" - if tool_name not in {"generate_image", "edit_image"}: + if tool_name != "image_generate": return result try: @@ -966,7 +966,7 @@ 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 not in {"generate_image", "edit_image"}: + if tool_name != "image_generate": return None try: diff --git a/cptr/utils/tools.py b/cptr/utils/tools.py index 009df49..b6d72d1 100644 --- a/cptr/utils/tools.py +++ b/cptr/utils/tools.py @@ -1439,67 +1439,60 @@ async def browser_evaluate(javascript: str, *, __context__: dict) -> str: return await client.evaluate(javascript) -async def generate_image( +async def image_generate( prompt: str, - size: Optional[str] = None, - n: int = 1, - *, - __context__: dict, -) -> str: - """Generate image files from a text prompt. - :param prompt: Detailed description of the image to generate. - :param size: Optional image size, such as 1024x1024. - :param n: Number of images to generate, from 1 to 4. - """ - from cptr.utils.images import generate_images - - images = await generate_images( - prompt, - user_id=__context__.get("user_id"), - size=size, - n=n, - workspace=__context__.get("workspace"), - ) - return json.dumps( - { - "status": "success", - "images": [image.as_dict() for image in images], - }, - ensure_ascii=False, - ) - - -async def edit_image( - prompt: str, - image_ids: list[str], + image: Optional[str] = None, + images: Optional[list[str]] = None, size: Optional[str] = None, n: int = 1, background: Optional[str] = None, *, __context__: dict, ) -> str: - """Edit existing cptr image files from a text prompt. - :param prompt: Description of the requested image changes. - :param image_ids: List of cptr file IDs or /api/files/... URLs for the source images. + """Generate or edit image files from a prompt. + :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. :param size: Optional image size, such as 1024x1024. :param n: Number of images to create, from 1 to 4. :param background: Optional background setting supported by the image provider. """ - from cptr.utils.images import edit_images + image_refs: list[str] = [] + if image: + image_refs.append(image) + if images: + image_refs.extend(images) + + if image_refs: + from cptr.utils.images import edit_images + + results = await edit_images( + prompt, + image_refs, + user_id=__context__.get("user_id"), + size=size, + n=n, + background=background, + workspace=__context__.get("workspace"), + ) + kind = "edit" + else: + from cptr.utils.images import generate_images + + results = await generate_images( + prompt, + user_id=__context__.get("user_id"), + size=size, + n=n, + workspace=__context__.get("workspace"), + ) + kind = "generation" - images = await edit_images( - prompt, - image_ids, - user_id=__context__.get("user_id"), - size=size, - n=n, - background=background, - workspace=__context__.get("workspace"), - ) return json.dumps( { "status": "success", - "images": [image.as_dict() for image in images], + "kind": kind, + "images": [result.as_dict() for result in results], }, ensure_ascii=False, ) @@ -1529,8 +1522,7 @@ TOOLS: dict[str, dict] = { "update_automation": {"fn": update_automation, "auto": False}, "toggle_automation": {"fn": toggle_automation, "auto": False}, "delete_automation": {"fn": delete_automation, "auto": False}, - "generate_image": {"fn": generate_image, "auto": False}, - "edit_image": {"fn": edit_image, "auto": False}, + "image_generate": {"fn": image_generate, "auto": False}, } # Browser tools — conditionally included in schemas based on browser.enabled @@ -2128,13 +2120,16 @@ async def get_tool_list() -> list[dict]: "true", "1", ) - if (await Config.get("images.generation_enabled")) not in (True, "true", "1"): - tools.pop("generate_image", None) - if (await Config.get("images.edit_enabled")) not in (True, "true", "1"): - tools.pop("edit_image", None) + images_generation_enabled = (await Config.get("images.generation_enabled")) in ( + True, + "true", + "1", + ) + images_edit_enabled = (await Config.get("images.edit_enabled")) in (True, "true", "1") + if not (images_generation_enabled or images_edit_enabled): + tools.pop("image_generate", None) except Exception: - tools.pop("generate_image", None) - tools.pop("edit_image", None) + tools.pop("image_generate", None) pass schemas = [_fn_to_schema(name, t["fn"]) for name, t in tools.items()]