orchajs 0.24.0 → 0.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -3
- package/dist/amarsia/analytics.js +12 -0
- package/dist/amarsia/inference.d.ts +6 -0
- package/dist/amarsia/inference.js +377 -0
- package/dist/browser.js +5 -0
- package/dist/cli-agents-template.d.ts +1 -1
- package/dist/cli-agents-template.js +44 -11
- package/dist/cli.js +22 -9
- package/dist/compiler/build-project.js +57 -14
- package/dist/compiler/node-esm-banner.d.ts +2 -0
- package/dist/compiler/node-esm-banner.js +5 -0
- package/dist/orcha.js +5 -0
- package/dist/playground/server.js +3 -0
- package/dist/project-loader.js +4 -2
- package/dist/providers/anthropic.js +67 -58
- package/dist/providers/bedrock.js +14 -8
- package/dist/providers/deepseek.js +9 -2
- package/dist/providers/google-gemini.js +20 -15
- package/dist/providers/openai.js +6 -0
- package/dist/providers/types.d.ts +5 -0
- package/dist/react-native.js +5 -0
- package/dist/runtime/agent.d.ts +3 -3
- package/dist/runtime/agent.js +278 -45
- package/dist/runtime/compiler-hook.d.ts +4 -0
- package/dist/runtime/compiler-hook.js +9 -0
- package/dist/runtime/create-orcha.d.ts +19 -0
- package/dist/runtime/create-orcha.js +109 -12
- package/dist/runtime/optional-features.d.ts +19 -0
- package/dist/runtime/optional-features.js +22 -0
- package/dist/runtime/platform-browser.js +16 -3
- package/dist/runtime/platform-node.js +16 -2
- package/dist/runtime/platform-react-native.js +16 -3
- package/dist/runtime/session-events.js +19 -0
- package/dist/storage/browser-indexeddb.d.ts +3 -0
- package/dist/storage/browser-indexeddb.js +72 -8
- package/dist/storage/cloud-outbox.d.ts +30 -0
- package/dist/storage/cloud-outbox.js +628 -0
- package/dist/storage/cloud.d.ts +1 -0
- package/dist/storage/cloud.js +1 -0
- package/dist/storage/node-jsonl.d.ts +3 -0
- package/dist/storage/node-jsonl.js +273 -4
- package/dist/storage/node-sqlite.d.ts +3 -0
- package/dist/storage/node-sqlite.js +66 -17
- package/dist/storage/react-native-jsonl.d.ts +3 -0
- package/dist/storage/react-native-jsonl.js +223 -0
- package/dist/testing.js +3 -0
- package/dist/types.d.ts +25 -4
- package/dist-cjs/amarsia/analytics.js +12 -0
- package/dist-cjs/amarsia/inference.js +372 -0
- package/dist-cjs/browser.js +5 -0
- package/dist-cjs/cli-agents-template.js +44 -11
- package/dist-cjs/compiler/build-project.js +68 -4
- package/dist-cjs/compiler/node-esm-banner.js +32 -0
- package/dist-cjs/orcha.js +5 -0
- package/dist-cjs/playground/server.js +1 -0
- package/dist-cjs/project-loader.js +7 -5
- package/dist-cjs/providers/anthropic.js +65 -60
- package/dist-cjs/providers/bedrock.js +17 -9
- package/dist-cjs/providers/deepseek.js +10 -2
- package/dist-cjs/providers/google-gemini.js +27 -20
- package/dist-cjs/providers/openai.js +8 -0
- package/dist-cjs/react-native.js +5 -0
- package/dist-cjs/runtime/agent.js +311 -61
- package/dist-cjs/runtime/compiler-hook.js +11 -0
- package/dist-cjs/runtime/create-orcha.js +117 -12
- package/dist-cjs/runtime/optional-features.js +58 -0
- package/dist-cjs/runtime/platform-browser.js +16 -3
- package/dist-cjs/runtime/platform-node.js +16 -2
- package/dist-cjs/runtime/platform-react-native.js +18 -3
- package/dist-cjs/runtime/session-events.js +14 -0
- package/dist-cjs/storage/browser-indexeddb.js +73 -8
- package/dist-cjs/storage/cloud-outbox.js +646 -0
- package/dist-cjs/storage/cloud.js +29 -0
- package/dist-cjs/storage/node-jsonl.js +246 -3
- package/dist-cjs/storage/node-sqlite.js +70 -19
- package/dist-cjs/storage/react-native-jsonl.js +207 -0
- package/dist-cjs/testing.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
"use strict"; const __orchaImportMetaUrl = require("url").pathToFileURL(__filename).href;
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var inference_exports = {};
|
|
21
|
+
__export(inference_exports, {
|
|
22
|
+
createAmarsiaInferenceGenerator: () => createAmarsiaInferenceGenerator
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(inference_exports);
|
|
25
|
+
var import_errors = require("../errors.js");
|
|
26
|
+
var import_file_content = require("../file-content.js");
|
|
27
|
+
var import_sse = require("../providers/sse.js");
|
|
28
|
+
var import_structured_output = require("../runtime/structured-output.js");
|
|
29
|
+
var import_endpoint = require("./endpoint.js");
|
|
30
|
+
const SUPPORTED_AMARSIA_PROVIDERS = /* @__PURE__ */ new Set([
|
|
31
|
+
"openai",
|
|
32
|
+
"anthropic",
|
|
33
|
+
"googlegenai",
|
|
34
|
+
"vertexai",
|
|
35
|
+
"deepseek"
|
|
36
|
+
]);
|
|
37
|
+
const TEXT_ONLY_AMARSIA_PROVIDERS = /* @__PURE__ */ new Set(["deepseek"]);
|
|
38
|
+
function createAmarsiaInferenceGenerator(configuration) {
|
|
39
|
+
const endpoint = (0, import_endpoint.resolveAmarsiaEndpoint)(configuration.endpoint);
|
|
40
|
+
return (provider, _providerConfiguration, request) => generateThroughAmarsia(endpoint, configuration.key, provider, request);
|
|
41
|
+
}
|
|
42
|
+
async function generateThroughAmarsia(endpoint, key, provider, request) {
|
|
43
|
+
if (!key.trim()) {
|
|
44
|
+
throw new Error(
|
|
45
|
+
"Amarsia inference requires a non-empty amarsia.key."
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
if (!SUPPORTED_AMARSIA_PROVIDERS.has(provider)) {
|
|
49
|
+
throw new import_errors.OrchaError(
|
|
50
|
+
"unsupported_provider_capability",
|
|
51
|
+
`Amarsia inference supports only OpenAI, Anthropic, Google GenAI, Vertex AI, and DeepSeek; received "${provider}".`
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
if (request.outputType === "image" || request.outputType === "audio") {
|
|
55
|
+
throw new import_errors.OrchaError(
|
|
56
|
+
"unsupported_provider_capability",
|
|
57
|
+
`Amarsia Responses does not support "${request.outputType}" as a direct agent output.`
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
assertProviderSupportsMessageContent(provider, request.messages);
|
|
61
|
+
const gatewayProvider = provider === "vertexai" ? "googlegenai" : provider;
|
|
62
|
+
const body = {
|
|
63
|
+
provider: gatewayProvider,
|
|
64
|
+
model: request.model,
|
|
65
|
+
instructions: request.systemPrompt,
|
|
66
|
+
input: await toResponsesInput(request.messages),
|
|
67
|
+
...provider === "vertexai" ? { region: "global" } : request.region && request.region !== "provider_managed" ? { region: request.region } : {},
|
|
68
|
+
...request.maxTokens ? { max_output_tokens: request.maxTokens } : {},
|
|
69
|
+
...request.publishOutput ? { stream: true } : {}
|
|
70
|
+
};
|
|
71
|
+
if (request.tools.length > 0) {
|
|
72
|
+
body.tools = request.tools.map((tool) => ({
|
|
73
|
+
type: "function",
|
|
74
|
+
name: tool.name,
|
|
75
|
+
description: tool.description,
|
|
76
|
+
parameters: tool.parameters
|
|
77
|
+
}));
|
|
78
|
+
}
|
|
79
|
+
if (request.outputType === "json") {
|
|
80
|
+
body.text = {
|
|
81
|
+
format: {
|
|
82
|
+
type: "json_schema",
|
|
83
|
+
name: "orcha_output",
|
|
84
|
+
schema: request.outputSchema ?? {},
|
|
85
|
+
strict: true
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
body.reasoning = {
|
|
90
|
+
summary: "auto",
|
|
91
|
+
...request.reasoningLevel ? { effort: request.reasoningLevel } : {}
|
|
92
|
+
};
|
|
93
|
+
body.include = ["reasoning.encrypted_content"];
|
|
94
|
+
if (request.trace) {
|
|
95
|
+
body.metadata = {
|
|
96
|
+
agent_external_id: request.trace.agentExternalId,
|
|
97
|
+
session_id: request.trace.sessionId
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
const response = await fetch(`${endpoint}/v3/model/v1/responses`, {
|
|
101
|
+
method: "POST",
|
|
102
|
+
headers: {
|
|
103
|
+
"content-type": "application/json",
|
|
104
|
+
authorization: `Bearer ${key}`
|
|
105
|
+
},
|
|
106
|
+
body: JSON.stringify(body),
|
|
107
|
+
signal: request.signal
|
|
108
|
+
});
|
|
109
|
+
if (!response.ok) {
|
|
110
|
+
const message = await responseError(response);
|
|
111
|
+
throw gatewayRequestError(message, response.status);
|
|
112
|
+
}
|
|
113
|
+
const responseBody = response.headers.get("content-type")?.includes("text/event-stream") ? await readGatewayStream(response, request.publishOutput) : await response.json();
|
|
114
|
+
return toProviderResponse(request, responseBody);
|
|
115
|
+
}
|
|
116
|
+
function assertProviderSupportsMessageContent(provider, messages) {
|
|
117
|
+
if (!TEXT_ONLY_AMARSIA_PROVIDERS.has(provider)) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
for (const message of messages) {
|
|
121
|
+
if (message.role === "user" && Array.isArray(message.content)) {
|
|
122
|
+
for (const block of message.content) {
|
|
123
|
+
if (block.type !== "text") {
|
|
124
|
+
throw deepSeekMultimodalError(block.type);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (message.role === "tool") {
|
|
129
|
+
for (const result of message.results) {
|
|
130
|
+
for (const block of result.contents ?? []) {
|
|
131
|
+
if (block.type !== "text") {
|
|
132
|
+
throw deepSeekMultimodalError(block.type);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function deepSeekMultimodalError(contentType) {
|
|
140
|
+
return new import_errors.OrchaError(
|
|
141
|
+
"unsupported_content_type",
|
|
142
|
+
`DeepSeek does not support multimodal content (received "${contentType}"). DeepSeek is text-only and cannot accept images, PDFs, or other file attachments.`
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
function gatewayRequestError(message, statusCode) {
|
|
146
|
+
const detail = message?.trim() || `Amarsia inference request failed with HTTP ${statusCode}.`;
|
|
147
|
+
if (/does not support multimodal|text-only/i.test(detail)) {
|
|
148
|
+
return new import_errors.OrchaError("unsupported_content_type", detail);
|
|
149
|
+
}
|
|
150
|
+
return new Error(detail);
|
|
151
|
+
}
|
|
152
|
+
async function toResponsesInput(messages) {
|
|
153
|
+
const input = [];
|
|
154
|
+
for (const message of messages) {
|
|
155
|
+
if (message.role === "user") {
|
|
156
|
+
input.push({
|
|
157
|
+
role: "user",
|
|
158
|
+
content: typeof message.content === "string" ? message.content : await Promise.all(
|
|
159
|
+
message.content.map(toResponsesUserContent)
|
|
160
|
+
)
|
|
161
|
+
});
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (message.role === "tool") {
|
|
165
|
+
for (const result of message.results) {
|
|
166
|
+
const contents = result.contents?.length ? await Promise.all(result.contents.map(toResponsesUserContent)) : void 0;
|
|
167
|
+
input.push({
|
|
168
|
+
type: "function_call_output",
|
|
169
|
+
call_id: result.callId,
|
|
170
|
+
output: JSON.stringify(result.output ?? null),
|
|
171
|
+
...contents ? { contents } : {},
|
|
172
|
+
...result.isError ? { is_error: true } : {}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
for (const block of message.content) {
|
|
178
|
+
if (block.type === "text") {
|
|
179
|
+
input.push({
|
|
180
|
+
role: "assistant",
|
|
181
|
+
content: [{ type: "output_text", text: block.text }]
|
|
182
|
+
});
|
|
183
|
+
} else if (block.type === "tool_call") {
|
|
184
|
+
input.push({
|
|
185
|
+
type: "function_call",
|
|
186
|
+
call_id: block.callId,
|
|
187
|
+
name: block.name,
|
|
188
|
+
arguments: JSON.stringify(block.arguments),
|
|
189
|
+
...block.replay?.providerId ? { id: block.replay.providerId } : {},
|
|
190
|
+
...block.replay?.opaqueData ? { encrypted_content: block.replay.opaqueData } : {}
|
|
191
|
+
});
|
|
192
|
+
} else {
|
|
193
|
+
input.push({
|
|
194
|
+
type: "reasoning",
|
|
195
|
+
...block.replay?.providerId ? { id: block.replay.providerId } : {},
|
|
196
|
+
summary: block.text ? [{ type: "summary_text", text: block.text }] : [],
|
|
197
|
+
...block.replay?.opaqueData ? { encrypted_content: block.replay.opaqueData } : {}
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return input;
|
|
203
|
+
}
|
|
204
|
+
async function toResponsesUserContent(content) {
|
|
205
|
+
if (content.type === "text") {
|
|
206
|
+
return { type: "input_text", text: content.text };
|
|
207
|
+
}
|
|
208
|
+
if (content.type === "file") {
|
|
209
|
+
const file = await (0, import_file_content.readLocalFileContent)(content);
|
|
210
|
+
return content.mimeType.startsWith("image/") ? {
|
|
211
|
+
type: "input_image",
|
|
212
|
+
image_url: file.dataUri,
|
|
213
|
+
detail: "auto"
|
|
214
|
+
} : {
|
|
215
|
+
type: "input_file",
|
|
216
|
+
file_data: file.dataUri,
|
|
217
|
+
filename: file.fileName
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
if (content.type === "image") {
|
|
221
|
+
return {
|
|
222
|
+
type: "input_image",
|
|
223
|
+
image_url: content.fileUri,
|
|
224
|
+
detail: "auto"
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
if (content.type === "url") {
|
|
228
|
+
return { type: "input_file", file_url: content.fileUri };
|
|
229
|
+
}
|
|
230
|
+
throw new import_errors.OrchaError(
|
|
231
|
+
"unsupported_content_type",
|
|
232
|
+
`Amarsia Responses does not support ${content.type} content from a URI.`
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
async function readGatewayStream(response, publishOutput) {
|
|
236
|
+
let text = "";
|
|
237
|
+
let terminal;
|
|
238
|
+
for await (const event of (0, import_sse.readServerSentEvents)(response)) {
|
|
239
|
+
if (event.type === "response.output_text.delta" && typeof event.delta === "string") {
|
|
240
|
+
text += event.delta;
|
|
241
|
+
publishOutput?.(text);
|
|
242
|
+
} else if (event.type === "response.completed" || event.type === "response.incomplete") {
|
|
243
|
+
if (!isRecord(event.response)) {
|
|
244
|
+
throw new Error(
|
|
245
|
+
"Amarsia returned an invalid terminal stream event."
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
terminal = event.response;
|
|
249
|
+
} else if (event.type === "response.failed" || event.type === "error") {
|
|
250
|
+
const error = isRecord(event.error) ? event.error : event;
|
|
251
|
+
const message = typeof error.message === "string" ? error.message : "Amarsia inference streaming request failed.";
|
|
252
|
+
throw gatewayRequestError(message, 500);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if (!terminal) {
|
|
256
|
+
throw new Error(
|
|
257
|
+
"Amarsia inference stream ended without a terminal response."
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
return terminal;
|
|
261
|
+
}
|
|
262
|
+
function toProviderResponse(request, response) {
|
|
263
|
+
if (response.status === "failed") {
|
|
264
|
+
throw new Error(
|
|
265
|
+
response.error?.message ?? "Amarsia failed to generate a response."
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
if (response.status === "incomplete" && response.incomplete_details?.reason !== "max_output_tokens") {
|
|
269
|
+
throw new Error(
|
|
270
|
+
`Amarsia returned an incomplete response: ${response.incomplete_details?.reason ?? "unknown reason"}.`
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
const content = [];
|
|
274
|
+
let refused = false;
|
|
275
|
+
for (const item of response.output ?? []) {
|
|
276
|
+
if (item.type === "message") {
|
|
277
|
+
for (const part of item.content ?? []) {
|
|
278
|
+
if (part.type === "output_text" && typeof part.text === "string") {
|
|
279
|
+
content.push({ type: "text", text: part.text });
|
|
280
|
+
} else if (part.type === "refusal" && typeof part.refusal === "string") {
|
|
281
|
+
refused = true;
|
|
282
|
+
content.push({ type: "text", text: part.refusal });
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
} else if (item.type === "function_call" && typeof item.call_id === "string" && typeof item.name === "string") {
|
|
286
|
+
content.push({
|
|
287
|
+
type: "tool_call",
|
|
288
|
+
callId: item.call_id,
|
|
289
|
+
name: item.name,
|
|
290
|
+
arguments: parseArguments(item.arguments, item.name),
|
|
291
|
+
replay: replay(item)
|
|
292
|
+
});
|
|
293
|
+
} else if (item.type === "reasoning") {
|
|
294
|
+
content.push({
|
|
295
|
+
type: "reasoning",
|
|
296
|
+
text: [...item.content ?? [], ...item.summary ?? []].filter(
|
|
297
|
+
(part) => (part.type === "reasoning_text" || part.type === "summary_text") && typeof part.text === "string"
|
|
298
|
+
).map((part) => part.text).join(""),
|
|
299
|
+
replay: replay(item)
|
|
300
|
+
});
|
|
301
|
+
} else {
|
|
302
|
+
throw new Error(
|
|
303
|
+
`Amarsia returned unsupported output item "${String(item.type)}".`
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
const toolCalls = content.filter(
|
|
308
|
+
(block) => block.type === "tool_call"
|
|
309
|
+
);
|
|
310
|
+
const textOutput = response.output_text ?? content.filter((block) => block.type === "text").map((block) => block.text).join("");
|
|
311
|
+
const stopReason = normalizeStopReason(response, toolCalls, refused);
|
|
312
|
+
const output = request.outputType === "json" && toolCalls.length === 0 && stopReason !== "max_tokens" ? (0, import_structured_output.parseStructuredOutput)(textOutput, request.outputSchema ?? {}) : textOutput;
|
|
313
|
+
if (textOutput) {
|
|
314
|
+
request.publishOutput?.(textOutput);
|
|
315
|
+
}
|
|
316
|
+
return {
|
|
317
|
+
output,
|
|
318
|
+
responseId: response.id,
|
|
319
|
+
stopReason,
|
|
320
|
+
content,
|
|
321
|
+
toolCalls,
|
|
322
|
+
usage: normalizeUsage(response.usage)
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
function replay(item) {
|
|
326
|
+
const value = {
|
|
327
|
+
...typeof item.id === "string" ? { providerId: item.id } : {},
|
|
328
|
+
...typeof item.encrypted_content === "string" ? { opaqueData: item.encrypted_content } : {}
|
|
329
|
+
};
|
|
330
|
+
return Object.keys(value).length > 0 ? value : void 0;
|
|
331
|
+
}
|
|
332
|
+
function parseArguments(value, name) {
|
|
333
|
+
const parsed = typeof value === "string" ? JSON.parse(value) : value;
|
|
334
|
+
if (!isRecord(parsed)) {
|
|
335
|
+
throw new Error(
|
|
336
|
+
`Amarsia returned non-object arguments for tool "${name}".`
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
return parsed;
|
|
340
|
+
}
|
|
341
|
+
function normalizeStopReason(response, toolCalls, refused) {
|
|
342
|
+
if (refused) return "content_filter";
|
|
343
|
+
if (toolCalls.length > 0) return "tool_call";
|
|
344
|
+
if (response.status === "incomplete") {
|
|
345
|
+
return response.incomplete_details?.reason === "max_output_tokens" ? "max_tokens" : "unknown";
|
|
346
|
+
}
|
|
347
|
+
return response.status === "completed" ? "end_turn" : "unknown";
|
|
348
|
+
}
|
|
349
|
+
function normalizeUsage(usage) {
|
|
350
|
+
return {
|
|
351
|
+
inputTokens: usage?.input_tokens ?? 0,
|
|
352
|
+
outputTokens: usage?.output_tokens ?? 0,
|
|
353
|
+
reasoningTokens: usage?.output_tokens_details?.reasoning_tokens ?? null,
|
|
354
|
+
cacheReadTokens: usage?.input_tokens_details?.cached_tokens ?? 0,
|
|
355
|
+
cacheWriteTokens: usage?.cache_write_tokens ?? 0
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
async function responseError(response) {
|
|
359
|
+
try {
|
|
360
|
+
const body = await response.json();
|
|
361
|
+
return body.error?.message ?? body.message ?? (typeof body.detail === "string" ? body.detail : void 0);
|
|
362
|
+
} catch {
|
|
363
|
+
return void 0;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
function isRecord(value) {
|
|
367
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
368
|
+
}
|
|
369
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
370
|
+
0 && (module.exports = {
|
|
371
|
+
createAmarsiaInferenceGenerator
|
|
372
|
+
});
|
package/dist-cjs/browser.js
CHANGED
|
@@ -23,15 +23,20 @@ __export(browser_exports, {
|
|
|
23
23
|
orcha: () => orcha
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(browser_exports);
|
|
26
|
+
var import_inference = require("./amarsia/inference.js");
|
|
26
27
|
var import_browser = require("./providers/browser.js");
|
|
27
28
|
var import_execute = require("./runtime/actions/execute.js");
|
|
28
29
|
var import_create_orcha = require("./runtime/create-orcha.js");
|
|
30
|
+
var import_optional_features = require("./runtime/optional-features.js");
|
|
29
31
|
var import_platform_browser = require("./runtime/platform-browser.js");
|
|
32
|
+
var import_cloud = require("./storage/cloud.js");
|
|
30
33
|
__reExport(browser_exports, require("./runtime-entry.js"), module.exports);
|
|
31
34
|
(0, import_execute.setNativeRuntimeLoader)(async () => {
|
|
32
35
|
const modulePath = "./runtime/actions/browser-native.js";
|
|
33
36
|
return import(modulePath);
|
|
34
37
|
});
|
|
38
|
+
(0, import_optional_features.setInferenceGeneratorFactory)(import_inference.createAmarsiaInferenceGenerator);
|
|
39
|
+
(0, import_optional_features.setCloudSessionStoreFactory)((options) => new import_cloud.CloudSessionStore(options));
|
|
35
40
|
const orcha = (0, import_create_orcha.createOrcha)(
|
|
36
41
|
() => {
|
|
37
42
|
throw new Error(
|
|
@@ -92,6 +92,23 @@ orcha.init({
|
|
|
92
92
|
});
|
|
93
93
|
\`\`\`
|
|
94
94
|
|
|
95
|
+
For Amarsia inference, the registry contains only project routing and agent
|
|
96
|
+
paths; provider and model selection stay in each agent \`index.json\`. Supported
|
|
97
|
+
providers are \`openai\`, \`anthropic\`, \`googlegenai\`, and \`deepseek\`; \`bedrock\`
|
|
98
|
+
and \`vertexai\` are direct-inference providers only:
|
|
99
|
+
|
|
100
|
+
\`\`\`ts
|
|
101
|
+
orcha.init({
|
|
102
|
+
amarsia: {
|
|
103
|
+
key: process.env.AMARSIA_PROJECT_KEY ?? "",
|
|
104
|
+
inference: true,
|
|
105
|
+
},
|
|
106
|
+
agents: {
|
|
107
|
+
supportBot: "./supportBot",
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
\`\`\`
|
|
111
|
+
|
|
95
112
|
Only registered agents are compiled. Agent keys become runtime properties
|
|
96
113
|
such as \`orcha.supportBot\`. Local actions use the native Node.js runtime by
|
|
97
114
|
default. Set \`actions.runtime: "sandbox"\` only when isolated execution is
|
|
@@ -99,22 +116,29 @@ required.
|
|
|
99
116
|
|
|
100
117
|
\`orcha.init()\` fields:
|
|
101
118
|
|
|
102
|
-
- \`providers\` (required): provider configurations keyed
|
|
103
|
-
name.
|
|
119
|
+
- \`providers\` (required for direct inference): provider configurations keyed
|
|
120
|
+
by built-in provider name. Omit it when \`amarsia.inference\` is true.
|
|
104
121
|
- \`agents\` (required): runtime property names mapped to folders relative to
|
|
105
122
|
\`orcha/\`. At least one agent is required.
|
|
106
123
|
- \`amarsia.key\` (optional): project connection key generated in Amarsia.
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
124
|
+
By itself, it syncs structural project and agent metadata only. Enabling
|
|
125
|
+
inference sends model requests through Amarsia, and cloud storage syncs
|
|
126
|
+
session events. Provider credentials, environment values, and action source
|
|
127
|
+
are never included.
|
|
110
128
|
- \`amarsia.endpoint\` (optional): Amarsia API URL for project sync. Defaults to
|
|
111
129
|
\`AMARSIA_API_URL\` and then \`https://api.amarsia.com\`.
|
|
130
|
+
- \`amarsia.inference\` (optional): routes all model calls through Amarsia while
|
|
131
|
+
preserving the provider and model in each manifest. It reuses \`amarsia.key\`;
|
|
132
|
+
local provider credentials are not required.
|
|
112
133
|
- \`actions\` (optional): overrides the native local-action runtime or configures
|
|
113
134
|
its environment and sandbox settings.
|
|
114
135
|
- \`mcp.credentials\` (optional): runtime-only values used to resolve
|
|
115
136
|
\`\${NAME}\` placeholders in MCP headers or stdio environment values.
|
|
116
|
-
- \`storage.strategy\` (optional): \`"node-jsonl"\` (default)
|
|
117
|
-
|
|
137
|
+
- \`storage.strategy\` (optional): \`"node-jsonl"\` (default), \`"sqlite"\`, or
|
|
138
|
+
local-first \`"cloud"\`. Cloud requires \`amarsia.key\`, keeps a native local
|
|
139
|
+
cache, commits appends locally without cloud latency, and delivers durable
|
|
140
|
+
bounded outbox batches in the background. Await \`orcha.close()\` before
|
|
141
|
+
reinitializing; close reports any cloud tail that could not be flushed.
|
|
118
142
|
- \`storage.directory\` (optional): session directory relative to project
|
|
119
143
|
root; defaults to \`.orcha/sessions\`.
|
|
120
144
|
- \`root\` (optional): absolute or working-directory-relative project root;
|
|
@@ -364,7 +388,7 @@ environment. The complete pause/continue flow is:
|
|
|
364
388
|
}
|
|
365
389
|
5. Application executes every requested action.
|
|
366
390
|
6. Application calls agent.resume(sessionId, {
|
|
367
|
-
toolResults: [{ callId, output, isError? }]
|
|
391
|
+
toolResults: [{ callId, output, contents?, isError? }]
|
|
368
392
|
}).
|
|
369
393
|
7. Orcha validates every callId and output, stores the results, and continues
|
|
370
394
|
the same paused run from its prior model context.
|
|
@@ -404,9 +428,9 @@ const result = await execution.result;
|
|
|
404
428
|
const evaluations = await execution.evaluations;
|
|
405
429
|
\`\`\`
|
|
406
430
|
|
|
407
|
-
\`run()\`
|
|
431
|
+
\`run()\` and conversational \`resume()\` accept either a direct \`AgentContentInput\` (a string, a single content item, or an array of content items) or an object with \`content\` and optional fields:
|
|
408
432
|
|
|
409
|
-
- \`content\` (required): a non-empty string, one content item, or an array.
|
|
433
|
+
- \`content\` (required when passing an options object): a non-empty string, one content item, or an array.
|
|
410
434
|
Use \`{ filePath: "./document.pdf" }\` for a local file or
|
|
411
435
|
\`{ url: "https://example.com/document.pdf" }\` for a remote file.
|
|
412
436
|
\`mimeType\` is optional when it can be inferred from the extension. Durable
|
|
@@ -436,7 +460,11 @@ client actions in the application and submit every result:
|
|
|
436
460
|
\`\`\`ts
|
|
437
461
|
orcha.supportBot.resume(sessionId, {
|
|
438
462
|
toolResults: [
|
|
439
|
-
{
|
|
463
|
+
{
|
|
464
|
+
callId: "call_123",
|
|
465
|
+
output: { approved: true },
|
|
466
|
+
contents: [{ filePath: "./signed-approval.pdf" }], // optional media/content items
|
|
467
|
+
}
|
|
440
468
|
],
|
|
441
469
|
});
|
|
442
470
|
\`\`\`
|
|
@@ -544,6 +572,11 @@ The local action signature is
|
|
|
544
572
|
\`sessionId\`, immutable session \`metadata\`, a stable \`idempotencyKey\`,
|
|
545
573
|
allowlisted \`env\`, guarded \`fetch\`, and prefixed \`log\`.
|
|
546
574
|
|
|
575
|
+
Actions can also return media or multimodal content back to the model alongside
|
|
576
|
+
their structured JSON output by returning \`{ output: jsonOutput, contents: [...] }\`.
|
|
577
|
+
\`contents\` accepts any \`AgentContentInput\` (such as \`{ filePath }\`,
|
|
578
|
+
\`{ url }\`, or content items). \`outputSchema\` validates \`output\` directly.
|
|
579
|
+
|
|
547
580
|
## Skills
|
|
548
581
|
|
|
549
582
|
Skills are lazy-loaded procedural instructions. Direct child folders under
|
|
@@ -27,6 +27,7 @@ var import_promises = require("fs/promises");
|
|
|
27
27
|
var import_path = require("path");
|
|
28
28
|
var import_url = require("url");
|
|
29
29
|
var import_esbuild = require("esbuild");
|
|
30
|
+
var import_env = require("../env.js");
|
|
30
31
|
var import_orcha = require("../orcha.js");
|
|
31
32
|
var import_compiler_hook = require("../runtime/compiler-hook.js");
|
|
32
33
|
var import_compile = require("./compile.js");
|
|
@@ -35,6 +36,7 @@ var import_dependencies = require("../providers/dependencies.js");
|
|
|
35
36
|
let registryImportSequence = 0;
|
|
36
37
|
async function buildProject(options = {}) {
|
|
37
38
|
const projectRoot = (0, import_path.resolve)(options.projectRoot ?? process.cwd());
|
|
39
|
+
(0, import_env.loadProjectEnvironment)(projectRoot);
|
|
38
40
|
const target = options.target ?? "node";
|
|
39
41
|
(0, import_compiler_hook.setRegistryCompiler)(
|
|
40
42
|
(configuration, compilerProjectRoot) => (0, import_compile.compileRegistry)(
|
|
@@ -62,8 +64,14 @@ async function buildProject(options = {}) {
|
|
|
62
64
|
projectRoot,
|
|
63
65
|
".orcha/dist/manifest.json"
|
|
64
66
|
);
|
|
67
|
+
if (import_orcha.orcha.isInitialized()) {
|
|
68
|
+
throw new Error(
|
|
69
|
+
"Cannot compile an Orcha project while the shared development runtime is initialized. Await orcha.close() first."
|
|
70
|
+
);
|
|
71
|
+
}
|
|
65
72
|
await (0, import_promises.mkdir)(temporaryDirectory, { recursive: true });
|
|
66
73
|
await (0, import_promises.mkdir)((0, import_path.dirname)(outputPath), { recursive: true });
|
|
74
|
+
let compilationRuntimeCreated = false;
|
|
67
75
|
try {
|
|
68
76
|
await (0, import_esbuild.build)({
|
|
69
77
|
entryPoints: [registryPath],
|
|
@@ -79,9 +87,12 @@ async function buildProject(options = {}) {
|
|
|
79
87
|
const previousProjectRoot = process.env.ORCHA_PROJECT_ROOT;
|
|
80
88
|
process.env.ORCHA_COMPILING = "1";
|
|
81
89
|
process.env.ORCHA_PROJECT_ROOT = projectRoot;
|
|
90
|
+
(0, import_compiler_hook.setRegistryCompilationActive)(true);
|
|
82
91
|
try {
|
|
83
92
|
await import(`${(0, import_url.pathToFileURL)(registryBundlePath).href}?build=${Date.now()}-${registryImportSequence++}`);
|
|
84
93
|
} finally {
|
|
94
|
+
compilationRuntimeCreated = import_orcha.orcha.isInitialized();
|
|
95
|
+
(0, import_compiler_hook.setRegistryCompilationActive)(false);
|
|
85
96
|
if (previousCompiling === void 0) {
|
|
86
97
|
delete process.env.ORCHA_COMPILING;
|
|
87
98
|
} else {
|
|
@@ -94,6 +105,10 @@ async function buildProject(options = {}) {
|
|
|
94
105
|
}
|
|
95
106
|
}
|
|
96
107
|
const compiledBundle = import_orcha.orcha.getCompiledBundle();
|
|
108
|
+
const inferenceEnabled = import_orcha.orcha.isInferenceEnabled();
|
|
109
|
+
const cloudStorageEnabled = import_orcha.orcha.isCloudStorageEnabled();
|
|
110
|
+
await import_orcha.orcha.close();
|
|
111
|
+
compilationRuntimeCreated = false;
|
|
97
112
|
if (target !== "node" && compiledBundle.actionRuntime === "sandbox") {
|
|
98
113
|
throw new Error(
|
|
99
114
|
`The ${target} target supports precompiled native actions only. Set actions.runtime to "native".`
|
|
@@ -118,7 +133,7 @@ async function buildProject(options = {}) {
|
|
|
118
133
|
const sandboxRuntimeSource = compiledBundle.actionRuntime === "sandbox" ? createSandboxRuntimeSource() : "";
|
|
119
134
|
const platformActionRuntimeSource = createPlatformActionRuntimeSource(target);
|
|
120
135
|
const nodeStorageExports = target === "node" ? createNodeStorageExports() : "";
|
|
121
|
-
const includedProviders = [
|
|
136
|
+
const includedProviders = inferenceEnabled ? [] : [
|
|
122
137
|
...new Set(
|
|
123
138
|
allCompiledAgents(compiledBundle.agents).flatMap(
|
|
124
139
|
(agent) => [
|
|
@@ -129,13 +144,18 @@ async function buildProject(options = {}) {
|
|
|
129
144
|
)
|
|
130
145
|
];
|
|
131
146
|
const providerRuntimeSource = createProviderRuntimeSource(includedProviders, target);
|
|
132
|
-
|
|
147
|
+
if (!inferenceEnabled) {
|
|
148
|
+
await (0, import_dependencies.ensureProviderDependencies)(includedProviders, projectRoot);
|
|
149
|
+
}
|
|
133
150
|
const hasMcp = allCompiledAgents(compiledBundle.agents).some(
|
|
134
151
|
(agent) => Object.keys(agent.mcpServers ?? {}).length > 0
|
|
135
152
|
);
|
|
136
153
|
await (0, import_dependencies.ensureCompiledMcpDependencies)(compiledBundle, projectRoot);
|
|
137
154
|
const mcpRuntimeSource = hasMcp ? createMcpRuntimeSource(target) : "";
|
|
138
|
-
const
|
|
155
|
+
const optionalFeatureRuntimeSource = createOptionalFeatureRuntimeSource({
|
|
156
|
+
inference: inferenceEnabled,
|
|
157
|
+
cloudStorage: cloudStorageEnabled
|
|
158
|
+
});
|
|
139
159
|
await (0, import_promises.writeFile)(
|
|
140
160
|
productionEntryPath,
|
|
141
161
|
[
|
|
@@ -147,6 +167,7 @@ async function buildProject(options = {}) {
|
|
|
147
167
|
providerRuntimeSource,
|
|
148
168
|
platformActionRuntimeSource,
|
|
149
169
|
mcpRuntimeSource,
|
|
170
|
+
optionalFeatureRuntimeSource,
|
|
150
171
|
sandboxRuntimeSource,
|
|
151
172
|
`const compiledBundle = ${JSON.stringify(compiledBundle)};`,
|
|
152
173
|
"const generateProviderResponse = createProviderResponseGenerator(includedProviders);",
|
|
@@ -170,7 +191,7 @@ async function buildProject(options = {}) {
|
|
|
170
191
|
compiledBundle.actionRuntime === "sandbox" ? "1" : "0"
|
|
171
192
|
)
|
|
172
193
|
},
|
|
173
|
-
...
|
|
194
|
+
...target === "node" ? {
|
|
174
195
|
banner: {
|
|
175
196
|
js: [
|
|
176
197
|
'import { createRequire as __orchaCreateRequire } from "module";',
|
|
@@ -193,6 +214,10 @@ async function buildProject(options = {}) {
|
|
|
193
214
|
);
|
|
194
215
|
return outputPath;
|
|
195
216
|
} finally {
|
|
217
|
+
(0, import_compiler_hook.setRegistryCompilationActive)(false);
|
|
218
|
+
if (compilationRuntimeCreated && import_orcha.orcha.isInitialized()) {
|
|
219
|
+
await import_orcha.orcha.close();
|
|
220
|
+
}
|
|
196
221
|
await (0, import_promises.rm)(temporaryDirectory, {
|
|
197
222
|
recursive: true,
|
|
198
223
|
force: true
|
|
@@ -263,6 +288,45 @@ function createMcpRuntimeSource(target) {
|
|
|
263
288
|
""
|
|
264
289
|
].join("\n");
|
|
265
290
|
}
|
|
291
|
+
function createOptionalFeatureRuntimeSource(options) {
|
|
292
|
+
if (!options.inference && !options.cloudStorage) {
|
|
293
|
+
return "";
|
|
294
|
+
}
|
|
295
|
+
const sourceRoot = (0, import_path.dirname)((0, import_url.fileURLToPath)(__orchaImportMetaUrl));
|
|
296
|
+
const hooksPath = (0, import_path.resolve)(
|
|
297
|
+
sourceRoot,
|
|
298
|
+
"../runtime/optional-features.js"
|
|
299
|
+
).replaceAll("\\", "/");
|
|
300
|
+
const imports = [];
|
|
301
|
+
const registrations = [];
|
|
302
|
+
if (options.inference) {
|
|
303
|
+
const inferencePath = (0, import_path.resolve)(
|
|
304
|
+
sourceRoot,
|
|
305
|
+
"../amarsia/inference.js"
|
|
306
|
+
).replaceAll("\\", "/");
|
|
307
|
+
imports.push(
|
|
308
|
+
`import { createAmarsiaInferenceGenerator } from ${JSON.stringify(inferencePath)};`,
|
|
309
|
+
`import { setInferenceGeneratorFactory } from ${JSON.stringify(hooksPath)};`
|
|
310
|
+
);
|
|
311
|
+
registrations.push(
|
|
312
|
+
"setInferenceGeneratorFactory(createAmarsiaInferenceGenerator);"
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
if (options.cloudStorage) {
|
|
316
|
+
const cloudPath = (0, import_path.resolve)(
|
|
317
|
+
sourceRoot,
|
|
318
|
+
"../storage/cloud.js"
|
|
319
|
+
).replaceAll("\\", "/");
|
|
320
|
+
imports.push(
|
|
321
|
+
`import { CloudSessionStore } from ${JSON.stringify(cloudPath)};`,
|
|
322
|
+
`import { setCloudSessionStoreFactory } from ${JSON.stringify(hooksPath)};`
|
|
323
|
+
);
|
|
324
|
+
registrations.push(
|
|
325
|
+
"setCloudSessionStoreFactory((options) => new CloudSessionStore(options));"
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
return [...imports, ...registrations, ""].join("\n");
|
|
329
|
+
}
|
|
266
330
|
function createNodeStorageExports() {
|
|
267
331
|
const sourceRoot = (0, import_path.dirname)((0, import_url.fileURLToPath)(__orchaImportMetaUrl));
|
|
268
332
|
const jsonlPath = (0, import_path.resolve)(
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict"; const __orchaImportMetaUrl = require("url").pathToFileURL(__filename).href;
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var node_esm_banner_exports = {};
|
|
21
|
+
__export(node_esm_banner_exports, {
|
|
22
|
+
nodeEsmRequireBanner: () => nodeEsmRequireBanner
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(node_esm_banner_exports);
|
|
25
|
+
const nodeEsmRequireBanner = [
|
|
26
|
+
'import { createRequire as __orchaCreateRequire } from "module";',
|
|
27
|
+
"const require = __orchaCreateRequire(import.meta.url);"
|
|
28
|
+
].join("\n");
|
|
29
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
30
|
+
0 && (module.exports = {
|
|
31
|
+
nodeEsmRequireBanner
|
|
32
|
+
});
|