@seavideo/cli 0.0.0-stage → 0.3.0
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.
- package/LICENSE +12 -0
- package/README.md +528 -2
- package/dist/bin/seavideo.js +11 -0
- package/dist/src/atomic-write.js +14 -0
- package/dist/src/canvas/api.js +172 -0
- package/dist/src/canvas/browser.js +40 -0
- package/dist/src/canvas/chrome-binary.js +109 -0
- package/dist/src/canvas/daemon.js +264 -0
- package/dist/src/canvas/engine-install.js +46 -0
- package/dist/src/canvas/engine-log.js +9 -0
- package/dist/src/canvas/engine.js +99 -0
- package/dist/src/canvas/entry.js +16 -0
- package/dist/src/canvas/env-lock.js +164 -0
- package/dist/src/canvas/launch.js +97 -0
- package/dist/src/canvas/lifecycle.js +65 -0
- package/dist/src/canvas/names.js +61 -0
- package/dist/src/canvas/params.js +38 -0
- package/dist/src/canvas/puppeteer.js +153 -0
- package/dist/src/canvas/screenshot.js +23 -0
- package/dist/src/canvas/session-record.js +77 -0
- package/dist/src/canvas/team.js +97 -0
- package/dist/src/canvas/tool-args.js +47 -0
- package/dist/src/canvas/tool-session.js +165 -0
- package/dist/src/canvas/tool-specs.js +54 -0
- package/dist/src/canvas/url.js +38 -0
- package/dist/src/cli/args.js +16 -0
- package/dist/src/cli/commands/auth.js +51 -0
- package/dist/src/cli/commands/canvas/common.js +31 -0
- package/dist/src/cli/commands/canvas/engine-choice.js +41 -0
- package/dist/src/cli/commands/canvas/index.js +5 -0
- package/dist/src/cli/commands/canvas/item.js +233 -0
- package/dist/src/cli/commands/canvas/list.js +168 -0
- package/dist/src/cli/commands/canvas/register.js +106 -0
- package/dist/src/cli/commands/canvas/render.js +229 -0
- package/dist/src/cli/commands/canvas/screenshot.js +52 -0
- package/dist/src/cli/commands/canvas/session.js +249 -0
- package/dist/src/cli/commands/canvas/teams.js +142 -0
- package/dist/src/cli/commands/config.js +74 -0
- package/dist/src/cli/commands/doctor.js +261 -0
- package/dist/src/cli/commands/engine.js +96 -0
- package/dist/src/cli/commands/env/create.js +160 -0
- package/dist/src/cli/commands/env/fields.js +26 -0
- package/dist/src/cli/commands/env/index.js +4 -0
- package/dist/src/cli/commands/env/list.js +44 -0
- package/dist/src/cli/commands/env/mutate.js +118 -0
- package/dist/src/cli/commands/login/flow-token.js +41 -0
- package/dist/src/cli/commands/login/index.js +12 -0
- package/dist/src/cli/commands/login/oauth.js +69 -0
- package/dist/src/cli/commands/login/save.js +26 -0
- package/dist/src/cli/commands/logout.js +92 -0
- package/dist/src/cli/commands/skill.js +185 -0
- package/dist/src/cli/commands/tool.js +279 -0
- package/dist/src/cli/commands/update.js +117 -0
- package/dist/src/cli/commands/wallet.js +69 -0
- package/dist/src/cli/commands/whoami.js +147 -0
- package/dist/src/cli/context.js +41 -0
- package/dist/src/cli/credential.js +93 -0
- package/dist/src/cli/elapsed.js +25 -0
- package/dist/src/cli/failure-screenshot.js +24 -0
- package/dist/src/cli/flags.js +1 -0
- package/dist/src/cli/hints.js +7 -0
- package/dist/src/cli/main.js +44 -0
- package/dist/src/cli/output-error.js +23 -0
- package/dist/src/cli/output.js +47 -0
- package/dist/src/cli/program.js +261 -0
- package/dist/src/cli/run.js +11 -0
- package/dist/src/cli/session.js +63 -0
- package/dist/src/config/base-url.js +26 -0
- package/dist/src/config/credential-vault.js +115 -0
- package/dist/src/config/dotenv.js +59 -0
- package/dist/src/config/envname.js +41 -0
- package/dist/src/config/paths.js +22 -0
- package/dist/src/config/preset-schema.js +67 -0
- package/dist/src/config/presets.js +132 -0
- package/dist/src/config/resolve.js +41 -0
- package/dist/src/config/secret-key.js +63 -0
- package/dist/src/config/store-schema.js +143 -0
- package/dist/src/config/store.js +273 -0
- package/dist/src/config/vault-crypto.js +44 -0
- package/dist/src/deps.js +191 -0
- package/dist/src/deps.types.js +1 -0
- package/dist/src/errors.js +24 -0
- package/dist/src/fs-retry.js +34 -0
- package/dist/src/i18n.js +1214 -0
- package/dist/src/loopback.js +134 -0
- package/dist/src/sdk/auth.js +20 -0
- package/dist/src/sdk/credential-expiry.js +67 -0
- package/dist/src/sdk/discovery.js +90 -0
- package/dist/src/sdk/http.js +98 -0
- package/dist/src/sdk/identity.js +17 -0
- package/dist/src/sdk/jwks.js +119 -0
- package/dist/src/sdk/oauth-protocol.js +226 -0
- package/dist/src/sdk/oauth.js +179 -0
- package/dist/src/sdk/provider/cn.js +34 -0
- package/dist/src/sdk/provider/index.js +16 -0
- package/dist/src/sdk/provider/oversea.js +41 -0
- package/dist/src/sdk/provider/types.js +1 -0
- package/dist/src/sdk/refresh.js +66 -0
- package/dist/src/semver.js +89 -0
- package/dist/src/skill/api.js +176 -0
- package/dist/src/text.js +3 -0
- package/dist/src/types.js +1 -0
- package/dist/src/update/check-cache.js +56 -0
- package/dist/src/update/install-layout.js +79 -0
- package/dist/src/update/notify.js +15 -0
- package/dist/src/update/npm-install.js +64 -0
- package/dist/src/update/passive.js +53 -0
- package/dist/src/update/registry.js +59 -0
- package/dist/src/wallet/api.js +107 -0
- package/package.json +67 -4
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { dirname, join } from 'node:path';
|
|
2
|
+
import { Browser } from '@puppeteer/browsers';
|
|
3
|
+
export const ENGINE_PATH_ENV = 'PUPPETEER_EXECUTABLE_PATH';
|
|
4
|
+
export const ENGINE_BROWSER = Browser.CHROMEHEADLESSSHELL;
|
|
5
|
+
export function engineCacheDir(deps) {
|
|
6
|
+
const override = deps.env?.['PUPPETEER_CACHE_DIR'];
|
|
7
|
+
let result;
|
|
8
|
+
if (override !== undefined && override !== '') {
|
|
9
|
+
result = override;
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
result = join(deps.homeDir(), '.cache', 'puppeteer');
|
|
13
|
+
}
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
function engineBinaryName(platform) {
|
|
17
|
+
return platform === 'win32' ? `${ENGINE_BROWSER}.exe` : ENGINE_BROWSER;
|
|
18
|
+
}
|
|
19
|
+
export function engineInstallDir(binaryPath) {
|
|
20
|
+
return dirname(dirname(binaryPath));
|
|
21
|
+
}
|
|
22
|
+
const VERSION_DIR = /^[a-z0-9_]+-(\d+\.\d+\.\d+\.\d+)$/;
|
|
23
|
+
async function exists(deps, path) {
|
|
24
|
+
let result;
|
|
25
|
+
try {
|
|
26
|
+
await deps.fs.stat(path);
|
|
27
|
+
result = true;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
result = false;
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
function compareVersion(a, b) {
|
|
35
|
+
const pa = a.split('.').map(Number);
|
|
36
|
+
const pb = b.split('.').map(Number);
|
|
37
|
+
let result = 0;
|
|
38
|
+
for (let i = 0; i < 4; i += 1) {
|
|
39
|
+
const d = pa[i] - pb[i];
|
|
40
|
+
result = result === 0 ? d : result;
|
|
41
|
+
}
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
export async function detectCanvasEngine(deps) {
|
|
45
|
+
const override = deps.env[ENGINE_PATH_ENV];
|
|
46
|
+
let result;
|
|
47
|
+
if (override !== undefined && override !== '') {
|
|
48
|
+
if (await exists(deps, override)) {
|
|
49
|
+
result = { found: true, version: null, path: override };
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
result = { found: false, searched: override };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
result = await searchCache(deps);
|
|
57
|
+
}
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
async function searchCache(deps) {
|
|
61
|
+
const root = join(engineCacheDir(deps), ENGINE_BROWSER);
|
|
62
|
+
let entries;
|
|
63
|
+
try {
|
|
64
|
+
entries = await deps.fs.readdir(root);
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
entries = [];
|
|
68
|
+
}
|
|
69
|
+
const versions = entries
|
|
70
|
+
.map((name) => ({ name, version: VERSION_DIR.exec(name)?.[1] }))
|
|
71
|
+
.filter((e) => e.version !== undefined)
|
|
72
|
+
.sort((a, b) => compareVersion(b.version, a.version));
|
|
73
|
+
const binary = engineBinaryName(deps.platform);
|
|
74
|
+
let result = { found: false, searched: root };
|
|
75
|
+
for (const { name, version } of versions) {
|
|
76
|
+
if (result.found)
|
|
77
|
+
break;
|
|
78
|
+
const dir = join(root, name);
|
|
79
|
+
let inner;
|
|
80
|
+
try {
|
|
81
|
+
inner = await deps.fs.readdir(dir);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
inner = [];
|
|
85
|
+
}
|
|
86
|
+
const binDir = inner.find((n) => n.startsWith(ENGINE_BROWSER));
|
|
87
|
+
if (binDir !== undefined) {
|
|
88
|
+
const bin = join(dir, binDir, binary);
|
|
89
|
+
if (await exists(deps, bin)) {
|
|
90
|
+
result = { found: true, version, path: bin };
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { CliError } from "../errors.js";
|
|
2
|
+
import { t } from "../i18n.js";
|
|
3
|
+
const CAPABILITIES = {
|
|
4
|
+
seavideo: new Set(['personal', 'team']),
|
|
5
|
+
seaflow: new Set(['personal']),
|
|
6
|
+
};
|
|
7
|
+
export function supportsSpace(entry, space) {
|
|
8
|
+
return CAPABILITIES[entry].has(space);
|
|
9
|
+
}
|
|
10
|
+
export function requireSpace(entry, space, envMap = process.env) {
|
|
11
|
+
if (supportsSpace(entry, space))
|
|
12
|
+
return;
|
|
13
|
+
throw new CliError('unsupported', t('canvas.spaceUnsupported', { entry, space }, envMap), {
|
|
14
|
+
detail: t('canvas.spaceUnsupportedDetail', { space }, envMap),
|
|
15
|
+
});
|
|
16
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { CliError } from "../errors.js";
|
|
3
|
+
import { t } from "../i18n.js";
|
|
4
|
+
const LOCK_ATTEMPTS = 3;
|
|
5
|
+
const ENOENT_ATTEMPTS = 2;
|
|
6
|
+
const heldLocks = new AsyncLocalStorage();
|
|
7
|
+
function errnoOf(err) {
|
|
8
|
+
const code = err?.code;
|
|
9
|
+
return typeof code === 'string' ? code : undefined;
|
|
10
|
+
}
|
|
11
|
+
export function parseLockHolder(text) {
|
|
12
|
+
let result = null;
|
|
13
|
+
try {
|
|
14
|
+
const raw = JSON.parse(text);
|
|
15
|
+
if (typeof raw === 'object' && raw !== null && !Array.isArray(raw)) {
|
|
16
|
+
const pid = raw['pid'];
|
|
17
|
+
result = typeof pid === 'number' && Number.isInteger(pid) && pid > 0 ? pid : null;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
result = null;
|
|
24
|
+
}
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
async function reclaimStale(file, expected, attempt, deps) {
|
|
28
|
+
const moved = `${file}.stale.${process.pid}.${attempt}`;
|
|
29
|
+
try {
|
|
30
|
+
await deps.fs.rename(file, moved);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
let content = null;
|
|
36
|
+
try {
|
|
37
|
+
content = await deps.fs.readFile(moved, 'utf8');
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
content = null;
|
|
41
|
+
}
|
|
42
|
+
if (content === expected) {
|
|
43
|
+
try {
|
|
44
|
+
await deps.fs.unlink(moved);
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
try {
|
|
51
|
+
await deps.fs.rename(moved, file);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
try {
|
|
55
|
+
await deps.fs.unlink(moved);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
throw busy(file, deps);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export class CanvasLockBusyError extends CliError {
|
|
64
|
+
constructor(message, options) {
|
|
65
|
+
super('config', message, options);
|
|
66
|
+
this.name = 'CanvasLockBusyError';
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export function isCanvasLockBusy(err) {
|
|
70
|
+
return err instanceof CanvasLockBusyError;
|
|
71
|
+
}
|
|
72
|
+
function busy(file, deps) {
|
|
73
|
+
return new CanvasLockBusyError(t('canvas.lockBusy', {}, deps.env), {
|
|
74
|
+
detail: t('canvas.lockBusyDetail', { path: file }, deps.env),
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
async function acquireLock(file, dir, deps) {
|
|
78
|
+
let attempt = 0;
|
|
79
|
+
let vanished = 0;
|
|
80
|
+
for (;;) {
|
|
81
|
+
try {
|
|
82
|
+
await deps.fs.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
83
|
+
await deps.fs.writeFile(file, `${JSON.stringify({ pid: process.pid })}\n`, {
|
|
84
|
+
mode: 0o600,
|
|
85
|
+
flag: 'wx',
|
|
86
|
+
});
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
catch (err) {
|
|
90
|
+
const code = errnoOf(err);
|
|
91
|
+
if (code !== 'EEXIST' && code !== 'ENOENT') {
|
|
92
|
+
throw new CliError('config', t('canvas.lockWriteFailed', {}, deps.env), {
|
|
93
|
+
detail: code ?? String(err),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
if (code === 'EEXIST') {
|
|
97
|
+
attempt += 1;
|
|
98
|
+
const stale = await staleContent(file, deps);
|
|
99
|
+
if (stale === null || attempt >= LOCK_ATTEMPTS) {
|
|
100
|
+
throw busy(file, deps);
|
|
101
|
+
}
|
|
102
|
+
await reclaimStale(file, stale, attempt, deps);
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
vanished += 1;
|
|
106
|
+
if (vanished >= ENOENT_ATTEMPTS) {
|
|
107
|
+
throw new CliError('config', t('canvas.lockWriteFailed', {}, deps.env), {
|
|
108
|
+
detail: 'ENOENT',
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async function staleContent(file, deps) {
|
|
116
|
+
let text = null;
|
|
117
|
+
try {
|
|
118
|
+
text = await deps.fs.readFile(file, 'utf8');
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
text = null;
|
|
122
|
+
}
|
|
123
|
+
let result = null;
|
|
124
|
+
if (text === null) {
|
|
125
|
+
result = null;
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
const holder = parseLockHolder(text);
|
|
129
|
+
if (holder === null || !deps.processAlive(holder)) {
|
|
130
|
+
result = text;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
result = null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
async function releaseLock(file, deps) {
|
|
139
|
+
try {
|
|
140
|
+
await deps.fs.unlink(file);
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
export async function withCanvasLock(scope, env, fn) {
|
|
146
|
+
const file = scope.paths.canvasLockFile(env);
|
|
147
|
+
const held = heldLocks.getStore();
|
|
148
|
+
let result;
|
|
149
|
+
if (held?.has(file) === true) {
|
|
150
|
+
result = await fn();
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
await acquireLock(file, scope.paths.envDir(env), scope.deps);
|
|
154
|
+
try {
|
|
155
|
+
const next = new Set(held ?? []);
|
|
156
|
+
next.add(file);
|
|
157
|
+
result = await heldLocks.run(next, fn);
|
|
158
|
+
}
|
|
159
|
+
finally {
|
|
160
|
+
await releaseLock(file, scope.deps);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return result;
|
|
164
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { CliError } from "../errors.js";
|
|
2
|
+
import { t } from "../i18n.js";
|
|
3
|
+
export const AGENT_LAUNCH_TIMEOUT_MS = 20_000;
|
|
4
|
+
const CANVAS_ID_PATTERN = /^[A-Za-z0-9_-]{1,128}$/;
|
|
5
|
+
export async function requestCanvasLaunch(deps, config, canvasId, token) {
|
|
6
|
+
const envMap = deps.env;
|
|
7
|
+
const craft = new URL(config.seavideoBaseUrl);
|
|
8
|
+
const flow = new URL(config.flowWebBaseUrl);
|
|
9
|
+
if (craft.protocol !== 'https:' || flow.protocol !== 'https:') {
|
|
10
|
+
throw new CliError('config', t('launch.insecureOrigin', {}, envMap), {
|
|
11
|
+
detail: t('launch.insecureOriginDetail', {}, envMap),
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
assertCanvasId(canvasId, envMap);
|
|
15
|
+
const endpoint = new URL('/api/craft/v1/flow/agent-launch', craft.origin).href;
|
|
16
|
+
let response;
|
|
17
|
+
try {
|
|
18
|
+
response = await deps.fetch(endpoint, {
|
|
19
|
+
method: 'POST',
|
|
20
|
+
headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' },
|
|
21
|
+
body: JSON.stringify({ canvas_id: canvasId }),
|
|
22
|
+
redirect: 'error',
|
|
23
|
+
signal: AbortSignal.timeout(AGENT_LAUNCH_TIMEOUT_MS),
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
catch (err) {
|
|
27
|
+
throw new CliError('network', t('launch.requestFailed', {}, envMap), {
|
|
28
|
+
detail: err instanceof Error ? err.message : String(err),
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
if (!response.ok) {
|
|
32
|
+
throw new CliError('server', t('launch.httpError', { status: response.status }, envMap), {
|
|
33
|
+
detail: t('launch.httpErrorDetail', {}, envMap),
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
let payload;
|
|
37
|
+
try {
|
|
38
|
+
payload = await response.json();
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
throw new CliError('server', t('launch.badJson', {}, envMap), {
|
|
42
|
+
detail: err instanceof Error ? err.message : String(err),
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return assertLaunchPayload(payload, flow, canvasId, token, envMap);
|
|
46
|
+
}
|
|
47
|
+
function assertCanvasId(canvasId, envMap) {
|
|
48
|
+
if (!CANVAS_ID_PATTERN.test(canvasId)) {
|
|
49
|
+
throw new CliError('usage', t('launch.badCanvasId', { id: canvasId }, envMap), {
|
|
50
|
+
detail: t('launch.badCanvasIdDetail', {}, envMap),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
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), {
|
|
57
|
+
detail: t('launch.mismatchDetail', {}, envMap),
|
|
58
|
+
});
|
|
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();
|
|
84
|
+
}
|
|
85
|
+
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
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { appendEngineLog } from "./engine-log.js";
|
|
2
|
+
import { clearSessionRecord, readSessionRecord } from "./session-record.js";
|
|
3
|
+
import { clearToolSpecs } from "./tool-specs.js";
|
|
4
|
+
import { withCanvasLock } from "./env-lock.js";
|
|
5
|
+
export function daemonDepsOf(scope) {
|
|
6
|
+
const { deps } = scope;
|
|
7
|
+
return {
|
|
8
|
+
fs: deps.fs,
|
|
9
|
+
spawnDetached: deps.spawnDetached,
|
|
10
|
+
terminate: deps.terminate,
|
|
11
|
+
processAlive: deps.processAlive,
|
|
12
|
+
fetch: deps.fetch,
|
|
13
|
+
sleep: deps.sleep,
|
|
14
|
+
clock: deps.clock,
|
|
15
|
+
nodePath: deps.nodePath,
|
|
16
|
+
env: deps.env,
|
|
17
|
+
platform: deps.platform,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export async function closeCanvasSession(scope, env) {
|
|
21
|
+
return withCanvasLock(scope, env, () => closeInLock(scope, env));
|
|
22
|
+
}
|
|
23
|
+
async function closeInLock(scope, env) {
|
|
24
|
+
const { deps } = scope;
|
|
25
|
+
const sessionFile = scope.paths.canvasSessionFile(env);
|
|
26
|
+
const record = await readSessionRecord(sessionFile, deps);
|
|
27
|
+
let result;
|
|
28
|
+
if (record === null) {
|
|
29
|
+
result = null;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
await clearSessionRecord(sessionFile, deps);
|
|
33
|
+
if (deps.processAlive(record.pid)) {
|
|
34
|
+
try {
|
|
35
|
+
deps.terminate(record.pid);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
}
|
|
42
|
+
await appendEngineLog(deps, scope.paths.engineLogFile(env), {
|
|
43
|
+
at: deps.clock().toISOString(),
|
|
44
|
+
event: 'exit',
|
|
45
|
+
env,
|
|
46
|
+
canvasId: record.id,
|
|
47
|
+
pid: record.pid,
|
|
48
|
+
reason: 'closed',
|
|
49
|
+
});
|
|
50
|
+
result = record.id;
|
|
51
|
+
}
|
|
52
|
+
await removeProfileDir(scope, env);
|
|
53
|
+
await clearToolSpecs(scope.deps.fs, scope.paths.toolSpecsDir(env));
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
async function removeProfileDir(scope, env) {
|
|
57
|
+
try {
|
|
58
|
+
await scope.deps.fs.rm(scope.paths.profileDir(env), {
|
|
59
|
+
recursive: true,
|
|
60
|
+
force: true,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { errorMessage } from "../errors.js";
|
|
2
|
+
import { createTeamApi } from "./team.js";
|
|
3
|
+
export function emptyNames() {
|
|
4
|
+
return { teams: new Map(), projects: new Map(), folders: new Map() };
|
|
5
|
+
}
|
|
6
|
+
export async function collectNames(deps, config, api, items, account, seedProjects) {
|
|
7
|
+
const names = emptyNames();
|
|
8
|
+
if (seedProjects !== undefined) {
|
|
9
|
+
for (const [id, name] of seedProjects)
|
|
10
|
+
names.projects.set(id, name);
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
}
|
|
14
|
+
const teamIds = new Set();
|
|
15
|
+
const projectIds = new Set();
|
|
16
|
+
let needsFolders = false;
|
|
17
|
+
for (const item of items) {
|
|
18
|
+
if (item.teamId !== null)
|
|
19
|
+
teamIds.add(item.teamId);
|
|
20
|
+
else {
|
|
21
|
+
}
|
|
22
|
+
if (item.projectId !== null && !names.projects.has(item.projectId)) {
|
|
23
|
+
projectIds.add(item.projectId);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
}
|
|
27
|
+
if (item.folderId !== null)
|
|
28
|
+
needsFolders = true;
|
|
29
|
+
else {
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
let result;
|
|
33
|
+
try {
|
|
34
|
+
if (account !== null && teamIds.size > 0) {
|
|
35
|
+
const teamApi = createTeamApi(deps, config, account.credential, account.flowToken);
|
|
36
|
+
for (const team of await teamApi.teams()) {
|
|
37
|
+
if (teamIds.has(team.id))
|
|
38
|
+
names.teams.set(team.id, team.name);
|
|
39
|
+
else {
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
}
|
|
45
|
+
for (const id of projectIds) {
|
|
46
|
+
const project = await api.teamProject(id);
|
|
47
|
+
names.projects.set(project.id, project.name);
|
|
48
|
+
}
|
|
49
|
+
if (needsFolders) {
|
|
50
|
+
for (const folder of await api.folders())
|
|
51
|
+
names.folders.set(folder.id, folder.name);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
}
|
|
55
|
+
result = { names, error: null };
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
result = { names, error: errorMessage(err) };
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { CliError } from "../errors.js";
|
|
2
|
+
import { t } from "../i18n.js";
|
|
3
|
+
export const DELETE_LIMIT = 100;
|
|
4
|
+
export const PAGE_SIZE_LIMIT = 100;
|
|
5
|
+
export function normalizeDeleteIds(raw, envMap) {
|
|
6
|
+
const ids = [...new Set(raw.map((id) => assertCanvasId(id, envMap)))];
|
|
7
|
+
if (ids.length === 0 || ids[0] === '') {
|
|
8
|
+
throw new CliError('usage', t('canvas.emptyIdList', {}, envMap));
|
|
9
|
+
}
|
|
10
|
+
if (ids.length > DELETE_LIMIT) {
|
|
11
|
+
throw new CliError('usage', t('canvas.tooManyIds', { count: ids.length, limit: DELETE_LIMIT }, envMap), { detail: t('canvas.tooManyIdsDetail', {}, envMap) });
|
|
12
|
+
}
|
|
13
|
+
return ids;
|
|
14
|
+
}
|
|
15
|
+
export const NAME_LIMIT = 100;
|
|
16
|
+
export function validateCanvasName(name, envMap) {
|
|
17
|
+
const trimmed = name.trim();
|
|
18
|
+
if (trimmed === '') {
|
|
19
|
+
throw new CliError('usage', t('canvas.nameEmpty', {}, envMap), {
|
|
20
|
+
detail: t('canvas.nameEmptyDetail', {}, envMap),
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
const len = [...trimmed].length;
|
|
24
|
+
if (len > NAME_LIMIT) {
|
|
25
|
+
throw new CliError('usage', t('canvas.nameTooLong', { count: len, limit: NAME_LIMIT }, envMap));
|
|
26
|
+
}
|
|
27
|
+
return trimmed;
|
|
28
|
+
}
|
|
29
|
+
const CANVAS_ID_PATTERN = /^[A-Za-z0-9_-]{1,128}$/u;
|
|
30
|
+
export function assertCanvasId(id, envMap) {
|
|
31
|
+
const trimmed = id.trim();
|
|
32
|
+
if (!CANVAS_ID_PATTERN.test(trimmed)) {
|
|
33
|
+
throw new CliError('usage', t('canvas.badId', { id: trimmed }, envMap), {
|
|
34
|
+
detail: t('canvas.badIdDetail', {}, envMap),
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return trimmed;
|
|
38
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { CliError, errorMessage } from "../errors.js";
|
|
2
|
+
import { t } from "../i18n.js";
|
|
3
|
+
import { maskUrlCredential } from "./url.js";
|
|
4
|
+
import { normalizeDefinition } from "./tool-session.js";
|
|
5
|
+
import { DEFAULT_OPEN_TIMEOUT_MS, READ_TOOLS_EXPR, WAIT_TOOLS_EXPR } from "./browser.js";
|
|
6
|
+
export const puppeteerConnect = async ({ port }) => {
|
|
7
|
+
const puppeteer = await import('puppeteer-core');
|
|
8
|
+
return puppeteer.default.connect({
|
|
9
|
+
browserURL: `http://127.0.0.1:${port}`,
|
|
10
|
+
defaultViewport: null,
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
export function connectPuppeteerBrowser(options) {
|
|
14
|
+
const envMap = options.envMap;
|
|
15
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_OPEN_TIMEOUT_MS;
|
|
16
|
+
let browser = null;
|
|
17
|
+
let page = null;
|
|
18
|
+
async function pagesOf(conn) {
|
|
19
|
+
const withPages = conn;
|
|
20
|
+
return withPages.pages === undefined ? [] : withPages.pages();
|
|
21
|
+
}
|
|
22
|
+
function isBlank(candidate) {
|
|
23
|
+
const url = candidate.url();
|
|
24
|
+
return url === '' || url === 'about:blank';
|
|
25
|
+
}
|
|
26
|
+
async function canvasPage(conn) {
|
|
27
|
+
return (await pagesOf(conn)).find((candidate) => !isBlank(candidate)) ?? null;
|
|
28
|
+
}
|
|
29
|
+
async function pageToNavigate(conn) {
|
|
30
|
+
const pages = await pagesOf(conn);
|
|
31
|
+
const existing = pages.find((candidate) => !isBlank(candidate)) ?? pages.find(isBlank);
|
|
32
|
+
return existing ?? conn.newPage();
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
async open(url, cookie) {
|
|
36
|
+
browser ??= await options.connect({ port: options.port });
|
|
37
|
+
page = await pageToNavigate(browser);
|
|
38
|
+
return openOnPage(page, url, timeoutMs, envMap, cookie);
|
|
39
|
+
},
|
|
40
|
+
async evaluate(expression) {
|
|
41
|
+
browser ??= await options.connect({ port: options.port });
|
|
42
|
+
page ??= await canvasPage(browser);
|
|
43
|
+
if (page === null) {
|
|
44
|
+
throw new CliError('config', t('canvas.notOpened', {}, envMap));
|
|
45
|
+
}
|
|
46
|
+
return page.evaluate(expression);
|
|
47
|
+
},
|
|
48
|
+
async screenshot() {
|
|
49
|
+
browser ??= await options.connect({ port: options.port });
|
|
50
|
+
page ??= await canvasPage(browser);
|
|
51
|
+
return page === null ? null : Buffer.from(await page.screenshot());
|
|
52
|
+
},
|
|
53
|
+
async close() {
|
|
54
|
+
if (browser !== null) {
|
|
55
|
+
const conn = browser;
|
|
56
|
+
if (conn.disconnect !== undefined) {
|
|
57
|
+
await conn.disconnect();
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
}
|
|
64
|
+
browser = null;
|
|
65
|
+
page = null;
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function openFailureDetail(err, envMap) {
|
|
70
|
+
const message = maskUrlCredential(errorMessage(err));
|
|
71
|
+
const isProxyFailure = message.includes('ERR_PROXY_CONNECTION_FAILED');
|
|
72
|
+
return {
|
|
73
|
+
detail: isProxyFailure ? `${message}\n${t('canvas.openFailedProxy', {}, envMap)}` : message,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
async function writeLaunchCookie(page, cookie, envMap) {
|
|
77
|
+
const session = await page.createCDPSession();
|
|
78
|
+
try {
|
|
79
|
+
const result = await session.send('Network.setCookie', {
|
|
80
|
+
name: cookie.name,
|
|
81
|
+
value: cookie.value,
|
|
82
|
+
url: cookie.url,
|
|
83
|
+
path: cookie.path,
|
|
84
|
+
secure: cookie.secure,
|
|
85
|
+
httpOnly: cookie.httpOnly,
|
|
86
|
+
sameSite: cookie.sameSite,
|
|
87
|
+
});
|
|
88
|
+
if (!result.success) {
|
|
89
|
+
throw new CliError('server', t('launch.cookieRejected', {}, envMap), {
|
|
90
|
+
detail: t('launch.cookieRejectedDetail', { url: cookie.url }, envMap),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
await session.detach();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async function openOnPage(page, url, timeoutMs, envMap, cookie) {
|
|
99
|
+
if (cookie !== undefined) {
|
|
100
|
+
await writeLaunchCookie(page, cookie, envMap);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
}
|
|
104
|
+
if (page.url() !== url) {
|
|
105
|
+
let status;
|
|
106
|
+
try {
|
|
107
|
+
const resp = await page.goto(url, { waitUntil: 'domcontentloaded', timeout: timeoutMs });
|
|
108
|
+
status = resp?.status();
|
|
109
|
+
}
|
|
110
|
+
catch (err) {
|
|
111
|
+
if (err instanceof Error && err.name === 'TimeoutError') {
|
|
112
|
+
throw new CliError('timeout', t('canvas.openTimeout', { ms: timeoutMs }, envMap));
|
|
113
|
+
}
|
|
114
|
+
throw new CliError('network', t('canvas.openFailed', {}, envMap), openFailureDetail(err, envMap));
|
|
115
|
+
}
|
|
116
|
+
if (status !== undefined && status >= 400) {
|
|
117
|
+
throw new CliError('server', t('canvas.pageStatus', { status }, envMap), {
|
|
118
|
+
detail: status === 403 ? t('canvas.page403', {}, envMap) : undefined,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
await page.waitForFunction(WAIT_TOOLS_EXPR, { timeout: timeoutMs });
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
const title = await page.title().catch(() => '');
|
|
129
|
+
let actual = url;
|
|
130
|
+
try {
|
|
131
|
+
actual = page.url();
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
}
|
|
135
|
+
throw new CliError('server', t('canvas.noTools', {}, envMap), {
|
|
136
|
+
detail: t('canvas.noToolsDetail', { title, url: actual }, envMap),
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
const raw = await page.evaluate(READ_TOOLS_EXPR);
|
|
140
|
+
const tools = raw.map((item) => normalizeDefinition(item, envMap));
|
|
141
|
+
const title = await page.title().catch(() => '');
|
|
142
|
+
return { url: actualUrlOf(page, url), title, tools };
|
|
143
|
+
}
|
|
144
|
+
function actualUrlOf(page, requested) {
|
|
145
|
+
let result;
|
|
146
|
+
try {
|
|
147
|
+
result = page.url();
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
result = requested;
|
|
151
|
+
}
|
|
152
|
+
return result;
|
|
153
|
+
}
|