orchajs 0.20.3 → 0.22.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 +103 -0
- package/dist/amarsia/analytics.d.ts +21 -0
- package/dist/amarsia/analytics.js +457 -0
- package/dist/amarsia/endpoint.d.ts +1 -0
- package/dist/amarsia/endpoint.js +8 -0
- package/dist/amarsia/sync.js +1 -4
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +13 -0
- package/dist/browser.d.ts +2 -0
- package/dist/browser.js +12 -0
- package/dist/cli.js +35 -9
- package/dist/compiler/build-project.d.ts +1 -0
- package/dist/compiler/build-project.js +75 -8
- package/dist/compiler/compile.d.ts +1 -1
- package/dist/compiler/compile.js +11 -9
- package/dist/errors.d.ts +5 -0
- package/dist/errors.js +102 -0
- package/dist/file-content.js +42 -6
- package/dist/index.d.ts +1 -5
- package/dist/index.js +1 -2
- package/dist/integrations/esbuild.js +5 -2
- package/dist/integrations/expo.d.ts +1 -0
- package/dist/integrations/expo.js +1 -0
- package/dist/integrations/metro.d.ts +15 -0
- package/dist/integrations/metro.js +25 -0
- package/dist/integrations/next.d.ts +23 -0
- package/dist/integrations/next.js +53 -0
- package/dist/integrations/runtime-build.d.ts +4 -0
- package/dist/integrations/runtime-build.js +32 -0
- package/dist/integrations/vite.d.ts +23 -0
- package/dist/integrations/vite.js +32 -0
- package/dist/integrations/webpack.d.ts +24 -0
- package/dist/integrations/webpack.js +21 -0
- package/dist/orcha.js +17 -8
- package/dist/playground/server.js +57 -7
- package/dist/project-loader.js +3 -0
- package/dist/providers/browser.d.ts +2 -0
- package/dist/providers/browser.js +20 -0
- package/dist/react-native.d.ts +2 -0
- package/dist/react-native.js +12 -0
- package/dist/runtime/actions/browser-native.d.ts +3 -0
- package/dist/runtime/actions/browser-native.js +28 -0
- package/dist/runtime/actions/execute.d.ts +7 -0
- package/dist/runtime/actions/execute.js +9 -2
- package/dist/runtime/agent.d.ts +1 -0
- package/dist/runtime/agent.js +38 -10
- package/dist/runtime/compiler-hook.d.ts +5 -0
- package/dist/runtime/compiler-hook.js +8 -0
- package/dist/runtime/create-orcha.d.ts +6 -1
- package/dist/runtime/create-orcha.js +35 -21
- package/dist/runtime/platform-browser.d.ts +2 -0
- package/dist/runtime/platform-browser.js +15 -0
- package/dist/runtime/platform-node.d.ts +2 -0
- package/dist/runtime/platform-node.js +31 -0
- package/dist/runtime/platform-react-native.d.ts +2 -0
- package/dist/runtime/platform-react-native.js +15 -0
- package/dist/runtime/platform.d.ts +15 -0
- package/dist/runtime/platform.js +1 -0
- package/dist/runtime/random-id.d.ts +1 -0
- package/dist/runtime/random-id.js +26 -0
- package/dist/runtime-entry.d.ts +5 -0
- package/dist/runtime-entry.js +2 -0
- package/dist/storage/browser-indexeddb.d.ts +7 -16
- package/dist/storage/browser-indexeddb.js +102 -19
- package/dist/storage/react-native-jsonl.d.ts +7 -16
- package/dist/storage/react-native-jsonl.js +65 -19
- package/dist/types.d.ts +54 -1
- package/dist-cjs/amarsia/analytics.js +447 -0
- package/dist-cjs/amarsia/endpoint.js +32 -0
- package/dist-cjs/amarsia/sync.js +2 -5
- package/dist-cjs/browser.js +48 -0
- package/dist-cjs/compiler/build-project.js +97 -8
- package/dist-cjs/compiler/compile.js +17 -9
- package/dist-cjs/errors.js +92 -2
- package/dist-cjs/file-content.js +42 -7
- package/dist-cjs/index.js +4 -9
- package/dist-cjs/integrations/esbuild.js +6 -2
- package/dist-cjs/integrations/expo.js +29 -0
- package/dist-cjs/integrations/metro.js +51 -0
- package/dist-cjs/integrations/next.js +75 -0
- package/dist-cjs/integrations/runtime-build.js +70 -0
- package/dist-cjs/integrations/vite.js +57 -0
- package/dist-cjs/integrations/webpack.js +51 -0
- package/dist-cjs/orcha.js +17 -12
- package/dist-cjs/playground/server.js +62 -9
- package/dist-cjs/project-loader.js +10 -0
- package/dist-cjs/providers/browser.js +61 -0
- package/dist-cjs/react-native.js +48 -0
- package/dist-cjs/runtime/actions/browser-native.js +68 -0
- package/dist-cjs/runtime/actions/execute.js +12 -2
- package/dist-cjs/runtime/agent.js +37 -14
- package/dist-cjs/runtime/compiler-hook.js +37 -0
- package/dist-cjs/runtime/create-orcha.js +34 -23
- package/dist-cjs/runtime/platform-browser.js +48 -0
- package/dist-cjs/runtime/platform-node.js +59 -0
- package/dist-cjs/runtime/platform-react-native.js +45 -0
- package/dist-cjs/runtime/platform.js +17 -0
- package/dist-cjs/runtime/random-id.js +55 -0
- package/dist-cjs/runtime-entry.js +34 -0
- package/dist-cjs/storage/browser-indexeddb.js +102 -5
- package/dist-cjs/storage/react-native-jsonl.js +73 -5
- package/dist-cjs/types/react-native-async-storage.d.js +2 -0
- package/package.json +69 -9
- package/dist/playground/public/app.css +0 -1
- package/dist/playground/public/app.js +0 -31
- package/dist/playground/public/index.html +0 -15
- package/dist/playground/public/orcha-logo.svg +0 -4
|
@@ -0,0 +1,447 @@
|
|
|
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 analytics_exports = {};
|
|
21
|
+
__export(analytics_exports, {
|
|
22
|
+
AmarsiaTelemetryClient: () => AmarsiaTelemetryClient,
|
|
23
|
+
withAmarsiaTelemetry: () => withAmarsiaTelemetry
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(analytics_exports);
|
|
26
|
+
var import_endpoint = require("./endpoint.js");
|
|
27
|
+
const RELEVANT_EVENTS = /* @__PURE__ */ new Set([
|
|
28
|
+
"run.started",
|
|
29
|
+
"run.completed",
|
|
30
|
+
"run.failed",
|
|
31
|
+
"run.paused",
|
|
32
|
+
"evaluation.completed",
|
|
33
|
+
"evaluation.failed"
|
|
34
|
+
]);
|
|
35
|
+
const MAX_BATCH_RUNS = 100;
|
|
36
|
+
const MAX_KEEPALIVE_BYTES = 48 * 1024;
|
|
37
|
+
const HEARTBEAT_INTERVAL_MS = 3e4;
|
|
38
|
+
class AmarsiaTelemetryClient {
|
|
39
|
+
#key;
|
|
40
|
+
#baseUrl;
|
|
41
|
+
#queue = /* @__PURE__ */ new Map();
|
|
42
|
+
#activeRuns = /* @__PURE__ */ new Map();
|
|
43
|
+
#timer;
|
|
44
|
+
#heartbeatTimer;
|
|
45
|
+
#attempt = 0;
|
|
46
|
+
#flushPromise;
|
|
47
|
+
constructor(options) {
|
|
48
|
+
this.#key = options.key;
|
|
49
|
+
this.#baseUrl = (0, import_endpoint.resolveAmarsiaEndpoint)(options.endpoint);
|
|
50
|
+
}
|
|
51
|
+
enqueue(fact) {
|
|
52
|
+
this.#queue.set(fact.event_id, fact);
|
|
53
|
+
if (fact.status === "running") {
|
|
54
|
+
this.#activeRuns.set(fact.event_id, fact);
|
|
55
|
+
this.#scheduleHeartbeat();
|
|
56
|
+
} else {
|
|
57
|
+
this.#activeRuns.delete(fact.event_id);
|
|
58
|
+
if (this.#activeRuns.size === 0 && this.#heartbeatTimer) {
|
|
59
|
+
clearTimeout(this.#heartbeatTimer);
|
|
60
|
+
this.#heartbeatTimer = void 0;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (fact.status !== "running") {
|
|
64
|
+
this.#scheduleTerminal();
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (this.#queue.size >= 20) {
|
|
68
|
+
void this.#flush();
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
this.#schedule(250);
|
|
72
|
+
}
|
|
73
|
+
#schedule(delay) {
|
|
74
|
+
if (this.#timer) return;
|
|
75
|
+
this.#timer = setTimeout(() => {
|
|
76
|
+
this.#timer = void 0;
|
|
77
|
+
void this.#flush();
|
|
78
|
+
}, delay);
|
|
79
|
+
const timer = this.#timer;
|
|
80
|
+
timer.unref?.();
|
|
81
|
+
}
|
|
82
|
+
#scheduleTerminal() {
|
|
83
|
+
if (this.#timer) clearTimeout(this.#timer);
|
|
84
|
+
this.#timer = setTimeout(() => {
|
|
85
|
+
this.#timer = void 0;
|
|
86
|
+
void this.#flush();
|
|
87
|
+
}, 0);
|
|
88
|
+
}
|
|
89
|
+
#scheduleHeartbeat() {
|
|
90
|
+
if (this.#heartbeatTimer || this.#activeRuns.size === 0) return;
|
|
91
|
+
this.#heartbeatTimer = setTimeout(() => {
|
|
92
|
+
this.#heartbeatTimer = void 0;
|
|
93
|
+
for (const fact of this.#activeRuns.values()) {
|
|
94
|
+
this.#queue.set(fact.event_id, fact);
|
|
95
|
+
}
|
|
96
|
+
void this.#flush();
|
|
97
|
+
this.#scheduleHeartbeat();
|
|
98
|
+
}, HEARTBEAT_INTERVAL_MS);
|
|
99
|
+
const timer = this.#heartbeatTimer;
|
|
100
|
+
timer.unref?.();
|
|
101
|
+
}
|
|
102
|
+
async drain(options = {}) {
|
|
103
|
+
const timeoutMs = Math.max(0, options.timeoutMs ?? 2e3);
|
|
104
|
+
const deadline = Date.now() + timeoutMs;
|
|
105
|
+
if (this.#timer) {
|
|
106
|
+
clearTimeout(this.#timer);
|
|
107
|
+
this.#timer = void 0;
|
|
108
|
+
}
|
|
109
|
+
while (this.#queue.size > 0 || this.#flushPromise) {
|
|
110
|
+
const remaining = deadline - Date.now();
|
|
111
|
+
if (remaining <= 0) return;
|
|
112
|
+
await Promise.race([
|
|
113
|
+
this.#flush(),
|
|
114
|
+
new Promise((resolve) => setTimeout(resolve, remaining))
|
|
115
|
+
]);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
async close(options = {}) {
|
|
119
|
+
if (this.#heartbeatTimer) {
|
|
120
|
+
clearTimeout(this.#heartbeatTimer);
|
|
121
|
+
this.#heartbeatTimer = void 0;
|
|
122
|
+
}
|
|
123
|
+
this.#activeRuns.clear();
|
|
124
|
+
await this.drain(options);
|
|
125
|
+
}
|
|
126
|
+
async #flush() {
|
|
127
|
+
if (this.#flushPromise) return this.#flushPromise;
|
|
128
|
+
if (this.#queue.size === 0) return;
|
|
129
|
+
this.#flushPromise = this.#performFlush();
|
|
130
|
+
try {
|
|
131
|
+
await this.#flushPromise;
|
|
132
|
+
} finally {
|
|
133
|
+
this.#flushPromise = void 0;
|
|
134
|
+
if (this.#queue.size > 0 && !this.#timer) {
|
|
135
|
+
const hasTerminalFact = [...this.#queue.values()].some(
|
|
136
|
+
(fact) => fact.status !== "running"
|
|
137
|
+
);
|
|
138
|
+
if (hasTerminalFact) {
|
|
139
|
+
void this.#flush();
|
|
140
|
+
} else {
|
|
141
|
+
this.#schedule(250);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async #performFlush() {
|
|
147
|
+
const batch = takeBatch(this.#queue);
|
|
148
|
+
if (batch.length === 0) return;
|
|
149
|
+
const body = JSON.stringify({
|
|
150
|
+
schema_version: 1,
|
|
151
|
+
runs: batch
|
|
152
|
+
});
|
|
153
|
+
for (const fact of batch) this.#queue.delete(fact.event_id);
|
|
154
|
+
try {
|
|
155
|
+
const response = await fetch(
|
|
156
|
+
`${this.#baseUrl}/v3/project-analytics/runs/batch`,
|
|
157
|
+
{
|
|
158
|
+
method: "POST",
|
|
159
|
+
headers: {
|
|
160
|
+
"content-type": "application/json",
|
|
161
|
+
"x-amarsia-project-key": this.#key
|
|
162
|
+
},
|
|
163
|
+
keepalive: utf8ByteLength(body) <= MAX_KEEPALIVE_BYTES,
|
|
164
|
+
body
|
|
165
|
+
}
|
|
166
|
+
);
|
|
167
|
+
if (!response.ok) {
|
|
168
|
+
throw new Error(`HTTP ${response.status}`);
|
|
169
|
+
}
|
|
170
|
+
this.#attempt = 0;
|
|
171
|
+
} catch {
|
|
172
|
+
this.#attempt += 1;
|
|
173
|
+
if (this.#attempt <= 6) {
|
|
174
|
+
for (const fact of batch) {
|
|
175
|
+
if (!this.#queue.has(fact.event_id)) {
|
|
176
|
+
this.#queue.set(fact.event_id, fact);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
this.#schedule(Math.min(3e4, 500 * 2 ** this.#attempt));
|
|
180
|
+
} else {
|
|
181
|
+
this.#attempt = 0;
|
|
182
|
+
console.warn(
|
|
183
|
+
`Amarsia telemetry delivery failed after 6 retries; dropping ${batch.length} run update(s).`
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function withAmarsiaTelemetry(options) {
|
|
190
|
+
const { store, manifest, client } = options;
|
|
191
|
+
const projections = /* @__PURE__ */ new Map();
|
|
192
|
+
const pending = /* @__PURE__ */ new Map();
|
|
193
|
+
return {
|
|
194
|
+
read: (sessionId) => store.read(sessionId),
|
|
195
|
+
listSessionIds: () => store.listSessionIds(),
|
|
196
|
+
describeLocation: store.describeLocation?.bind(store),
|
|
197
|
+
subscribe: store.subscribe?.bind(store),
|
|
198
|
+
subscribeAll: store.subscribeAll?.bind(store),
|
|
199
|
+
async append(sessionId, events) {
|
|
200
|
+
await store.append(sessionId, events);
|
|
201
|
+
const runs = relevantRuns(events);
|
|
202
|
+
if (runs.size === 0 && !projections.has(sessionId) && !pending.has(sessionId)) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
const previous = pending.get(sessionId) ?? Promise.resolve();
|
|
206
|
+
const processing = previous.then(async () => {
|
|
207
|
+
let projection = projections.get(sessionId);
|
|
208
|
+
if (!projection) {
|
|
209
|
+
projection = createProjection(await store.read(sessionId));
|
|
210
|
+
projections.set(sessionId, projection);
|
|
211
|
+
} else {
|
|
212
|
+
appendToProjection(projection, events);
|
|
213
|
+
}
|
|
214
|
+
if (projection.isTest) return;
|
|
215
|
+
for (const run of runs) {
|
|
216
|
+
const fact = projectRunFact(
|
|
217
|
+
manifest,
|
|
218
|
+
sessionId,
|
|
219
|
+
run,
|
|
220
|
+
projection
|
|
221
|
+
);
|
|
222
|
+
if (fact) client.enqueue(fact);
|
|
223
|
+
}
|
|
224
|
+
}).catch(() => void 0);
|
|
225
|
+
pending.set(sessionId, processing);
|
|
226
|
+
void processing.finally(() => {
|
|
227
|
+
if (pending.get(sessionId) === processing) {
|
|
228
|
+
pending.delete(sessionId);
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
function createProjection(events) {
|
|
235
|
+
const projection = {
|
|
236
|
+
seenSequences: /* @__PURE__ */ new Set(),
|
|
237
|
+
runs: /* @__PURE__ */ new Map(),
|
|
238
|
+
isTest: false
|
|
239
|
+
};
|
|
240
|
+
appendToProjection(projection, events);
|
|
241
|
+
return projection;
|
|
242
|
+
}
|
|
243
|
+
function appendToProjection(projection, events) {
|
|
244
|
+
for (const event of events) {
|
|
245
|
+
if (projection.seenSequences.has(event.sequence)) continue;
|
|
246
|
+
projection.seenSequences.add(event.sequence);
|
|
247
|
+
if (event.type === "session.created") {
|
|
248
|
+
projection.sessionCreated = event;
|
|
249
|
+
const metadata = asRecord(event.data.metadata);
|
|
250
|
+
projection.isTest = metadata?.orchaTest === true;
|
|
251
|
+
}
|
|
252
|
+
if (typeof event.run !== "number") continue;
|
|
253
|
+
const runEvents = projection.runs.get(event.run);
|
|
254
|
+
if (runEvents) {
|
|
255
|
+
runEvents.push(event);
|
|
256
|
+
} else {
|
|
257
|
+
projection.runs.set(event.run, [event]);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
function relevantRuns(events) {
|
|
262
|
+
const runs = /* @__PURE__ */ new Set();
|
|
263
|
+
for (const event of events) {
|
|
264
|
+
if (typeof event.run === "number" && RELEVANT_EVENTS.has(event.type)) {
|
|
265
|
+
runs.add(event.run);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return runs;
|
|
269
|
+
}
|
|
270
|
+
function projectRunFact(manifest, sessionId, run, projection) {
|
|
271
|
+
const runEvents = projection.runs.get(run);
|
|
272
|
+
if (!runEvents) return void 0;
|
|
273
|
+
const started = runEvents.find((event) => event.type === "run.started");
|
|
274
|
+
if (!started) return void 0;
|
|
275
|
+
const terminal = [...runEvents].reverse().find(
|
|
276
|
+
(event) => ["run.completed", "run.failed", "run.paused"].includes(event.type)
|
|
277
|
+
);
|
|
278
|
+
const lineage = asRecord(projection.sessionCreated?.data.lineage);
|
|
279
|
+
const assistants = runEvents.filter(
|
|
280
|
+
(event) => event.type === "message.created" && event.data.role === "assistant"
|
|
281
|
+
);
|
|
282
|
+
const usage = normalizeUsage(terminal?.data.usage, assistants);
|
|
283
|
+
const completedActions = runEvents.filter(
|
|
284
|
+
(event) => event.type === "action.completed"
|
|
285
|
+
);
|
|
286
|
+
const failedActions = runEvents.filter(
|
|
287
|
+
(event) => event.type === "action.failed"
|
|
288
|
+
);
|
|
289
|
+
const evaluations = runEvents.filter(
|
|
290
|
+
(event) => event.type === "evaluation.completed" || event.type === "evaluation.failed"
|
|
291
|
+
);
|
|
292
|
+
const error = asRecord(terminal?.data.error);
|
|
293
|
+
const lastAssistant = assistants.at(-1);
|
|
294
|
+
const status = terminal?.type === "run.completed" ? "completed" : terminal?.type === "run.failed" ? "failed" : terminal?.type === "run.paused" ? "paused" : "running";
|
|
295
|
+
return {
|
|
296
|
+
event_id: `${manifest.key}:${sessionId}:${run}`,
|
|
297
|
+
agent_external_id: manifest.key,
|
|
298
|
+
session_id: sessionId,
|
|
299
|
+
run_number: run,
|
|
300
|
+
...typeof lineage?.parentAgent === "string" ? { parent_agent_external_id: lineage.parentAgent } : {},
|
|
301
|
+
...typeof lineage?.parentSessionId === "string" ? { parent_session_id: lineage.parentSessionId } : {},
|
|
302
|
+
status,
|
|
303
|
+
provider: typeof started.data.provider === "string" ? started.data.provider : manifest.provider,
|
|
304
|
+
model: typeof started.data.model === "string" ? started.data.model : manifest.model,
|
|
305
|
+
...typeof started.data.region === "string" ? { region: started.data.region } : {},
|
|
306
|
+
...typeof started.data.outputType === "string" ? { output_type: started.data.outputType } : {},
|
|
307
|
+
...typeof lastAssistant?.data.stopReason === "string" ? { stop_reason: lastAssistant.data.stopReason } : {},
|
|
308
|
+
started_at: started.timestamp,
|
|
309
|
+
...terminal ? { ended_at: terminal.timestamp } : {},
|
|
310
|
+
...typeof terminal?.data.durationMs === "number" ? { duration_ms: terminal.data.durationMs } : {},
|
|
311
|
+
model_duration_ms: sumNumber(assistants, "durationMs"),
|
|
312
|
+
action_duration_ms: sumNumber(
|
|
313
|
+
[...completedActions, ...failedActions],
|
|
314
|
+
"durationMs"
|
|
315
|
+
),
|
|
316
|
+
...usage,
|
|
317
|
+
user_message_count: runEvents.filter(
|
|
318
|
+
(event) => event.type === "message.created" && event.data.role === "user"
|
|
319
|
+
).length,
|
|
320
|
+
assistant_message_count: assistants.length,
|
|
321
|
+
action_count: runEvents.filter(
|
|
322
|
+
(event) => event.type === "action.requested"
|
|
323
|
+
).length,
|
|
324
|
+
action_success_count: completedActions.length,
|
|
325
|
+
action_failure_count: failedActions.length,
|
|
326
|
+
subagent_count: runEvents.filter(
|
|
327
|
+
(event) => event.type === "subagent.initiated"
|
|
328
|
+
).length,
|
|
329
|
+
actions: projectActionLogs(runEvents),
|
|
330
|
+
evaluation_count: evaluations.length,
|
|
331
|
+
evaluation_pass_count: evaluations.filter(
|
|
332
|
+
(event) => event.type === "evaluation.completed" && event.data.status === "passed"
|
|
333
|
+
).length,
|
|
334
|
+
...typeof error?.code === "string" ? { error_code: error.code } : {},
|
|
335
|
+
...typeof error?.message === "string" ? { error_message: sanitizeErrorMessage(error.message) } : {},
|
|
336
|
+
...typeof error?.retryable === "boolean" ? { error_retryable: error.retryable } : {}
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
function normalizeUsage(terminalUsage, assistants) {
|
|
340
|
+
const direct = asRecord(terminalUsage);
|
|
341
|
+
if (direct) {
|
|
342
|
+
return usageFields(direct);
|
|
343
|
+
}
|
|
344
|
+
const total = {
|
|
345
|
+
inputTokens: 0,
|
|
346
|
+
outputTokens: 0,
|
|
347
|
+
reasoningTokens: 0,
|
|
348
|
+
cacheReadTokens: 0,
|
|
349
|
+
cacheWriteTokens: 0
|
|
350
|
+
};
|
|
351
|
+
for (const event of assistants) {
|
|
352
|
+
const usage = asRecord(event.data.usage);
|
|
353
|
+
if (!usage) continue;
|
|
354
|
+
total.inputTokens += numberValue(usage.inputTokens);
|
|
355
|
+
total.outputTokens += numberValue(usage.outputTokens);
|
|
356
|
+
total.reasoningTokens = (total.reasoningTokens ?? 0) + numberValue(usage.reasoningTokens);
|
|
357
|
+
total.cacheReadTokens += numberValue(usage.cacheReadTokens);
|
|
358
|
+
total.cacheWriteTokens += numberValue(usage.cacheWriteTokens);
|
|
359
|
+
}
|
|
360
|
+
return usageFields(total);
|
|
361
|
+
}
|
|
362
|
+
function usageFields(usage) {
|
|
363
|
+
const reasoning = numberValue(usage.reasoningTokens);
|
|
364
|
+
return {
|
|
365
|
+
input_tokens: numberValue(usage.inputTokens),
|
|
366
|
+
output_tokens: numberValue(usage.outputTokens),
|
|
367
|
+
...reasoning > 0 ? { reasoning_tokens: reasoning } : {},
|
|
368
|
+
cache_read_tokens: numberValue(usage.cacheReadTokens),
|
|
369
|
+
cache_write_tokens: numberValue(usage.cacheWriteTokens)
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
function sumNumber(events, key) {
|
|
373
|
+
return events.reduce((sum, event) => sum + numberValue(event.data[key]), 0);
|
|
374
|
+
}
|
|
375
|
+
function projectActionLogs(events) {
|
|
376
|
+
const requestedByCallId = /* @__PURE__ */ new Map();
|
|
377
|
+
for (const event of events) {
|
|
378
|
+
if (event.type === "action.requested" && typeof event.data.callId === "string") {
|
|
379
|
+
requestedByCallId.set(event.data.callId, event);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return events.filter(
|
|
383
|
+
(event) => event.type === "action.completed" || event.type === "action.failed"
|
|
384
|
+
).slice(0, 100).map((event) => {
|
|
385
|
+
const error = asRecord(event.data.error);
|
|
386
|
+
const requested = typeof event.data.callId === "string" ? requestedByCallId.get(event.data.callId) : void 0;
|
|
387
|
+
return {
|
|
388
|
+
call_id: typeof event.data.callId === "string" ? event.data.callId : `action-${event.sequence}`,
|
|
389
|
+
name: typeof event.data.name === "string" ? event.data.name : "unknown",
|
|
390
|
+
status: event.type === "action.completed" ? "completed" : "failed",
|
|
391
|
+
started_at: requested?.timestamp ?? event.timestamp,
|
|
392
|
+
ended_at: event.timestamp,
|
|
393
|
+
duration_ms: numberValue(event.data.durationMs),
|
|
394
|
+
...typeof error?.code === "string" ? { error_code: error.code } : {},
|
|
395
|
+
...typeof error?.message === "string" ? { error_message: sanitizeErrorMessage(error.message) } : {}
|
|
396
|
+
};
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
function takeBatch(queue) {
|
|
400
|
+
const batch = [];
|
|
401
|
+
let bytes = utf8ByteLength('{"schema_version":1,"runs":[]}');
|
|
402
|
+
for (const fact of queue.values()) {
|
|
403
|
+
const factBytes = utf8ByteLength(JSON.stringify(fact)) + 1;
|
|
404
|
+
if (batch.length > 0 && (batch.length >= MAX_BATCH_RUNS || bytes + factBytes > MAX_KEEPALIVE_BYTES)) {
|
|
405
|
+
break;
|
|
406
|
+
}
|
|
407
|
+
batch.push(fact);
|
|
408
|
+
bytes += factBytes;
|
|
409
|
+
}
|
|
410
|
+
return batch;
|
|
411
|
+
}
|
|
412
|
+
function utf8ByteLength(value) {
|
|
413
|
+
if (typeof TextEncoder !== "undefined") {
|
|
414
|
+
return new TextEncoder().encode(value).byteLength;
|
|
415
|
+
}
|
|
416
|
+
let bytes = 0;
|
|
417
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
418
|
+
const code = value.charCodeAt(index);
|
|
419
|
+
if (code < 128) bytes += 1;
|
|
420
|
+
else if (code < 2048) bytes += 2;
|
|
421
|
+
else if (code >= 55296 && code <= 56319) {
|
|
422
|
+
bytes += 4;
|
|
423
|
+
index += 1;
|
|
424
|
+
} else bytes += 3;
|
|
425
|
+
}
|
|
426
|
+
return bytes;
|
|
427
|
+
}
|
|
428
|
+
function numberValue(value) {
|
|
429
|
+
return typeof value === "number" && Number.isFinite(value) ? value : 0;
|
|
430
|
+
}
|
|
431
|
+
function asRecord(value) {
|
|
432
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
|
|
433
|
+
}
|
|
434
|
+
function sanitizeErrorMessage(value) {
|
|
435
|
+
return value.replace(/\bBearer\s+[A-Za-z0-9._~+/-]+=*/gi, "Bearer [redacted]").replace(
|
|
436
|
+
/\b(authorization|api[-_ ]?key|token|secret|password)\b\s*[:=]\s*[^\s,;]+/gi,
|
|
437
|
+
"$1=[redacted]"
|
|
438
|
+
).replace(
|
|
439
|
+
/\b(?:sk|pk|amarsia)_[A-Za-z0-9_-]{12,}\b/gi,
|
|
440
|
+
"[redacted]"
|
|
441
|
+
).slice(0, 8e3);
|
|
442
|
+
}
|
|
443
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
444
|
+
0 && (module.exports = {
|
|
445
|
+
AmarsiaTelemetryClient,
|
|
446
|
+
withAmarsiaTelemetry
|
|
447
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
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 endpoint_exports = {};
|
|
21
|
+
__export(endpoint_exports, {
|
|
22
|
+
resolveAmarsiaEndpoint: () => resolveAmarsiaEndpoint
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(endpoint_exports);
|
|
25
|
+
const DEFAULT_AMARSIA_API_URL = "https://api.amarsia.com";
|
|
26
|
+
function resolveAmarsiaEndpoint(endpoint) {
|
|
27
|
+
return (endpoint?.trim() || (typeof process !== "undefined" ? process.env?.AMARSIA_API_URL : void 0) || DEFAULT_AMARSIA_API_URL).replace(/\/$/, "");
|
|
28
|
+
}
|
|
29
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
30
|
+
0 && (module.exports = {
|
|
31
|
+
resolveAmarsiaEndpoint
|
|
32
|
+
});
|
package/dist-cjs/amarsia/sync.js
CHANGED
|
@@ -23,14 +23,14 @@ __export(sync_exports, {
|
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(sync_exports);
|
|
25
25
|
var import_metadata = require("./metadata.js");
|
|
26
|
-
|
|
26
|
+
var import_endpoint = require("./endpoint.js");
|
|
27
27
|
const syncedHashes = /* @__PURE__ */ new Map();
|
|
28
28
|
function syncAmarsiaProject(options) {
|
|
29
29
|
const metadata = (0, import_metadata.createAmarsiaProjectMetadata)(
|
|
30
30
|
options.bundle,
|
|
31
31
|
options.projectRoot
|
|
32
32
|
);
|
|
33
|
-
const baseUrl = resolveAmarsiaEndpoint(options.endpoint);
|
|
33
|
+
const baseUrl = (0, import_endpoint.resolveAmarsiaEndpoint)(options.endpoint);
|
|
34
34
|
const dedupeKey = `${baseUrl}:${options.key}`;
|
|
35
35
|
if (syncedHashes.get(dedupeKey) === metadata.content_hash) {
|
|
36
36
|
return;
|
|
@@ -46,9 +46,6 @@ function syncAmarsiaProject(options) {
|
|
|
46
46
|
console.warn(`[orcha] Amarsia project sync failed: ${message}`);
|
|
47
47
|
});
|
|
48
48
|
}
|
|
49
|
-
function resolveAmarsiaEndpoint(endpoint) {
|
|
50
|
-
return (endpoint?.trim() || process.env.AMARSIA_API_URL || DEFAULT_AMARSIA_API_URL).replace(/\/$/, "");
|
|
51
|
-
}
|
|
52
49
|
async function postProjectMetadata(options) {
|
|
53
50
|
const response = await fetch(`${options.baseUrl}/v3/project-sync`, {
|
|
54
51
|
method: "PUT",
|
|
@@ -0,0 +1,48 @@
|
|
|
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 __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
20
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
|
+
var browser_exports = {};
|
|
22
|
+
__export(browser_exports, {
|
|
23
|
+
orcha: () => orcha
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(browser_exports);
|
|
26
|
+
var import_browser = require("./providers/browser.js");
|
|
27
|
+
var import_execute = require("./runtime/actions/execute.js");
|
|
28
|
+
var import_create_orcha = require("./runtime/create-orcha.js");
|
|
29
|
+
var import_platform_browser = require("./runtime/platform-browser.js");
|
|
30
|
+
__reExport(browser_exports, require("./runtime-entry.js"), module.exports);
|
|
31
|
+
(0, import_execute.setNativeRuntimeLoader)(async () => {
|
|
32
|
+
const modulePath = "./runtime/actions/browser-native.js";
|
|
33
|
+
return import(modulePath);
|
|
34
|
+
});
|
|
35
|
+
const orcha = (0, import_create_orcha.createOrcha)(
|
|
36
|
+
() => {
|
|
37
|
+
throw new Error(
|
|
38
|
+
"The browser runtime requires a precompiled Orcha bundle. Configure the Orcha integration for your application bundler."
|
|
39
|
+
);
|
|
40
|
+
},
|
|
41
|
+
import_browser.generateBrowserProviderResponse,
|
|
42
|
+
import_platform_browser.browserRuntimePlatform
|
|
43
|
+
);
|
|
44
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
45
|
+
0 && (module.exports = {
|
|
46
|
+
orcha,
|
|
47
|
+
...require("./runtime-entry.js")
|
|
48
|
+
});
|