@cloudbase/agent-observability 1.0.1-alpha.10 → 1.0.1-alpha.11
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/langchain.d.mts +4 -1
- package/dist/langchain.d.ts +4 -1
- package/dist/langchain.js +38 -37
- package/dist/langchain.js.map +1 -1
- package/dist/langchain.mjs +38 -37
- package/dist/langchain.mjs.map +1 -1
- package/package.json +3 -2
- package/src/langchain/CallbackHandler.ts +41 -38
|
@@ -28,6 +28,7 @@ import {
|
|
|
28
28
|
type ObservationAttributes,
|
|
29
29
|
} from "../index.js";
|
|
30
30
|
import type { SpanContext } from "@opentelemetry/api";
|
|
31
|
+
import { type Logger, noopLogger } from "@cloudbase/agent-shared";
|
|
31
32
|
|
|
32
33
|
/**
|
|
33
34
|
* Constructor parameters for CallbackHandler.
|
|
@@ -41,6 +42,8 @@ type ConstructorParams = {
|
|
|
41
42
|
version?: string;
|
|
42
43
|
traceMetadata?: Record<string, unknown>;
|
|
43
44
|
adapterName?: string; // e.g., "LangGraph" or "LangChain"
|
|
45
|
+
/** Logger for debug output. Defaults to noopLogger (silent). */
|
|
46
|
+
logger?: Logger;
|
|
44
47
|
};
|
|
45
48
|
|
|
46
49
|
/**
|
|
@@ -104,6 +107,9 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
104
107
|
// Adapter name for ROOT span prefix
|
|
105
108
|
private adapterName?: string;
|
|
106
109
|
|
|
110
|
+
// Logger for debug output (defaults to noopLogger for silent operation)
|
|
111
|
+
private logger: Logger;
|
|
112
|
+
|
|
107
113
|
constructor(params?: ConstructorParams) {
|
|
108
114
|
super();
|
|
109
115
|
|
|
@@ -113,14 +119,11 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
113
119
|
this.traceMetadata = params?.traceMetadata;
|
|
114
120
|
this.version = params?.version;
|
|
115
121
|
this.adapterName = params?.adapterName;
|
|
122
|
+
this.logger = params?.logger ?? noopLogger;
|
|
116
123
|
|
|
117
124
|
this.promptToParentRunMap = new Map<string, PromptInfo>();
|
|
118
125
|
}
|
|
119
126
|
|
|
120
|
-
get logger() {
|
|
121
|
-
return console;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
127
|
/**
|
|
125
128
|
* Set external parent SpanContext from AG-UI.Server span.
|
|
126
129
|
* This allows the CallbackHandler to link LangChain/LangGraph spans
|
|
@@ -142,7 +145,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
142
145
|
_fields?: any
|
|
143
146
|
): Promise<void> {
|
|
144
147
|
if (runId && !(runId in this.completionStartTimes)) {
|
|
145
|
-
this.logger.debug(`LLM first streaming token: ${runId}`);
|
|
148
|
+
this.logger.debug?.(`LLM first streaming token: ${runId}`);
|
|
146
149
|
this.completionStartTimes[runId] = new Date();
|
|
147
150
|
}
|
|
148
151
|
}
|
|
@@ -158,7 +161,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
158
161
|
name?: string
|
|
159
162
|
): Promise<void> {
|
|
160
163
|
try {
|
|
161
|
-
this.logger.debug(`Chain start with Id: ${runId}`);
|
|
164
|
+
this.logger.debug?.(`Chain start with Id: ${runId}`);
|
|
162
165
|
|
|
163
166
|
const runName = name ?? chain.id.at(-1)?.toString() ?? "Langchain Run";
|
|
164
167
|
|
|
@@ -225,7 +228,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
225
228
|
});
|
|
226
229
|
}
|
|
227
230
|
} catch (e) {
|
|
228
|
-
this.logger.debug(e instanceof Error ? e.message : String(e));
|
|
231
|
+
this.logger.debug?.(e instanceof Error ? e.message : String(e));
|
|
229
232
|
}
|
|
230
233
|
}
|
|
231
234
|
|
|
@@ -235,7 +238,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
235
238
|
parentRunId?: string
|
|
236
239
|
): Promise<void> {
|
|
237
240
|
try {
|
|
238
|
-
this.logger.debug(`Agent action ${action.tool} with ID: ${runId}`);
|
|
241
|
+
this.logger.debug?.(`Agent action ${action.tool} with ID: ${runId}`);
|
|
239
242
|
this.startAndRegisterObservation({
|
|
240
243
|
runId,
|
|
241
244
|
parentRunId,
|
|
@@ -246,7 +249,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
246
249
|
asType: "tool",
|
|
247
250
|
});
|
|
248
251
|
} catch (e) {
|
|
249
|
-
this.logger.debug(e instanceof Error ? e.message : String(e));
|
|
252
|
+
this.logger.debug?.(e instanceof Error ? e.message : String(e));
|
|
250
253
|
}
|
|
251
254
|
}
|
|
252
255
|
|
|
@@ -256,13 +259,13 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
256
259
|
_parentRunId?: string
|
|
257
260
|
): Promise<void> {
|
|
258
261
|
try {
|
|
259
|
-
this.logger.debug(`Agent finish with ID: ${runId}`);
|
|
262
|
+
this.logger.debug?.(`Agent finish with ID: ${runId}`);
|
|
260
263
|
this.handleObservationEnd({
|
|
261
264
|
runId,
|
|
262
265
|
attributes: { output: action },
|
|
263
266
|
});
|
|
264
267
|
} catch (e) {
|
|
265
|
-
this.logger.debug(e instanceof Error ? e.message : String(e));
|
|
268
|
+
this.logger.debug?.(e instanceof Error ? e.message : String(e));
|
|
266
269
|
}
|
|
267
270
|
}
|
|
268
271
|
|
|
@@ -272,7 +275,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
272
275
|
_parentRunId?: string | undefined
|
|
273
276
|
): Promise<void> {
|
|
274
277
|
try {
|
|
275
|
-
this.logger.debug(`Chain error: ${err} with ID: ${runId}`);
|
|
278
|
+
this.logger.debug?.(`Chain error: ${err} with ID: ${runId}`);
|
|
276
279
|
this.handleObservationEnd({
|
|
277
280
|
runId,
|
|
278
281
|
attributes: {
|
|
@@ -281,7 +284,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
281
284
|
},
|
|
282
285
|
});
|
|
283
286
|
} catch (e) {
|
|
284
|
-
this.logger.debug(e instanceof Error ? e.message : String(e));
|
|
287
|
+
this.logger.debug?.(e instanceof Error ? e.message : String(e));
|
|
285
288
|
}
|
|
286
289
|
}
|
|
287
290
|
|
|
@@ -295,7 +298,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
295
298
|
metadata?: Record<string, unknown> | undefined,
|
|
296
299
|
name?: string
|
|
297
300
|
): Promise<void> {
|
|
298
|
-
this.logger.debug(
|
|
301
|
+
this.logger.debug?.(
|
|
299
302
|
`Generation start with ID: ${runId} and parentRunId ${parentRunId}`
|
|
300
303
|
);
|
|
301
304
|
|
|
@@ -370,7 +373,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
370
373
|
name?: string
|
|
371
374
|
): Promise<void> {
|
|
372
375
|
try {
|
|
373
|
-
this.logger.debug(`Chat model start with ID: ${runId}`);
|
|
376
|
+
this.logger.debug?.(`Chat model start with ID: ${runId}`);
|
|
374
377
|
|
|
375
378
|
const prompts = messages.flatMap((message) =>
|
|
376
379
|
message.map((m) => this.extractChatMessageContent(m))
|
|
@@ -387,7 +390,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
387
390
|
name
|
|
388
391
|
);
|
|
389
392
|
} catch (e) {
|
|
390
|
-
this.logger.debug(e instanceof Error ? e.message : String(e));
|
|
393
|
+
this.logger.debug?.(e instanceof Error ? e.message : String(e));
|
|
391
394
|
}
|
|
392
395
|
}
|
|
393
396
|
|
|
@@ -397,7 +400,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
397
400
|
_parentRunId?: string | undefined
|
|
398
401
|
): Promise<void> {
|
|
399
402
|
try {
|
|
400
|
-
this.logger.debug(`Chain end with ID: ${runId}`);
|
|
403
|
+
this.logger.debug?.(`Chain end with ID: ${runId}`);
|
|
401
404
|
|
|
402
405
|
let finalOutput: ChainValues | string = outputs;
|
|
403
406
|
if (
|
|
@@ -427,7 +430,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
427
430
|
});
|
|
428
431
|
this.deregisterPromptInfo(runId);
|
|
429
432
|
} catch (e) {
|
|
430
|
-
this.logger.debug(e instanceof Error ? e.message : String(e));
|
|
433
|
+
this.logger.debug?.(e instanceof Error ? e.message : String(e));
|
|
431
434
|
}
|
|
432
435
|
}
|
|
433
436
|
|
|
@@ -442,7 +445,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
442
445
|
name?: string
|
|
443
446
|
): Promise<void> {
|
|
444
447
|
try {
|
|
445
|
-
this.logger.debug(`LLM start with ID: ${runId}`);
|
|
448
|
+
this.logger.debug?.(`LLM start with ID: ${runId}`);
|
|
446
449
|
this.handleGenerationStart(
|
|
447
450
|
llm,
|
|
448
451
|
prompts,
|
|
@@ -454,7 +457,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
454
457
|
name
|
|
455
458
|
);
|
|
456
459
|
} catch (e) {
|
|
457
|
-
this.logger.debug(e instanceof Error ? e.message : String(e));
|
|
460
|
+
this.logger.debug?.(e instanceof Error ? e.message : String(e));
|
|
458
461
|
}
|
|
459
462
|
}
|
|
460
463
|
|
|
@@ -468,7 +471,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
468
471
|
name?: string
|
|
469
472
|
): Promise<void> {
|
|
470
473
|
try {
|
|
471
|
-
this.logger.debug(`Tool start with ID: ${runId}`);
|
|
474
|
+
this.logger.debug?.(`Tool start with ID: ${runId}`);
|
|
472
475
|
this.startAndRegisterObservation({
|
|
473
476
|
runId,
|
|
474
477
|
parentRunId,
|
|
@@ -481,7 +484,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
481
484
|
asType: "tool",
|
|
482
485
|
});
|
|
483
486
|
} catch (e) {
|
|
484
|
-
this.logger.debug(e instanceof Error ? e.message : String(e));
|
|
487
|
+
this.logger.debug?.(e instanceof Error ? e.message : String(e));
|
|
485
488
|
}
|
|
486
489
|
}
|
|
487
490
|
|
|
@@ -495,7 +498,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
495
498
|
name?: string
|
|
496
499
|
): Promise<void> {
|
|
497
500
|
try {
|
|
498
|
-
this.logger.debug(`Retriever start with ID: ${runId}`);
|
|
501
|
+
this.logger.debug?.(`Retriever start with ID: ${runId}`);
|
|
499
502
|
this.startAndRegisterObservation({
|
|
500
503
|
runId,
|
|
501
504
|
parentRunId,
|
|
@@ -508,7 +511,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
508
511
|
asType: "span",
|
|
509
512
|
});
|
|
510
513
|
} catch (e) {
|
|
511
|
-
this.logger.debug(e instanceof Error ? e.message : String(e));
|
|
514
|
+
this.logger.debug?.(e instanceof Error ? e.message : String(e));
|
|
512
515
|
}
|
|
513
516
|
}
|
|
514
517
|
|
|
@@ -518,7 +521,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
518
521
|
_parentRunId?: string | undefined
|
|
519
522
|
): Promise<void> {
|
|
520
523
|
try {
|
|
521
|
-
this.logger.debug(`Retriever end with ID: ${runId}`);
|
|
524
|
+
this.logger.debug?.(`Retriever end with ID: ${runId}`);
|
|
522
525
|
this.handleObservationEnd({
|
|
523
526
|
runId,
|
|
524
527
|
attributes: {
|
|
@@ -526,7 +529,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
526
529
|
},
|
|
527
530
|
});
|
|
528
531
|
} catch (e) {
|
|
529
|
-
this.logger.debug(e instanceof Error ? e.message : String(e));
|
|
532
|
+
this.logger.debug?.(e instanceof Error ? e.message : String(e));
|
|
530
533
|
}
|
|
531
534
|
}
|
|
532
535
|
|
|
@@ -536,7 +539,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
536
539
|
_parentRunId?: string | undefined
|
|
537
540
|
): Promise<void> {
|
|
538
541
|
try {
|
|
539
|
-
this.logger.debug(`Retriever error: ${err} with ID: ${runId}`);
|
|
542
|
+
this.logger.debug?.(`Retriever error: ${err} with ID: ${runId}`);
|
|
540
543
|
this.handleObservationEnd({
|
|
541
544
|
runId,
|
|
542
545
|
attributes: {
|
|
@@ -545,7 +548,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
545
548
|
},
|
|
546
549
|
});
|
|
547
550
|
} catch (e) {
|
|
548
|
-
this.logger.debug(e instanceof Error ? e.message : String(e));
|
|
551
|
+
this.logger.debug?.(e instanceof Error ? e.message : String(e));
|
|
549
552
|
}
|
|
550
553
|
}
|
|
551
554
|
|
|
@@ -555,13 +558,13 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
555
558
|
_parentRunId?: string | undefined
|
|
556
559
|
): Promise<void> {
|
|
557
560
|
try {
|
|
558
|
-
this.logger.debug(`Tool end with ID: ${runId}`);
|
|
561
|
+
this.logger.debug?.(`Tool end with ID: ${runId}`);
|
|
559
562
|
this.handleObservationEnd({
|
|
560
563
|
runId,
|
|
561
564
|
attributes: { output },
|
|
562
565
|
});
|
|
563
566
|
} catch (e) {
|
|
564
|
-
this.logger.debug(e instanceof Error ? e.message : String(e));
|
|
567
|
+
this.logger.debug?.(e instanceof Error ? e.message : String(e));
|
|
565
568
|
}
|
|
566
569
|
}
|
|
567
570
|
|
|
@@ -571,7 +574,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
571
574
|
_parentRunId?: string | undefined
|
|
572
575
|
): Promise<void> {
|
|
573
576
|
try {
|
|
574
|
-
this.logger.debug(`Tool error ${err} with ID: ${runId}`);
|
|
577
|
+
this.logger.debug?.(`Tool error ${err} with ID: ${runId}`);
|
|
575
578
|
this.handleObservationEnd({
|
|
576
579
|
runId,
|
|
577
580
|
attributes: {
|
|
@@ -580,7 +583,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
580
583
|
},
|
|
581
584
|
});
|
|
582
585
|
} catch (e) {
|
|
583
|
-
this.logger.debug(e instanceof Error ? e.message : String(e));
|
|
586
|
+
this.logger.debug?.(e instanceof Error ? e.message : String(e));
|
|
584
587
|
}
|
|
585
588
|
}
|
|
586
589
|
|
|
@@ -590,7 +593,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
590
593
|
_parentRunId?: string | undefined
|
|
591
594
|
): Promise<void> {
|
|
592
595
|
try {
|
|
593
|
-
this.logger.debug(`LLM end with ID: ${runId}`);
|
|
596
|
+
this.logger.debug?.(`LLM end with ID: ${runId}`);
|
|
594
597
|
|
|
595
598
|
const lastResponse =
|
|
596
599
|
output.generations[output.generations.length - 1][
|
|
@@ -661,7 +664,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
661
664
|
delete this.completionStartTimes[runId];
|
|
662
665
|
}
|
|
663
666
|
} catch (e) {
|
|
664
|
-
this.logger.debug(e instanceof Error ? e.message : String(e));
|
|
667
|
+
this.logger.debug?.(e instanceof Error ? e.message : String(e));
|
|
665
668
|
}
|
|
666
669
|
}
|
|
667
670
|
|
|
@@ -671,7 +674,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
671
674
|
_parentRunId?: string | undefined
|
|
672
675
|
): Promise<void> {
|
|
673
676
|
try {
|
|
674
|
-
this.logger.debug(`LLM error ${err} with ID: ${runId}`);
|
|
677
|
+
this.logger.debug?.(`LLM error ${err} with ID: ${runId}`);
|
|
675
678
|
this.handleObservationEnd({
|
|
676
679
|
runId,
|
|
677
680
|
attributes: {
|
|
@@ -680,7 +683,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
680
683
|
},
|
|
681
684
|
});
|
|
682
685
|
} catch (e) {
|
|
683
|
-
this.logger.debug(e instanceof Error ? e.message : String(e));
|
|
686
|
+
this.logger.debug?.(e instanceof Error ? e.message : String(e));
|
|
684
687
|
}
|
|
685
688
|
}
|
|
686
689
|
|
|
@@ -758,7 +761,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
758
761
|
|
|
759
762
|
const observation = this.runMap.get(runId);
|
|
760
763
|
if (!observation) {
|
|
761
|
-
this.logger.warn("Observation not found in runMap. Skipping operation.");
|
|
764
|
+
this.logger.warn?.("Observation not found in runMap. Skipping operation.");
|
|
762
765
|
return;
|
|
763
766
|
}
|
|
764
767
|
|
|
@@ -813,7 +816,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
813
816
|
: undefined;
|
|
814
817
|
return usageMetadata;
|
|
815
818
|
} catch (err) {
|
|
816
|
-
this.logger.debug(`Error extracting usage metadata: ${err}`);
|
|
819
|
+
this.logger.debug?.(`Error extracting usage metadata: ${err}`);
|
|
817
820
|
return;
|
|
818
821
|
}
|
|
819
822
|
}
|