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