@uselemma/tracing 2.10.0 → 2.12.0
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/debug-mode.js +2 -2
- package/dist/trace-wrapper.d.ts +8 -11
- package/dist/trace-wrapper.d.ts.map +1 -1
- package/dist/trace-wrapper.js +17 -28
- package/dist/trace-wrapper.js.map +1 -1
- package/package.json +1 -1
package/dist/debug-mode.js
CHANGED
|
@@ -18,10 +18,10 @@ function lemmaDebug(prefix, msg, data) {
|
|
|
18
18
|
if (!isDebugModeEnabled())
|
|
19
19
|
return;
|
|
20
20
|
if (data !== undefined) {
|
|
21
|
-
console.log(`[
|
|
21
|
+
console.log(`[LEMMA:${prefix}] ${msg}`, data);
|
|
22
22
|
}
|
|
23
23
|
else {
|
|
24
|
-
console.log(`[
|
|
24
|
+
console.log(`[LEMMA:${prefix}] ${msg}`);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
//# sourceMappingURL=debug-mode.js.map
|
package/dist/trace-wrapper.d.ts
CHANGED
|
@@ -5,23 +5,17 @@ export type TraceContext = {
|
|
|
5
5
|
/** Unique identifier for this agent run. */
|
|
6
6
|
runId: string;
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* Record the run output. Sets `ai.agent.output` on the span — call this
|
|
9
|
+
* before returning to store a specific value. If omitted, the function's
|
|
10
|
+
* return value is captured automatically.
|
|
11
11
|
*/
|
|
12
|
-
onComplete: (result: unknown) =>
|
|
12
|
+
onComplete: (result: unknown) => void;
|
|
13
13
|
/** Record an error on the span. Marks the span as errored. */
|
|
14
14
|
recordError: (error: unknown) => void;
|
|
15
15
|
};
|
|
16
16
|
export type WrapAgentOptions = {
|
|
17
17
|
/** Mark this run as an experiment in Lemma. */
|
|
18
18
|
isExperiment?: boolean;
|
|
19
|
-
/**
|
|
20
|
-
* If `true` (the default), the top-level span is automatically ended when
|
|
21
|
-
* the wrapped function returns or throws. Set to `false` to manage span
|
|
22
|
-
* lifetime manually via `ctx.onComplete`.
|
|
23
|
-
*/
|
|
24
|
-
autoEndRoot?: boolean;
|
|
25
19
|
};
|
|
26
20
|
/**
|
|
27
21
|
* Wraps an agent function with OpenTelemetry tracing, automatically creating
|
|
@@ -30,6 +24,10 @@ export type WrapAgentOptions = {
|
|
|
30
24
|
* The returned function creates a new root span on every invocation, attaches
|
|
31
25
|
* agent metadata (name, run ID, experiment flag), and handles error recording.
|
|
32
26
|
*
|
|
27
|
+
* `ai.agent.input` and `ai.agent.output` are set as JSON strings for Lemma
|
|
28
|
+
* ingestion and UI. The span ends automatically when the wrapped function
|
|
29
|
+
* returns or throws.
|
|
30
|
+
*
|
|
33
31
|
* @example
|
|
34
32
|
* const myAgent = wrapAgent<{ topic: string }>(
|
|
35
33
|
* 'my-agent',
|
|
@@ -44,7 +42,6 @@ export type WrapAgentOptions = {
|
|
|
44
42
|
* @param fn - The agent function to wrap. Receives a {@link TraceContext} as its first argument and the call-time input as its second.
|
|
45
43
|
* @param options - Configuration for the agent trace.
|
|
46
44
|
* @param options.isExperiment - Mark this run as an experiment in Lemma.
|
|
47
|
-
* @param options.autoEndRoot - Automatically end the top-level span when the wrapped function returns or throws (default: `true`). Set to `false` to end manually via `ctx.onComplete`.
|
|
48
45
|
* @returns An async function that accepts an `input`, executes `fn` inside a traced context, and returns `{ result, runId, span }`.
|
|
49
46
|
*/
|
|
50
47
|
export declare function wrapAgent<Input = unknown>(agentName: string, fn: (traceContext: TraceContext, input: Input) => any, options?: WrapAgentOptions): (this: any, input: Input) => Promise<{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trace-wrapper.d.ts","sourceRoot":"","sources":["../src/trace-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgC,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAKxE,MAAM,MAAM,YAAY,GAAG;IACzB,wDAAwD;IACxD,IAAI,EAAE,IAAI,CAAC;IACX,4CAA4C;IAC5C,KAAK,EAAE,MAAM,CAAC;IACd;;;;OAIG;IACH,UAAU,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"trace-wrapper.d.ts","sourceRoot":"","sources":["../src/trace-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgC,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAKxE,MAAM,MAAM,YAAY,GAAG;IACzB,wDAAwD;IACxD,IAAI,EAAE,IAAI,CAAC;IACX,4CAA4C;IAC5C,KAAK,EAAE,MAAM,CAAC;IACd;;;;OAIG;IACH,UAAU,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,IAAI,CAAC;IACtC,8DAA8D;IAC9D,WAAW,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;CACvC,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,+CAA+C;IAC/C,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,SAAS,CAAC,KAAK,GAAG,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,YAAY,EAAE,YAAY,EAAE,KAAK,EAAE,KAAK,KAAK,GAAG,EAAE,OAAO,CAAC,EAAE,gBAAgB,UAC/F,GAAG,SAAS,KAAK;;;;GAqDhE"}
|
package/dist/trace-wrapper.js
CHANGED
|
@@ -12,6 +12,10 @@ const experiment_mode_1 = require("./experiment-mode");
|
|
|
12
12
|
* The returned function creates a new root span on every invocation, attaches
|
|
13
13
|
* agent metadata (name, run ID, experiment flag), and handles error recording.
|
|
14
14
|
*
|
|
15
|
+
* `ai.agent.input` and `ai.agent.output` are set as JSON strings for Lemma
|
|
16
|
+
* ingestion and UI. The span ends automatically when the wrapped function
|
|
17
|
+
* returns or throws.
|
|
18
|
+
*
|
|
15
19
|
* @example
|
|
16
20
|
* const myAgent = wrapAgent<{ topic: string }>(
|
|
17
21
|
* 'my-agent',
|
|
@@ -26,63 +30,48 @@ const experiment_mode_1 = require("./experiment-mode");
|
|
|
26
30
|
* @param fn - The agent function to wrap. Receives a {@link TraceContext} as its first argument and the call-time input as its second.
|
|
27
31
|
* @param options - Configuration for the agent trace.
|
|
28
32
|
* @param options.isExperiment - Mark this run as an experiment in Lemma.
|
|
29
|
-
* @param options.autoEndRoot - Automatically end the top-level span when the wrapped function returns or throws (default: `true`). Set to `false` to end manually via `ctx.onComplete`.
|
|
30
33
|
* @returns An async function that accepts an `input`, executes `fn` inside a traced context, and returns `{ result, runId, span }`.
|
|
31
34
|
*/
|
|
32
35
|
function wrapAgent(agentName, fn, options) {
|
|
33
36
|
const wrappedFunction = async function (input) {
|
|
34
|
-
// Obtain the Lemma tracer from the global OTel provider
|
|
35
37
|
const tracer = api_1.trace.getTracer("lemma");
|
|
36
|
-
// Generate a unique run ID and open a new span for this agent invocation
|
|
37
38
|
const runId = (0, uuid_1.v4)();
|
|
38
|
-
const autoEndRoot = options?.autoEndRoot !== false; // default true
|
|
39
39
|
const span = tracer.startSpan("ai.agent.run", {
|
|
40
40
|
attributes: {
|
|
41
41
|
"ai.agent.name": agentName,
|
|
42
42
|
"lemma.run_id": runId,
|
|
43
43
|
"lemma.is_experiment": (0, experiment_mode_1.isExperimentModeEnabled)() || options?.isExperiment === true,
|
|
44
|
-
"lemma.auto_end_root": autoEndRoot,
|
|
45
44
|
},
|
|
46
45
|
}, api_1.ROOT_CONTEXT);
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
span.setAttribute("ai.agent.input", JSON.stringify(input) ?? "null");
|
|
47
|
+
(0, debug_mode_1.lemmaDebug)("trace-wrapper", "span started", { agentName, runId });
|
|
49
48
|
const ctx = api_1.trace.setSpan(api_1.ROOT_CONTEXT, span);
|
|
50
|
-
let
|
|
49
|
+
let outputSet = false;
|
|
51
50
|
try {
|
|
52
51
|
return await api_1.context.with(ctx, async () => {
|
|
53
|
-
const onComplete = (
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
(0, debug_mode_1.lemmaDebug)("trace-wrapper", "span ended via onComplete", { runId });
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
(0, debug_mode_1.lemmaDebug)("trace-wrapper", "onComplete called but span not ended (autoEndRoot active or already ended)", { runId });
|
|
61
|
-
return false;
|
|
52
|
+
const onComplete = (result) => {
|
|
53
|
+
span.setAttribute("ai.agent.output", JSON.stringify(result) ?? "null");
|
|
54
|
+
outputSet = true;
|
|
55
|
+
(0, debug_mode_1.lemmaDebug)("trace-wrapper", "onComplete called", { runId });
|
|
62
56
|
};
|
|
63
57
|
const recordError = (error) => {
|
|
64
58
|
span.recordException(error instanceof Error ? error : new Error(String(error)));
|
|
65
59
|
span.setStatus({ code: 2 }); // SpanStatusCode.ERROR
|
|
66
60
|
};
|
|
67
61
|
const result = await fn.call(this, { span, runId, onComplete, recordError }, input);
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
rootEnded = true;
|
|
71
|
-
span.end();
|
|
72
|
-
(0, debug_mode_1.lemmaDebug)("trace-wrapper", "span auto-ended after fn returned", { runId });
|
|
62
|
+
if (!outputSet) {
|
|
63
|
+
span.setAttribute("ai.agent.output", JSON.stringify(result) ?? "null");
|
|
73
64
|
}
|
|
65
|
+
span.end();
|
|
66
|
+
(0, debug_mode_1.lemmaDebug)("trace-wrapper", "span ended after fn returned", { runId });
|
|
74
67
|
return { result, runId, span };
|
|
75
68
|
});
|
|
76
69
|
}
|
|
77
70
|
catch (err) {
|
|
78
|
-
// Record the exception on the span, mark it as errored, and end it
|
|
79
71
|
span.recordException(err);
|
|
80
72
|
span.setStatus({ code: 2 }); // SpanStatusCode.ERROR
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
span.end();
|
|
84
|
-
(0, debug_mode_1.lemmaDebug)("trace-wrapper", "span ended on error", { runId, error: String(err) });
|
|
85
|
-
}
|
|
73
|
+
span.end();
|
|
74
|
+
(0, debug_mode_1.lemmaDebug)("trace-wrapper", "span ended on error", { runId, error: String(err) });
|
|
86
75
|
throw err;
|
|
87
76
|
}
|
|
88
77
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trace-wrapper.js","sourceRoot":"","sources":["../src/trace-wrapper.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"trace-wrapper.js","sourceRoot":"","sources":["../src/trace-wrapper.ts"],"names":[],"mappings":";;AAoDA,8BAsDC;AA1GD,4CAAwE;AACxE,+BAAoC;AACpC,6CAA0C;AAC1C,uDAA4D;AAsB5D;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,SAAgB,SAAS,CAAkB,SAAiB,EAAE,EAAqD,EAAE,OAA0B;IAC7I,MAAM,eAAe,GAAG,KAAK,WAAsB,KAAY;QAC7D,MAAM,MAAM,GAAG,WAAK,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;QACxC,MAAM,KAAK,GAAG,IAAA,SAAM,GAAE,CAAC;QAEvB,MAAM,IAAI,GAAG,MAAM,CAAC,SAAS,CAAC,cAAc,EAAE;YAC5C,UAAU,EAAE;gBACV,eAAe,EAAE,SAAS;gBAC1B,cAAc,EAAE,KAAK;gBACrB,qBAAqB,EAAE,IAAA,yCAAuB,GAAE,IAAI,OAAO,EAAE,YAAY,KAAK,IAAI;aACnF;SACF,EAAE,kBAAY,CAAC,CAAC;QAEjB,IAAI,CAAC,YAAY,CAAC,gBAAgB,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,CAAC;QAErE,IAAA,uBAAU,EAAC,eAAe,EAAE,cAAc,EAAE,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,CAAC;QAElE,MAAM,GAAG,GAAG,WAAK,CAAC,OAAO,CAAC,kBAAY,EAAE,IAAI,CAAC,CAAC;QAC9C,IAAI,SAAS,GAAG,KAAK,CAAC;QAEtB,IAAI,CAAC;YACH,OAAO,MA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