@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.js CHANGED
@@ -34,7 +34,9 @@ __export(index_exports, {
34
34
  ClientStateAnnotation: () => ClientStateAnnotation,
35
35
  LanggraphAgent: () => LanggraphAgent,
36
36
  TDAISaver: () => TDAISaver,
37
- TDAIStore: () => TDAIStore
37
+ TDAIStore: () => TDAIStore,
38
+ createConsoleLogger: () => import_agent_shared2.createConsoleLogger,
39
+ noopLogger: () => import_agent_shared2.noopLogger
38
40
  });
39
41
  module.exports = __toCommonJS(index_exports);
40
42
 
@@ -91,6 +93,7 @@ function convertJsonSchemaToZodSchema(jsonSchema, required) {
91
93
  }
92
94
 
93
95
  // src/agent.ts
96
+ var import_agent_shared = require("@cloudbase/agent-shared");
94
97
  var ClientPropertiesAnnotation = import_langgraph.Annotation.Root({
95
98
  tools: import_langgraph.Annotation
96
99
  });
@@ -102,6 +105,8 @@ var LanggraphAgent = class extends import_client.AbstractAgent {
102
105
  constructor(agentConfig) {
103
106
  super(agentConfig);
104
107
  this.compiledWorkflow = agentConfig.compiledWorkflow;
108
+ const baseLogger = agentConfig.logger ?? import_agent_shared.noopLogger;
109
+ this.logger = baseLogger.child?.({ component: "langgraph-agent" }) ?? baseLogger;
105
110
  }
106
111
  run(input) {
107
112
  return new import_rxjs.Observable((subscriber) => {
@@ -110,12 +115,29 @@ var LanggraphAgent = class extends import_client.AbstractAgent {
110
115
  }
111
116
  async _run(subscriber, input) {
112
117
  const { messages, runId, threadId } = input;
113
- subscriber.next({
118
+ const logger = this.logger.child?.({ runId, threadId }) ?? this.logger;
119
+ logger.info?.("Run started");
120
+ const runStartedEvent = {
114
121
  type: import_client.EventType.RUN_STARTED,
115
122
  threadId,
116
123
  runId
117
- });
118
- const streamEventInput = input.forwardedProps?.resume ? new import_langgraph.Command({
124
+ };
125
+ logger.trace?.({ aguiEvent: runStartedEvent }, "Emitting AGUI event");
126
+ subscriber.next(runStartedEvent);
127
+ const isResume = !!input.forwardedProps?.resume;
128
+ const lastUserMessage = messages.filter((m) => m.role === "user").pop();
129
+ logger.debug?.(
130
+ {
131
+ isResume,
132
+ messageCount: messages.length,
133
+ toolCount: input.tools?.length ?? 0,
134
+ tools: input.tools?.map((t) => t.name),
135
+ lastUserMessage: typeof lastUserMessage?.content === "string" ? lastUserMessage.content.slice(0, 200) : void 0
136
+ },
137
+ "Preparing stream input"
138
+ );
139
+ logger.trace?.({ messages, tools: input.tools }, "Full input messages");
140
+ const streamEventInput = isResume ? new import_langgraph.Command({
119
141
  resume: JSON.stringify(input.forwardedProps?.resume?.payload)
120
142
  }) : {
121
143
  messages: aguiMessagesToLangChain(messages),
@@ -135,6 +157,7 @@ var LanggraphAgent = class extends import_client.AbstractAgent {
135
157
  thread_id: threadId
136
158
  }
137
159
  });
160
+ logger.debug?.("Stream created, starting event processing");
138
161
  const chatModelRuns = [];
139
162
  const handledToolCallIds = /* @__PURE__ */ new Set();
140
163
  for (const msg of messages) {
@@ -142,14 +165,32 @@ var LanggraphAgent = class extends import_client.AbstractAgent {
142
165
  handledToolCallIds.add(msg.toolCallId);
143
166
  }
144
167
  }
168
+ if (handledToolCallIds.size > 0) {
169
+ logger.debug?.(
170
+ { count: handledToolCallIds.size },
171
+ "Pre-populated handled tool call IDs from input messages"
172
+ );
173
+ }
145
174
  let interrupt;
146
175
  let currentToolCall = null;
176
+ let eventCount = 0;
177
+ let toolCallCount = 0;
178
+ let textChunkCount = 0;
147
179
  try {
148
180
  for await (const event of stream) {
181
+ eventCount++;
182
+ logger.trace?.(
183
+ { eventType: event.event, eventCount, langGraphEvent: event },
184
+ "Processing stream event"
185
+ );
149
186
  if (event.event.startsWith("ChannelWrite<")) {
150
187
  continue;
151
188
  }
152
189
  if (event.event === "on_chat_model_start") {
190
+ logger.debug?.(
191
+ { chatModelRunId: event.run_id },
192
+ "Chat model started"
193
+ );
153
194
  chatModelRuns.push({ runId: event.run_id });
154
195
  continue;
155
196
  }
@@ -158,6 +199,10 @@ var LanggraphAgent = class extends import_client.AbstractAgent {
158
199
  (run) => run.runId === event.run_id
159
200
  );
160
201
  if (!chatModelRun) {
202
+ logger.warn?.(
203
+ { chatModelRunId: event.run_id },
204
+ "Received message from unknown chat model run"
205
+ );
161
206
  subscriber.next({
162
207
  type: import_client.EventType.RUN_ERROR,
163
208
  message: `Received a message from an unknown chat model run. Run Id: ${event.run_id}`
@@ -167,12 +212,26 @@ var LanggraphAgent = class extends import_client.AbstractAgent {
167
212
  const chunkId = event.data.chunk.id;
168
213
  if (!chatModelRun.messageId) {
169
214
  chatModelRun.messageId = chunkId;
170
- subscriber.next({
215
+ const textStartEvent = {
171
216
  messageId: chunkId,
172
217
  type: import_client.EventType.TEXT_MESSAGE_START,
173
218
  role: "assistant"
174
- });
219
+ };
220
+ logger.debug?.({ messageId: chunkId }, "Text message started");
221
+ logger.trace?.(
222
+ { aguiEvent: textStartEvent },
223
+ "Emitting AGUI event"
224
+ );
225
+ subscriber.next(textStartEvent);
175
226
  } else if (chatModelRun.messageId !== chunkId) {
227
+ logger.warn?.(
228
+ {
229
+ expectedMessageId: chatModelRun.messageId,
230
+ receivedMessageId: chunkId,
231
+ chatModelRunId: event.run_id
232
+ },
233
+ "Received message with unexpected ID"
234
+ );
176
235
  subscriber.next({
177
236
  type: import_client.EventType.RUN_ERROR,
178
237
  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}`
@@ -187,29 +246,68 @@ var LanggraphAgent = class extends import_client.AbstractAgent {
187
246
  })).forEach((toolCall) => {
188
247
  if (currentToolCall) {
189
248
  if (toolCall.id && currentToolCall.id !== toolCall.id) {
190
- subscriber.next({
249
+ const toolEndEvent = {
191
250
  toolCallId: currentToolCall.id,
192
251
  type: import_client.EventType.TOOL_CALL_END
193
- });
252
+ };
253
+ logger.debug?.(
254
+ {
255
+ toolCallId: currentToolCall.id,
256
+ toolCallName: currentToolCall.name
257
+ },
258
+ "Tool call ended"
259
+ );
260
+ logger.trace?.(
261
+ { aguiEvent: toolEndEvent },
262
+ "Emitting AGUI event"
263
+ );
264
+ subscriber.next(toolEndEvent);
194
265
  if (toolCall.name && toolCall.id) {
195
266
  currentToolCall = toolCall;
196
- subscriber.next({
267
+ toolCallCount++;
268
+ const toolStartEvent = {
197
269
  toolCallId: currentToolCall.id,
198
270
  toolCallName: currentToolCall.name,
199
271
  parentMessageId,
200
272
  type: import_client.EventType.TOOL_CALL_START
201
- });
273
+ };
274
+ logger.debug?.(
275
+ {
276
+ toolCallId: toolCall.id,
277
+ toolCallName: toolCall.name
278
+ },
279
+ "Tool call started"
280
+ );
281
+ logger.trace?.(
282
+ { aguiEvent: toolStartEvent },
283
+ "Emitting AGUI event"
284
+ );
285
+ subscriber.next(toolStartEvent);
202
286
  if (currentToolCall.args) {
203
- subscriber.next({
287
+ const toolArgsEvent = {
204
288
  toolCallId: currentToolCall.id,
205
289
  delta: currentToolCall.args,
206
290
  type: import_client.EventType.TOOL_CALL_ARGS
207
- });
291
+ };
292
+ logger.trace?.(
293
+ { aguiEvent: toolArgsEvent },
294
+ "Emitting AGUI event"
295
+ );
296
+ subscriber.next(toolArgsEvent);
208
297
  if (isValidJson(currentToolCall.args)) {
209
- subscriber.next({
298
+ const toolEndEvent2 = {
210
299
  toolCallId: currentToolCall.id,
211
300
  type: import_client.EventType.TOOL_CALL_END
212
- });
301
+ };
302
+ logger.debug?.(
303
+ { toolCallId: currentToolCall.id },
304
+ "Tool call ended (args complete)"
305
+ );
306
+ logger.trace?.(
307
+ { aguiEvent: toolEndEvent2 },
308
+ "Emitting AGUI event"
309
+ );
310
+ subscriber.next(toolEndEvent2);
213
311
  currentToolCall = null;
214
312
  }
215
313
  }
@@ -217,16 +315,30 @@ var LanggraphAgent = class extends import_client.AbstractAgent {
217
315
  } else {
218
316
  if (toolCall.args) {
219
317
  currentToolCall.args += toolCall.args;
220
- subscriber.next({
318
+ const toolArgsEvent = {
221
319
  toolCallId: currentToolCall.id,
222
320
  delta: toolCall.args,
223
321
  type: import_client.EventType.TOOL_CALL_ARGS
224
- });
322
+ };
323
+ logger.trace?.(
324
+ { aguiEvent: toolArgsEvent },
325
+ "Emitting AGUI event"
326
+ );
327
+ subscriber.next(toolArgsEvent);
225
328
  if (isValidJson(currentToolCall.args)) {
226
- subscriber.next({
329
+ const toolEndEvent = {
227
330
  toolCallId: currentToolCall.id,
228
331
  type: import_client.EventType.TOOL_CALL_END
229
- });
332
+ };
333
+ logger.debug?.(
334
+ { toolCallId: currentToolCall.id },
335
+ "Tool call ended (args complete)"
336
+ );
337
+ logger.trace?.(
338
+ { aguiEvent: toolEndEvent },
339
+ "Emitting AGUI event"
340
+ );
341
+ subscriber.next(toolEndEvent);
230
342
  currentToolCall = null;
231
343
  }
232
344
  }
@@ -234,23 +346,47 @@ var LanggraphAgent = class extends import_client.AbstractAgent {
234
346
  } else {
235
347
  if (toolCall.name && toolCall.id) {
236
348
  currentToolCall = toolCall;
237
- subscriber.next({
349
+ toolCallCount++;
350
+ const toolStartEvent = {
238
351
  toolCallId: toolCall.id,
239
352
  toolCallName: toolCall.name,
240
353
  parentMessageId,
241
354
  type: import_client.EventType.TOOL_CALL_START
242
- });
355
+ };
356
+ logger.debug?.(
357
+ { toolCallId: toolCall.id, toolCallName: toolCall.name },
358
+ "Tool call started"
359
+ );
360
+ logger.trace?.(
361
+ { aguiEvent: toolStartEvent },
362
+ "Emitting AGUI event"
363
+ );
364
+ subscriber.next(toolStartEvent);
243
365
  if (toolCall.args) {
244
- subscriber.next({
366
+ const toolArgsEvent = {
245
367
  toolCallId: toolCall.id,
246
368
  delta: toolCall.args,
247
369
  type: import_client.EventType.TOOL_CALL_ARGS
248
- });
370
+ };
371
+ logger.trace?.(
372
+ { aguiEvent: toolArgsEvent },
373
+ "Emitting AGUI event"
374
+ );
375
+ subscriber.next(toolArgsEvent);
249
376
  if (isValidJson(toolCall.args)) {
250
- subscriber.next({
377
+ const toolEndEvent = {
251
378
  toolCallId: toolCall.id,
252
379
  type: import_client.EventType.TOOL_CALL_END
253
- });
380
+ };
381
+ logger.debug?.(
382
+ { toolCallId: toolCall.id },
383
+ "Tool call ended (args complete)"
384
+ );
385
+ logger.trace?.(
386
+ { aguiEvent: toolEndEvent },
387
+ "Emitting AGUI event"
388
+ );
389
+ subscriber.next(toolEndEvent);
254
390
  currentToolCall = null;
255
391
  }
256
392
  }
@@ -260,11 +396,17 @@ var LanggraphAgent = class extends import_client.AbstractAgent {
260
396
  }
261
397
  const delta = event.data.chunk.content;
262
398
  if (typeof delta === "string" && delta) {
263
- subscriber.next({
399
+ textChunkCount++;
400
+ const textContentEvent = {
264
401
  messageId: chatModelRun.messageId,
265
402
  type: import_client.EventType.TEXT_MESSAGE_CONTENT,
266
403
  delta
267
- });
404
+ };
405
+ logger.trace?.(
406
+ { aguiEvent: textContentEvent },
407
+ "Emitting AGUI event"
408
+ );
409
+ subscriber.next(textContentEvent);
268
410
  }
269
411
  continue;
270
412
  }
@@ -273,16 +415,26 @@ var LanggraphAgent = class extends import_client.AbstractAgent {
273
415
  (run) => run.runId === event.run_id
274
416
  );
275
417
  if (!chatModelRun) {
418
+ logger.warn?.(
419
+ { chatModelRunId: event.run_id },
420
+ "Received on_chat_model_end from unknown run"
421
+ );
276
422
  subscriber.next({
277
423
  type: import_client.EventType.RUN_ERROR,
278
424
  message: `Received a on_chat_model_end event from an unknown chat model run. Run Id: ${event.run_id}`
279
425
  });
280
426
  continue;
281
427
  }
282
- subscriber.next({
428
+ const textEndEvent = {
283
429
  type: import_client.EventType.TEXT_MESSAGE_END,
284
430
  messageId: chatModelRun.messageId
285
- });
431
+ };
432
+ logger.debug?.(
433
+ { messageId: chatModelRun.messageId },
434
+ "Text message ended"
435
+ );
436
+ logger.trace?.({ aguiEvent: textEndEvent }, "Emitting AGUI event");
437
+ subscriber.next(textEndEvent);
286
438
  continue;
287
439
  }
288
440
  if (event.event === "on_tool_end") {
@@ -295,19 +447,40 @@ var LanggraphAgent = class extends import_client.AbstractAgent {
295
447
  toolMessage.lc_kwargs.id = toolMessage.id;
296
448
  }
297
449
  }
298
- subscriber.next({
450
+ const toolResultEvent = {
299
451
  toolCallId: toolMessage.tool_call_id,
300
452
  type: import_client.EventType.TOOL_CALL_RESULT,
301
453
  content: typeof toolMessage.content === "string" ? toolMessage.content : JSON.stringify(toolMessage.content),
302
454
  messageId: toolMessage.id
303
- });
455
+ };
456
+ logger.debug?.(
457
+ {
458
+ toolCallId: toolMessage.tool_call_id,
459
+ messageId: toolMessage.id
460
+ },
461
+ "Tool call result received"
462
+ );
463
+ logger.trace?.(
464
+ { aguiEvent: toolResultEvent },
465
+ "Emitting AGUI event"
466
+ );
467
+ subscriber.next(toolResultEvent);
304
468
  handledToolCallIds.add(toolMessage.tool_call_id);
469
+ } else {
470
+ logger.trace?.(
471
+ { toolCallId: toolMessage.tool_call_id },
472
+ "Skipping duplicate tool call result"
473
+ );
305
474
  }
306
475
  }
307
476
  continue;
308
477
  }
309
478
  if (event.event === "on_chain_stream" && event.data.chunk?.__interrupt__ && Array.isArray(event.data.chunk.__interrupt__) && event.data.chunk.__interrupt__.length > 0) {
310
479
  const rawInterrupt = event.data.chunk.__interrupt__[0];
480
+ logger.debug?.(
481
+ { interruptId: rawInterrupt.id },
482
+ "Interrupt received"
483
+ );
311
484
  interrupt = {
312
485
  id: rawInterrupt.id,
313
486
  // TODO: replace with actual reason
@@ -316,23 +489,36 @@ var LanggraphAgent = class extends import_client.AbstractAgent {
316
489
  };
317
490
  }
318
491
  }
492
+ const stats = { eventCount, toolCallCount, textChunkCount };
319
493
  if (interrupt) {
320
- subscriber.next({
494
+ const runFinishedEvent = {
321
495
  type: import_client.EventType.RUN_FINISHED,
322
496
  threadId,
323
497
  runId,
324
498
  outcome: "interrupt",
325
499
  interrupt
326
- });
500
+ };
501
+ logger.info?.(
502
+ { outcome: "interrupt", interruptId: interrupt.id, ...stats },
503
+ "Run finished with interrupt"
504
+ );
505
+ logger.trace?.({ aguiEvent: runFinishedEvent }, "Emitting AGUI event");
506
+ subscriber.next(runFinishedEvent);
327
507
  } else {
328
- subscriber.next({
508
+ const runFinishedEvent = {
329
509
  type: import_client.EventType.RUN_FINISHED,
330
510
  threadId,
331
511
  runId
332
- });
512
+ };
513
+ logger.info?.({ outcome: "complete", ...stats }, "Run finished");
514
+ logger.trace?.({ aguiEvent: runFinishedEvent }, "Emitting AGUI event");
515
+ subscriber.next(runFinishedEvent);
333
516
  }
334
517
  } catch (error) {
335
- console.error("[LanggraphAgent] Error during stream processing:", error);
518
+ logger.error?.(
519
+ { err: error, eventCount, toolCallCount, textChunkCount },
520
+ "Error during stream processing"
521
+ );
336
522
  subscriber.next({
337
523
  type: import_client.EventType.RUN_ERROR,
338
524
  message: error instanceof Error ? error.message : String(error)
@@ -342,10 +528,12 @@ var LanggraphAgent = class extends import_client.AbstractAgent {
342
528
  }
343
529
  clone() {
344
530
  const workflow = this.compiledWorkflow;
531
+ const logger = this.logger;
345
532
  this.compiledWorkflow = void 0;
346
533
  const cloned = super.clone();
347
534
  this.compiledWorkflow = workflow;
348
535
  cloned.compiledWorkflow = workflow;
536
+ cloned.logger = logger;
349
537
  return cloned;
350
538
  }
351
539
  };
@@ -397,8 +585,7 @@ function aguiMessagesToLangChain(messages) {
397
585
  id: message.id
398
586
  };
399
587
  default:
400
- console.error(`Message role ${message.role} is not implemented`);
401
- throw new Error("message role is not supported.");
588
+ throw new Error(`Message role ${message.role} is not supported.`);
402
589
  }
403
590
  });
404
591
  }
@@ -1086,12 +1273,17 @@ var TDAIStore = class extends import_langgraph3.BaseStore {
1086
1273
  }
1087
1274
  }
1088
1275
  };
1276
+
1277
+ // src/index.ts
1278
+ var import_agent_shared2 = require("@cloudbase/agent-shared");
1089
1279
  // Annotate the CommonJS export names for ESM import in node:
1090
1280
  0 && (module.exports = {
1091
1281
  ClientPropertiesAnnotation,
1092
1282
  ClientStateAnnotation,
1093
1283
  LanggraphAgent,
1094
1284
  TDAISaver,
1095
- TDAIStore
1285
+ TDAIStore,
1286
+ createConsoleLogger,
1287
+ noopLogger
1096
1288
  });
1097
1289
  //# sourceMappingURL=index.js.map