@seavideo/cli 0.3.0 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +92 -452
  2. package/README.publish.md +169 -0
  3. package/dist/skills/seavideo-canvas-tools/SKILL.md +324 -0
  4. package/dist/skills/seavideo-command/SKILL.md +215 -0
  5. package/dist/skills/seavideo-prompt-design/SKILL.md +735 -0
  6. package/dist/skills/seavideo-setup/SKILL.md +195 -0
  7. package/dist/skills/seavideo-setup/references/agents/claude-code.md +31 -0
  8. package/dist/skills/seavideo-setup/references/agents/codex.md +28 -0
  9. package/dist/skills/seavideo-setup/references/authentication.md +69 -0
  10. package/dist/skills/seavideo-setup/references/install-skills.md +68 -0
  11. package/dist/skills/seavideo-setup/references/install.md +92 -0
  12. package/dist/skills/seavideo-setup/references/platforms/linux.md +29 -0
  13. package/dist/skills/seavideo-setup/references/platforms/macos.md +31 -0
  14. package/dist/skills/seavideo-setup/references/platforms/windows.md +55 -0
  15. package/dist/skills/seavideo-setup/references/troubleshooting.md +69 -0
  16. package/dist/skills/seavideo-setup/scripts/install-cli.ps1 +109 -0
  17. package/dist/skills/seavideo-setup/scripts/install-cli.sh +89 -0
  18. package/dist/skills/seavideo-setup/scripts/install-skills.mjs +130 -0
  19. package/dist/skills/seavideo-setup/scripts/preflight.ps1 +60 -0
  20. package/dist/skills/seavideo-setup/scripts/preflight.sh +25 -0
  21. package/dist/skills/seavideo-setup/scripts/setup.ps1 +74 -0
  22. package/dist/skills/seavideo-setup/scripts/setup.sh +78 -0
  23. package/dist/skills/seavideo-workflow-draft/SKILL.md +203 -0
  24. package/dist/skills/seavideo-workflow-image/SKILL.md +283 -0
  25. package/dist/skills/seavideo-workflow-video/SKILL.md +352 -0
  26. package/dist/src/agent-skills/agents.js +62 -0
  27. package/dist/src/agent-skills/backup.js +17 -0
  28. package/dist/src/agent-skills/install.js +155 -0
  29. package/dist/src/agent-skills/manifest.js +68 -0
  30. package/dist/src/agent-skills/payload.js +46 -0
  31. package/dist/src/agent-skills/store.js +38 -0
  32. package/dist/src/canvas/browser.js +5 -0
  33. package/dist/src/canvas/launch.js +77 -42
  34. package/dist/src/canvas/lifecycle.js +21 -4
  35. package/dist/src/canvas/puppeteer.js +56 -6
  36. package/dist/src/cli/commands/agent-skills.js +249 -0
  37. package/dist/src/cli/commands/canvas/session.js +3 -1
  38. package/dist/src/cli/main.js +36 -6
  39. package/dist/src/cli/program.js +27 -2
  40. package/dist/src/cli/session.js +2 -0
  41. package/dist/src/deps.js +6 -1
  42. package/dist/src/i18n.js +86 -2
  43. package/dist/src/update/check-cache.js +0 -1
  44. package/dist/src/update/notify.js +6 -4
  45. package/dist/src/update/passive.js +9 -17
  46. package/package.json +4 -11
