orchajs 0.24.0 → 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.
Files changed (60) hide show
  1. package/README.md +50 -3
  2. package/dist/amarsia/analytics.js +12 -0
  3. package/dist/amarsia/inference.d.ts +6 -0
  4. package/dist/amarsia/inference.js +337 -0
  5. package/dist/browser.js +5 -0
  6. package/dist/cli-agents-template.d.ts +1 -1
  7. package/dist/cli-agents-template.js +31 -7
  8. package/dist/compiler/build-project.js +57 -14
  9. package/dist/compiler/node-esm-banner.d.ts +2 -0
  10. package/dist/compiler/node-esm-banner.js +5 -0
  11. package/dist/orcha.js +5 -0
  12. package/dist/project-loader.js +4 -2
  13. package/dist/providers/types.d.ts +4 -0
  14. package/dist/react-native.js +5 -0
  15. package/dist/runtime/agent.js +103 -14
  16. package/dist/runtime/compiler-hook.d.ts +4 -0
  17. package/dist/runtime/compiler-hook.js +9 -0
  18. package/dist/runtime/create-orcha.d.ts +19 -0
  19. package/dist/runtime/create-orcha.js +109 -12
  20. package/dist/runtime/optional-features.d.ts +19 -0
  21. package/dist/runtime/optional-features.js +22 -0
  22. package/dist/runtime/platform-browser.js +16 -3
  23. package/dist/runtime/platform-node.js +16 -2
  24. package/dist/runtime/platform-react-native.js +16 -3
  25. package/dist/storage/browser-indexeddb.d.ts +3 -0
  26. package/dist/storage/browser-indexeddb.js +72 -8
  27. package/dist/storage/cloud-outbox.d.ts +30 -0
  28. package/dist/storage/cloud-outbox.js +628 -0
  29. package/dist/storage/cloud.d.ts +1 -0
  30. package/dist/storage/cloud.js +1 -0
  31. package/dist/storage/node-jsonl.d.ts +3 -0
  32. package/dist/storage/node-jsonl.js +273 -4
  33. package/dist/storage/node-sqlite.d.ts +3 -0
  34. package/dist/storage/node-sqlite.js +66 -17
  35. package/dist/storage/react-native-jsonl.d.ts +3 -0
  36. package/dist/storage/react-native-jsonl.js +223 -0
  37. package/dist/types.d.ts +21 -2
  38. package/dist-cjs/amarsia/analytics.js +12 -0
  39. package/dist-cjs/amarsia/inference.js +335 -0
  40. package/dist-cjs/browser.js +5 -0
  41. package/dist-cjs/cli-agents-template.js +31 -7
  42. package/dist-cjs/compiler/build-project.js +68 -4
  43. package/dist-cjs/compiler/node-esm-banner.js +32 -0
  44. package/dist-cjs/orcha.js +5 -0
  45. package/dist-cjs/project-loader.js +7 -5
  46. package/dist-cjs/react-native.js +5 -0
  47. package/dist-cjs/runtime/agent.js +137 -30
  48. package/dist-cjs/runtime/compiler-hook.js +11 -0
  49. package/dist-cjs/runtime/create-orcha.js +117 -12
  50. package/dist-cjs/runtime/optional-features.js +58 -0
  51. package/dist-cjs/runtime/platform-browser.js +16 -3
  52. package/dist-cjs/runtime/platform-node.js +16 -2
  53. package/dist-cjs/runtime/platform-react-native.js +18 -3
  54. package/dist-cjs/storage/browser-indexeddb.js +73 -8
  55. package/dist-cjs/storage/cloud-outbox.js +646 -0
  56. package/dist-cjs/storage/cloud.js +29 -0
  57. package/dist-cjs/storage/node-jsonl.js +246 -3
  58. package/dist-cjs/storage/node-sqlite.js +70 -19
  59. package/dist-cjs/storage/react-native-jsonl.js +207 -0
  60. package/package.json +1 -1
@@ -1,5 +1,8 @@
1
1
  import AsyncStorage from "@react-native-async-storage/async-storage";
