@kioku-memory/plugin-opencode 0.1.0 → 0.5.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/command.mjs +7 -1
- package/lib/kioku.mjs +35 -433
- package/lib/run.mjs +372 -0
- package/package.json +15 -1
- package/lib/agy.mjs +0 -181
- package/lib/api.mjs +0 -38
- package/lib/clean.mjs +0 -96
- package/lib/codex.mjs +0 -320
- package/lib/conflicts.mjs +0 -1303
- package/lib/connect.mjs +0 -152
- package/lib/doctor.mjs +0 -158
- package/lib/dsh.mjs +0 -82
- package/lib/files.mjs +0 -92
- package/lib/gemini.mjs +0 -199
- package/lib/hermes.mjs +0 -276
- package/lib/import.mjs +0 -301
- package/lib/install.mjs +0 -196
- package/lib/local.mjs +0 -294
- package/lib/login.mjs +0 -359
- package/lib/openclaw.mjs +0 -256
- package/lib/opencode.mjs +0 -144
- package/lib/panel.mjs +0 -311
- package/lib/pi.mjs +0 -90
- package/lib/platforms.mjs +0 -297
- package/lib/prompts.mjs +0 -38
- package/lib/secret.mjs +0 -86
- package/lib/setup.mjs +0 -139
- package/lib/sources.mjs +0 -333
- package/lib/target.mjs +0 -62
- package/lib/wizard.mjs +0 -534
package/lib/login.mjs
DELETED
|
@@ -1,359 +0,0 @@
|
|
|
1
|
-
// kioku login:浏览器授权。命令行向 Kioku 申请,显示一个确认码;用户在管理界面的确认页输入它、
|
|
2
|
-
// 选好空间与权限后确认,命令行取走访问密钥,交给这个平台的 kioku setup 写下。
|
|
3
|
-
// 访问密钥不显示、不写进输出;确认码不放进打开的地址里,要用户照着终端输入:别人发来的链接带不了它。
|
|
4
|
-
// 服务端的回答不全信:确认页的路径只收一种写法,时限与间隔收在一个范围里
|
|
5
|
-
|
|
6
|
-
import { spawn } from 'node:child_process'
|
|
7
|
-
import { hostname } from 'node:os'
|
|
8
|
-
|
|
9
|
-
import { ensureNoConflicts } from './conflicts.mjs'
|
|
10
|
-
import { configure, orphanHint } from './connect.mjs'
|
|
11
|
-
import { fetchFailure, isRedirect, KiokuFailure, redirectError } from './http.mjs'
|
|
12
|
-
import { platformNames, PLATFORMS } from './platforms.mjs'
|
|
13
|
-
import { resolveServerUrl } from './target.mjs'
|
|
14
|
-
import { plain } from './text.mjs'
|
|
15
|
-
import { UsageError } from './usage.mjs'
|
|
16
|
-
|
|
17
|
-
export const LOGIN_USAGE = `用法:kioku login <平台> [--server-url <地址>] [--no-browser] [--remove-conflicts]
|
|
18
|
-
|
|
19
|
-
用浏览器授权给这个平台签发一个访问密钥并配好,不用手动复制:终端显示一个确认码,
|
|
20
|
-
在 Kioku 管理界面的确认页输入它,选好空间与权限后确认。
|
|
21
|
-
|
|
22
|
-
<平台> ${platformNames()}(Claude Code 的访问密钥在它的 /plugin 里填,不走这里)
|
|
23
|
-
--server-url <地址> Kioku 的服务地址,例如 https://kioku.example.com(也可以用 KIOKU_SERVER_URL;配过的沿用上次的)
|
|
24
|
-
--no-browser 不自动打开浏览器,只显示地址
|
|
25
|
-
--remove-conflicts 先卸载平台上已有的冲突记忆工具(Lore 及常见记忆 MCP)再继续
|
|
26
|
-
|
|
27
|
-
只在自己刚运行了这条命令时确认:确认之后,这台机器会拿到一个能按所选权限读写记忆的访问密钥。
|
|
28
|
-
`
|
|
29
|
-
|
|
30
|
-
const TIMEOUT = 8000
|
|
31
|
-
// 连着这么多次没得到回答(连不上、服务端 5xx)才放弃:等确认的这几分钟里抖一下不算失败
|
|
32
|
-
const MAX_FAILURES = 5
|
|
33
|
-
// 确认页的路径只收这种写法:接在服务地址后面打开,不带查询参数与别的符号
|
|
34
|
-
const VERIFICATION_PATH = /^\/[A-Za-z0-9_-]+(?:\/[A-Za-z0-9_-]+)*$/
|
|
35
|
-
// 服务端给的时限与间隔收在这个范围里(秒):给得再离谱也不会一直等、不会忙等
|
|
36
|
-
const MAX_EXPIRES = 1800
|
|
37
|
-
const MIN_INTERVAL = 1
|
|
38
|
-
const MAX_INTERVAL = 60
|
|
39
|
-
|
|
40
|
-
// 地址返回了跳转:配置的问题,重试没有用
|
|
41
|
-
class Redirected extends KiokuFailure {}
|
|
42
|
-
|
|
43
|
-
// 取走访问密钥这一步之后出的问题:访问密钥可能已经签发、却没有落到这台机器上
|
|
44
|
-
const ORPHAN_HINT =
|
|
45
|
-
'如果确认页上已经点了确认,可能已经签发了一个没被取走的访问密钥:到管理界面的应用页吊销它,再重新运行'
|
|
46
|
-
|
|
47
|
-
async function post(url, path, body) {
|
|
48
|
-
let res
|
|
49
|
-
let data
|
|
50
|
-
try {
|
|
51
|
-
res = await fetch(`${url}${path}`, {
|
|
52
|
-
method: 'POST',
|
|
53
|
-
headers: { 'content-type': 'application/json' },
|
|
54
|
-
body: JSON.stringify(body),
|
|
55
|
-
// 不跟着跳转走:设备码只发给配置的地址
|
|
56
|
-
redirect: 'manual',
|
|
57
|
-
signal: AbortSignal.timeout(TIMEOUT),
|
|
58
|
-
})
|
|
59
|
-
data = await res.json().catch(() => null)
|
|
60
|
-
} catch (err) {
|
|
61
|
-
throw new KiokuFailure(fetchFailure(err, TIMEOUT))
|
|
62
|
-
}
|
|
63
|
-
if (isRedirect(res.status)) {
|
|
64
|
-
throw new Redirected(redirectError(res.status, res.headers.get('location'), url))
|
|
65
|
-
}
|
|
66
|
-
return { status: res.status, data }
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// 服务端给的秒数收进范围;不是有限的正数时用默认值
|
|
70
|
-
function seconds(value, fallback, min, max) {
|
|
71
|
-
const number = typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : fallback
|
|
72
|
-
return Math.min(max, Math.max(min, number))
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// 服务端不认一次申请几个平台的写法:退回逐个平台申请
|
|
76
|
-
export class LegacyServer extends KiokuFailure {}
|
|
77
|
-
|
|
78
|
-
// 旧的服务端只认单个平台的写法,收到 platforms 时按校验失败回 400,第一条问题是缺 runtimeFamily
|
|
79
|
-
// (「runtimeFamily:…」)。别的 400 是申请本身有问题,照原样报
|
|
80
|
-
function legacyRejection(data) {
|
|
81
|
-
return typeof data?.error === 'string' && /^runtimeFamily[::]/.test(data.error)
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// 一次申请最多几个平台,与服务端的上限一致
|
|
85
|
-
export const MAX_PLATFORMS = 8
|
|
86
|
-
|
|
87
|
-
// 申请:返回设备码、确认码、确认页的地址与时限。body 是单平台的 { runtimeFamily, clientName },
|
|
88
|
-
// 或几个平台的 { platforms: [{ runtimeFamily }], clientName }
|
|
89
|
-
export async function requestGrant(url, body) {
|
|
90
|
-
const { clientName, ...rest } = body
|
|
91
|
-
const { status, data } = await post(url, '/v1/device/authorize', {
|
|
92
|
-
...rest,
|
|
93
|
-
...(clientName ? { clientName } : {}),
|
|
94
|
-
})
|
|
95
|
-
if (status === 400 && Array.isArray(body.platforms) && legacyRejection(data)) {
|
|
96
|
-
throw new LegacyServer('服务端版本较旧,需要逐个确认')
|
|
97
|
-
}
|
|
98
|
-
if (status === 400) {
|
|
99
|
-
const reason = typeof data?.error === 'string' ? plain(data.error) : ''
|
|
100
|
-
throw new KiokuFailure(`Kioku 拒绝了这次申请${reason ? `:${reason}` : ''}`)
|
|
101
|
-
}
|
|
102
|
-
if (status === 404) {
|
|
103
|
-
throw new KiokuFailure(
|
|
104
|
-
'这个 Kioku 还不支持浏览器授权:升级服务端,或者用 kioku setup 手动填访问密钥',
|
|
105
|
-
)
|
|
106
|
-
}
|
|
107
|
-
if (status === 429) throw new KiokuFailure('待确认的申请太多,稍后再试')
|
|
108
|
-
if (status !== 201) throw new KiokuFailure(`Kioku 返回 HTTP ${status}`)
|
|
109
|
-
if (
|
|
110
|
-
typeof data?.deviceCode !== 'string' ||
|
|
111
|
-
!data.deviceCode ||
|
|
112
|
-
typeof data.userCode !== 'string' ||
|
|
113
|
-
typeof data.verificationPath !== 'string' ||
|
|
114
|
-
!VERIFICATION_PATH.test(data.verificationPath)
|
|
115
|
-
) {
|
|
116
|
-
throw new KiokuFailure('Kioku 的响应格式不对')
|
|
117
|
-
}
|
|
118
|
-
return {
|
|
119
|
-
deviceCode: data.deviceCode,
|
|
120
|
-
userCode: data.userCode,
|
|
121
|
-
verificationUrl: `${url}${data.verificationPath}`,
|
|
122
|
-
expiresIn: seconds(data.expiresIn, 600, 1, MAX_EXPIRES),
|
|
123
|
-
interval: seconds(data.interval, 3, MIN_INTERVAL, MAX_INTERVAL),
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// 等确认:按服务端给的间隔轮询,确认了返回服务端的整个回答;被拒绝、过期时抛 KiokuFailure。
|
|
128
|
-
// sleep 与 now 可以换掉,给用例用;now 要是只往前走的钟,系统时间被改不影响什么时候停
|
|
129
|
-
async function waitForApproval(
|
|
130
|
-
url,
|
|
131
|
-
grant,
|
|
132
|
-
{
|
|
133
|
-
sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
134
|
-
now = () => performance.now(),
|
|
135
|
-
} = {},
|
|
136
|
-
) {
|
|
137
|
-
const deadline = now() + grant.expiresIn * 1000
|
|
138
|
-
let interval = grant.interval * 1000
|
|
139
|
-
let failures = 0
|
|
140
|
-
// 有过没得到回答的轮询:那一次服务端可能已经把访问密钥签出来了
|
|
141
|
-
let missed = false
|
|
142
|
-
while (now() < deadline) {
|
|
143
|
-
await sleep(interval)
|
|
144
|
-
let answer
|
|
145
|
-
try {
|
|
146
|
-
answer = await post(url, '/v1/device/token', { deviceCode: grant.deviceCode })
|
|
147
|
-
} catch (err) {
|
|
148
|
-
if (err instanceof Redirected) throw err
|
|
149
|
-
missed = true
|
|
150
|
-
if (++failures >= MAX_FAILURES) throw new KiokuFailure(`${err.message}。${ORPHAN_HINT}`)
|
|
151
|
-
continue
|
|
152
|
-
}
|
|
153
|
-
const { status, data } = answer
|
|
154
|
-
// 服务端嫌太勤:隔久一点
|
|
155
|
-
if (status === 429) {
|
|
156
|
-
interval = Math.min(interval + 1000, MAX_INTERVAL * 1000)
|
|
157
|
-
continue
|
|
158
|
-
}
|
|
159
|
-
// 服务端一时出错(重启、网关)和连不上一样,过会儿再问
|
|
160
|
-
if (status >= 500) {
|
|
161
|
-
missed = true
|
|
162
|
-
if (++failures >= MAX_FAILURES) {
|
|
163
|
-
throw new KiokuFailure(`Kioku 返回 HTTP ${status}。${ORPHAN_HINT}`)
|
|
164
|
-
}
|
|
165
|
-
continue
|
|
166
|
-
}
|
|
167
|
-
if (status !== 200) throw new KiokuFailure(`Kioku 返回 HTTP ${status}`)
|
|
168
|
-
failures = 0
|
|
169
|
-
if (data?.status === 'pending') continue
|
|
170
|
-
if (data?.status === 'denied') {
|
|
171
|
-
throw new KiokuFailure('申请被拒绝了,或者确认之后确认人的权限变了:没有签发访问密钥')
|
|
172
|
-
}
|
|
173
|
-
if (data?.status === 'approved') return data
|
|
174
|
-
if (data?.status === 'expired') break
|
|
175
|
-
throw new KiokuFailure('Kioku 的响应格式不对')
|
|
176
|
-
}
|
|
177
|
-
throw new KiokuFailure(
|
|
178
|
-
missed
|
|
179
|
-
? `申请已经不在了,没有取到访问密钥。${ORPHAN_HINT}`
|
|
180
|
-
: '申请过期了,没有等到确认:重新运行 kioku login',
|
|
181
|
-
)
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// 等一个平台的申请被确认,返回访问密钥与应用
|
|
185
|
-
export async function waitForKey(url, grant, options) {
|
|
186
|
-
const data = await waitForApproval(url, grant, options)
|
|
187
|
-
if (typeof data.key !== 'string' || !data.key) throw new KiokuFailure('Kioku 的响应格式不对')
|
|
188
|
-
return { key: data.key, app: data.app ?? {} }
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// 几个平台一起的申请被确认之后,每个平台的结果:按申请的顺序,
|
|
192
|
-
// { runtimeFamily, status: 'issued', key, app } | { runtimeFamily, status: 'skipped' } | { runtimeFamily, status: 'denied', reason }。
|
|
193
|
-
// 服务端没提到的平台算跳过,不认识的平台、重复的项不收;签发了却不像访问密钥的算响应不对
|
|
194
|
-
export async function waitForKeys(url, grant, families, options) {
|
|
195
|
-
const data = await waitForApproval(url, grant, options)
|
|
196
|
-
// 已经确认了:响应读不懂时,服务端可能已经签发了几个没人拿着的访问密钥
|
|
197
|
-
const malformed = () => new KiokuFailure(`Kioku 的响应格式不对。${ORPHAN_HINT}`)
|
|
198
|
-
if (!Array.isArray(data.keys)) throw malformed()
|
|
199
|
-
const byFamily = new Map()
|
|
200
|
-
for (const item of data.keys) {
|
|
201
|
-
const family = item?.runtimeFamily
|
|
202
|
-
if (!families.includes(family) || byFamily.has(family)) continue
|
|
203
|
-
if (item.status === 'issued') {
|
|
204
|
-
if (typeof item.key !== 'string' || !item.key) throw malformed()
|
|
205
|
-
byFamily.set(family, {
|
|
206
|
-
runtimeFamily: family,
|
|
207
|
-
status: 'issued',
|
|
208
|
-
key: item.key,
|
|
209
|
-
app: item.app ?? {},
|
|
210
|
-
})
|
|
211
|
-
} else if (item.status === 'denied') {
|
|
212
|
-
const reason = typeof item.reason === 'string' ? plain(item.reason) : ''
|
|
213
|
-
byFamily.set(family, { runtimeFamily: family, status: 'denied', reason })
|
|
214
|
-
} else byFamily.set(family, { runtimeFamily: family, status: 'skipped' })
|
|
215
|
-
}
|
|
216
|
-
return families.map(
|
|
217
|
-
(family) => byFamily.get(family) ?? { runtimeFamily: family, status: 'skipped' },
|
|
218
|
-
)
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
// 用系统默认的浏览器打开地址;打不开就算了,地址已经显示在终端里。都不经过 shell:
|
|
222
|
-
// Windows 上用 rundll32,不用 cmd 的 start(它会把地址里的 & 这类符号当成命令)
|
|
223
|
-
export function openBrowser(address) {
|
|
224
|
-
const [command, args] =
|
|
225
|
-
process.platform === 'darwin'
|
|
226
|
-
? ['open', [address]]
|
|
227
|
-
: process.platform === 'win32'
|
|
228
|
-
? ['rundll32', ['url.dll,FileProtocolHandler', address]]
|
|
229
|
-
: ['xdg-open', [address]]
|
|
230
|
-
try {
|
|
231
|
-
const child = spawn(command, args, { stdio: 'ignore', detached: true })
|
|
232
|
-
child.on('error', () => {})
|
|
233
|
-
child.unref()
|
|
234
|
-
} catch {
|
|
235
|
-
// 没有图形界面
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
// 本地网络给主机名加的后缀,不算机器的名字
|
|
240
|
-
const HOST_SUFFIX = /\.(?:local|localdomain|lan|home|internal)$/i
|
|
241
|
-
|
|
242
|
-
// 机器名是自报的:去掉控制字符与 .local 这类后缀,太长就截断;什么都不剩时用 kioku-cli。
|
|
243
|
-
// 服务端把它显示在确认页上,新建的应用默认叫「平台名 · 机器名」
|
|
244
|
-
export function machineName(clientName) {
|
|
245
|
-
const name = Array.from(plain(clientName).replace(HOST_SUFFIX, '').trim())
|
|
246
|
-
.slice(0, 64)
|
|
247
|
-
.join('')
|
|
248
|
-
.trim()
|
|
249
|
-
return name || 'kioku-cli'
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// 显示确认码与确认页,打开浏览器
|
|
253
|
-
function showGrant(grant, { out, open, browser }) {
|
|
254
|
-
out.write(`确认码:${plain(grant.userCode)}\n`)
|
|
255
|
-
// 显示的和打开的是同一个地址
|
|
256
|
-
out.write(`在浏览器里打开 ${plain(grant.verificationUrl)},登录后输入这个确认码\n`)
|
|
257
|
-
out.write(`等待确认(${Math.ceil(grant.expiresIn / 60)} 分钟内有效)…\n`)
|
|
258
|
-
if (browser) open(grant.verificationUrl)
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// 走一遍浏览器授权,返回访问密钥。提示写到 out,访问密钥不写
|
|
262
|
-
export async function deviceLogin(
|
|
263
|
-
{ url, runtimeFamily },
|
|
264
|
-
{ out, open = openBrowser, browser = true, clientName = hostname(), sleep, now } = {},
|
|
265
|
-
) {
|
|
266
|
-
const grant = await requestGrant(url, { runtimeFamily, clientName: machineName(clientName) })
|
|
267
|
-
showGrant(grant, { out, open, browser })
|
|
268
|
-
const { key, app } = await waitForKey(url, grant, { sleep, now })
|
|
269
|
-
out.write(`✓ 已确认:应用 ${plain(app.name ?? '')}(${plain(app.slug ?? '')})\n`)
|
|
270
|
-
return key
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
// 几个平台一起申请,只确认一次。返回 waitForKeys 的结果(按去重后的顺序);服务端不认这种写法时抛 LegacyServer。
|
|
274
|
-
// 一次最多 MAX_PLATFORMS 个,多了由调用方分批(connect.mjs),这里不发超出上限的申请
|
|
275
|
-
export async function deviceLoginMany(
|
|
276
|
-
{ url, runtimeFamilies },
|
|
277
|
-
{ out, open = openBrowser, browser = true, clientName = hostname(), sleep, now } = {},
|
|
278
|
-
) {
|
|
279
|
-
const families = [...new Set(runtimeFamilies)]
|
|
280
|
-
if (!families.length || families.length > MAX_PLATFORMS) {
|
|
281
|
-
throw new KiokuFailure(`一次申请 1 到 ${MAX_PLATFORMS} 个平台`)
|
|
282
|
-
}
|
|
283
|
-
const grant = await requestGrant(url, {
|
|
284
|
-
platforms: families.map((runtimeFamily) => ({ runtimeFamily })),
|
|
285
|
-
clientName: machineName(clientName),
|
|
286
|
-
})
|
|
287
|
-
showGrant(grant, { out, open, browser })
|
|
288
|
-
const results = await waitForKeys(url, grant, families, { sleep, now })
|
|
289
|
-
// 确认了也可能一个都没签发:取走时照签发规则再核对,全都没过(例如确认人已停用)
|
|
290
|
-
const issued = results.filter((item) => item.status === 'issued').length
|
|
291
|
-
out.write(issued ? `✓ 已确认,签发了 ${issued} 个访问密钥\n` : '✗ 已确认,但没有签发访问密钥\n')
|
|
292
|
-
return results
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
export function parseLoginArgs(argv) {
|
|
296
|
-
const options = { serverUrl: undefined, browser: true, removeConflicts: false }
|
|
297
|
-
const names = []
|
|
298
|
-
for (let i = 0; i < argv.length; i++) {
|
|
299
|
-
const arg = argv[i]
|
|
300
|
-
if (arg === '--no-browser') options.browser = false
|
|
301
|
-
else if (arg === '--remove-conflicts') options.removeConflicts = true
|
|
302
|
-
else if (arg === '--server-url') {
|
|
303
|
-
options.serverUrl = argv[++i]
|
|
304
|
-
if (!options.serverUrl) throw new UsageError('--server-url 后面要写地址')
|
|
305
|
-
} else if (arg.startsWith('-')) throw new UsageError(`不认识的选项 ${arg}`)
|
|
306
|
-
else names.push(arg)
|
|
307
|
-
}
|
|
308
|
-
if (names.length !== 1) throw new UsageError('要写一个平台')
|
|
309
|
-
if (!Object.hasOwn(PLATFORMS, names[0])) {
|
|
310
|
-
throw new UsageError(`不认识的平台 ${names[0]},可以用的:${platformNames()}`)
|
|
311
|
-
}
|
|
312
|
-
if (!PLATFORMS[names[0]].setup) {
|
|
313
|
-
throw new UsageError(
|
|
314
|
-
`${PLATFORMS[names[0]].name} 的访问密钥在它自己的插件配置里填,不走 kioku login`,
|
|
315
|
-
)
|
|
316
|
-
}
|
|
317
|
-
return { ...options, platform: names[0] }
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
export async function login(
|
|
321
|
-
argv,
|
|
322
|
-
{
|
|
323
|
-
env = process.env,
|
|
324
|
-
out = process.stdout,
|
|
325
|
-
cwd = process.cwd(),
|
|
326
|
-
stdin = process.stdin,
|
|
327
|
-
open,
|
|
328
|
-
sleep,
|
|
329
|
-
now,
|
|
330
|
-
clientName,
|
|
331
|
-
} = {},
|
|
332
|
-
) {
|
|
333
|
-
const options = parseLoginArgs(argv)
|
|
334
|
-
const platform = PLATFORMS[options.platform]
|
|
335
|
-
const saved = await Promise.resolve(platform.readConfig?.(env)).catch(() => null)
|
|
336
|
-
const url = resolveServerUrl(options, env, saved)
|
|
337
|
-
// 申请之前先看这个平台的配置写不写得进去、有没有冲突:访问密钥只在签发时给一次
|
|
338
|
-
await platform.preflight?.({ env, cwd })
|
|
339
|
-
const conflictStatus = await ensureNoConflicts(options.platform, platform.name, {
|
|
340
|
-
env,
|
|
341
|
-
cwd,
|
|
342
|
-
out,
|
|
343
|
-
stdin,
|
|
344
|
-
remove: options.removeConflicts,
|
|
345
|
-
})
|
|
346
|
-
if (conflictStatus === 'cancelled') return 0
|
|
347
|
-
const key = await deviceLogin(
|
|
348
|
-
{ url, runtimeFamily: platform.runtimeFamily },
|
|
349
|
-
{ out, open, browser: options.browser, sleep, now, ...(clientName ? { clientName } : {}) },
|
|
350
|
-
)
|
|
351
|
-
// 拿到的访问密钥交给这个平台的 setup:核对、写文件都照它的规矩
|
|
352
|
-
try {
|
|
353
|
-
return await configure(options.platform, { url, key }, { env, out, cwd }, { hints: true })
|
|
354
|
-
} catch (err) {
|
|
355
|
-
if (!(err instanceof KiokuFailure)) throw err
|
|
356
|
-
// 访问密钥已经签发、却没有写下来:说清楚,别让它悄悄留着
|
|
357
|
-
throw new KiokuFailure(`${err.message}。${orphanHint()}`)
|
|
358
|
-
}
|
|
359
|
-
}
|
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
|
-
}
|