@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/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
- 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
- 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
- const chunkId = event.data.chunk.id;
134
- if (!chatModelRun.messageId) {
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 (Array.isArray(event.data.chunk?.tool_call_chunks) && event.data.chunk?.tool_call_chunks?.length > 0) {
149
- const parentMessageId = chatModelRun.messageId;
150
- event.data.chunk.tool_call_chunks.map((x) => ({
151
- ...x,
152
- args: typeof x.args === "string" ? x.args : x.args ? JSON.stringify(x.args) : ""
153
- })).forEach((toolCall) => {
154
- if (currentToolCall) {
155
- if (toolCall.id && currentToolCall.id !== toolCall.id) {
156
- subscriber.next({
157
- toolCallId: currentToolCall.id,
158
- type: EventType.TOOL_CALL_END
159
- });
160
- if (toolCall.name && toolCall.id) {
161
- currentToolCall = toolCall;
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
- toolCallName: currentToolCall.name,
165
- parentMessageId,
166
- type: EventType.TOOL_CALL_START
159
+ type: EventType.TOOL_CALL_END
167
160
  });
168
- if (currentToolCall.args) {
161
+ if (toolCall.name && toolCall.id) {
162
+ currentToolCall = toolCall;
169
163
  subscriber.next({
170
164
  toolCallId: currentToolCall.id,
171
- 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,
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.args) {
185
- currentToolCall.args += toolCall.args;
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
- delta: toolCall.args,
213
- type: EventType.TOOL_CALL_ARGS
206
+ toolCallName: toolCall.name,
207
+ parentMessageId,
208
+ type: EventType.TOOL_CALL_START
214
209
  });
215
- if (isValidJson(toolCall.args)) {
210
+ if (toolCall.args) {
216
211
  subscriber.next({
217
212
  toolCallId: toolCall.id,
218
- type: EventType.TOOL_CALL_END
213
+ delta: toolCall.args,
214
+ type: EventType.TOOL_CALL_ARGS
219
215
  });
220
- 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
+ }
221
223
  }
222
224
  }
223
225
  }
224
- }
225
- });
226
- }
227
- const delta = event.data.chunk.content;
228
- if (typeof delta === "string" && delta) {
229
- subscriber.next({
230
- messageId: chatModelRun.messageId,
231
- type: EventType.TEXT_MESSAGE_CONTENT,
232
- delta
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
- continue;
236
- }
237
- if (event.event === "on_chat_model_end") {
238
- const chatModelRun = chatModelRuns.find(
239
- (run) => run.runId === event.run_id
240
- );
241
- if (!chatModelRun) {
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.RUN_ERROR,
244
- 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
245
252
  });
246
253
  continue;
247
254
  }
248
- subscriber.next({
249
- type: EventType.TEXT_MESSAGE_END,
250
- messageId: chatModelRun.messageId
251
- });
252
- continue;
253
- }
254
- if (event.event === "on_tool_end") {
255
- const toolMessage = event.data.output;
256
- if (toolMessage && toolMessage.tool_call_id) {
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 (event.event === "on_chain_stream" && event.data.chunk?.__interrupt__ && Array.isArray(event.data.chunk.__interrupt__) && event.data.chunk.__interrupt__.length > 0) {
276
- const rawInterrupt = event.data.chunk.__interrupt__[0];
277
- interrupt = {
278
- id: rawInterrupt.id,
279
- // TODO: replace with actual reason
280
- reason: "agent requested interrupt",
281
- payload: rawInterrupt.value
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
- if (interrupt) {
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.RUN_FINISHED,
296
- threadId,
297
- runId
304
+ type: EventType.RUN_ERROR,
305
+ message: error instanceof Error ? error.message : String(error)
298
306
  });
299
307
  }
300
308
  subscriber.complete();