@cloudbase/agent-adapter-langgraph 0.0.11 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/index.js +150 -142
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +150 -142
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -111,64 +111,82 @@ var LanggraphAgent = class extends AbstractAgent {
|
|
|
111
111
|
}
|
|
112
112
|
let interrupt;
|
|
113
113
|
let currentToolCall = null;
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
if (event.event === "on_chat_model_start") {
|
|
119
|
-
chatModelRuns.push({ runId: event.run_id });
|
|
120
|
-
continue;
|
|
121
|
-
}
|
|
122
|
-
if (event.event === "on_chat_model_stream") {
|
|
123
|
-
let chatModelRun = chatModelRuns.find(
|
|
124
|
-
(run) => run.runId === event.run_id
|
|
125
|
-
);
|
|
126
|
-
if (!chatModelRun) {
|
|
127
|
-
subscriber.next({
|
|
128
|
-
type: EventType.RUN_ERROR,
|
|
129
|
-
message: `Received a message from an unknown chat model run. Run Id: ${event.run_id}`
|
|
130
|
-
});
|
|
114
|
+
try {
|
|
115
|
+
for await (const event of stream) {
|
|
116
|
+
if (event.event.startsWith("ChannelWrite<")) {
|
|
131
117
|
continue;
|
|
132
118
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
chatModelRun.messageId = chunkId;
|
|
136
|
-
subscriber.next({
|
|
137
|
-
messageId: chunkId,
|
|
138
|
-
type: EventType.TEXT_MESSAGE_START,
|
|
139
|
-
role: "assistant"
|
|
140
|
-
});
|
|
141
|
-
} else if (chatModelRun.messageId !== chunkId) {
|
|
142
|
-
subscriber.next({
|
|
143
|
-
type: EventType.RUN_ERROR,
|
|
144
|
-
message: `Received a message of unknown message id from current run. Run Id: ${event.run_id} Message Id from current run: ${chatModelRun.messageId} Message Id from received message: ${chunkId}`
|
|
145
|
-
});
|
|
119
|
+
if (event.event === "on_chat_model_start") {
|
|
120
|
+
chatModelRuns.push({ runId: event.run_id });
|
|
146
121
|
continue;
|
|
147
122
|
}
|
|
148
|
-
if (
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
123
|
+
if (event.event === "on_chat_model_stream") {
|
|
124
|
+
const chatModelRun = chatModelRuns.find(
|
|
125
|
+
(run) => run.runId === event.run_id
|
|
126
|
+
);
|
|
127
|
+
if (!chatModelRun) {
|
|
128
|
+
subscriber.next({
|
|
129
|
+
type: EventType.RUN_ERROR,
|
|
130
|
+
message: `Received a message from an unknown chat model run. Run Id: ${event.run_id}`
|
|
131
|
+
});
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
const chunkId = event.data.chunk.id;
|
|
135
|
+
if (!chatModelRun.messageId) {
|
|
136
|
+
chatModelRun.messageId = chunkId;
|
|
137
|
+
subscriber.next({
|
|
138
|
+
messageId: chunkId,
|
|
139
|
+
type: EventType.TEXT_MESSAGE_START,
|
|
140
|
+
role: "assistant"
|
|
141
|
+
});
|
|
142
|
+
} else if (chatModelRun.messageId !== chunkId) {
|
|
143
|
+
subscriber.next({
|
|
144
|
+
type: EventType.RUN_ERROR,
|
|
145
|
+
message: `Received a message of unknown message id from current run. Run Id: ${event.run_id} Message Id from current run: ${chatModelRun.messageId} Message Id from received message: ${chunkId}`
|
|
146
|
+
});
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (Array.isArray(event.data.chunk?.tool_call_chunks) && event.data.chunk?.tool_call_chunks?.length > 0) {
|
|
150
|
+
const parentMessageId = chatModelRun.messageId;
|
|
151
|
+
event.data.chunk.tool_call_chunks.map((x) => ({
|
|
152
|
+
...x,
|
|
153
|
+
args: typeof x.args === "string" ? x.args : x.args ? JSON.stringify(x.args) : ""
|
|
154
|
+
})).forEach((toolCall) => {
|
|
155
|
+
if (currentToolCall) {
|
|
156
|
+
if (toolCall.id && currentToolCall.id !== toolCall.id) {
|
|
162
157
|
subscriber.next({
|
|
163
158
|
toolCallId: currentToolCall.id,
|
|
164
|
-
|
|
165
|
-
parentMessageId,
|
|
166
|
-
type: EventType.TOOL_CALL_START
|
|
159
|
+
type: EventType.TOOL_CALL_END
|
|
167
160
|
});
|
|
168
|
-
if (
|
|
161
|
+
if (toolCall.name && toolCall.id) {
|
|
162
|
+
currentToolCall = toolCall;
|
|
169
163
|
subscriber.next({
|
|
170
164
|
toolCallId: currentToolCall.id,
|
|
171
|
-
|
|
165
|
+
toolCallName: currentToolCall.name,
|
|
166
|
+
parentMessageId,
|
|
167
|
+
type: EventType.TOOL_CALL_START
|
|
168
|
+
});
|
|
169
|
+
if (currentToolCall.args) {
|
|
170
|
+
subscriber.next({
|
|
171
|
+
toolCallId: currentToolCall.id,
|
|
172
|
+
delta: currentToolCall.args,
|
|
173
|
+
type: EventType.TOOL_CALL_ARGS
|
|
174
|
+
});
|
|
175
|
+
if (isValidJson(currentToolCall.args)) {
|
|
176
|
+
subscriber.next({
|
|
177
|
+
toolCallId: currentToolCall.id,
|
|
178
|
+
type: EventType.TOOL_CALL_END
|
|
179
|
+
});
|
|
180
|
+
currentToolCall = null;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
} else {
|
|
185
|
+
if (toolCall.args) {
|
|
186
|
+
currentToolCall.args += toolCall.args;
|
|
187
|
+
subscriber.next({
|
|
188
|
+
toolCallId: currentToolCall.id,
|
|
189
|
+
delta: toolCall.args,
|
|
172
190
|
type: EventType.TOOL_CALL_ARGS
|
|
173
191
|
});
|
|
174
192
|
if (isValidJson(currentToolCall.args)) {
|
|
@@ -181,120 +199,110 @@ var LanggraphAgent = class extends AbstractAgent {
|
|
|
181
199
|
}
|
|
182
200
|
}
|
|
183
201
|
} else {
|
|
184
|
-
if (toolCall.
|
|
185
|
-
currentToolCall
|
|
186
|
-
subscriber.next({
|
|
187
|
-
toolCallId: currentToolCall.id,
|
|
188
|
-
delta: toolCall.args,
|
|
189
|
-
type: EventType.TOOL_CALL_ARGS
|
|
190
|
-
});
|
|
191
|
-
if (isValidJson(currentToolCall.args)) {
|
|
192
|
-
subscriber.next({
|
|
193
|
-
toolCallId: currentToolCall.id,
|
|
194
|
-
type: EventType.TOOL_CALL_END
|
|
195
|
-
});
|
|
196
|
-
currentToolCall = null;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
} else {
|
|
201
|
-
if (toolCall.name && toolCall.id) {
|
|
202
|
-
currentToolCall = toolCall;
|
|
203
|
-
subscriber.next({
|
|
204
|
-
toolCallId: toolCall.id,
|
|
205
|
-
toolCallName: toolCall.name,
|
|
206
|
-
parentMessageId,
|
|
207
|
-
type: EventType.TOOL_CALL_START
|
|
208
|
-
});
|
|
209
|
-
if (toolCall.args) {
|
|
202
|
+
if (toolCall.name && toolCall.id) {
|
|
203
|
+
currentToolCall = toolCall;
|
|
210
204
|
subscriber.next({
|
|
211
205
|
toolCallId: toolCall.id,
|
|
212
|
-
|
|
213
|
-
|
|
206
|
+
toolCallName: toolCall.name,
|
|
207
|
+
parentMessageId,
|
|
208
|
+
type: EventType.TOOL_CALL_START
|
|
214
209
|
});
|
|
215
|
-
if (
|
|
210
|
+
if (toolCall.args) {
|
|
216
211
|
subscriber.next({
|
|
217
212
|
toolCallId: toolCall.id,
|
|
218
|
-
|
|
213
|
+
delta: toolCall.args,
|
|
214
|
+
type: EventType.TOOL_CALL_ARGS
|
|
219
215
|
});
|
|
220
|
-
|
|
216
|
+
if (isValidJson(toolCall.args)) {
|
|
217
|
+
subscriber.next({
|
|
218
|
+
toolCallId: toolCall.id,
|
|
219
|
+
type: EventType.TOOL_CALL_END
|
|
220
|
+
});
|
|
221
|
+
currentToolCall = null;
|
|
222
|
+
}
|
|
221
223
|
}
|
|
222
224
|
}
|
|
223
225
|
}
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
const delta = event.data.chunk.content;
|
|
229
|
+
if (typeof delta === "string" && delta) {
|
|
230
|
+
subscriber.next({
|
|
231
|
+
messageId: chatModelRun.messageId,
|
|
232
|
+
type: EventType.TEXT_MESSAGE_CONTENT,
|
|
233
|
+
delta
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
continue;
|
|
234
237
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
(
|
|
240
|
-
|
|
241
|
-
|
|
238
|
+
if (event.event === "on_chat_model_end") {
|
|
239
|
+
const chatModelRun = chatModelRuns.find(
|
|
240
|
+
(run) => run.runId === event.run_id
|
|
241
|
+
);
|
|
242
|
+
if (!chatModelRun) {
|
|
243
|
+
subscriber.next({
|
|
244
|
+
type: EventType.RUN_ERROR,
|
|
245
|
+
message: `Received a on_chat_model_end event from an unknown chat model run. Run Id: ${event.run_id}`
|
|
246
|
+
});
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
242
249
|
subscriber.next({
|
|
243
|
-
type: EventType.
|
|
244
|
-
|
|
250
|
+
type: EventType.TEXT_MESSAGE_END,
|
|
251
|
+
messageId: chatModelRun.messageId
|
|
245
252
|
});
|
|
246
253
|
continue;
|
|
247
254
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
if (!handledToolCallIds.has(toolMessage.tool_call_id)) {
|
|
258
|
-
if (!toolMessage.id) {
|
|
259
|
-
toolMessage.id = crypto.randomUUID();
|
|
260
|
-
if (toolMessage.lc_kwargs) {
|
|
261
|
-
toolMessage.lc_kwargs.id = toolMessage.id;
|
|
255
|
+
if (event.event === "on_tool_end") {
|
|
256
|
+
const toolMessage = event.data.output;
|
|
257
|
+
if (toolMessage && toolMessage.tool_call_id) {
|
|
258
|
+
if (!handledToolCallIds.has(toolMessage.tool_call_id)) {
|
|
259
|
+
if (!toolMessage.id) {
|
|
260
|
+
toolMessage.id = crypto.randomUUID();
|
|
261
|
+
if (toolMessage.lc_kwargs) {
|
|
262
|
+
toolMessage.lc_kwargs.id = toolMessage.id;
|
|
263
|
+
}
|
|
262
264
|
}
|
|
265
|
+
subscriber.next({
|
|
266
|
+
toolCallId: toolMessage.tool_call_id,
|
|
267
|
+
type: EventType.TOOL_CALL_RESULT,
|
|
268
|
+
content: typeof toolMessage.content === "string" ? toolMessage.content : JSON.stringify(toolMessage.content),
|
|
269
|
+
messageId: toolMessage.id
|
|
270
|
+
});
|
|
271
|
+
handledToolCallIds.add(toolMessage.tool_call_id);
|
|
263
272
|
}
|
|
264
|
-
subscriber.next({
|
|
265
|
-
toolCallId: toolMessage.tool_call_id,
|
|
266
|
-
type: EventType.TOOL_CALL_RESULT,
|
|
267
|
-
content: typeof toolMessage.content === "string" ? toolMessage.content : JSON.stringify(toolMessage.content),
|
|
268
|
-
messageId: toolMessage.id
|
|
269
|
-
});
|
|
270
|
-
handledToolCallIds.add(toolMessage.tool_call_id);
|
|
271
273
|
}
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
if (event.event === "on_chain_stream" && event.data.chunk?.__interrupt__ && Array.isArray(event.data.chunk.__interrupt__) && event.data.chunk.__interrupt__.length > 0) {
|
|
277
|
+
const rawInterrupt = event.data.chunk.__interrupt__[0];
|
|
278
|
+
interrupt = {
|
|
279
|
+
id: rawInterrupt.id,
|
|
280
|
+
// TODO: replace with actual reason
|
|
281
|
+
reason: "agent requested interrupt",
|
|
282
|
+
payload: rawInterrupt.value
|
|
283
|
+
};
|
|
272
284
|
}
|
|
273
|
-
continue;
|
|
274
285
|
}
|
|
275
|
-
if (
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
};
|
|
286
|
+
if (interrupt) {
|
|
287
|
+
subscriber.next({
|
|
288
|
+
type: EventType.RUN_FINISHED,
|
|
289
|
+
threadId,
|
|
290
|
+
runId,
|
|
291
|
+
outcome: "interrupt",
|
|
292
|
+
interrupt
|
|
293
|
+
});
|
|
294
|
+
} else {
|
|
295
|
+
subscriber.next({
|
|
296
|
+
type: EventType.RUN_FINISHED,
|
|
297
|
+
threadId,
|
|
298
|
+
runId
|
|
299
|
+
});
|
|
283
300
|
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
subscriber.next({
|
|
287
|
-
type: EventType.RUN_FINISHED,
|
|
288
|
-
threadId,
|
|
289
|
-
runId,
|
|
290
|
-
outcome: "interrupt",
|
|
291
|
-
interrupt
|
|
292
|
-
});
|
|
293
|
-
} else {
|
|
301
|
+
} catch (error) {
|
|
302
|
+
console.error("[LanggraphAgent] Error during stream processing:", error);
|
|
294
303
|
subscriber.next({
|
|
295
|
-
type: EventType.
|
|
296
|
-
|
|
297
|
-
runId
|
|
304
|
+
type: EventType.RUN_ERROR,
|
|
305
|
+
message: error instanceof Error ? error.message : String(error)
|
|
298
306
|
});
|
|
299
307
|
}
|
|
300
308
|
subscriber.complete();
|