@kioku-memory/cli 0.3.0 → 0.4.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.
Files changed (66) hide show
  1. package/README.md +63 -23
  2. package/assets/plugins/agy/README.md +34 -0
  3. package/assets/plugins/agy/bin/kioku +36 -0
  4. package/assets/plugins/agy/hooks/forward.mjs +232 -0
  5. package/assets/plugins/agy/hooks.json +25 -0
  6. package/assets/plugins/agy/lib/kioku.mjs +53 -0
  7. package/{lib/kioku.mjs → assets/plugins/agy/lib/run.mjs} +4 -83
  8. package/assets/plugins/agy/package.json +7 -0
  9. package/assets/plugins/agy/plugin.json +6 -0
  10. package/assets/plugins/agy/vitest.config.mjs +5 -0
  11. package/assets/plugins/gemini/README.md +33 -0
  12. package/assets/plugins/gemini/bin/kioku +36 -0
  13. package/assets/plugins/gemini/gemini-extension.json +5 -0
  14. package/assets/plugins/gemini/hooks/forward.mjs +174 -0
  15. package/assets/plugins/gemini/hooks/hooks.json +43 -0
  16. package/assets/plugins/gemini/lib/command.mjs +152 -0
  17. package/assets/plugins/gemini/lib/config.mjs +50 -0
  18. package/assets/plugins/gemini/lib/http.mjs +57 -0
  19. package/assets/plugins/gemini/lib/kioku.mjs +53 -0
  20. package/assets/plugins/gemini/lib/lifecycle.mjs +128 -0
  21. package/assets/plugins/gemini/lib/mcp.mjs +142 -0
  22. package/assets/plugins/gemini/lib/profile.mjs +100 -0
  23. package/assets/plugins/gemini/lib/redact.mjs +91 -0
  24. package/assets/plugins/gemini/lib/run.mjs +372 -0
  25. package/assets/plugins/gemini/lib/text.mjs +14 -0
  26. package/assets/plugins/gemini/lib/usage.mjs +2 -0
  27. package/assets/plugins/gemini/package.json +7 -0
  28. package/assets/plugins/gemini/vitest.config.mjs +5 -0
  29. package/bin/kioku +16 -12
  30. package/dist/index.mjs +8846 -0
  31. package/package.json +14 -5
  32. package/lib/agy.mjs +0 -181
  33. package/lib/api.mjs +0 -38
  34. package/lib/clean.mjs +0 -96
  35. package/lib/codex.mjs +0 -320
  36. package/lib/conflicts.mjs +0 -1303
  37. package/lib/connect.mjs +0 -152
  38. package/lib/doctor.mjs +0 -158
  39. package/lib/dsh.mjs +0 -82
  40. package/lib/files.mjs +0 -92
  41. package/lib/gemini.mjs +0 -199
  42. package/lib/hermes.mjs +0 -276
  43. package/lib/import.mjs +0 -301
  44. package/lib/install.mjs +0 -196
  45. package/lib/local.mjs +0 -294
  46. package/lib/login.mjs +0 -359
  47. package/lib/openclaw.mjs +0 -256
  48. package/lib/opencode.mjs +0 -144
  49. package/lib/panel.mjs +0 -311
  50. package/lib/pi.mjs +0 -90
  51. package/lib/platforms.mjs +0 -297
  52. package/lib/prompts.mjs +0 -38
  53. package/lib/secret.mjs +0 -86
  54. package/lib/setup.mjs +0 -139
  55. package/lib/sources.mjs +0 -333
  56. package/lib/target.mjs +0 -62
  57. package/lib/wizard.mjs +0 -534
  58. /package/{lib → assets/plugins/agy/lib}/command.mjs +0 -0
  59. /package/{lib → assets/plugins/agy/lib}/config.mjs +0 -0
  60. /package/{lib → assets/plugins/agy/lib}/http.mjs +0 -0
  61. /package/{lib → assets/plugins/agy/lib}/lifecycle.mjs +0 -0
  62. /package/{lib → assets/plugins/agy/lib}/mcp.mjs +0 -0
  63. /package/{lib → assets/plugins/agy/lib}/profile.mjs +0 -0
  64. /package/{lib → assets/plugins/agy/lib}/redact.mjs +0 -0
  65. /package/{lib → assets/plugins/agy/lib}/text.mjs +0 -0
  66. /package/{lib → assets/plugins/agy/lib}/usage.mjs +0 -0
package/README.md CHANGED
@@ -1,44 +1,55 @@
1
1
  # Kioku 的命令行
2
2
 
