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
package/dist-cjs/errors.js
CHANGED
|
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
var errors_exports = {};
|
|
21
21
|
__export(errors_exports, {
|
|
22
22
|
OrchaError: () => OrchaError,
|
|
23
|
+
createProviderHttpError: () => createProviderHttpError,
|
|
23
24
|
describeProviderError: () => describeProviderError,
|
|
24
25
|
isRetryableProviderConnectionError: () => isRetryableProviderConnectionError
|
|
25
26
|
});
|
|
@@ -36,18 +37,74 @@ class OrchaError extends Error {
|
|
|
36
37
|
this.details = options.details;
|
|
37
38
|
}
|
|
38
39
|
}
|
|
40
|
+
const NON_RETRYABLE_STATUS_CODES = /* @__PURE__ */ new Set([400, 401, 403, 404, 413, 422]);
|
|
41
|
+
const NON_RETRYABLE_ERROR_NAMES = /* @__PURE__ */ new Set([
|
|
42
|
+
"ValidationException",
|
|
43
|
+
"AccessDeniedException",
|
|
44
|
+
"ResourceNotFoundException",
|
|
45
|
+
"UnrecognizedClientException",
|
|
46
|
+
"InvalidSignatureException",
|
|
47
|
+
"ExpiredTokenException"
|
|
48
|
+
]);
|
|
49
|
+
async function createProviderHttpError(response, providerLabel) {
|
|
50
|
+
const status = response.status;
|
|
51
|
+
const requestId = response.headers.get("request-id") ?? response.headers.get("x-request-id") ?? response.headers.get("x-amzn-requestid") ?? void 0;
|
|
52
|
+
let rawText = "";
|
|
53
|
+
try {
|
|
54
|
+
rawText = (await response.text()).trim();
|
|
55
|
+
} catch {
|
|
56
|
+
rawText = "";
|
|
57
|
+
}
|
|
58
|
+
let parsedMessage;
|
|
59
|
+
let errorCode;
|
|
60
|
+
if (rawText) {
|
|
61
|
+
try {
|
|
62
|
+
const parsed = JSON.parse(rawText);
|
|
63
|
+
const nestedError = typeof parsed.error === "object" && parsed.error !== null ? parsed.error : void 0;
|
|
64
|
+
if (typeof nestedError?.message === "string" && nestedError.message.trim()) {
|
|
65
|
+
parsedMessage = nestedError.message.trim();
|
|
66
|
+
} else if (typeof parsed.message === "string" && parsed.message.trim()) {
|
|
67
|
+
parsedMessage = parsed.message.trim();
|
|
68
|
+
} else if (typeof parsed.detail === "string" && parsed.detail.trim()) {
|
|
69
|
+
parsedMessage = parsed.detail.trim();
|
|
70
|
+
}
|
|
71
|
+
errorCode = stringOrNumberField(nestedError, "code") ?? stringOrNumberField(nestedError, "type") ?? stringOrNumberField(parsed, "code");
|
|
72
|
+
} catch {
|
|
73
|
+
const compact = rawText.replace(/\s+/g, " ").trim();
|
|
74
|
+
if (compact) {
|
|
75
|
+
parsedMessage = compact.length > 300 ? `${compact.slice(0, 300)}...` : compact;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const message = parsedMessage ?? `${providerLabel} request failed with HTTP ${status}.`;
|
|
80
|
+
const error = new Error(message);
|
|
81
|
+
error.statusCode = status;
|
|
82
|
+
if (requestId) {
|
|
83
|
+
error.requestId = requestId;
|
|
84
|
+
}
|
|
85
|
+
if (errorCode !== void 0) {
|
|
86
|
+
error.code = errorCode;
|
|
87
|
+
}
|
|
88
|
+
return error;
|
|
89
|
+
}
|
|
39
90
|
function describeProviderError(error) {
|
|
40
91
|
const rootMessage = errorMessage(error);
|
|
92
|
+
const statusCode = extractStatusCode(error);
|
|
93
|
+
const requestId = extractRequestId(error);
|
|
94
|
+
const errorName = extractErrorName(error);
|
|
95
|
+
const errorCode = stringOrNumberField(error, "code") ?? stringOrNumberField(error, "type");
|
|
96
|
+
const retryable = isProviderErrorRetryable(statusCode, errorName);
|
|
41
97
|
const cause = deepestUsefulCause(error);
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
const
|
|
47
|
-
const syscall = stringField(cause, "syscall");
|
|
48
|
-
const hostname = stringField(cause, "hostname");
|
|
49
|
-
const explanation = causeMessage !== rootMessage ? causeMessage : causeCode ? String(causeCode) : void 0;
|
|
98
|
+
const causeMessage = cause ? errorMessage(cause) : void 0;
|
|
99
|
+
const causeCode = cause ? stringOrNumberField(cause, "code") : void 0;
|
|
100
|
+
const syscall = cause ? stringField(cause, "syscall") : void 0;
|
|
101
|
+
const hostname = cause ? stringField(cause, "hostname") : void 0;
|
|
102
|
+
const explanation = causeMessage && causeMessage !== rootMessage ? causeMessage : causeCode !== void 0 ? String(causeCode) : void 0;
|
|
50
103
|
const details = {
|
|
104
|
+
...statusCode !== void 0 ? { statusCode } : {},
|
|
105
|
+
...requestId ? { requestId } : {},
|
|
106
|
+
...errorName ? { errorName } : {},
|
|
107
|
+
...errorCode !== void 0 ? { errorCode } : {},
|
|
51
108
|
...causeCode !== void 0 ? { causeCode } : {},
|
|
52
109
|
...syscall ? { syscall } : {},
|
|
53
110
|
...hostname ? { hostname } : {},
|
|
@@ -55,6 +112,7 @@ function describeProviderError(error) {
|
|
|
55
112
|
};
|
|
56
113
|
return {
|
|
57
114
|
message: explanation ? `${rootMessage}: ${explanation}` : rootMessage,
|
|
115
|
+
retryable,
|
|
58
116
|
...Object.keys(details).length > 0 ? { details } : {}
|
|
59
117
|
};
|
|
60
118
|
}
|
|
@@ -83,6 +141,45 @@ function isRetryableProviderConnectionError(error) {
|
|
|
83
141
|
}
|
|
84
142
|
return false;
|
|
85
143
|
}
|
|
144
|
+
function isProviderErrorRetryable(statusCode, errorName) {
|
|
145
|
+
if (errorName && NON_RETRYABLE_ERROR_NAMES.has(errorName)) {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
if (statusCode !== void 0 && NON_RETRYABLE_STATUS_CODES.has(statusCode)) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
function extractStatusCode(error) {
|
|
154
|
+
if (typeof error !== "object" || error === null) return void 0;
|
|
155
|
+
const record = error;
|
|
156
|
+
if (typeof record.statusCode === "number") return record.statusCode;
|
|
157
|
+
if (typeof record.status === "number") return record.status;
|
|
158
|
+
const metadata = typeof record.$metadata === "object" && record.$metadata !== null ? record.$metadata : void 0;
|
|
159
|
+
if (typeof metadata?.httpStatusCode === "number") {
|
|
160
|
+
return metadata.httpStatusCode;
|
|
161
|
+
}
|
|
162
|
+
return void 0;
|
|
163
|
+
}
|
|
164
|
+
function extractRequestId(error) {
|
|
165
|
+
if (typeof error !== "object" || error === null) return void 0;
|
|
166
|
+
const record = error;
|
|
167
|
+
if (typeof record.requestId === "string" && record.requestId.trim()) {
|
|
168
|
+
return record.requestId.trim();
|
|
169
|
+
}
|
|
170
|
+
const metadata = typeof record.$metadata === "object" && record.$metadata !== null ? record.$metadata : void 0;
|
|
171
|
+
if (typeof metadata?.requestId === "string" && metadata.requestId.trim()) {
|
|
172
|
+
return metadata.requestId.trim();
|
|
173
|
+
}
|
|
174
|
+
return void 0;
|
|
175
|
+
}
|
|
176
|
+
function extractErrorName(error) {
|
|
177
|
+
if (!(error instanceof Error)) return void 0;
|
|
178
|
+
if (error.name && error.name !== "Error" && error.name !== "OrchaError") {
|
|
179
|
+
return error.name;
|
|
180
|
+
}
|
|
181
|
+
return void 0;
|
|
182
|
+
}
|
|
86
183
|
function deepestUsefulCause(error) {
|
|
87
184
|
let current = error;
|
|
88
185
|
let useful;
|
|
@@ -125,6 +222,7 @@ function stringOrNumberField(value, key) {
|
|
|
125
222
|
// Annotate the CommonJS export names for ESM import in node:
|
|
126
223
|
0 && (module.exports = {
|
|
127
224
|
OrchaError,
|
|
225
|
+
createProviderHttpError,
|
|
128
226
|
describeProviderError,
|
|
129
227
|
isRetryableProviderConnectionError
|
|
130
228
|
});
|
package/dist-cjs/file-content.js
CHANGED
|
@@ -24,6 +24,7 @@ __export(file_content_exports, {
|
|
|
24
24
|
readLocalFileContent: () => readLocalFileContent
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(file_content_exports);
|
|
27
|
+
var import_errors = require("./errors.js");
|
|
27
28
|
const MIME_TYPES = {
|
|
28
29
|
".csv": "text/csv",
|
|
29
30
|
".doc": "application/msword",
|
|
@@ -72,7 +73,21 @@ function normalizeUrlContent(url, mimeType) {
|
|
|
72
73
|
async function readLocalFileContent(content) {
|
|
73
74
|
const fsModule = "fs/promises";
|
|
74
75
|
const { readFile } = await import(fsModule);
|
|
75
|
-
|
|
76
|
+
let bytes;
|
|
77
|
+
try {
|
|
78
|
+
bytes = await readFile(content.filePath);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
81
|
+
throw new import_errors.OrchaError(
|
|
82
|
+
"invalid_input",
|
|
83
|
+
`Failed to read local file "${content.filePath}": ${reason}`,
|
|
84
|
+
{
|
|
85
|
+
retryable: false,
|
|
86
|
+
cause: error,
|
|
87
|
+
details: { filePath: content.filePath }
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
}
|
|
76
91
|
const base64 = bytes.toString("base64");
|
|
77
92
|
return {
|
|
78
93
|
bytes,
|
|
@@ -82,10 +82,7 @@ ${JSON.stringify(request.outputSchema)}` : request.systemPrompt;
|
|
|
82
82
|
}
|
|
83
83
|
);
|
|
84
84
|
if (!response.ok) {
|
|
85
|
-
|
|
86
|
-
throw new Error(
|
|
87
|
-
body2.error?.message ?? `Anthropic request failed with HTTP ${response.status}.`
|
|
88
|
-
);
|
|
85
|
+
throw await (0, import_errors.createProviderHttpError)(response, "Anthropic");
|
|
89
86
|
}
|
|
90
87
|
const body = response.headers.get("content-type")?.includes("text/event-stream") ? await readAnthropicStream(response, request.publishOutput) : await response.json();
|
|
91
88
|
return toProviderResponse(request, body);
|
|
@@ -97,24 +94,27 @@ function toProviderResponse(request, body) {
|
|
|
97
94
|
(item) => item.type === "tool_call"
|
|
98
95
|
);
|
|
99
96
|
const stopReason = normalizeStopReason(body.stop_reason);
|
|
100
|
-
const output = request.outputType === "json" && toolCalls.length === 0 && stopReason !== "max_tokens" ? (0, import_structured_output.parseStructuredOutput)(textOutput, request.outputSchema ?? {}) : textOutput;
|
|
101
97
|
if (textOutput && request.publishOutput) {
|
|
102
98
|
request.publishOutput(textOutput);
|
|
103
99
|
}
|
|
104
|
-
return
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
100
|
+
return (0, import_structured_output.resolveProviderStructuredOutput)(
|
|
101
|
+
request,
|
|
102
|
+
{
|
|
103
|
+
output: textOutput,
|
|
104
|
+
responseId: body.id,
|
|
105
|
+
stopReason,
|
|
106
|
+
content,
|
|
107
|
+
toolCalls,
|
|
108
|
+
usage: {
|
|
109
|
+
inputTokens: body.usage?.input_tokens ?? 0,
|
|
110
|
+
outputTokens: body.usage?.output_tokens ?? 0,
|
|
111
|
+
reasoningTokens: null,
|
|
112
|
+
cacheReadTokens: body.usage?.cache_read_input_tokens ?? 0,
|
|
113
|
+
cacheWriteTokens: body.usage?.cache_creation_input_tokens ?? 0
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
textOutput
|
|
117
|
+
);
|
|
118
118
|
}
|
|
119
119
|
function normalizeStopReason(reason) {
|
|
120
120
|
if (reason === void 0 || reason === null) {
|
|
@@ -497,23 +497,24 @@ function toProviderResponse(request, response) {
|
|
|
497
497
|
(block) => block.type === "tool_call"
|
|
498
498
|
);
|
|
499
499
|
const stopReason = normalizeStopReason(response.stopReason);
|
|
500
|
-
return
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
}
|
|
516
|
-
|
|
500
|
+
return (0, import_structured_output.resolveProviderStructuredOutput)(
|
|
501
|
+
request,
|
|
502
|
+
{
|
|
503
|
+
output: textOutput,
|
|
504
|
+
responseId: response.requestId,
|
|
505
|
+
stopReason,
|
|
506
|
+
content: response.blocks,
|
|
507
|
+
toolCalls,
|
|
508
|
+
usage: {
|
|
509
|
+
inputTokens: response.usage?.inputTokens ?? 0,
|
|
510
|
+
outputTokens: response.usage?.outputTokens ?? 0,
|
|
511
|
+
reasoningTokens: null,
|
|
512
|
+
cacheReadTokens: response.usage?.cacheReadInputTokens ?? 0,
|
|
513
|
+
cacheWriteTokens: response.usage?.cacheWriteInputTokens ?? 0
|
|
514
|
+
}
|
|
515
|
+
},
|
|
516
|
+
textOutput
|
|
517
|
+
);
|
|
517
518
|
}
|
|
518
519
|
function normalizeStopReason(reason) {
|
|
519
520
|
if (!reason) {
|
|
@@ -86,10 +86,7 @@ async function generateDeepSeek(configuration, request) {
|
|
|
86
86
|
}
|
|
87
87
|
);
|
|
88
88
|
if (!response.ok) {
|
|
89
|
-
|
|
90
|
-
throw new Error(
|
|
91
|
-
error.error?.message ?? `DeepSeek request failed with HTTP ${response.status}.`
|
|
92
|
-
);
|
|
89
|
+
throw await (0, import_errors.createProviderHttpError)(response, "DeepSeek");
|
|
93
90
|
}
|
|
94
91
|
const collected = await collectDeepSeekStream(
|
|
95
92
|
response,
|
|
@@ -259,18 +256,18 @@ function toProviderResponse(request, response) {
|
|
|
259
256
|
(block) => block.type === "tool_call"
|
|
260
257
|
);
|
|
261
258
|
const stopReason = normalizeStopReason(response.finishReason, toolCalls);
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
259
|
+
return (0, import_structured_output.resolveProviderStructuredOutput)(
|
|
260
|
+
request,
|
|
261
|
+
{
|
|
262
|
+
output: response.content,
|
|
263
|
+
responseId: response.id,
|
|
264
|
+
stopReason,
|
|
265
|
+
content,
|
|
266
|
+
toolCalls,
|
|
267
|
+
usage: normalizeUsage(response.usage)
|
|
268
|
+
},
|
|
269
|
+
response.content
|
|
270
|
+
);
|
|
274
271
|
}
|
|
275
272
|
function parseToolArguments(value, toolName) {
|
|
276
273
|
let parsed;
|
|
@@ -130,10 +130,20 @@ async function readDeclaredPackages(projectRoot) {
|
|
|
130
130
|
return declared;
|
|
131
131
|
}
|
|
132
132
|
function allCompiledAgents(agents) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
133
|
+
const collected = [];
|
|
134
|
+
const visited = /* @__PURE__ */ new Set();
|
|
135
|
+
const visit = (agent) => {
|
|
136
|
+
if (visited.has(agent)) return;
|
|
137
|
+
visited.add(agent);
|
|
138
|
+
collected.push(agent);
|
|
139
|
+
for (const child of Object.values(agent.subagents ?? {})) {
|
|
140
|
+
visit(child);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
for (const agent of Object.values(agents)) {
|
|
144
|
+
visit(agent);
|
|
145
|
+
}
|
|
146
|
+
return collected;
|
|
137
147
|
}
|
|
138
148
|
function isInstalled(packageName, projectRoot) {
|
|
139
149
|
try {
|
|
@@ -150,15 +150,18 @@ function toProviderResponse(request, response) {
|
|
|
150
150
|
toolCalls.length > 0,
|
|
151
151
|
response.promptBlockReason !== void 0
|
|
152
152
|
);
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
153
|
+
return (0, import_structured_output.resolveProviderStructuredOutput)(
|
|
154
|
+
request,
|
|
155
|
+
{
|
|
156
|
+
output: textOutput,
|
|
157
|
+
responseId: response.responseId,
|
|
158
|
+
stopReason,
|
|
159
|
+
content,
|
|
160
|
+
toolCalls,
|
|
161
|
+
usage: normalizeUsage(response.usage)
|
|
162
|
+
},
|
|
163
|
+
textOutput
|
|
164
|
+
);
|
|
162
165
|
}
|
|
163
166
|
function appendGoogleTextPart(content, part) {
|
|
164
167
|
const text = part.text ?? "";
|
|
@@ -88,10 +88,7 @@ async function generateOpenAI(configuration, request) {
|
|
|
88
88
|
}
|
|
89
89
|
);
|
|
90
90
|
if (!response.ok) {
|
|
91
|
-
|
|
92
|
-
throw new Error(
|
|
93
|
-
errorBody.error?.message ?? `OpenAI request failed with HTTP ${response.status}.`
|
|
94
|
-
);
|
|
91
|
+
throw await (0, import_errors.createProviderHttpError)(response, "OpenAI");
|
|
95
92
|
}
|
|
96
93
|
const responseBody = response.headers.get("content-type")?.includes("text/event-stream") ? await readOpenAIStream(response, request.publishOutput) : await response.json();
|
|
97
94
|
return toProviderResponse(request, responseBody);
|
|
@@ -280,18 +277,21 @@ function toProviderResponse(request, response) {
|
|
|
280
277
|
);
|
|
281
278
|
const textOutput = response.output_text ?? content.filter((block) => block.type === "text").map((block) => block.text).join("");
|
|
282
279
|
const stopReason = normalizeStopReason(response, toolCalls, refused);
|
|
283
|
-
const output = request.outputType === "json" && toolCalls.length === 0 && stopReason !== "max_tokens" ? (0, import_structured_output.parseStructuredOutput)(textOutput, request.outputSchema ?? {}) : textOutput;
|
|
284
280
|
if (textOutput) {
|
|
285
281
|
request.publishOutput?.(textOutput);
|
|
286
282
|
}
|
|
287
|
-
return
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
283
|
+
return (0, import_structured_output.resolveProviderStructuredOutput)(
|
|
284
|
+
request,
|
|
285
|
+
{
|
|
286
|
+
output: textOutput,
|
|
287
|
+
responseId: response.id,
|
|
288
|
+
stopReason,
|
|
289
|
+
content,
|
|
290
|
+
toolCalls,
|
|
291
|
+
usage: normalizeUsage(response.usage)
|
|
292
|
+
},
|
|
293
|
+
textOutput
|
|
294
|
+
);
|
|
295
295
|
}
|
|
296
296
|
function parseFunctionArguments(argumentsJson, name) {
|
|
297
297
|
let parsed;
|