@kioku-memory/cli 0.0.0-stage → 0.1.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/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
package/NOTICE ADDED
@@ -0,0 +1,7 @@
1
+ kioku-plugins
2
+ Copyright 2026 lonelyman0108
3
+
4
+ This product is licensed under the Apache License, Version 2.0 (see LICENSE).
5
+
6
+ packages/core/lib/command.mjs 里给 cmd.exe 转义参数的规则改写自 cross-spawn
7
+ (https://github.com/moxystudio/node-cross-spawn),MIT License,Copyright (c) 2018 Made With MOXY Lda。
package/README.md CHANGED
@@ -1,3 +1,152 @@
1
- # Temporary Holding Version
1
+ # Kioku 的命令行
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [Kioku](https://github.com/lonelyman0108/kioku) 是自托管的长期记忆服务。这个包提供 `kioku` 命令,要 Node 18 或更新的版本:
4
+
5
+ ```bash
6
+ npx @kioku-memory/cli doctor
7
+ # 或者装到全局
8
+ npm install -g @kioku-memory/cli
9
+ ```
10
+
11
+ Claude Code 的 kioku 插件自带同一个命令,启用插件后在它的 Bash 工具里直接用 `kioku`,不用另装。
12
+
13
+ ## 子命令
14
+
15
+ | 子命令 | 作用 |
16
+ |---|---|
17
+ | `kioku setup <平台>` | 把平台交给 Kioku 接管:停用平台自带的长期记忆;Codex、pi、Hermes、OpenClaw、DeepSeek Harness 再写下服务地址与访问密钥,Codex 还配好 MCP 服务 |
18
+ | `kioku login <平台>` | 用浏览器授权签发访问密钥并配好这个平台,不用手动复制 |
19
+ | `kioku doctor [平台]` | 检查地址、访问密钥与平台的配置 |
20
+ | `kioku import --from <来源>` | 把平台自带的本地长期记忆迁进 Kioku |
21
+ | `kioku run -e 变量=<标识>#<字段> -- <命令>` | 向 Kioku 取凭据的值,放进环境变量交给命令 |
22
+
23
+ 每个子命令的完整说明:`kioku <子命令> --help`。现在认得的平台:`claude-code`、`codex`、`pi`、`hermes`、`openclaw`、`dsh`。
24
+
25
+ ## login
26
+
27
+ ```bash
28
+ kioku login codex --server-url https://kioku.example.com
29
+ ```
30
+
31
+ 终端显示一个确认码并打开 Kioku 管理界面的确认页(`--no-browser` 时只显示地址)。登录后在页面上输入确认码,选空间、应用与权限范围,点确认;命令行取到访问密钥后接着做 `kioku setup` 的事:核对、写下配置。访问密钥全程不显示。认得 `codex`、`pi`、`hermes`、`openclaw`、`dsh`;Claude Code 的访问密钥在它的 `/plugin` 里填。
32
+
33
+ - 确认码不在打开的地址里,要照着终端输入:只在自己刚运行了这条命令时确认。别人发来一个确认码让你去确认的,不要确认,那样对方的机器会拿到能读写你的记忆的访问密钥。
34
+ - 申请 10 分钟内有效。被拒绝、过期,或者确认之后确认人的角色变了,都拿不到访问密钥,重新运行即可。
35
+ - 不用在本机开端口,远程登录的机器上也能用:在任何一台登录了管理界面的浏览器里确认都行。
36
+ - 要 Kioku 服务端支持浏览器授权;旧的服务端会提示改用 `kioku setup` 手动填访问密钥。
37
+
38
+ `kioku setup codex`、`kioku setup pi` 第一次配置时,在终端里提示输入访问密钥的地方直接回车,走的是同一个流程。已经配过的再运行 `setup`,直接回车是沿用上次的访问密钥;要换一个就运行 `kioku login`。
39
+
40
+ - 确认之后如果这台机器没能把访问密钥写下(配置文件写不进去、中途断网),命令会提示去管理界面的应用页吊销刚签发的那个,再重新运行:访问密钥只在签发时给一次。
41
+
42
+ ## setup
43
+
44
+ ```bash
45
+ kioku setup claude-code --dry-run # 只看要改什么
46
+ kioku setup claude-code # 停用 Claude Code 的自动记忆
47
+ kioku setup claude-code --undo # 改回去
48
+ ```
49
+
50
+ 只改平台的用户设置里和自带记忆有关的几项(Claude Code 是 `~/.claude/settings.json` 的 `autoMemoryEnabled`),设置里的其他内容不动。第一次改之前把原来的文件备份成同目录下的 `settings.json.kioku-backup`(已有备份时不覆盖),改完新开会话生效。本地已有的记忆文件不会被删掉。
51
+
52
+ - 设置文件是符号链接时改它指向的文件;文件会按 JSON 重新排版(缩进、换行符沿用原来的)。
53
+ - 只看用户设置:当前项目的 `.claude/settings.json`、`.claude/settings.local.json` 里又启用了的话会提醒,要在那里一起改;托管的设置查不到。
54
+
55
+ 不停用的话,平台自带的记忆和 Kioku 会各记一份,两边的内容迟早对不上。
56
+
57
+ ### Codex
58
+
59
+ ```bash
60
+ kioku setup codex --server-url https://kioku.example.com
61
+ ```
62
+
63
+ 提示输入访问密钥(不回显;也可以放在 `KIOKU_API_KEY` 里或从标准输入给),核对后写下 `~/.config/kioku/codex.json`,在 `~/.codex/config.toml` 里加上 Kioku 的 MCP 服务并关掉 Memories,再列出在 Codex 这一边要做的三步。改了什么、怎么撤销见 [Codex 插件的说明](https://github.com/lonelyman0108/kioku-plugins/tree/main/plugins/codex#setup-改了什么)。
64
+
65
+ ### pi
66
+
67
+ ```bash
68
+ kioku setup pi --server-url https://kioku.example.com
69
+ ```
70
+
71
+ 核对后写下 `~/.config/kioku/pi.json`,不改 pi 的设置(pi 没有自带的长期记忆,MCP 服务由扩展注册)。扩展用 `pi install git:github.com/lonelyman0108/kioku-plugins` 装。
72
+
73
+ ### Hermes
74
+
75
+ ```bash
76
+ kioku setup hermes --server-url https://kioku.example.com
77
+ ```
78
+
79
+ 核对后把地址与访问密钥写进 `~/.hermes/.env`,再经 `hermes config set` 选 Kioku 做记忆 provider、关掉内置的 `MEMORY.md` 与 `USER.md`。provider 用 `hermes plugins install lonelyman0108/kioku-plugins/plugins/hermes --enable` 装。细节见 [Hermes provider 的说明](https://github.com/lonelyman0108/kioku-plugins/tree/main/plugins/hermes#setup-改了什么)。
80
+
81
+ ### OpenClaw
82
+
83
+ ```bash
84
+ kioku setup openclaw --server-url https://kioku.example.com
85
+ ```
86
+
87
+ 核对后写下 `~/.config/kioku/openclaw.json`,再经 `openclaw config set` 允许插件读对话(`plugins.entries.kioku.hooks.allowConversationAccess`);记忆槽位在装插件时由 OpenClaw 选上,被换走了才由 `setup` 选回来。插件用 `openclaw plugins install npm:@kioku-memory/openclaw` 装,装完重启网关。细节见 [OpenClaw 插件的说明](https://github.com/lonelyman0108/kioku-plugins/tree/main/plugins/openclaw#setup-改了什么)。
88
+
89
+ ### DeepSeek Harness
90
+
91
+ ```bash
92
+ kioku setup dsh --server-url https://kioku.example.com
93
+ ```
94
+
95
+ 核对后写下 `~/.config/kioku/dsh.json`,不改 dsh 的设置(它没有自带的长期记忆)。插件按 profile 装:`dsh plugin --profile <profile> add github:lonelyman0108/kioku-plugins`,装完重启 dsh。
96
+
97
+ ## doctor
98
+
99
+ ```bash
100
+ kioku doctor claude-code
101
+ ```
102
+
103
+ 每项一行:`✓` 正常,`!` 要留意,`✗` 有问题(这时退出码为 1)。查的是:地址连不连得上、访问密钥是否有效、它属于哪个应用、能读写哪些归属、最高敏感级与凭据能力,以及平台这一边插件是否启用、自带的长期记忆是否已停用。不写平台时按当前所在的会话认。
104
+
105
+ ## import
106
+
107
+ ```bash
108
+ kioku import --from claude-code --dry-run # 只看计划
109
+ kioku import --from claude-code # 列出计划,确认后写入
110
+ ```
111
+
112
+ 先读本地的记忆文件,列出每条会放到哪个坐标、Kioku 里有没有,确认之后才写。本地的文件不删、不改。
113
+
114
+ | 来源 | 读什么 | 放到哪 |
115
+ |---|---|---|
116
+ | `claude-code` | `~/.claude/projects/*/memory/*.md`(`MEMORY.md` 是索引,不读) | 按文件头里的类型:`user` → `user://about/<项目>/<名字>`;`project` → `user://projects/<项目>/<名字>`;`reference` → `user://reference/<项目>/<名字>`;`feedback` → `app://<应用>/feedback/<项目>/<名字>` |
117
+ | `hermes` | `~/.hermes/memories/` 的 `MEMORY.md`、`USER.md` | `MEMORY.md` → `app://<应用>/hermes/notes/…`;`USER.md` → `user://about/hermes/…` |
118
+ | `openclaw` | `~/.openclaw/workspace/` 的 `MEMORY.md`、`USER.md` 与 `memory/` 里按日期的笔记(`<日期>.md`、`<日期>-<名字>.md`) | `MEMORY.md` → `app://<应用>/openclaw/notes/…`;`USER.md` → `user://about/openclaw/…`;按日期的笔记 → `app://<应用>/openclaw/journal/<文件名>`,一个文件一条 |
119
+
120
+ - `<应用>` 是访问密钥所属的应用的标识。偏「这个 agent 该怎么干活」的放在应用下,别的 agent 用不上;关于用户本人和项目的放在本人下,各个 agent 都读得到。
121
+ - `<项目>` 取自 Claude Code 的项目目录名,去掉主目录与 `Documents`、`code` 这类常见的上级目录。分不清的两个项目(例如 `~/code/dev-proxy` 与 `~/code/proxy`)会得到同一个名字,同名的记忆会在计划里标出来,不会悄悄顶掉。
122
+ - Hermes、OpenClaw 的一个文件拆成几条:有只写着 `§` 的分隔行就按它拆,否则按二级标题拆,都没有就是一整条。有标题的用标题做标识;没有标题的用内容的摘要,所以文件里插一条、删一条不影响别的条,但改了某一段的内容会新建一条、旧的留着。Hermes 的位置与分隔符已经对着它的源码核对过(0.19.0),OpenClaw 的位置与文件名对着真实的安装核对过(2026.6.35)。OpenClaw 的 `USER.md` 是它生成的模板,没填过的话计划里会列出模板本身,不想要就先别迁。
123
+ - 标题与触发场景取文件头里的说明。写进去的记忆优先级是默认的,不进启动加载;要进的在管理界面里改。
124
+ - **重复运行不重复写**:坐标由来源决定,已经有、内容一样的跳过;已经有、内容不一样的默认也跳过(可能在 Kioku 里改过),`--overwrite` 才覆盖。
125
+ - **疑似明文凭据的不写**:列出是哪个文件、正文第几行、哪一类,不带值。先把值存成凭据、正文里写占位符,或者从文件里去掉,再运行一次。
126
+ - 不迁的会列出原因:正文超过 2 万字、没有内容、名字规整之后和别的一条撞了、文件头没有结尾。
127
+ - 只读普通的文本文件:符号链接不跟(它可以指到记忆目录之外的文件),太大的、不是文本的不读。
128
+ - 写到一半连不上时停下来,说明写了多少、还剩多少;重新运行会接着写。
129
+ - 不在终端里运行时要带 `--yes` 才写。`--path` 指明不在默认位置的目录。
130
+ - 地址与访问密钥读环境变量(Claude Code 的会话里已经配好);`--platform codex`、`--platform pi`、`--platform hermes`、`--platform openclaw` 或 `--platform dsh` 改用 `kioku setup` 给那个平台写下的。记忆迁进这个访问密钥所属的应用。
131
+ - 指令文件(`AGENTS.md`、`CLAUDE.md`)不是记忆,不迁。Codex 自带的 Memories 存在它自己的数据库里、格式没有公开,迁不了。
132
+
133
+ 迁完之后用 `kioku setup <平台>` 停用平台自带的记忆,免得两边接着各记一份。
134
+
135
+ ## 各系统
136
+
137
+ macOS 与 Linux 上各平台都用真实的模型实测过。Windows 上跑过单元用例,也在 CI 的 Windows 机器上装真实的平台走过一遍(pi、Codex、OpenClaw、Hermes、DeepSeek Harness;模型是假的,核对的是装插件、注入、上报与工具登记),交互界面没有看过:
138
+
139
+ - 配置文件在 `%USERPROFILE%\.config\kioku\`(和别的系统一样的相对位置,可以用 `KIOKU_CONFIG_DIR` 改)。Windows 没有 600 这种权限位,文件靠用户目录自己的访问控制保护。
140
+ - npm 装的命令是 `.cmd`:`kioku setup openclaw`、`kioku run -- npm …` 会经 `cmd.exe` 起它们,参数里的 `&`、`|`、`%` 这类字符照原样交给命令。
141
+ - `kioku run` 转发信号(Ctrl-C、终止)的行为只在 macOS、Linux 上核对过。
142
+
143
+ ## 地址与访问密钥
144
+
145
+ `kioku doctor codex`、`kioku doctor pi`、`kioku doctor hermes`、`kioku doctor openclaw`、`kioku doctor dsh` 读 `kioku setup` 写下的配置。别的情况下 `doctor` 与 `run` 从环境变量读:
146
+
147
+ - `KIOKU_SERVER_URL`:例如 `https://kioku.example.com`,不带 `/v1`
148
+ - `KIOKU_API_KEY`:在 Kioku 管理界面的「应用」页签发的访问密钥;或者用 `KIOKU_API_KEY_FILE` 指向只含访问密钥的文件
149
+
150
+ 在 Claude Code 里,插件在会话开始时已经配好这两项。`setup` 只改本机的设置文件,用不到它们。
151
+
152
+ `kioku run` 的用法与打码的限度见 [Claude Code 插件的说明](https://github.com/lonelyman0108/kioku-plugins/tree/main/plugins/claude-code#kioku-run)。
package/bin/kioku ADDED
@@ -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
+ })
package/lib/api.mjs ADDED
@@ -0,0 +1,35 @@
1
+ // kioku 命令向 Kioku 发的核对请求
2
+
3
+ import { fetchFailure, isRedirect, KiokuFailure, redirectError } from './http.mjs'
4
+
5
+ const TIMEOUT = 8000
6
+
7
+ // 这个访问密钥对应的应用与权限;连不上、跳转、访问密钥无效、响应不对都以 KiokuFailure 抛出
8
+ export async function whoami({ url, key }) {
9
+ let res
10
+ let data
11
+ try {
12
+ res = await fetch(`${url}/v1/whoami`, {
13
+ headers: { authorization: `Bearer ${key}` },
14
+ // 不跟着跳转走:访问密钥只发给配置的地址
15
+ redirect: 'manual',
16
+ signal: AbortSignal.timeout(TIMEOUT),
17
+ })
18
+ data = await res.json().catch(() => null)
19
+ } catch (err) {
20
+ throw new KiokuFailure(fetchFailure(err, TIMEOUT))
21
+ }
22
+ if (isRedirect(res.status)) {
23
+ throw new KiokuFailure(redirectError(res.status, res.headers.get('location'), url))
24
+ }
25
+ if (res.status === 401) throw new KiokuFailure('访问密钥无效或已吊销')
26
+ if (!res.ok) throw new KiokuFailure(`Kioku 返回 HTTP ${res.status}`)
27
+ if (
28
+ typeof data?.app?.runtimeFamily !== 'string' ||
29
+ data.scopes === null ||
30
+ typeof data.scopes !== 'object'
31
+ ) {
32
+ throw new KiokuFailure('Kioku 的响应格式不对')
33
+ }
34
+ return data
35
+ }