Files
openmotion/scripts/export-adapters.ts
Tom Huang acb80e4095 feat: add an asset workspace with generation, versions, and editor import (#413)
* feat: add a shared asset workspace with generation and version history

Unify direct model jobs and embedded Codex conversations around shared asset operations exposed through MCP. Add reusable media uploads, pinned references, version browsing, trash undo, and verified project imports.

Refine compact creation controls, persistent task history, solid menus, and stable resizable preview layouts. Integrate asset navigation with main's history and deep links, and align onboarding, tracking, documentation, and distribution.

* test: exercise openmotion_assets in the packaged phase-one e2e

The phase-one profile now exposes openmotion_assets, but the packaged
distribution smoke test still asserted that every public Tool was called
without ever invoking it, so the Distribution regression lane failed.

Import a real audio file into the library, read it back by asset and
version, list it by modality, and import the pinned version into the
project so the new Tool is covered end to end.

Generated-By: looper 0.16.2 (runner=fixer, agent=pi)

* fix: preserve asset request receipts and pending draft edits

Exercise asset library replay in the packaged MCP smoke test and align compact control documentation with verified shared geometry.
2026-11-15 23:24:33 +08:00

137 lines
11 KiB
TypeScript

import assert from 'node:assert/strict';
import { constants } from 'node:fs';
import { access, mkdir, readFile, readdir, realpath, rm, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { packagedSkills } from '../shared/workflow.ts';
import { isMain } from '../shared/entry.ts';
import { integrationConfig } from '../shared/integration.ts';
import type { IntegrationInfo } from '../shared/integration.ts';
import { zip } from './package.ts';
import { designSystemDocuments } from '../packages/design-system/index.js';
// Local adapters reference one installed runtime; these ZIPs are not portable installers.
export async function exportAdapters(runtimePath: string, dataPath: string, outputPath: string) {
assert([runtimePath, dataPath, outputPath].every(value => typeof value === 'string' && path.isAbsolute(value)), 'Provide absolute runtime, data and new output directory paths.');
async function canonical(filename: string): Promise<string> {
filename = path.resolve(filename);
try { return await realpath(filename); }
catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error;
return path.join(await canonical(path.dirname(filename)), path.basename(filename));
}
}
const runtime = await realpath(runtimePath), data = await canonical(dataPath), output = await canonical(outputPath);
const contains = (parent: string, child: string) => {
const relative = path.relative(parent, child);
return relative === '' || (relative !== '..' && !relative.startsWith('..' + path.sep) && !path.isAbsolute(relative));
};
assert(!contains(runtime, data) && !contains(data, runtime), 'Keep user projects outside the runtime directory.');
assert(!contains(data, output) && !contains(output, data), 'Keep exported adapters separate from user projects.');
assert(!contains(runtime, output) && !contains(output, runtime), 'Keep exported adapters separate from the runtime.');
const pkg = JSON.parse(await readFile(path.join(runtime, 'package.json'), 'utf8'));
assert(typeof pkg.version === 'string' && /^\d+\.\d+\.\d+(?:[-+][\w.-]+)?$/.test(pkg.version), 'Invalid runtime version.');
const entry = path.join(runtime, 'mcp.ts'); await access(entry);
const exists = async (filename: string) => {
try { await access(filename); return true; }
catch (error) { if ((error as NodeJS.ErrnoException).code === 'ENOENT') return false; throw error; }
};
const manifestFile = path.join(runtime, '.codex-plugin/plugin.json');
// Presence is tracked separately so only a missing manifest uses the legacy fallback; a present null is invalid.
const manifestPresent = await exists(manifestFile);
const runtimeManifest = manifestPresent ? JSON.parse(await readFile(manifestFile, 'utf8')) : null;
assert(!manifestPresent || (typeof runtimeManifest === 'object' && runtimeManifest !== null && !Array.isArray(runtimeManifest)), 'Invalid runtime plugin manifest.');
const manifestText = (value: unknown, field: string) => {
if (value === undefined) return undefined;
assert(typeof value === 'string' && value.trim().length > 0, `Invalid runtime plugin manifest ${field}; host discovery requires a non-empty string.`);
return value;
};
const runtimeInterface = runtimeManifest?.interface;
assert(runtimeInterface === undefined || (typeof runtimeInterface === 'object' && runtimeInterface !== null && !Array.isArray(runtimeInterface)), 'Invalid runtime plugin manifest interface.');
const manifestDescription = manifestText(runtimeManifest?.description, 'description');
const manifestShortDescription = manifestText(runtimeInterface?.shortDescription, 'interface.shortDescription');
const profileFile = path.join(runtime, 'shared/mcp-release.json');
const mcpProfile = await exists(profileFile) ? JSON.parse(await readFile(profileFile, 'utf8')).profile : 'legacy';
assert(['legacy', 'phase1'].includes(mcpProfile), 'Invalid runtime MCP release profile.');
const bundledNode = path.join(runtime, 'runtime/node', process.platform === 'win32' ? 'node.exe' : 'bin/node');
const command = await exists(bundledNode) ? bundledNode : process.execPath;
await access(command, constants.X_OK);
const bootstrap = path.join(runtime, 'bootstrap.mjs');
const args = ['--experimental-strip-types', ...(await exists(bootstrap) ? ['--import', bootstrap] : []), entry];
const info: IntegrationInfo = { name: 'OpenMotion', version: pkg.version, previewUrl: '', dataDirectory: data,
transport: { type: 'stdio', command, args, cwd: runtime, env: { MOTION_DATA_DIR: data } }, tools: [] };
const documents: { name: string; data: Buffer }[] = [];
async function readDocuments(directory: string) {
for (const item of await readdir(path.join(runtime, directory), { withFileTypes: true })) {
const filename = directory + '/' + item.name;
if (mcpProfile === 'phase1' && directory === 'skills' && !packagedSkills.includes(item.name)) continue;
if (item.isDirectory()) await readDocuments(filename);
else {
assert(item.isFile(), 'Adapter documents must be regular files: ' + filename);
documents.push({ name: filename, data: await readFile(path.join(runtime, filename)) });
}
}
}
for (const directory of ['skills', 'docs']) await readDocuments(directory);
for (const name of ['packages/design-system/remix-icon-LICENSE.txt', 'packages/design-system/remix-icon-NOTICE.txt']) documents.push({ name: path.basename(name), data: await readFile(path.join(runtime, name)) });
for (const name of ['AGENTS.md', 'CLAUDE.md', ...designSystemDocuments]) documents.push({ name, data: await readFile(path.join(runtime, name)) });
const filmIcon = (await readFile(path.join(runtime, 'packages/design-system/icons.svg'), 'utf8')).match(/<symbol id="icon-film" viewBox="0 0 24 24"[^>]*>([\s\S]*?)<\/symbol>/)?.[1];
assert(filmIcon, 'Missing shared film icon.');
await mkdir(path.dirname(output), { recursive: true });
await mkdir(output); // EEXIST deliberately leaves an earlier export untouched.
const hosts = ['codex', 'claude', 'workbuddy', 'deepseek'] as const;
try {
for (const host of hosts) {
const files = [...documents];
const text = (name: string, value: unknown) => files.push({ name, data: Buffer.from(typeof value === 'string' ? value : JSON.stringify(value, null, 2) + '\n') });
const notes = host === 'codex' ? 'Install from this local marketplace with Codex. The runtime remains external; no service source or executables are bundled.'
: host === 'claude' ? 'Install from this local marketplace with Claude Code. The runtime remains external.'
: host === 'workbuddy' ? 'Local MCP + Skill Connector. WorkBuddy installation and model/render acceptance must be tested on the target desktop version. Use mcp.json for manual custom MCP setup.'
: 'Merge the insert operation into the active Harness profile or load cordis.patch.yml with --patch. Do not replace existing profile settings.';
text('README.md', `# OpenMotion · ${host}\n\n${notes}\n\nRuntime: ${runtime}\nWorkspace: ${data}\nVersion: ${pkg.version}\n\nStart with [AGENTS.md](AGENTS.md), [the workflow](skills/openmotion-production/SKILL.md) and [adapter instructions](docs/agent-adapters.md). Service scripts live under scope.json runtimeRoot, not this plugin cache.\n\nGenerated on this machine. Regenerate after moving the runtime or installing on another machine. Keep user data when uninstalling an adapter.\n`);
text('scope.json', { host, version: pkg.version, mcpProfile, machineLocal: true, runtimeIncluded: false, runtimeRoot: runtime, dataDirectory: data, clientSettingsModified: false });
if (host === 'deepseek') text('cordis.patch.yml', integrationConfig(info, 'deepseek').text);
else if (host === 'workbuddy') {
// https://open.workbuddy.cn/en/docs/connector (MCP + Skill, cwd since 4.22.15).
text('connector-meta.json', { name: 'OpenMotion', name_en: 'OpenMotion', source: 'openmotion', type: 'mcp', version: pkg.version,
description: '用共用的本机创作运行时制作和修改视频,保留可继续编辑的工程与成片。',
description_zh: '用共用的本机创作运行时制作和修改视频,保留可继续编辑的工程与成片。',
description_en: 'Create and edit videos using the shared local OpenMotion runtime.',
examples_zh: ['查看可用的视频模板和制作流程', '修改当前视频工程并导出成片'],
examples_en: ['List video templates and the production workflow', 'Edit the current video project and export it'], minWorkbuddyVersion: '4.22.15' });
text('mcp.json', { mcpServers: { openmotion: info.transport } });
text('icon.svg', `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="#282828" stroke="none">${filmIcon}</svg>\n`);
}
else {
const manifest = { name: 'openmotion', version: pkg.version,
description: manifestDescription ?? 'OpenMotion with the shared local MCP runtime.',
author: { name: 'OpenDesign' }, skills: './skills/', mcpServers: './.mcp.json' };
text(`.${host}-plugin/plugin.json`, host === 'codex' ? { ...manifest, requires_local_executor: true,
interface: { ...runtimeInterface, displayName: 'OpenMotion', shortDescription: manifestShortDescription ?? 'OpenMotion production with the shared local runtime.' } } : manifest);
text('.mcp.json', host === 'codex' ? { mcpServers: { openmotion: info.transport } } : integrationConfig(info, 'mcp').text);
}
const directory = path.join(output, host, 'openmotion');
for (const file of files) {
const destination = path.join(directory, file.name);
await mkdir(path.dirname(destination), { recursive: true }); await writeFile(destination, file.data);
}
await writeFile(path.join(output, `openmotion-${host}-${pkg.version}-local.zip`), zip(files.map(file => ({ ...file, name: 'openmotion/' + file.name }))));
if (host === 'codex' || host === 'claude') {
const marketplace = path.join(output, host, host === 'codex' ? '.agents/plugins' : '.claude-plugin');
await mkdir(marketplace, { recursive: true });
await writeFile(path.join(marketplace, 'marketplace.json'), JSON.stringify({ name: 'openmotion-local', owner: { name: 'OpenDesign' }, plugins: [host === 'codex'
? { name: 'openmotion', source: { source: 'local', path: './openmotion' }, policy: { installation: 'AVAILABLE', authentication: 'ON_INSTALL' }, category: 'Productivity' }
: { name: 'openmotion', source: './openmotion' }] }, null, 2) + '\n');
await writeFile(path.join(output, host, 'mcp.json'), integrationConfig(info, 'mcp').text);
}
if (host === 'workbuddy') await writeFile(path.join(output, host, 'mcp.json'), JSON.stringify({ mcpServers: { openmotion: info.transport } }, null, 2) + '\n');
}
await writeFile(path.join(output, 'launch-spec.json'), JSON.stringify(info.transport, null, 2) + '\n');
return { output, version: pkg.version, hosts, runtimeIncluded: false, clientSettingsModified: false };
} catch (error) { await rm(output, { recursive: true, force: true }); throw error; }
}
if (isMain(import.meta.url)) {
const [runtime, data, output] = process.argv.slice(2);
console.log(JSON.stringify(await exportAdapters(runtime, data, output), null, 2));
}