Skip to content

Commit b8a0fa2

Browse files
authored
fix(plugins): derive media aliases from exports (#122523)
1 parent 5a643e3 commit b8a0fa2

4 files changed

Lines changed: 94 additions & 45 deletions

File tree

‎src/plugins/plugin-sdk-native-resolver.test.ts‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,23 @@ function writeInternalCorePackageSource(
9393
return sourcePath;
9494
}
9595

96+
function writeInternalCorePackageExports(
97+
root: string,
98+
packageDir: string,
99+
subpaths: readonly string[],
100+
): void {
101+
writeJsonFile(path.join(root, "packages", packageDir, "package.json"), {
102+
name: `@openclaw/${packageDir}`,
103+
exports: Object.fromEntries(
104+
subpaths.map((subpath) => {
105+
const exportKey = subpath ? `./${subpath}` : ".";
106+
const distFile = `./dist/${subpath || "index"}.mjs`;
107+
return [exportKey, { import: distFile, default: distFile }];
108+
}),
109+
),
110+
});
111+
}
112+
96113
function addFakePluginSdkDistExport(root: string, subpath: string): string {
97114
const packageJsonPath = path.join(root, "package.json");
98115
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")) as {
@@ -521,7 +538,19 @@ describe("installOpenClawPluginSdkNativeResolver", () => {
521538
);
522539
const resultSource = writeInternalCorePackageSource(root, "normalization-core", "result.ts");
523540
const agentIdSource = writeInternalCorePackageSource(root, "normalization-core", "agent-id.ts");
524-
const mediaCoreSource = writeInternalCorePackageSource(root, "media-core", "mime.ts");
541+
writeInternalCorePackageExports(root, "normalization-core", [
542+
"agent-id",
543+
"boolean-coercion",
544+
"result",
545+
"string-coerce",
546+
]);
547+
writeInternalCorePackageExports(root, "media-core", ["attachment-classify", "mime"]);
548+
const mediaMimeSource = writeInternalCorePackageSource(root, "media-core", "mime.ts");
549+
const mediaAttachmentClassifySource = writeInternalCorePackageSource(
550+
root,
551+
"media-core",
552+
"attachment-classify.ts",
553+
);
525554
const markdownCoreSource = writeInternalCorePackageSource(
526555
root,
527556
"markdown-core",
@@ -543,6 +572,7 @@ describe("installOpenClawPluginSdkNativeResolver", () => {
543572
"acp-core",
544573
path.join("runtime", "types.ts"),
545574
);
575+
writeInternalCorePackageExports(root, "acp-core", ["runtime/types"]);
546576
const llmCoreSource = writeInternalCorePackageSource(root, "llm-core", "index.ts");
547577
const externalPluginEntry = writeExternalPluginEntry(path.join(root, "external-plugin"));
548578
const coreSourceParent = path.join(root, "src", "config", "plugin-web-search-config.ts");
@@ -560,6 +590,7 @@ describe("installOpenClawPluginSdkNativeResolver", () => {
560590
expect(installedAliases).toContain("@openclaw/normalization-core/result");
561591
expect(installedAliases).toContain("@openclaw/normalization-core/agent-id");
562592
expect(installedAliases).toContain("@openclaw/media-core/mime");
593+
expect(installedAliases).toContain("@openclaw/media-core/attachment-classify");
563594
expect(installedAliases).toContain("@openclaw/markdown-core/code-spans");
564595
expect(installedAliases).toContain("@openclaw/ai/transports");
565596
expect(installedAliases).toContain("@openclaw/ai/internal/retry-after");
@@ -583,8 +614,11 @@ describe("installOpenClawPluginSdkNativeResolver", () => {
583614
fs.realpathSync(requireFromCoreSource.resolve("@openclaw/normalization-core/agent-id")),
584615
).toBe(fs.realpathSync(agentIdSource));
585616
expect(fs.realpathSync(requireFromCoreSource.resolve("@openclaw/media-core/mime"))).toBe(
586-
fs.realpathSync(mediaCoreSource),
617+
fs.realpathSync(mediaMimeSource),
587618
);
619+
expect(
620+
fs.realpathSync(requireFromCoreSource.resolve("@openclaw/media-core/attachment-classify")),
621+
).toBe(fs.realpathSync(mediaAttachmentClassifySource));
588622
expect(
589623
fs.realpathSync(requireFromCoreSource.resolve("@openclaw/markdown-core/code-spans")),
590624
).toBe(fs.realpathSync(markdownCoreSource));
@@ -609,6 +643,7 @@ describe("installOpenClawPluginSdkNativeResolver", () => {
609643
).toThrow();
610644
expect(() => requireFromPlugin.resolve("@openclaw/normalization-core/result")).toThrow();
611645
expect(() => requireFromPlugin.resolve("@openclaw/media-core/mime")).toThrow();
646+
expect(() => requireFromPlugin.resolve("@openclaw/media-core/attachment-classify")).toThrow();
612647
expect(() => requireFromPlugin.resolve("@openclaw/markdown-core/code-spans")).toThrow();
613648
expect(() => requireFromPlugin.resolve("@openclaw/ai/transports")).toThrow();
614649
expect(() => requireFromPlugin.resolve("@openclaw/ai/internal/retry-after")).toThrow();

‎src/plugins/plugin-sdk-native-resolver.ts‎

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,6 @@ const INTERNAL_CORE_PACKAGE_ALIASES = [
9191
["internal/shared", path.join("internal", "shared.ts")],
9292
],
9393
},
94-
{
95-
packageName: "@openclaw/media-core",
96-
packageDir: "media-core",
97-
subpaths: [
98-
["", "index.ts"],
99-
["base64", "base64.ts"],
100-
["constants", "constants.ts"],
101-
["content-length", "content-length.ts"],
102-
["file-name", "file-name.ts"],
103-
["inbound-path-policy", "inbound-path-policy.ts"],
104-
["inline-image-data-url", "inline-image-data-url.ts"],
105-
["media-source-url", "media-source-url.ts"],
106-
["mime", "mime.ts"],
107-
["read-byte-stream-with-limit", "read-byte-stream-with-limit.ts"],
108-
],
109-
},
11094
{
11195
packageName: "@openclaw/llm-core",
11296
packageDir: "llm-core",
@@ -341,7 +325,7 @@ function listInternalCorePackageNativeAliases(
341325
}> = [];
342326
const internalCorePackageAliases = [
343327
...INTERNAL_CORE_PACKAGE_ALIASES,
344-
...["normalization-core", "acp-core"].map((packageDir) => ({
328+
...["media-core", "normalization-core", "acp-core"].map((packageDir) => ({
345329
packageName: `@openclaw/${packageDir}`,
346330
packageDir,
347331
subpaths: listWorkspacePackageExportAliasEntries({

‎src/plugins/sdk-alias.test.ts‎

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,32 @@ function writeWorkspacePackageEntry(params: {
154154
return { srcFile, distFile };
155155
}
156156

157+
function writeWorkspacePackageExports(
158+
root: string,
159+
packageDir: string,
160+
subpaths: readonly string[],
161+
) {
162+
mkdirSafeDir(path.join(root, "packages", packageDir));
163+
fs.writeFileSync(
164+
path.join(root, "packages", packageDir, "package.json"),
165+
JSON.stringify(
166+
{
167+
name: `@openclaw/${packageDir}`,
168+
exports: Object.fromEntries(
169+
subpaths.map((subpath) => {
170+
const exportKey = subpath ? `./${subpath}` : ".";
171+
const distFile = `./dist/${subpath || "index"}.mjs`;
172+
return [exportKey, { import: distFile, default: distFile }];
173+
}),
174+
),
175+
},
176+
null,
177+
2,
178+
),
179+
"utf-8",
180+
);
181+
}
182+
157183
type WorkspaceAliasFixture = readonly [
158184
alias: `@openclaw/${string}`,
159185
packageDir: string,
@@ -1148,6 +1174,15 @@ describe("plugin sdk alias helpers", () => {
11481174

11491175
it("aliases workspace packages to source when dist artifacts are missing", () => {
11501176
const fixture = createPluginSdkAliasFixture();
1177+
writeWorkspacePackageExports(fixture.root, "media-core", ["", "attachment-classify", "mime"]);
1178+
writeWorkspacePackageExports(fixture.root, "acp-core", ["", "runtime/types"]);
1179+
writeWorkspacePackageExports(fixture.root, "normalization-core", [
1180+
"",
1181+
"agent-id",
1182+
"boolean-coercion",
1183+
"result",
1184+
"string-coerce",
1185+
]);
11511186
const workspaceAliases = writeWorkspaceAliasFixtures(fixture.root, [
11521187
["@openclaw/gateway-client", "gateway-client", "index"],
11531188
["@openclaw/gateway-client/timeouts", "gateway-client", "timeouts"],
@@ -1160,6 +1195,7 @@ describe("plugin sdk alias helpers", () => {
11601195
["@openclaw/media-generation-core", "media-generation-core", "index"],
11611196
["@openclaw/media-generation-core/model-ref", "media-generation-core", "model-ref"],
11621197
["@openclaw/media-core", "media-core", "index"],
1198+
["@openclaw/media-core/attachment-classify", "media-core", "attachment-classify"],
11631199
["@openclaw/media-core/mime", "media-core", "mime"],
11641200
["@openclaw/acp-core", "acp-core", "index"],
11651201
["@openclaw/acp-core/runtime/types", "acp-core", "runtime/types"],
@@ -1193,6 +1229,9 @@ describe("plugin sdk alias helpers", () => {
11931229

11941230
it("aliases workspace package subpaths to dist when available", () => {
11951231
const fixture = createPluginSdkAliasFixture();
1232+
writeWorkspacePackageExports(fixture.root, "media-core", ["attachment-classify"]);
1233+
writeWorkspacePackageExports(fixture.root, "acp-core", ["normalize-text"]);
1234+
writeWorkspacePackageExports(fixture.root, "normalization-core", ["record-coerce"]);
11961235
const workspaceAliases = writeWorkspaceAliasFixtures(fixture.root, [
11971236
["@openclaw/gateway-client/readiness", "gateway-client", "readiness"],
11981237
[
@@ -1203,6 +1242,7 @@ describe("plugin sdk alias helpers", () => {
12031242
["@openclaw/gateway-protocol/frame-guards", "gateway-protocol", "frame-guards"],
12041243
["@openclaw/markdown-core/render", "markdown-core", "render"],
12051244
["@openclaw/media-generation-core/catalog", "media-generation-core", "catalog"],
1245+
["@openclaw/media-core/attachment-classify", "media-core", "attachment-classify"],
12061246
[
12071247
"@openclaw/acp-core/normalize-text",
12081248
"acp-core",
@@ -1254,9 +1294,18 @@ describe("plugin sdk alias helpers", () => {
12541294
);
12551295
mkdirSafeDir(path.dirname(normalizationAgentId));
12561296
fs.writeFileSync(normalizationAgentId, "export {};\n", "utf-8");
1257-
const cwdWithoutOpenClawPackage = makeTempDir();
1297+
const mediaAttachmentClassify = path.join(
1298+
fixture.root,
1299+
"dist",
1300+
"media-core",
1301+
"attachment-classify.js",
1302+
);
1303+
mkdirSafeDir(path.dirname(mediaAttachmentClassify));
1304+
fs.writeFileSync(mediaAttachmentClassify, "export {};\n", "utf-8");
1305+
const staleCheckout = createPluginSdkAliasFixture();
1306+
writeWorkspacePackageExports(staleCheckout.root, "media-core", ["mime"]);
12581307

1259-
const aliases = withCwd(cwdWithoutOpenClawPackage, () =>
1308+
const aliases = withCwd(staleCheckout.root, () =>
12601309
withEnv({ NODE_ENV: undefined }, () =>
12611310
buildPluginLoaderAliasMap(sourcePluginEntry, undefined, undefined, "dist"),
12621311
),
@@ -1268,6 +1317,9 @@ describe("plugin sdk alias helpers", () => {
12681317
expect(fs.realpathSync(aliases["@openclaw/normalization-core/agent-id"] ?? "")).toBe(
12691318
fs.realpathSync(normalizationAgentId),
12701319
);
1320+
expect(fs.realpathSync(aliases["@openclaw/media-core/attachment-classify"] ?? "")).toBe(
1321+
fs.realpathSync(mediaAttachmentClassify),
1322+
);
12711323
});
12721324

12731325
it("aliases bundled plugin package public surfaces for source plugin transforms", () => {

‎src/plugins/sdk-alias.ts‎

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -517,21 +517,6 @@ const WORKSPACE_PACKAGE_ALIAS_SUBPATHS = [
517517
],
518518
],
519519
["media-generation-core", ["", "capability-model-ref", "catalog", "model-ref", "normalization"]],
520-
[
521-
"media-core",
522-
[
523-
"",
524-
"base64",
525-
"constants",
526-
"content-length",
527-
"file-name",
528-
"inbound-path-policy",
529-
"inline-image-data-url",
530-
"media-source-url",
531-
"mime",
532-
"read-byte-stream-with-limit",
533-
],
534-
],
535520
["retry", [""]],
536521
[
537522
"terminal-core",
@@ -669,14 +654,7 @@ export function listWorkspacePackageExportAliasEntries(params: {
669654
params.packageDir,
670655
"package.json",
671656
);
672-
const fallbackPackageRoot = resolveOpenClawPackageRootSync({ cwd: process.cwd() });
673-
const packageJson =
674-
tryReadJsonSync<PluginSdkPackageJson>(packageJsonPath) ??
675-
(fallbackPackageRoot
676-
? tryReadJsonSync<PluginSdkPackageJson>(
677-
path.join(fallbackPackageRoot, "packages", params.packageDir, "package.json"),
678-
)
679-
: null);
657+
const packageJson = tryReadJsonSync<PluginSdkPackageJson>(packageJsonPath);
680658
const exports = packageJson?.exports;
681659
if (!exports || typeof exports !== "object" || Array.isArray(exports)) {
682660
return listRootPackagedWorkspacePackageAliasEntries(params);
@@ -914,7 +892,7 @@ function resolveWorkspacePackageAliasMap(params: {
914892
const aliasMap: Record<string, string> = {};
915893
const workspacePackageAliasEntries = [
916894
...WORKSPACE_PACKAGE_ALIAS_ENTRIES,
917-
...["normalization-core", "acp-core"].flatMap((packageDir) =>
895+
...["media-core", "normalization-core", "acp-core"].flatMap((packageDir) =>
918896
listWorkspacePackageExportAliasEntries({
919897
packageRoot,
920898
packageName: `@openclaw/${packageDir}`,

0 commit comments

Comments
 (0)