Skip to content

Commit e30df72

Browse files
authored
fix(worker): keep source bundles npm-installable (#122430)
1 parent f6459a3 commit e30df72

4 files changed

Lines changed: 81 additions & 6 deletions

File tree

‎packages/ai/package.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,13 @@
101101
"@anthropic-ai/sdk": "0.115.0",
102102
"@google/genai": "2.13.0",
103103
"@mistralai/mistralai": "2.5.0",
104-
"@openclaw/normalization-core": "workspace:*",
105104
"openai": "6.49.0",
106105
"partial-json": "0.1.7",
107106
"typebox": "1.3.6"
108107
},
108+
"devDependencies": {
109+
"@openclaw/normalization-core": "workspace:*"
110+
},
109111
"engines": {
110112
"node": ">=22.19.0"
111113
},

‎packages/ai/src/package-dependencies.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async function productionImportsPackage(packageName: string): Promise<boolean> {
4444
}
4545

4646
describe("@openclaw/ai source dependency contract", () => {
47-
it("declares normalization-core while production source imports it", async () => {
47+
it("declares bundled normalization-core imports as a workspace dev dependency", async () => {
4848
const manifest = JSON.parse(
4949
await fs.readFile(path.join(PACKAGE_ROOT, "package.json"), "utf8"),
5050
) as {
@@ -53,6 +53,7 @@ describe("@openclaw/ai source dependency contract", () => {
5353
};
5454

5555
expect(await productionImportsPackage("@openclaw/normalization-core")).toBe(true);
56-
expect(manifest.dependencies?.["@openclaw/normalization-core"]).toBe("workspace:*");
56+
expect(manifest.dependencies?.["@openclaw/normalization-core"]).toBeUndefined();
57+
expect(manifest.devDependencies?.["@openclaw/normalization-core"]).toBe("workspace:*");
5758
});
5859
});

‎pnpm-lock.yaml‎

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/gateway/worker-environments/bundle.test.ts‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from "node:fs/promises";
22
import path from "node:path";
33
import * as tar from "tar";
44
import { describe, expect, it, vi } from "vitest";
5+
import { runCommandWithTimeout } from "../../process/exec.js";
56
import { withTestDir } from "../../test-helpers/temp-dir.js";
67
import {
78
createWorkerBundleProducer,
@@ -246,6 +247,76 @@ describe("worker bundle producer", () => {
246247
});
247248
});
248249

250+
it("installs a source bundle when the AI workspace import is bundled", async () => {
251+
await withTestDir({ prefix: "openclaw-worker-bundle-npm-install-" }, async (root) => {
252+
const repoRoot = path.resolve(import.meta.dirname, "../../..");
253+
const aiManifest = JSON.parse(
254+
await fs.readFile(path.join(repoRoot, "packages/ai/package.json"), "utf8"),
255+
) as {
256+
dependencies?: Record<string, string>;
257+
devDependencies?: Record<string, string>;
258+
};
259+
const dependencyFields = (["dependencies", "devDependencies"] as const).filter(
260+
(field) => aiManifest[field]?.["@openclaw/normalization-core"] !== undefined,
261+
);
262+
if (dependencyFields.length !== 1) {
263+
throw new Error(
264+
"@openclaw/ai must classify normalization-core in exactly one dependency field",
265+
);
266+
}
267+
const dependencyField = dependencyFields[0]!;
268+
const normalizationCoreSpec = aiManifest[dependencyField]?.["@openclaw/normalization-core"];
269+
if (!normalizationCoreSpec?.startsWith("workspace:")) {
270+
throw new Error("@openclaw/ai must use a workspace normalization-core dependency");
271+
}
272+
const packageRoot = path.join(root, "package");
273+
await writeFixture(packageRoot, [["dist/entry.js", 'import "@openclaw/ai";\nexport {};\n']]);
274+
await fs.writeFile(
275+
path.join(packageRoot, "package.json"),
276+
`${JSON.stringify({
277+
name: "openclaw",
278+
version: "1.2.3",
279+
type: "module",
280+
files: ["dist/"],
281+
dependencies: { "@openclaw/ai": "workspace:*" },
282+
})}\n`,
283+
"utf8",
284+
);
285+
const vendorSource = path.join(packageRoot, "node_modules/@openclaw/ai");
286+
await fs.mkdir(path.join(vendorSource, "dist"), { recursive: true });
287+
await fs.writeFile(
288+
path.join(vendorSource, "package.json"),
289+
`${JSON.stringify({
290+
name: "@openclaw/ai",
291+
version: "1.2.3",
292+
type: "module",
293+
main: "./dist/index.js",
294+
[dependencyField]: { "@openclaw/normalization-core": normalizationCoreSpec },
295+
})}\n`,
296+
"utf8",
297+
);
298+
await fs.writeFile(path.join(vendorSource, "dist/index.js"), "export {};\n", "utf8");
299+
300+
const bundle = await createWorkerBundleProducer({
301+
packageRoot,
302+
cacheDir: path.join(root, "cache"),
303+
}).prepare();
304+
const extractRoot = path.join(root, "extract");
305+
await fs.mkdir(extractRoot);
306+
await tar.extract({ file: bundle.tarballPath, cwd: extractRoot });
307+
308+
const install = await runCommandWithTimeout(
309+
["npm", "install", "--ignore-scripts", "--omit=dev", "--no-audit", "--no-fund"],
310+
{
311+
cwd: extractRoot,
312+
env: { NPM_CONFIG_CACHE: path.join(root, "npm-cache") },
313+
timeoutMs: 30_000,
314+
},
315+
);
316+
expect(install.code, install.stderr).toBe(0);
317+
});
318+
});
319+
249320
it("fails closed when a dist-referenced workspace package is not installed", async () => {
250321
await withTestDir({ prefix: "openclaw-worker-bundle-vendor-missing-" }, async (root) => {
251322
const packageRoot = path.join(root, "package");

0 commit comments

Comments
 (0)