dsh-session-history 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -30,7 +30,7 @@
30
30
 
31
31
  ## 功能
32
32
 
33
- - **多家 agent**:Claude Code、Codex、Antigravity、Pi、DSH、LobsterAI,以及 WorkBuddy(实验性)。会话格式与其中某一家相同的其他 agent,或同一 agent 的第二套配置,可以手动添加。
33
+ - **多家 agent**:Claude Code、Codex、Antigravity、Pi、DSH、OpenCode、LobsterAI,以及 WorkBuddy(实验性)。装好就自动检测,后装的也会自动出现。会话格式与其中某一家相同的其他 agent,或同一 agent 的第二套配置,可以「自动查找」或手动添加。
34
34
  - **检索以会话为单位**:会话标题和你的原话权重最高;命中太多时只列标题,并提示可以加哪些词收窄。繁简、全半角、大小写自动统一。
35
35
  - **原始记录兜底**:命令、参数、报错原文多藏在工具调用里,可以在单个会话的原始记录中定点查找。
36
36
  - **Claude Code 记忆提示**:检索时顺带列出对题的 Claude Code 记忆(整理过的结论),可先读这些。
@@ -55,7 +55,7 @@ dsh plugin --profile desktop add dsh-session-history
55
55
  装好后打开插件详情:
56
56
 
57
57
  1. 启用后约 20 秒会自动做第一次整理,也可以点 **立即整理**。首次要读全部历史会话,几百 MB 约需半分钟到一两分钟;之后只重读有变化的会话,通常一两秒。
58
- 2. 在 **技能接入** 里点 **建软链**(或 **全部接入**),让各 agent 能用上本技能。
58
+ 2. 在 **技能接入** 里点 **建软链**(或 **全部接入**),让各 agent 能用上本技能。几家共用一个技能文件夹时只需点一次。
59
59
  3. 新开一个对话,问一句“我们之前……”试试。
60
60
 
61
61
  不用 DSH 也能用:`npm i -g dsh-session-history`,再运行 `session-history setup`、`session-history refresh`、`session-history link all`。需要 Node 22.15 以上(推荐 24)。
@@ -63,15 +63,18 @@ dsh plugin --profile desktop add dsh-session-history
63
63
  ## 插件页
64
64
 
65
65
  - **数据目录**:位置、最近整理时间、会话与日志数量、整理是否出错;可打开目录或立即整理。
66
- - **Agents**:只列检测到的 agent(以及你添加的)。每家显示会话来源与数量、是否收录、技能是否生效。关掉「收录」只是不再整理它的新会话,已有日志照常检索。
67
- - **技能接入**:只有检测到、但技能还没生效的 agent 才需要操作。按它们读取的技能目录归组,一组一个按钮,写明点了之后哪些 agent 生效;已建的软链列在这里,可以移除。
68
- - **添加其他 agent**:见下节。
66
+ - **Agents**:只列检测到的 agent(以及你添加的),每次打开都会重新检测。每家显示会话来源与数量、是否收录、技能状态(已接入/待接入/冲突/不接入)。关掉「收录」只是不再整理它的新会话,已有日志照常检索。
67
+ - **技能接入**:只有检测到、但还没接入的 agent 才需要操作。按它们实际读取的文件夹归组,一组一个按钮,写明点了之后哪些 agent 生效;已建的软链列在这里,可以移除。
68
+ - **自动查找/手动添加**:见下节。
69
+ - **另有旧日志**:数据目录里有、但对应 agent 已不在的日志(比如卸载了某个 agent)。新装的用户一般没有这一行。
69
70
  - **设置**:整理频率、检索前是否补整理、日志里你的发言标题、日界、时区、不收录的目录、Claude Code 记忆提示、Git 快照。
70
71
  - **SKILL.md**:各 agent 实际读到的技能说明,可展开查看。
71
72
 
72
73
  ### 添加其他 agent
73
74
 
74
- 用于本插件未内置、但会话记录格式与某一家相同的 agent,或同一 agent 的另一套配置目录(多开、多账号)。要填:
75
+ 用于本插件未内置、但会话记录格式与某一家相同的 agent,或同一 agent 的另一套配置目录(多开、多账号)。
76
+
77
+ **自动查找** 会重新检测内置的 agent,并在几处已知位置(如 Qoder CLI、CodeBuddy Code)和家目录第一层的同名变体配置目录(如 `~/.claude-work`)里找能收录的会话,找到的一键添加;不会深入扫描你的文件。找不到的再 **手动添加**,要填:
75
78
 
76
79
  | 项 | 填什么 |
77
80
  |---|---|
@@ -86,6 +89,7 @@ dsh plugin --profile desktop add dsh-session-history
86
89
  | Codex | `sessions`(里面是 `<年>/<月>/<日>/rollout-*.jsonl`) | 另一个 `CODEX_HOME` 下的 `sessions` |
