orchajs 0.27.1 → 0.27.3
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/dist/amarsia/analytics.d.ts +1 -0
- package/dist/amarsia/analytics.js +128 -50
- package/dist/amarsia/inference.js +4 -9
- package/dist/compiler/build-project.js +15 -4
- package/dist/compiler/compile.js +53 -11
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +128 -9
- package/dist/file-content.js +13 -1
- package/dist/providers/anthropic.js +6 -12
- package/dist/providers/bedrock.js +4 -8
- package/dist/providers/deepseek.js +6 -13
- package/dist/providers/dependencies.js +15 -4
- package/dist/providers/google-gemini.js +4 -9
- package/dist/providers/openai.js +6 -13
- package/dist/runtime/agent.js +177 -10
- package/dist/runtime/create-orcha.js +18 -17
- package/dist/runtime/structured-output.d.ts +18 -1
- package/dist/runtime/structured-output.js +75 -5
- package/dist/testing.js +23 -14
- package/dist/types.d.ts +1 -0
- package/dist-cjs/amarsia/analytics.js +129 -49
- package/dist-cjs/amarsia/inference.js +12 -9
- package/dist-cjs/compiler/build-project.js +14 -4
- package/dist-cjs/compiler/compile.js +65 -20
- package/dist-cjs/errors.js +106 -8
- package/dist-cjs/file-content.js +16 -1
- package/dist-cjs/providers/anthropic.js +19 -19
- package/dist-cjs/providers/bedrock.js +18 -17
- package/dist-cjs/providers/deepseek.js +13 -16
- package/dist-cjs/providers/dependencies.js +14 -4
- package/dist-cjs/providers/google-gemini.js +12 -9
- package/dist-cjs/providers/openai.js +13 -13
- package/dist-cjs/runtime/agent.js +164 -9
- package/dist-cjs/runtime/create-orcha.js +29 -26
- package/dist-cjs/runtime/structured-output.js +81 -5
- package/dist-cjs/testing.js +30 -16
- package/package.json +1 -1
|
@@ -7,6 +7,7 @@ export declare class AmarsiaTelemetryClient {
|
|
|
7
7
|
endpoint?: string;
|
|
8
8
|
});
|
|
9
9
|
enqueue(fact: AmarsiaRunFact): void;
|
|
10
|
+
updateActiveRun(fact: AmarsiaRunFact): void;
|
|
10
11
|
/** @internal Tracks telemetry projection work that must settle before drain completes. */
|
|
11
12
|
trackProjection(projection: Promise<void>): void;
|
|
12
13
|
drain(options?: {
|
|
@@ -7,20 +7,28 @@ const RELEVANT_EVENTS = new Set([
|
|
|
7
7
|
"evaluation.completed",
|
|
8
8
|
"evaluation.failed",
|
|
9
9
|
]);
|
|
10
|
+
const PROGRESS_EVENTS = new Set([
|
|
11
|
+
"message.created",
|
|
12
|
+
"action.completed",
|
|
13
|
+
"action.failed",
|
|
14
|
+
"subagent.completed",
|
|
15
|
+
"subagent.failed",
|
|
16
|
+
]);
|
|
10
17
|
const MAX_BATCH_RUNS = 100;
|
|
11
18
|
const MAX_KEEPALIVE_BYTES = 48 * 1024;
|
|
12
19
|
const HEARTBEAT_INTERVAL_MS = 30_000;
|
|
13
|
-
const
|
|
20
|
+
const MAX_CONCURRENT_FLUSHES = 2;
|
|
14
21
|
export class AmarsiaTelemetryClient {
|
|
15
22
|
#key;
|
|
16
23
|
#baseUrl;
|
|
17
24
|
#queue = new Map();
|
|
18
25
|
#activeRuns = new Map();
|
|
26
|
+
#terminalRuns = new Set();
|
|
19
27
|
#pendingProjections = new Set();
|
|
28
|
+
#inFlightFlushes = new Set();
|
|
20
29
|
#timer;
|
|
21
30
|
#heartbeatTimer;
|
|
22
31
|
#attempt = 0;
|
|
23
|
-
#flushPromise;
|
|
24
32
|
constructor(options) {
|
|
25
33
|
this.#key = options.key;
|
|
26
34
|
this.#baseUrl = resolveAmarsiaEndpoint(options.endpoint);
|
|
@@ -28,25 +36,38 @@ export class AmarsiaTelemetryClient {
|
|
|
28
36
|
enqueue(fact) {
|
|
29
37
|
this.#queue.set(fact.event_id, fact);
|
|
30
38
|
if (fact.status === "running") {
|
|
39
|
+
this.#terminalRuns.delete(fact.event_id);
|
|
31
40
|
this.#activeRuns.set(fact.event_id, fact);
|
|
32
41
|
this.#scheduleHeartbeat();
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (this.#activeRuns.size === 0 && this.#heartbeatTimer) {
|
|
37
|
-
clearTimeout(this.#heartbeatTimer);
|
|
38
|
-
this.#heartbeatTimer = undefined;
|
|
42
|
+
if (this.#queue.size >= 20) {
|
|
43
|
+
void this.#flush();
|
|
44
|
+
return;
|
|
39
45
|
}
|
|
40
|
-
|
|
41
|
-
if (fact.status !== "running") {
|
|
42
|
-
this.#scheduleTerminal();
|
|
46
|
+
this.#schedule(250);
|
|
43
47
|
return;
|
|
44
48
|
}
|
|
45
|
-
|
|
46
|
-
|
|
49
|
+
this.#terminalRuns.add(fact.event_id);
|
|
50
|
+
this.#activeRuns.delete(fact.event_id);
|
|
51
|
+
if (this.#activeRuns.size === 0 && this.#heartbeatTimer) {
|
|
52
|
+
clearTimeout(this.#heartbeatTimer);
|
|
53
|
+
this.#heartbeatTimer = undefined;
|
|
54
|
+
}
|
|
55
|
+
if (this.#timer) {
|
|
56
|
+
clearTimeout(this.#timer);
|
|
57
|
+
this.#timer = undefined;
|
|
58
|
+
}
|
|
59
|
+
void this.#flush();
|
|
60
|
+
}
|
|
61
|
+
updateActiveRun(fact) {
|
|
62
|
+
if (fact.status !== "running" || this.#terminalRuns.has(fact.event_id)) {
|
|
47
63
|
return;
|
|
48
64
|
}
|
|
49
|
-
this.#
|
|
65
|
+
if (this.#activeRuns.has(fact.event_id)) {
|
|
66
|
+
this.#activeRuns.set(fact.event_id, fact);
|
|
67
|
+
}
|
|
68
|
+
if (this.#queue.has(fact.event_id)) {
|
|
69
|
+
this.#queue.set(fact.event_id, fact);
|
|
70
|
+
}
|
|
50
71
|
}
|
|
51
72
|
#schedule(delay) {
|
|
52
73
|
if (this.#timer)
|
|
@@ -65,21 +86,15 @@ export class AmarsiaTelemetryClient {
|
|
|
65
86
|
this.#pendingProjections.delete(projection);
|
|
66
87
|
});
|
|
67
88
|
}
|
|
68
|
-
#scheduleTerminal() {
|
|
69
|
-
if (this.#timer)
|
|
70
|
-
clearTimeout(this.#timer);
|
|
71
|
-
this.#timer = setTimeout(() => {
|
|
72
|
-
this.#timer = undefined;
|
|
73
|
-
void this.#flush();
|
|
74
|
-
}, 0);
|
|
75
|
-
}
|
|
76
89
|
#scheduleHeartbeat() {
|
|
77
90
|
if (this.#heartbeatTimer || this.#activeRuns.size === 0)
|
|
78
91
|
return;
|
|
79
92
|
this.#heartbeatTimer = setTimeout(() => {
|
|
80
93
|
this.#heartbeatTimer = undefined;
|
|
81
94
|
for (const fact of this.#activeRuns.values()) {
|
|
82
|
-
this.#
|
|
95
|
+
if (!this.#terminalRuns.has(fact.event_id)) {
|
|
96
|
+
this.#queue.set(fact.event_id, fact);
|
|
97
|
+
}
|
|
83
98
|
}
|
|
84
99
|
void this.#flush();
|
|
85
100
|
this.#scheduleHeartbeat();
|
|
@@ -92,7 +107,7 @@ export class AmarsiaTelemetryClient {
|
|
|
92
107
|
const deadline = Date.now() + timeoutMs;
|
|
93
108
|
while (this.#pendingProjections.size > 0 ||
|
|
94
109
|
this.#queue.size > 0 ||
|
|
95
|
-
this.#
|
|
110
|
+
this.#inFlightFlushes.size > 0) {
|
|
96
111
|
const remaining = deadline - Date.now();
|
|
97
112
|
if (remaining <= 0)
|
|
98
113
|
return;
|
|
@@ -107,10 +122,19 @@ export class AmarsiaTelemetryClient {
|
|
|
107
122
|
clearTimeout(this.#timer);
|
|
108
123
|
this.#timer = undefined;
|
|
109
124
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
125
|
+
if (this.#queue.size > 0) {
|
|
126
|
+
await Promise.race([
|
|
127
|
+
this.#flush(),
|
|
128
|
+
new Promise((resolve) => setTimeout(resolve, remaining)),
|
|
129
|
+
]);
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (this.#inFlightFlushes.size > 0) {
|
|
133
|
+
await Promise.race([
|
|
134
|
+
Promise.allSettled([...this.#inFlightFlushes]).then(() => undefined),
|
|
135
|
+
new Promise((resolve) => setTimeout(resolve, remaining)),
|
|
136
|
+
]);
|
|
137
|
+
}
|
|
114
138
|
}
|
|
115
139
|
}
|
|
116
140
|
async close(options = {}) {
|
|
@@ -127,22 +151,31 @@ export class AmarsiaTelemetryClient {
|
|
|
127
151
|
this.#activeRuns.clear();
|
|
128
152
|
}
|
|
129
153
|
async #flush() {
|
|
130
|
-
if (this.#
|
|
131
|
-
|
|
132
|
-
|
|
154
|
+
if (this.#queue.size === 0) {
|
|
155
|
+
if (this.#inFlightFlushes.size > 0) {
|
|
156
|
+
await Promise.allSettled([...this.#inFlightFlushes]);
|
|
157
|
+
}
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const hasTerminalFact = [...this.#queue.values()].some((fact) => fact.status !== "running");
|
|
161
|
+
const maxConcurrent = hasTerminalFact ? MAX_CONCURRENT_FLUSHES : 1;
|
|
162
|
+
if (this.#inFlightFlushes.size >= maxConcurrent) {
|
|
163
|
+
await Promise.allSettled([...this.#inFlightFlushes]);
|
|
133
164
|
return;
|
|
134
|
-
|
|
165
|
+
}
|
|
166
|
+
const flushPromise = this.#performFlush();
|
|
167
|
+
this.#inFlightFlushes.add(flushPromise);
|
|
135
168
|
try {
|
|
136
|
-
await
|
|
169
|
+
await flushPromise;
|
|
137
170
|
}
|
|
138
171
|
finally {
|
|
139
|
-
this.#flushPromise
|
|
172
|
+
this.#inFlightFlushes.delete(flushPromise);
|
|
140
173
|
if (this.#queue.size > 0 && !this.#timer) {
|
|
141
|
-
const
|
|
142
|
-
if (
|
|
174
|
+
const remainingHasTerminal = [...this.#queue.values()].some((fact) => fact.status !== "running");
|
|
175
|
+
if (remainingHasTerminal) {
|
|
143
176
|
void this.#flush();
|
|
144
177
|
}
|
|
145
|
-
else {
|
|
178
|
+
else if (this.#inFlightFlushes.size === 0) {
|
|
146
179
|
this.#schedule(250);
|
|
147
180
|
}
|
|
148
181
|
}
|
|
@@ -177,6 +210,10 @@ export class AmarsiaTelemetryClient {
|
|
|
177
210
|
this.#attempt += 1;
|
|
178
211
|
if (this.#attempt <= 6) {
|
|
179
212
|
for (const fact of batch) {
|
|
213
|
+
if (fact.status === "running" &&
|
|
214
|
+
this.#terminalRuns.has(fact.event_id)) {
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
180
217
|
if (!this.#queue.has(fact.event_id)) {
|
|
181
218
|
this.#queue.set(fact.event_id, fact);
|
|
182
219
|
}
|
|
@@ -215,29 +252,67 @@ export function withAmarsiaTelemetry(options) {
|
|
|
215
252
|
async append(sessionId, events) {
|
|
216
253
|
await store.append(sessionId, events);
|
|
217
254
|
const runs = relevantRuns(events);
|
|
255
|
+
const progressRuns = progressOnlyRuns(events, runs);
|
|
256
|
+
const hasSessionCreated = events.some((event) => event.type === "session.created");
|
|
218
257
|
if (runs.size === 0 &&
|
|
258
|
+
progressRuns.size === 0 &&
|
|
259
|
+
!hasSessionCreated &&
|
|
219
260
|
!projections.has(sessionId) &&
|
|
220
261
|
!pending.has(sessionId)) {
|
|
221
262
|
return;
|
|
222
263
|
}
|
|
264
|
+
if (!pending.has(sessionId)) {
|
|
265
|
+
let projection = projections.get(sessionId);
|
|
266
|
+
let hasNewEvents = false;
|
|
267
|
+
if (!projection && hasSessionCreated) {
|
|
268
|
+
projection = createProjection(events);
|
|
269
|
+
projections.set(sessionId, projection);
|
|
270
|
+
hasNewEvents = projection.seenSequences.size > 0;
|
|
271
|
+
}
|
|
272
|
+
else if (projection) {
|
|
273
|
+
hasNewEvents = appendToProjection(projection, events);
|
|
274
|
+
}
|
|
275
|
+
if (projection) {
|
|
276
|
+
if (projection.isTest || !hasNewEvents)
|
|
277
|
+
return;
|
|
278
|
+
for (const run of runs) {
|
|
279
|
+
const fact = projectRunFact(manifest, sessionId, run, projection);
|
|
280
|
+
if (fact)
|
|
281
|
+
client.enqueue(fact);
|
|
282
|
+
}
|
|
283
|
+
for (const run of progressRuns) {
|
|
284
|
+
const fact = projectRunFact(manifest, sessionId, run, projection);
|
|
285
|
+
if (fact)
|
|
286
|
+
client.updateActiveRun(fact);
|
|
287
|
+
}
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
223
291
|
const previous = pending.get(sessionId) ?? Promise.resolve();
|
|
224
292
|
const processing = previous
|
|
225
293
|
.then(async () => {
|
|
226
294
|
let projection = projections.get(sessionId);
|
|
295
|
+
let hasNewEvents = false;
|
|
227
296
|
if (!projection) {
|
|
228
297
|
projection = createProjection(await store.read(sessionId));
|
|
229
298
|
projections.set(sessionId, projection);
|
|
299
|
+
hasNewEvents = projection.seenSequences.size > 0;
|
|
230
300
|
}
|
|
231
301
|
else {
|
|
232
|
-
appendToProjection(projection, events);
|
|
302
|
+
hasNewEvents = appendToProjection(projection, events);
|
|
233
303
|
}
|
|
234
|
-
if (projection.isTest)
|
|
304
|
+
if (projection.isTest || !hasNewEvents)
|
|
235
305
|
return;
|
|
236
306
|
for (const run of runs) {
|
|
237
307
|
const fact = projectRunFact(manifest, sessionId, run, projection);
|
|
238
308
|
if (fact)
|
|
239
309
|
client.enqueue(fact);
|
|
240
310
|
}
|
|
311
|
+
for (const run of progressRuns) {
|
|
312
|
+
const fact = projectRunFact(manifest, sessionId, run, projection);
|
|
313
|
+
if (fact)
|
|
314
|
+
client.updateActiveRun(fact);
|
|
315
|
+
}
|
|
241
316
|
})
|
|
242
317
|
.catch(() => undefined);
|
|
243
318
|
pending.set(sessionId, processing);
|
|
@@ -247,17 +322,6 @@ export function withAmarsiaTelemetry(options) {
|
|
|
247
322
|
pending.delete(sessionId);
|
|
248
323
|
}
|
|
249
324
|
});
|
|
250
|
-
const hasTerminalRunEvent = events.some((event) => event.type === "run.completed" ||
|
|
251
|
-
event.type === "run.failed" ||
|
|
252
|
-
event.type === "run.paused");
|
|
253
|
-
const hasEvaluationResult = events.some((event) => event.type === "evaluation.completed" ||
|
|
254
|
-
event.type === "evaluation.failed");
|
|
255
|
-
if (hasTerminalRunEvent || hasEvaluationResult) {
|
|
256
|
-
await processing;
|
|
257
|
-
}
|
|
258
|
-
if (hasEvaluationResult) {
|
|
259
|
-
await client.drain({ timeoutMs: EVALUATION_FLUSH_TIMEOUT_MS });
|
|
260
|
-
}
|
|
261
325
|
},
|
|
262
326
|
};
|
|
263
327
|
}
|
|
@@ -271,10 +335,12 @@ function createProjection(events) {
|
|
|
271
335
|
return projection;
|
|
272
336
|
}
|
|
273
337
|
function appendToProjection(projection, events) {
|
|
338
|
+
let added = false;
|
|
274
339
|
for (const event of events) {
|
|
275
340
|
if (projection.seenSequences.has(event.sequence))
|
|
276
341
|
continue;
|
|
277
342
|
projection.seenSequences.add(event.sequence);
|
|
343
|
+
added = true;
|
|
278
344
|
if (event.type === "session.created") {
|
|
279
345
|
projection.sessionCreated = event;
|
|
280
346
|
const metadata = asRecord(event.data.metadata);
|
|
@@ -290,6 +356,7 @@ function appendToProjection(projection, events) {
|
|
|
290
356
|
projection.runs.set(event.run, [event]);
|
|
291
357
|
}
|
|
292
358
|
}
|
|
359
|
+
return added;
|
|
293
360
|
}
|
|
294
361
|
function relevantRuns(events) {
|
|
295
362
|
const runs = new Set();
|
|
@@ -300,6 +367,17 @@ function relevantRuns(events) {
|
|
|
300
367
|
}
|
|
301
368
|
return runs;
|
|
302
369
|
}
|
|
370
|
+
function progressOnlyRuns(events, relevant) {
|
|
371
|
+
const runs = new Set();
|
|
372
|
+
for (const event of events) {
|
|
373
|
+
if (typeof event.run === "number" &&
|
|
374
|
+
!relevant.has(event.run) &&
|
|
375
|
+
PROGRESS_EVENTS.has(event.type)) {
|
|
376
|
+
runs.add(event.run);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return runs;
|
|
380
|
+
}
|
|
303
381
|
function projectRunFact(manifest, sessionId, run, projection) {
|
|
304
382
|
const runEvents = projection.runs.get(run);
|
|
305
383
|
if (!runEvents)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { OrchaError } from "../errors.js";
|
|
2
2
|
import { readLocalFileContent } from "../file-content.js";
|
|
3
3
|
import { readServerSentEvents } from "../providers/sse.js";
|
|
4
|
-
import {
|
|
4
|
+
import { resolveProviderStructuredOutput } from "../runtime/structured-output.js";
|
|
5
5
|
import { resolveAmarsiaEndpoint } from "./endpoint.js";
|
|
6
6
|
const SUPPORTED_AMARSIA_PROVIDERS = new Set([
|
|
7
7
|
"openai",
|
|
@@ -309,22 +309,17 @@ function toProviderResponse(request, response) {
|
|
|
309
309
|
.map((block) => block.text)
|
|
310
310
|
.join("");
|
|
311
311
|
const stopReason = normalizeStopReason(response, toolCalls, refused);
|
|
312
|
-
const output = request.outputType === "json" &&
|
|
313
|
-
toolCalls.length === 0 &&
|
|
314
|
-
stopReason !== "max_tokens"
|
|
315
|
-
? parseStructuredOutput(textOutput, request.outputSchema ?? {})
|
|
316
|
-
: textOutput;
|
|
317
312
|
if (textOutput) {
|
|
318
313
|
request.publishOutput?.(textOutput);
|
|
319
314
|
}
|
|
320
|
-
return {
|
|
321
|
-
output,
|
|
315
|
+
return resolveProviderStructuredOutput(request, {
|
|
316
|
+
output: textOutput,
|
|
322
317
|
responseId: response.id,
|
|
323
318
|
stopReason,
|
|
324
319
|
content,
|
|
325
320
|
toolCalls,
|
|
326
321
|
usage: normalizeUsage(response.usage),
|
|
327
|
-
};
|
|
322
|
+
}, textOutput);
|
|
328
323
|
}
|
|
329
324
|
function replay(item) {
|
|
330
325
|
const value = {
|
|
@@ -290,10 +290,21 @@ function createNodeStorageExports() {
|
|
|
290
290
|
].join("\n");
|
|
291
291
|
}
|
|
292
292
|
function allCompiledAgents(agents) {
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
293
|
+
const collected = [];
|
|
294
|
+
const visited = new Set();
|
|
295
|
+
const visit = (agent) => {
|
|
296
|
+
if (visited.has(agent))
|
|
297
|
+
return;
|
|
298
|
+
visited.add(agent);
|
|
299
|
+
collected.push(agent);
|
|
300
|
+
for (const child of Object.values(agent.subagents ?? {})) {
|
|
301
|
+
visit(child);
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
for (const agent of Object.values(agents)) {
|
|
305
|
+
visit(agent);
|
|
306
|
+
}
|
|
307
|
+
return collected;
|
|
297
308
|
}
|
|
298
309
|
function createProviderRuntimeSource(providerNames, target) {
|
|
299
310
|
const providersDirectory = resolve(esmCompilerDirectory(), "../providers");
|
package/dist/compiler/compile.js
CHANGED
|
@@ -10,22 +10,55 @@ export function compileRegistry(registrations, registryRoot, actionRuntime = "na
|
|
|
10
10
|
Array.isArray(registrations)) {
|
|
11
11
|
throw new TypeError('orcha.init() requires an "agents" object of registered folder paths.');
|
|
12
12
|
}
|
|
13
|
-
const
|
|
13
|
+
const normalizedRegistrations = {};
|
|
14
|
+
const registrationsByPath = new Map();
|
|
14
15
|
for (const [key, registration] of Object.entries(registrations)) {
|
|
15
16
|
if (!key.trim()) {
|
|
16
17
|
throw new Error("Registered agent names cannot be empty.");
|
|
17
18
|
}
|
|
18
19
|
const normalized = normalizeAgentRegistration(key, registration);
|
|
20
|
+
normalizedRegistrations[key] = normalized;
|
|
21
|
+
const resolvedPath = resolve(registryRoot, normalized.path);
|
|
22
|
+
if (!registrationsByPath.has(resolvedPath)) {
|
|
23
|
+
registrationsByPath.set(resolvedPath, {
|
|
24
|
+
key,
|
|
25
|
+
registration: normalized,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (Object.keys(normalizedRegistrations).length === 0) {
|
|
30
|
+
throw new Error("At least one agent must be registered.");
|
|
31
|
+
}
|
|
32
|
+
const compileRegistrationTree = (key, normalized, ownerPath, ancestry) => {
|
|
33
|
+
const resolvedPath = resolve(registryRoot, normalized.path);
|
|
34
|
+
const inherited = normalizedRegistrations[key] ??
|
|
35
|
+
registrationsByPath.get(resolvedPath)?.registration;
|
|
36
|
+
const effectiveMcp = Object.keys(normalized.mcp).length > 0
|
|
37
|
+
? normalized.mcp
|
|
38
|
+
: (inherited?.mcp ?? {});
|
|
39
|
+
const effectiveSubagentRegistrations = Object.keys(normalized.subagents).length > 0
|
|
40
|
+
? normalized.subagents
|
|
41
|
+
: (inherited?.subagents ?? {});
|
|
19
42
|
const subagents = {};
|
|
20
|
-
for (const [subagentKey,
|
|
43
|
+
for (const [subagentKey, subagentRegistration] of Object.entries(effectiveSubagentRegistrations)) {
|
|
21
44
|
if (!subagentKey.trim()) {
|
|
22
|
-
throw new Error(`Agent "${
|
|
45
|
+
throw new Error(`Agent "${ownerPath}" has an empty subagent name.`);
|
|
23
46
|
}
|
|
24
|
-
const
|
|
25
|
-
|
|
47
|
+
const normalizedSubagent = normalizeSubagentRegistration(ownerPath, subagentKey, subagentRegistration);
|
|
48
|
+
const childResolvedPath = resolve(registryRoot, normalizedSubagent.path);
|
|
49
|
+
const cycleExists = ancestry.some((ancestor) => ancestor.resolvedPath === childResolvedPath ||
|
|
50
|
+
ancestor.key === subagentKey);
|
|
51
|
+
if (cycleExists) {
|
|
52
|
+
const chain = [
|
|
53
|
+
...ancestry.map((item) => item.key),
|
|
54
|
+
subagentKey,
|
|
55
|
+
].join(" -> ");
|
|
56
|
+
throw new Error(`Circular subagent registration detected: ${chain}.`);
|
|
57
|
+
}
|
|
58
|
+
subagents[subagentKey] = compileRegistrationTree(subagentKey, normalizedSubagent, `${ownerPath}/${subagentKey}`, [...ancestry, { key: subagentKey, resolvedPath: childResolvedPath }]);
|
|
26
59
|
}
|
|
27
|
-
const parent = compileAgent(key, normalized.path,
|
|
28
|
-
|
|
60
|
+
const parent = compileAgent(key, normalized.path, effectiveMcp, registryRoot, actionRuntime, target);
|
|
61
|
+
return Object.freeze({
|
|
29
62
|
...parent,
|
|
30
63
|
subagentPolicy: Object.keys(subagents).length > 0
|
|
31
64
|
? Object.freeze({
|
|
@@ -35,9 +68,11 @@ export function compileRegistry(registrations, registryRoot, actionRuntime = "na
|
|
|
35
68
|
: parent.subagentPolicy,
|
|
36
69
|
subagents: Object.freeze(subagents),
|
|
37
70
|
});
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
71
|
+
};
|
|
72
|
+
const agents = {};
|
|
73
|
+
for (const [key, normalized] of Object.entries(normalizedRegistrations)) {
|
|
74
|
+
const resolvedPath = resolve(registryRoot, normalized.path);
|
|
75
|
+
agents[key] = compileRegistrationTree(key, normalized, key, [{ key, resolvedPath }]);
|
|
41
76
|
}
|
|
42
77
|
return Object.freeze({
|
|
43
78
|
schemaVersion: 1,
|
|
@@ -76,7 +111,7 @@ function normalizeSubagentRegistration(parentKey, key, registration) {
|
|
|
76
111
|
if (!registration.trim()) {
|
|
77
112
|
throw new Error(`Subagent "${parentKey}/${key}" must register a folder path.`);
|
|
78
113
|
}
|
|
79
|
-
return { path: registration, mcp: {} };
|
|
114
|
+
return { path: registration, mcp: {}, subagents: {} };
|
|
80
115
|
}
|
|
81
116
|
if (!registration ||
|
|
82
117
|
typeof registration !== "object" ||
|
|
@@ -84,10 +119,17 @@ function normalizeSubagentRegistration(parentKey, key, registration) {
|
|
|
84
119
|
!registration.path.trim()) {
|
|
85
120
|
throw new Error(`Subagent "${parentKey}/${key}" must register a folder path.`);
|
|
86
121
|
}
|
|
122
|
+
if (registration.subagents !== undefined &&
|
|
123
|
+
(!registration.subagents ||
|
|
124
|
+
typeof registration.subagents !== "object" ||
|
|
125
|
+
Array.isArray(registration.subagents))) {
|
|
126
|
+
throw new Error(`Subagent "${parentKey}/${key}" subagents must be an object of named registrations.`);
|
|
127
|
+
}
|
|
87
128
|
validateMcpRegistration(`Subagent "${parentKey}/${key}"`, registration.mcp);
|
|
88
129
|
return {
|
|
89
130
|
path: registration.path,
|
|
90
131
|
mcp: registration.mcp ?? {},
|
|
132
|
+
subagents: registration.subagents ?? {},
|
|
91
133
|
};
|
|
92
134
|
}
|
|
93
135
|
function validateMcpRegistration(owner, registration) {
|
package/dist/errors.d.ts
CHANGED
|
@@ -9,8 +9,10 @@ export declare class OrchaError extends Error {
|
|
|
9
9
|
cause?: unknown;
|
|
10
10
|
});
|
|
11
11
|
}
|
|
12
|
+
export declare function createProviderHttpError(response: Response, providerLabel: string): Promise<Error>;
|
|
12
13
|
export declare function describeProviderError(error: unknown): {
|
|
13
14
|
message: string;
|
|
15
|
+
retryable: boolean;
|
|
14
16
|
details?: Record<string, unknown>;
|
|
15
17
|
};
|
|
16
18
|
export declare function isRetryableProviderConnectionError(error: unknown): boolean;
|
package/dist/errors.js
CHANGED
|
@@ -10,22 +10,92 @@ export class OrchaError extends Error {
|
|
|
10
10
|
this.details = options.details;
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
|
+
const NON_RETRYABLE_STATUS_CODES = new Set([400, 401, 403, 404, 413, 422]);
|
|
14
|
+
const NON_RETRYABLE_ERROR_NAMES = new Set([
|
|
15
|
+
"ValidationException",
|
|
16
|
+
"AccessDeniedException",
|
|
17
|
+
"ResourceNotFoundException",
|
|
18
|
+
"UnrecognizedClientException",
|
|
19
|
+
"InvalidSignatureException",
|
|
20
|
+
"ExpiredTokenException",
|
|
21
|
+
]);
|
|
22
|
+
export async function createProviderHttpError(response, providerLabel) {
|
|
23
|
+
const status = response.status;
|
|
24
|
+
const requestId = response.headers.get("request-id") ??
|
|
25
|
+
response.headers.get("x-request-id") ??
|
|
26
|
+
response.headers.get("x-amzn-requestid") ??
|
|
27
|
+
undefined;
|
|
28
|
+
let rawText = "";
|
|
29
|
+
try {
|
|
30
|
+
rawText = (await response.text()).trim();
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
rawText = "";
|
|
34
|
+
}
|
|
35
|
+
let parsedMessage;
|
|
36
|
+
let errorCode;
|
|
37
|
+
if (rawText) {
|
|
38
|
+
try {
|
|
39
|
+
const parsed = JSON.parse(rawText);
|
|
40
|
+
const nestedError = typeof parsed.error === "object" && parsed.error !== null
|
|
41
|
+
? parsed.error
|
|
42
|
+
: undefined;
|
|
43
|
+
if (typeof nestedError?.message === "string" && nestedError.message.trim()) {
|
|
44
|
+
parsedMessage = nestedError.message.trim();
|
|
45
|
+
}
|
|
46
|
+
else if (typeof parsed.message === "string" && parsed.message.trim()) {
|
|
47
|
+
parsedMessage = parsed.message.trim();
|
|
48
|
+
}
|
|
49
|
+
else if (typeof parsed.detail === "string" && parsed.detail.trim()) {
|
|
50
|
+
parsedMessage = parsed.detail.trim();
|
|
51
|
+
}
|
|
52
|
+
errorCode =
|
|
53
|
+
stringOrNumberField(nestedError, "code") ??
|
|
54
|
+
stringOrNumberField(nestedError, "type") ??
|
|
55
|
+
stringOrNumberField(parsed, "code");
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
const compact = rawText.replace(/\s+/g, " ").trim();
|
|
59
|
+
if (compact) {
|
|
60
|
+
parsedMessage =
|
|
61
|
+
compact.length > 300 ? `${compact.slice(0, 300)}...` : compact;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const message = parsedMessage ?? `${providerLabel} request failed with HTTP ${status}.`;
|
|
66
|
+
const error = new Error(message);
|
|
67
|
+
error.statusCode = status;
|
|
68
|
+
if (requestId) {
|
|
69
|
+
error.requestId = requestId;
|
|
70
|
+
}
|
|
71
|
+
if (errorCode !== undefined) {
|
|
72
|
+
error.code = errorCode;
|
|
73
|
+
}
|
|
74
|
+
return error;
|
|
75
|
+
}
|
|
13
76
|
export function describeProviderError(error) {
|
|
14
77
|
const rootMessage = errorMessage(error);
|
|
78
|
+
const statusCode = extractStatusCode(error);
|
|
79
|
+
const requestId = extractRequestId(error);
|
|
80
|
+
const errorName = extractErrorName(error);
|
|
81
|
+
const errorCode = stringOrNumberField(error, "code") ??
|
|
82
|
+
stringOrNumberField(error, "type");
|
|
83
|
+
const retryable = isProviderErrorRetryable(statusCode, errorName);
|
|
15
84
|
const cause = deepestUsefulCause(error);
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const syscall = stringField(cause, "syscall");
|
|
22
|
-
const hostname = stringField(cause, "hostname");
|
|
23
|
-
const explanation = causeMessage !== rootMessage
|
|
85
|
+
const causeMessage = cause ? errorMessage(cause) : undefined;
|
|
86
|
+
const causeCode = cause ? stringOrNumberField(cause, "code") : undefined;
|
|
87
|
+
const syscall = cause ? stringField(cause, "syscall") : undefined;
|
|
88
|
+
const hostname = cause ? stringField(cause, "hostname") : undefined;
|
|
89
|
+
const explanation = causeMessage && causeMessage !== rootMessage
|
|
24
90
|
? causeMessage
|
|
25
|
-
: causeCode
|
|
91
|
+
: causeCode !== undefined
|
|
26
92
|
? String(causeCode)
|
|
27
93
|
: undefined;
|
|
28
94
|
const details = {
|
|
95
|
+
...(statusCode !== undefined ? { statusCode } : {}),
|
|
96
|
+
...(requestId ? { requestId } : {}),
|
|
97
|
+
...(errorName ? { errorName } : {}),
|
|
98
|
+
...(errorCode !== undefined ? { errorCode } : {}),
|
|
29
99
|
...(causeCode !== undefined ? { causeCode } : {}),
|
|
30
100
|
...(syscall ? { syscall } : {}),
|
|
31
101
|
...(hostname ? { hostname } : {}),
|
|
@@ -33,6 +103,7 @@ export function describeProviderError(error) {
|
|
|
33
103
|
};
|
|
34
104
|
return {
|
|
35
105
|
message: explanation ? `${rootMessage}: ${explanation}` : rootMessage,
|
|
106
|
+
retryable,
|
|
36
107
|
...(Object.keys(details).length > 0 ? { details } : {}),
|
|
37
108
|
};
|
|
38
109
|
}
|
|
@@ -66,6 +137,54 @@ export function isRetryableProviderConnectionError(error) {
|
|
|
66
137
|
}
|
|
67
138
|
return false;
|
|
68
139
|
}
|
|
140
|
+
function isProviderErrorRetryable(statusCode, errorName) {
|
|
141
|
+
if (errorName && NON_RETRYABLE_ERROR_NAMES.has(errorName)) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
if (statusCode !== undefined && NON_RETRYABLE_STATUS_CODES.has(statusCode)) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
function extractStatusCode(error) {
|
|
150
|
+
if (typeof error !== "object" || error === null)
|
|
151
|
+
return undefined;
|
|
152
|
+
const record = error;
|
|
153
|
+
if (typeof record.statusCode === "number")
|
|
154
|
+
return record.statusCode;
|
|
155
|
+
if (typeof record.status === "number")
|
|
156
|
+
return record.status;
|
|
157
|
+
const metadata = typeof record.$metadata === "object" && record.$metadata !== null
|
|
158
|
+
? record.$metadata
|
|
159
|
+
: undefined;
|
|
160
|
+
if (typeof metadata?.httpStatusCode === "number") {
|
|
161
|
+
return metadata.httpStatusCode;
|
|
162
|
+
}
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
165
|
+
function extractRequestId(error) {
|
|
166
|
+
if (typeof error !== "object" || error === null)
|
|
167
|
+
return undefined;
|
|
168
|
+
const record = error;
|
|
169
|
+
if (typeof record.requestId === "string" && record.requestId.trim()) {
|
|
170
|
+
return record.requestId.trim();
|
|
171
|
+
}
|
|
172
|
+
const metadata = typeof record.$metadata === "object" && record.$metadata !== null
|
|
173
|
+
? record.$metadata
|
|
174
|
+
: undefined;
|
|
175
|
+
if (typeof metadata?.requestId === "string" && metadata.requestId.trim()) {
|
|
176
|
+
return metadata.requestId.trim();
|
|
177
|
+
}
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
function extractErrorName(error) {
|
|
181
|
+
if (!(error instanceof Error))
|
|
182
|
+
return undefined;
|
|
183
|
+
if (error.name && error.name !== "Error" && error.name !== "OrchaError") {
|
|
184
|
+
return error.name;
|
|
185
|
+
}
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
69
188
|
function deepestUsefulCause(error) {
|
|
70
189
|
let current = error;
|
|
71
190
|
let useful;
|