orchajs 0.27.1 → 0.27.2
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/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/google-gemini.js +4 -9
- package/dist/providers/openai.js +6 -13
- package/dist/runtime/agent.js +163 -9
- package/dist/runtime/structured-output.d.ts +18 -1
- package/dist/runtime/structured-output.js +75 -5
- package/dist-cjs/amarsia/analytics.js +129 -49
- package/dist-cjs/amarsia/inference.js +12 -9
- 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/google-gemini.js +12 -9
- package/dist-cjs/providers/openai.js +13 -13
- package/dist-cjs/runtime/agent.js +153 -8
- package/dist-cjs/runtime/structured-output.js +81 -5
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { OrchaError } from "../errors.js";
|
|
2
2
|
import { readLocalFileContent } from "../file-content.js";
|
|
3
|
-
import {
|
|
3
|
+
import { resolveProviderStructuredOutput } from "../runtime/structured-output.js";
|
|
4
4
|
export function toGoogleContentClient(client) {
|
|
5
5
|
if (typeof client !== "object" ||
|
|
6
6
|
client === null ||
|
|
@@ -131,19 +131,14 @@ function toProviderResponse(request, response) {
|
|
|
131
131
|
.map((block) => block.text)
|
|
132
132
|
.join("");
|
|
133
133
|
const stopReason = normalizeStopReason(response.finishReason, toolCalls.length > 0, response.promptBlockReason !== undefined);
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
stopReason !== "max_tokens"
|
|
137
|
-
? parseStructuredOutput(textOutput, request.outputSchema ?? {})
|
|
138
|
-
: textOutput;
|
|
139
|
-
return {
|
|
140
|
-
output,
|
|
134
|
+
return resolveProviderStructuredOutput(request, {
|
|
135
|
+
output: textOutput,
|
|
141
136
|
responseId: response.responseId,
|
|
142
137
|
stopReason,
|
|
143
138
|
content,
|
|
144
139
|
toolCalls,
|
|
145
140
|
usage: normalizeUsage(response.usage),
|
|
146
|
-
};
|
|
141
|
+
}, textOutput);
|
|
147
142
|
}
|
|
148
143
|
function appendGoogleTextPart(content, part) {
|
|
149
144
|
const text = part.text ?? "";
|
package/dist/providers/openai.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { OrchaError } from "../errors.js";
|
|
1
|
+
import { createProviderHttpError, OrchaError } from "../errors.js";
|
|
2
2
|
import { readLocalFileContent } from "../file-content.js";
|
|
3
|
-
import {
|
|
3
|
+
import { resolveProviderStructuredOutput } from "../runtime/structured-output.js";
|
|
4
4
|
import { readServerSentEvents } from "./sse.js";
|
|
5
5
|
export const openAIProvider = {
|
|
6
6
|
name: "openai",
|
|
@@ -58,9 +58,7 @@ async function generateOpenAI(configuration, request) {
|
|
|
58
58
|
signal: request.signal,
|
|
59
59
|
});
|
|
60
60
|
if (!response.ok) {
|
|
61
|
-
|
|
62
|
-
throw new Error(errorBody.error?.message ??
|
|
63
|
-
`OpenAI request failed with HTTP ${response.status}.`);
|
|
61
|
+
throw await createProviderHttpError(response, "OpenAI");
|
|
64
62
|
}
|
|
65
63
|
const responseBody = response.headers
|
|
66
64
|
.get("content-type")
|
|
@@ -264,22 +262,17 @@ function toProviderResponse(request, response) {
|
|
|
264
262
|
.map((block) => block.text)
|
|
265
263
|
.join("");
|
|
266
264
|
const stopReason = normalizeStopReason(response, toolCalls, refused);
|
|
267
|
-
const output = request.outputType === "json" &&
|
|
268
|
-
toolCalls.length === 0 &&
|
|
269
|
-
stopReason !== "max_tokens"
|
|
270
|
-
? parseStructuredOutput(textOutput, request.outputSchema ?? {})
|
|
271
|
-
: textOutput;
|
|
272
265
|
if (textOutput) {
|
|
273
266
|
request.publishOutput?.(textOutput);
|
|
274
267
|
}
|
|
275
|
-
return {
|
|
276
|
-
output,
|
|
268
|
+
return resolveProviderStructuredOutput(request, {
|
|
269
|
+
output: textOutput,
|
|
277
270
|
responseId: response.id,
|
|
278
271
|
stopReason,
|
|
279
272
|
content,
|
|
280
273
|
toolCalls,
|
|
281
274
|
usage: normalizeUsage(response.usage),
|
|
282
|
-
};
|
|
275
|
+
}, textOutput);
|
|
283
276
|
}
|
|
284
277
|
function parseFunctionArguments(argumentsJson, name) {
|
|
285
278
|
let parsed;
|
package/dist/runtime/agent.js
CHANGED
|
@@ -2,7 +2,7 @@ import { describeProviderError, isRetryableProviderConnectionError, OrchaError,
|
|
|
2
2
|
import { sanitizeErrorMessage } from "../amarsia/analytics.js";
|
|
3
3
|
import { normalizeLocalFile, normalizeUrlContent, } from "../file-content.js";
|
|
4
4
|
import { messagesFromEvents, normalizePersistedToolResults, SessionEventWriter, } from "./session-events.js";
|
|
5
|
-
import { validateStructuredValue } from "./structured-output.js";
|
|
5
|
+
import { StructuredOutputError, validateStructuredValue, } from "./structured-output.js";
|
|
6
6
|
import { executeLocalAction } from "./actions/execute.js";
|
|
7
7
|
import { createExecution, } from "./execution.js";
|
|
8
8
|
import { McpClientPool, } from "./mcp/client.js";
|
|
@@ -738,12 +738,64 @@ export class RuntimeAgent {
|
|
|
738
738
|
output: streamedOutput,
|
|
739
739
|
};
|
|
740
740
|
}
|
|
741
|
+
if (error instanceof StructuredOutputError && error.response) {
|
|
742
|
+
const modelDurationMs = Math.round(performance.now() - modelStartedAt);
|
|
743
|
+
const usage = aggregateUsage(previousEvents, writer.run, error.response.usage);
|
|
744
|
+
const assistantEvent = writer.create("message.created", {
|
|
745
|
+
status: "completed",
|
|
746
|
+
provider: this.#manifest.provider,
|
|
747
|
+
model: this.#manifest.model,
|
|
748
|
+
responseId: error.response.responseId,
|
|
749
|
+
stopReason: error.response.stopReason,
|
|
750
|
+
role: "assistant",
|
|
751
|
+
content: error.response.content,
|
|
752
|
+
usage: error.response.usage,
|
|
753
|
+
durationMs: modelDurationMs,
|
|
754
|
+
});
|
|
755
|
+
const failedContext = summarizeRunToolFailures(previousEvents, writer.run);
|
|
756
|
+
const message = failedContext.summary
|
|
757
|
+
? `After ${failedContext.summary}: ${error.message}`
|
|
758
|
+
: error.message;
|
|
759
|
+
const details = {
|
|
760
|
+
reason: error.kind,
|
|
761
|
+
outputType: this.#manifest.outputType,
|
|
762
|
+
provider: this.#manifest.provider,
|
|
763
|
+
model: this.#manifest.model,
|
|
764
|
+
...(error.responsePreview
|
|
765
|
+
? { responsePreview: error.responsePreview }
|
|
766
|
+
: {}),
|
|
767
|
+
...(error.schemaErrors
|
|
768
|
+
? { schemaErrors: error.schemaErrors }
|
|
769
|
+
: {}),
|
|
770
|
+
...(failedContext.failures.length > 0
|
|
771
|
+
? { failedActions: failedContext.failures }
|
|
772
|
+
: {}),
|
|
773
|
+
};
|
|
774
|
+
await this.#store.append(sessionId, [
|
|
775
|
+
assistantEvent,
|
|
776
|
+
writer.create("run.failed", {
|
|
777
|
+
status: "failed",
|
|
778
|
+
durationMs: elapsedRunDuration(previousEvents, writer.run),
|
|
779
|
+
usage,
|
|
780
|
+
error: {
|
|
781
|
+
code: "provider_error",
|
|
782
|
+
message,
|
|
783
|
+
retryable: true,
|
|
784
|
+
details,
|
|
785
|
+
},
|
|
786
|
+
}),
|
|
787
|
+
]);
|
|
788
|
+
return failure(sessionId, "provider_error", message, {
|
|
789
|
+
retryable: true,
|
|
790
|
+
details,
|
|
791
|
+
});
|
|
792
|
+
}
|
|
741
793
|
if (error instanceof OrchaError) {
|
|
742
794
|
throw error;
|
|
743
795
|
}
|
|
744
796
|
const providerError = describeProviderError(error);
|
|
745
797
|
throw new OrchaError("provider_error", providerError.message, {
|
|
746
|
-
retryable:
|
|
798
|
+
retryable: providerError.retryable,
|
|
747
799
|
cause: error,
|
|
748
800
|
details: {
|
|
749
801
|
provider: this.#manifest.provider,
|
|
@@ -755,7 +807,8 @@ export class RuntimeAgent {
|
|
|
755
807
|
const modelDurationMs = Math.round(performance.now() - modelStartedAt);
|
|
756
808
|
const usage = aggregateUsage(previousEvents, writer.run, response.usage);
|
|
757
809
|
const assistantEvent = writer.create("message.created", {
|
|
758
|
-
status: response.stopReason === "max_tokens"
|
|
810
|
+
status: response.stopReason === "max_tokens" ||
|
|
811
|
+
response.stopReason === "content_filter"
|
|
759
812
|
? "incomplete"
|
|
760
813
|
: "completed",
|
|
761
814
|
provider: this.#manifest.provider,
|
|
@@ -766,7 +819,8 @@ export class RuntimeAgent {
|
|
|
766
819
|
content: response.content,
|
|
767
820
|
...(this.#manifest.outputType === "json" &&
|
|
768
821
|
response.toolCalls.length === 0 &&
|
|
769
|
-
response.stopReason !== "max_tokens"
|
|
822
|
+
response.stopReason !== "max_tokens" &&
|
|
823
|
+
response.stopReason !== "content_filter"
|
|
770
824
|
? { parsedOutput: response.output }
|
|
771
825
|
: {}),
|
|
772
826
|
usage: response.usage,
|
|
@@ -797,6 +851,37 @@ export class RuntimeAgent {
|
|
|
797
851
|
]);
|
|
798
852
|
return failure(sessionId, "provider_error", message, { details });
|
|
799
853
|
}
|
|
854
|
+
if (response.stopReason === "content_filter") {
|
|
855
|
+
const preview = typeof response.output === "string" && response.output.trim()
|
|
856
|
+
? response.output.trim().slice(0, 240)
|
|
857
|
+
: undefined;
|
|
858
|
+
const message = preview
|
|
859
|
+
? `The model response was blocked by the provider's content filter: ${JSON.stringify(preview)}`
|
|
860
|
+
: "The model response was blocked by the provider's content filter.";
|
|
861
|
+
const details = {
|
|
862
|
+
reason: "content_filter",
|
|
863
|
+
outputType: this.#manifest.outputType,
|
|
864
|
+
...(preview ? { responsePreview: preview } : {}),
|
|
865
|
+
};
|
|
866
|
+
await this.#store.append(sessionId, [
|
|
867
|
+
assistantEvent,
|
|
868
|
+
writer.create("run.failed", {
|
|
869
|
+
status: "failed",
|
|
870
|
+
durationMs: elapsedRunDuration(previousEvents, writer.run),
|
|
871
|
+
usage,
|
|
872
|
+
error: {
|
|
873
|
+
code: "provider_error",
|
|
874
|
+
message,
|
|
875
|
+
retryable: false,
|
|
876
|
+
details,
|
|
877
|
+
},
|
|
878
|
+
}),
|
|
879
|
+
]);
|
|
880
|
+
return failure(sessionId, "provider_error", message, {
|
|
881
|
+
retryable: false,
|
|
882
|
+
details,
|
|
883
|
+
});
|
|
884
|
+
}
|
|
800
885
|
if (response.toolCalls.length > 0) {
|
|
801
886
|
await this.#store.append(sessionId, [assistantEvent]);
|
|
802
887
|
const eventsAfterAssistant = [...previousEvents, assistantEvent];
|
|
@@ -1093,7 +1178,7 @@ export class RuntimeAgent {
|
|
|
1093
1178
|
events.push(requested);
|
|
1094
1179
|
const startedAt = performance.now();
|
|
1095
1180
|
try {
|
|
1096
|
-
validateStructuredValue(call.arguments, action.parameters);
|
|
1181
|
+
validateStructuredValue(call.arguments, action.parameters, `action "${action.name}" parameters`);
|
|
1097
1182
|
const rawOutput = await executeLocalAction({
|
|
1098
1183
|
action,
|
|
1099
1184
|
parameters: call.arguments,
|
|
@@ -1105,7 +1190,7 @@ export class RuntimeAgent {
|
|
|
1105
1190
|
const serializedOutput = ensureJsonSerializable(rawOutput);
|
|
1106
1191
|
const { output, contents } = extractActionOutputAndContents(serializedOutput, this.#projectRoot);
|
|
1107
1192
|
if (action.outputSchema) {
|
|
1108
|
-
validateStructuredValue(output, action.outputSchema);
|
|
1193
|
+
validateStructuredValue(output, action.outputSchema, `action "${action.name}" outputSchema`);
|
|
1109
1194
|
}
|
|
1110
1195
|
const completed = writer.create("action.completed", {
|
|
1111
1196
|
callId: call.callId,
|
|
@@ -1187,10 +1272,11 @@ export class RuntimeAgent {
|
|
|
1187
1272
|
events.push(requested);
|
|
1188
1273
|
const startedAt = performance.now();
|
|
1189
1274
|
try {
|
|
1190
|
-
validateStructuredValue(call.arguments, tool.parameters);
|
|
1275
|
+
validateStructuredValue(call.arguments, tool.parameters, `MCP tool "${call.name}" parameters`);
|
|
1191
1276
|
const result = await this.#mcp.callTool(call.name, call.arguments, signal);
|
|
1192
1277
|
const serializedOutput = ensureJsonSerializable(result.output);
|
|
1193
1278
|
if (result.isError) {
|
|
1279
|
+
const errorDetail = extractMcpErrorDetail(serializedOutput);
|
|
1194
1280
|
const failed = writer.create("action.failed", {
|
|
1195
1281
|
callId: call.callId,
|
|
1196
1282
|
name: call.name,
|
|
@@ -1201,7 +1287,9 @@ export class RuntimeAgent {
|
|
|
1201
1287
|
durationMs: Math.round(performance.now() - startedAt),
|
|
1202
1288
|
error: {
|
|
1203
1289
|
code: "mcp_tool_error",
|
|
1204
|
-
message:
|
|
1290
|
+
message: errorDetail
|
|
1291
|
+
? `MCP tool "${call.name}" returned an error: ${errorDetail}`
|
|
1292
|
+
: `MCP tool "${call.name}" returned an error.`,
|
|
1205
1293
|
},
|
|
1206
1294
|
});
|
|
1207
1295
|
await this.#store.append(sessionId, [failed]);
|
|
@@ -1582,7 +1670,10 @@ export class RuntimeAgent {
|
|
|
1582
1670
|
const normalized = error instanceof OrchaError
|
|
1583
1671
|
? error
|
|
1584
1672
|
: new OrchaError("storage_error", error instanceof Error ? error.message : String(error), { retryable: true, cause: error });
|
|
1585
|
-
return failure(sessionId, normalized.code, normalized.message
|
|
1673
|
+
return failure(sessionId, normalized.code, normalized.message, {
|
|
1674
|
+
retryable: normalized.retryable,
|
|
1675
|
+
details: normalized.details,
|
|
1676
|
+
});
|
|
1586
1677
|
}
|
|
1587
1678
|
finally {
|
|
1588
1679
|
this.#sessionControls.delete(sessionId);
|
|
@@ -3046,6 +3137,69 @@ async function withProviderConnectionRetry(operation, signal) {
|
|
|
3046
3137
|
return operation();
|
|
3047
3138
|
}
|
|
3048
3139
|
}
|
|
3140
|
+
function summarizeRunToolFailures(events, run) {
|
|
3141
|
+
const failures = [];
|
|
3142
|
+
for (const event of events) {
|
|
3143
|
+
if (event.run !== run)
|
|
3144
|
+
continue;
|
|
3145
|
+
if (event.type === "action.failed") {
|
|
3146
|
+
const name = typeof event.data.name === "string" ? event.data.name : "action";
|
|
3147
|
+
const errorRecord = isRecord(event.data.error)
|
|
3148
|
+
? event.data.error
|
|
3149
|
+
: undefined;
|
|
3150
|
+
const message = typeof errorRecord?.message === "string" && errorRecord.message.trim()
|
|
3151
|
+
? errorRecord.message.trim()
|
|
3152
|
+
: "action execution failed";
|
|
3153
|
+
failures.push({ kind: "action", name, message });
|
|
3154
|
+
}
|
|
3155
|
+
else if (event.type === "subagent.failed") {
|
|
3156
|
+
const name = typeof event.data.agent === "string" ? event.data.agent : "subagent";
|
|
3157
|
+
const errorRecord = isRecord(event.data.error)
|
|
3158
|
+
? event.data.error
|
|
3159
|
+
: undefined;
|
|
3160
|
+
const message = typeof errorRecord?.message === "string" && errorRecord.message.trim()
|
|
3161
|
+
? errorRecord.message.trim()
|
|
3162
|
+
: "subagent execution failed";
|
|
3163
|
+
failures.push({ kind: "subagent", name, message });
|
|
3164
|
+
}
|
|
3165
|
+
}
|
|
3166
|
+
if (failures.length === 0) {
|
|
3167
|
+
return { failures };
|
|
3168
|
+
}
|
|
3169
|
+
const last = failures.at(-1);
|
|
3170
|
+
const label = `${last.kind} "${last.name}" failed (${last.message})`;
|
|
3171
|
+
return {
|
|
3172
|
+
summary: failures.length === 1
|
|
3173
|
+
? label
|
|
3174
|
+
: `${label} (+${failures.length - 1} earlier tool failure${failures.length > 2 ? "s" : ""})`,
|
|
3175
|
+
failures,
|
|
3176
|
+
};
|
|
3177
|
+
}
|
|
3178
|
+
function extractMcpErrorDetail(output) {
|
|
3179
|
+
if (typeof output === "string" && output.trim()) {
|
|
3180
|
+
return output.trim();
|
|
3181
|
+
}
|
|
3182
|
+
if (!isRecord(output)) {
|
|
3183
|
+
return undefined;
|
|
3184
|
+
}
|
|
3185
|
+
if (typeof output.error === "string" && output.error.trim()) {
|
|
3186
|
+
return output.error.trim();
|
|
3187
|
+
}
|
|
3188
|
+
if (typeof output.message === "string" && output.message.trim()) {
|
|
3189
|
+
return output.message.trim();
|
|
3190
|
+
}
|
|
3191
|
+
if (Array.isArray(output.content)) {
|
|
3192
|
+
const texts = output.content
|
|
3193
|
+
.map((item) => isRecord(item) && typeof item.text === "string"
|
|
3194
|
+
? item.text.trim()
|
|
3195
|
+
: "")
|
|
3196
|
+
.filter(Boolean);
|
|
3197
|
+
if (texts.length > 0) {
|
|
3198
|
+
return texts.join(" ");
|
|
3199
|
+
}
|
|
3200
|
+
}
|
|
3201
|
+
return undefined;
|
|
3202
|
+
}
|
|
3049
3203
|
function failure(sessionId, code, message, options = {}) {
|
|
3050
3204
|
const retryable = options.retryable ??
|
|
3051
3205
|
(code === "session_busy" ||
|
|
@@ -1,2 +1,19 @@
|
|
|
1
|
+
import type { ProviderRequest, ProviderResponse } from "../providers/types.js";
|
|
2
|
+
export type StructuredOutputFailureKind = "missing_json" | "invalid_json" | "schema_mismatch";
|
|
3
|
+
export declare class StructuredOutputError extends Error {
|
|
4
|
+
readonly kind: StructuredOutputFailureKind;
|
|
5
|
+
readonly rawText: string;
|
|
6
|
+
readonly responsePreview?: string;
|
|
7
|
+
readonly schemaErrors?: string[];
|
|
8
|
+
response?: ProviderResponse;
|
|
9
|
+
constructor(message: string, options: {
|
|
10
|
+
kind: StructuredOutputFailureKind;
|
|
11
|
+
rawText: string;
|
|
12
|
+
responsePreview?: string;
|
|
13
|
+
schemaErrors?: string[];
|
|
14
|
+
cause?: unknown;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
export declare function resolveProviderStructuredOutput(request: ProviderRequest, baseResponse: ProviderResponse, textOutput: string): ProviderResponse;
|
|
1
18
|
export declare function parseStructuredOutput(text: string, schema: Record<string, unknown>): unknown;
|
|
2
|
-
export declare function validateStructuredValue(value: unknown, schema: Record<string, unknown
|
|
19
|
+
export declare function validateStructuredValue(value: unknown, schema: Record<string, unknown>, label?: string): void;
|
|
@@ -1,24 +1,94 @@
|
|
|
1
|
+
export class StructuredOutputError extends Error {
|
|
2
|
+
kind;
|
|
3
|
+
rawText;
|
|
4
|
+
responsePreview;
|
|
5
|
+
schemaErrors;
|
|
6
|
+
response;
|
|
7
|
+
constructor(message, options) {
|
|
8
|
+
super(message, { cause: options.cause });
|
|
9
|
+
this.name = "StructuredOutputError";
|
|
10
|
+
this.kind = options.kind;
|
|
11
|
+
this.rawText = options.rawText;
|
|
12
|
+
this.responsePreview = options.responsePreview;
|
|
13
|
+
this.schemaErrors = options.schemaErrors;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export function resolveProviderStructuredOutput(request, baseResponse, textOutput) {
|
|
17
|
+
if (request.outputType !== "json" ||
|
|
18
|
+
baseResponse.toolCalls.length > 0 ||
|
|
19
|
+
baseResponse.stopReason === "max_tokens" ||
|
|
20
|
+
baseResponse.stopReason === "content_filter") {
|
|
21
|
+
return baseResponse;
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
return {
|
|
25
|
+
...baseResponse,
|
|
26
|
+
output: parseStructuredOutput(textOutput, request.outputSchema ?? {}),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
if (error instanceof StructuredOutputError) {
|
|
31
|
+
error.response = baseResponse;
|
|
32
|
+
}
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
1
36
|
export function parseStructuredOutput(text, schema) {
|
|
37
|
+
const preview = previewText(text);
|
|
2
38
|
const candidate = extractJsonCandidate(text);
|
|
3
39
|
if (!candidate) {
|
|
4
|
-
|
|
40
|
+
const suffix = preview
|
|
41
|
+
? ` Model returned plain text: ${JSON.stringify(preview)}`
|
|
42
|
+
: " Model returned an empty response.";
|
|
43
|
+
throw new StructuredOutputError(`Model response did not contain a JSON object or array.${suffix}`, {
|
|
44
|
+
kind: "missing_json",
|
|
45
|
+
rawText: text,
|
|
46
|
+
responsePreview: preview,
|
|
47
|
+
});
|
|
5
48
|
}
|
|
6
49
|
let value;
|
|
7
50
|
try {
|
|
8
51
|
value = JSON.parse(candidate);
|
|
9
52
|
}
|
|
10
53
|
catch (error) {
|
|
11
|
-
|
|
54
|
+
const detail = error instanceof Error ? error.message : "Invalid JSON";
|
|
55
|
+
const suffix = preview
|
|
56
|
+
? ` Model returned: ${JSON.stringify(preview)}`
|
|
57
|
+
: "";
|
|
58
|
+
throw new StructuredOutputError(`Model response contained invalid JSON (${detail}).${suffix}`, {
|
|
59
|
+
kind: "invalid_json",
|
|
60
|
+
rawText: text,
|
|
61
|
+
responsePreview: preview,
|
|
62
|
+
cause: error,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
const errors = [];
|
|
66
|
+
validateSchemaValue(value, schema, "$", errors);
|
|
67
|
+
if (errors.length > 0) {
|
|
68
|
+
throw new StructuredOutputError(`Value failed outputSchema: ${errors.join("; ")}`, {
|
|
69
|
+
kind: "schema_mismatch",
|
|
70
|
+
rawText: text,
|
|
71
|
+
responsePreview: preview,
|
|
72
|
+
schemaErrors: errors,
|
|
73
|
+
});
|
|
12
74
|
}
|
|
13
|
-
validateStructuredValue(value, schema);
|
|
14
75
|
return value;
|
|
15
76
|
}
|
|
16
|
-
export function validateStructuredValue(value, schema) {
|
|
77
|
+
export function validateStructuredValue(value, schema, label = "outputSchema") {
|
|
17
78
|
const errors = [];
|
|
18
79
|
validateSchemaValue(value, schema, "$", errors);
|
|
19
80
|
if (errors.length > 0) {
|
|
20
|
-
throw new Error(`Value failed
|
|
81
|
+
throw new Error(`Value failed ${label}: ${errors.join("; ")}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function previewText(text, maxLength = 240) {
|
|
85
|
+
const compact = text.replace(/\s+/g, " ").trim();
|
|
86
|
+
if (!compact) {
|
|
87
|
+
return undefined;
|
|
21
88
|
}
|
|
89
|
+
return compact.length > maxLength
|
|
90
|
+
? `${compact.slice(0, maxLength)}...`
|
|
91
|
+
: compact;
|
|
22
92
|
}
|
|
23
93
|
function extractJsonCandidate(text) {
|
|
24
94
|
const trimmed = text.trim();
|