@kioku-memory/cli 0.0.0-stage → 0.0.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 +4 -0
- package/README.md +54 -2
- package/bin/kioku +25 -0
- package/lib/config.mjs +48 -0
- package/lib/doctor.mjs +163 -0
- package/lib/http.mjs +57 -0
- package/lib/kioku.mjs +397 -0
- package/lib/lifecycle.mjs +93 -0
- package/lib/platforms.mjs +79 -0
- package/lib/redact.mjs +91 -0
- package/lib/setup.mjs +154 -0
- package/lib/usage.mjs +2 -0
- package/package.json +29 -4
package/lib/setup.mjs
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// kioku setup:停用平台自带的长期记忆,由 Kioku 接管。只改平台的用户设置里的那几项,
|
|
2
|
+
// 改之前显示要改什么、留一份备份;--undo 只把这几项改回去,不动设置里的其他内容(文件会按 JSON 重新排版)
|
|
3
|
+
|
|
4
|
+
import { randomUUID } from 'node:crypto'
|
|
5
|
+
import { constants } from 'node:fs'
|
|
6
|
+
import { copyFile, mkdir, readFile, realpath, rename, rm, stat, writeFile } from 'node:fs/promises'
|
|
7
|
+
import { dirname } from 'node:path'
|
|
8
|
+
|
|
9
|
+
import { errorKind, KiokuFailure } from './http.mjs'
|
|
10
|
+
import { platformNames, PLATFORMS } from './platforms.mjs'
|
|
11
|
+
import { UsageError } from './usage.mjs'
|
|
12
|
+
|
|
13
|
+
export const SETUP_USAGE = `用法:kioku setup <平台> [--dry-run] [--undo]
|
|
14
|
+
|
|
15
|
+
停用平台自带的长期记忆,由 Kioku 接管;只改平台的用户设置里和它有关的几项。
|
|
16
|
+
|
|
17
|
+
<平台> ${platformNames()}
|
|
18
|
+
--dry-run 只显示要改什么,不写文件
|
|
19
|
+
--undo 把这几项改回默认,重新启用平台自带的长期记忆
|
|
20
|
+
|
|
21
|
+
第一次改之前把原来的设置文件备份成同目录下的 <文件名>.kioku-backup(已有备份时不覆盖);
|
|
22
|
+
设置文件会按 JSON 重新排版。改完新开会话生效。本地已有的记忆文件不会被删掉。
|
|
23
|
+
`
|
|
24
|
+
|
|
25
|
+
export function parseSetupArgs(argv) {
|
|
26
|
+
const options = { dryRun: false, undo: false }
|
|
27
|
+
const names = []
|
|
28
|
+
for (const arg of argv) {
|
|
29
|
+
if (arg === '--dry-run') options.dryRun = true
|
|
30
|
+
else if (arg === '--undo') options.undo = true
|
|
31
|
+
else if (arg.startsWith('-')) throw new UsageError(`不认识的选项 ${arg}`)
|
|
32
|
+
else names.push(arg)
|
|
33
|
+
}
|
|
34
|
+
if (names.length !== 1) throw new UsageError('要写一个平台')
|
|
35
|
+
if (!Object.hasOwn(PLATFORMS, names[0])) {
|
|
36
|
+
throw new UsageError(`不认识的平台 ${names[0]},可以用的:${platformNames()}`)
|
|
37
|
+
}
|
|
38
|
+
return { ...options, platform: names[0] }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 读设置文件:没有这个文件、或者文件是空的,按空的设置;内容不是 JSON 对象时不动它,报错。
|
|
42
|
+
// text 是读到的原文,没有这个文件时为 null
|
|
43
|
+
export async function readSettings(file) {
|
|
44
|
+
let text
|
|
45
|
+
try {
|
|
46
|
+
text = await readFile(file, 'utf8')
|
|
47
|
+
} catch (err) {
|
|
48
|
+
if (err?.code === 'ENOENT') return { settings: {}, text: null }
|
|
49
|
+
throw new KiokuFailure(`无法读取 ${file}(${errorKind(err)})`)
|
|
50
|
+
}
|
|
51
|
+
// 开头的 BOM 不算内容
|
|
52
|
+
const body = text.replace(/^\uFEFF/, '')
|
|
53
|
+
if (!body.trim()) return { settings: {}, text }
|
|
54
|
+
let settings
|
|
55
|
+
try {
|
|
56
|
+
settings = JSON.parse(body)
|
|
57
|
+
} catch {
|
|
58
|
+
settings = null
|
|
59
|
+
}
|
|
60
|
+
if (settings === null || typeof settings !== 'object' || Array.isArray(settings)) {
|
|
61
|
+
throw new KiokuFailure(`${file} 不是 JSON 对象,没有改动它`)
|
|
62
|
+
}
|
|
63
|
+
return { settings, text }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 沿用原文件的缩进(取第一处,没有就用两个空格)、换行符和结尾有没有换行;别的排版按 JSON 的默认写法
|
|
67
|
+
function serialize(settings, text) {
|
|
68
|
+
const indent = /\n([ \t]+)"/.exec(text ?? '')?.[1] ?? ' '
|
|
69
|
+
const eol = text?.includes('\r\n') ? '\r\n' : '\n'
|
|
70
|
+
const tail = text === null || !text.trim() || /\n$/.test(text) ? eol : ''
|
|
71
|
+
return JSON.stringify(settings, null, indent).replaceAll('\n', eol) + tail
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function shown(value) {
|
|
75
|
+
return value === undefined ? '(未设置)' : JSON.stringify(value)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// 写回设置文件,返回这次有没有留备份。
|
|
79
|
+
// - 设置文件是符号链接时改它指向的文件,不把链接换成普通文件。
|
|
80
|
+
// - 先写临时文件再改名:写到一半被打断时原来的设置还在;失败时临时文件删掉。
|
|
81
|
+
// - 改名之前再读一次,和开始时读到的不一样就不写:别的程序刚改过,写下去会盖掉它的改动。
|
|
82
|
+
// - 备份只留第一份:之后再改时不覆盖,最早的原文件才留得住。
|
|
83
|
+
// 权限沿用原文件的,新建的只给本人
|
|
84
|
+
export async function writeSettings(file, settings, text) {
|
|
85
|
+
let target = file
|
|
86
|
+
let temp
|
|
87
|
+
try {
|
|
88
|
+
if (text === null) await mkdir(dirname(file), { recursive: true })
|
|
89
|
+
else target = await realpath(file)
|
|
90
|
+
const mode = text === null ? 0o600 : (await stat(target)).mode & 0o777
|
|
91
|
+
temp = `${target}.${randomUUID()}.tmp`
|
|
92
|
+
await writeFile(temp, serialize(settings, text), { mode, flag: 'wx' })
|
|
93
|
+
const current = await readFile(target, 'utf8').catch((err) => {
|
|
94
|
+
if (err?.code === 'ENOENT') return null
|
|
95
|
+
throw err
|
|
96
|
+
})
|
|
97
|
+
if (current !== text) {
|
|
98
|
+
throw new KiokuFailure(`${file} 刚被别的程序改过,没有写入:重新运行一次`)
|
|
99
|
+
}
|
|
100
|
+
let backedUp = false
|
|
101
|
+
if (text !== null) {
|
|
102
|
+
try {
|
|
103
|
+
await copyFile(target, `${file}.kioku-backup`, constants.COPYFILE_EXCL)
|
|
104
|
+
backedUp = true
|
|
105
|
+
} catch (err) {
|
|
106
|
+
if (err?.code !== 'EEXIST') throw err
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
await rename(temp, target)
|
|
110
|
+
return backedUp
|
|
111
|
+
} catch (err) {
|
|
112
|
+
if (temp) await rm(temp, { force: true }).catch(() => {})
|
|
113
|
+
if (err instanceof KiokuFailure) throw err
|
|
114
|
+
throw new KiokuFailure(`无法写入 ${file}(${errorKind(err)})`)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export async function setup(
|
|
119
|
+
argv,
|
|
120
|
+
{ env = process.env, out = process.stdout, cwd = process.cwd() } = {},
|
|
121
|
+
) {
|
|
122
|
+
const { platform: id, dryRun, undo } = parseSetupArgs(argv)
|
|
123
|
+
const platform = PLATFORMS[id]
|
|
124
|
+
const { memory } = platform
|
|
125
|
+
const file = platform.settingsFile(env)
|
|
126
|
+
const { settings, text } = await readSettings(file)
|
|
127
|
+
const next = undo ? memory.restore(settings) : memory.disable(settings)
|
|
128
|
+
const changes = memory.changed.filter(
|
|
129
|
+
(key) => JSON.stringify(settings[key]) !== JSON.stringify(next[key]),
|
|
130
|
+
)
|
|
131
|
+
const goal = undo ? '已启用' : '已停用'
|
|
132
|
+
if (!changes.length) {
|
|
133
|
+
out.write(`${platform.name} 的${memory.name}${goal},${file} 不用改\n`)
|
|
134
|
+
} else {
|
|
135
|
+
out.write(`${file}\n`)
|
|
136
|
+
for (const key of changes) {
|
|
137
|
+
out.write(` ${key}:${shown(settings[key])} → ${shown(next[key])}\n`)
|
|
138
|
+
}
|
|
139
|
+
if (dryRun) {
|
|
140
|
+
out.write('没有写入(--dry-run)\n')
|
|
141
|
+
return 0
|
|
142
|
+
}
|
|
143
|
+
const backedUp = await writeSettings(file, next, text)
|
|
144
|
+
if (backedUp) out.write(`原来的设置备份在 ${file}.kioku-backup\n`)
|
|
145
|
+
else if (text !== null) out.write(`${file}.kioku-backup 已有更早的备份,没有覆盖\n`)
|
|
146
|
+
out.write(`${platform.name} 的${memory.name}${goal},新开会话生效\n`)
|
|
147
|
+
}
|
|
148
|
+
// 别处也停用着时,改回设置并不能重新启用它;别处又启用了时,停用也不生效
|
|
149
|
+
const by = undo ? memory.disabledBy(next, env) : null
|
|
150
|
+
if (by) out.write(`注意:${by}仍然停用着它\n`)
|
|
151
|
+
const again = undo ? null : await memory.enabledBy(env, cwd)
|
|
152
|
+
if (again) out.write(`注意:${again}又启用了它,要在那里一起改\n`)
|
|
153
|
+
return 0
|
|
154
|
+
}
|
package/lib/usage.mjs
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kioku-memory/cli",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Kioku 的命令行:把凭据交给命令(kioku run)、停用各平台自带的长期记忆(kioku setup)、检查配置(kioku doctor)",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"homepage": "https://github.com/lonelyman0108/kioku-plugins/tree/main/packages/cli",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/lonelyman0108/kioku-plugins.git",
|
|
10
|
+
"directory": "packages/cli"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"kioku",
|
|
14
|
+
"memory",
|
|
15
|
+
"agent"
|
|
16
|
+
],
|
|
17
|
+
"bin": {
|
|
18
|
+
"kioku": "bin/kioku"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"bin",
|
|
22
|
+
"lib",
|
|
23
|
+
"NOTICE"
|
|
24
|
+
],
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=18"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
}
|
|
31
|
+
}
|