@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/officecli.js
CHANGED
|
@@ -1,437 +1,307 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
|
-
import { knownTitle } from './
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
2
|
+
import { knownTitle, OFFICECLI_REPO_SLUG, OFFICE_INTEGRATION } from './management-catalog.js';
|
|
3
|
+
import { sourcesFile, currentSkill, prepareSkillFiles, skillFilesStamp, disableSkillEntry, repairSkillSource, ownsSkill, checkSkillSource, } from './skill-files.js';
|
|
4
|
+
import { isGitRevision, isObject, errorCode, succeeded } from './machine.js';
|
|
5
|
+
import { createHash } from 'node:crypto';
|
|
6
|
+
import { json, userBinDirectory } from './model.js';
|
|
7
|
+
import { isExactVersion, compareVersions } from './npm-registry.js';
|
|
8
|
+
import { samePath } from './program-channels.js';
|
|
9
|
+
import { removeProgramFile, deployProgram, programPath } from './program-operations.js';
|
|
10
|
+
import { step, sequence, work } from './work.js';
|
|
11
|
+
const PROGRAM_ACTION_CHANGE = { install: 'program-install', update: 'program-update' };
|
|
12
|
+
const CHECKSUM_DOWNLOAD_TIMEOUT_MS = 10_000;
|
|
13
|
+
const PROGRAM_DOWNLOAD_TIMEOUT_MS = 30_000;
|
|
14
|
+
const VERSION_PROBE_TIMEOUT_MS = 5_000;
|
|
15
|
+
const MAX_TAG_DEREFERENCES = 4;
|
|
16
|
+
/**
|
|
17
|
+
* 管理 OfficeCLI 原生程序和配套技能
|
|
18
|
+
*
|
|
19
|
+
* @remarks 仅管理固定用户目录;按发布摘要及技能提交安装,摘要匹配时复用程序
|
|
20
|
+
* 拒绝降级、来源不明或覆盖本地技能改动
|
|
21
|
+
* 卸载移除程序并保留自有技能副本,不动其他技能和用户数据
|
|
22
|
+
*/
|
|
14
23
|
export function createOfficecli(ctx) {
|
|
15
|
-
const directory = path.join(ctx.claudeRoot, 'skills',
|
|
16
|
-
async function commandOwner(command, found) {
|
|
17
|
-
const candidates = path.isAbsolute(command)
|
|
18
|
-
? [command]
|
|
19
|
-
: command === 'officecli' || command === 'officecli.exe'
|
|
20
|
-
? await ctx.machine.locate('officecli')
|
|
21
|
-
: [];
|
|
22
|
-
const candidate = candidates[0];
|
|
23
|
-
if (candidate === undefined)
|
|
24
|
-
return undefined;
|
|
25
|
-
for (const [id, program] of found.values)
|
|
26
|
-
if (samePath(ctx, candidate, program.binary))
|
|
27
|
-
return id;
|
|
28
|
-
const canonical = await ctx.machine.realpath(candidate);
|
|
29
|
-
if (canonical === null)
|
|
30
|
-
return undefined;
|
|
31
|
-
for (const [id, program] of found.values) {
|
|
32
|
-
if (samePath(ctx, candidate, program.binary) || samePath(ctx, canonical, program.binary))
|
|
33
|
-
return id;
|
|
34
|
-
if (program.channel !== 'npm')
|
|
35
|
-
continue;
|
|
36
|
-
const packageRoot = path.dirname(path.dirname(program.binary));
|
|
37
|
-
const normalized = ctx.platform === 'win32' ? canonical.toLowerCase() : canonical;
|
|
38
|
-
const prefix = ctx.platform === 'win32' ? packageRoot.toLowerCase() : packageRoot;
|
|
39
|
-
if (normalized.startsWith(`${prefix}${path.sep}`))
|
|
40
|
-
return id;
|
|
41
|
-
if (program.binary.startsWith(path.join(path.dirname(candidate), 'node_modules', '@officecli', 'officecli') + path.sep))
|
|
42
|
-
return id;
|
|
43
|
-
}
|
|
44
|
-
return undefined;
|
|
45
|
-
}
|
|
46
|
-
async function observe(found) {
|
|
47
|
-
found ??= await programs(ctx, 'officecli');
|
|
48
|
-
const problems = found.problems.map(code => ({ code, detail: 'officecli' }));
|
|
49
|
-
let isConnectionsUnknown = false;
|
|
50
|
-
let skillEvidence = 'none';
|
|
51
|
-
let skillStamp = null;
|
|
52
|
-
let mcpCommand;
|
|
53
|
-
let registration;
|
|
54
|
-
let metadata = {};
|
|
55
|
-
try {
|
|
56
|
-
metadata = await json(ctx.machine, path.join(ctx.storage, 'sources.json'));
|
|
57
|
-
}
|
|
58
|
-
catch {
|
|
59
|
-
problems.push({ code: 'source-metadata-unavailable', detail: 'officecli' });
|
|
60
|
-
}
|
|
61
|
-
try {
|
|
62
|
-
skillStamp = await ctx.machine.treeStamp(directory);
|
|
63
|
-
const text = skillStamp !== null ? await ctx.machine.read(path.join(directory, 'SKILL.md')) : null;
|
|
64
|
-
skillEvidence = officeSkillEvidence(text, metadata[directory], skillStamp);
|
|
65
|
-
}
|
|
66
|
-
catch (error) {
|
|
67
|
-
isConnectionsUnknown = true;
|
|
68
|
-
problems.push({ code: errorCode(error), detail: directory });
|
|
69
|
-
}
|
|
70
|
-
try {
|
|
71
|
-
registration = object((await json(ctx.machine, ctx.stateFile)).mcpServers).officecli;
|
|
72
|
-
mcpCommand = officeMcpCommand(registration);
|
|
73
|
-
}
|
|
74
|
-
catch (error) {
|
|
75
|
-
isConnectionsUnknown = true;
|
|
76
|
-
problems.push({ code: errorCode(error), detail: ctx.stateFile });
|
|
77
|
-
}
|
|
78
|
-
const entries = [];
|
|
79
|
-
// 无程序时只认领有来源记录的残留技能入口;仅凭 frontmatter 同名不认领用户自建内容
|
|
80
|
-
if (skillEvidence !== 'none' && (found.values.size > 0 || skillEvidence === 'confirmed'))
|
|
81
|
-
entries.push({
|
|
82
|
-
id: 'skill:officecli',
|
|
83
|
-
owner: found.values.size === 0 ? null : await commandOwner('officecli', found),
|
|
84
|
-
});
|
|
85
|
-
if (mcpCommand !== undefined)
|
|
86
|
-
entries.push({
|
|
87
|
-
id: 'mcp:officecli',
|
|
88
|
-
owner: found.values.size === 0 ? null : await commandOwner(mcpCommand, found),
|
|
89
|
-
});
|
|
90
|
-
return {
|
|
91
|
-
found,
|
|
92
|
-
entries,
|
|
93
|
-
problems,
|
|
94
|
-
skillEvidence,
|
|
95
|
-
skillStamp,
|
|
96
|
-
sourceRecord: metadata[directory],
|
|
97
|
-
mcpCommand,
|
|
98
|
-
registration,
|
|
99
|
-
isConnectionsUnknown,
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
function related(observed, id) {
|
|
103
|
-
return observed.entries.filter(entry => entry.owner === id);
|
|
104
|
-
}
|
|
105
|
-
async function detach(entries, action) {
|
|
106
|
-
if (entries.length === 0)
|
|
107
|
-
throw new Error('no-connection');
|
|
108
|
-
const operations = [];
|
|
109
|
-
for (const entry of entries) {
|
|
110
|
-
const operation = entry.id === 'skill:officecli'
|
|
111
|
-
? await prepareSkill(ctx, 'officecli', 'skills', { id: entry.id, action: 'detach' })
|
|
112
|
-
: await prepareMcp(ctx, 'officecli', { id: entry.id, action: 'detach' });
|
|
113
|
-
let isComplete = false;
|
|
114
|
-
const verify = async () => {
|
|
115
|
-
if (!isComplete || action === 'reinstall')
|
|
116
|
-
return;
|
|
117
|
-
if (entry.id === 'skill:officecli')
|
|
118
|
-
unchanged(await ctx.machine.treeStamp(directory), null);
|
|
119
|
-
else
|
|
120
|
-
unchanged(object((await json(ctx.machine, ctx.stateFile)).mcpServers).officecli, undefined);
|
|
121
|
-
};
|
|
122
|
-
const step = {
|
|
123
|
-
...(operation.writes ? { writes: operation.writes } : {}),
|
|
124
|
-
changes: operation.changes,
|
|
125
|
-
async run(report, signal) {
|
|
126
|
-
cancelled(signal);
|
|
127
|
-
if (isComplete) {
|
|
128
|
-
await verify();
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
await operation.run(report, signal);
|
|
132
|
-
isComplete = true;
|
|
133
|
-
},
|
|
134
|
-
remaining: () => (isComplete ? undefined : step),
|
|
135
|
-
verify,
|
|
136
|
-
};
|
|
137
|
-
operations.push(step);
|
|
138
|
-
}
|
|
139
|
-
return sequence(operations);
|
|
140
|
-
}
|
|
24
|
+
const directory = path.join(ctx.claudeRoot, 'skills', 'officecli');
|
|
141
25
|
return {
|
|
142
26
|
async inspect() {
|
|
143
|
-
const
|
|
144
|
-
const items = observed.found.items.map(item => {
|
|
145
|
-
const entries = related(observed, item.id);
|
|
146
|
-
const program = observed.found.values.get(item.id);
|
|
147
|
-
const problem = observed.isConnectionsUnknown ? 'installation-state-unconfirmed' : undefined;
|
|
148
|
-
return {
|
|
149
|
-
...item,
|
|
150
|
-
facts: [
|
|
151
|
-
...item.facts,
|
|
152
|
-
...(program?.channel === 'native' || program?.channel === 'manual'
|
|
153
|
-
? [{ code: 'program-identity-unchecked' }]
|
|
154
|
-
: []),
|
|
155
|
-
...connectionFacts(entries),
|
|
156
|
-
],
|
|
157
|
-
actions: [...item.actions, ...(entries.length ? ['detach'] : [])],
|
|
158
|
-
...(problem ? { problem } : {}),
|
|
159
|
-
};
|
|
160
|
-
});
|
|
161
|
-
if (items.length === 0) {
|
|
162
|
-
const entries = related(observed, null);
|
|
163
|
-
items.push({
|
|
164
|
-
id: 'officecli',
|
|
165
|
-
title: knownTitle('officecli'),
|
|
166
|
-
kind: 'officecli',
|
|
167
|
-
canInstall: true,
|
|
168
|
-
facts: [
|
|
169
|
-
{ code: observed.found.problems.length ? 'inspection-incomplete' : 'not-installed' },
|
|
170
|
-
...connectionFacts(entries),
|
|
171
|
-
],
|
|
172
|
-
actions: ['install', ...(entries.length ? ['detach'] : [])],
|
|
173
|
-
...(observed.isConnectionsUnknown ? { problem: 'installation-state-unconfirmed' } : {}),
|
|
174
|
-
});
|
|
175
|
-
}
|
|
27
|
+
const found = await officePrograms(ctx);
|
|
176
28
|
return {
|
|
177
|
-
items
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
29
|
+
items: found.items.length
|
|
30
|
+
? found.items
|
|
31
|
+
: [
|
|
32
|
+
{
|
|
33
|
+
id: 'officecli',
|
|
34
|
+
title: knownTitle('officecli'),
|
|
35
|
+
kind: 'officecli',
|
|
36
|
+
location: officeBinary(ctx),
|
|
37
|
+
canInstall: true,
|
|
38
|
+
facts: [{ code: 'not-installed' }],
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
problems: [],
|
|
182
42
|
};
|
|
183
43
|
},
|
|
184
44
|
async prepare(selection) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
const
|
|
199
|
-
|
|
200
|
-
|
|
45
|
+
if (selection.id !== 'officecli')
|
|
46
|
+
throw new Error('channel-not-supported');
|
|
47
|
+
const found = await officePrograms(ctx);
|
|
48
|
+
const program = found.values.get('officecli');
|
|
49
|
+
if (!program && found.values.size)
|
|
50
|
+
throw new Error('channel-not-supported');
|
|
51
|
+
let metadata;
|
|
52
|
+
try {
|
|
53
|
+
metadata = await json(ctx.machine, sourcesFile(ctx));
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
throw new Error('source-metadata-unavailable');
|
|
57
|
+
}
|
|
58
|
+
const before = await ctx.machine.treeStamp(directory);
|
|
59
|
+
const record = metadata[directory];
|
|
60
|
+
const owned = ownsSkill(record, OFFICE_INTEGRATION.skill.repository, before);
|
|
61
|
+
if (selection.action !== 'uninstall' && before !== null)
|
|
62
|
+
checkSkillSource(record, OFFICE_INTEGRATION.skill.repository);
|
|
201
63
|
if (selection.action === 'uninstall') {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
},
|
|
212
|
-
]);
|
|
213
|
-
const removal = await prepareProgram(ctx, 'officecli', selection, program);
|
|
214
|
-
const belongs = (value, entry) => entry.owner === identity ||
|
|
215
|
-
(entry.owner === null && value.found.values.size === 0) ||
|
|
216
|
-
(entry.id === 'mcp:officecli' &&
|
|
217
|
-
isObject(value.registration) &&
|
|
218
|
-
typeof value.registration.command === 'string' &&
|
|
219
|
-
path.isAbsolute(value.registration.command) &&
|
|
220
|
-
samePath(ctx, value.registration.command, program.binary));
|
|
221
|
-
const scope = (value) => value.entries.filter(entry => belongs(value, entry));
|
|
222
|
-
const expected = scope(observed);
|
|
223
|
-
const unmatched = [
|
|
224
|
-
...(observed.skillStamp !== null &&
|
|
225
|
-
(observed.skillEvidence === 'none' ||
|
|
226
|
-
observed.entries.some(entry => entry.id === 'skill:officecli' && entry.owner === undefined))
|
|
227
|
-
? [directory]
|
|
228
|
-
: []),
|
|
229
|
-
...(observed.registration !== undefined &&
|
|
230
|
-
(observed.mcpCommand === undefined ||
|
|
231
|
-
observed.entries.some(entry => entry.id === 'mcp:officecli' && entry.owner === undefined))
|
|
232
|
-
? ['officecli']
|
|
233
|
-
: []),
|
|
234
|
-
];
|
|
235
|
-
const preserved = {
|
|
236
|
-
changes: unmatched.map(detail => ({ code: 'unmatched-integration-kept', detail })),
|
|
237
|
-
run(report) {
|
|
238
|
-
for (const detail of unmatched)
|
|
239
|
-
report({ state: 'kept', code: 'unmatched-integration-kept', detail });
|
|
240
|
-
return Promise.resolve();
|
|
241
|
-
},
|
|
242
|
-
};
|
|
243
|
-
const guard = (hasDetached) => {
|
|
244
|
-
const verify = async () => {
|
|
245
|
-
const current = await observe(observed.found);
|
|
246
|
-
if (current.isConnectionsUnknown)
|
|
247
|
-
throw new Error('installation-state-unconfirmed');
|
|
248
|
-
const actual = scope(current);
|
|
249
|
-
unchanged(actual.map(entry => entry.id), hasDetached ? [] : expected.map(entry => entry.id));
|
|
250
|
-
if (!hasDetached)
|
|
251
|
-
for (const entry of expected)
|
|
252
|
-
unchanged(entry.id === 'skill:officecli' ? current.skillStamp : current.registration, entry.id === 'skill:officecli' ? observed.skillStamp : observed.registration);
|
|
253
|
-
};
|
|
254
|
-
return { changes: [], run: verify, ...(hasDetached ? { verify } : {}) };
|
|
255
|
-
};
|
|
256
|
-
return sequence([
|
|
257
|
-
guard(false),
|
|
258
|
-
...(expected.length ? [await detach(expected, selection.action)] : []),
|
|
259
|
-
guard(true),
|
|
260
|
-
removal,
|
|
261
|
-
preserved,
|
|
262
|
-
]);
|
|
64
|
+
const operation = await prepareOfficeRemoval(ctx, program);
|
|
65
|
+
const detach = owned && before !== null ? disableSkillEntry(ctx, directory, before, record) : work([]);
|
|
66
|
+
const kept = step([], () => Promise.resolve({
|
|
67
|
+
state: 'complete',
|
|
68
|
+
effects: before !== null && !owned
|
|
69
|
+
? [{ state: 'kept', code: 'unmatched-integration-kept', detail: directory }]
|
|
70
|
+
: [],
|
|
71
|
+
}));
|
|
72
|
+
return work([sequence([detach, operation]), kept]);
|
|
263
73
|
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
const
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
(
|
|
272
|
-
|
|
273
|
-
const
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
targetVersion: version,
|
|
286
|
-
targetBinary: program.binary,
|
|
287
|
-
...(program.channel === 'npm' ? { targetRegistry: null } : {}),
|
|
288
|
-
changes: [{ code: 'program-kept' }],
|
|
289
|
-
async run(report) {
|
|
290
|
-
unchanged(await ctx.machine.treeStamp(program.binary), stamp);
|
|
291
|
-
if (program.channel === 'native')
|
|
292
|
-
await ctx.machine.ensureUserPath(path.dirname(program.binary));
|
|
293
|
-
report({ state: 'kept', code: 'program-kept' });
|
|
294
|
-
},
|
|
295
|
-
async verify() {
|
|
296
|
-
unchanged(await ctx.machine.treeStamp(program.binary), stamp);
|
|
297
|
-
},
|
|
298
|
-
}
|
|
299
|
-
: await prepareProgram(ctx, 'officecli', selection, program);
|
|
300
|
-
const binary = operation.targetBinary ?? program?.binary ?? officeBinary(ctx);
|
|
301
|
-
const connection = await prepareOfficeConnection(ctx, selection, observed, {
|
|
302
|
-
version: operation.targetVersion,
|
|
303
|
-
binary,
|
|
304
|
-
source: program?.channel === 'npm' ? 'npm' : 'native',
|
|
305
|
-
...(operation.targetRegistry !== undefined ? { registry: operation.targetRegistry } : {}),
|
|
306
|
-
});
|
|
307
|
-
const before = selection.action === 'reinstall' && entries.length ? await detach(entries, selection.action) : undefined;
|
|
308
|
-
return sequence([...(before ? [before] : []), operation, connection]);
|
|
74
|
+
const operation = await prepareOfficeNative(ctx, program);
|
|
75
|
+
const revision = await officeRevision(ctx, operation.targetVersion);
|
|
76
|
+
const files = await ctx.machine.source(OFFICE_INTEGRATION.skill.repository, revision, OFFICE_INTEGRATION.skill.folder);
|
|
77
|
+
const source = {
|
|
78
|
+
repository: OFFICE_INTEGRATION.skill.repository,
|
|
79
|
+
revision,
|
|
80
|
+
files,
|
|
81
|
+
stamp: skillFilesStamp(files),
|
|
82
|
+
};
|
|
83
|
+
const target = { name: 'officecli', directory, stamp: before, sourceRecord: record };
|
|
84
|
+
if (before !== null && !owned && before !== source.stamp)
|
|
85
|
+
throw new Error('local-edits-preserved');
|
|
86
|
+
const skill = before === source.stamp
|
|
87
|
+
? owned
|
|
88
|
+
? currentSkill(ctx, 'officecli', directory, before)
|
|
89
|
+
: repairSkillSource(ctx, target, source)
|
|
90
|
+
: prepareSkillFiles(ctx, target, source);
|
|
91
|
+
return {
|
|
92
|
+
...sequence([operation, skill]),
|
|
93
|
+
action: operation.action ?? 'install',
|
|
94
|
+
};
|
|
309
95
|
},
|
|
310
96
|
};
|
|
311
97
|
}
|
|
312
|
-
function
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
98
|
+
async function prepareOfficeRemoval(ctx, program) {
|
|
99
|
+
if (program?.channel === 'manual')
|
|
100
|
+
throw new Error('channel-not-supported');
|
|
101
|
+
if (!program || program.channel !== 'native')
|
|
102
|
+
throw new Error('program-not-installed');
|
|
103
|
+
if ((await officeVersion(ctx, program.binary)) === null)
|
|
104
|
+
throw new Error('program-identity-unconfirmed');
|
|
105
|
+
const targets = [
|
|
106
|
+
{ file: program.binary, stamp: await ctx.machine.treeStamp(program.binary) },
|
|
107
|
+
...(await officeUpdates(ctx, program.binary)),
|
|
318
108
|
];
|
|
109
|
+
return work(targets.map(target => removeProgramFile(ctx, { file: target.file, before: target.stamp }, [
|
|
110
|
+
{ code: 'program-uninstall', detail: target.file },
|
|
111
|
+
])));
|
|
319
112
|
}
|
|
320
|
-
function
|
|
321
|
-
if (
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
const
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
113
|
+
async function prepareOfficeNative(ctx, program) {
|
|
114
|
+
if (program?.channel === 'manual')
|
|
115
|
+
throw new Error('channel-not-supported');
|
|
116
|
+
const action = program ? 'update' : 'install';
|
|
117
|
+
const binary = program?.binary ?? officeBinary(ctx);
|
|
118
|
+
if (!samePath(ctx, binary, officeBinary(ctx)))
|
|
119
|
+
throw new Error('channel-not-supported');
|
|
120
|
+
const before = await ctx.machine.fingerprint(binary);
|
|
121
|
+
if (!program && before !== null)
|
|
122
|
+
throw new Error('program-changed');
|
|
123
|
+
const updates = await officeUpdates(ctx, binary);
|
|
124
|
+
const assetName = officeAsset(ctx);
|
|
125
|
+
const release = JSON.parse(await ctx.machine.fetchText(`https://api.github.com/repos/${OFFICECLI_REPO_SLUG}/releases/latest`));
|
|
126
|
+
if (!isObject(release) ||
|
|
127
|
+
release.draft !== false ||
|
|
128
|
+
release.prerelease !== false ||
|
|
129
|
+
typeof release.tag_name !== 'string' ||
|
|
130
|
+
!/^v\d+\.\d+\.\d+$/.test(release.tag_name) ||
|
|
131
|
+
!Array.isArray(release.assets))
|
|
132
|
+
throw new Error('invalid-release');
|
|
133
|
+
const tag = release.tag_name;
|
|
134
|
+
const targetVersion = tag.slice(1);
|
|
135
|
+
const baseUrl = `https://github.com/${OFFICECLI_REPO_SLUG}/releases/download/${tag}/`;
|
|
136
|
+
const assets = release.assets.filter(isObject);
|
|
137
|
+
const asset = assets.find(value => value.name === assetName);
|
|
138
|
+
const manifest = assets.find(value => value.name === 'SHA256SUMS');
|
|
139
|
+
if (!asset ||
|
|
140
|
+
asset.browser_download_url !== `${baseUrl}${assetName}` ||
|
|
141
|
+
!manifest ||
|
|
142
|
+
manifest.browser_download_url !== `${baseUrl}SHA256SUMS`)
|
|
143
|
+
throw new Error('release-asset-unavailable');
|
|
144
|
+
const fetchReleaseFile = async (name, read, timeoutMs) => {
|
|
145
|
+
try {
|
|
146
|
+
return await read(`${baseUrl}${name}`, { timeoutMs });
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
if (errorCode(error) === 'cancelled')
|
|
150
|
+
throw error;
|
|
151
|
+
// 上游官方镜像只作为传输备选,标签、文件名与摘要仍由 GitHub 发布约束
|
|
152
|
+
return read(`${OFFICE_INTEGRATION.releaseMirror}/releases/download/${tag}/${name}`);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
const checksums = await fetchReleaseFile('SHA256SUMS', (url, options) => ctx.machine.fetchText(url, options), CHECKSUM_DOWNLOAD_TIMEOUT_MS);
|
|
156
|
+
const matching = checksums
|
|
157
|
+
.split(/\r?\n/)
|
|
158
|
+
.map(line => line.trim().split(/\s+/))
|
|
159
|
+
.filter(parts => parts[1]?.replace(/^\*/, '') === assetName);
|
|
160
|
+
const digest = matching.length === 1 ? matching[0]?.[0]?.toLowerCase() : undefined;
|
|
161
|
+
if (digest === undefined ||
|
|
162
|
+
!/^[a-f0-9]{64}$/.test(digest) ||
|
|
163
|
+
(asset.digest !== undefined && asset.digest !== null && asset.digest !== `sha256:${digest}`))
|
|
164
|
+
throw new Error('release-checksum-unavailable');
|
|
165
|
+
if (before !== null) {
|
|
166
|
+
const currentVersion = await officeVersion(ctx, binary);
|
|
167
|
+
if (currentVersion === null)
|
|
168
|
+
throw new Error('program-version-unconfirmed');
|
|
169
|
+
if (compareVersions(currentVersion, targetVersion) > 0)
|
|
170
|
+
throw new Error('newer-version-preserved');
|
|
357
171
|
}
|
|
358
|
-
|
|
172
|
+
// 完整摘要与发布原件一致才复用,版本号相同但内容不同仍需下载校验
|
|
173
|
+
const shouldKeepBinary = action === 'update' && before === digest;
|
|
174
|
+
const bytes = shouldKeepBinary
|
|
175
|
+
? null
|
|
176
|
+
: await fetchReleaseFile(assetName, (url, options) => ctx.machine.fetchBytes(url, options), PROGRAM_DOWNLOAD_TIMEOUT_MS);
|
|
177
|
+
if (bytes !== null && createHash('sha256').update(bytes).digest('hex') !== digest)
|
|
178
|
+
throw new Error('release-checksum-mismatch');
|
|
179
|
+
const removals = updates.map(target => removeProgramFile(ctx, { file: target.file, before: target.stamp }, target.stamp === null ? [] : [{ code: 'program-copy-remove', detail: target.file }]));
|
|
180
|
+
const deploy = deployProgram(ctx, { binary, before, digest, bytes }, [
|
|
181
|
+
{
|
|
182
|
+
code: shouldKeepBinary ? 'program-kept' : PROGRAM_ACTION_CHANGE[action],
|
|
183
|
+
detail: 'OfficeCLI · native · ' + targetVersion + ' · ' + binary,
|
|
184
|
+
},
|
|
185
|
+
]);
|
|
186
|
+
const ready = step([], async () => {
|
|
187
|
+
if ((await ctx.machine.fingerprint(binary)) !== digest || (await officeVersion(ctx, binary)) !== targetVersion)
|
|
188
|
+
throw new Error('program-not-ready');
|
|
189
|
+
return { state: 'complete', effects: [{ state: 'done', code: 'program-ready' }] };
|
|
190
|
+
});
|
|
191
|
+
const userPath = programPath(ctx, path.dirname(binary), [
|
|
192
|
+
{ code: 'program-user-path', detail: path.dirname(binary) },
|
|
193
|
+
{ code: 'keep-user-data' },
|
|
194
|
+
]);
|
|
195
|
+
return {
|
|
196
|
+
...sequence([...(removals.length ? [work(removals)] : []), deploy, ready, userPath]),
|
|
197
|
+
action,
|
|
198
|
+
targetVersion,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
async function officeRevision(ctx, version) {
|
|
202
|
+
if (!isExactVersion(version))
|
|
359
203
|
throw new Error('source-version-unavailable');
|
|
360
|
-
const reference = JSON.parse(await ctx.machine.fetchText(
|
|
204
|
+
const reference = JSON.parse(await ctx.machine.fetchText(`https://api.github.com/repos/${OFFICECLI_REPO_SLUG}/git/ref/tags/v${version}`));
|
|
361
205
|
if (!isObject(reference) || reference.ref !== 'refs/tags/v' + version || !isObject(reference.object))
|
|
362
206
|
throw new Error('source-version-unavailable');
|
|
363
207
|
let object = reference.object;
|
|
364
|
-
|
|
365
|
-
|
|
208
|
+
// 技能固定到对应程序版本的提交,最多解引用四层附注标签
|
|
209
|
+
for (let depth = 0; depth < MAX_TAG_DEREFERENCES; depth++) {
|
|
210
|
+
if (typeof object.sha !== 'string' || !isGitRevision(object.sha))
|
|
366
211
|
throw new Error('source-version-unavailable');
|
|
367
212
|
if (object.type === 'commit')
|
|
368
213
|
return object.sha;
|
|
369
214
|
if (object.type !== 'tag')
|
|
370
215
|
throw new Error('source-version-unavailable');
|
|
371
|
-
const tag = JSON.parse(await ctx.machine.fetchText(
|
|
216
|
+
const tag = JSON.parse(await ctx.machine.fetchText(`https://api.github.com/repos/${OFFICECLI_REPO_SLUG}/git/tags/${object.sha}`));
|
|
372
217
|
if (!isObject(tag) || tag.sha !== object.sha || !isObject(tag.object))
|
|
373
218
|
throw new Error('source-version-unavailable');
|
|
374
219
|
object = tag.object;
|
|
375
220
|
}
|
|
376
221
|
throw new Error('source-version-unavailable');
|
|
377
222
|
}
|
|
378
|
-
async function
|
|
379
|
-
const
|
|
380
|
-
const
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
};
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
const isReinstall = selection.action === 'reinstall';
|
|
413
|
-
if ((isReinstall || integration === 'mcp' || integration === 'both') &&
|
|
414
|
-
observed.registration !== undefined &&
|
|
415
|
-
!hasMcp)
|
|
416
|
-
throw new Error('integration-source-conflict');
|
|
417
|
-
const shouldConnectBoth = integration === 'both' ||
|
|
418
|
-
(isReinstall &&
|
|
419
|
-
((hasMcp && integration !== 'mcp') || (observed.skillEvidence !== 'none' && integration === 'mcp')));
|
|
420
|
-
if (integration === 'mcp' && !shouldConnectBoth)
|
|
421
|
-
return mcp('connect', isReinstall);
|
|
422
|
-
if (before !== null && selection.fields?.replace !== 'yes' && (!isObject(record) || record.stamp !== before))
|
|
423
|
-
throw new Error('local-edits-preserved');
|
|
424
|
-
if (isReinstall && before !== null && observed.skillEvidence === 'none')
|
|
425
|
-
throw new Error('integration-source-conflict');
|
|
426
|
-
const revision = await officeRevision(ctx, target);
|
|
427
|
-
let skill;
|
|
428
|
-
if (before !== null && isObject(record) && record.stamp === before && record.revision === revision && !isReinstall) {
|
|
429
|
-
skill = currentSkill(ctx, 'officecli', directory, before);
|
|
430
|
-
}
|
|
431
|
-
else {
|
|
432
|
-
const repository = officeIntegration.skill.repository;
|
|
433
|
-
const files = await ctx.machine.source(repository, revision, officeIntegration.skill.folder);
|
|
434
|
-
skill = prepareSkillFiles(ctx, { name: 'officecli', directory, stamp: isReinstall ? null : before, sourceRecord: record }, { repository, revision, files, stamp: skillFilesStamp(files) });
|
|
223
|
+
async function officeUpdates(ctx, binary) {
|
|
224
|
+
const files = ['.update', '.update.partial', '.old'].map(suffix => binary + suffix);
|
|
225
|
+
const entries = await ctx.machine.entries(path.dirname(binary));
|
|
226
|
+
return Promise.all(files.map(async (file) => {
|
|
227
|
+
if (entries.some(entry => entry.name === path.basename(file) && entry.kind === 'directory'))
|
|
228
|
+
throw new Error('program-update-conflict');
|
|
229
|
+
return { file, stamp: await ctx.machine.treeStamp(file) };
|
|
230
|
+
}));
|
|
231
|
+
}
|
|
232
|
+
function officeAsset(ctx) {
|
|
233
|
+
const arch = ctx.arch ?? process.arch;
|
|
234
|
+
if (arch !== 'x64' && arch !== 'arm64')
|
|
235
|
+
throw new Error('unsupported-architecture');
|
|
236
|
+
if (ctx.platform === 'win32')
|
|
237
|
+
return `officecli-win-${arch}.exe`;
|
|
238
|
+
if (ctx.platform === 'darwin')
|
|
239
|
+
return `officecli-mac-${arch}`;
|
|
240
|
+
return `officecli-linux-${ctx.libc === 'musl' ? 'alpine-' : ''}${arch}`;
|
|
241
|
+
}
|
|
242
|
+
/** OfficeCLI 自行发现受管原生安装和只读的外部入口,不参与 npm 发现 */
|
|
243
|
+
async function officePrograms(ctx) {
|
|
244
|
+
const values = new Map();
|
|
245
|
+
const native = officeBinary(ctx);
|
|
246
|
+
if ((await ctx.machine.treeStamp(native)) !== null)
|
|
247
|
+
values.set('officecli', { channel: 'native', binary: native });
|
|
248
|
+
for (const binary of await ctx.machine.locate('officecli')) {
|
|
249
|
+
const canonical = await ctx.machine.realpath(binary);
|
|
250
|
+
if (!canonical ||
|
|
251
|
+
[...values.values()].some(program => samePath(ctx, program.binary, binary) || samePath(ctx, program.binary, canonical)))
|
|
252
|
+
continue;
|
|
253
|
+
values.set('officecli:external:' + binary, { channel: 'manual', binary });
|
|
435
254
|
}
|
|
436
|
-
return
|
|
255
|
+
return {
|
|
256
|
+
values,
|
|
257
|
+
items: [...values.entries()].map(([id, program]) => ({
|
|
258
|
+
id,
|
|
259
|
+
title: knownTitle('officecli'),
|
|
260
|
+
kind: 'officecli',
|
|
261
|
+
location: program.binary,
|
|
262
|
+
canInstall: program.channel !== 'manual',
|
|
263
|
+
canUninstall: program.channel !== 'manual',
|
|
264
|
+
facts: [
|
|
265
|
+
{ code: 'program-exists', detail: program.channel },
|
|
266
|
+
...(program.channel === 'manual' ? [{ code: 'external-program-preserved' }] : []),
|
|
267
|
+
],
|
|
268
|
+
})),
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* 计算 OfficeCLI 固定入口:Windows 本地应用目录,其他平台用户 bin 目录
|
|
273
|
+
*
|
|
274
|
+
* @throws Error - 目录不是绝对路径或指向文件系统根时抛出 invalid-config-root
|
|
275
|
+
*/
|
|
276
|
+
function officeBinary(ctx) {
|
|
277
|
+
const root = ctx.platform === 'win32'
|
|
278
|
+
? path.join(ctx.localAppData ?? path.join(ctx.home, 'AppData/Local'), 'OfficeCli')
|
|
279
|
+
: userBinDirectory(ctx);
|
|
280
|
+
if (!path.isAbsolute(root) || path.resolve(root) === path.parse(path.resolve(root)).root)
|
|
281
|
+
throw new Error('invalid-config-root');
|
|
282
|
+
return path.join(root, ctx.platform === 'win32' ? 'officecli.exe' : 'officecli');
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* 禁用自更新后探测 OfficeCLI 身份和版本
|
|
286
|
+
*
|
|
287
|
+
* @returns 经确认的三段版本,无法确认时为 null;命令调用拒绝仍向外传播
|
|
288
|
+
* @remarks 版本与帮助探测共用五秒;版本输出缺少程序名时再查帮助
|
|
289
|
+
*/
|
|
290
|
+
async function officeVersion(ctx, binary) {
|
|
291
|
+
const deadlineMs = performance.now() + VERSION_PROBE_TIMEOUT_MS;
|
|
292
|
+
const probe = (argument) => ctx.machine.execute(binary, [argument], {
|
|
293
|
+
env: { ...OFFICE_INTEGRATION.suppressSelfUpdate },
|
|
294
|
+
cwd: ctx.home,
|
|
295
|
+
timeoutMs: Math.max(1, Math.ceil(deadlineMs - performance.now())),
|
|
296
|
+
});
|
|
297
|
+
const output = await probe('--version');
|
|
298
|
+
const version = /\b(\d+\.\d+\.\d+)\b/.exec(output.stdout)?.[1];
|
|
299
|
+
if (!succeeded(output) || version === undefined)
|
|
300
|
+
return null;
|
|
301
|
+
if (/\bofficecli\b/i.test(output.stdout))
|
|
302
|
+
return version;
|
|
303
|
+
if (performance.now() >= deadlineMs)
|
|
304
|
+
return null;
|
|
305
|
+
const help = await probe('--help');
|
|
306
|
+
return succeeded(help) && /\bofficecli\b/i.test(help.stdout) ? version : null;
|
|
437
307
|
}
|