|
| 1 | +import { |
| 2 | + isAIMessageChunk, |
| 3 | + isToolMessage, |
| 4 | + isToolMessageChunk, |
| 5 | +} from "@langchain/core/messages"; |
| 6 | + |
| 7 | +function extractText(contentLike) { |
| 8 | + if (!contentLike) return ""; |
| 9 | + if (typeof contentLike === "string") return contentLike; |
| 10 | + if (Array.isArray(contentLike)) { |
| 11 | + return contentLike |
| 12 | + .map((item) => { |
| 13 | + if (!item) return ""; |
| 14 | + if (typeof item === "string") return item; |
| 15 | + if (typeof item === "object" && "text" in item) return item.text ?? ""; |
| 16 | + if (typeof item === "object" && "content" in item) |
| 17 | + return extractText(item.content); |
| 18 | + return ""; |
| 19 | + }) |
| 20 | + .join(""); |
| 21 | + } |
| 22 | + if (typeof contentLike === "object" && "text" in contentLike) { |
| 23 | + return contentLike.text ?? ""; |
| 24 | + } |
| 25 | + return ""; |
| 26 | +} |
| 27 | + |
| 28 | +function ensureToolState(toolStates, toolCallId, defaults = {}) { |
| 29 | + if (!toolStates.has(toolCallId)) { |
| 30 | + toolStates.set(toolCallId, { |
| 31 | + id: toolCallId, |
| 32 | + name: defaults.name || "Tool", |
| 33 | + argsBuffer: "", |
| 34 | + argsObject: undefined, |
| 35 | + output: "", |
| 36 | + status: "running", |
| 37 | + isNew: true, |
| 38 | + stage: "start", |
| 39 | + }); |
| 40 | + } |
| 41 | + const state = toolStates.get(toolCallId); |
| 42 | + Object.assign(state, defaults); |
| 43 | + return state; |
| 44 | +} |
| 45 | + |
| 46 | +export function createSessionManager(agent) { |
| 47 | + return { |
| 48 | + async runTurn({ |
| 49 | + conversationId, |
| 50 | + userMessage, |
| 51 | + signal, |
| 52 | + assistantMessageId, |
| 53 | + onStart, |
| 54 | + onToken, |
| 55 | + onToolEvent, |
| 56 | + }) { |
| 57 | + const toolStates = new Map(); |
| 58 | + let accumulatedText = ""; |
| 59 | + |
| 60 | + const emitToolState = (state) => { |
| 61 | + if (!onToolEvent) return; |
| 62 | + state.parentMessageId = assistantMessageId; |
| 63 | + onToolEvent({ |
| 64 | + id: state.id, |
| 65 | + name: state.name, |
| 66 | + argsText: state.argsBuffer, |
| 67 | + argsObject: state.argsObject, |
| 68 | + output: state.output, |
| 69 | + status: state.status, |
| 70 | + stage: state.stage, |
| 71 | + isNew: state.isNew, |
| 72 | + parentMessageId: assistantMessageId, |
| 73 | + }); |
| 74 | + state.isNew = false; |
| 75 | + }; |
| 76 | + |
| 77 | + try { |
| 78 | + const stream = await agent.stream( |
| 79 | + { messages: [userMessage] }, |
| 80 | + { |
| 81 | + streamMode: "messages", |
| 82 | + signal, |
| 83 | + configurable: { |
| 84 | + thread_id: conversationId, |
| 85 | + }, |
| 86 | + }, |
| 87 | + ); |
| 88 | + |
| 89 | + onStart?.(); |
| 90 | + |
| 91 | + for await (const event of stream) { |
| 92 | + const payload = Array.isArray(event) ? event[0] : event; |
| 93 | + if (!payload) continue; |
| 94 | + |
| 95 | + if (isToolMessageChunk(payload) || isToolMessage(payload)) { |
| 96 | + const toolCallId = |
| 97 | + payload.tool_call_id || payload.id || `tool_${toolStates.size}`; |
| 98 | + const state = ensureToolState(toolStates, toolCallId); |
| 99 | + const chunkText = extractText(payload.content ?? payload); |
| 100 | + if (chunkText) { |
| 101 | + state.output = `${state.output || ""}${chunkText}`; |
| 102 | + } |
| 103 | + if (payload.status) { |
| 104 | + state.status = payload.status === "error" ? "error" : "success"; |
| 105 | + } |
| 106 | + state.stage = "output"; |
| 107 | + emitToolState(state); |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + if (isAIMessageChunk(payload)) { |
| 112 | + const { tool_call_chunks: toolCallChunks, tool_calls: toolCalls } = |
| 113 | + payload; |
| 114 | + |
| 115 | + if (Array.isArray(toolCallChunks) && toolCallChunks.length) { |
| 116 | + for (const chunk of toolCallChunks) { |
| 117 | + const toolCallId = |
| 118 | + chunk.id || |
| 119 | + chunk.tool_call_id || |
| 120 | + chunk.name || |
| 121 | + `tool_${toolStates.size}`; |
| 122 | + const state = ensureToolState(toolStates, toolCallId, { |
| 123 | + name: |
| 124 | + chunk.name || toolStates.get(toolCallId)?.name || "Tool", |
| 125 | + }); |
| 126 | + if (chunk.args) { |
| 127 | + state.argsBuffer = `${state.argsBuffer || ""}${chunk.args}`; |
| 128 | + } |
| 129 | + state.stage = "args-delta"; |
| 130 | + emitToolState(state); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if (Array.isArray(toolCalls) && toolCalls.length) { |
| 135 | + for (const call of toolCalls) { |
| 136 | + const toolCallId = |
| 137 | + call.id || call.name || `tool_${toolStates.size}`; |
| 138 | + const state = ensureToolState(toolStates, toolCallId, { |
| 139 | + name: call.name || toolStates.get(toolCallId)?.name || "Tool", |
| 140 | + }); |
| 141 | + state.argsObject = call.args; |
| 142 | + try { |
| 143 | + state.argsBuffer = JSON.stringify(call.args, null, 2); |
| 144 | + } catch (error) { |
| 145 | + state.argsBuffer = String(call.args); |
| 146 | + } |
| 147 | + state.stage = "args-final"; |
| 148 | + emitToolState(state); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + const chunkText = extractText(payload?.content ?? payload); |
| 154 | + if (chunkText) { |
| 155 | + accumulatedText += chunkText; |
| 156 | + onToken?.({ |
| 157 | + fullText: accumulatedText, |
| 158 | + delta: chunkText, |
| 159 | + }); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + const toolRuns = Array.from(toolStates.values()).map((state) => { |
| 164 | + if (!state.status || state.status === "running") { |
| 165 | + state.status = "success"; |
| 166 | + } |
| 167 | + state.stage = "complete"; |
| 168 | + emitToolState(state); |
| 169 | + return { |
| 170 | + id: state.id, |
| 171 | + name: state.name, |
| 172 | + status: state.status, |
| 173 | + argsText: state.argsBuffer, |
| 174 | + argsObject: state.argsObject, |
| 175 | + output: state.output, |
| 176 | + timestamp: Date.now(), |
| 177 | + parentMessageId: assistantMessageId, |
| 178 | + }; |
| 179 | + }); |
| 180 | + |
| 181 | + return { |
| 182 | + content: accumulatedText, |
| 183 | + toolRuns, |
| 184 | + }; |
| 185 | + } catch (error) { |
| 186 | + if (toolStates.size) { |
| 187 | + error.toolRuns = Array.from(toolStates.values()).map((state) => ({ |
| 188 | + id: state.id, |
| 189 | + name: state.name, |
| 190 | + status: state.status, |
| 191 | + argsText: state.argsBuffer, |
| 192 | + argsObject: state.argsObject, |
| 193 | + output: state.output, |
| 194 | + timestamp: Date.now(), |
| 195 | + parentMessageId: assistantMessageId, |
| 196 | + })); |
| 197 | + } |
| 198 | + throw error; |
| 199 | + } |
| 200 | + }, |
| 201 | + }; |
| 202 | +} |
0 commit comments