@@ -0,0 +1,46 @@
1
+ import { dirname, join, resolve } from 'node:path';
2
+ import { CliError } from "../errors.js";
3
+ import { t } from "../i18n.js";
4
+ export const SKILL_ROOTS = [
5
+ ['..', 'skills'],
6
+ ['..', 'packaging'],
7
+ ];
8
+ async function isDirectory(deps, path) {
9
+ let result;
10
+ try {
11
+ await deps.fs.stat(path);
12
+ result = true;
13
+ }
14
+ catch {
15
+ result = false;
16
+ }
17
+ return result;
18
+ }
19
+ export async function locateSkillsPayload(deps, expected) {
20
+ const base = dirname(resolve(deps.scriptPath()));
21
+ const roots = SKILL_ROOTS.map((segments) => join(base, ...segments));
22
+ const sources = new Map();
23
+ const missing = [];
24
+ for (const skill of expected) {
25
+ let found;
26
+ for (const root of roots) {
27
+ const candidate = join(root, skill);
28
+ const usable = found === undefined && (await isDirectory(deps, candidate));
29
+ if (usable) {
30
+ found = candidate;
31
+ }
32
+ else {
33
+ }
34
+ }
35
+ if (found === undefined)
36
+ missing.push(skill);
37
+ else
38
+ sources.set(skill, found);
39
+ }
40
+ if (missing.length > 0) {
41
+ throw new CliError('config', t('agentSkills.payloadMissing', {}, deps.env), {
42
+ detail: t('agentSkills.payloadMissingDetail', { path: roots.join(', '), missing: missing.join(', ') }, deps.env),
43
+ });
44
+ }
45
+ return { sources };
46
+ }
@@ -0,0 +1,38 @@
1
+ import { join } from 'node:path';
2
+ import { writeFileAtomic } from "../atomic-write.js";
3
+ import { manifestPath } from "./manifest.js";
4
+ const MODE_FILE = 0o600;
5
+ const MODE_DIR = 0o700;
6
+ export const LOCK_FILE = 'agent-skills.lock';
7
+ export function lockPath(home) {
8
+ return join(home, '.seavideo', LOCK_FILE);
9
+ }
10
+ export class LockHeldError extends Error {
11
+ path;
12
+ constructor(path) {
13
+ super(path);
14
+ this.name = 'LockHeldError';
15
+ this.path = path;
16
+ }
17
+ }
18
+ export async function acquireLock(deps, home) {
19
+ const path = lockPath(home);
20
+ await deps.fs.mkdir(join(home, '.seavideo'), { recursive: true, mode: MODE_DIR });
21
+ try {
22
+ await deps.fs.writeFile(path, deps.clock().toISOString(), { mode: MODE_FILE, flag: 'wx' });
23
+ }
24
+ catch (error) {
25
+ if (error.code === 'EEXIST')
26
+ throw new LockHeldError(path);
27
+ throw error;
28
+ }
29
+ return path;
30
+ }
31
+ export async function releaseLock(deps, path) {
32
+ await deps.fs.rm(path, { recursive: false, force: true });
33
+ }
34
+ export async function writeManifest(deps, home, manifest) {
35
+ await deps.fs.mkdir(join(home, '.seavideo'), { recursive: true, mode: MODE_DIR });
36
+ const content = `${JSON.stringify(manifest, null, 2)}\n`;
37
+ await writeFileAtomic(deps, manifestPath(home), content, MODE_FILE);
38
+ }
@@ -1,3 +1,8 @@
1
+ export function craftCanvasOf(config, canvasId) {
2
+ return config.platform === 'cn'
3
+ ? { origin: new URL(config.seavideoBaseUrl).origin, id: canvasId }
4
+ : null;
5
+ }
1
6
  export const LAUNCH_ARGS = [
2
7
  '--disable-gpu',
3
8
  '--disable-blink-features=AutomationControlled',
@@ -2,11 +2,17 @@ import { CliError } from "../errors.js";
2
2
  import { t } from "../i18n.js";
3
3
  export const AGENT_LAUNCH_TIMEOUT_MS = 20_000;
4
4
  const CANVAS_ID_PATTERN = /^[A-Za-z0-9_-]{1,128}$/;
5
+ const COOKIE_NAME_PATTERN = /^[A-Za-z0-9_-]{1,64}$/u;
6
+ const COOKIE_VALUE_PATTERN = /^[A-Za-z0-9_-]{32,128}$/u;
7
+ export function craftCanvasUrl(craftOrigin, canvasId) {
8
+ const url = new URL('/apps/seaflow/standalone', craftOrigin);
9
+ url.searchParams.set('redirect', `/workflow/${canvasId}`);
10
+ return url.href;
11
+ }
5
12
  export async function requestCanvasLaunch(deps, config, canvasId, token) {
6
13
  const envMap = deps.env;
7
14
  const craft = new URL(config.seavideoBaseUrl);
8
- const flow = new URL(config.flowWebBaseUrl);
9
- if (craft.protocol !== 'https:' || flow.protocol !== 'https:') {
15
+ if (craft.protocol !== 'https:') {
10
16
  throw new CliError('config', t('launch.insecureOrigin', {}, envMap), {
11
17
  detail: t('launch.insecureOriginDetail', {}, envMap),
12
18
  });
@@ -18,7 +24,7 @@ export async function requestCanvasLaunch(deps, config, canvasId, token) {
18
24
  response = await deps.fetch(endpoint, {
19
25
  method: 'POST',
20
26
  headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' },
21
- body: JSON.stringify({ canvas_id: canvasId }),
27
+ body: JSON.stringify({ canvas_id: canvasId, mode: 'craft' }),
22
28
  redirect: 'error',
23
29
  signal: AbortSignal.timeout(AGENT_LAUNCH_TIMEOUT_MS),
24
30
  });
@@ -42,7 +48,7 @@ export async function requestCanvasLaunch(deps, config, canvasId, token) {
42
48
  detail: err instanceof Error ? err.message : String(err),
43
49
  });
44
50
  }
45
- return assertLaunchPayload(payload, flow, canvasId, token, envMap);
51
+ return assertLaunchPayload(payload, craft, canvasId, response.headers, envMap);
46
52
  }
47
53
  function assertCanvasId(canvasId, envMap) {
48
54
  if (!CANVAS_ID_PATTERN.test(canvasId)) {
@@ -51,47 +57,76 @@ function assertCanvasId(canvasId, envMap) {
51
57
  });
52
58
  }
53
59
  }
54
- function assertLaunchPayload(payload, flow, canvasId, token, envMap) {
55
- const expectedUrl = new URL(`/workflow/${canvasId}`, flow.origin).href;
56
- const failure = () => new CliError('server', t('launch.mismatch', {}, envMap), {
60
+ function assertLaunchPayload(payload, craft, canvasId, headers, envMap) {
61
+ const expected = craftCanvasUrl(craft.origin, canvasId);
62
+ const mismatch = () => new CliError('server', t('launch.mismatch', {}, envMap), {
57
63
  detail: t('launch.mismatchDetail', {}, envMap),
58
64
  });
59
- if (typeof payload !== 'object' || payload === null)
60
- throw failure();
61
- const root = payload;
62
- if (root['code'] !== 10_000)
63
- throw failure();
64
- const data = root['data'];
65
- if (typeof data !== 'object' || data === null)
66
- throw failure();
67
- const d = data;
68
- if (d['canvas_id'] !== canvasId)
69
- throw failure();
70
- if (d['open_url'] !== expectedUrl)
71
- throw failure();
72
- const rawCookie = d['cookie'];
73
- if (typeof rawCookie !== 'object' || rawCookie === null)
74
- throw failure();
75
- const c = rawCookie;
76
- const sameSite = c['sameSite'];
77
- if (c['name'] !== 'T' ||
78
- c['url'] !== `${flow.origin}/` ||
79
- c['path'] !== '/' ||
80
- c['secure'] !== true ||
81
- c['httpOnly'] !== false ||
82
- sameSite !== 'Lax') {
83
- throw failure();
65
+ const data = readLaunchData(payload, expected, canvasId, mismatch);
66
+ const cookie = parseSetCookie(headers, data.sessionCookieName, craft.origin, mismatch);
67
+ return { url: expected, cookie };
68
+ }
69
+ function readLaunchData(payload, expected, canvasId, mismatch) {
70
+ const root = asRecord(payload);
71
+ const data = asRecord(root?.['data']);
72
+ let result;
73
+ if (root === null ||
74
+ root['code'] !== 10_000 ||
75
+ data === null ||
76
+ data['mode'] !== 'craft' ||
77
+ data['canvas_id'] !== canvasId ||
78
+ data['open_url'] !== expected) {
79
+ throw mismatch();
80
+ }
81
+ else {
82
+ const name = data['session_cookie_name'];
83
+ if (typeof name !== 'string' || !COOKIE_NAME_PATTERN.test(name))
84
+ throw mismatch();
85
+ result = { sessionCookieName: name };
84
86
  }
87
+ return result;
88
+ }
89
+ function parseSetCookie(headers, expectedName, craftOrigin, mismatch) {
90
+ const cookies = headers.getSetCookie();
91
+ const first = cookies.length === 1 ? cookies[0] : null;
92
+ if (first === null)
93
+ throw mismatch();
94
+ const [pair, ...attributes] = first.split(';').map((part) => part.trim());
95
+ const parts = pair.split('=');
96
+ const name = parts[0];
97
+ const value = parts[1];
98
+ const flags = new Set(attributes.map((attr) => attr.toLowerCase()));
99
+ const expiry = attributes.find((attr) => /^expires=/iu.test(attr));
100
+ const expires = expiry === undefined ? null : parseExpiry(expiry);
101
+ const shapeOk = parts.length === 2 &&
102
+ name === expectedName &&
103
+ typeof value === 'string' &&
104
+ COOKIE_VALUE_PATTERN.test(value);
105
+ const flagsOk = flags.has('secure') &&
106
+ flags.has('httponly') &&
107
+ flags.has('path=/') &&
108
+ flags.has('samesite=lax') &&
109
+ !attributes.some((attr) => /^domain=/iu.test(attr));
110
+ const freshOk = expires !== null && expires > Date.now() / 1000;
111
+ if (!shapeOk || !flagsOk || !freshOk)
112
+ throw mismatch();
85
113
  return {
86
- url: expectedUrl,
87
- cookie: {
88
- name: 'T',
89
- value: token,
90
- url: `${flow.origin}/`,
91
- path: '/',
92
- secure: true,
93
- httpOnly: false,
94
- sameSite,
95
- },
114
+ name,
115
+ value: value,
116
+ url: `${craftOrigin}/`,
117
+ path: '/',
118
+ secure: true,
119
+ httpOnly: true,
120
+ sameSite: 'Lax',
121
+ expires,
96
122
  };
97
123
  }
124
+ function parseExpiry(attribute) {
125
+ const ms = Date.parse(attribute.slice('expires='.length));
126
+ return Number.isFinite(ms) ? ms / 1000 : null;
127
+ }
128
+ function asRecord(value) {
129
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
130
+ ? value
131
+ : null;
132
+ }
@@ -1,3 +1,5 @@
1
+ import { CliError } from "../errors.js";
2
+ import { t } from "../i18n.js";
1
3
  import { appendEngineLog } from "./engine-log.js";
2
4
  import { clearSessionRecord, readSessionRecord } from "./session-record.js";
3
5
  import { clearToolSpecs } from "./tool-specs.js";
@@ -17,10 +19,10 @@ export function daemonDepsOf(scope) {
17
19
  platform: deps.platform,
18
20
  };
19
21
  }
20
- export async function closeCanvasSession(scope, env) {
21
- return withCanvasLock(scope, env, () => closeInLock(scope, env));
22
+ export async function closeCanvasSession(scope, env, waitForExit = false) {
23
+ return withCanvasLock(scope, env, () => closeInLock(scope, env, waitForExit));
22
24
  }
23
- async function closeInLock(scope, env) {
25
+ async function closeInLock(scope, env, waitForExit) {
24
26
  const { deps } = scope;
25
27
  const sessionFile = scope.paths.canvasSessionFile(env);
26
28
  const record = await readSessionRecord(sessionFile, deps);
@@ -29,7 +31,6 @@ async function closeInLock(scope, env) {
29
31
  result = null;
30
32
  }
31
33
  else {
32
- await clearSessionRecord(sessionFile, deps);
33
34
  if (deps.processAlive(record.pid)) {
34
35
  try {
35
36
  deps.terminate(record.pid);
@@ -39,6 +40,12 @@ async function closeInLock(scope, env) {
39
40
  }
40
41
  else {
41
42
  }
43
+ if (waitForExit) {
44
+ await waitExit(deps, record.pid);
45
+ }
46
+ else {
47
+ }
48
+ await clearSessionRecord(sessionFile, deps);
42
49
  await appendEngineLog(deps, scope.paths.engineLogFile(env), {
43
50
  at: deps.clock().toISOString(),
44
51
  event: 'exit',
@@ -63,3 +70,13 @@ async function removeProfileDir(scope, env) {
63
70
  catch {
64
71
  }
65
72
  }
73
+ async function waitExit(deps, pid) {
74
+ for (let attempt = 0; attempt < 80 && deps.processAlive(pid); attempt += 1) {
75
+ await deps.sleep(100);
76
+ }
77
+ if (deps.processAlive(pid)) {
78
+ throw new CliError('timeout', t('canvas.previousClosing', {}, deps.env));
79
+ }
80
+ else {
81
+ }
82
+ }
@@ -35,7 +35,7 @@ export function connectPuppeteerBrowser(options) {
35
35
  async open(url, cookie) {
36
36
  browser ??= await options.connect({ port: options.port });
37
37
  page = await pageToNavigate(browser);
38
- return openOnPage(page, url, timeoutMs, envMap, cookie);
38
+ return openOnPage(page, url, timeoutMs, envMap, cookie, options.craftCanvas);
39
39
  },
40
40
  async evaluate(expression) {
41
41
  browser ??= await options.connect({ port: options.port });
@@ -43,7 +43,8 @@ export function connectPuppeteerBrowser(options) {
43
43
  if (page === null) {
44
44
  throw new CliError('config', t('canvas.notOpened', {}, envMap));
45
45
  }
46
- return page.evaluate(expression);
46
+ const target = await toolTarget(page, options.craftCanvas, timeoutMs, envMap);
47
+ return target.evaluate(expression);
47
48
  },
48
49
  async screenshot() {
49
50
  browser ??= await options.connect({ port: options.port });
@@ -84,6 +85,7 @@ async function writeLaunchCookie(page, cookie, envMap) {
84
85
  secure: cookie.secure,
85
86
  httpOnly: cookie.httpOnly,
86
87
  sameSite: cookie.sameSite,
88
+ expires: cookie.expires,
87
89
  });
88
90
  if (!result.success) {
89
91
  throw new CliError('server', t('launch.cookieRejected', {}, envMap), {
@@ -95,7 +97,54 @@ async function writeLaunchCookie(page, cookie, envMap) {
95
97
  await session.detach();
96
98
  }
97
99
  }
98
- async function openOnPage(page, url, timeoutMs, envMap, cookie) {
100
+ async function toolTarget(page, canvas, timeoutMs, envMap) {
101
+ let target;
102
+ if (canvas === null || canvas === undefined) {
103
+ target = page;
104
+ }
105
+ else if (!onCraftPage(page, canvas)) {
106
+ throw new CliError('server', t('canvas.craftLocationChanged', {}, envMap));
107
+ }
108
+ else {
109
+ target = await page.waitForFrame((candidate) => matchesCanvasFrame(candidate, page, canvas), {
110
+ timeout: timeoutMs,
111
+ });
112
+ }
113
+ return target;
114
+ }
115
+ function onCraftPage(page, canvas) {
116
+ const url = new URL(page.url());
117
+ return (url.origin === canvas.origin &&
118
+ url.pathname === '/apps/seaflow/standalone' &&
119
+ url.searchParams.get('redirect') === `/workflow/${canvas.id}`);
120
+ }
121
+ async function matchesCanvasFrame(candidate, page, canvas) {
122
+ const anchored = candidate.parentFrame() === page.mainFrame() && onCraftPage(page, canvas);
123
+ const current = anchored
124
+ ? new URL(candidate.url() === '' ? 'about:blank' : candidate.url())
125
+ : null;
126
+ const urlOk = current !== null &&
127
+ current.protocol === 'https:' &&
128
+ current.pathname === `/workflow/${canvas.id}`;
129
+ const element = urlOk ? await candidate.frameElement() : null;
130
+ const matched = element !== null && current !== null
131
+ ? await evaluateFrameElement(element, canvas, current.origin)
132
+ : false;
133
+ return matched;
134
+ }
135
+ async function evaluateFrameElement(element, canvas, frameOrigin) {
136
+ try {
137
+ return await element.evaluate((el, expected) => {
138
+ const node = el;
139
+ const want = expected;
140
+ return node.dataset.seavideoCanvas === want.id && new URL(node.src).origin === want.origin;
141
+ }, { id: canvas.id, origin: frameOrigin });
142
+ }
143
+ finally {
144
+ await element.dispose();
145
+ }
146
+ }
147
+ async function openOnPage(page, url, timeoutMs, envMap, cookie, canvas) {
99
148
  if (cookie !== undefined) {
100
149
  await writeLaunchCookie(page, cookie, envMap);
101
150
  }
@@ -121,8 +170,9 @@ async function openOnPage(page, url, timeoutMs, envMap, cookie) {
121
170
  }
122
171
  else {
123
172
  }
173
+ const target = await toolTarget(page, canvas, timeoutMs, envMap);
124
174
  try {
125
- await page.waitForFunction(WAIT_TOOLS_EXPR, { timeout: timeoutMs });
175
+ await target.waitForFunction(WAIT_TOOLS_EXPR, { timeout: timeoutMs });
126
176
  }
127
177
  catch {
128
178
  const title = await page.title().catch(() => '');
@@ -136,9 +186,9 @@ async function openOnPage(page, url, timeoutMs, envMap, cookie) {
136
186
  detail: t('canvas.noToolsDetail', { title, url: actual }, envMap),
137
187
  });
138
188
  }
139
- const raw = await page.evaluate(READ_TOOLS_EXPR);
189
+ const raw = await target.evaluate(READ_TOOLS_EXPR);
140
190
  const tools = raw.map((item) => normalizeDefinition(item, envMap));
141
- const title = await page.title().catch(() => '');
191
+ const title = await target.title().catch(() => '');
142
192
  return { url: actualUrlOf(page, url), title, tools };
143
193
  }
144
194
  function actualUrlOf(page, requested) {
@@ -0,0 +1,249 @@
1
+ import { join, resolve } from 'node:path';
2
+ import { CliError } from "../../errors.js";
3
+ import { t } from "../../i18n.js";
4
+ import { renderTable, style } from "../output.js";
5
+ import { AGENT_IDS, isAgentId } from "../../agent-skills/agents.js";
6
+ import { installTargets, resolveTargets, } from "../../agent-skills/install.js";
7
+ import { locateSkillsPayload } from "../../agent-skills/payload.js";
8
+ import { ManifestCorruptError, normalizeRoot, readManifest, } from "../../agent-skills/manifest.js";
9
+ import { acquireLock, LockHeldError, releaseLock, writeManifest, } from "../../agent-skills/store.js";
10
+ import { timestampOf } from "../../agent-skills/backup.js";
11
+ import { VERSION } from "../program.js";
12
+ export const BUNDLED_SKILLS = [
13
+ 'seavideo-setup',
14
+ 'seavideo-command',
15
+ 'seavideo-canvas-tools',
16
+ 'seavideo-prompt-design',
17
+ 'seavideo-workflow-draft',
18
+ 'seavideo-workflow-image',
19
+ 'seavideo-workflow-video',
20
+ ];
21
+ const LEGACY_SKILLS = ['seavideo-skill'];
22
+ export function planOf(options, cwd, env) {
23
+ const scope = options.global === true ? 'user' : 'project';
24
+ const agents = parseAgents(options.agent ?? [], env);
25
+ let plan;
26
+ if (options.skillsDir !== undefined) {
27
+ const conflicting = agents !== undefined || options.project !== undefined;
28
+ if (conflicting) {
29
+ throw new CliError('usage', t('agentSkills.skillsDirConflict', {}, env));
30
+ }
31
+ plan = { scope, agents: undefined, project: cwd, skillsDir: options.skillsDir };
32
+ }
33
+ else if (scope === 'user' && options.project !== undefined) {
34
+ throw new CliError('usage', t('agentSkills.globalWithProject', {}, env));
35
+ }
36
+ else {
37
+ plan = { scope, agents, project: options.project ?? cwd, skillsDir: undefined };
38
+ }
39
+ return plan;
40
+ }
41
+ function parseAgents(values, env) {
42
+ let result;
43
+ if (values.length === 0) {
44
+ result = undefined;
45
+ }
46
+ else if (values.length === 1 && values[0] === 'all') {
47
+ result = 'all';
48
+ }
49
+ else {
50
+ result = values.map((value) => {
51
+ if (!isAgentId(value)) {
52
+ throw new CliError('usage', t('agentSkills.unknownAgent', { value, agents: AGENT_IDS.join(', ') }, env));
53
+ }
54
+ return value;
55
+ });
56
+ }
57
+ return result;
58
+ }
59
+ function inputOf(ctx, project) {
60
+ return {
61
+ home: ctx.deps.homeDir(),
62
+ project: resolve(project),
63
+ env: ctx.deps.env,
64
+ };
65
+ }
66
+ async function targetsOf(ctx, plan) {
67
+ return resolveTargets(ctx.deps, {
68
+ agents: plan.agents,
69
+ scope: plan.scope,
70
+ project: inputOf(ctx, plan.project).project,
71
+ skillsDir: plan.skillsDir,
72
+ customBase: inputOf(ctx, plan.project).project,
73
+ });
74
+ }
75
+ function say(ctx, options, text) {
76
+ if (options.quiet === true) {
77
+ }
78
+ else {
79
+ ctx.deps.stdout(`${text}\n`);
80
+ }
81
+ }
82
+ function uniqueHints(failures) {
83
+ const hints = failures
84
+ .map((failure) => failure.hint)
85
+ .filter((hint) => hint !== null);
86
+ return [...new Set(hints)];
87
+ }
88
+ async function removeLegacy(ctx, targets) {
89
+ for (const target of targets) {
90
+ for (const legacy of LEGACY_SKILLS) {
91
+ await ctx.deps.fs.rm(join(target.root, legacy), { recursive: true, force: true });
92
+ }
93
+ }
94
+ }
95
+ export async function agentSkillsAddCommand(ctx, options) {
96
+ const { deps } = ctx;
97
+ const plan = planOf(options, deps.cwd(), deps.env);
98
+ const targets = await targetsOf(ctx, plan);
99
+ const payload = await locateSkillsPayload(deps, BUNDLED_SKILLS);
100
+ const home = deps.homeDir();
101
+ let code = 0;
102
+ if (options.dryRun === true) {
103
+ for (const target of targets) {
104
+ say(ctx, options, t('agentSkills.plannedTarget', { root: target.root }, deps.env));
105
+ for (const skill of BUNDLED_SKILLS) {
106
+ say(ctx, options, ` ${t('agentSkills.plannedInstall', { skill }, deps.env)}`);
107
+ }
108
+ }
109
+ }
110
+ else {
111
+ code = await runAdd(ctx, options, targets, payload.sources, home);
112
+ }
113
+ return code;
114
+ }
115
+ async function runAdd(ctx, options, targets, sources, home) {
116
+ const { deps } = ctx;
117
+ const lock = await lockOf(ctx, home);
118
+ try {
119
+ const manifest = await readManifest(deps, home);
120
+ const timestamp = timestampOf(deps.clock());
121
+ await removeLegacy(ctx, targets);
122
+ const result = await installTargets(deps, home, targets, manifest.entries, {
123
+ sources,
124
+ skills: BUNDLED_SKILLS,
125
+ force: options.force === true,
126
+ cliVersion: VERSION,
127
+ timestamp,
128
+ });
129
+ for (const backup of result.backedUp) {
130
+ say(ctx, options, t('agentSkills.backedUp', { skill: backup.skill, backup: backup.backup }, deps.env));
131
+ }
132
+ for (const failure of result.failures) {
133
+ deps.stderr(`${t('agentSkills.failed', { skill: failure.skill, reason: failure.reason }, deps.env)}\n`);
134
+ }
135
+ for (const hint of uniqueHints(result.failures)) {
136
+ deps.stderr(` ${t('agentSkills.hint', { hint }, deps.env)}\n`);
137
+ }
138
+ for (const item of result.installed) {
139
+ say(ctx, options, t('agentSkills.addDone', { count: item.skills.length, root: item.target.root }, deps.env));
140
+ }
141
+ for (const item of result.removed) {
142
+ say(ctx, options, t('agentSkills.removed', { count: item.skills.length, root: item.target.root }, deps.env));
143
+ }
144
+ await writeManifest(deps, home, { version: 1, entries: result.entries });
145
+ const failed = result.failures.length > 0;
146
+ if (failed) {
147
+ deps.stderr(`${t('agentSkills.partialFailure', { count: result.failures.length }, deps.env)}\n`);
148
+ }
149
+ else {
150
+ }
151
+ return failed ? 1 : 0;
152
+ }
153
+ finally {
154
+ await releaseLock(deps, lock);
155
+ }
156
+ }
157
+ async function lockOf(ctx, home) {
158
+ try {
159
+ return await acquireLock(ctx.deps, home);
160
+ }
161
+ catch (error) {
162
+ if (error instanceof LockHeldError) {
163
+ throw new CliError('config', t('agentSkills.locked', { path: error.path }, ctx.deps.env));
164
+ }
165
+ throw error;
166
+ }
167
+ }
168
+ export async function agentSkillsRemoveCommand(ctx, options) {
169
+ const { deps } = ctx;
170
+ const plan = planOf(options, deps.cwd(), deps.env);
171
+ const home = deps.homeDir();
172
+ const targets = await targetsOf(ctx, plan);
173
+ const roots = new Set(targets.map((target) => normalizeRoot(target.root)));
174
+ const manifest = await manifestOrFail(ctx, home);
175
+ const selected = manifest.entries.filter((entry) => roots.has(normalizeRoot(entry.root)));
176
+ if (options.dryRun === true) {
177
+ for (const entry of selected) {
178
+ say(ctx, options, ` ${t('agentSkills.plannedRemove', { skill: entry.skill }, deps.env)}`);
179
+ }
180
+ }
181
+ else {
182
+ await removeEntries(ctx, home, manifest, selected, roots, options);
183
+ }
184
+ return 0;
185
+ }
186
+ async function removeEntries(ctx, home, manifest, selected, roots, options) {
187
+ const { deps } = ctx;
188
+ for (const entry of selected) {
189
+ await deps.fs.rm(entry.path, { recursive: true, force: true });
190
+ await restoreBackup(ctx, entry, options);
191
+ }
192
+ const kept = manifest.entries.filter((entry) => !roots.has(normalizeRoot(entry.root)));
193
+ await writeManifest(deps, home, { version: manifest.version, entries: kept });
194
+ let message;
195
+ if (selected.length === 0) {
196
+ message = t('agentSkills.nothingToRemove', {}, deps.env);
197
+ }
198
+ else {
199
+ message = t('agentSkills.removedAll', { count: selected.length }, deps.env);
200
+ }
201
+ say(ctx, options, message);
202
+ }
203
+ async function manifestOrFail(ctx, home) {
204
+ try {
205
+ return await readManifest(ctx.deps, home);
206
+ }
207
+ catch (error) {
208
+ if (error instanceof ManifestCorruptError) {
209
+ throw new CliError('config', t('agentSkills.manifestCorrupt', { path: error.path, reason: error.reason }, ctx.deps.env), { detail: t('agentSkills.manifestMissingDetail', {}, ctx.deps.env) });
210
+ }
211
+ throw error;
212
+ }
213
+ }
214
+ export async function agentSkillsListCommand(ctx, options) {
215
+ const { deps } = ctx;
216
+ const plan = planOf(options, deps.cwd(), deps.env);
217
+ const home = deps.homeDir();
218
+ const targets = await targetsOf(ctx, plan);
219
+ const roots = new Set(targets.map((target) => normalizeRoot(target.root)));
220
+ const manifest = await readManifest(deps, home);
221
+ const entries = manifest.entries.filter((entry) => roots.has(normalizeRoot(entry.root)));
222
+ deps.stdout(`${renderList(entries, manifest.version, options, deps.env)}\n`);
223
+ return 0;
224
+ }
225
+ function renderList(entries, version, options, env) {
226
+ let result;
227
+ if (options.json === true) {
228
+ result = JSON.stringify({ version, entries }, null, 2);
229
+ }
230
+ else if (entries.length === 0) {
231
+ result = style.dim(t('agentSkills.listEmpty', {}, env));
232
+ }
233
+ else {
234
+ result = renderTable([
235
+ [t('agentSkills.listHeader', {}, env)],
236
+ ...entries.map((entry) => [entry.agent, entry.scope, entry.skill, entry.cliVersion]),
237
+ ]);
238
+ }
239
+ return result;
240
+ }
241
+ async function restoreBackup(ctx, entry, options) {
242
+ if (entry.backup === null)
243
+ return;
244
+ await ctx.deps.fs.cp(entry.backup, entry.path, {
245
+ recursive: true,
246
+ verbatimSymlinks: true,
247
+ });
248
+ say(ctx, options, t('agentSkills.restored', { skill: entry.skill }, ctx.deps.env));
249
+ }
@@ -1,6 +1,7 @@
1
1
  import { CliError, errorMessage } from "../../../errors.js";
2
2
  import { appendEngineLog } from "../../../canvas/engine-log.js";
3
3
  import { connectPuppeteerBrowser, puppeteerConnect } from "../../../canvas/puppeteer.js";
4
+ import { craftCanvasOf } from "../../../canvas/browser.js";
4
5
  import { DEFAULT_IDLE_TIMEOUT_MS, spawnCanvasBrowser } from "../../../canvas/daemon.js";
5
6
  import { clearSessionRecord, readSessionRecord, sessionFingerprint, writeSessionRecord, } from "../../../canvas/session-record.js";
6
7
  import { closeCanvasSession, daemonDepsOf } from "../../../canvas/lifecycle.js";
@@ -54,7 +55,7 @@ async function startCanvas(ctx, args) {
54
55
  const { deps } = ctx;
55
56
  const { id, url, cookie, config, enginePath, headful, json } = args;
56
57
  const { env } = config;
57
- const replaced = await closeCanvasSession(ctx, env);
58
+ const replaced = await closeCanvasSession(ctx, env, config.platform === 'cn');
58
59
  const sessionFile = ctx.paths.canvasSessionFile(env);
59
60
  const logFile = ctx.paths.engineLogFile(env);
60
61
  const daemonDeps = daemonDepsOf(ctx);
@@ -72,6 +73,7 @@ async function startCanvas(ctx, args) {
72
73
  args: spawned.args,
73
74
  });
74
75
  const browser = connectPuppeteerBrowser({
76
+ craftCanvas: craftCanvasOf(config, id),
75
77
  connect: puppeteerConnect,
76
78
  port: spawned.port,
77
79
  envMap: deps.env,