@uselemma/tracing 2.7.0 → 2.9.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/README.md CHANGED
@@ -33,7 +33,6 @@ enableExperimentMode();
33
33
  `wrapAgent` creates a root OpenTelemetry span named `ai.agent.run` and records:
34
34
  - `ai.agent.name`
35
35
  - `lemma.run_id`
36
- - `ai.agent.input`
37
36
  - `lemma.is_experiment`
38
37
 
39
38
  ```typescript
@@ -5,8 +5,9 @@ export type TraceContext = {
5
5
  /** Unique identifier for this agent run. */
6
6
  runId: string;
7
7
  /**
8
- * Record output and complete the run when `autoEndRoot` is disabled.
9
- * Returns `true` when this call ends the top-level span.
8
+ * Signal the run is complete. When `autoEndRoot` is enabled (the default),
9
+ * this is a no-op and returns `false` the span is ended automatically.
10
+ * When `autoEndRoot` is disabled, this ends the top-level span and returns `true`.
10
11
  */
11
12
  onComplete: (result: unknown) => boolean;
12
13
  /** Record an error on the span. Marks the span as errored. */
@@ -16,8 +17,9 @@ export type WrapAgentOptions = {
16
17
  /** Mark this run as an experiment in Lemma. */
17
18
  isExperiment?: boolean;
18
19
  /**
19
- * If `true`, the run processor will automatically end the top-level span
20
- * when all direct child spans have ended.
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`.
21
23
  */
22
24
  autoEndRoot?: boolean;
23
25
  };
