@steerable/steer 0.3.28 → 0.3.30
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 +1 -1
- package/dist/products/steer/active.js +3 -1
- package/dist/products/steer/foreign-import/deps.js +1 -0
- package/dist/products/steer/foreign-import/experts.js +62 -0
- package/dist/products/steer/foreign-import/ledger.js +68 -0
- package/dist/products/steer/foreign-import/scan.js +657 -0
- package/dist/products/steer/foreign-import/skills.js +206 -0
- package/dist/products/steer/foreign-import/sources.js +127 -0
- package/dist/products/steer/foreign-import/tasks.js +187 -0
- package/dist/products/steer/foreign-import/types.js +12 -0
- package/dist/products/steer/foreign-import-routes.js +87 -0
- package/dist/products/steer/locales/shell.zh.json +12 -3
- package/dist/products/steer/locales/zh.json +41 -1
- package/package.json +1 -1
|
@@ -0,0 +1,657 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 按来源表扫描本机目录,并用扫描得到的 id 复制任务、技能和专家。
|
|
3
|
+
*/
|
|
4
|
+
import crypto from 'node:crypto';
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { discoverExpertFiles, parseExpertMarkdown } from './experts.js';
|
|
8
|
+
import { readLedger, updateLedger } from './ledger.js';
|
|
9
|
+
import { copySkillDirectory, discoverSkillDirs, skillDestName, skillDisplayName, } from './skills.js';
|
|
10
|
+
import { EXCLUDED_PATH_SEGMENTS, SOURCE_ORDER, sourceBaseLabel, foreignImportRoots, } from './sources.js';
|
|
11
|
+
import { clipForeignMessage, FOREIGN_MESSAGE_LIMIT, foreignTaskBareTitle, parseForeignSession, } from './tasks.js';
|
|
12
|
+
const MAX_SESSION_BYTES = 32 * 1024 * 1024;
|
|
13
|
+
function pathInside(root, candidate) {
|
|
14
|
+
const base = root.endsWith(path.sep) ? root.slice(0, -1) : root;
|
|
15
|
+
return candidate === base || candidate.startsWith(base + path.sep);
|
|
16
|
+
}
|
|
17
|
+
function note(deps, filePath) {
|
|
18
|
+
deps.observe?.(filePath);
|
|
19
|
+
}
|
|
20
|
+
function itemId(source, kind, realPath) {
|
|
21
|
+
return crypto
|
|
22
|
+
.createHash('sha256')
|
|
23
|
+
.update(`${source}\0${kind}\0${realPath}`)
|
|
24
|
+
.digest('hex')
|
|
25
|
+
.slice(0, 24);
|
|
26
|
+
}
|
|
27
|
+
function resolveDir(deps, abs) {
|
|
28
|
+
note(deps, abs);
|
|
29
|
+
let st;
|
|
30
|
+
try {
|
|
31
|
+
st = fs.lstatSync(abs);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
if (!st.isDirectory() && !st.isSymbolicLink())
|
|
37
|
+
return null;
|
|
38
|
+
try {
|
|
39
|
+
const real = fs.realpathSync(abs);
|
|
40
|
+
note(deps, real);
|
|
41
|
+
if (!fs.statSync(real).isDirectory())
|
|
42
|
+
return null;
|
|
43
|
+
return real;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function excluded(rel) {
|
|
50
|
+
return rel.split(path.sep).some((segment) => EXCLUDED_PATH_SEGMENTS.has(segment));
|
|
51
|
+
}
|
|
52
|
+
function readInside(deps, file, fence) {
|
|
53
|
+
note(deps, file);
|
|
54
|
+
const st = fs.lstatSync(file);
|
|
55
|
+
if (!st.isFile() || st.isSymbolicLink())
|
|
56
|
+
throw new Error('read-failed');
|
|
57
|
+
const real = fs.realpathSync(file);
|
|
58
|
+
note(deps, real);
|
|
59
|
+
if (!pathInside(fence, real))
|
|
60
|
+
throw new Error('read-failed');
|
|
61
|
+
if (deps.readFile)
|
|
62
|
+
return deps.readFile(real);
|
|
63
|
+
return fs.readFileSync(real, 'utf8');
|
|
64
|
+
}
|
|
65
|
+
function notice(source, section, id, skipReason) {
|
|
66
|
+
return {
|
|
67
|
+
id,
|
|
68
|
+
source,
|
|
69
|
+
kind: 'notice',
|
|
70
|
+
section,
|
|
71
|
+
bare: '',
|
|
72
|
+
detail: null,
|
|
73
|
+
updatedAt: null,
|
|
74
|
+
messageCount: null,
|
|
75
|
+
selectable: false,
|
|
76
|
+
status: 'unsupported',
|
|
77
|
+
skipReason,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function listDir(deps, dir) {
|
|
81
|
+
note(deps, dir);
|
|
82
|
+
try {
|
|
83
|
+
return fs.readdirSync(dir, { withFileTypes: true });
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return [];
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function considerFile(deps, file, fence) {
|
|
90
|
+
if (excluded(path.relative(fence, file)))
|
|
91
|
+
return null;
|
|
92
|
+
note(deps, file);
|
|
93
|
+
let st;
|
|
94
|
+
try {
|
|
95
|
+
st = fs.lstatSync(file);
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
if (!st.isFile() || st.isSymbolicLink())
|
|
101
|
+
return null;
|
|
102
|
+
let real;
|
|
103
|
+
try {
|
|
104
|
+
real = fs.realpathSync(file);
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
note(deps, real);
|
|
110
|
+
if (!pathInside(fence, real))
|
|
111
|
+
return null;
|
|
112
|
+
return { real, size: st.size, mtime: st.mtime };
|
|
113
|
+
}
|
|
114
|
+
function walkProjectJsonl(deps, root, fence, shape, out) {
|
|
115
|
+
const projects = path.join(fence, 'projects');
|
|
116
|
+
if (!resolveDir(deps, projects))
|
|
117
|
+
return;
|
|
118
|
+
for (const slug of listDir(deps, projects)) {
|
|
119
|
+
if (EXCLUDED_PATH_SEGMENTS.has(slug.name) || slug.isSymbolicLink() || !slug.isDirectory())
|
|
120
|
+
continue;
|
|
121
|
+
const slugDir = path.join(projects, slug.name);
|
|
122
|
+
for (const ent of listDir(deps, slugDir)) {
|
|
123
|
+
if (ent.isSymbolicLink() || !ent.isFile() || !ent.name.endsWith('.jsonl'))
|
|
124
|
+
continue;
|
|
125
|
+
if (shape === 'claude-jsonl' && ent.name.startsWith('agent-'))
|
|
126
|
+
continue;
|
|
127
|
+
const file = path.join(slugDir, ent.name);
|
|
128
|
+
const info = considerFile(deps, file, fence);
|
|
129
|
+
if (!info)
|
|
130
|
+
continue;
|
|
131
|
+
pushTask(deps, root, fence, shape, file, info, out);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
function walkCodex(deps, root, fence, out) {
|
|
136
|
+
const sessions = path.join(fence, 'sessions');
|
|
137
|
+
if (!resolveDir(deps, sessions))
|
|
138
|
+
return;
|
|
139
|
+
const walk = (dir) => {
|
|
140
|
+
for (const ent of listDir(deps, dir)) {
|
|
141
|
+
if (EXCLUDED_PATH_SEGMENTS.has(ent.name) || ent.isSymbolicLink())
|
|
142
|
+
continue;
|
|
143
|
+
const abs = path.join(dir, ent.name);
|
|
144
|
+
if (ent.isDirectory()) {
|
|
145
|
+
walk(abs);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (!ent.isFile() || !ent.name.startsWith('rollout-') || !ent.name.endsWith('.jsonl'))
|
|
149
|
+
continue;
|
|
150
|
+
const info = considerFile(deps, abs, fence);
|
|
151
|
+
if (!info)
|
|
152
|
+
continue;
|
|
153
|
+
pushTask(deps, root, fence, 'codex-rollout', abs, info, out);
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
walk(sessions);
|
|
157
|
+
}
|
|
158
|
+
function walkCursor(deps, root, fence, out) {
|
|
159
|
+
const projects = path.join(fence, 'projects');
|
|
160
|
+
if (!resolveDir(deps, projects))
|
|
161
|
+
return;
|
|
162
|
+
for (const slug of listDir(deps, projects)) {
|
|
163
|
+
if (EXCLUDED_PATH_SEGMENTS.has(slug.name) || slug.isSymbolicLink() || !slug.isDirectory())
|
|
164
|
+
continue;
|
|
165
|
+
const transcripts = path.join(projects, slug.name, 'agent-transcripts');
|
|
166
|
+
if (!resolveDir(deps, transcripts))
|
|
167
|
+
continue;
|
|
168
|
+
for (const uuid of listDir(deps, transcripts)) {
|
|
169
|
+
if (EXCLUDED_PATH_SEGMENTS.has(uuid.name) || uuid.isSymbolicLink() || !uuid.isDirectory())
|
|
170
|
+
continue;
|
|
171
|
+
const file = path.join(transcripts, uuid.name, `${uuid.name}.jsonl`);
|
|
172
|
+
const info = considerFile(deps, file, fence);
|
|
173
|
+
if (!info)
|
|
174
|
+
continue;
|
|
175
|
+
pushTask(deps, root, fence, 'cursor-transcript', file, info, out);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
function pushTask(deps, root, fence, shape, file, info, out) {
|
|
180
|
+
const id = itemId(root.source, 'task', info.real);
|
|
181
|
+
if (info.size > MAX_SESSION_BYTES) {
|
|
182
|
+
out.push({
|
|
183
|
+
id,
|
|
184
|
+
source: root.source,
|
|
185
|
+
kind: 'task',
|
|
186
|
+
section: 'tasks',
|
|
187
|
+
bare: path.basename(file, '.jsonl').slice(0, 64),
|
|
188
|
+
detail: file,
|
|
189
|
+
updatedAt: info.mtime.toISOString(),
|
|
190
|
+
messageCount: null,
|
|
191
|
+
selectable: false,
|
|
192
|
+
status: 'unsupported',
|
|
193
|
+
skipReason: 'too-large',
|
|
194
|
+
});
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
let raw;
|
|
198
|
+
try {
|
|
199
|
+
raw = readInside(deps, file, fence);
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const parsed = parseForeignSession(shape, file, raw);
|
|
205
|
+
if (!parsed)
|
|
206
|
+
return;
|
|
207
|
+
out.push({
|
|
208
|
+
id,
|
|
209
|
+
source: root.source,
|
|
210
|
+
kind: 'task',
|
|
211
|
+
section: 'tasks',
|
|
212
|
+
bare: foreignTaskBareTitle(parsed),
|
|
213
|
+
detail: parsed.cwd ?? file,
|
|
214
|
+
updatedAt: info.mtime.toISOString(),
|
|
215
|
+
messageCount: parsed.messages.length,
|
|
216
|
+
selectable: true,
|
|
217
|
+
status: 'available',
|
|
218
|
+
skipReason: null,
|
|
219
|
+
located: { kind: 'task', shape, file, fence },
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
function scanTasks(deps, root, fence, out) {
|
|
223
|
+
if (!fence) {
|
|
224
|
+
out.push(notice(root.source, 'tasks', `${root.source}:tasks-missing`, 'not-found'));
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
if (root.unsupported === 'encrypted-db') {
|
|
228
|
+
out.push(notice(root.source, 'tasks', `${root.source}:encrypted-db`, 'encrypted-db'));
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
if (root.unsupported === 'indexed-db') {
|
|
232
|
+
out.push(notice(root.source, 'tasks', `${root.source}:indexed-db`, 'indexed-db'));
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
if (root.source === 'cursor') {
|
|
236
|
+
out.push(notice(root.source, 'tasks', 'cursor:store-db', 'store-db'));
|
|
237
|
+
}
|
|
238
|
+
switch (root.taskShape) {
|
|
239
|
+
case 'claude-jsonl':
|
|
240
|
+
case 'qoder-jsonl':
|
|
241
|
+
walkProjectJsonl(deps, root, fence, root.taskShape, out);
|
|
242
|
+
break;
|
|
243
|
+
case 'codex-rollout':
|
|
244
|
+
walkCodex(deps, root, fence, out);
|
|
245
|
+
break;
|
|
246
|
+
case 'cursor-transcript':
|
|
247
|
+
walkCursor(deps, root, fence, out);
|
|
248
|
+
break;
|
|
249
|
+
case 'unsupported':
|
|
250
|
+
break;
|
|
251
|
+
default: {
|
|
252
|
+
const neverShape = root.taskShape;
|
|
253
|
+
void neverShape;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
function scanSkills(deps, root, takenNames, out) {
|
|
258
|
+
const fences = root.skillRoots
|
|
259
|
+
.map((skillRoot) => resolveDir(deps, skillRoot))
|
|
260
|
+
.filter((fence) => fence !== null);
|
|
261
|
+
if (fences.length === 0) {
|
|
262
|
+
out.push(notice(root.source, 'skills', `${root.source}:skills-missing`, 'not-found'));
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
for (const fence of fences) {
|
|
266
|
+
for (const skill of discoverSkillDirs(fence, deps)) {
|
|
267
|
+
const id = itemId(root.source, 'skill', skill.real);
|
|
268
|
+
if (skill.escaped) {
|
|
269
|
+
out.push({
|
|
270
|
+
id,
|
|
271
|
+
source: root.source,
|
|
272
|
+
kind: 'skill',
|
|
273
|
+
section: 'skills',
|
|
274
|
+
bare: path.basename(skill.dir),
|
|
275
|
+
detail: skill.dir,
|
|
276
|
+
updatedAt: skill.mtime.toISOString(),
|
|
277
|
+
messageCount: null,
|
|
278
|
+
selectable: false,
|
|
279
|
+
status: 'unsupported',
|
|
280
|
+
skipReason: 'symlink-escape',
|
|
281
|
+
located: { kind: 'skill', dir: skill.dir, fence },
|
|
282
|
+
});
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
let markdown = '';
|
|
286
|
+
try {
|
|
287
|
+
markdown = readInside(deps, path.join(skill.dir, 'SKILL.md'), fence);
|
|
288
|
+
}
|
|
289
|
+
catch {
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
const bare = skillDisplayName(markdown, path.basename(skill.dir));
|
|
293
|
+
const destName = skillDestName(root.source, skill.real, markdown, path.basename(skill.dir));
|
|
294
|
+
const nameTaken = takenNames.has(destName) || directoryExists(path.join(deps.userSkillsDir, destName));
|
|
295
|
+
out.push({
|
|
296
|
+
id,
|
|
297
|
+
source: root.source,
|
|
298
|
+
kind: 'skill',
|
|
299
|
+
section: 'skills',
|
|
300
|
+
bare,
|
|
301
|
+
detail: skill.dir,
|
|
302
|
+
updatedAt: skill.mtime.toISOString(),
|
|
303
|
+
messageCount: null,
|
|
304
|
+
selectable: true,
|
|
305
|
+
status: nameTaken ? 'name-taken' : 'available',
|
|
306
|
+
skipReason: null,
|
|
307
|
+
located: { kind: 'skill', dir: skill.dir, fence },
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
function scanExperts(deps, root, fence, names, out) {
|
|
313
|
+
if (!root.agentsDir || !fence) {
|
|
314
|
+
out.push(notice(root.source, 'experts', `${root.source}:no-experts`, 'no-experts'));
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
const agentsDir = path.join(fence, 'agents');
|
|
318
|
+
const agentsReal = resolveDir(deps, agentsDir);
|
|
319
|
+
if (!agentsReal) {
|
|
320
|
+
out.push(notice(root.source, 'experts', `${root.source}:no-experts`, 'no-experts'));
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
const files = discoverExpertFiles(agentsReal, deps);
|
|
324
|
+
if (files.length === 0) {
|
|
325
|
+
out.push(notice(root.source, 'experts', `${root.source}:no-experts`, 'no-experts'));
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
for (const file of files) {
|
|
329
|
+
if (!pathInside(agentsReal, file.real))
|
|
330
|
+
continue;
|
|
331
|
+
let markdown = '';
|
|
332
|
+
try {
|
|
333
|
+
markdown = readInside(deps, file.file, agentsReal);
|
|
334
|
+
}
|
|
335
|
+
catch {
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
const parsed = parseExpertMarkdown(markdown, file.file);
|
|
339
|
+
const nameTaken = names.has(parsed.name.trim());
|
|
340
|
+
out.push({
|
|
341
|
+
id: itemId(root.source, 'expert', file.real),
|
|
342
|
+
source: root.source,
|
|
343
|
+
kind: 'expert',
|
|
344
|
+
section: 'experts',
|
|
345
|
+
bare: parsed.name,
|
|
346
|
+
detail: file.file,
|
|
347
|
+
updatedAt: file.mtime.toISOString(),
|
|
348
|
+
messageCount: null,
|
|
349
|
+
selectable: true,
|
|
350
|
+
status: nameTaken ? 'name-taken' : 'available',
|
|
351
|
+
skipReason: null,
|
|
352
|
+
located: { kind: 'expert', file: file.file, fence: agentsReal },
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
function directoryExists(dir) {
|
|
357
|
+
try {
|
|
358
|
+
return fs.statSync(dir).isDirectory();
|
|
359
|
+
}
|
|
360
|
+
catch {
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
function labelsFor(drafts) {
|
|
365
|
+
const findings = new Set();
|
|
366
|
+
for (const draft of drafts) {
|
|
367
|
+
if (draft.skipReason === 'not-found' || draft.skipReason === 'no-experts')
|
|
368
|
+
continue;
|
|
369
|
+
findings.add(draft.source);
|
|
370
|
+
}
|
|
371
|
+
const labels = new Map();
|
|
372
|
+
const pair = (global, cn, base) => {
|
|
373
|
+
const both = findings.has(global) && findings.has(cn);
|
|
374
|
+
labels.set(global, both ? `${base} · Global` : base);
|
|
375
|
+
labels.set(cn, both ? `${base} · CN` : base);
|
|
376
|
+
};
|
|
377
|
+
for (const source of SOURCE_ORDER) {
|
|
378
|
+
if (source === 'qoder' || source === 'qoder-cn' || source === 'trae' || source === 'trae-cn')
|
|
379
|
+
continue;
|
|
380
|
+
labels.set(source, sourceBaseLabel(source));
|
|
381
|
+
}
|
|
382
|
+
pair('qoder', 'qoder-cn', 'Qoder');
|
|
383
|
+
pair('trae', 'trae-cn', 'Trae');
|
|
384
|
+
return labels;
|
|
385
|
+
}
|
|
386
|
+
function finish(drafts) {
|
|
387
|
+
const labels = labelsFor(drafts);
|
|
388
|
+
const rank = (draft) => (draft.kind === 'notice' || draft.status === 'unsupported' ? 1 : 0);
|
|
389
|
+
const sectionRank = { tasks: 0, skills: 1, experts: 2 };
|
|
390
|
+
drafts.sort((a, b) => {
|
|
391
|
+
const bySource = SOURCE_ORDER.indexOf(a.source) - SOURCE_ORDER.indexOf(b.source);
|
|
392
|
+
if (bySource !== 0)
|
|
393
|
+
return bySource;
|
|
394
|
+
const bySection = sectionRank[a.section] - sectionRank[b.section];
|
|
395
|
+
if (bySection !== 0)
|
|
396
|
+
return bySection;
|
|
397
|
+
return rank(a) - rank(b);
|
|
398
|
+
});
|
|
399
|
+
return drafts.map((draft) => {
|
|
400
|
+
const label = labels.get(draft.source) ?? sourceBaseLabel(draft.source);
|
|
401
|
+
let title = label;
|
|
402
|
+
if (draft.skipReason === 'store-db')
|
|
403
|
+
title = `${label} · store.db`;
|
|
404
|
+
else if (draft.kind !== 'notice')
|
|
405
|
+
title = `${label} · ${draft.bare}`;
|
|
406
|
+
return {
|
|
407
|
+
id: draft.id,
|
|
408
|
+
source: draft.source,
|
|
409
|
+
kind: draft.kind,
|
|
410
|
+
section: draft.section,
|
|
411
|
+
title,
|
|
412
|
+
detail: draft.detail,
|
|
413
|
+
updatedAt: draft.updatedAt,
|
|
414
|
+
messageCount: draft.messageCount,
|
|
415
|
+
selectable: draft.selectable,
|
|
416
|
+
status: draft.status,
|
|
417
|
+
skipReason: draft.skipReason,
|
|
418
|
+
};
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
async function aliveLedgerIds(deps, items) {
|
|
422
|
+
const alive = new Set();
|
|
423
|
+
for (const entry of items) {
|
|
424
|
+
if (entry.kind === 'task') {
|
|
425
|
+
const chat = await deps.store.getChat(entry.targetId);
|
|
426
|
+
if (chat)
|
|
427
|
+
alive.add(entry.id);
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
if (entry.kind === 'skill') {
|
|
431
|
+
if (directoryExists(path.join(deps.userSkillsDir, entry.targetId)))
|
|
432
|
+
alive.add(entry.id);
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
const agent = await deps.store.getChatAgent(entry.targetId);
|
|
436
|
+
if (agent && agent.isArchived !== true)
|
|
437
|
+
alive.add(entry.id);
|
|
438
|
+
}
|
|
439
|
+
return alive;
|
|
440
|
+
}
|
|
441
|
+
function applyLedger(drafts, alive) {
|
|
442
|
+
for (const draft of drafts) {
|
|
443
|
+
if (!alive.has(draft.id) || draft.kind === 'notice')
|
|
444
|
+
continue;
|
|
445
|
+
draft.status = 'imported';
|
|
446
|
+
draft.selectable = false;
|
|
447
|
+
draft.skipReason = null;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
/** 扫描声明的根。调用前不读盘。 */
|
|
451
|
+
export async function scanForeignImport(deps) {
|
|
452
|
+
const ledger = readLedger(deps.userDataDir);
|
|
453
|
+
const alive = await aliveLedgerIds(deps, ledger.ledger.items);
|
|
454
|
+
const agents = await deps.store.listChatAgents(false);
|
|
455
|
+
const agentNames = new Set(agents.map((agent) => agent.name.trim()));
|
|
456
|
+
const takenSkillNames = new Set(ledger.ledger.items.filter((entry) => entry.kind === 'skill' && alive.has(entry.id)).map((entry) => entry.targetId));
|
|
457
|
+
const drafts = [];
|
|
458
|
+
for (const root of foreignImportRoots(deps)) {
|
|
459
|
+
const fence = root.root ? resolveDir(deps, root.root) : null;
|
|
460
|
+
scanTasks(deps, root, fence, drafts);
|
|
461
|
+
scanSkills(deps, root, takenSkillNames, drafts);
|
|
462
|
+
scanExperts(deps, root, fence, agentNames, drafts);
|
|
463
|
+
}
|
|
464
|
+
applyLedger(drafts, alive);
|
|
465
|
+
const located = new Map();
|
|
466
|
+
for (const draft of drafts) {
|
|
467
|
+
if (draft.located)
|
|
468
|
+
located.set(draft.id, draft.located);
|
|
469
|
+
}
|
|
470
|
+
return {
|
|
471
|
+
items: finish(drafts),
|
|
472
|
+
located,
|
|
473
|
+
...(ledger.warning ? { ledgerWarning: ledger.warning } : {}),
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
function emptyResult() {
|
|
477
|
+
return { chats: 0, skills: 0, experts: 0, skipped: 0, failed: 0, truncated: 0, failures: [] };
|
|
478
|
+
}
|
|
479
|
+
function bindProject(deps, cwd) {
|
|
480
|
+
if (!cwd || !deps.projects)
|
|
481
|
+
return null;
|
|
482
|
+
let real;
|
|
483
|
+
try {
|
|
484
|
+
real = fs.realpathSync(cwd);
|
|
485
|
+
if (!fs.statSync(real).isDirectory())
|
|
486
|
+
return null;
|
|
487
|
+
}
|
|
488
|
+
catch {
|
|
489
|
+
return null;
|
|
490
|
+
}
|
|
491
|
+
const existing = deps.projects.findByFolder(real);
|
|
492
|
+
if (existing)
|
|
493
|
+
return existing.id;
|
|
494
|
+
const name = path.basename(real) || 'project';
|
|
495
|
+
try {
|
|
496
|
+
return deps.projects.create({ name, folderPath: real }).id;
|
|
497
|
+
}
|
|
498
|
+
catch {
|
|
499
|
+
return deps.projects.createUnique({ name, folderPath: real }).id;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
async function importTask(deps, item, located, result) {
|
|
503
|
+
const raw = readInside(deps, located.file, located.fence);
|
|
504
|
+
const parsed = parseForeignSession(located.shape, located.file, raw);
|
|
505
|
+
if (!parsed)
|
|
506
|
+
throw new Error('read-failed');
|
|
507
|
+
const projectId = bindProject(deps, parsed.cwd);
|
|
508
|
+
const chat = await deps.store.createChat(item.title, null, projectId);
|
|
509
|
+
try {
|
|
510
|
+
await deps.store.updateChat(chat.id, { titleSource: 'user' });
|
|
511
|
+
const kept = parsed.messages.slice(-FOREIGN_MESSAGE_LIMIT);
|
|
512
|
+
for (const message of kept) {
|
|
513
|
+
await deps.store.addMessage(chat.id, message.role, clipForeignMessage(message.text));
|
|
514
|
+
}
|
|
515
|
+
remember(deps, {
|
|
516
|
+
id: item.id,
|
|
517
|
+
kind: 'task',
|
|
518
|
+
targetId: chat.id,
|
|
519
|
+
sourcePath: located.file,
|
|
520
|
+
importedAt: new Date().toISOString(),
|
|
521
|
+
});
|
|
522
|
+
if (parsed.messages.length > FOREIGN_MESSAGE_LIMIT)
|
|
523
|
+
result.truncated += 1;
|
|
524
|
+
}
|
|
525
|
+
catch (err) {
|
|
526
|
+
await deps.store.deleteChat?.(chat.id);
|
|
527
|
+
throw err;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
async function importSkill(deps, item, located, result) {
|
|
531
|
+
if (item.status === 'name-taken') {
|
|
532
|
+
result.skipped += 1;
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
const markdown = readInside(deps, path.join(located.dir, 'SKILL.md'), located.fence);
|
|
536
|
+
const destName = skillDestName(item.source, fs.realpathSync(located.dir), markdown, path.basename(located.dir));
|
|
537
|
+
if (!/^[a-z0-9][a-z0-9-]{0,80}$/.test(destName))
|
|
538
|
+
throw new Error('copy-failed');
|
|
539
|
+
const dest = path.join(deps.userSkillsDir, destName);
|
|
540
|
+
if (path.resolve(path.dirname(dest)) !== path.resolve(deps.userSkillsDir))
|
|
541
|
+
throw new Error('copy-failed');
|
|
542
|
+
if (directoryExists(dest) || fs.existsSync(dest)) {
|
|
543
|
+
result.skipped += 1;
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
const copied = copySkillDirectory(located.dir, dest);
|
|
547
|
+
if (copied === 'symlink-escape')
|
|
548
|
+
throw Object.assign(new Error('symlink-escape'), { reason: 'symlink-escape' });
|
|
549
|
+
if (copied !== 'ok')
|
|
550
|
+
throw new Error('copy-failed');
|
|
551
|
+
try {
|
|
552
|
+
remember(deps, {
|
|
553
|
+
id: item.id,
|
|
554
|
+
kind: 'skill',
|
|
555
|
+
targetId: destName,
|
|
556
|
+
sourcePath: located.dir,
|
|
557
|
+
importedAt: new Date().toISOString(),
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
catch (err) {
|
|
561
|
+
fs.rmSync(dest, { recursive: true, force: true });
|
|
562
|
+
throw err;
|
|
563
|
+
}
|
|
564
|
+
result.skills += 1;
|
|
565
|
+
}
|
|
566
|
+
async function importExpert(deps, item, located, result) {
|
|
567
|
+
if (item.status === 'name-taken') {
|
|
568
|
+
result.skipped += 1;
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
const markdown = readInside(deps, located.file, located.fence);
|
|
572
|
+
const parsed = parseExpertMarkdown(markdown, located.file);
|
|
573
|
+
const existing = await deps.store.listChatAgents(false);
|
|
574
|
+
if (existing.some((agent) => agent.name.trim() === parsed.name.trim())) {
|
|
575
|
+
result.skipped += 1;
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
const created = await deps.store.createChatAgent({
|
|
579
|
+
name: parsed.name,
|
|
580
|
+
description: parsed.description,
|
|
581
|
+
rolePrompt: parsed.body,
|
|
582
|
+
});
|
|
583
|
+
try {
|
|
584
|
+
remember(deps, {
|
|
585
|
+
id: item.id,
|
|
586
|
+
kind: 'expert',
|
|
587
|
+
targetId: created.id,
|
|
588
|
+
sourcePath: located.file,
|
|
589
|
+
importedAt: new Date().toISOString(),
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
catch (err) {
|
|
593
|
+
throw err;
|
|
594
|
+
}
|
|
595
|
+
result.experts += 1;
|
|
596
|
+
}
|
|
597
|
+
function remember(deps, entry) {
|
|
598
|
+
updateLedger(deps.userDataDir, (items) => [...items.filter((item) => item.id !== entry.id), entry]);
|
|
599
|
+
}
|
|
600
|
+
function failureReason(err) {
|
|
601
|
+
if (err && typeof err === 'object' && 'reason' in err && typeof err.reason === 'string')
|
|
602
|
+
return err.reason;
|
|
603
|
+
if (err instanceof Error && (err.message === 'read-failed' || err.message === 'copy-failed' || err.message === 'symlink-escape')) {
|
|
604
|
+
return err.message;
|
|
605
|
+
}
|
|
606
|
+
return 'read-failed';
|
|
607
|
+
}
|
|
608
|
+
/** 按 id 重新扫描后导入。未知 id 和不可选项不拿来当路径打开。 */
|
|
609
|
+
export async function importForeignSelection(deps, ids) {
|
|
610
|
+
const scan = await scanForeignImport(deps);
|
|
611
|
+
const byId = new Map(scan.items.map((item) => [item.id, item]));
|
|
612
|
+
const result = emptyResult();
|
|
613
|
+
for (const id of [...new Set(ids)]) {
|
|
614
|
+
const item = byId.get(id);
|
|
615
|
+
const located = scan.located.get(id);
|
|
616
|
+
if (!item || !located) {
|
|
617
|
+
result.failed += 1;
|
|
618
|
+
result.failures.push({ id, reason: 'unknown-id' });
|
|
619
|
+
continue;
|
|
620
|
+
}
|
|
621
|
+
if (item.status === 'imported') {
|
|
622
|
+
result.skipped += 1;
|
|
623
|
+
continue;
|
|
624
|
+
}
|
|
625
|
+
if (!item.selectable || item.kind === 'notice') {
|
|
626
|
+
result.failed += 1;
|
|
627
|
+
result.failures.push({ id, reason: item.skipReason === 'symlink-escape' ? 'symlink-escape' : 'unknown-id' });
|
|
628
|
+
continue;
|
|
629
|
+
}
|
|
630
|
+
try {
|
|
631
|
+
if (item.kind === 'task' && located.kind === 'task') {
|
|
632
|
+
await importTask(deps, item, located, result);
|
|
633
|
+
result.chats += 1;
|
|
634
|
+
}
|
|
635
|
+
else if (item.kind === 'skill' && located.kind === 'skill') {
|
|
636
|
+
await importSkill(deps, item, located, result);
|
|
637
|
+
}
|
|
638
|
+
else if (item.kind === 'expert' && located.kind === 'expert') {
|
|
639
|
+
await importExpert(deps, item, located, result);
|
|
640
|
+
}
|
|
641
|
+
else {
|
|
642
|
+
result.failed += 1;
|
|
643
|
+
result.failures.push({ id, reason: 'unknown-id' });
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
catch (err) {
|
|
647
|
+
result.failed += 1;
|
|
648
|
+
result.failures.push({ id, reason: failureReason(err) });
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
const again = await scanForeignImport(deps);
|
|
652
|
+
return {
|
|
653
|
+
items: again.items,
|
|
654
|
+
result,
|
|
655
|
+
...(scan.ledgerWarning ?? again.ledgerWarning ? { ledgerWarning: scan.ledgerWarning ?? again.ledgerWarning } : {}),
|
|
656
|
+
};
|
|
657
|
+
}
|