@phyzess/kitto 0.1.1 → 0.2.1

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.
@@ -0,0 +1,68 @@
1
+ import { createInterface } from 'node:readline';
2
+
3
+ export async function setupPrompts(input, output) {
4
+ const print = message => output.write(message + '\n');
5
+ if (input.isTTY && output.isTTY && typeof input.setRawMode === 'function') {
6
+ const ui = await import('@clack/prompts');
7
+ const settings = { input, output };
8
+ const answer = async prompt => {
9
+ const result = await prompt;
10
+ return ui.isCancel(result) ? undefined : result;
11
+ };
12
+ return {
13
+ print,
14
+ close() {},
15
+ hosts: (options, defaults) => answer(ui.multiselect({
16
+ ...settings, message: '选择宿主(↑↓ 移动,空格勾选,Enter 确认)', options,
17
+ initialValues: defaults, required: true, showInstructions: false,
18
+ })),
19
+ select: (message, options, initialValue) => answer(ui.select({ ...settings, message, options, initialValue })),
20
+ yesNo: (message, initialValue) => answer(ui.confirm({ ...settings, message, initialValue, active: '是', inactive: '否' })),
21
+ text: (message, { initialValue, validate } = {}) => answer(ui.text({
22
+ ...settings, message, initialValue, validate: value => validate?.((value ?? '').trim()),
23
+ })).then(value => value?.trim()),
24
+ };
25
+ }
26
+
27
+ const rl = createInterface({ input, output, terminal: Boolean(input.isTTY && output.isTTY) });
28
+ const lines = rl[Symbol.asyncIterator]();
29
+ rl.on('SIGINT', () => rl.close());
30
+ async function read(message, parse) {
31
+ while (true) {
32
+ print(message);
33
+ const reply = await lines.next();
34
+ if (reply.done) return;
35
+ const value = parse(reply.value.trim());
36
+ if (value !== undefined) return value;
37
+ print('输入无效,请重试。');
38
+ }
39
+ }
40
+ return {
41
+ print,
42
+ close: () => rl.close(),
43
+ hosts(options, defaults) {
44
+ options.forEach(({ label, hint }, i) => print(` ${i + 1}. ${label}(${hint})`));
45
+ return read(`选择宿主 [${defaults.join(',')}],可输入多个名称或编号,0 取消。`, answer => {
46
+ if (answer === '0') return [];
47
+ if (!answer && defaults.length) return defaults;
48
+ const values = answer.toLowerCase().split(/[,,\s]+/).filter(Boolean)
49
+ .map(item => options.find((o, i) => o.value === item || String(i + 1) === item)?.value);
50
+ if (values.length && values.every(Boolean)) return [...new Set(values)];
51
+ });
52
+ },
53
+ select(message, options, initialValue) {
54
+ return read(message + ' ' + options.map(o => o.value + ' ' + o.label).join(',') + '。', value =>
55
+ options.find(o => o.value === (value || initialValue))?.value);
56
+ },
57
+ yesNo(message, initialValue) {
58
+ return read(message + (initialValue ? ' [Y/n]' : ' [y/N]'), value => {
59
+ if (!value) return initialValue;
60
+ if (/^(y|yes|是)$/i.test(value)) return true;
61
+ if (/^(n|no|否)$/i.test(value)) return false;
62
+ });
63
+ },
64
+ text(message, { initialValue = '', validate } = {}) {
65
+ return read(message, value => validate?.(value || initialValue) ? undefined : value || initialValue);
66
+ },
67
+ };
68
+ }
@@ -0,0 +1,190 @@
1
+ import { execFile } from 'node:child_process';
2
+ import { homedir } from 'node:os';
3
+ import { resolve } from 'node:path';
4
+ import { setupPrompts } from './setup-prompts.mjs';
5
+ import { promisify } from 'node:util';
6
+
7
+ const agents = ['pi', 'codex', 'dsh'];
8
+ const names = { pi: 'Pi', codex: 'Codex', dsh: 'DeepSeek Harness (DSH)' };
9
+ const serviceUnit = 'dsh.service';
10
+ const exec = promisify(execFile);
11
+
12
+ async function detectEnvironment() {
13
+ async function works(command, args) {
14
+ try {
15
+ await exec(command, args, { timeout: 3000 });
16
+ return true;
17
+ } catch {
18
+ return false;
19
+ }
20
+ }
21
+ const [detected, pnpmAvailable, serviceActive] = await Promise.all([
22
+ Promise.all(agents.map(name => works(name, ['--version']))),
23
+ works('pnpm', ['--version']),
24
+ process.platform === 'linux' && works('/usr/bin/systemctl', ['--system', 'is-active', '--quiet', serviceUnit]),
25
+ ]);
26
+ return {
27
+ available: agents.filter((_, index) => detected[index]),
28
+ pnpmAvailable,
29
+ serviceActive,
30
+ canConfigureService: process.getuid?.() === 0 && process.geteuid?.() === 0,
31
+ };
32
+ }
33
+
34
+ function expandHome(path) {
35
+ if (path === '~') return homedir();
36
+ return path.startsWith('~/') ? resolve(homedir(), path.slice(2)) : path;
37
+ }
38
+
39
+ async function configure({ input, output, environment, execute, probeServicePnpm }) {
40
+ const { print, hosts, select, text, yesNo, close } = await setupPrompts(input, output);
41
+ try {
42
+ const { available, pnpmAvailable, serviceActive, canConfigureService } = environment;
43
+ const serviceAvailable = serviceActive && canConfigureService;
44
+ const defaults = agents.filter(agent => available.includes(agent) || (agent === 'dsh' && serviceAvailable));
45
+ print('Kitto 环境安装向导');
46
+ print('为选中的宿主配置当前账号的共享规则与 skills。按 Ctrl+C 可取消。');
47
+ const hostOptions = agents.map(value => ({
48
+ value, label: names[value],
49
+ hint: value === 'dsh' && serviceAvailable ? serviceUnit : available.includes(value) ? '已安装 CLI' : '未检测到 CLI',
50
+ }));
51
+ if (serviceActive) {
52
+ print('检测到正在运行的系统服务 dsh.service。' +
53
+ (canConfigureService ? '可以配置服务账号。' : '需要 root 权限才能配置服务账号。'));
54
+ }
55
+
56
+ let selected;
57
+ let unit;
58
+ let installAgents;
59
+ while (true) {
60
+ selected = await hosts(hostOptions, defaults);
61
+ if (!selected?.length) return undefined;
62
+
63
+ unit = undefined;
64
+ if (selected.includes('dsh') && serviceAvailable) {
65
+ const useService = await yesNo('配置 dsh.service 使用的 DSH 账号?', true);
66
+ if (useService === undefined) return undefined;
67
+ if (useService) unit = serviceUnit;
68
+ }
69
+
70
+ const missing = selected.filter(agent => !available.includes(agent) && !(agent === 'dsh' && unit));
71
+ installAgents = false;
72
+ if (missing.length) {
73
+ installAgents = await yesNo(`补装 ${missing.map(agent => names[agent]).join('、')}?`, true);
74
+ if (installAgents === undefined) return undefined;
75
+ if (!installAgents) continue;
76
+ }
77
+ break;
78
+ }
79
+
80
+ print('完整配置包含仓库的 AGENTS.md、skills 和所选宿主的 Kitto 集成。选择 Pi 时还会同步 Pi 包、扩展和 MCP 设置。');
81
+ let source;
82
+ let serviceInstallAgents;
83
+ let servicePnpmAvailable;
84
+ while (true) {
85
+ const sourceChoice = await select('选择配置来源', [
86
+ { value: '1', label: '默认 Kitto 配置', hint: '优先复用已有安装' },
87
+ { value: '2', label: '指定配置仓库路径或 Git URL' },
88
+ { value: '3', label: '仅配置共享规则与 skills' },
89
+ { value: '0', label: '取消' },
90
+ ], '1');
91
+ if (sourceChoice === undefined || sourceChoice === '0') return;
92
+
93
+ const needsPnpm = sourceChoice !== '3' && selected.includes('dsh') && !unit && pnpmAvailable === false;
94
+ if (needsPnpm) {
95
+ const installPnpm = await yesNo('本机 DSH 的完整配置需要 pnpm。允许补装 pnpm?', true);
96
+ if (installPnpm === undefined) return;
97
+ if (!installPnpm) {
98
+ print('可选择仅配置共享规则与 skills。');
99
+ continue;
100
+ }
101
+ installAgents = true;
102
+ }
103
+
104
+ if (unit && sourceChoice !== '3') {
105
+ servicePnpmAvailable ??= await probeServicePnpm(unit);
106
+ if (servicePnpmAvailable === false) {
107
+ const installPnpm = await yesNo(`服务 ${unit} 的完整配置需要 pnpm。允许在服务账号中补装 pnpm?`, true);
108
+ if (installPnpm === undefined) return;
109
+ if (!installPnpm) {
110
+ print('可选择仅配置共享规则与 skills。');
111
+ continue;
112
+ }
113
+ serviceInstallAgents = true;
114
+ }
115
+ }
116
+
117
+ if (sourceChoice === '2') {
118
+ const path = await text('配置仓库路径或 Git URL', { validate: value => value ? undefined : '请输入路径或 Git URL' });
119
+ if (path === undefined) return;
120
+ source = expandHome(path);
121
+ } else if (sourceChoice === '3') {
122
+ source = false;
123
+ }
124
+ break;
125
+ }
126
+
127
+ const deps = source === false ? false : await yesNo('安装配置中声明的辅助工具和依赖?', false);
128
+ if (deps === undefined) return undefined;
129
+ const workspace = await text('工作区父目录(可选)。例如未来项目会放在 ~/workspaces/workspace-1 下,就填 ~/workspaces。留空使用全局配置。');
130
+ if (workspace === undefined) return undefined;
131
+
132
+ let dshProfile = 'web';
133
+ if (selected.includes('dsh')) {
134
+ dshProfile = await text('DSH 配置档名称', {
135
+ initialValue: 'web',
136
+ validate: value => /^[a-zA-Z0-9][a-zA-Z0-9_-]*$/.test(value) && value !== 'desktop' ? undefined : '请输入有效的 DSH 配置档名称',
137
+ });
138
+ if (dshProfile === undefined) return undefined;
139
+ }
140
+
141
+ const plan = {
142
+ agents: selected, source, installAgents, deps, workspace: workspace ? expandHome(workspace) : undefined,
143
+ unit, dshProfile,
144
+ };
145
+ if (serviceInstallAgents) plan.serviceInstallAgents = true;
146
+ print('安装计划');
147
+ print(` 宿主 ${selected.map(agent => names[agent]).join('、')}`);
148
+ print(` 配置来源 ${source === false ? '仅配置共享规则与 skills' : source ?? '默认 Kitto 配置'}`);
149
+ print(' 共享范围 全局' + (plan.workspace ? `,以及工作区 ${plan.workspace}` : ''));
150
+ if (selected.includes('dsh')) print(` DSH ${unit ?? '当前账号'},配置档 ${dshProfile}`);
151
+ print(` 补装缺少的宿主和必需工具 ${installAgents ? '是' : '否'}`);
152
+ if (serviceInstallAgents) print(` 为 ${unit} 的账号补装 pnpm 是`);
153
+ if (source !== false) print(` 安装配置声明的辅助依赖 ${deps ? '是' : '否'}`);
154
+
155
+ let action = await select('下一步', [
156
+ { value: '1', label: '预览' }, { value: '2', label: '安装' }, { value: '0', label: '取消' },
157
+ ], '1');
158
+ if (action === '1') {
159
+ await execute({ ...plan, dryRun: true });
160
+ action = await select('预览完成', [
161
+ { value: '0', label: '退出' }, { value: '2', label: '按此计划安装' },
162
+ ], '0');
163
+ if (action !== '2') {
164
+ print('已退出,未执行安装。');
165
+ return true;
166
+ }
167
+ }
168
+ if (action !== '2') return;
169
+ close();
170
+ await execute({ ...plan, dryRun: false });
171
+ return true;
172
+ } finally {
173
+ close();
174
+ }
175
+ }
176
+
177
+ export async function runWizard({
178
+ input = process.stdin, output = process.stdout, detect = detectEnvironment, execute,
179
+ probeServicePnpm = async unit => {
180
+ const { servicePnpmAvailable } = await import('./scope-service.mjs');
181
+ return servicePnpmAvailable(unit);
182
+ },
183
+ } = {}) {
184
+ const runSetup = execute ?? (async plan => {
185
+ const { runEnvironmentSetup } = await import('./environment-setup.mjs');
186
+ await runEnvironmentSetup(plan);
187
+ });
188
+ const handled = await configure({ input, output, environment: await detect(), execute: runSetup, probeServicePnpm });
189
+ if (!handled) output.write('已取消安装。\n');
190
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phyzess/kitto",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
4
4
  "description": "Portable personal configuration for Pi, Codex and DeepSeek Harness",
5
5
  "type": "module",
6
6
  "bin": { "kitto": "bin/kitto.mjs" },
@@ -8,8 +8,9 @@
8
8
  "publishConfig": { "access": "public" },
9
9
  "repository": { "type": "git", "url": "git+https://github.com/phyzess/kitto.git" },
10
10
  "files": ["bin/*.mjs", "lib/*.mjs", "lib/*.py", "integration/", "sync.sh", "CHANGELOG.md"],
11
+ "dependencies": { "@clack/prompts": "1.9.0" },
11
12
  "scripts": {
12
- "test": "node --test tests/kitto.test.mjs",
13
+ "test": "node --test tests/kitto.test.mjs tests/scope*.test.mjs tests/setup-wizard.test.mjs",
13
14
  "prepublishOnly": "npm test && python3 -m unittest discover -s tests -v",
14
15
  "release": "node scripts/release.mjs"
15
16
  },