87
90
  | Pi | `sessions`(里面是 `<目录>/<会话>.jsonl`) | 另一个 Pi 配置目录下的 `sessions` |
88
91
  | DSH | `sessions`(里面是 `<目录>/<会话>/session.vN.jsonl.zstd`) | 另一个 `DSH_HOME` 下的 `sessions` |
92
+ | OpenCode | `opencode`(里面是 `opencode.db`) | 另一个 `XDG_DATA_HOME` 下的 `opencode` |
89
93
  | Antigravity | `brain`(里面是 `<会话>/.system_generated/logs/…`) | 第二个实例的 `antigravity-cli/brain` |
90
94
  | LobsterAI(OpenClaw) | `agents`(里面是 `<agent>/sessions/<会话>.jsonl`) | 其他基于 OpenClaw 的客户端 |
91
95
  | WorkBuddy(CodeBuddy) | `projects`(里面是 `<项目>/<会话>.jsonl`) | CodeBuddy Code 的 `~/.codebuddy/projects` |
@@ -103,10 +107,13 @@ dsh plugin --profile desktop add dsh-session-history
103
107
  | Antigravity | `~/.gemini/config/skills`(命令行)、`~/.gemini/antigravity/skills`(桌面版) |
104
108
  | Pi | `~/.pi/agent/skills`、`~/.agents/skills` |
105
109
  | DSH | `~/.dsh/skills`、`~/.agents/skills` |
110
+ | OpenCode | `~/.config/opencode/skills`、`~/.claude/skills`、`~/.agents/skills` |
106
111
  | LobsterAI | 应用数据目录下的 `LobsterAI/SKILLs`(可能还需在它的技能设置里启用) |
107
112
  | WorkBuddy | `~/.workbuddy/skills` |
108
113
 
109
- 很多人只维护一个技能文件夹,其余目录用软链指向它。插件按真实路径判断:几家实际读的是同一个文件夹时,只需一条软链,建好后它们一起生效。已有同名的独立文件夹(多半是旧版拷贝)时不会覆盖,会提示冲突。Windows 用目录联接,不需要管理员权限。
114
+ 很多人只维护一个技能文件夹,其余目录用软链指向它(例如 `~/.claude/skills`、`~/.agents/skills` 都指向 `~/my-skills`)。这时各 agent 读的其实是同一个文件夹,但本技能的文件夹(数据目录)不在里面,所以显示「待接入」是准确的:只需在那个共用文件夹里建一条 `session-history` 软链,它们就一起接入。
115
+
116
+ 判断是否已接入,是从每个 agent 自己的技能目录出发、顺着软链去读 `session-history/SKILL.md`,读得到、名字对才算,与 agent 加载技能走同一条路。已有同名的独立文件夹(多半是旧版拷贝)时不会覆盖,会提示冲突。Windows 用目录联接,不需要管理员权限。
110
117
 
111
118
  ## 数据与卸载
112
119
 
@@ -149,7 +156,7 @@ session-history refresh | status | setup | link all | unlink claude | config
149
156
  Git 子目录地址(不提供原生更新检查):
150
157
 
151
158
  ```text
152
- git+https://github.com/fujingzhai/youxia-dsh-plugins.git#dsh-session-history@0.2.0&path:/dsh-session-history
159
+ git+https://github.com/fujingzhai/youxia-dsh-plugins.git#dsh-session-history@0.2.1&path:/dsh-session-history
153
160
  ```
154
161
 
155
162
  ## 兼容与限制
@@ -158,7 +165,7 @@ git+https://github.com/fujingzhai/youxia-dsh-plugins.git#dsh-session-history@0.2
158
165
  - Windows:PowerShell/cmd 用 `bin\session-history.cmd`,Git Bash 用 `bin/session-history`,SKILL.md 会按平台写明;CI 在 Windows 上实际调用两种入口。Windows 版 DSH 的插件页尚未实测,Linux 只跑了 CI。
159
166
  - WorkBuddy 的会话格式按其内核 CodeBuddy Code 的公开代码推断,尚未用真实会话验证,所以标为实验性。
160
167
  - 只读取各 agent 的会话文件,从不修改它们。日志里的指令是历史数据,SKILL.md 已要求 agent 不得照做。
161
- - 日志不含思考过程与工具调用;这些要用 `raw` 回查原始记录,原始记录不在了就查不到。
168
+ - 日志不含思考过程与工具调用;这些要用 `raw` 回查原始记录,原始记录不在了就查不到。OpenCode 的原始记录存在数据库里,暂不支持 `raw`。
162
169
 
163
170
  ## 许可
164
171
 
