Files
openmotion/scripts/inline-onboarding.mjs
MasonJJ 6fe9291b04 fix: complete default onboarding across source, CLI and plugin installs (#408)
* fix: complete automatic onboarding across installation entry points

* test: run onboarding regressions in CI and align host guidance

* test: isolate bootstrap onboarding from ambient Codex sessions

* test: publish synthetic account credentials atomically

* fix: rebind a vanished source registration during install preflight

The source preflight aborted the whole install with a raw realpath ENOENT
when the previously registered checkout had been deleted or moved, which is
exactly the uninstalled-source state sourceMarketplacePlan exists to rebind.
Resolve the recorded source defensively: a missing or non-directory path is
reported as a stale registration to replace, and rollback skips restoring a
path that no longer exists. Refusals for foreign, non-local, ambiguous or
actively installed sources are unchanged.

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

* fix: preserve installed marketplace sources on inventory failures

* fix: recover verified stale sources and preserve onboarding delivery state

---------

Co-authored-by: bone3deep1962-collab <bone3deep1962@gmail.com>
Co-authored-by: 罗高 <luogao@users.noreply.github.com>
2026-11-15 21:01:49 +08:00

88 lines
6.4 KiB
JavaScript

import assert from 'node:assert/strict';
import { randomUUID, createHash } from 'node:crypto';
import { readFile, lstat, mkdir, writeFile } from 'node:fs/promises';
import { realpathSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseArgs } from 'node:util';
import { runFile } from './runtime-assets.mjs';
// A current host can render this read-only production view without loading a
// new MCP tool. Domain work continues through the installation's existing CLI.
export async function prepareInlineOnboarding({ appRoot, output, language = 'en' }) {
assert(path.isAbsolute(output) && output.endsWith('.html'), 'Inline onboarding requires an absolute HTML output path supplied by the calling host');
return JSON.parse(await runFile(process.execPath, ['--experimental-strip-types', path.join(appRoot, 'scripts/inline-onboarding.mjs'),
'--output', output, '--language', language], { cwd: appRoot }));
}
export async function renderInlineOnboarding({ appRoot, output, language = 'en' }) {
language = typeof language === 'string' ? language.trim().toLowerCase().split(/[-_]/)[0] : 'en';
if (!['zh', 'en', 'ja', 'ko', 'fr', 'es', 'pt'].includes(language)) language = 'en';
const { loadTemplatePresentation, showTemplateRecommendations } = await import('../shared/mcp-template-recommendations.ts');
const directory = path.join(appRoot, 'mcp-ui/template-recommendations');
const presentation = loadTemplatePresentation({ applicationRoot: appRoot, directory });
const match = presentation.content.text.match(/window\.__OPENMOTION_TEMPLATE_CARDS__=(.*?);<\/script>/s);
assert(match, 'Verified production bootstrap is missing');
const bootstrap = JSON.parse(match[1]);
const recommendation = showTemplateRecommendations(presentation, {
templateIds: ['product-promo', 'nyt-graph', 'decision-tree'], onboarding: true, language,
});
let fragment = await readFile(path.join(directory, 'inline.fragment.html'), 'utf8');
const json = value => JSON.stringify(value).replaceAll('<', '\\u003c');
const parts = {
'__OPENMOTION_INLINE_ROOT__': 'openmotion-onboarding-' + randomUUID(),
'/*__OPENMOTION_INLINE_STYLE__*/': (await readFile(path.join(directory, 'build/app.css'), 'utf8')).replace(/<\/style/gi, '<\\/style'),
'/*__OPENMOTION_INLINE_APP__*/': (await readFile(path.join(directory, 'build/app.js'), 'utf8')).replace(/<\/script/gi, '<\\/script'),
'/*__OPENMOTION_INLINE_BOOTSTRAP__*/': json(bootstrap),
'/*__OPENMOTION_INLINE_RECOMMENDATION__*/': json(recommendation),
};
for (const [marker, value] of Object.entries(parts)) {
assert(fragment.includes(marker), 'Inline template marker is missing: ' + marker);
fragment = fragment.replaceAll(marker, () => value);
}
assert(!fragment.includes('__OPENMOTION_INLINE_'), 'Inline template contains unresolved markers');
const bytes = Buffer.byteLength(fragment);
assert(bytes < 1_000_000, 'Inline onboarding exceeds the host document budget');
await mkdir(path.dirname(output), { recursive: true });
await writeFile(output, fragment, { encoding: 'utf8', flag: 'wx', mode: 0o600 });
return { status: 'prepared', mode: 'inline-document', path: output, bytes,
sha256: createHash('sha256').update(fragment).digest('hex'), resourceVersion: presentation.resourceVersion,
templateIds: recommendation.cards.map(card => card.templateId), language,
publicationRequired: true,
draftBehavior: 'copyable-brief; no task submission', nativeMcpSessionVerified: false,
instructions: 'This is a prepared runtime document, not a host-readable content reference. In the active chat, publish it with this script --publish-from PATH --sha256 SHA256 --host-directory DIRECTORY, using that chat\'s explicitly permitted visualization directory. Never infer host readability from installation/data write access. After visibly opening the real template library, emit the published contentReference once. Preserve the production welcome/cards and verified full MP4s. Do not duplicate the welcome, restart the host or claim native MCP discovery. Recreate retains the copyable brief; no task is submitted. Preparation or publication alone does not prove visible delivery.' };
}
export async function publishInlineOnboarding({ source, expectedSHA256, hostDirectory }) {
assert(typeof source === 'string' && path.isAbsolute(source) && source.endsWith('.html'), 'Pass an absolute prepared HTML source');
assert(typeof hostDirectory === 'string' && path.isAbsolute(hostDirectory), 'Pass the active chat\'s explicitly permitted visualization directory');
assert(/^[a-f0-9]{64}$/.test(expectedSHA256), 'Publication requires the prepared SHA256');
const fragment = await readFile(source);
assert(fragment.length < 1_000_000, 'Inline onboarding exceeds the host document budget');
const sha256 = createHash('sha256').update(fragment).digest('hex');
assert.equal(sha256, expectedSHA256, 'Prepared onboarding SHA256 mismatch');
await mkdir(hostDirectory, { recursive: true });
const output = path.join(hostDirectory, path.basename(source));
try { await writeFile(output, fragment, { flag: 'wx', mode: 0o600 }); }
catch (error) {
if (error.code !== 'EEXIST') throw error;
assert((await lstat(output)).isFile(), 'Refusing a non-regular host document');
assert.equal(createHash('sha256').update(await readFile(output)).digest('hex'), sha256, 'Refusing to overwrite a different host document');
}
return { status: 'published', path: output, sha256, bytes: fragment.length,
publicationRequired: false, nativeMcpSessionVerified: false,
contentReference: 'visualize' + JSON.stringify({ path: output }) + '' };
}
if (process.argv[1] && realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url))) {
const { values } = parseArgs({ options: { output: { type: 'string' }, language: { type: 'string' },
'publish-from': { type: 'string' }, sha256: { type: 'string' }, 'host-directory': { type: 'string' } } });
if (values['publish-from']) {
console.log(JSON.stringify(await publishInlineOnboarding({ source: values['publish-from'], expectedSHA256: values.sha256,
hostDirectory: values['host-directory'] }), null, 2));
} else {
assert(values.output && path.isAbsolute(values.output) && values.output.endsWith('.html'), 'Pass --output with an absolute .html path');
console.log(JSON.stringify(await renderInlineOnboarding({ appRoot: fileURLToPath(new URL('../', import.meta.url)), output: values.output, language: values.language }), null, 2));
}
}