@kioku-memory/cli 0.2.0 → 0.4.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.
Files changed (62) hide show
  1. package/NOTICE +3 -0
  2. package/README.md +105 -25
  3. package/assets/plugins/agy/README.md +34 -0
  4. package/assets/plugins/agy/bin/kioku +36 -0
  5. package/assets/plugins/agy/hooks/forward.mjs +232 -0
  6. package/assets/plugins/agy/hooks.json +25 -0
  7. package/assets/plugins/agy/lib/kioku.mjs +53 -0
  8. package/{lib → assets/plugins/agy/lib}/lifecycle.mjs +13 -3
  9. package/{lib/kioku.mjs → assets/plugins/agy/lib/run.mjs} +4 -80
  10. package/assets/plugins/agy/package.json +7 -0
  11. package/assets/plugins/agy/plugin.json +6 -0
  12. package/assets/plugins/agy/vitest.config.mjs +5 -0
  13. package/assets/plugins/gemini/README.md +33 -0
  14. package/assets/plugins/gemini/bin/kioku +36 -0
  15. package/assets/plugins/gemini/gemini-extension.json +5 -0
  16. package/assets/plugins/gemini/hooks/forward.mjs +174 -0
  17. package/assets/plugins/gemini/hooks/hooks.json +43 -0
  18. package/assets/plugins/gemini/lib/command.mjs +152 -0
  19. package/assets/plugins/gemini/lib/config.mjs +50 -0
  20. package/assets/plugins/gemini/lib/http.mjs +57 -0
  21. package/assets/plugins/gemini/lib/kioku.mjs +53 -0
  22. package/assets/plugins/gemini/lib/lifecycle.mjs +128 -0
  23. package/assets/plugins/gemini/lib/mcp.mjs +142 -0
  24. package/assets/plugins/gemini/lib/profile.mjs +100 -0
  25. package/assets/plugins/gemini/lib/redact.mjs +91 -0
  26. package/assets/plugins/gemini/lib/run.mjs +372 -0
  27. package/assets/plugins/gemini/lib/text.mjs +14 -0
  28. package/assets/plugins/gemini/lib/usage.mjs +2 -0
  29. package/assets/plugins/gemini/package.json +7 -0
  30. package/assets/plugins/gemini/vitest.config.mjs +5 -0
  31. package/bin/kioku +16 -12
  32. package/dist/index.mjs +8846 -0
  33. package/package.json +14 -5
  34. package/lib/api.mjs +0 -38
  35. package/lib/codex.mjs +0 -320
  36. package/lib/connect.mjs +0 -152
  37. package/lib/doctor.mjs +0 -145
  38. package/lib/dsh.mjs +0 -82
  39. package/lib/files.mjs +0 -92
  40. package/lib/hermes.mjs +0 -276
  41. package/lib/import.mjs +0 -301
  42. package/lib/install.mjs +0 -146
  43. package/lib/local.mjs +0 -294
  44. package/lib/login.mjs +0 -347
  45. package/lib/openclaw.mjs +0 -256
  46. package/lib/panel.mjs +0 -295
  47. package/lib/pi.mjs +0 -90
  48. package/lib/platforms.mjs +0 -220
  49. package/lib/prompts.mjs +0 -38
  50. package/lib/secret.mjs +0 -86
  51. package/lib/setup.mjs +0 -112
  52. package/lib/sources.mjs +0 -333
  53. package/lib/target.mjs +0 -62
  54. package/lib/wizard.mjs +0 -447
  55. /package/{lib → assets/plugins/agy/lib}/command.mjs +0 -0
  56. /package/{lib → assets/plugins/agy/lib}/config.mjs +0 -0
  57. /package/{lib → assets/plugins/agy/lib}/http.mjs +0 -0
  58. /package/{lib → assets/plugins/agy/lib}/mcp.mjs +0 -0
  59. /package/{lib → assets/plugins/agy/lib}/profile.mjs +0 -0
  60. /package/{lib → assets/plugins/agy/lib}/redact.mjs +0 -0
  61. /package/{lib → assets/plugins/agy/lib}/text.mjs +0 -0
  62. /package/{lib → assets/plugins/agy/lib}/usage.mjs +0 -0
