@kioku-memory/openclaw 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/kioku.js +19 -2
- package/lib/kioku.mjs +35 -430
- package/lib/lifecycle.mjs +13 -3
- package/lib/run.mjs +372 -0
- package/package.json +1 -1
- 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/openclaw.mjs
DELETED
|
@@ -1,256 +0,0 @@
|
|
|
1
|
-
// OpenClaw 这个平台的 kioku setup 与 kioku doctor。
|
|
2
|
-
// 插件读本机文件里的服务地址与访问密钥(不放进 OpenClaw 的配置:那份配置会被 openclaw config get 打印出来);
|
|
3
|
-
// OpenClaw 自己的两项(记忆槽位、允许插件读对话)交给它的 openclaw config set 去改,不自己动它的配置文件
|
|
4
|
-
|
|
5
|
-
import { capture } from './command.mjs'
|
|
6
|
-
import { KiokuFailure } from './http.mjs'
|
|
7
|
-
import { listTools, removeToolCache, writeToolCache } from './mcp.mjs'
|
|
8
|
-
import { profileFile, readProfile, removeProfile, writeProfile } from './profile.mjs'
|
|
9
|
-
import { resolveTarget } from './target.mjs'
|
|
10
|
-
|
|
11
|
-
const PLUGIN = 'kioku'
|
|
12
|
-
const SOURCE = 'npm:@kioku-memory/openclaw'
|
|
13
|
-
// 允许插件读对话:第三方插件默认拿不到每轮的输入与回复,召回与会话摘要都要它
|
|
14
|
-
const ACCESS = `plugins.entries.${PLUGIN}.hooks.allowConversationAccess`
|
|
15
|
-
const SLOT = 'plugins.slots.memory'
|
|
16
|
-
// OpenClaw 自带的记忆插件,还原时选回它
|
|
17
|
-
const BUILTIN = 'memory-core'
|
|
18
|
-
|
|
19
|
-
// 命令以失败退出:output 是它打印的内容,只用来认是哪种失败,不打印(里面可能有别的配置)
|
|
20
|
-
class CommandFailed extends KiokuFailure {
|
|
21
|
-
constructor(args, output) {
|
|
22
|
-
super(`openclaw ${args.slice(0, 3).join(' ')} 没有成功`)
|
|
23
|
-
this.output = output
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// 调 openclaw 命令。找不到这个命令时返回 null;失败(含超时)时抛 CommandFailed
|
|
28
|
-
async function openclaw(env, args) {
|
|
29
|
-
const result = await capture(env.KIOKU_OPENCLAW_BIN || 'openclaw', args, env, {
|
|
30
|
-
timeout: 60_000,
|
|
31
|
-
})
|
|
32
|
-
if (!result.found) return null
|
|
33
|
-
if (!result.ok) throw new CommandFailed(args, result.output)
|
|
34
|
-
return result.stdout
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// 读 OpenClaw 的一项配置:null 是没有 openclaw 命令,undefined 是没读出来,否则是值(没设时是空字符串)
|
|
38
|
-
async function configValue(env, key) {
|
|
39
|
-
let out
|
|
40
|
-
try {
|
|
41
|
-
out = await openclaw(env, ['config', 'get', key, '--json'])
|
|
42
|
-
} catch (err) {
|
|
43
|
-
// 没设过的项它以失败退出,并说没有这个路径;别的失败(超时这类)不当成没设
|
|
44
|
-
return /not found/i.test(err?.output ?? '') ? '' : undefined
|
|
45
|
-
}
|
|
46
|
-
if (out === null) return null
|
|
47
|
-
const last = out.trim().split(/\r?\n/).at(-1)?.trim() ?? ''
|
|
48
|
-
try {
|
|
49
|
-
const value = JSON.parse(last)
|
|
50
|
-
return typeof value === 'string' || typeof value === 'boolean' ? String(value) : undefined
|
|
51
|
-
} catch {
|
|
52
|
-
return undefined
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// 输出里的那个 JSON 对象:前面可能有几行提示,其中也可能带花括号,所以从每一个行首的花括号试
|
|
57
|
-
function jsonIn(out) {
|
|
58
|
-
for (const match of out.matchAll(/^\{/gm)) {
|
|
59
|
-
try {
|
|
60
|
-
return JSON.parse(out.slice(match.index))
|
|
61
|
-
} catch {
|
|
62
|
-
// 不是从这里开始的
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return undefined
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// 插件在 OpenClaw 里的状态:null 是没有 openclaw 命令,false 是没装,undefined 是没查出来
|
|
69
|
-
// (命令失败、输出读不懂),否则是 { enabled, loaded, slot }。没查出来不当成没装:照着没装去做会留下半截的配置
|
|
70
|
-
async function pluginState(env) {
|
|
71
|
-
let out
|
|
72
|
-
try {
|
|
73
|
-
out = await openclaw(env, ['plugins', 'inspect', PLUGIN, '--json'])
|
|
74
|
-
} catch (err) {
|
|
75
|
-
return /plugin not found/i.test(err?.output ?? '') ? false : undefined
|
|
76
|
-
}
|
|
77
|
-
if (out === null) return null
|
|
78
|
-
const plugin = jsonIn(out)?.plugin
|
|
79
|
-
if (plugin?.id !== PLUGIN) return undefined
|
|
80
|
-
return {
|
|
81
|
-
enabled: plugin.enabled === true,
|
|
82
|
-
loaded: plugin.status === 'loaded',
|
|
83
|
-
slot: plugin.memorySlotSelected === true,
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// 让 OpenClaw 去掉一项配置;去不掉不算失败
|
|
88
|
-
async function removeSetting(env, key) {
|
|
89
|
-
try {
|
|
90
|
-
return (await openclaw(env, ['config', 'unset', key])) !== null
|
|
91
|
-
} catch {
|
|
92
|
-
return false
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// 让 OpenClaw 改一项配置,改完读回来核对。返回 'done'、'no-openclaw' 或 'failed'。不抛错:
|
|
97
|
-
// 调用方这时多半已经把访问密钥写下了,不能让这里的失败看起来像什么都没写
|
|
98
|
-
async function applySetting(env, key, value) {
|
|
99
|
-
try {
|
|
100
|
-
if ((await openclaw(env, ['config', 'set', key, value])) === null) return 'no-openclaw'
|
|
101
|
-
return (await configValue(env, key)) === value ? 'done' : 'failed'
|
|
102
|
-
} catch {
|
|
103
|
-
return 'failed'
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function manual(out, settings, state) {
|
|
108
|
-
out.write(
|
|
109
|
-
state === 'no-openclaw'
|
|
110
|
-
? '没有找到 openclaw 命令,这几项要自己运行:\n'
|
|
111
|
-
: 'OpenClaw 的配置没有改成,这几项要自己运行:\n',
|
|
112
|
-
)
|
|
113
|
-
for (const [key, value] of settings) out.write(` openclaw config set ${key} ${value}\n`)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
async function undo({ dryRun }, { env, out }) {
|
|
117
|
-
const profile = profileFile('openclaw', env)
|
|
118
|
-
const state = await pluginState(env)
|
|
119
|
-
// 只在记忆槽位现在选的是 kioku 时改回去:之后换成了别的插件的话,不去动它
|
|
120
|
-
const ours = Boolean(state && state.slot)
|
|
121
|
-
out.write(`${profile}:删掉\n`)
|
|
122
|
-
if (state === null) out.write('没有找到 openclaw 命令,OpenClaw 的配置没有动\n')
|
|
123
|
-
else {
|
|
124
|
-
out.write(`OpenClaw 的配置\n ${ACCESS}:去掉\n`)
|
|
125
|
-
if (ours) out.write(` ${SLOT} → ${BUILTIN}\n`)
|
|
126
|
-
else if (state === undefined) out.write(` ${SLOT}:没有查出来现在选的是什么\n`)
|
|
127
|
-
else out.write(` ${SLOT}:现在选的不是 kioku,不动\n`)
|
|
128
|
-
}
|
|
129
|
-
if (dryRun) {
|
|
130
|
-
out.write('没有写入(--dry-run)\n')
|
|
131
|
-
return 0
|
|
132
|
-
}
|
|
133
|
-
if (await removeProfile('openclaw', env)) out.write(`已删掉 ${profile}\n`)
|
|
134
|
-
else out.write(`${profile} 本来就没有\n`)
|
|
135
|
-
await removeToolCache('openclaw', env)
|
|
136
|
-
if (state !== null) {
|
|
137
|
-
// 不再允许插件读对话:插件还装着的话,留着这一项它照样拿得到每轮的输入与回复
|
|
138
|
-
const access = await configValue(env, ACCESS)
|
|
139
|
-
if (access !== '' && !(await removeSetting(env, ACCESS))) {
|
|
140
|
-
out.write(
|
|
141
|
-
`OpenClaw 里允许插件读对话的那一项没有去掉,要自己运行:\n openclaw config unset ${ACCESS}\n`,
|
|
142
|
-
)
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
if (ours) {
|
|
146
|
-
const result = await applySetting(env, SLOT, BUILTIN)
|
|
147
|
-
if (result === 'done') out.write('OpenClaw 改回用自带的记忆,重启网关生效。\n')
|
|
148
|
-
else manual(out, [[SLOT, BUILTIN]], result)
|
|
149
|
-
} else if (state === undefined) {
|
|
150
|
-
out.write(
|
|
151
|
-
`OpenClaw 的记忆槽位没有查出来(openclaw plugins inspect 失败了):用 openclaw config get ${SLOT} 看,还是 kioku 的话运行\n openclaw config set ${SLOT} ${BUILTIN}\n`,
|
|
152
|
-
)
|
|
153
|
-
}
|
|
154
|
-
out.write(`插件要另外卸载:openclaw plugins uninstall ${PLUGIN}\n`)
|
|
155
|
-
return 0
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export async function setupOpenclaw(options, io) {
|
|
159
|
-
const { env, out } = io
|
|
160
|
-
if (options.undo) return undo(options, io)
|
|
161
|
-
const { url, key, saved } = await resolveTarget({ id: 'openclaw', name: 'OpenClaw' }, options, io)
|
|
162
|
-
const profile = profileFile('openclaw', env)
|
|
163
|
-
const same = Boolean(saved && saved.url === url && saved.key === key)
|
|
164
|
-
if (same) out.write(`${profile} 不用改\n`)
|
|
165
|
-
else out.write(`${profile}:写入服务地址与访问密钥(只给本人读写)\n`)
|
|
166
|
-
out.write(`OpenClaw 的配置\n ${ACCESS} → true\n ${SLOT} → ${PLUGIN}(装上插件之后)\n`)
|
|
167
|
-
if (options.dryRun) {
|
|
168
|
-
out.write('没有写入(--dry-run)\n')
|
|
169
|
-
return 0
|
|
170
|
-
}
|
|
171
|
-
if (!same) await writeProfile('openclaw', { url, key }, env)
|
|
172
|
-
// 工具清单先取一份留在本机:插件第一次登记工具时就有得用。取不到不算失败,插件自己会再取
|
|
173
|
-
if (options.check) {
|
|
174
|
-
try {
|
|
175
|
-
const tools = await listTools({ url, key })
|
|
176
|
-
// 一个工具都没有的回答不留:留下的话插件会当成新取到的清单用上一小时
|
|
177
|
-
if (tools.length) await writeToolCache('openclaw', { url, tools }, env)
|
|
178
|
-
} catch {
|
|
179
|
-
// 留给插件
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
// 访问密钥这时已经写下了:后面改 OpenClaw 的配置不成也不算失败,把要做的列出来
|
|
183
|
-
const todo = []
|
|
184
|
-
let result = await applySetting(env, ACCESS, 'true')
|
|
185
|
-
if (result !== 'done') todo.push([ACCESS, 'true'])
|
|
186
|
-
const state = result === 'no-openclaw' ? null : await pluginState(env)
|
|
187
|
-
// 槽位只在插件装上之后才选得了;装的时候 OpenClaw 自己会选上,这里只管装好之后被换走的
|
|
188
|
-
if (state && !state.slot) {
|
|
189
|
-
result = await applySetting(env, SLOT, PLUGIN)
|
|
190
|
-
if (result !== 'done') todo.push([SLOT, PLUGIN])
|
|
191
|
-
}
|
|
192
|
-
if (todo.length) {
|
|
193
|
-
out.write('地址与访问密钥已经写下。\n')
|
|
194
|
-
manual(out, todo, result)
|
|
195
|
-
} else if (state) {
|
|
196
|
-
out.write('OpenClaw 已配好,重启网关(openclaw gateway restart)后生效。\n')
|
|
197
|
-
} else {
|
|
198
|
-
out.write('地址与访问密钥已经写下。\n')
|
|
199
|
-
}
|
|
200
|
-
if (state === undefined) {
|
|
201
|
-
out.write(
|
|
202
|
-
'插件装了没有、记忆槽位选的是什么没有查出来(openclaw plugins inspect 失败了):用 kioku doctor openclaw 再看一次\n',
|
|
203
|
-
)
|
|
204
|
-
} else if (!state && io.hints !== false) {
|
|
205
|
-
out.write(`接下来装上插件:openclaw plugins install ${SOURCE}\n`)
|
|
206
|
-
if (todo.length) out.write('装好之后再运行一次 kioku setup openclaw。\n')
|
|
207
|
-
}
|
|
208
|
-
out.write(
|
|
209
|
-
'只在本机入口的对话里生效;外部渠道来的消息里没有长期记忆(自带的记忆工具也随槽位停用)。\n',
|
|
210
|
-
)
|
|
211
|
-
return 0
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
export async function inspectOpenclaw({ env }, { ok, warn, note }) {
|
|
215
|
-
const profile = await readProfile('openclaw', env).catch(() => null)
|
|
216
|
-
if (!profile) {
|
|
217
|
-
warn('没有 kioku setup openclaw 写下的地址与访问密钥,插件不会生效:运行 kioku setup openclaw')
|
|
218
|
-
}
|
|
219
|
-
const state = await pluginState(env)
|
|
220
|
-
if (state === null) {
|
|
221
|
-
note('没有找到 openclaw 命令,查不了它的配置:用 openclaw plugins inspect kioku 看')
|
|
222
|
-
return
|
|
223
|
-
}
|
|
224
|
-
if (state === undefined) {
|
|
225
|
-
warn(
|
|
226
|
-
'OpenClaw 里的插件状态没有查出来(openclaw plugins inspect 失败了):用 openclaw plugins inspect kioku 看',
|
|
227
|
-
)
|
|
228
|
-
return
|
|
229
|
-
}
|
|
230
|
-
if (!state) {
|
|
231
|
-
warn(`OpenClaw 里没有装 kioku 的插件:openclaw plugins install ${SOURCE}`)
|
|
232
|
-
return
|
|
233
|
-
}
|
|
234
|
-
if (state.enabled && state.loaded) ok('OpenClaw 里装了 kioku 的插件')
|
|
235
|
-
else
|
|
236
|
-
warn('OpenClaw 里的 kioku 插件没有启用或没有加载成功:用 openclaw plugins inspect kioku 看原因')
|
|
237
|
-
if (state.slot) ok('OpenClaw 的记忆槽位选的是 kioku,自带的记忆工具已停用')
|
|
238
|
-
else warn('OpenClaw 的记忆槽位选的不是 kioku:运行 kioku setup openclaw')
|
|
239
|
-
const access = await configValue(env, ACCESS)
|
|
240
|
-
if (access === 'true') ok('OpenClaw 允许 kioku 读对话(召回与会话摘要要用)')
|
|
241
|
-
else if (access === undefined)
|
|
242
|
-
warn('OpenClaw 允不允许 kioku 读对话没有查出来(openclaw config get 失败了)')
|
|
243
|
-
else warn('OpenClaw 没有允许 kioku 读对话,召回与会话摘要不会生效:运行 kioku setup openclaw')
|
|
244
|
-
note('只在本机入口的对话里生效;外部渠道来的消息里不注入、不上报、不给工具')
|
|
245
|
-
note(
|
|
246
|
-
'工作区里的 MEMORY.md 有内容时 OpenClaw 仍会放进提示:用 kioku import --from openclaw 迁走后清空它',
|
|
247
|
-
)
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
// 给向导的面板:插件装没装、记忆槽位选的是不是 kioku。没有 openclaw 命令、没查出来时为 null
|
|
251
|
-
export async function statusOpenclaw({ env }) {
|
|
252
|
-
const state = await pluginState(env)
|
|
253
|
-
if (state === null || state === undefined) return { plugin: null, memory: null }
|
|
254
|
-
if (!state) return { plugin: false, memory: 'on' }
|
|
255
|
-
return { plugin: state.enabled && state.loaded, memory: state.slot ? 'off' : 'on' }
|
|
256
|
-
}
|
package/lib/panel.mjs
DELETED
|
@@ -1,295 +0,0 @@
|
|
|
1
|
-
// 向导的面板:扫描服务器与本机各平台的现状,排成几行文字,再按缺什么列出可做的动作。
|
|
2
|
-
// 只读:不写文件、不签发访问密钥。查不了的项写「无法判断」,不猜
|
|
3
|
-
|
|
4
|
-
import { stat } from 'node:fs/promises'
|
|
5
|
-
|
|
6
|
-
import { KeyRejected, whoami } from './api.mjs'
|
|
7
|
-
import { hasCommand } from './command.mjs'
|
|
8
|
-
import { failureText, fetchFailure, isRedirect, KiokuFailure } from './http.mjs'
|
|
9
|
-
import { countLocal } from './import.mjs'
|
|
10
|
-
import { PLATFORMS } from './platforms.mjs'
|
|
11
|
-
import { serverUrlProblem } from './profile.mjs'
|
|
12
|
-
import { plain, shownUrl } from './text.mjs'
|
|
13
|
-
|
|
14
|
-
const TIMEOUT = 5000
|
|
15
|
-
|
|
16
|
-
async function getJson(url, path) {
|
|
17
|
-
let res
|
|
18
|
-
let data
|
|
19
|
-
try {
|
|
20
|
-
res = await fetch(`${url}${path}`, { redirect: 'manual', signal: AbortSignal.timeout(TIMEOUT) })
|
|
21
|
-
data = await res.json().catch(() => null)
|
|
22
|
-
} catch (err) {
|
|
23
|
-
throw new KiokuFailure(fetchFailure(err, TIMEOUT))
|
|
24
|
-
}
|
|
25
|
-
if (isRedirect(res.status)) throw new KiokuFailure(`返回了跳转(${res.status})`)
|
|
26
|
-
return { status: res.status, data }
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// 服务器的现状:{ reachable, version, problem, needsSetup }。不要访问密钥
|
|
30
|
-
export async function serverHealth(url) {
|
|
31
|
-
let answer
|
|
32
|
-
try {
|
|
33
|
-
answer = await getJson(url, '/v1/health')
|
|
34
|
-
} catch (err) {
|
|
35
|
-
return { reachable: false, problem: failureText(err) }
|
|
36
|
-
}
|
|
37
|
-
const version = typeof answer.data?.version === 'string' ? plain(answer.data.version) : null
|
|
38
|
-
if (answer.status === 503) {
|
|
39
|
-
return { reachable: true, version, problem: '数据库暂时无法连接' }
|
|
40
|
-
}
|
|
41
|
-
if (answer.status !== 200 || answer.data?.status !== 'ok') {
|
|
42
|
-
return { reachable: false, problem: `不像 Kioku(HTTP ${answer.status})` }
|
|
43
|
-
}
|
|
44
|
-
let needsSetup = false
|
|
45
|
-
try {
|
|
46
|
-
const setup = await getJson(url, '/v1/setup/status')
|
|
47
|
-
needsSetup = setup.status === 200 && setup.data?.needsSetup === true
|
|
48
|
-
} catch {
|
|
49
|
-
// 查不到按已初始化算
|
|
50
|
-
}
|
|
51
|
-
return { reachable: true, version, problem: null, needsSetup }
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
async function isDir(path) {
|
|
55
|
-
return Boolean((await stat(path).catch(() => null))?.isDirectory())
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// 各平台写下的服务地址(按平台的顺序,同一个只留一次)
|
|
59
|
-
export async function knownServers(env) {
|
|
60
|
-
const urls = []
|
|
61
|
-
for (const platform of Object.values(PLATFORMS)) {
|
|
62
|
-
const config = await Promise.resolve(platform.readConfig?.(env)).catch(() => null)
|
|
63
|
-
if (config?.url && !urls.includes(config.url)) urls.push(config.url)
|
|
64
|
-
}
|
|
65
|
-
return urls
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// 没有指明时用哪个服务地址:KIOKU_SERVER_URL,否则各平台写下的里面最先找到的
|
|
69
|
-
export async function defaultServerUrl(env) {
|
|
70
|
-
const fromEnv = (env.KIOKU_SERVER_URL ?? '').trim().replace(/\/+$/, '')
|
|
71
|
-
if (fromEnv && !serverUrlProblem(fromEnv)) return fromEnv
|
|
72
|
-
return (await knownServers(env))[0] ?? null
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// 一个平台的现状。connection:
|
|
76
|
-
// - ok:访问密钥可用;none:没配过;invalid:服务端说访问密钥无效或已吊销
|
|
77
|
-
// - unknown:连不上、响应不对,查不了;broken:配置文件读不懂
|
|
78
|
-
// - manual:Claude Code,访问密钥在它的 /plugin 里填,这里读不到
|
|
79
|
-
export async function scanPlatform(id, { env, cwd, url }) {
|
|
80
|
-
const platform = PLATFORMS[id]
|
|
81
|
-
const present = hasCommand(platform.command(env), env) || (await isDir(platform.home(env)))
|
|
82
|
-
const row = { id, name: platform.name, present, connection: 'none' }
|
|
83
|
-
|
|
84
|
-
let config = null
|
|
85
|
-
if (!platform.readConfig) row.connection = 'manual'
|
|
86
|
-
else {
|
|
87
|
-
try {
|
|
88
|
-
config = await platform.readConfig(env)
|
|
89
|
-
} catch (err) {
|
|
90
|
-
row.connection = 'broken'
|
|
91
|
-
row.reason = failureText(err)
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
if (config) {
|
|
95
|
-
row.url = config.url
|
|
96
|
-
row.otherServer = Boolean(url && config.url !== url)
|
|
97
|
-
try {
|
|
98
|
-
row.who = await whoami(config)
|
|
99
|
-
row.connection = 'ok'
|
|
100
|
-
} catch (err) {
|
|
101
|
-
row.connection = err instanceof KeyRejected ? 'invalid' : 'unknown'
|
|
102
|
-
row.reason = failureText(err)
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const status = await platform.status({ env, cwd }).catch(() => ({ plugin: null, memory: null }))
|
|
107
|
-
row.plugin = status.plugin
|
|
108
|
-
row.memory = status.memory
|
|
109
|
-
if (status.reenabledIn) row.reenabledIn = status.reenabledIn
|
|
110
|
-
|
|
111
|
-
// 只数本地有几条;和 Kioku 比(没迁的有几条)要一条一条地问,选了迁移再比
|
|
112
|
-
if (platform.source) row.local = await countLocal(platform.source, { env }).catch(() => null)
|
|
113
|
-
return row
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// 面板的全部现状。url 为空时只扫本机
|
|
117
|
-
export async function scan({ env, cwd, url }) {
|
|
118
|
-
const server = url ? { url, ...(await serverHealth(url)) } : null
|
|
119
|
-
const platforms = await Promise.all(
|
|
120
|
-
Object.keys(PLATFORMS).map((id) => scanPlatform(id, { env, cwd, url })),
|
|
121
|
-
)
|
|
122
|
-
return { server, platforms }
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const PLUGIN_NAMES = new Map([
|
|
126
|
-
[true, '已安装'],
|
|
127
|
-
[false, '未安装'],
|
|
128
|
-
[null, '无法判断'],
|
|
129
|
-
])
|
|
130
|
-
const MEMORY_NAMES = { off: '已停用', on: '未停用', none: '无' }
|
|
131
|
-
|
|
132
|
-
// 访问密钥属于哪个空间:服务端给了空间名就用名字,否则用 id 的前 8 位
|
|
133
|
-
function spaceText(who) {
|
|
134
|
-
if (typeof who.spaceName === 'string' && who.spaceName) return plain(who.spaceName)
|
|
135
|
-
return typeof who.spaceId === 'string' ? plain(who.spaceId).slice(0, 8) : null
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// 能不能停用:配过(不管现在能不能用),或者 Claude Code 的自动记忆停着
|
|
139
|
-
export function canDisable(row) {
|
|
140
|
-
return (
|
|
141
|
-
['ok', 'invalid', 'unknown', 'broken'].includes(row.connection) ||
|
|
142
|
-
(row.connection === 'manual' && row.memory === 'off')
|
|
143
|
-
)
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// 平台接到哪一步,一个短词
|
|
147
|
-
export function connectionText(row) {
|
|
148
|
-
if (row.connection === 'manual') return row.plugin ? '插件已启用' : '未接入'
|
|
149
|
-
if (row.connection === 'ok' && row.otherServer) return '接到另一台服务器'
|
|
150
|
-
return (
|
|
151
|
-
{
|
|
152
|
-
ok: '已接入',
|
|
153
|
-
none: row.present ? '未接入' : '未安装',
|
|
154
|
-
invalid: '已失效',
|
|
155
|
-
unknown: '无法判断',
|
|
156
|
-
broken: '配置有误',
|
|
157
|
-
}[row.connection] ?? '无法判断'
|
|
158
|
-
)
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function platformLines(row) {
|
|
162
|
-
const head = connectionText(row)
|
|
163
|
-
const facts = []
|
|
164
|
-
if (row.connection === 'ok') {
|
|
165
|
-
const app = row.who.app
|
|
166
|
-
facts.push(`应用 ${plain(app.name)}(${plain(app.slug)})`)
|
|
167
|
-
const space = spaceText(row.who)
|
|
168
|
-
if (space) facts.push(`空间 ${space}`)
|
|
169
|
-
if (typeof row.who.accountName === 'string' && row.who.accountName) {
|
|
170
|
-
facts.push(`账号 ${plain(row.who.accountName)}`)
|
|
171
|
-
}
|
|
172
|
-
if (app.runtimeFamily !== PLATFORMS[row.id].runtimeFamily) facts.push('应用的运行时不对')
|
|
173
|
-
}
|
|
174
|
-
if (row.otherServer) facts.push(shownUrl(row.url))
|
|
175
|
-
if (row.reason) facts.push(row.reason)
|
|
176
|
-
const first = [`${row.name}:${head}`, ...facts].join(' · ')
|
|
177
|
-
// 没装、也没配过的平台只写一行
|
|
178
|
-
if (!row.present && row.connection === 'none') return [first]
|
|
179
|
-
const details = []
|
|
180
|
-
if (row.connection === 'manual') details.push('访问密钥 无法判断(在 /plugin 里填)')
|
|
181
|
-
details.push(`插件 ${PLUGIN_NAMES.get(row.plugin) ?? '无法判断'}`)
|
|
182
|
-
const reenabled = row.reenabledIn ? `(被 ./${row.reenabledIn} 重新启用)` : ''
|
|
183
|
-
details.push(`自带记忆 ${MEMORY_NAMES[row.memory] ?? '无法判断'}${reenabled}`)
|
|
184
|
-
if (row.local) {
|
|
185
|
-
// 未迁移的条数只在选过迁移、和 Kioku 比过之后才有
|
|
186
|
-
const pending = !row.pending
|
|
187
|
-
? ''
|
|
188
|
-
: row.pending.pending === null
|
|
189
|
-
? '(未迁移 无法判断)'
|
|
190
|
-
: `(未迁移 ${row.pending.pending} 条)`
|
|
191
|
-
details.push(`本地记忆 ${row.local} 条${pending}`)
|
|
192
|
-
}
|
|
193
|
-
return [first, ` ${details.join(' · ')}`]
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// 面板的文字
|
|
197
|
-
export function renderPanel(state) {
|
|
198
|
-
const lines = []
|
|
199
|
-
const { server } = state
|
|
200
|
-
if (!server) lines.push('服务器:未设置')
|
|
201
|
-
else {
|
|
202
|
-
const facts = []
|
|
203
|
-
if (!server.reachable) facts.push(`无法连接(${server.problem})`)
|
|
204
|
-
else {
|
|
205
|
-
facts.push(server.problem ?? '可连接')
|
|
206
|
-
if (server.version) facts.push(`版本 ${server.version}`)
|
|
207
|
-
if (server.needsSetup) facts.push('未初始化')
|
|
208
|
-
}
|
|
209
|
-
lines.push(`服务器:${shownUrl(server.url)}(${facts.join(' · ')})`)
|
|
210
|
-
const spaces = [
|
|
211
|
-
...new Set(
|
|
212
|
-
state.platforms
|
|
213
|
-
.filter((row) => row.connection === 'ok')
|
|
214
|
-
.map((row) => spaceText(row.who))
|
|
215
|
-
.filter(Boolean),
|
|
216
|
-
),
|
|
217
|
-
]
|
|
218
|
-
if (spaces.length > 1) lines.push(`访问密钥分属 ${spaces.length} 个空间:${spaces.join('、')}`)
|
|
219
|
-
}
|
|
220
|
-
lines.push('')
|
|
221
|
-
for (const row of state.platforms) lines.push(...platformLines(row))
|
|
222
|
-
return lines.join('\n')
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
// 要接入的候选:有自己的配置、还没接到这台服务器的(失效的另算)
|
|
226
|
-
export function connectCandidates(state) {
|
|
227
|
-
return state.platforms.filter(
|
|
228
|
-
(row) =>
|
|
229
|
-
PLATFORMS[row.id].setup &&
|
|
230
|
-
row.connection !== 'invalid' &&
|
|
231
|
-
!(row.connection === 'ok' && !row.otherServer),
|
|
232
|
-
)
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
const names = (rows) => rows.map((row) => row.name).join('、')
|
|
236
|
-
|
|
237
|
-
// 面板下面列出的动作:{ value, label, hint? },按缺什么排在前面
|
|
238
|
-
export function actionsFor(state) {
|
|
239
|
-
const actions = []
|
|
240
|
-
const { server } = state
|
|
241
|
-
const ready = Boolean(server?.reachable && !server.problem && !server.needsSetup)
|
|
242
|
-
if (!server || !server.reachable) {
|
|
243
|
-
actions.push({ value: 'local', label: '本机起一个 Kioku', hint: '用 Docker' })
|
|
244
|
-
}
|
|
245
|
-
if (!server) actions.push({ value: 'server', label: '填写服务器地址' })
|
|
246
|
-
if (server?.needsSetup) {
|
|
247
|
-
actions.push({ value: 'open', label: '打开管理界面初始化', hint: '建第一个管理员' })
|
|
248
|
-
}
|
|
249
|
-
if (ready) {
|
|
250
|
-
const candidates = connectCandidates(state)
|
|
251
|
-
const present = candidates.filter((row) => row.present)
|
|
252
|
-
if (present.length) {
|
|
253
|
-
actions.push({
|
|
254
|
-
value: 'connect',
|
|
255
|
-
label: `接入 ${names(present)}(${present.length} 个未接入)`,
|
|
256
|
-
})
|
|
257
|
-
} else if (candidates.length) {
|
|
258
|
-
actions.push({ value: 'connect', label: '接入其他平台', hint: '本机没发现的' })
|
|
259
|
-
}
|
|
260
|
-
const invalid = state.platforms.filter((row) => row.connection === 'invalid')
|
|
261
|
-
if (invalid.length) {
|
|
262
|
-
actions.push({ value: 'reauth', label: `重新授权 ${names(invalid)}(访问密钥已失效)` })
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
// Claude Code 的访问密钥在它的 /plugin 里填,浏览器授权不给它发:插件没启用时列出安装与要填的地址,
|
|
266
|
-
// 顺带停用自动记忆;插件启用了、自动记忆还开着时只停用自动记忆。项目设置又启用了的,停用也没用,不提供
|
|
267
|
-
const claude = state.platforms.find((row) => row.id === 'claude-code')
|
|
268
|
-
if (claude?.present && server && claude.plugin === false) {
|
|
269
|
-
actions.push({ value: 'claude-connect', label: '接入 Claude Code', hint: '在 /plugin 里填' })
|
|
270
|
-
} else if (claude?.present && claude.memory === 'on' && !claude.reenabledIn) {
|
|
271
|
-
actions.push({ value: 'claude-memory', label: '停用 Claude Code 的自动记忆' })
|
|
272
|
-
}
|
|
273
|
-
if (ready) {
|
|
274
|
-
for (const row of state.platforms) {
|
|
275
|
-
if (!row.local) continue
|
|
276
|
-
// 迁移要这个平台的访问密钥(Claude Code 的另外问)
|
|
277
|
-
if (row.connection !== 'manual' && !(row.connection === 'ok' && !row.otherServer)) continue
|
|
278
|
-
// 比过、确实都迁了的不再提供
|
|
279
|
-
if (row.pending?.pending === 0) continue
|
|
280
|
-
const count =
|
|
281
|
-
typeof row.pending?.pending === 'number'
|
|
282
|
-
? `未迁移 ${row.pending.pending} 条`
|
|
283
|
-
: `${row.local} 条`
|
|
284
|
-
actions.push({ value: `import:${row.id}`, label: `迁移 ${row.name} 的本地记忆(${count})` })
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
if (state.platforms.some(canDisable)) {
|
|
288
|
-
actions.push({ value: 'disable', label: '停用某个平台', hint: '恢复自带记忆、清掉配置' })
|
|
289
|
-
}
|
|
290
|
-
actions.push({ value: 'doctor', label: '检查(doctor)' })
|
|
291
|
-
if (server) actions.push({ value: 'server', label: '更换服务器地址' })
|
|
292
|
-
actions.push({ value: 'refresh', label: '刷新' })
|
|
293
|
-
actions.push({ value: 'exit', label: '退出' })
|
|
294
|
-
return actions
|
|
295
|
-
}
|
package/lib/pi.mjs
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
// pi 这个平台的 kioku setup 与 kioku doctor。
|
|
2
|
-
// pi 没有自带的长期记忆要停用,扩展自己注册 MCP 服务,所以 setup 只做一件事:把服务地址与访问密钥写进本机的文件,
|
|
3
|
-
// 扩展读它
|
|
4
|
-
|
|
5
|
-
import { homedir } from 'node:os'
|
|
6
|
-
import { join } from 'node:path'
|
|
7
|
-
|
|
8
|
-
import { readSettings, readText } from './files.mjs'
|
|
9
|
-
import { profileFile, readProfile, removeProfile, writeProfile } from './profile.mjs'
|
|
10
|
-
import { resolveTarget } from './target.mjs'
|
|
11
|
-
|
|
12
|
-
const SOURCE = 'git:github.com/lonelyman0108/kioku-plugins'
|
|
13
|
-
|
|
14
|
-
export function piHome(env) {
|
|
15
|
-
return env.PI_CODING_AGENT_DIR || join(homedir(), '.pi', 'agent')
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// 包的来源写法有几种(git:、https://、本地路径),都带着仓库名
|
|
19
|
-
function installed(settings) {
|
|
20
|
-
const packages = Array.isArray(settings?.packages) ? settings.packages : []
|
|
21
|
-
return packages.some((entry) => {
|
|
22
|
-
const source = typeof entry === 'string' ? entry : entry?.source
|
|
23
|
-
return typeof source === 'string' && source.includes('kioku-plugins')
|
|
24
|
-
})
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export async function setupPi(options, io) {
|
|
28
|
-
const { env, out } = io
|
|
29
|
-
const profile = profileFile('pi', env)
|
|
30
|
-
if (options.undo) {
|
|
31
|
-
if (options.dryRun) out.write(`${profile}:删掉\n没有写入(--dry-run)\n`)
|
|
32
|
-
else if (await removeProfile('pi', env)) out.write(`已删掉 ${profile}\n`)
|
|
33
|
-
else out.write(`${profile} 本来就没有\n`)
|
|
34
|
-
out.write(`扩展要另外卸载:pi remove ${SOURCE}\n`)
|
|
35
|
-
return 0
|
|
36
|
-
}
|
|
37
|
-
const { url, key, saved } = await resolveTarget({ id: 'pi', name: 'pi' }, options, io)
|
|
38
|
-
const same = Boolean(saved && saved.url === url && saved.key === key)
|
|
39
|
-
if (same) out.write(`${profile} 不用改\n`)
|
|
40
|
-
else out.write(`${profile}:写入服务地址与访问密钥(只给本人读写)\n`)
|
|
41
|
-
if (options.dryRun) {
|
|
42
|
-
out.write('没有写入(--dry-run)\n')
|
|
43
|
-
return 0
|
|
44
|
-
}
|
|
45
|
-
if (!same) await writeProfile('pi', { url, key }, env)
|
|
46
|
-
const { settings } = await readSettings(join(piHome(env), 'settings.json')).catch(() => ({}))
|
|
47
|
-
out.write('pi 已配好,新开会话(或 /reload)生效。\n')
|
|
48
|
-
if (!installed(settings) && io.hints !== false) {
|
|
49
|
-
out.write(`接下来装上扩展:pi install ${SOURCE}\n`)
|
|
50
|
-
}
|
|
51
|
-
return 0
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// 一份 mcp.json 里有没有叫 kioku 的服务;没有这个文件、读不懂时为假(读不懂的由 pi 自己报)
|
|
55
|
-
async function namesKioku(file) {
|
|
56
|
-
try {
|
|
57
|
-
return Object.hasOwn(JSON.parse((await readText(file)) ?? '{}')?.mcpServers ?? {}, 'kioku')
|
|
58
|
-
} catch {
|
|
59
|
-
return false
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export async function inspectPi({ env, cwd }, { ok, warn }) {
|
|
64
|
-
const profile = await readProfile('pi', env).catch(() => null)
|
|
65
|
-
if (!profile) warn('没有 kioku setup pi 写下的地址与访问密钥,扩展不会生效:运行 kioku setup pi')
|
|
66
|
-
|
|
67
|
-
// 用户级的与当前项目的设置都看:扩展可以只装在项目里,项目的 mcp.json 也盖过扩展注册的服务
|
|
68
|
-
const home = piHome(env)
|
|
69
|
-
const project = join(cwd, '.pi')
|
|
70
|
-
const user = await readSettings(join(home, 'settings.json'))
|
|
71
|
-
const local = await readSettings(join(project, 'settings.json')).catch(() => ({}))
|
|
72
|
-
if (installed(user.settings) || installed(local.settings)) ok('pi 里装了 kioku 的扩展')
|
|
73
|
-
else warn(`pi 里没有装 kioku 的扩展:pi install ${SOURCE}`)
|
|
74
|
-
|
|
75
|
-
const shadowing = []
|
|
76
|
-
for (const file of [join(home, 'mcp.json'), join(project, 'mcp.json')]) {
|
|
77
|
-
if (await namesKioku(file)) shadowing.push(file)
|
|
78
|
-
}
|
|
79
|
-
if (shadowing.length) {
|
|
80
|
-
warn(`${shadowing.join('、')} 里有一个叫 kioku 的 MCP 服务,它会盖过扩展注册的那个:去掉它`)
|
|
81
|
-
} else ok('MCP 服务由扩展在会话开始时注册')
|
|
82
|
-
ok('pi 没有自带的长期记忆要停用')
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// 给向导的面板:扩展装没装(用户级或当前项目里),pi 没有自带的长期记忆
|
|
86
|
-
export async function statusPi({ env, cwd }) {
|
|
87
|
-
const user = await readSettings(join(piHome(env), 'settings.json')).catch(() => ({}))
|
|
88
|
-
const local = await readSettings(join(cwd, '.pi', 'settings.json')).catch(() => ({}))
|
|
89
|
-
return { plugin: installed(user.settings) || installed(local.settings), memory: 'none' }
|
|
90
|
-
}
|