@kioku-memory/openclaw 0.1.0 → 0.2.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/lib/api.mjs +4 -1
- package/lib/codex.mjs +12 -1
- package/lib/command.mjs +19 -2
- package/lib/connect.mjs +152 -0
- package/lib/dsh.mjs +8 -1
- package/lib/hermes.mjs +16 -1
- package/lib/import.mjs +81 -42
- package/lib/install.mjs +146 -0
- package/lib/kioku.mjs +30 -6
- package/lib/local.mjs +294 -0
- package/lib/login.mjs +114 -24
- package/lib/openclaw.mjs +9 -1
- package/lib/panel.mjs +295 -0
- package/lib/pi.mjs +10 -1
- package/lib/platforms.mjs +93 -10
- package/lib/prompts.mjs +38 -0
- package/lib/wizard.mjs +447 -0
- package/package.json +1 -1
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
|
|
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/codex.mjs
CHANGED
|
@@ -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
|
|
package/lib/connect.mjs
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// 把几个平台接到 Kioku:拿到访问密钥之后交给各平台的 kioku setup 写下(核对、写文件都照它的规矩)。
|
|
2
|
+
// 向导走浏览器授权,几个平台一起申请、只确认一次;服务端不认这种写法时逐个平台申请。
|
|
3
|
+
// kioku install 的访问密钥从环境变量来,直接写下。插件不替用户装,只列出要运行的命令
|
|
4
|
+
|
|
5
|
+
import { KiokuFailure } from './http.mjs'
|
|
6
|
+
import { deviceLogin, deviceLoginMany, LegacyServer, MAX_PLATFORMS } from './login.mjs'
|
|
7
|
+
import { PLATFORMS } from './platforms.mjs'
|
|
8
|
+
import { plain } from './text.mjs'
|
|
9
|
+
|
|
10
|
+
// 访问密钥已经签发、却没有写下来:说清楚,别让它悄悄留着。kioku login 也用它
|
|
11
|
+
export function orphanHint(app) {
|
|
12
|
+
const name = app?.name ? `(应用 ${plain(app.name)})` : ''
|
|
13
|
+
return `访问密钥已经签发、但没有写下:到管理界面的应用页吊销它${name},解决上面的问题后重新运行`
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// 用给定的地址与访问密钥配好一个平台。setup 的输出写到 out;hints 为假时不打印「接下来装上」,
|
|
17
|
+
// 由调用方统一列出(向导与 kioku install),kioku login 只配一个平台,照旧打印
|
|
18
|
+
export function configure(id, { url, key }, { env, out, cwd }, { hints = false } = {}) {
|
|
19
|
+
return PLATFORMS[id].setup(
|
|
20
|
+
{ serverUrl: url, dryRun: false, undo: false, check: true },
|
|
21
|
+
{ env, out, cwd, readKey: async () => key, plain, login: null, hints },
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// 签发好的访问密钥写下;写不下时提醒去吊销
|
|
26
|
+
async function settle(id, key, app, io) {
|
|
27
|
+
io.out.write(`\n${PLATFORMS[id].name}\n`)
|
|
28
|
+
try {
|
|
29
|
+
await configure(id, { url: io.url, key }, io)
|
|
30
|
+
return { id, status: 'connected', app }
|
|
31
|
+
} catch (err) {
|
|
32
|
+
// 别的错误照旧抛出(由入口报内部错误),不当成一个平台的失败吞掉
|
|
33
|
+
if (!(err instanceof KiokuFailure)) throw err
|
|
34
|
+
return { id, status: 'failed', reason: `${err.message}。${orphanHint(app)}` }
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 用浏览器授权把这几个平台接到 url。返回每个平台的结果(按 ids 的顺序):
|
|
39
|
+
// { id, status: 'connected' | 'skipped' | 'denied' | 'failed', reason?, app? }
|
|
40
|
+
export async function connectPlatforms(
|
|
41
|
+
{ url, ids },
|
|
42
|
+
{ env, out, cwd, open, browser = true, clientName, sleep, now },
|
|
43
|
+
) {
|
|
44
|
+
const io = { env, out, cwd, url }
|
|
45
|
+
const login = { out, open, browser, sleep, now, ...(clientName ? { clientName } : {}) }
|
|
46
|
+
const results = new Map()
|
|
47
|
+
// 申请之前先看各平台的配置写不写得进去:访问密钥只在签发时给一次
|
|
48
|
+
const ready = []
|
|
49
|
+
for (const id of ids) {
|
|
50
|
+
try {
|
|
51
|
+
await PLATFORMS[id].preflight?.({ env, cwd })
|
|
52
|
+
ready.push(id)
|
|
53
|
+
} catch (err) {
|
|
54
|
+
if (!(err instanceof KiokuFailure)) throw err
|
|
55
|
+
results.set(id, { id, status: 'failed', reason: err.message })
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// 一次最多 MAX_PLATFORMS 个:多了分批,每批确认一次,前一批签发的先写下再申请下一批
|
|
59
|
+
const batches = []
|
|
60
|
+
for (let i = 0; i < ready.length; i += MAX_PLATFORMS) {
|
|
61
|
+
batches.push(ready.slice(i, i + MAX_PLATFORMS))
|
|
62
|
+
}
|
|
63
|
+
if (batches.length > 1) {
|
|
64
|
+
out.write(`一次最多申请 ${MAX_PLATFORMS} 个平台:分 ${batches.length} 批,每批确认一次\n`)
|
|
65
|
+
}
|
|
66
|
+
let legacy = false
|
|
67
|
+
for (const batch of batches) {
|
|
68
|
+
out.write(
|
|
69
|
+
`这次申请 ${batch.length} 个平台:${batch.map((id) => PLATFORMS[id].name).join('、')}\n`,
|
|
70
|
+
)
|
|
71
|
+
let answers
|
|
72
|
+
try {
|
|
73
|
+
answers = await deviceLoginMany(
|
|
74
|
+
{ url, runtimeFamilies: batch.map((id) => PLATFORMS[id].runtimeFamily) },
|
|
75
|
+
login,
|
|
76
|
+
)
|
|
77
|
+
} catch (err) {
|
|
78
|
+
if (err instanceof LegacyServer) {
|
|
79
|
+
legacy = true
|
|
80
|
+
out.write(`${err.message}\n`)
|
|
81
|
+
break
|
|
82
|
+
}
|
|
83
|
+
if (!(err instanceof KiokuFailure)) throw err
|
|
84
|
+
for (const id of batch) results.set(id, { id, status: 'failed', reason: err.message })
|
|
85
|
+
continue
|
|
86
|
+
}
|
|
87
|
+
for (const id of batch) {
|
|
88
|
+
const answer = answers.find((item) => item.runtimeFamily === PLATFORMS[id].runtimeFamily)
|
|
89
|
+
if (answer?.status === 'issued') results.set(id, await settle(id, answer.key, answer.app, io))
|
|
90
|
+
else if (answer?.status === 'denied') {
|
|
91
|
+
results.set(id, { id, status: 'denied', reason: answer.reason || '没有签发' })
|
|
92
|
+
} else results.set(id, { id, status: 'skipped', reason: '确认页上没有勾选' })
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (legacy) {
|
|
96
|
+
// 旧的服务端:还没有结果的平台逐个申请,每个确认一次;一个不成不影响下一个
|
|
97
|
+
for (const id of ready.filter((item) => !results.has(item))) {
|
|
98
|
+
out.write(`\n${PLATFORMS[id].name}:浏览器授权\n`)
|
|
99
|
+
let key
|
|
100
|
+
try {
|
|
101
|
+
key = await deviceLogin({ url, runtimeFamily: PLATFORMS[id].runtimeFamily }, login)
|
|
102
|
+
} catch (err) {
|
|
103
|
+
if (!(err instanceof KiokuFailure)) throw err
|
|
104
|
+
results.set(id, { id, status: 'failed', reason: err.message })
|
|
105
|
+
continue
|
|
106
|
+
}
|
|
107
|
+
results.set(id, await settle(id, key, null, io))
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return ids.map((id) => results.get(id))
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const STATUS_NAMES = {
|
|
114
|
+
connected: '已接入',
|
|
115
|
+
manual: '待完成',
|
|
116
|
+
skipped: '跳过',
|
|
117
|
+
denied: '被拒绝',
|
|
118
|
+
failed: '失败',
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// 结果的文字:按状态分组,一组一行,原因跟在平台名后面
|
|
122
|
+
export function resultsText(results) {
|
|
123
|
+
const lines = []
|
|
124
|
+
for (const status of Object.keys(STATUS_NAMES)) {
|
|
125
|
+
const group = results.filter((item) => item.status === status)
|
|
126
|
+
if (!group.length) continue
|
|
127
|
+
const names = group.map((item) => {
|
|
128
|
+
const detail = item.reason ?? (item.app?.name ? `应用 ${plain(item.app.name)}` : '')
|
|
129
|
+
return detail ? `${PLATFORMS[item.id].name}(${detail})` : PLATFORMS[item.id].name
|
|
130
|
+
})
|
|
131
|
+
lines.push(`${STATUS_NAMES[status]}:${names.join('、')}`)
|
|
132
|
+
}
|
|
133
|
+
return lines.join('\n')
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// 这几个平台里还要自己装插件的(查得到装好了的不列),每个平台一段:在哪里运行、可以直接复制的命令、之后要做的事。
|
|
137
|
+
// 访问密钥在插件配置里填的平台(Claude Code),给了 url 时写明要填的服务地址
|
|
138
|
+
export async function pluginSteps(ids, { env, cwd, url }) {
|
|
139
|
+
const blocks = []
|
|
140
|
+
for (const id of ids) {
|
|
141
|
+
const platform = PLATFORMS[id]
|
|
142
|
+
const status = await platform.status({ env, cwd }).catch(() => ({ plugin: null }))
|
|
143
|
+
if (status.plugin === true) continue
|
|
144
|
+
const { where = '在终端里', commands, after } = platform.install
|
|
145
|
+
const lines = [`${platform.name}(${where}运行)`, ...commands.map((line) => ` ${line}`)]
|
|
146
|
+
if (!platform.setup && url) lines.push(` 服务地址填:${url}`)
|
|
147
|
+
lines.push(` 然后:${after}`)
|
|
148
|
+
blocks.push(lines.join('\n'))
|
|
149
|
+
}
|
|
150
|
+
if (!blocks.length) return ''
|
|
151
|
+
return `接下来自己装插件,装好之后再运行 kioku 看状态:\n\n${blocks.join('\n\n')}`
|
|
152
|
+
}
|
package/lib/dsh.mjs
CHANGED
|
@@ -59,7 +59,9 @@ export async function setupDsh(options, io) {
|
|
|
59
59
|
const profiles = await installedProfiles(env)
|
|
60
60
|
out.write('DeepSeek Harness 已配好,重启 dsh 后生效(读写记忆的工具在启动时连上)。\n')
|
|
61
61
|
if (profiles.length) out.write(`装了插件的 profile:${profiles.map(plain).join('、')}\n`)
|
|
62
|
-
else
|
|
62
|
+
else if (io.hints !== false) {
|
|
63
|
+
out.write(`接下来给要用的 profile 装上插件(web、tui 这些各装一次):${installHint}\n`)
|
|
64
|
+
}
|
|
63
65
|
return 0
|
|
64
66
|
}
|
|
65
67
|
|
|
@@ -73,3 +75,8 @@ export async function inspectDsh({ env }, { ok, warn, note }) {
|
|
|
73
75
|
note('插件按 profile 装:没装的 profile 里没有长期记忆')
|
|
74
76
|
ok('DeepSeek Harness 没有自带的长期记忆要停用')
|
|
75
77
|
}
|
|
78
|
+
|
|
79
|
+
// 给向导的面板:有没有哪个 profile 装了插件,dsh 没有自带的长期记忆
|
|
80
|
+
export async function statusDsh({ env }) {
|
|
81
|
+
return { plugin: (await installedProfiles(env)).length > 0, memory: 'none' }
|
|
82
|
+
}
|
package/lib/hermes.mjs
CHANGED
|
@@ -219,7 +219,7 @@ export async function setupHermes(options, io) {
|
|
|
219
219
|
out.write('地址与访问密钥已经写下。\n')
|
|
220
220
|
manual(out, SETTINGS, state)
|
|
221
221
|
}
|
|
222
|
-
if (!(await installed(env))) {
|
|
222
|
+
if (!(await installed(env)) && io.hints !== false) {
|
|
223
223
|
out.write(`接下来装上 provider:hermes plugins install ${SOURCE} --enable\n`)
|
|
224
224
|
}
|
|
225
225
|
return 0
|
|
@@ -259,3 +259,18 @@ export async function inspectHermes({ env }, { ok, warn, note }) {
|
|
|
259
259
|
} else ok('Hermes 内置的记忆文件已停用')
|
|
260
260
|
note('只在本机的会话里生效;消息网关来的会话里不注入、不上报、不给工具')
|
|
261
261
|
}
|
|
262
|
+
|
|
263
|
+
// 给向导的面板:provider 装没装;自带的记忆停没停(选的是 kioku、内置的两份都关了才算停了;
|
|
264
|
+
// 没有 hermes 命令、命令失败时为 null)
|
|
265
|
+
export async function statusHermes({ env }) {
|
|
266
|
+
const plugin = await installed(env)
|
|
267
|
+
const provider = await configValue(env, 'memory.provider')
|
|
268
|
+
if (provider === null || provider === undefined) return { plugin, memory: null }
|
|
269
|
+
const flags = [
|
|
270
|
+
await configValue(env, 'memory.memory_enabled'),
|
|
271
|
+
await configValue(env, 'memory.user_profile_enabled'),
|
|
272
|
+
]
|
|
273
|
+
if (flags.some((value) => value === null || value === undefined)) return { plugin, memory: null }
|
|
274
|
+
const off = provider === 'kioku' && flags.every((value) => !/^true$/i.test(value))
|
|
275
|
+
return { plugin, memory: off ? 'off' : 'on' }
|
|
276
|
+
}
|
package/lib/import.mjs
CHANGED
|
@@ -135,7 +135,7 @@ async function write(config, item) {
|
|
|
135
135
|
const KINDS = { new: '新建', same: '已有,内容一样', differs: '已有,内容不一样' }
|
|
136
136
|
|
|
137
137
|
// 问一句是或否;只认 y 开头的回答
|
|
138
|
-
async function confirm(question, { stdin, out }) {
|
|
138
|
+
export async function confirm(question, { stdin, out }) {
|
|
139
139
|
out.write(question)
|
|
140
140
|
for await (const chunk of stdin) {
|
|
141
141
|
return /^\s*y/i.test(chunk.toString('utf8'))
|
|
@@ -143,38 +143,53 @@ async function confirm(question, { stdin, out }) {
|
|
|
143
143
|
return false
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
{
|
|
149
|
-
)
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
)
|
|
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
160
|
}
|
|
161
|
-
}
|
|
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]
|
|
162
181
|
const who = await whoami(config)
|
|
163
182
|
out.write(
|
|
164
183
|
`Kioku:${shownUrl(config.url)},应用 ${plain(who.app.name)}(${plain(who.app.slug)})\n`,
|
|
165
184
|
)
|
|
166
185
|
|
|
167
|
-
const { root, found, entries } = await source.read({
|
|
168
|
-
env,
|
|
169
|
-
path: options.path,
|
|
170
|
-
app: who.app.slug,
|
|
171
|
-
})
|
|
186
|
+
const { root, found, entries } = await source.read({ env, path, app: who.app.slug })
|
|
172
187
|
if (!found)
|
|
173
188
|
throw new KiokuFailure(`没有找到${source.name}:${plain(root)} 不是目录,用 --path 指明位置`)
|
|
174
189
|
out.write(`来源:${source.name}(${plain(root)})\n`)
|
|
175
190
|
if (!entries.length) {
|
|
176
191
|
out.write('没有可以迁的记忆\n')
|
|
177
|
-
return
|
|
192
|
+
return { compared: [], skipped: [], todo: [], empty: true }
|
|
178
193
|
}
|
|
179
194
|
|
|
180
195
|
// 两条落到同一个坐标的(名字规整之后撞了)只留第一条
|
|
@@ -190,8 +205,8 @@ export async function importMemories(
|
|
|
190
205
|
usable.push(item)
|
|
191
206
|
}
|
|
192
207
|
}
|
|
193
|
-
|
|
194
|
-
|
|
208
|
+
// 一条一个请求:有界并发,几百条也不至于等太久
|
|
209
|
+
const compared = await mapLimit(usable, COMPARE_CONCURRENCY, (item) => compare(config, item))
|
|
195
210
|
|
|
196
211
|
out.write('\n')
|
|
197
212
|
for (const item of compared) {
|
|
@@ -202,29 +217,17 @@ export async function importMemories(
|
|
|
202
217
|
|
|
203
218
|
const count = (state) => compared.filter((item) => item.state === state).length
|
|
204
219
|
const todo = compared.filter(
|
|
205
|
-
(item) => item.state === 'new' || (item.state === 'differs' &&
|
|
220
|
+
(item) => item.state === 'new' || (item.state === 'differs' && overwrite),
|
|
206
221
|
)
|
|
207
222
|
out.write(
|
|
208
223
|
`\n新建 ${count('new')} 条,已有且一样 ${count('same')} 条,已有但不一样 ${count('differs')} 条` +
|
|
209
|
-
`(${
|
|
224
|
+
`(${overwrite ? '覆盖' : '跳过,要覆盖加 --overwrite'}),不迁 ${skipped.length} 条\n`,
|
|
210
225
|
)
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
return 0
|
|
214
|
-
}
|
|
215
|
-
if (!todo.length) {
|
|
216
|
-
out.write('没有要写的\n')
|
|
217
|
-
return compared.some((item) => item.state === 'failed') ? 1 : 0
|
|
218
|
-
}
|
|
219
|
-
if (!options.yes) {
|
|
220
|
-
if (!stdin.isTTY)
|
|
221
|
-
throw new KiokuFailure('不在终端里运行时要带 --yes 才写;先用 --dry-run 看计划')
|
|
222
|
-
if (!(await confirm(`写入这 ${todo.length} 条?[y/N] `, { stdin, out }))) {
|
|
223
|
-
out.write('没有写入\n')
|
|
224
|
-
return 0
|
|
225
|
-
}
|
|
226
|
-
}
|
|
226
|
+
return { compared, skipped, todo, empty: false }
|
|
227
|
+
}
|
|
227
228
|
|
|
229
|
+
// 按计划写入,返回退出码:有没写成的、读取失败的为 1
|
|
230
|
+
export async function writeImport(config, { compared, todo }, { out }) {
|
|
228
231
|
let written = 0
|
|
229
232
|
const failed = []
|
|
230
233
|
// 连不上、超时这类不是某一条的问题:停下来,把已经写了多少、还剩多少说清楚,不让它跳出整个命令
|
|
@@ -260,3 +263,39 @@ export async function importMemories(
|
|
|
260
263
|
out.write('本地的文件没有动。\n')
|
|
261
264
|
return stopped || failed.length || compared.some((item) => item.state === 'failed') ? 1 : 0
|
|
262
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
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
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
|
+
}
|
package/lib/kioku.mjs
CHANGED
|
@@ -10,6 +10,7 @@ import { isatty } from 'node:tty'
|
|
|
10
10
|
import { killTree, launch } from './command.mjs'
|
|
11
11
|
import { loadConfig } from './config.mjs'
|
|
12
12
|
import { IMPORT_USAGE, importMemories } from './import.mjs'
|
|
13
|
+
import { install, INSTALL_USAGE } from './install.mjs'
|
|
13
14
|
import { login, LOGIN_USAGE } from './login.mjs'
|
|
14
15
|
import { doctor, DOCTOR_USAGE } from './doctor.mjs'
|
|
15
16
|
import {
|
|
@@ -20,6 +21,7 @@ import {
|
|
|
20
21
|
KiokuFailure,
|
|
21
22
|
redirectError,
|
|
22
23
|
} from './http.mjs'
|
|
24
|
+
import { loadPrompts } from './prompts.mjs'
|
|
23
25
|
import { setup, SETUP_USAGE } from './setup.mjs'
|
|
24
26
|
import { UsageError } from './usage.mjs'
|
|
25
27
|
import { createRedactor, MIN_REDACT_CHARS, redactable } from './redact.mjs'
|
|
@@ -378,15 +380,31 @@ async function run(argv) {
|
|
|
378
380
|
// 入口:返回退出码
|
|
379
381
|
const OVERVIEW = `用法:kioku <子命令> [参数…]
|
|
380
382
|
|
|
381
|
-
run
|
|
382
|
-
login
|
|
383
|
-
setup
|
|
384
|
-
|
|
385
|
-
|
|
383
|
+
run 向 Kioku 取凭据的值,放进环境变量交给命令
|
|
384
|
+
login 用浏览器授权签发访问密钥并配好这个平台
|
|
385
|
+
setup 停用平台自带的长期记忆,由 Kioku 接管
|
|
386
|
+
install 几个平台一起接入,访问密钥从环境变量读(给脚本用)
|
|
387
|
+
doctor 检查地址、访问密钥与平台的配置
|
|
388
|
+
import 把平台自带的本地长期记忆迁进 Kioku
|
|
386
389
|
|
|
390
|
+
在终端里不带参数运行 kioku 是交互式的向导:看各平台接到哪一步,按缺的一步步接上。
|
|
387
391
|
各子命令的说明:kioku <子命令> --help
|
|
388
392
|
`
|
|
389
393
|
|
|
394
|
+
const NO_WIZARD =
|
|
395
|
+
'交互式的向导要 npm 上的 kioku(Node 20.12 以上):npm install -g @kioku-memory/cli\n'
|
|
396
|
+
|
|
397
|
+
// 交互式的向导。插件目录里的 kioku 带不了交互库,装不到时打印用法并说明
|
|
398
|
+
async function startWizard() {
|
|
399
|
+
const ui = await loadPrompts()
|
|
400
|
+
if (!ui) {
|
|
401
|
+
process.stderr.write(`${OVERVIEW}\n${NO_WIZARD}`)
|
|
402
|
+
return 2
|
|
403
|
+
}
|
|
404
|
+
const { barOut, wizard } = await import('./wizard.mjs')
|
|
405
|
+
return wizard({ ui, env: process.env, cwd: process.cwd(), out: barOut(process.stdout) })
|
|
406
|
+
}
|
|
407
|
+
|
|
390
408
|
// 说明类的子命令:参数写错时打印原因与说明,以 2 退出;别的失败原因写到标准错误
|
|
391
409
|
async function guarded(name, usage, fn, argv) {
|
|
392
410
|
if (argv.includes('-h') || argv.includes('--help')) {
|
|
@@ -408,8 +426,13 @@ async function guarded(name, usage, fn, argv) {
|
|
|
408
426
|
}
|
|
409
427
|
}
|
|
410
428
|
|
|
411
|
-
|
|
429
|
+
// interactive:标准输入与输出都是终端时才进向导;管道、agent 的命令里照旧打印用法
|
|
430
|
+
export async function main(
|
|
431
|
+
argv,
|
|
432
|
+
{ interactive = Boolean(process.stdin.isTTY && process.stdout.isTTY), wizard = startWizard } = {},
|
|
433
|
+
) {
|
|
412
434
|
const [sub, ...rest] = argv
|
|
435
|
+
if (sub === undefined && interactive) return wizard()
|
|
413
436
|
if (sub === '-h' || sub === '--help') {
|
|
414
437
|
process.stdout.write(OVERVIEW)
|
|
415
438
|
return 0
|
|
@@ -417,6 +440,7 @@ export async function main(argv) {
|
|
|
417
440
|
if (sub === 'run') return run(rest)
|
|
418
441
|
if (sub === 'login') return guarded('login', LOGIN_USAGE, login, rest)
|
|
419
442
|
if (sub === 'setup') return guarded('setup', SETUP_USAGE, setup, rest)
|
|
443
|
+
if (sub === 'install') return guarded('install', INSTALL_USAGE, install, rest)
|
|
420
444
|
if (sub === 'doctor') return guarded('doctor', DOCTOR_USAGE, doctor, rest)
|
|
421
445
|
if (sub === 'import') return guarded('import', IMPORT_USAGE, importMemories, rest)
|
|
422
446
|
process.stderr.write(sub === undefined ? OVERVIEW : `kioku:不认识的子命令 ${sub}\n\n${OVERVIEW}`)
|