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
|
@@ -439,7 +439,8 @@ class RuntimeAgent {
|
|
|
439
439
|
await this.#store.append(sessionId, pauseEvents);
|
|
440
440
|
return projectSessionSnapshot([...events, ...pauseEvents]);
|
|
441
441
|
}
|
|
442
|
-
async #ownedChild(parentSessionId, childSessionId) {
|
|
442
|
+
async #ownedChild(parentSessionId, childSessionId, visited = /* @__PURE__ */ new Set()) {
|
|
443
|
+
visited.add(this);
|
|
443
444
|
for (const child of Object.values(this.#subagents)) {
|
|
444
445
|
const events = await child.#readSession(childSessionId);
|
|
445
446
|
const lineage = sessionLineage(events);
|
|
@@ -447,6 +448,15 @@ class RuntimeAgent {
|
|
|
447
448
|
return child;
|
|
448
449
|
}
|
|
449
450
|
}
|
|
451
|
+
if (parentSessionId === void 0) {
|
|
452
|
+
for (const child of Object.values(this.#subagents)) {
|
|
453
|
+
if (visited.has(child)) continue;
|
|
454
|
+
try {
|
|
455
|
+
return await child.#ownedChild(void 0, childSessionId, visited);
|
|
456
|
+
} catch {
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
450
460
|
throw new import_errors.OrchaError(
|
|
451
461
|
"session_not_found",
|
|
452
462
|
`Child session "${childSessionId}" was not found for agent "${this.#agentKey}".`
|
|
@@ -986,6 +996,59 @@ class RuntimeAgent {
|
|
|
986
996
|
output: streamedOutput
|
|
987
997
|
};
|
|
988
998
|
}
|
|
999
|
+
if (error instanceof import_structured_output.StructuredOutputError && error.response) {
|
|
1000
|
+
const modelDurationMs2 = Math.round(
|
|
1001
|
+
performance.now() - modelStartedAt
|
|
1002
|
+
);
|
|
1003
|
+
const usage2 = aggregateUsage(
|
|
1004
|
+
previousEvents,
|
|
1005
|
+
writer.run,
|
|
1006
|
+
error.response.usage
|
|
1007
|
+
);
|
|
1008
|
+
const assistantEvent2 = writer.create("message.created", {
|
|
1009
|
+
status: "completed",
|
|
1010
|
+
provider: this.#manifest.provider,
|
|
1011
|
+
model: this.#manifest.model,
|
|
1012
|
+
responseId: error.response.responseId,
|
|
1013
|
+
stopReason: error.response.stopReason,
|
|
1014
|
+
role: "assistant",
|
|
1015
|
+
content: error.response.content,
|
|
1016
|
+
usage: error.response.usage,
|
|
1017
|
+
durationMs: modelDurationMs2
|
|
1018
|
+
});
|
|
1019
|
+
const failedContext = summarizeRunToolFailures(
|
|
1020
|
+
previousEvents,
|
|
1021
|
+
writer.run
|
|
1022
|
+
);
|
|
1023
|
+
const message = failedContext.summary ? `After ${failedContext.summary}: ${error.message}` : error.message;
|
|
1024
|
+
const details = {
|
|
1025
|
+
reason: error.kind,
|
|
1026
|
+
outputType: this.#manifest.outputType,
|
|
1027
|
+
provider: this.#manifest.provider,
|
|
1028
|
+
model: this.#manifest.model,
|
|
1029
|
+
...error.responsePreview ? { responsePreview: error.responsePreview } : {},
|
|
1030
|
+
...error.schemaErrors ? { schemaErrors: error.schemaErrors } : {},
|
|
1031
|
+
...failedContext.failures.length > 0 ? { failedActions: failedContext.failures } : {}
|
|
1032
|
+
};
|
|
1033
|
+
await this.#store.append(sessionId, [
|
|
1034
|
+
assistantEvent2,
|
|
1035
|
+
writer.create("run.failed", {
|
|
1036
|
+
status: "failed",
|
|
1037
|
+
durationMs: elapsedRunDuration(previousEvents, writer.run),
|
|
1038
|
+
usage: usage2,
|
|
1039
|
+
error: {
|
|
1040
|
+
code: "provider_error",
|
|
1041
|
+
message,
|
|
1042
|
+
retryable: true,
|
|
1043
|
+
details
|
|
1044
|
+
}
|
|
1045
|
+
})
|
|
1046
|
+
]);
|
|
1047
|
+
return failure(sessionId, "provider_error", message, {
|
|
1048
|
+
retryable: true,
|
|
1049
|
+
details
|
|
1050
|
+
});
|
|
1051
|
+
}
|
|
989
1052
|
if (error instanceof import_errors.OrchaError) {
|
|
990
1053
|
throw error;
|
|
991
1054
|
}
|
|
@@ -994,7 +1057,7 @@ class RuntimeAgent {
|
|
|
994
1057
|
"provider_error",
|
|
995
1058
|
providerError.message,
|
|
996
1059
|
{
|
|
997
|
-
retryable:
|
|
1060
|
+
retryable: providerError.retryable,
|
|
998
1061
|
cause: error,
|
|
999
1062
|
details: {
|
|
1000
1063
|
provider: this.#manifest.provider,
|
|
@@ -1007,14 +1070,14 @@ class RuntimeAgent {
|
|
|
1007
1070
|
const modelDurationMs = Math.round(performance.now() - modelStartedAt);
|
|
1008
1071
|
const usage = aggregateUsage(previousEvents, writer.run, response.usage);
|
|
1009
1072
|
const assistantEvent = writer.create("message.created", {
|
|
1010
|
-
status: response.stopReason === "max_tokens" ? "incomplete" : "completed",
|
|
1073
|
+
status: response.stopReason === "max_tokens" || response.stopReason === "content_filter" ? "incomplete" : "completed",
|
|
1011
1074
|
provider: this.#manifest.provider,
|
|
1012
1075
|
model: this.#manifest.model,
|
|
1013
1076
|
responseId: response.responseId,
|
|
1014
1077
|
stopReason: response.stopReason,
|
|
1015
1078
|
role: "assistant",
|
|
1016
1079
|
content: response.content,
|
|
1017
|
-
...this.#manifest.outputType === "json" && response.toolCalls.length === 0 && response.stopReason !== "max_tokens" ? { parsedOutput: response.output } : {},
|
|
1080
|
+
...this.#manifest.outputType === "json" && response.toolCalls.length === 0 && response.stopReason !== "max_tokens" && response.stopReason !== "content_filter" ? { parsedOutput: response.output } : {},
|
|
1018
1081
|
usage: response.usage,
|
|
1019
1082
|
durationMs: modelDurationMs
|
|
1020
1083
|
});
|
|
@@ -1042,6 +1105,33 @@ class RuntimeAgent {
|
|
|
1042
1105
|
]);
|
|
1043
1106
|
return failure(sessionId, "provider_error", message, { details });
|
|
1044
1107
|
}
|
|
1108
|
+
if (response.stopReason === "content_filter") {
|
|
1109
|
+
const preview = typeof response.output === "string" && response.output.trim() ? response.output.trim().slice(0, 240) : void 0;
|
|
1110
|
+
const message = preview ? `The model response was blocked by the provider's content filter: ${JSON.stringify(preview)}` : "The model response was blocked by the provider's content filter.";
|
|
1111
|
+
const details = {
|
|
1112
|
+
reason: "content_filter",
|
|
1113
|
+
outputType: this.#manifest.outputType,
|
|
1114
|
+
...preview ? { responsePreview: preview } : {}
|
|
1115
|
+
};
|
|
1116
|
+
await this.#store.append(sessionId, [
|
|
1117
|
+
assistantEvent,
|
|
1118
|
+
writer.create("run.failed", {
|
|
1119
|
+
status: "failed",
|
|
1120
|
+
durationMs: elapsedRunDuration(previousEvents, writer.run),
|
|
1121
|
+
usage,
|
|
1122
|
+
error: {
|
|
1123
|
+
code: "provider_error",
|
|
1124
|
+
message,
|
|
1125
|
+
retryable: false,
|
|
1126
|
+
details
|
|
1127
|
+
}
|
|
1128
|
+
})
|
|
1129
|
+
]);
|
|
1130
|
+
return failure(sessionId, "provider_error", message, {
|
|
1131
|
+
retryable: false,
|
|
1132
|
+
details
|
|
1133
|
+
});
|
|
1134
|
+
}
|
|
1045
1135
|
if (response.toolCalls.length > 0) {
|
|
1046
1136
|
await this.#store.append(sessionId, [assistantEvent]);
|
|
1047
1137
|
const eventsAfterAssistant = [...previousEvents, assistantEvent];
|
|
@@ -1427,7 +1517,11 @@ class RuntimeAgent {
|
|
|
1427
1517
|
events.push(requested);
|
|
1428
1518
|
const startedAt = performance.now();
|
|
1429
1519
|
try {
|
|
1430
|
-
(0, import_structured_output.validateStructuredValue)(
|
|
1520
|
+
(0, import_structured_output.validateStructuredValue)(
|
|
1521
|
+
call.arguments,
|
|
1522
|
+
action.parameters,
|
|
1523
|
+
`action "${action.name}" parameters`
|
|
1524
|
+
);
|
|
1431
1525
|
const rawOutput = await (0, import_execute.executeLocalAction)({
|
|
1432
1526
|
action,
|
|
1433
1527
|
parameters: call.arguments,
|
|
@@ -1442,7 +1536,11 @@ class RuntimeAgent {
|
|
|
1442
1536
|
this.#projectRoot
|
|
1443
1537
|
);
|
|
1444
1538
|
if (action.outputSchema) {
|
|
1445
|
-
(0, import_structured_output.validateStructuredValue)(
|
|
1539
|
+
(0, import_structured_output.validateStructuredValue)(
|
|
1540
|
+
output,
|
|
1541
|
+
action.outputSchema,
|
|
1542
|
+
`action "${action.name}" outputSchema`
|
|
1543
|
+
);
|
|
1446
1544
|
}
|
|
1447
1545
|
const completed = writer.create("action.completed", {
|
|
1448
1546
|
callId: call.callId,
|
|
@@ -1520,7 +1618,11 @@ class RuntimeAgent {
|
|
|
1520
1618
|
events.push(requested);
|
|
1521
1619
|
const startedAt = performance.now();
|
|
1522
1620
|
try {
|
|
1523
|
-
(0, import_structured_output.validateStructuredValue)(
|
|
1621
|
+
(0, import_structured_output.validateStructuredValue)(
|
|
1622
|
+
call.arguments,
|
|
1623
|
+
tool.parameters,
|
|
1624
|
+
`MCP tool "${call.name}" parameters`
|
|
1625
|
+
);
|
|
1524
1626
|
const result = await this.#mcp.callTool(
|
|
1525
1627
|
call.name,
|
|
1526
1628
|
call.arguments,
|
|
@@ -1528,6 +1630,7 @@ class RuntimeAgent {
|
|
|
1528
1630
|
);
|
|
1529
1631
|
const serializedOutput = ensureJsonSerializable(result.output);
|
|
1530
1632
|
if (result.isError) {
|
|
1633
|
+
const errorDetail = extractMcpErrorDetail(serializedOutput);
|
|
1531
1634
|
const failed = writer.create("action.failed", {
|
|
1532
1635
|
callId: call.callId,
|
|
1533
1636
|
name: call.name,
|
|
@@ -1538,7 +1641,7 @@ class RuntimeAgent {
|
|
|
1538
1641
|
durationMs: Math.round(performance.now() - startedAt),
|
|
1539
1642
|
error: {
|
|
1540
1643
|
code: "mcp_tool_error",
|
|
1541
|
-
message: `MCP tool "${call.name}" returned an error.`
|
|
1644
|
+
message: errorDetail ? `MCP tool "${call.name}" returned an error: ${errorDetail}` : `MCP tool "${call.name}" returned an error.`
|
|
1542
1645
|
}
|
|
1543
1646
|
});
|
|
1544
1647
|
await this.#store.append(sessionId, [failed]);
|
|
@@ -1948,7 +2051,10 @@ class RuntimeAgent {
|
|
|
1948
2051
|
error instanceof Error ? error.message : String(error),
|
|
1949
2052
|
{ retryable: true, cause: error }
|
|
1950
2053
|
);
|
|
1951
|
-
return failure(sessionId, normalized.code, normalized.message
|
|
2054
|
+
return failure(sessionId, normalized.code, normalized.message, {
|
|
2055
|
+
retryable: normalized.retryable,
|
|
2056
|
+
details: normalized.details
|
|
2057
|
+
});
|
|
1952
2058
|
} finally {
|
|
1953
2059
|
this.#sessionControls.delete(sessionId);
|
|
1954
2060
|
this.#releaseSessionLock(sessionId);
|
|
@@ -3309,6 +3415,55 @@ async function withProviderConnectionRetry(operation, signal) {
|
|
|
3309
3415
|
return operation();
|
|
3310
3416
|
}
|
|
3311
3417
|
}
|
|
3418
|
+
function summarizeRunToolFailures(events, run) {
|
|
3419
|
+
const failures = [];
|
|
3420
|
+
for (const event of events) {
|
|
3421
|
+
if (event.run !== run) continue;
|
|
3422
|
+
if (event.type === "action.failed") {
|
|
3423
|
+
const name = typeof event.data.name === "string" ? event.data.name : "action";
|
|
3424
|
+
const errorRecord = isRecord(event.data.error) ? event.data.error : void 0;
|
|
3425
|
+
const message = typeof errorRecord?.message === "string" && errorRecord.message.trim() ? errorRecord.message.trim() : "action execution failed";
|
|
3426
|
+
failures.push({ kind: "action", name, message });
|
|
3427
|
+
} else if (event.type === "subagent.failed") {
|
|
3428
|
+
const name = typeof event.data.agent === "string" ? event.data.agent : "subagent";
|
|
3429
|
+
const errorRecord = isRecord(event.data.error) ? event.data.error : void 0;
|
|
3430
|
+
const message = typeof errorRecord?.message === "string" && errorRecord.message.trim() ? errorRecord.message.trim() : "subagent execution failed";
|
|
3431
|
+
failures.push({ kind: "subagent", name, message });
|
|
3432
|
+
}
|
|
3433
|
+
}
|
|
3434
|
+
if (failures.length === 0) {
|
|
3435
|
+
return { failures };
|
|
3436
|
+
}
|
|
3437
|
+
const last = failures.at(-1);
|
|
3438
|
+
const label = `${last.kind} "${last.name}" failed (${last.message})`;
|
|
3439
|
+
return {
|
|
3440
|
+
summary: failures.length === 1 ? label : `${label} (+${failures.length - 1} earlier tool failure${failures.length > 2 ? "s" : ""})`,
|
|
3441
|
+
failures
|
|
3442
|
+
};
|
|
3443
|
+
}
|
|
3444
|
+
function extractMcpErrorDetail(output) {
|
|
3445
|
+
if (typeof output === "string" && output.trim()) {
|
|
3446
|
+
return output.trim();
|
|
3447
|
+
}
|
|
3448
|
+
if (!isRecord(output)) {
|
|
3449
|
+
return void 0;
|
|
3450
|
+
}
|
|
3451
|
+
if (typeof output.error === "string" && output.error.trim()) {
|
|
3452
|
+
return output.error.trim();
|
|
3453
|
+
}
|
|
3454
|
+
if (typeof output.message === "string" && output.message.trim()) {
|
|
3455
|
+
return output.message.trim();
|
|
3456
|
+
}
|
|
3457
|
+
if (Array.isArray(output.content)) {
|
|
3458
|
+
const texts = output.content.map(
|
|
3459
|
+
(item) => isRecord(item) && typeof item.text === "string" ? item.text.trim() : ""
|
|
3460
|
+
).filter(Boolean);
|
|
3461
|
+
if (texts.length > 0) {
|
|
3462
|
+
return texts.join(" ");
|
|
3463
|
+
}
|
|
3464
|
+
}
|
|
3465
|
+
return void 0;
|
|
3466
|
+
}
|
|
3312
3467
|
function failure(sessionId, code, message, options = {}) {
|
|
3313
3468
|
const retryable = options.retryable ?? (code === "session_busy" || code === "provider_error" || code === "storage_error");
|
|
3314
3469
|
return {
|
|
@@ -117,38 +117,41 @@ class OrchaRuntimeCore {
|
|
|
117
117
|
return runtimeStore;
|
|
118
118
|
};
|
|
119
119
|
const agents = /* @__PURE__ */ new Map();
|
|
120
|
-
|
|
120
|
+
const createRuntimeAgent = (manifest, delegatedOnly, ancestry) => {
|
|
121
121
|
const subagents = Object.fromEntries(
|
|
122
122
|
Object.entries(manifest.subagents ?? {}).map(
|
|
123
|
-
([subagentName, subagentManifest]) =>
|
|
124
|
-
subagentName
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
]
|
|
123
|
+
([subagentName, subagentManifest]) => {
|
|
124
|
+
if (ancestry.has(subagentName)) {
|
|
125
|
+
throw new Error(
|
|
126
|
+
`Circular subagent registration detected: ${[...ancestry, subagentName].join(" -> ")}.`
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
const nextAncestry = new Set(ancestry);
|
|
130
|
+
nextAncestry.add(subagentName);
|
|
131
|
+
return [
|
|
132
|
+
subagentName,
|
|
133
|
+
createRuntimeAgent(subagentManifest, true, nextAncestry)
|
|
134
|
+
];
|
|
135
|
+
}
|
|
137
136
|
)
|
|
138
137
|
);
|
|
138
|
+
return new import_agent.RuntimeAgent({
|
|
139
|
+
manifest,
|
|
140
|
+
configuration: runtimeConfiguration,
|
|
141
|
+
store: createRuntimeStore(manifest),
|
|
142
|
+
activeSessions: this.#activeSessions,
|
|
143
|
+
sessionControls: this.#sessionControls,
|
|
144
|
+
generateProviderResponse,
|
|
145
|
+
subagents,
|
|
146
|
+
delegatedOnly,
|
|
147
|
+
projectRoot,
|
|
148
|
+
randomUUID: this.#platform.randomUUID
|
|
149
|
+
});
|
|
150
|
+
};
|
|
151
|
+
for (const [name, manifest] of Object.entries(bundle.agents)) {
|
|
139
152
|
agents.set(
|
|
140
153
|
name,
|
|
141
|
-
new
|
|
142
|
-
manifest,
|
|
143
|
-
configuration: runtimeConfiguration,
|
|
144
|
-
store: createRuntimeStore(manifest),
|
|
145
|
-
activeSessions: this.#activeSessions,
|
|
146
|
-
sessionControls: this.#sessionControls,
|
|
147
|
-
generateProviderResponse,
|
|
148
|
-
subagents,
|
|
149
|
-
projectRoot,
|
|
150
|
-
randomUUID: this.#platform.randomUUID
|
|
151
|
-
})
|
|
154
|
+
createRuntimeAgent(manifest, false, /* @__PURE__ */ new Set([name]))
|
|
152
155
|
);
|
|
153
156
|
}
|
|
154
157
|
this.#bundle = bundle;
|
|
@@ -19,30 +19,104 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
19
19
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
20
|
var structured_output_exports = {};
|
|
21
21
|
__export(structured_output_exports, {
|
|
22
|
+
StructuredOutputError: () => StructuredOutputError,
|
|
22
23
|
parseStructuredOutput: () => parseStructuredOutput,
|
|
24
|
+
resolveProviderStructuredOutput: () => resolveProviderStructuredOutput,
|
|
23
25
|
validateStructuredValue: () => validateStructuredValue
|
|
24
26
|
});
|
|
25
27
|
module.exports = __toCommonJS(structured_output_exports);
|
|
28
|
+
class StructuredOutputError extends Error {
|
|
29
|
+
kind;
|
|
30
|
+
rawText;
|
|
31
|
+
responsePreview;
|
|
32
|
+
schemaErrors;
|
|
33
|
+
response;
|
|
34
|
+
constructor(message, options) {
|
|
35
|
+
super(message, { cause: options.cause });
|
|
36
|
+
this.name = "StructuredOutputError";
|
|
37
|
+
this.kind = options.kind;
|
|
38
|
+
this.rawText = options.rawText;
|
|
39
|
+
this.responsePreview = options.responsePreview;
|
|
40
|
+
this.schemaErrors = options.schemaErrors;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function resolveProviderStructuredOutput(request, baseResponse, textOutput) {
|
|
44
|
+
if (request.outputType !== "json" || baseResponse.toolCalls.length > 0 || baseResponse.stopReason === "max_tokens" || baseResponse.stopReason === "content_filter") {
|
|
45
|
+
return baseResponse;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
return {
|
|
49
|
+
...baseResponse,
|
|
50
|
+
output: parseStructuredOutput(
|
|
51
|
+
textOutput,
|
|
52
|
+
request.outputSchema ?? {}
|
|
53
|
+
)
|
|
54
|
+
};
|
|
55
|
+
} catch (error) {
|
|
56
|
+
if (error instanceof StructuredOutputError) {
|
|
57
|
+
error.response = baseResponse;
|
|
58
|
+
}
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
26
62
|
function parseStructuredOutput(text, schema) {
|
|
63
|
+
const preview = previewText(text);
|
|
27
64
|
const candidate = extractJsonCandidate(text);
|
|
28
65
|
if (!candidate) {
|
|
29
|
-
|
|
66
|
+
const suffix = preview ? ` Model returned plain text: ${JSON.stringify(preview)}` : " Model returned an empty response.";
|
|
67
|
+
throw new StructuredOutputError(
|
|
68
|
+
`Model response did not contain a JSON object or array.${suffix}`,
|
|
69
|
+
{
|
|
70
|
+
kind: "missing_json",
|
|
71
|
+
rawText: text,
|
|
72
|
+
responsePreview: preview
|
|
73
|
+
}
|
|
74
|
+
);
|
|
30
75
|
}
|
|
31
76
|
let value;
|
|
32
77
|
try {
|
|
33
78
|
value = JSON.parse(candidate);
|
|
34
79
|
} catch (error) {
|
|
35
|
-
|
|
80
|
+
const detail = error instanceof Error ? error.message : "Invalid JSON";
|
|
81
|
+
const suffix = preview ? ` Model returned: ${JSON.stringify(preview)}` : "";
|
|
82
|
+
throw new StructuredOutputError(
|
|
83
|
+
`Model response contained invalid JSON (${detail}).${suffix}`,
|
|
84
|
+
{
|
|
85
|
+
kind: "invalid_json",
|
|
86
|
+
rawText: text,
|
|
87
|
+
responsePreview: preview,
|
|
88
|
+
cause: error
|
|
89
|
+
}
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
const errors = [];
|
|
93
|
+
validateSchemaValue(value, schema, "$", errors);
|
|
94
|
+
if (errors.length > 0) {
|
|
95
|
+
throw new StructuredOutputError(
|
|
96
|
+
`Value failed outputSchema: ${errors.join("; ")}`,
|
|
97
|
+
{
|
|
98
|
+
kind: "schema_mismatch",
|
|
99
|
+
rawText: text,
|
|
100
|
+
responsePreview: preview,
|
|
101
|
+
schemaErrors: errors
|
|
102
|
+
}
|
|
103
|
+
);
|
|
36
104
|
}
|
|
37
|
-
validateStructuredValue(value, schema);
|
|
38
105
|
return value;
|
|
39
106
|
}
|
|
40
|
-
function validateStructuredValue(value, schema) {
|
|
107
|
+
function validateStructuredValue(value, schema, label = "outputSchema") {
|
|
41
108
|
const errors = [];
|
|
42
109
|
validateSchemaValue(value, schema, "$", errors);
|
|
43
110
|
if (errors.length > 0) {
|
|
44
|
-
throw new Error(`Value failed
|
|
111
|
+
throw new Error(`Value failed ${label}: ${errors.join("; ")}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function previewText(text, maxLength = 240) {
|
|
115
|
+
const compact = text.replace(/\s+/g, " ").trim();
|
|
116
|
+
if (!compact) {
|
|
117
|
+
return void 0;
|
|
45
118
|
}
|
|
119
|
+
return compact.length > maxLength ? `${compact.slice(0, maxLength)}...` : compact;
|
|
46
120
|
}
|
|
47
121
|
function extractJsonCandidate(text) {
|
|
48
122
|
const trimmed = text.trim();
|
|
@@ -140,6 +214,8 @@ function isObject(value) {
|
|
|
140
214
|
}
|
|
141
215
|
// Annotate the CommonJS export names for ESM import in node:
|
|
142
216
|
0 && (module.exports = {
|
|
217
|
+
StructuredOutputError,
|
|
143
218
|
parseStructuredOutput,
|
|
219
|
+
resolveProviderStructuredOutput,
|
|
144
220
|
validateStructuredValue
|
|
145
221
|
});
|
package/dist-cjs/testing.js
CHANGED
|
@@ -88,24 +88,38 @@ async function runTests(orcha, options = {}) {
|
|
|
88
88
|
const store = createStore(manifest);
|
|
89
89
|
const activeSessions = /* @__PURE__ */ new Set();
|
|
90
90
|
const sessionControls = /* @__PURE__ */ new Map();
|
|
91
|
-
const
|
|
92
|
-
Object.entries(
|
|
93
|
-
([name, subagentManifest]) =>
|
|
94
|
-
name
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
91
|
+
const createTestSubagents = (parentManifest, ancestry) => Object.fromEntries(
|
|
92
|
+
Object.entries(parentManifest.subagents ?? {}).map(
|
|
93
|
+
([name, subagentManifest]) => {
|
|
94
|
+
if (ancestry.has(name)) {
|
|
95
|
+
throw new Error(
|
|
96
|
+
`Circular subagent registration detected: ${[...ancestry, name].join(" -> ")}.`
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
const nextAncestry = new Set(ancestry);
|
|
100
|
+
nextAncestry.add(name);
|
|
101
|
+
return [
|
|
102
|
+
name,
|
|
103
|
+
new import_agent.RuntimeAgent({
|
|
104
|
+
manifest: subagentManifest,
|
|
105
|
+
configuration: context.configuration,
|
|
106
|
+
store: createStore(subagentManifest),
|
|
107
|
+
activeSessions,
|
|
108
|
+
sessionControls,
|
|
109
|
+
generateProviderResponse: context.generateProviderResponse,
|
|
110
|
+
subagents: createTestSubagents(subagentManifest, nextAncestry),
|
|
111
|
+
delegatedOnly: true,
|
|
112
|
+
sessionIdPrefix: "ses_test_",
|
|
113
|
+
projectRoot: context.projectRoot
|
|
114
|
+
})
|
|
115
|
+
];
|
|
116
|
+
}
|
|
107
117
|
)
|
|
108
118
|
);
|
|
119
|
+
const subagents = createTestSubagents(
|
|
120
|
+
manifest,
|
|
121
|
+
/* @__PURE__ */ new Set([manifest.key ?? manifest.name])
|
|
122
|
+
);
|
|
109
123
|
const agent = new import_agent.RuntimeAgent({
|
|
110
124
|
manifest: withTestActions(
|
|
111
125
|
manifest,
|