trodo-node 1.2.0 → 2.2.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.
Files changed (64) hide show
  1. package/README.md +182 -2
  2. package/dist/cjs/TrodoClient.js +85 -102
  3. package/dist/cjs/TrodoClient.js.map +1 -1
  4. package/dist/cjs/api/ApiClient.js +20 -2
  5. package/dist/cjs/api/ApiClient.js.map +1 -1
  6. package/dist/cjs/api/endpoints.js +7 -1
  7. package/dist/cjs/api/endpoints.js.map +1 -1
  8. package/dist/cjs/index.js +115 -29
  9. package/dist/cjs/index.js.map +1 -1
  10. package/dist/cjs/otel/autoInstrument.js +253 -0
  11. package/dist/cjs/otel/autoInstrument.js.map +1 -0
  12. package/dist/cjs/otel/context.js +49 -0
  13. package/dist/cjs/otel/context.js.map +1 -0
  14. package/dist/cjs/otel/helpers.js +254 -0
  15. package/dist/cjs/otel/helpers.js.map +1 -0
  16. package/dist/cjs/otel/processor.js +153 -0
  17. package/dist/cjs/otel/processor.js.map +1 -0
  18. package/dist/cjs/otel/uuid.js +24 -0
  19. package/dist/cjs/otel/uuid.js.map +1 -0
  20. package/dist/cjs/otel/wrapAgent.js +460 -0
  21. package/dist/cjs/otel/wrapAgent.js.map +1 -0
  22. package/dist/esm/TrodoClient.d.ts +44 -11
  23. package/dist/esm/TrodoClient.d.ts.map +1 -1
  24. package/dist/esm/TrodoClient.js +85 -102
  25. package/dist/esm/TrodoClient.js.map +1 -1
  26. package/dist/esm/api/ApiClient.d.ts +9 -1
  27. package/dist/esm/api/ApiClient.d.ts.map +1 -1
  28. package/dist/esm/api/ApiClient.js +20 -2
  29. package/dist/esm/api/ApiClient.js.map +1 -1
  30. package/dist/esm/api/endpoints.d.ts +6 -1
  31. package/dist/esm/api/endpoints.d.ts.map +1 -1
  32. package/dist/esm/api/endpoints.js +7 -1
  33. package/dist/esm/api/endpoints.js.map +1 -1
  34. package/dist/esm/index.d.ts +96 -19
  35. package/dist/esm/index.d.ts.map +1 -1
  36. package/dist/esm/index.js +98 -23
  37. package/dist/esm/index.js.map +1 -1
  38. package/dist/esm/otel/autoInstrument.d.ts +61 -0
  39. package/dist/esm/otel/autoInstrument.d.ts.map +1 -0
  40. package/dist/esm/otel/autoInstrument.js +248 -0
  41. package/dist/esm/otel/autoInstrument.js.map +1 -0
  42. package/dist/esm/otel/context.d.ts +26 -0
  43. package/dist/esm/otel/context.d.ts.map +1 -0
  44. package/dist/esm/otel/context.js +44 -0
  45. package/dist/esm/otel/context.js.map +1 -0
  46. package/dist/esm/otel/helpers.d.ts +119 -0
  47. package/dist/esm/otel/helpers.d.ts.map +1 -0
  48. package/dist/esm/otel/helpers.js +244 -0
  49. package/dist/esm/otel/helpers.js.map +1 -0
  50. package/dist/esm/otel/processor.d.ts +104 -0
  51. package/dist/esm/otel/processor.d.ts.map +1 -0
  52. package/dist/esm/otel/processor.js +149 -0
  53. package/dist/esm/otel/processor.js.map +1 -0
  54. package/dist/esm/otel/uuid.d.ts +7 -0
  55. package/dist/esm/otel/uuid.d.ts.map +1 -0
  56. package/dist/esm/otel/uuid.js +21 -0
  57. package/dist/esm/otel/uuid.js.map +1 -0
  58. package/dist/esm/otel/wrapAgent.d.ts +133 -0
  59. package/dist/esm/otel/wrapAgent.d.ts.map +1 -0
  60. package/dist/esm/otel/wrapAgent.js +448 -0
  61. package/dist/esm/otel/wrapAgent.js.map +1 -0
  62. package/dist/esm/types/index.d.ts +24 -48
  63. package/dist/esm/types/index.d.ts.map +1 -1
  64. package/package.json +1 -1
