@hit-sudomaker/ccw 0.4.0 → 0.5.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/README.md +54 -52
- package/assets/ccline/config.toml +34 -34
- package/assets/skills/amirc-research/SKILL.md +39 -39
- package/dist/ccline.js +149 -616
- package/dist/ccw.js +167 -378
- package/dist/claude-program.js +191 -0
- package/dist/claude.js +177 -0
- package/dist/cleanup.js +150 -348
- package/dist/effect-machine.js +65 -0
- package/dist/frozen-json.js +99 -0
- package/dist/instance.js +71 -0
- package/dist/machine.js +613 -573
- package/dist/main.js +68 -58
- package/dist/management-catalog.js +60 -54
- package/dist/messages.js +182 -0
- package/dist/model.js +39 -144
- package/dist/npm-registry.js +110 -27
- package/dist/officecli.js +274 -404
- package/dist/planning.js +141 -0
- package/dist/plugins.js +134 -294
- package/dist/program-channels.js +94 -384
- package/dist/program-operations.js +155 -0
- package/dist/read-machine.js +94 -43
- package/dist/render.js +350 -347
- package/dist/settings.js +380 -0
- package/dist/skill-files.js +184 -77
- package/dist/skills.js +393 -237
- package/dist/terminal-view.js +303 -395
- package/dist/terminal.js +207 -467
- package/dist/work.js +135 -0
- package/package.json +1 -2
- package/dist/claude-config.js +0 -347
- package/dist/errors.js +0 -139
- package/dist/legacy-amirc.js +0 -60
- package/dist/mcp.js +0 -299
- package/dist/programs.js +0 -778
- package/dist/run-presentation.js +0 -155
- package/dist/titles.js +0 -43
- package/dist/warnings.js +0 -6
package/dist/planning.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { freezeClaudeCommand } from './claude-program.js';
|
|
3
|
+
import { knownTitle } from './management-catalog.js';
|
|
4
|
+
import { createCcline } from './ccline.js';
|
|
5
|
+
import { createClaudeDomain } from './claude.js';
|
|
6
|
+
import { createPluginsDomain } from './plugins.js';
|
|
7
|
+
import { createSkillsDomain, overlappingSkillSelections } from './skills.js';
|
|
8
|
+
import { createOfficecli } from './officecli.js';
|
|
9
|
+
import { errorCode } from './machine.js';
|
|
10
|
+
import { cancelled, kindOf, restOf } from './model.js';
|
|
11
|
+
const PREPARATION_CONCURRENCY = 2;
|
|
12
|
+
/** 业务入口:Claude、Plugins、Skills,以及各自负责的额外工具 */
|
|
13
|
+
export const CONTENT_DOMAINS = [
|
|
14
|
+
[['claude', 'settings', 'data'], 'claude', createClaudeDomain],
|
|
15
|
+
[['plugin'], 'plugins', createPluginsDomain],
|
|
16
|
+
[['skill', 'skill-set'], 'skills', createSkillsDomain],
|
|
17
|
+
[['officecli'], 'officecli', createOfficecli],
|
|
18
|
+
[['ccline'], 'ccline', createCcline],
|
|
19
|
+
];
|
|
20
|
+
/**
|
|
21
|
+
* 生成资源已准备、尚未执行的计划
|
|
22
|
+
*
|
|
23
|
+
* @param readScope - 为并行准备隔离取消和进度归属的读取作用域
|
|
24
|
+
* @returns 接收请求、取消信号和进度回调的准备函数
|
|
25
|
+
* @remarks 单项问题进入预览,无效请求和取消使准备拒绝;调用方隔离回调异常并消费计划
|
|
26
|
+
*/
|
|
27
|
+
export function createPlanner(ctx, readScope) {
|
|
28
|
+
// 准备域复用同一上下文;检查域另行装配以使用当次读取预算
|
|
29
|
+
const prepareDomains = new Map();
|
|
30
|
+
for (const [keys, , factory] of CONTENT_DOMAINS) {
|
|
31
|
+
const domain = factory(ctx);
|
|
32
|
+
for (const key of keys)
|
|
33
|
+
prepareDomains.set(key, domain);
|
|
34
|
+
}
|
|
35
|
+
return async (input, signal, emit) => {
|
|
36
|
+
cancelled(signal);
|
|
37
|
+
const snapshot = structuredClone(input);
|
|
38
|
+
if (new Set(snapshot.selected.map(pick => pick.id)).size !== snapshot.selected.length)
|
|
39
|
+
throw new Error('duplicate-selection');
|
|
40
|
+
if (!['install', 'uninstall', 'clean'].includes(snapshot.task))
|
|
41
|
+
throw new Error('action-unavailable');
|
|
42
|
+
if (snapshot.selected.some(pick => (kindOf(pick.id) === 'data') !== (snapshot.task === 'clean')))
|
|
43
|
+
throw new Error('action-unavailable');
|
|
44
|
+
if ('removeData' in snapshot || snapshot.selected.some(pick => 'action' in pick))
|
|
45
|
+
throw new Error('action-unavailable');
|
|
46
|
+
const request = snapshot.selected.map(pick => ({
|
|
47
|
+
id: pick.id,
|
|
48
|
+
action: snapshot.task === 'clean'
|
|
49
|
+
? 'clean'
|
|
50
|
+
: snapshot.task === 'install'
|
|
51
|
+
? pick.id === 'settings'
|
|
52
|
+
? 'configure'
|
|
53
|
+
: 'install'
|
|
54
|
+
: ['claude', 'officecli', 'ccline'].includes(kindOf(pick.id))
|
|
55
|
+
? 'uninstall'
|
|
56
|
+
: 'detach',
|
|
57
|
+
...(pick.fields ? { fields: pick.fields } : {}),
|
|
58
|
+
}));
|
|
59
|
+
const work = new Map();
|
|
60
|
+
const claudeSelections = request.filter(selection => kindOf(selection.id) === 'claude');
|
|
61
|
+
const otherSelections = request.filter(selection => kindOf(selection.id) !== 'claude');
|
|
62
|
+
let existingCommand;
|
|
63
|
+
const commandFor = async () => {
|
|
64
|
+
if (claudeSelections.length) {
|
|
65
|
+
const selection = claudeSelections.length === 1 ? claudeSelections[0] : undefined;
|
|
66
|
+
const command = selection ? work.get(selection.id)?.claude : undefined;
|
|
67
|
+
if (!command)
|
|
68
|
+
throw new Error('dependency-not-ready');
|
|
69
|
+
return command;
|
|
70
|
+
}
|
|
71
|
+
return (existingCommand ??= freezeClaudeCommand(ctx));
|
|
72
|
+
};
|
|
73
|
+
const prepareItem = async (selection) => {
|
|
74
|
+
cancelled(signal);
|
|
75
|
+
const title = planDisplayTitle(selection.id);
|
|
76
|
+
const scope = {
|
|
77
|
+
...(signal ? { signal } : {}),
|
|
78
|
+
report: stage => emit({ id: selection.id, title, stage }),
|
|
79
|
+
};
|
|
80
|
+
return readScope.run(scope, async () => {
|
|
81
|
+
scope.report('inspect');
|
|
82
|
+
let operation;
|
|
83
|
+
let problem = null;
|
|
84
|
+
try {
|
|
85
|
+
const domain = prepareDomains.get(kindOf(selection.id));
|
|
86
|
+
if (!domain)
|
|
87
|
+
throw new Error('unknown-selection');
|
|
88
|
+
operation = await domain.prepare(selection, commandFor);
|
|
89
|
+
work.set(selection.id, operation);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
problem = errorCode(error);
|
|
93
|
+
}
|
|
94
|
+
cancelled(signal);
|
|
95
|
+
return {
|
|
96
|
+
id: selection.id,
|
|
97
|
+
title,
|
|
98
|
+
action: operation?.action ?? selection.action,
|
|
99
|
+
changes: operation?.changes ?? [],
|
|
100
|
+
problem,
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
const prepareGroup = async (selections) => {
|
|
105
|
+
const items = [];
|
|
106
|
+
let next = 0;
|
|
107
|
+
const workers = await Promise.allSettled(Array.from({ length: Math.min(PREPARATION_CONCURRENCY, selections.length) }, async () => {
|
|
108
|
+
while (next < selections.length) {
|
|
109
|
+
const index = next++;
|
|
110
|
+
const selection = selections[index];
|
|
111
|
+
if (selection)
|
|
112
|
+
items[index] = await prepareItem(selection);
|
|
113
|
+
}
|
|
114
|
+
}));
|
|
115
|
+
for (const result of workers)
|
|
116
|
+
if (result.status === 'rejected')
|
|
117
|
+
throw result.reason;
|
|
118
|
+
return items;
|
|
119
|
+
};
|
|
120
|
+
const claudeItems = await prepareGroup(claudeSelections);
|
|
121
|
+
const otherItems = await prepareGroup(otherSelections);
|
|
122
|
+
const ordered = snapshot.task === 'uninstall' ? [...otherItems, ...claudeItems] : [...claudeItems, ...otherItems];
|
|
123
|
+
const overlaps = overlappingSkillSelections(ctx, request, work);
|
|
124
|
+
const items = ordered.map(item => overlaps.has(item.id) ? { ...item, changes: [], problem: 'overlapping-selection' } : item);
|
|
125
|
+
for (const id of overlaps)
|
|
126
|
+
work.delete(id);
|
|
127
|
+
const plan = { id: randomUUID(), items, problem: items.length === 0 ? 'empty-selection' : null };
|
|
128
|
+
cancelled(signal);
|
|
129
|
+
return { plan, work };
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/** 根据对象标识生成计划标题,不以显示名称代替身份验证 */
|
|
133
|
+
function planDisplayTitle(id) {
|
|
134
|
+
const kind = kindOf(id);
|
|
135
|
+
const key = restOf(id) ?? '';
|
|
136
|
+
if (kind === 'plugin' || kind === 'skill')
|
|
137
|
+
return knownTitle(id, key || id);
|
|
138
|
+
if (kind === 'claude' || kind === 'officecli')
|
|
139
|
+
return knownTitle(kind);
|
|
140
|
+
return knownTitle(id);
|
|
141
|
+
}
|
package/dist/plugins.js
CHANGED
|
@@ -1,309 +1,149 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
|
-
import { knownTitle } from './
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const market = markets[marketName];
|
|
16
|
-
if (market === undefined)
|
|
17
|
-
return { repository: expected.repository, marketName };
|
|
18
|
-
if (!isObject(market) || !isObject(market.source))
|
|
19
|
-
return null;
|
|
20
|
-
const source = typeof market.source.repo === 'string'
|
|
21
|
-
? market.source.repo
|
|
22
|
-
: typeof market.source.url === 'string'
|
|
23
|
-
? market.source.url.replace(/^https:\/\/github.com\//, '').replace(/\.git$/, '')
|
|
24
|
-
: '';
|
|
25
|
-
return expected.acceptedRepositories.some(repository => repository === source)
|
|
26
|
-
? { repository: expected.repository, marketName }
|
|
27
|
-
: null;
|
|
28
|
-
}
|
|
29
|
-
/** 解析插件标题:未安装方案入口用官方名;实际对象须市场记录在场且来源匹配,证据不足保留原始键名 */
|
|
30
|
-
function pluginTitle(name, markets, isInstalled) {
|
|
31
|
-
if (!isInstalled)
|
|
32
|
-
return knownTitle(`plugin:${name}`, name);
|
|
33
|
-
if (markets === null)
|
|
34
|
-
return name;
|
|
35
|
-
const marketName = name.split('@')[1];
|
|
36
|
-
return marketName !== undefined && markets[marketName] !== undefined && pluginSource(name, markets) !== null
|
|
37
|
-
? knownTitle(`plugin:${name}`, name)
|
|
38
|
-
: name;
|
|
39
|
-
}
|
|
40
|
-
async function pluginRecords(ctx) {
|
|
41
|
-
const document = await json(ctx.machine, path.join(ctx.claudeRoot, 'plugins/installed_plugins.json'));
|
|
42
|
-
return object(document.plugins);
|
|
43
|
-
}
|
|
44
|
-
function userPluginRecords(value) {
|
|
45
|
-
if (value === undefined)
|
|
46
|
-
return [];
|
|
47
|
-
if (!Array.isArray(value))
|
|
48
|
-
throw new Error('invalid-plugin-record');
|
|
49
|
-
const entries = [];
|
|
50
|
-
for (const entry of value) {
|
|
51
|
-
if (!isObject(entry) || typeof entry.scope !== 'string')
|
|
52
|
-
throw new Error('invalid-plugin-record');
|
|
53
|
-
if (entry.scope === 'user')
|
|
54
|
-
entries.push(entry);
|
|
55
|
-
}
|
|
56
|
-
return entries;
|
|
57
|
-
}
|
|
58
|
-
/** 只回答安装记录的载荷是否存在,不决定复用资格或错误处理 */
|
|
59
|
-
async function pluginFilePresent(ctx, record) {
|
|
60
|
-
return typeof record.installPath === 'string' && (await ctx.machine.realpath(record.installPath)) !== null;
|
|
61
|
-
}
|
|
62
|
-
/** 创建插件域渠道:余段即插件名 */
|
|
2
|
+
import { knownTitle, PLUGIN_SPECS, PLUGIN_MARKETPLACE } from './management-catalog.js';
|
|
3
|
+
import { isObject, errorCode } from './machine.js';
|
|
4
|
+
import { json, object, restOf, commandReceipt, unchanged } from './model.js';
|
|
5
|
+
import { jsonFingerprint, frozenJsonWork } from './frozen-json.js';
|
|
6
|
+
import { step } from './work.js';
|
|
7
|
+
const names = Object.keys(PLUGIN_SPECS);
|
|
8
|
+
/**
|
|
9
|
+
* 管理已知 Claude 插件的用户级安装和启用状态
|
|
10
|
+
*
|
|
11
|
+
* @remarks 安装使用绑定的 Claude 命令,拒绝同名市场来源冲突,不管理访问令牌
|
|
12
|
+
* 完成须确认文件和启用声明;更新还须命令成功退出
|
|
13
|
+
* 停用只写设置并保留文件,不依赖 Claude 命令
|
|
14
|
+
*/
|
|
63
15
|
export function createPluginsDomain(ctx) {
|
|
64
16
|
return {
|
|
65
|
-
inspect
|
|
66
|
-
|
|
17
|
+
async inspect() {
|
|
18
|
+
const items = [];
|
|
19
|
+
for (const name of names) {
|
|
20
|
+
const facts = [];
|
|
21
|
+
const item = {
|
|
22
|
+
id: 'plugin:' + name,
|
|
23
|
+
title: knownTitle('plugin:' + name, name),
|
|
24
|
+
kind: 'plugin',
|
|
25
|
+
location: ctx.settingsFile,
|
|
26
|
+
canInstall: true,
|
|
27
|
+
canUninstall: false,
|
|
28
|
+
facts,
|
|
29
|
+
};
|
|
30
|
+
try {
|
|
31
|
+
item.canUninstall = (await readEnabled(ctx, name)) === true;
|
|
32
|
+
facts.push({ code: item.canUninstall ? 'declared-enabled' : 'declared-disabled' });
|
|
33
|
+
const current = await observeInstallation(ctx, name);
|
|
34
|
+
facts.unshift({ code: current.ready ? 'plugin-installed' : 'plugin-not-installed' });
|
|
35
|
+
items.push(item);
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
items.push({ ...item, problem: errorCode(error) });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return { items, problems: [] };
|
|
42
|
+
},
|
|
43
|
+
async prepare(selection, claude) {
|
|
67
44
|
const name = restOf(selection.id);
|
|
68
|
-
if (name
|
|
45
|
+
if (!name || !names.includes(name))
|
|
69
46
|
throw new Error('unknown-selection');
|
|
70
|
-
return preparePlugin(ctx, name, selection,
|
|
47
|
+
return preparePlugin(ctx, name, selection, selection.action === 'detach' ? undefined : await claude?.());
|
|
71
48
|
},
|
|
72
49
|
};
|
|
73
50
|
}
|
|
74
|
-
async function
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
settingsProblem = errorCode(error);
|
|
87
|
-
problems.push(settingsProblem);
|
|
88
|
-
}
|
|
89
|
-
try {
|
|
90
|
-
records = await pluginRecords(ctx);
|
|
91
|
-
}
|
|
92
|
-
catch (error) {
|
|
93
|
-
recordsProblem = errorCode(error);
|
|
94
|
-
problems.push(recordsProblem);
|
|
95
|
-
}
|
|
96
|
-
try {
|
|
97
|
-
markets = await json(ctx.machine, path.join(ctx.claudeRoot, 'plugins/known_marketplaces.json'));
|
|
98
|
-
}
|
|
99
|
-
catch (error) {
|
|
100
|
-
marketsProblem = errorCode(error);
|
|
101
|
-
problems.push(marketsProblem);
|
|
51
|
+
async function preparePlugin(ctx, name, selection, claude) {
|
|
52
|
+
const detach = selection.action === 'detach';
|
|
53
|
+
if (detach) {
|
|
54
|
+
const enabled = await readEnabled(ctx, name);
|
|
55
|
+
if (enabled !== true)
|
|
56
|
+
throw new Error('no-connection');
|
|
57
|
+
return frozenJsonWork(ctx, {
|
|
58
|
+
file: ctx.settingsFile,
|
|
59
|
+
fields: [{ path: ['enabledPlugins', name], before: jsonFingerprint(enabled), value: false }],
|
|
60
|
+
changes: [{ code: 'plugin-disable', detail: name }, { code: 'keep-plugin-files' }],
|
|
61
|
+
effects: [{ state: 'done', code: 'plugin-disabled' }],
|
|
62
|
+
});
|
|
102
63
|
}
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
])
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
? hasFilesPresent
|
|
145
|
-
? ['update', 'reinstall', 'connect']
|
|
146
|
-
: ['install']
|
|
147
|
-
: []),
|
|
148
|
-
...(!settingsProblem && (enabled[name] === true || (enabled[name] === undefined && isInstalled))
|
|
149
|
-
? ['detach']
|
|
150
|
-
: []),
|
|
151
|
-
],
|
|
152
|
-
});
|
|
64
|
+
const before = await observe(ctx, name);
|
|
65
|
+
if (!claude)
|
|
66
|
+
throw new Error('claude-not-installed');
|
|
67
|
+
const hadMarket = await marketplace(ctx);
|
|
68
|
+
const expected = jsonFingerprint(before);
|
|
69
|
+
const verb = before.ready ? 'update' : 'install';
|
|
70
|
+
const install = step([{ code: before.ready ? 'plugin-update' : 'plugin-install', detail: name }, { code: 'new-session-required' }], async (fact) => {
|
|
71
|
+
if (!fact.dispatched)
|
|
72
|
+
await claude.verify();
|
|
73
|
+
await marketplace(ctx);
|
|
74
|
+
const current = await observe(ctx, name);
|
|
75
|
+
// 更新既有插件还需命令正常返回,避免未发生更新时把原有文件误判为本次成功
|
|
76
|
+
if (fact.dispatched)
|
|
77
|
+
return current.ready &&
|
|
78
|
+
current.enabled === true &&
|
|
79
|
+
(!before.ready || (fact.receipt?.kind === 'returned' && !fact.receipt.failure))
|
|
80
|
+
? {
|
|
81
|
+
state: 'complete',
|
|
82
|
+
effects: [{ state: 'done', code: 'plugin-ready', detail: name }],
|
|
83
|
+
}
|
|
84
|
+
: { state: 'blocked', problem: fact.receipt?.failure ?? 'plugin-not-ready' };
|
|
85
|
+
unchanged(jsonFingerprint(current), expected);
|
|
86
|
+
return { state: 'ready' };
|
|
87
|
+
}, async () => {
|
|
88
|
+
unchanged(jsonFingerprint(await observe(ctx, name)), expected);
|
|
89
|
+
if (!(await marketplace(ctx))) {
|
|
90
|
+
if (hadMarket)
|
|
91
|
+
throw new Error('marketplace-source-conflict');
|
|
92
|
+
const added = await claude.execute([
|
|
93
|
+
'plugin',
|
|
94
|
+
'marketplace',
|
|
95
|
+
'add',
|
|
96
|
+
PLUGIN_MARKETPLACE.repository,
|
|
97
|
+
'--scope',
|
|
98
|
+
'user',
|
|
99
|
+
]);
|
|
100
|
+
const receipt = commandReceipt(added, 'marketplace-add-failed');
|
|
101
|
+
if (receipt.failure)
|
|
102
|
+
return { receipt };
|
|
103
|
+
if (!(await marketplace(ctx)))
|
|
104
|
+
throw new Error('marketplace-source-conflict');
|
|
153
105
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
kind: 'plugin',
|
|
159
|
-
facts: [],
|
|
160
|
-
actions: enabled[name] === true ? ['detach'] : [],
|
|
161
|
-
problem: errorCode(error),
|
|
162
|
-
});
|
|
106
|
+
const result = await claude.execute(['plugin', verb, name, '--scope', 'user']);
|
|
107
|
+
if (before.enabled !== true && verb === 'update' && result.code === 0 && !result.failed) {
|
|
108
|
+
const enabled = await claude.execute(['plugin', 'enable', name, '--scope', 'user']);
|
|
109
|
+
return { receipt: commandReceipt(enabled, 'plugin-command-failed') };
|
|
163
110
|
}
|
|
164
|
-
|
|
165
|
-
|
|
111
|
+
return { receipt: commandReceipt(result, 'plugin-command-failed') };
|
|
112
|
+
});
|
|
113
|
+
return { ...install, action: verb };
|
|
166
114
|
}
|
|
167
|
-
async function
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
if (
|
|
183
|
-
throw
|
|
184
|
-
|
|
185
|
-
const settings = await json(ctx.machine, ctx.settingsFile);
|
|
186
|
-
const before = object(settings.enabledPlugins);
|
|
187
|
-
if (action === 'detach') {
|
|
188
|
-
const installed = before[name] === true ? null : userPluginRecords((await pluginRecords(ctx))[name]);
|
|
189
|
-
if (before[name] !== true && (before[name] !== undefined || !installed?.length))
|
|
190
|
-
throw new Error('no-connection');
|
|
191
|
-
describe?.(pluginTitle(name, markets, before[name] !== undefined || (installed?.length ?? 0) > 0));
|
|
192
|
-
return {
|
|
193
|
-
writes: [ctx.settingsFile],
|
|
194
|
-
changes: [{ code: 'plugin-disable', detail: name }, { code: 'keep-plugin-files' }],
|
|
195
|
-
async run(report) {
|
|
196
|
-
if (installed !== null)
|
|
197
|
-
unchanged(userPluginRecords((await pluginRecords(ctx))[name]), installed);
|
|
198
|
-
await editFields(ctx, ctx.settingsFile, before, { [name]: false }, 'enabledPlugins');
|
|
199
|
-
report({ state: 'done', code: 'plugin-disabled', detail: name });
|
|
200
|
-
},
|
|
201
|
-
};
|
|
115
|
+
async function observe(ctx, name) {
|
|
116
|
+
return { enabled: await readEnabled(ctx, name), ...(await observeInstallation(ctx, name)) };
|
|
117
|
+
}
|
|
118
|
+
async function readEnabled(ctx, name) {
|
|
119
|
+
return object((await json(ctx.machine, ctx.settingsFile)).enabledPlugins)[name];
|
|
120
|
+
}
|
|
121
|
+
async function observeInstallation(ctx, name) {
|
|
122
|
+
const value = object((await json(ctx.machine, path.join(ctx.claudeRoot, 'plugins/installed_plugins.json'))).plugins)[name];
|
|
123
|
+
if (value !== undefined && !Array.isArray(value))
|
|
124
|
+
throw new Error('invalid-plugin-record');
|
|
125
|
+
if ((value ?? []).some((record) => !isObject(record) || typeof record.scope !== 'string'))
|
|
126
|
+
throw new Error('invalid-plugin-record');
|
|
127
|
+
const records = (value ?? []).filter((record) => isObject(record) && record.scope === 'user');
|
|
128
|
+
const files = [];
|
|
129
|
+
for (const record of records) {
|
|
130
|
+
if (!isObject(record) || typeof record.installPath !== 'string')
|
|
131
|
+
throw new Error('invalid-plugin-record');
|
|
132
|
+
files.push(await ctx.machine.treeStamp(record.installPath));
|
|
202
133
|
}
|
|
203
|
-
|
|
134
|
+
return { records, files, ready: records.length > 0 && files.every(stamp => stamp !== null) };
|
|
135
|
+
}
|
|
136
|
+
async function marketplace(ctx) {
|
|
137
|
+
const value = (await json(ctx.machine, path.join(ctx.claudeRoot, 'plugins/known_marketplaces.json')))[PLUGIN_MARKETPLACE.market];
|
|
138
|
+
if (value === undefined)
|
|
139
|
+
return false;
|
|
140
|
+
const source = object(object(value).source);
|
|
141
|
+
const repo = typeof source.repo === 'string'
|
|
142
|
+
? source.repo
|
|
143
|
+
: typeof source.url === 'string'
|
|
144
|
+
? source.url.replace(/^https:\/\/github.com\//, '').replace(/\.git$/, '')
|
|
145
|
+
: '';
|
|
146
|
+
if (!PLUGIN_MARKETPLACE.acceptedRepositories.some(accepted => accepted === repo))
|
|
204
147
|
throw new Error('marketplace-source-conflict');
|
|
205
|
-
|
|
206
|
-
describe?.(pluginTitle(name, markets, records.length > 0 || before[name] !== undefined));
|
|
207
|
-
const canReuse = action === 'install' &&
|
|
208
|
-
records.length > 0 &&
|
|
209
|
-
(await Promise.all(records.map(record => pluginFilePresent(ctx, record)))).every(Boolean);
|
|
210
|
-
const help = action === 'reinstall' ? await claudeCommand(ctx, ['plugin', 'uninstall', '--help']) : null;
|
|
211
|
-
// 探测失败保持未知:不以「不支持保留」代替,阻断重装以免官方卸载删除插件数据
|
|
212
|
-
if (help !== null && (help.failed || help.code !== 0))
|
|
213
|
-
throw new Error('plugin-keep-data-unconfirmed');
|
|
214
|
-
const shouldKeepData = help !== null && help.stdout.includes('--keep-data');
|
|
215
|
-
let isRemoved = false;
|
|
216
|
-
let isCommandDone = false;
|
|
217
|
-
let hasCommandSnapshot = false;
|
|
218
|
-
let isCredentialSaved = false;
|
|
219
|
-
let expectedRecords = records;
|
|
220
|
-
let expectedEnabled = before[name];
|
|
221
|
-
const validateSource = async () => {
|
|
222
|
-
const current = pluginSource(name, await json(ctx.machine, marketplaceFile));
|
|
223
|
-
if (current === null || current.marketName !== source.marketName || current.repository !== source.repository)
|
|
224
|
-
throw new Error('marketplace-source-conflict');
|
|
225
|
-
};
|
|
226
|
-
return {
|
|
227
|
-
get changes() {
|
|
228
|
-
return [
|
|
229
|
-
{ code: isCommandDone ? 'plugin-confirm' : canReuse ? 'plugin-reuse' : `plugin-${action}`, detail: name },
|
|
230
|
-
...(!isCredentialSaved && selection.fields?.secret && name.startsWith('github@')
|
|
231
|
-
? [{ code: 'github-credential-configure' }]
|
|
232
|
-
: []),
|
|
233
|
-
...(action === 'reinstall' && !isRemoved
|
|
234
|
-
? [
|
|
235
|
-
{
|
|
236
|
-
code: shouldKeepData ? 'plugin-keep-data' : 'plugin-delete-own-data',
|
|
237
|
-
},
|
|
238
|
-
{ code: 'plugin-credentials-removed' },
|
|
239
|
-
]
|
|
240
|
-
: [{ code: 'new-session-required' }]),
|
|
241
|
-
];
|
|
242
|
-
},
|
|
243
|
-
async run(report, signal) {
|
|
244
|
-
cancelled(signal);
|
|
245
|
-
if (isCommandDone && !hasCommandSnapshot)
|
|
246
|
-
throw new Error('program-installation-unconfirmed');
|
|
247
|
-
await validateSource();
|
|
248
|
-
unchanged(object((await json(ctx.machine, ctx.settingsFile)).enabledPlugins)[name], expectedEnabled);
|
|
249
|
-
unchanged(userPluginRecords((await pluginRecords(ctx))[name]), expectedRecords);
|
|
250
|
-
if (action === 'reinstall' && !isRemoved) {
|
|
251
|
-
success(await claudeCommand(ctx, [
|
|
252
|
-
'plugin',
|
|
253
|
-
'uninstall',
|
|
254
|
-
name,
|
|
255
|
-
'--scope',
|
|
256
|
-
'user',
|
|
257
|
-
...(shouldKeepData ? ['--keep-data'] : []),
|
|
258
|
-
]), 'plugin-command-failed');
|
|
259
|
-
if (userPluginRecords((await pluginRecords(ctx))[name]).length > 0 ||
|
|
260
|
-
object((await json(ctx.machine, ctx.settingsFile)).enabledPlugins)[name] === true)
|
|
261
|
-
throw new Error('plugin-remains');
|
|
262
|
-
expectedRecords = userPluginRecords((await pluginRecords(ctx))[name]);
|
|
263
|
-
expectedEnabled = object((await json(ctx.machine, ctx.settingsFile)).enabledPlugins)[name];
|
|
264
|
-
isRemoved = true;
|
|
265
|
-
report({ state: 'done', code: 'plugin-removed' });
|
|
266
|
-
cancelled(signal);
|
|
267
|
-
}
|
|
268
|
-
if (!isCommandDone && (action === 'install' || action === 'reinstall')) {
|
|
269
|
-
await validateSource();
|
|
270
|
-
const markets = await json(ctx.machine, marketplaceFile);
|
|
271
|
-
if (markets[source.marketName] === undefined)
|
|
272
|
-
success(await claudeCommand(ctx, ['plugin', 'marketplace', 'add', source.repository]), 'marketplace-command-failed');
|
|
273
|
-
}
|
|
274
|
-
const verb = action === 'connect' || canReuse ? 'enable' : action === 'update' ? 'update' : 'install';
|
|
275
|
-
await validateSource();
|
|
276
|
-
if (!isCommandDone) {
|
|
277
|
-
success(await claudeCommand(ctx, ['plugin', verb, name, '--scope', 'user']), 'plugin-command-failed');
|
|
278
|
-
isCommandDone = true;
|
|
279
|
-
report({ state: 'done', code: 'plugin-command-completed' });
|
|
280
|
-
try {
|
|
281
|
-
expectedRecords = userPluginRecords((await pluginRecords(ctx))[name]);
|
|
282
|
-
expectedEnabled = object((await json(ctx.machine, ctx.settingsFile)).enabledPlugins)[name];
|
|
283
|
-
hasCommandSnapshot = true;
|
|
284
|
-
}
|
|
285
|
-
catch (error) {
|
|
286
|
-
throw new Error('program-installation-unconfirmed', { cause: error });
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
if (name.startsWith('github@') && selection.fields?.secret) {
|
|
290
|
-
if (!isCredentialSaved)
|
|
291
|
-
await editFields(ctx, ctx.settingsFile, object(settings.env), { GITHUB_PERSONAL_ACCESS_TOKEN: selection.fields.secret }, 'env', () => {
|
|
292
|
-
isCredentialSaved = true;
|
|
293
|
-
report({ state: 'done', code: 'github-credential-saved' });
|
|
294
|
-
});
|
|
295
|
-
else
|
|
296
|
-
unchanged(object((await json(ctx.machine, ctx.settingsFile)).env).GITHUB_PERSONAL_ACCESS_TOKEN, selection.fields.secret);
|
|
297
|
-
}
|
|
298
|
-
const after = userPluginRecords((await pluginRecords(ctx))[name]);
|
|
299
|
-
if (after.length === 0)
|
|
300
|
-
throw new Error('plugin-not-ready');
|
|
301
|
-
for (const record of after)
|
|
302
|
-
if (!(await pluginFilePresent(ctx, record)))
|
|
303
|
-
throw new Error('plugin-not-ready');
|
|
304
|
-
if (action !== 'update' && object((await json(ctx.machine, ctx.settingsFile)).enabledPlugins)[name] !== true)
|
|
305
|
-
throw new Error('plugin-not-enabled');
|
|
306
|
-
report({ state: 'done', code: 'plugin-ready' });
|
|
307
|
-
},
|
|
308
|
-
};
|
|
148
|
+
return true;
|
|
309
149
|
}
|