orchajs 0.23.12 → 0.25.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 +122 -11
- package/dist/amarsia/analytics.js +12 -0
- package/dist/amarsia/inference.d.ts +6 -0
- package/dist/amarsia/inference.js +337 -0
- package/dist/browser.js +5 -0
- package/dist/cli-agents-template.d.ts +1 -1
- package/dist/cli-agents-template.js +70 -8
- package/dist/compiler/build-project.js +78 -15
- package/dist/compiler/compile.js +131 -10
- package/dist/compiler/node-esm-banner.d.ts +2 -0
- package/dist/compiler/node-esm-banner.js +5 -0
- package/dist/orcha.js +11 -0
- package/dist/project-loader.js +6 -3
- package/dist/providers/dependencies.d.ts +1 -0
- package/dist/providers/dependencies.js +30 -4
- package/dist/providers/types.d.ts +4 -0
- package/dist/react-native.js +5 -0
- package/dist/runtime/agent.d.ts +2 -0
- package/dist/runtime/agent.js +262 -21
- 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 +110 -7
- package/dist/runtime/mcp/client.d.ts +31 -0
- package/dist/runtime/mcp/client.js +106 -0
- package/dist/runtime/mcp/http-client.d.ts +6 -0
- package/dist/runtime/mcp/http-client.js +87 -0
- package/dist/runtime/mcp/node-client.d.ts +3 -0
- package/dist/runtime/mcp/node-client.js +60 -0
- 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-entry.d.ts +1 -1
- 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 +1 -0
- package/dist/types.d.ts +54 -3
- package/dist-cjs/amarsia/analytics.js +12 -0
- package/dist-cjs/amarsia/inference.js +335 -0
- package/dist-cjs/browser.js +5 -0
- package/dist-cjs/cli-agents-template.js +70 -8
- package/dist-cjs/compiler/build-project.js +92 -4
- package/dist-cjs/compiler/compile.js +148 -11
- package/dist-cjs/compiler/node-esm-banner.js +32 -0
- package/dist-cjs/orcha.js +11 -0
- package/dist-cjs/project-loader.js +8 -2
- package/dist-cjs/providers/dependencies.js +37 -5
- package/dist-cjs/react-native.js +5 -0
- package/dist-cjs/runtime/agent.js +327 -37
- package/dist-cjs/runtime/compiler-hook.js +11 -0
- package/dist-cjs/runtime/create-orcha.js +118 -7
- package/dist-cjs/runtime/mcp/client.js +157 -0
- package/dist-cjs/runtime/mcp/http-client.js +134 -0
- package/dist-cjs/runtime/mcp/node-client.js +95 -0
- 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/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 +5 -1
- package/package.json +13 -3
package/README.md
CHANGED
|
@@ -319,6 +319,104 @@ npm install quickjs-emscripten-core @jitl/quickjs-singlefile-cjs-release-sync
|
|
|
319
319
|
|
|
320
320
|
---
|
|
321
321
|
|
|
322
|
+
## Amarsia inference
|
|
323
|
+
|
|
324
|
+
Set `amarsia.inference: true` to route every model round through Amarsia:
|
|
325
|
+
|
|
326
|
+
```js
|
|
327
|
+
orcha.init({
|
|
328
|
+
amarsia: {
|
|
329
|
+
key: process.env.AMARSIA_PROJECT_KEY,
|
|
330
|
+
inference: true,
|
|
331
|
+
// endpoint: "https://api.amarsia.com",
|
|
332
|
+
},
|
|
333
|
+
agents: {
|
|
334
|
+
coordinator: "./coordinator",
|
|
335
|
+
},
|
|
336
|
+
});
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
The provider and model remain in each agent or evaluation `index.json`.
|
|
340
|
+
Amarsia inference supports `openai`, `anthropic`, `googlegenai`, and
|
|
341
|
+
`deepseek`. Infrastructure-specific `bedrock` and `vertexai` providers remain
|
|
342
|
+
available only for direct inference. Inference uses
|
|
343
|
+
`POST /v3/model/v1/responses`, requires no local provider keys, and covers
|
|
344
|
+
normal runs, streaming, tool loops, MCP, subagents, tests, evaluations, replay,
|
|
345
|
+
structured output, and abort signals. Amarsia uses the workspace owner's
|
|
346
|
+
configured BYOK provider key first and falls back to managed credit billing.
|
|
347
|
+
When `inference` is absent or false, direct-provider behavior is unchanged.
|
|
348
|
+
|
|
349
|
+
---
|
|
350
|
+
|
|
351
|
+
## MCP connections
|
|
352
|
+
|
|
353
|
+
Assign named MCP connections directly to agents and subagents:
|
|
354
|
+
|
|
355
|
+
```js
|
|
356
|
+
orcha.init({
|
|
357
|
+
mcp: {
|
|
358
|
+
credentials: {
|
|
359
|
+
GITHUB_MCP_TOKEN: process.env.GITHUB_MCP_TOKEN,
|
|
360
|
+
},
|
|
361
|
+
},
|
|
362
|
+
agents: {
|
|
363
|
+
coordinator: {
|
|
364
|
+
path: "./coordinator",
|
|
365
|
+
mcp: {
|
|
366
|
+
github: "./mcp/github",
|
|
367
|
+
},
|
|
368
|
+
subagents: {
|
|
369
|
+
researcher: {
|
|
370
|
+
path: "./researcher",
|
|
371
|
+
mcp: {
|
|
372
|
+
docs: "./mcp/docs",
|
|
373
|
+
},
|
|
374
|
+
},
|
|
375
|
+
},
|
|
376
|
+
},
|
|
377
|
+
},
|
|
378
|
+
});
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
Each path contains an `index.json`. Streamable HTTP works in Node.js,
|
|
382
|
+
browsers, Expo, and React Native:
|
|
383
|
+
|
|
384
|
+
```json
|
|
385
|
+
{
|
|
386
|
+
"transport": "streamable-http",
|
|
387
|
+
"url": "https://mcp.example.com",
|
|
388
|
+
"headers": {
|
|
389
|
+
"Authorization": "Bearer ${GITHUB_MCP_TOKEN}"
|
|
390
|
+
},
|
|
391
|
+
"timeoutMs": 30000
|
|
392
|
+
}
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
Node.js also supports local stdio servers:
|
|
396
|
+
|
|
397
|
+
```json
|
|
398
|
+
{
|
|
399
|
+
"transport": "stdio",
|
|
400
|
+
"command": "npx",
|
|
401
|
+
"args": ["-y", "@example/mcp-server"],
|
|
402
|
+
"env": {
|
|
403
|
+
"API_TOKEN": "${EXAMPLE_MCP_TOKEN}"
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
Orcha discovers every server tool lazily and exposes it as
|
|
409
|
+
`<connection>__<tool>`, for example `github__get_issue`. Tool calls and
|
|
410
|
+
results use the durable action event timeline. Credential placeholders resolve
|
|
411
|
+
from `orcha.init().mcp.credentials`; Node.js additionally resolves them from
|
|
412
|
+
`process.env`. Never put secret values directly in MCP JSON files.
|
|
413
|
+
|
|
414
|
+
The optional MCP SDK is installed automatically when an agent uses MCP.
|
|
415
|
+
Projects without MCP do not install or bundle it. Browser and React Native
|
|
416
|
+
targets reject stdio connections during compilation.
|
|
417
|
+
|
|
418
|
+
---
|
|
419
|
+
|
|
322
420
|
## Quick start
|
|
323
421
|
|
|
324
422
|
```bash
|
|
@@ -329,23 +427,15 @@ This installs `orchajs` and its development tooling in the current project,
|
|
|
329
427
|
then creates the starter agent files.
|
|
330
428
|
|
|
331
429
|
The default installation includes the compiler, CLI, native actions, JSONL
|
|
332
|
-
storage, and fetch-based Anthropic, DeepSeek, and OpenAI adapters.
|
|
333
|
-
|
|
430
|
+
storage, and fetch-based Anthropic, DeepSeek, and OpenAI adapters. Orcha
|
|
431
|
+
automatically installs the Bedrock, Google, or MCP SDK only when a compiled
|
|
432
|
+
agent selects that integration. Other optional integrations remain explicit:
|
|
334
433
|
|
|
335
434
|
```bash
|
|
336
|
-
# Amazon Bedrock
|
|
337
|
-
npm install @aws-sdk/client-bedrock-runtime
|
|
338
|
-
|
|
339
|
-
# Google GenAI or Vertex AI (Node.js 20+)
|
|
340
|
-
npm install @google/genai
|
|
341
|
-
|
|
342
435
|
# SQLite session storage
|
|
343
436
|
npm install better-sqlite3
|
|
344
437
|
```
|
|
345
438
|
|
|
346
|
-
Selecting an integration without its package produces an error that names the
|
|
347
|
-
dependency to install.
|
|
348
|
-
|
|
349
439
|
Install the OrchaJS skill to give supported coding agents version-aware Orcha
|
|
350
440
|
workflows:
|
|
351
441
|
|
|
@@ -390,6 +480,27 @@ JSONL by default at
|
|
|
390
480
|
shared `.orcha/sessions/orcha-sqlite.db` store, set
|
|
391
481
|
`storage: { strategy: "sqlite" }` in `orcha.init()` and install
|
|
392
482
|
`better-sqlite3`.
|
|
483
|
+
Use `storage: { strategy: "cloud" }` with `amarsia.key` for local-first cloud
|
|
484
|
+
storage. Node.js keeps JSONL locally, browsers use IndexedDB, and React
|
|
485
|
+
Native/Expo use AsyncStorage. Existing `list`, `get`, `history`, `events`, and
|
|
486
|
+
`resume` calls remain unchanged. Every append commits locally first and returns
|
|
487
|
+
without waiting for cloud latency. Delivery continues in bounded background
|
|
488
|
+
batches and is recovered from the durable local acknowledgement after restart.
|
|
489
|
+
Warm local reads return immediately and safely refresh an already-synchronized
|
|
490
|
+
tail in the background; unsent local tails are never overwritten. Permanent
|
|
491
|
+
delivery failures are parked to avoid retry loops and are retried by a new
|
|
492
|
+
append or an explicit `orcha.flushCloudStorage()`.
|
|
493
|
+
|
|
494
|
+
`flushCloudStorage({ timeoutMs })` resolves only when every outbox is empty and
|
|
495
|
+
throws if data remains pending. `orcha.close({ timeoutMs })` flushes, aborts and
|
|
496
|
+
joins in-flight cloud requests, and likewise throws on an incomplete flush.
|
|
497
|
+
Close establishes a storage lifecycle barrier: appends already writing locally
|
|
498
|
+
are included in the final outbox scan, while later appends are rejected.
|
|
499
|
+
Local events remain durable in either case. JSONL-backed cloud stores maintain
|
|
500
|
+
recoverable tail indexes so bounded delivery batches do not repeatedly parse
|
|
501
|
+
the full session history. Immutable-prefix conflicts preserve both sides and
|
|
502
|
+
surface as `storage_error`. Call and await `orcha.close()` before calling
|
|
503
|
+
synchronous `orcha.init()` again; overlapping initialized runtimes are rejected.
|
|
393
504
|
Client-action pause/resume, compiled local actions, and cumulative output
|
|
394
505
|
streaming are supported.
|
|
395
506
|
`run()` always starts a new session; `resume()` continues one with either a new
|
|
@@ -195,8 +195,20 @@ export function withAmarsiaTelemetry(options) {
|
|
|
195
195
|
const projections = new Map();
|
|
196
196
|
const pending = new Map();
|
|
197
197
|
return {
|
|
198
|
+
get cloudStoragePendingCount() {
|
|
199
|
+
return store.cloudStoragePendingCount;
|
|
200
|
+
},
|
|
201
|
+
get cloudStorageLastError() {
|
|
202
|
+
return store.cloudStorageLastError;
|
|
203
|
+
},
|
|
198
204
|
read: (sessionId) => store.read(sessionId),
|
|
205
|
+
readAfterSequence: store.readAfterSequence?.bind(store),
|
|
199
206
|
listSessionIds: () => store.listSessionIds(),
|
|
207
|
+
getCloudAcknowledgement: store.getCloudAcknowledgement?.bind(store),
|
|
208
|
+
setCloudAcknowledgement: store.setCloudAcknowledgement?.bind(store),
|
|
209
|
+
refreshCloudSession: store.refreshCloudSession?.bind(store),
|
|
210
|
+
flushCloudStorage: store.flushCloudStorage?.bind(store),
|
|
211
|
+
closeCloudStorage: store.closeCloudStorage?.bind(store),
|
|
200
212
|
describeLocation: store.describeLocation?.bind(store),
|
|
201
213
|
subscribe: store.subscribe?.bind(store),
|
|
202
214
|
subscribeAll: store.subscribeAll?.bind(store),
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ProviderResponseGenerator } from "../providers/types.js";
|
|
2
|
+
export interface AmarsiaInferenceConfiguration {
|
|
3
|
+
key: string;
|
|
4
|
+
endpoint?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function createAmarsiaInferenceGenerator(configuration: AmarsiaInferenceConfiguration): ProviderResponseGenerator;
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
import { OrchaError } from "../errors.js";
|
|
2
|
+
import { readLocalFileContent } from "../file-content.js";
|
|
3
|
+
import { readServerSentEvents } from "../providers/sse.js";
|
|
4
|
+
import { parseStructuredOutput } from "../runtime/structured-output.js";
|
|
5
|
+
import { resolveAmarsiaEndpoint } from "./endpoint.js";
|
|
6
|
+
const SUPPORTED_AMARSIA_PROVIDERS = new Set([
|
|
7
|
+
"openai",
|
|
8
|
+
"anthropic",
|
|
9
|
+
"googlegenai",
|
|
10
|
+
"vertexai",
|
|
11
|
+
"deepseek",
|
|
12
|
+
]);
|
|
13
|
+
export function createAmarsiaInferenceGenerator(configuration) {
|
|
14
|
+
const endpoint = resolveAmarsiaEndpoint(configuration.endpoint);
|
|
15
|
+
return (provider, _providerConfiguration, request) => generateThroughAmarsia(endpoint, configuration.key, provider, request);
|
|
16
|
+
}
|
|
17
|
+
async function generateThroughAmarsia(endpoint, key, provider, request) {
|
|
18
|
+
if (!key.trim()) {
|
|
19
|
+
throw new Error("Amarsia inference requires a non-empty amarsia.key.");
|
|
20
|
+
}
|
|
21
|
+
if (!SUPPORTED_AMARSIA_PROVIDERS.has(provider)) {
|
|
22
|
+
throw new OrchaError("unsupported_provider_capability", `Amarsia inference supports only OpenAI, Anthropic, Google GenAI, Vertex AI, and DeepSeek; received "${provider}".`);
|
|
23
|
+
}
|
|
24
|
+
if (request.outputType === "image" || request.outputType === "audio") {
|
|
25
|
+
throw new OrchaError("unsupported_provider_capability", `Amarsia Responses does not support "${request.outputType}" as a direct agent output.`);
|
|
26
|
+
}
|
|
27
|
+
const gatewayProvider = provider === "vertexai" ? "googlegenai" : provider;
|
|
28
|
+
const body = {
|
|
29
|
+
provider: gatewayProvider,
|
|
30
|
+
model: request.model,
|
|
31
|
+
instructions: request.systemPrompt,
|
|
32
|
+
input: await toResponsesInput(request.messages),
|
|
33
|
+
...(provider === "vertexai"
|
|
34
|
+
? { region: "global" }
|
|
35
|
+
: request.region && request.region !== "provider_managed"
|
|
36
|
+
? { region: request.region }
|
|
37
|
+
: {}),
|
|
38
|
+
...(request.maxTokens
|
|
39
|
+
? { max_output_tokens: request.maxTokens }
|
|
40
|
+
: {}),
|
|
41
|
+
...(request.publishOutput ? { stream: true } : {}),
|
|
42
|
+
};
|
|
43
|
+
if (request.tools.length > 0) {
|
|
44
|
+
body.tools = request.tools.map((tool) => ({
|
|
45
|
+
type: "function",
|
|
46
|
+
name: tool.name,
|
|
47
|
+
description: tool.description,
|
|
48
|
+
parameters: tool.parameters,
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
51
|
+
if (request.outputType === "json") {
|
|
52
|
+
body.text = {
|
|
53
|
+
format: {
|
|
54
|
+
type: "json_schema",
|
|
55
|
+
name: "orcha_output",
|
|
56
|
+
schema: request.outputSchema ?? {},
|
|
57
|
+
strict: true,
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
body.reasoning = {
|
|
62
|
+
summary: "auto",
|
|
63
|
+
...(request.reasoningLevel ? { effort: request.reasoningLevel } : {}),
|
|
64
|
+
};
|
|
65
|
+
body.include = ["reasoning.encrypted_content"];
|
|
66
|
+
if (request.trace) {
|
|
67
|
+
body.metadata = {
|
|
68
|
+
agent_external_id: request.trace.agentExternalId,
|
|
69
|
+
session_id: request.trace.sessionId,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
const response = await fetch(`${endpoint}/v3/model/v1/responses`, {
|
|
73
|
+
method: "POST",
|
|
74
|
+
headers: {
|
|
75
|
+
"content-type": "application/json",
|
|
76
|
+
authorization: `Bearer ${key}`,
|
|
77
|
+
},
|
|
78
|
+
body: JSON.stringify(body),
|
|
79
|
+
signal: request.signal,
|
|
80
|
+
});
|
|
81
|
+
if (!response.ok) {
|
|
82
|
+
const message = await responseError(response);
|
|
83
|
+
throw new Error(message ??
|
|
84
|
+
`Amarsia inference request failed with HTTP ${response.status}.`);
|
|
85
|
+
}
|
|
86
|
+
const responseBody = response.headers
|
|
87
|
+
.get("content-type")
|
|
88
|
+
?.includes("text/event-stream")
|
|
89
|
+
? await readGatewayStream(response, request.publishOutput)
|
|
90
|
+
: (await response.json());
|
|
91
|
+
return toProviderResponse(request, responseBody);
|
|
92
|
+
}
|
|
93
|
+
async function toResponsesInput(messages) {
|
|
94
|
+
const input = [];
|
|
95
|
+
for (const message of messages) {
|
|
96
|
+
if (message.role === "user") {
|
|
97
|
+
input.push({
|
|
98
|
+
role: "user",
|
|
99
|
+
content: typeof message.content === "string"
|
|
100
|
+
? message.content
|
|
101
|
+
: await Promise.all(message.content.map(toResponsesUserContent)),
|
|
102
|
+
});
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (message.role === "tool") {
|
|
106
|
+
for (const result of message.results) {
|
|
107
|
+
input.push({
|
|
108
|
+
type: "function_call_output",
|
|
109
|
+
call_id: result.callId,
|
|
110
|
+
output: JSON.stringify(result.output ?? null),
|
|
111
|
+
...(result.isError ? { is_error: true } : {}),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
for (const block of message.content) {
|
|
117
|
+
if (block.type === "text") {
|
|
118
|
+
input.push({
|
|
119
|
+
role: "assistant",
|
|
120
|
+
content: [{ type: "output_text", text: block.text }],
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
else if (block.type === "tool_call") {
|
|
124
|
+
input.push({
|
|
125
|
+
type: "function_call",
|
|
126
|
+
call_id: block.callId,
|
|
127
|
+
name: block.name,
|
|
128
|
+
arguments: JSON.stringify(block.arguments),
|
|
129
|
+
...(block.replay?.providerId
|
|
130
|
+
? { id: block.replay.providerId }
|
|
131
|
+
: {}),
|
|
132
|
+
...(block.replay?.opaqueData
|
|
133
|
+
? { encrypted_content: block.replay.opaqueData }
|
|
134
|
+
: {}),
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
input.push({
|
|
139
|
+
type: "reasoning",
|
|
140
|
+
...(block.replay?.providerId
|
|
141
|
+
? { id: block.replay.providerId }
|
|
142
|
+
: {}),
|
|
143
|
+
summary: block.text
|
|
144
|
+
? [{ type: "summary_text", text: block.text }]
|
|
145
|
+
: [],
|
|
146
|
+
...(block.replay?.opaqueData
|
|
147
|
+
? { encrypted_content: block.replay.opaqueData }
|
|
148
|
+
: {}),
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return input;
|
|
154
|
+
}
|
|
155
|
+
async function toResponsesUserContent(content) {
|
|
156
|
+
if (content.type === "text") {
|
|
157
|
+
return { type: "input_text", text: content.text };
|
|
158
|
+
}
|
|
159
|
+
if (content.type === "file") {
|
|
160
|
+
const file = await readLocalFileContent(content);
|
|
161
|
+
return content.mimeType.startsWith("image/")
|
|
162
|
+
? {
|
|
163
|
+
type: "input_image",
|
|
164
|
+
image_url: file.dataUri,
|
|
165
|
+
detail: "auto",
|
|
166
|
+
}
|
|
167
|
+
: {
|
|
168
|
+
type: "input_file",
|
|
169
|
+
file_data: file.dataUri,
|
|
170
|
+
filename: file.fileName,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
if (content.type === "image") {
|
|
174
|
+
return {
|
|
175
|
+
type: "input_image",
|
|
176
|
+
image_url: content.fileUri,
|
|
177
|
+
detail: "auto",
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
if (content.type === "url") {
|
|
181
|
+
return { type: "input_file", file_url: content.fileUri };
|
|
182
|
+
}
|
|
183
|
+
throw new OrchaError("unsupported_content_type", `Amarsia Responses does not support ${content.type} content from a URI.`);
|
|
184
|
+
}
|
|
185
|
+
async function readGatewayStream(response, publishOutput) {
|
|
186
|
+
let text = "";
|
|
187
|
+
let terminal;
|
|
188
|
+
for await (const event of readServerSentEvents(response)) {
|
|
189
|
+
if (event.type === "response.output_text.delta" &&
|
|
190
|
+
typeof event.delta === "string") {
|
|
191
|
+
text += event.delta;
|
|
192
|
+
publishOutput?.(text);
|
|
193
|
+
}
|
|
194
|
+
else if (event.type === "response.completed" ||
|
|
195
|
+
event.type === "response.incomplete") {
|
|
196
|
+
if (!isRecord(event.response)) {
|
|
197
|
+
throw new Error("Amarsia returned an invalid terminal stream event.");
|
|
198
|
+
}
|
|
199
|
+
terminal = event.response;
|
|
200
|
+
}
|
|
201
|
+
else if (event.type === "response.failed" ||
|
|
202
|
+
event.type === "error") {
|
|
203
|
+
const error = isRecord(event.error) ? event.error : event;
|
|
204
|
+
throw new Error(typeof error.message === "string"
|
|
205
|
+
? error.message
|
|
206
|
+
: "Amarsia inference streaming request failed.");
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (!terminal) {
|
|
210
|
+
throw new Error("Amarsia inference stream ended without a terminal response.");
|
|
211
|
+
}
|
|
212
|
+
return terminal;
|
|
213
|
+
}
|
|
214
|
+
function toProviderResponse(request, response) {
|
|
215
|
+
if (response.status === "failed") {
|
|
216
|
+
throw new Error(response.error?.message ?? "Amarsia failed to generate a response.");
|
|
217
|
+
}
|
|
218
|
+
if (response.status === "incomplete" &&
|
|
219
|
+
response.incomplete_details?.reason !== "max_output_tokens") {
|
|
220
|
+
throw new Error(`Amarsia returned an incomplete response: ${response.incomplete_details?.reason ?? "unknown reason"}.`);
|
|
221
|
+
}
|
|
222
|
+
const content = [];
|
|
223
|
+
let refused = false;
|
|
224
|
+
for (const item of response.output ?? []) {
|
|
225
|
+
if (item.type === "message") {
|
|
226
|
+
for (const part of item.content ?? []) {
|
|
227
|
+
if (part.type === "output_text" &&
|
|
228
|
+
typeof part.text === "string") {
|
|
229
|
+
content.push({ type: "text", text: part.text });
|
|
230
|
+
}
|
|
231
|
+
else if (part.type === "refusal" &&
|
|
232
|
+
typeof part.refusal === "string") {
|
|
233
|
+
refused = true;
|
|
234
|
+
content.push({ type: "text", text: part.refusal });
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
else if (item.type === "function_call" &&
|
|
239
|
+
typeof item.call_id === "string" &&
|
|
240
|
+
typeof item.name === "string") {
|
|
241
|
+
content.push({
|
|
242
|
+
type: "tool_call",
|
|
243
|
+
callId: item.call_id,
|
|
244
|
+
name: item.name,
|
|
245
|
+
arguments: parseArguments(item.arguments, item.name),
|
|
246
|
+
replay: replay(item),
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
else if (item.type === "reasoning") {
|
|
250
|
+
content.push({
|
|
251
|
+
type: "reasoning",
|
|
252
|
+
text: [...(item.content ?? []), ...(item.summary ?? [])]
|
|
253
|
+
.filter((part) => (part.type === "reasoning_text" ||
|
|
254
|
+
part.type === "summary_text") &&
|
|
255
|
+
typeof part.text === "string")
|
|
256
|
+
.map((part) => part.text)
|
|
257
|
+
.join(""),
|
|
258
|
+
replay: replay(item),
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
throw new Error(`Amarsia returned unsupported output item "${String(item.type)}".`);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
const toolCalls = content.filter((block) => block.type === "tool_call");
|
|
266
|
+
const textOutput = response.output_text ??
|
|
267
|
+
content
|
|
268
|
+
.filter((block) => block.type === "text")
|
|
269
|
+
.map((block) => block.text)
|
|
270
|
+
.join("");
|
|
271
|
+
const stopReason = normalizeStopReason(response, toolCalls, refused);
|
|
272
|
+
const output = request.outputType === "json" &&
|
|
273
|
+
toolCalls.length === 0 &&
|
|
274
|
+
stopReason !== "max_tokens"
|
|
275
|
+
? parseStructuredOutput(textOutput, request.outputSchema ?? {})
|
|
276
|
+
: textOutput;
|
|
277
|
+
if (textOutput) {
|
|
278
|
+
request.publishOutput?.(textOutput);
|
|
279
|
+
}
|
|
280
|
+
return {
|
|
281
|
+
output,
|
|
282
|
+
responseId: response.id,
|
|
283
|
+
stopReason,
|
|
284
|
+
content,
|
|
285
|
+
toolCalls,
|
|
286
|
+
usage: normalizeUsage(response.usage),
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
function replay(item) {
|
|
290
|
+
const value = {
|
|
291
|
+
...(typeof item.id === "string" ? { providerId: item.id } : {}),
|
|
292
|
+
...(typeof item.encrypted_content === "string"
|
|
293
|
+
? { opaqueData: item.encrypted_content }
|
|
294
|
+
: {}),
|
|
295
|
+
};
|
|
296
|
+
return Object.keys(value).length > 0 ? value : undefined;
|
|
297
|
+
}
|
|
298
|
+
function parseArguments(value, name) {
|
|
299
|
+
const parsed = typeof value === "string" ? JSON.parse(value) : value;
|
|
300
|
+
if (!isRecord(parsed)) {
|
|
301
|
+
throw new Error(`Amarsia returned non-object arguments for tool "${name}".`);
|
|
302
|
+
}
|
|
303
|
+
return parsed;
|
|
304
|
+
}
|
|
305
|
+
function normalizeStopReason(response, toolCalls, refused) {
|
|
306
|
+
if (refused)
|
|
307
|
+
return "content_filter";
|
|
308
|
+
if (toolCalls.length > 0)
|
|
309
|
+
return "tool_call";
|
|
310
|
+
if (response.status === "incomplete") {
|
|
311
|
+
return response.incomplete_details?.reason === "max_output_tokens"
|
|
312
|
+
? "max_tokens"
|
|
313
|
+
: "unknown";
|
|
314
|
+
}
|
|
315
|
+
return response.status === "completed" ? "end_turn" : "unknown";
|
|
316
|
+
}
|
|
317
|
+
function normalizeUsage(usage) {
|
|
318
|
+
return {
|
|
319
|
+
inputTokens: usage?.input_tokens ?? 0,
|
|
320
|
+
outputTokens: usage?.output_tokens ?? 0,
|
|
321
|
+
reasoningTokens: usage?.output_tokens_details?.reasoning_tokens ?? null,
|
|
322
|
+
cacheReadTokens: usage?.input_tokens_details?.cached_tokens ?? 0,
|
|
323
|
+
cacheWriteTokens: usage?.cache_write_tokens ?? 0,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
async function responseError(response) {
|
|
327
|
+
try {
|
|
328
|
+
const body = (await response.json());
|
|
329
|
+
return body.error?.message ?? body.message;
|
|
330
|
+
}
|
|
331
|
+
catch {
|
|
332
|
+
return undefined;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
function isRecord(value) {
|
|
336
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
337
|
+
}
|
package/dist/browser.js
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
+
import { createAmarsiaInferenceGenerator } from "./amarsia/inference.js";
|
|
1
2
|
import { generateBrowserProviderResponse } from "./providers/browser.js";
|
|
2
3
|
import { setNativeRuntimeLoader } from "./runtime/actions/execute.js";
|
|
3
4
|
import { createOrcha } from "./runtime/create-orcha.js";
|
|
5
|
+
import { setCloudSessionStoreFactory, setInferenceGeneratorFactory, } from "./runtime/optional-features.js";
|
|
4
6
|
import { browserRuntimePlatform } from "./runtime/platform-browser.js";
|
|
7
|
+
import { CloudSessionStore } from "./storage/cloud.js";
|
|
5
8
|
setNativeRuntimeLoader(async () => {
|
|
6
9
|
const modulePath = "./runtime/actions/browser-native.js";
|
|
7
10
|
return import(modulePath);
|
|
8
11
|
});
|
|
12
|
+
setInferenceGeneratorFactory(createAmarsiaInferenceGenerator);
|
|
13
|
+
setCloudSessionStoreFactory((options) => new CloudSessionStore(options));
|
|
9
14
|
export const orcha = createOrcha(() => {
|
|
10
15
|
throw new Error("The browser runtime requires a precompiled Orcha bundle. Configure the Orcha integration for your application bundler.");
|
|
11
16
|
}, generateBrowserProviderResponse, browserRuntimePlatform);
|