@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.
- package/NOTICE +3 -0
- package/README.md +105 -25
- package/assets/plugins/agy/README.md +34 -0
- package/assets/plugins/agy/bin/kioku +36 -0
- package/assets/plugins/agy/hooks/forward.mjs +232 -0
- package/assets/plugins/agy/hooks.json +25 -0
- package/assets/plugins/agy/lib/kioku.mjs +53 -0
- package/{lib → assets/plugins/agy/lib}/lifecycle.mjs +13 -3
- package/{lib/kioku.mjs → assets/plugins/agy/lib/run.mjs} +4 -80
- package/assets/plugins/agy/package.json +7 -0
- package/assets/plugins/agy/plugin.json +6 -0
- package/assets/plugins/agy/vitest.config.mjs +5 -0
- package/assets/plugins/gemini/README.md +33 -0
- package/assets/plugins/gemini/bin/kioku +36 -0
- package/assets/plugins/gemini/gemini-extension.json +5 -0
- package/assets/plugins/gemini/hooks/forward.mjs +174 -0
- package/assets/plugins/gemini/hooks/hooks.json +43 -0
- package/assets/plugins/gemini/lib/command.mjs +152 -0
- package/assets/plugins/gemini/lib/config.mjs +50 -0
- package/assets/plugins/gemini/lib/http.mjs +57 -0
- package/assets/plugins/gemini/lib/kioku.mjs +53 -0
- package/assets/plugins/gemini/lib/lifecycle.mjs +128 -0
- package/assets/plugins/gemini/lib/mcp.mjs +142 -0
- package/assets/plugins/gemini/lib/profile.mjs +100 -0
- package/assets/plugins/gemini/lib/redact.mjs +91 -0
- package/assets/plugins/gemini/lib/run.mjs +372 -0
- package/assets/plugins/gemini/lib/text.mjs +14 -0
- package/assets/plugins/gemini/lib/usage.mjs +2 -0
- package/assets/plugins/gemini/package.json +7 -0
- package/assets/plugins/gemini/vitest.config.mjs +5 -0
- package/bin/kioku +16 -12
- package/dist/index.mjs +8846 -0
- package/package.json +14 -5
- package/lib/api.mjs +0 -38
- package/lib/codex.mjs +0 -320
- package/lib/connect.mjs +0 -152
- package/lib/doctor.mjs +0 -145
- package/lib/dsh.mjs +0 -82
- package/lib/files.mjs +0 -92
- package/lib/hermes.mjs +0 -276
- package/lib/import.mjs +0 -301
- package/lib/install.mjs +0 -146
- package/lib/local.mjs +0 -294
- package/lib/login.mjs +0 -347
- package/lib/openclaw.mjs +0 -256
- package/lib/panel.mjs +0 -295
- package/lib/pi.mjs +0 -90
- package/lib/platforms.mjs +0 -220
- package/lib/prompts.mjs +0 -38
- package/lib/secret.mjs +0 -86
- package/lib/setup.mjs +0 -112
- package/lib/sources.mjs +0 -333
- package/lib/target.mjs +0 -62
- package/lib/wizard.mjs +0 -447
- /package/{lib → assets/plugins/agy/lib}/command.mjs +0 -0
- /package/{lib → assets/plugins/agy/lib}/config.mjs +0 -0
- /package/{lib → assets/plugins/agy/lib}/http.mjs +0 -0
- /package/{lib → assets/plugins/agy/lib}/mcp.mjs +0 -0
- /package/{lib → assets/plugins/agy/lib}/profile.mjs +0 -0
- /package/{lib → assets/plugins/agy/lib}/redact.mjs +0 -0
- /package/{lib → assets/plugins/agy/lib}/text.mjs +0 -0
- /package/{lib → assets/plugins/agy/lib}/usage.mjs +0 -0
package/lib/import.mjs
DELETED
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
// kioku import:把平台自带的本地长期记忆迁进 Kioku。先读本地文件出一份计划(每条放到哪个坐标),
|
|
2
|
-
// 和 Kioku 里已有的比过之后列出来,确认了才写。坐标由来源决定,所以重复运行不会重复写:
|
|
3
|
-
// 已经有、内容一样的跳过;已经有、内容不一样的不覆盖(可能在 Kioku 里改过),要覆盖得明说。
|
|
4
|
-
// 本地的文件不删、不改
|
|
5
|
-
|
|
6
|
-
import { whoami } from './api.mjs'
|
|
7
|
-
import { loadConfig } from './config.mjs'
|
|
8
|
-
import { fetchFailure, isRedirect, KiokuFailure, redirectError } from './http.mjs'
|
|
9
|
-
import { platformNames, PLATFORMS } from './platforms.mjs'
|
|
10
|
-
import { sourceNames, SOURCES, UNSUPPORTED_SOURCES } from './sources.mjs'
|
|
11
|
-
import { plain, shownUrl } from './text.mjs'
|
|
12
|
-
import { UsageError } from './usage.mjs'
|
|
13
|
-
|
|
14
|
-
export const IMPORT_USAGE = `用法:kioku import --from <来源> [--path <目录>] [--platform <平台>] [--dry-run] [--yes] [--overwrite]
|
|
15
|
-
|
|
16
|
-
把平台自带的本地长期记忆迁进 Kioku:先列出每条会放到哪个坐标,确认之后才写。
|
|
17
|
-
|
|
18
|
-
--from <来源> ${sourceNames()}
|
|
19
|
-
--path <目录> 不用默认的位置时指明(Claude Code 是 projects 目录或某个项目的 memory 目录)
|
|
20
|
-
--platform <平台> 用 kioku setup 给这个平台配好的地址与访问密钥(codex、pi、hermes、openclaw、dsh);不写时读环境变量
|
|
21
|
-
--dry-run 只列出计划,不写
|
|
22
|
-
--yes 不问,直接写(不在终端里运行时要带上)
|
|
23
|
-
--overwrite Kioku 里同一个坐标已经有、内容不一样的也覆盖(默认跳过)
|
|
24
|
-
|
|
25
|
-
本地的文件不删、不改。重复运行不会重复写:已经迁过、内容没变的会跳过。
|
|
26
|
-
疑似明文凭据的不写,列出是哪个文件的哪一行:先把值存成凭据,或者从文件里去掉再迁。
|
|
27
|
-
地址与访问密钥读 KIOKU_SERVER_URL 与 KIOKU_API_KEY(Claude Code 的会话里已经配好),或者用 --platform 指明。
|
|
28
|
-
记忆迁进这个访问密钥所属的应用。
|
|
29
|
-
指令文件(AGENTS.md、CLAUDE.md)不是记忆,不迁。
|
|
30
|
-
`
|
|
31
|
-
|
|
32
|
-
const TIMEOUT = 15_000
|
|
33
|
-
|
|
34
|
-
export function parseImportArgs(argv) {
|
|
35
|
-
const options = {
|
|
36
|
-
from: undefined,
|
|
37
|
-
path: undefined,
|
|
38
|
-
platform: undefined,
|
|
39
|
-
dryRun: false,
|
|
40
|
-
yes: false,
|
|
41
|
-
overwrite: false,
|
|
42
|
-
}
|
|
43
|
-
for (let i = 0; i < argv.length; i++) {
|
|
44
|
-
const arg = argv[i]
|
|
45
|
-
if (arg === '--dry-run') options.dryRun = true
|
|
46
|
-
else if (arg === '--yes') options.yes = true
|
|
47
|
-
else if (arg === '--overwrite') options.overwrite = true
|
|
48
|
-
else if (arg === '--from' || arg === '--path' || arg === '--platform') {
|
|
49
|
-
const value = argv[++i]
|
|
50
|
-
if (!value) throw new UsageError(`${arg} 后面要写值`)
|
|
51
|
-
options[arg.slice(2)] = value
|
|
52
|
-
} else throw new UsageError(`不认识的参数 ${arg}`)
|
|
53
|
-
}
|
|
54
|
-
if (!options.from) throw new UsageError('要写来源:--from <来源>')
|
|
55
|
-
if (Object.hasOwn(UNSUPPORTED_SOURCES, options.from)) {
|
|
56
|
-
throw new KiokuFailure(UNSUPPORTED_SOURCES[options.from])
|
|
57
|
-
}
|
|
58
|
-
if (!Object.hasOwn(SOURCES, options.from)) {
|
|
59
|
-
throw new UsageError(`不认识的来源 ${options.from},可以用的:${sourceNames()}`)
|
|
60
|
-
}
|
|
61
|
-
if (options.platform !== undefined && !PLATFORMS[options.platform]?.readConfig) {
|
|
62
|
-
throw new UsageError(
|
|
63
|
-
`--platform 只收用 kioku setup 写下配置的平台,可以用的:${platformNames()}`,
|
|
64
|
-
)
|
|
65
|
-
}
|
|
66
|
-
return options
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async function request({ url, key }, method, path, body) {
|
|
70
|
-
let res
|
|
71
|
-
let data
|
|
72
|
-
try {
|
|
73
|
-
res = await fetch(`${url}${path}`, {
|
|
74
|
-
method,
|
|
75
|
-
headers: {
|
|
76
|
-
authorization: `Bearer ${key}`,
|
|
77
|
-
...(body ? { 'content-type': 'application/json' } : {}),
|
|
78
|
-
},
|
|
79
|
-
...(body ? { body: JSON.stringify(body) } : {}),
|
|
80
|
-
// 不跟着跳转走:访问密钥只发给配置的地址
|
|
81
|
-
redirect: 'manual',
|
|
82
|
-
signal: AbortSignal.timeout(TIMEOUT),
|
|
83
|
-
})
|
|
84
|
-
data = await res.json().catch(() => null)
|
|
85
|
-
} catch (err) {
|
|
86
|
-
throw new KiokuFailure(fetchFailure(err, TIMEOUT))
|
|
87
|
-
}
|
|
88
|
-
if (isRedirect(res.status)) {
|
|
89
|
-
throw new KiokuFailure(redirectError(res.status, res.headers.get('location'), url))
|
|
90
|
-
}
|
|
91
|
-
return { status: res.status, data }
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// 服务端的错误文字压成一行;万一带着访问密钥也盖掉
|
|
95
|
-
function serverText(data, status, key) {
|
|
96
|
-
const text = typeof data?.error === 'string' ? data.error.split(key).join('***') : ''
|
|
97
|
-
return plain(text) || `HTTP ${status}`
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// 和 Kioku 里现有的比:new 没有,same 有且正文一样,differs 有但不一样(含读不到正文的机密)
|
|
101
|
-
async function compare(config, item) {
|
|
102
|
-
const { status, data } = await request(
|
|
103
|
-
config,
|
|
104
|
-
'GET',
|
|
105
|
-
`/v1/memories?uri=${encodeURIComponent(item.uri)}`,
|
|
106
|
-
)
|
|
107
|
-
if (status === 404) return { ...item, state: 'new' }
|
|
108
|
-
if (status !== 200)
|
|
109
|
-
return { ...item, state: 'failed', reason: serverText(data, status, config.key) }
|
|
110
|
-
const same = typeof data?.content === 'string' && data.content.trim() === item.content
|
|
111
|
-
return { ...item, state: same ? 'same' : 'differs', version: data?.version, id: data?.id }
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// 写一条:新建,或者覆盖已有的。返回 { ok } 或 { ok: false, reason, plaintext }
|
|
115
|
-
async function write(config, item) {
|
|
116
|
-
const fields = {
|
|
117
|
-
uri: item.uri,
|
|
118
|
-
title: item.title,
|
|
119
|
-
content: item.content,
|
|
120
|
-
...(item.disclosure ? { disclosure: item.disclosure } : {}),
|
|
121
|
-
}
|
|
122
|
-
const { status, data } =
|
|
123
|
-
item.state === 'differs'
|
|
124
|
-
? await request(config, 'PATCH', '/v1/memories', {
|
|
125
|
-
...fields,
|
|
126
|
-
...(Number.isInteger(item.version) ? { expectedVersion: item.version } : {}),
|
|
127
|
-
...(item.id ? { expectedId: item.id } : {}),
|
|
128
|
-
})
|
|
129
|
-
: await request(config, 'POST', '/v1/memories', fields)
|
|
130
|
-
if (status === 200 || status === 201) return { ok: true }
|
|
131
|
-
const plaintext = Array.isArray(data?.plaintext) ? data.plaintext : null
|
|
132
|
-
return { ok: false, reason: serverText(data, status, config.key), plaintext }
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const KINDS = { new: '新建', same: '已有,内容一样', differs: '已有,内容不一样' }
|
|
136
|
-
|
|
137
|
-
// 问一句是或否;只认 y 开头的回答
|
|
138
|
-
export async function confirm(question, { stdin, out }) {
|
|
139
|
-
out.write(question)
|
|
140
|
-
for await (const chunk of stdin) {
|
|
141
|
-
return /^\s*y/i.test(chunk.toString('utf8'))
|
|
142
|
-
}
|
|
143
|
-
return false
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// 本地的这个来源里有几条能迁的记忆(不和 Kioku 比)。来源的目录不在时为 null
|
|
147
|
-
export async function countLocal(from, { env, path }) {
|
|
148
|
-
const { found, entries } = await SOURCES[from].read({ env, path, app: 'app' })
|
|
149
|
-
return found ? entries.filter((item) => !item.skipped).length : null
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// 按顺序对每一项做 fn,同时最多 limit 个;结果的顺序和 items 一样
|
|
153
|
-
async function mapLimit(items, limit, fn) {
|
|
154
|
-
const results = new Array(items.length)
|
|
155
|
-
let next = 0
|
|
156
|
-
const worker = async () => {
|
|
157
|
-
while (next < items.length) {
|
|
158
|
-
const index = next++
|
|
159
|
-
results[index] = await fn(items[index])
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker))
|
|
163
|
-
return results
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// 和 Kioku 里比时同时发的请求数
|
|
167
|
-
const COMPARE_CONCURRENCY = 8
|
|
168
|
-
|
|
169
|
-
// 计划里还没迁进 Kioku 的条数(Kioku 里同一个坐标还没有的)。有比对失败的(不是 200、404)时
|
|
170
|
-
// 说不准,pending 为 null,reason 写明几条读取失败
|
|
171
|
-
export function pendingOf(plan) {
|
|
172
|
-
const failed = plan.compared.filter((item) => item.state === 'failed').length
|
|
173
|
-
if (failed) return { pending: null, reason: `${failed} 条读取失败` }
|
|
174
|
-
return { pending: plan.compared.filter((item) => item.state === 'new').length }
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// 读本地的记忆、和 Kioku 里的比过,列出计划:每条放到哪个坐标、Kioku 里有没有。
|
|
178
|
-
// 返回 { compared, skipped, todo };没有可以迁的时 todo 是空的、empty 为真
|
|
179
|
-
export async function planImport(config, { from, path, overwrite = false }, { env, out }) {
|
|
180
|
-
const source = SOURCES[from]
|
|
181
|
-
const who = await whoami(config)
|
|
182
|
-
out.write(
|
|
183
|
-
`Kioku:${shownUrl(config.url)},应用 ${plain(who.app.name)}(${plain(who.app.slug)})\n`,
|
|
184
|
-
)
|
|
185
|
-
|
|
186
|
-
const { root, found, entries } = await source.read({ env, path, app: who.app.slug })
|
|
187
|
-
if (!found)
|
|
188
|
-
throw new KiokuFailure(`没有找到${source.name}:${plain(root)} 不是目录,用 --path 指明位置`)
|
|
189
|
-
out.write(`来源:${source.name}(${plain(root)})\n`)
|
|
190
|
-
if (!entries.length) {
|
|
191
|
-
out.write('没有可以迁的记忆\n')
|
|
192
|
-
return { compared: [], skipped: [], todo: [], empty: true }
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// 两条落到同一个坐标的(名字规整之后撞了)只留第一条
|
|
196
|
-
const seen = new Set()
|
|
197
|
-
const usable = []
|
|
198
|
-
const skipped = []
|
|
199
|
-
for (const item of entries) {
|
|
200
|
-
if (item.skipped) skipped.push(item)
|
|
201
|
-
else if (seen.has(item.uri))
|
|
202
|
-
skipped.push({ ...item, skipped: `和前面的一条落到同一个坐标 ${item.uri}` })
|
|
203
|
-
else {
|
|
204
|
-
seen.add(item.uri)
|
|
205
|
-
usable.push(item)
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
// 一条一个请求:有界并发,几百条也不至于等太久
|
|
209
|
-
const compared = await mapLimit(usable, COMPARE_CONCURRENCY, (item) => compare(config, item))
|
|
210
|
-
|
|
211
|
-
out.write('\n')
|
|
212
|
-
for (const item of compared) {
|
|
213
|
-
const kind = item.state === 'failed' ? `读取失败(${item.reason})` : KINDS[item.state]
|
|
214
|
-
out.write(`${kind} ${item.uri}\n ${plain(item.title)}\n ← ${plain(item.source)}\n`)
|
|
215
|
-
}
|
|
216
|
-
for (const item of skipped) out.write(`不迁 ${plain(item.source)}\n ${item.skipped}\n`)
|
|
217
|
-
|
|
218
|
-
const count = (state) => compared.filter((item) => item.state === state).length
|
|
219
|
-
const todo = compared.filter(
|
|
220
|
-
(item) => item.state === 'new' || (item.state === 'differs' && overwrite),
|
|
221
|
-
)
|
|
222
|
-
out.write(
|
|
223
|
-
`\n新建 ${count('new')} 条,已有且一样 ${count('same')} 条,已有但不一样 ${count('differs')} 条` +
|
|
224
|
-
`(${overwrite ? '覆盖' : '跳过,要覆盖加 --overwrite'}),不迁 ${skipped.length} 条\n`,
|
|
225
|
-
)
|
|
226
|
-
return { compared, skipped, todo, empty: false }
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
// 按计划写入,返回退出码:有没写成的、读取失败的为 1
|
|
230
|
-
export async function writeImport(config, { compared, todo }, { out }) {
|
|
231
|
-
let written = 0
|
|
232
|
-
const failed = []
|
|
233
|
-
// 连不上、超时这类不是某一条的问题:停下来,把已经写了多少、还剩多少说清楚,不让它跳出整个命令
|
|
234
|
-
let stopped = null
|
|
235
|
-
for (const [index, item] of todo.entries()) {
|
|
236
|
-
let result
|
|
237
|
-
try {
|
|
238
|
-
result = await write(config, item)
|
|
239
|
-
} catch (err) {
|
|
240
|
-
if (!(err instanceof KiokuFailure)) throw err
|
|
241
|
-
stopped = { reason: err.message, left: todo.length - index }
|
|
242
|
-
break
|
|
243
|
-
}
|
|
244
|
-
if (result.ok) written++
|
|
245
|
-
else failed.push({ item, ...result })
|
|
246
|
-
}
|
|
247
|
-
out.write(`\n写入 ${written} 条\n`)
|
|
248
|
-
for (const { item, reason, plaintext } of failed) {
|
|
249
|
-
out.write(`没有写入 ${item.uri}\n ← ${plain(item.source)}\n ${reason}\n`)
|
|
250
|
-
// 只说第几行、哪一类,不带值;行号按正文算(文件头之后)
|
|
251
|
-
for (const spot of plaintext ?? []) {
|
|
252
|
-
out.write(` 正文第 ${Number(spot.line) || '?'} 行:${plain(spot.kind)}\n`)
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
if (failed.some((entry) => entry.plaintext)) {
|
|
256
|
-
out.write('疑似明文凭据的没有写:把值存成凭据、正文里写占位符,或者从文件里去掉,再运行一次\n')
|
|
257
|
-
}
|
|
258
|
-
if (stopped) {
|
|
259
|
-
out.write(
|
|
260
|
-
`${stopped.reason}:还有 ${stopped.left} 条没有写,重新运行会接着写(已经写进去的会跳过)\n`,
|
|
261
|
-
)
|
|
262
|
-
}
|
|
263
|
-
out.write('本地的文件没有动。\n')
|
|
264
|
-
return stopped || failed.length || compared.some((item) => item.state === 'failed') ? 1 : 0
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
export async function importMemories(
|
|
268
|
-
argv,
|
|
269
|
-
{ env = process.env, out = process.stdout, stdin = process.stdin } = {},
|
|
270
|
-
) {
|
|
271
|
-
const options = parseImportArgs(argv)
|
|
272
|
-
// 地址与访问密钥:指明了平台就用 kioku setup 给它写下的,否则读环境变量
|
|
273
|
-
let config
|
|
274
|
-
if (options.platform) {
|
|
275
|
-
config = await PLATFORMS[options.platform].readConfig(env)
|
|
276
|
-
if (!config) {
|
|
277
|
-
throw new KiokuFailure(
|
|
278
|
-
`还没有给 ${options.platform} 配过:先运行 kioku setup ${options.platform}`,
|
|
279
|
-
)
|
|
280
|
-
}
|
|
281
|
-
} else config = await loadConfig(env)
|
|
282
|
-
const plan = await planImport(config, options, { env, out })
|
|
283
|
-
if (plan.empty) return 0
|
|
284
|
-
if (options.dryRun) {
|
|
285
|
-
out.write('没有写入(--dry-run)\n')
|
|
286
|
-
return 0
|
|
287
|
-
}
|
|
288
|
-
if (!plan.todo.length) {
|
|
289
|
-
out.write('没有要写的\n')
|
|
290
|
-
return plan.compared.some((item) => item.state === 'failed') ? 1 : 0
|
|
291
|
-
}
|
|
292
|
-
if (!options.yes) {
|
|
293
|
-
if (!stdin.isTTY)
|
|
294
|
-
throw new KiokuFailure('不在终端里运行时要带 --yes 才写;先用 --dry-run 看计划')
|
|
295
|
-
if (!(await confirm(`写入这 ${plan.todo.length} 条?[y/N] `, { stdin, out }))) {
|
|
296
|
-
out.write('没有写入\n')
|
|
297
|
-
return 0
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
return writeImport(config, plan, { out })
|
|
301
|
-
}
|
package/lib/install.mjs
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
// kioku install:给脚本用的接入。几个平台一起配好,访问密钥按平台从环境变量取(KIOKU_API_KEY_CODEX 这类),
|
|
2
|
-
// 没给的平台跳过并列出来。插件不替用户装,最后列出要运行的命令
|
|
3
|
-
|
|
4
|
-
import { whoami } from './api.mjs'
|
|
5
|
-
import { configure, pluginSteps, resultsText } from './connect.mjs'
|
|
6
|
-
import { failureText, keyFormatError, KiokuFailure } from './http.mjs'
|
|
7
|
-
import { confirm } from './import.mjs'
|
|
8
|
-
import { platformNames, PLATFORMS } from './platforms.mjs'
|
|
9
|
-
import { setup } from './setup.mjs'
|
|
10
|
-
import { resolveServerUrl } from './target.mjs'
|
|
11
|
-
import { plain, shownUrl } from './text.mjs'
|
|
12
|
-
import { UsageError } from './usage.mjs'
|
|
13
|
-
|
|
14
|
-
// 平台的访问密钥放在哪个环境变量里:平台 id 大写、连字符换成下划线
|
|
15
|
-
export function keyVariable(id) {
|
|
16
|
-
return `KIOKU_API_KEY_${id.toUpperCase().replaceAll('-', '_')}`
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export const INSTALL_USAGE = `用法:kioku install --platforms <平台,…> [--server-url <地址>] [-y] [--dry-run]
|
|
20
|
-
|
|
21
|
-
把几个平台一起接到 Kioku(给脚本用;在终端里直接运行 kioku 是交互式的向导)。
|
|
22
|
-
每个平台的访问密钥从它的环境变量读,没给的平台跳过:
|
|
23
|
-
|
|
24
|
-
${Object.keys(PLATFORMS)
|
|
25
|
-
.map((id) => ` ${keyVariable(id).padEnd(28)}${PLATFORMS[id].name}`)
|
|
26
|
-
.join('\n')}
|
|
27
|
-
|
|
28
|
-
--platforms <平台,…> 逗号分隔:${platformNames().replaceAll('、', ',')}
|
|
29
|
-
--server-url <地址> Kioku 的服务地址(也可以用 KIOKU_SERVER_URL)
|
|
30
|
-
-y, --yes 不问,直接写(在终端里运行时默认先列计划再确认)
|
|
31
|
-
--dry-run 只列计划,不写
|
|
32
|
-
|
|
33
|
-
各平台照 kioku setup 写下地址与访问密钥、停用自带的长期记忆。
|
|
34
|
-
Claude Code 的访问密钥在它的 /plugin 里填:这里只核对访问密钥、停用自动记忆。
|
|
35
|
-
插件不替你装:最后列出要运行的命令,装好之后运行 kioku doctor <平台> 检查。
|
|
36
|
-
`
|
|
37
|
-
|
|
38
|
-
export function parseInstallArgs(argv) {
|
|
39
|
-
const options = { platforms: [], serverUrl: undefined, yes: false, dryRun: false }
|
|
40
|
-
let given = false
|
|
41
|
-
for (let i = 0; i < argv.length; i++) {
|
|
42
|
-
const arg = argv[i]
|
|
43
|
-
if (arg === '-y' || arg === '--yes') options.yes = true
|
|
44
|
-
else if (arg === '--dry-run') options.dryRun = true
|
|
45
|
-
else if (arg === '--server-url' || arg === '--platforms') {
|
|
46
|
-
const value = argv[++i]
|
|
47
|
-
if (!value) throw new UsageError(`${arg} 后面要写值`)
|
|
48
|
-
if (arg === '--server-url') options.serverUrl = value
|
|
49
|
-
else {
|
|
50
|
-
given = true
|
|
51
|
-
for (const name of value.split(',').map((item) => item.trim())) {
|
|
52
|
-
if (!name) continue
|
|
53
|
-
if (!Object.hasOwn(PLATFORMS, name)) {
|
|
54
|
-
throw new UsageError(`不认识的平台 ${name},可以用的:${platformNames()}`)
|
|
55
|
-
}
|
|
56
|
-
if (!options.platforms.includes(name)) options.platforms.push(name)
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
} else throw new UsageError(`不认识的参数 ${arg}`)
|
|
60
|
-
}
|
|
61
|
-
if (!given || !options.platforms.length) throw new UsageError('要写平台:--platforms codex,pi')
|
|
62
|
-
return options
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// 每个平台要做什么:{ id, key } 或 { id, skip: 原因 }
|
|
66
|
-
export function installPlan(platforms, env) {
|
|
67
|
-
return platforms.map((id) => {
|
|
68
|
-
const name = keyVariable(id)
|
|
69
|
-
const key = (env[name] ?? '').trim()
|
|
70
|
-
if (!key) return { id, skip: `没有 ${name}` }
|
|
71
|
-
const problem = keyFormatError(key)
|
|
72
|
-
if (problem) return { id, skip: `${name}:${problem}` }
|
|
73
|
-
return { id, key }
|
|
74
|
-
})
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// Claude Code:核对访问密钥是不是给它的,停用自动记忆;地址与访问密钥要用户在 /plugin 里填
|
|
78
|
-
async function installClaudeCode(url, key, { env, out, cwd }) {
|
|
79
|
-
const who = await whoami({ url, key })
|
|
80
|
-
out.write(`✓ 访问密钥可用:应用 ${plain(who.app.name)},运行时 ${plain(who.app.runtimeFamily)}\n`)
|
|
81
|
-
if (who.app.runtimeFamily !== PLATFORMS['claude-code'].runtimeFamily) {
|
|
82
|
-
out.write('! 这个应用的运行时不是 claudecode:在管理界面为 Claude Code 另建一个应用\n')
|
|
83
|
-
}
|
|
84
|
-
const code = await setup(['claude-code'], { env, out, cwd, login: null })
|
|
85
|
-
if (code !== 0) throw new KiokuFailure('停用自动记忆没有成功')
|
|
86
|
-
out.write('地址与访问密钥要在 Claude Code 的 /plugin 里填(启用插件时会问)\n')
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export async function install(
|
|
90
|
-
argv,
|
|
91
|
-
{ env = process.env, out = process.stdout, cwd = process.cwd(), stdin = process.stdin } = {},
|
|
92
|
-
) {
|
|
93
|
-
const options = parseInstallArgs(argv)
|
|
94
|
-
const url = resolveServerUrl(options, env, null)
|
|
95
|
-
const plan = installPlan(options.platforms, env)
|
|
96
|
-
out.write(`Kioku:${shownUrl(url)}\n`)
|
|
97
|
-
for (const item of plan) {
|
|
98
|
-
const name = PLATFORMS[item.id].name
|
|
99
|
-
if (item.skip) out.write(` 跳过 ${name}(${item.skip})\n`)
|
|
100
|
-
else if (item.id === 'claude-code') out.write(` 核对 ${name}:核对访问密钥,停用自动记忆\n`)
|
|
101
|
-
else out.write(` 接入 ${name}:写下地址与访问密钥,停用自带的长期记忆\n`)
|
|
102
|
-
}
|
|
103
|
-
const todo = plan.filter((item) => item.key)
|
|
104
|
-
if (options.dryRun) {
|
|
105
|
-
out.write('没有写入(--dry-run)\n')
|
|
106
|
-
return 0
|
|
107
|
-
}
|
|
108
|
-
if (!todo.length) {
|
|
109
|
-
out.write('没有要接入的平台:按上面的变量名给出访问密钥\n')
|
|
110
|
-
return 1
|
|
111
|
-
}
|
|
112
|
-
if (!options.yes && stdin.isTTY) {
|
|
113
|
-
if (!(await confirm(`接入这 ${todo.length} 个平台?[y/N] `, { stdin, out }))) {
|
|
114
|
-
out.write('没有写入\n')
|
|
115
|
-
return 0
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const results = plan
|
|
120
|
-
.filter((item) => item.skip)
|
|
121
|
-
.map((item) => ({ ...item, status: 'skipped', reason: item.skip }))
|
|
122
|
-
for (const { id, key } of todo) {
|
|
123
|
-
out.write(`\n${PLATFORMS[id].name}\n`)
|
|
124
|
-
try {
|
|
125
|
-
if (id === 'claude-code') {
|
|
126
|
-
await installClaudeCode(url, key, { env, out, cwd })
|
|
127
|
-
results.push({ id, status: 'manual', reason: '地址与访问密钥在 /plugin 里填' })
|
|
128
|
-
} else {
|
|
129
|
-
await configure(id, { url, key }, { env, out, cwd })
|
|
130
|
-
results.push({ id, status: 'connected' })
|
|
131
|
-
}
|
|
132
|
-
} catch (err) {
|
|
133
|
-
results.push({ id, status: 'failed', reason: failureText(err) })
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
const ordered = options.platforms.map((id) => results.find((item) => item.id === id))
|
|
137
|
-
out.write(`\n${resultsText(ordered)}\n`)
|
|
138
|
-
const steps = await pluginSteps(
|
|
139
|
-
ordered
|
|
140
|
-
.filter((item) => item.status === 'connected' || item.status === 'manual')
|
|
141
|
-
.map((item) => item.id),
|
|
142
|
-
{ env, cwd, url },
|
|
143
|
-
)
|
|
144
|
-
if (steps) out.write(`\n${steps}\n`)
|
|
145
|
-
return ordered.some((item) => item.status === 'failed') ? 1 : 0
|
|
146
|
-
}
|