@@ -26,9 +28,7 @@ export type WrapAgentOptions = {
26
28
  * a span for the agent run and providing a `TraceContext` to the wrapped function.
27
29
  *
28
30
  * The returned function creates a new root span on every invocation, attaches
29
- * agent metadata (name, run ID, input, experiment flag), and handles error recording.
30
- * The `input` passed to the returned function is recorded as the agent's initial
31
- * state on the span.
31
+ * agent metadata (name, run ID, experiment flag), and handles error recording.
32
32
  *
33
33
  * @example
34
34
  * const myAgent = wrapAgent<{ topic: string }>(
@@ -44,7 +44,7 @@ export type WrapAgentOptions = {
44
44
  * @param fn - The agent function to wrap. Receives a {@link TraceContext} as its first argument and the call-time input as its second.
45
45
  * @param options - Configuration for the agent trace.
46
46
  * @param options.isExperiment - Mark this run as an experiment in Lemma.
47
- * @param options.autoEndRoot - Enable processor-driven automatic ending of the top-level span after direct children have ended.
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
48
  * @returns An async function that accepts an `input`, executes `fn` inside a traced context, and returns `{ result, runId, span }`.
49
49
  */
50
50
  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;AAIxE,MAAM,MAAM,YAAY,GAAG;IACzB,wDAAwD;IACxD,IAAI,EAAE,IAAI,CAAC;IACX,4CAA4C;IAC5C,KAAK,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,UAAU,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,OAAO,CAAC;IACzC,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;IACvB;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;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;;;;GAkDhE"}
1
+ {"version":3,"file":"trace-wrapper.d.ts","sourceRoot":"","sources":["../src/trace-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgC,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAIxE,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,OAAO,CAAC;IACzC,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;IACvB;;;;OAIG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;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;;;;GA4DhE"}
@@ -9,9 +9,7 @@ const experiment_mode_1 = require("./experiment-mode");
9
9
  * a span for the agent run and providing a `TraceContext` to the wrapped function.
10
10
  *
11
11
  * The returned function creates a new root span on every invocation, attaches
12
- * agent metadata (name, run ID, input, experiment flag), and handles error recording.
13
- * The `input` passed to the returned function is recorded as the agent's initial
14
- * state on the span.
12
+ * agent metadata (name, run ID, experiment flag), and handles error recording.
15
13
  *
16
14
  * @example
17
15
  * const myAgent = wrapAgent<{ topic: string }>(
@@ -27,7 +25,7 @@ const experiment_mode_1 = require("./experiment-mode");
27
25
  * @param fn - The agent function to wrap. Receives a {@link TraceContext} as its first argument and the call-time input as its second.
28
26
  * @param options - Configuration for the agent trace.
29
27
  * @param options.isExperiment - Mark this run as an experiment in Lemma.
30
- * @param options.autoEndRoot - Enable processor-driven automatic ending of the top-level span after direct children have ended.
28
+ * @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`.
31
29
  * @returns An async function that accepts an `input`, executes `fn` inside a traced context, and returns `{ result, runId, span }`.
32
30
  */
33
31
  function wrapAgent(agentName, fn, options) {
@@ -36,41 +34,49 @@ function wrapAgent(agentName, fn, options) {
36
34
  const tracer = api_1.trace.getTracer("lemma");
37
35
  // Generate a unique run ID and open a new span for this agent invocation
38
36
  const runId = (0, uuid_1.v4)();
37
+ const autoEndRoot = options?.autoEndRoot !== false; // default true
39
38
  const span = tracer.startSpan("ai.agent.run", {
40
39
  attributes: {
41
40
  "ai.agent.name": agentName,
42
41
  "lemma.run_id": runId,
43
- "ai.agent.input": JSON.stringify(input),
44
42
  "lemma.is_experiment": (0, experiment_mode_1.isExperimentModeEnabled)() || options?.isExperiment === true,
45
- "lemma.auto_end_root": options?.autoEndRoot === true,
43
+ "lemma.auto_end_root": autoEndRoot,
46
44
  },
47
45
  }, api_1.ROOT_CONTEXT);
48
46
  // Propagate the span as the active context so child spans are nested correctly
49
47
  const ctx = api_1.trace.setSpan(api_1.ROOT_CONTEXT, span);
50
- const autoEndRoot = options?.autoEndRoot === true;
51
48
  let rootEnded = false;
52
49
  try {
53
50
  return await api_1.context.with(ctx, async () => {
54
- const onComplete = (result) => {
55
- span.setAttribute("ai.agent.output", JSON.stringify(result));
56
- if (autoEndRoot || rootEnded)
57
- return false;
58
- rootEnded = true;
59
- span.end();
60
- return true;
51
+ const onComplete = (_result) => {
52
+ if (!autoEndRoot && !rootEnded) {
53
+ rootEnded = true;
54
+ span.end();
55
+ return true;
56
+ }
57
+ return false;
61
58
  };
62
59
  const recordError = (error) => {
63
60
  span.recordException(error instanceof Error ? error : new Error(String(error)));
64
61
  span.setStatus({ code: 2 }); // SpanStatusCode.ERROR
65
62
  };
66
63
  const result = await fn.call(this, { span, runId, onComplete, recordError }, input);
64
+ // Auto-end the span if autoEndRoot is enabled and onComplete hasn't ended it yet
65
+ if (autoEndRoot && !rootEnded) {
66
+ rootEnded = true;
67
+ span.end();
68
+ }
67
69
  return { result, runId, span };
68
70
  });
69
71
  }
70
72
  catch (err) {
71
- // Record the exception on the span and mark it as errored
73
+ // Record the exception on the span, mark it as errored, and end it
72
74
  span.recordException(err);
73
75
  span.setStatus({ code: 2 }); // SpanStatusCode.ERROR
76
+ if (!rootEnded) {
77
+ rootEnded = true;
78
+ span.end();
79
+ }
74
80
  throw err;
75
81
  }
76
82
  };
@@ -1 +1 @@
1
- {"version":3,"file":"trace-wrapper.js","sourceRoot":"","sources":["../src/trace-wrapper.ts"],"names":[],"mappings":";;AAsDA,8BAmDC;AAzGD,4CAAwE;AACxE,+BAAoC;AACpC,uDAA4D;AA0B5D;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,SAAgB,SAAS,CAAkB,SAAiB,EAAE,EAAqD,EAAE,OAA0B;IAC7I,MAAM,eAAe,GAAG,KAAK,WAAsB,KAAY;QAC7D,wDAAwD;QACxD,MAAM,MAAM,GAAG,WAAK,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;QAExC,yEAAyE;QACzE,MAAM,KAAK,GAAG,IAAA,SAAM,GAAE,CAAC;QACvB,MAAM,IAAI,GAAG,MAAM,CAAC,SAAS,CAAC,cAAc,EAAE;YAC5C,UAAU,EAAE;gBACV,eAAe,EAAE,SAAS;gBAC1B,cAAc,EAAE,KAAK;gBACrB,gBAAgB,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC;gBACvC,qBAAqB,EAAE,IAAA,yCAAuB,GAAE,IAAI,OAAO,EAAE,YAAY,KAAK,IAAI;gBAClF,qBAAqB,EAAE,OAAO,EAAE,WAAW,KAAK,IAAI;aACrD;SACF,EAAE,kBAAY,CAAC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CAAC;IACH,CAAC,CAAC;IAEF,OAAO,eAAe,CAAC;AACzB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uselemma/tracing",
3
- "version": "2.7.0",
3
+ "version": "2.9.0",
4
4
  "description": "OpenTelemetry-based tracing module for Lemma",
5
5
  "license": "MIT",
6
6
  "author": "Lemma",
@@ -41,15 +41,21 @@
41
41
  "uuid": "^13.0.0"
42
42
  },
43
43
  "devDependencies": {
44
+ "@arizeai/openinference-instrumentation-openai": "^4.0.5",
45
+ "@opentelemetry/instrumentation": "^0.213.0",
44
46
  "@trivago/prettier-plugin-sort-imports": "^5.2.2",
45
47
  "@types/node": "^22.18.8",
48
+ "@vitest/coverage-v8": "^3.2.4",
49
+ "openai": "^6.26.0",
46
50
  "prettier": "^3.6.2",
47
51
  "prettier-plugin-tailwindcss": "^0.6.14",
48
- "typescript": "^5.9.3"
52
+ "typescript": "^5.9.3",
53
+ "vitest": "^3.2.4"
49
54
  },
50
55
  "scripts": {
51
56
  "type-check": "tsc --noEmit",
52
57
  "build": "tsc",
53
- "clean": "rm -rf dist"
58
+ "clean": "rm -rf dist",
59
+ "test": "vitest run"
54
60
  }
55
61
  }