package/client.js CHANGED
@@ -67,10 +67,10 @@ window.__ModuleLoader__.load({
67
67
  const num = n => Number(n || 0).toLocaleString('zh-CN');
68
68
 
69
69
  const STATE = {
70
- ready: ['技能已生效', a => `经 ${a.via}`],
71
- missing: ['技能未生效', () => '见下方「技能接入」'],
72
- conflict: ['技能冲突', a => `${a.conflict?.path} 已有同名的其他内容`],
73
- nodir: ['不接入技能', () => '没有设置技能目录,只收录会话'],
70
+ ready: ['已接入', a => `经 ${a.via} 读到本技能`],
71
+ missing: ['待接入', () => '它的技能目录里还没有 session-history,见下方「技能接入」'],
72
+ conflict: ['冲突', a => `${a.conflict?.path} 已有同名的其他内容`],
73
+ nodir: ['不接入', () => '没有设置技能目录,只收录会话'],
74
74
  };
75
75
 
76
76
  return {
@@ -142,12 +142,15 @@ window.__ModuleLoader__.load({
142
142
  const items = [];
143
143
  for (const t of targets) {
144
144
  const note = data.agents.find(a => t.agents.includes(a.name) && a.note)?.note;
145
+ const shared = t.agents.length > 1;
146
+ const where = t.create ? ['将新建技能目录 ', h('code', { key: 'p' }, t.display), ',']
147
+ : t.realDisplay ? [shared ? '它们读取的 ' : '它读取的 ', (t.paths || [t.display]).map((p, i) => [i ? '、' : '', h('code', { key: p }, p)]), ' 实际都是 ', h('code', { key: 'r' }, t.realDisplay), ',']
148
+ : ['技能目录 ', h('code', { key: 'p' }, t.display), ' '];
145
149
  items.push(h('div', { className: 'yxsh-item', key: 't' + t.key },
146
150
  h('div', null,
147
- h('div', null, h('b', null, t.agents.join('、')), ' 的技能尚未生效'),
148
- h('div', { className: 'yxsh-muted' }, t.create ? '将新建技能目录 ' : '在它们读取的技能目录 ', h('code', null, t.display),
149
- t.realDisplay ? ['(实际位置 ', h('code', { key: 'r' }, t.realDisplay), ')'] : null, t.realDisplay ? '里建一条指向数据目录的软链即可。' : ' 里建一条指向数据目录的软链即可。',
150
- note ? ` 注意:${note}。` : '')),
151
+ h('div', null, h('b', null, t.agents.join('、')), ':待接入'),
152
+ h('div', { className: 'yxsh-muted' }, ...where, '其中还没有 session-history。在这里建一条指向数据目录的软链,',
153
+ shared ? `这 ${t.agents.length} 家同时生效。` : '即可生效。', note ? ` 注意:${note}。` : '')),
151
154
  h('button', { className: 'yxsh-btn primary', disabled: !!pending, onClick: () => run({ op: 'linkTarget', key: t.key }, 'link') }, '建软链')));
152
155
  }
153
156
  for (const l of links) items.push(h('div', { className: 'yxsh-item', key: 'l' + l.key },
@@ -161,8 +164,9 @@ window.__ModuleLoader__.load({
161
164
  return h('div', { className: 'yxsh-card yxsh-access' },
162
165
  h('div', { className: 'yxsh-row' }, h('h4', { className: 'yxsh-grow' }, '技能接入'),
163
166
  targets.length > 1 ? h('button', { className: 'yxsh-btn primary', disabled: !!pending, onClick: () => run({ op: 'linkAll' }, 'link') }, '全部接入') : null),
164
- items.length ? items : h('div', { className: 'yxsh-muted' }, '检测到的 agent 都已能使用本技能。'),
165
- h('div', { className: 'yxsh-muted' }, '技能不拷副本:数据目录本身就是技能文件夹,在各 agent 读取的技能目录里放一条指向它的软链即可。几家共用同一个技能目录时只需一条;按钮只在有 agent 尚未生效时出现。'));
167
+ items.length ? items : h('div', { className: 'yxsh-muted' }, '检测到的 agent 都已接入本技能。'),
168
+ h('div', { className: 'yxsh-muted' }, '数据目录本身就是技能文件夹,不拷副本:在 agent 读取的技能目录里放一条指向它的软链即可。几家实际读同一个文件夹(比如都软链到你统一管理的技能目录)时,只需一条。'),
169
+ h('div', { className: 'yxsh-muted' }, '是否已接入,是从每个 agent 自己的技能目录出发、顺着软链去读 session-history/SKILL.md 来判断的,与 agent 加载技能走同一条路。'));
166
170
  }
167
171
 
168
172
  function AddAgent({ data, pending }) {
@@ -176,7 +180,26 @@ window.__ModuleLoader__.load({
176
180
  catch (error) { setCheck({ error: error.message }); }
177
181
  };
178
182
  const add = async () => { if (await run({ op: 'addCustom', ...form }, 'add')) { setOpen(false); setForm({ name: '', format: 'claude', sessions: '', skills: '' }); setCheck(null); } };
179
- if (!open) return h('div', { style: { marginTop: '10px' } }, h('button', { className: 'yxsh-btn', onClick: () => setOpen(true) }, '+ 添加其他 agent'));
183
+ const [found, setFound] = React.useState(null);
184
+ const find = async () => {
185
+ setFound({ busy: true });
186
+ try { const r = await api({ op: 'scan' }); if (alive) state.set({ data: r }); setFound({ list: r.found }); }
187
+ catch (error) { setFound({ error: error.message }); }
188
+ };
189
+ const quick = c => run({ op: 'addCustom', name: c.name, format: c.format, sessions: c.sessions, skills: c.skills || '' }, 'add')
190
+ .then(ok => { if (ok) setFound(f => ({ list: (f?.list || []).filter(x => x.sessions !== c.sessions) })); });
191
+ const results = !found ? null : found.busy ? h('div', { className: 'yxsh-muted' }, '查找中…')
192
+ : found.error ? h('div', { className: 'yxsh-error' }, found.error)
193
+ : !found.list.length ? h('div', { className: 'yxsh-muted' }, '没有找到其他可收录的 agent。内置支持的 agent 装好并产生对话后,会自动出现在上面。')
194
+ : h('div', { className: 'yxsh-card', style: { padding: '4px 14px', marginTop: '8px' } }, ...found.list.map(c => h('div', { className: 'yxsh-item', key: c.sessions, style: { padding: '8px 0' } },
195
+ h('div', null, h('div', null, h('b', null, c.name), h('span', { className: 'yxsh-muted' }, ` · ${data.formats.find(f => f.id === c.format)?.label || c.format} · ${num(c.files)} 个会话文件`)),
196
+ h('div', { className: 'yxsh-muted' }, h('code', null, c.sessions), c.skills ? [' 技能目录 ', h('code', { key: 's' }, c.skills)] : ' 未发现技能目录,只收录会话')),
197
+ h('button', { className: 'yxsh-btn', disabled: !!pending, onClick: () => quick(c) }, '添加'))));
198
+ if (!open) return h('div', { style: { marginTop: '10px' } },
199
+ h('div', { className: 'yxsh-row' },
200
+ h('button', { className: 'yxsh-btn', disabled: found?.busy, onClick: find, title: '重新检测内置 agent,并查找格式相同的其他 agent 或同一 agent 的其他配置目录' }, '自动查找'),
201
+ h('button', { className: 'yxsh-btn', onClick: () => setOpen(true) }, '手动添加')),
202
+ results);
180
203
  const verdict = !check ? null : check.error ? h('span', { className: 'yxsh-error' }, check.error)
181
204
  : !check.exists ? h('span', { className: 'yxsh-error' }, `目录不存在:${check.path}`)
182
205
  : check.files && check.turns ? h('span', null, `找到 ${num(check.files)} 个会话文件,格式对得上。如 `, h('code', null, check.sample))
@@ -205,13 +228,12 @@ window.__ModuleLoader__.load({
205
228
  const live = data.agents, ready = live.filter(a => a.state === 'ready').length, linkable = live.filter(a => a.state !== 'nodir').length;
206
229
  return h('section', null,
207
230
  h('div', { className: 'yxsh-head' }, h('h3', { className: 'yxsh-grow' }, 'Agents'),
208
- linkable ? h('span', { className: 'yxsh-muted' }, `技能已生效 ${ready}/${linkable}`) : null),
231
+ linkable ? h('span', { className: 'yxsh-muted' }, `已接入 ${ready}/${linkable}`) : null),
209
232
  live.length ? h('div', { className: 'yxsh-card' }, ...live.map(a => h(AgentRow, { a, pending, key: a.id })))
210
233
  : h('div', { className: 'yxsh-card', style: { padding: '14px' } }, h('span', { className: 'yxsh-muted' }, '还没有检测到支持的 agent。装好后产生过对话就会出现在这里;也可以在下面手动添加。')),
211
234
  live.length ? h(Access, { data, pending }) : null,
212
235
  h('div', { className: 'yxsh-notes' },
213
- data.undetected.length ? h('div', { className: 'yxsh-muted' }, `未检测到:${data.undetected.join('、')}(装好后自动出现)`) : null,
214
- data.legacy.length ? h('div', { className: 'yxsh-muted' }, `仅有旧日志:${data.legacy.join('、')}(照常检索)`) : null),
236
+ data.legacy.length ? h('div', { className: 'yxsh-muted' }, `另有旧日志:${data.legacy.join('、')}(对应的 agent 已不在,日志照常检索)`) : null),
215
237
  h(AddAgent, { data, pending }));
216
238
  }
217
239
 
package/index.js CHANGED
@@ -8,6 +8,7 @@ import { SOURCES, FORMATS, enabled, byId, allSources, customSource, folderFor }
8
8
  import { installSkill, skillMarkdown, logFolders, findNode, ENGINE, VERSION } from './lib/skill.js';
9
9
  import { skillStatus, linkTarget, linkAll, unlinkTarget } from './lib/links.js';
10
10
  import { tilde, expand, isDir } from './lib/files.js';
11
+ import { scan } from './lib/scan.js';
11
12
 
12
13
  export const name = 'session-history';
13
14
  export const ROUTE = '/youxia-session-history/api';
@@ -85,13 +86,13 @@ export function snapshot(home, runner) {
85
86
  run: run && { ok: run.ok, files: run.files, days: run.days, error: run.error || null },
86
87
  ...(a.custom ? { def: config.custom.find(c => c.id === a.id) } : {}) };
87
88
  });
88
- const known = new Set(allSources(config).map(s => s.folder));
89
+ // 只剩旧日志:日志文件夹对应的 agent 不在列表里(已卸载,或来自不再支持的来源)。新用户一般没有。
90
+ const listed = new Set(shown.map(a => a.folder));
89
91
  return {
90
92
  version: VERSION, home: tilde(home), homeAbs: home, config, last, running: runner?.running ?? false,
91
93
  agents: shown,
92
- undetected: status.agents.filter(a => !a.installed && !a.custom).map(a => a.name),
93
94
  targets: status.targets, links: status.links, conflicts: status.conflicts,
94
- legacy: logFolders(home).filter(f => !known.has(f)),
95
+ legacy: logFolders(home).filter(f => !listed.has(f)),
95
96
  formats: Object.entries(FORMATS).map(([id, f]) => ({ id, label: f.label, dir: f.dir, layout: f.layout, example: f.example })),
96
97
  git: existsSync(join(home, '.git')),
97
98
  skill: skillMarkdown(home, { config }),
@@ -148,6 +149,7 @@ export async function command(home, runner, input) {
148
149
  case 'linkAll': linkAll(home, config); break;
149
150
  case 'unlinkTarget': unlinkTarget(home, String(input.key), config); break;
150
151
  case 'probe': return { probe: probe(input.format, input.sessions) };
152
+ case 'scan': return { ...snapshot(home, runner), found: scan(config) };
151
153
  case 'addCustom': {
152
154
  const name = String(input.name || '').trim();
153
155
  if (!name || name.length > 24 || /[\n#]/.test(name)) throw invalid('名称须为 1–24 个字,不含 # 与换行');
package/lib/agents.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // agent 注册表:内置各家的会话来源(清洗用)与技能目录(接入技能用),外加用户手动添加的同格式 agent。
2
- import { dirname } from 'node:path';
2
+ import { dirname, join } from 'node:path';
3
3
  import claude from './sources/claude.js';
4
4
  import codex from './sources/codex.js';
5
5
  import antigravity from './sources/antigravity.js';
@@ -7,9 +7,10 @@ import pi from './sources/pi.js';
7
7
  import dsh from './sources/dsh.js';
8
8
  import lobsterai from './sources/lobsterai.js';
9
9
  import workbuddy from './sources/workbuddy.js';
10
+ import opencode from './sources/opencode.js';
10
11
  import { isDir, expand } from './files.js';
11
12
 
12
- export const SOURCES = [claude, codex, antigravity, pi, dsh, lobsterai, workbuddy];
13
+ export const SOURCES = [claude, codex, antigravity, pi, dsh, opencode, lobsterai, workbuddy];
13
14
 
14
15
  /**
15
16
  * 自定义 agent 可选的会话格式:与哪一家相同,会话目录选哪一层。
@@ -24,6 +25,8 @@ export const FORMATS = {
24
25
  dsh: { source: dsh, label: 'DSH 格式', dir: 'sessions', layout: '<目录>/<会话>/session.vN.jsonl.zstd', example: '另一个 DSH_HOME:~/.dsh-work/sessions' },
25
26
  antigravity: { source: antigravity, label: 'Antigravity 格式', dir: 'brain', layout: '<会话>/.system_generated/logs/transcript_full.jsonl',
26
27
  example: '第二个实例:~/.gemini-work/antigravity-cli/brain' },
28
+ opencode: { source: opencode, label: 'OpenCode 格式', dir: 'opencode', layout: 'opencode.db',
29
+ example: '另一个 XDG_DATA_HOME 下的 opencode 目录' },
27
30
  lobsterai: { source: lobsterai, label: 'LobsterAI(OpenClaw)格式', dir: 'agents', layout: '<agent>/sessions/<会话>.jsonl',
28
31
  example: '其他基于 OpenClaw 的客户端的 agents 目录' },
29
32
  workbuddy: { source: workbuddy, label: 'WorkBuddy(CodeBuddy)格式', dir: 'projects', layout: '<项目>/<会话>.jsonl',
@@ -49,7 +52,7 @@ export function customSource(def) {
49
52
  ...base, id: def.id, folder: def.folder, name: def.name, custom: true, format: def.format,
50
53
  locate() {
51
54
  const sessions = expand(def.sessions);
52
- return { base: dirname(sessions), sessions, projects: sessions, agentsRoot: sessions, roots: [sessions], cowork: null,
55
+ return { base: dirname(sessions), sessions, projects: sessions, agentsRoot: sessions, db: join(sessions, 'opencode.db'), roots: [sessions], cowork: null,
53
56
  marks: [sessions], skills: def.skills ? [expand(def.skills)] : [] };
54
57
  },
55
58
  render: (all, clock, userLabel) => base.render(all, clock, userLabel, def.name),
package/lib/links.js CHANGED
@@ -31,11 +31,14 @@ export function entryState(dir, home) {
31
31
  let st;
32
32
  try { st = lstatSync(entry); } catch { return { state: 'none', entry }; }
33
33
  const target = real(entry), homeReal = real(home);
34
+ // 只认 realpath 还不够:要像 agent 加载技能那样,从这个技能目录出发顺着软链读到 SKILL.md、名字对得上。
35
+ const readable = () => skillName(entry) === SKILL_NAME;
34
36
  if (st.isSymbolicLink()) {
35
37
  if (!target) return { state: 'broken', entry, target: readlinkSync(entry) };
36
- return { state: target === homeReal ? 'link' : (skillName(target) === SKILL_NAME ? 'copy' : 'other'), entry, target };
38
+ if (target === homeReal) return readable() ? { state: 'link', entry, target } : { state: 'broken', entry, target };
39
+ return { state: skillName(target) === SKILL_NAME ? 'copy' : 'other', entry, target };
37
40
  }
38
- if (target && target === homeReal) return { state: 'shared', entry, target };
41
+ if (target && target === homeReal) return readable() ? { state: 'shared', entry, target } : { state: 'broken', entry, target };
39
42
  return { state: st.isDirectory() && skillName(entry) === SKILL_NAME ? 'copy' : 'other', entry, target };
40
43
  }
41
44
 
@@ -75,6 +78,7 @@ export function skillStatus(home, config = {}) {
75
78
  for (const a of live.filter(x => x.state === 'missing' && !g.agents.includes(x.name))) if (a.dirs.some(d => d.real === g.key)) g.agents.push(a.name);
76
79
  const realPath = g.create ? null : realpathSafe(g.path);
77
80
  g.realDisplay = realPath && key(realPath) !== key(g.path) ? tilde(realPath) : null;
81
+ g.paths = [...new Set(live.filter(a => g.agents.includes(a.name)).flatMap(a => a.dirs.filter(d => d.real === g.key).map(d => d.display)))];
78
82
  }
79
83
  const links = new Map();
80
84
  for (const a of live) for (const d of a.dirs) if (d.state === 'link' && d.real && !links.has(d.real)) {
package/lib/refresh.js CHANGED
@@ -67,7 +67,7 @@ export function cleanSource(source, { home, config, cache, clock, log = () => {}
67
67
  try {
68
68
  for (const path of list) {
69
69
  let sig;
70
- try { sig = signature(path); } catch { continue; }
70
+ try { sig = (source.signature || signature)(path); } catch { continue; }
71
71
  const row = cached.get(path);
72
72
  let data;
73
73
  if (row && row.sig === sig && row.version === source.version) data = JSON.parse(row.data);
package/lib/scan.js ADDED
@@ -0,0 +1,55 @@
1
+ // 自动查找:内置 agent 每次读状态都会重新检测;这里另外找格式相同、但不在内置列表里的 agent,
2
+ // 以及同一 agent 的其他配置目录(多开、多账号)。只看固定的几处位置和家目录第一层,不深入扫描。
3
+ import { realpathSync } from 'node:fs';
4
+ import { join } from 'node:path';
5
+ import { home, entries, isDir, tilde } from './files.js';
6
+ import { allSources, pathsOf, customSource } from './agents.js';
7
+
8
+ // 已知的同格式 agent:会话目录确定;技能目录只写有官方依据的。
9
+ const KNOWN = [
10
+ { name: 'Qoder CLI', format: 'claude', sessions: '.qoder/projects' },
11
+ { name: 'Qoder CLI(国内版)', format: 'claude', sessions: '.qoder-cn/projects' },
12
+ { name: 'CodeBuddy Code', format: 'workbuddy', sessions: '.codebuddy/projects', skills: '.codebuddy/skills' },
13
+ ];
14
+
15
+ // 内置 agent 的配置目录换个后缀(~/.claude-work 之类),按同样的子路径找会话与技能目录。
16
+ const FAMILIES = [
17
+ { re: /^\.claude[-_.].+$/, label: 'Claude Code', format: 'claude', sessions: 'projects', skills: 'skills' },
18
+ { re: /^\.codex[-_.].+$/, label: 'Codex', format: 'codex', sessions: 'sessions', skills: 'skills' },
19
+ { re: /^\.dsh[-_.].+$/, label: 'DSH', format: 'dsh', sessions: 'sessions', skills: 'skills' },
20
+ { re: /^\.gemini[-_.].+$/, label: 'Antigravity', format: 'antigravity', sessions: 'antigravity-cli/brain', skills: 'config/skills' },
21
+ { re: /^\.pi[-_.].+$/, label: 'Pi', format: 'pi', sessions: 'agent/sessions', skills: 'agent/skills' },
22
+ { re: /^\.workbuddy[-_.].+$/, label: 'WorkBuddy', format: 'workbuddy', sessions: 'projects', skills: 'skills' },
23
+ ];
24
+
25
+ const real = path => { try { return realpathSync(path); } catch { return path; } };
26
+
27
+ /** 找到的候选:{ name, format, sessions, skills?, files, turns },已内置或已添加的不列。 */
28
+ export function scan(config = {}) {
29
+ const h = home();
30
+ const covered = new Set(allSources(config).flatMap(s => pathsOf(s, config).roots).filter(isDir).map(real));
31
+ const found = [];
32
+ const consider = c => {
33
+ const abs = join(h, c.sessions);
34
+ if (!isDir(abs) || covered.has(real(abs))) return;
35
+ const source = customSource({ id: 'scan', folder: 'scan', name: c.name, format: c.format, sessions: abs });
36
+ const files = source.files(source.locate());
37
+ if (!files.length) return;
38
+ let turns = 0;
39
+ for (const file of files.slice(0, 5)) { try { const d = source.parse(file); turns += (d.turns || d.records || d.sessions || []).length; } catch { /* 跳过 */ } }
40
+ if (!turns) return;
41
+ covered.add(real(abs));
42
+ found.push({ name: c.name.slice(0, 24), format: c.format, sessions: tilde(abs),
43
+ ...(c.skills && isDir(join(h, c.skills)) ? { skills: tilde(join(h, c.skills)) } : {}), files: files.length });
44
+ };
45
+ for (const c of KNOWN) consider(c);
46
+ for (const d of entries(h)) {
47
+ const family = FAMILIES.find(f => f.re.test(d.name));
48
+ if (!family || !isDir(join(h, d.name))) continue;
49
+ // 名称取配置目录的后缀:~/.claude-work → Claude Code(work)。
50
+ const suffix = d.name.replace(/^\.[a-z]+[-_.]/i, '').slice(0, 24 - family.label.length - 2);
51
+ consider({ name: `${family.label}(${suffix})`, format: family.format,
52
+ sessions: join(d.name, family.sessions), skills: join(d.name, family.skills) });
53
+ }
54
+ return found;
55
+ }
@@ -0,0 +1,125 @@
1
+ // OpenCode:会话存在 SQLite 库 ~/.local/share/opencode/opencode.db(session/message/part 三表),只读打开。
2
+ // 标题行沿用 OpenCode 自带的会话标题:「# 标题(ses_xxxx)」。
3
+ import { join } from 'node:path';
4
+ import { statSync } from 'node:fs';
5
+ import { DatabaseSync } from 'node:sqlite';
6
+ import { home, expand, isFile } from '../files.js';
7
+ import { parseJson, strip, isObject } from '../py.js';
8
+ import { demote } from '../markdown.js';
9
+ import { excluded, str } from './common.js';
10
+
11
+ const dataHome = env => env.XDG_DATA_HOME ? expand(env.XDG_DATA_HOME) : join(home(), '.local/share');
12
+ const configHome = env => env.XDG_CONFIG_HOME ? expand(env.XDG_CONFIG_HOME) : join(home(), '.config');
13
+ const agentsHome = env => env.AGENTS_HOME ? expand(env.AGENTS_HOME) : join(home(), '.agents');
14
+
15
+ export function locate(env = process.env) {
16
+ const base = join(dataHome(env), 'opencode');
17
+ return { base, db: join(base, 'opencode.db'), roots: [base], marks: [base],
18
+ skills: [join(configHome(env), 'opencode/skills'), join(home(), '.claude/skills'), join(agentsHome(env), 'skills')] };
19
+ }
20
+
21
+ export function files(paths) {
22
+ const db = paths.db || join(paths.base, 'opencode.db');
23
+ return isFile(db) ? [db] : [];
24
+ }
25
+
26
+ /** 库在 WAL 模式下,新写入可能还在 -wal 里,签名要把它算上。 */
27
+ export function signature(path) {
28
+ const parts = [];
29
+ for (const p of [path, `${path}-wal`]) { try { const s = statSync(p, { bigint: true }); parts.push(`${s.mtimeNs}:${s.size}`); } catch { parts.push('-'); } }
30
+ return parts.join('|');
31
+ }
32
+
33
+ const THINK = /<think\b[^>]*>[\s\S]*?<\/think>\s*/g;
34
+ const TEMP = ['/private/tmp', '/tmp', '/.tmp/'];
35
+
36
+ export function parse(path) {
37
+ const db = new DatabaseSync(path, { readOnly: true });
38
+ try {
39
+ const sessions = {};
40
+ for (const r of db.prepare('SELECT id, title, directory FROM session').all()) sessions[r.id] = [r.title || '', r.directory || ''];
41
+ const rows = db.prepare(`SELECT m.session_id AS sid, m.time_created AS ms, json_extract(m.data, '$.role') AS role,
42
+ json_extract(p.data, '$.type') AS ptype, p.data AS pdata FROM part p JOIN message m ON p.message_id = m.id
43
+ ORDER BY m.time_created, p.time_created`).all();
44
+ const per = new Map();
45
+ let cur = null, pieces = [];
46
+ const flush = () => {
47
+ if (cur && pieces.length) { if (!per.has(cur.sid)) per.set(cur.sid, []); per.get(cur.sid).push([cur.ms, cur.role, pieces.join('\n\n')]); }
48
+ pieces = [];
49
+ };
50
+ for (const r of rows) {
51
+ if (!cur || r.sid !== cur.sid || r.ms !== cur.ms || r.role !== cur.role) { flush(); cur = { sid: r.sid, ms: Number(r.ms), role: r.role }; }
52
+ const d = parseJson(r.pdata);
53
+ if (!isObject(d)) continue;
54
+ if (r.ptype === 'text') {
55
+ let t = strip(str(d.text));
56
+ if (r.role === 'assistant') t = strip(t.replace(THINK, ''));
57
+ if (t) pieces.push(t);
58
+ } else if (r.ptype === 'file') {
59
+ const name = d.filename || '';
60
+ pieces.push(name ? `[🖼️ 图片: ${name}]` : '[🖼️ 图片]');
61
+ }
62
+ }
63
+ flush();
64
+ const out = [];
65
+ for (const [sid, turns] of per) {
66
+ if (!sessions[sid] || !turns.some(t => t[1] === 'user')) continue;
67
+ out.push({ sid, title: sessions[sid][0], directory: sessions[sid][1], turns });
68
+ }
69
+ return { sessions: out };
70
+ } finally { db.close(); }
71
+ }
72
+
73
+ export function build(entries, options = {}) {
74
+ const all = [];
75
+ for (const { data } of entries) for (const s of data.sessions) {
76
+ if (TEMP.some(p => s.directory.includes(p)) || excluded(s.directory, options.excludeCwds)) continue;
77
+ const key = `${strip(s.title) || s.sid.slice(0, 8)}(${s.sid.slice(0, 8)})`;
78
+ for (const [ms, role, text] of s.turns) all.push([ms, role, text, key]);
79
+ }
80
+ return { turns: all, titles: {}, count: all.length };
81
+ }
82
+
83
+ const core = text => text.split('\n').map(l => strip(l)).filter(s => s && !s.startsWith('MEDIA:') && !s.startsWith('[🖼️') && !s.startsWith('[📎')).join('\n');
84
+ function merge(turns) {
85
+ const out = [];
86
+ for (const [ts, role, text] of turns) {
87
+ const last = out[out.length - 1];
88
+ if (last && last[1] === role) {
89
+ const prev = core(last[2]), c = core(text);
90
+ if (c === prev || (c && prev.includes(c))) continue;
91
+ if (prev && c.includes(prev)) { last[2] = text; continue; }
92
+ last[2] += '\n\n' + text;
93
+ } else out.push([ts, role, text]);
94
+ }
95
+ return out;
96
+ }
97
+
98
+ export function render(all, clock, userLabel, name = 'OpenCode') {
99
+ const days = new Map();
100
+ for (const [ms, role, text, key] of all) {
101
+ const day = clock.day(ms);
102
+ if (!days.has(day)) days.set(day, new Map());
103
+ const sessions = days.get(day);
104
+ if (!sessions.has(key)) sessions.set(key, []);
105
+ sessions.get(key).push([ms, role, text]);
106
+ }
107
+ const bodies = new Map();
108
+ for (const [day, sessions] of days) {
109
+ const out = [];
110
+ for (const [key, turns] of [...sessions].sort((a, b) => a[1][0][0] - b[1][0][0])) {
111
+ out.push(`# ${key}\n`);
112
+ for (const [ms, role, text] of merge(turns)) {
113
+ out.push(`## ${clock.hm(ms)} ${role === 'user' ? `🧑 ${userLabel}` : `🤖 ${name}`}\n`);
114
+ out.push(demote(text) + '\n');
115
+ }
116
+ }
117
+ bodies.set(day, out.join('\n'));
118
+ }
119
+ return bodies;
120
+ }
121
+
122
+ // 原始记录在 SQLite 里,不是逐行的 JSONL;raw 暂不支持 OpenCode。
123
+ export const rawFiles = () => [];
124
+
125
+ export default { id: 'opencode', folder: 'OpenCode', name: 'OpenCode', version: 1, locate, files, signature, parse, build, render, rawFiles };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-session-history",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "把各个 agent(Claude Code、Codex 等)的对话整理成可检索的原文日志,以一个共用技能提供给所有 agent;保留原话,不做摘要,不用向量,不调用模型。",
5
5
  "author": "youxia",
6
6
  "repository": {