From c1e8636dd19d49389c9cd35c0f8e646b0e2bc944 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sat, 6 Jun 2026 00:22:53 -0400 Subject: [PATCH] refac --- .../src/lib/components/DirectoryPicker.svelte | 6 ++--- .../components/chat/AssistantMessage.svelte | 24 ++++++++++++++++++- cptr/utils/chat_task.py | 21 ++++++++++++++++ cptr/utils/tools.py | 5 ++++ 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/cptr/frontend/src/lib/components/DirectoryPicker.svelte b/cptr/frontend/src/lib/components/DirectoryPicker.svelte index 3ac1960..44f349e 100644 --- a/cptr/frontend/src/lib/components/DirectoryPicker.svelte +++ b/cptr/frontend/src/lib/components/DirectoryPicker.svelte @@ -197,9 +197,9 @@ } function handlePathKeydown(e: KeyboardEvent) { - if (e.key === 'Enter') { e.preventDefault(); confirmPath(); } - else if (e.key === 'Escape') { e.preventDefault(); cancelEditing(); } - else if (e.key === 'Tab') { e.preventDefault(); tabComplete(); } + if (e.key === 'Enter') { e.preventDefault(); e.stopPropagation(); confirmPath(); } + else if (e.key === 'Escape') { e.preventDefault(); e.stopPropagation(); cancelEditing(); } + else if (e.key === 'Tab') { e.preventDefault(); e.stopPropagation(); tabComplete(); } } function handleKeydown(e: KeyboardEvent) { diff --git a/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte b/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte index 61a3d8f..15b0df9 100644 --- a/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte +++ b/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte @@ -154,7 +154,12 @@ item: any; } - type DisplayItem = ToolGroup | MessageItem; + interface ArtifactItem { + type: 'artifact_item'; + item: any; + } + + type DisplayItem = ToolGroup | MessageItem | ArtifactItem; const displayItems = $derived.by((): DisplayItem[] => { if (!output?.length) return []; @@ -186,6 +191,9 @@ } else if (item.type === 'message') { flushGroup(); items.push({ type: 'message_item', item }); + } else if (item.type === 'artifact') { + flushGroup(); + items.push({ type: 'artifact_item', item }); } // function_call_output items are handled via outputMap, skip standalone render } @@ -269,6 +277,20 @@ {#if displayItem.type === 'message_item'} c.text).join('') || ''} /> + {:else if displayItem.type === 'artifact_item'} + {@const artifact = displayItem.item} +
+
+ + + + {artifact.title || 'Artifact'} +
+
+ +
+
+ {:else if displayItem.type === 'tool_group'} {@const calls = displayItem.calls} {@const outputs = displayItem.outputs} diff --git a/cptr/utils/chat_task.py b/cptr/utils/chat_task.py index 0529c18..e61d439 100644 --- a/cptr/utils/chat_task.py +++ b/cptr/utils/chat_task.py @@ -353,6 +353,27 @@ async def run_chat_task( await emit(output=result_item) _sync_state() + # Artifact: write to artifacts dir and emit card + args = event["arguments"] + artifact_type = args.get("artifact_type", "") + if artifact_type: + from datetime import datetime, timezone + ts = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%S") + artifact_dir = Path(workspace) / ".cptr" / "artifacts" / chat_id + artifact_dir.mkdir(parents=True, exist_ok=True) + artifact_path = artifact_dir / f"{ts}_{artifact_type}.md" + artifact_path.write_text(args.get("content", "")) + artifact_item = { + "type": "artifact", + "artifact_type": artifact_type, + "title": Path(args.get("path", artifact_type)).stem.replace("_", " ").title(), + "content": args.get("content", ""), + "path": str(artifact_path.relative_to(Path(workspace))), + } + output_items.append(artifact_item) + await emit(output=artifact_item) + _sync_state() + # Append to messages for next iteration _append_tool_to_messages(messages, event, result, provider) restart = True diff --git a/cptr/utils/tools.py b/cptr/utils/tools.py index a0d3e6c..400d37d 100644 --- a/cptr/utils/tools.py +++ b/cptr/utils/tools.py @@ -263,13 +263,18 @@ async def create_file( path: str, content: str, overwrite: bool = False, + artifact_type: str = "", *, workspace: str, ) -> str: """Create a new file. Errors if file already exists unless overwrite is true. + For non-trivial tasks (multi-file changes, ambiguous requirements, architectural decisions), + create an implementation plan first with artifact_type='implementation_plan'. Write the plan + as markdown, then wait for the user to review before making changes. :param path: Path relative to workspace root. :param content: File contents to write. :param overwrite: Set to true to overwrite an existing file. + :param artifact_type: Set to 'implementation_plan' to present a plan for user review before coding. """ full = _resolve_path(path, workspace) if full.is_file() and not overwrite: