@kioku-memory/plugin-opencode 0.6.2-edge.20261129034607.gd35e667 → 0.6.2-edge.20261201040109.g41fb4ec
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 +14 -13
- package/index.js +19 -10
- package/lib/command.mjs +10 -2
- package/lib/config.mjs +19 -12
- package/lib/http.mjs +33 -21
- package/lib/i18n.mjs +715 -0
- package/lib/kioku.mjs +81 -18
- package/lib/lifecycle.mjs +24 -8
- package/lib/mcp.mjs +34 -11
- package/lib/profile.mjs +34 -20
- package/lib/run.mjs +79 -68
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,31 +1,32 @@
|
|
|
1
|
-
# Kioku
|
|
1
|
+
# Kioku Plugin for OpenCode
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Connects OpenCode to Kioku long-term memory:
|
|
4
4
|
|
|
5
|
-
-
|
|
6
|
-
-
|
|
7
|
-
-
|
|
8
|
-
-
|
|
5
|
+
- Injects startup boot context via `experimental.chat.system.transform` (and re-injects after context compaction).
|
|
6
|
+
- Injects per-turn recall candidates via `chat.message` when a user submits a message.
|
|
7
|
+
- Submits the completed turn to Kioku for session summaries on `session.idle`.
|
|
8
|
+
- Automatically registers the remote `mcp.kioku` MCP server inside the plugin's `config` hook while keeping the access key strictly inside `~/.config/kioku/opencode.json` (mode `600`).
|
|
9
9
|
|
|
10
|
-
##
|
|
10
|
+
## Installation & Setup
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
Issue an access key for `OpenCode` (`opencode`) in the Kioku web console under **Settings → Clients & Keys** (or skip this step and use browser authorization below), then run:
|
|
13
13
|
|
|
14
14
|
```bash
|
|
15
15
|
npx @kioku-memory/cli setup opencode --server-url https://kioku.example.com
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
Or configure via browser authorization:
|
|
19
19
|
|
|
20
20
|
```bash
|
|
21
21
|
npx @kioku-memory/cli login opencode --server-url https://kioku.example.com
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
-
`setup opencode`
|
|
25
|
-
1. 将服务地址与访问密钥写入 `~/.config/kioku/opencode.json`(权限 `600`);
|
|
26
|
-
2. 在 `~/.config/opencode/opencode.json` 的 `plugin` 列表中加入 `@kioku-memory/plugin-opencode`(原配置备份为 `opencode.json.kioku-backup`)。
|
|
24
|
+
`setup opencode` performs two actions:
|
|
27
25
|
|
|
28
|
-
|
|
26
|
+
1. Writes the server URL and access key to `~/.config/kioku/opencode.json` (mode `600`).
|
|
27
|
+
2. Adds `@kioku-memory/plugin-opencode` to the `plugin` array in `~/.config/opencode/opencode.json` (backing up the original file to `opencode.json.kioku-backup`).
|
|
28
|
+
|
|
29
|
+
Verify the configuration:
|
|
29
30
|
|
|
30
31
|
```bash
|
|
31
32
|
npx @kioku-memory/cli doctor opencode
|
package/index.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import { randomUUID } from 'node:crypto'
|
|
8
8
|
|
|
9
9
|
import { failureText, KiokuFailure } from './lib/http.mjs'
|
|
10
|
+
import { detectLocale, tCore } from './lib/i18n.mjs'
|
|
10
11
|
import {
|
|
11
12
|
clip,
|
|
12
13
|
contextOf,
|
|
@@ -88,12 +89,13 @@ export async function createKiokuPlugin({
|
|
|
88
89
|
now = () => Date.now(),
|
|
89
90
|
stderr = (text) => process.stderr.write(`${text}\n`),
|
|
90
91
|
} = {}) {
|
|
92
|
+
const locale = detectLocale(env)
|
|
91
93
|
let profile = null
|
|
92
94
|
let profileError = null
|
|
93
95
|
try {
|
|
94
|
-
profile = await readProfile('opencode', env)
|
|
96
|
+
profile = await readProfile('opencode', env, locale)
|
|
95
97
|
} catch (err) {
|
|
96
|
-
profileError = err instanceof KiokuFailure ? err.message : failureText(err)
|
|
98
|
+
profileError = err instanceof KiokuFailure ? err.message : failureText(err, locale)
|
|
97
99
|
}
|
|
98
100
|
|
|
99
101
|
let quietUntil = 0
|
|
@@ -107,7 +109,7 @@ export async function createKiokuPlugin({
|
|
|
107
109
|
function logOnce(reason) {
|
|
108
110
|
if (loggedErrors.has(reason)) return
|
|
109
111
|
loggedErrors.add(reason)
|
|
110
|
-
stderr(
|
|
112
|
+
stderr(tCore('plugin.inactive', { reason }, locale))
|
|
111
113
|
}
|
|
112
114
|
if (profileError) logOnce(profileError)
|
|
113
115
|
|
|
@@ -146,10 +148,17 @@ export async function createKiokuPlugin({
|
|
|
146
148
|
...(Object.keys(project).length ? { project } : {}),
|
|
147
149
|
...body,
|
|
148
150
|
}
|
|
149
|
-
const res = await postEvent(
|
|
151
|
+
const res = await postEvent(
|
|
152
|
+
profile.url,
|
|
153
|
+
profile.key,
|
|
154
|
+
payload,
|
|
155
|
+
TIMEOUTS[event],
|
|
156
|
+
signal,
|
|
157
|
+
locale,
|
|
158
|
+
)
|
|
150
159
|
const json = await res.json().catch(() => null)
|
|
151
160
|
if (!json || typeof json !== 'object') {
|
|
152
|
-
throw new KiokuFailure('
|
|
161
|
+
throw new KiokuFailure(tCore('core.http.invalidResponse', undefined, locale))
|
|
153
162
|
}
|
|
154
163
|
loggedErrors.clear()
|
|
155
164
|
return contextOf(json.hostOutput) ?? null
|
|
@@ -157,7 +166,7 @@ export async function createKiokuPlugin({
|
|
|
157
166
|
if (signal?.aborted) return undefined
|
|
158
167
|
if (event === 'turn.end') reportQuietUntil = now() + QUIET_MS
|
|
159
168
|
else quietUntil = now() + QUIET_MS
|
|
160
|
-
logOnce(failureText(err))
|
|
169
|
+
logOnce(failureText(err, locale))
|
|
161
170
|
return undefined
|
|
162
171
|
}
|
|
163
172
|
}
|
|
@@ -196,7 +205,7 @@ export async function createKiokuPlugin({
|
|
|
196
205
|
}
|
|
197
206
|
}
|
|
198
207
|
} catch (err) {
|
|
199
|
-
logOnce(failureText(err))
|
|
208
|
+
logOnce(failureText(err, locale))
|
|
200
209
|
}
|
|
201
210
|
},
|
|
202
211
|
|
|
@@ -280,7 +289,7 @@ export async function createKiokuPlugin({
|
|
|
280
289
|
)
|
|
281
290
|
}
|
|
282
291
|
} catch (err) {
|
|
283
|
-
logOnce(failureText(err))
|
|
292
|
+
logOnce(failureText(err, locale))
|
|
284
293
|
}
|
|
285
294
|
},
|
|
286
295
|
|
|
@@ -293,7 +302,7 @@ export async function createKiokuPlugin({
|
|
|
293
302
|
retained.push(wrapSystemContext(text))
|
|
294
303
|
output.system.splice(0, output.system.length, ...retained)
|
|
295
304
|
} catch (err) {
|
|
296
|
-
logOnce(failureText(err))
|
|
305
|
+
logOnce(failureText(err, locale))
|
|
297
306
|
}
|
|
298
307
|
},
|
|
299
308
|
|
|
@@ -329,7 +338,7 @@ export async function createKiokuPlugin({
|
|
|
329
338
|
metadata: { kioku_injected: true, marker: RECALL_MARKER },
|
|
330
339
|
})
|
|
331
340
|
} catch (err) {
|
|
332
|
-
logOnce(failureText(err))
|
|
341
|
+
logOnce(failureText(err, locale))
|
|
333
342
|
}
|
|
334
343
|
},
|
|
335
344
|
}
|
package/lib/command.mjs
CHANGED
|
@@ -9,6 +9,7 @@ import { accessSync, constants, statSync } from 'node:fs'
|
|
|
9
9
|
import { join, win32 } from 'node:path'
|
|
10
10
|
|
|
11
11
|
import { KiokuFailure } from './http.mjs'
|
|
12
|
+
import { resolveLocale, tCore } from './i18n.mjs'
|
|
12
13
|
|
|
13
14
|
const WINDOWS_APPS_STUB = /[\\/]Microsoft[\\/]WindowsApps[\\/][^\\/]+$/i
|
|
14
15
|
|
|
@@ -88,14 +89,21 @@ function escapeArgument(value, twice) {
|
|
|
88
89
|
|
|
89
90
|
// 怎么起这个命令:{ file, args, options, shell },交给 spawn、execFile;Windows 上找不到这个命令时为 null。
|
|
90
91
|
// shell 为真表示中间隔着一层 cmd.exe,结束它要用 killTree。别的系统上原样返回,找不到由系统报 ENOENT
|
|
91
|
-
export function launch(
|
|
92
|
+
export function launch(
|
|
93
|
+
name,
|
|
94
|
+
args,
|
|
95
|
+
env,
|
|
96
|
+
{ platform = process.platform, exists = undefined, locale = undefined } = {},
|
|
97
|
+
) {
|
|
92
98
|
if (platform !== 'win32') return { file: name, args, options: {}, shell: false }
|
|
93
99
|
const file = findCommand(name, env, { exists })
|
|
94
100
|
if (!file) return null
|
|
95
101
|
if (!/\.(cmd|bat)$/i.test(file)) return { file, args, options: {}, shell: false }
|
|
96
102
|
// cmd.exe 把换行当成一行命令的结尾,后面的参数会悄悄丢掉:不起
|
|
97
103
|
if ([file, ...args].some((item) => /[\r\n]/.test(String(item)))) {
|
|
98
|
-
throw new KiokuFailure(
|
|
104
|
+
throw new KiokuFailure(
|
|
105
|
+
tCore('core.command.newlineInCmdArgs', undefined, resolveLocale(env, locale)),
|
|
106
|
+
)
|
|
99
107
|
}
|
|
100
108
|
const twice = /node_modules[\\/]\.bin[\\/][^\\/]+\.cmd$/i.test(file)
|
|
101
109
|
const line = [file.replace(META, '^$1'), ...args.map((arg) => escapeArgument(arg, twice))]
|
package/lib/config.mjs
CHANGED
|
@@ -3,17 +3,16 @@
|
|
|
3
3
|
import { readFile, utimes } from 'node:fs/promises'
|
|
4
4
|
|
|
5
5
|
import { errorKind, keyFormatError, KiokuFailure } from './http.mjs'
|
|
6
|
+
import { resolveLocale, tCore } from './i18n.mjs'
|
|
6
7
|
import { serverUrlProblem } from './profile.mjs'
|
|
7
8
|
|
|
8
|
-
const NOT_CONFIGURED =
|
|
9
|
-
'缺少 Kioku 的地址或访问密钥:在 Claude Code 里启用 kioku 插件、填好配置后新开会话,或者设置 KIOKU_SERVER_URL 与 KIOKU_API_KEY'
|
|
10
|
-
|
|
11
9
|
// 地址或访问密钥根本没有配(区别于配了但不对)
|
|
12
10
|
export class NotConfigured extends KiokuFailure {}
|
|
13
11
|
|
|
14
12
|
// 地址与访问密钥。访问密钥先读插件在会话开始时配好的 KIOKU_API_KEY_FILE,读不到再用 KIOKU_API_KEY;
|
|
15
13
|
// 两样都没有才报错,报错只写文件的位置与错误类别
|
|
16
|
-
export async function loadConfig(env) {
|
|
14
|
+
export async function loadConfig(env, locale = undefined) {
|
|
15
|
+
const loc = resolveLocale(env, locale)
|
|
17
16
|
const url = (env.KIOKU_SERVER_URL ?? '').trim().replace(/\/+$/, '')
|
|
18
17
|
const file = env.KIOKU_API_KEY_FILE
|
|
19
18
|
let key = ''
|
|
@@ -21,24 +20,32 @@ export async function loadConfig(env) {
|
|
|
21
20
|
if (file) {
|
|
22
21
|
try {
|
|
23
22
|
key = (await readFile(file, 'utf8')).trim()
|
|
24
|
-
if (!key) fileProblem = '
|
|
23
|
+
if (!key) fileProblem = tCore('core.config.emptyKeyFile', undefined, loc)
|
|
25
24
|
} catch (err) {
|
|
26
|
-
fileProblem = errorKind(err)
|
|
25
|
+
fileProblem = errorKind(err, loc)
|
|
27
26
|
}
|
|
28
27
|
// 用的时候更新修改时间:会话开始时会删掉很久没用过的访问密钥文件
|
|
29
28
|
if (key) await utimes(file, new Date(), new Date()).catch(() => {})
|
|
30
29
|
}
|
|
31
30
|
if (!key) key = (env.KIOKU_API_KEY ?? '').trim()
|
|
32
|
-
if (!url) throw new NotConfigured(
|
|
31
|
+
if (!url) throw new NotConfigured(tCore('core.config.notConfigured', undefined, loc))
|
|
33
32
|
if (!key) {
|
|
34
|
-
if (!fileProblem) throw new NotConfigured(
|
|
33
|
+
if (!fileProblem) throw new NotConfigured(tCore('core.config.notConfigured', undefined, loc))
|
|
34
|
+
throw new KiokuFailure(
|
|
35
|
+
tCore('core.config.readKeyFileFailed', { file, problem: fileProblem }, loc),
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
const urlProblem = serverUrlProblem(url, loc)
|
|
39
|
+
if (urlProblem) {
|
|
40
|
+
const subject = tCore('core.profile.urlSubject', undefined, loc)
|
|
41
|
+
const replacement = tCore('core.config.urlSubjectReplacement', undefined, loc)
|
|
35
42
|
throw new KiokuFailure(
|
|
36
|
-
|
|
43
|
+
urlProblem.startsWith(subject)
|
|
44
|
+
? `${replacement}${urlProblem.slice(subject.length)}`
|
|
45
|
+
: urlProblem,
|
|
37
46
|
)
|
|
38
47
|
}
|
|
39
|
-
const
|
|
40
|
-
if (urlProblem) throw new KiokuFailure(urlProblem.replace(/^服务地址/, 'KIOKU_SERVER_URL '))
|
|
41
|
-
const problem = keyFormatError(key)
|
|
48
|
+
const problem = keyFormatError(key, loc)
|
|
42
49
|
if (problem) throw new KiokuFailure(problem)
|
|
43
50
|
return { url, key }
|
|
44
51
|
}
|
package/lib/http.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// 钩子与 kioku 命令共用的访问密钥检查与报错。报错只给插件自己写的文字或错误类别:
|
|
2
2
|
// fetch 的报错会带上整个 Authorization 请求头,响应解析失败的文字会带上响应的开头
|
|
3
3
|
|
|
4
|
+
import { CORE_MESSAGES, getLocale, tCore } from './i18n.mjs'
|
|
5
|
+
|
|
4
6
|
// 访问密钥只能是可见的 ASCII 字符:别的字符放不进请求头,fetch 报错时会把整个请求头写进错误文字
|
|
5
7
|
const KEY = /^[\x21-\x7e]+$/
|
|
6
8
|
// 错误码(ECONNREFUSED、UND_ERR_SOCKET 这类)与错误类名(TypeError 这类)的样子
|
|
@@ -11,31 +13,31 @@ const NAME = /^[A-Z][A-Za-z]*$/
|
|
|
11
13
|
export class KiokuFailure extends Error {}
|
|
12
14
|
|
|
13
15
|
// 访问密钥的格式不对时返回原因,否则为 null
|
|
14
|
-
export function keyFormatError(key) {
|
|
15
|
-
return KEY.test(key) ? null : '
|
|
16
|
+
export function keyFormatError(key, locale = getLocale()) {
|
|
17
|
+
return KEY.test(key) ? null : tCore('core.http.keyFormatError', undefined, locale)
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
// 错误的类别:只取 code 或 name,不取文字
|
|
19
|
-
export function errorKind(err) {
|
|
21
|
+
export function errorKind(err, locale = getLocale()) {
|
|
20
22
|
if (typeof err?.code === 'string' && CODE.test(err.code)) return err.code
|
|
21
23
|
if (typeof err?.name === 'string' && NAME.test(err.name)) return err.name
|
|
22
|
-
return '
|
|
24
|
+
return tCore('core.http.unknownError', undefined, locale)
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
// fetch 失败的原因:超时,或者连不上(附 cause 的错误码)
|
|
26
|
-
export function fetchFailure(err, timeoutMs) {
|
|
28
|
+
export function fetchFailure(err, timeoutMs, locale = getLocale()) {
|
|
27
29
|
if (err?.name === 'TimeoutError' || err?.name === 'AbortError') {
|
|
28
|
-
return
|
|
30
|
+
return tCore('core.http.timeout', { seconds: timeoutMs / 1000 }, locale)
|
|
29
31
|
}
|
|
30
32
|
const code = err?.cause?.code
|
|
31
33
|
return typeof code === 'string' && CODE.test(code)
|
|
32
|
-
?
|
|
33
|
-
: '
|
|
34
|
+
? tCore('core.http.connectFailedWithCode', { code }, locale)
|
|
35
|
+
: tCore('core.http.connectFailed', undefined, locale)
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
// 收到跳转(3xx)时的报错:说明要把地址改成跳转之后的地址。请求都不跟着跳转走,访问密钥只发给配置的地址;
|
|
37
39
|
// 跳转目标只给协议与主机,路径与查询参数里可能带着令牌;跳到同一个主机、或者没给目标时不举例
|
|
38
|
-
export function redirectError(status, location, url) {
|
|
40
|
+
export function redirectError(status, location, url, locale = getLocale()) {
|
|
39
41
|
let origin
|
|
40
42
|
try {
|
|
41
43
|
const target = new URL(location ?? '', `${url}/`)
|
|
@@ -44,7 +46,9 @@ export function redirectError(status, location, url) {
|
|
|
44
46
|
} catch {
|
|
45
47
|
// 跳转目标解析不了时不举例
|
|
46
48
|
}
|
|
47
|
-
return
|
|
49
|
+
return origin
|
|
50
|
+
? tCore('core.http.redirectWithOrigin', { status, origin }, locale)
|
|
51
|
+
: tCore('core.http.redirect', { status }, locale)
|
|
48
52
|
}
|
|
49
53
|
|
|
50
54
|
export function isRedirect(status) {
|
|
@@ -52,28 +56,36 @@ export function isRedirect(status) {
|
|
|
52
56
|
}
|
|
53
57
|
|
|
54
58
|
// 要打印的失败原因:插件自己写的原样给出,别的只给类别
|
|
55
|
-
export function failureText(err) {
|
|
56
|
-
return err instanceof KiokuFailure ? err.message : errorKind(err)
|
|
59
|
+
export function failureText(err, locale = getLocale()) {
|
|
60
|
+
return err instanceof KiokuFailure ? err.message : errorKind(err, locale)
|
|
57
61
|
}
|
|
58
62
|
|
|
59
63
|
// 服务端在响应体里给的错误码(与服务端 contracts 的常量一致):
|
|
60
64
|
// database_unavailable 表示数据库尚未配置或暂时无法连接(503)
|
|
61
65
|
export const DATABASE_UNAVAILABLE_CODE = 'database_unavailable'
|
|
62
66
|
|
|
63
|
-
export const INVALID_KEY_TEXT = '
|
|
64
|
-
export const DATABASE_UNAVAILABLE_TEXT = '
|
|
67
|
+
export const INVALID_KEY_TEXT = CORE_MESSAGES['zh-CN']['core.http.invalidKey']
|
|
68
|
+
export const DATABASE_UNAVAILABLE_TEXT = CORE_MESSAGES['zh-CN']['core.http.databaseUnavailable']
|
|
69
|
+
|
|
70
|
+
export function invalidKeyText(locale = getLocale()) {
|
|
71
|
+
return tCore('core.http.invalidKey', undefined, locale)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function databaseUnavailableText(locale = getLocale()) {
|
|
75
|
+
return tCore('core.http.databaseUnavailable', undefined, locale)
|
|
76
|
+
}
|
|
65
77
|
|
|
66
78
|
// 服务端明说访问密钥不能用(401):和连不上、响应不对分开
|
|
67
79
|
export class KeyRejected extends KiokuFailure {
|
|
68
|
-
constructor() {
|
|
69
|
-
super(
|
|
80
|
+
constructor(locale = getLocale()) {
|
|
81
|
+
super(invalidKeyText(locale))
|
|
70
82
|
}
|
|
71
83
|
}
|
|
72
84
|
|
|
73
85
|
// 服务端数据库尚未配置或暂时无法连接(503 database_unavailable)
|
|
74
86
|
export class DatabaseUnavailable extends KiokuFailure {
|
|
75
|
-
constructor() {
|
|
76
|
-
super(
|
|
87
|
+
constructor(locale = getLocale()) {
|
|
88
|
+
super(databaseUnavailableText(locale))
|
|
77
89
|
}
|
|
78
90
|
}
|
|
79
91
|
|
|
@@ -84,10 +96,10 @@ export function errorCodeOf(data) {
|
|
|
84
96
|
}
|
|
85
97
|
|
|
86
98
|
// 401 与带 database_unavailable 的 503 的统一解读:返回要抛的失败,别的状态返回 null
|
|
87
|
-
export function rejectionOf(status, data) {
|
|
88
|
-
if (status === 401) return new KeyRejected()
|
|
99
|
+
export function rejectionOf(status, data, locale = getLocale()) {
|
|
100
|
+
if (status === 401) return new KeyRejected(locale)
|
|
89
101
|
if (status === 503 && errorCodeOf(data) === DATABASE_UNAVAILABLE_CODE) {
|
|
90
|
-
return new DatabaseUnavailable()
|
|
102
|
+
return new DatabaseUnavailable(locale)
|
|
91
103
|
}
|
|
92
104
|
return null
|
|
93
105
|
}
|