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.
- 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 +337 -0
- package/dist/browser.js +5 -0
- package/dist/cli-agents-template.d.ts +1 -1
- package/dist/cli-agents-template.js +31 -7
- 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/project-loader.js +4 -2
- package/dist/providers/types.d.ts +4 -0
- package/dist/react-native.js +5 -0
- package/dist/runtime/agent.js +103 -14
- 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/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/types.d.ts +21 -2
- 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 +31 -7
- 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/project-loader.js +7 -5
- package/dist-cjs/react-native.js +5 -0
- package/dist-cjs/runtime/agent.js +137 -30
- 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/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/package.json +1 -1
package/dist/runtime/agent.js
CHANGED
|
@@ -94,7 +94,7 @@ export class RuntimeAgent {
|
|
|
94
94
|
if (typeof normalized === "object" &&
|
|
95
95
|
normalized !== null &&
|
|
96
96
|
"sessionId" in normalized) {
|
|
97
|
-
return this.#createExecution(sessionId, async () =>
|
|
97
|
+
return this.#createExecution(sessionId, async () => this.#recordRejectedRun(sessionId, "run() always creates a new session and does not accept sessionId.", true, lineage));
|
|
98
98
|
}
|
|
99
99
|
return this.#createExecution(sessionId, (publishOutput, publishSnapshot) => this.#start(sessionId, normalized, true, publishOutput, publishSnapshot, lineage));
|
|
100
100
|
}
|
|
@@ -121,12 +121,12 @@ export class RuntimeAgent {
|
|
|
121
121
|
return this.#createExecution(sessionId, (publishOutput, publishSnapshot) => this.#start(sessionId, continuedInput, false, publishOutput, publishSnapshot));
|
|
122
122
|
}
|
|
123
123
|
if (!input || typeof input !== "object") {
|
|
124
|
-
return this.#createExecution(sessionId, async () =>
|
|
124
|
+
return this.#createExecution(sessionId, async () => this.#recordRejectedRun(sessionId, "resume() requires content or toolResults.", false));
|
|
125
125
|
}
|
|
126
126
|
const hasMessage = "content" in input;
|
|
127
127
|
const hasToolResults = "toolResults" in input;
|
|
128
128
|
if (hasMessage === hasToolResults) {
|
|
129
|
-
return this.#createExecution(sessionId, async () =>
|
|
129
|
+
return this.#createExecution(sessionId, async () => this.#recordRejectedRun(sessionId, "resume() requires either content or toolResults, but not both.", false));
|
|
130
130
|
}
|
|
131
131
|
const normalizedInput = hasMessage && this.#delegatedOnly
|
|
132
132
|
? {
|
|
@@ -145,6 +145,55 @@ export class RuntimeAgent {
|
|
|
145
145
|
return task ? task : [];
|
|
146
146
|
});
|
|
147
147
|
}
|
|
148
|
+
async #recordRejectedRun(sessionId, message, createSession, lineage) {
|
|
149
|
+
return this.#withSessionExecutionLock(sessionId, async () => {
|
|
150
|
+
const events = await this.#readSession(sessionId);
|
|
151
|
+
if (!createSession && events.length === 0) {
|
|
152
|
+
return failure(sessionId, "session_not_found", `Session "${sessionId}" does not exist.`);
|
|
153
|
+
}
|
|
154
|
+
if (events.length > 0) {
|
|
155
|
+
try {
|
|
156
|
+
this.#assertOwnedSession(sessionId, events);
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
return failure(sessionId, "session_not_found", `Session "${sessionId}" was not created by this agent.`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const writer = new SessionEventWriter(sessionId, events);
|
|
163
|
+
const rejectedEvents = [];
|
|
164
|
+
if (events.length === 0) {
|
|
165
|
+
rejectedEvents.push(writer.create("session.created", {
|
|
166
|
+
schemaVersion: 1,
|
|
167
|
+
sessionId,
|
|
168
|
+
agent: this.#agentKey,
|
|
169
|
+
status: "active",
|
|
170
|
+
metadata: {},
|
|
171
|
+
variables: {},
|
|
172
|
+
...(lineage ? { lineage } : {}),
|
|
173
|
+
}, "session"));
|
|
174
|
+
}
|
|
175
|
+
rejectedEvents.push(writer.create("run.started", {
|
|
176
|
+
status: "running",
|
|
177
|
+
agent: this.#agentKey,
|
|
178
|
+
provider: this.#manifest.provider,
|
|
179
|
+
model: this.#manifest.model,
|
|
180
|
+
region: this.#manifest.region ?? "provider_managed",
|
|
181
|
+
reasoningLevel: this.#manifest.reasoningLevel,
|
|
182
|
+
outputType: this.#manifest.outputType,
|
|
183
|
+
clientCapabilities: [],
|
|
184
|
+
}), writer.create("run.failed", {
|
|
185
|
+
status: "failed",
|
|
186
|
+
durationMs: 0,
|
|
187
|
+
error: {
|
|
188
|
+
code: "invalid_input",
|
|
189
|
+
message,
|
|
190
|
+
retryable: false,
|
|
191
|
+
},
|
|
192
|
+
}));
|
|
193
|
+
await this.#store.append(sessionId, rejectedEvents);
|
|
194
|
+
return failure(sessionId, "invalid_input", message);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
148
197
|
async get(sessionId) {
|
|
149
198
|
const events = await this.#readSession(sessionId);
|
|
150
199
|
this.#assertOwnedSession(sessionId, events);
|
|
@@ -417,10 +466,10 @@ export class RuntimeAgent {
|
|
|
417
466
|
content = normalizeContent(input.content, this.#projectRoot);
|
|
418
467
|
}
|
|
419
468
|
catch (error) {
|
|
420
|
-
return
|
|
469
|
+
return this.#recordRejectedRun(sessionId, error instanceof Error ? error.message : String(error), createSession, lineage);
|
|
421
470
|
}
|
|
422
471
|
if (!content) {
|
|
423
|
-
return
|
|
472
|
+
return this.#recordRejectedRun(sessionId, "Agent content must contain at least one valid item.", createSession, lineage);
|
|
424
473
|
}
|
|
425
474
|
let initialName;
|
|
426
475
|
let initialMetadata = {};
|
|
@@ -433,16 +482,16 @@ export class RuntimeAgent {
|
|
|
433
482
|
}
|
|
434
483
|
catch (error) {
|
|
435
484
|
const message = error instanceof Error ? error.message : String(error);
|
|
436
|
-
return
|
|
485
|
+
return this.#recordRejectedRun(sessionId, message, createSession, lineage);
|
|
437
486
|
}
|
|
438
487
|
}
|
|
439
488
|
const capabilities = normalizeClientCapabilities(input.clientCapabilities ?? []);
|
|
440
489
|
if (!capabilities) {
|
|
441
|
-
return
|
|
490
|
+
return this.#recordRejectedRun(sessionId, "clientCapabilities must contain tool names or client tool definitions.", createSession, lineage);
|
|
442
491
|
}
|
|
443
492
|
const unknownCapabilities = capabilities.filter((name) => !(this.#manifest.actions ?? {})[name]);
|
|
444
493
|
if (unknownCapabilities.length > 0) {
|
|
445
|
-
return
|
|
494
|
+
return this.#recordRejectedRun(sessionId, `Unknown client capabilities: ${unknownCapabilities.join(", ")}.`, createSession, lineage);
|
|
446
495
|
}
|
|
447
496
|
return this.#withSessionExecutionLock(sessionId, async () => {
|
|
448
497
|
let previousEvents = await this.#readSession(sessionId);
|
|
@@ -650,11 +699,16 @@ export class RuntimeAgent {
|
|
|
650
699
|
publishOutput?.(output);
|
|
651
700
|
},
|
|
652
701
|
signal,
|
|
702
|
+
trace: {
|
|
703
|
+
agentExternalId: this.#agentKey,
|
|
704
|
+
sessionId,
|
|
705
|
+
},
|
|
653
706
|
}), signal);
|
|
654
707
|
}
|
|
655
708
|
catch (error) {
|
|
656
709
|
if (this.#sessionControls.get(sessionId)?.pauseRequested) {
|
|
657
710
|
const pausedEvents = [];
|
|
711
|
+
const usage = usageForRun(previousEvents, writer.run);
|
|
658
712
|
if (streamedOutput) {
|
|
659
713
|
pausedEvents.push(writer.create("message.created", {
|
|
660
714
|
status: "incomplete",
|
|
@@ -668,6 +722,7 @@ export class RuntimeAgent {
|
|
|
668
722
|
pausedEvents.push(writer.create("run.paused", {
|
|
669
723
|
status: "paused",
|
|
670
724
|
output: streamedOutput,
|
|
725
|
+
usage,
|
|
671
726
|
durationMs: elapsedRunDuration(previousEvents, writer.run),
|
|
672
727
|
}), writer.create("session.paused", { status: "paused" }, "session"));
|
|
673
728
|
await this.#store.append(sessionId, pausedEvents);
|
|
@@ -836,10 +891,12 @@ export class RuntimeAgent {
|
|
|
836
891
|
const code = error instanceof OrchaError ? error.code : "execution_failed";
|
|
837
892
|
const details = error instanceof OrchaError ? error.details : undefined;
|
|
838
893
|
const retryable = error instanceof OrchaError ? error.retryable : false;
|
|
894
|
+
const usage = usageForRun(previousEvents, writer.run);
|
|
839
895
|
await this.#store.append(sessionId, [
|
|
840
896
|
writer.create("run.failed", {
|
|
841
897
|
status: "failed",
|
|
842
898
|
durationMs: elapsedRunDuration(previousEvents, writer.run),
|
|
899
|
+
usage,
|
|
843
900
|
error: {
|
|
844
901
|
code,
|
|
845
902
|
message,
|
|
@@ -867,7 +924,7 @@ export class RuntimeAgent {
|
|
|
867
924
|
evaluatedThroughSequence,
|
|
868
925
|
}));
|
|
869
926
|
await this.#store.append(sessionId, requestedEvents);
|
|
870
|
-
const task = Promise.all(evaluations.map((evaluation) => this.#runEvaluation(evaluation, events))).then((results) => this.#persistEvaluationResults(sessionId, writer.run, evaluatedThroughSequence, evaluations, results)).catch((error) => {
|
|
927
|
+
const task = Promise.all(evaluations.map((evaluation) => this.#runEvaluation(evaluation, events, sessionId))).then((results) => this.#persistEvaluationResults(sessionId, writer.run, evaluatedThroughSequence, evaluations, results)).catch((error) => {
|
|
871
928
|
const message = error instanceof Error ? error.message : String(error);
|
|
872
929
|
return evaluations.map((evaluation) => ({
|
|
873
930
|
name: evaluation.name,
|
|
@@ -879,14 +936,14 @@ export class RuntimeAgent {
|
|
|
879
936
|
});
|
|
880
937
|
this.#evaluationTasks.set(sessionId, task);
|
|
881
938
|
}
|
|
882
|
-
async #runEvaluation(evaluation, events) {
|
|
939
|
+
async #runEvaluation(evaluation, events, sessionId) {
|
|
883
940
|
const startedAt = performance.now();
|
|
884
941
|
try {
|
|
885
942
|
const provider = this.#configuration.providers[evaluation.provider];
|
|
886
943
|
if (!provider) {
|
|
887
944
|
throw new Error(`Evaluation "${evaluation.name}" requires provider "${evaluation.provider}" in orcha.init().`);
|
|
888
945
|
}
|
|
889
|
-
const response = await this.#generateProviderResponse(evaluation.provider, provider, createEvaluationRequest(evaluation, events));
|
|
946
|
+
const response = await this.#generateProviderResponse(evaluation.provider, provider, createEvaluationRequest(evaluation, events, this.#agentKey, sessionId));
|
|
890
947
|
const metrics = normalizeEvaluationMetrics(evaluation, response.output);
|
|
891
948
|
return {
|
|
892
949
|
name: evaluation.name,
|
|
@@ -1810,6 +1867,9 @@ function normalizeContent(content, projectRoot) {
|
|
|
1810
1867
|
}
|
|
1811
1868
|
const normalized = [];
|
|
1812
1869
|
for (const item of items) {
|
|
1870
|
+
const record = item && typeof item === "object"
|
|
1871
|
+
? item
|
|
1872
|
+
: {};
|
|
1813
1873
|
if (item &&
|
|
1814
1874
|
"type" in item &&
|
|
1815
1875
|
item.type === "text" &&
|
|
@@ -1836,6 +1896,23 @@ function normalizeContent(content, projectRoot) {
|
|
|
1836
1896
|
: undefined));
|
|
1837
1897
|
continue;
|
|
1838
1898
|
}
|
|
1899
|
+
const remoteUri = typeof record.fileUri === "string"
|
|
1900
|
+
? record.fileUri
|
|
1901
|
+
: typeof record.file_uri === "string"
|
|
1902
|
+
? record.file_uri
|
|
1903
|
+
: undefined;
|
|
1904
|
+
const remoteMimeType = typeof record.mimeType === "string"
|
|
1905
|
+
? record.mimeType
|
|
1906
|
+
: typeof record.mime_type === "string"
|
|
1907
|
+
? record.mime_type
|
|
1908
|
+
: undefined;
|
|
1909
|
+
if (remoteUri?.trim() &&
|
|
1910
|
+
(record.type === "url" ||
|
|
1911
|
+
record.type === "document" ||
|
|
1912
|
+
record.type === "file")) {
|
|
1913
|
+
normalized.push(normalizeUrlContent(remoteUri, remoteMimeType));
|
|
1914
|
+
continue;
|
|
1915
|
+
}
|
|
1839
1916
|
if (item &&
|
|
1840
1917
|
"type" in item &&
|
|
1841
1918
|
typeof item.type === "string" &&
|
|
@@ -1851,7 +1928,16 @@ function normalizeContent(content, projectRoot) {
|
|
|
1851
1928
|
}
|
|
1852
1929
|
return undefined;
|
|
1853
1930
|
}
|
|
1854
|
-
|
|
1931
|
+
if (normalized.some((item) => item.type === "text")) {
|
|
1932
|
+
return normalized;
|
|
1933
|
+
}
|
|
1934
|
+
return [
|
|
1935
|
+
{
|
|
1936
|
+
type: "text",
|
|
1937
|
+
text: "Process the attached content according to your agent instructions.",
|
|
1938
|
+
},
|
|
1939
|
+
...normalized,
|
|
1940
|
+
];
|
|
1855
1941
|
}
|
|
1856
1942
|
function normalizeDelegatedInput(input, projectRoot) {
|
|
1857
1943
|
if (typeof input === "string") {
|
|
@@ -2109,7 +2195,7 @@ function loadedSkillNames(events) {
|
|
|
2109
2195
|
? [event.data.name]
|
|
2110
2196
|
: []));
|
|
2111
2197
|
}
|
|
2112
|
-
function createEvaluationRequest(evaluation, events) {
|
|
2198
|
+
function createEvaluationRequest(evaluation, events, agentExternalId, sessionId) {
|
|
2113
2199
|
return {
|
|
2114
2200
|
model: evaluation.model,
|
|
2115
2201
|
maxTokens: evaluation.maxTokens ?? 2_000,
|
|
@@ -2179,6 +2265,7 @@ function createEvaluationRequest(evaluation, events) {
|
|
|
2179
2265
|
},
|
|
2180
2266
|
],
|
|
2181
2267
|
tools: [],
|
|
2268
|
+
trace: { agentExternalId, sessionId },
|
|
2182
2269
|
};
|
|
2183
2270
|
}
|
|
2184
2271
|
function evaluationTranscript(events) {
|
|
@@ -2378,7 +2465,9 @@ function projectSessionSnapshot(events) {
|
|
|
2378
2465
|
const pending = getPendingClientActions(events);
|
|
2379
2466
|
const lastAssistant = findLast(events, (event) => event.type === "message.created" &&
|
|
2380
2467
|
event.data.role === "assistant");
|
|
2381
|
-
const latestUsageEvent = findLast(events, (event) => (event.type === "run.completed" ||
|
|
2468
|
+
const latestUsageEvent = findLast(events, (event) => (event.type === "run.completed" ||
|
|
2469
|
+
event.type === "run.failed" ||
|
|
2470
|
+
event.type === "run.paused") &&
|
|
2382
2471
|
isUsage(event.data.usage));
|
|
2383
2472
|
return {
|
|
2384
2473
|
sessionId: created.data.sessionId,
|
|
@@ -3,3 +3,7 @@ export type RegistryCompiler = (configuration: OrchaInitConfiguration, projectRo
|
|
|
3
3
|
/** @internal Installed by Node development tooling, never by runtime bundles. */
|
|
4
4
|
export declare function setRegistryCompiler(compiler: RegistryCompiler): void;
|
|
5
5
|
export declare function getRegistryCompiler(): RegistryCompiler | undefined;
|
|
6
|
+
/** @internal Prevents registry evaluation from starting runtime resources. */
|
|
7
|
+
export declare function setRegistryCompilationActive(active: boolean): void;
|
|
8
|
+
/** @internal True only while development tooling evaluates a registry. */
|
|
9
|
+
export declare function isRegistryCompilationActive(): boolean;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
let registryCompiler;
|
|
2
|
+
let registryCompilationActive = false;
|
|
2
3
|
/** @internal Installed by Node development tooling, never by runtime bundles. */
|
|
3
4
|
export function setRegistryCompiler(compiler) {
|
|
4
5
|
registryCompiler = compiler;
|
|
@@ -6,3 +7,11 @@ export function setRegistryCompiler(compiler) {
|
|
|
6
7
|
export function getRegistryCompiler() {
|
|
7
8
|
return registryCompiler;
|
|
8
9
|
}
|
|
10
|
+
/** @internal Prevents registry evaluation from starting runtime resources. */
|
|
11
|
+
export function setRegistryCompilationActive(active) {
|
|
12
|
+
registryCompilationActive = active;
|
|
13
|
+
}
|
|
14
|
+
/** @internal True only while development tooling evaluates a registry. */
|
|
15
|
+
export function isRegistryCompilationActive() {
|
|
16
|
+
return registryCompilationActive;
|
|
17
|
+
}
|
|
@@ -4,12 +4,31 @@ import type { AgentRuntime, CompiledBundle, OrchaInitConfiguration } from "../ty
|
|
|
4
4
|
export interface Orcha {
|
|
5
5
|
init(configuration: OrchaInitConfiguration): OrchaClient;
|
|
6
6
|
isInitialized(): boolean;
|
|
7
|
+
/** Number of sessions whose local tail is waiting for cloud acknowledgement. */
|
|
8
|
+
readonly cloudStoragePendingCount: number;
|
|
9
|
+
/** Most recent background cloud storage error, if any. */
|
|
10
|
+
readonly cloudStorageLastError: Error | undefined;
|
|
11
|
+
/** Waits for cloud outboxes to drain, up to the supplied timeout. */
|
|
12
|
+
flushCloudStorage(options?: number | {
|
|
13
|
+
timeoutMs?: number;
|
|
14
|
+
}): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* Closes background runtime resources and makes a best-effort backup flush.
|
|
17
|
+
* Use flushCloudStorage() first when remote delivery must be enforced.
|
|
18
|
+
*/
|
|
19
|
+
close(options?: {
|
|
20
|
+
timeoutMs?: number;
|
|
21
|
+
}): Promise<void>;
|
|
7
22
|
/** Flushes queued Amarsia telemetry without blocking normal agent runs. */
|
|
8
23
|
flushTelemetry(options?: {
|
|
9
24
|
timeoutMs?: number;
|
|
10
25
|
}): Promise<void>;
|
|
11
26
|
/** @internal Used by the development compiler. */
|
|
12
27
|
getCompiledBundle(): CompiledBundle;
|
|
28
|
+
/** @internal Used to omit direct providers from inferred builds. */
|
|
29
|
+
isInferenceEnabled(): boolean;
|
|
30
|
+
/** @internal Used to include cloud storage only when selected. */
|
|
31
|
+
isCloudStorageEnabled(): boolean;
|
|
13
32
|
}
|
|
14
33
|
export type OrchaClient = Orcha & Record<string, AgentRuntime>;
|
|
15
34
|
export type BundleResolver = (configuration: OrchaInitConfiguration) => CompiledBundle;
|
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import { RuntimeAgent } from "./agent.js";
|
|
2
2
|
import { AmarsiaTelemetryClient, withAmarsiaTelemetry, } from "../amarsia/analytics.js";
|
|
3
|
+
import { isRegistryCompilationActive } from "./compiler-hook.js";
|
|
3
4
|
import { setOrchaRuntimeContext } from "./context.js";
|
|
5
|
+
import { createInferenceGenerator } from "./optional-features.js";
|
|
4
6
|
class OrchaRuntimeCore {
|
|
5
7
|
#agents = new Map();
|
|
6
8
|
#bundle;
|
|
9
|
+
#inferenceEnabled = false;
|
|
10
|
+
#cloudStorageEnabled = false;
|
|
7
11
|
#client;
|
|
8
12
|
#telemetry;
|
|
13
|
+
#stores = new Set();
|
|
14
|
+
#closing;
|
|
9
15
|
#activeSessions = new Set();
|
|
10
16
|
#sessionControls = new Map();
|
|
11
17
|
#resolveBundle;
|
|
@@ -20,16 +26,22 @@ class OrchaRuntimeCore {
|
|
|
20
26
|
this.#client = client;
|
|
21
27
|
}
|
|
22
28
|
init(configuration) {
|
|
23
|
-
if (this.#
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
for (const agent of this.#agents.values()) {
|
|
27
|
-
if (agent instanceof RuntimeAgent) {
|
|
28
|
-
void agent._close();
|
|
29
|
-
}
|
|
29
|
+
if (this.#bundle || this.#closing) {
|
|
30
|
+
throw new Error("Orcha is already initialized. Await orcha.close() before initializing it again.");
|
|
30
31
|
}
|
|
31
32
|
const projectRoot = this.#platform.projectRoot(configuration);
|
|
32
33
|
const bundle = configuration.bundle ?? this.#resolveBundle(configuration);
|
|
34
|
+
const inferenceEnabled = configuration.amarsia?.inference === true;
|
|
35
|
+
if (isRegistryCompilationActive()) {
|
|
36
|
+
this.#bundle = bundle;
|
|
37
|
+
this.#inferenceEnabled = inferenceEnabled;
|
|
38
|
+
this.#cloudStorageEnabled =
|
|
39
|
+
configuration.storage?.strategy === "cloud";
|
|
40
|
+
return this.#client;
|
|
41
|
+
}
|
|
42
|
+
if (inferenceEnabled && !configuration.amarsia?.key.trim()) {
|
|
43
|
+
throw new Error("Amarsia inference requires a non-empty amarsia.key.");
|
|
44
|
+
}
|
|
33
45
|
if (configuration.amarsia?.key && this.#platform.syncProject) {
|
|
34
46
|
this.#platform.syncProject({
|
|
35
47
|
configuration,
|
|
@@ -38,7 +50,7 @@ class OrchaRuntimeCore {
|
|
|
38
50
|
});
|
|
39
51
|
}
|
|
40
52
|
const runtimeConfiguration = {
|
|
41
|
-
providers: Object.fromEntries(Object.entries(configuration.providers).map(([name, provider]) => [
|
|
53
|
+
providers: Object.fromEntries(Object.entries(configuration.providers ?? {}).map(([name, provider]) => [
|
|
42
54
|
name,
|
|
43
55
|
typeof provider === "string" ? { apiKey: provider } : provider,
|
|
44
56
|
])),
|
|
@@ -49,6 +61,16 @@ class OrchaRuntimeCore {
|
|
|
49
61
|
mcp: configuration.mcp,
|
|
50
62
|
storage: configuration.storage,
|
|
51
63
|
};
|
|
64
|
+
if (inferenceEnabled) {
|
|
65
|
+
for (const provider of compiledProviderNames(bundle)) {
|
|
66
|
+
const providers = runtimeConfiguration.providers;
|
|
67
|
+
providers[provider] ??= { apiKey: "" };
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const inferenceGenerator = inferenceEnabled
|
|
71
|
+
? createInferenceGenerator(configuration.amarsia)
|
|
72
|
+
: undefined;
|
|
73
|
+
const generateProviderResponse = inferenceGenerator ?? this.#generateProviderResponse;
|
|
52
74
|
if (bundle.actionRuntime &&
|
|
53
75
|
runtimeConfiguration.actions?.runtime !== bundle.actionRuntime) {
|
|
54
76
|
throw new Error(`Production bundle expects local action runtime "${bundle.actionRuntime}".`);
|
|
@@ -64,9 +86,11 @@ class OrchaRuntimeCore {
|
|
|
64
86
|
this.#telemetry = telemetry;
|
|
65
87
|
const createRuntimeStore = (manifest) => {
|
|
66
88
|
const store = createStore(manifest);
|
|
67
|
-
|
|
89
|
+
const runtimeStore = telemetry
|
|
68
90
|
? withAmarsiaTelemetry({ store, manifest, client: telemetry })
|
|
69
91
|
: store;
|
|
92
|
+
this.#stores.add(runtimeStore);
|
|
93
|
+
return runtimeStore;
|
|
70
94
|
};
|
|
71
95
|
const agents = new Map();
|
|
72
96
|
for (const [name, manifest] of Object.entries(bundle.agents)) {
|
|
@@ -78,7 +102,7 @@ class OrchaRuntimeCore {
|
|
|
78
102
|
store: createRuntimeStore(subagentManifest),
|
|
79
103
|
activeSessions: this.#activeSessions,
|
|
80
104
|
sessionControls: this.#sessionControls,
|
|
81
|
-
generateProviderResponse
|
|
105
|
+
generateProviderResponse,
|
|
82
106
|
delegatedOnly: true,
|
|
83
107
|
projectRoot,
|
|
84
108
|
randomUUID: this.#platform.randomUUID,
|
|
@@ -90,26 +114,80 @@ class OrchaRuntimeCore {
|
|
|
90
114
|
store: createRuntimeStore(manifest),
|
|
91
115
|
activeSessions: this.#activeSessions,
|
|
92
116
|
sessionControls: this.#sessionControls,
|
|
93
|
-
generateProviderResponse
|
|
117
|
+
generateProviderResponse,
|
|
94
118
|
subagents,
|
|
95
119
|
projectRoot,
|
|
96
120
|
randomUUID: this.#platform.randomUUID,
|
|
97
121
|
}));
|
|
98
122
|
}
|
|
99
123
|
this.#bundle = bundle;
|
|
124
|
+
this.#inferenceEnabled = inferenceEnabled;
|
|
125
|
+
this.#cloudStorageEnabled =
|
|
126
|
+
configuration.storage?.strategy === "cloud";
|
|
100
127
|
this.#agents = agents;
|
|
101
128
|
setOrchaRuntimeContext(this.#client, {
|
|
102
129
|
projectRoot,
|
|
103
130
|
registrations: { ...configuration.agents },
|
|
104
131
|
bundle,
|
|
105
132
|
configuration: runtimeConfiguration,
|
|
106
|
-
generateProviderResponse
|
|
133
|
+
generateProviderResponse,
|
|
107
134
|
});
|
|
108
135
|
return this.#client;
|
|
109
136
|
}
|
|
110
137
|
isInitialized() {
|
|
111
138
|
return this.#bundle !== undefined;
|
|
112
139
|
}
|
|
140
|
+
get cloudStoragePendingCount() {
|
|
141
|
+
let pending = 0;
|
|
142
|
+
for (const store of this.#stores) {
|
|
143
|
+
pending += store.cloudStoragePendingCount ?? 0;
|
|
144
|
+
}
|
|
145
|
+
return pending;
|
|
146
|
+
}
|
|
147
|
+
get cloudStorageLastError() {
|
|
148
|
+
let error;
|
|
149
|
+
for (const store of this.#stores) {
|
|
150
|
+
error = store.cloudStorageLastError ?? error;
|
|
151
|
+
}
|
|
152
|
+
return error;
|
|
153
|
+
}
|
|
154
|
+
async flushCloudStorage(options = {}) {
|
|
155
|
+
const normalized = typeof options === "number" ? { timeoutMs: options } : options;
|
|
156
|
+
await Promise.all([...this.#stores].map((store) => store.flushCloudStorage?.(normalized)));
|
|
157
|
+
}
|
|
158
|
+
async close(options = {}) {
|
|
159
|
+
if (this.#closing)
|
|
160
|
+
return this.#closing;
|
|
161
|
+
const close = (async () => {
|
|
162
|
+
const backupOperations = [
|
|
163
|
+
this.#telemetry?.close(options),
|
|
164
|
+
...[...this.#stores].map((store) => store.closeCloudStorage?.(options)),
|
|
165
|
+
].filter((operation) => operation !== undefined);
|
|
166
|
+
const resourceOperations = [
|
|
167
|
+
...[...this.#agents.values()].flatMap((agent) => agent instanceof RuntimeAgent ? [agent._close()] : []),
|
|
168
|
+
];
|
|
169
|
+
const [, resourceResults] = await Promise.all([
|
|
170
|
+
Promise.allSettled(backupOperations),
|
|
171
|
+
Promise.allSettled(resourceOperations),
|
|
172
|
+
]);
|
|
173
|
+
const failure = resourceResults.find((result) => result.status === "rejected");
|
|
174
|
+
this.#stores.clear();
|
|
175
|
+
this.#agents.clear();
|
|
176
|
+
this.#telemetry = undefined;
|
|
177
|
+
this.#bundle = undefined;
|
|
178
|
+
this.#inferenceEnabled = false;
|
|
179
|
+
this.#cloudStorageEnabled = false;
|
|
180
|
+
if (failure)
|
|
181
|
+
throw failure.reason;
|
|
182
|
+
})();
|
|
183
|
+
this.#closing = close;
|
|
184
|
+
try {
|
|
185
|
+
await close;
|
|
186
|
+
}
|
|
187
|
+
finally {
|
|
188
|
+
this.#closing = undefined;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
113
191
|
async flushTelemetry(options) {
|
|
114
192
|
await this.#telemetry?.drain(options);
|
|
115
193
|
}
|
|
@@ -119,10 +197,29 @@ class OrchaRuntimeCore {
|
|
|
119
197
|
}
|
|
120
198
|
return this.#bundle;
|
|
121
199
|
}
|
|
200
|
+
isInferenceEnabled() {
|
|
201
|
+
return this.#inferenceEnabled;
|
|
202
|
+
}
|
|
203
|
+
isCloudStorageEnabled() {
|
|
204
|
+
return this.#cloudStorageEnabled;
|
|
205
|
+
}
|
|
122
206
|
agent(name) {
|
|
123
207
|
return this.#agents.get(name);
|
|
124
208
|
}
|
|
125
209
|
}
|
|
210
|
+
function compiledProviderNames(bundle) {
|
|
211
|
+
const names = new Set();
|
|
212
|
+
const visit = (manifest) => {
|
|
213
|
+
names.add(manifest.provider);
|
|
214
|
+
for (const evaluation of Object.values(manifest.evaluations ?? {})) {
|
|
215
|
+
if (evaluation.enabled !== false)
|
|
216
|
+
names.add(evaluation.provider);
|
|
217
|
+
}
|
|
218
|
+
Object.values(manifest.subagents ?? {}).forEach(visit);
|
|
219
|
+
};
|
|
220
|
+
Object.values(bundle.agents).forEach(visit);
|
|
221
|
+
return names;
|
|
222
|
+
}
|
|
126
223
|
export function createOrcha(resolveBundle, generateProviderResponse, platform) {
|
|
127
224
|
const core = new OrchaRuntimeCore(resolveBundle, generateProviderResponse, platform);
|
|
128
225
|
const client = new Proxy(core, {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ProviderResponseGenerator } from "../providers/types.js";
|
|
2
|
+
import type { SessionStore } from "../types.js";
|
|
3
|
+
export interface CloudSessionStoreFactoryOptions {
|
|
4
|
+
local: SessionStore;
|
|
5
|
+
key: string;
|
|
6
|
+
endpoint?: string;
|
|
7
|
+
agentName: string;
|
|
8
|
+
}
|
|
9
|
+
export type CloudSessionStoreFactory = (options: CloudSessionStoreFactoryOptions) => SessionStore;
|
|
10
|
+
export type InferenceGeneratorFactory = (configuration: {
|
|
11
|
+
key: string;
|
|
12
|
+
endpoint?: string;
|
|
13
|
+
}) => ProviderResponseGenerator;
|
|
14
|
+
/** @internal Registers the optional inference implementation. */
|
|
15
|
+
export declare function setInferenceGeneratorFactory(factory: InferenceGeneratorFactory): void;
|
|
16
|
+
/** @internal Registers the optional cloud storage implementation. */
|
|
17
|
+
export declare function setCloudSessionStoreFactory(factory: CloudSessionStoreFactory): void;
|
|
18
|
+
export declare function createInferenceGenerator(configuration: Parameters<InferenceGeneratorFactory>[0]): ProviderResponseGenerator;
|
|
19
|
+
export declare function createCloudSessionStore(options: CloudSessionStoreFactoryOptions): SessionStore;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
let inferenceGeneratorFactory;
|
|
2
|
+
let cloudSessionStoreFactory;
|
|
3
|
+
/** @internal Registers the optional inference implementation. */
|
|
4
|
+
export function setInferenceGeneratorFactory(factory) {
|
|
5
|
+
inferenceGeneratorFactory = factory;
|
|
6
|
+
}
|
|
7
|
+
/** @internal Registers the optional cloud storage implementation. */
|
|
8
|
+
export function setCloudSessionStoreFactory(factory) {
|
|
9
|
+
cloudSessionStoreFactory = factory;
|
|
10
|
+
}
|
|
11
|
+
export function createInferenceGenerator(configuration) {
|
|
12
|
+
if (!inferenceGeneratorFactory) {
|
|
13
|
+
throw new Error("Amarsia inference was not included in this Orcha runtime.");
|
|
14
|
+
}
|
|
15
|
+
return inferenceGeneratorFactory(configuration);
|
|
16
|
+
}
|
|
17
|
+
export function createCloudSessionStore(options) {
|
|
18
|
+
if (!cloudSessionStoreFactory) {
|
|
19
|
+
throw new Error("Cloud session storage was not included in this Orcha runtime.");
|
|
20
|
+
}
|
|
21
|
+
return cloudSessionStoreFactory(options);
|
|
22
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { BrowserIndexedDbSessionStore } from "../storage/browser-indexeddb.js";
|
|
2
|
+
import { createCloudSessionStore } from "./optional-features.js";
|
|
2
3
|
import { portableRandomUUID } from "./random-id.js";
|
|
3
4
|
export const browserRuntimePlatform = {
|
|
4
5
|
randomUUID: portableRandomUUID,
|
|
@@ -7,9 +8,21 @@ export const browserRuntimePlatform = {
|
|
|
7
8
|
},
|
|
8
9
|
createSessionStore({ configuration, manifest }) {
|
|
9
10
|
const strategy = configuration.storage?.strategy ?? "indexeddb";
|
|
10
|
-
if (strategy !== "indexeddb") {
|
|
11
|
-
throw new Error(`Browser runtimes require storage strategy "indexeddb", received "${strategy}".`);
|
|
11
|
+
if (strategy !== "indexeddb" && strategy !== "cloud") {
|
|
12
|
+
throw new Error(`Browser runtimes require storage strategy "indexeddb" or "cloud", received "${strategy}".`);
|
|
12
13
|
}
|
|
13
|
-
|
|
14
|
+
const local = new BrowserIndexedDbSessionStore(manifest.key ?? manifest.name, configuration.storage?.directory ?? "orchajs");
|
|
15
|
+
if (strategy !== "cloud") {
|
|
16
|
+
return local;
|
|
17
|
+
}
|
|
18
|
+
if (!configuration.amarsia?.key) {
|
|
19
|
+
throw new Error('Storage strategy "cloud" requires amarsia.key.');
|
|
20
|
+
}
|
|
21
|
+
return createCloudSessionStore({
|
|
22
|
+
local,
|
|
23
|
+
key: configuration.amarsia.key,
|
|
24
|
+
endpoint: configuration.amarsia.endpoint,
|
|
25
|
+
agentName: manifest.key ?? manifest.name,
|
|
26
|
+
});
|
|
14
27
|
},
|
|
15
28
|
};
|
|
@@ -2,6 +2,7 @@ import { randomUUID } from "node:crypto";
|
|
|
2
2
|
import { resolve } from "node:path";
|
|
3
3
|
import { syncAmarsiaProject } from "../amarsia/sync.js";
|
|
4
4
|
import { createSessionStore } from "../storage/create-store.js";
|
|
5
|
+
import { createCloudSessionStore } from "./optional-features.js";
|
|
5
6
|
export const nodeRuntimePlatform = {
|
|
6
7
|
randomUUID,
|
|
7
8
|
projectRoot(configuration) {
|
|
@@ -10,12 +11,25 @@ export const nodeRuntimePlatform = {
|
|
|
10
11
|
process.cwd());
|
|
11
12
|
},
|
|
12
13
|
createSessionStore({ configuration, projectRoot, manifest }) {
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
const strategy = configuration.storage?.strategy;
|
|
15
|
+
const local = createSessionStore({
|
|
16
|
+
strategy: strategy === "cloud" ? "node-jsonl" : strategy,
|
|
15
17
|
directory: configuration.storage?.directory,
|
|
16
18
|
projectRoot,
|
|
17
19
|
agentName: manifest.key ?? manifest.name,
|
|
18
20
|
});
|
|
21
|
+
if (strategy !== "cloud") {
|
|
22
|
+
return local;
|
|
23
|
+
}
|
|
24
|
+
if (!configuration.amarsia?.key) {
|
|
25
|
+
throw new Error('Storage strategy "cloud" requires amarsia.key.');
|
|
26
|
+
}
|
|
27
|
+
return createCloudSessionStore({
|
|
28
|
+
local,
|
|
29
|
+
key: configuration.amarsia.key,
|
|
30
|
+
endpoint: configuration.amarsia.endpoint,
|
|
31
|
+
agentName: manifest.key ?? manifest.name,
|
|
32
|
+
});
|
|
19
33
|
},
|
|
20
34
|
syncProject({ configuration, bundle, projectRoot }) {
|
|
21
35
|
if (!configuration.amarsia?.key) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ReactNativeJsonlSessionStore } from "../storage/react-native-jsonl.js";
|
|
2
|
+
import { createCloudSessionStore } from "./optional-features.js";
|
|
2
3
|
import { portableRandomUUID } from "./random-id.js";
|
|
3
4
|
export const reactNativeRuntimePlatform = {
|
|
4
5
|
randomUUID: portableRandomUUID,
|
|
@@ -7,9 +8,21 @@ export const reactNativeRuntimePlatform = {
|
|
|
7
8
|
},
|
|
8
9
|
createSessionStore({ configuration, manifest }) {
|
|
9
10
|
const strategy = configuration.storage?.strategy ?? "react-native";
|
|
10
|
-
if (strategy !== "react-native") {
|
|
11
|
-
throw new Error(`React Native requires storage strategy "react-native", received "${strategy}".`);
|
|
11
|
+
if (strategy !== "react-native" && strategy !== "cloud") {
|
|
12
|
+
throw new Error(`React Native requires storage strategy "react-native" or "cloud", received "${strategy}".`);
|
|
12
13
|
}
|
|
13
|
-
|
|
14
|
+
const local = new ReactNativeJsonlSessionStore(manifest.key ?? manifest.name);
|
|
15
|
+
if (strategy !== "cloud") {
|
|
16
|
+
return local;
|
|
17
|
+
}
|
|
18
|
+
if (!configuration.amarsia?.key) {
|
|
19
|
+
throw new Error('Storage strategy "cloud" requires amarsia.key.');
|
|
20
|
+
}
|
|
21
|
+
return createCloudSessionStore({
|
|
22
|
+
local,
|
|
23
|
+
key: configuration.amarsia.key,
|
|
24
|
+
endpoint: configuration.amarsia.endpoint,
|
|
25
|
+
agentName: manifest.key ?? manifest.name,
|
|
26
|
+
});
|
|
14
27
|
},
|
|
15
28
|
};
|