diff --git a/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte b/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte index 51443d9..3727ba3 100644 --- a/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte +++ b/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte @@ -1,10 +1,9 @@ + +
+ + + {#if expanded} +
+
+ {#each entries as item, entryIdx} + {#if item.type === 'reasoning'} + + {:else} + + {/if} + {/each} +
+
+ {/if} + + {#if !expanded && hasPendingApproval && chatId} +
+ {#each pendingCalls as item} +
+ + {toolLabel(item.name, item.arguments || {})} + + + + + +
+ {/each} +
+ {/if} +
diff --git a/cptr/frontend/src/lib/components/chat/ReasoningCollapsible.svelte b/cptr/frontend/src/lib/components/chat/ReasoningCollapsible.svelte new file mode 100644 index 0000000..4c1c4c5 --- /dev/null +++ b/cptr/frontend/src/lib/components/chat/ReasoningCollapsible.svelte @@ -0,0 +1,86 @@ + + +
+ + + {#if expanded && thoughtText} +
+
+
+ {thoughtText} +
+
+
+ {/if} +
diff --git a/cptr/frontend/src/lib/components/chat/ToolCallCollapsible.svelte b/cptr/frontend/src/lib/components/chat/ToolCallCollapsible.svelte new file mode 100644 index 0000000..629d7b3 --- /dev/null +++ b/cptr/frontend/src/lib/components/chat/ToolCallCollapsible.svelte @@ -0,0 +1,267 @@ + + +
+
+
+ {#if isExecuting} +
+ +
+ {:else if isDone} +
+ + + +
+ {:else if isRejected} +
+ + + +
+ {:else} +
+ + + +
+ {/if} + +
+ {toolLabel(toolName, args)} +
+ + {#if isPending && chatId} + + + + + {:else} +
+ + + +
+ {/if} +
+
+ + {#if expanded} +
+
+ {#if Object.keys(args).length > 0} +
+
+ {$t('chat.toolInput')} +
+ {#if toolName === 'edit_file' && args.target} +
+
+ -{args.target + .length > 500 + ? args.target.slice(0, 500) + '...' + : args.target} +
+
+ +{( + args.replacement || '' + ).length > 500 + ? args.replacement.slice(0, 500) + '...' + : args.replacement || ''} +
+
+ {#if args.start_line} +
+ {$t('chat.toolLines', { + start: args.start_line, + end: args.end_line || 'end' + })} +
+ {/if} + {:else if toolName === 'run_command'} + {args.command} + {:else} +
+ {#each Object.entries(args) as [key, value]} +
+ {key} + + {typeof value === 'object' ? JSON.stringify(value) : value} + +
+ {/each} +
+ {/if} +
+ {/if} + + {#if pairedOutput?.output} +
+
+ {$t('chat.toolOutput')} +
+
+
{pairedOutput
+									.output.length > 10000
+									? pairedOutput.output.slice(0, 10000)
+									: pairedOutput.output}
+ {#if pairedOutput.output.length > 10000} +
+ {$t('chat.totalChars', { + count: pairedOutput.output.length.toLocaleString() + })} +
+ {/if} +
+
+ {/if} +
+
+ {/if} +
diff --git a/cptr/utils/ai.py b/cptr/utils/ai.py index 898a57e..62b7b10 100644 --- a/cptr/utils/ai.py +++ b/cptr/utils/ai.py @@ -477,6 +477,17 @@ async def stream_openai_completions( tool_calls: dict[int, dict] = {} reasoning_buffer = "" # Accumulate reasoning_content deltas (Kimi, DeepSeek) reasoning_item: dict | None = None + + def complete_reasoning_item() -> dict | None: + nonlocal reasoning_buffer, reasoning_item + if reasoning_item is None: + return None + reasoning_item["status"] = "completed" + item = copy.deepcopy(reasoning_item) + reasoning_item = None + reasoning_buffer = "" + return item + async for line in resp.aiter_lines(): if not line.startswith("data: ") or line == "data: [DONE]": continue @@ -484,10 +495,6 @@ async def stream_openai_completions( choices = chunk.get("choices", []) delta = choices[0].get("delta", {}) if choices else {} - if delta.get("content"): - emitted = True - yield {"type": "text_delta", "content": delta["content"]} - # Capture reasoning_content from providers like Kimi / DeepSeek if delta.get("reasoning_content"): reasoning_buffer += delta["reasoning_content"] @@ -497,6 +504,20 @@ async def stream_openai_completions( emitted = True yield {"type": "output", "item": copy.deepcopy(reasoning_item)} + if delta.get("content"): + item = complete_reasoning_item() + if item is not None: + emitted = True + yield {"type": "output", "item": item} + emitted = True + yield {"type": "text_delta", "content": delta["content"]} + + if delta.get("tool_calls"): + item = complete_reasoning_item() + if item is not None: + emitted = True + yield {"type": "output", "item": item} + for tc in delta.get("tool_calls", []): idx = tc["index"] if idx not in tool_calls: @@ -509,12 +530,10 @@ async def stream_openai_completions( if choices and choices[0].get("finish_reason") == "tool_calls": # Emit accumulated reasoning before tool calls - if reasoning_item is not None: - reasoning_item["status"] = "completed" + item = complete_reasoning_item() + if item is not None: emitted = True - yield {"type": "output", "item": copy.deepcopy(reasoning_item)} - reasoning_item = None - reasoning_buffer = "" + yield {"type": "output", "item": item} for tc in tool_calls.values(): emitted = True yield { @@ -527,12 +546,10 @@ async def stream_openai_completions( if chunk.get("usage"): # Emit any remaining reasoning BEFORE usage # (usage triggers immediate save+return in chat_task) - if reasoning_item is not None: - reasoning_item["status"] = "completed" + item = complete_reasoning_item() + if item is not None: emitted = True - yield {"type": "output", "item": copy.deepcopy(reasoning_item)} - reasoning_item = None - reasoning_buffer = "" + yield {"type": "output", "item": item} raw = chunk["usage"] emitted = True yield { @@ -542,10 +559,10 @@ async def stream_openai_completions( } # Emit any remaining reasoning if no usage event was received - if reasoning_item is not None: - reasoning_item["status"] = "completed" + item = complete_reasoning_item() + if item is not None: emitted = True - yield {"type": "output", "item": copy.deepcopy(reasoning_item)} + yield {"type": "output", "item": item} emitted = True yield {"type": "done"} return diff --git a/cptr/utils/chat_task.py b/cptr/utils/chat_task.py index 4f48e62..24265f3 100644 --- a/cptr/utils/chat_task.py +++ b/cptr/utils/chat_task.py @@ -1108,6 +1108,9 @@ async def run_chat_task( nonlocal text_buffer if not text_buffer: return None + if not text_buffer.strip(): + text_buffer = "" + return None logger.info( "[task %s] flush_text: %d chars into message item", message_id[:8], len(text_buffer) )