@kioku-memory/plugin-opencode 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +10 -0
- package/README.md +31 -2
- package/index.js +336 -0
- package/lib/agy.mjs +181 -0
- package/lib/api.mjs +38 -0
- package/lib/clean.mjs +96 -0
- package/lib/codex.mjs +320 -0
- package/lib/command.mjs +152 -0
- package/lib/config.mjs +50 -0
- package/lib/conflicts.mjs +1303 -0
- package/lib/connect.mjs +152 -0
- package/lib/doctor.mjs +158 -0
- package/lib/dsh.mjs +82 -0
- package/lib/files.mjs +92 -0
- package/lib/gemini.mjs +199 -0
- package/lib/hermes.mjs +276 -0
- package/lib/http.mjs +57 -0
- package/lib/import.mjs +301 -0
- package/lib/install.mjs +196 -0
- package/lib/kioku.mjs +451 -0
- package/lib/lifecycle.mjs +128 -0
- package/lib/local.mjs +294 -0
- package/lib/login.mjs +359 -0
- package/lib/mcp.mjs +142 -0
- package/lib/openclaw.mjs +256 -0
- package/lib/opencode.mjs +144 -0
- package/lib/panel.mjs +311 -0
- package/lib/pi.mjs +90 -0
- package/lib/platforms.mjs +297 -0
- package/lib/profile.mjs +100 -0
- package/lib/prompts.mjs +38 -0
- package/lib/redact.mjs +91 -0
- package/lib/secret.mjs +86 -0
- package/lib/setup.mjs +139 -0
- package/lib/sources.mjs +333 -0
- package/lib/target.mjs +62 -0
- package/lib/text.mjs +14 -0
- package/lib/usage.mjs +2 -0
- package/lib/wizard.mjs +534 -0
- package/package.json +18 -4
package/lib/openclaw.mjs
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
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/opencode.mjs
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// OpenCode 这个平台的 kioku setup 与 kioku doctor。
|
|
2
|
+
// OpenCode 没有自带的长期记忆要停用,插件会在 config 钩子里按需注册 Kioku 的远程 MCP 服务,
|
|
3
|
+
// 所以 setup 做两件事:把服务地址与访问密钥写进本机的文件(只给本人读写),并在 opencode.json 的 plugin 列表里登记插件
|
|
4
|
+
|
|
5
|
+
import { homedir } from 'node:os'
|
|
6
|
+
import { join } from 'node:path'
|
|
7
|
+
|
|
8
|
+
import { readSettings, writeSettings } from './files.mjs'
|
|
9
|
+
import { KiokuFailure } from './http.mjs'
|
|
10
|
+
import { profileFile, readProfile, removeProfile, writeProfile } from './profile.mjs'
|
|
11
|
+
import { resolveTarget } from './target.mjs'
|
|
12
|
+
|
|
13
|
+
export const OPENCODE_PLUGIN = '@kioku-memory/plugin-opencode'
|
|
14
|
+
|
|
15
|
+
export function opencodeHome(env) {
|
|
16
|
+
if (env.OPENCODE_CONFIG_DIR) return env.OPENCODE_CONFIG_DIR
|
|
17
|
+
return join(env.XDG_CONFIG_HOME || join(env.HOME || homedir(), '.config'), 'opencode')
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function opencodeConfigFile(env) {
|
|
21
|
+
return join(opencodeHome(env), 'opencode.json')
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isKiokuPlugin(entry) {
|
|
25
|
+
if (typeof entry !== 'string') return false
|
|
26
|
+
return entry.includes('plugin-opencode') || entry.includes('kioku-plugins')
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function pluginInstalled(settings) {
|
|
30
|
+
return Array.isArray(settings?.plugin) && settings.plugin.some(isKiokuPlugin)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function checkPluginShape(settings, file) {
|
|
34
|
+
if (settings.plugin !== undefined && !Array.isArray(settings.plugin)) {
|
|
35
|
+
throw new KiokuFailure(`${file} 里的 plugin 不是数组,没有改动它`)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function ensurePlugin(settings, add) {
|
|
40
|
+
const list = Array.isArray(settings?.plugin) ? [...settings.plugin] : []
|
|
41
|
+
if (add) {
|
|
42
|
+
if (list.some(isKiokuPlugin)) return settings
|
|
43
|
+
return { ...settings, plugin: [...list, OPENCODE_PLUGIN] }
|
|
44
|
+
}
|
|
45
|
+
if (!list.some(isKiokuPlugin)) return settings
|
|
46
|
+
const filtered = list.filter((entry) => !isKiokuPlugin(entry))
|
|
47
|
+
const next = { ...settings }
|
|
48
|
+
if (filtered.length) next.plugin = filtered
|
|
49
|
+
else delete next.plugin
|
|
50
|
+
return next
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export async function preflightOpenCode({ env }) {
|
|
54
|
+
const file = opencodeConfigFile(env)
|
|
55
|
+
const { settings } = await readSettings(file)
|
|
56
|
+
checkPluginShape(settings, file)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export async function setupOpenCode(options, io) {
|
|
60
|
+
const { env, out } = io
|
|
61
|
+
const profile = profileFile('opencode', env)
|
|
62
|
+
const file = opencodeConfigFile(env)
|
|
63
|
+
|
|
64
|
+
if (options.undo) {
|
|
65
|
+
const { settings, text } = await readSettings(file)
|
|
66
|
+
checkPluginShape(settings, file)
|
|
67
|
+
if (pluginInstalled(settings)) {
|
|
68
|
+
const next = ensurePlugin(settings, false)
|
|
69
|
+
out.write(`${file}:从 plugin 中移除 ${OPENCODE_PLUGIN}\n`)
|
|
70
|
+
if (!options.dryRun) await writeSettings(file, next, text)
|
|
71
|
+
} else if (text !== null) {
|
|
72
|
+
out.write(`${file} 里没有登记 Kioku 插件,不用改\n`)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (options.dryRun) out.write(`${profile}:删掉\n没有写入(--dry-run)\n`)
|
|
76
|
+
else if (await removeProfile('opencode', env)) out.write(`已删掉 ${profile}\n`)
|
|
77
|
+
else out.write(`${profile} 本来就没有\n`)
|
|
78
|
+
return 0
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
await preflightOpenCode({ env })
|
|
82
|
+
const { url, key, saved } = await resolveTarget({ id: 'opencode', name: 'OpenCode' }, options, {
|
|
83
|
+
...io,
|
|
84
|
+
beforeLogin: () => preflightOpenCode({ env }),
|
|
85
|
+
})
|
|
86
|
+
const sameProfile = Boolean(saved && saved.url === url && saved.key === key)
|
|
87
|
+
if (sameProfile) out.write(`${profile} 不用改\n`)
|
|
88
|
+
else out.write(`${profile}:写入服务地址与访问密钥(只给本人读写)\n`)
|
|
89
|
+
|
|
90
|
+
const { settings, text } = await readSettings(file)
|
|
91
|
+
checkPluginShape(settings, file)
|
|
92
|
+
const needPlugin = !pluginInstalled(settings)
|
|
93
|
+
if (needPlugin) out.write(`${file}:在 plugin 中加入 ${OPENCODE_PLUGIN}\n`)
|
|
94
|
+
else out.write(`${file} 不用改\n`)
|
|
95
|
+
|
|
96
|
+
if (options.dryRun) {
|
|
97
|
+
out.write('没有写入(--dry-run)\n')
|
|
98
|
+
return 0
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!sameProfile) await writeProfile('opencode', { url, key }, env)
|
|
102
|
+
if (needPlugin) {
|
|
103
|
+
const backedUp = await writeSettings(file, ensurePlugin(settings, true), text)
|
|
104
|
+
if (backedUp) out.write(`原配置已备份到 ${file}.kioku-backup\n`)
|
|
105
|
+
}
|
|
106
|
+
out.write('OpenCode 已配好,重启 OpenCode 后生效。\n')
|
|
107
|
+
return 0
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export async function inspectOpenCode({ env, cwd }, { ok, warn }) {
|
|
111
|
+
const profile = await readProfile('opencode', env).catch(() => null)
|
|
112
|
+
if (!profile) {
|
|
113
|
+
warn('没有 kioku setup opencode 写下的地址与访问密钥,插件不会生效:运行 kioku setup opencode')
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const user = await readSettings(opencodeConfigFile(env))
|
|
117
|
+
const local = await readSettings(join(cwd, 'opencode.json')).catch(() => ({}))
|
|
118
|
+
if (pluginInstalled(user.settings) || pluginInstalled(local.settings)) {
|
|
119
|
+
ok('OpenCode 里登记了 kioku 插件')
|
|
120
|
+
} else {
|
|
121
|
+
warn(
|
|
122
|
+
`OpenCode 里没有登记 kioku 插件:运行 kioku setup opencode,或在 opencode.json 中加入 ${OPENCODE_PLUGIN}`,
|
|
123
|
+
)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
for (const [name, cfg] of [
|
|
127
|
+
[opencodeConfigFile(env), user.settings],
|
|
128
|
+
[join(cwd, 'opencode.json'), local.settings],
|
|
129
|
+
]) {
|
|
130
|
+
if (cfg?.mcp && typeof cfg.mcp === 'object' && Object.hasOwn(cfg.mcp, 'kioku')) {
|
|
131
|
+
warn(`${name} 里自己写了 mcp.kioku,它会覆盖插件注册的地址与访问密钥:建议去掉它`)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
ok('OpenCode 没有自带的长期记忆要停用')
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export async function statusOpenCode({ env, cwd }) {
|
|
138
|
+
const user = await readSettings(opencodeConfigFile(env)).catch(() => ({}))
|
|
139
|
+
const local = await readSettings(join(cwd, 'opencode.json')).catch(() => ({}))
|
|
140
|
+
return {
|
|
141
|
+
plugin: pluginInstalled(user.settings) || pluginInstalled(local.settings),
|
|
142
|
+
memory: 'none',
|
|
143
|
+
}
|
|
144
|
+
}
|