2
2
  const writes = new Map();
3
+ const tailIndexes = new Map();
4
+ const TAIL_CHUNK_SIZE = 100;
5
+ let tailChunkGeneration = 0;
3
6
  export class ReactNativeJsonlSessionStore {
4
7
  #namespace;
5
8
  #listeners = new Map();
@@ -17,6 +20,33 @@ export class ReactNativeJsonlSessionStore {
17
20
  .filter(Boolean)
18
21
  .map((line) => JSON.parse(line));
19
22
  }
23
+ async readAfterSequence(sessionId, afterSequence, limit) {
24
+ if (!Number.isInteger(afterSequence) ||
25
+ afterSequence < 0 ||
26
+ !Number.isInteger(limit) ||
27
+ limit <= 0) {
28
+ throw new Error("Invalid session event tail range.");
29
+ }
30
+ await writes.get(this.#key(sessionId))?.catch(() => undefined);
31
+ const index = await this.#ensureTailIndex(sessionId);
32
+ const relevant = index.chunks.filter((chunk) => chunk.lastSequence > afterSequence);
33
+ const events = [];
34
+ for (const chunk of relevant) {
35
+ const contents = await AsyncStorage.getItem(chunk.key);
36
+ if (contents === null) {
37
+ tailIndexes.delete(this.#tailIndexKey(sessionId));
38
+ return this.#readTailFromCanonical(sessionId, afterSequence, limit);
39
+ }
40
+ const parsed = parseEvents(contents).slice(0, chunk.count);
41
+ for (const event of parsed) {
42
+ if (event.sequence > afterSequence)
43
+ events.push(event);
44
+ if (events.length === limit)
45
+ return events;
46
+ }
47
+ }
48
+ return events;
49
+ }
20
50
  async append(sessionId, events) {
21
51
  if (events.length === 0) {
22
52
  return;
@@ -24,9 +54,20 @@ export class ReactNativeJsonlSessionStore {
24
54
  const key = this.#key(sessionId);
25
55
  const previous = writes.get(key) ?? Promise.resolve();
26
56
  const write = previous.then(async () => {
57
+ const index = await this.#ensureTailIndex(sessionId);
58
+ await AsyncStorage.setItem(this.#tailPendingKey(sessionId), "1");
27
59
  const existing = await AsyncStorage.getItem(key);
28
60
  const appended = events.map((event) => JSON.stringify(event)).join("\n");
29
61
  await AsyncStorage.setItem(key, `${existing ? `${existing.replace(/\n*$/, "")}\n` : ""}${appended}\n`);
62
+ try {
63
+ await this.#appendTailIndex(sessionId, index, events);
64
+ await AsyncStorage.removeItem(this.#tailPendingKey(sessionId));
65
+ }
66
+ catch {
67
+ tailIndexes.delete(this.#tailIndexKey(sessionId));
68
+ // Canonical JSONL is already durable. A pending marker makes the
69
+ // auxiliary index rebuild on the next tail read.
70
+ }
30
71
  });
31
72
  writes.set(key, write);
32
73
  try {
@@ -47,6 +88,31 @@ export class ReactNativeJsonlSessionStore {
47
88
  .map((key) => key.slice(prefix.length))
48
89
  .sort();
49
90
  }
91
+ async getCloudAcknowledgement(sessionId) {
92
+ const value = Number(await AsyncStorage.getItem(this.#acknowledgementKey(sessionId)));
93
+ return Number.isInteger(value) && value >= 0 ? value : 0;
94
+ }
95
+ async setCloudAcknowledgement(sessionId, sequence) {
96
+ if (!Number.isInteger(sequence) || sequence < 0) {
97
+ throw new Error("Invalid cloud acknowledgement sequence.");
98
+ }
99
+ const key = this.#acknowledgementKey(sessionId);
100
+ const previous = writes.get(key) ?? Promise.resolve();
101
+ const write = previous.then(async () => {
102
+ const current = Number(await AsyncStorage.getItem(key));
103
+ if (!Number.isInteger(current) || current < sequence) {
104
+ await AsyncStorage.setItem(key, String(sequence));
105
+ }
106
+ });
107
+ writes.set(key, write);
108
+ try {
109
+ await write;
110
+ }
111
+ finally {
112
+ if (writes.get(key) === write)
113
+ writes.delete(key);
114
+ }
115
+ }
50
116
  subscribe(sessionId, listener) {
51
117
  const listeners = this.#listeners.get(sessionId) ?? new Set();
52
118
  listeners.add(listener);
@@ -68,4 +134,161 @@ export class ReactNativeJsonlSessionStore {
68
134
  #key(sessionId) {
69
135
  return `@orchajs:${this.#namespace}:${sessionId}`;
70
136
  }
137
+ #acknowledgementKey(sessionId) {
138
+ return `@orchajs-cloud-ack:${this.#namespace}:${sessionId}`;
139
+ }
140
+ #tailIndexKey(sessionId) {
141
+ return `@orchajs-tail-index:${this.#namespace}:${sessionId}`;
142
+ }
143
+ #tailPendingKey(sessionId) {
144
+ return `@orchajs-tail-pending:${this.#namespace}:${sessionId}`;
145
+ }
146
+ #tailChunkKey(sessionId) {
147
+ tailChunkGeneration += 1;
148
+ return `@orchajs-tail-chunk:${this.#namespace}:${sessionId}:${Date.now()}:${tailChunkGeneration}`;
149
+ }
150
+ async #ensureTailIndex(sessionId) {
151
+ const indexKey = this.#tailIndexKey(sessionId);
152
+ const pending = await AsyncStorage.getItem(this.#tailPendingKey(sessionId));
153
+ if (!pending) {
154
+ const cached = tailIndexes.get(indexKey);
155
+ if (cached)
156
+ return cached;
157
+ const stored = await AsyncStorage.getItem(indexKey);
158
+ if (stored) {
159
+ try {
160
+ const parsed = parseTailIndex(stored);
161
+ tailIndexes.set(indexKey, parsed);
162
+ return parsed;
163
+ }
164
+ catch {
165
+ // Rebuild invalid auxiliary data from canonical JSONL.
166
+ }
167
+ }
168
+ }
169
+ return this.#rebuildTailIndex(sessionId);
170
+ }
171
+ async #rebuildTailIndex(sessionId) {
172
+ const events = await this.read(sessionId);
173
+ const chunks = [];
174
+ const writes = [];
175
+ for (let start = 0; start < events.length; start += TAIL_CHUNK_SIZE) {
176
+ const chunkEvents = events.slice(start, start + TAIL_CHUNK_SIZE);
177
+ const key = this.#tailChunkKey(sessionId);
178
+ writes.push([key, serializeEvents(chunkEvents)]);
179
+ chunks.push(tailChunk(key, chunkEvents));
180
+ }
181
+ const index = {
182
+ version: 1,
183
+ eventCount: events.length,
184
+ chunks,
185
+ };
186
+ writes.push([this.#tailIndexKey(sessionId), JSON.stringify(index)]);
187
+ if (writes.length > 0)
188
+ await AsyncStorage.multiSet(writes);
189
+ await AsyncStorage.removeItem(this.#tailPendingKey(sessionId));
190
+ tailIndexes.set(this.#tailIndexKey(sessionId), index);
191
+ return index;
192
+ }
193
+ async #appendTailIndex(sessionId, current, events) {
194
+ const chunks = [...current.chunks];
195
+ const replacements = [];
196
+ const obsolete = [];
197
+ let remaining = [...events];
198
+ const last = chunks.at(-1);
199
+ if (last && last.count < TAIL_CHUNK_SIZE) {
200
+ const stored = await AsyncStorage.getItem(last.key);
201
+ if (stored === null)
202
+ throw new Error("Missing tail index chunk.");
203
+ const retained = parseEvents(stored).slice(0, last.count);
204
+ const added = remaining.splice(0, TAIL_CHUNK_SIZE - retained.length);
205
+ const combined = [...retained, ...added];
206
+ const key = this.#tailChunkKey(sessionId);
207
+ replacements.push([key, serializeEvents(combined)]);
208
+ chunks[chunks.length - 1] = tailChunk(key, combined);
209
+ obsolete.push(last.key);
210
+ }
211
+ while (remaining.length > 0) {
212
+ const chunkEvents = remaining.splice(0, TAIL_CHUNK_SIZE);
213
+ const key = this.#tailChunkKey(sessionId);
214
+ replacements.push([key, serializeEvents(chunkEvents)]);
215
+ chunks.push(tailChunk(key, chunkEvents));
216
+ }
217
+ const index = {
218
+ version: 1,
219
+ eventCount: current.eventCount + events.length,
220
+ chunks,
221
+ };
222
+ replacements.push([
223
+ this.#tailIndexKey(sessionId),
224
+ JSON.stringify(index),
225
+ ]);
226
+ await AsyncStorage.multiSet(replacements);
227
+ tailIndexes.set(this.#tailIndexKey(sessionId), index);
228
+ await Promise.all(obsolete.map((key) => AsyncStorage.removeItem(key)));
229
+ }
230
+ async #readTailFromCanonical(sessionId, afterSequence, limit) {
231
+ const index = await this.#rebuildTailIndex(sessionId);
232
+ const events = [];
233
+ for (const chunk of index.chunks) {
234
+ if (chunk.lastSequence <= afterSequence)
235
+ continue;
236
+ const contents = await AsyncStorage.getItem(chunk.key);
237
+ if (!contents)
238
+ continue;
239
+ for (const event of parseEvents(contents).slice(0, chunk.count)) {
240
+ if (event.sequence > afterSequence)
241
+ events.push(event);
242
+ if (events.length === limit)
243
+ return events;
244
+ }
245
+ }
246
+ return events;
247
+ }
248
+ }
249
+ function tailChunk(key, events) {
250
+ return {
251
+ key,
252
+ firstSequence: events[0].sequence,
253
+ lastSequence: events.at(-1).sequence,
254
+ count: events.length,
255
+ };
256
+ }
257
+ function serializeEvents(events) {
258
+ return `${events.map((event) => JSON.stringify(event)).join("\n")}\n`;
259
+ }
260
+ function parseEvents(contents) {
261
+ return contents
262
+ .split("\n")
263
+ .filter(Boolean)
264
+ .map((line) => JSON.parse(line));
265
+ }
266
+ function parseTailIndex(contents) {
267
+ const value = JSON.parse(contents);
268
+ if (value.version !== 1 ||
269
+ !Number.isInteger(value.eventCount) ||
270
+ (value.eventCount ?? -1) < 0 ||
271
+ !Array.isArray(value.chunks)) {
272
+ throw new Error("Invalid React Native tail index.");
273
+ }
274
+ let count = 0;
275
+ let previousSequence = 0;
276
+ for (const chunk of value.chunks) {
277
+ if (typeof chunk?.key !== "string" ||
278
+ !Number.isInteger(chunk.firstSequence) ||
279
+ !Number.isInteger(chunk.lastSequence) ||
280
+ !Number.isInteger(chunk.count) ||
281
+ chunk.count <= 0 ||
282
+ chunk.count > TAIL_CHUNK_SIZE ||
283
+ chunk.firstSequence <= previousSequence ||
284
+ chunk.lastSequence < chunk.firstSequence) {
285
+ throw new Error("Invalid React Native tail index.");
286
+ }
287
+ count += chunk.count;
288
+ previousSequence = chunk.lastSequence;
289
+ }
290
+ if (count !== value.eventCount) {
291
+ throw new Error("Invalid React Native tail index.");
292
+ }
293
+ return value;
71
294
  }
package/dist/types.d.ts CHANGED
@@ -46,7 +46,7 @@ export interface ProjectConfiguration {
46
46
  credentials?: Record<string, string | undefined>;
47
47
  };
48
48
  storage?: {
49
- strategy?: "node-jsonl" | "sqlite" | "indexeddb" | "react-native";
49
+ strategy?: "node-jsonl" | "sqlite" | "indexeddb" | "react-native" | "cloud";
50
50
  directory?: string;
51
51
  };
52
52
  }
@@ -61,13 +61,14 @@ export type AgentRegistration = string | {
61
61
  subagents?: Record<string, SubagentRegistration>;
62
62
  };
63
63
  export interface OrchaInitConfiguration {
64
- providers: ProviderInitConfigurations;
64
+ providers?: ProviderInitConfigurations;
65
65
  agents: Record<string, AgentRegistration>;
66
66
  /** Precompiled registry used by browser, React Native, and custom runtimes. */
67
67
  bundle?: CompiledBundle;
68
68
  amarsia?: {
69
69
  key: string;
70
70
  endpoint?: string;
71
+ inference?: boolean;
71
72
  };
72
73
  actions?: LocalActionRuntimeConfiguration;
73
74
  mcp?: ProjectConfiguration["mcp"];
@@ -404,8 +405,26 @@ export interface SessionEvent<TData = Record<string, unknown>> {
404
405
  }
405
406
  export interface SessionStore {
406
407
  read(sessionId: string): Promise<SessionEvent[]>;
408
+ /** Reads a bounded contiguous local tail without materializing prior events. */
409
+ readAfterSequence?(sessionId: string, afterSequence: number, limit: number): Promise<SessionEvent[]>;
407
410
  append(sessionId: string, events: SessionEvent[]): Promise<void>;
408
411
  listSessionIds(): Promise<string[]>;
412
+ /** Durable cloud delivery checkpoint owned by the local store. */
413
+ getCloudAcknowledgement?(sessionId: string): Promise<number>;
414
+ /** Advances the durable cloud delivery checkpoint for a session. */
415
+ setCloudAcknowledgement?(sessionId: string, sequence: number): Promise<void>;
416
+ /** Number of sessions with local events not yet acknowledged by cloud. */
417
+ readonly cloudStoragePendingCount?: number | undefined;
418
+ /** Most recent background cloud delivery error. */
419
+ readonly cloudStorageLastError?: Error | undefined;
420
+ /** Explicitly reconciles a warm local session with cloud storage. */
421
+ refreshCloudSession?(sessionId: string): Promise<void>;
422
+ flushCloudStorage?(options?: {
423
+ timeoutMs?: number;
424
+ }): Promise<void>;
425
+ closeCloudStorage?(options?: {
426
+ timeoutMs?: number;
427
+ }): Promise<void>;
409
428
  subscribe?(sessionId: string, listener: (events: SessionEvent[]) => void): () => void;
410
429
  subscribeAll?(listener: (sessionId: string, events: SessionEvent[]) => void): () => void;
411
430
  describeLocation?(sessionId: string): Promise<string>;
@@ -212,8 +212,20 @@ function withAmarsiaTelemetry(options) {
212
212
  const projections = /* @__PURE__ */ new Map();
213
213
  const pending = /* @__PURE__ */ new Map();
214
214
  return {
215
+ get cloudStoragePendingCount() {
216
+ return store.cloudStoragePendingCount;
217
+ },
218
+ get cloudStorageLastError() {
219
+ return store.cloudStorageLastError;
220
+ },
215
221
  read: (sessionId) => store.read(sessionId),
222
+ readAfterSequence: store.readAfterSequence?.bind(store),
216
223
  listSessionIds: () => store.listSessionIds(),
224
+ getCloudAcknowledgement: store.getCloudAcknowledgement?.bind(store),
225
+ setCloudAcknowledgement: store.setCloudAcknowledgement?.bind(store),
226
+ refreshCloudSession: store.refreshCloudSession?.bind(store),
227
+ flushCloudStorage: store.flushCloudStorage?.bind(store),
228
+ closeCloudStorage: store.closeCloudStorage?.bind(store),
217
229
  describeLocation: store.describeLocation?.bind(store),
218
230
  subscribe: store.subscribe?.bind(store),
219
231
  subscribeAll: store.subscribeAll?.bind(store),
@@ -0,0 +1,335 @@
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
+ function createAmarsiaInferenceGenerator(configuration) {
38
+ const endpoint = (0, import_endpoint.resolveAmarsiaEndpoint)(configuration.endpoint);
39
+ return (provider, _providerConfiguration, request) => generateThroughAmarsia(endpoint, configuration.key, provider, request);
40
+ }
41
+ async function generateThroughAmarsia(endpoint, key, provider, request) {
42
+ if (!key.trim()) {
43
+ throw new Error(
44
+ "Amarsia inference requires a non-empty amarsia.key."
45
+ );
46
+ }
47
+ if (!SUPPORTED_AMARSIA_PROVIDERS.has(provider)) {
48
+ throw new import_errors.OrchaError(
49
+ "unsupported_provider_capability",
50
+ `Amarsia inference supports only OpenAI, Anthropic, Google GenAI, Vertex AI, and DeepSeek; received "${provider}".`
51
+ );
52
+ }
53
+ if (request.outputType === "image" || request.outputType === "audio") {
54
+ throw new import_errors.OrchaError(
55
+ "unsupported_provider_capability",
56
+ `Amarsia Responses does not support "${request.outputType}" as a direct agent output.`
57
+ );
58
+ }
59
+ const gatewayProvider = provider === "vertexai" ? "googlegenai" : provider;
60
+ const body = {
61
+ provider: gatewayProvider,
62
+ model: request.model,
63
+ instructions: request.systemPrompt,
64
+ input: await toResponsesInput(request.messages),
65
+ ...provider === "vertexai" ? { region: "global" } : request.region && request.region !== "provider_managed" ? { region: request.region } : {},
66
+ ...request.maxTokens ? { max_output_tokens: request.maxTokens } : {},
67
+ ...request.publishOutput ? { stream: true } : {}
68
+ };
69
+ if (request.tools.length > 0) {
70
+ body.tools = request.tools.map((tool) => ({
71
+ type: "function",
72
+ name: tool.name,
73
+ description: tool.description,
74
+ parameters: tool.parameters
75
+ }));
76
+ }
77
+ if (request.outputType === "json") {
78
+ body.text = {
79
+ format: {
80
+ type: "json_schema",
81
+ name: "orcha_output",
82
+ schema: request.outputSchema ?? {},
83
+ strict: true
84
+ }
85
+ };
86
+ }
87
+ body.reasoning = {
88
+ summary: "auto",
89
+ ...request.reasoningLevel ? { effort: request.reasoningLevel } : {}
90
+ };
91
+ body.include = ["reasoning.encrypted_content"];
92
+ if (request.trace) {
93
+ body.metadata = {
94
+ agent_external_id: request.trace.agentExternalId,
95
+ session_id: request.trace.sessionId
96
+ };
97
+ }
98
+ const response = await fetch(`${endpoint}/v3/model/v1/responses`, {
99
+ method: "POST",
100
+ headers: {
101
+ "content-type": "application/json",
102
+ authorization: `Bearer ${key}`
103
+ },
104
+ body: JSON.stringify(body),
105
+ signal: request.signal
106
+ });
107
+ if (!response.ok) {
108
+ const message = await responseError(response);
109
+ throw new Error(
110
+ message ?? `Amarsia inference request failed with HTTP ${response.status}.`
111
+ );
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
+ async function toResponsesInput(messages) {
117
+ const input = [];
118
+ for (const message of messages) {
119
+ if (message.role === "user") {
120
+ input.push({
121
+ role: "user",
122
+ content: typeof message.content === "string" ? message.content : await Promise.all(
123
+ message.content.map(toResponsesUserContent)
124
+ )
125
+ });
126
+ continue;
127
+ }
128
+ if (message.role === "tool") {
129
+ for (const result of message.results) {
130
+ input.push({
131
+ type: "function_call_output",
132
+ call_id: result.callId,
133
+ output: JSON.stringify(result.output ?? null),
134
+ ...result.isError ? { is_error: true } : {}
135
+ });
136
+ }
137
+ continue;
138
+ }
139
+ for (const block of message.content) {
140
+ if (block.type === "text") {
141
+ input.push({
142
+ role: "assistant",
143
+ content: [{ type: "output_text", text: block.text }]
144
+ });
145
+ } else if (block.type === "tool_call") {
146
+ input.push({
147
+ type: "function_call",
148
+ call_id: block.callId,
149
+ name: block.name,
150
+ arguments: JSON.stringify(block.arguments),
151
+ ...block.replay?.providerId ? { id: block.replay.providerId } : {},
152
+ ...block.replay?.opaqueData ? { encrypted_content: block.replay.opaqueData } : {}
153
+ });
154
+ } else {
155
+ input.push({
156
+ type: "reasoning",
157
+ ...block.replay?.providerId ? { id: block.replay.providerId } : {},
158
+ summary: block.text ? [{ type: "summary_text", text: block.text }] : [],
159
+ ...block.replay?.opaqueData ? { encrypted_content: block.replay.opaqueData } : {}
160
+ });
161
+ }
162
+ }
163
+ }
164
+ return input;
165
+ }
166
+ async function toResponsesUserContent(content) {
167
+ if (content.type === "text") {
168
+ return { type: "input_text", text: content.text };
169
+ }
170
+ if (content.type === "file") {
171
+ const file = await (0, import_file_content.readLocalFileContent)(content);
172
+ return content.mimeType.startsWith("image/") ? {
173
+ type: "input_image",
174
+ image_url: file.dataUri,
175
+ detail: "auto"
176
+ } : {
177
+ type: "input_file",
178
+ file_data: file.dataUri,
179
+ filename: file.fileName
180
+ };
181
+ }
182
+ if (content.type === "image") {
183
+ return {
184
+ type: "input_image",
185
+ image_url: content.fileUri,
186
+ detail: "auto"
187
+ };
188
+ }
189
+ if (content.type === "url") {
190
+ return { type: "input_file", file_url: content.fileUri };
191
+ }
192
+ throw new import_errors.OrchaError(
193
+ "unsupported_content_type",
194
+ `Amarsia Responses does not support ${content.type} content from a URI.`
195
+ );
196
+ }
197
+ async function readGatewayStream(response, publishOutput) {
198
+ let text = "";
199
+ let terminal;
200
+ for await (const event of (0, import_sse.readServerSentEvents)(response)) {
201
+ if (event.type === "response.output_text.delta" && typeof event.delta === "string") {
202
+ text += event.delta;
203
+ publishOutput?.(text);
204
+ } else if (event.type === "response.completed" || event.type === "response.incomplete") {
205
+ if (!isRecord(event.response)) {
206
+ throw new Error(
207
+ "Amarsia returned an invalid terminal stream event."
208
+ );
209
+ }
210
+ terminal = event.response;
211
+ } else if (event.type === "response.failed" || event.type === "error") {
212
+ const error = isRecord(event.error) ? event.error : event;
213
+ throw new Error(
214
+ typeof error.message === "string" ? error.message : "Amarsia inference streaming request failed."
215
+ );
216
+ }
217
+ }
218
+ if (!terminal) {
219
+ throw new Error(
220
+ "Amarsia inference stream ended without a terminal response."
221
+ );
222
+ }
223
+ return terminal;
224
+ }
225
+ function toProviderResponse(request, response) {
226
+ if (response.status === "failed") {
227
+ throw new Error(
228
+ response.error?.message ?? "Amarsia failed to generate a response."
229
+ );
230
+ }
231
+ if (response.status === "incomplete" && response.incomplete_details?.reason !== "max_output_tokens") {
232
+ throw new Error(
233
+ `Amarsia returned an incomplete response: ${response.incomplete_details?.reason ?? "unknown reason"}.`
234
+ );
235
+ }
236
+ const content = [];
237
+ let refused = false;
238
+ for (const item of response.output ?? []) {
239
+ if (item.type === "message") {
240
+ for (const part of item.content ?? []) {
241
+ if (part.type === "output_text" && typeof part.text === "string") {
242
+ content.push({ type: "text", text: part.text });
243
+ } else if (part.type === "refusal" && typeof part.refusal === "string") {
244
+ refused = true;
245
+ content.push({ type: "text", text: part.refusal });
246
+ }
247
+ }
248
+ } else if (item.type === "function_call" && typeof item.call_id === "string" && typeof item.name === "string") {
249
+ content.push({
250
+ type: "tool_call",
251
+ callId: item.call_id,
252
+ name: item.name,
253
+ arguments: parseArguments(item.arguments, item.name),
254
+ replay: replay(item)
255
+ });
256
+ } else if (item.type === "reasoning") {
257
+ content.push({
258
+ type: "reasoning",
259
+ text: [...item.content ?? [], ...item.summary ?? []].filter(
260
+ (part) => (part.type === "reasoning_text" || part.type === "summary_text") && typeof part.text === "string"
261
+ ).map((part) => part.text).join(""),
262
+ replay: replay(item)
263
+ });
264
+ } else {
265
+ throw new Error(
266
+ `Amarsia returned unsupported output item "${String(item.type)}".`
267
+ );
268
+ }
269
+ }
270
+ const toolCalls = content.filter(
271
+ (block) => block.type === "tool_call"
272
+ );
273
+ const textOutput = response.output_text ?? content.filter((block) => block.type === "text").map((block) => block.text).join("");
274
+ const stopReason = normalizeStopReason(response, toolCalls, refused);
275
+ const output = request.outputType === "json" && toolCalls.length === 0 && stopReason !== "max_tokens" ? (0, import_structured_output.parseStructuredOutput)(textOutput, request.outputSchema ?? {}) : textOutput;
276
+ if (textOutput) {
277
+ request.publishOutput?.(textOutput);
278
+ }
279
+ return {
280
+ output,
281
+ responseId: response.id,
282
+ stopReason,
283
+ content,
284
+ toolCalls,
285
+ usage: normalizeUsage(response.usage)
286
+ };
287
+ }
288
+ function replay(item) {
289
+ const value = {
290
+ ...typeof item.id === "string" ? { providerId: item.id } : {},
291
+ ...typeof item.encrypted_content === "string" ? { opaqueData: item.encrypted_content } : {}
292
+ };
293
+ return Object.keys(value).length > 0 ? value : void 0;
294
+ }
295
+ function parseArguments(value, name) {
296
+ const parsed = typeof value === "string" ? JSON.parse(value) : value;
297
+ if (!isRecord(parsed)) {
298
+ throw new Error(
299
+ `Amarsia returned non-object arguments for tool "${name}".`
300
+ );
301
+ }
302
+ return parsed;
303
+ }
304
+ function normalizeStopReason(response, toolCalls, refused) {
305
+ if (refused) return "content_filter";
306
+ if (toolCalls.length > 0) return "tool_call";
307
+ if (response.status === "incomplete") {
308
+ return response.incomplete_details?.reason === "max_output_tokens" ? "max_tokens" : "unknown";
309
+ }
310
+ return response.status === "completed" ? "end_turn" : "unknown";
311
+ }
312
+ function normalizeUsage(usage) {
313
+ return {
314
+ inputTokens: usage?.input_tokens ?? 0,
315
+ outputTokens: usage?.output_tokens ?? 0,
316
+ reasoningTokens: usage?.output_tokens_details?.reasoning_tokens ?? null,
317
+ cacheReadTokens: usage?.input_tokens_details?.cached_tokens ?? 0,
318
+ cacheWriteTokens: usage?.cache_write_tokens ?? 0
319
+ };
320
+ }
321
+ async function responseError(response) {
322
+ try {
323
+ const body = await response.json();
324
+ return body.error?.message ?? body.message;
325
+ } catch {
326
+ return void 0;
327
+ }
328
+ }
329
+ function isRecord(value) {
330
+ return typeof value === "object" && value !== null && !Array.isArray(value);
331
+ }
332
+ // Annotate the CommonJS export names for ESM import in node:
333
+ 0 && (module.exports = {
334
+ createAmarsiaInferenceGenerator
335
+ });
@@ -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(