Changesets · Branches · Tags · Compare
| Changeset | 1bedc7304a6417f4f5b3f74325b95f1cb80fa223a52d480a22989295a68e641f |
|---|---|
| Author | Nate <sestinj@gmail.com> · 2025-08-28 19:12 -07:00 |
| Committer | Nate <sestinj@gmail.com> · 2025-08-28 19:12 -07:00 |
| Parents | 10459a948591 |
1 file changed
modified extensions/cli/src/commands/serve.helpers.ts +37 −35
| @@ -1,9 +1,4 @@ | ||
| 1 | -import type { | |
| 2 | - ChatHistoryItem, | |
| 3 | - Session, | |
| 4 | - ToolCallState, | |
| 5 | - ToolStatus, | |
| 6 | -} from "core/index.js"; | |
| 1 | +import type { Session, ToolCallState, ToolStatus } from "core/index.js"; | |
| 7 | 2 | |
| 8 | 3 | import { streamChatResponse } from "../stream/streamChatResponse.js"; |
| 9 | 4 | import { StreamCallbacks } from "../stream/streamChatResponse.types.js"; |
| @@ -27,42 +22,41 @@ | ||
| 27 | 22 | // Set up periodic interruption checks |
| 28 | 23 | const interruptionChecker = setInterval(checkInterruption, 100); |
| 29 | 24 | |
| 30 | - let currentStreamingItem: ChatHistoryItem | null = null; | |
| 31 | - | |
| 32 | 25 | // Create callbacks to capture tool events |
| 33 | 26 | const callbacks: StreamCallbacks = { |
| 34 | - onContent: (_: string) => {}, | |
| 35 | - onContentComplete: (content: string) => { | |
| 36 | - currentStreamingItem = { | |
| 37 | - message: { role: "assistant", content }, | |
| 38 | - contextItems: [], | |
| 39 | - }; | |
| 40 | - state.session.history.push(currentStreamingItem); | |
| 27 | + onContent: (_: string) => { | |
| 28 | + // onContent is empty - doesn't update history during streaming | |
| 29 | + // This is just for real-time display purposes | |
| 30 | + }, | |
| 31 | + onContentComplete: (_: string) => { | |
| 32 | + // Note: streamChatResponse already adds messages to history via handleToolCalls | |
| 33 | + // so we don't need to add them here - this callback is just for notification | |
| 34 | + // that content streaming is complete | |
| 41 | 35 | }, |
| 42 | 36 | onToolStart: (toolName: string, toolArgs?: any) => { |
| 43 | - // If there was streaming content, finalize it first | |
| 44 | - if (currentStreamingItem && currentStreamingItem.message.content) { | |
| 45 | - currentStreamingItem = null; | |
| 46 | - } | |
| 47 | - | |
| 48 | 37 | // Always create a new assistant message for each tool call |
| 49 | 38 | const toolCallId = `tool_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`; |
| 39 | + const toolCall = { | |
| 40 | + id: toolCallId, | |
| 41 | + type: "function" as const, | |
| 42 | + function: { | |
| 43 | + name: toolName, | |
| 44 | + arguments: JSON.stringify(toolArgs || {}), | |
| 45 | + }, | |
| 46 | + }; | |
| 50 | 47 | const toolCallState: ToolCallState = { |
| 51 | 48 | toolCallId: toolCallId, |
| 52 | - toolCall: { | |
| 53 | - id: toolCallId, | |
| 54 | - type: "function", | |
| 55 | - function: { | |
| 56 | - name: toolName, | |
| 57 | - arguments: JSON.stringify(toolArgs || {}), | |
| 58 | - }, | |
| 59 | - }, | |
| 49 | + toolCall: toolCall, | |
| 60 | 50 | status: "calling", |
| 61 | 51 | parsedArgs: toolArgs, |
| 62 | 52 | }; |
| 63 | 53 | |
| 64 | 54 | state.session.history.push({ |
| 65 | - message: { role: "assistant", content: "" }, | |
| 55 | + message: { | |
| 56 | + role: "assistant", | |
| 57 | + content: "", | |
| 58 | + toolCalls: [toolCall], | |
| 59 | + }, | |
| 66 | 60 | contextItems: [], |
| 67 | 61 | toolCallStates: [toolCallState], |
| 68 | 62 | }); |
| @@ -85,7 +79,7 @@ | ||
| 85 | 79 | description: "Tool execution result", |
| 86 | 80 | }, |
| 87 | 81 | ]; |
| 88 | - return; | |
| 82 | + break; | |
| 89 | 83 | } |
| 90 | 84 | } |
| 91 | 85 | } |
| @@ -110,7 +104,7 @@ | ||
| 110 | 104 | description: "Tool execution error", |
| 111 | 105 | }, |
| 112 | 106 | ]; |
| 113 | - return; | |
| 107 | + break; | |
| 114 | 108 | } |
| 115 | 109 | } |
| 116 | 110 | } |
| @@ -125,6 +119,7 @@ | ||
| 125 | 119 | toolName: string, |
| 126 | 120 | toolArgs: any, |
| 127 | 121 | requestId: string, |
| 122 | + toolCallPreview?: any[], | |
| 128 | 123 | ) => { |
| 129 | 124 | // Set pending permission state |
| 130 | 125 | state.pendingPermission = { |
| @@ -132,6 +127,7 @@ | ||
| 132 | 127 | toolArgs, |
| 133 | 128 | requestId, |
| 134 | 129 | timestamp: Date.now(), |
| 130 | + toolCallPreview, | |
| 135 | 131 | }; |
| 136 | 132 | |
| 137 | 133 | // Add a system message indicating permission is needed |
| @@ -145,6 +141,15 @@ | ||
| 145 | 141 | |
| 146 | 142 | // Don't wait here - the streamChatResponse will handle waiting |
| 147 | 143 | }, |
| 144 | + onSystemMessage: (message: string) => { | |
| 145 | + state.session.history.push({ | |
| 146 | + message: { | |
| 147 | + role: "system", | |
| 148 | + content: message, | |
| 149 | + }, | |
| 150 | + contextItems: [], | |
| 151 | + }); | |
| 152 | + }, | |
| 148 | 153 | }; |
| 149 | 154 | |
| 150 | 155 | try { |
| @@ -158,10 +163,6 @@ | ||
| 158 | 163 | return response || ""; |
| 159 | 164 | } finally { |
| 160 | 165 | clearInterval(interruptionChecker); |
| 161 | - // Ensure any streaming message is finalized | |
| 162 | - if (currentStreamingItem !== null) { | |
| 163 | - currentStreamingItem = null; | |
| 164 | - } | |
| 165 | 166 | } |
| 166 | 167 | } |
| 167 | 168 | |
| @@ -170,6 +171,7 @@ | ||
| 170 | 171 | toolArgs: any; |
| 171 | 172 | requestId: string; |
| 172 | 173 | timestamp: number; |
| 174 | + toolCallPreview?: any[]; | |
| 173 | 175 | } |
| 174 | 176 | |
| 175 | 177 | export interface ServerState { |