zcode-pro 1.7.0 → 1.8.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.
- package/README.en.md +4 -3
- package/README.md +4 -3
- package/dist/inject.js +776 -109
- package/package.json +2 -2
- package/src/host/helper.mjs +60 -12
- package/src/host/mcp-bridge.mjs +488 -0
- package/src/host/session-import/index.mjs +140 -0
- package/src/host/session-import/sources.mjs +839 -0
- package/src/host/session-import/util.mjs +239 -0
- package/src/host/session-import/write.mjs +405 -0
- package/src/host/session-import/zstd.mjs +68 -0
- package/src/host/taskIndex.mjs +108 -0
- package/src/host/wallpaper.mjs +3 -1
- package/src/inject/core.js +73 -0
- package/src/inject/features/idle-reclaim.js +48 -23
- package/src/inject/features/session-import.js +367 -0
- package/src/inject/features/settings-dialog.js +16 -1
- package/src/inject/features/status-bar.js +56 -24
- package/src/inject/features/styles.js +28 -8
- package/src/inject/features/subagent-icon.js +16 -17
- package/src/inject/features/theme.js +43 -12
- package/src/inject/features/turn-usage.js +59 -14
- package/src/inject/features/ws-running.js +38 -8
- package/src/inject/ui.js +12 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// 导入会话的入口:列出来源会话、批量导入。
|
|
2
|
+
import { existsSync, mkdirSync } from 'node:fs';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { isUnder, oneLine } from './util.mjs';
|
|
6
|
+
import { listSourceSessions, readSourceSession, sourceAvailable, sourceList, sourceRoots } from './sources.mjs';
|
|
7
|
+
import {
|
|
8
|
+
aliveTaskIds, aliveTaskStates, getProject, readRegistry, sourceKey, workspaceForImport, writeImportedSession, writeRegistry,
|
|
9
|
+
} from './write.mjs';
|
|
10
|
+
|
|
11
|
+
function present(session) {
|
|
12
|
+
return {
|
|
13
|
+
source: session.source,
|
|
14
|
+
sessionId: session.sessionId,
|
|
15
|
+
title: oneLine(session.title, 200) || session.sessionId,
|
|
16
|
+
cwd: session.cwd || '',
|
|
17
|
+
createdAt: session.createdAt || 0,
|
|
18
|
+
updatedAt: session.updatedAt || 0,
|
|
19
|
+
sourcePath: session.sourcePath,
|
|
20
|
+
archived: !!session.archived,
|
|
21
|
+
subagent: !!session.subagent,
|
|
22
|
+
codexImport: !!session.codexImport,
|
|
23
|
+
large: !!session.large,
|
|
24
|
+
model: session.model || '',
|
|
25
|
+
cwdExists: !!session.cwd && existsSync(session.cwd),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function listImportSessions({ dataRoot, source, showAll = false, search = '', roots = [] }) {
|
|
30
|
+
if (!source) return { ok: true, sources: sourceList(), sessions: [] };
|
|
31
|
+
if (!sourceList().some((s) => s.id === source)) {
|
|
32
|
+
return { ok: false, code: 'bad-source', error: '未知来源' };
|
|
33
|
+
}
|
|
34
|
+
if (!sourceAvailable(source)) {
|
|
35
|
+
return { ok: true, sources: sourceList(), sessions: [], unavailable: true };
|
|
36
|
+
}
|
|
37
|
+
const all = await listSourceSessions(source, { showAll: !!showAll });
|
|
38
|
+
const filterRoots = (Array.isArray(roots) ? roots : []).filter((r) => typeof r === 'string' && r);
|
|
39
|
+
const needle = String(search || '').trim().toLowerCase();
|
|
40
|
+
const registry = readRegistry(dataRoot);
|
|
41
|
+
const alive = aliveTaskStates(dataRoot);
|
|
42
|
+
const sessions = all
|
|
43
|
+
.filter((s) => (showAll || !s.subagent) && (showAll || !s.codexImport))
|
|
44
|
+
.filter((s) => {
|
|
45
|
+
if (filterRoots.length && !isUnder(s.cwd, filterRoots)) return false;
|
|
46
|
+
if (!needle) return true;
|
|
47
|
+
return `${s.title}\n${s.cwd}`.toLowerCase().includes(needle);
|
|
48
|
+
})
|
|
49
|
+
.map((s) => {
|
|
50
|
+
const rec = registry.imports[sourceKey(source, s.sessionId)];
|
|
51
|
+
const state = rec ? alive.get(rec.taskId) : null;
|
|
52
|
+
return {
|
|
53
|
+
...present(s),
|
|
54
|
+
imported: !!state,
|
|
55
|
+
importedArchived: !!(state && state.archived),
|
|
56
|
+
importedTaskId: state ? rec.taskId : '',
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
return { ok: true, sources: sourceList(), sessions };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export async function runImport({ dataRoot, source, items: rawItems, projectId = null }) {
|
|
63
|
+
if (!source || !sourceList().some((s) => s.id === source)) {
|
|
64
|
+
return { ok: false, code: 'bad-source', error: '未知来源' };
|
|
65
|
+
}
|
|
66
|
+
if (!sourceAvailable(source)) {
|
|
67
|
+
return { ok: false, code: 'source-unavailable', error: '没有找到该工具的会话目录' };
|
|
68
|
+
}
|
|
69
|
+
const items = Array.isArray(rawItems) ? rawItems : [];
|
|
70
|
+
if (!items.length) return { ok: false, code: 'bad-request', error: '没有选择会话' };
|
|
71
|
+
if (items.length > 1000) {
|
|
72
|
+
return { ok: false, code: 'too-many-items', error: '一次最多导入 1000 条,请分批选择' };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let projectRoot = '';
|
|
76
|
+
if (projectId) {
|
|
77
|
+
let project;
|
|
78
|
+
try {
|
|
79
|
+
project = getProject(dataRoot, projectId);
|
|
80
|
+
} catch (err) {
|
|
81
|
+
return { ok: false, code: err.code || 'index-error', error: err.message || String(err) };
|
|
82
|
+
}
|
|
83
|
+
if (!project || !project.roots.length) {
|
|
84
|
+
return { ok: false, code: 'project-not-found', error: '项目不存在或没有可用目录' };
|
|
85
|
+
}
|
|
86
|
+
projectRoot = project.roots.find((r) => r.primary)?.path || project.roots[0].path;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const roots = sourceRoots(source);
|
|
90
|
+
const registry = readRegistry(dataRoot);
|
|
91
|
+
const alive = aliveTaskIds(dataRoot);
|
|
92
|
+
const imported = [];
|
|
93
|
+
const skipped = [];
|
|
94
|
+
const failed = [];
|
|
95
|
+
|
|
96
|
+
for (const item of items) {
|
|
97
|
+
const sessionId = String(item && item.sessionId || '');
|
|
98
|
+
const sourcePath = String(item && item.sourcePath || '');
|
|
99
|
+
if (!sessionId || !sourcePath) {
|
|
100
|
+
failed.push({ sessionId, error: '缺少会话 id 或文件路径', code: 'bad-request' });
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (!isUnder(sourcePath, roots) || !existsSync(sourcePath)) {
|
|
104
|
+
failed.push({ sessionId, error: '会话文件不在来源目录内或已不存在', code: 'bad-source-path' });
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const key = sourceKey(source, sessionId);
|
|
108
|
+
const prev = registry.imports[key];
|
|
109
|
+
if (prev && alive.has(prev.taskId)) {
|
|
110
|
+
skipped.push({ sessionId, taskId: prev.taskId, reason: 'already-imported' });
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
const parsed = await readSourceSession(source, { ...item, sessionId, sourcePath });
|
|
115
|
+
if (parsed.sessionId && parsed.sessionId !== sessionId) {
|
|
116
|
+
throw Object.assign(new Error('会话 id 与文件内容不一致'), { code: 'session-mismatch' });
|
|
117
|
+
}
|
|
118
|
+
let workspace = workspaceForImport({ cwd: parsed.cwd, projectRoot });
|
|
119
|
+
if (!workspace) {
|
|
120
|
+
workspace = join(dataRoot, 'workspace', `draft-${randomUUID()}`);
|
|
121
|
+
mkdirSync(workspace, { recursive: true });
|
|
122
|
+
}
|
|
123
|
+
const result = writeImportedSession({
|
|
124
|
+
dataRoot, source, parsed, workspace, projectId: projectId || null,
|
|
125
|
+
});
|
|
126
|
+
registry.imports[key] = {
|
|
127
|
+
taskId: result.taskId,
|
|
128
|
+
sourcePath,
|
|
129
|
+
title: result.title,
|
|
130
|
+
importedAt: Date.now(),
|
|
131
|
+
};
|
|
132
|
+
writeRegistry(dataRoot, registry);
|
|
133
|
+
imported.push({ sessionId, taskId: result.taskId, title: result.title, workspace });
|
|
134
|
+
} catch (err) {
|
|
135
|
+
failed.push({ sessionId, error: err.message || String(err), code: err.code || 'import-failed' });
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return { ok: true, imported, skipped, failed };
|
|
140
|
+
}
|