Skip to content

An open-source, faster Devin alternative

TypeScript

fix: package imported agent source modules (#687)

Mohamed Habibdd1bba4052e3
diff --git a/cli/package-lock.json b/cli/package-lock.json
index 9575b215..cc6ae019 100644
--- a/cli/package-lock.json
+++ b/cli/package-lock.json
@@ -1,16 +1,17 @@
{
"name": "@opencomputer/cli",
- "version": "0.6.4",
+ "version": "0.6.5",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@opencomputer/cli",
- "version": "0.6.4",
+ "version": "0.6.5",
"dependencies": {
"@opencode-ai/sdk": "1.18.4",
"ai": "^7.0.45",
"croner": "^9.1.0",
+ "esbuild": "^0.28.2",
"opencode-ai": "1.18.4",
"react": "^19.2.8",
"typescript": "^5.9.0"
@@ -156,6 +157,422 @@
"tslib": "^2.4.0"
}
},
+ "node_modules/@esbuild/aix-ppc64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.28.2.tgz",
+ "integrity": "sha512-XExcO+dvLKvVtNTibSTBej1NCAbaGhWn9Ww1ZPx80qsahhPFe/8jgWP0IchNe0F3HwkU7n8ejhH8bjonqht8mQ==",
+ "cpu": [
+ "ppc64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "aix"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/android-arm": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.28.2.tgz",
+ "integrity": "sha512-kXXoiPVVGQcnIYGOeaovwOURpniDBpSq4A03qkQ+BMQqtGG6HYap3xne9C1O1yo4TR3qxlCX5IqqmX6fFo2Lqg==",
+ "cpu": [
+ "arm"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "android"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/android-arm64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.28.2.tgz",
+ "integrity": "sha512-5YfKeeI8qWfBZIX+u2xZC3Zlb3Os/gLS2sbEKM+I4ZOcsWmHS2WLysCcQZDAFRslDUU5Oiq44gf6PYN1vGwG5A==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "android"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/android-x64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.28.2.tgz",
+ "integrity": "sha512-O387ite7SzUyCcy3JQX4P4bLtEA7bLLkx+esve5JHnyYfNTxcVpXZo9jhdB0lTKN44gztELTdU7nS8Nr16Fs1Q==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "android"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/darwin-arm64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.28.2.tgz",
+ "integrity": "sha512-n4KqkOQrraxHJcgjM1RvwbigfQKIKJVpM7xp+KsxiyUSrRdIXnt73VhrPAx0fV44hgfmIVKjxMN9J1t5jySVkw==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "darwin"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/darwin-x64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.28.2.tgz",
+ "integrity": "sha512-uq6suIWYP37qzGddBKPw5QEQPi6HiLGsO7UmkpfyaYNQ3D+rN6w6WfwH+nuqcGXWvawGwxOEroO4YGnFh95azw==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "darwin"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/freebsd-arm64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.28.2.tgz",
+ "integrity": "sha512-n+I0BTSRIoy+d6RPKnEVwql5UwBJolytvY4mAOIEJorKlqgPII8ix6slVVrfZ5Tnj7glIZvloylbB/EJPMWEXw==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "freebsd"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/freebsd-x64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.28.2.tgz",
+ "integrity": "sha512-78XJTJkvPs0kz2w61301PJjXl4g7q3JqiYMZ/M/yVI73EHBrCRTgkhu9oqG7vPqq+a/yadEW8aD+agKlk5xrmg==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "freebsd"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/linux-arm": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.28.2.tgz",
+ "integrity": "sha512-XlDnu2q5yoqems+xay6wSAcg9DDD7K9RLKZEBOMZm3ckNpJBvOX20tSfby8KfrrhINDyv9V2YVZKY/SpoGJI8w==",
+ "cpu": [
+ "arm"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/linux-arm64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.28.2.tgz",
+ "integrity": "sha512-pW4AC0P3it8c7do9MVM4p51FzHzdM/TZrerurgRcHJ2WTa1VQ1CIq18xncfpBJw4ojkiZZrKW2yIBWBP92j6Ug==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/linux-ia32": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.28.2.tgz",
+ "integrity": "sha512-CYbnj78HsIeA+DhgUKgFCfvNsTHFhMMrinUrMZpDXJXKN8T3XViTZ/+wtHeVxEWY8ewSzTFN+nRmSwO2tZaLUQ==",
+ "cpu": [
+ "ia32"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/linux-loong64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.28.2.tgz",
+ "integrity": "sha512-buwkd8nsph4R+ajRvw0qM5Hja/TXQow3ptzWO2EbG/cqcIkHloRrdlBtQlshyYGTNFvfkfJ5tpPLVkY4DtsPfQ==",
+ "cpu": [
+ "loong64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/linux-mips64el": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.28.2.tgz",
+ "integrity": "sha512-ZVykbDyk7519VwiNb9Lcj9m8XM6v5V9uKPvrEMkkEedVewf+0itkhahp4HDpgERXhwLRpWFypsGbG/J8s0QjJA==",
+ "cpu": [
+ "mips64el"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/linux-ppc64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.28.2.tgz",
+ "integrity": "sha512-CAXl+Dtd9UUuJd8pKKdwh6MLm3MUMiqMPmhZ3tTSXPqfyQ3vDl6R5hZdZ/kYojK4ofXtdfSv1tFq8XzWx3heNQ==",
+ "cpu": [
+ "ppc64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/linux-riscv64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.28.2.tgz",
+ "integrity": "sha512-GeXCej4IQtU1B+QlDV8W/RRvbzI3O/Stss+/bCXv4lZls5WGRtu2a+3JkA3i4qIUlMXpcHebWpF8AkJhATowuA==",
+ "cpu": [
+ "riscv64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/linux-s390x": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.28.2.tgz",
+ "integrity": "sha512-3H1weTYZPxt/WOhByszQZybS9w5lKzUn1FDMsgEChbHWQwHYQQRfBxgCcZvPhjHfKyJjIievvMmEUawJrdY9Dg==",
+ "cpu": [
+ "s390x"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/linux-x64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.28.2.tgz",
+ "integrity": "sha512-4xTZr1FUmSoQW4XIWmit3tzQrUTZM+N3P0XV8xROKYF50XfI7xeO90+1bZvNwxIufQ9hDQVRJH5YhgPVF8A/HQ==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/netbsd-arm64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.28.2.tgz",
+ "integrity": "sha512-sSATRjPeDBg3pdgHoQfoYBob11Kk1FGa9lui5RIHZCoCkJa9QKlvl3/vKz2usCmYYjs7ymJR/2Nnsqe+Hjt5nw==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "netbsd"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/netbsd-x64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.28.2.tgz",
+ "integrity": "sha512-lqnzCV+mM0gIADaKihiCg6ifgfU2L3h5E33rNQBN1Y4MaVGnzryzmvvf7UHxprpQdE8hpqLolJ9Rl+SkIRDpyw==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "netbsd"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/openbsd-arm64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.28.2.tgz",
+ "integrity": "sha512-AL2qJILH7lNjrDmCQDvdxMfAUIv8KMNZOvrwAQ8i8//ntL9FflhOyMJ8OZSMBb8/AWXe3/5v5S20y3zCoZWKoQ==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "openbsd"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/openbsd-x64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.28.2.tgz",
+ "integrity": "sha512-QtiuPytchRyC4rwUKhexJdQKvDuZ6hWloi3igqPQNUJCS1/v9EiO3UTOXR6A3FoMo4fnAKbWJdqaIwhOzh8qEw==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "openbsd"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/openharmony-arm64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.28.2.tgz",
+ "integrity": "sha512-WkhYDmpTjLvGlScA1rwjRUmhl4k8oXR3cIbtqWmELgU/dFeHHlEllxDvdWcNJV9rbzCexB5vz8gtNewWLgCT7Q==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "openharmony"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/sunos-x64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.28.2.tgz",
+ "integrity": "sha512-GPMSkTOtMnv2U2F8gxe4Io6qmVs+YKyp832Etqqxr0hFngmXQ3rzwytelm3GIn7T4VviRUlf3sOgBOiTdvaf7g==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "sunos"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/win32-arm64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.28.2.tgz",
+ "integrity": "sha512-PIhhEkE9uPBleRBrQEJpUn7MBnibZzbGzYWPmY3x+YoVg/95zbjB4CxPPOQ8l5tYYM4mMaCthF8/1DIfBQQyWQ==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "win32"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/win32-ia32": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.28.2.tgz",
+ "integrity": "sha512-YmJbfTlvU7Sdn9BB+4PRES4oB6pxgS37MAONj+hBr/cpXS1aBPKXxNnDbu+QCWPj0o9dgyxeq79g6c5P8KeuYA==",
+ "cpu": [
+ "ia32"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "win32"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/win32-x64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.28.2.tgz",
+ "integrity": "sha512-5ebpxr3nWMzrL/rnUI755Jkuee0bHL/Gq0WTF9lvcpv73wAp5eu8MfBUgWK9bhWvZjj7yX8etf/8tI8Ney695g==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "win32"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
"node_modules/@napi-rs/wasm-runtime": {
"version": "1.2.2",
"resolved": "https://registry.npmjs.org/@napi-rs/wasm-runtime/-/wasm-runtime-1.2.2.tgz",
@@ -822,6 +1239,47 @@
"url": "https://github.com/sponsors/wooorm"
}
},
+ "node_modules/esbuild": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.28.2.tgz",
+ "integrity": "sha512-HKVLS8dvII+xoKW9kmqxbRKrnWEXfJJr/FZhhJmiqIB0e053QNYFqOBouTMO/k5sID4MvCiUCvv8b9M4h32wIA==",
+ "hasInstallScript": true,
+ "license": "MIT",
+ "bin": {
+ "esbuild": "bin/esbuild"
+ },
+ "engines": {
+ "node": ">=18"
+ },
+ "optionalDependencies": {
+ "@esbuild/aix-ppc64": "0.28.2",
+ "@esbuild/android-arm": "0.28.2",
+ "@esbuild/android-arm64": "0.28.2",
+ "@esbuild/android-x64": "0.28.2",
+ "@esbuild/darwin-arm64": "0.28.2",
+ "@esbuild/darwin-x64": "0.28.2",
+ "@esbuild/freebsd-arm64": "0.28.2",
+ "@esbuild/freebsd-x64": "0.28.2",
+ "@esbuild/linux-arm": "0.28.2",
+ "@esbuild/linux-arm64": "0.28.2",
+ "@esbuild/linux-ia32": "0.28.2",
+ "@esbuild/linux-loong64": "0.28.2",
+ "@esbuild/linux-mips64el": "0.28.2",
+ "@esbuild/linux-ppc64": "0.28.2",
+ "@esbuild/linux-riscv64": "0.28.2",
+ "@esbuild/linux-s390x": "0.28.2",
+ "@esbuild/linux-x64": "0.28.2",
+ "@esbuild/netbsd-arm64": "0.28.2",
+ "@esbuild/netbsd-x64": "0.28.2",
+ "@esbuild/openbsd-arm64": "0.28.2",
+ "@esbuild/openbsd-x64": "0.28.2",
+ "@esbuild/openharmony-arm64": "0.28.2",
+ "@esbuild/sunos-x64": "0.28.2",
+ "@esbuild/win32-arm64": "0.28.2",
+ "@esbuild/win32-ia32": "0.28.2",
+ "@esbuild/win32-x64": "0.28.2"
+ }
+ },
"node_modules/estree-util-is-identifier-name": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/estree-util-is-identifier-name/-/estree-util-is-identifier-name-3.0.0.tgz",
diff --git a/cli/package.json b/cli/package.json
index f7695586..dd38bb5a 100644
--- a/cli/package.json
+++ b/cli/package.json
@@ -1,6 +1,6 @@
{
"name": "@opencomputer/cli",
- "version": "0.6.4",
+ "version": "0.6.5",
"description": "Build, test, deploy, and share OpenComputer agents as code.",
"type": "module",
"bin": {
@@ -46,6 +46,7 @@
"@opencode-ai/sdk": "1.18.4",
"ai": "^7.0.45",
"croner": "^9.1.0",
+ "esbuild": "^0.28.2",
"opencode-ai": "1.18.4",
"react": "^19.2.8",
"typescript": "^5.9.0"
diff --git a/cli/src/project.test.ts b/cli/src/project.test.ts
index ca56ab70..1c218f75 100644
--- a/cli/src/project.test.ts
+++ b/cli/src/project.test.ts
@@ -685,13 +685,91 @@ export default function Agent() {
) as { tools: string[]; toolModules: string[] };
assert.ok(manifest.tools.includes("hacker_news"));
assert.ok(manifest.toolModules.includes("../tools/hacker-news.js"));
- assert.match(
- await readFile(resolve(runtime, "tools", "hacker-news.js"), "utf8"),
- /from "\.\.\/opencomputer-agent\.js"/,
+ const tools = await import(
+ `${pathToFileURL(resolve(runtime, "tools", "hacker-news.js")).href}?test=${crypto.randomUUID()}`
+ ) as { hackerNews: { id: string } };
+ assert.equal(tools.hackerNews.id, "hacker_news");
+ } finally {
+ await rm(parent, { recursive: true, force: true });
+ }
+});
+
+test("the compiler packages agent source modules outside the tools directory", async () => {
+ const parent = await mkdtemp(resolve(tmpdir(), "opencomputer-source-modules-"));
+ const root = resolve(parent, "app");
+ try {
+ const initialized = await initializeAgentProject(root);
+ await writeFile(
+ resolve(initialized.agentRoot, "config.ts"),
+ `import { defineConnection } from "@opencomputer/agent";
+import settings from "./settings.json";
+
+export const github = defineConnection({
+ id: "fixture-github",
+ origin: "https://api.github.com",
+ methods: ["GET"],
+ pathPrefix: "/repos/opencomputer/example/",
+});
+export const repository = settings.repository;
+`,
);
- assert.match(
- await readFile(resolve(runtime, "agent.js"), "utf8"),
- /useTool\(hackerNews\)/,
+ await writeFile(
+ resolve(initialized.agentRoot, "settings.json"),
+ `${JSON.stringify({ repository: "opencomputer/example" })}\n`,
+ );
+ await writeFile(
+ resolve(initialized.agentRoot, "agent.ts"),
+ `import { repository } from "./config";
+
+export default function Agent() {
+ return \`Review missing tests in \${repository}.\`;
+}
+`,
+ );
+
+ const built = await buildAgentArtifact(initialized.agentRoot);
+ const artifact = JSON.parse(built.body.toString("utf8")) as {
+ files: Array<{ path: string }>;
+ };
+ assert.ok(!artifact.files.some((file) => file.path === "config.js"));
+ assert.ok(!artifact.files.some((file) => file.path === "settings.json"));
+ assert.ok(artifact.files.some((file) => file.path === "agent.js"));
+ assert.ok(built.connections.includes("fixture-github"));
+ const packaged = await import(
+ `${pathToFileURL(resolve(initialized.agentRoot, ".opencomputer", "runtime", "agent.js")).href}?test=${crypto.randomUUID()}`
+ ) as { default(): string };
+ assert.equal(
+ packaged.default(),
+ "Review missing tests in opencomputer/example.",
+ );
+ } finally {
+ await rm(parent, { recursive: true, force: true });
+ }
+});
+
+test("the compiler rejects imports outside the agent directory", async () => {
+ const parent = await mkdtemp(
+ resolve(tmpdir(), "opencomputer-source-boundary-"),
+ );
+ const root = resolve(parent, "app");
+ try {
+ const initialized = await initializeAgentProject(root);
+ await writeFile(
+ resolve(initialized.agentRoot, "..", "outside.ts"),
+ "export const value = 1;\n",
+ );
+ await writeFile(
+ resolve(initialized.agentRoot, "agent.ts"),
+ `import { value } from "../outside";
+
+export default function Agent() {
+ return String(value);
+}
+`,
+ );
+ await assert.rejects(
+ buildAgentArtifact(initialized.agentRoot),
+ /must stay inside the agent directory/,
);
} finally {
await rm(parent, { recursive: true, force: true });
diff --git a/cli/src/project.ts b/cli/src/project.ts
index 6076a509..894c9a6e 100644
--- a/cli/src/project.ts
+++ b/cli/src/project.ts
@@ -10,6 +10,7 @@ import {
} from "node:fs/promises";
import { basename, dirname, relative, resolve } from "node:path";
import { Cron } from "croner";
+import { build as bundle } from "esbuild";
import ts from "typescript";
export interface AgentManifest {
@@ -1730,6 +1731,90 @@ export const useSessionData = (key) => hooks().useSessionData(key);
`;
}
+interface AgentSourceModule {
+ path: string;
+ source: string;
+}
+
+const AGENT_SOURCE_MODULE_PATTERN = /\.[cm]?[jt]sx?$/;
+
+async function bundleAgentSourceModules(
+ root: string,
+ runtime: string,
+ entries: string[],
+): Promise<{ modules: AgentSourceModule[]; entryPaths: Set<string> }> {
+ const entryPoints: Record<string, string> = {};
+ const entryPaths = new Set<string>();
+ for (const entry of entries) {
+ const path = relative(root, resolve(entry)).split("\\").join("/");
+ const outputPath = compiledModulePath(path);
+ const outputName = outputPath.replace(/\.js$/, "");
+ if (entryPoints[outputName]) {
+ throw new Error(`Agent source modules emit the same path: ${outputPath}`);
+ }
+ entryPoints[outputName] = resolve(entry);
+ entryPaths.add(path);
+ }
+ const result = await bundle({
+ absWorkingDir: root,
+ bundle: true,
+ entryPoints,
+ format: "esm",
+ logLevel: "silent",
+ metafile: true,
+ outdir: runtime,
+ platform: "node",
+ target: "node22",
+ write: false,
+ plugins: [
+ {
+ name: "opencomputer-agent-runtime",
+ setup(build) {
+ build.onResolve(
+ { filter: /^@opencomputer\/agent$/ },
+ () => ({ path: "api", namespace: "opencomputer-agent-runtime" }),
+ );
+ build.onLoad(
+ { filter: /^api$/, namespace: "opencomputer-agent-runtime" },
+ () => ({ contents: agentApiRuntimeSource(), loader: "js" }),
+ );
+ },
+ },
+ ],
+ });
+
+ const modules: AgentSourceModule[] = [];
+ for (const input of Object.keys(result.metafile.inputs)) {
+ if (input === "opencomputer-agent-runtime:api") continue;
+ const absolute = resolve(root, input);
+ const path = relative(root, absolute).split("\\").join("/");
+ if (!path || path === ".." || path.startsWith("../")) {
+ throw new Error(
+ "Agent source modules must stay inside the agent directory",
+ );
+ }
+ if (AGENT_SOURCE_MODULE_PATTERN.test(path)) {
+ modules.push({ path, source: await readFile(absolute, "utf8") });
+ }
+ }
+ for (const output of result.outputFiles) {
+ const path = relative(runtime, output.path).split("\\").join("/");
+ if (!path || path === ".." || path.startsWith("../")) {
+ throw new Error("Agent compiler emitted a file outside the runtime");
+ }
+ await mkdir(dirname(output.path), { recursive: true });
+ await writeFile(output.path, output.contents);
+ }
+ return {
+ modules: modules.sort((left, right) => left.path.localeCompare(right.path)),
+ entryPaths,
+ };
+}
+
+function compiledModulePath(path: string): string {
+ return path === "agent.ts" ? "agent.js" : path.replace(/\.[^.]+$/, ".js");
+}
+
export async function prepareAgent(root: string): Promise<string> {
const runtime = resolve(root, ".opencomputer", "runtime");
await rm(runtime, { recursive: true, force: true });
@@ -1825,55 +1910,28 @@ the product or support surface presented to users.
resolve(runtime, "package.json"),
`${JSON.stringify({ private: true, type: "module" }, null, 2)}\n`,
);
- const transpile = (source: string, filename: string) =>
- ts.transpileModule(source, {
- fileName: filename,
- compilerOptions: {
- target: ts.ScriptTarget.ES2022,
- module: ts.ModuleKind.ESNext,
- moduleResolution: ts.ModuleResolutionKind.Bundler,
- },
- reportDiagnostics: true,
- });
- const compiledAgent = transpile(agentSource, "agent.ts");
- const diagnostics = compiledAgent.diagnostics ?? [];
- if (
- diagnostics.some(
- (diagnostic) => diagnostic.category === ts.DiagnosticCategory.Error,
- )
- ) {
- throw new Error(
- `agent.ts could not be compiled: ${diagnostics
- .map((diagnostic) =>
- ts.flattenDiagnosticMessageText(diagnostic.messageText, " "),
- )
- .join("; ")}`,
- );
- }
- const compiledSource = compiledAgent.outputText.replace(
- /(["'])@opencomputer\/agent\1/g,
- '"./opencomputer-agent.js"',
- );
- await writeFile(resolve(runtime, "agent.js"), compiledSource);
await writeFile(
resolve(runtime, "opencomputer-agent.js"),
agentApiRuntimeSource(),
);
- const toolSources: Array<{ filename: string; source: string }> = [];
+ const sourceEntries = [resolve(root, "agent.ts")];
const sourceTools = resolve(root, "tools");
if (await exists(sourceTools)) {
const entries = await readdir(sourceTools, { withFileTypes: true });
for (const entry of entries) {
if (!entry.isFile() || !/\.[cm]?[jt]s$/.test(entry.name)) continue;
- toolSources.push({
- filename: entry.name,
- source: await readFile(resolve(sourceTools, entry.name), "utf8"),
- });
+ sourceEntries.push(resolve(sourceTools, entry.name));
}
}
+ const bundled = await bundleAgentSourceModules(root, runtime, sourceEntries);
+ const sourceModules = bundled.modules;
+ const toolSources = sourceModules.filter(
+ (candidate) =>
+ bundled.entryPaths.has(candidate.path) &&
+ candidate.path.startsWith("tools/"),
+ );
const reactiveTools: string[] = [];
const toolModules: string[] = [];
- await mkdir(resolve(runtime, "tools"), { recursive: true });
for (const candidate of toolSources) {
const ids = definedToolIds(candidate.source);
const calls = [
@@ -1881,33 +1939,12 @@ the product or support surface presented to users.
].length;
if (ids.length !== calls) {
throw new Error(
- `${candidate.filename} must give every defineTool() a literal string name`,
+ `${candidate.path} must give every defineTool() a literal string name`,
);
}
- const compiledTool = transpile(candidate.source, candidate.filename);
- const toolDiagnostics = compiledTool.diagnostics ?? [];
- if (
- toolDiagnostics.some(
- (diagnostic) => diagnostic.category === ts.DiagnosticCategory.Error,
- )
- ) {
- throw new Error(
- `${candidate.filename} could not be compiled: ${toolDiagnostics
- .map((diagnostic) =>
- ts.flattenDiagnosticMessageText(diagnostic.messageText, " "),
- )
- .join("; ")}`,
- );
- }
- const outputName = candidate.filename.replace(/\.[^.]+$/, ".js");
- const output = compiledTool.outputText.replace(
- /(["'])@opencomputer\/agent\1/g,
- '"../opencomputer-agent.js"',
- );
- await writeFile(resolve(runtime, "tools", outputName), output);
if (ids.length > 0) {
reactiveTools.push(...ids);
- toolModules.push(`../tools/${outputName}`);
+ toolModules.push(`../${compiledModulePath(candidate.path)}`);
}
}
const duplicateTool = reactiveTools.find(
@@ -1918,14 +1955,8 @@ the product or support surface presented to users.
`Tool id ${JSON.stringify(duplicateTool)} is defined more than once`,
);
}
- const httpConnections = [
- agentSource,
- ...toolSources.map((item) => item.source),
- ].flatMap((source, index) =>
- definedHttpConnections(
- source,
- index === 0 ? "agent.ts" : toolSources[index - 1]!.filename,
- ),
+ const httpConnections = sourceModules.flatMap((module) =>
+ definedHttpConnections(module.source, module.path),
);
const duplicateConnection = httpConnections.find(
(connection, index) =>
@@ -1939,12 +1970,7 @@ the product or support surface presented to users.
);
}
const connectionBindings = new Map<string, HttpConnectionManifest>();
- for (const [index, source] of [
- agentSource,
- ...toolSources.map((item) => item.source),
- ].entries()) {
- const filename =
- index === 0 ? "agent.ts" : toolSources[index - 1]!.filename;
+ for (const { path: filename, source } of sourceModules) {
for (const [name, definition] of definedConnectionBindings(
source,
filename,
@@ -1958,10 +1984,8 @@ the product or support surface presented to users.
connectionBindings.set(name, definition);
}
}
- const mcpServerDefinitions = definedMcpServers(
- agentSource,
- "agent.ts",
- connectionBindings,
+ const mcpServerDefinitions = sourceModules.flatMap((module) =>
+ definedMcpServers(module.source, module.path, connectionBindings),
);
const duplicateMcpServer = mcpServerDefinitions.find(
(server, index) =>
diff --git a/create-start/package.json b/create-start/package.json
index 5c4995d8..d846d71e 100644
--- a/create-start/package.json
+++ b/create-start/package.json
@@ -1,6 +1,6 @@
{
"name": "@opencomputer/create-start",
- "version": "0.6.4",
+ "version": "0.6.5",
"description": "Create a hello-world OpenComputer agent application.",
"type": "module",
"bin": {
@@ -18,7 +18,7 @@
"node": ">=22.0.0"
},
"dependencies": {
- "@opencomputer/cli": "0.6.4"
+ "@opencomputer/cli": "0.6.5"
},
"publishConfig": {
"access": "public"