@kioku-memory/cli 0.3.0 → 0.4.1
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/README.md +63 -23
- package/assets/plugins/agy/README.md +34 -0
- package/assets/plugins/agy/bin/kioku +36 -0
- package/assets/plugins/agy/hooks/forward.mjs +232 -0
- package/assets/plugins/agy/hooks.json +25 -0
- package/assets/plugins/agy/lib/kioku.mjs +53 -0
- package/{lib/kioku.mjs → assets/plugins/agy/lib/run.mjs} +4 -83
- package/assets/plugins/agy/package.json +7 -0
- package/assets/plugins/agy/plugin.json +6 -0
- package/assets/plugins/agy/vitest.config.mjs +5 -0
- package/assets/plugins/gemini/README.md +33 -0
- package/assets/plugins/gemini/bin/kioku +36 -0
- package/assets/plugins/gemini/gemini-extension.json +5 -0
- package/assets/plugins/gemini/hooks/forward.mjs +174 -0
- package/assets/plugins/gemini/hooks/hooks.json +43 -0
- package/assets/plugins/gemini/lib/command.mjs +152 -0
- package/assets/plugins/gemini/lib/config.mjs +50 -0
- package/assets/plugins/gemini/lib/http.mjs +57 -0
- package/assets/plugins/gemini/lib/kioku.mjs +53 -0
- package/assets/plugins/gemini/lib/lifecycle.mjs +128 -0
- package/assets/plugins/gemini/lib/mcp.mjs +142 -0
- package/assets/plugins/gemini/lib/profile.mjs +100 -0
- package/assets/plugins/gemini/lib/redact.mjs +91 -0
- package/assets/plugins/gemini/lib/run.mjs +372 -0
- package/assets/plugins/gemini/lib/text.mjs +14 -0
- package/assets/plugins/gemini/lib/usage.mjs +2 -0
- package/assets/plugins/gemini/package.json +7 -0
- package/assets/plugins/gemini/vitest.config.mjs +5 -0
- package/bin/kioku +16 -12
- package/dist/index.mjs +8846 -0
- package/package.json +14 -5
- package/lib/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 → assets/plugins/agy/lib}/command.mjs +0 -0
- /package/{lib → assets/plugins/agy/lib}/config.mjs +0 -0
- /package/{lib → assets/plugins/agy/lib}/http.mjs +0 -0
- /package/{lib → assets/plugins/agy/lib}/lifecycle.mjs +0 -0
- /package/{lib → assets/plugins/agy/lib}/mcp.mjs +0 -0
- /package/{lib → assets/plugins/agy/lib}/profile.mjs +0 -0
- /package/{lib → assets/plugins/agy/lib}/redact.mjs +0 -0
- /package/{lib → assets/plugins/agy/lib}/text.mjs +0 -0
- /package/{lib → assets/plugins/agy/lib}/usage.mjs +0 -0
package/lib/opencode.mjs
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
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
|
-
}
|
package/lib/panel.mjs
DELETED
|
@@ -1,311 +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 { conflictToolsText, scanConflicts } from './conflicts.mjs'
|
|
9
|
-
import { failureText, fetchFailure, isRedirect, KiokuFailure } from './http.mjs'
|
|
10
|
-
import { countLocal } from './import.mjs'
|
|
11
|
-
import { PLATFORMS } from './platforms.mjs'
|
|
12
|
-
import { serverUrlProblem } from './profile.mjs'
|
|
13
|
-
import { plain, shownUrl } from './text.mjs'
|
|
14
|
-
|
|
15
|
-
const TIMEOUT = 5000
|
|
16
|
-
|
|
17
|
-
async function getJson(url, path) {
|
|
18
|
-
let res
|
|
19
|
-
let data
|
|
20
|
-
try {
|
|
21
|
-
res = await fetch(`${url}${path}`, { redirect: 'manual', signal: AbortSignal.timeout(TIMEOUT) })
|
|
22
|
-
data = await res.json().catch(() => null)
|
|
23
|
-
} catch (err) {
|
|
24
|
-
throw new KiokuFailure(fetchFailure(err, TIMEOUT))
|
|
25
|
-
}
|
|
26
|
-
if (isRedirect(res.status)) throw new KiokuFailure(`返回了跳转(${res.status})`)
|
|
27
|
-
return { status: res.status, data }
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// 服务器的现状:{ reachable, version, problem, needsSetup }。不要访问密钥
|
|
31
|
-
export async function serverHealth(url) {
|
|
32
|
-
let answer
|
|
33
|
-
try {
|
|
34
|
-
answer = await getJson(url, '/v1/health')
|
|
35
|
-
} catch (err) {
|
|
36
|
-
return { reachable: false, problem: failureText(err) }
|
|
37
|
-
}
|
|
38
|
-
const version = typeof answer.data?.version === 'string' ? plain(answer.data.version) : null
|
|
39
|
-
if (answer.status === 503) {
|
|
40
|
-
return { reachable: true, version, problem: '数据库暂时无法连接' }
|
|
41
|
-
}
|
|
42
|
-
if (answer.status !== 200 || answer.data?.status !== 'ok') {
|
|
43
|
-
return { reachable: false, problem: `不像 Kioku(HTTP ${answer.status})` }
|
|
44
|
-
}
|
|
45
|
-
let needsSetup = false
|
|
46
|
-
try {
|
|
47
|
-
const setup = await getJson(url, '/v1/setup/status')
|
|
48
|
-
needsSetup = setup.status === 200 && setup.data?.needsSetup === true
|
|
49
|
-
} catch {
|
|
50
|
-
// 查不到按已初始化算
|
|
51
|
-
}
|
|
52
|
-
return { reachable: true, version, problem: null, needsSetup }
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
async function isDir(path) {
|
|
56
|
-
return Boolean((await stat(path).catch(() => null))?.isDirectory())
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// 各平台写下的服务地址(按平台的顺序,同一个只留一次)
|
|
60
|
-
export async function knownServers(env) {
|
|
61
|
-
const urls = []
|
|
62
|
-
for (const platform of Object.values(PLATFORMS)) {
|
|
63
|
-
const config = await Promise.resolve(platform.readConfig?.(env)).catch(() => null)
|
|
64
|
-
if (config?.url && !urls.includes(config.url)) urls.push(config.url)
|
|
65
|
-
}
|
|
66
|
-
return urls
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// 没有指明时用哪个服务地址:KIOKU_SERVER_URL,否则各平台写下的里面最先找到的
|
|
70
|
-
export async function defaultServerUrl(env) {
|
|
71
|
-
const fromEnv = (env.KIOKU_SERVER_URL ?? '').trim().replace(/\/+$/, '')
|
|
72
|
-
if (fromEnv && !serverUrlProblem(fromEnv)) return fromEnv
|
|
73
|
-
return (await knownServers(env))[0] ?? null
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// 一个平台的现状。connection:
|
|
77
|
-
// - ok:访问密钥可用;none:没配过;invalid:服务端说访问密钥无效或已吊销
|
|
78
|
-
// - unknown:连不上、响应不对,查不了;broken:配置文件读不懂
|
|
79
|
-
// - manual:Claude Code,访问密钥在它的 /plugin 里填,这里读不到
|
|
80
|
-
export async function scanPlatform(id, { env, cwd, url }) {
|
|
81
|
-
const platform = PLATFORMS[id]
|
|
82
|
-
const present = hasCommand(platform.command(env), env) || (await isDir(platform.home(env)))
|
|
83
|
-
const row = { id, name: platform.name, present, connection: 'none' }
|
|
84
|
-
|
|
85
|
-
let config = null
|
|
86
|
-
if (!platform.readConfig) row.connection = 'manual'
|
|
87
|
-
else {
|
|
88
|
-
try {
|
|
89
|
-
config = await platform.readConfig(env)
|
|
90
|
-
} catch (err) {
|
|
91
|
-
row.connection = 'broken'
|
|
92
|
-
row.reason = failureText(err)
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
if (config) {
|
|
96
|
-
row.url = config.url
|
|
97
|
-
row.otherServer = Boolean(url && config.url !== url)
|
|
98
|
-
try {
|
|
99
|
-
row.who = await whoami(config)
|
|
100
|
-
row.connection = 'ok'
|
|
101
|
-
} catch (err) {
|
|
102
|
-
row.connection = err instanceof KeyRejected ? 'invalid' : 'unknown'
|
|
103
|
-
row.reason = failureText(err)
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const status = await platform.status({ env, cwd }).catch(() => ({ plugin: null, memory: null }))
|
|
108
|
-
row.plugin = status.plugin
|
|
109
|
-
row.memory = status.memory
|
|
110
|
-
if (status.reenabledIn) row.reenabledIn = status.reenabledIn
|
|
111
|
-
|
|
112
|
-
// 只数本地有几条;和 Kioku 比(没迁的有几条)要一条一条地问,选了迁移再比
|
|
113
|
-
if (platform.source) row.local = await countLocal(platform.source, { env }).catch(() => null)
|
|
114
|
-
const conflicts = await scanConflicts(id, { env, cwd }).catch(() => [])
|
|
115
|
-
if (conflicts.length) row.conflicts = conflicts
|
|
116
|
-
return row
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// 面板的全部现状。url 为空时只扫本机
|
|
120
|
-
export async function scan({ env, cwd, url }) {
|
|
121
|
-
const server = url ? { url, ...(await serverHealth(url)) } : null
|
|
122
|
-
const platforms = await Promise.all(
|
|
123
|
-
Object.keys(PLATFORMS).map((id) => scanPlatform(id, { env, cwd, url })),
|
|
124
|
-
)
|
|
125
|
-
return { server, platforms }
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const PLUGIN_NAMES = new Map([
|
|
129
|
-
[true, '已安装'],
|
|
130
|
-
[false, '未安装'],
|
|
131
|
-
[null, '无法判断'],
|
|
132
|
-
])
|
|
133
|
-
const MEMORY_NAMES = { off: '已停用', on: '未停用', none: '无' }
|
|
134
|
-
|
|
135
|
-
// 访问密钥属于哪个空间:服务端给了空间名就用名字,否则用 id 的前 8 位
|
|
136
|
-
function spaceText(who) {
|
|
137
|
-
if (typeof who.spaceName === 'string' && who.spaceName) return plain(who.spaceName)
|
|
138
|
-
return typeof who.spaceId === 'string' ? plain(who.spaceId).slice(0, 8) : null
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// 能不能停用:配过(不管现在能不能用),或者 Claude Code 的自动记忆停着
|
|
142
|
-
export function canDisable(row) {
|
|
143
|
-
return (
|
|
144
|
-
['ok', 'invalid', 'unknown', 'broken'].includes(row.connection) ||
|
|
145
|
-
(row.connection === 'manual' && row.memory === 'off')
|
|
146
|
-
)
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// 平台接到哪一步,一个短词
|
|
150
|
-
export function connectionText(row) {
|
|
151
|
-
if (row.connection === 'manual') return row.plugin ? '插件已启用' : '未接入'
|
|
152
|
-
if (row.connection === 'ok' && row.otherServer) return '接到另一台服务器'
|
|
153
|
-
return (
|
|
154
|
-
{
|
|
155
|
-
ok: '已接入',
|
|
156
|
-
none: row.present ? '未接入' : '未安装',
|
|
157
|
-
invalid: '已失效',
|
|
158
|
-
unknown: '无法判断',
|
|
159
|
-
broken: '配置有误',
|
|
160
|
-
}[row.connection] ?? '无法判断'
|
|
161
|
-
)
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function platformLines(row) {
|
|
165
|
-
const head = connectionText(row)
|
|
166
|
-
const facts = []
|
|
167
|
-
if (row.connection === 'ok') {
|
|
168
|
-
const app = row.who.app
|
|
169
|
-
facts.push(`应用 ${plain(app.name)}(${plain(app.slug)})`)
|
|
170
|
-
const space = spaceText(row.who)
|
|
171
|
-
if (space) facts.push(`空间 ${space}`)
|
|
172
|
-
if (typeof row.who.accountName === 'string' && row.who.accountName) {
|
|
173
|
-
facts.push(`账号 ${plain(row.who.accountName)}`)
|
|
174
|
-
}
|
|
175
|
-
if (app.runtimeFamily !== PLATFORMS[row.id].runtimeFamily) facts.push('应用的运行时不对')
|
|
176
|
-
}
|
|
177
|
-
if (row.otherServer) facts.push(shownUrl(row.url))
|
|
178
|
-
if (row.reason) facts.push(row.reason)
|
|
179
|
-
const first = [`${row.name}:${head}`, ...facts].join(' · ')
|
|
180
|
-
// 没装、也没配过且没有冲突的平台只写一行
|
|
181
|
-
if (!row.present && row.connection === 'none' && !row.conflicts?.length) return [first]
|
|
182
|
-
const details = []
|
|
183
|
-
if (row.connection === 'manual') details.push('访问密钥 无法判断(在 /plugin 里填)')
|
|
184
|
-
details.push(`插件 ${PLUGIN_NAMES.get(row.plugin) ?? '无法判断'}`)
|
|
185
|
-
const reenabled = row.reenabledIn ? `(被 ./${row.reenabledIn} 重新启用)` : ''
|
|
186
|
-
details.push(`自带记忆 ${MEMORY_NAMES[row.memory] ?? '无法判断'}${reenabled}`)
|
|
187
|
-
if (row.local) {
|
|
188
|
-
// 未迁移的条数只在选过迁移、和 Kioku 比过之后才有
|
|
189
|
-
const pending = !row.pending
|
|
190
|
-
? ''
|
|
191
|
-
: row.pending.pending === null
|
|
192
|
-
? '(未迁移 无法判断)'
|
|
193
|
-
: `(未迁移 ${row.pending.pending} 条)`
|
|
194
|
-
details.push(`本地记忆 ${row.local} 条${pending}`)
|
|
195
|
-
}
|
|
196
|
-
if (row.conflicts?.length) {
|
|
197
|
-
details.push(`冲突 ${conflictToolsText(row.conflicts)}`)
|
|
198
|
-
}
|
|
199
|
-
return [first, ` ${details.join(' · ')}`]
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// 面板的文字
|
|
203
|
-
export function renderPanel(state) {
|
|
204
|
-
const lines = []
|
|
205
|
-
const { server } = state
|
|
206
|
-
if (!server) lines.push('服务器:未设置')
|
|
207
|
-
else {
|
|
208
|
-
const facts = []
|
|
209
|
-
if (!server.reachable) facts.push(`无法连接(${server.problem})`)
|
|
210
|
-
else {
|
|
211
|
-
facts.push(server.problem ?? '可连接')
|
|
212
|
-
if (server.version) facts.push(`版本 ${server.version}`)
|
|
213
|
-
if (server.needsSetup) facts.push('未初始化')
|
|
214
|
-
}
|
|
215
|
-
lines.push(`服务器:${shownUrl(server.url)}(${facts.join(' · ')})`)
|
|
216
|
-
const spaces = [
|
|
217
|
-
...new Set(
|
|
218
|
-
state.platforms
|
|
219
|
-
.filter((row) => row.connection === 'ok')
|
|
220
|
-
.map((row) => spaceText(row.who))
|
|
221
|
-
.filter(Boolean),
|
|
222
|
-
),
|
|
223
|
-
]
|
|
224
|
-
if (spaces.length > 1) lines.push(`访问密钥分属 ${spaces.length} 个空间:${spaces.join('、')}`)
|
|
225
|
-
}
|
|
226
|
-
lines.push('')
|
|
227
|
-
for (const row of state.platforms) lines.push(...platformLines(row))
|
|
228
|
-
return lines.join('\n')
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// 要接入的候选:有自己的配置、还没接到这台服务器的(失效的另算)
|
|
232
|
-
export function connectCandidates(state) {
|
|
233
|
-
return state.platforms.filter(
|
|
234
|
-
(row) =>
|
|
235
|
-
PLATFORMS[row.id].setup &&
|
|
236
|
-
row.connection !== 'invalid' &&
|
|
237
|
-
!(row.connection === 'ok' && !row.otherServer),
|
|
238
|
-
)
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
const names = (rows) => rows.map((row) => row.name).join('、')
|
|
242
|
-
|
|
243
|
-
// 面板下面列出的动作:{ value, label, hint? },按缺什么排在前面
|
|
244
|
-
export function actionsFor(state) {
|
|
245
|
-
const actions = []
|
|
246
|
-
const { server } = state
|
|
247
|
-
const ready = Boolean(server?.reachable && !server.problem && !server.needsSetup)
|
|
248
|
-
if (!server || !server.reachable) {
|
|
249
|
-
actions.push({ value: 'local', label: '本机起一个 Kioku', hint: '用 Docker' })
|
|
250
|
-
}
|
|
251
|
-
if (!server) actions.push({ value: 'server', label: '填写服务器地址' })
|
|
252
|
-
if (server?.needsSetup) {
|
|
253
|
-
actions.push({ value: 'open', label: '打开管理界面初始化', hint: '建第一个管理员' })
|
|
254
|
-
}
|
|
255
|
-
if (ready) {
|
|
256
|
-
const candidates = connectCandidates(state)
|
|
257
|
-
const present = candidates.filter((row) => row.present)
|
|
258
|
-
if (present.length) {
|
|
259
|
-
actions.push({
|
|
260
|
-
value: 'connect',
|
|
261
|
-
label: `接入 ${names(present)}(${present.length} 个未接入)`,
|
|
262
|
-
})
|
|
263
|
-
} else if (candidates.length) {
|
|
264
|
-
actions.push({ value: 'connect', label: '接入其他平台', hint: '本机没发现的' })
|
|
265
|
-
}
|
|
266
|
-
const invalid = state.platforms.filter((row) => row.connection === 'invalid')
|
|
267
|
-
if (invalid.length) {
|
|
268
|
-
actions.push({ value: 'reauth', label: `重新授权 ${names(invalid)}(访问密钥已失效)` })
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
// Claude Code 的访问密钥在它的 /plugin 里填,浏览器授权不给它发:插件没启用时列出安装与要填的地址,
|
|
272
|
-
// 顺带停用自动记忆;插件启用了、自动记忆还开着时只停用自动记忆。项目设置又启用了的,停用也没用,不提供
|
|
273
|
-
const claude = state.platforms.find((row) => row.id === 'claude-code')
|
|
274
|
-
if (claude?.present && server && claude.plugin === false) {
|
|
275
|
-
actions.push({ value: 'claude-connect', label: '接入 Claude Code', hint: '在 /plugin 里填' })
|
|
276
|
-
} else if (claude?.present && claude.memory === 'on' && !claude.reenabledIn) {
|
|
277
|
-
actions.push({ value: 'claude-memory', label: '停用 Claude Code 的自动记忆' })
|
|
278
|
-
}
|
|
279
|
-
if (ready) {
|
|
280
|
-
for (const row of state.platforms) {
|
|
281
|
-
if (!row.local) continue
|
|
282
|
-
// 迁移要这个平台的访问密钥(Claude Code 的另外问)
|
|
283
|
-
if (row.connection !== 'manual' && !(row.connection === 'ok' && !row.otherServer)) continue
|
|
284
|
-
// 比过、确实都迁了的不再提供
|
|
285
|
-
if (row.pending?.pending === 0) continue
|
|
286
|
-
const count =
|
|
287
|
-
typeof row.pending?.pending === 'number'
|
|
288
|
-
? `未迁移 ${row.pending.pending} 条`
|
|
289
|
-
: `${row.local} 条`
|
|
290
|
-
actions.push({ value: `import:${row.id}`, label: `迁移 ${row.name} 的本地记忆(${count})` })
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
const conflicted = state.platforms.filter((row) => row.conflicts?.some((c) => c.removable))
|
|
294
|
-
if (conflicted.length) {
|
|
295
|
-
actions.push({
|
|
296
|
-
value: 'clean',
|
|
297
|
-
label: '清理冲突的记忆工具',
|
|
298
|
-
hint: conflictToolsText(
|
|
299
|
-
conflicted.flatMap((row) => row.conflicts.filter((c) => c.removable)),
|
|
300
|
-
),
|
|
301
|
-
})
|
|
302
|
-
}
|
|
303
|
-
if (state.platforms.some(canDisable)) {
|
|
304
|
-
actions.push({ value: 'disable', label: '停用某个平台', hint: '恢复自带记忆、清掉配置' })
|
|
305
|
-
}
|
|
306
|
-
actions.push({ value: 'doctor', label: '检查(doctor)' })
|
|
307
|
-
if (server) actions.push({ value: 'server', label: '更换服务器地址' })
|
|
308
|
-
actions.push({ value: 'refresh', label: '刷新' })
|
|
309
|
-
actions.push({ value: 'exit', label: '退出' })
|
|
310
|
-
return actions
|
|
311
|
-
}
|
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
|
-
}
|