Skip to content

Turn one illustration into an animated 2D mesh avatar with a coding agent and a local editor

Guide the editor workflow from project to drawn variants

shinshin8636b33d23f48e
diff --git a/e2e/editor.spec.ts b/e2e/editor.spec.ts
index b584109..d2d60a8 100644
--- a/e2e/editor.spec.ts
+++ b/e2e/editor.spec.ts
@@ -1,6 +1,7 @@
import { dismissGuide, samplePresent, sampleSkipReason } from './sample';
import { expect, test } from '@playwright/test';
-import { readFile } from 'node:fs/promises';
+import { cp, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
+import { join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import fixture from '../samples/miko-qipao/rig.json' with { type: 'json' };
@@ -87,7 +88,7 @@ test('rebuilds from cached assets, marks cut-outs stale, and sweeps angles', asy
await expect(page.getByRole('button', { name: 'Sweep angles', exact: true })).toBeVisible();
});
-test('opens a local image folder without the installed sample', async ({ page }) => {
+test('opens a local project folder without the installed sample', async ({ page }) => {
test.skip(!samplePresent, sampleSkipReason);
await page.route('**/miko-qipao/**', route => {
if (new URL(route.request().url()).pathname.endsWith('.png')) return route.fulfill({ status: 404, body: '' });
@@ -96,9 +97,35 @@ test('opens a local image folder without the installed sample', async ({ page })
await page.goto('/');
await dismissGuide(page);
await expect(page.getByRole('heading', { name: 'Open a project', exact: true })).toBeVisible();
- await page.getByLabel('Open image folder files', { exact: true }).setInputFiles(fileURLToPath(new URL('../samples/miko-qipao/', import.meta.url)));
+ await page.getByLabel('Open project folder files', { exact: true }).setInputFiles(fileURLToPath(new URL('../samples/miko-qipao/', import.meta.url)));
await expect(page.getByText('Engine ready', { exact: true })).toBeVisible();
await expect(page.getByTestId('editor')).toBeVisible();
await page.getByTestId('part-head').click();
await expect(page.getByRole('spinbutton', { name: 'head.cx', exact: true })).toHaveValue('615');
});
+
+test('a newly opened project establishes its own cut-out baseline', async ({ page }) => {
+ test.skip(!samplePresent, sampleSkipReason);
+ const directory = await mkdtemp(join(resolve('projects'), 'mesh-avatar-project-'));
+ try {
+ await cp(fileURLToPath(new URL('../samples/miko-qipao/', import.meta.url)), directory, { recursive: true });
+ const rig = structuredClone(fixture);
+ rig.eyes[0].opening[0][0] += 1;
+ await writeFile(join(directory, 'rig.json'), JSON.stringify(rig));
+ await page.goto('/');
+ await dismissGuide(page);
+ const folder = page.getByLabel('Open project folder files', { exact: true });
+ await folder.setInputFiles(directory);
+ await page.getByTestId('part-eyes').click();
+ await page.getByText(`1 · Opening (${rig.eyes[0].opening.length})`, { exact: true }).click();
+ const point = page.getByRole('spinbutton', { name: 'eyes.0.opening.0.0', exact: true });
+ await expect(point).toHaveValue('447');
+ await expect(page.locator('.stale')).toHaveCount(0);
+ await point.fill('448');
+ await expect(page.locator('.stale')).toContainText('run build-layers');
+ await folder.setInputFiles(directory);
+ await expect(page.locator('.stale')).toHaveCount(0);
+ } finally {
+ await rm(directory, { recursive: true, force: true });
+ }
+});
diff --git a/e2e/empty.spec.ts b/e2e/empty.spec.ts
index 3a0e0e6..754ab32 100644
--- a/e2e/empty.spec.ts
+++ b/e2e/empty.spec.ts
@@ -7,10 +7,14 @@ test('missing sample images show an empty workspace with project opening control
});
await page.goto('/');
await expect(page.getByRole('heading', { name: 'Open a project', exact: true })).toBeVisible();
- await expect(page.getByText('Open rig.json and an image folder to begin. Sample images are installed separately.')).toBeVisible();
+ await expect(page.getByText('Choose a project from Open project, or browse for its folder. Sample images are installed separately.')).toBeVisible();
+ await expect(page.getByTestId('ask-agent-new')).toContainText('Open Codex in this repository folder');
await page.locator('.open-menu > summary').click();
- await expect(page.getByRole('button', { name: 'Open rig.json…', exact: true })).toBeEnabled();
- await expect(page.getByRole('button', { name: 'Open image folder…', exact: true })).toBeEnabled();
+ await expect(page.getByRole('button', { name: 'Load rig.json only…', exact: true })).toBeEnabled();
+ await expect(page.getByRole('button', { name: 'Browse for a project folder…', exact: true })).toBeEnabled();
await expect(page.getByTestId('preview')).toHaveCount(0);
await page.screenshot({ path: 'docs/screenshots/empty-workspace.png' });
+ await page.getByRole('button', { name: '日本語', exact: true }).click();
+ await expect(page.getByTestId('ask-agent-new')).toContainText('このリポジトリのフォルダで Codex を開いて');
+ await page.screenshot({ path: 'docs/screenshots/empty-workspace-ja.png' });
});
diff --git a/e2e/persistent-directory.spec.ts b/e2e/persistent-directory.spec.ts
new file mode 100644
index 0000000..fac299b
--- /dev/null
+++ b/e2e/persistent-directory.spec.ts
@@ -0,0 +1,27 @@
+import { chromium, expect, test } from '@playwright/test';
+import { mkdir, mkdtemp, rm } from 'node:fs/promises';
+import { join, resolve } from 'node:path';
+
+test('directory handles round-trip through IndexedDB without storing image data', async ({ baseURL }) => {
+ await mkdir(resolve('projects'), { recursive: true });
+ const profile = await mkdtemp(join(resolve('projects'), '.directory-test-'));
+ const context = await chromium.launchPersistentContext(profile, { channel: 'chromium', headless: true });
+ try {
+ const page = context.pages()[0];
+ await page.goto(baseURL!);
+ const result = await page.evaluate(async () => {
+ const path = '/src/editor/recent-projects.ts';
+ const helpers = await import(path);
+ const root = await navigator.storage.getDirectory();
+ const handle = await root.getDirectoryHandle('empty-history-check', { create: true });
+ const kept = await helpers.keepDirectory('folder:handle-check', handle);
+ const restored = await helpers.restoreDirectory('folder:handle-check');
+ const same = restored ? await handle.isSameEntry(restored) : false;
+ await helpers.forgetDirectory('folder:handle-check');
+ const forgotten = await helpers.restoreDirectory('folder:handle-check');
+ await root.removeEntry('empty-history-check', { recursive: true });
+ return { kept, same, forgotten: forgotten === undefined };
+ });
+ expect(result).toEqual({ kept: true, same: true, forgotten: true });
+ } finally { await context.close(); await rm(profile, { recursive: true, force: true }); }
+});
diff --git a/e2e/projects-lipsync.spec.ts b/e2e/projects-lipsync.spec.ts
new file mode 100644
index 0000000..c5fb73f
--- /dev/null
+++ b/e2e/projects-lipsync.spec.ts
@@ -0,0 +1,162 @@
+import { expect, test, type Page } from '@playwright/test';
+import { cp, mkdir, mkdtemp, readFile, rm } from 'node:fs/promises';
+import { resolve, basename, join } from 'node:path';
+import { createHash } from 'node:crypto';
+import { request as httpRequest } from 'node:http';
+import fixture from '../samples/miko-qipao/rig.json' with { type: 'json' };
+import { dismissGuide, samplePresent, sampleSkipReason } from './sample';
+
+let directory: string, name: string;
+test.beforeEach(async () => {
+ test.skip(!samplePresent, sampleSkipReason);
+ await mkdir(resolve('projects'), { recursive: true });
+ directory = await mkdtemp(join(resolve('projects'), 'lip-check-')); name = basename(directory);
+ await cp(resolve('samples/miko-qipao'), directory, { recursive: true,
+ filter: path => !path.includes(`${resolve('samples/miko-qipao')}/review`) && !path.includes(`${resolve('samples/miko-qipao')}/built/sprites`) });
+});
+test.afterEach(async () => { if (directory) await rm(directory, { recursive: true, force: true }); });
+async function open(page: Page, project: string) {
+ await page.goto('/'); await dismissGuide(page);
+ await page.locator('.open-menu > summary').click();
+ await page.getByTestId(`project-${project}`).click();
+ await expect(page.getByTestId('project-location')).toBeVisible();
+ await expect(page.getByTestId('preview-status')).toHaveAttribute('data-state', 'ready');
+}
+async function hashMouth(page: Page) {
+ const bytes = await page.getByTestId('preview').evaluate((element, rig) => {
+ const canvas = element as HTMLCanvasElement;
+ const { width, height } = canvas;
+ const scale = Math.min(width / (rig.image.width * (1 + 2 * rig.view.padSide)), height / (rig.image.height * (1 + rig.view.padTop)));
+ const ox = (width - rig.image.width * scale) / 2, oy = height - rig.image.height * scale;
+ const mouth = rig.mouth.area;
+ const crop = document.createElement('canvas'); crop.width = Math.ceil((mouth.rx * 2 + 32) * scale); crop.height = Math.ceil((mouth.ry * 2 + 32) * scale);
+ const ctx = crop.getContext('2d')!;
+ ctx.drawImage(canvas, Math.floor(ox + (mouth.cx - mouth.rx - 16) * scale), Math.floor(oy + (mouth.cy - mouth.ry - 16) * scale), crop.width, crop.height, 0, 0, crop.width, crop.height);
+ return Array.from(ctx.getImageData(0, 0, crop.width, crop.height).data);
+ }, fixture);
+ return createHash('sha256').update(Buffer.from(bytes)).digest('hex');
+}
+
+test('listed project saves in place with one backup, supports keyboard save, path copy and stubbed reveal', async ({ page, context }) => {
+ await context.grantPermissions(['clipboard-read', 'clipboard-write']);
+ await open(page, name);
+ const location = page.getByTestId('project-location'); await expect(location).toContainText(`projects/${name}`);
+ await expect(location.locator('.project-path')).toHaveAttribute('title', directory);
+ await page.getByRole('button', { name: 'Copy full path' }).click();
+ expect(await page.evaluate(() => navigator.clipboard.readText())).toBe(directory);
+ await expect(page.getByTestId('project-location').getByRole('button', { name: '✓ Copied', exact: true })).toHaveAttribute('aria-live', 'polite');
+ await expect(page.getByTestId('project-location').getByRole('button', { name: 'Copy full path', exact: true })).toBeVisible({ timeout: 5000 });
+ let revealed = false;
+ await page.route(`**/__studio/projects/${name}/reveal`, route => { revealed = true; return route.fulfill({ contentType: 'application/json', body: JSON.stringify({ path: directory }) }); });
+ await location.locator('button').last().click(); await expect.poll(() => revealed).toBe(true);
+ await page.getByTestId('part-head').click();
+ const x = page.getByRole('spinbutton', { name: 'head.cx', exact: true }); await x.fill('635');
+ await page.getByRole('button', { name: 'Save rig', exact: true }).click();
+ await expect(page.getByRole('status').filter({ hasText: `Saved to projects/${name}/rig.json` })).toBeVisible();
+ expect(JSON.parse(await readFile(join(directory, 'rig.json.bak'), 'utf8')).head.cx).toBe(fixture.head.cx);
+ expect(JSON.parse(await readFile(join(directory, 'rig.json'), 'utf8')).head.cx).toBe(635);
+ await x.fill('645'); await page.keyboard.press('ControlOrMeta+s');
+ await expect.poll(async () => JSON.parse(await readFile(join(directory, 'rig.json'), 'utf8')).head.cx).toBe(645);
+ expect(JSON.parse(await readFile(join(directory, 'rig.json.bak'), 'utf8')).head.cx).toBe(635);
+});
+
+for (const sprites of [true, false]) test(`vowels and text change mouth pixels ${sprites ? 'with sprites' : 'without sprites'}`, async ({ page }) => {
+ await open(page, sprites ? 'sample-miko-qipao' : name);
+ await page.getByTestId('idle-toggle').click();
+ await expect(page.getByTestId('preview-status')).toHaveAttribute('data-state', 'ready');
+ await page.getByRole('tab', { name: 'Lip sync', exact: true }).click();
+ const hashes: string[] = [];
+ for (const vowel of ['あ', 'い', 'お']) {
+ await page.getByRole('button', { name: vowel, exact: true }).click();
+ await expect(page.getByRole('button', { name: vowel, exact: true })).toHaveAttribute('aria-pressed', 'true');
+ await expect(page.getByRole('slider', { name: 'Mouth open', exact: true })).toBeDisabled();
+ await page.waitForTimeout(350); hashes.push(await hashMouth(page));
+ }
+ expect(new Set(hashes).size).toBe(3);
+ await page.getByRole('button', { name: 'ん', exact: true }).click();
+ await expect.poll(async () => Number(await page.getByRole('slider', { name: 'Mouth open', exact: true }).inputValue())).toBeLessThan(0.02);
+ await page.getByRole('button', { name: 'Release', exact: true }).click();
+ await expect(page.getByRole('slider', { name: 'Mouth open', exact: true })).toBeEnabled();
+ await page.getByLabel('Kana text', { exact: true }).fill('アイオ 漢字A');
+ await expect(page.locator('.lip-skipped')).toContainText('漢 字 A');
+ await page.getByLabel('Loop', { exact: true }).check();
+ const speed = page.getByRole('slider', { name: 'Morae per second', exact: true }); await speed.fill('4');
+ await page.getByRole('button', { name: 'Play', exact: true }).click();
+ const frames = [];
+ for (let i = 0; i < 6; i++) { await page.waitForTimeout(120); frames.push(await hashMouth(page)); }
+ expect(new Set(frames).size).toBeGreaterThan(2);
+ await page.waitForTimeout(700); await expect(page.getByRole('button', { name: 'Stop', exact: true })).toBeEnabled();
+ await page.getByRole('button', { name: 'Stop', exact: true }).click();
+ await expect(page.getByRole('slider', { name: 'Mouth open', exact: true })).toBeEnabled();
+});
+
+test('unavailable local server keeps the project picker and rig download without page errors', async ({ page }) => {
+ const errors: string[] = []; page.on('pageerror', error => errors.push(error.message));
+ await page.route('**/__studio/projects', route => route.fulfill({ contentType: 'text/html', body: '<!doctype html>' }));
+ await page.goto('/'); await dismissGuide(page);
+ await page.locator('.open-menu > summary').click();
+ await expect(page.getByLabel('Local projects', { exact: true })).toHaveCount(0);
+ await expect(page.getByRole('button', { name: 'Browse for a project folder…', exact: true })).toBeVisible();
+ await page.getByLabel('Open project folder files', { exact: true }).setInputFiles(directory);
+ await expect(page.getByTestId('preview-status')).toHaveAttribute('data-state', 'ready');
+ await expect(page.getByTestId('project-location')).toContainText('full path unavailable');
+ const download = page.waitForEvent('download'); await page.getByRole('button', { name: 'Save rig', exact: true }).click();
+ expect((await download).suggestedFilename()).toBe('rig.json'); expect(errors).toEqual([]);
+});
+
+test('dev endpoint rejects traversal even when the raw HTTP path preserves dot segments', async ({ baseURL }) => {
+ const url = new URL(baseURL!);
+ for (const path of ['../rig', '%2e%2e/rig', '%2Fprivate/rig', `${name}%2F..%2Fother/rig`, `${name}/built/../../rig`]) {
+ const status = await new Promise<number>((done, reject) => {
+ const req = httpRequest({ hostname: url.hostname, port: url.port, path: `/__studio/projects/${path}`, method: 'POST', headers: { 'Content-Type': 'application/json' } }, res => { res.resume(); done(res.statusCode!); });
+ req.on('error', reject); req.end(JSON.stringify(fixture));
+ });
+ expect(status, path).toBe(400);
+ }
+});
+
+test('recent projects persist in order, auto-reopen, remove missing entries, and can be removed or cleared', async ({ page }) => {
+ await open(page, name);
+ await page.getByTestId('part-head').click();
+ await page.getByRole('spinbutton', { name: 'head.cx', exact: true }).fill('555');
+ await expect(page.getByRole('button', { name: 'Undo', exact: true })).toBeEnabled();
+ await page.locator('.open-menu > summary').click(); await page.getByTestId('project-sample-miko-qipao').click();
+ await expect(page.getByTestId('project-location')).toContainText('samples/miko-qipao');
+ await expect(page.getByRole('button', { name: 'Undo', exact: true })).toBeDisabled();
+ await page.reload();
+ await expect(page.getByTestId('project-location')).toContainText('samples/miko-qipao');
+ await page.locator('.open-menu > summary').click();
+ const menu = page.locator('.project-menu');
+ const ids = await menu.locator('.recent-row > button:first-child').evaluateAll(buttons => buttons.map(button => button.getAttribute('data-testid')));
+ expect(ids.slice(0, 2)).toEqual(['recent-server:sample-miko-qipao', `recent-server:${name}`]);
+ await menu.getByTestId(`recent-server:${name}`).click();
+ await expect(page.getByTestId('project-location')).toContainText(`projects/${name}`);
+ await page.reload(); await expect(page.getByTestId('project-location')).toContainText(`projects/${name}`);
+ await page.evaluate(() => {
+ const key = 'mesh-avatar-recent-projects', entries = JSON.parse(localStorage.getItem(key)!);
+ entries.unshift({ id: 'server:missing-ui-test', kind: 'server', name: 'missing-ui-test', serverName: 'missing-ui-test', relativePath: 'projects/missing-ui-test', lastOpened: new Date().toISOString() });
+ localStorage.setItem(key, JSON.stringify(entries));
+ });
+ await page.reload();
+ await expect(page.getByRole('status').filter({ hasText: 'Project no longer exists; removed from history: missing-ui-test' })).toBeVisible();
+ expect(await page.evaluate(() => JSON.parse(localStorage.getItem('mesh-avatar-recent-projects')!).some((entry: { name: string }) => entry.name === 'missing-ui-test'))).toBe(false);
+ await page.locator('.open-menu > summary').click();
+ await menu.getByRole('checkbox', { name: 'Reopen last project on start' }).uncheck();
+ await page.reload(); await dismissGuide(page);
+ await expect(page.getByTestId('project-location')).toHaveCount(0);
+ await page.locator('.open-menu > summary').click();
+ await menu.getByRole('button', { name: `Remove from history ${name}`, exact: true }).click();
+ await expect(menu.getByTestId(`recent-server:${name}`)).toHaveCount(0);
+ await menu.getByRole('button', { name: 'Clear history', exact: true }).click();
+ await expect(menu.getByText('No recent projects.', { exact: true })).toBeVisible();
+});
+
+test('picker projects without a persistent handle show Browse again in recent history', async ({ page }) => {
+ await page.goto('/'); await dismissGuide(page);
+ await page.getByLabel('Open project folder files', { exact: true }).setInputFiles(directory);
+ await page.locator('.open-menu > summary').click();
+ await expect(page.locator('.project-menu').getByTestId(`recent-folder:${name}`)).toContainText('Browse again');
+ await page.reload(); await dismissGuide(page);
+ await page.locator('.open-menu > summary').click();
+ await expect(page.locator('.project-menu').getByTestId(`recent-folder:${name}`)).toContainText('Browse again');
+});
diff --git a/e2e/usability.spec.ts b/e2e/usability.spec.ts
index d2bd60c..7e66590 100644
--- a/e2e/usability.spec.ts
+++ b/e2e/usability.spec.ts
@@ -97,13 +97,18 @@ test('handles have human names and line editing, zoom, pan and fit remain availa
const middle = await canvasPoint(page, 485, 190);
await page.mouse.dblclick(middle.x, middle.y);
await expect(page.getByText('Nodes (5)', { exact: true })).toBeVisible();
- await page.keyboard.down('Alt'); await page.mouse.click(middle.x, middle.y); await page.keyboard.up('Alt');
+ const inserted = await canvasPoint(page, 485, 190);
+ await page.keyboard.down('Alt'); await page.mouse.click(inserted.x, inserted.y); await page.keyboard.up('Alt');
await expect(page.getByText('Nodes (4)', { exact: true })).toBeVisible();
+ // The stale notice changes canvas size; wait for ResizeObserver and the fitted view.
+ await page.evaluate(() => new Promise<void>(resolve => requestAnimationFrame(() => requestAnimationFrame(() => resolve()))));
const scale = Number(await canvas.getAttribute('data-scale'));
await page.mouse.wheel(0, -200);
- await expect.poll(async () => Number(await canvas.getAttribute('data-scale'))).toBeGreaterThan(scale);
+ await expect.poll(async () => Number(await canvas.getAttribute('data-scale'))).toBeCloseTo(scale * Math.exp(0.2));
const offset = Number(await canvas.getAttribute('data-offset-x'));
- await page.mouse.down({ button: 'middle' }); await page.mouse.move(middle.x + 30, middle.y + 20); await page.mouse.up({ button: 'middle' });
+ const panStart = await canvasPoint(page, 485, 190);
+ await page.mouse.move(panStart.x, panStart.y);
+ await page.mouse.down({ button: 'middle' }); await page.mouse.move(panStart.x + 30, panStart.y + 20); await page.mouse.up({ button: 'middle' });
await expect.poll(async () => Number(await canvas.getAttribute('data-offset-x'))).toBeGreaterThan(offset);
await page.getByRole('button', { name: 'Fit', exact: true }).click();
await expect.poll(async () => Number(await canvas.getAttribute('data-scale'))).toBeCloseTo(scale);
@@ -143,7 +148,7 @@ for (const [width, height] of [[1440, 900], [1280, 800]]) for (const language of
await page.getByTestId('idle-toggle').click();
await expect(page.getByTestId('preview-status')).toHaveAttribute('data-state', 'ready');
await expect(page.locator('.part-tip')).toBeVisible();
- await expect(page.locator('.pose-test')).toHaveAttribute('open', '');
+ await expect(page.getByRole('tab', { name: language === 'ja' ? 'ポーズ確認' : 'Pose test', exact: true })).toHaveAttribute('aria-selected', 'true');
expect(await page.evaluate(() => document.documentElement.scrollWidth)).toBeLessThanOrEqual(width);
for (const selector of ['.parts-panel', '.editor-panel', '.preview-panel']) {
const box = (await page.locator(selector).boundingBox())!;
diff --git a/e2e/workflow.spec.ts b/e2e/workflow.spec.ts
new file mode 100644
index 0000000..2bc7afb
--- /dev/null
+++ b/e2e/workflow.spec.ts
@@ -0,0 +1,185 @@
+import { test, expect, type Page } from '@playwright/test';
+import { cp, mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
+import { join, resolve, basename } from 'node:path';
+import { createHash } from 'node:crypto';
+import { PNG } from 'pngjs';
+import fixture from '../samples/miko-qipao/rig.json' with { type: 'json' };
+import { dismissGuide, samplePresent, sampleSkipReason } from './sample';
+import { runTool } from '../src/server/project-jobs';
+
+let directory: string, name: string;
+test.beforeEach(async () => {
+ test.skip(!samplePresent, sampleSkipReason);
+ await mkdir(resolve('projects'), { recursive: true });
+ directory = await mkdtemp(join(resolve('projects'), 'workflow-check-')); name = basename(directory);
+ await cp(resolve('samples/miko-qipao'), directory, { recursive: true,
+ filter: path => !['review', 'variants', 'variant-masks', 'built/sprites'].some(folder => path.startsWith(`${resolve('samples/miko-qipao')}/${folder}`)) });
+});
+test.afterEach(async () => { if (directory) await rm(directory, { recursive: true, force: true }); });
+async function open(page: Page) {
+ await page.goto('/'); await dismissGuide(page); await page.locator('.open-menu > summary').click();
+ await page.getByTestId(`project-${name}`).click();
+ await expect(page.getByTestId('preview-status')).toHaveAttribute('data-state', 'ready');
+ await page.getByTestId('idle-toggle').click();
+ await expect(page.getByTestId('preview-status')).toHaveAttribute('data-state', 'ready');
+}
+async function editEye(page: Page) {
+ await page.getByTestId('part-eyes').click();
+ await page.getByText(`1 · Opening (${fixture.eyes[0].opening.length})`, { exact: true }).click();
+ await page.getByRole('spinbutton', { name: 'eyes.0.opening.0.0', exact: true }).fill('451');
+ await expect(page.getByTestId('stale-banner')).toBeVisible();
+ await expect(page.getByTestId('preview-status')).toHaveAttribute('data-state', 'ready');
+}
+async function pixels(page: Page) {
+ return createHash('sha256').update(await page.getByTestId('preview').evaluate(canvas => (canvas as HTMLCanvasElement).toDataURL())).digest('hex');
+}
+async function drop(page: Page, bytes: Buffer, filename: string) {
+ await page.getByTestId('variant-drop').evaluate((element, file) => {
+ const transfer = new DataTransfer(); transfer.items.add(new File([new Uint8Array(file.bytes)], file.filename, { type: 'image/png' }));
+ element.dispatchEvent(new DragEvent('drop', { bubbles: true, cancelable: true, dataTransfer: transfer }));
+ }, { bytes: Array.from(bytes), filename });
+}
+test('rebuild runs the real local tool, reloads changed pixels and keeps selection, zoom and undo history', async ({ page }) => {
+ test.setTimeout(90000); await open(page); await editEye(page);
+ const editor = page.getByTestId('editor'); const point = page.getByRole('spinbutton', { name: 'eyes.0.opening.0.0', exact: true });
+ await editor.hover(); await page.mouse.wheel(0, -200);
+ await expect(page.locator('.zoom-tools > span')).toHaveText('122%');
+ const before = await pixels(page), base = await readFile(join(directory, 'built/base.png'));
+ const beforeSize = await page.getByTestId('preview').evaluate(canvas => [(canvas as HTMLCanvasElement).width, (canvas as HTMLCanvasElement).height]);
+ const button = page.getByRole('button', { name: 'Save and rebuild layers', exact: true });
+ await button.click(); await expect(page.getByTestId('stale-banner')).toHaveCount(0, { timeout: 60000 });
+ await expect(page.getByTestId('preview-status')).toHaveAttribute('data-state', 'ready');
+ expect(await page.getByTestId('preview').evaluate(canvas => [(canvas as HTMLCanvasElement).width, (canvas as HTMLCanvasElement).height])).toEqual(beforeSize);
+ expect(await pixels(page)).not.toBe(before); expect(await readFile(join(directory, 'built/base.png'))).not.toEqual(base);
+ await expect(editor).toHaveAttribute('data-focus-group', 'eyes'); await expect(page.locator('.zoom-tools > span')).toHaveText('122%');
+ await expect(point).toHaveValue('451'); await expect(page.getByRole('button', { name: 'Undo', exact: true })).toBeEnabled();
+ await page.getByRole('button', { name: 'Undo', exact: true }).click(); await expect(point).toHaveValue('446');
+ await expect(page.getByTestId('stale-banner')).toBeVisible();
+});
+test('a failed build leaves the previous layers and view, with a useful reason and expandable log', async ({ page }) => {
+ test.setTimeout(90000); await open(page); await editEye(page);
+ const old = await readFile(join(directory, 'built/base.png')), before = await pixels(page);
+ const tiny = new PNG({ width: 1, height: 1 }); tiny.data.fill(255); await writeFile(join(directory, 'source.png'), PNG.sync.write(tiny));
+ await page.getByRole('button', { name: 'Save and rebuild layers', exact: true }).click();
+ await expect(page.getByTestId('stale-banner').getByRole('alert')).toContainText('Previous layers were kept.', { timeout: 60000 });
+ expect(await readFile(join(directory, 'built/base.png'))).toEqual(old); expect(await pixels(page)).toBe(before);
+ await page.getByTestId('stale-banner').getByText('Show log', { exact: true }).click();
+ await expect(page.getByTestId('stale-banner').locator('pre')).toContainText('rig.image must match source.png');
+ await expect(page.getByRole('button', { name: 'Save and rebuild layers', exact: true })).toBeEnabled();
+});
+test('requests provide masks, root handoff and prompts; drops accept valid drawings and reject outside changes atomically', async ({ page, context }) => {
+ test.setTimeout(120000); await context.grantPermissions(['clipboard-read', 'clipboard-write']); await open(page);
+ const panel = page.getByTestId('variants-panel'); await expect(panel.getByRole('checkbox')).toHaveCount(2);
+ await expect(panel.getByTestId('ask-agent-variants')).toHaveCount(0);
+ await expect(panel.locator('.manual-variants')).not.toHaveAttribute('open', '');
+ await panel.getByRole('checkbox', { name: 'Mouth', exact: true }).check();
+ const agent = page.getByTestId('ask-agent-variants'); await agent.getByRole('button', { name: 'Copy full path', exact: true }).click();
+ expect(await page.evaluate(() => navigator.clipboard.readText())).toBe(resolve('.'));
+ await agent.getByRole('button', { name: 'Copy message', exact: true }).click();
+ const prompt = await page.evaluate(() => navigator.clipboard.readText()); expect(prompt).toContain(`projects/${name}`); expect(prompt).toContain('Step 6'); expect(prompt).toContain('mouth drawn variants');
+ expect(prompt).toContain('ask me before sending images to an external generator');
+ await expect(agent.getByRole('button', { name: '✓ Copied', exact: true })).toHaveCount(2);
+ await expect(agent.getByRole('button', { name: 'Copy message', exact: true })).toBeVisible({ timeout: 5000 });
+ await expect(agent.locator('ol')).toHaveCount(0); await expect(panel.locator('select')).toHaveCount(0);
+ await expect(agent.getByRole('button', { name: 'Reload project', exact: true })).toHaveCount(0);
+ await panel.locator('.manual-variants > summary').click();
+ await page.getByRole('button', { name: 'Export masks and prompts', exact: true }).click();
+ await expect(page.getByRole('status').filter({ hasText: 'Local masks and prompts are ready.' })).toBeVisible({ timeout: 60000 });
+ expect((await readFile(join(directory, 'variant-requests/mouth_a/prompt.md'), 'utf8'))).toContain('full-size RGBA PNG');
+ const manual = panel.locator('.variant-requests'); await manual.getByText('Mouth あ · mouth_a.png', { exact: true }).click();
+ await expect(manual.locator('img').first()).toBeVisible(); await expect(manual.locator('img').first()).toHaveJSProperty('naturalWidth', fixture.image.width);
+ await manual.getByRole('button', { name: 'Copy image prompt', exact: true }).click();
+ expect(await page.evaluate(() => navigator.clipboard.readText())).toContain('full-size RGBA PNG');
+ await manual.getByRole('button', { name: 'Copy file paths', exact: true }).click();
+ expect(await page.evaluate(() => navigator.clipboard.readText())).toContain(`projects/${name}/variant-requests/mouth_a/mask.png`);
+ await expect(manual.getByRole('button', { name: '✓ Copied', exact: true })).toHaveCount(2);
+ await expect(manual.getByRole('button', { name: 'Copy file paths', exact: true })).toBeVisible({ timeout: 5000 });
+ const source = PNG.sync.read(await readFile(join(directory, 'source.png')));
+ const { cx, cy } = fixture.mouth.area;
+ for (let y = Math.round(cy) - 3; y <= Math.round(cy) + 3; y++) for (let x = Math.round(cx) - 6; x <= Math.round(cx) + 6; x++) { const index = (y * source.width + x) * 4; source.data.set([170, 38, 65, 255], index); }
+ await page.getByRole('tab', { name: 'Lip sync', exact: true }).click(); await page.getByRole('button', { name: 'あ', exact: true }).click(); await page.waitForTimeout(300);
+ const before = await pixels(page); await drop(page, PNG.sync.write(source), 'mouth_a.png');
+ await expect(panel.getByRole('status').filter({ hasText: 'Accepted; preview updated.' })).toBeVisible({ timeout: 60000 });
+ await expect(panel.locator('.variant-count').last()).toHaveText('1 of 4 images');
+ await expect(page.getByTestId('preview-status')).toHaveAttribute('data-state', 'ready'); await page.waitForTimeout(300);
+ expect(await pixels(page)).not.toBe(before);
+ const savedVariant = await readFile(join(directory, 'variants/mouth_a.png')), savedManifest = await readFile(join(directory, 'built/sprites/sprites.json'));
+ for (let index = 0; index < source.data.length; index += 4) if (source.data[index + 3] === 255) source.data.set([255, 255, 255, 255], index);
+ await drop(page, PNG.sync.write(source), 'mouth_a.png');
+ await expect(panel.getByRole('alert')).toContainText('Pixels outside the edit mask changed.', { timeout: 60000 });
+ expect(await readFile(join(directory, 'variants/mouth_a.png'))).toEqual(savedVariant); expect(await readFile(join(directory, 'built/sprites/sprites.json'))).toEqual(savedManifest);
+ await panel.getByText('Measured comparison outside the mask', { exact: true }).click(); await expect(panel.locator('.job-feedback')).toContainText('Maximum difference');
+ await expect(page.getByTestId('project-location')).toContainText(`projects/${name}`);
+});
+test('empty workspace has a single prompt copy and a one-line repository path, without extra steps', async ({ page, context }) => {
+ await context.grantPermissions(['clipboard-read', 'clipboard-write']);
+ await page.route('**/miko-qipao/**/*.png', route => route.fulfill({ status: 404, body: '' }));
+ await page.route('**/miko-qipao/source.png', route => route.fulfill({ status: 404, body: '' }));
+ await page.goto('/'); const card = page.getByTestId('ask-agent-new'); await expect(card).toBeVisible();
+ await expect(card.locator('.root-path')).toHaveText(resolve('.'));
+ await card.getByRole('button', { name: 'Copy message', exact: true }).click();
+ expect(await page.evaluate(() => navigator.clipboard.readText())).toContain('projects/my-avatar');
+ await expect(card.getByRole('button', { name: '✓ Copied', exact: true })).toHaveAttribute('aria-live', 'polite');
+ await expect(card.getByRole('button', { name: 'Copy message', exact: true })).toBeVisible({ timeout: 5000 });
+ await expect(card.getByRole('button')).toHaveCount(2); await expect(card.locator('ol')).toHaveCount(0);
+ await expect(card.getByRole('button', { name: 'Reload project', exact: true })).toHaveCount(0);
+});
+test('eye and mouth checkboxes immediately adapt the prompt and lip fallback opens the mouth request', async ({ page }) => {
+ await open(page); const panel = page.getByTestId('variants-panel');
+ const prompt = panel.getByRole('textbox', { name: 'Copy message', exact: true });
+ await panel.getByRole('checkbox', { name: 'Eyes', exact: true }).check(); await expect(prompt).toContainText('eye drawn variants');
+ await panel.getByRole('checkbox', { name: 'Mouth', exact: true }).check(); await expect(prompt).toContainText('eye and mouth drawn variants');
+ await panel.getByRole('checkbox', { name: 'Eyes', exact: true }).uncheck(); await expect(prompt).toContainText('mouth drawn variants');
+ await panel.getByRole('checkbox', { name: 'Mouth', exact: true }).uncheck(); await expect(prompt).toHaveCount(0);
+ await panel.locator(':scope > summary').click();
+ await page.getByRole('tab', { name: 'Lip sync', exact: true }).click();
+ await expect(page.locator('.mouth-fallback')).toContainText('using mesh deformation');
+ await page.locator('.mouth-fallback button').click();
+ await expect(panel).toHaveAttribute('open', ''); await expect(panel.getByRole('checkbox', { name: 'Mouth', exact: true })).toBeChecked();
+ await expect(prompt).toContainText(`projects/${name}`); await expect(prompt).toBeVisible();
+});
+test('the read-only sample still gives a mouth request and keeps its source protected', async ({ page }) => {
+ await page.goto('/'); await dismissGuide(page);
+ await expect(page.getByTestId('preview-status')).toHaveAttribute('data-state', 'ready');
+ await page.getByTestId('variants-panel').getByRole('checkbox', { name: 'Mouth', exact: true }).check();
+ const prompt = page.getByTestId('ask-agent-variants').getByRole('textbox');
+ await expect(prompt).toContainText('samples/miko-qipao'); await expect(prompt).toContainText('projects/miko-qipao-variants');
+ await expect(prompt).toContainText('Keep the original project unchanged'); await expect(prompt).toContainText('mouth drawn variants');
+});
+test('external sprite updates load automatically while keeping edited outlines, selection, zoom and undo', async ({ page, request }) => {
+ test.setTimeout(90000); await page.setViewportSize({ width: 1440, height: 900 }); await open(page); await editEye(page);
+ const point = page.getByRole('spinbutton', { name: 'eyes.0.opening.0.0', exact: true });
+ await page.getByTestId('editor').hover(); await page.mouse.wheel(0, -200); await expect(page.locator('.zoom-tools > span')).toHaveText('122%');
+ await page.getByRole('tab', { name: 'Lip sync', exact: true }).click(); await page.getByRole('button', { name: 'あ', exact: true }).click(); await page.waitForTimeout(300);
+ const before = await pixels(page);
+ await page.screenshot({ path: 'docs/screenshots/ui4-auto-before-en-1440x900.png' });
+ const result = await request.post(`/__studio/projects/${name}/variant-requests`, { data: { rig: fixture } }); expect(result.ok()).toBe(true);
+ const image = PNG.sync.read(await readFile(join(directory, 'source.png'))), { cx, cy } = fixture.mouth.area;
+ for (let y = Math.round(cy) - 3; y <= Math.round(cy) + 3; y++) for (let x = Math.round(cx) - 6; x <= Math.round(cx) + 6; x++) image.data.set([170, 38, 65, 255], (y * image.width + x) * 4);
+ await mkdir(join(directory, 'variants'), { recursive: true }); await writeFile(join(directory, 'variants/mouth_a.png'), PNG.sync.write(image));
+ await runTool(resolve('.'), directory, 'build-sprites');
+ await expect(page.getByTestId('variants-panel').locator('.variant-count').last()).toHaveText('1 of 4 images', { timeout: 20000 });
+ await expect(page.getByRole('status').filter({ hasText: 'Drawn variants loaded.' })).toBeVisible();
+ await expect(page.getByTestId('preview-status')).toHaveAttribute('data-state', 'ready'); await page.waitForTimeout(300);
+ expect(await pixels(page)).not.toBe(before); await expect(page.locator('.mouth-fallback')).toHaveCount(0);
+ await page.screenshot({ path: 'docs/screenshots/ui4-auto-after-en-1440x900.png' });
+ await page.getByRole('button', { name: '日本語', exact: true }).click();
+ await expect(page.getByRole('status').filter({ hasText: '描き分け画像を読み込みました' })).toBeVisible();
+ await page.screenshot({ path: 'docs/screenshots/ui4-auto-after-ja-1440x900.png' });
+ await page.getByRole('button', { name: '英語', exact: true }).click();
+ await expect(point).toHaveValue('451'); await expect(page.getByTestId('stale-banner')).toBeVisible();
+ await expect(page.getByTestId('editor')).toHaveAttribute('data-focus-group', 'eyes'); await expect(page.locator('.zoom-tools > span')).toHaveText('122%');
+ await page.getByRole('button', { name: 'Undo', exact: true }).click(); await expect(point).toHaveValue('446');
+});
+test('pose and lip tabs keep a large preview at 1440 by 900 in both languages', async ({ page }) => {
+ await page.setViewportSize({ width: 1440, height: 900 }); await open(page);
+ for (const language of ['en', 'ja']) {
+ if (language === 'ja') await page.getByRole('button', { name: '日本語', exact: true }).click();
+ for (const tab of language === 'en' ? ['Pose test', 'Lip sync'] : ['ポーズ確認', '口の動き']) {
+ await page.getByRole('tab', { name: tab, exact: true }).click();
+ expect((await page.getByTestId('preview').boundingBox())!.height).toBeGreaterThanOrEqual(420);
+ await expect(page.getByRole('tabpanel')).toHaveCount(1);
+ expect((await page.locator('.preview-panel').boundingBox())!.y + (await page.locator('.preview-panel').boundingBox())!.height).toBeLessThanOrEqual(900);
+ }
+ }
+});
diff --git a/playwright.config.ts b/playwright.config.ts
index fbded78..8dceae8 100644
--- a/playwright.config.ts
+++ b/playwright.config.ts
@@ -4,8 +4,8 @@ const baseURL = `http://127.0.0.1:${port}`;
export default defineConfig({
testDir: 'e2e',
- // Canvas previews share GPU resources; bound concurrent browser rendering.
- workers: 2,
+ // Real layer builds and pixel comparisons must not compete for GPU/CPU resources.
+ workers: 1,
use: {
baseURL,
viewport: { width: 1440, height: 1000 },
diff --git a/src/editor/App.tsx b/src/editor/App.tsx
index d77d410..3011f75 100644
--- a/src/editor/App.tsx
+++ b/src/editor/App.tsx
@@ -6,7 +6,7 @@ import type { Rig } from '../rig/types';
import { parseRig, validateRig } from '../rig/validate';
import { Preview } from './Preview';
import { RigHistory, downloadRig } from './history';
-import { openImageFolder, sampleImagesAvailable } from './project';
+import { openProjectFolder, openLocalProject, localProjects, projectAction, sampleImagesAvailable, repositoryContext, runProjectJob, ProjectJobError, type ProjectJob, type LocalProject } from './project';
import { layerSignature } from './stale';
import { RigFields } from './RigFields';
import { GROUPS } from './parts';
@@ -14,13 +14,18 @@ import { PartList } from './PartList';
import { GUIDE_KEY, I18nProvider, readPreference, savePreference, useI18n, type PartGroup } from './i18n';
import { FirstGuide, GuideSteps, Help } from './Guide';
import { Icon } from './Icon';
+import { RecentProjects } from './RecentProjects';
+import { AskAgent } from './AskAgent';
+import { VariantsPanel } from './VariantsPanel';
+import { JobFeedback } from './JobFeedback';
+import { CopyButton } from './CopyButton';
+import { readRecent, addRecent, saveRecent, REOPEN_KEY, pickDirectory, hasDirectoryPicker, directoryFiles,
+ keepDirectory, restoreDirectory, forgetDirectory, clearDirectories, type RecentProject, type ProjectDirectory } from './recent-projects';
import './style.css';
-type EditorError = { kind: 'invalidRig' | 'invalidFolder' | 'invalidValue'; paths: string[] };
+type EditorError = { kind: 'invalidRig' | 'invalidFolder' | 'invalidValue' | 'saveError' | 'revealError' | 'copyError'; paths: string[] };
function errorPaths(value: string) { return [...new Set(value.match(/rig(?:\.[\w]+|\[\d+\])+/g) ?? [])]; }
-const builtSignature = layerSignature(parseRig(fixture));
-
export function App() { return <I18nProvider><Workspace /></I18nProvider>; }
function Workspace() {
const { t, parts, language, setLanguage, title } = useI18n();
@@ -29,12 +34,68 @@ function Workspace() {
const [help, setHelp] = useState(false);
const [history] = useState(() => new RigHistory(parseRig(fixture)));
const [rig, setRig] = useState(history.present);
+ const [builtSignature, setBuiltSignature] = useState(() => layerSignature(history.present));
const fileInput = useRef<HTMLInputElement>(null);
const folderInput = useRef<HTMLInputElement>(null);
const projectOpened = useRef(false);
const objectUrls = useRef<string[]>([]);
const [sourceUrl, setSourceUrl] = useState('');
const [assets, setAssets] = useState<Record<string, string> | undefined>(undefined);
+ const [projects, setProjects] = useState<LocalProject[] | null>(null);
+ const [localProject, setLocalProject] = useState<LocalProject | null>(null);
+ const [pickedName, setPickedName] = useState('');
+ const [notice, setNotice] = useState<{ key: 'savedTo' | 'missingRecent' | 'variantsReloaded' | 'reloadFailed'; path?: string } | null>(null);
+ const [saving, setSaving] = useState(false);
+ const [opening, setOpening] = useState(false);
+ const [rootPath, setRootPath] = useState<string>();
+ const [mouthSprites, setMouthSprites] = useState(true), [mouthRequest, setMouthRequest] = useState(0);
+ const [job, setJob] = useState<ProjectJob | null>(null);
+ const jobRef = useRef<ProjectJob | null>(null);
+ const [rebuildError, setRebuildError] = useState<ProjectJobError | null>(null);
+ useEffect(() => { void repositoryContext().then(context => setRootPath(context?.rootPath)); }, []);
+ const [recent, setRecent] = useState(readRecent);
+ const [reopen, setReopen] = useState(() => readPreference(REOPEN_KEY) !== '0');
+ const remember = (entry: RecentProject) => {
+ setRecent(current => {
+ const next = addRecent(current, entry); saveRecent(next);
+ for (const item of current) if (!next.some(value => value.id === item.id)) void forgetDirectory(item.id);
+ return next;
+ });
+ };
+ const removeRecent = (id: string) => {
+ setRecent(current => { const next = current.filter(item => item.id !== id); saveRecent(next); return next; });
+ void forgetDirectory(id);
+ };
+ useEffect(() => {
+ let active = true;
+ void localProjects().then(async list => {
+ if (!active) return;
+ setProjects(list);
+ const entries = readRecent();
+ const missing = list ? entries.filter(entry => entry.kind === 'server' && !list.some(project => project.name === entry.serverName)) : [];
+ const available = entries.filter(entry => !missing.includes(entry));
+ if (missing.length) { setRecent(available); saveRecent(available); setNotice({ key: 'missingRecent', path: missing.map(entry => entry.name).join(', ') }); }
+ if (readPreference(REOPEN_KEY) === '0' || !available.length || projectOpened.current) return;
+ const latest = available[0];
+ if (latest.kind === 'server') {
+ const project = list?.find(item => item.name === latest.serverName);
+ if (project) { projectOpened.current = true; await openLocal(project, true); }
+ } else if (latest.hasHandle) {
+ const handle = await restoreDirectory(latest.id);
+ if (!active || !handle) return;
+ try {
+ if (await handle.queryPermission({ mode: 'read' }) === 'granted' && active && !projectOpened.current) {
+ projectOpened.current = true; await openFolder(await directoryFiles(handle), handle);
+ }
+ } catch (error) {
+ if (error instanceof DOMException && error.name === 'NotFoundError') { removeRecent(latest.id); setNotice({ key: 'missingRecent', path: latest.name }); }
+ // Permission prompts are reserved for a click on the recent entry.
+ }
+ }
+ });
+ return () => { active = false; };
+ }, []);
+ useEffect(() => { if (!notice) return; const timer = setTimeout(() => setNotice(null), 4000); return () => clearTimeout(timer); }, [notice]);
const [checking, setChecking] = useState(true);
useEffect(() => {
let cancelled = false;
@@ -47,8 +108,10 @@ function Workspace() {
}, []);
const [visible, setVisible] = useState<string[]>([...GROUPS]);
const [selected, setSelected] = useState<string | null>(null);
+ const [dragStale, setDragStale] = useState<boolean | null>(null);
const [error, setError] = useState<EditorError | null>(null);
const update = (next: Rig) => {
+ if (jobRef.current) return;
const errors = validateRig(next);
if (errors.length) { setError({ kind: 'invalidValue', paths: errorPaths(errors.join('\n')) }); return; }
setError(null);
@@ -56,32 +119,92 @@ function Workspace() {
setRig(history.present);
};
const openFile = async (file?: File) => {
- if (!file) return;
+ if (!file || jobRef.current) return;
try {
update(parseRig(JSON.parse(await file.text())));
setSelected(null);
+ setLocalProject(null);
} catch (error) { setError({ kind: 'invalidRig', paths: errorPaths(String(error)) }); }
};
- const openFolder = async (files: File[]) => {
+ const openFolder = async (files: File[], handle?: ProjectDirectory) => {
+ if (jobRef.current) return false;
try {
- const project = await openImageFolder(files);
+ const project = await openProjectFolder(files);
projectOpened.current = true;
- if (project.rig) update(project.rig);
+ if (project.rig) {
+ history.load(project.rig); setRig(history.present); setDragStale(null);
+ setBuiltSignature(layerSignature(project.rig));
+ }
setSelected(null);
objectUrls.current.forEach(url => URL.revokeObjectURL(url));
objectUrls.current = project.urls;
setSourceUrl(project.sourceUrl);
setAssets(project.assets);
+ setLocalProject(null);
+ setPickedName(files[0]?.webkitRelativePath.split('/')[0] || t.openProject);
setChecking(false);
setError(null);
- } catch { setError({ kind: 'invalidFolder', paths: [] }); }
+ setRebuildError(null);
+ const name = handle?.name || files[0]?.webkitRelativePath.split('/')[0] || t.openProject;
+ let id = `folder:${name}`;
+ if (handle) {
+ id = `folder:${crypto.randomUUID()}`;
+ for (const entry of recent.filter(item => item.kind === 'folder' && item.hasHandle)) {
+ const previous = await restoreDirectory(entry.id);
+ try { if (previous && await handle.isSameEntry(previous)) { id = entry.id; break; } } catch { /* An old handle may no longer exist. */ }
+ }
+ }
+ const hasHandle = handle ? await keepDirectory(id, handle) : false;
+ if (!hasHandle) id = `folder:${name}`;
+ remember({ id, name, kind: 'folder', relativePath: name, lastOpened: new Date().toISOString(), hasHandle });
+ if (openMenu.current) openMenu.current.open = false;
+ return true;
+ } catch { setError({ kind: 'invalidFolder', paths: [] }); return false; }
+ };
+ const openLocal = async (entry: LocalProject, automatic = false) => {
+ if (jobRef.current) return;
+ setOpening(true);
+ try {
+ const project = await openLocalProject(entry);
+ projectOpened.current = true;
+ history.load(project.rig!); setRig(history.present); setDragStale(null); setBuiltSignature(layerSignature(project.rig!));
+ setSelected(null); setLocalProject(entry); setPickedName('');
+ objectUrls.current.forEach(url => URL.revokeObjectURL(url)); objectUrls.current = [];
+ setSourceUrl(project.sourceUrl); setAssets(project.assets); setChecking(false); setError(null);
+ setRebuildError(null);
+ if (!automatic) setNotice(null);
+ remember({ id: `server:${entry.name}`, kind: 'server', name: entry.name, serverName: entry.name,
+ relativePath: entry.relativePath, lastOpened: new Date().toISOString() });
+ if (openMenu.current) openMenu.current.open = false;
+ } catch {
+ if (automatic) { removeRecent(`server:${entry.name}`); setNotice({ key: 'missingRecent', path: entry.name }); }
+ else setError({ kind: 'invalidFolder', paths: [] });
+ }
+ finally { setOpening(false); }
+ };
+ const saveRef = useRef<() => void>(() => undefined);
+ const save = async () => {
+ if (jobRef.current) return;
+ if (!localProject || localProject.readOnly) { downloadRig(history.present); return; }
+ if (saving) return;
+ setSaving(true);
+ try { const result = await projectAction(localProject, 'rig', history.present); setNotice({ key: 'savedTo', path: result.path }); setError(null); }
+ catch { setError({ kind: 'saveError', paths: [] }); }
+ finally { setSaving(false); }
+ };
+ saveRef.current = () => { void save(); };
+ const reveal = async () => {
+ if (!localProject) return;
+ try { await projectAction(localProject, 'reveal'); setError(null); }
+ catch { setError({ kind: 'revealError', paths: [] }); }
};
useEffect(() => {
const keyboard = (event: KeyboardEvent) => {
if (event.key === 'Escape') { setHelp(false); if (openMenu.current) openMenu.current.open = false; }
if (!(event.ctrlKey || event.metaKey)) return;
+ if (jobRef.current) { if (['s', 'z'].includes(event.key.toLowerCase())) event.preventDefault(); return; }
const typing = event.target instanceof HTMLInputElement || event.target instanceof HTMLTextAreaElement;
- if (event.key.toLowerCase() === 's') { event.preventDefault(); downloadRig(history.present); }
+ if (event.key.toLowerCase() === 's') { event.preventDefault(); saveRef.current(); }
if (!typing && event.key.toLowerCase() === 'z') {
event.preventDefault();
setRig(event.shiftKey ? history.redo() : history.undo());
@@ -97,7 +220,90 @@ function Workspace() {
const changed = Object.keys(currentLayers).filter(key => JSON.stringify(currentLayers[key]) !== JSON.stringify(builtLayers[key]));
const errorNotice = error && <p role="alert" className="error">{t[error.kind]} {error.paths.join(', ')}</p>;
const dismissGuide = () => { setGuide(false); savePreference(GUIDE_KEY, '1'); };
- const chooseFile = (folder: boolean) => { if (openMenu.current) openMenu.current.open = false; (folder ? folderInput : fileInput).current!.click(); };
+ const chooseFile = async (folder: boolean) => {
+ if (jobRef.current) return;
+ if (openMenu.current) openMenu.current.open = false;
+ if (folder && hasDirectoryPicker()) {
+ try { const handle = await pickDirectory(); await openFolder(await directoryFiles(handle), handle); }
+ catch (error) { if (!(error instanceof DOMException && error.name === 'AbortError')) setError({ kind: 'invalidFolder', paths: [] }); }
+ } else (folder ? folderInput : fileInput).current!.click();
+ };
+ const openRecent = async (entry: RecentProject) => {
+ if (entry.kind === 'server') {
+ const list = await localProjects(); setProjects(list);
+ if (list) {
+ const project = list.find(item => item.name === entry.serverName);
+ if (!project) { removeRecent(entry.id); setNotice({ key: 'missingRecent', path: entry.name }); return; }
+ await openLocal(project); return;
+ }
+ } else if (entry.hasHandle) {
+ const handle = await restoreDirectory(entry.id);
+ if (handle) {
+ try {
+ const permission = await handle.queryPermission({ mode: 'read' });
+ if (permission !== 'granted' && await handle.requestPermission({ mode: 'read' }) !== 'granted') return;
+ await openFolder(await directoryFiles(handle), handle); return;
+ } catch (error) {
+ if (error instanceof DOMException && error.name === 'NotFoundError') { removeRecent(entry.id); setNotice({ key: 'missingRecent', path: entry.name }); return; }
+ }
+ }
+ }
+ await chooseFile(true);
+ };
+ const recentControl = <RecentProjects recent={recent} projects={projects} reopen={reopen}
+ onOpen={entry => { void openRecent(entry); }} onRemove={removeRecent}
+ onClear={() => { setRecent([]); saveRecent([]); void clearDirectories(); }}
+ onReopen={value => { setReopen(value); savePreference(REOPEN_KEY, value ? '1' : '0'); }} />;
+ const reloadInPlace = async (entry: LocalProject) => {
+ const list = await localProjects(), fresh = list?.find(item => item.name === entry.name);
+ if (!fresh) throw new ProjectJobError('toolFailed', t.reloadFailed);
+ const loaded = await openLocalProject(fresh);
+ // Keep the canvas mounted, its view, selection and the existing undo/redo stack.
+ history.replacePresent(loaded.rig!); setRig(history.present); setDragStale(null);
+ setBuiltSignature(layerSignature(loaded.rig!)); setAssets(loaded.assets); setLocalProject(fresh); setProjects(list); setRebuildError(null);
+ };
+ useEffect(() => {
+ if (!import.meta.hot || !localProject) return;
+ const entry = localProject;
+ let active = true, revision = 0;
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ const changed = ({ name }: { name: string }) => {
+ if (name !== entry.name || jobRef.current) return;
+ const generation = ++revision;
+ clearTimeout(timer);
+ const reload = async (attempt = 0) => {
+ if (!active || generation !== revision || jobRef.current) return;
+ try {
+ const list = await localProjects(), fresh = list?.find(item => item.name === entry.name);
+ if (!fresh) throw new Error('Project unavailable');
+ const loaded = await openLocalProject(fresh);
+ if (!active || generation !== revision || jobRef.current) return;
+ // Sprite changes must not discard unsaved outlines, selection, view or undo history.
+ setAssets(loaded.assets); setLocalProject(fresh); setProjects(list);
+ setNotice({ key: 'variantsReloaded' });
+ } catch {
+ if (!active || generation !== revision || jobRef.current) return;
+ if (attempt < 3) timer = setTimeout(() => { void reload(attempt + 1); }, 600 * (attempt + 1));
+ else setNotice({ key: 'reloadFailed' });
+ }
+ };
+ timer = setTimeout(() => { void reload(); }, 300);
+ };
+ import.meta.hot.on('studio:variants-changed', changed);
+ return () => { active = false; clearTimeout(timer); import.meta.hot?.off('studio:variants-changed', changed); };
+ }, [localProject?.name]);
+ const runJob = async (action: ProjectJob, files?: File[]) => {
+ if (!localProject || localProject.readOnly || jobRef.current || saving) throw new ProjectJobError('toolFailed');
+ jobRef.current = action; setJob(action);
+ try {
+ const result = await runProjectJob(localProject, action, history.present, files);
+ if (action !== 'variant-requests') {
+ try { await reloadInPlace(localProject); }
+ catch { throw new ProjectJobError('reloadFailed', t.reloadFailed); }
+ }
+ return result;
+ } finally { jobRef.current = null; setJob(null); }
+ };
return <main onDragOver={event => event.preventDefault()} onDrop={event => { event.preventDefault(); void openFile(event.dataTransfer.files[0]); }}>
<header className="toolbar">
<div className="brand"><h1>{t.product}</h1><p>{t.subtitle}</p></div>
@@ -106,13 +312,25 @@ function Workspace() {
onChange={event => { void openFile(event.target.files?.[0]); event.target.value = ''; }} />
<input ref={folderInput} type="file" multiple hidden aria-label={t.folderFiles} {...{ webkitdirectory: '' }}
onChange={event => { void openFolder(Array.from(event.target.files ?? [])); event.target.value = ''; }} />
- <details ref={openMenu} className="open-menu"><summary>{t.openProject}<Icon name="chevron" /></summary>
- <div className="project-menu"><button onClick={() => chooseFile(false)}>{t.openRig}</button><button onClick={() => chooseFile(true)}>{t.openFolder}</button></div>
+ <details ref={openMenu} className="open-menu" inert={job !== null} onToggle={event => {
+ if (event.currentTarget.open) void localProjects().then(setProjects);
+ }}><summary>{t.openProject}<Icon name="chevron" /></summary>
+ <div className="project-menu">{recentControl}<p className="project-help">{t.projectHelp}</p>
+ {projects && <div className="project-list" aria-label={t.localProjects}>
+ {projects.length === 0 && <p>{t.noProjects}</p>}
+ {projects.map(project => <button key={project.name} disabled={opening} data-testid={`project-${project.name}`} onClick={() => { void openLocal(project); }}>
+ <strong>{project.readOnly ? t.sampleProject : project.name}</strong><small>{project.relativePath}</small>
+ <small>{t.updated}: {new Date(project.updatedAt).toLocaleString(language === 'ja' ? 'ja-JP' : 'en-GB')}
+ {project.hasSprites && <span className="badge">{t.drawnVariants}</span>}{project.readOnly && <span className="badge">{t.readOnly}</span>}</small>
+ </button>)}
+ </div>}
+ <button onClick={() => { void chooseFile(true); }}>{t.openFolder}</button><button onClick={() => { void chooseFile(false); }}>{t.openRig}</button>
+ </div>
</details>
- <button className="icon-button" aria-label={t.save} title={`${t.save} · ⌘S`} onClick={() => downloadRig(rig)}><Icon name="save" /></button>
+ <button className="icon-button" aria-label={t.save} disabled={saving || job !== null} title={`${t.save} · ⌘S`} onClick={() => { void save(); }}><Icon name="save" /></button>
<span className="toolbar-divider" />
- <button className="icon-button" aria-label={t.undo} title={`${t.undo} · ⌘Z`} disabled={!history.canUndo} onClick={() => setRig(history.undo())}><Icon name="undo" /></button>
- <button className="icon-button" aria-label={t.redo} title={`${t.redo} · ⇧⌘Z`} disabled={!history.canRedo} onClick={() => setRig(history.redo())}><Icon name="redo" /></button>
+ <button className="icon-button" aria-label={t.undo} title={`${t.undo} · ⌘Z`} disabled={!history.canUndo || job !== null} onClick={() => setRig(history.undo())}><Icon name="undo" /></button>
+ <button className="icon-button" aria-label={t.redo} title={`${t.redo} · ⇧⌘Z`} disabled={!history.canRedo || job !== null} onClick={() => setRig(history.redo())}><Icon name="redo" /></button>
<span className="toolbar-divider" />
<button className="icon-button" aria-label={t.help} title={t.help} aria-expanded={help} onClick={() => setHelp(current => !current)}><Icon name="help" /></button>
<div className="language-toggle" role="group" aria-label={t.language}>
@@ -121,22 +339,36 @@ function Workspace() {
</div>
</nav>
</header>
+ {(localProject || pickedName) && <div className="project-location" data-testid="project-location">
+ <strong>{localProject?.readOnly ? t.sampleProject : localProject?.name || pickedName}</strong>
+ <span className="project-path" title={localProject?.absolutePath}>{localProject?.relativePath || t.unknownPath}</span>
+ {localProject && <><CopyButton value={localProject.absolutePath} label={t.copyPath} icon />
+ <button className="icon-button" aria-label={navigator.platform.includes('Mac') ? t.showFinder : t.showFolder} title={navigator.platform.includes('Mac') ? t.showFinder : t.showFolder} onClick={() => { void reveal(); }}><Icon name="folder" /></button>
+ {localProject.readOnly && <span className="badge">{t.readOnly}</span>}</>}
+ </div>}
+ {notice && <div role="status" className="save-notice">{t[notice.key]}{notice.path && ` ${notice.path}`}</div>}
{help && <Help onClose={() => setHelp(false)} onGuide={() => { setGuide(true); setHelp(false); }} />}
- {changed.length > 0 && <div role="status" className="stale">{t.stale}<small>{t.changedParts}: {changed.map(key => parts[key as PartGroup]?.[0] ?? title(key)).join(' · ')}</small></div>}
- {!sourceUrl && <section className="panel empty-project"><h2>{checking ? t.checking : t.emptyTitle}</h2><p>{t.emptyHelp}</p>{errorNotice}</section>}
+ {(dragStale ?? changed.length > 0) && <div className="stale" data-testid="stale-banner"><p role="status">{localProject && !localProject.readOnly ? t.staleLocal : t.stale}</p><small>{t.changedParts}: {changed.map(key => parts[key as PartGroup]?.[0] ?? title(key)).join(' · ')}</small>
+ {localProject && !localProject.readOnly ? <button className="primary" disabled={job !== null || saving} onClick={() => { setRebuildError(null); void runJob('rebuild').catch(error => setRebuildError(error instanceof ProjectJobError ? error : new ProjectJobError('toolFailed'))); }}>{job === 'rebuild' ? t.rebuilding : t.rebuild}</button> : <p>{t.rebuildUnavailable}</p>}
+ {rebuildError && <JobFeedback error={rebuildError} />}
+ </div>}
+ {!sourceUrl && <section className="panel empty-project"><h2>{checking ? t.checking : t.emptyTitle}</h2><p>{t.projectHelp}</p><p>{t.emptyHelp}</p>{recentControl}<AskAgent newProject rootPath={rootPath} />{errorNotice}</section>}
{sourceUrl && <div className="workspace">
- <PartList rig={rig} visible={visible} selected={selected} onSelect={selectPart} onVisible={setVisible} />
- <section className="panel editor-panel">
+ <div className="parts-container" inert={job !== null}><PartList rig={rig} visible={visible} selected={selected} onSelect={selectPart} onVisible={setVisible} /></div>
+ <section className="panel editor-panel" inert={job !== null}>
<div className="panel-title"><h2>{t.source}</h2><span>{rig.image.width} × {rig.image.height} {t.px}</span></div>
<div className="canvas-stage">
<EditorCanvas sourceUrl={sourceUrl} rig={rig} visible={visible} selected={selected} onSelect={setSelected} onChange={update}
- onBegin={() => history.begin()} onEnd={() => { history.end(); setRig(structuredClone(history.present)); }} />
+ onBegin={() => { history.begin(); setDragStale(changed.length > 0); }}
+ onEnd={() => { history.end(); setRig(structuredClone(history.present)); setDragStale(null); }} />
{guide && <FirstGuide onDismiss={dismissGuide} />}
</div>
</section>
<div className="right-column">
- <Preview rig={rig} assets={assets} />
- <aside className="panel inspector">
+ <Preview rig={rig} assets={assets} hasMouthSprites={mouthSprites} onDrawMouth={() => setMouthRequest(value => value + 1)} />
+ <VariantsPanel project={localProject} projectPath={localProject?.relativePath ?? (pickedName ? `<${pickedName}>` : 'samples/miko-qipao')} assets={assets} rootPath={rootPath} busy={job !== null || saving} stale={changed.length > 0} onRun={runJob} mouthRequest={mouthRequest} onMouthPresence={setMouthSprites} />
+ {!localProject && !pickedName && <details className="panel new-illustration"><summary>{t.newIllustration}</summary><AskAgent newProject rootPath={rootPath} /></details>}
+ <aside className="panel inspector" inert={job !== null}>
<div className="selection-heading"><h2>{selectedPart?.[0] ?? t.selection}</h2></div>
{errorNotice}
{selectedPart ? <><p className="part-description">{selectedPart[1]}</p><p className="part-tip"><strong>{t.tip}</strong> {selectedPart[2]}</p>
diff --git a/src/editor/AskAgent.tsx b/src/editor/AskAgent.tsx
new file mode 100644
index 0000000..a9fb12f
--- /dev/null
+++ b/src/editor/AskAgent.tsx
@@ -0,0 +1,16 @@
+import { useI18n } from './i18n';
+import { CopyButton } from './CopyButton';
+
+export function AskAgent({ rootPath, projectPath, target = 'both', newProject = false, readOnlySource }: {
+ rootPath?: string; projectPath?: string; target?: 'eyes' | 'mouth' | 'both'; newProject?: boolean; readOnlySource?: string;
+}) {
+ const { t } = useI18n();
+ const path = projectPath ?? 'projects/my-avatar';
+ const prompt = (readOnlySource ? t.agentCopySample.replace('SOURCE', readOnlySource).replace('PROJECT', path) : '') + (newProject ? t.agentNewPrompt : t.agentVariantPrompt).replace('PROJECT', path).replace('TARGET', target === 'eyes' ? t.agentEyes : target === 'mouth' ? t.agentMouth : t.agentBoth);
+ return <section className="ask-agent" data-testid={newProject ? 'ask-agent-new' : 'ask-agent-variants'} aria-label={t.askAgent}>
+ <p className="agent-instruction">{t.agentInstruction}</p>
+ {rootPath ? <div className="agent-path"><code className="root-path" title={rootPath}>{rootPath}</code><CopyButton value={rootPath} label={t.copyPath} icon /></div> : <p className="workflow-note">{t.rootUnavailable}</p>}
+ <textarea aria-label={t.copyPrompt} readOnly value={prompt} rows={4} />
+ <CopyButton value={prompt} label={t.copyPrompt} />
+ </section>;
+}
diff --git a/src/editor/CopyButton.tsx b/src/editor/CopyButton.tsx
new file mode 100644
index 0000000..49e256c
--- /dev/null
+++ b/src/editor/CopyButton.tsx
@@ -0,0 +1,20 @@
+import { useEffect, useRef, useState } from 'react';
+import { useI18n } from './i18n';
+import { Icon } from './Icon';
+
+export function CopyButton({ value, label, icon = false }: { value: string; label: string; icon?: boolean }) {
+ const { t } = useI18n();
+ const [feedback, setFeedback] = useState<'done' | 'error' | null>(null);
+ const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
+ useEffect(() => () => clearTimeout(timer.current), []);
+ const copy = async () => {
+ clearTimeout(timer.current);
+ try { await navigator.clipboard.writeText(value); setFeedback('done'); }
+ catch { setFeedback('error'); }
+ timer.current = setTimeout(() => setFeedback(null), 2000);
+ };
+ const text = feedback === 'done' ? `✓ ${t.copyDone}` : feedback === 'error' ? t.copyError : label;
+ return <button type="button" className={`copy-button${icon && !feedback ? ' icon-button' : ''}`} aria-label={text} title={label} aria-live="polite" onClick={() => { void copy(); }}>
+ {icon && !feedback ? <Icon name="copy" /> : text}
+ </button>;
+}
diff --git a/src/editor/Icon.tsx b/src/editor/Icon.tsx
index 3dee311..0ae3dcc 100644
--- a/src/editor/Icon.tsx
+++ b/src/editor/Icon.tsx
@@ -7,6 +7,8 @@ const paths = {
play: 'm8 4 12 8-12 8V4Z', pause: 'M7 4v16 M17 4v16',
help: 'M9 8a3 3 0 1 1 5 2c-2 1-2 2-2 4 M12 18h.01 M22 12a10 10 0 1 1-20 0 10 10 0 0 1 20 0Z',
chevron: 'm7 10 5 5 5-5', close: 'm6 6 12 12 M6 18 18 6',
+ folder: 'M3 7V4h6l2 3h10v13H3V7Z',
+ copy: 'M8 8h13v13H8V8Z M16 8V3H3v13h5',
};
export function Icon({ name }: { name: keyof typeof paths }) {
return <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7"
diff --git a/src/editor/JobFeedback.tsx b/src/editor/JobFeedback.tsx
new file mode 100644
index 0000000..9d9ef77
--- /dev/null
+++ b/src/editor/JobFeedback.tsx
@@ -0,0 +1,15 @@
+import { useI18n } from './i18n';
+import { ProjectJobError } from './project';
+
+export function JobFeedback({ error, log = '', accepted = false, importing = false }: { error?: ProjectJobError | null; log?: string; accepted?: boolean; importing?: boolean }) {
+ const { t } = useI18n();
+ const details = error?.log || log;
+ const reason = error?.code === 'dependencies' ? t.dependenciesHelp : error?.code === 'unsafeFiles' ? t.unsafeFilesHelp : error?.code === 'invalidImages' ? t.importInvalid : /outside-mask changes/.test(details) ? t.importOutside : /expected PNG size/.test(details) ? t.importSize : /mask/.test(details) ? t.importMask : t.jobFailed;
+ const metrics = [...details.matchAll(/(eyes_\w+|mouth_\w+): outside-mask max ([\d.]+)\/255, mean ([\d.]+)\/255, (\d+) pixels over tolerance ([\d.]+)/g)];
+ return <div className="job-feedback" data-testid="job-feedback">
+ {error && <p role="alert">{error.code === 'reloadFailed' ? t.reloadFailed : <>{importing ? t.importRejected : t.jobFailed}{reason !== t.jobFailed && ` ${reason}`}</>}</p>}
+ {accepted && <p role="status">{t.importAccepted}</p>}
+ {metrics.length > 0 && <details><summary>{t.metricsTitle}</summary>{metrics.map(metric => <p key={metric[1]}><code>{metric[1]}</code>: {t.outsideMax} {metric[2]}/255 · {t.outsideMean} {metric[3]}/255 · {t.outsidePixels} {metric[4]} ({metric[5]}/255)</p>)}</details>}
+ {details && <details><summary>{t.showLog}</summary><pre>{details}</pre></details>}
+ </div>;
+}
diff --git a/src/editor/Preview.tsx b/src/editor/Preview.tsx
index ea2b74d..01641d3 100644
--- a/src/editor/Preview.tsx
+++ b/src/editor/Preview.tsx
@@ -1,6 +1,7 @@
import { useEffect, useRef, useState } from 'react';
import { createMeshAvatar, type MeshAvatar } from '../engine';
import { PARAMS } from '../engine/rig.js';
+import { VOWELS, skippedKanaCharacters } from '../engine/kana.js';
import type { Rig } from '../rig/types';
import { useI18n } from './i18n';
import { Icon } from './Icon';
@@ -14,7 +15,9 @@ const sliders = [
{ id: 'mouthOpen', label: 'mouthOpen', min: 0, max: 1, def: 0 },
{ id: 'bodyAngleZ', label: 'bodyTilt', min: -10, max: 10, def: 0 },
] as const;
-export function Preview({ rig, assets }: { rig: Rig; assets?: Record<string, string> }) {
+type Vowel = 'a' | 'i' | 'u' | 'e' | 'o' | 'n';
+type Lip = { kind: 'hold'; vowel: Vowel } | { kind: 'text'; text: string; speed: number; loop: boolean } | null;
+export function Preview({ rig, assets, hasMouthSprites, onDrawMouth }: { rig: Rig; assets?: Record<string, string>; hasMouthSprites: boolean; onDrawMouth: () => void }) {
const { t } = useI18n();
const canvas = useRef<HTMLCanvasElement>(null);
const avatar = useRef<MeshAvatar | null>(null);
@@ -23,6 +26,13 @@ export function Preview({ rig, assets }: { rig: Rig; assets?: Record<string, str
const [idle, setIdle] = useState(true);
const [stress, setStress] = useState(false);
const [parameters, setParameters] = useState<Record<string, number>>({});
+ const [lip, setLip] = useState<Lip>(null);
+ const [text, setText] = useState('あいうえお');
+ const [speed, setSpeed] = useState(7);
+ const [loop, setLoop] = useState(false);
+ const [liveMouth, setLiveMouth] = useState(0);
+ const [tab, setTab] = useState<'pose' | 'lip'>('pose');
+ const skipped = skippedKanaCharacters(text).join(' ');
const controls = useRef({ idle, stress, parameters });
controls.current = { idle, stress, parameters };
useEffect(() => {
@@ -56,11 +66,16 @@ export function Preview({ rig, assets }: { rig: Rig; assets?: Record<string, str
if (!value) return;
value.setAutoIdle(idle && !stress);
value.setAutoMotion(idle && !stress);
- value.setParameters(idle && !stress ? parameters : { ...defaults, ...parameters });
+ const pose = lip ? { ...parameters, mouthOpen: 0 } : parameters;
+ value.setParameters(idle && !stress ? pose : { ...defaults, ...pose });
+ value.stopLipSync();
+ if (lip?.kind === 'hold') value.holdMouth(lip.vowel);
+ if (lip?.kind === 'text') value.speakKana(lip.text, { speed: lip.speed, loop: lip.loop });
value.advance(0);
- if (!idle && !stress) return;
+ if (!idle && !stress && !lip) return;
let raf = 0;
const start = performance.now();
+ let previous = start;
const frame = (now: number) => {
if (stress) {
const t = (now - start) / 1000;
@@ -69,12 +84,18 @@ export function Preview({ rig, assets }: { rig: Rig; assets?: Record<string, str
angleZ: Math.sin(t * 1.1) * 25, bodyAngleZ: Math.cos(t * 0.7) * 8 });
if (t >= 6) { setStress(false); return; }
}
- value.advance(1 / 60);
+ value.advance(lip ? Math.min(0.05, Math.max(0, (now - previous) / 1000)) : 1 / 60);
+ previous = now;
+ if (lip) {
+ const state = value.getLipSyncState();
+ setLiveMouth(Math.round(state.open * 100) / 100);
+ if (!state.active) setLip(null);
+ }
raf = requestAnimationFrame(frame);
};
raf = requestAnimationFrame(frame);
return () => cancelAnimationFrame(raf);
- }, [rig, assets, idle, stress, parameters, revision]);
+ }, [rig, assets, idle, stress, parameters, revision, lip]);
return (
<section className="panel preview-panel">
<div className="panel-title"><h2>{t.preview}</h2><span role="status" data-testid="preview-status" data-state={status}>{t[status]}</span></div>
@@ -84,25 +105,49 @@ export function Preview({ rig, assets }: { rig: Rig; assets?: Record<string, str
<button className="icon-button" aria-label={idle ? t.pause : t.play} title={idle ? t.pause : t.play}
data-testid="idle-toggle" aria-pressed={idle} onClick={() => setIdle(current => !current)}><Icon name={idle ? 'pause' : 'play'} /></button><span>{t.idle}</span>
</div>
- <details className="pose-test" open><summary>{t.pose}</summary>
- <button className="reset-pose" onClick={() => { setParameters({}); setStress(false); }}>{t.reset}</button>
+ <div className="preview-tabs" role="tablist" aria-label={t.preview}>
+ <button id="pose-tab" role="tab" aria-selected={tab === 'pose'} aria-controls="pose-panel" onClick={() => setTab('pose')}>{t.pose}</button>
+ <button id="lip-tab" role="tab" aria-selected={tab === 'lip'} aria-controls="lip-panel" onClick={() => setTab('lip')}>{t.lipSync}</button>
+ </div>
+ <div className="pose-test" id="pose-panel" role="tabpanel" aria-labelledby="pose-tab" hidden={tab !== 'pose'}>
<div className="sliders">
{sliders.map(slider => (
<label key={slider.id}>{t[slider.label]}
<input type="range" aria-label={t[slider.label]} min={slider.min} max={slider.max} step="0.05"
- value={parameters[slider.id === 'EyeOpen' ? 'eyeLOpen' : slider.id] ?? slider.def}
+ disabled={slider.id === 'mouthOpen' && lip !== null}
+ value={slider.id === 'mouthOpen' && lip ? liveMouth : parameters[slider.id === 'EyeOpen' ? 'eyeLOpen' : slider.id] ?? slider.def}
onChange={event => {
const value = Number(event.target.value);
setParameters(current => slider.id === 'EyeOpen'
? { ...current, eyeLOpen: value, eyeROpen: value }
: { ...current, [slider.id]: value });
}} />
- <output>{(parameters[slider.id === 'EyeOpen' ? 'eyeLOpen' : slider.id] ?? slider.def).toFixed(2)}</output>
+ <output>{(slider.id === 'mouthOpen' && lip ? liveMouth : parameters[slider.id === 'EyeOpen' ? 'eyeLOpen' : slider.id] ?? slider.def).toFixed(2)}</output>
</label>
))}
</div>
- <button className="sweep-button" title={t.sweepTip} onClick={() => setStress(current => !current)}>{stress ? t.stopSweep : t.sweep}</button>
- </details>
+ <div className="pose-actions"><button className="reset-pose" onClick={() => { setParameters({}); setStress(false); setLip(null); }}>{t.reset}</button>
+ <button className="sweep-button" title={t.sweepTip} onClick={() => setStress(current => !current)}>{stress ? t.stopSweep : t.sweep}</button></div>
+ </div>
+ <div className="lip-sync" id="lip-panel" role="tabpanel" aria-labelledby="lip-tab" hidden={tab !== 'lip'}>
+ <div className="vowel-buttons" role="group" aria-label={t.lipSync}>
+ {(Object.keys(VOWELS) as Vowel[]).map(vowel => <button key={vowel} aria-pressed={lip?.kind === 'hold' && lip.vowel === vowel}
+ onClick={() => setLip(current => current?.kind === 'hold' && current.vowel === vowel ? null : { kind: 'hold', vowel })}>{VOWELS[vowel].label}</button>)}
+ <button onClick={() => setLip(null)}>{t.release}</button>
+ </div>
+ <div className="lip-text-row"><input aria-label={t.lipText} value={text} onChange={event => setText(event.target.value)} />
+ <button disabled={!text.trim()} onClick={() => setLip({ kind: 'text', text, speed, loop })}>{t.lipPlay}</button>
+ <button disabled={!lip} onClick={() => setLip(null)}>{t.lipStop}</button>
+ </div>
+ <div className="lip-options"><label className="lip-speed">{t.lipSpeed}<input type="range" min="4" max="12" step="1" value={speed} aria-label={t.lipSpeed}
+ onChange={event => { const value = Number(event.target.value); setSpeed(value); setLip(current => current?.kind === 'text' ? { ...current, speed: value } : current); }} /><output>{speed}</output></label>
+ <label><input type="checkbox" checked={loop} onChange={event => { const value = event.target.checked; setLoop(value); setLip(current => current?.kind === 'text' ? { ...current, loop: value } : current); }} />{t.lipLoop}</label>
+ </div>
+ {!hasMouthSprites && <p className="mouth-fallback">{t.mouthFallback} <button type="button" onClick={onDrawMouth}>{t.drawMouth}</button></p>}
+ <p className="lip-help">{t.lipHelp}</p>{skipped && <p className="lip-skipped">{t.skippedKana} {skipped}</p>}
+ <div className="sliders lip-live"><label>{t.mouthOpen}<input type="range" aria-label={t.mouthOpen} min="0" max="1" step="0.01" disabled={lip !== null}
+ value={lip ? liveMouth : parameters.mouthOpen ?? 0} onChange={event => setParameters(current => ({ ...current, mouthOpen: Number(event.target.value) }))} /><output>{(lip ? liveMouth : parameters.mouthOpen ?? 0).toFixed(2)}</output></label></div>
+ </div>
</section>
);
}
diff --git a/src/editor/RecentProjects.tsx b/src/editor/RecentProjects.tsx
new file mode 100644
index 0000000..c1655d3
--- /dev/null
+++ b/src/editor/RecentProjects.tsx
@@ -0,0 +1,25 @@
+import { useI18n } from './i18n';
+import type { LocalProject } from './project';
+import type { RecentProject } from './recent-projects';
+
+export function RecentProjects({ recent, projects, reopen, onOpen, onRemove, onClear, onReopen }: {
+ recent: RecentProject[]; projects: LocalProject[] | null; reopen: boolean;
+ onOpen(entry: RecentProject): void; onRemove(id: string): void; onClear(): void; onReopen(value: boolean): void;
+}) {
+ const { t, language } = useI18n();
+ return <section className="recent-projects" aria-label={t.recent}>
+ <div className="recent-heading"><h3>{t.recent}</h3>{recent.length > 0 && <button onClick={onClear}>{t.clearHistory}</button>}</div>
+ {recent.length === 0 && <p className="recent-empty">{t.noRecent}</p>}
+ <div className="recent-list">{recent.map(entry => {
+ const browse = entry.kind === 'folder' ? !entry.hasHandle : !projects?.some(item => item.name === entry.serverName);
+ return <div key={entry.id} className="recent-row">
+ <button data-testid={`recent-${entry.id}`} onClick={() => onOpen(entry)}>
+ <strong>{entry.serverName === 'sample-miko-qipao' ? t.sampleProject : entry.name}</strong>
+ <small>{entry.relativePath} · {new Date(entry.lastOpened).toLocaleString(language === 'ja' ? 'ja-JP' : 'en-GB')}</small>
+ {browse && <small>{t.browseAgain}</small>}
+ </button><button className="remove-recent" aria-label={`${t.removeRecent} ${entry.name}`} onClick={() => onRemove(entry.id)}>×</button>
+ </div>;
+ })}</div>
+ <label className="reopen-setting"><input type="checkbox" checked={reopen} onChange={event => onReopen(event.target.checked)} />{t.reopenLast}</label>
+ </section>;
+}
diff --git a/src/editor/VariantsPanel.tsx b/src/editor/VariantsPanel.tsx
new file mode 100644
index 0000000..849eaf6
--- /dev/null
+++ b/src/editor/VariantsPanel.tsx
@@ -0,0 +1,81 @@
+import { useEffect, useRef, useState } from 'react';
+import { useI18n } from './i18n';
+import { variantNames, projectVariantRequests, ProjectJobError, type LocalProject, type ProjectJob, type JobResult, type VariantRequest } from './project';
+import { AskAgent } from './AskAgent';
+import { JobFeedback } from './JobFeedback';
+import { CopyButton } from './CopyButton';
+
+const labels = ['eyesClosed', 'eyesHalf', 'eyesSmile', 'mouthA', 'mouthAHalf', 'mouthI', 'mouthO'] as const;
+export function VariantsPanel({ project, projectPath, assets, rootPath, busy, stale, onRun, mouthRequest, onMouthPresence }: {
+ project: LocalProject | null; projectPath?: string; assets?: Record<string, string>; rootPath?: string; busy: boolean; stale: boolean;
+ onRun: (action: ProjectJob, files?: File[]) => Promise<JobResult>; mouthRequest: number; onMouthPresence: (present: boolean) => void;
+}) {
+ const { t } = useI18n();
+ const [present, setPresent] = useState<string[]>([]), [requests, setRequests] = useState<VariantRequest[]>([]);
+ const [eyes, setEyes] = useState(false), [mouth, setMouth] = useState(false);
+ const target = eyes && mouth ? 'both' : eyes ? 'eyes' : 'mouth';
+ const panel = useRef<HTMLDetailsElement>(null), mouthCheckbox = useRef<HTMLInputElement>(null);
+ useEffect(() => {
+ if (!mouthRequest) return;
+ setMouth(true);
+ if (panel.current) { panel.current.open = true; panel.current.scrollIntoView({ behavior: 'smooth', block: 'start' }); }
+ mouthCheckbox.current?.focus({ preventScroll: true });
+ }, [mouthRequest]);
+ const [error, setError] = useState<ProjectJobError | null>(null), [log, setLog] = useState(''), [accepted, setAccepted] = useState(false), [notice, setNotice] = useState('');
+ const [lastAction, setLastAction] = useState<ProjectJob>('variant-requests');
+ const input = useRef<HTMLInputElement>(null), current = useRef(0);
+ const writable = !!project && !project.readOnly;
+ useEffect(() => { setEyes(false); setMouth(false); setError(null); setLog(''); setAccepted(false); setNotice(''); }, [project?.name, projectPath]);
+ useEffect(() => {
+ const revision = ++current.current;
+ setPresent([]); setRequests([]);
+ const url = assets?.['sprites/sprites.json'] ?? (!assets ? '/miko-qipao/built/sprites/sprites.json' : undefined);
+ if (url) void fetch(url).then(response => response.ok ? response.json() : null).then(data => {
+ if (revision !== current.current) return;
+ const ready = data?.layers ? variantNames.filter(name => name.startsWith('eyes_') ? `${name}_0` in data.layers && `${name}_1` in data.layers : name in data.layers) : [];
+ setPresent(ready); onMouthPresence(ready.some(name => name.startsWith('mouth_')));
+ }).catch(() => { if (revision === current.current) onMouthPresence(false); });
+ else onMouthPresence(false);
+ if (project) void projectVariantRequests(project).then(value => { if (revision === current.current) setRequests(value); });
+ }, [project?.name, assets, onMouthPresence]);
+ const run = async (action: ProjectJob, files?: File[]) => {
+ setLastAction(action);
+ setError(null); setLog(''); setAccepted(false); setNotice('');
+ try {
+ const result = await onRun(action, files);
+ setLog(result.log); if (result.requests) { setRequests(result.requests); setNotice(t.requestsReady); }
+ setAccepted(action === 'import-variants');
+ } catch (error) { setError(error instanceof ProjectJobError ? error : new ProjectJobError('toolFailed')); }
+ };
+ const count = (prefix: string, total: number) => {
+ const value = present.filter(name => name.startsWith(prefix)).length;
+ return value ? t.variantCount.replace('COUNT', String(value)).replace('TOTAL', String(total)) : t.variantMissing;
+ };
+ return <details ref={panel} className="panel variants-panel" data-testid="variants-panel" open>
+ <summary>{t.variantsTitle}</summary>
+ <div className="variants-content"><p>{t.variantsHelp}</p>
+ <div className="variant-choices">
+ <label><input type="checkbox" checked={eyes} onChange={event => setEyes(event.target.checked)} aria-label={t.targetEyes} /><span>{t.targetEyes}<small>{t.eyesKinds}</small></span><span className="variant-count">{count('eyes_', 3)}</span></label>
+ <label><input ref={mouthCheckbox} type="checkbox" checked={mouth} onChange={event => setMouth(event.target.checked)} aria-label={t.targetMouth} /><span>{t.targetMouth}<small>{t.mouthKinds}</small></span><span className="variant-count">{count('mouth_', 4)}</span></label>
+ </div>
+ {(eyes || mouth) && projectPath && (projectPath.startsWith('<') ? <p className="workflow-note">{t.agentUnknownPath}</p> : <AskAgent rootPath={rootPath} projectPath={project?.readOnly || projectPath === 'samples/miko-qipao' ? 'projects/miko-qipao-variants' : projectPath} readOnlySource={project?.readOnly || projectPath === 'samples/miko-qipao' ? projectPath : undefined} target={target} />)}
+ <details className="manual-variants"><summary>{t.manualVariants}</summary>
+ <p>{t.manualHelp}</p>
+ {writable ? <button disabled={busy || stale} onClick={() => { void run('variant-requests'); }}>{busy ? t.operationBusy : t.createRequests}</button> : <p className="workflow-note">{t.variantsUnavailable}</p>}
+ {stale && <p className="workflow-note">{t.variantsNeedRebuild}</p>}
+ {requests.length > 0 && <div className="variant-requests">{requests.filter(request => (!eyes && !mouth) || target === 'both' || request.name.startsWith(target === 'eyes' ? 'eyes_' : 'mouth_')).map(request => <details key={request.name}>
+ <summary>{t[labels[variantNames.indexOf(request.name)]]} · <code>{request.name}.png</code></summary>
+ <img src={request.maskUrl} alt={`${t.requestFiles}: ${t[labels[variantNames.indexOf(request.name)]]}`} />
+ <p><code>{project?.relativePath}/source.png</code><br /><code>{project?.relativePath}/variant-requests/{request.name}/mask.png</code></p>
+ <div className="workflow-buttons"><CopyButton value={request.prompt} label={t.copyVariantPrompt} /><CopyButton value={`${project?.relativePath}/source.png\n${project?.relativePath}/variant-requests/${request.name}/mask.png\n${project?.relativePath}/variant-requests/${request.name}/prompt.md`} label={t.copyFilePaths} /></div>
+ <pre>{request.prompt}</pre><p>{t.variantOutput}: <code>{project?.relativePath}/variants/{request.name}.png</code></p>
+ </details>)}</div>}
+ {writable && <div className="variant-drop" data-testid="variant-drop" aria-disabled={busy || stale} onDragOver={event => { event.preventDefault(); event.stopPropagation(); }} onDrop={event => { event.preventDefault(); event.stopPropagation(); if (!busy && !stale) void run('import-variants', Array.from(event.dataTransfer.files)); }}>
+ <strong>{t.dropVariants}</strong><p>{t.variantFilenameHelp}</p><button disabled={busy || stale} onClick={() => input.current?.click()}>{t.chooseVariants}</button>
+ <input ref={input} hidden multiple type="file" accept=".png,image/png" aria-label={t.variantFiles} disabled={busy || stale} onChange={event => { if (event.target.files) void run('import-variants', Array.from(event.target.files)); event.target.value = ''; }} />
+ </div>}
+ </details>
+ {notice && <p role="status">{notice}</p>}<JobFeedback error={error} log={log} accepted={accepted} importing={lastAction === 'import-variants'} />
+ </div>
+ </details>;
+}
diff --git a/src/editor/history.ts b/src/editor/history.ts
index e19f68d..12204be 100644
--- a/src/editor/history.ts
+++ b/src/editor/history.ts
@@ -8,6 +8,11 @@ export class RigHistory {
constructor(rig: Rig) { this.present = structuredClone(rig); }
get canUndo() { return this.past.length > 0; }
get canRedo() { return this.future.length > 0; }
+ load(rig: Rig) {
+ this.present = structuredClone(rig);
+ this.past = []; this.future = []; this.gesture = null;
+ }
+ replacePresent(rig: Rig) { this.present = structuredClone(rig); }
begin() { if (!this.gesture) this.gesture = structuredClone(this.present); }
change(rig: Rig) {
if (JSON.stringify(rig) === JSON.stringify(this.present)) return;
diff --git a/src/editor/i18n.tsx b/src/editor/i18n.tsx
index 70d26de..46cd1d5 100644
--- a/src/editor/i18n.tsx
+++ b/src/editor/i18n.tsx
@@ -1,19 +1,24 @@
import { createContext, useContext, useEffect, useState, type ReactNode } from 'react';
+import { readPreference, savePreference } from './preferences';
+import { workflowEn, workflowJa } from './workflow-i18n';
+export { readPreference, savePreference } from './preferences';
export type Language = 'en' | 'ja';
export const LANGUAGE_KEY = 'mesh-avatar-language';
export const GUIDE_KEY = 'mesh-avatar-guide-seen';
-export function readPreference(key: string) {
- try { return localStorage.getItem(key); } catch { return null; }
-}
-export function savePreference(key: string, value: string) {
- try { localStorage.setItem(key, value); } catch { /* The editor also works without storage. */ }
-}
const en = {
+ ...workflowEn,
product: 'Mesh Avatar Studio', subtitle: 'Shape the motion in your illustration',
- tools: 'Project tools', openProject: 'Open project', openRig: 'Open rig.json…', openFolder: 'Open image folder…',
- rigFile: 'Open rig file', folderFiles: 'Open image folder files', save: 'Save rig', undo: 'Undo', redo: 'Redo',
+ tools: 'Project tools', openProject: 'Open project', openRig: 'Load rig.json only…', openFolder: 'Browse for a project folder…',
+ rigFile: 'Open rig file', folderFiles: 'Open project folder files', save: 'Save rig', undo: 'Undo', redo: 'Redo',
+ projectHelp: 'A project is the folder an agent created from your illustration: rig.json, source.png and built/.',
+ recent: 'Recent', noRecent: 'No recent projects.', clearHistory: 'Clear history', removeRecent: 'Remove from history',
+ reopenLast: 'Reopen last project on start', browseAgain: 'Browse again', missingRecent: 'Project no longer exists; removed from history:',
+ localProjects: 'Local projects', noProjects: 'No local projects yet.', sampleProject: 'Sample project', updated: 'Updated',
+ drawnVariants: 'Drawn eyes/mouths', readOnly: 'Read-only', copyPath: 'Copy full path', copied: 'Path copied',
+ showFinder: 'Show in Finder', showFolder: 'Show in folder', unknownPath: 'Browser-picked project · full path unavailable',
+ savedTo: 'Saved to', saveError: 'Could not save the project. Your edits are still in the editor; try again.', revealError: 'Could not open the project folder.', copyError: 'Could not copy the path.',
help: 'Help', close: 'Close help', language: 'Language', english: 'English', japanese: '日本語', enCode: 'EN', jaCode: 'JA',
parts: 'Parts', faceSection: 'Face', hairSection: 'Hair & accessories', bodySection: 'Body', advanced: 'Advanced',
notPresent: 'Not in this rig', show: 'Show overlay', hide: 'Hide overlay', showAll: 'Show all', hideAll: 'Hide all',
@@ -23,6 +28,8 @@ const en = {
panHint: 'Scroll to zoom · Space-drag to pan', noDots: 'Edit the values in the selected part card',
preview: 'Live preview', idle: 'Idle motion', play: 'Play idle motion', pause: 'Pause idle motion',
pose: 'Pose test', reset: 'Reset', sweep: 'Sweep angles', stopSweep: 'Stop sweep',
+ lipSync: 'Lip sync', release: 'Release', lipText: 'Kana text', lipPlay: 'Play', lipStop: 'Stop',
+ lipSpeed: 'Morae per second', lipLoop: 'Loop', skippedKana: 'Skipped characters:', lipHelp: 'Hiragana, katakana and spaces. Preview only; no audio.',
sweepTip: 'Swings the head through its full range to find tears',
turn: 'Turn left/right', look: 'Look up/down', tilt: 'Tilt', eyeOpen: 'Eyes open', mouthOpen: 'Mouth open', bodyTilt: 'Body tilt',
loading: 'Loading local assets…', updating: 'Updating preview…', ready: 'Engine ready', previewError: 'Preview could not load',
@@ -31,18 +38,27 @@ const en = {
guide3: 'Watch the preview on the right', gotIt: 'Got it', shortcuts: 'Keyboard & mouse', guideAgain: 'Show the guide again',
shortcutUndo: 'Undo / redo', shortcutSave: 'Save rig', shortcutPan: 'Pan the image', shortcutZoom: 'Zoom the image',
shortcutVertex: 'Add / remove a dot', spaceDrag: 'Space + drag', wheel: 'Scroll', vertexKeys: 'Double-click a line / Alt-click a dot',
- stale: 'Outlines changed — cut-out images will be rebuilt by the layer builder (coming later). Preview still uses the old images.',
+ stale: 'Outlines changed. Save the rig, run build-layers, then reopen the project. Preview still uses the old images.',
changedParts: 'Changed parts', checking: 'Checking for sample images…', emptyTitle: 'Open a project',
- emptyHelp: 'Open rig.json and an image folder to begin. Sample images are installed separately.',
+ emptyHelp: 'Choose a project from Open project, or browse for its folder. Sample images are installed separately.',
+ newProjectHelp: 'Starting from a new illustration? Ask your agent to prepare a project using the agent guide.',
invalidRig: 'Could not open the rig file. Check the JSON and these field paths:',
invalidFolder: 'Could not open this folder. Include source.png, rig.json, layers.json and every cut-out image.',
invalidValue: 'Check the values at these field paths:', point: 'point', node: 'node', strand: 'Strand', eye: 'Eye',
accessory: 'Accessory', x: 'X', y: 'Y', px: 'px',
};
const ja: typeof en = {
+ ...workflowJa,
product: 'Mesh Avatar Studio', subtitle: 'イラストの動く範囲を調整',
- tools: 'プロジェクト操作', openProject: 'プロジェクトを開く', openRig: '設定ファイルを開く…', openFolder: '画像フォルダーを開く…',
- rigFile: '設定ファイルを開く', folderFiles: '画像フォルダーのファイルを開く', save: '設定を保存', undo: '元に戻す', redo: 'やり直す',
+ tools: 'プロジェクト操作', openProject: 'プロジェクトを開く', openRig: 'rig.json だけ読み込む…', openFolder: 'プロジェクトフォルダを選ぶ…',
+ rigFile: '設定ファイルを開く', folderFiles: 'プロジェクトフォルダのファイルを開く', save: '設定を保存', undo: '元に戻す', redo: 'やり直す',
+ projectHelp: 'プロジェクトは、AI エージェントがイラストから作ったフォルダです(rig.json・source.png・built/ を含む)。',
+ recent: '最近開いたプロジェクト', noRecent: '履歴はまだありません。', clearHistory: '履歴を消去', removeRecent: '履歴から削除',
+ reopenLast: '起動時に最後のプロジェクトを開く', browseAgain: 'フォルダを選び直す', missingRecent: 'プロジェクトが見つからないため、履歴から削除しました:',
+ localProjects: 'ローカルのプロジェクト', noProjects: 'プロジェクトはまだありません。', sampleProject: 'サンプル', updated: '更新',
+ drawnVariants: '目・口の差分画像あり', readOnly: '読み取り専用', copyPath: 'フルパスをコピー', copied: 'パスをコピーしました',
+ showFinder: 'フォルダを開く', showFolder: 'フォルダを開く', unknownPath: 'ブラウザで選択 · フルパスは取得できません',
+ savedTo: '保存しました:', saveError: 'プロジェクトを保存できませんでした。編集内容は画面に残っています。もう一度お試しください。', revealError: 'プロジェクトのフォルダを開けませんでした。', copyError: 'パスをコピーできませんでした。',
help: 'ヘルプ', close: 'ヘルプを閉じる', language: '言語', english: '英語', japanese: '日本語', enCode: '英語', jaCode: '日本語',
parts: 'パーツ', faceSection: '顔', hairSection: '髪・飾り', bodySection: '体', advanced: '詳細設定',
notPresent: 'この設定にはありません', show: 'ガイドを表示', hide: 'ガイドを非表示', showAll: 'すべて表示', hideAll: 'すべて非表示',
@@ -52,6 +68,8 @@ const ja: typeof en = {
panHint: 'スクロールで拡大・縮小 · スペース+ドラッグで移動', noDots: '右の選択パーツ欄で数値を調整',
preview: '動きのプレビュー', idle: '待機中の動き', play: '待機中の動きを再生', pause: '待機中の動きを一時停止',
pose: 'ポーズ確認', reset: 'リセット', sweep: '角度を連続確認', stopSweep: '連続確認を停止',
+ lipSync: '口の動き', release: '解除', lipText: 'かなの文章', lipPlay: '再生', lipStop: '停止',
+ lipSpeed: '1秒あたりの拍数', lipLoop: '繰り返し', skippedKana: '読み飛ばす文字:', lipHelp: 'ひらがな・カタカナ・空白に対応。音声は再生しません。',
sweepTip: '頭を最大角度まで動かし、画像の隙間や破れを確認します',
turn: '顔を左右に向ける', look: '顔を上下に向ける', tilt: '頭を傾ける', eyeOpen: '目の開き', mouthOpen: '口の開き', bodyTilt: '体を傾ける',
loading: '画像を読み込み中…', updating: 'プレビューを更新中…', ready: 'プレビューの準備完了', previewError: 'プレビューを読み込めませんでした',
@@ -60,9 +78,10 @@ const ja: typeof en = {
guide3: '右のプレビューで動きを見る', gotIt: 'わかりました', shortcuts: 'キーボード・マウス操作', guideAgain: '使い方をもう一度表示',
shortcutUndo: '元に戻す/やり直す', shortcutSave: '設定を保存', shortcutPan: '画像を移動', shortcutZoom: '画像を拡大・縮小',
shortcutVertex: '点を追加/削除', spaceDrag: 'スペース+ドラッグ', wheel: 'スクロール', vertexKeys: '線をダブルクリック/⌥+点をクリック',
- stale: '輪郭が変更されました。切り抜き画像の再生成機能は今後追加予定です。プレビューには変更前の画像を使っています。',
+ stale: '輪郭が変更されました。設定を保存し、build-layersを実行してからプロジェクトを開き直してください。プレビューには変更前の画像を使っています。',
changedParts: '変更されたパーツ', checking: 'サンプル画像を確認中…', emptyTitle: 'プロジェクトを開く',
- emptyHelp: '設定ファイルと画像フォルダーを開いてください。サンプル画像は別途用意が必要です。',
+ emptyHelp: '「プロジェクトを開く」の一覧から選ぶか、フォルダを指定してください。サンプル画像は別途用意が必要です。',
+ newProjectHelp: '新しいイラストでは、エージェントに手順書に沿ってプロジェクトを作るよう依頼してください。',
invalidRig: '設定ファイルを開けませんでした。JSONの形式と次の項目を確認してください:',
invalidFolder: 'フォルダーを開けませんでした。source.png、rig.json、layers.json とすべての切り抜き画像を用意してください。',
invalidValue: '次の項目の数値を確認してください:', point: '点', node: '節点', strand: '髪の束', eye: '目',
diff --git a/src/editor/preferences.ts b/src/editor/preferences.ts
new file mode 100644
index 0000000..2a7b943
--- /dev/null
+++ b/src/editor/preferences.ts
@@ -0,0 +1,6 @@
+export function readPreference(key: string) {
+ try { return localStorage.getItem(key); } catch { return null; }
+}
+export function savePreference(key: string, value: string) {
+ try { localStorage.setItem(key, value); } catch { /* Editing also works without storage. */ }
+}
diff --git a/src/editor/project.ts b/src/editor/project.ts
index 717e136..eab3ac9 100644
--- a/src/editor/project.ts
+++ b/src/editor/project.ts
@@ -7,7 +7,7 @@ export interface ProjectAssets {
rig?: Rig;
urls: string[];
}
-export async function openImageFolder(files: File[]): Promise<ProjectAssets> {
+export async function openProjectFolder(files: File[]): Promise<ProjectAssets> {
const entries = files.map(file => ({ file, path: file.webkitRelativePath.replace(/^[^/]+\//, '') || file.name }));
const source = entries.find(entry => /(^|\/)source\.png$/i.test(entry.path));
const layers = entries.find(entry => /(^|\/)layers\.json$/i.test(entry.path));
@@ -32,6 +32,89 @@ export async function openImageFolder(files: File[]): Promise<ProjectAssets> {
return { sourceUrl, assets, rig, urls };
}
+export interface LocalProject {
+ name: string;
+ relativePath: string;
+ absolutePath: string;
+ updatedAt: string;
+ hasSprites: boolean;
+ hasVariants: boolean;
+ readOnly: boolean;
+ rigFile?: 'rig.json' | 'rig.draft.json';
+}
+export async function localProjects(): Promise<LocalProject[] | null> {
+ if (!import.meta.env.DEV) return null;
+ try {
+ const response = await fetch('/__studio/projects');
+ if (!response.ok || !response.headers.get('content-type')?.includes('application/json')) return null;
+ const list = await response.json();
+ return Array.isArray(list) ? list : null;
+ } catch { return null; }
+}
+const baseUrl = (project: LocalProject) => `/__studio/projects/${encodeURIComponent(project.name)}/`;
+export async function repositoryContext(): Promise<{ rootPath: string } | null> {
+ if (!import.meta.env.DEV) return null;
+ try { const response = await fetch('/__studio/context'); return response.ok && response.headers.get('content-type')?.includes('application/json') ? await response.json() : null; } catch { return null; }
+}
+export async function revealRepository() {
+ const response = await fetch('/__studio/reveal', { method: 'POST' });
+ if (!response.ok) throw new Error('Could not open repository.');
+}
+export const variantNames = ['eyes_closed', 'eyes_half', 'eyes_smile', 'mouth_a', 'mouth_a_half', 'mouth_i', 'mouth_o'] as const;
+export type VariantName = typeof variantNames[number];
+export interface VariantRequest { name: VariantName; prompt: string; maskUrl: string }
+export interface JobResult { path: string; log: string; requests?: VariantRequest[] }
+export type ProjectJob = 'rebuild' | 'variant-requests' | 'import-variants';
+export class ProjectJobError extends Error {
+ constructor(public code: string, public log = '') { super(code); }
+}
+export async function runProjectJob(project: LocalProject, action: ProjectJob, rig: Rig, files: File[] = []): Promise<JobResult> {
+ let body: object = { rig };
+ if (action === 'import-variants') {
+ if (!files.length || files.length > 7 || new Set(files.map(file => file.name)).size !== files.length || files.some(file => !variantNames.some(name => file.name === `${name}.png`) || file.size > 24 * 1024 * 1024) || files.reduce((sum, file) => sum + file.size, 0) > 26 * 1024 * 1024) throw new ProjectJobError('invalidImages');
+ body = { files: await Promise.all(files.map(file => new Promise<{ name: string; data: string }>((done, reject) => {
+ const reader = new FileReader(); reader.onload = () => done({ name: file.name, data: String(reader.result).split(',')[1] }); reader.onerror = () => reject(new ProjectJobError('invalidImages')); reader.readAsDataURL(file);
+ }))) };
+ }
+ const response = await fetch(`${baseUrl(project)}${action}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
+ const result = await response.json();
+ if (!response.ok) throw new ProjectJobError(result.code ?? (response.status === 400 ? 'invalidImages' : 'toolFailed'), result.log ?? result.error);
+ return result;
+}
+export async function projectVariantRequests(project: LocalProject): Promise<VariantRequest[]> {
+ try { return (await jsonFile(`${baseUrl(project)}variants`)).requests ?? []; } catch { return []; }
+}
+async function jsonFile(url: string) {
+ const response = await fetch(url);
+ if (!response.ok) throw new Error('Project could not load.');
+ return response.json();
+}
+export async function openLocalProject(project: LocalProject): Promise<ProjectAssets> {
+ const base = baseUrl(project), metadata = await jsonFile(`${base}built/layers.json`), version = `?v=${Date.now()}`;
+ const raw = await jsonFile(`${base}${project.rigFile ?? 'rig.json'}`);
+ // A draft with built layers can use the curves recorded by build-layers.
+ if (project.rigFile === 'rig.draft.json' && Array.isArray(metadata.eyes)) raw.eyes = raw.eyes.map((eye: object, index: number) => ({ ...eye, ...metadata.eyes[index] }));
+ const rig = parseRig(raw);
+ const assets: Record<string, string> = {};
+ for (const name of ['layers.json', 'base.png', 'hairmask.png', ...Object.keys(metadata.layers).map(name => `${name}.png`)]) assets[name] = `${base}built/${name}${version}`;
+ if (project.hasSprites) {
+ const sprites = await jsonFile(`${base}built/sprites/sprites.json`);
+ assets['sprites/sprites.json'] = `${base}built/sprites/sprites.json${version}`;
+ for (const name of Object.keys(sprites.layers)) assets[`sprites/${name}.png`] = `${base}built/sprites/${name}.png${version}`;
+ }
+ // Check images before replacing the editor's current project.
+ const urls = [`${base}source.png${version}`, ...Object.entries(assets).filter(([name]) => name.endsWith('.png')).map(([, url]) => url)];
+ await Promise.all(urls.map(url => new Promise<void>((done, reject) => {
+ const image = new Image(); image.onload = () => done(); image.onerror = () => reject(new Error('Missing project image.')); image.src = url;
+ })));
+ return { sourceUrl: `${base}source.png${version}`, assets, rig, urls: [] };
+}
+export async function projectAction(project: LocalProject, action: 'rig' | 'reveal', rig?: Rig): Promise<{ path: string }> {
+ const response = await fetch(`${baseUrl(project)}${action}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: rig ? JSON.stringify(rig) : '{}' });
+ if (!response.ok) throw new Error('Project operation failed.');
+ return response.json();
+}
+
export function sampleImagesAvailable(): Promise<boolean> {
return Promise.all(['/miko-qipao/source.png', '/miko-qipao/built/base.png'].map(url => new Promise<boolean>(resolve => {
const image = new Image();
diff --git a/src/editor/recent-projects.ts b/src/editor/recent-projects.ts
new file mode 100644
index 0000000..6a2bd2f
--- /dev/null
+++ b/src/editor/recent-projects.ts
@@ -0,0 +1,77 @@
+import { readPreference, savePreference } from './preferences';
+
+export const RECENT_KEY = 'mesh-avatar-recent-projects';
+export const REOPEN_KEY = 'mesh-avatar-reopen-project';
+export interface RecentProject {
+ id: string;
+ name: string;
+ relativePath: string;
+ lastOpened: string;
+ kind: 'server' | 'folder';
+ serverName?: string;
+ hasHandle?: boolean;
+}
+export function readRecent(): RecentProject[] {
+ try {
+ const value: unknown = JSON.parse(readPreference(RECENT_KEY) ?? '[]');
+ if (!Array.isArray(value)) return [];
+ return value.filter((entry): entry is RecentProject => entry && typeof entry.id === 'string' && typeof entry.name === 'string'
+ && typeof entry.relativePath === 'string' && typeof entry.lastOpened === 'string' && Number.isFinite(Date.parse(entry.lastOpened))
+ && (entry.kind === 'folder' || (entry.kind === 'server' && typeof entry.serverName === 'string'))).slice(0, 10);
+ } catch { return []; }
+}
+export function addRecent(current: RecentProject[], entry: RecentProject) {
+ return [entry, ...current.filter(item => item.id !== entry.id)].slice(0, 10);
+}
+export function saveRecent(value: RecentProject[]) { savePreference(RECENT_KEY, JSON.stringify(value)); }
+
+export type ProjectDirectory = FileSystemDirectoryHandle & {
+ queryPermission(options: { mode: 'read' }): Promise<PermissionState>;
+ requestPermission(options: { mode: 'read' }): Promise<PermissionState>;
+ values(): AsyncIterable<FileSystemDirectoryHandle | FileSystemFileHandle>;
+};
+export function hasDirectoryPicker() { return typeof (window as unknown as { showDirectoryPicker?: unknown }).showDirectoryPicker === 'function'; }
+export function pickDirectory(): Promise<ProjectDirectory> {
+ return (window as unknown as { showDirectoryPicker(options: { mode: 'read' }): Promise<ProjectDirectory> }).showDirectoryPicker({ mode: 'read' });
+}
+export async function directoryFiles(handle: ProjectDirectory): Promise<File[]> {
+ const files: File[] = [];
+ async function walk(directory: ProjectDirectory, prefix: string) {
+ for await (const entry of directory.values()) {
+ const path = `${prefix}/${entry.name}`;
+ if (entry.kind === 'directory') await walk(entry as ProjectDirectory, path);
+ else {
+ const file = await (entry as FileSystemFileHandle).getFile();
+ Object.defineProperty(file, 'webkitRelativePath', { value: path }); files.push(file);
+ }
+ }
+ }
+ // Read only the files the editor uses, not scratch images or unrelated project documents.
+ for await (const entry of handle.values()) {
+ if (entry.kind === 'directory' && entry.name === 'built') await walk(entry as ProjectDirectory, `${handle.name}/built`);
+ else if (entry.kind === 'file' && ['rig.json', 'source.png'].includes(entry.name)) {
+ const file = await (entry as FileSystemFileHandle).getFile();
+ Object.defineProperty(file, 'webkitRelativePath', { value: `${handle.name}/${entry.name}` }); files.push(file);
+ }
+ }
+ return files;
+}
+async function handleStore<T>(mode: IDBTransactionMode, operation: (store: IDBObjectStore) => IDBRequest<T>): Promise<T | undefined> {
+ let db: IDBDatabase | undefined;
+ try {
+ db = await new Promise<IDBDatabase>((done, reject) => {
+ const request = indexedDB.open('mesh-avatar-project-handles', 1);
+ request.onupgradeneeded = () => request.result.createObjectStore('directories');
+ request.onerror = () => reject(request.error); request.onsuccess = () => done(request.result);
+ });
+ return await new Promise<T>((done, reject) => {
+ const tx = db!.transaction('directories', mode), request = operation(tx.objectStore('directories'));
+ tx.oncomplete = () => done(request.result); tx.onerror = () => reject(tx.error); tx.onabort = () => reject(tx.error);
+ });
+ } catch { return undefined; }
+ finally { db?.close(); }
+}
+export async function keepDirectory(id: string, handle: ProjectDirectory) { return (await handleStore('readwrite', store => store.put(handle, id))) !== undefined; }
+export function restoreDirectory(id: string) { return handleStore<ProjectDirectory>('readonly', store => store.get(id)); }
+export function forgetDirectory(id: string) { return handleStore('readwrite', store => store.delete(id)); }
+export function clearDirectories() { return handleStore('readwrite', store => store.clear()); }
diff --git a/src/editor/stale.ts b/src/editor/stale.ts
index 5544ece..759d4f2 100644
--- a/src/editor/stale.ts
+++ b/src/editor/stale.ts
@@ -3,7 +3,12 @@ import type { Rig } from '../rig/types';
export function layerSignature(rig: Rig) {
return JSON.stringify({
image: rig.image,
+ head: { cx: rig.head.cx, cy: rig.head.cy, rx: rig.head.rx, ry: rig.head.ry },
+ buns: rig.buns ?? {},
+ body: { chest: rig.body.chest, shoulders: rig.body.shoulders },
eyes: rig.eyes,
+ face: { brow: rig.face.brow, mouth: rig.face.mouth, nose: rig.face.nose },
+ strands: (rig.strands ?? []).map(part => ({ nodes: part.nodes, sigma: part.sigma, max: part.max })),
hand: rig.hand ? {
outline: rig.hand.outline, jaw: rig.hand.jaw,
jawRange: rig.hand.jawRange, background: rig.hand.background,
diff --git a/src/editor/style.css b/src/editor/style.css
index 40b1603..62cefc3 100644
--- a/src/editor/style.css
+++ b/src/editor/style.css
@@ -33,8 +33,33 @@ button.primary:hover { color: white; background: #1d4ed8; }
.open-menu { position: relative; }
.open-menu > summary { display: flex; align-items: center; gap: 12px; list-style: none; }
summary::-webkit-details-marker { display: none; }
-.project-menu { position: absolute; right: 0; top: calc(100% + 8px); z-index: 5; min-width: 220px; padding: 8px; border: 1px solid var(--line); border-radius: var(--radius); background: white; box-shadow: 0 8px 24px #24334818; }
+.project-menu { position: absolute; right: 0; top: calc(100% + 8px); z-index: 5; width: min(420px, calc(100vw - 32px)); max-height: calc(100dvh - 100px); overflow: auto; padding: 8px; border: 1px solid var(--line); border-radius: var(--radius); background: white; box-shadow: 0 8px 24px #24334818; }
.project-menu button { display: block; width: 100%; border: 0; text-align: left; }
+.project-help { padding: 0 8px; font-size: 12px; color: var(--muted); }
+.project-list { max-height: 320px; overflow: auto; border-bottom: 1px solid var(--line); margin-bottom: 6px; }
+.project-list button { margin: 4px 0; background: #f5f7fb; }
+.project-list small { display: block; color: var(--muted); font-size: 11px; margin-top: 4px; overflow-wrap: anywhere; }
+.badge { display: inline-block; font-size: 10px; color: #395476; background: #edf3ff; border-radius: 4px; padding: 2px 4px; margin-left: 6px; }
+.recent-projects { padding: 8px; border-bottom: 1px solid var(--line); text-align: left; }
+.recent-heading { display: flex; align-items: center; justify-content: space-between; }
+.recent-heading h3 { color: var(--ink); }
+.recent-heading button { width: auto; padding: 4px 6px; color: var(--muted); font-size: 11px; }
+.recent-list { max-height: 200px; overflow: auto; }
+.recent-row { display: flex; align-items: center; }
+.recent-row > button:first-child { flex: 1; min-width: 0; padding: 6px; text-align: left; }
+.recent-row strong { font-size: 12px; }
+.recent-row small { display: block; overflow-wrap: anywhere; color: var(--muted); font-size: 11px; margin-top: 3px; }
+.recent-row .remove-recent { flex: 0 0 28px; width: 28px; padding: 4px; text-align: center; font-size: 18px; color: var(--muted); }
+.reopen-setting { display: flex; align-items: center; gap: 5px; margin-top: 6px; font-size: 11px; }
+.recent-empty { font-size: 12px; color: var(--muted); }
+.empty-project .recent-projects { max-width: 460px; margin: 16px auto 0; }
+.project-location { display: flex; align-items: center; gap: 8px; margin: -8px 0 12px; min-height: 32px; font-size: 12px; }
+.project-path { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }
+.project-location .icon-button { width: 28px; height: 28px; padding: 5px; }
+.save-notice { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 20; background: var(--ink); color: white; padding: 10px 16px; border-radius: 8px; font-size: 12px; }
+main:has(.project-location) .editor-panel { height: calc(100dvh - 132px); }
+main:has(.project-location) .parts-panel { max-height: calc(100dvh - 132px); }
+main:has(.project-location):has(.stale) .editor-panel { height: calc(100dvh - 212px); }
.workspace { display: grid; grid-template-columns: 240px minmax(0, 1fr) 420px; gap: 12px; align-items: start; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); min-width: 0; }
.panel-title { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; gap: 8px; border-bottom: 1px solid var(--line); min-height: 44px; }
@@ -68,17 +93,33 @@ summary::-webkit-details-marker { display: none; }
.handle-tooltip { position: absolute; z-index: 2; pointer-events: none; max-width: 210px; background: var(--ink); color: white; padding: 6px 8px; border-radius: 6px; font-size: 12px; box-shadow: 0 2px 8px #0002; }
.right-column { display: flex; flex-direction: column; gap: 12px; min-height: 0; }
.preview-panel { overflow: hidden; flex-shrink: 0; }
-.preview-canvas { display: block; height: 360px; width: auto; max-width: 100%; margin: 0 auto; }
+.preview-canvas { display: block; height: 420px; width: auto; max-width: 100%; margin: 0 auto; }
.preview-tools { display: flex; align-items: center; gap: 8px; padding: 2px 16px; border-top: 1px solid var(--line); font-size: 12px; }
.preview-tools .icon-button { width: 28px; height: 28px; border: 0; color: var(--accent); }
-.pose-test { border-top: 1px solid var(--line); position: relative; padding: 8px 16px; }
-.pose-test > summary { font-weight: 600; cursor: pointer; width: fit-content; }
-.reset-pose { position: absolute; right: 16px; top: 4px; border: 0; color: var(--accent); padding: 6px 0; font-size: 12px; background: none; }
-.sliders { display: grid; gap: 2px; margin: 6px 0; }
+.pose-test { position: relative; padding: 8px 16px; }
+.pose-actions { display: flex; gap: 8px; }
+.reset-pose { color: var(--accent); padding: 6px 8px; font-size: 12px; }
+.pose-actions .sweep-button { flex: 1; width: auto; }
+.sliders { display: grid; gap: 0; margin: 6px 0; }
.sliders label { display: grid; grid-template-columns: 120px minmax(0, 1fr) 32px; gap: 8px; align-items: center; min-height: 20px; font-size: 12px; }
.sliders input { min-width: 0; width: 100%; accent-color: var(--accent); margin: 0; }
.sliders output { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.sweep-button { width: 100%; font-size: 12px; padding: 6px 12px; }
+.lip-sync { padding: 8px 16px; }
+.preview-tabs { display: flex; padding: 4px 12px; gap: 4px; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
+.preview-tabs button { flex: 1; border: 0; font-size: 12px; padding: 6px; }
+.preview-tabs button[aria-selected="true"] { color: var(--accent); background: var(--accent-soft); font-weight: 600; }
+.vowel-buttons { display: flex; gap: 6px; margin: 10px 0 8px; }
+.vowel-buttons button { padding: 5px 8px; flex: 1; }
+.vowel-buttons button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); border-color: #b3cdfa; }
+.lip-text-row { display: flex; gap: 6px; align-items: center; }
+.lip-text-row input { min-width: 0; flex: 1; padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; }
+.lip-text-row button { padding: 6px 10px; }
+.lip-options { display: flex; gap: 10px; align-items: center; margin-top: 8px; font-size: 12px; }
+.lip-speed { display: flex; align-items: center; gap: 6px; flex: 1; }
+.lip-speed input { min-width: 0; width: 60px; flex: 1; accent-color: var(--accent); }
+.lip-help { font-size: 11px; color: var(--muted); margin: 6px 0 0; }
+.lip-skipped { font-size: 11px; color: #7c4b05; margin: 4px 0 0; }
.inspector { min-height: 160px; max-height: 400px; overflow: auto; padding: 16px; }
.selection-heading { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.part-description { font-size: 12px; color: var(--muted); }
@@ -111,6 +152,53 @@ summary::-webkit-details-marker { display: none; }
.stale small { display: block; margin-top: 4px; font-size: 12px; }
main:has(.stale) .editor-panel, main:has(.stale) .parts-panel { height: calc(100dvh - 180px); }
.empty-project { padding: 48px 24px; text-align: center; }
+.stale > p { margin: 0 0 4px; }
+.stale > button { margin-top: 8px; }
+.stale:has(> button) { display: grid; grid-template-columns: minmax(0, 1fr) auto; column-gap: 16px; align-items: center; }
+.stale:has(> button) > button { grid-column: 2; grid-row: 1 / span 2; margin-top: 0; }
+.stale:has(> button) > .job-feedback { grid-column: 1 / -1; }
+.ask-agent { border: 1px solid #b3cdfa; border-radius: 8px; padding: 12px; background: #f5f8ff; margin: 12px 0; text-align: left; }
+.empty-project .ask-agent { max-width: 620px; margin: 20px auto; }
+.agent-instruction { margin: 0 0 8px; font-size: 12px; }
+.agent-path { display: flex; gap: 6px; align-items: center; }
+.root-path { display: block; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 11px; background: white; padding: 6px; border-radius: 4px; }
+.ask-agent textarea { display: block; resize: vertical; width: 100%; border: 1px solid var(--line); border-radius: 6px; padding: 8px; font: inherit; font-size: 12px; margin: 8px 0; }
+.copy-button { flex-shrink: 0; white-space: nowrap; font-size: 12px; }
+.agent-path .icon-button { width: 28px; height: 28px; min-height: 28px; padding: 4px; }
+.mouth-fallback { margin: 4px 0; font-size: 10px; color: var(--muted); }
+.mouth-fallback button { color: var(--accent); border: 0; background: none; font: inherit; padding: 0; cursor: pointer; }
+.workflow-buttons { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; }
+.variants-panel { flex-shrink: 0; }
+.variants-panel > summary, .new-illustration > summary { display: flex; align-items: center; justify-content: flex-start; cursor: pointer; padding: 12px 16px; font-weight: 600; }
+.variants-panel > summary::before, .new-illustration > summary::before { content: '▸'; margin-right: 8px; }
+.variants-panel[open] > summary::before, .new-illustration[open] > summary::before { content: '▾'; }
+.variants-panel > summary span { margin-left: auto; font-weight: 400; color: var(--muted); }
+.variants-content { padding: 0 16px 12px; font-size: 12px; }
+.variants-content > p { color: var(--muted); }
+.variant-choices { display: grid; gap: 8px; margin: 12px 0; }
+.variant-choices > label { display: flex; align-items: center; gap: 8px; padding: 8px; border: 1px solid var(--line); border-radius: 6px; cursor: pointer; }
+.variant-choices small { display: block; margin-top: 3px; color: var(--muted); font-size: 11px; }
+.variant-count { margin-left: auto; flex-shrink: 0; color: var(--muted); font-size: 11px; }
+.manual-variants { margin-top: 14px; font-size: 11px; color: var(--muted); }
+.manual-variants > summary { cursor: pointer; }
+.manual-variants > p { line-height: 1.6; }
+.variant-requests > details { padding: 8px 0; border-bottom: 1px solid var(--line); }
+.variant-requests summary, .job-feedback summary { cursor: pointer; }
+.variant-requests img { display: block; max-width: 100%; max-height: 220px; margin: 8px auto; background: repeating-conic-gradient(#e3e8ed 0% 25%, white 0% 50%) 50% / 12px 12px; }
+.variant-requests pre, .job-feedback pre { white-space: pre-wrap; overflow-wrap: anywhere; max-height: 200px; overflow: auto; font-size: 11px; background: #f5f7fb; padding: 8px; }
+.variant-requests code, .workflow-note { overflow-wrap: anywhere; }
+.variant-drop { border: 1px dashed #8ea9d4; border-radius: 8px; background: #f5f8ff; padding: 12px; margin-top: 12px; }
+.variant-drop[aria-disabled="true"] { opacity: .6; }
+.job-feedback [role="alert"] { color: #a12828; }.job-feedback [role="status"] { color: #166534; }
+.job-feedback details { font-size: 11px; margin-top: 6px; }
+.new-illustration { flex-shrink: 0; }.new-illustration > .ask-agent { margin: 0 12px 12px; }
+@media (min-width: 1200px) {
+ .right-column { max-height: calc(100dvh - 96px); overflow: auto; scrollbar-width: thin; }
+ main:has(.project-location) .right-column { max-height: calc(100dvh - 132px); }
+ .right-column .inspector { flex-shrink: 0; }
+ main:has(.project-location):has(.stale) .right-column { max-height: calc(100dvh - 188px); }
+ main:has(.project-location):has(.stale) .parts-panel { height: calc(100dvh - 212px); }
+}
@media (min-width: 1200px) and (max-width: 1350px) {
.workspace { grid-template-columns: 240px minmax(0, 1fr) 380px; }
.preview-canvas { height: 300px; }
@@ -124,6 +212,7 @@ main:has(.stale) .editor-panel, main:has(.stale) .parts-panel { height: calc(100
.inspector { max-height: 650px; }
.preview-canvas { height: 240px; }
main:has(.stale) .editor-panel { height: 650px; }
+ main:has(.project-location) .editor-panel { height: 650px; }
}
@media (max-width: 760px) {
main { padding: 8px; }
diff --git a/src/editor/workflow-i18n.ts b/src/editor/workflow-i18n.ts
new file mode 100644
index 0000000..409639e
--- /dev/null
+++ b/src/editor/workflow-i18n.ts
@@ -0,0 +1,56 @@
+export const workflowEn = {
+ agentInstruction: 'Open Codex in this repository folder and paste the following message.',
+ variantsReloaded: 'Drawn variants loaded.', mouthFallback: 'No drawn mouth images; using mesh deformation.', drawMouth: '→ Draw mouth variants',
+ eyesKinds: 'Closed, half-closed, smiling', mouthKinds: 'あ, half-open あ, い, お', variantCount: 'COUNT of TOTAL images',
+ askAgent: 'Ask your agent',
+ copyPrompt: 'Copy message', copyDone: 'Copied', rootUnavailable: 'Open the folder containing this app and docs/agent-guide.md. Start the development server to copy its full path here.',
+ newIllustration: 'Create from a new illustration',
+ agentNewPrompt: 'Using the illustration I provide, create a project at PROJECT following docs/agent-guide.md Steps 1–5. Start with its model check and coordinate calibration, build the layers, and visually review the poses. Keep all image files inside this repository. If that folder already exists, use a new unused name. Tell me when it is ready to open from the app’s project menu.',
+ agentVariantPrompt: 'Create TARGET drawn variants for PROJECT following docs/agent-guide.md Step 6. Prepare the local masks and prompts, ask me before sending images to an external generator, import the returned drawings with build-sprites, and review the poses in Step 5. Keep all files inside this repository.',
+ agentCopySample: 'First copy the read-only project at SOURCE into PROJECT; if the destination exists, use an unused name. Keep the original project unchanged. ',
+ agentUnknownPath: 'The browser cannot read this folder’s full path. Open it from the development server’s project list to prepare an exact request.',
+ agentEyes: 'eye', agentMouth: 'mouth', agentBoth: 'eye and mouth',
+ rebuild: 'Save and rebuild layers', rebuilding: 'Saving and rebuilding layers…', staleLocal: 'Outlines changed. Rebuild layers to update the preview.',
+ rebuildUnavailable: 'Automatic rebuild needs a writable project from the local list. Browser-picked folders have no server path; save the rig, run build-layers from the repository root, then reopen the folder.',
+ jobFailed: 'The local tool could not finish. Previous layers were kept.', dependenciesHelp: 'Install uv and Python 3.10 or later, then try again. With Homebrew: brew install uv python. Setup: docs/agent-guide.md.',
+ unsafeFilesHelp: 'The project contains redirected files. Use regular files inside this project and try again.', showLog: 'Show log', operationBusy: 'Working locally…', reloadFailed: 'Could not reload the project. The current view is still open.',
+ variantsTitle: 'Drawn variants', variantMissing: 'Missing', variantsHelp: 'Drawn expressions make closed eyes and speaking look more natural.',
+ eyesClosed: 'Closed eyes', eyesHalf: 'Half-closed eyes', eyesSmile: 'Smiling eyes', mouthA: 'Mouth あ', mouthAHalf: 'Half-open あ', mouthI: 'Mouth い', mouthO: 'Mouth お',
+ targetEyes: 'Eyes', targetMouth: 'Mouth',
+ createRequests: 'Export masks and prompts', requestsReady: 'Local masks and prompts are ready.', requestFiles: 'Masks and prompts', copyVariantPrompt: 'Copy image prompt', copyFilePaths: 'Copy file paths',
+ manualVariants: 'If you prepare images yourself', manualHelp: 'Give your image editor source.png, the mask and its prompt. Save a full-size PNG with the filename below, then drop it here. Transparent mask pixels may change; opaque pixels must stay identical.',
+ variantOutput: 'Save the returned drawing as', dropVariants: 'Drop returned PNG images here', chooseVariants: 'Choose returned images', variantFiles: 'Returned variant images',
+ variantFilenameHelp: 'Use eyes_closed.png, eyes_half.png, eyes_smile.png, mouth_a.png, mouth_a_half.png, mouth_i.png or mouth_o.png. The app keeps the images local.',
+ importAccepted: 'Accepted; preview updated.', importRejected: 'Rejected; previous drawings and layers were kept.', importOutside: 'Pixels outside the edit mask changed.', importSize: 'The returned image must be a PNG with the same dimensions as source.png.', importMask: 'The edit mask is not valid for this drawing.',
+ importInvalid: 'Use the listed PNG filenames (one of each), up to 24 MB per file and 26 MB total.', metricsTitle: 'Measured comparison outside the mask', outsideMax: 'Maximum difference', outsideMean: 'Mean difference', outsidePixels: 'Pixels over tolerance',
+ variantsUnavailable: 'To create requests or import images here, open a writable project from the local development server. An agent can use docs/agent-guide.md Step 6 for other folders.',
+ variantsNeedRebuild: 'Rebuild the changed outlines first, so the masks match the current layers.',
+};
+export const workflowJa: typeof workflowEn = {
+ agentInstruction: 'このリポジトリのフォルダで Codex を開いて、次の文章を貼ってください。',
+ variantsReloaded: '描き分け画像を読み込みました', mouthFallback: '描き分け画像がないため、メッシュの変形で代用しています', drawMouth: '→ 口を描き分ける',
+ eyesKinds: '閉じた目・半目・笑った目', mouthKinds: 'あ・半分のあ・い・お', variantCount: 'TOTAL枚中COUNT枚',
+ askAgent: 'エージェントに依頼',
+ copyPrompt: '依頼文をコピー', copyDone: 'コピーしました', rootUnavailable: 'このアプリと docs/agent-guide.md があるフォルダで開いてください。開発サーバーを起動すると、ここでフルパスをコピーできます。',
+ newIllustration: '新しいイラストから作る',
+ agentNewPrompt: '私が渡すイラストから、docs/agent-guide.md の Step 1〜5 に沿って PROJECT にプロジェクトを作ってください。最初にモデルの確認と座標読取りの校正を行い、レイヤーを作成して、ポーズを目視で確認してください。画像と生成物はすべてこのリポジトリ内に保存してください。作成先がすでに存在する場合は、未使用の名前を選んでください。アプリのプロジェクト一覧から開ける状態になったら知らせてください。',
+ agentVariantPrompt: 'PROJECT の TARGETの描き分け画像を、docs/agent-guide.md の Step 6 に沿って作ってください。ローカルでマスクと依頼文を準備し、外部の画像生成サービスへ送る前には私に確認してください。戻ってきた画像を build-sprites で取り込み、Step 5 のポーズ確認を行ってください。ファイルはすべてこのリポジトリ内に保存してください。',
+ agentCopySample: '最初に読み取り専用の SOURCE を PROJECT にコピーしてください。作成先が存在する場合は未使用の名前を選び、元のプロジェクトは変更しないでください。',
+ agentUnknownPath: 'ブラウザで選んだフォルダの実際の場所は取得できません。正しい場所を含む依頼文を作るには、開発サーバーのプロジェクト一覧から開いてください。',
+ agentEyes: '目', agentMouth: '口', agentBoth: '目と口',
+ rebuild: '保存してレイヤーを作り直す', rebuilding: '保存してレイヤーを作り直しています…', staleLocal: '輪郭が変更されました。レイヤーを作り直すとプレビューに反映されます。',
+ rebuildUnavailable: '自動で作り直すには、ローカル一覧から編集できるプロジェクトを開いてください。ブラウザで選んだフォルダはサーバー上の場所を取得できません。設定を保存し、リポジトリのルートで build-layers を実行してから開き直してください。',
+ jobFailed: '作業を完了できませんでした。以前のレイヤーは残しています。', dependenciesHelp: 'uv と Python 3.10 以降を用意してから、もう一度お試しください。Homebrew の場合: brew install uv python。準備手順: docs/agent-guide.md。',
+ unsafeFilesHelp: 'プロジェクトに別の場所へ転送するファイルがあります。このプロジェクト内の通常のファイルにしてからお試しください。', showLog: 'ログを表示', operationBusy: 'この端末で処理中…', reloadFailed: 'プロジェクトを読み込み直せませんでした。現在の画面は残っています。',
+ variantsTitle: '描き分け画像', variantMissing: 'なし', variantsHelp: '目を閉じたときや話すときの絵を描き分けると、動きが自然になります。',
+ eyesClosed: '閉じた目', eyesHalf: '半分閉じた目', eyesSmile: '笑った目', mouthA: '口「あ」', mouthAHalf: '半分開いた「あ」', mouthI: '口「い」', mouthO: '口「お」',
+ targetEyes: '目', targetMouth: '口',
+ createRequests: 'マスクと依頼文を書き出す', requestsReady: 'マスクと依頼文をこの端末に作りました。', requestFiles: 'マスクと依頼文', copyVariantPrompt: '画像の依頼文をコピー', copyFilePaths: 'ファイルの場所をコピー',
+ manualVariants: '自分で画像を用意する場合', manualHelp: '元画像 source.png・マスク・依頼文を画像編集ツールに渡してください。同じ大きさの PNG を下記の名前で保存し、この欄にドロップします。マスクの透明部分だけ変更でき、不透明部分は元画像と同じにします。',
+ variantOutput: '戻ってきた画像の保存先', dropVariants: '戻ってきた PNG 画像をここにドロップ', chooseVariants: '戻ってきた画像を選ぶ', variantFiles: '戻ってきた描き分け画像',
+ variantFilenameHelp: 'eyes_closed.png・eyes_half.png・eyes_smile.png・mouth_a.png・mouth_a_half.png・mouth_i.png・mouth_o.png の名前を使います。画像はこの端末内に保存します。',
+ importAccepted: '合格しました。プレビューに反映しました。', importRejected: '不合格でした。以前の画像とレイヤーは残しています。', importOutside: 'マスクの外側の画素が変更されています。', importSize: '元画像 source.png と同じ大きさの PNG にしてください。', importMask: 'この描き分けに使うマスクの形が正しくありません。',
+ importInvalid: '指定された PNG 名を使ってください(同じ名前は1枚まで)。1枚24 MB・合計26 MBまで取り込めます。', metricsTitle: 'マスクの外側の比較結果', outsideMax: '差の最大値', outsideMean: '差の平均値', outsidePixels: '許容差を超えた画素数',
+ variantsUnavailable: 'ここで依頼ファイルを作ったり画像を取り込んだりするには、開発サーバーの一覧から編集できるプロジェクトを開いてください。ほかのフォルダは、エージェントに docs/agent-guide.md の Step 6 を依頼できます。',
+ variantsNeedRebuild: '先に輪郭の変更を反映して、マスクを現在のレイヤーに合わせてください。',
+};
diff --git a/src/engine/createMeshAvatar.js b/src/engine/createMeshAvatar.js
index 2b67dee..9b0d0a4 100644
--- a/src/engine/createMeshAvatar.js
+++ b/src/engine/createMeshAvatar.js
@@ -143,6 +143,11 @@ export async function createMeshAvatarImpl(canvas, options) {
const tmp = [0, 0];
function tick(dt) {
const P = { ...motion.update(dt), ...parameters };
+ // Speech owns the mouth while active; explicit pose sliders still own all other parameters.
+ if (motion.lipOpen !== null) {
+ P.mouthOpen = motion.P.mouthOpen;
+ P.mouthForm = motion.P.mouthForm;
+ }
const phys = physics.step(P, dt);
const bp = baseMesh.pos, br = baseMesh.rest;
@@ -215,7 +220,10 @@ export async function createMeshAvatarImpl(canvas, options) {
/** Play a motion or idle motion by id (see `motions`). */
play(id) { if (motion.hasMotion(id)) motion.playMotion(id); },
/** Move the mouth through the vowels of kana text (no audio; for previews). */
- speakKana(text) { motion.speakKana(String(text ?? '')); },
+ speakKana(text, options) { motion.speakKana(String(text ?? ''), options); },
+ holdMouth(vowel) { motion.holdMouth(vowel); },
+ stopLipSync() { motion.stopLipSync(); },
+ getLipSyncState() { return motion.getLipSyncState(); },
setAutoIdle(on) { motion.autoIdle = !!on; },
setAutoMotion(on) { motion.autoMotion = !!on; },
/** Hair / tassel sway multiplier (1 = default). */
diff --git a/src/engine/index.ts b/src/engine/index.ts
index 918ef41..21c31bf 100644
--- a/src/engine/index.ts
+++ b/src/engine/index.ts
@@ -18,7 +18,10 @@ export interface MeshAvatar {
setEmotion(tag: string | null, options?: { playMotion?: boolean }): void;
setTalkGain(gain: number): void;
play(id: string): void;
- speakKana(text: string): void;
+ speakKana(text: string, options?: { speed?: number; loop?: boolean }): void;
+ holdMouth(vowel: 'a' | 'i' | 'u' | 'e' | 'o' | 'n'): void;
+ stopLipSync(): void;
+ getLipSyncState(): { active: boolean; open: number; form: number };
setAutoIdle(on: boolean): void;
setAutoMotion(on: boolean): void;
setSwayGain(gain: number): void;
diff --git a/src/engine/kana.js b/src/engine/kana.js
index aefa452..24b11ce 100644
--- a/src/engine/kana.js
+++ b/src/engine/kana.js
@@ -27,6 +27,11 @@ const PAUSE = new Set([...'、。,.,.!?!?  …']);
const toHiragana = s => s.replace(/[ァ-ヶ]/g, c => String.fromCharCode(c.charCodeAt(0) - 0x60));
+/** Unsupported characters, for explaining omissions in the editor. */
+export function skippedKanaCharacters(text) {
+ return [...new Set([...toHiragana(text)].filter(c => !VOWEL_OF[c] && !SMALL_Y[c] && c !== 'ー' && c !== 'っ' && c !== 'ん' && !PAUSE.has(c)))];
+}
+
/** -> [{ vowel, dur, onset }] where onset is 'lips' | 'consonant' | null */
export function kanaToMoras(text, mora = 0.14) {
const out = [];
diff --git a/src/engine/motion.js b/src/engine/motion.js
index c80cd15..76e0e03 100644
--- a/src/engine/motion.js
+++ b/src/engine/motion.js
@@ -85,6 +85,7 @@ export class Motion {
this.voicePeak = 0;
this.voiceTrough = 0;
this.kana = null;
+ this.lipHold = null;
this.emotionUntil = 0; // when the emotion face goes back to normal
this.talkGain = 1; // how much the head moves along with the voice
this.play = null; // { id, t } of the motion being played
@@ -122,8 +123,24 @@ export class Motion {
}
/** Mouth shapes from kana text, without audio (preview / tuning). */
- speakKana(text) {
- this.kana = { moras: kanaToMoras(text), t: 0 };
+ /** @param {string} text @param {{ speed?: number, loop?: boolean }} [options] */
+ speakKana(text, { speed, loop = false } = {}) {
+ this.lipHold = null;
+ const moras = kanaToMoras(text, speed === undefined ? 0.14 : 1 / clamp(Number(speed) || 7, 4, 12));
+ this.kana = moras.length ? { moras, t: 0, loop, duration: moras.reduce((sum, m) => sum + m.dur, 0) } : null;
+ }
+
+ holdMouth(vowel) {
+ this.kana = null;
+ this.lipHold = Object.hasOwn(VOWELS, vowel) ? vowel : null;
+ }
+
+ stopLipSync() {
+ this.kana = null; this.lipHold = null; this.lipOpen = null; this.mouth = 0;
+ }
+
+ getLipSyncState() {
+ return { active: this.kana !== null || this.lipHold !== null, open: this.P.mouthOpen, form: this.P.mouthForm };
}
// Mouth from the TTS loudness. A volume signal has no vowel information, so each syllable
@@ -133,9 +150,14 @@ export class Motion {
// is scaled by where the voice sits between the recent trough and peak, so it closes in
// the dip before each mora even when a loud voice never gets quiet.
speechMouth(dt) {
- if (this.kana) {
- this.kana.t += dt;
- const m = sampleMoras(this.kana.moras, this.kana.t);
+ if (this.kana || this.lipHold) {
+ let m;
+ if (this.lipHold) m = VOWELS[this.lipHold];
+ else {
+ this.kana.t += dt;
+ if (this.kana.loop) this.kana.t %= this.kana.duration;
+ m = sampleMoras(this.kana.moras, this.kana.t);
+ }
if (!m) { this.kana = null; return this.mouth > 0.01 ? [this.mouth *= 0.6, this.vowelForm] : null; }
const k = m.open > this.mouth ? 1 - Math.exp(-dt * 30) : 1 - Math.exp(-dt * 22);
this.mouth += (m.open - this.mouth) * k;
diff --git a/src/server/local-projects.ts b/src/server/local-projects.ts
new file mode 100644
index 0000000..2b2dcc4
--- /dev/null
+++ b/src/server/local-projects.ts
@@ -0,0 +1,176 @@
+import { execFile } from 'node:child_process';
+import { randomUUID } from 'node:crypto';
+import { readFile, readdir, realpath, stat, writeFile, rename, unlink } from 'node:fs/promises';
+import { resolve, relative, extname, sep } from 'node:path';
+import type { IncomingMessage, ServerResponse } from 'node:http';
+import type { Plugin } from 'vite';
+import { validateRig } from '../rig/validate';
+import { decodeVariants, JobError, projectJob, runTool, variantState, VARIANTS, type Runner } from './project-jobs';
+
+const SAMPLE = 'sample-miko-qipao';
+const safeName = /^[A-Za-z0-9._-]+$/;
+class HttpError extends Error { constructor(public status: number, message: string) { super(message); } }
+const inside = (base: string, path: string) => { const rel = relative(base, path); return rel === '' || (!rel.startsWith(`..${sep}`) && rel !== '..' && !rel.startsWith(sep)); };
+async function exists(path: string) { try { await stat(path); return true; } catch (error) { if ((error as NodeJS.ErrnoException).code === 'ENOENT') return false; throw error; } }
+async function checked(base: string, path: string) {
+ if (!inside(base, resolve(path)) || !inside(base, await realpath(path))) throw new HttpError(400, 'Path must stay inside the project.');
+ return path;
+}
+export function revealFolder(path: string): Promise<void> {
+ const command = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'explorer' : 'xdg-open';
+ return new Promise((done, reject) => execFile(command, [path], error => error ? reject(error) : done()));
+}
+export function localProjectMiddleware(root: string, reveal = revealFolder, runner: Runner = runTool) {
+ root = resolve(root);
+ const base = resolve(root, 'projects'), sample = resolve(root, 'samples/miko-qipao');
+ const saving = new Set<string>();
+ async function readBody(req: IncomingMessage, limit = 1024 * 1024) {
+ if (req.headers['content-type']?.split(';')[0] !== 'application/json') throw new HttpError(400, 'Send application/json.');
+ const chunks: Buffer[] = []; let size = 0;
+ for await (const chunk of req) { size += chunk.length; if (size > limit) throw new HttpError(413, 'Request is too large.'); chunks.push(chunk); }
+ try { return JSON.parse(Buffer.concat(chunks).toString('utf8')); } catch { throw new HttpError(400, 'Invalid JSON.'); }
+ }
+ async function saveRig(path: string, rig: unknown) {
+ const errors = validateRig(rig);
+ if (errors.length) throw new HttpError(400, `Invalid rig: ${errors.join(', ')}`);
+ const target = resolve(path, 'rig.json'), temporary = resolve(path, `.rig-${randomUUID()}.tmp`), backup = `${temporary}.bak`;
+ try {
+ if (await exists(target)) {
+ const previous = await readFile(await checked(path, target));
+ await writeFile(backup, previous, { flag: 'wx' }); await rename(backup, `${target}.bak`);
+ }
+ await writeFile(temporary, JSON.stringify(rig, null, 2) + '\n', { flag: 'wx' }); await rename(temporary, target);
+ } finally { await Promise.all([temporary, backup].map(file => unlink(file).catch(() => undefined))); }
+ }
+ async function requests(path: string, name: string) {
+ const items = [];
+ for (const variant of VARIANTS) {
+ const prompt = resolve(path, 'variant-requests', variant, 'prompt.md');
+ if (await exists(prompt)) {
+ await checked(path, resolve(path, 'variant-requests', variant, 'mask.png'));
+ items.push({ name: variant, prompt: await readFile(await checked(path, prompt), 'utf8'),
+ maskUrl: `/__studio/projects/${encodeURIComponent(name)}/variant-requests/${variant}/mask.png?v=${Date.now()}` });
+ }
+ }
+ return items;
+ }
+ async function folder(name: string) {
+ if (!safeName.test(name) || name === '.' || name === '..' || name.startsWith('.studio-job-')) throw new HttpError(400, 'Invalid project name.');
+ const parent = name === SAMPLE ? sample : base;
+ const path = name === SAMPLE ? sample : resolve(base, name);
+ // The root itself and every nested symlink must stay under the intended physical folder.
+ if (await realpath(parent) !== parent) throw new HttpError(400, 'Project root cannot redirect elsewhere.');
+ await checked(parent, path);
+ if (!(await stat(path)).isDirectory()) throw new HttpError(404, 'Project not found.');
+ return path;
+ }
+ async function info(name: string) {
+ const path = await folder(name), rigPath = resolve(path, 'rig.json');
+ const draft = resolve(path, 'rig.draft.json');
+ if (!await exists(rigPath) && !(await exists(draft) && await exists(resolve(path, 'built')))) return null;
+ const file = await checked(path, await exists(rigPath) ? rigPath : draft);
+ return { name, relativePath: relative(root, path).split(sep).join('/'), absolutePath: path,
+ rigFile: await exists(rigPath) ? 'rig.json' : 'rig.draft.json',
+ updatedAt: (await stat(file)).mtime.toISOString(), hasSprites: await exists(resolve(path, 'built/sprites/sprites.json')),
+ hasVariants: await exists(resolve(path, 'variants')), readOnly: name === SAMPLE };
+ }
+ return async (req: IncomingMessage, res: ServerResponse, next: () => void) => {
+ if (!req.url?.startsWith('/__studio/')) { next(); return; }
+ const json = (status: number, value: unknown) => { res.writeHead(status, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store', 'X-Content-Type-Options': 'nosniff' }); res.end(JSON.stringify(value)); };
+ try {
+ // Reject cross-origin browser requests, including writes triggered by another website.
+ if (req.headers.origin && req.headers.origin !== `http://${req.headers.host}`) throw new HttpError(403, 'Use the local studio origin.');
+ const host = req.headers.host?.split(':')[0];
+ if (host !== '127.0.0.1' && host !== 'localhost') throw new HttpError(403, 'Local requests only.');
+ const raw = req.url.split('?')[0].slice('/__studio/'.length).split('/');
+ const parts = raw.map(part => {
+ let value; try { value = decodeURIComponent(part); } catch { throw new HttpError(400, 'Invalid path encoding.'); }
+ if (!safeName.test(value) || value === '.' || value === '..') throw new HttpError(400, 'Invalid path segment.');
+ return value;
+ });
+ if (parts.length === 1 && parts[0] === 'context' && req.method === 'GET') { json(200, { rootPath: root }); return; }
+ if (parts.length === 1 && parts[0] === 'reveal' && req.method === 'POST') { await reveal(root); json(200, { path: root }); return; }
+ if (parts[0] !== 'projects') throw new HttpError(404, 'Not found.');
+ if (parts.length === 1 && req.method === 'GET') {
+ if (await exists(base) && await realpath(base) !== base) throw new HttpError(400, 'Project root cannot redirect elsewhere.');
+ const entries = await exists(base) ? await readdir(base, { withFileTypes: true }) : [];
+ const list = [];
+ for (const entry of entries) if (entry.isDirectory() && !entry.name.startsWith('.') && safeName.test(entry.name) && entry.name !== SAMPLE) {
+ try { const value = await info(entry.name); if (value) list.push(value); } catch (error) { if (!(error instanceof HttpError)) throw error; }
+ }
+ if (await exists(resolve(sample, 'source.png')) && await exists(resolve(sample, 'built/base.png'))) {
+ const value = await info(SAMPLE); if (value) list.push(value);
+ }
+ list.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
+ json(200, list); return;
+ }
+ if (parts.length < 3) throw new HttpError(404, 'Not found.');
+ const name = parts[1], path = await folder(name), tail = parts.slice(2);
+ if (req.method === 'POST' && tail.length === 1 && tail[0] === 'reveal') {
+ await reveal(path); json(200, { path }); return;
+ }
+ if (req.method === 'GET' && tail.length === 1 && tail[0] === 'variants') {
+ const manifest = resolve(path, 'built/sprites/sprites.json'); if (await exists(manifest)) await checked(path, manifest);
+ json(200, { variants: await variantState(path), requests: await requests(path, name) }); return;
+ }
+ if (req.method === 'POST' && tail.length === 1 && ['rig', 'rebuild', 'variant-requests', 'import-variants'].includes(tail[0])) {
+ if (name === SAMPLE) throw new HttpError(403, 'The sample is read-only.');
+ const action = tail[0], body = await readBody(req, action === 'import-variants' ? 36 * 1024 * 1024 : 1024 * 1024);
+ let files: ReturnType<typeof decodeVariants> = [];
+ if (action === 'import-variants') { try { files = decodeVariants(body); } catch (error) { throw new HttpError(400, (error as Error).message); } }
+ if (saving.has(path)) throw new HttpError(409, 'A project operation is already in progress.');
+ saving.add(path);
+ try {
+ if (action === 'rig') { await saveRig(path, body); json(200, { path: relative(root, resolve(path, 'rig.json')).split(sep).join('/') }); }
+ else {
+ if (action === 'rebuild' || action === 'variant-requests') await saveRig(path, body?.rig);
+ const result = await projectJob(root, path, action === 'rebuild' ? 'build-layers' : action === 'variant-requests' ? 'variant-requests' : 'build-sprites', files, runner);
+ json(200, { ...result, ...(action === 'variant-requests' ? { requests: await requests(path, name) } : {}) });
+ }
+ } finally { saving.delete(path); }
+ return;
+ }
+ if (req.method !== 'GET') throw new HttpError(405, 'Method not allowed.');
+ const fileName = tail.join('/');
+ if (!(fileName === 'rig.json' || fileName === 'rig.draft.json' || fileName === 'source.png' || ['built', 'variants'].includes(tail[0]) || (tail.length === 3 && tail[0] === 'variant-requests' && VARIANTS.includes(tail[1] as typeof VARIANTS[number]) && tail[2] === 'mask.png'))) throw new HttpError(404, 'File not available.');
+ const mime: Record<string, string> = { '.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.webp': 'image/webp' };
+ const contentType = mime[extname(fileName)];
+ if (!contentType) throw new HttpError(404, 'File not available.');
+ const file = await checked(path, resolve(path, ...tail));
+ const bytes = await readFile(file);
+ res.writeHead(200, { 'Content-Type': contentType, 'Cache-Control': 'no-store', 'X-Content-Type-Options': 'nosniff' });
+ res.end(bytes);
+ } catch (error) {
+ if (error instanceof JobError) { json(422, { error: error.code, code: error.code, log: error.log }); return; }
+ const status = error instanceof HttpError ? error.status : (error as NodeJS.ErrnoException).code === 'ENOENT' ? 404 : 500;
+ json(status, { error: error instanceof HttpError ? error.message : status === 404 ? 'Project or file not found.' : 'Local project operation failed.' });
+ }
+ };
+}
+export function localProjectsPlugin(root: string): Plugin {
+ const base = resolve(root, 'projects');
+ const variantProject = (file: string) => {
+ const parts = relative(base, file).split(sep);
+ if (!safeName.test(parts[0]) || parts[0].startsWith('.') || parts[0] === SAMPLE) return;
+ if (parts[1] === 'variants' || (parts[1] === 'built' && parts[2] === 'sprites')) return parts[0];
+ };
+ return { name: 'local-studio-projects', apply: 'serve', configureServer(server) {
+ server.middlewares.use(localProjectMiddleware(root));
+ const timers = new Map<string, ReturnType<typeof setTimeout>>();
+ const changed = (file: string) => {
+ const name = variantProject(file);
+ if (!name) return;
+ clearTimeout(timers.get(name));
+ timers.set(name, setTimeout(() => {
+ timers.delete(name);
+ server.ws.send({ type: 'custom', event: 'studio:variants-changed', data: { name } });
+ }, 800));
+ };
+ server.watcher.add(base);
+ server.watcher.on('all', (_event, file) => changed(file));
+ server.httpServer?.once('close', () => { for (const timer of timers.values()) clearTimeout(timer); });
+ }, handleHotUpdate(context) {
+ // Only the current project's assets change; keep the editor and its history mounted.
+ if (variantProject(context.file)) return [];
+ } };
+}
diff --git a/src/server/project-jobs.ts b/src/server/project-jobs.ts
new file mode 100644
index 0000000..27ed593
--- /dev/null
+++ b/src/server/project-jobs.ts
@@ -0,0 +1,100 @@
+import { execFile } from 'node:child_process';
+import { copyFile, lstat, mkdir, mkdtemp, readdir, readFile, realpath, rename, rm, writeFile } from 'node:fs/promises';
+import { join, resolve, relative, dirname } from 'node:path';
+
+export const VARIANTS = ['eyes_closed', 'eyes_half', 'eyes_smile', 'mouth_a', 'mouth_a_half', 'mouth_i', 'mouth_o'] as const;
+export type Tool = 'build-layers' | 'variant-requests' | 'build-sprites';
+export class JobError extends Error {
+ constructor(public code: 'dependencies' | 'toolFailed' | 'unsafeFiles', public log: string) { super(code); }
+}
+export type Runner = (root: string, project: string, tool: Tool) => Promise<string>;
+export const runTool: Runner = (root, project, tool) => new Promise((done, reject) => {
+ const args = ['run', '--no-project', '--no-python-downloads', '--python', '>=3.10', '--with', 'numpy', '--with', 'pillow', '--with', 'opencv-python-headless', resolve(root, `tools/${tool}.py`), project];
+ execFile('uv', args, { cwd: root, timeout: 120_000, maxBuffer: 2 * 1024 * 1024 }, (error, stdout, stderr) => {
+ const log = `${stdout}\n${stderr}`.replaceAll(root, '<repo>').trim().slice(-12000);
+ if (!error) { done(log); return; }
+ const missing = (error as NodeJS.ErrnoException).code === 'ENOENT' || /No interpreter found|No Python installation|Python interpreter not found/i.test(log);
+ reject(new JobError(missing ? 'dependencies' : 'toolFailed', log || (missing ? 'uv is not installed.' : 'The local tool did not finish.')));
+ });
+});
+
+// Python tools read and write several nested paths. Reject redirected entries before
+// copying their inputs, then run in an isolated directory under projects/.
+async function copyTree(source: string, target: string) {
+ const info = await lstat(source);
+ if (info.isSymbolicLink()) throw new JobError('unsafeFiles', 'Project files must not be symbolic links.');
+ if (info.isDirectory()) {
+ await mkdir(target, { recursive: true });
+ for (const entry of await readdir(source)) await copyTree(join(source, entry), join(target, entry));
+ } else if (info.isFile()) await copyFile(source, target);
+ else throw new JobError('unsafeFiles', 'Unsupported project file.');
+}
+async function exists(path: string) { try { await lstat(path); return true; } catch (error) { if ((error as NodeJS.ErrnoException).code === 'ENOENT') return false; throw error; } }
+async function safeTree(path: string) {
+ const info = await lstat(path);
+ if (info.isSymbolicLink()) throw new JobError('unsafeFiles', 'Project files must not be symbolic links.');
+ if (info.isDirectory()) for (const entry of await readdir(path)) await safeTree(join(path, entry));
+}
+async function publish(project: string, stage: string, names: string[]) {
+ // Validate all targets before moving anything. Keep old directories until all
+ // replacements succeed, and roll back on an I/O error.
+ for (const name of names) if (await exists(join(project, name))) await safeTree(join(project, name));
+ const moved: { name: string; hadPrevious: boolean; installed: boolean }[] = [];
+ try {
+ for (const name of names) {
+ const target = join(project, name), previous = join(stage, `previous-${name}`);
+ const item = { name, hadPrevious: await exists(target), installed: false }; moved.push(item);
+ if (item.hadPrevious) await rename(target, previous);
+ await rename(join(stage, name), target); item.installed = true;
+ }
+ } catch (error) {
+ for (const item of moved.reverse()) {
+ if (item.installed) await rm(join(project, item.name), { recursive: true, force: true });
+ if (item.hadPrevious && await exists(join(stage, `previous-${item.name}`))) await rename(join(stage, `previous-${item.name}`), join(project, item.name));
+ }
+ throw error;
+ }
+}
+export interface VariantInput { name: string; data: string }
+export function decodeVariants(value: unknown): { name: string; bytes: Buffer }[] {
+ const files = (value as { files?: VariantInput[] })?.files;
+ if (!Array.isArray(files) || !files.length || files.length > VARIANTS.length) throw new Error('Choose one to seven named PNG files.');
+ const names = new Set<string>();
+ return files.map(file => {
+ if (!file || !VARIANTS.some(name => file.name === `${name}.png`) || names.has(file.name) || typeof file.data !== 'string' || !/^[A-Za-z0-9+/]+={0,2}$/.test(file.data)) throw new Error('Use the seven variant filenames and PNG images.');
+ names.add(file.name);
+ const bytes = Buffer.from(file.data, 'base64');
+ if (bytes.length > 24 * 1024 * 1024 || !bytes.subarray(0, 8).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]))) throw new Error('Use PNG images smaller than 24 MB each.');
+ return { name: file.name, bytes };
+ });
+}
+export async function variantState(project: string) {
+ let layers: Record<string, unknown> = {};
+ try { layers = JSON.parse(await readFile(join(project, 'built/sprites/sprites.json'), 'utf8')).layers ?? {}; }
+ catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; }
+ return VARIANTS.map(name => ({ name, present: name.startsWith('eyes_') ? `${name}_0` in layers && `${name}_1` in layers : name in layers }));
+}
+export async function projectJob(root: string, project: string, tool: Tool, files: ReturnType<typeof decodeVariants> = [], runner = runTool) {
+ const checkFolder = async () => {
+ const base = resolve(root, 'projects');
+ if (dirname(project) !== base || await realpath(base) !== base || await realpath(project) !== project) throw new JobError('unsafeFiles', 'The project must be a regular folder inside projects/.');
+ };
+ await checkFolder();
+ const stage = await mkdtemp(join(resolve(root, 'projects'), '.studio-job-'));
+ try {
+ for (const name of ['source.png', 'rig.json', 'built', 'variants', 'variant-requests', 'variant-masks']) {
+ const source = join(project, name);
+ if (await exists(source)) await copyTree(source, join(stage, name));
+ }
+ if (files.length) {
+ await mkdir(join(stage, 'variants'), { recursive: true });
+ for (const file of files) await writeFile(join(stage, 'variants', file.name), file.bytes);
+ }
+ const log = await runner(root, stage, tool);
+ await checkFolder();
+ if (tool === 'build-layers') await publish(project, stage, ['built', 'rig.json']);
+ if (tool === 'variant-requests') await publish(project, stage, ['variant-requests']);
+ if (tool === 'build-sprites') await publish(project, stage, ['variants', 'built']);
+ return { log, path: relative(root, project).split('\\').join('/') };
+ } finally { await rm(stage, { recursive: true, force: true }); }
+}
diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts
new file mode 100644
index 0000000..11f02fe
--- /dev/null
+++ b/src/vite-env.d.ts
@@ -0,0 +1 @@
+/// <reference types="vite/client" />
diff --git a/tests/history.test.ts b/tests/history.test.ts
index 89eed8c..2f593b0 100644
--- a/tests/history.test.ts
+++ b/tests/history.test.ts
@@ -26,3 +26,12 @@ test('a click with no change adds no history entry', () => {
history.end();
expect(history.canUndo).toBe(false);
});
+test('opening another project drops edits and a pending gesture from the preceding project', () => {
+ const first = parseRig(fixture), second = setAt(first, 'head.cx', 999);
+ const history = new RigHistory(first);
+ history.change(setAt(first, 'head.cx', 630)); history.begin();
+ history.change(setAt(first, 'head.cx', 650)); history.load(second);
+ expect(history.canUndo).toBe(false); expect(history.canRedo).toBe(false);
+ expect(history.undo()).toEqual(second);
+ history.change(setAt(second, 'head.cx', 1000)); expect(history.undo()).toEqual(second);
+});
diff --git a/tests/lip-sync.test.ts b/tests/lip-sync.test.ts
new file mode 100644
index 0000000..6891971
--- /dev/null
+++ b/tests/lip-sync.test.ts
@@ -0,0 +1,33 @@
+import { expect, test } from 'vitest';
+import { Motion } from '../src/engine/motion.js';
+import { kanaToMoras, sampleMoras, skippedKanaCharacters } from '../src/engine/kana.js';
+
+test('held vowels share speech smoothing, preserve the preceding form for ん, and release', () => {
+ const motion = new Motion([0, 0]); motion.mode = 'manual';
+ const shapes = [];
+ for (const vowel of ['a', 'i', 'o']) {
+ motion.holdMouth(vowel);
+ for (let i = 0; i < 30; i++) motion.update(1 / 60);
+ shapes.push(motion.getLipSyncState());
+ }
+ expect(new Set(shapes.map(shape => `${shape.open.toFixed(2)},${shape.form}`)).size).toBe(3);
+ motion.holdMouth('n'); for (let i = 0; i < 30; i++) motion.update(1 / 60);
+ expect(motion.P.mouthOpen).toBeLessThan(0.001); expect(motion.P.mouthForm).toBe(0.6);
+ motion.stopLipSync(); motion.update(1 / 60); expect(motion.getLipSyncState().active).toBe(false);
+});
+
+test('kana playback uses requested speed, loops, stops, and ignores unsupported text', () => {
+ expect(kanaToMoras('アイ ウエオ').map(m => m.vowel)).toEqual(['a', 'i', 'n', 'u', 'e', 'o']);
+ expect(skippedKanaCharacters('あA漢字いA')).toEqual(['A', '漢', '字']);
+ const timeline = kanaToMoras('あいお', 1 / 4);
+ expect(sampleMoras(timeline, 0.3)?.vowel).toBe('i');
+ const motion = new Motion([0, 0]); motion.mode = 'manual';
+ motion.speakKana('あいお', { speed: 4, loop: true });
+ for (let i = 0; i < 120; i++) motion.update(1 / 60);
+ expect(motion.getLipSyncState().active).toBe(true);
+ motion.stopLipSync(); expect(motion.getLipSyncState().active).toBe(false);
+ motion.speakKana('あ', { speed: 12 });
+ for (let i = 0; i < 10; i++) motion.update(1 / 60);
+ expect(motion.getLipSyncState().active).toBe(false);
+ motion.speakKana('漢字', { loop: true }); expect(motion.getLipSyncState().active).toBe(false);
+});
diff --git a/tests/local-projects.test.ts b/tests/local-projects.test.ts
new file mode 100644
index 0000000..9169348
--- /dev/null
+++ b/tests/local-projects.test.ts
@@ -0,0 +1,94 @@
+import { beforeEach, afterEach, expect, test, vi } from 'vitest';
+import { mkdtemp, mkdir, writeFile, readFile, rm, symlink } from 'node:fs/promises';
+import { resolve, join } from 'node:path';
+import { Readable } from 'node:stream';
+import type { IncomingMessage, ServerResponse } from 'node:http';
+import fixture from '../samples/miko-qipao/rig.json';
+import { localProjectMiddleware, localProjectsPlugin } from '../src/server/local-projects';
+
+let root: string;
+beforeEach(async () => {
+ const scratch = resolve('projects'); await mkdir(scratch, { recursive: true });
+ root = await mkdtemp(join(scratch, '.local-api-test-'));
+ for (const path of ['projects/nova/built/sprites', 'projects/nova/variants', 'samples/miko-qipao/built']) await mkdir(resolve(root, path), { recursive: true });
+ for (const path of ['projects/nova', 'samples/miko-qipao']) {
+ await writeFile(resolve(root, path, 'rig.json'), JSON.stringify(fixture));
+ await writeFile(resolve(root, path, 'source.png'), 'synthetic image bytes');
+ await writeFile(resolve(root, path, 'built/base.png'), 'synthetic base bytes');
+ }
+ await writeFile(resolve(root, 'projects/nova/built/sprites/sprites.json'), '{}');
+});
+afterEach(async () => { await rm(root, { recursive: true, force: true }); });
+
+async function call(path: string, method = 'GET', body?: unknown, headers: Record<string, string> = {}, reveal = vi.fn(async (_path: string) => { void _path; })) {
+ const request = Readable.from(body === undefined ? [] : [Buffer.from(JSON.stringify(body))]) as IncomingMessage;
+ request.url = path; request.method = method;
+ request.headers = { host: '127.0.0.1:5173', 'content-type': 'application/json', ...headers };
+ const result = { status: 0, body: '', headers: {} as Record<string, string> };
+ const response = {
+ writeHead(status: number, values: Record<string, string>) { result.status = status; result.headers = values; },
+ end(value: string | Buffer) { result.body = String(value); },
+ } as unknown as ServerResponse;
+ await localProjectMiddleware(root, reveal)(request, response, () => { result.status = 404; });
+ return result;
+}
+
+test('lists eligible projects and the read-only installed sample, and serves project images', async () => {
+ await mkdir(resolve(root, 'projects/unbuilt'), { recursive: true });
+ await writeFile(resolve(root, 'projects/unbuilt/rig.draft.json'), '{}');
+ await mkdir(resolve(root, 'projects/draft/built'), { recursive: true });
+ await writeFile(resolve(root, 'projects/draft/rig.draft.json'), '{}');
+ const response = await call('/__studio/projects'); expect(response.status).toBe(200);
+ const list = JSON.parse(response.body);
+ expect(list.map((item: { name: string }) => item.name).sort()).toEqual(['draft', 'nova', 'sample-miko-qipao']);
+ expect(list.find((item: { name: string }) => item.name === 'nova')).toMatchObject({ relativePath: 'projects/nova', absolutePath: resolve(root, 'projects/nova'), hasSprites: true, hasVariants: true, readOnly: false });
+ expect(list.find((item: { name: string }) => item.name === 'sample-miko-qipao').readOnly).toBe(true);
+ const image = await call('/__studio/projects/nova/built/base.png');
+ expect(image.status).toBe(200); expect(image.body).toBe('synthetic base bytes'); expect(image.headers['Content-Type']).toBe('image/png');
+ expect((await call('/__studio/projects/nova/rig.json.bak')).status).toBe(404);
+});
+
+test('rejects raw and encoded traversal, absolute paths, malformed encoding and symlink escapes', async () => {
+ for (const tail of ['../rig.json', './rig.json', '%2e%2e/rig.json', '%2Fprivate/rig.json', 'nova%2F..%2Fother/rig.json', 'nova%5Cother/rig.json', '%252e%252e/rig.json', 'C%3A/rig.json', '%/rig.json', 'nova/built/../../rig.json']) {
+ expect((await call(`/__studio/projects/${tail}`)).status, tail).toBe(400);
+ expect((await call(`/__studio/projects/${tail}`, 'POST', fixture)).status, tail).toBe(400);
+ }
+ await mkdir(resolve(root, 'outside'), { recursive: true });
+ await writeFile(resolve(root, 'outside/secret.png'), 'private');
+ await symlink(resolve(root, 'outside'), resolve(root, 'projects/redirect'));
+ await symlink(resolve(root, 'outside/secret.png'), resolve(root, 'projects/nova/built/escape.png'));
+ expect((await call('/__studio/projects/redirect/source.png')).status).toBe(400);
+ expect((await call('/__studio/projects/nova/built/escape.png')).status).toBe(400);
+ expect((await call('/__studio/projects/redirect/rig', 'POST', fixture)).status).toBe(400);
+});
+
+test('validates before writing and keeps exactly the preceding rig in a one-generation backup', async () => {
+ const original = await readFile(resolve(root, 'projects/nova/rig.json'), 'utf8');
+ const first = structuredClone(fixture); first.head.cx += 10;
+ expect((await call('/__studio/projects/nova/rig', 'POST', first)).status).toBe(200);
+ expect(await readFile(resolve(root, 'projects/nova/rig.json.bak'), 'utf8')).toBe(original);
+ const second = structuredClone(first); second.head.cx += 10;
+ const saved = await call('/__studio/projects/nova/rig', 'POST', second);
+ expect(JSON.parse(saved.body)).toEqual({ path: 'projects/nova/rig.json' });
+ expect(JSON.parse(await readFile(resolve(root, 'projects/nova/rig.json.bak'), 'utf8'))).toEqual(first);
+ expect((await call('/__studio/projects/nova/rig', 'POST', { version: 1 })).status).toBe(400);
+ expect(JSON.parse(await readFile(resolve(root, 'projects/nova/rig.json'), 'utf8'))).toEqual(second);
+ expect((await call('/__studio/projects/sample-miko-qipao/rig', 'POST', first)).status).toBe(403);
+});
+
+test('refuses redirected rig writes and cross-origin operations, and reveals the validated folder', async () => {
+ const reveal = vi.fn(async (_path: string) => { void _path; });
+ expect((await call('/__studio/projects/nova/reveal', 'POST', {}, {}, reveal)).status).toBe(200);
+ expect(reveal).toHaveBeenCalledExactlyOnceWith(resolve(root, 'projects/nova'));
+ expect((await call('/__studio/projects/sample-miko-qipao/reveal', 'POST', {}, {}, reveal)).status).toBe(200);
+ expect((await call('/__studio/projects/nova/rig', 'POST', fixture, { origin: 'https://example.invalid' })).status).toBe(403);
+ expect((await call('/__studio/projects', 'GET', undefined, { host: 'example.invalid' })).status).toBe(403);
+ await rm(resolve(root, 'projects/nova/rig.json'));
+ await symlink(resolve(root, 'samples/miko-qipao/rig.json'), resolve(root, 'projects/nova/rig.json'));
+ expect((await call('/__studio/projects/nova/rig', 'POST', fixture)).status).toBe(400);
+});
+
+test('plugin is development-only and has no production or preview middleware hook', () => {
+ const plugin = localProjectsPlugin(root);
+ expect(plugin.apply).toBe('serve'); expect(plugin.configurePreviewServer).toBeUndefined();
+});
diff --git a/tests/project-jobs.test.ts b/tests/project-jobs.test.ts
new file mode 100644
index 0000000..0e4124a
--- /dev/null
+++ b/tests/project-jobs.test.ts
@@ -0,0 +1,111 @@
+import { afterEach, beforeEach, expect, test, vi } from 'vitest';
+import { mkdtemp, mkdir, writeFile, readFile, rm, readdir, symlink } from 'node:fs/promises';
+import { join, resolve } from 'node:path';
+import { Readable } from 'node:stream';
+import { execFile } from 'node:child_process';
+import type { IncomingMessage, ServerResponse } from 'node:http';
+import fixture from '../samples/miko-qipao/rig.json';
+import { JobError, projectJob, decodeVariants, runTool, type Runner } from '../src/server/project-jobs';
+import { localProjectMiddleware } from '../src/server/local-projects';
+vi.mock('node:child_process', () => ({ execFile: vi.fn() }));
+
+let root: string, project: string;
+beforeEach(async () => {
+ await mkdir(resolve('projects'), { recursive: true });
+ root = await mkdtemp(join(resolve('projects'), '.jobs-test-')); project = join(root, 'projects/nova');
+ await mkdir(join(project, 'built'), { recursive: true });
+ await writeFile(join(project, 'rig.json'), JSON.stringify(fixture));
+ await writeFile(join(project, 'source.png'), 'synthetic source');
+ await writeFile(join(project, 'built/base.png'), 'old layers');
+});
+afterEach(async () => { await rm(root, { recursive: true, force: true }); });
+function api(runner: Runner) {
+ const handler = localProjectMiddleware(root, vi.fn(async () => undefined), runner);
+ return async (url: string, body: unknown = { rig: fixture }, method = 'POST') => {
+ const req = Readable.from([Buffer.from(JSON.stringify(body))]) as IncomingMessage;
+ req.url = url; req.method = method; req.headers = { host: '127.0.0.1:5173', 'content-type': 'application/json' };
+ const result = { status: 0, body: '' };
+ const res = { writeHead(status: number) { result.status = status; }, end(value: string | Buffer) { result.body = String(value); } } as unknown as ServerResponse;
+ await handler(req, res, () => { result.status = 404; }); return result;
+ };
+}
+test('tool jobs run on an isolated project copy and publish only completed layers', async () => {
+ const runner: Runner = vi.fn(async (receivedRoot, stage, tool) => {
+ expect(receivedRoot).toBe(root); expect(stage.startsWith(join(root, 'projects/.studio-job-'))).toBe(true);
+ expect(tool).toBe('build-layers');
+ expect(await readFile(join(project, 'built/base.png'), 'utf8')).toBe('old layers');
+ await writeFile(join(stage, 'built/base.png'), 'rebuilt layers'); return 'built';
+ });
+ const call = api(runner), rig = structuredClone(fixture); rig.head.cx += 5;
+ expect((await call('/__studio/projects/nova/rebuild', { rig })).status).toBe(200);
+ expect(await readFile(join(project, 'built/base.png'), 'utf8')).toBe('rebuilt layers');
+ expect(JSON.parse(await readFile(join(project, 'rig.json.bak'), 'utf8'))).toEqual(fixture);
+ expect((await readdir(join(root, 'projects'))).filter(name => name.startsWith('.studio-job-'))).toEqual([]);
+});
+test('failure rolls back inputs and output layers, reports dependency errors and frees the project lock', async () => {
+ const runner: Runner = vi.fn(async (_root, stage) => { await writeFile(join(stage, 'built/base.png'), 'partial'); throw new JobError('dependencies', 'uv missing'); });
+ const call = api(runner);
+ const result = await call('/__studio/projects/nova/rebuild'); expect(result.status).toBe(422); expect(JSON.parse(result.body).code).toBe('dependencies');
+ expect(await readFile(join(project, 'built/base.png'), 'utf8')).toBe('old layers');
+ expect((await call('/__studio/projects/nova/rig', fixture)).status).toBe(200);
+ expect((await readdir(join(root, 'projects'))).filter(name => name.startsWith('.studio-job-'))).toEqual([]);
+});
+test('only one job or save runs per project and redirected paths cannot reach the runner', async () => {
+ let release!: () => void;
+ const runner: Runner = vi.fn(async () => { await new Promise<void>(done => { release = done; }); return 'built'; });
+ const call = api(runner), first = call('/__studio/projects/nova/rebuild');
+ await vi.waitFor(() => expect(runner).toHaveBeenCalledOnce());
+ expect((await call('/__studio/projects/nova/rig', fixture)).status).toBe(409);
+ expect((await call('/__studio/projects/nova/variant-requests')).status).toBe(409);
+ release(); await first;
+ await mkdir(join(root, 'outside')); await symlink(join(root, 'outside'), join(project, 'variants'));
+ const rejected = await call('/__studio/projects/nova/rebuild'); expect(rejected.status).toBe(422); expect(JSON.parse(rejected.body).code).toBe('unsafeFiles');
+ expect(runner).toHaveBeenCalledOnce();
+ for (const name of ['%2Fother', '..', 'nova%2F..%2Fother']) expect((await call(`/__studio/projects/${name}/rebuild`)).status).toBe(400);
+ expect((await readdir(join(root, 'outside'))).length).toBe(0);
+});
+test('variant import rejects filenames and failed validation preserves previous variants and sprites', async () => {
+ const png = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10, 0]);
+ for (const name of ['../mouth_a.png', '/mouth_a.png', 'mouth_a.jpg', 'other.png']) expect(() => decodeVariants({ files: [{ name, data: png.toString('base64') }] })).toThrow();
+ expect(() => decodeVariants({ files: [{ name: 'mouth_a.png', data: 'invalid' }] })).toThrow();
+ await mkdir(join(project, 'variants')); await writeFile(join(project, 'variants/mouth_a.png'), 'old variant');
+ const files = decodeVariants({ files: [{ name: 'mouth_a.png', data: png.toString('base64') }] });
+ await expect(projectJob(root, project, 'build-sprites', files, async (_root, stage) => {
+ expect(await readFile(join(stage, 'variants/mouth_a.png'))).toEqual(png);
+ throw new JobError('toolFailed', 'mouth_a: outside-mask max 255.000/255, mean 1.000000/255, 25 pixels over tolerance 8');
+ })).rejects.toBeInstanceOf(JobError);
+ expect(await readFile(join(project, 'variants/mouth_a.png'), 'utf8')).toBe('old variant');
+ expect(await readFile(join(project, 'built/base.png'), 'utf8')).toBe('old layers');
+});
+test('repository context/reveal are fixed to the root and request files expose only validated masks', async () => {
+ const reveal = vi.fn(async () => undefined);
+ const handler = localProjectMiddleware(root, reveal, async (_root, stage) => {
+ await mkdir(join(stage, 'variant-requests/mouth_a'), { recursive: true });
+ await writeFile(join(stage, 'variant-requests/mouth_a/prompt.md'), 'a prompt');
+ await writeFile(join(stage, 'variant-requests/mouth_a/mask.png'), 'a mask'); return 'prepared';
+ });
+ const request = async (url: string, method: string, body = {}) => {
+ const req = Readable.from([Buffer.from(JSON.stringify(body))]) as IncomingMessage;
+ req.url = url; req.method = method; req.headers = { host: 'localhost:5173', 'content-type': 'application/json' };
+ const result = { status: 0, body: '' }; const res = { writeHead(status: number) { result.status = status; }, end(value: string | Buffer) { result.body = String(value); } } as unknown as ServerResponse;
+ await handler(req, res, () => undefined); return result;
+ };
+ expect(JSON.parse((await request('/__studio/context', 'GET')).body).rootPath).toBe(root);
+ expect((await request('/__studio/reveal', 'POST', { path: '/other' })).status).toBe(200); expect(reveal).toHaveBeenCalledExactlyOnceWith(root);
+ const result = await request('/__studio/projects/nova/variant-requests', 'POST', { rig: fixture });
+ expect(result.status).toBe(200); expect(JSON.parse(result.body).requests[0].prompt).toBe('a prompt');
+ expect((await request('/__studio/projects/nova/variant-requests/mouth_a/mask.png', 'GET')).body).toBe('a mask');
+ expect((await request('/__studio/projects/nova/variant-requests/mouth_a/prompt.md', 'GET')).status).toBe(404);
+});
+test('tools use execFile with a fixed argument array, and missing dependencies return an actionable error', async () => {
+ const execute = vi.mocked(execFile);
+ execute.mockImplementation((...args: unknown[]) => {
+ (args[3] as (error: Error | null, stdout: string, stderr: string) => void)(null, 'complete', ''); return {} as ReturnType<typeof execFile>;
+ });
+ expect(await runTool(root, project, 'build-layers')).toBe('complete');
+ expect(execute).toHaveBeenCalledWith('uv', ['run', '--no-project', '--no-python-downloads', '--python', '>=3.10', '--with', 'numpy', '--with', 'pillow', '--with', 'opencv-python-headless', resolve(root, 'tools/build-layers.py'), project], expect.objectContaining({ cwd: root, timeout: 120000 }), expect.any(Function));
+ execute.mockImplementation((...args: unknown[]) => {
+ (args[3] as (error: Error, stdout: string, stderr: string) => void)(Object.assign(new Error('not installed'), { code: 'ENOENT' }), '', ''); return {} as ReturnType<typeof execFile>;
+ });
+ await expect(runTool(root, project, 'build-sprites')).rejects.toMatchObject({ code: 'dependencies' });
+});
diff --git a/tests/recent-projects.test.ts b/tests/recent-projects.test.ts
new file mode 100644
index 0000000..a86f8e1
--- /dev/null
+++ b/tests/recent-projects.test.ts
@@ -0,0 +1,18 @@
+import { afterEach, expect, test, vi } from 'vitest';
+import { addRecent, readRecent, saveRecent, RECENT_KEY, type RecentProject } from '../src/editor/recent-projects';
+
+afterEach(() => vi.unstubAllGlobals());
+const entry = (n: number): RecentProject => ({ id: `server:${n}`, kind: 'server', name: `project-${n}`, serverName: `project-${n}`, relativePath: `projects/project-${n}`, lastOpened: new Date(n * 1000).toISOString() });
+test('history deduplicates most-recent-first, caps at ten, and tolerates malformed or unavailable storage', () => {
+ let recent: RecentProject[] = [];
+ for (let n = 0; n < 15; n++) recent = addRecent(recent, entry(n));
+ expect(recent).toHaveLength(10); expect(recent[0].name).toBe('project-14');
+ recent = addRecent(recent, entry(7)); expect(recent[0].name).toBe('project-7'); expect(recent.filter(item => item.id === 'server:7')).toHaveLength(1);
+ const storage = new Map<string, string>();
+ vi.stubGlobal('localStorage', { getItem: (key: string) => storage.get(key), setItem: (key: string, value: string) => storage.set(key, value) });
+ saveRecent(recent); expect(readRecent()).toEqual(recent);
+ storage.set(RECENT_KEY, '{bad json'); expect(readRecent()).toEqual([]);
+ storage.set(RECENT_KEY, JSON.stringify([null, { name: 'bad' }, entry(1)])); expect(readRecent()).toEqual([entry(1)]);
+ vi.stubGlobal('localStorage', { getItem: () => { throw new Error('blocked'); }, setItem: () => { throw new Error('blocked'); } });
+ expect(readRecent()).toEqual([]); expect(() => saveRecent(recent)).not.toThrow();
+});
diff --git a/tests/stale.test.ts b/tests/stale.test.ts
index 72ba424..49babc0 100644
--- a/tests/stale.test.ts
+++ b/tests/stale.test.ts
@@ -6,7 +6,7 @@ import { layerSignature } from '../src/editor/stale';
test('cut-out changes mark layers stale without altering precut eye curves', () => {
const rig = parseRig(fixture);
const original = layerSignature(rig);
- rig.head.cx += 20;
+ rig.head.shiftX += 20;
rig.hand!.wrist[0] += 10;
expect(layerSignature(rig)).toBe(original);
rig.eyes[0].opening[0][0] += 5;
@@ -14,3 +14,23 @@ test('cut-out changes mark layers stale without altering precut eye curves', ()
expect(rig.eyes[0].top).toEqual(fixture.eyes[0].top);
expect(rig.eyes[0].bot).toEqual(fixture.eyes[0].bot);
});
+
+test('hair sampling and skin regions require rebuilding but physics strength does not', () => {
+ const original = layerSignature(parseRig(fixture));
+ for (const edit of [
+ (rig: ReturnType<typeof parseRig>) => { rig.strands![0].nodes[0][0] += 1; },
+ (rig: ReturnType<typeof parseRig>) => { rig.strands![0].sigma += 1; },
+ (rig: ReturnType<typeof parseRig>) => { rig.strands![0].max += 1; },
+ (rig: ReturnType<typeof parseRig>) => { rig.face.brow.cx += 1; },
+ (rig: ReturnType<typeof parseRig>) => { rig.head.rx += 1; },
+ (rig: ReturnType<typeof parseRig>) => { rig.buns!.bunL!.cx += 1; },
+ (rig: ReturnType<typeof parseRig>) => { rig.body.chest.cy += 1; },
+ ]) {
+ const rig = parseRig(fixture);
+ edit(rig);
+ expect(layerSignature(rig)).not.toBe(original);
+ }
+ const rig = parseRig(fixture);
+ rig.strands![0].k += 0.1;
+ expect(layerSignature(rig)).toBe(original);
+});
diff --git a/vite.config.ts b/vite.config.ts
index a37cdd3..c12db95 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -1,8 +1,11 @@
import { defineConfig } from 'vitest/config';
import react from '@vitejs/plugin-react';
import { readFileSync } from 'node:fs';
+import { fileURLToPath } from 'node:url';
+import { localProjectsPlugin } from './src/server/local-projects';
export default defineConfig({
- plugins: [react(), {
+ server: { host: '127.0.0.1' },
+ plugins: [react(), localProjectsPlugin(fileURLToPath(new URL('.', import.meta.url))), {
name: 'sample-rig',
resolveId(id) {
if (id === 'virtual:sample-rig') return '\0sample-rig';