@saturn-universe/cli 0.0.0-stage → 0.1.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 +661 -0
- package/dist/attach.d.ts +9 -0
- package/dist/attach.js +80 -0
- package/dist/auth-config.d.ts +31 -0
- package/dist/auth-config.js +285 -0
- package/dist/choice-menu.d.ts +13 -0
- package/dist/choice-menu.js +238 -0
- package/dist/cli-diagnostics.d.ts +23 -0
- package/dist/cli-diagnostics.js +42 -0
- package/dist/cli-help.d.ts +6 -0
- package/dist/cli-help.js +163 -0
- package/dist/client.d.ts +13 -0
- package/dist/client.js +90 -0
- package/dist/commands.d.ts +64 -0
- package/dist/commands.js +299 -0
- package/dist/compaction-command.d.ts +16 -0
- package/dist/compaction-command.js +16 -0
- package/dist/composer.d.ts +88 -0
- package/dist/composer.js +124 -0
- package/dist/config-cli.d.ts +1 -0
- package/dist/config-cli.js +47 -0
- package/dist/config-file.d.ts +1 -0
- package/dist/config-file.js +43 -0
- package/dist/config-lock.d.ts +5 -0
- package/dist/config-lock.js +45 -0
- package/dist/duration.d.ts +2 -0
- package/dist/duration.js +11 -0
- package/dist/export.d.ts +19 -0
- package/dist/export.js +66 -0
- package/dist/host-command.d.ts +40 -0
- package/dist/host-command.js +48 -0
- package/dist/host-launcher.d.ts +22 -0
- package/dist/host-launcher.js +300 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +400 -0
- package/dist/markdown.d.ts +1 -0
- package/dist/markdown.js +118 -0
- package/dist/mcp-cli.d.ts +44 -0
- package/dist/mcp-cli.js +647 -0
- package/dist/menu.d.ts +7 -0
- package/dist/menu.js +91 -0
- package/dist/project-mcp-config.d.ts +9 -0
- package/dist/project-mcp-config.js +98 -0
- package/dist/provider-discovery.d.ts +26 -0
- package/dist/provider-discovery.js +138 -0
- package/dist/provider-pricing.d.ts +3 -0
- package/dist/provider-pricing.js +130 -0
- package/dist/provider.d.ts +16 -0
- package/dist/provider.js +370 -0
- package/dist/reconcile-command.d.ts +36 -0
- package/dist/reconcile-command.js +64 -0
- package/dist/renderer.d.ts +204 -0
- package/dist/renderer.js +1134 -0
- package/dist/repl-host.d.ts +22 -0
- package/dist/repl-host.js +51 -0
- package/dist/repl-run-observer.d.ts +27 -0
- package/dist/repl-run-observer.js +86 -0
- package/dist/repl.d.ts +12 -0
- package/dist/repl.js +1834 -0
- package/dist/results.d.ts +23 -0
- package/dist/results.js +1 -0
- package/dist/sandbox-cli.d.ts +39 -0
- package/dist/sandbox-cli.js +324 -0
- package/dist/session-history.d.ts +17 -0
- package/dist/session-history.js +76 -0
- package/dist/settings-menu.d.ts +2 -0
- package/dist/settings-menu.js +111 -0
- package/dist/skill-cli.d.ts +25 -0
- package/dist/skill-cli.js +355 -0
- package/dist/slash-commands.d.ts +66 -0
- package/dist/slash-commands.js +1449 -0
- package/dist/terminal-input.d.ts +24 -0
- package/dist/terminal-input.js +111 -0
- package/dist/terminal-selection.d.ts +21 -0
- package/dist/terminal-selection.js +72 -0
- package/dist/terminal-text.d.ts +38 -0
- package/dist/terminal-text.js +238 -0
- package/dist/tui.d.ts +170 -0
- package/dist/tui.js +2559 -0
- package/dist/usage.d.ts +13 -0
- package/dist/usage.js +46 -0
- package/dist/user-config.d.ts +48 -0
- package/dist/user-config.js +188 -0
- package/package.json +18 -3
- package/README.md +0 -3
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
export class CliError extends Error {
|
|
3
|
+
code;
|
|
4
|
+
diagnosticId;
|
|
5
|
+
retryable;
|
|
6
|
+
constructor(code, diagnosticId = randomUUID(), retryable = false) {
|
|
7
|
+
super(code);
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.diagnosticId = diagnosticId;
|
|
10
|
+
this.retryable = retryable;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export class UsageError extends CliError {
|
|
14
|
+
detail;
|
|
15
|
+
constructor(detail) {
|
|
16
|
+
super('USAGE');
|
|
17
|
+
this.detail = detail;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export class AuthError extends CliError {
|
|
21
|
+
detail;
|
|
22
|
+
constructor(detail, code = 'INVALID_CONFIG') {
|
|
23
|
+
super(code);
|
|
24
|
+
this.detail = detail;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export function safeCode(value) {
|
|
28
|
+
return /^[a-zA-Z][a-zA-Z0-9_]{0,79}$/.test(value) ? value : 'RUN_FAILED';
|
|
29
|
+
}
|
|
30
|
+
export function formatDiagnosticReference(error) {
|
|
31
|
+
return `${safeCode(error.code)} diagnosticId=${error.diagnosticId}`;
|
|
32
|
+
}
|
|
33
|
+
export function isHostConnectionError(code) {
|
|
34
|
+
return ['HOST_UNAVAILABLE', 'HOST_DISCONNECTED', 'HOST_TIMEOUT', 'HOST_CLOSING'].includes(code);
|
|
35
|
+
}
|
|
36
|
+
export function exitCode(code) {
|
|
37
|
+
if (['INVALID_COMMAND', 'INVALID_CURSOR', 'INVALID_REPLY', 'INVALID_TEAM_CONFIG', 'USAGE'].includes(code))
|
|
38
|
+
return 2;
|
|
39
|
+
if (code.startsWith('HOST_') || code === 'WORKSPACE_UNAVAILABLE' || code === 'WORKSPACE_OWNED')
|
|
40
|
+
return 3;
|
|
41
|
+
return 1;
|
|
42
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const help: string;
|
|
2
|
+
export declare function hasHelpTopic(topic: string | undefined): topic is string;
|
|
3
|
+
export declare function helpTarget(args: readonly string[]): string[];
|
|
4
|
+
export declare function topicHelp(topic?: string, action?: string): string;
|
|
5
|
+
export declare const advancedHelp: string;
|
|
6
|
+
export declare function requestedHelp(args: readonly string[]): string | undefined;
|
package/dist/cli-help.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { CliError } from './cli-diagnostics.js';
|
|
2
|
+
import { compactionOptionUsage } from './compaction-command.js';
|
|
3
|
+
import { reconciliationUsage } from './reconcile-command.js';
|
|
4
|
+
const topics = {
|
|
5
|
+
provider: {
|
|
6
|
+
summary: '配置 AI 服务和默认模型',
|
|
7
|
+
actions: {
|
|
8
|
+
add: [
|
|
9
|
+
'[<provider>] [--base-url URL] [--protocol openai-responses|openai-chat|anthropic|gemini] [--models ID[,ID...]] [--default ID] [--api-key-stdin | --credential-ref env:NAME] [--no-probe] [--json]',
|
|
10
|
+
'连接服务商并选择默认模型。交互模式下可直接运行。',
|
|
11
|
+
'saturn provider add',
|
|
12
|
+
],
|
|
13
|
+
list: ['[--json]', '列出已配置的服务和模型。'],
|
|
14
|
+
use: ['[<provider/model>]', '设置默认模型。'],
|
|
15
|
+
test: ['[<provider/model>]', '发送一条短消息测试模型。'],
|
|
16
|
+
remove: ['<provider> [--keep-config]', '移除服务或只移除密钥。'],
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
config: {
|
|
20
|
+
summary: '查看和检查配置',
|
|
21
|
+
actions: {
|
|
22
|
+
show: ['[--json]', '查看生效的配置。'],
|
|
23
|
+
check: ['[--json]', '检查当前工作区配置。'],
|
|
24
|
+
init: ['[--force] [--json]', '创建用户配置。'],
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
mcp: {
|
|
28
|
+
summary: '连接和管理外部工具',
|
|
29
|
+
actions: {
|
|
30
|
+
list: ['[--json]', '查看服务和连接状态。'],
|
|
31
|
+
get: ['<name> [--json]', '查看服务详情。'],
|
|
32
|
+
auth: ['<name> [--header NAME] [--json]', '通过隐藏输入更新 HTTP MCP 凭据并重新连接。'],
|
|
33
|
+
add: [
|
|
34
|
+
'[--scope user|project] [--transport stdio|http] [--env KEY=VAL ...] [--header "Key: Value" ...] <name> [--] <url-or-command> [args...]',
|
|
35
|
+
'添加 HTTP 服务或本地命令。',
|
|
36
|
+
'saturn mcp add example https://example.com/mcp',
|
|
37
|
+
],
|
|
38
|
+
'add-json': [
|
|
39
|
+
"<name> '<json>' [--scope user|project] [--json]",
|
|
40
|
+
'从单条配置或 mcpServers 对象导入指定名称的服务。',
|
|
41
|
+
],
|
|
42
|
+
remove: ['<name> [--scope user|project]', '移除服务。'],
|
|
43
|
+
trust: ['', '信任当前工作区的项目级 MCP 配置。'],
|
|
44
|
+
untrust: ['', '撤销对当前工作区 MCP 配置的信任。'],
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
skill: {
|
|
48
|
+
summary: '查看和管理技能',
|
|
49
|
+
actions: {
|
|
50
|
+
list: ['[--json]', '列出已安装的技能。'],
|
|
51
|
+
add: [
|
|
52
|
+
'<local-path | git-url | github:owner/repo[/subpath]> [--scope user|project] [--name <alias>] [--force] [--json]',
|
|
53
|
+
'从目录或仓库安装技能。',
|
|
54
|
+
],
|
|
55
|
+
remove: ['<name> [--scope user|project] [--json]', '移除技能。'],
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
sandbox: {
|
|
59
|
+
summary: '查看和清理隔离环境',
|
|
60
|
+
actions: {
|
|
61
|
+
list: ['[--session <id>] [--json]', '查看隔离环境与磁盘占用。'],
|
|
62
|
+
clean: ['[--all | --older-than <hours>] [--session <id>] [--json]', '清理隔离环境。'],
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
session: {
|
|
66
|
+
summary: '查看或新建对话',
|
|
67
|
+
actions: {
|
|
68
|
+
list: ['[--json]', '查看最近的对话。'],
|
|
69
|
+
new: ['[--title <title>] [--json]', '新建对话。', 'saturn session new --title "New session"'],
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
run: {
|
|
73
|
+
summary: '查看和管理任务',
|
|
74
|
+
actions: {
|
|
75
|
+
list: ['[--json]', '查看最近 50 个对话各自的最后一项任务。'],
|
|
76
|
+
show: ['<id> [--json]', '查看任务详情。'],
|
|
77
|
+
attach: ['<id> [--json]', '跟踪任务进度。'],
|
|
78
|
+
wait: ['<id> [--timeout-ms <ms>] [--json]', '等待任务结束。'],
|
|
79
|
+
cancel: ['<id> [--json]', '取消任务。'],
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
export const help = [
|
|
84
|
+
'Saturn · 本地 AI 工作台',
|
|
85
|
+
'',
|
|
86
|
+
' saturn 打开工作台',
|
|
87
|
+
' saturn provider add 连接 AI 服务',
|
|
88
|
+
' saturn --session ID 打开已有对话',
|
|
89
|
+
'',
|
|
90
|
+
...Object.entries(topics).map(([name, { summary }]) => ' saturn ' + name.padEnd(16) + summary),
|
|
91
|
+
'',
|
|
92
|
+
'运行 saturn help mcp 或 saturn mcp --help 查看命令用法。',
|
|
93
|
+
'脚本参数和退出码见 saturn --advanced-help。',
|
|
94
|
+
].join('\n');
|
|
95
|
+
export function hasHelpTopic(topic) {
|
|
96
|
+
return topic !== undefined && Object.hasOwn(topics, topic);
|
|
97
|
+
}
|
|
98
|
+
export function helpTarget(args) {
|
|
99
|
+
const [topic, action] = args;
|
|
100
|
+
if (!hasHelpTopic(topic))
|
|
101
|
+
return [];
|
|
102
|
+
return action && Object.hasOwn(topics[topic].actions, action) ? [topic, action] : [topic];
|
|
103
|
+
}
|
|
104
|
+
export function topicHelp(topic, action) {
|
|
105
|
+
if (topic === undefined)
|
|
106
|
+
return help;
|
|
107
|
+
if (!hasHelpTopic(topic))
|
|
108
|
+
throw new CliError('USAGE');
|
|
109
|
+
const { summary, actions } = topics[topic];
|
|
110
|
+
const command = `saturn ${topic}`;
|
|
111
|
+
if (action !== undefined) {
|
|
112
|
+
if (!Object.hasOwn(actions, action))
|
|
113
|
+
throw new CliError('USAGE');
|
|
114
|
+
const [options, description, example] = actions[action];
|
|
115
|
+
return [
|
|
116
|
+
description,
|
|
117
|
+
'',
|
|
118
|
+
` ${command} ${action} ${options}`.trimEnd(),
|
|
119
|
+
...(example ? ['', `示例 ${example}`] : []),
|
|
120
|
+
].join('\n');
|
|
121
|
+
}
|
|
122
|
+
const width = Math.max(...Object.keys(actions).map((name) => name.length)) + 3;
|
|
123
|
+
return [
|
|
124
|
+
summary,
|
|
125
|
+
'',
|
|
126
|
+
...Object.entries(actions).map(([name, [, description]]) => ` ${command} ${name.padEnd(width)}${description}`),
|
|
127
|
+
'',
|
|
128
|
+
`运行 ${command} <子命令> --help 查看参数。`,
|
|
129
|
+
].join('\n');
|
|
130
|
+
}
|
|
131
|
+
export const advancedHelp = [
|
|
132
|
+
'添加服务:saturn provider add 服务名 --base-url URL --models 模型 --api-key-stdin',
|
|
133
|
+
'也可用 --credential-ref env:变量名 引用已有密钥。--no-probe 跳过模型列表读取。',
|
|
134
|
+
'工作区选项:-C 目录。查询结果可使用 --json。',
|
|
135
|
+
'归档发行包:saturn backup --help / saturn restore --help。',
|
|
136
|
+
'运行观察:progress、attach、wait。单次命令可指定 --start-host。',
|
|
137
|
+
'幂等重试:--command-id ID。进度分页:--after CURSOR。',
|
|
138
|
+
'退出码:0 成功,1 请求被拒绝,2 参数错误,3 连接失败,4 任务失败,5 已取消,130 已退出观察。',
|
|
139
|
+
reconciliationUsage('reconcile RUN'),
|
|
140
|
+
'compact RUN ' + compactionOptionUsage,
|
|
141
|
+
].join('\n');
|
|
142
|
+
export function requestedHelp(args) {
|
|
143
|
+
const end = args.indexOf('--');
|
|
144
|
+
const options = args.slice(0, end < 0 ? args.length : end).filter((arg) => arg !== '--json');
|
|
145
|
+
const [head, second, third, ...extra] = options;
|
|
146
|
+
const isHelpFlag = (arg) => arg === '--help' || arg === '-h';
|
|
147
|
+
if (head === '--advanced-help' && options.length === 1)
|
|
148
|
+
return advancedHelp;
|
|
149
|
+
if (head === 'help') {
|
|
150
|
+
if (extra.length)
|
|
151
|
+
throw new CliError('USAGE');
|
|
152
|
+
return topicHelp(isHelpFlag(second) ? undefined : second, third);
|
|
153
|
+
}
|
|
154
|
+
if (isHelpFlag(head))
|
|
155
|
+
return help;
|
|
156
|
+
if (!hasHelpTopic(head))
|
|
157
|
+
return undefined;
|
|
158
|
+
if (isHelpFlag(second) || second === 'help')
|
|
159
|
+
return topicHelp(head, second === 'help' ? third : undefined);
|
|
160
|
+
if (isHelpFlag(third))
|
|
161
|
+
return topicHelp(head, second);
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ProtocolReply } from '@saturn-universe/protocol';
|
|
2
|
+
import { CliError } from './cli-diagnostics.js';
|
|
3
|
+
export declare const MAX_HOST_REQUEST_TIMEOUT_MS = 30000;
|
|
4
|
+
type HostReply = Extract<ProtocolReply, {
|
|
5
|
+
ok: true;
|
|
6
|
+
}> | (Omit<Extract<ProtocolReply, {
|
|
7
|
+
ok: false;
|
|
8
|
+
}>, 'error'> & {
|
|
9
|
+
readonly error: CliError;
|
|
10
|
+
});
|
|
11
|
+
export declare function request(root: string, command: unknown, commandId: string, timeoutMs?: number, signal?: AbortSignal): Promise<HostReply>;
|
|
12
|
+
export declare function workspaceRoot(value: string): Promise<string>;
|
|
13
|
+
export {};
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { connect } from 'node:net';
|
|
2
|
+
import { realpath } from 'node:fs/promises';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { decodeCommand, decodeReply, encodeIpcFrame, IpcFrameReader, maxReplyBodyBytes, resolveHostEndpoint, validHostCommandResult, } from '@saturn-universe/protocol';
|
|
6
|
+
import { CliError, isHostConnectionError, safeCode } from './cli-diagnostics.js';
|
|
7
|
+
export const MAX_HOST_REQUEST_TIMEOUT_MS = 30_000;
|
|
8
|
+
export async function request(root, command, commandId, timeoutMs = MAX_HOST_REQUEST_TIMEOUT_MS, signal) {
|
|
9
|
+
signal?.throwIfAborted();
|
|
10
|
+
let decoded;
|
|
11
|
+
try {
|
|
12
|
+
decoded = decodeCommand(JSON.stringify({ protocolVersion: 1, requestId: randomUUID(), commandId, command }));
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
throw new CliError('INVALID_COMMAND');
|
|
16
|
+
}
|
|
17
|
+
const timeout = Math.min(timeoutMs, MAX_HOST_REQUEST_TIMEOUT_MS);
|
|
18
|
+
if (!Number.isFinite(timeout) || timeout < 0)
|
|
19
|
+
throw new RangeError('Invalid Host request timeout');
|
|
20
|
+
const deadline = timeout === 0 ? Infinity : performance.now() + timeout;
|
|
21
|
+
const { endpoint } = resolveHostEndpoint(root);
|
|
22
|
+
const frame = await new Promise((resolve, reject) => {
|
|
23
|
+
const socket = connect({ path: endpoint, ...(signal ? { signal } : {}) });
|
|
24
|
+
const reader = new IpcFrameReader(maxReplyBodyBytes);
|
|
25
|
+
let settled = false;
|
|
26
|
+
const timer = timeout === 0 ? undefined : setTimeout(() => fail('HOST_TIMEOUT'), timeout).unref();
|
|
27
|
+
function settle() {
|
|
28
|
+
if (settled)
|
|
29
|
+
return false;
|
|
30
|
+
settled = true;
|
|
31
|
+
clearTimeout(timer);
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
function fail(code) {
|
|
35
|
+
if (!settle())
|
|
36
|
+
return;
|
|
37
|
+
socket.destroy();
|
|
38
|
+
reject(new CliError(code, undefined, isHostConnectionError(code)));
|
|
39
|
+
}
|
|
40
|
+
socket.setEncoding('utf8');
|
|
41
|
+
socket.once('error', (error) => fail(['ENOENT', 'ECONNREFUSED'].includes(error.code ?? '') ? 'HOST_UNAVAILABLE' : 'HOST_DISCONNECTED'));
|
|
42
|
+
socket.once('connect', () => {
|
|
43
|
+
if (settled)
|
|
44
|
+
return;
|
|
45
|
+
if (performance.now() >= deadline)
|
|
46
|
+
return fail('HOST_TIMEOUT');
|
|
47
|
+
socket.write(encodeIpcFrame(decoded));
|
|
48
|
+
});
|
|
49
|
+
socket.on('data', (chunk) => {
|
|
50
|
+
if (settled)
|
|
51
|
+
return;
|
|
52
|
+
if (performance.now() >= deadline)
|
|
53
|
+
return fail('HOST_TIMEOUT');
|
|
54
|
+
reader.append(String(chunk));
|
|
55
|
+
const body = reader.take();
|
|
56
|
+
if (body === null)
|
|
57
|
+
return fail('INVALID_REPLY');
|
|
58
|
+
if (body === undefined || !settle())
|
|
59
|
+
return;
|
|
60
|
+
socket.end();
|
|
61
|
+
resolve(body);
|
|
62
|
+
});
|
|
63
|
+
socket.once('close', () => fail('HOST_DISCONNECTED'));
|
|
64
|
+
});
|
|
65
|
+
try {
|
|
66
|
+
const reply = decodeReply(frame);
|
|
67
|
+
if (reply.requestId !== decoded.requestId ||
|
|
68
|
+
(reply.ok && !validHostCommandResult(decoded.command, reply.result)))
|
|
69
|
+
throw new Error();
|
|
70
|
+
if (!reply.ok) {
|
|
71
|
+
const { code, message, retryable } = reply.error;
|
|
72
|
+
return {
|
|
73
|
+
...reply,
|
|
74
|
+
error: new CliError(safeCode(code), /\[diagnosticId: ([a-f0-9-]{36})\]/.exec(message)?.[1], retryable),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return reply;
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
throw new CliError('INVALID_REPLY');
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
export async function workspaceRoot(value) {
|
|
84
|
+
try {
|
|
85
|
+
return await realpath(path.resolve(value));
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
throw new CliError('WORKSPACE_UNAVAILABLE');
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { CliResult } from './results.js';
|
|
2
|
+
export declare const knownSubcommands: Set<string>;
|
|
3
|
+
export declare function argumentsFor(args: string[]): {
|
|
4
|
+
skillRefs: string[] | undefined;
|
|
5
|
+
commandId: string;
|
|
6
|
+
compaction?: {
|
|
7
|
+
summary?: string;
|
|
8
|
+
throughTurnId?: string;
|
|
9
|
+
};
|
|
10
|
+
name: string;
|
|
11
|
+
runId: string;
|
|
12
|
+
values: {
|
|
13
|
+
approve?: boolean;
|
|
14
|
+
deny?: boolean;
|
|
15
|
+
'decided-at'?: string;
|
|
16
|
+
'defer-resume'?: boolean;
|
|
17
|
+
reconnect?: boolean;
|
|
18
|
+
'poll-ms'?: string;
|
|
19
|
+
'timeout-ms'?: string;
|
|
20
|
+
'tool-call'?: string;
|
|
21
|
+
'not-applied'?: boolean;
|
|
22
|
+
completed?: boolean;
|
|
23
|
+
'abandon-unknown'?: boolean;
|
|
24
|
+
evidence?: string;
|
|
25
|
+
'result-json'?: string;
|
|
26
|
+
reason?: string;
|
|
27
|
+
'decided-by'?: string;
|
|
28
|
+
'through-turn'?: string;
|
|
29
|
+
summary?: string;
|
|
30
|
+
json?: boolean;
|
|
31
|
+
'start-host'?: boolean;
|
|
32
|
+
'allow-unsafe-sandbox-fallback'?: boolean;
|
|
33
|
+
'command-id'?: string;
|
|
34
|
+
text?: string;
|
|
35
|
+
session?: string;
|
|
36
|
+
title?: string;
|
|
37
|
+
profile?: string;
|
|
38
|
+
'task-profile'?: string;
|
|
39
|
+
mode?: string;
|
|
40
|
+
'team-file'?: string;
|
|
41
|
+
'team-preset'?: string;
|
|
42
|
+
concurrency?: string;
|
|
43
|
+
'max-members'?: string;
|
|
44
|
+
member?: string;
|
|
45
|
+
after?: string;
|
|
46
|
+
timeline?: boolean;
|
|
47
|
+
limit?: string;
|
|
48
|
+
approval?: string;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
export declare function integer(value: string | boolean | undefined, fallback: number, max: number): number;
|
|
52
|
+
type CliArguments = ReturnType<typeof argumentsFor> & {
|
|
53
|
+
interactiveProgress?: boolean;
|
|
54
|
+
};
|
|
55
|
+
export type RunProgressArguments = Pick<CliArguments, 'runId' | 'values' | 'commandId' | 'interactiveProgress'>;
|
|
56
|
+
export declare function runProgressCommand(args: RunProgressArguments, after?: string): {
|
|
57
|
+
interactive?: boolean;
|
|
58
|
+
type: "run.progress";
|
|
59
|
+
runId: string;
|
|
60
|
+
after: string;
|
|
61
|
+
limit: number;
|
|
62
|
+
};
|
|
63
|
+
export declare function execute(root: string, args: CliArguments, timeoutMs?: number, signal?: AbortSignal): Promise<CliResult>;
|
|
64
|
+
export {};
|
package/dist/commands.js
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { parseArgs } from 'node:util';
|
|
2
|
+
import { randomUUID, createHash } from 'node:crypto';
|
|
3
|
+
import { readFile } from 'node:fs/promises';
|
|
4
|
+
import { contextExtensionRefPattern, MAX_RUN_PROGRESS_PAGE_SIZE, parseTeamConfig, } from '@saturn-universe/protocol';
|
|
5
|
+
import { CliError } from './cli-diagnostics.js';
|
|
6
|
+
import { compactionArguments, compactionOptions } from './compaction-command.js';
|
|
7
|
+
import { ensureHostWorkspace, reconcileRunTool, resolveRunApproval, sendHostCommand, } from './host-command.js';
|
|
8
|
+
import { reconciliationCommand, reconciliationOptions } from './reconcile-command.js';
|
|
9
|
+
import { help } from './cli-help.js';
|
|
10
|
+
export const knownSubcommands = new Set([
|
|
11
|
+
'init',
|
|
12
|
+
'session',
|
|
13
|
+
'submit',
|
|
14
|
+
'query',
|
|
15
|
+
'refresh',
|
|
16
|
+
'team',
|
|
17
|
+
'inbox',
|
|
18
|
+
'wait',
|
|
19
|
+
'progress',
|
|
20
|
+
'attach',
|
|
21
|
+
'cancel',
|
|
22
|
+
'resume',
|
|
23
|
+
'compact',
|
|
24
|
+
'reconcile',
|
|
25
|
+
'approval',
|
|
26
|
+
'status',
|
|
27
|
+
'shutdown',
|
|
28
|
+
'help',
|
|
29
|
+
]);
|
|
30
|
+
function parseSubcommandArgs(args) {
|
|
31
|
+
try {
|
|
32
|
+
return parseArgs({
|
|
33
|
+
args,
|
|
34
|
+
allowPositionals: true,
|
|
35
|
+
strict: true,
|
|
36
|
+
options: {
|
|
37
|
+
json: { type: 'boolean' },
|
|
38
|
+
'start-host': { type: 'boolean' },
|
|
39
|
+
'allow-unsafe-sandbox-fallback': { type: 'boolean' },
|
|
40
|
+
'command-id': { type: 'string' },
|
|
41
|
+
text: { type: 'string' },
|
|
42
|
+
session: { type: 'string' },
|
|
43
|
+
title: { type: 'string' },
|
|
44
|
+
profile: { type: 'string' },
|
|
45
|
+
'task-profile': { type: 'string' },
|
|
46
|
+
mode: { type: 'string' },
|
|
47
|
+
skill: { type: 'string', multiple: true },
|
|
48
|
+
'team-file': { type: 'string' },
|
|
49
|
+
'team-preset': { type: 'string' },
|
|
50
|
+
concurrency: { type: 'string' },
|
|
51
|
+
'max-members': { type: 'string' },
|
|
52
|
+
member: { type: 'string' },
|
|
53
|
+
after: { type: 'string' },
|
|
54
|
+
timeline: { type: 'boolean' },
|
|
55
|
+
limit: { type: 'string' },
|
|
56
|
+
approval: { type: 'string' },
|
|
57
|
+
...compactionOptions,
|
|
58
|
+
...reconciliationOptions,
|
|
59
|
+
approve: { type: 'boolean' },
|
|
60
|
+
deny: { type: 'boolean' },
|
|
61
|
+
'decided-at': { type: 'string' },
|
|
62
|
+
'defer-resume': { type: 'boolean' },
|
|
63
|
+
reconnect: { type: 'boolean' },
|
|
64
|
+
'poll-ms': { type: 'string' },
|
|
65
|
+
'timeout-ms': { type: 'string' },
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
throw new CliError('USAGE');
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
export function argumentsFor(args) {
|
|
74
|
+
const parsed = parseSubcommandArgs(args);
|
|
75
|
+
const [name, runId, ...extra] = parsed.positionals;
|
|
76
|
+
const allowed = {
|
|
77
|
+
init: [],
|
|
78
|
+
session: ['title'],
|
|
79
|
+
submit: [
|
|
80
|
+
'text',
|
|
81
|
+
'session',
|
|
82
|
+
'title',
|
|
83
|
+
'profile',
|
|
84
|
+
'task-profile',
|
|
85
|
+
'mode',
|
|
86
|
+
'skill',
|
|
87
|
+
'team-file',
|
|
88
|
+
'team-preset',
|
|
89
|
+
'concurrency',
|
|
90
|
+
'max-members',
|
|
91
|
+
],
|
|
92
|
+
query: ['timeline'],
|
|
93
|
+
refresh: [],
|
|
94
|
+
team: [],
|
|
95
|
+
inbox: ['member'],
|
|
96
|
+
wait: ['poll-ms', 'timeout-ms', 'reconnect'],
|
|
97
|
+
progress: ['after', 'limit'],
|
|
98
|
+
attach: ['after', 'limit', 'poll-ms', 'timeout-ms', 'reconnect'],
|
|
99
|
+
cancel: ['reason'],
|
|
100
|
+
resume: [],
|
|
101
|
+
compact: Object.keys(compactionOptions),
|
|
102
|
+
reconcile: Object.keys(reconciliationOptions),
|
|
103
|
+
approval: [
|
|
104
|
+
'approval',
|
|
105
|
+
'tool-call',
|
|
106
|
+
'approve',
|
|
107
|
+
'deny',
|
|
108
|
+
'decided-by',
|
|
109
|
+
'decided-at',
|
|
110
|
+
'reason',
|
|
111
|
+
'defer-resume',
|
|
112
|
+
],
|
|
113
|
+
status: [],
|
|
114
|
+
shutdown: [],
|
|
115
|
+
help: [],
|
|
116
|
+
};
|
|
117
|
+
if (!name ||
|
|
118
|
+
!allowed[name] ||
|
|
119
|
+
extra.length ||
|
|
120
|
+
(['init', 'session', 'submit', 'status', 'shutdown', 'help'].includes(name)
|
|
121
|
+
? runId !== undefined
|
|
122
|
+
: !runId))
|
|
123
|
+
throw new CliError('USAGE');
|
|
124
|
+
const globalAllowed = name === 'help'
|
|
125
|
+
? ['json', 'command-id']
|
|
126
|
+
: ['json', 'command-id', 'start-host', 'allow-unsafe-sandbox-fallback'];
|
|
127
|
+
for (const key of Object.keys(parsed.values))
|
|
128
|
+
if (![...globalAllowed, ...allowed[name]].includes(key))
|
|
129
|
+
throw new CliError('USAGE');
|
|
130
|
+
const { skill: skillRefs, ...values } = parsed.values;
|
|
131
|
+
if ((values.mode !== undefined && !contextExtensionRefPattern.test(String(values.mode))) ||
|
|
132
|
+
skillRefs?.some((ref) => !contextExtensionRefPattern.test(ref)))
|
|
133
|
+
throw new CliError('USAGE');
|
|
134
|
+
if (name === 'submit') {
|
|
135
|
+
if (!values.text)
|
|
136
|
+
throw new CliError('USAGE');
|
|
137
|
+
if (values.concurrency !== undefined)
|
|
138
|
+
integer(values.concurrency, 1, 16);
|
|
139
|
+
if (values['max-members'] !== undefined)
|
|
140
|
+
integer(values['max-members'], 1, 15);
|
|
141
|
+
if (values['team-file'] &&
|
|
142
|
+
(values['team-preset'] !== undefined ||
|
|
143
|
+
values.concurrency !== undefined ||
|
|
144
|
+
values['max-members'] !== undefined))
|
|
145
|
+
throw new CliError('USAGE');
|
|
146
|
+
if (values['team-preset'] !== undefined &&
|
|
147
|
+
(values.concurrency !== undefined || values['max-members'] !== undefined))
|
|
148
|
+
throw new CliError('USAGE');
|
|
149
|
+
}
|
|
150
|
+
if (name === 'approval' &&
|
|
151
|
+
(!values.approval || !values['tool-call'] || Boolean(values.approve) === Boolean(values.deny)))
|
|
152
|
+
throw new CliError('USAGE');
|
|
153
|
+
return {
|
|
154
|
+
name,
|
|
155
|
+
runId: runId ?? '',
|
|
156
|
+
values,
|
|
157
|
+
...(name === 'compact' ? { compaction: compactionArguments(values) } : {}),
|
|
158
|
+
skillRefs,
|
|
159
|
+
commandId: String(values['command-id'] ?? randomUUID()),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
export function integer(value, fallback, max) {
|
|
163
|
+
const number = value === undefined ? fallback : Number(value);
|
|
164
|
+
if (!Number.isSafeInteger(number) || number < 1 || number > max)
|
|
165
|
+
throw new CliError('USAGE');
|
|
166
|
+
return number;
|
|
167
|
+
}
|
|
168
|
+
export function runProgressCommand(args, after = String(args.values.after ?? '0')) {
|
|
169
|
+
return {
|
|
170
|
+
type: 'run.progress',
|
|
171
|
+
runId: args.runId,
|
|
172
|
+
after,
|
|
173
|
+
limit: integer(args.values.limit, MAX_RUN_PROGRESS_PAGE_SIZE, MAX_RUN_PROGRESS_PAGE_SIZE),
|
|
174
|
+
...(args.interactiveProgress ? { interactive: true } : {}),
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
export async function execute(root, args, timeoutMs, signal) {
|
|
178
|
+
const { name, runId, values, commandId } = args;
|
|
179
|
+
const options = { commandId, timeoutMs, signal };
|
|
180
|
+
switch (name) {
|
|
181
|
+
case 'help':
|
|
182
|
+
return { help };
|
|
183
|
+
case 'init':
|
|
184
|
+
return ensureHostWorkspace(root, options);
|
|
185
|
+
case 'session': {
|
|
186
|
+
const w = await ensureHostWorkspace(root, options);
|
|
187
|
+
return sendHostCommand(root, { type: 'session.create', workspaceId: w.workspaceId, title: values.title ?? 'CLI session' }, options);
|
|
188
|
+
}
|
|
189
|
+
case 'submit': {
|
|
190
|
+
const teamFile = values['team-file'];
|
|
191
|
+
const team = typeof teamFile === 'string' ? await readTeamFile(teamFile) : undefined;
|
|
192
|
+
const concurrency = values.concurrency === undefined ? undefined : integer(values.concurrency, 1, 16);
|
|
193
|
+
const maxMembers = values['max-members'] === undefined ? undefined : integer(values['max-members'], 1, 15);
|
|
194
|
+
const teamHint = concurrency !== undefined || maxMembers !== undefined
|
|
195
|
+
? {
|
|
196
|
+
force: true,
|
|
197
|
+
...(concurrency !== undefined ? { concurrency } : {}),
|
|
198
|
+
...(maxMembers !== undefined ? { maxMembers } : {}),
|
|
199
|
+
}
|
|
200
|
+
: undefined;
|
|
201
|
+
const w = await ensureHostWorkspace(root, options);
|
|
202
|
+
const sessionId = values.session === undefined
|
|
203
|
+
? (await sendHostCommand(root, { type: 'session.create', workspaceId: w.workspaceId, title: values.title ?? 'CLI task' }, {
|
|
204
|
+
...options,
|
|
205
|
+
commandId: 'cli:session:' + createHash('sha256').update(commandId).digest('hex'),
|
|
206
|
+
})).sessionId
|
|
207
|
+
: String(values.session);
|
|
208
|
+
const result = await sendHostCommand(root, {
|
|
209
|
+
type: 'run.submit',
|
|
210
|
+
input: {
|
|
211
|
+
workspaceId: w.workspaceId,
|
|
212
|
+
sessionId,
|
|
213
|
+
content: [{ type: 'text', text: values.text }],
|
|
214
|
+
...(values.profile ? { profile: values.profile } : {}),
|
|
215
|
+
...(values['task-profile'] ? { taskProfile: values['task-profile'] } : {}),
|
|
216
|
+
...(values.mode || team
|
|
217
|
+
? {
|
|
218
|
+
config: {
|
|
219
|
+
...(values.mode ? { modeRef: values.mode } : {}),
|
|
220
|
+
...(team ? { team: { ...team, members: [...team.members] } } : {}),
|
|
221
|
+
},
|
|
222
|
+
}
|
|
223
|
+
: {}),
|
|
224
|
+
...(args.skillRefs ? { skillRefs: args.skillRefs } : {}),
|
|
225
|
+
...(teamHint ? { teamHint } : {}),
|
|
226
|
+
...(values['team-preset'] ? { teamPreset: values['team-preset'] } : {}),
|
|
227
|
+
},
|
|
228
|
+
}, options);
|
|
229
|
+
return { ...result, sessionId };
|
|
230
|
+
}
|
|
231
|
+
case 'query': {
|
|
232
|
+
if (!values.timeline)
|
|
233
|
+
return sendHostCommand(root, { type: 'run.query', runId }, options);
|
|
234
|
+
let after = '0';
|
|
235
|
+
const events = [];
|
|
236
|
+
while (true) {
|
|
237
|
+
const { events: page, nextAfter, ...result } = await sendHostCommand(root, { type: 'run.timeline', runId, after }, options);
|
|
238
|
+
events.push(...page);
|
|
239
|
+
if (nextAfter === undefined)
|
|
240
|
+
return { ...result, events };
|
|
241
|
+
after = nextAfter;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
case 'refresh':
|
|
245
|
+
return sendHostCommand(root, { type: 'run.query', runId }, options);
|
|
246
|
+
case 'team':
|
|
247
|
+
return sendHostCommand(root, { type: 'team.query', runId }, options);
|
|
248
|
+
case 'inbox':
|
|
249
|
+
return sendHostCommand(root, {
|
|
250
|
+
type: 'team.inbox',
|
|
251
|
+
runId,
|
|
252
|
+
...(values.member === undefined ? {} : { memberId: values.member }),
|
|
253
|
+
}, options);
|
|
254
|
+
case 'progress':
|
|
255
|
+
case 'attach':
|
|
256
|
+
case 'wait':
|
|
257
|
+
return sendHostCommand(root, runProgressCommand(args), options);
|
|
258
|
+
case 'cancel':
|
|
259
|
+
return sendHostCommand(root, { type: 'run.cancel', runId, reason: values.reason ?? 'Cancelled from CLI' }, options);
|
|
260
|
+
case 'resume':
|
|
261
|
+
return sendHostCommand(root, { type: 'run.resume', runId }, options);
|
|
262
|
+
case 'compact':
|
|
263
|
+
return sendHostCommand(root, { type: 'surface.compact', runId, ...args.compaction }, options);
|
|
264
|
+
case 'reconcile': {
|
|
265
|
+
const command = reconciliationCommand(values);
|
|
266
|
+
if ('error' in command)
|
|
267
|
+
throw new CliError('USAGE');
|
|
268
|
+
return reconcileRunTool(root, runId, command, options);
|
|
269
|
+
}
|
|
270
|
+
case 'approval': {
|
|
271
|
+
const resolved = await resolveRunApproval(root, runId, {
|
|
272
|
+
approvalId: values.approval,
|
|
273
|
+
toolCallId: values['tool-call'],
|
|
274
|
+
approved: Boolean(values.approve),
|
|
275
|
+
decidedBy: values['decided-by'],
|
|
276
|
+
decidedAt: values['decided-at'],
|
|
277
|
+
reason: values.reason,
|
|
278
|
+
deferResume: values['defer-resume'],
|
|
279
|
+
}, options);
|
|
280
|
+
if (!resolved)
|
|
281
|
+
throw new CliError('INVALID_COMMAND');
|
|
282
|
+
return resolved.receipt;
|
|
283
|
+
}
|
|
284
|
+
case 'status':
|
|
285
|
+
return sendHostCommand(root, { type: 'host.status' }, options);
|
|
286
|
+
case 'shutdown':
|
|
287
|
+
return sendHostCommand(root, { type: 'host.shutdown' }, options);
|
|
288
|
+
default:
|
|
289
|
+
throw new CliError('USAGE');
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
async function readTeamFile(filename) {
|
|
293
|
+
try {
|
|
294
|
+
return parseTeamConfig(JSON.parse(await readFile(filename, 'utf8')));
|
|
295
|
+
}
|
|
296
|
+
catch {
|
|
297
|
+
throw new CliError('INVALID_TEAM_CONFIG');
|
|
298
|
+
}
|
|
299
|
+
}
|