3
- [Kioku](https://github.com/lonelyman0108/kioku) 是自托管的长期记忆服务。这个包提供 `kioku` 命令,要 Node 18 或更新的版本:
3
+ [Kioku](https://github.com/lonelyman0108/kioku) 是自托管的长期记忆服务。`@kioku-memory/cli` 以零运行时依赖单文件包分发(仅依赖 Node 22 / Node 20.12 以上交互能力),提供终端地层视觉界面(Strata UI)、客户端一站式接入与日常记忆读写:
4
4
 
5
5
  ```bash
6
6
  npx @kioku-memory/cli
7
- # 检查已有配置,或装到全局
8
- npx @kioku-memory/cli doctor
7
+ # 检查本机与服务端接入现状,或装到全局
8
+ npx @kioku-memory/cli status
9
+ npx @kioku-memory/cli doctor --all
9
10
  npm install -g @kioku-memory/cli
10
11
  ```
11
12
 
12
- Claude Code 的 kioku 插件自带同一个命令,启用插件后在它的 Bash 工具里直接用 `kioku`,不用另装(插件里的那份没有向导)。
13
+ Claude Code、Codex 等插件目录内部保留精简的 `packages/core` 运行时副本以执行 `kioku run` 与钩子转发;全功能交互控制台与管理命令由本包(`@kioku-memory/cli`)统一提供。
13
14
 
14
- ## 向导
15
+ ## 交互式控制台
15
16
 
16
- 在终端里不带参数运行 `kioku`,要 Node 20.12 或更新的版本。先扫描一遍现状:
17
+ 在终端里不带参数运行 `kioku`,要 Node 20.12 或更新的版本。界面遵循 Kioku 地层设计规范:1px 直角细线框、归属色彩与文字标签并重、敏感级纯字符纹理(`[ ] 公开` / `[·] 内部` / `[╱] 私密` / `[■] 机密`)、砖红仅用于错误与高风险确认,并遵循 `NO_COLOR`、`FORCE_COLOR`、`TERM=dumb` 与非全屏原地局部重绘:
17
18
 
18
- - 服务器:地址(取各平台已有的配置或 `KIOKU_SERVER_URL`,都没有就问)、连不连得上、版本、初始化了没有。
19
- - 每个平台一行:本机装没装(命令或配置目录在不在)、接到哪个应用和空间、访问密钥是否有效、插件装没装、自带的长期记忆停没停、本地有几条记忆。查不了的写「无法判断」。
19
+ - 服务器:地址(取各平台已有的配置或 `KIOKU_SERVER_URL`,都没有就问)、连不连得上、版本、初始化状态。
20
+ - 本机客户端:渐进披露各客户端现状(接入、访问密钥、插件、自带记忆停用与本地待迁移记忆),自动折叠未安装的客户端。
20
21
 
21
- 下面按缺什么列出动作,例如「接入 Codex、pi(2 个未接入)」「重新授权 Hermes(访问密钥已失效)」「接入 Claude Code」「迁移 Claude Code 的本地记忆(12 条)」「停用某个平台」「本机起一个 Kioku」「检查(doctor)」。每个动作先列计划、确认后才做,做完回到面板。
22
+ 下面按现状提供聚焦动作,例如「一键接入全部已检测未接入客户端」「接入特定客户端」「重新授权已失效访问密钥」「迁移本地记忆」「检查(doctor)」「在本机用 Docker 起一个 Kioku」「断开或清理冲突工具」。每个动作先列计划、确认后执行,做完回到控制台刷新。
22
23
 
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 被别的程序占着时只说明,不动它。
24
+ - **多平台联合授权**:一次申请勾选的几个平台(最多 8 个),终端弹出宽松确认码框并自动尝试复制确认码,附带 `[o]` 打开浏览器 / `[c]` 复制链接 / `[Esc]` 取消快捷键与 10 分钟实时倒计时。浏览器确认页只输入一次确认码即可完成全部选定平台的配置写入、自带记忆停用,并自动尝试安装对应插件与执行 `whoami` + MCP `tools/list` 连通性握手测试。
25
+ - **插件自动安装与托管更新**:支持调用 Claude Code、Codex、Gemini CLI、pi、Hermes、OpenClaw 的官方安装/更新命令,或同步内置 Antigravity / Gemini 扩展(Antigravity 目录带 `.kioku-managed.json` 托管标记,更新或 `kioku disconnect` 清理时绝不误删用户自己放置的目录);未检测到客户端 CLI 时自动降级为可复制的手动命令。
26
+ - **Claude Code 统一鉴权**:`kioku connect claude-code` 与 `kioku login claude-code` 直接走浏览器授权写入 `~/.config/kioku/claude-code.json` 并配好 `autoMemoryEnabled: false`;在 `/plugin` 里填入的配置优先,`/plugin` 留空时插件钩子与 `mcp-stdio.mjs` 自动回退读取 `~/.config/kioku/claude-code.json`。
27
+ - **迁移本地记忆**:控制台仅统计本地条数;选定迁移后与服务端逐条比对并列出计划,支持 `--select` 勾选子集写入。
28
+ - **本机起一个 Kioku**:没有服务器时可在 `~/kioku` 生成 `compose.yaml` 与仅本人可读写的随机密钥 `.env`,`docker compose up -d` 等健康检查通过后展示初始化码并在浏览器打开管理界面。
28
29
 
29
30
  ## 子命令
30
31
 
31
32
  | 子命令 | 作用 |
32
33
  |---|---|
33
- | `kioku install --platforms <平台,…>` | 几个平台一起接入,访问密钥从环境变量读(给脚本用) |
34
- | `kioku setup <平台>` | 把平台交给 Kioku 接管:停用平台自带的长期记忆;Codex、Gemini CLI、Antigravity、OpenCode、pi、Hermes、OpenClaw、DeepSeek Harness 再写下服务地址与访问密钥,其中 Codex、Gemini CLI、Antigravity 还配好 MCP 服务 |
35
- | `kioku login <平台>` | 用浏览器授权签发访问密钥并配好这个平台,不用手动复制 |
36
- | `kioku doctor [平台]` | 检查地址、访问密钥与平台的配置 |
34
+ | `kioku search <查询>` | 召回相关记忆(语义、全文与关键词),交互终端回车查看正文,支持 `--json` |
35
+ | `kioku get <坐标>` | 按坐标读取单条记忆完整属性与正文,支持 `--json` |
36
+ | `kioku ls [坐标或归属]` | 按地层顺序(本人、应用、人格、通用)或指定前缀浏览目录节点 |
37
+ | `kioku add [坐标]` | 写入新记忆,支持管道标准输入、`$EDITOR`、明文凭据拦阻与 `--plaintext-ok` |
38
+ | `kioku status [--json]` | 只读输出服务端与本机各客户端接入概况 |
39
+ | `kioku connect [平台…]` | 一站式授权、停用自带记忆、自动装插件与 MCP 握手验证 |
40
+ | `kioku disconnect <平台>` | 恢复自带记忆、删除凭据配置并安全清理托管插件 |
41
+ | `kioku upgrade [平台…]` | 升级已接入插件并刷新本机 MCP 工具清单缓存 |
42
+ | `kioku doctor [平台] [--all] [--json]` | 诊断服务地址、访问密钥权限、客户端配置与冲突工具 |
37
43
  | `kioku clean [平台]` | 检测并卸载冲突的记忆工具(Lore 及常见记忆 MCP) |
38
44
  | `kioku import --from <来源>` | 把平台自带的本地长期记忆迁进 Kioku |
45
+ | `kioku local [up\|down\|status\|logs]` | 使用 Docker Compose 管理本机 Kioku 服务 |
46
+ | `kioku open` | 在默认浏览器中打开 Kioku 管理界面 |
47
+ | `kioku login <平台>` | 用浏览器授权签发访问密钥并配好单个平台 |
48
+ | `kioku setup <平台>` | 停用平台自带记忆并手动填写服务地址与访问密钥 |
49
+ | `kioku install --platforms <平台,…>` | 多个平台批量接入,访问密钥从环境变量读(给脚本用) |
39
50
  | `kioku run -e 变量=<标识>#<字段> -- <命令>` | 向 Kioku 取凭据的值,放进环境变量交给命令 |
40
51
 
41
- 每个子命令的完整说明:`kioku <子命令> --help`。现在认得的平台:`claude-code`、`codex`、`gemini`、`agy`、`opencode`、`pi`、`hermes`、`openclaw`、`dsh`。
52
+ 每个子命令的完整说明:`kioku <子命令> --help`。支持的平台:`claude-code`、`codex`、`gemini`、`agy`、`opencode`、`pi`、`hermes`、`openclaw`、`dsh`。
42
53
 
43
54
  ## login
44
55
 
@@ -46,7 +57,7 @@ Claude Code 的 kioku 插件自带同一个命令,启用插件后在它的 Bas
46
57
  kioku login codex --server-url https://kioku.example.com
47
58
  ```
48
59
 
49
- 终端显示一个确认码并打开 Kioku 管理界面的确认页(`--no-browser` 时只显示地址)。登录后在页面上输入确认码,选空间、应用与权限范围,点确认;命令行取到访问密钥后接着做 `kioku setup` 的事:核对、写下配置。访问密钥全程不显示。认得 `codex`、`gemini`、`agy`、`opencode`、`pi`、`hermes`、`openclaw`、`dsh`;Claude Code 的访问密钥在它的 `/plugin` 里填。
60
+ 终端显示确认码框并打开 Kioku 管理界面的确认页(`--no-browser` 时只显示地址)。登录后在页面上输入确认码,选空间、应用与权限范围,点确认;命令行取到访问密钥后接着做 `kioku setup` 的事:核对、写下配置。访问密钥全程不显示。支持全部客户端:`claude-code`、`codex`、`gemini`、`agy`、`opencode`、`pi`、`hermes`、`openclaw`、`dsh`。
50
61
 
51
62
  - 确认码不在打开的地址里,要照着终端输入:只在自己刚运行了这条命令时确认。别人发来一个确认码让你去确认的,不要确认,那样对方的机器会拿到能读写你的记忆的访问密钥。
52
63
  - 申请 10 分钟内有效。被拒绝、过期,或者确认之后确认人的角色变了,都拿不到访问密钥,重新运行即可。
@@ -64,7 +75,7 @@ export KIOKU_API_KEY_CODEX=… KIOKU_API_KEY_PI=…
64
75
  kioku install --platforms codex,pi --server-url https://kioku.example.com -y
65
76
  ```
66
77
 
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` 里填。
78
+ 每个平台的访问密钥放在 `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` 核对、写下本机配置并停用自带长期记忆。
68
79
 
69
80
  - 在终端里运行、没带 `-y` 时先列计划再确认;`--dry-run` 只列计划。
70
81
  - 最后列出要自己运行的插件安装命令。有平台失败时退出码为 1。
@@ -77,7 +88,7 @@ kioku setup claude-code # 停用 Claude Code 的自动记忆
77
88
  kioku setup claude-code --undo # 改回去
78
89
  ```
79
90
 
80
- 只改平台的用户设置里和自带记忆有关的几项(Claude Code 是 `~/.claude/settings.json` 的 `autoMemoryEnabled`),设置里的其他内容不动。第一次改之前把原来的文件备份成同目录下的 `settings.json.kioku-backup`(已有备份时不覆盖),改完新开会话生效。本地已有的记忆文件不会被删掉。
91
+ `kioku setup claude-code` 核对并写下 `~/.config/kioku/claude-code.json`(当 `/plugin` 选项留空时由钩子与 `mcp-stdio.mjs` 自动回退读取;若 `/plugin` 已填则优先采用 `/plugin` 配置),同时把 `~/.claude/settings.json` 的 `autoMemoryEnabled` 设为 `false`,设置里的其他内容不动。第一次改之前把原来的文件备份成同目录下的 `settings.json.kioku-backup`(已有备份时不覆盖),改完新开会话生效。本地已有的记忆文件不会被删掉。
81
92
 
82
93
  - 设置文件是符号链接时改它指向的文件;文件会按 JSON 重新排版(缩进、换行符沿用原来的)。
83
94
  - 只看用户设置:当前项目的 `.claude/settings.json`、`.claude/settings.local.json` 里又启用了的话会提醒,要在那里一起改;托管的设置查不到。
@@ -148,13 +159,40 @@ kioku setup opencode --server-url https://kioku.example.com
148
159
 
149
160
  核对后写下 `~/.config/kioku/opencode.json`,并在 `~/.config/opencode/opencode.json` 的 `plugin` 列表里加上 `@kioku-memory/plugin-opencode`(原文件备份成 `opencode.json.kioku-backup`;OpenCode 没有自带的长期记忆,MCP 由插件在 `config` 钩子里注册)。
150
161
 
162
+ ## 日常记忆读写(search / get / ls / add)
163
+
164
+ 四个记忆读写命令共用零配置身份解析链:优先读 `--platform <平台>`,其次读 `KIOKU_SERVER_URL` + `KIOKU_API_KEY`(或 `KIOKU_API_KEY_FILE`),再读 `~/.config/kioku/cli.json` 记住的默认身份;本机若仅配置了一个客户端则直接选用,存在多个客户端时在交互式终端让你选择并记住默认项(非终端环境下提示传入 `--platform`)。
165
+
166
+ ```bash
167
+ kioku search "发布流程" --limit 5
168
+ kioku get core://agent
169
+ kioku ls # 按 本人 / 应用 / 人格 / 通用 地层顺序列出根级目录
170
+ kioku ls user://projects --json
171
+ echo "优先使用 pnpm" | kioku add user://preferences/tooling --title "包管理工具"
172
+ ```
173
+
174
+ - **`add` 的明文拦阻检查**:正文若触发服务端疑似明文凭据检查(HTTP 400),命令行只显示「第几行、哪一类」而不回显值;交互式终端可选择取消或确认为非凭据后写入,非终端环境可带 `--plaintext-ok` 放行。
175
+ - 四个命令均支持 `--json` 结构化输出与 `--platform <平台>`。
176
+
177
+ ## status / connect / disconnect / upgrade / local / open
178
+
179
+ ```bash
180
+ kioku status --json # 只读检查服务端与本机各客户端状态(不含访问密钥)
181
+ kioku connect codex gemini # 批量授权、写入配置、停用自带记忆、装插件并测试 MCP 握手
182
+ kioku disconnect codex # 恢复自带记忆、删去凭据配置并清理托管安装的插件
183
+ kioku upgrade # 升级所有已安装插件并刷新 MCP 工具清单缓存
184
+ kioku local up # 用 Docker Compose 启动本机 127.0.0.1:18920 服务
185
+ kioku open # 在浏览器中打开 Kioku 管理界面
186
+ ```
187
+
151
188
  ## doctor
152
189
 
153
190
  ```bash
154
191
  kioku doctor claude-code
192
+ kioku doctor --all --json
155
193
  ```
156
194
 
157
- 每项一行:`✓` 正常,`!` 要留意,`✗` 有问题(这时退出码为 1)。查的是:地址连不连得上、访问密钥是否有效、它属于哪个应用、能读写哪些归属、最高敏感级与凭据能力,以及平台这一边插件是否启用、自带的长期记忆是否已停用、是否有冲突的记忆工具(如 Lore)。不写平台时按当前所在的会话认。
195
+ 每项一行:`✓` 正常,`!` 要留意,`✗` 有问题(这时退出码为 1)。不写平台时优先按当前所在会话识别,否则自动巡检全部已安装或已配置客户端(`--all` 覆盖全部受支持平台,`--json` 输出不含访问密钥的结构化报告)。
158
196
 
159
197
  ## clean
160
198
 
@@ -175,6 +213,8 @@ kioku clean agy --tool lore -y # 非交互下只清理 Lore
175
213
  ```bash
176
214
  kioku import --from claude-code --dry-run # 只看计划
177
215
  kioku import --from claude-code # 列出计划,确认后写入
216
+ kioku import --from claude-code --select # 交互勾选本次要迁入的条目子集
217
+ kioku import --from claude-code -y # 免确认直接写入
178
218
  ```
179
219
 
180
220
  先读本地的记忆文件,列出每条会放到哪个坐标、Kioku 里有没有,确认之后才写。本地的文件不删、不改。
@@ -0,0 +1,34 @@
1
+ # Kioku 的 Antigravity (agy) 插件
2
+
3
+ 为 Antigravity 接入 Kioku 长期记忆:
4
+
5
+ - `SessionStart` 注入启动加载内容(`injectSteps.systemMessage`);
6
+ - `PreInvocation` 在每轮新用户输入时注入召回候选(同一轮内的工具调用自动去重);
7
+ - `Stop` 从对话记录读取本轮最终回复并上报 Kioku 生成会话摘要;
8
+ - 在 `~/.gemini/config/mcp_config.json` 中自动写入 `mcpServers.kioku`(Streamable HTTP)。
9
+
10
+ ## 安装与配置
11
+
12
+ 先在 Kioku 管理界面的「应用」页建一个运行时是 `Antigravity`(`agy`)的应用,然后:
13
+
14
+ ```bash
15
+ # 1. 配好服务地址、访问密钥与全局 mcp_config.json
16
+ npx @kioku-memory/cli setup agy --server-url https://kioku.example.com
17
+
18
+ # 2. 复制(或软链)插件到 Antigravity 插件目录
19
+ git clone https://github.com/lonelyman0108/kioku-plugins
20
+ mkdir -p ~/.gemini/config/plugins
21
+ cp -r kioku-plugins/plugins/agy ~/.gemini/config/plugins/kioku
22
+ ```
23
+
24
+ 也可按项目放置于 `<workspace>/.agents/plugins/kioku/`,或使用浏览器授权:
25
+
26
+ ```bash
27
+ npx @kioku-memory/cli login agy --server-url https://kioku.example.com
28
+ ```
29
+
30
+ 检查配置:
31
+
32
+ ```bash
33
+ npx @kioku-memory/cli doctor agy
34
+ ```
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env node
2
+ // kioku 命令的入口,实现在 ../lib/kioku.mjs。插件启用期间,Claude Code 把插件的 bin/ 加进 Bash 工具的 PATH。
3
+ // 没有扩展名的文件按 CommonJS 还是 ES 模块执行,取决于 Node 的版本与上层的 package.json,
4
+ // 所以这里只用两边都能用的写法:动态 import,位置从 process.argv[1] 推
5
+ const load = (name) =>
6
+ Promise.all([import('node:fs'), import('node:path'), import('node:url')]).then(
7
+ ([fs, path, url]) => {
8
+ const lib = path.join(path.dirname(fs.realpathSync(process.argv[1])), '..', 'lib')
9
+ return import(url.pathToFileURL(path.join(lib, name)).href)
10
+ },
11
+ )
12
+
13
+ // 输出被读的一方提前关掉(kioku doctor | head):之后的输出写不出去就算了,不抛一段堆栈;
14
+ // 手上的事照常做完、按它的结果退出(中途停下的话,写了一半的导入、配置会被当成成功)。别的写入错误照旧抛。
15
+ // run 不在此列:它转发命令的输出,自己处理这种情况
16
+ if (process.argv[2] !== 'run') {
17
+ for (const stream of [process.stdout, process.stderr]) {
18
+ stream.on('error', (err) => {
19
+ if (!err || err.code !== 'EPIPE') throw err
20
+ })
21
+ }
22
+ }
23
+
24
+ load('kioku.mjs')
25
+ .then((cli) => cli.main(process.argv.slice(2)))
26
+ .then((code) => {
27
+ process.exitCode = code
28
+ })
29
+ .catch((err) => {
30
+ process.exitCode = 2
31
+ // 只给错误类别:错误文字里可能带着环境变量的值。取不到共用的函数时只说一句固定的
32
+ return load('http.mjs').then(
33
+ ({ errorKind }) => process.stderr.write(`kioku:内部错误(${errorKind(err)})\n`),
34
+ () => process.stderr.write('kioku:内部错误\n'),
35
+ )
36
+ })
@@ -0,0 +1,232 @@
1
+ // Antigravity (agy) 插件钩子:转发 SessionStart、PreInvocation、Stop 给 Kioku。
2
+ // PreInvocation 在同一轮工具调用里会多次触发,按 transcript 最新 USER_INPUT 位置去重,每轮只召回一次。
3
+ // 任何失败都保持退出码 0、输出合法 JSON {},不阻断会话,错误写 stderr(不带访问密钥)。
4
+
5
+ import { createHash } from 'node:crypto'
6
+ import { mkdir, open, readdir, readFile, rm, stat, writeFile } from 'node:fs/promises'
7
+ import { join } from 'node:path'
8
+ import { pathToFileURL } from 'node:url'
9
+
10
+ import { failureText, KiokuFailure } from '../lib/http.mjs'
11
+ import {
12
+ clip,
13
+ contextOf,
14
+ MAX_PROMPT,
15
+ MAX_REPLY,
16
+ MAX_SESSION_ID,
17
+ Misconfigured,
18
+ postEvent,
19
+ projectOf,
20
+ TIMEOUTS,
21
+ } from '../lib/lifecycle.mjs'
22
+ import { profileDir, readProfile } from '../lib/profile.mjs'
23
+
24
+ const ID = /^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/
25
+ const STATE_PREFIX = 'turn.'
26
+ const STATE_MAX_AGE = 24 * 60 * 60 * 1000
27
+ const TRANSCRIPT_TAIL_BYTES = 2 * 1024 * 1024
28
+ const SETUP_HINT =
29
+ '运行 kioku setup agy 配置(没有 kioku 命令时用 npx @kioku-memory/cli setup agy)'
30
+
31
+ async function readStdin() {
32
+ const chunks = []
33
+ for await (const chunk of process.stdin) chunks.push(chunk)
34
+ return Buffer.concat(chunks).toString('utf8')
35
+ }
36
+
37
+ function stateDir(env) {
38
+ return join(env.PLUGIN_DATA || join(profileDir(env), 'agy-state'), 'turns')
39
+ }
40
+
41
+ function stateFile(env, sessionId) {
42
+ if (!ID.test(sessionId ?? '')) return null
43
+ return join(stateDir(env), `${STATE_PREFIX}${sessionId}.json`)
44
+ }
45
+
46
+ async function readState(env, sessionId) {
47
+ const file = stateFile(env, sessionId)
48
+ if (!file) return null
49
+ try {
50
+ return JSON.parse(await readFile(file, 'utf8'))
51
+ } catch {
52
+ return null
53
+ }
54
+ }
55
+
56
+ async function writeState(env, sessionId, data) {
57
+ const file = stateFile(env, sessionId)
58
+ if (!file) return
59
+ await mkdir(stateDir(env), { recursive: true, mode: 0o700 })
60
+ await writeFile(file, JSON.stringify(data), { mode: 0o600 })
61
+ }
62
+
63
+ async function sweepState(env) {
64
+ const dir = stateDir(env)
65
+ const names = await readdir(dir).catch(() => [])
66
+ for (const name of names) {
67
+ if (!name.startsWith(STATE_PREFIX)) continue
68
+ const file = join(dir, name)
69
+ const age = Date.now() - ((await stat(file).catch(() => null))?.mtimeMs ?? Date.now())
70
+ if (age > STATE_MAX_AGE) await rm(file, { force: true }).catch(() => {})
71
+ }
72
+ }
73
+
74
+ // 只读 transcript 末尾一段 JSONL,提取最新用户步骤序号与本轮最新的助手回复文本
75
+ async function readTranscriptTail(path) {
76
+ if (typeof path !== 'string' || !path) return { userStep: null, reply: '' }
77
+ let handle
78
+ try {
79
+ handle = await open(path, 'r')
80
+ const info = await handle.stat()
81
+ const size = info.size
82
+ const start = Math.max(0, size - TRANSCRIPT_TAIL_BYTES)
83
+ const len = size - start
84
+ if (len <= 0) return { userStep: null, reply: '' }
85
+ const buf = Buffer.alloc(len)
86
+ const { bytesRead } = await handle.read(buf, 0, len, start)
87
+ if (bytesRead <= 0) return { userStep: null, reply: '' }
88
+ let text = buf.subarray(0, bytesRead).toString('utf8')
89
+ if (start > 0) {
90
+ const nl = text.indexOf('\n')
91
+ text = nl >= 0 ? text.slice(nl + 1) : ''
92
+ }
93
+ let userStep = null
94
+ let reply = ''
95
+ for (const line of text.split('\n')) {
96
+ if (!line.trim()) continue
97
+ try {
98
+ const entry = JSON.parse(line)
99
+ if (entry?.type === 'USER_INPUT' && entry.step_index !== undefined) {
100
+ userStep = String(entry.step_index)
101
+ reply = ''
102
+ } else if (
103
+ entry?.type === 'PLANNER_RESPONSE' &&
104
+ typeof entry.content === 'string' &&
105
+ entry.content.trim()
106
+ ) {
107
+ reply = entry.content
108
+ }
109
+ } catch {
110
+ // 忽略截断或不完整行
111
+ }
112
+ }
113
+ return { userStep, reply }
114
+ } catch {
115
+ return { userStep: null, reply: '' }
116
+ } finally {
117
+ await handle?.close().catch(() => {})
118
+ }
119
+ }
120
+
121
+ function turnSignature(prompt, userStep) {
122
+ const hash = createHash('sha256').update(prompt).digest('hex').slice(0, 16)
123
+ return `${userStep ?? 'none'}:${hash}`
124
+ }
125
+
126
+ function buildInjectResult(text) {
127
+ if (typeof text !== 'string' || !text) return {}
128
+ return { injectSteps: [{ systemMessage: { systemMessage: text } }] }
129
+ }
130
+
131
+ export async function forwardAgyEvent(event, rawStdin, env = process.env) {
132
+ const timeout = TIMEOUTS[event]
133
+ if (!timeout) throw new KiokuFailure(`未知事件 ${event}`)
134
+ let input = {}
135
+ try {
136
+ input = JSON.parse(rawStdin || '{}')
137
+ } catch {
138
+ // 非 JSON 时按空输入处理,避免阻断模型执行
139
+ }
140
+ if (event !== 'turn.end') await sweepState(env)
141
+
142
+ let profile
143
+ try {
144
+ profile = await readProfile('agy', env)
145
+ } catch (err) {
146
+ throw err instanceof KiokuFailure ? new Misconfigured(err.message, 'profile') : err
147
+ }
148
+ if (!profile) {
149
+ if (event === 'session.start') {
150
+ return buildInjectResult(
151
+ `Kioku 插件还没有配置服务地址和访问密钥,长期记忆不可用:${SETUP_HINT}。`,
152
+ )
153
+ }
154
+ return {}
155
+ }
156
+
157
+ const { url: base, key } = profile
158
+ const sessionId =
159
+ typeof input.conversationId === 'string' && input.conversationId
160
+ ? input.conversationId.slice(0, MAX_SESSION_ID)
161
+ : undefined
162
+ const body = { event, ...(sessionId ? { sessionId } : {}) }
163
+
164
+ if (event === 'session.start') {
165
+ const cwd =
166
+ (typeof input.cwd === 'string' && input.cwd) ||
167
+ (Array.isArray(input.workspacePaths) && input.workspacePaths[0]) ||
168
+ process.cwd()
169
+ const project = projectOf(cwd)
170
+ if (Object.keys(project).length) body.project = project
171
+ const res = await postEvent(base, key, body, timeout)
172
+ const { hostOutput } = await res.json()
173
+ return buildInjectResult(contextOf(hostOutput))
174
+ }
175
+
176
+ if (event === 'prompt.submit') {
177
+ const prompt = typeof input.lastUserInput === 'string' ? input.lastUserInput.trim() : ''
178
+ if (!prompt) return {}
179
+ const { userStep } = await readTranscriptTail(input.transcriptPath)
180
+ const sig = turnSignature(prompt, userStep)
181
+ if (sessionId) {
182
+ const prev = await readState(env, sessionId)
183
+ if (prev?.sig === sig) return {}
184
+ await writeState(env, sessionId, { sig, prompt: clip(prompt, MAX_PROMPT) }).catch(() => {})
185
+ }
186
+ body.prompt = clip(prompt, MAX_PROMPT)
187
+ const res = await postEvent(base, key, body, timeout)
188
+ const { hostOutput } = await res.json()
189
+ return buildInjectResult(contextOf(hostOutput))
190
+ }
191
+
192
+ if (event === 'turn.end') {
193
+ if (input.continuationActive === true) return {}
194
+ const file = sessionId ? stateFile(env, sessionId) : null
195
+ const prev = sessionId ? await readState(env, sessionId) : null
196
+ if (file) await rm(file, { force: true }).catch(() => {})
197
+ const user =
198
+ (typeof input.lastUserInput === 'string' && input.lastUserInput.trim()) || prev?.prompt || ''
199
+ if (!sessionId || !user) return {}
200
+ const { reply } = await readTranscriptTail(input.transcriptPath)
201
+ if (!reply.trim()) return {}
202
+ body.user = clip(user, MAX_PROMPT)
203
+ body.reply = clip(reply.trim(), MAX_REPLY)
204
+ await postEvent(base, key, body, timeout)
205
+ return {}
206
+ }
207
+
208
+ return {}
209
+ }
210
+
211
+ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
212
+ const event = process.argv[2]
213
+ readStdin()
214
+ .then((raw) => forwardAgyEvent(event, raw, process.env))
215
+ .then((output) => {
216
+ process.stdout.write(JSON.stringify(output ?? {}))
217
+ })
218
+ .catch((err) => {
219
+ if (event === 'session.start' && err instanceof Misconfigured) {
220
+ process.stdout.write(
221
+ JSON.stringify(
222
+ buildInjectResult(
223
+ `Kioku 插件的配置无法使用,长期记忆不可用:${err.message}。${SETUP_HINT}。`,
224
+ ),
225
+ ),
226
+ )
227
+ } else {
228
+ process.stdout.write('{}')
229
+ }
230
+ process.stderr.write(`Kioku ${event ?? ''} 未生效:${failureText(err)}\n`)
231
+ })
232
+ }
@@ -0,0 +1,25 @@
1
+ {
2
+ "kioku": {
3
+ "SessionStart": [
4
+ {
5
+ "type": "command",
6
+ "command": "node ./hooks/forward.mjs session.start",
7
+ "timeout": 10
8
+ }
9
+ ],
10
+ "PreInvocation": [
11
+ {
12
+ "type": "command",
13
+ "command": "node ./hooks/forward.mjs prompt.submit",
14
+ "timeout": 10
15
+ }
16
+ ],
17
+ "Stop": [
18
+ {
19
+ "type": "command",
20
+ "command": "node ./hooks/forward.mjs turn.end",
21
+ "timeout": 10
22
+ }
23
+ ]
24
+ }
25
+ }
@@ -0,0 +1,53 @@
1
+ // 插件目录里放着的精简 kioku 入口:只实现 kioku run(插件启用期间放在 PATH 里给 Bash 工具用)。
2
+ // 向导、接入、检查、日常记忆读写等完整命令行在 npm 包 @kioku-memory/cli
3
+
4
+ import { parseRunArgs, run, USAGE, UsageError } from './run.mjs'
5
+
6
+ export { parseRunArgs, run, USAGE, UsageError }
7
+
8
+ const OVERVIEW = `用法:kioku run -e 变量=<标识>#<字段> [-e …] [--purpose <用途>] -- <命令> [参数…]
9
+
10
+ 插件目录内置的命令行入口仅包含 kioku run(向 Kioku 取凭据,放进环境变量并打码输出)。
11
+ 向导、接入管理与记忆读写等完整功能见 @kioku-memory/cli:
12
+
13
+ npx @kioku-memory/cli
14
+ npm install -g @kioku-memory/cli
15
+
16
+ 运行子命令用法:kioku run --help
17
+ `
18
+
19
+ const FULL_CLI_COMMANDS = new Set([
20
+ 'connect',
21
+ 'disconnect',
22
+ 'upgrade',
23
+ 'status',
24
+ 'login',
25
+ 'setup',
26
+ 'install',
27
+ 'doctor',
28
+ 'clean',
29
+ 'import',
30
+ 'local',
31
+ 'open',
32
+ 'search',
33
+ 'get',
34
+ 'ls',
35
+ 'add',
36
+ ])
37
+
38
+ export async function main(argv) {
39
+ const [sub, ...rest] = argv
40
+ if (sub === '-h' || sub === '--help') {
41
+ process.stdout.write(OVERVIEW)
42
+ return 0
43
+ }
44
+ if (sub === 'run') return run(rest)
45
+ if (sub && FULL_CLI_COMMANDS.has(sub)) {
46
+ process.stderr.write(
47
+ `kioku:插件内置入口仅含 run 子命令;运行 npx @kioku-memory/cli ${sub}(或全局安装 @kioku-memory/cli)\n`,
48
+ )
49
+ return 2
50
+ }
51
+ process.stderr.write(sub === undefined ? OVERVIEW : `kioku:不认识的子命令 ${sub}\n\n${OVERVIEW}`)
52
+ return 2
53
+ }