package/lib/wizard.mjs DELETED
@@ -1,447 +0,0 @@
1
- // 在终端里不带参数运行 kioku 时的向导:一个状态面板,不是一条直线。先扫描服务器与本机各平台的现状,
2
- // 再按缺什么列出动作;每个动作先列计划、确认后执行,做完回到面板刷新。
3
- // 交互(选择、确认、输入)经 ui 这一层,接口照 @clack/prompts,用例里换成假的;
4
- // 各动作的实际步骤都是别的子命令已有的那一套(setup、login、import、doctor)
5
-
6
- import { loadConfig } from './config.mjs'
7
- import { connectPlatforms, pluginSteps, resultsText } from './connect.mjs'
8
- import { doctor } from './doctor.mjs'
9
- import { keyFormatError, KiokuFailure } from './http.mjs'
10
- import { pendingOf, planImport, writeImport } from './import.mjs'
11
- import {
12
- composeState,
13
- composeUp,
14
- defaultLocalDir,
15
- expandDir,
16
- hasCompose,
17
- healthy,
18
- latestRelease,
19
- LOCAL_PORT,
20
- LOCAL_URL,
21
- manualDownload,
22
- portBusy,
23
- prepareDir,
24
- setupCodeIn,
25
- setupDone,
26
- waitUntil,
27
- } from './local.mjs'
28
- import { openBrowser } from './login.mjs'
29
- import {
30
- actionsFor,
31
- canDisable,
32
- connectCandidates,
33
- defaultServerUrl,
34
- renderPanel,
35
- scan,
36
- serverHealth,
37
- } from './panel.mjs'
38
- import { PLATFORMS } from './platforms.mjs'
39
- import { serverUrlProblem } from './profile.mjs'
40
- import { setup } from './setup.mjs'
41
- import { shownUrl } from './text.mjs'
42
-
43
- // 收下一段输出,给计划预览用
44
- export function bufferOut() {
45
- let text = ''
46
- return {
47
- write(chunk) {
48
- text += chunk
49
- return true
50
- },
51
- text: () => text.replace(/\n+$/, ''),
52
- }
53
- }
54
-
55
- // 接在向导的竖线下面写:每行前面加「│ 」,和 @clack/prompts 的排版对齐
56
- export function barOut(stream) {
57
- let atLineStart = true
58
- return {
59
- write(chunk) {
60
- let text = ''
61
- for (const char of String(chunk)) {
62
- if (atLineStart) text += char === '\n' ? '│' : '│ '
63
- text += char
64
- atLineStart = char === '\n'
65
- }
66
- return stream.write(text)
67
- },
68
- }
69
- }
70
-
71
- // 每个动作的默认依赖,用例里换成假的
72
- const DEPS = {
73
- scan,
74
- serverHealth,
75
- defaultServerUrl,
76
- connectPlatforms,
77
- pluginSteps,
78
- setup,
79
- doctor,
80
- planImport,
81
- writeImport,
82
- loadConfig,
83
- open: openBrowser,
84
- local: {
85
- composeState,
86
- composeUp,
87
- hasCompose,
88
- healthy,
89
- latestRelease,
90
- portBusy,
91
- prepareDir,
92
- setupCodeIn,
93
- setupDone,
94
- waitUntil,
95
- },
96
- }
97
-
98
- const cancelled = (ui, value) => ui.isCancel(value)
99
-
100
- async function chooseServer(ctx) {
101
- const { ui, deps } = ctx
102
- const value = await ui.text({
103
- message: 'Kioku 的服务地址',
104
- placeholder: 'https://kioku.example.com',
105
- ...(ctx.url ? { initialValue: ctx.url } : {}),
106
- validate: (text) => serverUrlProblem((text ?? '').trim().replace(/\/+$/, '')) ?? undefined,
107
- })
108
- if (cancelled(ui, value)) return
109
- const url = value.trim().replace(/\/+$/, '')
110
- const health = await deps.serverHealth(url)
111
- if (!health.reachable) {
112
- const keep = await ui.confirm({
113
- message: `${shownUrl(url)} 无法连接(${health.problem}),仍然用这个地址?`,
114
- initialValue: false,
115
- })
116
- if (cancelled(ui, keep) || !keep) return
117
- }
118
- ctx.url = url
119
- }
120
-
121
- // 接入(或重新授权)几个平台:浏览器确认一次,签发的访问密钥照 kioku setup 写下
122
- async function connect(ctx, rows, { reauth = false } = {}) {
123
- const { ui, deps, env, cwd, out } = ctx
124
- if (!rows.length) return
125
- const picked = await ui.multiselect({
126
- message: reauth ? '重新授权哪些平台' : '接入哪些平台',
127
- options: rows.map((row) => ({
128
- value: row.id,
129
- label: row.name,
130
- ...(row.present ? {} : { hint: '本机没发现' }),
131
- })),
132
- initialValues: rows.filter((row) => reauth || row.present).map((row) => row.id),
133
- })
134
- if (cancelled(ui, picked) || !picked.length) return
135
- const plan = picked.map((id) => {
136
- const row = rows.find((item) => item.id === id)
137
- const memory = row.memory === 'none' ? '' : ',停用自带的长期记忆'
138
- const moved = row.otherServer ? `;原来接到 ${shownUrl(row.url)}` : ''
139
- return `${PLATFORMS[id].name}:签发访问密钥,写下配置${memory}${moved}`
140
- })
141
- ui.note(
142
- [`服务器 ${shownUrl(ctx.url)}`, ...plan, '', '浏览器里确认一次,访问密钥不显示'].join('\n'),
143
- '计划',
144
- )
145
- const ok = await ui.confirm({ message: '开始?' })
146
- if (cancelled(ui, ok) || !ok) return
147
- const results = await deps.connectPlatforms({ url: ctx.url, ids: picked }, { env, out, cwd })
148
- const done = results.filter((item) => item.status === 'connected').map((item) => item.id)
149
- ui.note(
150
- done.length ? resultsText(results) : `一个平台都没有接上\n${resultsText(results)}`,
151
- '结果',
152
- )
153
- const steps = await deps.pluginSteps(done, { env, cwd })
154
- if (steps) ui.note(steps, '装插件')
155
- }
156
-
157
- // 接入 Claude Code:它的访问密钥只认 /plugin 里填的,浏览器授权不给它发。列出安装命令与要填的服务地址,
158
- // 顺带停用自动记忆(项目设置又启用了的,说明要在那里改)
159
- async function connectClaude(ctx, row) {
160
- const { ui, deps, env, cwd } = ctx
161
- const steps = await deps.pluginSteps(['claude-code'], { env, cwd, url: ctx.url })
162
- const memory = row.memory === 'on' && !row.reenabledIn
163
- if (row.reenabledIn) {
164
- ui.log.warn(
165
- `Claude Code 的自动记忆被 ./${row.reenabledIn} 重新启用:要在那里把 autoMemoryEnabled 去掉`,
166
- )
167
- }
168
- if (memory) {
169
- const preview = bufferOut()
170
- await deps.setup(['claude-code', '--dry-run'], { env, out: preview, cwd, login: null })
171
- ui.note(
172
- [
173
- '停用自动记忆',
174
- preview.text().replace(/\n?没有写入(--dry-run)$/, ''),
175
- '',
176
- '插件与访问密钥在 Claude Code 的 /plugin 里装、填(浏览器授权不给它发访问密钥),做完列出步骤',
177
- ].join('\n'),
178
- '计划',
179
- )
180
- const ok = await ui.confirm({ message: '开始?' })
181
- if (cancelled(ui, ok) || !ok) return
182
- await deps.setup(['claude-code'], { env, out: ctx.out, cwd, login: null })
183
- }
184
- if (steps) ui.note(steps, '装插件')
185
- }
186
-
187
- // 先用 --dry-run 看要改什么,确认后再做
188
- async function previewThenRun(ctx, args, question) {
189
- const { ui, deps, env, cwd } = ctx
190
- const preview = bufferOut()
191
- await deps.setup([...args, '--dry-run'], { env, out: preview, cwd, login: null })
192
- ui.note(preview.text().replace(/\n?没有写入(--dry-run)$/, ''), '计划')
193
- const ok = await ui.confirm({ message: question })
194
- if (cancelled(ui, ok) || !ok) return
195
- await deps.setup(args, { env, out: ctx.out, cwd, login: null })
196
- }
197
-
198
- // 迁移一个平台的本地记忆:照 kioku import 列计划(这时才和 Kioku 逐条比对)、确认后写入。
199
- // 比出来的未迁移条数记在这次向导里,面板上显示;写入之后作废
200
- async function migrate(ctx, id) {
201
- const { ui, deps, env, out } = ctx
202
- const platform = PLATFORMS[id]
203
- let config
204
- if (platform.readConfig) config = await platform.readConfig(env)
205
- else {
206
- // Claude Code 的访问密钥在它的 /plugin 里,读不到:Claude Code 的会话里有环境变量,否则请用户输入
207
- config = await deps.loadConfig(env).catch(() => null)
208
- if (!config || config.url !== ctx.url) {
209
- const key = await ui.password({
210
- message: `${platform.name} 用的访问密钥(记忆迁进它所属的应用)`,
211
- validate: (text) => keyFormatError((text ?? '').trim()) ?? undefined,
212
- })
213
- if (cancelled(ui, key)) return
214
- config = { url: ctx.url, key: key.trim() }
215
- }
216
- }
217
- if (!config) throw new KiokuFailure(`还没有给 ${platform.name} 配过:先接入`)
218
- const plan = await deps.planImport(config, { from: platform.source }, { env, out })
219
- if (plan.empty) {
220
- ctx.pending.set(id, { pending: 0 })
221
- return
222
- }
223
- ctx.pending.set(id, pendingOf(plan))
224
- if (!plan.todo.length) {
225
- ui.log.info('没有要写的')
226
- return
227
- }
228
- const ok = await ui.confirm({ message: `写入这 ${plan.todo.length} 条?` })
229
- if (cancelled(ui, ok) || !ok) return
230
- ctx.pending.delete(id)
231
- await deps.writeImport(config, plan, { out })
232
- }
233
-
234
- async function disable(ctx, state) {
235
- const { ui } = ctx
236
- const rows = state.platforms.filter(canDisable)
237
- const id = await ui.select({
238
- message: '停用哪个平台(恢复自带记忆、清掉配置)',
239
- options: rows.map((row) => ({ value: row.id, label: row.name })),
240
- })
241
- if (cancelled(ui, id)) return
242
- await previewThenRun(ctx, [id, '--undo'], `停用 ${PLATFORMS[id].name}?`)
243
- }
244
-
245
- async function check(ctx) {
246
- const { ui, deps, env, cwd, out } = ctx
247
- const id = await ui.select({
248
- message: '检查哪个平台',
249
- options: Object.entries(PLATFORMS).map(([value, platform]) => ({
250
- value,
251
- label: platform.name,
252
- })),
253
- })
254
- if (cancelled(ui, id)) return
255
- await deps.doctor([id], { env, out, cwd })
256
- }
257
-
258
- // 打开管理界面,等第一个管理员建好
259
- async function awaitSetup(ctx, url, code) {
260
- const { ui, deps } = ctx
261
- if (code) ui.note(`初始化码:${code}\n在 ${url} 输入它,建第一个管理员`, '初始化')
262
- deps.open(url)
263
- const spin = ui.spinner()
264
- spin.start(`等待在 ${url} 建好管理员(Ctrl-C 退出)`)
265
- const done = await deps.local.waitUntil(() => deps.local.setupDone(url), {
266
- timeoutMs: 60 * 60_000,
267
- intervalMs: 3000,
268
- })
269
- spin.stop(done ? '管理员已建好' : '没有等到初始化完成')
270
- return done
271
- }
272
-
273
- // 等这个目录里的 Kioku 就绪
274
- async function waitHealthy(ctx, dir, spin) {
275
- spin.message?.('等待 Kioku 就绪')
276
- const up = await ctx.deps.local.waitUntil(() => ctx.deps.local.healthy(LOCAL_URL), {
277
- timeoutMs: 180_000,
278
- })
279
- spin.stop(up ? 'Kioku 已就绪' : 'Kioku 没有就绪')
280
- if (!up) {
281
- throw new KiokuFailure(
282
- `Kioku 在 3 分钟内没有就绪:运行 cd ${dir} && docker compose logs kioku 看原因`,
283
- )
284
- }
285
- }
286
-
287
- // docker compose up -d,等它就绪
288
- async function bringUp(ctx, dir) {
289
- const spin = ctx.ui.spinner()
290
- spin.start('docker compose up -d(第一次要拉镜像,可能要几分钟)')
291
- try {
292
- await ctx.deps.local.composeUp(dir, ctx.env)
293
- } catch (err) {
294
- spin.stop('没有起来')
295
- throw err
296
- }
297
- await waitHealthy(ctx, dir, spin)
298
- }
299
-
300
- // 本机起一个 Kioku:放好 compose.yaml 与 .env,docker compose up -d,等它就绪、等用户建好管理员。
301
- // 目录里已经有这个 compose 项目(上次起过)时不再判端口、不重新准备,接着启动或等它就绪
302
- async function startLocal(ctx) {
303
- const { ui, deps, env } = ctx
304
- const local = deps.local
305
- if (await local.healthy(LOCAL_URL)) {
306
- ui.log.info(`本机 ${LOCAL_PORT} 端口上已经有 Kioku 在运行,直接用它`)
307
- ctx.url = LOCAL_URL
308
- return
309
- }
310
- if (!(await local.hasCompose(env))) {
311
- throw new KiokuFailure('没有找到 docker compose:先装好 Docker(含 compose 插件)再来')
312
- }
313
- const answer = await ui.text({
314
- message: '放在哪个目录',
315
- initialValue: defaultLocalDir(env),
316
- validate: (text) => ((text ?? '').trim() ? undefined : '要写目录'),
317
- })
318
- if (cancelled(ui, answer)) return
319
- const dir = expandDir(answer.trim(), env)
320
-
321
- const existing = await local.composeState(dir, env)
322
- let code
323
- if (existing === 'running') {
324
- ui.log.info(`${dir} 里的 Kioku 已经在运行,等它就绪`)
325
- const spin = ui.spinner()
326
- spin.start('等待 Kioku 就绪')
327
- await waitHealthy(ctx, dir, spin)
328
- code = await local.setupCodeIn(dir)
329
- } else if (existing === 'stopped') {
330
- ui.note(`目录 ${dir}\n这里的 Kioku 建过、没在运行:docker compose up -d`, '计划')
331
- const ok = await ui.confirm({ message: '启动?' })
332
- if (cancelled(ui, ok) || !ok) return
333
- await bringUp(ctx, dir)
334
- code = await local.setupCodeIn(dir)
335
- } else {
336
- if (await local.portBusy(LOCAL_PORT)) {
337
- throw new KiokuFailure(
338
- `端口 ${LOCAL_PORT} 被别的程序占着:先停掉它(lsof -i :${LOCAL_PORT} 看是哪个),或者改 compose.yaml 里的端口后自己启动`,
339
- )
340
- }
341
- const release = await local.latestRelease()
342
- if (!release.release) {
343
- ui.log.warn(
344
- `没取到最新的正式版(${release.problem}):用 main 上的 compose.yaml 与 edge 镜像(main 上最新的提交,不保证稳定)`,
345
- )
346
- }
347
- ui.note(
348
- [
349
- `目录 ${dir}`,
350
- `版本 ${release.imageTag}${release.release ? '(最新的正式版)' : ''}`,
351
- `下载 ${release.ref} 的 compose.yaml、.env.example(已有的不覆盖)`,
352
- `生成 .env:数据库密码、主密钥、初始化码随机生成,镜像固定在 ${release.imageTag},只给本人读写(已有的不覆盖)`,
353
- `docker compose up -d,端口只开在 127.0.0.1:${LOCAL_PORT}`,
354
- ].join('\n'),
355
- '计划',
356
- )
357
- const ok = await ui.confirm({ message: '开始?' })
358
- if (cancelled(ui, ok) || !ok) return
359
-
360
- let prepared
361
- try {
362
- prepared = await local.prepareDir(dir, release)
363
- } catch (err) {
364
- if (!(err instanceof KiokuFailure)) throw err
365
- ui.note(manualDownload(dir, release.ref), '手动下载')
366
- throw err
367
- }
368
- if (prepared.env.written) {
369
- ui.log.warn(
370
- `主密钥在 ${prepared.env.file} 的 KIOKU_MASTER_KEY:另外保存一份,和数据库备份分开放;丢了机密记忆与托管凭据解不开`,
371
- )
372
- } else ui.log.info(`${prepared.env.file} 已经有了,沿用`)
373
- await bringUp(ctx, dir)
374
- code = prepared.env.setupCode
375
- }
376
- ctx.url = LOCAL_URL
377
- if (await local.setupDone(LOCAL_URL)) return 'connect'
378
- if (!code) ui.log.info(`初始化码看日志:cd ${dir} && docker compose logs kioku`)
379
- if (await awaitSetup(ctx, LOCAL_URL, code)) return 'connect'
380
- }
381
-
382
- async function perform(ctx, choice, state) {
383
- if (choice === 'server') return chooseServer(ctx)
384
- if (choice === 'local') {
385
- if ((await startLocal(ctx)) === 'connect') {
386
- const next = await ctx.deps.scan({ env: ctx.env, cwd: ctx.cwd, url: ctx.url })
387
- return connect(ctx, connectCandidates(next))
388
- }
389
- return
390
- }
391
- if (choice === 'open') {
392
- await awaitSetup(ctx, ctx.url, null)
393
- return
394
- }
395
- if (choice === 'connect') return connect(ctx, connectCandidates(state))
396
- if (choice === 'reauth') {
397
- return connect(
398
- ctx,
399
- state.platforms.filter((row) => row.connection === 'invalid'),
400
- { reauth: true },
401
- )
402
- }
403
- if (choice === 'claude-connect') {
404
- return connectClaude(
405
- ctx,
406
- state.platforms.find((row) => row.id === 'claude-code'),
407
- )
408
- }
409
- if (choice === 'claude-memory') {
410
- return previewThenRun(ctx, ['claude-code'], '停用 Claude Code 的自动记忆?')
411
- }
412
- if (choice.startsWith('import:')) return migrate(ctx, choice.slice('import:'.length))
413
- if (choice === 'disable') return disable(ctx, state)
414
- if (choice === 'doctor') return check(ctx)
415
- }
416
-
417
- // 向导的主循环,返回退出码
418
- export async function wizard({ ui, env, cwd, out, deps = {} }) {
419
- // pending:选过迁移的平台比出来的未迁移条数(平台 id → pendingOf 的结果),只在这次向导里有效
420
- const ctx = { ui, env, cwd, out, deps: { ...DEPS, ...deps }, url: null, pending: new Map() }
421
- ctx.url = await ctx.deps.defaultServerUrl(env)
422
- ui.intro('Kioku')
423
- for (;;) {
424
- const spin = ui.spinner()
425
- spin.start('读取现状')
426
- const state = await ctx.deps.scan({ env, cwd, url: ctx.url })
427
- for (const row of state.platforms) {
428
- if (ctx.pending.has(row.id)) row.pending = ctx.pending.get(row.id)
429
- }
430
- spin.stop('现状')
431
- ui.note(renderPanel(state), '面板')
432
- const choice = await ui.select({ message: '要做什么', options: actionsFor(state) })
433
- if (cancelled(ui, choice) || choice === 'exit') {
434
- ui.outro('再运行 kioku 回到这里')
435
- return 0
436
- }
437
- if (choice === 'refresh') continue
438
- // 换了服务器、接入或停用之后,比过的未迁移条数可能对不上了
439
- if (!choice.startsWith('import:') && choice !== 'doctor') ctx.pending.clear()
440
- try {
441
- await perform(ctx, choice, state)
442
- } catch (err) {
443
- if (!(err instanceof KiokuFailure)) throw err
444
- ui.log.error(err.message)
445
- }
446
- }
447
- }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes