orchajs 0.27.4 → 0.28.1
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 +31 -0
- package/dist/amarsia/analytics.d.ts +37 -3
- package/dist/amarsia/analytics.js +57 -5
- package/dist/amarsia/sync.js +1 -1
- package/dist/browser.d.ts +1 -1
- package/dist/cli-agents-template.d.ts +1 -1
- package/dist/cli-agents-template.js +9 -0
- package/dist/cli.js +1 -1
- package/dist/integrations/expo.d.ts +2 -1
- package/dist/integrations/expo.js +1 -1
- package/dist/integrations/webpack.d.ts +1 -1
- package/dist/orcha.d.ts +1 -1
- package/dist/orcha.js +1 -1
- package/dist/playground/server.js +1 -1
- package/dist/providers/bedrock.d.ts +2 -1
- package/dist/react-native.d.ts +1 -1
- package/dist/runtime/actions/execute.d.ts +1 -1
- package/dist/runtime/actions/execute.js +1 -1
- package/dist/runtime/agent.js +15 -2
- package/dist/runtime/create-orcha.js +15 -5
- package/dist/storage/cloud.d.ts +2 -1
- package/dist/storage/cloud.js +1 -1
- package/dist/telemetry/open-telemetry.d.ts +19 -0
- package/dist/telemetry/open-telemetry.js +630 -0
- package/dist/types.d.ts +13 -0
- package/dist-cjs/amarsia/analytics.js +60 -5
- package/dist-cjs/cli-agents-template.js +9 -0
- package/dist-cjs/runtime/agent.js +9 -2
- package/dist-cjs/runtime/create-orcha.js +12 -2
- package/dist-cjs/telemetry/open-telemetry.js +635 -0
- package/package.json +7 -4
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
"use strict"; const __orchaImportMetaUrl = require("url").pathToFileURL(__filename).href;
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var open_telemetry_exports = {};
|
|
21
|
+
__export(open_telemetry_exports, {
|
|
22
|
+
CompositeRunTelemetryClient: () => CompositeRunTelemetryClient,
|
|
23
|
+
createOpenTelemetryClient: () => createOpenTelemetryClient
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(open_telemetry_exports);
|
|
26
|
+
const MAX_QUEUED_RUNS = 1e3;
|
|
27
|
+
const MAX_CONCURRENT_EXPORTS = 2;
|
|
28
|
+
class CompositeRunTelemetryClient {
|
|
29
|
+
#clients;
|
|
30
|
+
constructor(clients) {
|
|
31
|
+
this.#clients = clients;
|
|
32
|
+
}
|
|
33
|
+
enqueue(fact, details) {
|
|
34
|
+
for (const client of this.#clients) {
|
|
35
|
+
try {
|
|
36
|
+
client.enqueue(fact, details);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
console.warn("A telemetry exporter rejected an Orcha run.", error);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
updateActiveRun(fact) {
|
|
43
|
+
for (const client of this.#clients) {
|
|
44
|
+
try {
|
|
45
|
+
client.updateActiveRun(fact);
|
|
46
|
+
} catch (error) {
|
|
47
|
+
console.warn("A telemetry exporter rejected an Orcha update.", error);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
trackProjection(projection) {
|
|
52
|
+
for (const client of this.#clients) client.trackProjection(projection);
|
|
53
|
+
}
|
|
54
|
+
async drain(options) {
|
|
55
|
+
await Promise.allSettled(
|
|
56
|
+
this.#clients.map((client) => client.drain(options))
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
async close(options) {
|
|
60
|
+
await Promise.allSettled(
|
|
61
|
+
this.#clients.map((client) => client.close(options))
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function createOpenTelemetryClient(configuration) {
|
|
66
|
+
return configuration === true ? new GlobalOpenTelemetryClient() : new OtlpHttpTelemetryClient(configuration);
|
|
67
|
+
}
|
|
68
|
+
class GlobalOpenTelemetryClient {
|
|
69
|
+
#pendingProjections = /* @__PURE__ */ new Set();
|
|
70
|
+
#pendingEmissions = /* @__PURE__ */ new Set();
|
|
71
|
+
#emissionChains = /* @__PURE__ */ new Map();
|
|
72
|
+
#activeSpans = /* @__PURE__ */ new Map();
|
|
73
|
+
enqueue(fact, details) {
|
|
74
|
+
const previous = this.#emissionChains.get(fact.event_id) ?? Promise.resolve();
|
|
75
|
+
const emission = previous.then(() => this.#emit(fact, details)).catch((error) => {
|
|
76
|
+
console.warn("OpenTelemetry global-provider delivery failed.", error);
|
|
77
|
+
});
|
|
78
|
+
this.#emissionChains.set(fact.event_id, emission);
|
|
79
|
+
this.#pendingEmissions.add(emission);
|
|
80
|
+
void emission.finally(() => {
|
|
81
|
+
this.#pendingEmissions.delete(emission);
|
|
82
|
+
if (this.#emissionChains.get(fact.event_id) === emission) {
|
|
83
|
+
this.#emissionChains.delete(fact.event_id);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
async #emit(fact, details) {
|
|
88
|
+
const api = await loadOpenTelemetryApi();
|
|
89
|
+
const tracer = api.trace.getTracer("orchajs");
|
|
90
|
+
const active = this.#activeSpans.get(fact.event_id) ?? this.#startSpan(api, tracer, fact, details);
|
|
91
|
+
if (fact.status === "running") return;
|
|
92
|
+
const { span, context: parentContext } = active;
|
|
93
|
+
span.setAttributes(runAttributes(fact));
|
|
94
|
+
for (const modelCall of details?.modelCalls ?? []) {
|
|
95
|
+
const modelSpan = tracer.startSpan(
|
|
96
|
+
`chat ${modelCall.model}`,
|
|
97
|
+
{
|
|
98
|
+
kind: api.SpanKind.CLIENT,
|
|
99
|
+
startTime: new Date(modelCall.started_at),
|
|
100
|
+
attributes: modelCallAttributes(fact, modelCall)
|
|
101
|
+
},
|
|
102
|
+
parentContext
|
|
103
|
+
);
|
|
104
|
+
modelSpan.setStatus({ code: api.SpanStatusCode.OK });
|
|
105
|
+
modelSpan.end(new Date(modelCall.ended_at));
|
|
106
|
+
}
|
|
107
|
+
for (const action of fact.actions) {
|
|
108
|
+
const actionSpan = tracer.startSpan(
|
|
109
|
+
`execute_tool ${action.name}`,
|
|
110
|
+
{
|
|
111
|
+
kind: api.SpanKind.INTERNAL,
|
|
112
|
+
startTime: new Date(action.started_at),
|
|
113
|
+
attributes: {
|
|
114
|
+
"gen_ai.operation.name": "execute_tool",
|
|
115
|
+
"gen_ai.tool.name": action.name,
|
|
116
|
+
"gen_ai.tool.type": "function",
|
|
117
|
+
"gen_ai.tool.call.id": action.call_id,
|
|
118
|
+
"gen_ai.agent.name": fact.agent_external_id,
|
|
119
|
+
"gen_ai.conversation.id": fact.session_id,
|
|
120
|
+
"orcha.span.type": "action",
|
|
121
|
+
"orcha.action.call_id": action.call_id,
|
|
122
|
+
"orcha.action.duration_ms": action.duration_ms
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
parentContext
|
|
126
|
+
);
|
|
127
|
+
if (action.status === "failed") {
|
|
128
|
+
actionSpan.addEvent("exception", {
|
|
129
|
+
"exception.type": action.error_code ?? "action_error",
|
|
130
|
+
"exception.message": action.error_message ?? "Action failed"
|
|
131
|
+
});
|
|
132
|
+
actionSpan.setStatus({
|
|
133
|
+
code: api.SpanStatusCode.ERROR,
|
|
134
|
+
message: action.error_message
|
|
135
|
+
});
|
|
136
|
+
if (action.error_code) {
|
|
137
|
+
actionSpan.setAttribute("error.type", action.error_code);
|
|
138
|
+
}
|
|
139
|
+
} else {
|
|
140
|
+
actionSpan.setStatus({ code: api.SpanStatusCode.OK });
|
|
141
|
+
}
|
|
142
|
+
actionSpan.end(new Date(action.ended_at));
|
|
143
|
+
}
|
|
144
|
+
for (const event of evaluationResultEvents(fact)) {
|
|
145
|
+
span.addEvent(
|
|
146
|
+
event.name,
|
|
147
|
+
event.attributes,
|
|
148
|
+
new Date(fact.ended_at ?? fact.started_at)
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
if (fact.status === "failed") {
|
|
152
|
+
span.addEvent("exception", {
|
|
153
|
+
"exception.type": fact.error_code ?? "agent_run_error",
|
|
154
|
+
"exception.message": fact.error_message ?? "Agent run failed"
|
|
155
|
+
});
|
|
156
|
+
span.setStatus({
|
|
157
|
+
code: api.SpanStatusCode.ERROR,
|
|
158
|
+
message: fact.error_message
|
|
159
|
+
});
|
|
160
|
+
if (fact.error_code) span.setAttribute("error.type", fact.error_code);
|
|
161
|
+
} else {
|
|
162
|
+
span.setStatus({ code: api.SpanStatusCode.OK });
|
|
163
|
+
}
|
|
164
|
+
span.end(new Date(fact.ended_at ?? fact.started_at));
|
|
165
|
+
this.#activeSpans.delete(fact.event_id);
|
|
166
|
+
}
|
|
167
|
+
#startSpan(api, tracer, fact, details) {
|
|
168
|
+
const parent = parentEventId(fact, details);
|
|
169
|
+
const parentContext = parent ? this.#activeSpans.get(parent)?.context : void 0;
|
|
170
|
+
const span = tracer.startSpan(
|
|
171
|
+
`invoke_agent ${fact.agent_external_id}`,
|
|
172
|
+
{
|
|
173
|
+
kind: api.SpanKind.INTERNAL,
|
|
174
|
+
startTime: new Date(fact.started_at),
|
|
175
|
+
attributes: runAttributes(fact)
|
|
176
|
+
},
|
|
177
|
+
parentContext
|
|
178
|
+
);
|
|
179
|
+
const active = {
|
|
180
|
+
api,
|
|
181
|
+
span,
|
|
182
|
+
context: api.trace.setSpan(parentContext ?? api.context.active(), span)
|
|
183
|
+
};
|
|
184
|
+
this.#activeSpans.set(fact.event_id, active);
|
|
185
|
+
return active;
|
|
186
|
+
}
|
|
187
|
+
updateActiveRun(_fact) {
|
|
188
|
+
}
|
|
189
|
+
trackProjection(projection) {
|
|
190
|
+
this.#pendingProjections.add(projection);
|
|
191
|
+
void projection.finally(() => this.#pendingProjections.delete(projection));
|
|
192
|
+
}
|
|
193
|
+
async drain(options = {}) {
|
|
194
|
+
if (this.#pendingProjections.size === 0 && this.#pendingEmissions.size === 0) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
await withTimeout(
|
|
198
|
+
Promise.allSettled([
|
|
199
|
+
...this.#pendingProjections,
|
|
200
|
+
...this.#pendingEmissions
|
|
201
|
+
]).then(() => void 0),
|
|
202
|
+
options.timeoutMs ?? 2e3
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
async close(options) {
|
|
206
|
+
await this.drain(options);
|
|
207
|
+
for (const { api, span } of this.#activeSpans.values()) {
|
|
208
|
+
span.setAttributes({
|
|
209
|
+
"orcha.run.status": "interrupted",
|
|
210
|
+
"error.type": "process_shutdown"
|
|
211
|
+
});
|
|
212
|
+
span.addEvent("exception", {
|
|
213
|
+
"exception.type": "process_shutdown",
|
|
214
|
+
"exception.message": "Orcha closed before the agent run emitted a terminal event."
|
|
215
|
+
});
|
|
216
|
+
span.setStatus({
|
|
217
|
+
code: api.SpanStatusCode.ERROR,
|
|
218
|
+
message: "Agent run interrupted by process shutdown"
|
|
219
|
+
});
|
|
220
|
+
span.end();
|
|
221
|
+
}
|
|
222
|
+
this.#activeSpans.clear();
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
class OtlpHttpTelemetryClient {
|
|
226
|
+
#endpoint;
|
|
227
|
+
#headers;
|
|
228
|
+
#queue = /* @__PURE__ */ new Map();
|
|
229
|
+
#activeRuns = /* @__PURE__ */ new Map();
|
|
230
|
+
#pendingProjections = /* @__PURE__ */ new Set();
|
|
231
|
+
#inFlight = /* @__PURE__ */ new Set();
|
|
232
|
+
#timer;
|
|
233
|
+
#attempt = 0;
|
|
234
|
+
#closed = false;
|
|
235
|
+
constructor(options) {
|
|
236
|
+
const endpoint = options.endpoint.trim().replace(/\/+$/, "");
|
|
237
|
+
if (!endpoint) {
|
|
238
|
+
throw new Error("telemetry.otel.endpoint must be a non-empty URL.");
|
|
239
|
+
}
|
|
240
|
+
this.#endpoint = endpoint.endsWith("/v1/traces") ? endpoint : `${endpoint}/v1/traces`;
|
|
241
|
+
this.#headers = options.headers ?? {};
|
|
242
|
+
}
|
|
243
|
+
enqueue(fact, details) {
|
|
244
|
+
if (this.#closed) return;
|
|
245
|
+
if (fact.status === "running") {
|
|
246
|
+
this.#activeRuns.set(fact.event_id, { fact, details });
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
this.#activeRuns.delete(fact.event_id);
|
|
250
|
+
if (!this.#queue.has(fact.event_id) && this.#queue.size >= MAX_QUEUED_RUNS) {
|
|
251
|
+
const oldest = this.#queue.keys().next().value;
|
|
252
|
+
if (oldest) this.#queue.delete(oldest);
|
|
253
|
+
console.warn(
|
|
254
|
+
"OpenTelemetry queue is full; dropping the oldest queued run."
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
this.#queue.set(fact.event_id, otlpSpans(fact, details));
|
|
258
|
+
if (this.#queue.size >= 20) {
|
|
259
|
+
void this.#flush();
|
|
260
|
+
} else {
|
|
261
|
+
this.#schedule();
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
updateActiveRun(fact) {
|
|
265
|
+
const active = this.#activeRuns.get(fact.event_id);
|
|
266
|
+
if (active) this.#activeRuns.set(fact.event_id, { ...active, fact });
|
|
267
|
+
}
|
|
268
|
+
trackProjection(projection) {
|
|
269
|
+
this.#pendingProjections.add(projection);
|
|
270
|
+
void projection.finally(() => this.#pendingProjections.delete(projection));
|
|
271
|
+
}
|
|
272
|
+
async drain(options = {}) {
|
|
273
|
+
const timeoutMs = Math.max(0, options.timeoutMs ?? 2e3);
|
|
274
|
+
const operation = (async () => {
|
|
275
|
+
await Promise.allSettled([...this.#pendingProjections]);
|
|
276
|
+
if (this.#timer) {
|
|
277
|
+
clearTimeout(this.#timer);
|
|
278
|
+
this.#timer = void 0;
|
|
279
|
+
}
|
|
280
|
+
while (this.#queue.size > 0) {
|
|
281
|
+
if (this.#inFlight.size >= MAX_CONCURRENT_EXPORTS) {
|
|
282
|
+
await Promise.allSettled([...this.#inFlight]);
|
|
283
|
+
}
|
|
284
|
+
const queuedBeforeFlush = this.#queue.size;
|
|
285
|
+
await this.#flush();
|
|
286
|
+
if (this.#queue.size >= queuedBeforeFlush) break;
|
|
287
|
+
}
|
|
288
|
+
await Promise.allSettled([...this.#inFlight]);
|
|
289
|
+
})();
|
|
290
|
+
await withTimeout(operation, timeoutMs);
|
|
291
|
+
}
|
|
292
|
+
async close(options) {
|
|
293
|
+
const endedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
294
|
+
for (const [eventId, active] of this.#activeRuns) {
|
|
295
|
+
this.#queue.set(
|
|
296
|
+
eventId,
|
|
297
|
+
otlpSpans(
|
|
298
|
+
{
|
|
299
|
+
...active.fact,
|
|
300
|
+
status: "failed",
|
|
301
|
+
ended_at: endedAt,
|
|
302
|
+
duration_ms: Math.max(
|
|
303
|
+
0,
|
|
304
|
+
new Date(endedAt).getTime() - new Date(active.fact.started_at).getTime()
|
|
305
|
+
),
|
|
306
|
+
error_code: "process_shutdown",
|
|
307
|
+
error_message: "Orcha closed before the agent run emitted a terminal event.",
|
|
308
|
+
error_retryable: true
|
|
309
|
+
},
|
|
310
|
+
active.details
|
|
311
|
+
)
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
this.#activeRuns.clear();
|
|
315
|
+
await this.drain(options);
|
|
316
|
+
this.#closed = true;
|
|
317
|
+
if (this.#timer) {
|
|
318
|
+
clearTimeout(this.#timer);
|
|
319
|
+
this.#timer = void 0;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
#schedule(delay = 250) {
|
|
323
|
+
if (this.#closed || this.#timer) return;
|
|
324
|
+
this.#timer = setTimeout(() => {
|
|
325
|
+
this.#timer = void 0;
|
|
326
|
+
void this.#flush();
|
|
327
|
+
}, delay);
|
|
328
|
+
const timer = this.#timer;
|
|
329
|
+
timer.unref?.();
|
|
330
|
+
}
|
|
331
|
+
async #flush() {
|
|
332
|
+
if (this.#queue.size === 0) return;
|
|
333
|
+
if (this.#inFlight.size >= MAX_CONCURRENT_EXPORTS) {
|
|
334
|
+
this.#schedule();
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
const entries = [...this.#queue.entries()].slice(0, 20);
|
|
338
|
+
for (const [id] of entries) this.#queue.delete(id);
|
|
339
|
+
const spans = entries.flatMap(([, value]) => value);
|
|
340
|
+
const body = JSON.stringify(otlpRequest(spans));
|
|
341
|
+
const operation = fetch(this.#endpoint, {
|
|
342
|
+
method: "POST",
|
|
343
|
+
headers: {
|
|
344
|
+
"content-type": "application/json",
|
|
345
|
+
...this.#headers
|
|
346
|
+
},
|
|
347
|
+
body,
|
|
348
|
+
keepalive: utf8ByteLength(body) <= 48 * 1024
|
|
349
|
+
}).then((response) => {
|
|
350
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
351
|
+
this.#attempt = 0;
|
|
352
|
+
}).catch((error) => {
|
|
353
|
+
this.#attempt += 1;
|
|
354
|
+
if (this.#attempt <= 6) {
|
|
355
|
+
for (const [id, value] of entries) this.#queue.set(id, value);
|
|
356
|
+
this.#schedule(Math.min(3e4, 500 * 2 ** this.#attempt));
|
|
357
|
+
} else {
|
|
358
|
+
this.#attempt = 0;
|
|
359
|
+
console.warn(
|
|
360
|
+
`OpenTelemetry delivery failed after 6 retries; dropping ${entries.length} run(s).`,
|
|
361
|
+
error
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
this.#inFlight.add(operation);
|
|
366
|
+
try {
|
|
367
|
+
await operation;
|
|
368
|
+
} finally {
|
|
369
|
+
this.#inFlight.delete(operation);
|
|
370
|
+
if (this.#queue.size > 0) this.#schedule();
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
function runAttributes(fact) {
|
|
375
|
+
return {
|
|
376
|
+
"gen_ai.operation.name": "invoke_agent",
|
|
377
|
+
"gen_ai.agent.id": fact.agent_external_id,
|
|
378
|
+
"gen_ai.agent.name": fact.agent_external_id,
|
|
379
|
+
"gen_ai.provider.name": fact.provider,
|
|
380
|
+
"gen_ai.request.model": fact.model,
|
|
381
|
+
"gen_ai.conversation.id": fact.session_id,
|
|
382
|
+
"gen_ai.usage.input_tokens": fact.input_tokens,
|
|
383
|
+
"gen_ai.usage.output_tokens": fact.output_tokens,
|
|
384
|
+
"orcha.run.number": fact.run_number,
|
|
385
|
+
"orcha.run.status": fact.status,
|
|
386
|
+
"orcha.span.type": "agent_run",
|
|
387
|
+
"orcha.agent.id": fact.agent_external_id,
|
|
388
|
+
"orcha.session.id": fact.session_id,
|
|
389
|
+
"orcha.run.duration_ms": fact.duration_ms ?? 0,
|
|
390
|
+
"orcha.run.model_duration_ms": fact.model_duration_ms,
|
|
391
|
+
"orcha.run.action_duration_ms": fact.action_duration_ms,
|
|
392
|
+
"orcha.usage.reasoning_tokens": fact.reasoning_tokens ?? 0,
|
|
393
|
+
"orcha.usage.cache_read_tokens": fact.cache_read_tokens,
|
|
394
|
+
"orcha.usage.cache_write_tokens": fact.cache_write_tokens,
|
|
395
|
+
"orcha.action.count": fact.action_count,
|
|
396
|
+
"orcha.action.success_count": fact.action_success_count,
|
|
397
|
+
"orcha.action.failure_count": fact.action_failure_count,
|
|
398
|
+
"orcha.subagent.count": fact.subagent_count,
|
|
399
|
+
"orcha.evaluation.count": fact.evaluation_count,
|
|
400
|
+
...fact.parent_agent_external_id ? { "orcha.parent.agent_id": fact.parent_agent_external_id } : {},
|
|
401
|
+
...fact.parent_session_id ? { "orcha.parent.session_id": fact.parent_session_id } : {},
|
|
402
|
+
...fact.stop_reason ? { "gen_ai.response.finish_reasons": [fact.stop_reason] } : {}
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
function modelCallAttributes(fact, modelCall) {
|
|
406
|
+
return {
|
|
407
|
+
"gen_ai.operation.name": "chat",
|
|
408
|
+
"gen_ai.provider.name": modelCall.provider,
|
|
409
|
+
"gen_ai.request.model": modelCall.model,
|
|
410
|
+
"gen_ai.response.model": modelCall.model,
|
|
411
|
+
"gen_ai.conversation.id": fact.session_id,
|
|
412
|
+
"gen_ai.agent.name": fact.agent_external_id,
|
|
413
|
+
"gen_ai.usage.input_tokens": modelCall.input_tokens,
|
|
414
|
+
"gen_ai.usage.output_tokens": modelCall.output_tokens,
|
|
415
|
+
"orcha.usage.reasoning_tokens": modelCall.reasoning_tokens,
|
|
416
|
+
"orcha.usage.cache_read_tokens": modelCall.cache_read_tokens,
|
|
417
|
+
"orcha.usage.cache_write_tokens": modelCall.cache_write_tokens,
|
|
418
|
+
"orcha.model.duration_ms": modelCall.duration_ms,
|
|
419
|
+
...modelCall.response_id ? { "gen_ai.response.id": modelCall.response_id } : {},
|
|
420
|
+
...modelCall.finish_reason ? { "gen_ai.response.finish_reasons": [modelCall.finish_reason] } : {}
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
function evaluationResultEvents(fact) {
|
|
424
|
+
const events = [];
|
|
425
|
+
for (const evaluation of fact.evaluations) {
|
|
426
|
+
if (evaluation.metrics.length === 0) {
|
|
427
|
+
events.push({
|
|
428
|
+
name: "gen_ai.evaluation.result",
|
|
429
|
+
attributes: {
|
|
430
|
+
"gen_ai.evaluation.name": evaluation.name,
|
|
431
|
+
"gen_ai.evaluation.score.label": evaluation.status,
|
|
432
|
+
"orcha.evaluation.duration_ms": evaluation.duration_ms,
|
|
433
|
+
...evaluation.error_message ? {
|
|
434
|
+
"error.type": "evaluation_error",
|
|
435
|
+
"orcha.evaluation.error_message": evaluation.error_message
|
|
436
|
+
} : {}
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
for (const metric of evaluation.metrics) {
|
|
442
|
+
events.push({
|
|
443
|
+
name: "gen_ai.evaluation.result",
|
|
444
|
+
attributes: {
|
|
445
|
+
"gen_ai.evaluation.name": metric.name,
|
|
446
|
+
"gen_ai.evaluation.score.value": metric.score,
|
|
447
|
+
"gen_ai.evaluation.score.label": metric.passed ? "pass" : "fail",
|
|
448
|
+
"gen_ai.evaluation.explanation": metric.reasoning,
|
|
449
|
+
"orcha.evaluation.name": evaluation.name,
|
|
450
|
+
"orcha.evaluation.duration_ms": evaluation.duration_ms,
|
|
451
|
+
...metric.evidence.length > 0 ? { "orcha.evaluation.evidence": metric.evidence } : {}
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return events;
|
|
457
|
+
}
|
|
458
|
+
function otlpSpans(fact, details) {
|
|
459
|
+
const rootSessionId = details?.lineage?.rootSessionId ?? fact.parent_session_id ?? fact.session_id;
|
|
460
|
+
const rootRunNumber = details?.lineage?.rootRunNumber ?? details?.lineage?.parentRunNumber ?? fact.run_number;
|
|
461
|
+
const traceId = stableHex(
|
|
462
|
+
`trace:${rootSessionId}:${rootRunNumber}`,
|
|
463
|
+
32
|
|
464
|
+
);
|
|
465
|
+
const rootSpanId = stableHex(`span:${fact.event_id}`, 16);
|
|
466
|
+
const parentId = parentEventId(fact, details);
|
|
467
|
+
const root = {
|
|
468
|
+
traceId,
|
|
469
|
+
spanId: rootSpanId,
|
|
470
|
+
...parentId ? { parentSpanId: stableHex(`span:${parentId}`, 16) } : {},
|
|
471
|
+
name: `invoke_agent ${fact.agent_external_id}`,
|
|
472
|
+
kind: 1,
|
|
473
|
+
startTimeUnixNano: unixNanos(fact.started_at),
|
|
474
|
+
endTimeUnixNano: unixNanos(fact.ended_at ?? fact.started_at),
|
|
475
|
+
attributes: otlpAttributes(runAttributes(fact)),
|
|
476
|
+
events: [
|
|
477
|
+
...evaluationResultEvents(fact).map((event) => ({
|
|
478
|
+
timeUnixNano: unixNanos(fact.ended_at ?? fact.started_at),
|
|
479
|
+
name: event.name,
|
|
480
|
+
attributes: otlpAttributes(event.attributes)
|
|
481
|
+
})),
|
|
482
|
+
...fact.status === "failed" ? [
|
|
483
|
+
{
|
|
484
|
+
timeUnixNano: unixNanos(fact.ended_at ?? fact.started_at),
|
|
485
|
+
name: "exception",
|
|
486
|
+
attributes: otlpAttributes({
|
|
487
|
+
"exception.type": fact.error_code ?? "agent_run_error",
|
|
488
|
+
"exception.message": fact.error_message ?? "Agent run failed"
|
|
489
|
+
})
|
|
490
|
+
}
|
|
491
|
+
] : []
|
|
492
|
+
],
|
|
493
|
+
status: fact.status === "failed" ? { code: 2, message: fact.error_message } : { code: 1 }
|
|
494
|
+
};
|
|
495
|
+
const modelCalls = (details?.modelCalls ?? []).map(
|
|
496
|
+
(modelCall, index) => ({
|
|
497
|
+
traceId,
|
|
498
|
+
spanId: stableHex(`model:${fact.event_id}:${index}`, 16),
|
|
499
|
+
parentSpanId: rootSpanId,
|
|
500
|
+
name: `chat ${modelCall.model}`,
|
|
501
|
+
kind: 3,
|
|
502
|
+
startTimeUnixNano: unixNanos(modelCall.started_at),
|
|
503
|
+
endTimeUnixNano: unixNanos(modelCall.ended_at),
|
|
504
|
+
attributes: otlpAttributes(modelCallAttributes(fact, modelCall)),
|
|
505
|
+
status: { code: 1 }
|
|
506
|
+
})
|
|
507
|
+
);
|
|
508
|
+
const actions = fact.actions.map(
|
|
509
|
+
(action) => ({
|
|
510
|
+
traceId,
|
|
511
|
+
spanId: stableHex(`action:${fact.event_id}:${action.call_id}`, 16),
|
|
512
|
+
parentSpanId: rootSpanId,
|
|
513
|
+
name: `execute_tool ${action.name}`,
|
|
514
|
+
kind: 1,
|
|
515
|
+
startTimeUnixNano: unixNanos(action.started_at),
|
|
516
|
+
endTimeUnixNano: unixNanos(action.ended_at),
|
|
517
|
+
attributes: otlpAttributes({
|
|
518
|
+
"gen_ai.operation.name": "execute_tool",
|
|
519
|
+
"gen_ai.tool.name": action.name,
|
|
520
|
+
"gen_ai.tool.type": "function",
|
|
521
|
+
"gen_ai.tool.call.id": action.call_id,
|
|
522
|
+
"gen_ai.agent.name": fact.agent_external_id,
|
|
523
|
+
"gen_ai.conversation.id": fact.session_id,
|
|
524
|
+
"orcha.span.type": "action",
|
|
525
|
+
"orcha.action.call_id": action.call_id,
|
|
526
|
+
"orcha.action.duration_ms": action.duration_ms,
|
|
527
|
+
...action.error_code ? { "error.type": action.error_code } : {}
|
|
528
|
+
}),
|
|
529
|
+
events: action.status === "failed" ? [
|
|
530
|
+
{
|
|
531
|
+
timeUnixNano: unixNanos(action.ended_at),
|
|
532
|
+
name: "exception",
|
|
533
|
+
attributes: otlpAttributes({
|
|
534
|
+
"exception.type": action.error_code ?? "action_error",
|
|
535
|
+
"exception.message": action.error_message ?? "Action failed"
|
|
536
|
+
})
|
|
537
|
+
}
|
|
538
|
+
] : void 0,
|
|
539
|
+
status: action.status === "failed" ? { code: 2, message: action.error_message } : { code: 1 }
|
|
540
|
+
})
|
|
541
|
+
);
|
|
542
|
+
return [root, ...modelCalls, ...actions];
|
|
543
|
+
}
|
|
544
|
+
function parentEventId(fact, details) {
|
|
545
|
+
if (!fact.parent_agent_external_id || !fact.parent_session_id || !details?.lineage?.parentRunNumber) {
|
|
546
|
+
return void 0;
|
|
547
|
+
}
|
|
548
|
+
return `${fact.parent_agent_external_id}:${fact.parent_session_id}:${details.lineage.parentRunNumber}`;
|
|
549
|
+
}
|
|
550
|
+
function otlpRequest(spans) {
|
|
551
|
+
return {
|
|
552
|
+
resourceSpans: [
|
|
553
|
+
{
|
|
554
|
+
resource: {
|
|
555
|
+
attributes: otlpAttributes({
|
|
556
|
+
"service.name": "orchajs",
|
|
557
|
+
"telemetry.sdk.language": "nodejs"
|
|
558
|
+
})
|
|
559
|
+
},
|
|
560
|
+
scopeSpans: [
|
|
561
|
+
{
|
|
562
|
+
scope: { name: "orchajs" },
|
|
563
|
+
spans
|
|
564
|
+
}
|
|
565
|
+
]
|
|
566
|
+
}
|
|
567
|
+
]
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
function otlpAttributes(attributes) {
|
|
571
|
+
const output = [];
|
|
572
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
573
|
+
if (value === void 0 || value === null) continue;
|
|
574
|
+
if (Array.isArray(value)) {
|
|
575
|
+
output.push({
|
|
576
|
+
key,
|
|
577
|
+
value: {
|
|
578
|
+
arrayValue: {
|
|
579
|
+
values: value.map((item) => ({ stringValue: String(item) }))
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
});
|
|
583
|
+
} else if (typeof value === "string") {
|
|
584
|
+
output.push({ key, value: { stringValue: value } });
|
|
585
|
+
} else if (typeof value === "boolean") {
|
|
586
|
+
output.push({ key, value: { boolValue: value } });
|
|
587
|
+
} else if (typeof value === "number") {
|
|
588
|
+
output.push({
|
|
589
|
+
key,
|
|
590
|
+
value: Number.isInteger(value) ? { intValue: String(value) } : { doubleValue: value }
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
return output;
|
|
595
|
+
}
|
|
596
|
+
function unixNanos(value) {
|
|
597
|
+
return (BigInt(new Date(value).getTime()) * 1000000n).toString();
|
|
598
|
+
}
|
|
599
|
+
function stableHex(value, length) {
|
|
600
|
+
let first = 0xcbf29ce484222325n;
|
|
601
|
+
let second = 0x84222325cbf29ce4n;
|
|
602
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
603
|
+
const code = BigInt(value.charCodeAt(index));
|
|
604
|
+
first = BigInt.asUintN(64, (first ^ code) * 0x100000001b3n);
|
|
605
|
+
second = BigInt.asUintN(64, (second ^ code + 31n) * 0x100000001b3n);
|
|
606
|
+
}
|
|
607
|
+
return `${first.toString(16).padStart(16, "0")}${second.toString(16).padStart(16, "0")}`.slice(0, length);
|
|
608
|
+
}
|
|
609
|
+
async function withTimeout(operation, timeoutMs) {
|
|
610
|
+
if (timeoutMs <= 0) return;
|
|
611
|
+
await Promise.race([
|
|
612
|
+
operation,
|
|
613
|
+
new Promise((resolve) => setTimeout(resolve, timeoutMs))
|
|
614
|
+
]);
|
|
615
|
+
}
|
|
616
|
+
function utf8ByteLength(value) {
|
|
617
|
+
if (typeof TextEncoder !== "undefined") {
|
|
618
|
+
return new TextEncoder().encode(value).byteLength;
|
|
619
|
+
}
|
|
620
|
+
return value.length;
|
|
621
|
+
}
|
|
622
|
+
let openTelemetryApiPromise;
|
|
623
|
+
function loadOpenTelemetryApi() {
|
|
624
|
+
openTelemetryApiPromise ??= runtimeImport("@opentelemetry/api");
|
|
625
|
+
return openTelemetryApiPromise;
|
|
626
|
+
}
|
|
627
|
+
const runtimeImport = new Function(
|
|
628
|
+
"specifier",
|
|
629
|
+
"return import(specifier)"
|
|
630
|
+
);
|
|
631
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
632
|
+
0 && (module.exports = {
|
|
633
|
+
CompositeRunTelemetryClient,
|
|
634
|
+
createOpenTelemetryClient
|
|
635
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "orchajs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.28.1",
|
|
4
4
|
"description": "A filesystem-convention, stateful agent framework for JavaScript.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -154,12 +154,12 @@
|
|
|
154
154
|
"node": ">=18.20.8"
|
|
155
155
|
},
|
|
156
156
|
"peerDependencies": {
|
|
157
|
-
"@orchajs/playground-ui": ">=0.2.5",
|
|
158
|
-
"@react-native-async-storage/async-storage": ">=2 <3",
|
|
159
157
|
"@aws-sdk/client-bedrock-runtime": ">=3.864.0 <4",
|
|
160
158
|
"@google/genai": ">=2.22.0 <3",
|
|
161
159
|
"@jitl/quickjs-singlefile-cjs-release-sync": ">=0.32.0 <0.33",
|
|
162
160
|
"@modelcontextprotocol/sdk": ">=1.32.1 <2",
|
|
161
|
+
"@orchajs/playground-ui": ">=0.2.5",
|
|
162
|
+
"@react-native-async-storage/async-storage": ">=2 <3",
|
|
163
163
|
"better-sqlite3": ">=11.10.0 <12",
|
|
164
164
|
"chokidar": ">=4 <5",
|
|
165
165
|
"dotenv": ">=16 <17",
|
|
@@ -221,10 +221,10 @@
|
|
|
221
221
|
"chokidar": "4.0.3",
|
|
222
222
|
"dotenv": "16.6.1",
|
|
223
223
|
"esbuild": "^0.25.9",
|
|
224
|
+
"quickjs-emscripten-core": "0.32.0",
|
|
224
225
|
"react": "^18.3.1",
|
|
225
226
|
"react-dom": "^18.3.1",
|
|
226
227
|
"react-markdown": "^10.1.0",
|
|
227
|
-
"quickjs-emscripten-core": "0.32.0",
|
|
228
228
|
"styletron-engine-atomic": "^1.6.2",
|
|
229
229
|
"styletron-react": "^6.1.1",
|
|
230
230
|
"typescript": "^5.9.2",
|
|
@@ -238,5 +238,8 @@
|
|
|
238
238
|
"@changesets/write": {
|
|
239
239
|
"human-id": "4.1.1"
|
|
240
240
|
}
|
|
241
|
+
},
|
|
242
|
+
"dependencies": {
|
|
243
|
+
"@opentelemetry/api": "^1.10.0"
|
|
241
244
|
}
|
|
242
245
|
}
|