@kioku-memory/cli 0.1.0 → 0.3.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/NOTICE CHANGED
@@ -5,3 +5,6 @@ This product is licensed under the Apache License, Version 2.0 (see LICENSE).
5
5
 
6
6
  packages/core/lib/command.mjs 里给 cmd.exe 转义参数的规则改写自 cross-spawn
7
7
  (https://github.com/moxystudio/node-cross-spawn),MIT License,Copyright (c) 2018 Made With MOXY Lda。
8
+
9
+ plugins/opencode/index.js 里的对话事件映射与系统提示标记做法参考自 Lore
10
+ (opencode-plugin),MIT License,Copyright (c) Lore contributors。
package/README.md CHANGED
@@ -3,24 +3,42 @@
3
3
  [Kioku](https://github.com/lonelyman0108/kioku) 是自托管的长期记忆服务。这个包提供 `kioku` 命令,要 Node 18 或更新的版本:
4
4
 
5
5
  ```bash
6
+ npx @kioku-memory/cli
7
+ # 检查已有配置,或装到全局
6
8
  npx @kioku-memory/cli doctor
7
- # 或者装到全局
8
9
  npm install -g @kioku-memory/cli
9
10
  ```
10
11
 
11
- Claude Code 的 kioku 插件自带同一个命令,启用插件后在它的 Bash 工具里直接用 `kioku`,不用另装。
12
+ Claude Code 的 kioku 插件自带同一个命令,启用插件后在它的 Bash 工具里直接用 `kioku`,不用另装(插件里的那份没有向导)。
13
+
14
+ ## 向导
15
+
16
+ 在终端里不带参数运行 `kioku`,要 Node 20.12 或更新的版本。先扫描一遍现状:
17
+
18
+ - 服务器:地址(取各平台已有的配置或 `KIOKU_SERVER_URL`,都没有就问)、连不连得上、版本、初始化了没有。
19
+ - 每个平台一行:本机装没装(命令或配置目录在不在)、接到哪个应用和空间、访问密钥是否有效、插件装没装、自带的长期记忆停没停、本地有几条记忆。查不了的写「无法判断」。
20
+
21
+ 下面按缺什么列出动作,例如「接入 Codex、pi(2 个未接入)」「重新授权 Hermes(访问密钥已失效)」「接入 Claude Code」「迁移 Claude Code 的本地记忆(12 条)」「停用某个平台」「本机起一个 Kioku」「检查(doctor)」。每个动作先列计划、确认后才做,做完回到面板。
22
+
23
+ - **接入几个平台只确认一次**:一次申请勾选的几个平台(最多 8 个,多了分批,每批确认一次),确认页上为每个平台选应用与权限;拿到的访问密钥照 `kioku setup` 写下。确认页上没勾的平台跳过;确认之后、取走之前不再符合签发规则的(例如确认人已停用、应用被删)列出原因。服务端版本较旧、不认这种申请时,逐个平台申请,每个确认一次。签发了却没能写下的,提示去管理界面吊销。
24
+ - **迁移本地记忆**:面板上只数本地有几条;选了迁移才和 Kioku 逐条比对、列出计划,比出来的未迁移条数在这次向导里显示,写入之后重新算。比对失败的写「未迁移 无法判断」。
25
+ - **插件不替你装**:做完列出每个平台要自己运行的安装命令与之后要做的事,装好之后再运行 `kioku`,面板上就是「已安装」。
26
+ - **Claude Code**:访问密钥只认它的 `/plugin` 里填的,浏览器授权不给它发。「接入 Claude Code」停用自动记忆,再列出 `/plugin` 的安装命令与要填的服务地址;迁移本地记忆时问一次访问密钥。项目的 `.claude/settings.json` 又启用了自动记忆时,面板写明是哪份文件,不再提供停用(要在那里改)。
27
+ - **本机起一个 Kioku**:没有服务器时可选。取 Kioku 最新的正式版,在一个目录(默认 `~/kioku`)里下载这个版本的 `compose.yaml` 与 `.env.example`,生成 `.env`(数据库密码、主密钥、初始化码随机生成,镜像固定在这个版本,只给本人读写,端口只开在本机,已有的 `.env` 不覆盖),`docker compose up -d`,等服务就绪后显示初始化码并打开管理界面,建好管理员就接着接入平台。取不到正式版时用 main 上的文件与 `edge` 镜像,并明说。主密钥在 `.env` 里,另外保存一份。目录里已经起过(上次中断、重跑)的接着启动或等它就绪;端口 18920 被别的程序占着时只说明,不动它。
12
28
 
13
29
  ## 子命令
14
30
 
15
31
  | 子命令 | 作用 |
16
32
  |---|---|
17
- | `kioku setup <平台>` | 把平台交给 Kioku 接管:停用平台自带的长期记忆;Codex、pi、Hermes、OpenClaw、DeepSeek Harness 再写下服务地址与访问密钥,Codex 还配好 MCP 服务 |
33
+ | `kioku install --platforms <平台,…>` | 几个平台一起接入,访问密钥从环境变量读(给脚本用) |
34
+ | `kioku setup <平台>` | 把平台交给 Kioku 接管:停用平台自带的长期记忆;Codex、Gemini CLI、Antigravity、OpenCode、pi、Hermes、OpenClaw、DeepSeek Harness 再写下服务地址与访问密钥,其中 Codex、Gemini CLI、Antigravity 还配好 MCP 服务 |
18
35
  | `kioku login <平台>` | 用浏览器授权签发访问密钥并配好这个平台,不用手动复制 |
19
36
  | `kioku doctor [平台]` | 检查地址、访问密钥与平台的配置 |
37
+ | `kioku clean [平台]` | 检测并卸载冲突的记忆工具(Lore 及常见记忆 MCP) |
20
38
  | `kioku import --from <来源>` | 把平台自带的本地长期记忆迁进 Kioku |
21
39
  | `kioku run -e 变量=<标识>#<字段> -- <命令>` | 向 Kioku 取凭据的值,放进环境变量交给命令 |
22
40
 
23
- 每个子命令的完整说明:`kioku <子命令> --help`。现在认得的平台:`claude-code`、`codex`、`pi`、`hermes`、`openclaw`、`dsh`。
41
+ 每个子命令的完整说明:`kioku <子命令> --help`。现在认得的平台:`claude-code`、`codex`、`gemini`、`agy`、`opencode`、`pi`、`hermes`、`openclaw`、`dsh`。
24
42
 
25
43
  ## login
26
44
 
@@ -28,7 +46,7 @@ Claude Code 的 kioku 插件自带同一个命令,启用插件后在它的 Bas
28
46
  kioku login codex --server-url https://kioku.example.com
29
47
  ```
30
48
 
31
- 终端显示一个确认码并打开 Kioku 管理界面的确认页(`--no-browser` 时只显示地址)。登录后在页面上输入确认码,选空间、应用与权限范围,点确认;命令行取到访问密钥后接着做 `kioku setup` 的事:核对、写下配置。访问密钥全程不显示。认得 `codex`、`pi`、`hermes`、`openclaw`、`dsh`;Claude Code 的访问密钥在它的 `/plugin` 里填。
49
+ 终端显示一个确认码并打开 Kioku 管理界面的确认页(`--no-browser` 时只显示地址)。登录后在页面上输入确认码,选空间、应用与权限范围,点确认;命令行取到访问密钥后接着做 `kioku setup` 的事:核对、写下配置。访问密钥全程不显示。认得 `codex`、`gemini`、`agy`、`opencode`、`pi`、`hermes`、`openclaw`、`dsh`;Claude Code 的访问密钥在它的 `/plugin` 里填。
32
50
 
33
51
  - 确认码不在打开的地址里,要照着终端输入:只在自己刚运行了这条命令时确认。别人发来一个确认码让你去确认的,不要确认,那样对方的机器会拿到能读写你的记忆的访问密钥。
34
52
  - 申请 10 分钟内有效。被拒绝、过期,或者确认之后确认人的角色变了,都拿不到访问密钥,重新运行即可。
@@ -39,6 +57,18 @@ kioku login codex --server-url https://kioku.example.com
39
57
 
40
58
  - 确认之后如果这台机器没能把访问密钥写下(配置文件写不进去、中途断网),命令会提示去管理界面的应用页吊销刚签发的那个,再重新运行:访问密钥只在签发时给一次。
41
59
 
60
+ ## install
61
+
62
+ ```bash
63
+ export KIOKU_API_KEY_CODEX=… KIOKU_API_KEY_PI=…
64
+ kioku install --platforms codex,pi --server-url https://kioku.example.com -y
65
+ ```
66
+
67
+ 每个平台的访问密钥放在 `KIOKU_API_KEY_<平台>` 里(平台名大写、连字符换成下划线:`KIOKU_API_KEY_CLAUDE_CODE`、`KIOKU_API_KEY_CODEX`、`KIOKU_API_KEY_GEMINI`、`KIOKU_API_KEY_AGY`、`KIOKU_API_KEY_OPENCODE`、`KIOKU_API_KEY_PI`、`KIOKU_API_KEY_HERMES`、`KIOKU_API_KEY_OPENCLAW`、`KIOKU_API_KEY_DSH`),没给的平台跳过并在结果里列出。各平台照 `kioku setup` 核对、写下配置、停用自带的长期记忆;Claude Code 只核对访问密钥、停用自动记忆,地址与访问密钥要在它的 `/plugin` 里填。
68
+
69
+ - 在终端里运行、没带 `-y` 时先列计划再确认;`--dry-run` 只列计划。
70
+ - 最后列出要自己运行的插件安装命令。有平台失败时退出码为 1。
71
+
42
72
  ## setup
43
73
 
44
74
  ```bash
@@ -94,13 +124,51 @@ kioku setup dsh --server-url https://kioku.example.com
94
124
 
95
125
  核对后写下 `~/.config/kioku/dsh.json`,不改 dsh 的设置(它没有自带的长期记忆)。插件按 profile 装:`dsh plugin --profile <profile> add github:lonelyman0108/kioku-plugins`,装完重启 dsh。
96
126
 
127
+ ### Gemini CLI
128
+
129
+ ```bash
130
+ kioku setup gemini --server-url https://kioku.example.com
131
+ ```
132
+
133
+ 核对后写下 `~/.config/kioku/gemini.json`,在 `~/.gemini/settings.json` 里加上 `mcpServers.kioku` 并把自带的 `save_memory` 加入 `excludeTools`。扩展按源码链接:`git clone https://github.com/lonelyman0108/kioku-plugins` 后 `gemini extensions link kioku-plugins/plugins/gemini`。
134
+
135
+ ### Antigravity
136
+
137
+ ```bash
138
+ kioku setup agy --server-url https://kioku.example.com
139
+ ```
140
+
141
+ 核对后写下 `~/.config/kioku/agy.json`,并在 `~/.gemini/config/mcp_config.json` 里加上 `mcpServers.kioku`(Antigravity 没有自带的长期记忆)。插件放到 `~/.gemini/config/plugins/kioku` 后新开会话生效。
142
+
143
+ ### OpenCode
144
+
145
+ ```bash
146
+ kioku setup opencode --server-url https://kioku.example.com
147
+ ```
148
+
149
+ 核对后写下 `~/.config/kioku/opencode.json`,并在 `~/.config/opencode/opencode.json` 的 `plugin` 列表里加上 `@kioku-memory/plugin-opencode`(原文件备份成 `opencode.json.kioku-backup`;OpenCode 没有自带的长期记忆,MCP 由插件在 `config` 钩子里注册)。
150
+
97
151
  ## doctor
98
152
 
99
153
  ```bash
100
154
  kioku doctor claude-code
101
155
  ```
102
156
 
103
- 每项一行:`✓` 正常,`!` 要留意,`✗` 有问题(这时退出码为 1)。查的是:地址连不连得上、访问密钥是否有效、它属于哪个应用、能读写哪些归属、最高敏感级与凭据能力,以及平台这一边插件是否启用、自带的长期记忆是否已停用。不写平台时按当前所在的会话认。
157
+ 每项一行:`✓` 正常,`!` 要留意,`✗` 有问题(这时退出码为 1)。查的是:地址连不连得上、访问密钥是否有效、它属于哪个应用、能读写哪些归属、最高敏感级与凭据能力,以及平台这一边插件是否启用、自带的长期记忆是否已停用、是否有冲突的记忆工具(如 Lore)。不写平台时按当前所在的会话认。
158
+
159
+ ## clean
160
+
161
+ ```bash
162
+ kioku clean --dry-run # 扫描全部平台,只列发现的冲突
163
+ kioku clean agy # 清理 Antigravity 上的冲突(交互确认)
164
+ kioku clean agy --tool lore -y # 非交互下只清理 Lore
165
+ ```
166
+
167
+ 检测并卸载与 Kioku 冲突的记忆工具(完整覆盖 Lore 的插件、MCP、钩子、环境变量与兼容状态,以及 Mem0、Supermemory、MCP 官方 Memory Server、Cipher、Claude-Mem 等常见记忆 MCP):
168
+
169
+ - **只清理目标平台侧的配置与插件挂载**:不会跨平台清理,也不会删除 `~/.lore/docker`、`~/.lore/config.json` 等服务端或用户本地数据。
170
+ - **接入前自动检查**:`kioku setup`、`kioku login`、`kioku install` 与交互向导在发现冲突时都会先提示。交互模式下询问是否先卸载;非交互模式下默认阻断,传入 `--remove-conflicts` 后先卸载再继续接入。
171
+ - **保守处理非托管文件**:非符号链接或缺少托管标记的插件目录、泛用名称 `memory` 但特征不明的 MCP 条目、`CLAUDE.md` 里的手写指引只告警提示、不自动删改(仅精确移除 Lore 托管的 `@~/.claude/lore-guidance.md` 导入行与 `lore-guidance.md`)。
104
172
 
105
173
  ## import
106
174
 
@@ -127,7 +195,7 @@ kioku import --from claude-code # 列出计划,确认后写入
127
195
  - 只读普通的文本文件:符号链接不跟(它可以指到记忆目录之外的文件),太大的、不是文本的不读。
128
196
  - 写到一半连不上时停下来,说明写了多少、还剩多少;重新运行会接着写。
129
197
  - 不在终端里运行时要带 `--yes` 才写。`--path` 指明不在默认位置的目录。
130
- - 地址与访问密钥读环境变量(Claude Code 的会话里已经配好);`--platform codex`、`--platform pi`、`--platform hermes`、`--platform openclaw` 或 `--platform dsh` 改用 `kioku setup` 给那个平台写下的。记忆迁进这个访问密钥所属的应用。
198
+ - 地址与访问密钥读环境变量(Claude Code 的会话里已经配好);`--platform codex`、`--platform gemini`、`--platform agy`、`--platform opencode`、`--platform pi`、`--platform hermes`、`--platform openclaw` 或 `--platform dsh` 改用 `kioku setup` 给那个平台写下的。记忆迁进这个访问密钥所属的应用。
131
199
  - 指令文件(`AGENTS.md`、`CLAUDE.md`)不是记忆,不迁。Codex 自带的 Memories 存在它自己的数据库里、格式没有公开,迁不了。
132
200
 
133
201
  迁完之后用 `kioku setup <平台>` 停用平台自带的记忆,免得两边接着各记一份。
@@ -142,7 +210,7 @@ macOS 与 Linux 上各平台都用真实的模型实测过。Windows 上跑过
142
210
 
143
211
  ## 地址与访问密钥
144
212
 
145
- `kioku doctor codex`、`kioku doctor pi`、`kioku doctor hermes`、`kioku doctor openclaw`、`kioku doctor dsh` 读 `kioku setup` 写下的配置。别的情况下 `doctor` 与 `run` 从环境变量读:
213
+ `kioku doctor codex`、`kioku doctor gemini`、`kioku doctor agy`、`kioku doctor opencode`、`kioku doctor pi`、`kioku doctor hermes`、`kioku doctor openclaw`、`kioku doctor dsh` 读 `kioku setup` 写下的配置。别的情况下 `doctor` 与 `run` 从环境变量读:
146
214
 
147
215
  - `KIOKU_SERVER_URL`:例如 `https://kioku.example.com`,不带 `/v1`
148
216
  - `KIOKU_API_KEY`:在 Kioku 管理界面的「应用」页签发的访问密钥;或者用 `KIOKU_API_KEY_FILE` 指向只含访问密钥的文件
package/lib/agy.mjs ADDED
@@ -0,0 +1,181 @@
1
+ // Antigravity (agy) 这个平台的 kioku setup 与 kioku doctor。
2
+ // agy 没有自带的长期记忆要停用,setup 做两件事:
3
+ // 1. 把服务地址与访问密钥写进本机文件(只给本人读写,插件的 hooks.json 钩子读它);
4
+ // 2. 在 ~/.gemini/config/mcp_config.json 里写入 mcpServers.kioku(Streamable HTTP)
5
+
6
+ import { stat } from 'node:fs/promises'
7
+ import { homedir } from 'node:os'
8
+ import { join } from 'node:path'
9
+
10
+ import { readSettings, safeWrite, serialize } from './files.mjs'
11
+ import { KiokuFailure } from './http.mjs'
12
+ import { profileFile, readProfile, removeProfile, writeProfile } from './profile.mjs'
13
+ import { resolveTarget } from './target.mjs'
14
+
15
+ export function agyHome(env) {
16
+ if (env.AGY_HOME) return env.AGY_HOME
17
+ return join(env.HOME || homedir(), '.gemini', 'config')
18
+ }
19
+
20
+ export function agyMcpFile(env) {
21
+ return join(agyHome(env), 'mcp_config.json')
22
+ }
23
+
24
+ function checkMcpShape(settings, file) {
25
+ if (
26
+ settings.mcpServers !== undefined &&
27
+ (settings.mcpServers === null ||
28
+ typeof settings.mcpServers !== 'object' ||
29
+ Array.isArray(settings.mcpServers))
30
+ ) {
31
+ throw new KiokuFailure(`${file} 里的 mcpServers 不是对象,没有改动它`)
32
+ }
33
+ }
34
+
35
+ function mcpEntry(url, key) {
36
+ return {
37
+ url: `${url}/v1/mcp`,
38
+ headers: { Authorization: `Bearer ${key}` },
39
+ }
40
+ }
41
+
42
+ function sameMcp(existing, wanted) {
43
+ return (
44
+ existing?.url === wanted.url &&
45
+ existing?.headers?.Authorization === wanted.headers.Authorization
46
+ )
47
+ }
48
+
49
+ function hasStoredKey(settings) {
50
+ return Boolean(settings?.mcpServers?.kioku?.headers?.Authorization)
51
+ }
52
+
53
+ function buildMcp(settings, { url, key, undo }) {
54
+ const next = { ...settings }
55
+ if (undo) {
56
+ if (
57
+ settings.mcpServers &&
58
+ typeof settings.mcpServers === 'object' &&
59
+ Object.hasOwn(settings.mcpServers, 'kioku')
60
+ ) {
61
+ const mcp = { ...settings.mcpServers }
62
+ delete mcp.kioku
63
+ if (Object.keys(mcp).length) next.mcpServers = mcp
64
+ else delete next.mcpServers
65
+ }
66
+ return next
67
+ }
68
+ next.mcpServers = { ...(settings.mcpServers ?? {}), kioku: mcpEntry(url, key) }
69
+ return next
70
+ }
71
+
72
+ async function pluginInstalled(env, cwd) {
73
+ for (const file of [
74
+ join(agyHome(env), 'plugins', 'kioku', 'plugin.json'),
75
+ join(cwd, '.agents', 'plugins', 'kioku', 'plugin.json'),
76
+ ]) {
77
+ if ((await stat(file).catch(() => null))?.isFile()) return true
78
+ }
79
+ return false
80
+ }
81
+
82
+ export async function preflightAgy({ env }) {
83
+ const file = agyMcpFile(env)
84
+ const { settings } = await readSettings(file)
85
+ checkMcpShape(settings, file)
86
+ }
87
+
88
+ export async function setupAgy(options, io) {
89
+ const { env, out, cwd } = io
90
+ const profile = profileFile('agy', env)
91
+ const file = agyMcpFile(env)
92
+
93
+ if (options.undo) {
94
+ const { settings, text } = await readSettings(file)
95
+ checkMcpShape(settings, file)
96
+ const next = buildMcp(settings, { undo: true })
97
+ if (JSON.stringify(settings) === JSON.stringify(next)) {
98
+ if (text !== null) out.write(`${file} 不用改\n`)
99
+ } else {
100
+ out.write(`${file}:去掉 mcpServers.kioku\n`)
101
+ if (!options.dryRun) {
102
+ // 原文件里带着访问密钥时不留备份
103
+ await safeWrite(file, serialize(next, text), text, { backup: !hasStoredKey(settings) })
104
+ }
105
+ }
106
+
107
+ if (options.dryRun) out.write(`${profile}:删掉\n没有写入(--dry-run)\n`)
108
+ else if (await removeProfile('agy', env)) out.write(`已删掉 ${profile}\n`)
109
+ else out.write(`${profile} 本来就没有\n`)
110
+ return 0
111
+ }
112
+
113
+ await preflightAgy({ env })
114
+ const { url, key, saved } = await resolveTarget({ id: 'agy', name: 'Antigravity' }, options, {
115
+ ...io,
116
+ beforeLogin: () => preflightAgy({ env }),
117
+ })
118
+
119
+ const { settings, text } = await readSettings(file)
120
+ checkMcpShape(settings, file)
121
+ const next = buildMcp(settings, { url, key, undo: false })
122
+ const sameProfile = Boolean(saved && saved.url === url && saved.key === key)
123
+ const sameFile = JSON.stringify(settings) === JSON.stringify(next)
124
+
125
+ if (sameProfile) out.write(`${profile} 不用改\n`)
126
+ else out.write(`${profile}:写入服务地址与访问密钥(只给本人读写)\n`)
127
+
128
+ if (sameFile) out.write(`${file} 不用改\n`)
129
+ else out.write(`${file}:配置 mcpServers.kioku\n`)
130
+
131
+ if (options.dryRun) {
132
+ out.write('没有写入(--dry-run)\n')
133
+ return 0
134
+ }
135
+
136
+ if (!sameProfile) await writeProfile('agy', { url, key }, env)
137
+ if (!sameFile) {
138
+ const backedUp = await safeWrite(file, serialize(next, text), text, {
139
+ backup: !hasStoredKey(settings),
140
+ })
141
+ if (backedUp) out.write(`原配置已备份到 ${file}.kioku-backup\n`)
142
+ }
143
+ out.write('Antigravity 已配好,新开会话生效。\n')
144
+ if (!(await pluginInstalled(env, cwd)) && io.hints !== false) {
145
+ out.write(
146
+ `接下来安装插件以启用生命周期钩子:cp -r <仓库目录>/plugins/agy ${join(agyHome(env), 'plugins', 'kioku')}\n`,
147
+ )
148
+ }
149
+ return 0
150
+ }
151
+
152
+ export async function inspectAgy({ env, cwd }, { ok, warn }) {
153
+ const profile = await readProfile('agy', env).catch(() => null)
154
+ if (!profile) {
155
+ warn('没有 kioku setup agy 写下的地址与访问密钥,插件钩子不会生效:运行 kioku setup agy')
156
+ }
157
+
158
+ const file = agyMcpFile(env)
159
+ const { settings } = await readSettings(file)
160
+ const entry = settings?.mcpServers?.kioku
161
+ if (!entry) {
162
+ warn(`${file} 里没有 mcpServers.kioku:运行 kioku setup agy`)
163
+ } else if (!entry.url) {
164
+ warn(`${file} 里的 mcpServers.kioku 缺少 url:运行 kioku setup agy`)
165
+ } else if (profile && !sameMcp(entry, mcpEntry(profile.url, profile.key))) {
166
+ warn(`${file} 里的 mcpServers.kioku 与当前地址或访问密钥不一致:运行 kioku setup agy`)
167
+ } else {
168
+ ok(`${file} 里的 mcpServers.kioku 已配好`)
169
+ }
170
+
171
+ if (await pluginInstalled(env, cwd)) ok('Antigravity 已安装 kioku 插件')
172
+ else
173
+ warn(
174
+ `Antigravity 未安装 kioku 插件:将 plugins/agy 放到 ${join(agyHome(env), 'plugins', 'kioku')}`,
175
+ )
176
+ ok('Antigravity 没有自带的长期记忆要停用')
177
+ }
178
+
179
+ export async function statusAgy({ env, cwd }) {
180
+ return { plugin: await pluginInstalled(env, cwd), memory: 'none' }
181
+ }
package/lib/api.mjs CHANGED
@@ -4,6 +4,9 @@ import { fetchFailure, isRedirect, KiokuFailure, redirectError } from './http.mj
4
4
 
5
5
  const TIMEOUT = 8000
6
6
 
7
+ // 服务端明说访问密钥不能用(401):和连不上、响应不对分开,向导据此标「已失效」
8
+ export class KeyRejected extends KiokuFailure {}
9
+
7
10
  // 这个访问密钥对应的应用与权限;连不上、跳转、访问密钥无效、响应不对都以 KiokuFailure 抛出
8
11
  export async function whoami({ url, key }) {
9
12
  let res
@@ -22,7 +25,7 @@ export async function whoami({ url, key }) {
22
25
  if (isRedirect(res.status)) {
23
26
  throw new KiokuFailure(redirectError(res.status, res.headers.get('location'), url))
24
27
  }
25
- if (res.status === 401) throw new KiokuFailure('访问密钥无效或已吊销')
28
+ if (res.status === 401) throw new KeyRejected('访问密钥无效或已吊销')
26
29
  if (!res.ok) throw new KiokuFailure(`Kioku 返回 HTTP ${res.status}`)
27
30
  if (
28
31
  typeof data?.app?.runtimeFamily !== 'string' ||
package/lib/clean.mjs ADDED
@@ -0,0 +1,96 @@
1
+ // kioku clean:检测并卸载与 Kioku 冲突的记忆工具(Lore 及常见记忆 MCP)。
2
+ // 默认先列计划并询问确认(非终端运行需传 -y/--yes);只清理平台侧配置,不删服务端数据目录。
3
+
4
+ import { CONFLICT_TOOLS, formatConflicts, removeConflicts, scanAllConflicts } from './conflicts.mjs'
5
+ import { confirm } from './import.mjs'
6
+ import { KiokuFailure } from './http.mjs'
7
+ import { platformNames, PLATFORMS } from './platforms.mjs'
8
+ import { UsageError } from './usage.mjs'
9
+
10
+ const CLEANABLE_TOOLS = Object.keys(CONFLICT_TOOLS).filter((id) => id !== 'unknown-memory')
11
+
12
+ export const CLEAN_USAGE = `用法:kioku clean [平台] [--tool <工具>] [--dry-run] [-y|--yes]
13
+
14
+ 检测并卸载与 Kioku 冲突的记忆工具(Lore 及常见记忆 MCP)。
15
+ 只清理目标平台上的接入配置或插件挂载,不删服务端数据目录。
16
+
17
+ [平台] ${platformNames()};不写时扫描全部平台
18
+ --tool <工具> 只清理指定工具:${CLEANABLE_TOOLS.join('、')}
19
+ --dry-run 只列出发现的冲突,不修改文件
20
+ -y, --yes 不问,直接清理(不在终端里运行时要带上)
21
+ `
22
+
23
+ export function parseCleanArgs(argv) {
24
+ const options = { platform: null, tool: null, dryRun: false, yes: false }
25
+ for (let i = 0; i < argv.length; i++) {
26
+ const arg = argv[i]
27
+ if (arg === '--dry-run') options.dryRun = true
28
+ else if (arg === '-y' || arg === '--yes') options.yes = true
29
+ else if (arg === '--tool') {
30
+ const value = argv[++i]
31
+ if (!value) throw new UsageError('--tool 后面要写工具名')
32
+ if (!CLEANABLE_TOOLS.includes(value)) {
33
+ throw new UsageError(`不认识的工具 ${value},可以用的:${CLEANABLE_TOOLS.join('、')}`)
34
+ }
35
+ options.tool = value
36
+ } else if (arg.startsWith('-')) {
37
+ throw new UsageError(`不认识的选项 ${arg}`)
38
+ } else if (options.platform !== null) {
39
+ throw new UsageError('最多写一个平台')
40
+ } else {
41
+ if (!Object.hasOwn(PLATFORMS, arg)) {
42
+ throw new UsageError(`不认识的平台 ${arg},可以用的:${platformNames()}`)
43
+ }
44
+ options.platform = arg
45
+ }
46
+ }
47
+ return options
48
+ }
49
+
50
+ export async function clean(
51
+ argv,
52
+ { env = process.env, out = process.stdout, cwd = process.cwd(), stdin = process.stdin } = {},
53
+ ) {
54
+ const options = parseCleanArgs(argv)
55
+ const targetIds = options.platform ? [options.platform] : Object.keys(PLATFORMS)
56
+ const all = await scanAllConflicts(targetIds, { env, cwd })
57
+ const items = options.tool ? all.filter((item) => item.tool === options.tool) : all
58
+
59
+ if (!items.length) {
60
+ out.write(
61
+ options.platform
62
+ ? `${PLATFORMS[options.platform].name}:没有发现冲突的记忆工具\n`
63
+ : '没有发现冲突的记忆工具\n',
64
+ )
65
+ return 0
66
+ }
67
+
68
+ out.write('发现以下冲突或疑似项:\n')
69
+ out.write(`${formatConflicts(items, PLATFORMS).join('\n')}\n`)
70
+
71
+ const removable = items.filter((item) => item.removable)
72
+ if (options.dryRun) {
73
+ out.write('没有改动(--dry-run)\n')
74
+ return 0
75
+ }
76
+ if (!removable.length) {
77
+ out.write('没有可自动卸载的项,请按上面的提示手动处理。\n')
78
+ return 0
79
+ }
80
+
81
+ if (!options.yes) {
82
+ if (!stdin.isTTY) {
83
+ throw new KiokuFailure('不在终端里运行时要带 --yes 才清理;先用 --dry-run 看计划')
84
+ }
85
+ if (!(await confirm(`清理以上 ${removable.length} 项冲突?[y/N] `, { stdin, out }))) {
86
+ out.write('没有改动\n')
87
+ return 0
88
+ }
89
+ }
90
+
91
+ out.write('\n')
92
+ const results = await removeConflicts(removable, { env, cwd, out })
93
+ const failed = results.some((r) => !r.ok)
94
+ if (!failed) out.write('清理完成,重启对应平台后生效。\n')
95
+ return failed ? 1 : 0
96
+ }
package/lib/codex.mjs CHANGED
@@ -50,7 +50,7 @@ const HEADER = new RegExp(
50
50
 
51
51
  // 段的标题:[a.b] 或 [[a.b]],返回里面的名字;不是标题时为 null。
52
52
  // 名字规整成一种写法再比:点两边的空白去掉,只含普通字符的那一段去掉引号
53
- function headerOf(line) {
53
+ export function headerOf(line) {
54
54
  const name = HEADER.exec(line)?.[1]
55
55
  if (name === undefined) return null
56
56
  return name
@@ -59,7 +59,7 @@ function headerOf(line) {
59
59
  }
60
60
 
61
61
  // 这一行让没有合上的方括号多了几层:多行的数组里,单独成行的 [1, 2] 不是标题。引号里的不算
62
- function bracketDelta(line) {
62
+ export function bracketDelta(line) {
63
63
  const bare = line.replace(/"(?:[^"\\]|\\.)*"|'[^']*'/g, '').replace(/#.*$/, '')
64
64
  return (bare.match(/\[/g)?.length ?? 0) - (bare.match(/\]/g)?.length ?? 0)
65
65
  }
@@ -262,7 +262,7 @@ export async function setupCodex(options, io) {
262
262
  `! ${file} 里 features 的写法认不出来,没有核对 Memories:运行 codex features disable memories 关掉它\n`,
263
263
  )
264
264
  }
265
- out.write(`Codex 已配好,新开会话生效。\n${found.plugin ? '' : NEXT_STEPS}`)
265
+ out.write(`Codex 已配好,新开会话生效。\n${found.plugin || io.hints === false ? '' : NEXT_STEPS}`)
266
266
  return 0
267
267
  }
268
268
 
@@ -307,3 +307,14 @@ export async function inspectCodex({ env }, { ok, warn, bad, note }) {
307
307
  warn('Codex 自带的 Memories 开着,会和 Kioku 各记一份:运行 kioku setup codex 关掉它')
308
308
  } else ok('Codex 自带的 Memories 没有启用')
309
309
  }
310
+
311
+ // 给向导的面板:插件启用没有、自带的 Memories 关了没有(null 是认不出)
312
+ export async function statusCodex({ env }) {
313
+ const found = scanConfig(await readText(join(codexHome(env), 'config.toml')))
314
+ const memory = found.memories?.value
315
+ ? 'on'
316
+ : found.memoriesUnknown && !found.memories
317
+ ? null
318
+ : 'off'
319
+ return { plugin: found.plugin, memory }
320
+ }
package/lib/command.mjs CHANGED
@@ -5,8 +5,8 @@
5
5
  // 转义的规则照 cross-spawn 的做法写(MIT,来源登记在 NOTICE)
6
6
 
7
7
  import { execFile, spawn } from 'node:child_process'
8
- import { statSync } from 'node:fs'
9
- import { win32 } from 'node:path'
8
+ import { accessSync, constants, statSync } from 'node:fs'
9
+ import { join, win32 } from 'node:path'
10
10
 
11
11
  import { KiokuFailure } from './http.mjs'
12
12
 
@@ -50,6 +50,23 @@ export function findCommand(name, env, { exists = isFile } = {}) {
50
50
  return null
51
51
  }
52
52
 
53
+ // PATH 里有没有这个命令(向导的面板用来看平台装没装)。Windows 上照 findCommand 的规则找;
54
+ // 别的系统上找能执行的普通文件
55
+ export function hasCommand(name, env, { platform = process.platform } = {}) {
56
+ if (platform === 'win32') return findCommand(name, env) !== null
57
+ if (typeof name !== 'string' || !name) return false
58
+ const dirs = /\//.test(name) ? [''] : (envValue(env, 'PATH') ?? '').split(':').filter(Boolean)
59
+ return dirs.some((dir) => {
60
+ const file = dir ? join(dir, name) : name
61
+ try {
62
+ accessSync(file, constants.X_OK)
63
+ return statSync(file).isFile()
64
+ } catch {
65
+ return false
66
+ }
67
+ })
68
+ }
69
+
53
70
  // cmd.exe 认作有特殊含义的字符
54
71
  const META = /([()\][%!^"`<>&|;, *?])/g
55
72