@cloudbase/agent-adapter-langgraph 0.0.12 → 0.0.13

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
@@ -58,6 +58,7 @@ function convertJsonSchemaToZodSchema(jsonSchema, required) {
58
58
  }
59
59
 
60
60
  // src/agent.ts
61
+ import { noopLogger } from "@cloudbase/agent-shared";
61
62
  var ClientPropertiesAnnotation = Annotation.Root({
62
63
  tools: Annotation
63
64
  });
@@ -69,6 +70,8 @@ var LanggraphAgent = class extends AbstractAgent {
69
70
  constructor(agentConfig) {
70
71
  super(agentConfig);
71
72
  this.compiledWorkflow = agentConfig.compiledWorkflow;
73
+ const baseLogger = agentConfig.logger ?? noopLogger;
74
+ this.logger = baseLogger.child?.({ component: "langgraph-agent" }) ?? baseLogger;
72
75
  }
73
76
  run(input) {
74
77
  return new Observable((subscriber) => {
@@ -77,12 +80,29 @@ var LanggraphAgent = class extends AbstractAgent {
77
80
  }
78
81
  async _run(subscriber, input) {
79
82
  const { messages, runId, threadId } = input;
80
- subscriber.next({
83
+ const logger = this.logger.child?.({ runId, threadId }) ?? this.logger;
84
+ logger.info?.("Run started");
85
+ const runStartedEvent = {
81
86
  type: EventType.RUN_STARTED,
82
87
  threadId,
83
88
  runId
84
- });
85
- const streamEventInput = input.forwardedProps?.resume ? new Command({
89
+ };
90
+ logger.trace?.({ aguiEvent: runStartedEvent }, "Emitting AGUI event");
91
+ subscriber.next(runStartedEvent);
92
+ const isResume = !!input.forwardedProps?.resume;
93
+ const lastUserMessage = messages.filter((m) => m.role === "user").pop();
94
+ logger.debug?.(
95
+ {
96
+ isResume,
97
+ messageCount: messages.length,
98
+ toolCount: input.tools?.length ?? 0,
99
+ tools: input.tools?.map((t) => t.name),
100
+ lastUserMessage: typeof lastUserMessage?.content === "string" ? lastUserMessage.content.slice(0, 200) : void 0
101
+ },
102
+ "Preparing stream input"
103
+ );
104
+ logger.trace?.({ messages, tools: input.tools }, "Full input messages");
105
+ const streamEventInput = isResume ? new Command({
86
106
  resume: JSON.stringify(input.forwardedProps?.resume?.payload)
87
107
  }) : {
88
108
  messages: aguiMessagesToLangChain(messages),
@@ -102,6 +122,7 @@ var LanggraphAgent = class extends AbstractAgent {
102
122
  thread_id: threadId
103
123
  }
104
124
  });
125
+ logger.debug?.("Stream created, starting event processing");
105
126
  const chatModelRuns = [];
106
127
  const handledToolCallIds = /* @__PURE__ */ new Set();
107
128
  for (const msg of messages) {
@@ -109,14 +130,32 @@ var LanggraphAgent = class extends AbstractAgent {
109
130
  handledToolCallIds.add(msg.toolCallId);
110
131
  }
111
132
  }
133
+ if (handledToolCallIds.size > 0) {
134
+ logger.debug?.(
135
+ { count: handledToolCallIds.size },
136
+ "Pre-populated handled tool call IDs from input messages"
137
+ );
138
+ }
112
139
  let interrupt;
113
140
  let currentToolCall = null;
141
+ let eventCount = 0;
142
+ let toolCallCount = 0;
143
+ let textChunkCount = 0;
114
144
  try {
115
145
  for await (const event of stream) {
146
+ eventCount++;
147
+ logger.trace?.(
148
+ { eventType: event.event, eventCount, langGraphEvent: event },
149
+ "Processing stream event"
150
+ );
116
151
  if (event.event.startsWith("ChannelWrite<")) {
117
152
  continue;
118
153
  }
119
154
  if (event.event === "on_chat_model_start") {
155
+ logger.debug?.(
156
+ { chatModelRunId: event.run_id },
157
+ "Chat model started"
158
+ );
120
159
  chatModelRuns.push({ runId: event.run_id });
121
160
  continue;
122
161
  }
@@ -125,6 +164,10 @@ var LanggraphAgent = class extends AbstractAgent {
125
164
  (run) => run.runId === event.run_id
126
165
  );
127
166
  if (!chatModelRun) {
167
+ logger.warn?.(
168
+ { chatModelRunId: event.run_id },
169
+ "Received message from unknown chat model run"
170
+ );
128
171
  subscriber.next({
129
172
  type: EventType.RUN_ERROR,
130
173
  message: `Received a message from an unknown chat model run. Run Id: ${event.run_id}`
@@ -134,12 +177,26 @@ var LanggraphAgent = class extends AbstractAgent {
134
177
  const chunkId = event.data.chunk.id;
135
178
  if (!chatModelRun.messageId) {
136
179
  chatModelRun.messageId = chunkId;
137
- subscriber.next({
180
+ const textStartEvent = {
138
181
  messageId: chunkId,
139
182
  type: EventType.TEXT_MESSAGE_START,
140
183
  role: "assistant"
141
- });
184
+ };
185
+ logger.debug?.({ messageId: chunkId }, "Text message started");
186
+ logger.trace?.(
187
+ { aguiEvent: textStartEvent },
188
+ "Emitting AGUI event"
189
+ );
190
+ subscriber.next(textStartEvent);
142
191
  } else if (chatModelRun.messageId !== chunkId) {
192
+ logger.warn?.(
193
+ {
194
+ expectedMessageId: chatModelRun.messageId,
195
+ receivedMessageId: chunkId,
196
+ chatModelRunId: event.run_id
197
+ },
198
+ "Received message with unexpected ID"
199
+ );
143
200
  subscriber.next({
144
201
  type: EventType.RUN_ERROR,
145
202
  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}`
@@ -154,29 +211,68 @@ var LanggraphAgent = class extends AbstractAgent {
154
211
  })).forEach((toolCall) => {
155
212
  if (currentToolCall) {
156
213
  if (toolCall.id && currentToolCall.id !== toolCall.id) {
157
- subscriber.next({
214
+ const toolEndEvent = {
158
215
  toolCallId: currentToolCall.id,
159
216
  type: EventType.TOOL_CALL_END
160
- });
217
+ };
218
+ logger.debug?.(
219
+ {
220
+ toolCallId: currentToolCall.id,
221
+ toolCallName: currentToolCall.name
222
+ },
223
+ "Tool call ended"
224
+ );
225
+ logger.trace?.(
226
+ { aguiEvent: toolEndEvent },
227
+ "Emitting AGUI event"
228
+ );
229
+ subscriber.next(toolEndEvent);
161
230
  if (toolCall.name && toolCall.id) {
162
231
  currentToolCall = toolCall;
163
- subscriber.next({
232
+ toolCallCount++;
233
+ const toolStartEvent = {
164
234
  toolCallId: currentToolCall.id,
165
235
  toolCallName: currentToolCall.name,
166
236
  parentMessageId,
167
237
  type: EventType.TOOL_CALL_START
168
- });
238
+ };
239
+ logger.debug?.(
240
+ {
241
+ toolCallId: toolCall.id,
242
+ toolCallName: toolCall.name
243
+ },
244
+ "Tool call started"
245
+ );
246
+ logger.trace?.(
247
+ { aguiEvent: toolStartEvent },
248
+ "Emitting AGUI event"
249
+ );
250
+ subscriber.next(toolStartEvent);
169
251
  if (currentToolCall.args) {
170
- subscriber.next({
252
+ const toolArgsEvent = {
171
253
  toolCallId: currentToolCall.id,
172
254
  delta: currentToolCall.args,
173
255
  type: EventType.TOOL_CALL_ARGS
174
- });
256
+ };
257
+ logger.trace?.(
258
+ { aguiEvent: toolArgsEvent },
259
+ "Emitting AGUI event"
260
+ );
261
+ subscriber.next(toolArgsEvent);
175
262
  if (isValidJson(currentToolCall.args)) {
176
- subscriber.next({
263
+ const toolEndEvent2 = {
177
264
  toolCallId: currentToolCall.id,
178
265
  type: EventType.TOOL_CALL_END
179
- });
266
+ };
267
+ logger.debug?.(
268
+ { toolCallId: currentToolCall.id },
269
+ "Tool call ended (args complete)"
270
+ );
271
+ logger.trace?.(
272
+ { aguiEvent: toolEndEvent2 },
273
+ "Emitting AGUI event"
274
+ );
275
+ subscriber.next(toolEndEvent2);
180
276
  currentToolCall = null;
181
277
  }
182
278
  }
@@ -184,16 +280,30 @@ var LanggraphAgent = class extends AbstractAgent {
184
280
  } else {
185
281
  if (toolCall.args) {
186
282
  currentToolCall.args += toolCall.args;
187
- subscriber.next({
283
+ const toolArgsEvent = {
188
284
  toolCallId: currentToolCall.id,
189
285
  delta: toolCall.args,
190
286
  type: EventType.TOOL_CALL_ARGS
191
- });
287
+ };
288
+ logger.trace?.(
289
+ { aguiEvent: toolArgsEvent },
290
+ "Emitting AGUI event"
291
+ );
292
+ subscriber.next(toolArgsEvent);
192
293
  if (isValidJson(currentToolCall.args)) {
193
- subscriber.next({
294
+ const toolEndEvent = {
194
295
  toolCallId: currentToolCall.id,
195
296
  type: EventType.TOOL_CALL_END
196
- });
297
+ };
298
+ logger.debug?.(
299
+ { toolCallId: currentToolCall.id },
300
+ "Tool call ended (args complete)"
301
+ );
302
+ logger.trace?.(
303
+ { aguiEvent: toolEndEvent },
304
+ "Emitting AGUI event"
305
+ );
306
+ subscriber.next(toolEndEvent);
197
307
  currentToolCall = null;
198
308
  }
199
309
  }
@@ -201,23 +311,47 @@ var LanggraphAgent = class extends AbstractAgent {
201
311
  } else {
202
312
  if (toolCall.name && toolCall.id) {
203
313
  currentToolCall = toolCall;
204
- subscriber.next({
314
+ toolCallCount++;
315
+ const toolStartEvent = {
205
316
  toolCallId: toolCall.id,
206
317
  toolCallName: toolCall.name,
207
318
  parentMessageId,
208
319
  type: EventType.TOOL_CALL_START
209
- });
320
+ };
321
+ logger.debug?.(
322
+ { toolCallId: toolCall.id, toolCallName: toolCall.name },
323
+ "Tool call started"
324
+ );
325
+ logger.trace?.(
326
+ { aguiEvent: toolStartEvent },
327
+ "Emitting AGUI event"
328
+ );
329
+ subscriber.next(toolStartEvent);
210
330
  if (toolCall.args) {
211
- subscriber.next({
331
+ const toolArgsEvent = {
212
332
  toolCallId: toolCall.id,
213
333
  delta: toolCall.args,
214
334
  type: EventType.TOOL_CALL_ARGS
215
- });
335
+ };
336
+ logger.trace?.(
337
+ { aguiEvent: toolArgsEvent },
338
+ "Emitting AGUI event"
339
+ );
340
+ subscriber.next(toolArgsEvent);
216
341
  if (isValidJson(toolCall.args)) {
217
- subscriber.next({
342
+ const toolEndEvent = {
218
343
  toolCallId: toolCall.id,
219
344
  type: EventType.TOOL_CALL_END
220
- });
345
+ };
346
+ logger.debug?.(
347
+ { toolCallId: toolCall.id },
348
+ "Tool call ended (args complete)"
349
+ );
350
+ logger.trace?.(
351
+ { aguiEvent: toolEndEvent },
352
+ "Emitting AGUI event"
353
+ );
354
+ subscriber.next(toolEndEvent);
221
355
  currentToolCall = null;
222
356
  }
223
357
  }
@@ -227,11 +361,17 @@ var LanggraphAgent = class extends AbstractAgent {
227
361
  }
228
362
  const delta = event.data.chunk.content;
229
363
  if (typeof delta === "string" && delta) {
230
- subscriber.next({
364
+ textChunkCount++;
365
+ const textContentEvent = {
231
366
  messageId: chatModelRun.messageId,
232
367
  type: EventType.TEXT_MESSAGE_CONTENT,
233
368
  delta
234
- });
369
+ };
370
+ logger.trace?.(
371
+ { aguiEvent: textContentEvent },
372
+ "Emitting AGUI event"
373
+ );
374
+ subscriber.next(textContentEvent);
235
375
  }
236
376
  continue;
237
377
  }
@@ -240,16 +380,26 @@ var LanggraphAgent = class extends AbstractAgent {
240
380
  (run) => run.runId === event.run_id
241
381
  );
242
382
  if (!chatModelRun) {
383
+ logger.warn?.(
384
+ { chatModelRunId: event.run_id },
385
+ "Received on_chat_model_end from unknown run"
386
+ );
243
387
  subscriber.next({
244
388
  type: EventType.RUN_ERROR,
245
389
  message: `Received a on_chat_model_end event from an unknown chat model run. Run Id: ${event.run_id}`
246
390
  });
247
391
  continue;
248
392
  }
249
- subscriber.next({
393
+ const textEndEvent = {
250
394
  type: EventType.TEXT_MESSAGE_END,
251
395
  messageId: chatModelRun.messageId
252
- });
396
+ };
397
+ logger.debug?.(
398
+ { messageId: chatModelRun.messageId },
399
+ "Text message ended"
400
+ );
401
+ logger.trace?.({ aguiEvent: textEndEvent }, "Emitting AGUI event");
402
+ subscriber.next(textEndEvent);
253
403
  continue;
254
404
  }
255
405
  if (event.event === "on_tool_end") {
@@ -262,19 +412,40 @@ var LanggraphAgent = class extends AbstractAgent {
262
412
  toolMessage.lc_kwargs.id = toolMessage.id;
263
413
  }
264
414
  }
265
- subscriber.next({
415
+ const toolResultEvent = {
266
416
  toolCallId: toolMessage.tool_call_id,
267
417
  type: EventType.TOOL_CALL_RESULT,
268
418
  content: typeof toolMessage.content === "string" ? toolMessage.content : JSON.stringify(toolMessage.content),
269
419
  messageId: toolMessage.id
270
- });
420
+ };
421
+ logger.debug?.(
422
+ {
423
+ toolCallId: toolMessage.tool_call_id,
424
+ messageId: toolMessage.id
425
+ },
426
+ "Tool call result received"
427
+ );
428
+ logger.trace?.(
429
+ { aguiEvent: toolResultEvent },
430
+ "Emitting AGUI event"
431
+ );
432
+ subscriber.next(toolResultEvent);
271
433
  handledToolCallIds.add(toolMessage.tool_call_id);
434
+ } else {
435
+ logger.trace?.(
436
+ { toolCallId: toolMessage.tool_call_id },
437
+ "Skipping duplicate tool call result"
438
+ );
272
439
  }
273
440
  }
274
441
  continue;
275
442
  }
276
443
  if (event.event === "on_chain_stream" && event.data.chunk?.__interrupt__ && Array.isArray(event.data.chunk.__interrupt__) && event.data.chunk.__interrupt__.length > 0) {
277
444
  const rawInterrupt = event.data.chunk.__interrupt__[0];
445
+ logger.debug?.(
446
+ { interruptId: rawInterrupt.id },
447
+ "Interrupt received"
448
+ );
278
449
  interrupt = {
279
450
  id: rawInterrupt.id,
280
451
  // TODO: replace with actual reason
@@ -283,23 +454,36 @@ var LanggraphAgent = class extends AbstractAgent {
283
454
  };
284
455
  }
285
456
  }
457
+ const stats = { eventCount, toolCallCount, textChunkCount };
286
458
  if (interrupt) {
287
- subscriber.next({
459
+ const runFinishedEvent = {
288
460
  type: EventType.RUN_FINISHED,
289
461
  threadId,
290
462
  runId,
291
463
  outcome: "interrupt",
292
464
  interrupt
293
- });
465
+ };
466
+ logger.info?.(
467
+ { outcome: "interrupt", interruptId: interrupt.id, ...stats },
468
+ "Run finished with interrupt"
469
+ );
470
+ logger.trace?.({ aguiEvent: runFinishedEvent }, "Emitting AGUI event");
471
+ subscriber.next(runFinishedEvent);
294
472
  } else {
295
- subscriber.next({
473
+ const runFinishedEvent = {
296
474
  type: EventType.RUN_FINISHED,
297
475
  threadId,
298
476
  runId
299
- });
477
+ };
478
+ logger.info?.({ outcome: "complete", ...stats }, "Run finished");
479
+ logger.trace?.({ aguiEvent: runFinishedEvent }, "Emitting AGUI event");
480
+ subscriber.next(runFinishedEvent);
300
481
  }
301
482
  } catch (error) {
302
- console.error("[LanggraphAgent] Error during stream processing:", error);
483
+ logger.error?.(
484
+ { err: error, eventCount, toolCallCount, textChunkCount },
485
+ "Error during stream processing"
486
+ );
303
487
  subscriber.next({
304
488
  type: EventType.RUN_ERROR,
305
489
  message: error instanceof Error ? error.message : String(error)
@@ -309,10 +493,12 @@ var LanggraphAgent = class extends AbstractAgent {
309
493
  }
310
494
  clone() {
311
495
  const workflow = this.compiledWorkflow;
496
+ const logger = this.logger;
312
497
  this.compiledWorkflow = void 0;
313
498
  const cloned = super.clone();
314
499
  this.compiledWorkflow = workflow;
315
500
  cloned.compiledWorkflow = workflow;
501
+ cloned.logger = logger;
316
502
  return cloned;
317
503
  }
318
504
  };
@@ -364,8 +550,7 @@ function aguiMessagesToLangChain(messages) {
364
550
  id: message.id
365
551
  };
366
552
  default:
367
- console.error(`Message role ${message.role} is not implemented`);
368
- throw new Error("message role is not supported.");
553
+ throw new Error(`Message role ${message.role} is not supported.`);
369
554
  }
370
555
  });
371
556
  }
@@ -1060,11 +1245,16 @@ var TDAIStore = class extends BaseStore {
1060
1245
  }
1061
1246
  }
1062
1247
  };
1248
+
1249
+ // src/index.ts
1250
+ import { noopLogger as noopLogger2, createConsoleLogger } from "@cloudbase/agent-shared";
1063
1251
  export {
1064
1252
  ClientPropertiesAnnotation,
1065
1253
  ClientStateAnnotation,
1066
1254
  LanggraphAgent,
1067
1255
  TDAISaver,
1068
- TDAIStore
1256
+ TDAIStore,
1257
+ createConsoleLogger,
1258
+ noopLogger2 as noopLogger
1069
1259
  };
1070
1260
  //# sourceMappingURL=index.mjs.map