@@ -0,0 +1,21 @@
1
+ /**
2
+ * RFC4122 v4 UUIDs — used for run_id, span_id, parent_span_id.
3
+ * Uses the runtime's crypto.randomUUID when available, falls back to a
4
+ * Math.random-based generator so we don't pull in a dependency for tests.
5
+ */
6
+ export function uuidv4() {
7
+ try {
8
+ const c = globalThis.crypto;
9
+ if (c?.randomUUID)
10
+ return c.randomUUID();
11
+ }
12
+ catch {
13
+ /* fall through */
14
+ }
15
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (ch) => {
16
+ const r = (Math.random() * 16) | 0;
17
+ const v = ch === 'x' ? r : (r & 0x3) | 0x8;
18
+ return v.toString(16);
19
+ });
20
+ }
21
+ //# sourceMappingURL=uuid.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuid.js","sourceRoot":"","sources":["../../../src/otel/uuid.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,MAAM;IACpB,IAAI,CAAC;QACH,MAAM,CAAC,GAAI,UAAyD,CAAC,MAAM,CAAC;QAC5E,IAAI,CAAC,EAAE,UAAU;YAAE,OAAO,CAAC,CAAC,UAAU,EAAE,CAAC;IAC3C,CAAC;IAAC,MAAM,CAAC;QACP,kBAAkB;IACpB,CAAC;IACD,OAAO,sCAAsC,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,EAAE,EAAE,EAAE;QACpE,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC;QACnC,MAAM,CAAC,GAAG,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC;QAC3C,OAAO,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IACxB,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,133 @@
1
+ import type { TrodoSpanProcessor, TrodoSpan } from './processor.js';
2
+ export interface WrapAgentOptions {
3
+ distinctId?: string | null;
4
+ conversationId?: string | null;
5
+ parentRunId?: string | null;
6
+ metadata?: Record<string, unknown>;
7
+ }
8
+ export interface WrapAgentResult<R> {
9
+ result: R;
10
+ runId: string;
11
+ }
12
+ export interface WithSpanOptions {
13
+ kind?: TrodoSpan['kind'];
14
+ input?: unknown;
15
+ attributes?: Record<string, unknown>;
16
+ }
17
+ export interface JoinRunOptions {
18
+ name?: string;
19
+ kind?: TrodoSpan['kind'];
20
+ input?: unknown;
21
+ attributes?: Record<string, unknown>;
22
+ }
23
+ /** Return the currently active run_id, if any. */
24
+ export declare function currentRunId(): string | null;
25
+ /** Return the currently active span_id, if any. */
26
+ export declare function currentSpanId(): string | null;
27
+ /**
28
+ * RunHandle — returned by wrapAgent so callers can set input/output after
29
+ * inspecting the arguments / result. Mirrors the Python RunHandle API.
30
+ */
31
+ export declare class RunHandle {
32
+ readonly runId: string;
33
+ readonly agentName: string;
34
+ input?: string;
35
+ output?: string;
36
+ metadata: Record<string, unknown>;
37
+ constructor(runId: string, agentName: string);
38
+ setInput(value: unknown): void;
39
+ setOutput(value: unknown): void;
40
+ setMetadata(extra: Record<string, unknown>): void;
41
+ }
42
+ /**
43
+ * SpanHandle — returned by withSpan / joinRun. Collect LLM model/token/cost
44
+ * info and tool name here; the processor picks them up at span close.
45
+ */
46
+ export declare class SpanHandle {
47
+ readonly spanId: string;
48
+ readonly name: string;
49
+ input?: string;
50
+ output?: string;
51
+ attributes: Record<string, unknown>;
52
+ model?: string;
53
+ provider?: string;
54
+ inputTokens?: number;
55
+ outputTokens?: number;
56
+ cost?: number;
57
+ temperature?: number;
58
+ toolName?: string;
59
+ constructor(spanId: string, name: string);
60
+ setInput(value: unknown): void;
61
+ setOutput(value: unknown): void;
62
+ setAttribute(key: string, value: unknown): void;
63
+ setLlm(opts: {
64
+ model?: string;
65
+ provider?: string;
66
+ inputTokens?: number;
67
+ outputTokens?: number;
68
+ cost?: number;
69
+ temperature?: number;
70
+ }): void;
71
+ setTool(name: string): void;
72
+ }
73
+ export interface StartRunOptions {
74
+ runId?: string;
75
+ distinctId?: string | null;
76
+ conversationId?: string | null;
77
+ parentRunId?: string | null;
78
+ metadata?: Record<string, unknown>;
79
+ input?: unknown;
80
+ }
81
+ export interface EndRunOptions {
82
+ output?: unknown;
83
+ status?: 'ok' | 'error';
84
+ errorSummary?: string;
85
+ metadata?: Record<string, unknown>;
86
+ }
87
+ /**
88
+ * Open a Run record without holding a context manager.
89
+ *
90
+ * Pairs with {@link endRun} for sessions that span multiple processes or HTTP
91
+ * requests (e.g. an MCP server where ``initialize`` opens a run and later
92
+ * ``tools/call`` requests append spans before a final close).
93
+ *
94
+ * Returns the runId (caller-supplied or freshly minted). Between startRun and
95
+ * endRun any process can use ``joinRun(runId, ...)`` to add spans — they flush
96
+ * incrementally via append_spans.
97
+ */
98
+ export declare function startRun(processor: TrodoSpanProcessor, agentName: string, options?: StartRunOptions): Promise<string>;
99
+ /**
100
+ * Finalise a Run opened by {@link startRun}.
101
+ *
102
+ * Aggregates any locally-buffered spans for `runId`, POSTs to
103
+ * `/runs/{id}/end`, and unmarks the run as joined.
104
+ */
105
+ export declare function endRun(processor: TrodoSpanProcessor, runId: string, options?: EndRunOptions): Promise<void>;
106
+ /**
107
+ * Wrap an async function as an agent run. Every OTel span (or withSpan call)
108
+ * created inside `fn` is auto-nested under the run via AsyncLocalStorage.
109
+ *
110
+ * The callback receives a RunHandle so it can call `handle.setInput(...)` /
111
+ * `handle.setOutput(...)`. Old-style callbacks (no arg) still work.
112
+ */
113
+ export declare function wrapAgent<R>(processor: TrodoSpanProcessor, teamSiteId: string, agentName: string, fn: (handle: RunHandle) => Promise<R> | R, options?: WrapAgentOptions): Promise<WrapAgentResult<R>>;
114
+ /**
115
+ * Create a nested span for non-auto-instrumented work (KB lookups, custom
116
+ * tools, computation). Requires an enclosing wrapAgent/joinRun.
117
+ *
118
+ * The callback receives a SpanHandle so the caller can record LLM tokens /
119
+ * tool name / output inside the function body.
120
+ */
121
+ export declare function withSpan<R>(_processor: TrodoSpanProcessor, name: string, fn: (handle: SpanHandle) => Promise<R> | R, options?: WithSpanOptions): Promise<R>;
122
+ /**
123
+ * Join an existing agent run owned by a remote service. Opens a SPAN (not a
124
+ * run) under ``parentSpanId``; every span produced inside ``fn`` — including
125
+ * OTel auto-instrumented LLM spans — flushes to the backend as a child of
126
+ * the caller's run via append_spans. We never ingest a run here.
127
+ *
128
+ * Typical use: Express/FastAPI middleware detects ``X-Trodo-Run-Id`` +
129
+ * ``X-Trodo-Parent-Span-Id`` on an inbound request and wraps the handler
130
+ * with joinRun so the handler's work nests under the caller's run.
131
+ */
132
+ export declare function joinRun<R>(processor: TrodoSpanProcessor, teamSiteId: string, runId: string, parentSpanId: string | null, fn: (handle: SpanHandle) => Promise<R> | R, options?: JoinRunOptions): Promise<R>;
133
+ //# sourceMappingURL=wrapAgent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wrapAgent.d.ts","sourceRoot":"","sources":["../../../src/otel/wrapAgent.ts"],"names":[],"mappings":"AAmBA,OAAO,KAAK,EAAE,kBAAkB,EAAE,SAAS,EAAY,MAAM,gBAAgB,CAAC;AAE9E,MAAM,WAAW,gBAAgB;IAC/B,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED,MAAM,WAAW,eAAe,CAAC,CAAC;IAChC,MAAM,EAAE,CAAC,CAAC;IACV,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,eAAe;IAC9B,IAAI,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;IACzB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACtC;AAED,MAAM,WAAW,cAAc;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;IACzB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACt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E,aAAkB,GAC1B,OAAO,CAAC,IAAI,CAAC,CAwBf;AAED;;;;;;GAMG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAC/B,SAAS,EAAE,kBAAkB,EAC7B,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,EAAE,EAAE,CAAC,MAAM,EAAE,SAAS,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,EACzC,OAAO,GAAE,gBAAqB,GAC7B,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CA6E7B;AAED;;;;;;GAMG;AACH,wBAAsB,QAAQ,CAAC,CAAC,EAC9B,UAAU,EAAE,kBAAkB,EAC9B,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,CAAC,MAAM,EAAE,UAAU,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,EAC1C,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,CAAC,CAAC,CAkFZ;AAED;;;;;;;;;GASG;AACH,wBAAsB,OAAO,CAAC,CAAC,EAC7B,SAAS,EAAE,kBAAkB,EAC7B,UAAU,EAAE,MAAM,EAClB,KAAK,EAAE,MAAM,EACb,YAAY,EAAE,MAAM,GAAG,IAAI,EAC3B,EAAE,EAAE,CAAC,MAAM,EAAE,UAAU,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,EAC1C,OAAO,GAAE,cAAmB,GAC3B,OAAO,CAAC,CAAC,CAAC,CAiFZ"}
@@ -0,0 +1,448 @@
1
+ /**
2
+ * wrapAgent / withSpan / joinRun — user-facing surface for agent tracking.
3
+ *
4
+ * Users never generate IDs. The SDK creates run_id on wrapAgent, threads a
5
+ * span_id through AsyncLocalStorage, and any nested withSpan (or OTel
6
+ * auto-instrumented span from Anthropic/OpenAI/Vercel AI SDK/LangChain)
7
+ * inherits the current span as its parent.
8
+ *
9
+ * joinRun is for downstream microservices: opens a SPAN (not a run) in the
10
+ * context of the caller's run_id, flushing each span to the backend via
11
+ * append_spans. The originating service owns run lifecycle.
12
+ */
13
+ import { uuidv4 } from './uuid.js';
14
+ import { getActiveContext, runWithContextAsync, } from './context.js';
15
+ function nowIso() {
16
+ return new Date().toISOString();
17
+ }
18
+ function truncate(value, max = 64000) {
19
+ if (value == null)
20
+ return undefined;
21
+ const str = typeof value === 'string' ? value : safeJson(value);
22
+ return str.length > max ? str.slice(0, max) : str;
23
+ }
24
+ function safeJson(v) {
25
+ try {
26
+ return JSON.stringify(v);
27
+ }
28
+ catch {
29
+ return String(v);
30
+ }
31
+ }
32
+ /** Return the currently active run_id, if any. */
33
+ export function currentRunId() {
34
+ return getActiveContext()?.runId ?? null;
35
+ }
36
+ /** Return the currently active span_id, if any. */
37
+ export function currentSpanId() {
38
+ return getActiveContext()?.spanId ?? null;
39
+ }
40
+ function aggregate(spans) {
41
+ let totalIn = 0;
42
+ let totalOut = 0;
43
+ let totalCost = 0;
44
+ let toolCount = 0;
45
+ let errorCount = 0;
46
+ for (const s of spans) {
47
+ if (s.input_tokens)
48
+ totalIn += Number(s.input_tokens);
49
+ if (s.output_tokens)
50
+ totalOut += Number(s.output_tokens);
51
+ if (s.cost)
52
+ totalCost += Number(s.cost);
53
+ if (s.kind === 'tool')
54
+ toolCount += 1;
55
+ if (s.status === 'error')
56
+ errorCount += 1;
57
+ }
58
+ return {
59
+ total_tokens_in: totalIn,
60
+ total_tokens_out: totalOut,
61
+ total_cost: totalCost ? Math.round(totalCost * 1e8) / 1e8 : 0,
62
+ span_count: spans.length,
63
+ tool_count: toolCount,
64
+ error_count: errorCount,
65
+ };
66
+ }
67
+ /**
68
+ * RunHandle — returned by wrapAgent so callers can set input/output after
69
+ * inspecting the arguments / result. Mirrors the Python RunHandle API.
70
+ */
71
+ export class RunHandle {
72
+ constructor(runId, agentName) {
73
+ this.metadata = {};
74
+ this.runId = runId;
75
+ this.agentName = agentName;
76
+ }
77
+ setInput(value) {
78
+ this.input = truncate(value);
79
+ }
80
+ setOutput(value) {
81
+ this.output = truncate(value);
82
+ }
83
+ setMetadata(extra) {
84
+ Object.assign(this.metadata, extra);
85
+ }
86
+ }
87
+ /**
88
+ * SpanHandle — returned by withSpan / joinRun. Collect LLM model/token/cost
89
+ * info and tool name here; the processor picks them up at span close.
90
+ */
91
+ export class SpanHandle {
92
+ constructor(spanId, name) {
93
+ this.attributes = {};
94
+ this.spanId = spanId;
95
+ this.name = name;
96
+ }
97
+ setInput(value) {
98
+ this.input = truncate(value);
99
+ }
100
+ setOutput(value) {
101
+ this.output = truncate(value);
102
+ }
103
+ setAttribute(key, value) {
104
+ this.attributes[key] = value;
105
+ }
106
+ setLlm(opts) {
107
+ if (opts.model !== undefined)
108
+ this.model = opts.model;
109
+ if (opts.provider !== undefined)
110
+ this.provider = opts.provider;
111
+ if (opts.inputTokens !== undefined)
112
+ this.inputTokens = Number(opts.inputTokens);
113
+ if (opts.outputTokens !== undefined)
114
+ this.outputTokens = Number(opts.outputTokens);
115
+ if (opts.cost !== undefined)
116
+ this.cost = Number(opts.cost);
117
+ if (opts.temperature !== undefined)
118
+ this.temperature = Number(opts.temperature);
119
+ }
120
+ setTool(name) {
121
+ this.toolName = name;
122
+ }
123
+ }
124
+ /**
125
+ * Open a Run record without holding a context manager.
126
+ *
127
+ * Pairs with {@link endRun} for sessions that span multiple processes or HTTP
128
+ * requests (e.g. an MCP server where ``initialize`` opens a run and later
129
+ * ``tools/call`` requests append spans before a final close).
130
+ *
131
+ * Returns the runId (caller-supplied or freshly minted). Between startRun and
132
+ * endRun any process can use ``joinRun(runId, ...)`` to add spans — they flush
133
+ * incrementally via append_spans.
134
+ */
135
+ export async function startRun(processor, agentName, options = {}) {
136
+ const runId = options.runId || uuidv4();
137
+ const run = {
138
+ run_id: runId,
139
+ agent_name: agentName,
140
+ distinct_id: options.distinctId ?? null,
141
+ conversation_id: options.conversationId ?? null,
142
+ parent_run_id: options.parentRunId ?? null,
143
+ status: 'running',
144
+ input: truncate(options.input),
145
+ started_at: nowIso(),
146
+ metadata: options.metadata,
147
+ };
148
+ processor.markJoined(runId);
149
+ await processor.startRun(run);
150
+ return runId;
151
+ }
152
+ /**
153
+ * Finalise a Run opened by {@link startRun}.
154
+ *
155
+ * Aggregates any locally-buffered spans for `runId`, POSTs to
156
+ * `/runs/{id}/end`, and unmarks the run as joined.
157
+ */
158
+ export async function endRun(processor, runId, options = {}) {
159
+ const pending = processor.getPending(runId);
160
+ const agg = aggregate(pending);
161
+ const payload = {
162
+ ended_at: nowIso(),
163
+ status: options.status ?? 'ok',
164
+ total_tokens_in: agg.total_tokens_in,
165
+ total_tokens_out: agg.total_tokens_out,
166
+ total_cost: agg.total_cost,
167
+ span_count: agg.span_count,
168
+ tool_count: agg.tool_count,
169
+ error_count: agg.error_count,
170
+ };
171
+ if (options.output !== undefined) {
172
+ payload.output = truncate(options.output);
173
+ }
174
+ if (options.errorSummary !== undefined) {
175
+ payload.error_summary = truncate(options.errorSummary, 4000);
176
+ }
177
+ if (options.metadata !== undefined) {
178
+ payload.metadata = options.metadata;
179
+ }
180
+ await processor.endRun(runId, payload);
181
+ processor.unmarkJoined(runId);
182
+ }
183
+ /**
184
+ * Wrap an async function as an agent run. Every OTel span (or withSpan call)
185
+ * created inside `fn` is auto-nested under the run via AsyncLocalStorage.
186
+ *
187
+ * The callback receives a RunHandle so it can call `handle.setInput(...)` /
188
+ * `handle.setOutput(...)`. Old-style callbacks (no arg) still work.
189
+ */
190
+ export async function wrapAgent(processor, teamSiteId, agentName, fn, options = {}) {
191
+ const runId = uuidv4();
192
+ const rootSpanId = uuidv4();
193
+ const startedAt = nowIso();
194
+ const startedMs = Date.now();
195
+ const handle = new RunHandle(runId, agentName);
196
+ const ctx = {
197
+ runId,
198
+ spanId: rootSpanId,
199
+ parentSpanId: null,
200
+ teamSiteId,
201
+ processor,
202
+ };
203
+ let status = 'ok';
204
+ let output;
205
+ let errorSummary;
206
+ let result;
207
+ try {
208
+ result = await runWithContextAsync(ctx, async () => fn(handle));
209
+ output = handle.output ?? truncate(result);
210
+ }
211
+ catch (err) {
212
+ status = 'error';
213
+ errorSummary = truncate(err?.message ?? String(err), 4000);
214
+ const pending = processor.getPending(runId);
215
+ const agg = aggregate(pending);
216
+ const run = {
217
+ run_id: runId,
218
+ agent_name: agentName,
219
+ distinct_id: options.distinctId ?? null,
220
+ conversation_id: options.conversationId ?? null,
221
+ parent_run_id: options.parentRunId ?? null,
222
+ status,
223
+ input: handle.input,
224
+ started_at: startedAt,
225
+ ended_at: nowIso(),
226
+ duration_ms: Date.now() - startedMs,
227
+ error_summary: errorSummary,
228
+ metadata: { ...(options.metadata || {}), ...handle.metadata },
229
+ total_tokens_in: agg.total_tokens_in,
230
+ total_tokens_out: agg.total_tokens_out,
231
+ total_cost: agg.total_cost,
232
+ span_count: agg.span_count,
233
+ tool_count: agg.tool_count,
234
+ error_count: agg.error_count,
235
+ };
236
+ await processor.ingestRun(run);
237
+ throw err;
238
+ }
239
+ const pending = processor.getPending(runId);
240
+ const agg = aggregate(pending);
241
+ const run = {
242
+ run_id: runId,
243
+ agent_name: agentName,
244
+ distinct_id: options.distinctId ?? null,
245
+ conversation_id: options.conversationId ?? null,
246
+ parent_run_id: options.parentRunId ?? null,
247
+ status,
248
+ input: handle.input,
249
+ output,
250
+ started_at: startedAt,
251
+ ended_at: nowIso(),
252
+ duration_ms: Date.now() - startedMs,
253
+ metadata: { ...(options.metadata || {}), ...handle.metadata },
254
+ total_tokens_in: agg.total_tokens_in,
255
+ total_tokens_out: agg.total_tokens_out,
256
+ total_cost: agg.total_cost,
257
+ span_count: agg.span_count,
258
+ tool_count: agg.tool_count,
259
+ error_count: agg.error_count,
260
+ };
261
+ await processor.ingestRun(run);
262
+ return { result, runId };
263
+ }
264
+ /**
265
+ * Create a nested span for non-auto-instrumented work (KB lookups, custom
266
+ * tools, computation). Requires an enclosing wrapAgent/joinRun.
267
+ *
268
+ * The callback receives a SpanHandle so the caller can record LLM tokens /
269
+ * tool name / output inside the function body.
270
+ */
271
+ export async function withSpan(_processor, name, fn, options = {}) {
272
+ const active = getActiveContext();
273
+ const spanId = uuidv4();
274
+ const handle = new SpanHandle(spanId, name);
275
+ if (options.input !== undefined)
276
+ handle.setInput(options.input);
277
+ if (options.attributes)
278
+ Object.assign(handle.attributes, options.attributes);
279
+ if (!active) {
280
+ // Outside of a run — execute but skip tracking (don't crash user code).
281
+ return Promise.resolve(fn(handle));
282
+ }
283
+ const startedAt = nowIso();
284
+ const startedMs = Date.now();
285
+ const child = {
286
+ runId: active.runId,
287
+ spanId,
288
+ parentSpanId: active.spanId,
289
+ teamSiteId: active.teamSiteId,
290
+ processor: active.processor,
291
+ };
292
+ let status = 'ok';
293
+ let errorType;
294
+ let errorMessage;
295
+ try {
296
+ const result = await runWithContextAsync(child, async () => fn(handle));
297
+ if (handle.output === undefined)
298
+ handle.setOutput(result);
299
+ const span = {
300
+ span_id: spanId,
301
+ run_id: active.runId,
302
+ parent_span_id: active.spanId,
303
+ kind: options.kind || 'generic',
304
+ name,
305
+ status,
306
+ started_at: startedAt,
307
+ ended_at: nowIso(),
308
+ duration_ms: Date.now() - startedMs,
309
+ input: handle.input,
310
+ output: handle.output,
311
+ model: handle.model,
312
+ provider: handle.provider,
313
+ input_tokens: handle.inputTokens,
314
+ output_tokens: handle.outputTokens,
315
+ cost: handle.cost,
316
+ temperature: handle.temperature,
317
+ tool_name: handle.toolName,
318
+ attributes: Object.keys(handle.attributes).length ? handle.attributes : undefined,
319
+ };
320
+ active.processor.enqueueSpan(span);
321
+ return result;
322
+ }
323
+ catch (err) {
324
+ status = 'error';
325
+ errorType = err?.name || 'Error';
326
+ errorMessage = truncate(err?.message ?? String(err), 4000);
327
+ const span = {
328
+ span_id: spanId,
329
+ run_id: active.runId,
330
+ parent_span_id: active.spanId,
331
+ kind: options.kind || 'generic',
332
+ name,
333
+ status,
334
+ started_at: startedAt,
335
+ ended_at: nowIso(),
336
+ duration_ms: Date.now() - startedMs,
337
+ input: handle.input,
338
+ output: handle.output,
339
+ error_type: errorType,
340
+ error_message: errorMessage,
341
+ model: handle.model,
342
+ provider: handle.provider,
343
+ input_tokens: handle.inputTokens,
344
+ output_tokens: handle.outputTokens,
345
+ cost: handle.cost,
346
+ temperature: handle.temperature,
347
+ tool_name: handle.toolName,
348
+ attributes: Object.keys(handle.attributes).length ? handle.attributes : undefined,
349
+ };
350
+ active.processor.enqueueSpan(span);
351
+ throw err;
352
+ }
353
+ }
354
+ /**
355
+ * Join an existing agent run owned by a remote service. Opens a SPAN (not a
356
+ * run) under ``parentSpanId``; every span produced inside ``fn`` — including
357
+ * OTel auto-instrumented LLM spans — flushes to the backend as a child of
358
+ * the caller's run via append_spans. We never ingest a run here.
359
+ *
360
+ * Typical use: Express/FastAPI middleware detects ``X-Trodo-Run-Id`` +
361
+ * ``X-Trodo-Parent-Span-Id`` on an inbound request and wraps the handler
362
+ * with joinRun so the handler's work nests under the caller's run.
363
+ */
364
+ export async function joinRun(processor, teamSiteId, runId, parentSpanId, fn, options = {}) {
365
+ const spanId = uuidv4();
366
+ const name = options.name || 'remote.handler';
367
+ const kind = options.kind || 'agent';
368
+ const handle = new SpanHandle(spanId, name);
369
+ if (options.input !== undefined)
370
+ handle.setInput(options.input);
371
+ if (options.attributes)
372
+ Object.assign(handle.attributes, options.attributes);
373
+ processor.markJoined(runId);
374
+ const startedAt = nowIso();
375
+ const startedMs = Date.now();
376
+ const ctx = {
377
+ runId,
378
+ spanId,
379
+ parentSpanId: parentSpanId ?? null,
380
+ teamSiteId,
381
+ processor,
382
+ };
383
+ let status = 'ok';
384
+ let errorType;
385
+ let errorMessage;
386
+ try {
387
+ const result = await runWithContextAsync(ctx, async () => fn(handle));
388
+ if (handle.output === undefined)
389
+ handle.setOutput(result);
390
+ const span = {
391
+ span_id: spanId,
392
+ run_id: runId,
393
+ parent_span_id: parentSpanId ?? null,
394
+ kind,
395
+ name,
396
+ status,
397
+ started_at: startedAt,
398
+ ended_at: nowIso(),
399
+ duration_ms: Date.now() - startedMs,
400
+ input: handle.input,
401
+ output: handle.output,
402
+ model: handle.model,
403
+ provider: handle.provider,
404
+ input_tokens: handle.inputTokens,
405
+ output_tokens: handle.outputTokens,
406
+ cost: handle.cost,
407
+ temperature: handle.temperature,
408
+ tool_name: handle.toolName,
409
+ attributes: Object.keys(handle.attributes).length ? handle.attributes : undefined,
410
+ };
411
+ await processor.appendSpans(runId, [span]);
412
+ return result;
413
+ }
414
+ catch (err) {
415
+ status = 'error';
416
+ errorType = err?.name || 'Error';
417
+ errorMessage = truncate(err?.message ?? String(err), 4000);
418
+ const span = {
419
+ span_id: spanId,
420
+ run_id: runId,
421
+ parent_span_id: parentSpanId ?? null,
422
+ kind,
423
+ name,
424
+ status,
425
+ started_at: startedAt,
426
+ ended_at: nowIso(),
427
+ duration_ms: Date.now() - startedMs,
428
+ input: handle.input,
429
+ output: handle.output,
430
+ error_type: errorType,
431
+ error_message: errorMessage,
432
+ model: handle.model,
433
+ provider: handle.provider,
434
+ input_tokens: handle.inputTokens,
435
+ output_tokens: handle.outputTokens,
436
+ cost: handle.cost,
437
+ temperature: handle.temperature,
438
+ tool_name: handle.toolName,
439
+ attributes: Object.keys(handle.attributes).length ? handle.attributes : undefined,
440
+ };
441
+ await processor.appendSpans(runId, [span]);
442
+ throw err;
443
+ }
444
+ finally {
445
+ processor.unmarkJoined(runId);
446
+ }
447
+ }
448
+ //# sourceMappingURL=wrapAgent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wrapAgent.js","sourceRoot":"","sources":["../../../src/otel/wrapAgent.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EACL,gBAAgB,EAEhB,mBAAmB,GAEpB,MAAM,cAAc,CAAC;AA4BtB,SAAS,MAAM;IACb,OAAO,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;AAClC,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc,EAAE,GAAG,GAAG,KAAM;IAC5C,IAAI,KAAK,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