trodo-node 1.1.0 → 2.1.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 +340 -103
- package/dist/cjs/TrodoClient.js +77 -19
- package/dist/cjs/TrodoClient.js.map +1 -1
- package/dist/cjs/api/ApiClient.js +21 -3
- package/dist/cjs/api/ApiClient.js.map +1 -1
- package/dist/cjs/api/endpoints.js +7 -1
- package/dist/cjs/api/endpoints.js.map +1 -1
- package/dist/cjs/index.js +104 -8
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/otel/autoInstrument.js +253 -0
- package/dist/cjs/otel/autoInstrument.js.map +1 -0
- package/dist/cjs/otel/context.js +49 -0
- package/dist/cjs/otel/context.js.map +1 -0
- package/dist/cjs/otel/helpers.js +254 -0
- package/dist/cjs/otel/helpers.js.map +1 -0
- package/dist/cjs/otel/processor.js +129 -0
- package/dist/cjs/otel/processor.js.map +1 -0
- package/dist/cjs/otel/uuid.js +24 -0
- package/dist/cjs/otel/uuid.js.map +1 -0
- package/dist/cjs/otel/wrapAgent.js +399 -0
- package/dist/cjs/otel/wrapAgent.js.map +1 -0
- package/dist/cjs/queue/BatchFlusher.js +1 -1
- package/dist/cjs/queue/BatchFlusher.js.map +1 -1
- package/dist/esm/TrodoClient.d.ts +34 -1
- package/dist/esm/TrodoClient.d.ts.map +1 -1
- package/dist/esm/TrodoClient.js +77 -19
- package/dist/esm/TrodoClient.js.map +1 -1
- package/dist/esm/api/ApiClient.d.ts +9 -1
- package/dist/esm/api/ApiClient.d.ts.map +1 -1
- package/dist/esm/api/ApiClient.js +21 -3
- package/dist/esm/api/ApiClient.js.map +1 -1
- package/dist/esm/api/endpoints.d.ts +6 -1
- package/dist/esm/api/endpoints.d.ts.map +1 -1
- package/dist/esm/api/endpoints.js +7 -1
- package/dist/esm/api/endpoints.js.map +1 -1
- package/dist/esm/index.d.ts +84 -8
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +89 -7
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/otel/autoInstrument.d.ts +61 -0
- package/dist/esm/otel/autoInstrument.d.ts.map +1 -0
- package/dist/esm/otel/autoInstrument.js +248 -0
- package/dist/esm/otel/autoInstrument.js.map +1 -0
- package/dist/esm/otel/context.d.ts +26 -0
- package/dist/esm/otel/context.d.ts.map +1 -0
- package/dist/esm/otel/context.js +44 -0
- package/dist/esm/otel/context.js.map +1 -0
- package/dist/esm/otel/helpers.d.ts +119 -0
- package/dist/esm/otel/helpers.d.ts.map +1 -0
- package/dist/esm/otel/helpers.js +244 -0
- package/dist/esm/otel/helpers.js.map +1 -0
- package/dist/esm/otel/processor.d.ts +94 -0
- package/dist/esm/otel/processor.d.ts.map +1 -0
- package/dist/esm/otel/processor.js +125 -0
- package/dist/esm/otel/processor.js.map +1 -0
- package/dist/esm/otel/uuid.d.ts +7 -0
- package/dist/esm/otel/uuid.d.ts.map +1 -0
- package/dist/esm/otel/uuid.js +21 -0
- package/dist/esm/otel/uuid.js.map +1 -0
- package/dist/esm/otel/wrapAgent.d.ts +100 -0
- package/dist/esm/otel/wrapAgent.d.ts.map +1 -0
- package/dist/esm/otel/wrapAgent.js +389 -0
- package/dist/esm/otel/wrapAgent.js.map +1 -0
- package/dist/esm/queue/BatchFlusher.js +1 -1
- package/dist/esm/queue/BatchFlusher.js.map +1 -1
- package/dist/esm/types/index.d.ts +26 -0
- package/dist/esm/types/index.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SpanHandle = exports.RunHandle = void 0;
|
|
4
|
+
exports.currentRunId = currentRunId;
|
|
5
|
+
exports.currentSpanId = currentSpanId;
|
|
6
|
+
exports.wrapAgent = wrapAgent;
|
|
7
|
+
exports.withSpan = withSpan;
|
|
8
|
+
exports.joinRun = joinRun;
|
|
9
|
+
/**
|
|
10
|
+
* wrapAgent / withSpan / joinRun — user-facing surface for agent tracking.
|
|
11
|
+
*
|
|
12
|
+
* Users never generate IDs. The SDK creates run_id on wrapAgent, threads a
|
|
13
|
+
* span_id through AsyncLocalStorage, and any nested withSpan (or OTel
|
|
14
|
+
* auto-instrumented span from Anthropic/OpenAI/Vercel AI SDK/LangChain)
|
|
15
|
+
* inherits the current span as its parent.
|
|
16
|
+
*
|
|
17
|
+
* joinRun is for downstream microservices: opens a SPAN (not a run) in the
|
|
18
|
+
* context of the caller's run_id, flushing each span to the backend via
|
|
19
|
+
* append_spans. The originating service owns run lifecycle.
|
|
20
|
+
*/
|
|
21
|
+
const uuid_js_1 = require("./uuid.js");
|
|
22
|
+
const context_js_1 = require("./context.js");
|
|
23
|
+
function nowIso() {
|
|
24
|
+
return new Date().toISOString();
|
|
25
|
+
}
|
|
26
|
+
function truncate(value, max = 64000) {
|
|
27
|
+
if (value == null)
|
|
28
|
+
return undefined;
|
|
29
|
+
const str = typeof value === 'string' ? value : safeJson(value);
|
|
30
|
+
return str.length > max ? str.slice(0, max) : str;
|
|
31
|
+
}
|
|
32
|
+
function safeJson(v) {
|
|
33
|
+
try {
|
|
34
|
+
return JSON.stringify(v);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return String(v);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** Return the currently active run_id, if any. */
|
|
41
|
+
function currentRunId() {
|
|
42
|
+
return (0, context_js_1.getActiveContext)()?.runId ?? null;
|
|
43
|
+
}
|
|
44
|
+
/** Return the currently active span_id, if any. */
|
|
45
|
+
function currentSpanId() {
|
|
46
|
+
return (0, context_js_1.getActiveContext)()?.spanId ?? null;
|
|
47
|
+
}
|
|
48
|
+
function aggregate(spans) {
|
|
49
|
+
let totalIn = 0;
|
|
50
|
+
let totalOut = 0;
|
|
51
|
+
let totalCost = 0;
|
|
52
|
+
let toolCount = 0;
|
|
53
|
+
let errorCount = 0;
|
|
54
|
+
for (const s of spans) {
|
|
55
|
+
if (s.input_tokens)
|
|
56
|
+
totalIn += Number(s.input_tokens);
|
|
57
|
+
if (s.output_tokens)
|
|
58
|
+
totalOut += Number(s.output_tokens);
|
|
59
|
+
if (s.cost)
|
|
60
|
+
totalCost += Number(s.cost);
|
|
61
|
+
if (s.kind === 'tool')
|
|
62
|
+
toolCount += 1;
|
|
63
|
+
if (s.status === 'error')
|
|
64
|
+
errorCount += 1;
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
total_tokens_in: totalIn,
|
|
68
|
+
total_tokens_out: totalOut,
|
|
69
|
+
total_cost: totalCost ? Math.round(totalCost * 1e8) / 1e8 : 0,
|
|
70
|
+
span_count: spans.length,
|
|
71
|
+
tool_count: toolCount,
|
|
72
|
+
error_count: errorCount,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* RunHandle — returned by wrapAgent so callers can set input/output after
|
|
77
|
+
* inspecting the arguments / result. Mirrors the Python RunHandle API.
|
|
78
|
+
*/
|
|
79
|
+
class RunHandle {
|
|
80
|
+
constructor(runId, agentName) {
|
|
81
|
+
this.metadata = {};
|
|
82
|
+
this.runId = runId;
|
|
83
|
+
this.agentName = agentName;
|
|
84
|
+
}
|
|
85
|
+
setInput(value) {
|
|
86
|
+
this.input = truncate(value);
|
|
87
|
+
}
|
|
88
|
+
setOutput(value) {
|
|
89
|
+
this.output = truncate(value);
|
|
90
|
+
}
|
|
91
|
+
setMetadata(extra) {
|
|
92
|
+
Object.assign(this.metadata, extra);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
exports.RunHandle = RunHandle;
|
|
96
|
+
/**
|
|
97
|
+
* SpanHandle — returned by withSpan / joinRun. Collect LLM model/token/cost
|
|
98
|
+
* info and tool name here; the processor picks them up at span close.
|
|
99
|
+
*/
|
|
100
|
+
class SpanHandle {
|
|
101
|
+
constructor(spanId, name) {
|
|
102
|
+
this.attributes = {};
|
|
103
|
+
this.spanId = spanId;
|
|
104
|
+
this.name = name;
|
|
105
|
+
}
|
|
106
|
+
setInput(value) {
|
|
107
|
+
this.input = truncate(value);
|
|
108
|
+
}
|
|
109
|
+
setOutput(value) {
|
|
110
|
+
this.output = truncate(value);
|
|
111
|
+
}
|
|
112
|
+
setAttribute(key, value) {
|
|
113
|
+
this.attributes[key] = value;
|
|
114
|
+
}
|
|
115
|
+
setLlm(opts) {
|
|
116
|
+
if (opts.model !== undefined)
|
|
117
|
+
this.model = opts.model;
|
|
118
|
+
if (opts.provider !== undefined)
|
|
119
|
+
this.provider = opts.provider;
|
|
120
|
+
if (opts.inputTokens !== undefined)
|
|
121
|
+
this.inputTokens = Number(opts.inputTokens);
|
|
122
|
+
if (opts.outputTokens !== undefined)
|
|
123
|
+
this.outputTokens = Number(opts.outputTokens);
|
|
124
|
+
if (opts.cost !== undefined)
|
|
125
|
+
this.cost = Number(opts.cost);
|
|
126
|
+
if (opts.temperature !== undefined)
|
|
127
|
+
this.temperature = Number(opts.temperature);
|
|
128
|
+
}
|
|
129
|
+
setTool(name) {
|
|
130
|
+
this.toolName = name;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
exports.SpanHandle = SpanHandle;
|
|
134
|
+
/**
|
|
135
|
+
* Wrap an async function as an agent run. Every OTel span (or withSpan call)
|
|
136
|
+
* created inside `fn` is auto-nested under the run via AsyncLocalStorage.
|
|
137
|
+
*
|
|
138
|
+
* The callback receives a RunHandle so it can call `handle.setInput(...)` /
|
|
139
|
+
* `handle.setOutput(...)`. Old-style callbacks (no arg) still work.
|
|
140
|
+
*/
|
|
141
|
+
async function wrapAgent(processor, teamSiteId, agentName, fn, options = {}) {
|
|
142
|
+
const runId = (0, uuid_js_1.uuidv4)();
|
|
143
|
+
const rootSpanId = (0, uuid_js_1.uuidv4)();
|
|
144
|
+
const startedAt = nowIso();
|
|
145
|
+
const startedMs = Date.now();
|
|
146
|
+
const handle = new RunHandle(runId, agentName);
|
|
147
|
+
const ctx = {
|
|
148
|
+
runId,
|
|
149
|
+
spanId: rootSpanId,
|
|
150
|
+
parentSpanId: null,
|
|
151
|
+
teamSiteId,
|
|
152
|
+
processor,
|
|
153
|
+
};
|
|
154
|
+
let status = 'ok';
|
|
155
|
+
let output;
|
|
156
|
+
let errorSummary;
|
|
157
|
+
let result;
|
|
158
|
+
try {
|
|
159
|
+
result = await (0, context_js_1.runWithContextAsync)(ctx, async () => fn(handle));
|
|
160
|
+
output = handle.output ?? truncate(result);
|
|
161
|
+
}
|
|
162
|
+
catch (err) {
|
|
163
|
+
status = 'error';
|
|
164
|
+
errorSummary = truncate(err?.message ?? String(err), 4000);
|
|
165
|
+
const pending = processor.getPending(runId);
|
|
166
|
+
const agg = aggregate(pending);
|
|
167
|
+
const run = {
|
|
168
|
+
run_id: runId,
|
|
169
|
+
agent_name: agentName,
|
|
170
|
+
distinct_id: options.distinctId ?? null,
|
|
171
|
+
conversation_id: options.conversationId ?? null,
|
|
172
|
+
parent_run_id: options.parentRunId ?? null,
|
|
173
|
+
status,
|
|
174
|
+
input: handle.input,
|
|
175
|
+
started_at: startedAt,
|
|
176
|
+
ended_at: nowIso(),
|
|
177
|
+
duration_ms: Date.now() - startedMs,
|
|
178
|
+
error_summary: errorSummary,
|
|
179
|
+
metadata: { ...(options.metadata || {}), ...handle.metadata },
|
|
180
|
+
total_tokens_in: agg.total_tokens_in,
|
|
181
|
+
total_tokens_out: agg.total_tokens_out,
|
|
182
|
+
total_cost: agg.total_cost,
|
|
183
|
+
span_count: agg.span_count,
|
|
184
|
+
tool_count: agg.tool_count,
|
|
185
|
+
error_count: agg.error_count,
|
|
186
|
+
};
|
|
187
|
+
await processor.ingestRun(run);
|
|
188
|
+
throw err;
|
|
189
|
+
}
|
|
190
|
+
const pending = processor.getPending(runId);
|
|
191
|
+
const agg = aggregate(pending);
|
|
192
|
+
const run = {
|
|
193
|
+
run_id: runId,
|
|
194
|
+
agent_name: agentName,
|
|
195
|
+
distinct_id: options.distinctId ?? null,
|
|
196
|
+
conversation_id: options.conversationId ?? null,
|
|
197
|
+
parent_run_id: options.parentRunId ?? null,
|
|
198
|
+
status,
|
|
199
|
+
input: handle.input,
|
|
200
|
+
output,
|
|
201
|
+
started_at: startedAt,
|
|
202
|
+
ended_at: nowIso(),
|
|
203
|
+
duration_ms: Date.now() - startedMs,
|
|
204
|
+
metadata: { ...(options.metadata || {}), ...handle.metadata },
|
|
205
|
+
total_tokens_in: agg.total_tokens_in,
|
|
206
|
+
total_tokens_out: agg.total_tokens_out,
|
|
207
|
+
total_cost: agg.total_cost,
|
|
208
|
+
span_count: agg.span_count,
|
|
209
|
+
tool_count: agg.tool_count,
|
|
210
|
+
error_count: agg.error_count,
|
|
211
|
+
};
|
|
212
|
+
await processor.ingestRun(run);
|
|
213
|
+
return { result, runId };
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Create a nested span for non-auto-instrumented work (KB lookups, custom
|
|
217
|
+
* tools, computation). Requires an enclosing wrapAgent/joinRun.
|
|
218
|
+
*
|
|
219
|
+
* The callback receives a SpanHandle so the caller can record LLM tokens /
|
|
220
|
+
* tool name / output inside the function body.
|
|
221
|
+
*/
|
|
222
|
+
async function withSpan(_processor, name, fn, options = {}) {
|
|
223
|
+
const active = (0, context_js_1.getActiveContext)();
|
|
224
|
+
const spanId = (0, uuid_js_1.uuidv4)();
|
|
225
|
+
const handle = new SpanHandle(spanId, name);
|
|
226
|
+
if (options.input !== undefined)
|
|
227
|
+
handle.setInput(options.input);
|
|
228
|
+
if (options.attributes)
|
|
229
|
+
Object.assign(handle.attributes, options.attributes);
|
|
230
|
+
if (!active) {
|
|
231
|
+
// Outside of a run — execute but skip tracking (don't crash user code).
|
|
232
|
+
return Promise.resolve(fn(handle));
|
|
233
|
+
}
|
|
234
|
+
const startedAt = nowIso();
|
|
235
|
+
const startedMs = Date.now();
|
|
236
|
+
const child = {
|
|
237
|
+
runId: active.runId,
|
|
238
|
+
spanId,
|
|
239
|
+
parentSpanId: active.spanId,
|
|
240
|
+
teamSiteId: active.teamSiteId,
|
|
241
|
+
processor: active.processor,
|
|
242
|
+
};
|
|
243
|
+
let status = 'ok';
|
|
244
|
+
let errorType;
|
|
245
|
+
let errorMessage;
|
|
246
|
+
try {
|
|
247
|
+
const result = await (0, context_js_1.runWithContextAsync)(child, async () => fn(handle));
|
|
248
|
+
if (handle.output === undefined)
|
|
249
|
+
handle.setOutput(result);
|
|
250
|
+
const span = {
|
|
251
|
+
span_id: spanId,
|
|
252
|
+
run_id: active.runId,
|
|
253
|
+
parent_span_id: active.spanId,
|
|
254
|
+
kind: options.kind || 'generic',
|
|
255
|
+
name,
|
|
256
|
+
status,
|
|
257
|
+
started_at: startedAt,
|
|
258
|
+
ended_at: nowIso(),
|
|
259
|
+
duration_ms: Date.now() - startedMs,
|
|
260
|
+
input: handle.input,
|
|
261
|
+
output: handle.output,
|
|
262
|
+
model: handle.model,
|
|
263
|
+
provider: handle.provider,
|
|
264
|
+
input_tokens: handle.inputTokens,
|
|
265
|
+
output_tokens: handle.outputTokens,
|
|
266
|
+
cost: handle.cost,
|
|
267
|
+
temperature: handle.temperature,
|
|
268
|
+
tool_name: handle.toolName,
|
|
269
|
+
attributes: Object.keys(handle.attributes).length ? handle.attributes : undefined,
|
|
270
|
+
};
|
|
271
|
+
active.processor.enqueueSpan(span);
|
|
272
|
+
return result;
|
|
273
|
+
}
|
|
274
|
+
catch (err) {
|
|
275
|
+
status = 'error';
|
|
276
|
+
errorType = err?.name || 'Error';
|
|
277
|
+
errorMessage = truncate(err?.message ?? String(err), 4000);
|
|
278
|
+
const span = {
|
|
279
|
+
span_id: spanId,
|
|
280
|
+
run_id: active.runId,
|
|
281
|
+
parent_span_id: active.spanId,
|
|
282
|
+
kind: options.kind || 'generic',
|
|
283
|
+
name,
|
|
284
|
+
status,
|
|
285
|
+
started_at: startedAt,
|
|
286
|
+
ended_at: nowIso(),
|
|
287
|
+
duration_ms: Date.now() - startedMs,
|
|
288
|
+
input: handle.input,
|
|
289
|
+
output: handle.output,
|
|
290
|
+
error_type: errorType,
|
|
291
|
+
error_message: errorMessage,
|
|
292
|
+
model: handle.model,
|
|
293
|
+
provider: handle.provider,
|
|
294
|
+
input_tokens: handle.inputTokens,
|
|
295
|
+
output_tokens: handle.outputTokens,
|
|
296
|
+
cost: handle.cost,
|
|
297
|
+
temperature: handle.temperature,
|
|
298
|
+
tool_name: handle.toolName,
|
|
299
|
+
attributes: Object.keys(handle.attributes).length ? handle.attributes : undefined,
|
|
300
|
+
};
|
|
301
|
+
active.processor.enqueueSpan(span);
|
|
302
|
+
throw err;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Join an existing agent run owned by a remote service. Opens a SPAN (not a
|
|
307
|
+
* run) under ``parentSpanId``; every span produced inside ``fn`` — including
|
|
308
|
+
* OTel auto-instrumented LLM spans — flushes to the backend as a child of
|
|
309
|
+
* the caller's run via append_spans. We never ingest a run here.
|
|
310
|
+
*
|
|
311
|
+
* Typical use: Express/FastAPI middleware detects ``X-Trodo-Run-Id`` +
|
|
312
|
+
* ``X-Trodo-Parent-Span-Id`` on an inbound request and wraps the handler
|
|
313
|
+
* with joinRun so the handler's work nests under the caller's run.
|
|
314
|
+
*/
|
|
315
|
+
async function joinRun(processor, teamSiteId, runId, parentSpanId, fn, options = {}) {
|
|
316
|
+
const spanId = (0, uuid_js_1.uuidv4)();
|
|
317
|
+
const name = options.name || 'remote.handler';
|
|
318
|
+
const kind = options.kind || 'agent';
|
|
319
|
+
const handle = new SpanHandle(spanId, name);
|
|
320
|
+
if (options.input !== undefined)
|
|
321
|
+
handle.setInput(options.input);
|
|
322
|
+
if (options.attributes)
|
|
323
|
+
Object.assign(handle.attributes, options.attributes);
|
|
324
|
+
processor.markJoined(runId);
|
|
325
|
+
const startedAt = nowIso();
|
|
326
|
+
const startedMs = Date.now();
|
|
327
|
+
const ctx = {
|
|
328
|
+
runId,
|
|
329
|
+
spanId,
|
|
330
|
+
parentSpanId: parentSpanId ?? null,
|
|
331
|
+
teamSiteId,
|
|
332
|
+
processor,
|
|
333
|
+
};
|
|
334
|
+
let status = 'ok';
|
|
335
|
+
let errorType;
|
|
336
|
+
let errorMessage;
|
|
337
|
+
try {
|
|
338
|
+
const result = await (0, context_js_1.runWithContextAsync)(ctx, async () => fn(handle));
|
|
339
|
+
if (handle.output === undefined)
|
|
340
|
+
handle.setOutput(result);
|
|
341
|
+
const span = {
|
|
342
|
+
span_id: spanId,
|
|
343
|
+
run_id: runId,
|
|
344
|
+
parent_span_id: parentSpanId ?? null,
|
|
345
|
+
kind,
|
|
346
|
+
name,
|
|
347
|
+
status,
|
|
348
|
+
started_at: startedAt,
|
|
349
|
+
ended_at: nowIso(),
|
|
350
|
+
duration_ms: Date.now() - startedMs,
|
|
351
|
+
input: handle.input,
|
|
352
|
+
output: handle.output,
|
|
353
|
+
model: handle.model,
|
|
354
|
+
provider: handle.provider,
|
|
355
|
+
input_tokens: handle.inputTokens,
|
|
356
|
+
output_tokens: handle.outputTokens,
|
|
357
|
+
cost: handle.cost,
|
|
358
|
+
temperature: handle.temperature,
|
|
359
|
+
tool_name: handle.toolName,
|
|
360
|
+
attributes: Object.keys(handle.attributes).length ? handle.attributes : undefined,
|
|
361
|
+
};
|
|
362
|
+
await processor.appendSpans(runId, [span]);
|
|
363
|
+
return result;
|
|
364
|
+
}
|
|
365
|
+
catch (err) {
|
|
366
|
+
status = 'error';
|
|
367
|
+
errorType = err?.name || 'Error';
|
|
368
|
+
errorMessage = truncate(err?.message ?? String(err), 4000);
|
|
369
|
+
const span = {
|
|
370
|
+
span_id: spanId,
|
|
371
|
+
run_id: runId,
|
|
372
|
+
parent_span_id: parentSpanId ?? null,
|
|
373
|
+
kind,
|
|
374
|
+
name,
|
|
375
|
+
status,
|
|
376
|
+
started_at: startedAt,
|
|
377
|
+
ended_at: nowIso(),
|
|
378
|
+
duration_ms: Date.now() - startedMs,
|
|
379
|
+
input: handle.input,
|
|
380
|
+
output: handle.output,
|
|
381
|
+
error_type: errorType,
|
|
382
|
+
error_message: errorMessage,
|
|
383
|
+
model: handle.model,
|
|
384
|
+
provider: handle.provider,
|
|
385
|
+
input_tokens: handle.inputTokens,
|
|
386
|
+
output_tokens: handle.outputTokens,
|
|
387
|
+
cost: handle.cost,
|
|
388
|
+
temperature: handle.temperature,
|
|
389
|
+
tool_name: handle.toolName,
|
|
390
|
+
attributes: Object.keys(handle.attributes).length ? handle.attributes : undefined,
|
|
391
|
+
};
|
|
392
|
+
await processor.appendSpans(runId, [span]);
|
|
393
|
+
throw err;
|
|
394
|
+
}
|
|
395
|
+
finally {
|
|
396
|
+
processor.unmarkJoined(runId);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
//# sourceMappingURL=wrapAgent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrapAgent.js","sourceRoot":"","sources":["../../../src/otel/wrapAgent.ts"],"names":[],"mappings":";;;AAiEA,oCAEC;AAGD,sCAEC;AA8HD,8BAmFC;AASD,4BAuFC;AAYD,0BAwFC;AA7dD;;;;;;;;;;;GAWG;AACH,uCAAmC;AACnC,6CAKsB;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,IAAI,IAAI;QAAE,OAAO,SAAS,CAAC;IACpC,MAAM,GAAG,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAChE,OAAO,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;AACpD,CAAC;AAED,SAAS,QAAQ,CAAC,CAAU;IAC1B,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,SAA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AAM,CAAC,UAAU,CAAC,CAAC,CAAC,SAAS;SAClF,CAAC;QACF,MAAM,SAAS,CAAC,WAAW,CAAC,KAAK,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;QAC3C,MAAM,GAAG,CAAC;IACZ,CAAC;YAAS,CAAC;QACT,SAAS,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAChC,CAAC;AACH,CAAC"}
|
|
@@ -28,7 +28,7 @@ class BatchFlusher {
|
|
|
28
28
|
const events = this.queue.drain();
|
|
29
29
|
if (events.length === 0)
|
|
30
30
|
return;
|
|
31
|
-
await this.apiClient.
|
|
31
|
+
await Promise.all(events.map((e) => this.apiClient.postEvent(e)));
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
exports.BatchFlusher = BatchFlusher;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BatchFlusher.js","sourceRoot":"","sources":["../../../src/queue/BatchFlusher.ts"],"names":[],"mappings":";;;AAGA,MAAa,YAAY;IAGvB,YACmB,KAAiB,EACjB,SAAoB,EACpB,eAAuB;QAFvB,UAAK,GAAL,KAAK,CAAY;QACjB,cAAS,GAAT,SAAS,CAAW;QACpB,oBAAe,GAAf,eAAe,CAAQ;QALlC,UAAK,GAA0C,IAAI,CAAC;IAMzD,CAAC;IAEJ,KAAK;QACH,IAAI,IAAI,CAAC,KAAK;YAAE,OAAO;QACvB,IAAI,CAAC,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE;YAC5B,KAAK,IAAI,CAAC,KAAK,EAAE,CAAC;QACpB,CAAC,EAAE,IAAI,CAAC,eAAe,CAAC,CAAC;QACzB,gEAAgE;QAChE,IAAI,IAAI,CAAC,KAAK,CAAC,KAAK;YAAE,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IAC3C,CAAC;IAED,IAAI;QACF,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YACf,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAC1B,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AE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACpE,CAAC;CACF;AA9BD,oCA8BC"}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import type { TrodoConfig, ForUserOptions, TrackOptions, WalletAddressResult, ResetResult, ApiResult, GroupProfile } from './types/index.js';
|
|
1
|
+
import type { TrodoConfig, ForUserOptions, TrackOptions, WalletAddressResult, ResetResult, ApiResult, GroupProfile, FeedbackProps } from './types/index.js';
|
|
2
2
|
import { UserContext } from './UserContext.js';
|
|
3
|
+
import { type WrapAgentOptions, type WrapAgentResult, type WithSpanOptions, type JoinRunOptions, type RunHandle, type SpanHandle } from './otel/wrapAgent.js';
|
|
4
|
+
import { expressMiddleware as expressMiddlewareImpl, type ToolOptions, type TrackLlmCallParams } from './otel/helpers.js';
|
|
3
5
|
export declare class TrodoClient {
|
|
4
6
|
readonly siteId: string;
|
|
5
7
|
private readonly apiBase;
|
|
@@ -9,6 +11,7 @@ export declare class TrodoClient {
|
|
|
9
11
|
private readonly batchFlusher;
|
|
10
12
|
private readonly autoEventManager;
|
|
11
13
|
private readonly userContextCache;
|
|
14
|
+
private readonly spanProcessor;
|
|
12
15
|
readonly people: {
|
|
13
16
|
set(distinctId: string, properties: Record<string, unknown>): Promise<ApiResult>;
|
|
14
17
|
setOnce(distinctId: string, properties: Record<string, unknown>): Promise<ApiResult>;
|
|
@@ -35,5 +38,35 @@ export declare class TrodoClient {
|
|
|
35
38
|
disableAutoEvents(): void;
|
|
36
39
|
flush(): Promise<void>;
|
|
37
40
|
shutdown(): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Wrap an async function as an agent run. Callback receives a RunHandle
|
|
43
|
+
* — call `handle.setInput(...)` / `handle.setOutput(...)` to populate the
|
|
44
|
+
* run's input/output fields. Every OTel-auto-instrumented LLM call or
|
|
45
|
+
* nested `withSpan` inside `fn` is captured as a child span.
|
|
46
|
+
* Returns { result, runId } — runId is used to attach feedback later.
|
|
47
|
+
*/
|
|
48
|
+
wrapAgent<R>(agentName: string, fn: (handle: RunHandle) => Promise<R> | R, options?: WrapAgentOptions): Promise<WrapAgentResult<R>>;
|
|
49
|
+
/** Create a nested span inside the current run. */
|
|
50
|
+
withSpan<R>(name: string, fn: (handle: SpanHandle) => Promise<R> | R, options?: WithSpanOptions): Promise<R>;
|
|
51
|
+
/**
|
|
52
|
+
* Join an existing run owned by a remote service — opens a span (not a new
|
|
53
|
+
* run) in that run's context. Spans inside `fn` are streamed to the backend
|
|
54
|
+
* via append_spans. Used by the express middleware and for manual cross-
|
|
55
|
+
* service join on any request shape.
|
|
56
|
+
*/
|
|
57
|
+
joinRun<R>(runId: string, parentSpanId: string | null, fn: (handle: SpanHandle) => Promise<R> | R, options?: JoinRunOptions): Promise<R>;
|
|
58
|
+
/** Higher-order wrapper: decorate a tool-like function so each call becomes a span. */
|
|
59
|
+
tool<Args extends unknown[], R>(fn: (...args: Args) => Promise<R> | R, options?: ToolOptions): (...args: Args) => Promise<R>;
|
|
60
|
+
/** Record a one-shot LLM span for a raw-HTTP caller. */
|
|
61
|
+
trackLlmCall(params: TrackLlmCallParams): Promise<void>;
|
|
62
|
+
/** Return an Express/Connect middleware that auto-joins inbound runs. */
|
|
63
|
+
expressMiddleware(): ReturnType<typeof expressMiddlewareImpl>;
|
|
64
|
+
/** Outbound HTTP headers carrying the current run/span context. */
|
|
65
|
+
propagationHeaders(): Record<string, string>;
|
|
66
|
+
/** Expose current run/span ids for bespoke propagation needs. */
|
|
67
|
+
currentRunId(): string | null;
|
|
68
|
+
currentSpanId(): string | null;
|
|
69
|
+
/** Attach feedback to a completed run. Call with the runId returned from wrapAgent. */
|
|
70
|
+
feedback(runId: string, props: Omit<FeedbackProps, 'runId'>): Promise<ApiResult>;
|
|
38
71
|
}
|
|
39
72
|
//# sourceMappingURL=TrodoClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TrodoClient.d.ts","sourceRoot":"","sources":["../../src/TrodoClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,cAAc,EACd,YAAY,EACZ,mBAAmB,EACnB,WAAW,EACX,SAAS,EACT,YAAY,
|
|
1
|
+
{"version":3,"file":"TrodoClient.d.ts","sourceRoot":"","sources":["../../src/TrodoClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,cAAc,EACd,YAAY,EACZ,mBAAmB,EACnB,WAAW,EACX,SAAS,EACT,YAAY,EACZ,aAAa,EACd,MAAM,kBAAkB,CAAC;AAM1B,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,OAAO,EAML,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,eAAe,EACpB,KAAK,cAAc,EACnB,KAAK,SAAS,EACd,KAAK,UAAU,EAChB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAIL,iBAAiB,IAAI,qBAAqB,EAC1C,KAAK,WAAW,EAChB,KAAK,kBAAkB,EACxB,MAAM,mBAAmB,CAAC;AAK3B,qBAAa,WAAW;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAY;IACtC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAiB;IAChD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAoB;IAC/C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAsB;IACnD,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAmB;IACpD,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAkC;IACnE,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAqB;IAEnD,QAAQ,CAAC,MAAM,EAAE;QACf,GAAG,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,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ue/EventQueue.js';
|
|
|
4
4
|
import { BatchFlusher } from './queue/BatchFlusher.js';
|
|
5
5
|
import { AutoEventManager } from './auto/AutoEventManager.js';
|
|
6
6
|
import { UserContext } from './UserContext.js';
|
|
7
|
+
import { TrodoSpanProcessor } from './otel/processor.js';
|
|
8
|
+
import { wrapAgent as wrapAgentImpl, withSpan as withSpanImpl, joinRun as joinRunImpl, currentRunId, currentSpanId, } from './otel/wrapAgent.js';
|
|
9
|
+
import { tool as toolImpl, trackLlmCall as trackLlmCallImpl, propagationHeaders, expressMiddleware as expressMiddlewareImpl, } from './otel/helpers.js';
|
|
10
|
+
import { enableAutoInstrument } from './otel/autoInstrument.js';
|
|
7
11
|
const DEFAULT_API_BASE = 'https://sdkapi.trodo.ai';
|
|
8
12
|
export class TrodoClient {
|
|
9
13
|
constructor(config) {
|
|
@@ -27,7 +31,11 @@ export class TrodoClient {
|
|
|
27
31
|
if (config.autoEvents) {
|
|
28
32
|
this.autoEventManager.enable();
|
|
29
33
|
}
|
|
30
|
-
|
|
34
|
+
this.spanProcessor = new TrodoSpanProcessor({ apiClient: this.apiClient });
|
|
35
|
+
// Default ON — matches Python SDK. Opt out with autoInstrument: false.
|
|
36
|
+
if (config.autoInstrument !== false) {
|
|
37
|
+
enableAutoInstrument({ processor: this.spanProcessor });
|
|
38
|
+
}
|
|
31
39
|
const client = this;
|
|
32
40
|
this.people = {
|
|
33
41
|
set: (d, p) => client.forUser(d).people.set(p),
|
|
@@ -46,17 +54,13 @@ export class TrodoClient {
|
|
|
46
54
|
// Primary pattern: forUser()
|
|
47
55
|
// --------------------------------------------------------------------------
|
|
48
56
|
forUser(distinctId, options) {
|
|
49
|
-
const
|
|
50
|
-
const cached = this.userContextCache.get(key);
|
|
57
|
+
const cached = this.userContextCache.get(distinctId);
|
|
51
58
|
if (cached)
|
|
52
59
|
return cached;
|
|
53
60
|
const ctx = new UserContext(distinctId, this.siteId, this.apiClient, this.sessionManager, this.eventQueue, this.batchFlusher, this.autoEventManager, options);
|
|
54
|
-
this.userContextCache.set(
|
|
61
|
+
this.userContextCache.set(distinctId, ctx);
|
|
55
62
|
return ctx;
|
|
56
63
|
}
|
|
57
|
-
// --------------------------------------------------------------------------
|
|
58
|
-
// Direct-call pattern (distinctId as first param)
|
|
59
|
-
// --------------------------------------------------------------------------
|
|
60
64
|
async track(distinctId, eventName, properties, options) {
|
|
61
65
|
return this.forUser(distinctId).track(eventName, properties, options);
|
|
62
66
|
}
|
|
@@ -87,21 +91,12 @@ export class TrodoClient {
|
|
|
87
91
|
get_group(distinctId, groupKey, groupId) {
|
|
88
92
|
return this.forUser(distinctId).get_group(groupKey, groupId);
|
|
89
93
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
// --------------------------------------------------------------------------
|
|
93
|
-
enableAutoEvents() {
|
|
94
|
-
this.autoEventManager.enable();
|
|
95
|
-
}
|
|
96
|
-
disableAutoEvents() {
|
|
97
|
-
this.autoEventManager.disable();
|
|
98
|
-
}
|
|
99
|
-
// --------------------------------------------------------------------------
|
|
100
|
-
// Lifecycle
|
|
101
|
-
// --------------------------------------------------------------------------
|
|
94
|
+
enableAutoEvents() { this.autoEventManager.enable(); }
|
|
95
|
+
disableAutoEvents() { this.autoEventManager.disable(); }
|
|
102
96
|
async flush() {
|
|
103
97
|
if (this.batchFlusher)
|
|
104
98
|
await this.batchFlusher.flush();
|
|
99
|
+
await this.spanProcessor.forceFlush();
|
|
105
100
|
}
|
|
106
101
|
async shutdown() {
|
|
107
102
|
this.autoEventManager.disable();
|
|
@@ -109,6 +104,69 @@ export class TrodoClient {
|
|
|
109
104
|
this.batchFlusher.stop();
|
|
110
105
|
await this.batchFlusher.flush();
|
|
111
106
|
}
|
|
107
|
+
await this.spanProcessor.shutdown();
|
|
108
|
+
}
|
|
109
|
+
// --------------------------------------------------------------------------
|
|
110
|
+
// Agent Runs — unified surface
|
|
111
|
+
// --------------------------------------------------------------------------
|
|
112
|
+
/**
|
|
113
|
+
* Wrap an async function as an agent run. Callback receives a RunHandle
|
|
114
|
+
* — call `handle.setInput(...)` / `handle.setOutput(...)` to populate the
|
|
115
|
+
* run's input/output fields. Every OTel-auto-instrumented LLM call or
|
|
116
|
+
* nested `withSpan` inside `fn` is captured as a child span.
|
|
117
|
+
* Returns { result, runId } — runId is used to attach feedback later.
|
|
118
|
+
*/
|
|
119
|
+
wrapAgent(agentName, fn, options = {}) {
|
|
120
|
+
return wrapAgentImpl(this.spanProcessor, this.siteId, agentName, fn, options);
|
|
121
|
+
}
|
|
122
|
+
/** Create a nested span inside the current run. */
|
|
123
|
+
withSpan(name, fn, options = {}) {
|
|
124
|
+
return withSpanImpl(this.spanProcessor, name, fn, options);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Join an existing run owned by a remote service — opens a span (not a new
|
|
128
|
+
* run) in that run's context. Spans inside `fn` are streamed to the backend
|
|
129
|
+
* via append_spans. Used by the express middleware and for manual cross-
|
|
130
|
+
* service join on any request shape.
|
|
131
|
+
*/
|
|
132
|
+
joinRun(runId, parentSpanId, fn, options = {}) {
|
|
133
|
+
return joinRunImpl(this.spanProcessor, this.siteId, runId, parentSpanId, fn, options);
|
|
134
|
+
}
|
|
135
|
+
/** Higher-order wrapper: decorate a tool-like function so each call becomes a span. */
|
|
136
|
+
tool(fn, options = {}) {
|
|
137
|
+
return toolImpl(fn, options);
|
|
138
|
+
}
|
|
139
|
+
/** Record a one-shot LLM span for a raw-HTTP caller. */
|
|
140
|
+
trackLlmCall(params) {
|
|
141
|
+
return trackLlmCallImpl(params);
|
|
142
|
+
}
|
|
143
|
+
/** Return an Express/Connect middleware that auto-joins inbound runs. */
|
|
144
|
+
expressMiddleware() {
|
|
145
|
+
return expressMiddlewareImpl({
|
|
146
|
+
processor: this.spanProcessor,
|
|
147
|
+
teamSiteId: this.siteId,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
/** Outbound HTTP headers carrying the current run/span context. */
|
|
151
|
+
propagationHeaders() {
|
|
152
|
+
return propagationHeaders();
|
|
153
|
+
}
|
|
154
|
+
/** Expose current run/span ids for bespoke propagation needs. */
|
|
155
|
+
currentRunId() {
|
|
156
|
+
return currentRunId();
|
|
157
|
+
}
|
|
158
|
+
currentSpanId() {
|
|
159
|
+
return currentSpanId();
|
|
160
|
+
}
|
|
161
|
+
/** Attach feedback to a completed run. Call with the runId returned from wrapAgent. */
|
|
162
|
+
async feedback(runId, props) {
|
|
163
|
+
return this.apiClient.postRunFeedback(runId, {
|
|
164
|
+
satisfaction: props.satisfaction ?? null,
|
|
165
|
+
rating: props.rating ?? null,
|
|
166
|
+
comment: props.comment ?? props.feedback ?? null,
|
|
167
|
+
distinct_id: props.distinctId ?? null,
|
|
168
|
+
attributes: props.metadata ?? {},
|
|
169
|
+
});
|
|
112
170
|
}
|
|
113
171
|
}
|
|
114
172
|
//# sourceMappingURL=TrodoClient.js.map
|