orchajs 0.26.0 → 0.27.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/dist/browser.d.ts +2 -1
- package/dist/browser.js +17 -3
- package/dist/cli.js +58 -9
- package/dist/compiler/build-project.js +49 -12
- package/dist/compiler/compile.js +12 -1
- package/dist/initialize-project.js +48 -0
- package/dist/integrations/metro.d.ts +1 -0
- package/dist/integrations/metro.js +1 -1
- package/dist/integrations/next.d.ts +1 -0
- package/dist/integrations/next.js +1 -1
- package/dist/integrations/runtime-build.js +40 -9
- package/dist/orcha.d.ts +3 -2
- package/dist/orcha.js +76 -18
- package/dist/project-loader.js +4 -5
- package/dist/react-native.d.ts +2 -1
- package/dist/react-native.js +17 -3
- package/dist/runtime/compiled-manifest.d.ts +6 -0
- package/dist/runtime/compiled-manifest.js +5 -0
- package/dist/runtime/compiler-hook.d.ts +4 -0
- package/dist/runtime/compiler-hook.js +24 -6
- package/dist/runtime/context.js +10 -3
- package/dist/storage/react-native-jsonl.js +66 -19
- package/dist-cjs/browser.js +22 -8
- package/dist-cjs/compiler/build-project.js +53 -10
- package/dist-cjs/compiler/compile.js +15 -2
- package/dist-cjs/initialize-project.js +45 -0
- package/dist-cjs/integrations/metro.js +1 -1
- package/dist-cjs/integrations/next.js +1 -1
- package/dist-cjs/integrations/runtime-build.js +44 -19
- package/dist-cjs/orcha.js +109 -19
- package/dist-cjs/project-loader.js +4 -2
- package/dist-cjs/react-native.js +24 -8
- package/dist-cjs/runtime/compiled-manifest.js +29 -0
- package/dist-cjs/runtime/compiler-hook.js +26 -6
- package/dist-cjs/runtime/context.js +10 -3
- package/dist-cjs/storage/react-native-jsonl.js +64 -29
- package/package.json +1 -1
package/dist/browser.d.ts
CHANGED
package/dist/browser.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { createAmarsiaInferenceGenerator } from "./amarsia/inference.js";
|
|
2
2
|
import { generateBrowserProviderResponse } from "./providers/browser.js";
|
|
3
3
|
import { setNativeRuntimeLoader } from "./runtime/actions/execute.js";
|
|
4
|
+
import { compiledManifest } from "./runtime/compiled-manifest.js";
|
|
5
|
+
import { getGlobalCompiledManifest } from "./runtime/compiler-hook.js";
|
|
4
6
|
import { createOrcha } from "./runtime/create-orcha.js";
|
|
5
7
|
import { setCloudSessionStoreFactory, setInferenceGeneratorFactory, } from "./runtime/optional-features.js";
|
|
6
8
|
import { browserRuntimePlatform } from "./runtime/platform-browser.js";
|
|
@@ -11,7 +13,19 @@ setNativeRuntimeLoader(async () => {
|
|
|
11
13
|
});
|
|
12
14
|
setInferenceGeneratorFactory(createAmarsiaInferenceGenerator);
|
|
13
15
|
setCloudSessionStoreFactory((options) => new CloudSessionStore(options));
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
const BROWSER_ORCHA_SINGLETON_KEY = Symbol.for("orchajs.browserRuntime");
|
|
17
|
+
function getSharedBrowserOrcha() {
|
|
18
|
+
const globalRecord = globalThis;
|
|
19
|
+
if (!globalRecord[BROWSER_ORCHA_SINGLETON_KEY]) {
|
|
20
|
+
globalRecord[BROWSER_ORCHA_SINGLETON_KEY] = createOrcha(() => {
|
|
21
|
+
const manifest = getGlobalCompiledManifest() ?? compiledManifest;
|
|
22
|
+
if (manifest) {
|
|
23
|
+
return manifest;
|
|
24
|
+
}
|
|
25
|
+
throw new Error('The browser runtime requires a compiled Orcha manifest. Run "npx orchajs dev" or "npx orchajs build".');
|
|
26
|
+
}, generateBrowserProviderResponse, browserRuntimePlatform);
|
|
27
|
+
}
|
|
28
|
+
return globalRecord[BROWSER_ORCHA_SINGLETON_KEY];
|
|
29
|
+
}
|
|
30
|
+
export const orcha = getSharedBrowserOrcha();
|
|
17
31
|
export * from "./runtime-entry.js";
|
package/dist/cli.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { existsSync } from "node:fs";
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
3
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
4
4
|
import { dirname, relative, resolve } from "node:path";
|
|
5
5
|
import { watch } from "chokidar";
|
|
@@ -43,12 +43,14 @@ switch (command) {
|
|
|
43
43
|
case "build": {
|
|
44
44
|
const parsed = parseArguments(commandArguments, new Set(["--target"]), new Set());
|
|
45
45
|
if (parsed.positionals.length > 0) {
|
|
46
|
-
throw new Error("Usage: orchajs build [--target node|browser|react-native]");
|
|
46
|
+
throw new Error("Usage: orchajs build [--target node|browser|react-native|all]");
|
|
47
47
|
}
|
|
48
|
-
const
|
|
48
|
+
const targets = parseBuildTargets(projectRoot, parsed.values.get("--target"));
|
|
49
49
|
loadProjectEnvironment(projectRoot);
|
|
50
|
-
const
|
|
51
|
-
|
|
50
|
+
for (const target of targets) {
|
|
51
|
+
const outputPath = await buildProject({ projectRoot, target });
|
|
52
|
+
console.log(`Built ${relative(projectRoot, outputPath)}`);
|
|
53
|
+
}
|
|
52
54
|
break;
|
|
53
55
|
}
|
|
54
56
|
case "help":
|
|
@@ -79,7 +81,9 @@ async function watchProject(projectRoot, targets) {
|
|
|
79
81
|
for (const target of targets) {
|
|
80
82
|
await buildProject({ projectRoot, target });
|
|
81
83
|
}
|
|
82
|
-
const
|
|
84
|
+
const manifestRaw = await readFile(resolve(projectRoot, ".orcha/dist/manifest.json"), "utf8");
|
|
85
|
+
const manifest = JSON.parse(manifestRaw);
|
|
86
|
+
const agents = Object.keys(manifest.agents ?? {});
|
|
83
87
|
console.log(`[orcha] valid — ${agents.length} agent${agents.length === 1 ? "" : "s"}: ${agents.join(", ") || "none"}`);
|
|
84
88
|
}
|
|
85
89
|
catch (error) {
|
|
@@ -300,11 +304,56 @@ function parseDevTargets(arguments_) {
|
|
|
300
304
|
if (parsed.positionals.length > 0) {
|
|
301
305
|
throw new Error("Usage: orchajs dev [--target node|browser|react-native|all]");
|
|
302
306
|
}
|
|
303
|
-
|
|
304
|
-
|
|
307
|
+
return parseBuildTargets(process.cwd(), parsed.values.get("--target"));
|
|
308
|
+
}
|
|
309
|
+
function parseBuildTargets(projectRoot, value) {
|
|
310
|
+
if (value === "all") {
|
|
305
311
|
return ["browser", "react-native", "node"];
|
|
306
312
|
}
|
|
307
|
-
|
|
313
|
+
if (value !== undefined) {
|
|
314
|
+
return [parseBuildTarget(value)];
|
|
315
|
+
}
|
|
316
|
+
return detectProjectTargets(projectRoot);
|
|
317
|
+
}
|
|
318
|
+
function detectProjectTargets(projectRoot) {
|
|
319
|
+
let dependencies = {};
|
|
320
|
+
const packagePath = resolve(projectRoot, "package.json");
|
|
321
|
+
if (existsSync(packagePath)) {
|
|
322
|
+
try {
|
|
323
|
+
const pkg = JSON.parse(readFileSync(packagePath, "utf8"));
|
|
324
|
+
dependencies = {
|
|
325
|
+
...(pkg.dependencies ?? {}),
|
|
326
|
+
...(pkg.devDependencies ?? {}),
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
catch {
|
|
330
|
+
// ignore invalid package.json
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
const hasNext = "next" in dependencies ||
|
|
334
|
+
existsSync(resolve(projectRoot, "next.config.ts")) ||
|
|
335
|
+
existsSync(resolve(projectRoot, "next.config.mjs")) ||
|
|
336
|
+
existsSync(resolve(projectRoot, "next.config.js"));
|
|
337
|
+
const hasBrowserApp = hasNext ||
|
|
338
|
+
"vite" in dependencies ||
|
|
339
|
+
"react-dom" in dependencies ||
|
|
340
|
+
existsSync(resolve(projectRoot, "vite.config.ts")) ||
|
|
341
|
+
existsSync(resolve(projectRoot, "vite.config.js")) ||
|
|
342
|
+
existsSync(resolve(projectRoot, "webpack.config.js"));
|
|
343
|
+
const hasReactNative = "react-native" in dependencies ||
|
|
344
|
+
"expo" in dependencies ||
|
|
345
|
+
existsSync(resolve(projectRoot, "metro.config.js"));
|
|
346
|
+
const targets = [];
|
|
347
|
+
if (hasBrowserApp) {
|
|
348
|
+
targets.push("browser");
|
|
349
|
+
}
|
|
350
|
+
if (hasReactNative) {
|
|
351
|
+
targets.push("react-native");
|
|
352
|
+
}
|
|
353
|
+
if (hasNext || targets.length === 0) {
|
|
354
|
+
targets.push("node");
|
|
355
|
+
}
|
|
356
|
+
return targets;
|
|
308
357
|
}
|
|
309
358
|
function parseBuildTarget(value) {
|
|
310
359
|
if (value === undefined ||
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { existsSync } from "node:fs";
|
|
1
|
+
import { existsSync, realpathSync } from "node:fs";
|
|
2
2
|
import { mkdir, rm, writeFile } from "node:fs/promises";
|
|
3
3
|
import { dirname, resolve } from "node:path";
|
|
4
4
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
5
5
|
import { build as bundleEntry } from "esbuild";
|
|
6
6
|
import { loadProjectEnvironment } from "../env.js";
|
|
7
7
|
import { orcha } from "../orcha.js";
|
|
8
|
-
import { setRegistryCompilationActive, setRegistryCompiler, } from "../runtime/compiler-hook.js";
|
|
8
|
+
import { setGlobalCompiledManifest, setRegistryCompilationActive, setRegistryCompiler, } from "../runtime/compiler-hook.js";
|
|
9
9
|
import { compileRegistry } from "./compile.js";
|
|
10
10
|
import { builtInProviderCatalog, isBuiltInProvider, } from "../providers/catalog.js";
|
|
11
11
|
import { ensureCompiledMcpDependencies, ensureProviderDependencies, } from "../providers/dependencies.js";
|
|
@@ -74,10 +74,11 @@ export async function buildProject(options = {}) {
|
|
|
74
74
|
compiledBundle.actionRuntime === "sandbox") {
|
|
75
75
|
throw new Error(`The ${target} target supports precompiled native actions only. Set actions.runtime to "native".`);
|
|
76
76
|
}
|
|
77
|
-
const
|
|
78
|
-
const
|
|
79
|
-
const
|
|
80
|
-
const
|
|
77
|
+
const compilerDirectory = esmCompilerDirectory();
|
|
78
|
+
const runtimeFactoryPath = resolve(compilerDirectory, "../runtime/create-orcha.js").replaceAll("\\", "/");
|
|
79
|
+
const runtimePlatformPath = resolve(compilerDirectory, `../runtime/platform-${target}.js`).replaceAll("\\", "/");
|
|
80
|
+
const runtimeEntryPath = resolve(compilerDirectory, "../runtime-entry.js").replaceAll("\\", "/");
|
|
81
|
+
const providerDispatcherPath = resolve(compilerDirectory, "../providers/dispatcher.js").replaceAll("\\", "/");
|
|
81
82
|
const sandboxRuntimeSource = compiledBundle.actionRuntime === "sandbox"
|
|
82
83
|
? createSandboxRuntimeSource()
|
|
83
84
|
: "";
|
|
@@ -159,6 +160,7 @@ export async function buildProject(options = {}) {
|
|
|
159
160
|
logLevel: "silent",
|
|
160
161
|
});
|
|
161
162
|
await writeFile(manifestPath, `${JSON.stringify(compiledBundle)}\n`, "utf8");
|
|
163
|
+
await syncPackageCompiledManifest(projectRoot, compiledBundle);
|
|
162
164
|
return outputPath;
|
|
163
165
|
}
|
|
164
166
|
finally {
|
|
@@ -172,6 +174,41 @@ export async function buildProject(options = {}) {
|
|
|
172
174
|
});
|
|
173
175
|
}
|
|
174
176
|
}
|
|
177
|
+
async function syncPackageCompiledManifest(projectRoot, bundle) {
|
|
178
|
+
setGlobalCompiledManifest(bundle);
|
|
179
|
+
try {
|
|
180
|
+
const ownPackageRoot = resolve(esmCompilerDirectory(), "../..");
|
|
181
|
+
const consumerPackageRoot = resolve(projectRoot, "node_modules/orchajs");
|
|
182
|
+
if (existsSync(consumerPackageRoot) &&
|
|
183
|
+
realpathSync(consumerPackageRoot) !== realpathSync(ownPackageRoot)) {
|
|
184
|
+
const serialized = JSON.stringify(bundle);
|
|
185
|
+
const esmContents = `export const compiledManifest = ${serialized};\n`;
|
|
186
|
+
const cjsContents = `"use strict";\nexports.compiledManifest = ${serialized};\n`;
|
|
187
|
+
const esmTarget = resolve(consumerPackageRoot, "dist/runtime/compiled-manifest.js");
|
|
188
|
+
const cjsTarget = resolve(consumerPackageRoot, "dist-cjs/runtime/compiled-manifest.js");
|
|
189
|
+
if (existsSync(dirname(esmTarget))) {
|
|
190
|
+
await writeFile(esmTarget, esmContents, "utf8").catch(() => undefined);
|
|
191
|
+
}
|
|
192
|
+
if (existsSync(dirname(cjsTarget))) {
|
|
193
|
+
await writeFile(cjsTarget, cjsContents, "utf8").catch(() => undefined);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
catch {
|
|
198
|
+
// Best-effort package manifest sync.
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
function esmCompilerDirectory() {
|
|
202
|
+
const currentDirectory = dirname(fileURLToPath(import.meta.url));
|
|
203
|
+
const normalized = currentDirectory.replaceAll("\\", "/");
|
|
204
|
+
if (normalized.endsWith("/dist-cjs/compiler")) {
|
|
205
|
+
const esmCandidate = resolve(currentDirectory, "../../dist/compiler");
|
|
206
|
+
if (existsSync(esmCandidate)) {
|
|
207
|
+
return esmCandidate;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return currentDirectory;
|
|
211
|
+
}
|
|
175
212
|
function bundleFileName(target) {
|
|
176
213
|
return target === "node" ? "bundle.js" : `bundle.${target}.js`;
|
|
177
214
|
}
|
|
@@ -185,7 +222,7 @@ function runtimePlatformExport(target) {
|
|
|
185
222
|
return "nodeRuntimePlatform";
|
|
186
223
|
}
|
|
187
224
|
function createSandboxRuntimeSource() {
|
|
188
|
-
const runtimeDirectory =
|
|
225
|
+
const runtimeDirectory = esmCompilerDirectory();
|
|
189
226
|
const executePath = resolve(runtimeDirectory, "../runtime/actions/execute.js").replaceAll("\\", "/");
|
|
190
227
|
const sandboxPath = resolve(runtimeDirectory, "../runtime/actions/sandbox.js").replaceAll("\\", "/");
|
|
191
228
|
return [
|
|
@@ -196,7 +233,7 @@ function createSandboxRuntimeSource() {
|
|
|
196
233
|
].join("\n");
|
|
197
234
|
}
|
|
198
235
|
function createPlatformActionRuntimeSource(target) {
|
|
199
|
-
const runtimeDirectory =
|
|
236
|
+
const runtimeDirectory = esmCompilerDirectory();
|
|
200
237
|
const executePath = resolve(runtimeDirectory, "../runtime/actions/execute.js").replaceAll("\\", "/");
|
|
201
238
|
const nativePath = resolve(runtimeDirectory, target === "node"
|
|
202
239
|
? "../runtime/actions/native.js"
|
|
@@ -209,7 +246,7 @@ function createPlatformActionRuntimeSource(target) {
|
|
|
209
246
|
].join("\n");
|
|
210
247
|
}
|
|
211
248
|
function createMcpRuntimeSource(target) {
|
|
212
|
-
const runtimeDirectory =
|
|
249
|
+
const runtimeDirectory = esmCompilerDirectory();
|
|
213
250
|
const clientPath = resolve(runtimeDirectory, "../runtime/mcp/client.js").replaceAll("\\", "/");
|
|
214
251
|
const implementationPath = resolve(runtimeDirectory, target === "node"
|
|
215
252
|
? "../runtime/mcp/node-client.js"
|
|
@@ -226,7 +263,7 @@ function createOptionalFeatureRuntimeSource(options) {
|
|
|
226
263
|
if (!options.inference && !options.cloudStorage) {
|
|
227
264
|
return "";
|
|
228
265
|
}
|
|
229
|
-
const sourceRoot =
|
|
266
|
+
const sourceRoot = esmCompilerDirectory();
|
|
230
267
|
const hooksPath = resolve(sourceRoot, "../runtime/optional-features.js").replaceAll("\\", "/");
|
|
231
268
|
const imports = [];
|
|
232
269
|
const registrations = [];
|
|
@@ -243,7 +280,7 @@ function createOptionalFeatureRuntimeSource(options) {
|
|
|
243
280
|
return [...imports, ...registrations, ""].join("\n");
|
|
244
281
|
}
|
|
245
282
|
function createNodeStorageExports() {
|
|
246
|
-
const sourceRoot =
|
|
283
|
+
const sourceRoot = esmCompilerDirectory();
|
|
247
284
|
const jsonlPath = resolve(sourceRoot, "../storage/node-jsonl.js").replaceAll("\\", "/");
|
|
248
285
|
const sqlitePath = resolve(sourceRoot, "../storage/node-sqlite.js").replaceAll("\\", "/");
|
|
249
286
|
return [
|
|
@@ -259,7 +296,7 @@ function allCompiledAgents(agents) {
|
|
|
259
296
|
]);
|
|
260
297
|
}
|
|
261
298
|
function createProviderRuntimeSource(providerNames, target) {
|
|
262
|
-
const providersDirectory = resolve(
|
|
299
|
+
const providersDirectory = resolve(esmCompilerDirectory(), "../providers");
|
|
263
300
|
const imports = [];
|
|
264
301
|
const entries = [];
|
|
265
302
|
for (const [index, name] of providerNames.entries()) {
|
package/dist/compiler/compile.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { existsSync, readFileSync, readdirSync, statSync, } from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
3
4
|
import { resolve } from "node:path";
|
|
4
|
-
import { buildSync } from "esbuild";
|
|
5
5
|
import { isBuiltInProvider } from "../providers/catalog.js";
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
6
7
|
export function compileRegistry(registrations, registryRoot, actionRuntime = "native", target = "node") {
|
|
7
8
|
if (!registrations ||
|
|
8
9
|
typeof registrations !== "object" ||
|
|
@@ -534,11 +535,21 @@ function validateEvaluationConfiguration(agentName, registrationName, configurat
|
|
|
534
535
|
names.add(metric.name);
|
|
535
536
|
}
|
|
536
537
|
}
|
|
538
|
+
function loadEsbuildSync() {
|
|
539
|
+
try {
|
|
540
|
+
const esbuildModule = require("esbuild");
|
|
541
|
+
return esbuildModule.buildSync;
|
|
542
|
+
}
|
|
543
|
+
catch (error) {
|
|
544
|
+
throw new Error('Compiling local actions on the fly requires "esbuild". Install it with "npm install --save-dev esbuild" or run "npx orchajs build" before deploying.', { cause: error });
|
|
545
|
+
}
|
|
546
|
+
}
|
|
537
547
|
function compileLocalAction(agentName, directoryName, actionsDirectory, actionRuntime, target) {
|
|
538
548
|
const entryPath = resolve(actionsDirectory, directoryName, "index.js");
|
|
539
549
|
if (!existsSync(entryPath)) {
|
|
540
550
|
throw new Error(`Local action "${agentName}/${directoryName}" requires index.js.`);
|
|
541
551
|
}
|
|
552
|
+
const buildSync = loadEsbuildSync();
|
|
542
553
|
let output;
|
|
543
554
|
try {
|
|
544
555
|
const result = buildSync({
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
2
|
import { appendFile, mkdir, readFile, writeFile, } from "node:fs/promises";
|
|
3
3
|
import { dirname, resolve } from "node:path";
|
|
4
|
+
import { buildProject } from "./compiler/build-project.js";
|
|
4
5
|
import { AGENTS_MD } from "./cli-agents-template.js";
|
|
5
6
|
export async function initializeProject(projectRoot) {
|
|
6
7
|
const files = new Map([
|
|
@@ -49,8 +50,55 @@ export async function initializeProject(projectRoot) {
|
|
|
49
50
|
console.log(`Created ${relativePath}`);
|
|
50
51
|
}
|
|
51
52
|
await ensureGitignore(projectRoot);
|
|
53
|
+
await ensurePackageScripts(projectRoot);
|
|
54
|
+
await buildProject({ projectRoot }).catch(() => undefined);
|
|
52
55
|
console.log('\nRun "orchajs dev" to validate changes or "orchajs run exampleAgent --input \\"Hello\\"" to execute the agent.');
|
|
53
56
|
}
|
|
57
|
+
async function ensurePackageScripts(projectRoot) {
|
|
58
|
+
const packagePath = resolve(projectRoot, "package.json");
|
|
59
|
+
if (!existsSync(packagePath)) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
const raw = await readFile(packagePath, "utf8");
|
|
64
|
+
const parsed = JSON.parse(raw);
|
|
65
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const scripts = { ...(parsed.scripts ?? {}) };
|
|
69
|
+
let changed = false;
|
|
70
|
+
if (!scripts.build) {
|
|
71
|
+
scripts.build = "orchajs build";
|
|
72
|
+
changed = true;
|
|
73
|
+
}
|
|
74
|
+
else if (!scripts.build.includes("orchajs build")) {
|
|
75
|
+
if (!scripts.prebuild) {
|
|
76
|
+
scripts.prebuild = "orchajs build";
|
|
77
|
+
changed = true;
|
|
78
|
+
}
|
|
79
|
+
else if (!scripts.prebuild.includes("orchajs build")) {
|
|
80
|
+
scripts.prebuild = `orchajs build && ${scripts.prebuild}`;
|
|
81
|
+
changed = true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (scripts.dev &&
|
|
85
|
+
!scripts.dev.includes("orchajs") &&
|
|
86
|
+
!scripts.predev?.includes("orchajs build")) {
|
|
87
|
+
scripts.predev = scripts.predev
|
|
88
|
+
? `orchajs build && ${scripts.predev}`
|
|
89
|
+
: "orchajs build";
|
|
90
|
+
changed = true;
|
|
91
|
+
}
|
|
92
|
+
if (changed) {
|
|
93
|
+
parsed.scripts = scripts;
|
|
94
|
+
await writeFile(packagePath, `${JSON.stringify(parsed, null, 2)}\n`, "utf8");
|
|
95
|
+
console.log("Updated package.json scripts");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
// Best-effort package.json script update.
|
|
100
|
+
}
|
|
101
|
+
}
|
|
54
102
|
async function ensureGitignore(projectRoot) {
|
|
55
103
|
const path = resolve(projectRoot, ".gitignore");
|
|
56
104
|
const entry = ".orcha/";
|
|
@@ -3,7 +3,7 @@ import { generatedBundlePath, queueRuntimeBuild, watchRuntimeTargets, } from "./
|
|
|
3
3
|
export async function withOrchaMetro(configuration, options = {}) {
|
|
4
4
|
const projectRoot = resolve(options.projectRoot ?? process.cwd());
|
|
5
5
|
await queueRuntimeBuild(projectRoot, "react-native");
|
|
6
|
-
if (process.env.NODE_ENV !== "production") {
|
|
6
|
+
if (options.watch !== false && process.env.NODE_ENV !== "production") {
|
|
7
7
|
watchRuntimeTargets(projectRoot, ["react-native"]);
|
|
8
8
|
}
|
|
9
9
|
const existingResolver = configuration.resolver?.resolveRequest;
|
|
@@ -5,7 +5,7 @@ export async function withOrcha(configuration = {}, options = {}) {
|
|
|
5
5
|
const projectRoot = resolve(options.projectRoot ?? process.cwd());
|
|
6
6
|
await queueRuntimeBuild(projectRoot, "browser");
|
|
7
7
|
await queueRuntimeBuild(projectRoot, "node");
|
|
8
|
-
if (process.env.NODE_ENV !== "production") {
|
|
8
|
+
if (options.watch !== false && process.env.NODE_ENV !== "production") {
|
|
9
9
|
watchRuntimeTargets(projectRoot, ["browser", "node"]);
|
|
10
10
|
}
|
|
11
11
|
const existingWebpack = configuration.webpack;
|
|
@@ -1,32 +1,63 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
1
3
|
import { resolve } from "node:path";
|
|
2
|
-
import { watch } from "chokidar";
|
|
3
4
|
import { buildProject } from "../compiler/build-project.js";
|
|
4
5
|
import { loadProjectEnvironment } from "../env.js";
|
|
5
|
-
|
|
6
|
-
const
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
7
|
+
const RUNTIME_BUILD_STATE_KEY = Symbol.for("orchajs.runtimeBuildState");
|
|
8
|
+
function getRuntimeBuildState() {
|
|
9
|
+
const globalRecord = globalThis;
|
|
10
|
+
if (!globalRecord[RUNTIME_BUILD_STATE_KEY]) {
|
|
11
|
+
globalRecord[RUNTIME_BUILD_STATE_KEY] = {
|
|
12
|
+
buildQueue: Promise.resolve(),
|
|
13
|
+
watchedTargets: new Set(),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
return globalRecord[RUNTIME_BUILD_STATE_KEY];
|
|
17
|
+
}
|
|
7
18
|
export function generatedBundlePath(projectRoot, target) {
|
|
8
19
|
const fileName = target === "node" ? "bundle.js" : `bundle.${target}.js`;
|
|
9
20
|
return resolve(projectRoot, ".orcha/dist", fileName);
|
|
10
21
|
}
|
|
11
22
|
export function queueRuntimeBuild(projectRoot, target) {
|
|
12
|
-
const
|
|
23
|
+
const state = getRuntimeBuildState();
|
|
24
|
+
const build = state.buildQueue.then(async () => {
|
|
13
25
|
loadProjectEnvironment(projectRoot);
|
|
14
26
|
await buildProject({ projectRoot, target });
|
|
15
27
|
});
|
|
16
|
-
buildQueue = build.catch(() => undefined);
|
|
28
|
+
state.buildQueue = build.catch(() => undefined);
|
|
17
29
|
return build;
|
|
18
30
|
}
|
|
19
31
|
export function watchRuntimeTargets(projectRoot, targets) {
|
|
32
|
+
const state = getRuntimeBuildState();
|
|
20
33
|
const key = `${projectRoot}:${targets.join(",")}`;
|
|
21
|
-
if (watchedTargets.has(key)) {
|
|
34
|
+
if (state.watchedTargets.has(key)) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
let watchFn;
|
|
38
|
+
try {
|
|
39
|
+
const chokidarModule = require("chokidar");
|
|
40
|
+
watchFn = chokidarModule.watch;
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
22
43
|
return;
|
|
23
44
|
}
|
|
24
|
-
watchedTargets.add(key);
|
|
45
|
+
state.watchedTargets.add(key);
|
|
25
46
|
let timer;
|
|
26
|
-
|
|
47
|
+
const watcher = watchFn(resolve(projectRoot, "orcha"), {
|
|
48
|
+
ignoreInitial: true,
|
|
49
|
+
persistent: false,
|
|
50
|
+
});
|
|
51
|
+
watcher.on("all", () => {
|
|
27
52
|
clearTimeout(timer);
|
|
28
53
|
timer = setTimeout(() => {
|
|
29
|
-
|
|
54
|
+
if (!existsSync(resolve(projectRoot, "orcha/index.ts"))) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
void targets
|
|
58
|
+
.reduce((pending, target) => pending.then(() => queueRuntimeBuild(projectRoot, target)), Promise.resolve())
|
|
59
|
+
.catch(() => undefined);
|
|
30
60
|
}, 75);
|
|
61
|
+
timer.unref?.();
|
|
31
62
|
});
|
|
32
63
|
}
|
package/dist/orcha.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
1
|
+
import { type Orcha, type OrchaClient } from "./runtime/create-orcha.js";
|
|
2
|
+
export type { Orcha, OrchaClient };
|
|
3
|
+
export declare const orcha: OrchaClient;
|
package/dist/orcha.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
-
import { resolve } from "node:path";
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, realpathSync, writeFileSync, } from "node:fs";
|
|
2
|
+
import { dirname, resolve } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
3
4
|
import { createAmarsiaInferenceGenerator } from "./amarsia/inference.js";
|
|
5
|
+
import { compileRegistry } from "./compiler/compile.js";
|
|
4
6
|
import { generateProviderResponse } from "./providers/index.js";
|
|
5
7
|
import { setNativeRuntimeLoader, setSandboxRuntimeLoader, } from "./runtime/actions/execute.js";
|
|
6
8
|
import { executeNativeAction } from "./runtime/actions/native.js";
|
|
7
|
-
import { getRegistryCompiler } from "./runtime/compiler-hook.js";
|
|
8
|
-
import { createOrcha } from "./runtime/create-orcha.js";
|
|
9
|
+
import { getRegistryCompiler, setGlobalCompiledManifest, } from "./runtime/compiler-hook.js";
|
|
10
|
+
import { createOrcha, } from "./runtime/create-orcha.js";
|
|
9
11
|
import { setMcpClientFactory } from "./runtime/mcp/client.js";
|
|
10
12
|
import { setCloudSessionStoreFactory, setInferenceGeneratorFactory, } from "./runtime/optional-features.js";
|
|
11
13
|
import { nodeRuntimePlatform } from "./runtime/platform-node.js";
|
|
@@ -22,30 +24,80 @@ setMcpClientFactory(async (server, credentials) => {
|
|
|
22
24
|
});
|
|
23
25
|
setInferenceGeneratorFactory(createAmarsiaInferenceGenerator);
|
|
24
26
|
setCloudSessionStoreFactory((options) => new CloudSessionStore(options));
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
const NODE_ORCHA_SINGLETON_KEY = Symbol.for("orchajs.runtime");
|
|
28
|
+
function getSharedNodeOrcha() {
|
|
29
|
+
const globalRecord = globalThis;
|
|
30
|
+
if (!globalRecord[NODE_ORCHA_SINGLETON_KEY]) {
|
|
31
|
+
globalRecord[NODE_ORCHA_SINGLETON_KEY] = createOrcha((configuration) => {
|
|
32
|
+
const projectRoot = resolve(configuration.root ??
|
|
33
|
+
process.env.ORCHA_PROJECT_ROOT ??
|
|
34
|
+
process.cwd());
|
|
35
|
+
const compiler = getRegistryCompiler();
|
|
36
|
+
if (process.env.ORCHA_COMPILING === "1" && compiler) {
|
|
37
|
+
return compiler(configuration, projectRoot);
|
|
38
|
+
}
|
|
39
|
+
const precompiled = isPrecompiledRuntime();
|
|
40
|
+
const manifestFile = compiledManifestPath(projectRoot);
|
|
41
|
+
if (precompiled || existsSync(manifestFile)) {
|
|
42
|
+
return loadCompiledManifest(projectRoot, configuration);
|
|
43
|
+
}
|
|
44
|
+
const registryRoot = resolve(projectRoot, "orcha");
|
|
45
|
+
if (existsSync(registryRoot)) {
|
|
46
|
+
const compiled = compileRegistry(configuration.agents, registryRoot, configuration.actions?.runtime ?? "native", "node");
|
|
47
|
+
persistDevManifest(projectRoot, manifestFile, compiled);
|
|
48
|
+
return compiled;
|
|
49
|
+
}
|
|
50
|
+
throw new Error('Orcha agents could not be found in "orcha/" and no precompiled manifest exists at ".orcha/dist/manifest.json". Run "npx orchajs init" or "npx orchajs build".');
|
|
51
|
+
}, generateProviderResponse, nodeRuntimePlatform);
|
|
32
52
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
throw new Error('Orcha agents must be compiled before runtime initialization. Run "npx orchajs dev" during development or "npx orchajs build" before starting the application.');
|
|
38
|
-
}, generateProviderResponse, nodeRuntimePlatform);
|
|
53
|
+
return globalRecord[NODE_ORCHA_SINGLETON_KEY];
|
|
54
|
+
}
|
|
55
|
+
export const orcha = getSharedNodeOrcha();
|
|
39
56
|
function isPrecompiledRuntime() {
|
|
40
57
|
return (process.env.ORCHA_USE_BUILD === "1" ||
|
|
41
58
|
process.env.NODE_ENV === "production" ||
|
|
42
59
|
process.env.AWS_LAMBDA_FUNCTION_NAME !== undefined ||
|
|
43
60
|
process.env.AWS_EXECUTION_ENV?.startsWith("AWS_Lambda_") === true);
|
|
44
61
|
}
|
|
62
|
+
function persistDevManifest(projectRoot, manifestPath, bundle) {
|
|
63
|
+
setGlobalCompiledManifest(bundle);
|
|
64
|
+
try {
|
|
65
|
+
mkdirSync(dirname(manifestPath), { recursive: true });
|
|
66
|
+
writeFileSync(manifestPath, `${JSON.stringify(bundle)}\n`, "utf8");
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
// Best-effort cache write in development environments.
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
const ownPackageRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
73
|
+
const consumerPackageRoot = resolve(projectRoot, "node_modules/orchajs");
|
|
74
|
+
if (existsSync(consumerPackageRoot) &&
|
|
75
|
+
realpathSync(consumerPackageRoot) !== realpathSync(ownPackageRoot)) {
|
|
76
|
+
const serialized = JSON.stringify(bundle);
|
|
77
|
+
const esmTarget = resolve(consumerPackageRoot, "dist/runtime/compiled-manifest.js");
|
|
78
|
+
const cjsTarget = resolve(consumerPackageRoot, "dist-cjs/runtime/compiled-manifest.js");
|
|
79
|
+
if (existsSync(dirname(esmTarget))) {
|
|
80
|
+
writeFileSync(esmTarget, `export const compiledManifest = ${serialized};\n`, "utf8");
|
|
81
|
+
}
|
|
82
|
+
if (existsSync(dirname(cjsTarget))) {
|
|
83
|
+
writeFileSync(cjsTarget, `"use strict";\nexports.compiledManifest = ${serialized};\n`, "utf8");
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
// Best-effort package manifest sync in development environments.
|
|
89
|
+
}
|
|
90
|
+
}
|
|
45
91
|
function loadCompiledManifest(projectRoot, configuration) {
|
|
46
92
|
const manifestPath = compiledManifestPath(projectRoot);
|
|
47
93
|
if (!existsSync(manifestPath)) {
|
|
48
|
-
|
|
94
|
+
const registryRoot = resolve(projectRoot, "orcha");
|
|
95
|
+
if (existsSync(registryRoot)) {
|
|
96
|
+
const compiled = compileRegistry(configuration.agents, registryRoot, configuration.actions?.runtime ?? "native", "node");
|
|
97
|
+
persistDevManifest(projectRoot, manifestPath, compiled);
|
|
98
|
+
return compiled;
|
|
99
|
+
}
|
|
100
|
+
throw new Error(`ORCHA_PRODUCTION_BUNDLE_MISSING: "${manifestPath}" was not found. Run "npx orchajs build" before packaging, include ".orcha/dist" in the deployment, or keep the "orcha/" directory available at runtime.`);
|
|
49
101
|
}
|
|
50
102
|
let parsed;
|
|
51
103
|
try {
|
|
@@ -69,6 +121,12 @@ function loadCompiledManifest(projectRoot, configuration) {
|
|
|
69
121
|
const compiledAgents = Object.keys(bundle.agents).sort();
|
|
70
122
|
if (registeredAgents.length !== compiledAgents.length ||
|
|
71
123
|
registeredAgents.some((name, index) => name !== compiledAgents[index])) {
|
|
124
|
+
const registryRoot = resolve(projectRoot, "orcha");
|
|
125
|
+
if (!isPrecompiledRuntime() && existsSync(registryRoot)) {
|
|
126
|
+
const recompiled = compileRegistry(configuration.agents, registryRoot, configuration.actions?.runtime ?? "native", "node");
|
|
127
|
+
persistDevManifest(projectRoot, manifestPath, recompiled);
|
|
128
|
+
return recompiled;
|
|
129
|
+
}
|
|
72
130
|
throw new Error(`Orcha compiled manifest does not match the registered agents. Run "npx orchajs build" again. Expected [${registeredAgents.join(", ")}], found [${compiledAgents.join(", ")}].`);
|
|
73
131
|
}
|
|
74
132
|
return bundle;
|
package/dist/project-loader.js
CHANGED
|
@@ -19,9 +19,9 @@ export async function loadProject(projectRoot) {
|
|
|
19
19
|
const bundlePath = resolve(temporaryDirectory, `registry-${process.pid}-${Date.now()}.mjs`);
|
|
20
20
|
await mkdir(temporaryDirectory, { recursive: true });
|
|
21
21
|
try {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
if (orcha.isInitialized()) {
|
|
23
|
+
await orcha.close();
|
|
24
|
+
}
|
|
25
25
|
await bundleEntry({
|
|
26
26
|
entryPoints: [registryPath],
|
|
27
27
|
outfile: bundlePath,
|
|
@@ -43,8 +43,7 @@ export async function loadProject(projectRoot) {
|
|
|
43
43
|
restoreEnvironment("ORCHA_PROJECT_ROOT", previousRoot);
|
|
44
44
|
restoreEnvironment("ORCHA_COMPILING", previousCompiling);
|
|
45
45
|
}
|
|
46
|
-
if (!orcha.isInitialized()
|
|
47
|
-
orcha.getCompiledBundle() === previousBundle) {
|
|
46
|
+
if (!orcha.isInitialized()) {
|
|
48
47
|
throw new Error('"orcha/index.ts" must call orcha.init({ agents, ... }).');
|
|
49
48
|
}
|
|
50
49
|
if (!orcha.isInferenceEnabled()) {
|
package/dist/react-native.d.ts
CHANGED