cortico-world-coo-doc 0.4.0 → 0.4.3
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/CHANGELOG.md +23 -0
- package/ENV_PROMPT.md +1 -1
- package/README.md +28 -10
- package/describe.json +1 -1
- package/index.js +125 -34
- package/install/install.ps1 +312 -0
- package/install/install.sh +432 -0
- package/install//345/256/211/350/243/205.cmd +11 -0
- package/install//345/256/211/350/243/205.command +6 -0
- package/install//345/256/211/350/243/205.sh +4 -0
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
# 更新说明
|
|
2
2
|
|
|
3
|
+
## 0.4.3
|
|
4
|
+
|
|
5
|
+
- 时间按本机时区显示。之前存的是 UTC,`list` 里那句「扫于」、文档开头的「快照:扫描时(…)」、`index.md` 的「刷新时间」都比本地时间早 8 小时,看着像上午扫的。
|
|
6
|
+
- 覆盖文件开头加一行记号,说明这行由说明书写、换回手边时会整份删掉。
|
|
7
|
+
- 同步时清掉已卸载扩展留在 `worlds/<id>/ENV_PROMPT.md` 的覆盖文件:那些世界已经不会加载,留着只是一堆指向不存在文件的影子。
|
|
8
|
+
|
|
9
|
+
## 0.4.2
|
|
10
|
+
|
|
11
|
+
- 库改放 Coopanion 的数据目录(`data/home/companion/data/coo-doc`),不再放扩展包目录里:从市场升级扩展时 pnpm 会替换整个包目录,放里面的 `docs/`、`state.json` 会一起丢。第一次启动会把旧位置的东西搬过去。
|
|
12
|
+
- 收进库的覆盖文件和 `docs-tool-stub.json` 改写成绝对路径。之前写的是 `docs/prompts/<id>.md`,引擎在部署目录渲染这段,那边没有这个相对路径,机器人照着找不到文件。
|
|
13
|
+
- 每次同步清掉已卸载扩展留下的旧文档:之前只写不删,卸掉的扩展还能被 `read`、`find` 读到。
|
|
14
|
+
- `docs-tool-stub.json` 每次整张重写,不再一条条增删:卸载掉的扩展不会留下同名旧条目,留手边的工具也不会带着半句描述。
|
|
15
|
+
- 扫描别人的扩展时,`dataDir` 用真实的数据目录,不再编一个 `botDir/data/<id>`。
|
|
16
|
+
- 覆盖文件里补一句 `coodoc_run` 的取法,单看这段也知道怎么取。
|
|
17
|
+
|
|
18
|
+
## 0.4.1
|
|
19
|
+
|
|
20
|
+
- 内置世界的扫描补 macOS 的 `<app>/Contents/Resources/app/node_modules`,两种打包层级都试。
|
|
21
|
+
- 安装脚本随包发在 `install/` 下:`install.ps1` 给 Windows,`install.sh` 给 macOS 和 Linux,各自带双击入口。
|
|
22
|
+
- 脚本找包改成按「同目录 → 上一层」的顺序试,zip 里的老布局(`install/` 旁边放 `coo-doc/`)和 npm 包里的新布局都认。
|
|
23
|
+
- README 的安装一节补上三个系统的入口和「不打补丁也能用」的说明。
|
|
24
|
+
- 安装器按「脚本同目录 → 上一层」找包,从 `install/` 里直接跑不会找错;copy 模式把 `describe.json`、`icon.svg`、README、CHANGELOG 和 `install/` 一起带上。
|
|
25
|
+
|
|
3
26
|
## 0.4.0
|
|
4
27
|
|
|
5
28
|
- 工具改名 `coodoc_run`,按 `<id>_<verb>` 的命名约定。
|
package/ENV_PROMPT.md
CHANGED
package/README.md
CHANGED
|
@@ -16,16 +16,20 @@
|
|
|
16
16
|
## 存到哪
|
|
17
17
|
|
|
18
18
|
```
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
<data>/coo-doc/docs/prompts/<id>.md 提示词全文
|
|
20
|
+
<data>/coo-doc/docs/prompts/<id>.template.md 模板原文(只有模板型才有)
|
|
21
|
+
<data>/coo-doc/docs/tools/<id>.md 工具说明
|
|
22
|
+
<data>/coo-doc/docs/index.md 总目录
|
|
23
|
+
<data>/coo-doc/catalog.json 常驻提示词用的小目录
|
|
24
|
+
<data>/coo-doc/state.json 谁在手边、取用记录
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
+
`<data>` 是 Coopanion 的 `data/home/companion/data`,跟别的 world 的数据放一起。库里放的是自己攒的东西,不跟着扩展包走:从市场升级扩展时 pnpm 会整个替换包目录,放在包里的 `docs/` 和 `state.json` 会一起没。旧版(0.4.0 及以前)把库写在包目录里,第一次启动会自己搬到新位置。
|
|
28
|
+
|
|
27
29
|
`catalog.json` 只放 id、名字、一句用途、字数、工具数、有没有模板;常驻提示词读它,不读 `index.md` 全文。
|
|
28
30
|
|
|
31
|
+
收进库的扩展,覆盖文件里写的是绝对路径(`<data>/coo-doc/docs/prompts/<id>.md`),因为引擎在部署目录渲染这段,相对路径在那儿找不到。
|
|
32
|
+
|
|
29
33
|
## 目录与标记
|
|
30
34
|
|
|
31
35
|
`list` 是给人看的目录,每本一行:一句「什么时候用」+ 字数 + 工具数 + 标记。
|
|
@@ -68,9 +72,21 @@
|
|
|
68
72
|
|
|
69
73
|
## 安装
|
|
70
74
|
|
|
71
|
-
在 Coopanion
|
|
75
|
+
在 Coopanion 的扩展页搜 `cortico-world` 装上,重启。配置文件里会出现 `worlds.coodoc`,默认 `enabled: false`,打开后提示词才挂上。到这一步提示词那一半就能用了,Windows、macOS、Linux 一样。
|
|
76
|
+
|
|
77
|
+
包是纯 ESM、零依赖,只用 `node:fs`、`node:path`、`node:url`,`engines` 要求 `node >= 22`,没有 `os` 限制。
|
|
78
|
+
|
|
79
|
+
**工具说明那一半**要引擎补丁,补丁脚本随包发在 `install/` 下。补丁改的是 Coopanion 自己的 `build/cortico/src/core/` 两个文件,所以脚本要能写进 app 目录,macOS 和 Linux 上通常需要 sudo。
|
|
80
|
+
|
|
81
|
+
| 系统 | 入口 | 备注 |
|
|
82
|
+
| --- | --- | --- |
|
|
83
|
+
| Windows | 双击 `install/安装.cmd`,或 `.\install.ps1 -Mode copy` | 装之前退出 Coopanion,托盘里的也要退 |
|
|
84
|
+
| macOS | 双击 `install/安装.command` | 打不开就先 `chmod +x`,或右键「打开」;装完会 ad-hoc 重签 app |
|
|
85
|
+
| Linux | `bash install/安装.sh` | 需要 bash 和 python3 |
|
|
86
|
+
|
|
87
|
+
安装器按「脚本同目录 → 上一层的 `index.js`」找包,所以从 `install/` 里直接跑也行,把 `install/` 挪出去单放也行(那就得用 `-Ext` / `--ext` 指一下)。参数:`-DryRun` / `--dry-run` 只看不动,`-SkipPatch` / `--skip-patch` 只装包不打补丁,`-Check` / `--check` 自检,`-Revert` / `--revert` 卸载并还原引擎。
|
|
72
88
|
|
|
73
|
-
|
|
89
|
+
不打补丁也能用:提示词照常收,只是工具说明还是原来那么长。
|
|
74
90
|
|
|
75
91
|
## 配置
|
|
76
92
|
|
|
@@ -91,7 +107,7 @@
|
|
|
91
107
|
| `find` | 按关键词定位 |
|
|
92
108
|
| `hot` | 留手边,带 `days` 是临时留 |
|
|
93
109
|
| `cold` | 收进库里 |
|
|
94
|
-
| `status` | 最早的目录版本,回
|
|
110
|
+
| `status` | 最早的目录版本,回 `<库>/index.md` 全文 |
|
|
95
111
|
|
|
96
112
|
## 标签
|
|
97
113
|
|
|
@@ -105,4 +121,6 @@
|
|
|
105
121
|
- 模板快照不自动更新,实时值要问工具。
|
|
106
122
|
- 新装扩展最多等 5 分钟才进库,重启立刻生效。
|
|
107
123
|
- 工具说明被换成一句之后,要看细节得 `read` 出来。
|
|
108
|
-
- macOS
|
|
124
|
+
- 安装器只在 Windows 上跑过;macOS 和 Linux 的路径探测是按 Coopanion 的常见安装位写的,没在真机验证。
|
|
125
|
+
- 内置世界的扫描看 `<app>/resources/app/node_modules` 和 macOS 的 `<app>/Contents/Resources/app/node_modules` 两处,别的打包布局不认。
|
|
126
|
+
- 旧版写在包目录里的库会在第一次启动时搬过来;旧覆盖文件里的相对路径会在下一次同步时改写成绝对路径。
|
package/describe.json
CHANGED
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"key": "worlds.coodoc.envPrompt",
|
|
62
62
|
"title": "说明书 · 环境提示词",
|
|
63
63
|
"role": "envPrompt",
|
|
64
|
-
"content": "## 说明书库\n\n正文在文件里,这里只放路径。库:`{{doc.docs}}
|
|
64
|
+
"content": "## 说明书库\n\n正文在文件里,这里只放路径。库:`{{doc.docs}}`;每本两处:`<库>/prompts/<id>.md`、`<库>/tools/<id>.md`。\n\n**手边(全文就在下面那几段里)**:{{doc.hot}}\n\n**库里(正文没贴,只有路径)**:\n- {{doc.cold}}\n\n标 ※ 的是每轮渲染的模板,库里那份只是扫描时的快照;额度、登录、位置、天气这类实时值要当场问工具。\n\n取一节:`coodoc_run(action=\"read\", doc=\"<id>\", section=\"…\")`,长节可加 `max` 放宽字数;定位:`coodoc_run(action=\"find\", q=\"…\")`;换位置:`hot` / `cold`,`hot` 带 `days` 是临时留、到期自动收回。\n"
|
|
65
65
|
}
|
|
66
66
|
]
|
|
67
67
|
}
|
package/index.js
CHANGED
|
@@ -15,25 +15,54 @@
|
|
|
15
15
|
* 装包本身不动引擎,这两处由安装包里的 install.ps1 / install.sh 打上;没打时只是收不了工具说明,提示词那半照常。
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
|
|
18
|
+
import { cpSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
|
|
19
19
|
import { dirname, join, resolve } from 'node:path'
|
|
20
20
|
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
21
21
|
|
|
22
22
|
const WORLD_ID = 'coodoc'
|
|
23
23
|
const SELF_PKG = 'cortico-world-coo-doc'
|
|
24
24
|
const SELF_DIR = dirname(fileURLToPath(import.meta.url))
|
|
25
|
-
const DOCS_DIR = join(SELF_DIR, 'docs')
|
|
26
|
-
const PROMPTS_DIR = join(DOCS_DIR, 'prompts')
|
|
27
|
-
const TOOLS_DIR = join(DOCS_DIR, 'tools')
|
|
28
|
-
const STATE_FILE = join(SELF_DIR, 'state.json')
|
|
29
25
|
const ENV_PROMPT_FILE = join(SELF_DIR, 'ENV_PROMPT.md')
|
|
30
26
|
const TOOL = 'coodoc_run'
|
|
31
27
|
|
|
28
|
+
// 库放 dataDir 下(每个 bot 一份,升级扩展不会连库一起被 pnpm 换掉);拿不到就退回包目录。
|
|
29
|
+
let DATA_DIR = SELF_DIR
|
|
30
|
+
let DOCS_DIR = join(DATA_DIR, 'docs')
|
|
31
|
+
let PROMPTS_DIR = join(DOCS_DIR, 'prompts')
|
|
32
|
+
let TOOLS_DIR = join(DOCS_DIR, 'tools')
|
|
33
|
+
let STATE_FILE = join(DATA_DIR, 'state.json')
|
|
34
|
+
let CATALOG_FILE = join(DATA_DIR, 'catalog.json')
|
|
35
|
+
|
|
32
36
|
const DEFAULTS = () => ({ enabled: false, readChars: 2400 })
|
|
33
37
|
|
|
34
38
|
let ctxRef = { cfg: DEFAULTS(), dataDir: '', botDir: '' }
|
|
35
39
|
const cfg = () => ctxRef.cfg ?? DEFAULTS()
|
|
36
40
|
|
|
41
|
+
function useDataDir(dir) {
|
|
42
|
+
DATA_DIR = dir || SELF_DIR
|
|
43
|
+
DOCS_DIR = join(DATA_DIR, 'docs')
|
|
44
|
+
PROMPTS_DIR = join(DOCS_DIR, 'prompts')
|
|
45
|
+
TOOLS_DIR = join(DOCS_DIR, 'tools')
|
|
46
|
+
STATE_FILE = join(DATA_DIR, 'state.json')
|
|
47
|
+
CATALOG_FILE = join(DATA_DIR, 'catalog.json')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 0.4.0 把库写在包目录里:第一次用新位置时把旧的搬过去,老用户升级不丢 hot 名单和文档。 */
|
|
51
|
+
function migrateOldData() {
|
|
52
|
+
if (DATA_DIR === SELF_DIR) return
|
|
53
|
+
// 新位置可能已经被建过(空目录),按文件判断要不要搬,别拿目录存不存在当信号。
|
|
54
|
+
const needDocs = !existsSync(DOCS_DIR) && existsSync(join(SELF_DIR, 'docs'))
|
|
55
|
+
if (!needDocs && existsSync(STATE_FILE)) return
|
|
56
|
+
try {
|
|
57
|
+
if (needDocs) cpSync(join(SELF_DIR, 'docs'), DOCS_DIR, { recursive: true })
|
|
58
|
+
for (const f of ['state.json', 'catalog.json']) {
|
|
59
|
+
const from = join(SELF_DIR, f)
|
|
60
|
+
const to = join(DATA_DIR, f)
|
|
61
|
+
if (existsSync(from) && !existsSync(to)) cpSync(from, to)
|
|
62
|
+
}
|
|
63
|
+
} catch { /* 搬不动就从头扫一遍,别把启动拖死 */ }
|
|
64
|
+
}
|
|
65
|
+
|
|
37
66
|
/* ───────────── 小工具 ───────────── */
|
|
38
67
|
|
|
39
68
|
function readText(p) {
|
|
@@ -61,6 +90,14 @@ function oneLine(text, n = 60) {
|
|
|
61
90
|
return s.length > n ? `${s.slice(0, n - 1)}…` : s
|
|
62
91
|
}
|
|
63
92
|
|
|
93
|
+
/** 存的是 UTC,给人看的一律按本机时区写成「2026-12-05 19:07」。 */
|
|
94
|
+
function stamp(iso) {
|
|
95
|
+
const d = iso ? new Date(iso) : new Date()
|
|
96
|
+
if (Number.isNaN(d.getTime())) return ''
|
|
97
|
+
const p = (n) => String(n).padStart(2, '0')
|
|
98
|
+
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}`
|
|
99
|
+
}
|
|
100
|
+
|
|
64
101
|
const botFile = (...parts) => join(ctxRef.botDir, ...parts)
|
|
65
102
|
|
|
66
103
|
/* ───────────── 状态:谁留手边 ───────────── */
|
|
@@ -120,8 +157,21 @@ function packageRoots() {
|
|
|
120
157
|
break
|
|
121
158
|
}
|
|
122
159
|
}
|
|
123
|
-
|
|
124
|
-
|
|
160
|
+
// 应用自带的扩展:Windows / Linux 在 <app>/resources/app,macOS 在
|
|
161
|
+
// <app>/Contents/Resources/app;打包进 app 后 path 层级也不同,所以两种都试。
|
|
162
|
+
const botDir = ctxRef.botDir
|
|
163
|
+
const appCandidates = [
|
|
164
|
+
resolve(botDir, '..', '..', '..', 'resources', 'app', 'node_modules'),
|
|
165
|
+
resolve(botDir, '..', '..', '..', '..', 'resources', 'app', 'node_modules'),
|
|
166
|
+
resolve(botDir, '..', '..', '..', 'Contents', 'Resources', 'app', 'node_modules'),
|
|
167
|
+
resolve(botDir, '..', '..', '..', '..', 'Contents', 'Resources', 'app', 'node_modules'),
|
|
168
|
+
]
|
|
169
|
+
for (const appModules of appCandidates) {
|
|
170
|
+
if (!existsSync(appModules)) continue
|
|
171
|
+
if (roots.some((r) => r.nodeModules === appModules)) continue
|
|
172
|
+
roots.push({ root: appModules, nodeModules: appModules, kind: 'builtin' })
|
|
173
|
+
break
|
|
174
|
+
}
|
|
125
175
|
return roots
|
|
126
176
|
}
|
|
127
177
|
|
|
@@ -181,7 +231,7 @@ async function readTools(pkgPath, pkg, id) {
|
|
|
181
231
|
const decl = mod.default ?? mod.world ?? mod
|
|
182
232
|
if (typeof decl?.create === 'function') {
|
|
183
233
|
const cfgStub = typeof decl.defaults === 'function' ? { ...decl.defaults() } : {}
|
|
184
|
-
const inst = decl.create({ cfg: cfgStub, dataDir: join(ctxRef.
|
|
234
|
+
const inst = decl.create({ cfg: cfgStub, dataDir: join(ctxRef.dataDir || DATA_DIR, id), botDir: ctxRef.botDir })
|
|
185
235
|
// 有的世界提示词是模板({{stroll.quota}} 这种),渲染值只有引擎知道。
|
|
186
236
|
// 顺手问一下它当前的值,抄库时能抄成一份「当时的快照」,不至于只剩 {{xxx}}。
|
|
187
237
|
let vars = null
|
|
@@ -276,7 +326,8 @@ const DEFAULT_HOOKS = {
|
|
|
276
326
|
|
|
277
327
|
/* 新装、没写钩子的:开关类给一句通用的,其余从提示词里挑一句干净的,挑不出就留空。 */
|
|
278
328
|
function hookOf(id, prompt) {
|
|
279
|
-
|
|
329
|
+
// 自己写的钩子优先放数据目录(升级不会丢);包目录里的那份留给随包带的默认值。
|
|
330
|
+
const custom = readJson(join(DATA_DIR, 'hooks.json')) ?? readJson(join(SELF_DIR, 'hooks.json'))
|
|
280
331
|
const fromFile = custom && typeof custom === 'object' ? String(custom[id] ?? '').trim() : ''
|
|
281
332
|
if (fromFile) return fromFile
|
|
282
333
|
if (DEFAULT_HOOKS[id]) return DEFAULT_HOOKS[id]
|
|
@@ -344,7 +395,7 @@ async function scan() {
|
|
|
344
395
|
/* ───────────── 文档 ───────────── */
|
|
345
396
|
|
|
346
397
|
function promptDoc(w) {
|
|
347
|
-
const at =
|
|
398
|
+
const at = stamp(w.at)
|
|
348
399
|
const lines = [
|
|
349
400
|
`<!-- 来源:${w.pkgName}@${w.version} · ${w.envFile ?? '(包里没有 ENV_PROMPT.md)'} -->`,
|
|
350
401
|
]
|
|
@@ -388,7 +439,7 @@ function toolsDoc(w) {
|
|
|
388
439
|
function indexDoc(worlds, state) {
|
|
389
440
|
const hot = worlds.filter((w) => isHot(w.id, state))
|
|
390
441
|
const cold = worlds.filter((w) => !isHot(w.id, state))
|
|
391
|
-
const row = (w) => `- ${w.label}(${w.id}${w.kind === 'builtin' ? '·自带' : ''})${w.hook ? `|${w.hook}` : ''}${w.dynamic ? '(动态快照)' : ''}:提示词 ${w.prompt.length} 字 →
|
|
442
|
+
const row = (w) => `- ${w.label}(${w.id}${w.kind === 'builtin' ? '·自带' : ''})${w.hook ? `|${w.hook}` : ''}${w.dynamic ? '(动态快照)' : ''}:提示词 ${w.prompt.length} 字 → ${join(PROMPTS_DIR, `${w.id}.md`)};工具 ${w.tools.length} 件 → ${join(TOOLS_DIR, `${w.id}.md`)}`
|
|
392
443
|
return [
|
|
393
444
|
'# 说明书目录',
|
|
394
445
|
'',
|
|
@@ -401,7 +452,7 @@ function indexDoc(worlds, state) {
|
|
|
401
452
|
cold.length ? cold.map(row).join('\n') : '(都留手边了)',
|
|
402
453
|
'',
|
|
403
454
|
'取法:`coodoc_run(action="read", doc="<id>", section="<章节号或几个字>")`;`coodoc_run(action="find", q="…")` 只回命中的行。',
|
|
404
|
-
`刷新时间:${state.syncedAt || '(还没扫过)'}`,
|
|
455
|
+
`刷新时间:${stamp(state.syncedAt) || '(还没扫过)'}`,
|
|
405
456
|
].join('\n')
|
|
406
457
|
}
|
|
407
458
|
|
|
@@ -410,47 +461,80 @@ function indexDoc(worlds, state) {
|
|
|
410
461
|
const stubFile = () => botFile('docs-tool-stub.json')
|
|
411
462
|
const overrideFile = (id) => botFile('worlds', id, 'ENV_PROMPT.md')
|
|
412
463
|
const docPaths = (id) => ({ prompt: join(PROMPTS_DIR, `${id}.md`), tools: join(TOOLS_DIR, `${id}.md`) })
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
const s = readJson(stubFile())
|
|
416
|
-
return s && typeof s === 'object' ? s : {}
|
|
417
|
-
}
|
|
464
|
+
// 覆盖文件开头的记号:谁写的、什么时候会被删,顺带让清理认得出来。
|
|
465
|
+
const OVERRIDE_MARK = '<!-- coodoc:这段由说明书库改写,换回手边时这个文件会被删掉,别手改 -->'
|
|
418
466
|
|
|
419
467
|
function writeStubs(map) {
|
|
420
468
|
writeAtomic(stubFile(), `${JSON.stringify(map, null, 2)}\n`)
|
|
421
469
|
}
|
|
422
470
|
|
|
423
471
|
function applyCold(w) {
|
|
472
|
+
const { prompt, tools } = docPaths(w.id)
|
|
473
|
+
// 路径写绝对路径:覆盖文件由引擎在部署目录渲染,相对路径在那边找不到。
|
|
424
474
|
const lines = [
|
|
475
|
+
OVERRIDE_MARK,
|
|
425
476
|
`## ${w.label}`,
|
|
426
477
|
'',
|
|
427
|
-
|
|
478
|
+
`说明和用法在库里:\`${prompt}\`(${w.prompt.length} 字)`,
|
|
428
479
|
]
|
|
429
|
-
if (w.tools.length) lines.push(
|
|
480
|
+
if (w.tools.length) lines.push(`每件工具怎么调:\`${tools}\`(${w.tools.length} 件)`)
|
|
430
481
|
lines.push(
|
|
431
482
|
'',
|
|
483
|
+
`取法:\`${TOOL}(action="read", doc="${w.id}", section="<章节号或几个字>")\`;想不起来放在哪就用 \`${TOOL}(action="find", q="…")\`。`,
|
|
432
484
|
w.dynamic
|
|
433
485
|
? '这篇是每轮渲染的模板,库里那份是扫描时的快照;额度、登录、位置、天气这类实时值要当场问工具。'
|
|
434
486
|
: '要用时按章节取,不用先读整份。',
|
|
435
487
|
)
|
|
436
488
|
writeAtomic(overrideFile(w.id), lines.join('\n'))
|
|
437
|
-
if (!w.tools.length) return
|
|
438
|
-
const map = readStubs()
|
|
439
|
-
for (const t of w.tools) map[t.name] = `${oneLine(t.use, 48)}|docs/tools/${w.id}.md#${t.name}`
|
|
440
|
-
writeStubs(map)
|
|
441
489
|
}
|
|
442
490
|
|
|
443
491
|
function applyHot(w) {
|
|
444
492
|
try { rmSync(overrideFile(w.id), { force: true }) } catch { /* 没有就算了 */ }
|
|
445
|
-
|
|
446
|
-
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/** 替身表每次整张重写:卸掉的扩展不留下同名的旧条目,留手边的也不会带着半句描述。 */
|
|
496
|
+
function rebuildStubs(worlds, state) {
|
|
497
|
+
const map = {}
|
|
498
|
+
for (const w of worlds) {
|
|
499
|
+
if (isHot(w.id, state) || !w.tools.length) continue
|
|
500
|
+
const { tools } = docPaths(w.id)
|
|
501
|
+
for (const t of w.tools) map[t.name] = `${oneLine(t.use, 48)}|${tools}#${t.name}`
|
|
502
|
+
}
|
|
447
503
|
writeStubs(map)
|
|
448
504
|
}
|
|
449
505
|
|
|
506
|
+
/** 卸载掉的扩展留在 worlds/<id>/ 里的覆盖文件也清掉:那些世界已经不会被加载,留着只是一堆找不到库的影子。 */
|
|
507
|
+
function cleanOverrides(live) {
|
|
508
|
+
const root = botFile('worlds')
|
|
509
|
+
let names = []
|
|
510
|
+
try { names = readdirSync(root, { withFileTypes: true }).filter((d) => d.isDirectory()).map((d) => d.name) } catch { return }
|
|
511
|
+
for (const id of names) {
|
|
512
|
+
if (live.has(id)) continue
|
|
513
|
+
const p = join(root, id, 'ENV_PROMPT.md')
|
|
514
|
+
const text = readText(p)
|
|
515
|
+
if (!text) continue
|
|
516
|
+
// 只认自己写的:新版带记号,0.4.3 之前的老格式认「说明和用法在库里 + docs/prompts/」。
|
|
517
|
+
const mine = text.includes(OVERRIDE_MARK) || (text.includes('说明和用法在库里') && text.includes('docs/prompts/'))
|
|
518
|
+
if (!mine) continue
|
|
519
|
+
try { rmSync(p, { force: true }) } catch { /* 删不掉就算了 */ }
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
450
523
|
async function syncAll() {
|
|
451
524
|
for (const d of [PROMPTS_DIR, TOOLS_DIR]) { try { mkdirSync(d, { recursive: true }) } catch { /* 只读包 */ } }
|
|
452
525
|
const { worlds, error } = await scan()
|
|
453
526
|
if (error) return { ok: false, message: error }
|
|
527
|
+
// 卸载掉的扩展留下的旧文档没人再写,留着会被 read/find 当成活的,扫到就清掉。
|
|
528
|
+
const live = new Set(worlds.map((w) => w.id))
|
|
529
|
+
for (const dir of [PROMPTS_DIR, TOOLS_DIR]) {
|
|
530
|
+
let names = []
|
|
531
|
+
try { names = readdirSync(dir) } catch { continue }
|
|
532
|
+
for (const name of names) {
|
|
533
|
+
if (!name.endsWith('.md')) continue
|
|
534
|
+
const id = name.replace(/\.template\.md$/, '').replace(/\.md$/, '')
|
|
535
|
+
if (!live.has(id)) { try { rmSync(join(dir, name), { force: true }) } catch { /* 删不掉就算了 */ } }
|
|
536
|
+
}
|
|
537
|
+
}
|
|
454
538
|
for (const w of worlds) {
|
|
455
539
|
writeAtomic(join(PROMPTS_DIR, `${w.id}.md`), promptDoc(w))
|
|
456
540
|
writeAtomic(join(TOOLS_DIR, `${w.id}.md`), toolsDoc(w))
|
|
@@ -481,12 +565,14 @@ async function syncAll() {
|
|
|
481
565
|
hot: worlds.filter((w) => isHot(w.id, state)).map(brief),
|
|
482
566
|
cold: worlds.filter((w) => !isHot(w.id, state)).map(brief),
|
|
483
567
|
}
|
|
484
|
-
writeAtomic(
|
|
568
|
+
writeAtomic(CATALOG_FILE, `${JSON.stringify(catalog, null, 2)}\n`)
|
|
485
569
|
// 收库里的刷覆盖与替身;留手边的顺手把以前的覆盖和替身清掉,别留尾巴。
|
|
486
570
|
for (const w of worlds) {
|
|
487
571
|
if (isHot(w.id, state)) applyHot(w)
|
|
488
572
|
else applyCold(w)
|
|
489
573
|
}
|
|
574
|
+
rebuildStubs(worlds, state)
|
|
575
|
+
cleanOverrides(live)
|
|
490
576
|
return { ok: true, worlds, state }
|
|
491
577
|
}
|
|
492
578
|
|
|
@@ -597,21 +683,21 @@ function statusText() {
|
|
|
597
683
|
|
|
598
684
|
/** list:每个扩展一行带着钩子,顺手把「这周取多了」的标出来。 */
|
|
599
685
|
function listText() {
|
|
600
|
-
const catalog = readJson(
|
|
686
|
+
const catalog = readJson(CATALOG_FILE)
|
|
601
687
|
if (!catalog) return '还没扫过,先 coodoc_run(action="scan")。'
|
|
602
688
|
const state = readState()
|
|
603
689
|
const hot = Array.isArray(catalog.hot) ? catalog.hot : []
|
|
604
690
|
const cold = Array.isArray(catalog.cold) ? catalog.cold : []
|
|
605
|
-
const at =
|
|
691
|
+
const at = stamp(catalog.at)
|
|
606
692
|
const line = (w, inLib) => {
|
|
607
693
|
const t = tempOf(w.id, state)
|
|
608
694
|
const n = readsThisWeek(w.id, state)
|
|
609
695
|
const tags = []
|
|
610
|
-
if (t) tags.push(`临时留手边,${t.until.slice(0, 10)} 收回`)
|
|
696
|
+
if (t) tags.push(`临时留手边,${stamp(t.until).slice(0, 10)} 收回`)
|
|
611
697
|
if (n >= 3) tags.push(`常用(这周取了 ${n} 次)`)
|
|
612
698
|
if (w.dynamic) tags.push('动态快照')
|
|
613
699
|
const head = `- ${w.label}(${w.id})${w.hook ? `|${w.hook}` : ''} · ${w.chars} 字 · ${w.tools} 件${tags.length ? ` · ${tags.join(' · ')}` : ''}`
|
|
614
|
-
return inLib ? `${head}\n
|
|
700
|
+
return inLib ? `${head}\n ${join(PROMPTS_DIR, `${w.id}.md`)} | ${join(TOOLS_DIR, `${w.id}.md`)}` : head
|
|
615
701
|
}
|
|
616
702
|
const tempCount = hot.filter((w) => tempOf(w.id, state)).length
|
|
617
703
|
return [
|
|
@@ -724,18 +810,23 @@ export default {
|
|
|
724
810
|
for (const key of Object.keys(DEFAULTS())) if (ctx.cfg[key] === undefined) ctx.cfg[key] = DEFAULTS()[key]
|
|
725
811
|
ctxRef.cfg = ctx.cfg
|
|
726
812
|
}
|
|
813
|
+
// 库跟着 bot 的数据目录走:扩展升级时 pnpm 会整个替换包目录,放里面的东西留不住。
|
|
814
|
+
if (ctx.dataDir) {
|
|
815
|
+
useDataDir(join(String(ctx.dataDir), 'coo-doc'))
|
|
816
|
+
migrateOldData()
|
|
817
|
+
}
|
|
727
818
|
|
|
728
819
|
return {
|
|
729
820
|
id: WORLD_ID,
|
|
730
821
|
|
|
731
822
|
envPromptVars() {
|
|
732
823
|
try {
|
|
733
|
-
const catalog = readJson(
|
|
734
|
-
if (!catalog) return { 'doc.dir':
|
|
824
|
+
const catalog = readJson(CATALOG_FILE)
|
|
825
|
+
if (!catalog) return { 'doc.dir': DATA_DIR, 'doc.docs': DOCS_DIR, 'doc.hot': '(还没扫过)', 'doc.cold': '(还没扫过)' }
|
|
735
826
|
const hot = Array.isArray(catalog.hot) ? catalog.hot : []
|
|
736
827
|
const cold = Array.isArray(catalog.cold) ? catalog.cold : []
|
|
737
828
|
return {
|
|
738
|
-
'doc.dir':
|
|
829
|
+
'doc.dir': DATA_DIR,
|
|
739
830
|
'doc.docs': DOCS_DIR,
|
|
740
831
|
'doc.hot': hot.length ? hot.map((w) => `${w.label}(${w.id})${w.hook ? `——${w.hook}` : ''}`).join('、') : '(都收进库里了)',
|
|
741
832
|
// 冷库只报 id + 钩子,路径按 <库>/prompts/<id>.md 推;少写几百字,提示词才真的短。
|
|
@@ -786,7 +877,7 @@ export default {
|
|
|
786
877
|
path: ENV_PROMPT_FILE,
|
|
787
878
|
role: 'envPrompt',
|
|
788
879
|
vars: [
|
|
789
|
-
{ name: 'doc.dir', description: '
|
|
880
|
+
{ name: 'doc.dir', description: '库里放说明书的目录。' },
|
|
790
881
|
{ name: 'doc.docs', description: '库目录(磁盘上的绝对路径,各系统分隔符不同)。' },
|
|
791
882
|
{ name: 'doc.hot', description: '留手边的扩展(它们的正文就在下面几段里)。' },
|
|
792
883
|
{ name: 'doc.cold', description: '收进库的扩展:每个一行,只有文件路径。', multiline: true },
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
# 说明书库(coodoc)一键安装 / 卸载
|
|
2
|
+
#
|
|
3
|
+
# 用法:
|
|
4
|
+
# 双击 安装.bat # 最省事:自动找 Coopanion,拷贝模式装好
|
|
5
|
+
# .\install.ps1 # 同上,命令行跑
|
|
6
|
+
# .\install.ps1 -Coopanion 'E:\Coopanion'
|
|
7
|
+
# .\install.ps1 -Mode link # 软链模式(自己还要改代码时才用)
|
|
8
|
+
# .\install.ps1 -DryRun # 只看会做什么,不动手
|
|
9
|
+
# .\install.ps1 -Check # 自检:扩展在不在、引擎补丁还在不在
|
|
10
|
+
# .\install.ps1 -Revert # 卸载并还原引擎
|
|
11
|
+
#
|
|
12
|
+
param(
|
|
13
|
+
[string]$Coopanion = '',
|
|
14
|
+
[string]$Ext = '',
|
|
15
|
+
[ValidateSet('link', 'copy')][string]$Mode = 'copy',
|
|
16
|
+
[switch]$Revert,
|
|
17
|
+
[switch]$SkipPatch,
|
|
18
|
+
[switch]$Check,
|
|
19
|
+
[switch]$DryRun
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
$ErrorActionPreference = 'Stop'
|
|
23
|
+
$PkgName = 'cortico-world-coo-doc'
|
|
24
|
+
$WorldId = 'coodoc'
|
|
25
|
+
$Utf8 = New-Object System.Text.UTF8Encoding($false)
|
|
26
|
+
|
|
27
|
+
function Say($m, $color = 'Gray') { Write-Host $m -ForegroundColor $color }
|
|
28
|
+
function Step($desc, [scriptblock]$action) {
|
|
29
|
+
if ($DryRun) { Say "[dry] $desc" 'DarkYellow'; return }
|
|
30
|
+
Say " > $desc"
|
|
31
|
+
& $action
|
|
32
|
+
}
|
|
33
|
+
function ReadJsonFile($path, $fallback = $null) {
|
|
34
|
+
if (-not (Test-Path -LiteralPath $path)) { return $fallback }
|
|
35
|
+
$raw = Get-Content -LiteralPath $path -Raw -Encoding UTF8
|
|
36
|
+
if ([string]::IsNullOrWhiteSpace($raw)) { return $fallback }
|
|
37
|
+
try { return $raw | ConvertFrom-Json } catch { throw "JSON 坏了:$path" }
|
|
38
|
+
}
|
|
39
|
+
function WriteJsonFile($path, $obj) {
|
|
40
|
+
$json = $obj | ConvertTo-Json -Depth 20
|
|
41
|
+
[System.IO.File]::WriteAllText($path, $json + "`n", $Utf8)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
# 没给路径就自己找:常见安装位 + 环境变量
|
|
45
|
+
if (-not $Coopanion) {
|
|
46
|
+
$tries = @(
|
|
47
|
+
'D:\Coopanion', 'C:\Coopanion', 'E:\Coopanion',
|
|
48
|
+
(Join-Path $env:LOCALAPPDATA 'Programs\Coopanion'),
|
|
49
|
+
(Join-Path $env:USERPROFILE 'Coopanion'),
|
|
50
|
+
(Join-Path $env:USERPROFILE 'Desktop\Coopanion')
|
|
51
|
+
)
|
|
52
|
+
foreach ($t in $tries) { if ($t -and (Test-Path -LiteralPath (Join-Path $t 'Coopanion.exe'))) { $Coopanion = $t; break } }
|
|
53
|
+
if (-not $Coopanion) { throw '没找到 Coopanion,找不到的话这样跑:.\install.ps1 -Coopanion "你的安装目录"' }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
$App = Join-Path $Coopanion 'Coopanion.exe'
|
|
57
|
+
$Core = Join-Path $Coopanion 'resources\app\build\cortico\src\core'
|
|
58
|
+
$Prefix = Join-Path $Core 'prefix.ts'
|
|
59
|
+
$Loop = Join-Path $Core 'loop.ts'
|
|
60
|
+
$ExtRoot = Join-Path $Coopanion 'data\extensions'
|
|
61
|
+
$NodeMod = Join-Path $ExtRoot "node_modules\$PkgName"
|
|
62
|
+
$Deploy = Join-Path $Coopanion 'data\home\companion'
|
|
63
|
+
$Owners = Join-Path $ExtRoot 'world-owners.json'
|
|
64
|
+
$ExtPkg = Join-Path $ExtRoot 'package.json'
|
|
65
|
+
$Config = Join-Path $Deploy 'config.json'
|
|
66
|
+
$StubFile = Join-Path $Deploy 'docs-tool-stub.json'
|
|
67
|
+
$Src = if ($Ext) {
|
|
68
|
+
$Ext
|
|
69
|
+
} elseif (Test-Path -LiteralPath (Join-Path $PSScriptRoot 'index.js')) {
|
|
70
|
+
$PSScriptRoot
|
|
71
|
+
} elseif (Test-Path -LiteralPath (Join-Path $PSScriptRoot 'coo-doc\index.js')) {
|
|
72
|
+
Join-Path $PSScriptRoot 'coo-doc'
|
|
73
|
+
} elseif (Test-Path -LiteralPath (Join-Path (Split-Path $PSScriptRoot -Parent) 'index.js')) {
|
|
74
|
+
Split-Path $PSScriptRoot -Parent
|
|
75
|
+
} else {
|
|
76
|
+
$PSScriptRoot
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
Say ''
|
|
80
|
+
Say '=== 说明书库安装器 ===' 'Cyan'
|
|
81
|
+
Say "安装目录:$Coopanion"
|
|
82
|
+
Say "模式:$Mode$(if ($Revert) { ' / 卸载' })"
|
|
83
|
+
Say ''
|
|
84
|
+
|
|
85
|
+
foreach ($f in @($App, $Prefix, $Loop)) {
|
|
86
|
+
if (-not (Test-Path -LiteralPath $f)) { throw "找不到:$f(-Coopanion 指的不是 Coopanion 目录?)" }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if ($Check) {
|
|
90
|
+
# 自检:升级 Coopanion 之后跑一下,就知道补丁还在不在
|
|
91
|
+
Say '— 自检 —'
|
|
92
|
+
$hasPrefix = Select-String -LiteralPath $Prefix -Pattern 'export function toolStubMap' -Quiet
|
|
93
|
+
$hasLoop = Select-String -LiteralPath $Loop -Pattern 'const stubs = toolStubMap' -Quiet
|
|
94
|
+
Say " prefix.ts $(if ($hasPrefix) { '补丁在' } else { '补丁没了(升级覆盖过?重跑安装)' })"
|
|
95
|
+
Say " loop.ts $(if ($hasLoop) { '补丁在' } else { '补丁没了(同上)' })"
|
|
96
|
+
Say " 扩展 $(if (Test-Path -LiteralPath $NodeMod) { "在:$NodeMod" } else { '不在了' })"
|
|
97
|
+
$cfgOk = (Test-Path -LiteralPath $Config) -and ((Get-Content -LiteralPath $Config -Raw -Encoding UTF8) -match '"coodoc"')
|
|
98
|
+
Say " config $(if ($cfgOk) { '开了' } else { '没开' })"
|
|
99
|
+
$pkgJson = Join-Path $Coopanion 'resources\app\package.json'
|
|
100
|
+
if (Test-Path -LiteralPath $pkgJson) {
|
|
101
|
+
try { Say " 应用版本 $((Get-Content -LiteralPath $pkgJson -Raw -Encoding UTF8 | ConvertFrom-Json).version)" } catch { }
|
|
102
|
+
}
|
|
103
|
+
exit 0
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if ($Revert) {
|
|
107
|
+
# ── 卸载 ──
|
|
108
|
+
Step '删掉扩展链接/拷贝' {
|
|
109
|
+
if (Test-Path -LiteralPath $NodeMod) {
|
|
110
|
+
$item = Get-Item -LiteralPath $NodeMod -Force
|
|
111
|
+
if ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) {
|
|
112
|
+
[System.IO.Directory]::Delete($NodeMod)
|
|
113
|
+
} else {
|
|
114
|
+
Remove-Item -LiteralPath $NodeMod -Recurse -Force
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
Step '把世界归属里的 coodoc 去掉' {
|
|
119
|
+
$o = ReadJsonFile $Owners @{}
|
|
120
|
+
$o.PSObject.Properties.Remove($WorldId)
|
|
121
|
+
WriteJsonFile $Owners $o
|
|
122
|
+
}
|
|
123
|
+
Step 'config.json 里关掉 coodoc' {
|
|
124
|
+
$c = ReadJsonFile $Config @{}
|
|
125
|
+
if ($c.worlds -and $c.worlds.$WorldId) { $c.worlds.$WorldId.enabled = $false }
|
|
126
|
+
WriteJsonFile $Config $c
|
|
127
|
+
}
|
|
128
|
+
Step '删掉工具说明替身表(不然工具还是缩着的)' {
|
|
129
|
+
if (Test-Path -LiteralPath $StubFile) { Remove-Item -LiteralPath $StubFile -Force }
|
|
130
|
+
}
|
|
131
|
+
Step '把引擎改回来' {
|
|
132
|
+
foreach ($f in @($Prefix, $Loop)) {
|
|
133
|
+
$bak = "$f.bak-coodoc"
|
|
134
|
+
if (Test-Path -LiteralPath $bak) { Copy-Item -LiteralPath $bak -Destination $f -Force }
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
Say ''
|
|
138
|
+
Say '卸完了。重新打开 Coopanion 生效。' 'Green'
|
|
139
|
+
Say '(库里那几份 worlds\<id>\ENV_PROMPT.md 覆盖还留着,它们只在没装扩展时没影响,要清可以手删。)' 'Gray'
|
|
140
|
+
exit 0
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
# ── 0. 装之前先备份引擎 ──
|
|
144
|
+
foreach ($f in @($Prefix, $Loop)) {
|
|
145
|
+
$bak = "$f.bak-coodoc"
|
|
146
|
+
if (-not (Test-Path -LiteralPath $bak)) {
|
|
147
|
+
Step "备份 $([IO.Path]::GetFileName($f)) → 同名 .bak-coodoc" { Copy-Item -LiteralPath $f -Destination $bak -Force }
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
# ── 1. 扩展本体 ──
|
|
152
|
+
if (-not (Test-Path -LiteralPath (Join-Path $Src 'index.js'))) {
|
|
153
|
+
throw "找不到扩展源码:$Src(-Ext 指错了?)"
|
|
154
|
+
}
|
|
155
|
+
Step "建扩展目录 $ExtRoot" { New-Item -ItemType Directory -Force -Path $ExtRoot | Out-Null }
|
|
156
|
+
Step "扩展落位($Mode)" {
|
|
157
|
+
# 重装时先把攒下来的东西挪走:docs/ 是抄好的说明书,state.json 记着谁留手边
|
|
158
|
+
$keepDir = $null
|
|
159
|
+
if (Test-Path -LiteralPath $NodeMod) {
|
|
160
|
+
$item = Get-Item -LiteralPath $NodeMod -Force
|
|
161
|
+
if ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) { [System.IO.Directory]::Delete($NodeMod) }
|
|
162
|
+
else {
|
|
163
|
+
$keepDir = Join-Path ([IO.Path]::GetTempPath()) ('coodoc-keep-' + [guid]::NewGuid().ToString('N'))
|
|
164
|
+
New-Item -ItemType Directory -Force -Path $keepDir | Out-Null
|
|
165
|
+
foreach ($n in @('docs', 'state.json', 'catalog.json')) {
|
|
166
|
+
$from = Join-Path $NodeMod $n
|
|
167
|
+
if (Test-Path -LiteralPath $from) { Move-Item -LiteralPath $from -Destination (Join-Path $keepDir $n) -Force }
|
|
168
|
+
}
|
|
169
|
+
Remove-Item -LiteralPath $NodeMod -Recurse -Force
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if ($Mode -eq 'link') {
|
|
173
|
+
New-Item -ItemType Junction -Path $NodeMod -Target $Src | Out-Null
|
|
174
|
+
} else {
|
|
175
|
+
New-Item -ItemType Directory -Force -Path $NodeMod | Out-Null
|
|
176
|
+
foreach ($n in @('index.js', 'package.json', 'ENV_PROMPT.md', 'describe.json', 'icon.svg', 'README.md', 'CHANGELOG.md')) {
|
|
177
|
+
$from = Join-Path $Src $n
|
|
178
|
+
if (Test-Path -LiteralPath $from) { Copy-Item -LiteralPath $from -Destination (Join-Path $NodeMod $n) -Force }
|
|
179
|
+
}
|
|
180
|
+
$instSrc = Join-Path $Src 'install'
|
|
181
|
+
if (Test-Path -LiteralPath (Join-Path $instSrc 'install.ps1')) {
|
|
182
|
+
New-Item -ItemType Directory -Force -Path (Join-Path $NodeMod 'install') | Out-Null
|
|
183
|
+
Copy-Item -Path (Join-Path $instSrc '*') -Destination (Join-Path $NodeMod 'install') -Force
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if ($keepDir) {
|
|
187
|
+
foreach ($n in @('docs', 'state.json', 'catalog.json')) {
|
|
188
|
+
$from = Join-Path $keepDir $n
|
|
189
|
+
if (-not (Test-Path -LiteralPath $from)) { continue }
|
|
190
|
+
$to = Join-Path $NodeMod $n
|
|
191
|
+
if (Test-Path -LiteralPath $to) { Remove-Item -LiteralPath $to -Recurse -Force }
|
|
192
|
+
Move-Item -LiteralPath $from -Destination $to -Force
|
|
193
|
+
}
|
|
194
|
+
Remove-Item -LiteralPath $keepDir -Recurse -Force
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
# ── 2. 注册:归属 + 依赖 + 开关 ──
|
|
199
|
+
Step 'world-owners.json 认领 coodoc' {
|
|
200
|
+
$o = ReadJsonFile $Owners @{}
|
|
201
|
+
$o | Add-Member -NotePropertyName $WorldId -NotePropertyValue $PkgName -Force
|
|
202
|
+
WriteJsonFile $Owners $o
|
|
203
|
+
}
|
|
204
|
+
if ($Mode -eq 'link') {
|
|
205
|
+
Step 'data\extensions\package.json 记下源码位置' {
|
|
206
|
+
$p = ReadJsonFile $ExtPkg @{ name = 'extensions'; private = $true }
|
|
207
|
+
if (-not $p.dependencies) { $p | Add-Member -NotePropertyName dependencies -NotePropertyValue ([pscustomobject]@{}) -Force }
|
|
208
|
+
$p.dependencies | Add-Member -NotePropertyName $PkgName -NotePropertyValue ("link:" + ($Src -replace '\\', '/')) -Force
|
|
209
|
+
WriteJsonFile $ExtPkg $p
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
Step 'config.json 里开启 coodoc' {
|
|
213
|
+
New-Item -ItemType Directory -Force -Path $Deploy | Out-Null
|
|
214
|
+
$c = ReadJsonFile $Config @{}
|
|
215
|
+
if (-not $c.worlds) { $c | Add-Member -NotePropertyName worlds -NotePropertyValue ([pscustomobject]@{}) -Force }
|
|
216
|
+
$c.worlds | Add-Member -NotePropertyName $WorldId -NotePropertyValue ([pscustomobject]@{ enabled = $true; readChars = 2400 }) -Force
|
|
217
|
+
WriteJsonFile $Config $c
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
# ── 3. 引擎补丁 ──
|
|
221
|
+
if ($SkipPatch) {
|
|
222
|
+
Say ' 跳过引擎补丁(那就只能撤提示词,撤不了工具说明)' 'Yellow'
|
|
223
|
+
} else {
|
|
224
|
+
$PrefixSnippet = @'
|
|
225
|
+
interface StubCache {
|
|
226
|
+
stamp: string;
|
|
227
|
+
map: Map<string, string>;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
let stubCache: StubCache | null = null;
|
|
231
|
+
|
|
232
|
+
export function toolStubMap(dirs: EnvPromptDirs | undefined): Map<string, string> {
|
|
233
|
+
const dir = dirs?.deploymentDir;
|
|
234
|
+
if (!dir) return new Map();
|
|
235
|
+
const path = join(dir, 'docs-tool-stub.json');
|
|
236
|
+
let stamp: string;
|
|
237
|
+
try {
|
|
238
|
+
const st = statSync(path);
|
|
239
|
+
stamp = `${path}:${st.mtimeMs}:${st.size}`;
|
|
240
|
+
} catch {
|
|
241
|
+
stubCache = null;
|
|
242
|
+
return new Map();
|
|
243
|
+
}
|
|
244
|
+
if (stubCache && stubCache.stamp === stamp) return stubCache.map;
|
|
245
|
+
const map = new Map<string, string>();
|
|
246
|
+
try {
|
|
247
|
+
const parsed: unknown = JSON.parse(readTextFile(path));
|
|
248
|
+
if (parsed && typeof parsed === 'object') {
|
|
249
|
+
for (const [name, text] of Object.entries(parsed as Record<string, unknown>)) {
|
|
250
|
+
const s = String(text ?? '').trim();
|
|
251
|
+
if (name && s) map.set(name, s);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
} catch {
|
|
255
|
+
// 文件写坏了就当没有,工具照原样发
|
|
256
|
+
}
|
|
257
|
+
stubCache = { stamp, map };
|
|
258
|
+
return map;
|
|
259
|
+
}
|
|
260
|
+
'@
|
|
261
|
+
|
|
262
|
+
Step 'prefix.ts:加替身表读取' {
|
|
263
|
+
$t = Get-Content -LiteralPath $Prefix -Raw -Encoding UTF8
|
|
264
|
+
if ($t -match 'export function toolStubMap') { Say ' 已经有了,跳过' 'DarkGray'; return }
|
|
265
|
+
if ($t -notmatch "statSync") {
|
|
266
|
+
$t = $t -replace "import \{ existsSync", "import { existsSync, statSync"
|
|
267
|
+
}
|
|
268
|
+
if ($t -notmatch "readTextFile") {
|
|
269
|
+
$t = $t.TrimEnd() + "`nimport { readTextFile } from './util.ts';`n"
|
|
270
|
+
}
|
|
271
|
+
$t = $t.TrimEnd() + "`n`n" + $PrefixSnippet + "`n"
|
|
272
|
+
[System.IO.File]::WriteAllText($Prefix, $t, $Utf8)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
Step 'loop.ts:导入 toolStubMap' {
|
|
276
|
+
$t = Get-Content -LiteralPath $Loop -Raw -Encoding UTF8
|
|
277
|
+
if ($t -match "toolStubMap") { Say ' 已经有了,跳过' 'DarkGray'; return }
|
|
278
|
+
$t = [regex]::Replace($t, "import \{([^}]*)\} from '\./prefix\.ts'", {
|
|
279
|
+
param($m)
|
|
280
|
+
$inner = $m.Groups[1].Value.Trim()
|
|
281
|
+
if ($inner -match 'toolStubMap') { return $m.Value }
|
|
282
|
+
return "import {$inner, toolStubMap} from './prefix.ts'"
|
|
283
|
+
}, 1)
|
|
284
|
+
[System.IO.File]::WriteAllText($Loop, $t, $Utf8)
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
Step 'loop.ts:assembleTools 里换成替身说明' {
|
|
288
|
+
$t = Get-Content -LiteralPath $Loop -Raw -Encoding UTF8
|
|
289
|
+
if ($t -match 'const stubs = toolStubMap') { Say ' 已经有了,跳过' 'DarkGray'; return }
|
|
290
|
+
$anchor = 'const { decl, log } = this.d;'
|
|
291
|
+
if (-not $t.Contains($anchor)) { throw 'assembleTools 的锚点变了,这个版本得手动改(看 README「手动打补丁」)' }
|
|
292
|
+
$hits = ([regex]::Matches($t, [regex]::Escape($anchor))).Count
|
|
293
|
+
if ($hits -ne 1) { throw "锚点出现 $hits 次,不敢乱插,请手动改" }
|
|
294
|
+
$r1 = $anchor + "`n // 说明书库收进库里的工具:description 换成「一句场合 + 文件路径」,工具照旧可调。`n const stubs = toolStubMap(this.d.dirs);"
|
|
295
|
+
$t = $t.Replace($anchor, $r1)
|
|
296
|
+
$old = 'defs.push(def)'
|
|
297
|
+
if ($t.Contains($old)) {
|
|
298
|
+
$r2 = "const stub = stubs.get(def.name);`n defs.push(stub ? { ...def, description: stub } : def)"
|
|
299
|
+
$t = $t.Replace($old, $r2)
|
|
300
|
+
} else {
|
|
301
|
+
Say ' push 那行没认出来,请手动把 defs.push(def) 换成带 stub 的写法' 'Yellow'
|
|
302
|
+
}
|
|
303
|
+
[System.IO.File]::WriteAllText($Loop, $t, $Utf8)
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
Say ''
|
|
308
|
+
Say '装好了。下一步:' 'Green'
|
|
309
|
+
Say ' 1. 完全退出 Coopanion(托盘也要退),再打开'
|
|
310
|
+
Say ' 2. 打开后它会自己扫一遍已装扩展,文档落在扩展目录的 docs\ 下'
|
|
311
|
+
Say ' 3. 默认一本都不收,全留在手边;想收哪本,在 Coo 里说一声,或让它调 coodoc_run(cold)'
|
|
312
|
+
Say ''
|
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# 说明书库(coodoc)一键安装 / 卸载 — Linux / macOS
|
|
3
|
+
#
|
|
4
|
+
# 用法:
|
|
5
|
+
# bash install.sh # 自动找 Coopanion,拷贝模式装好
|
|
6
|
+
# bash install.sh --coopanion /opt/Coopanion
|
|
7
|
+
# bash install.sh --mode link # 软链模式(自己还要改代码时才用)
|
|
8
|
+
# bash install.sh --dry-run # 只看会做什么
|
|
9
|
+
# bash install.sh --revert # 卸载并还原引擎
|
|
10
|
+
#
|
|
11
|
+
# 依赖:bash + python3(改 JSON / 打补丁用;macOS 自带,Linux 一般也有)
|
|
12
|
+
|
|
13
|
+
PKG='cortico-world-coo-doc'
|
|
14
|
+
WORLD='coodoc'
|
|
15
|
+
MODE='copy'
|
|
16
|
+
COOPANION=''
|
|
17
|
+
DATA=''
|
|
18
|
+
SRC=''
|
|
19
|
+
DRY=0
|
|
20
|
+
REVERT=0
|
|
21
|
+
SKIP_PATCH=0
|
|
22
|
+
CHECK=0
|
|
23
|
+
|
|
24
|
+
usage() {
|
|
25
|
+
sed -n '2,12p' "$0" | sed 's/^# \{0,1\}//'
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
while [ $# -gt 0 ]; do
|
|
29
|
+
case "$1" in
|
|
30
|
+
--coopanion) COOPANION="${2:-}"; shift 2 ;;
|
|
31
|
+
--data) DATA="${2:-}"; shift 2 ;;
|
|
32
|
+
--ext) SRC="${2:-}"; shift 2 ;;
|
|
33
|
+
--mode) MODE="${2:-copy}"; shift 2 ;;
|
|
34
|
+
--dry-run|-n) DRY=1; shift ;;
|
|
35
|
+
--revert) REVERT=1; shift ;;
|
|
36
|
+
--skip-patch) SKIP_PATCH=1; shift ;;
|
|
37
|
+
--check) CHECK=1; shift ;;
|
|
38
|
+
-h|--help) usage; exit 0 ;;
|
|
39
|
+
*) echo "不认识的参数:$1" >&2; usage; exit 2 ;;
|
|
40
|
+
esac
|
|
41
|
+
done
|
|
42
|
+
|
|
43
|
+
say() { printf '%s\n' "$*"; }
|
|
44
|
+
ind() { printf ' > %s\n' "$*"; }
|
|
45
|
+
warn() { printf '%s\n' "$*"; }
|
|
46
|
+
|
|
47
|
+
need_py() {
|
|
48
|
+
command -v python3 >/dev/null 2>&1 && return 0
|
|
49
|
+
say '缺 python3,改 JSON 和打补丁靠它。'
|
|
50
|
+
say ' macOS: xcode-select --install (或 brew install python3)'
|
|
51
|
+
say ' Linux: sudo apt install python3 / sudo dnf install python3'
|
|
52
|
+
exit 1
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
# 在安装根里找 build/cortico/src/core(app 包 / 普通目录都认)
|
|
56
|
+
find_core() {
|
|
57
|
+
local root="$1" hit p
|
|
58
|
+
for p in "$root/Contents/Resources/app" "$root/resources/app" "$root/app" "$root"; do
|
|
59
|
+
if [ -f "$p/build/cortico/src/core/prefix.ts" ]; then
|
|
60
|
+
printf '%s\n' "$p/build/cortico/src/core"; return 0
|
|
61
|
+
fi
|
|
62
|
+
done
|
|
63
|
+
hit=$(find "$root" -maxdepth 6 -path '*/build/cortico/src/core/prefix.ts' 2>/dev/null | head -n1)
|
|
64
|
+
if [ -n "$hit" ]; then dirname "$hit"; return 0; fi
|
|
65
|
+
return 1
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
# 找 data 目录(里面有 home/companion 和 extensions)
|
|
69
|
+
find_data() {
|
|
70
|
+
local core="$1" app hit p out=''
|
|
71
|
+
app=$(cd "$core/../../../.." && pwd)
|
|
72
|
+
for p in "$app/data" "$app/../../data" "$app/../../../data" \
|
|
73
|
+
"$HOME/Library/Application Support/Coopanion/data" \
|
|
74
|
+
"$HOME/Library/Application Support/Coopanion" \
|
|
75
|
+
"$HOME/.config/Coopanion/data" "$HOME/.config/Coopanion" \
|
|
76
|
+
"$HOME/.local/share/Coopanion/data" "$HOME/.local/share/Coopanion"; do
|
|
77
|
+
if [ -f "$p/home/companion/config.json" ]; then out="$p"; break; fi
|
|
78
|
+
done
|
|
79
|
+
if [ -z "$out" ]; then
|
|
80
|
+
for p in "$app/data" "$app/../../data" \
|
|
81
|
+
"$HOME/Library/Application Support/Coopanion/data" \
|
|
82
|
+
"$HOME/.config/Coopanion/data" \
|
|
83
|
+
"$HOME/.local/share/Coopanion/data"; do
|
|
84
|
+
if [ -d "$p/extensions" ]; then out="$p"; break; fi
|
|
85
|
+
done
|
|
86
|
+
fi
|
|
87
|
+
if [ -z "$out" ]; then
|
|
88
|
+
hit=$(find "$app/../.." -maxdepth 6 -path '*/home/companion/config.json' 2>/dev/null | head -n1)
|
|
89
|
+
if [ -z "$hit" ]; then
|
|
90
|
+
hit=$(find "$HOME/Library/Application Support" "$HOME/.config" "$HOME/.local/share" \
|
|
91
|
+
-maxdepth 5 -path '*Coopanion*/home/companion/config.json' 2>/dev/null | head -n1)
|
|
92
|
+
fi
|
|
93
|
+
if [ -n "$hit" ]; then out="${hit%/home/companion/config.json}"; else out="$app/data"; fi
|
|
94
|
+
fi
|
|
95
|
+
if [ -d "$out" ]; then (cd "$out" && pwd); else printf '%s\n' "$out"; fi
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
# 没给路径就自己找
|
|
99
|
+
if [ -z "$COOPANION" ]; then
|
|
100
|
+
cands=()
|
|
101
|
+
[ "$(uname -s)" = 'Darwin' ] && cands+=("/Applications/Coopanion.app" "$HOME/Applications/Coopanion.app")
|
|
102
|
+
cands+=("$HOME/Coopanion" "/opt/Coopanion" "/opt/coopanion" "/usr/local/Coopanion" \
|
|
103
|
+
"/usr/local/coopanion" "$HOME/.local/share/Coopanion" "/Applications/Coopanion.app")
|
|
104
|
+
for c in "${cands[@]}"; do
|
|
105
|
+
[ -d "$c" ] || continue
|
|
106
|
+
if find_core "$c" >/dev/null 2>&1; then COOPANION="$c"; break; fi
|
|
107
|
+
done
|
|
108
|
+
if [ -z "$COOPANION" ]; then
|
|
109
|
+
for base in /Applications /opt "$HOME" "/usr/local"; do
|
|
110
|
+
hit=$(find "$base" -maxdepth 6 -path '*/build/cortico/src/core/prefix.ts' 2>/dev/null | head -n1)
|
|
111
|
+
if [ -n "$hit" ]; then
|
|
112
|
+
core="${hit%/build/cortico/src/core/prefix.ts}"
|
|
113
|
+
COOPANION=$(cd "$core/../.." && pwd)
|
|
114
|
+
break
|
|
115
|
+
fi
|
|
116
|
+
done
|
|
117
|
+
fi
|
|
118
|
+
if [ -z "$COOPANION" ]; then
|
|
119
|
+
say '没找到 Coopanion。知道在哪就这样跑:bash install.sh --coopanion "/你的/Coopanion"'
|
|
120
|
+
exit 1
|
|
121
|
+
fi
|
|
122
|
+
fi
|
|
123
|
+
|
|
124
|
+
[ -d "$COOPANION" ] || { say "目录不存在:$COOPANION"; exit 1; }
|
|
125
|
+
|
|
126
|
+
CORE=$(find_core "$COOPANION") || { say "这个目录里没有 Coopanion 的引擎:$COOPANION"; exit 1; }
|
|
127
|
+
PREFIX="$CORE/prefix.ts"
|
|
128
|
+
LOOP="$CORE/loop.ts"
|
|
129
|
+
[ -n "$DATA" ] || DATA=$(find_data "$CORE")
|
|
130
|
+
EXT_ROOT="$DATA/extensions"
|
|
131
|
+
NODE_MOD="$EXT_ROOT/node_modules/$PKG"
|
|
132
|
+
DEPLOY="$DATA/home/companion"
|
|
133
|
+
OWNERS="$EXT_ROOT/world-owners.json"
|
|
134
|
+
EXT_PKG="$EXT_ROOT/package.json"
|
|
135
|
+
CONFIG="$DEPLOY/config.json"
|
|
136
|
+
STUB="$DEPLOY/docs-tool-stub.json"
|
|
137
|
+
SUDO=''
|
|
138
|
+
if [ "$(id -u)" -ne 0 ] && { [ ! -w "$CORE" ] || [ ! -w "$EXT_ROOT" ]; }; then
|
|
139
|
+
command -v sudo >/dev/null 2>&1 && SUDO='sudo'
|
|
140
|
+
fi
|
|
141
|
+
|
|
142
|
+
if [ -z "$SRC" ]; then
|
|
143
|
+
_here="$(cd "$(dirname "$0")" && pwd)"
|
|
144
|
+
if [ -f "$_here/index.js" ]; then
|
|
145
|
+
SRC="$_here"
|
|
146
|
+
elif [ -f "$_here/coo-doc/index.js" ]; then
|
|
147
|
+
SRC="$_here/coo-doc"
|
|
148
|
+
elif [ -f "$(dirname "$_here")/index.js" ]; then
|
|
149
|
+
SRC="$(dirname "$_here")"
|
|
150
|
+
else
|
|
151
|
+
SRC="$_here"
|
|
152
|
+
fi
|
|
153
|
+
fi
|
|
154
|
+
|
|
155
|
+
say ''
|
|
156
|
+
say '=== 说明书库安装器 ==='
|
|
157
|
+
say "系统:$(uname -s) $(uname -m)"
|
|
158
|
+
say "安装目录:$COOPANION"
|
|
159
|
+
say "数据目录:$DATA"
|
|
160
|
+
say "模式:$MODE$([ "$REVERT" = 1 ] && echo ' / 卸载')$([ "$DRY" = 1 ] && echo ' / 仅预览')$([ "$CHECK" = 1 ] && echo ' / 自检')"
|
|
161
|
+
say ''
|
|
162
|
+
|
|
163
|
+
# 自检:装了没、补丁还在不在(升级 Coopanion 之后最有用的就是它)
|
|
164
|
+
if [ "$CHECK" = 1 ]; then
|
|
165
|
+
say '— 自检 —'
|
|
166
|
+
if grep -q 'export function toolStubMap' "$PREFIX" 2>/dev/null; then say ' prefix.ts 补丁在'; else say ' prefix.ts 补丁没了(升级覆盖过?重跑安装)'; fi
|
|
167
|
+
if grep -q 'const stubs = toolStubMap' "$LOOP" 2>/dev/null; then say ' loop.ts 补丁在'; else say ' loop.ts 补丁没了(同上)'; fi
|
|
168
|
+
if [ -d "$NODE_MOD" ]; then say " 扩展 在:$NODE_MOD"; else say ' 扩展 不在了'; fi
|
|
169
|
+
if grep -q '"coodoc"' "$CONFIG" 2>/dev/null; then say ' config 开了'; else say ' config 没开'; fi
|
|
170
|
+
pkgjson="$CORE/../../../../package.json"
|
|
171
|
+
if [ -f "$pkgjson" ]; then
|
|
172
|
+
ver=$(sed -n 's/.*"version"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$pkgjson" | head -n1)
|
|
173
|
+
[ -n "$ver" ] && say " 应用版本 $ver"
|
|
174
|
+
fi
|
|
175
|
+
exit 0
|
|
176
|
+
fi
|
|
177
|
+
|
|
178
|
+
need_py
|
|
179
|
+
|
|
180
|
+
if [ "$REVERT" = 1 ]; then
|
|
181
|
+
say '— 卸载 —'
|
|
182
|
+
if [ "$DRY" = 1 ]; then
|
|
183
|
+
say ' [dry] 删扩展目录 / 软链、去注册、关开关、删替身表、还原引擎补丁'
|
|
184
|
+
exit 0
|
|
185
|
+
fi
|
|
186
|
+
if [ -L "$NODE_MOD" ] || [ -d "$NODE_MOD" ]; then
|
|
187
|
+
ind '删掉扩展链接 / 拷贝'
|
|
188
|
+
$SUDO rm -rf "$NODE_MOD"
|
|
189
|
+
fi
|
|
190
|
+
ind '把世界归属里的 coodoc 去掉'
|
|
191
|
+
$SUDO python3 - "$OWNERS" "$WORLD" <<'PY'
|
|
192
|
+
import json, os, sys
|
|
193
|
+
p, world = sys.argv[1], sys.argv[2]
|
|
194
|
+
if os.path.exists(p):
|
|
195
|
+
raw = open(p, encoding='utf-8').read().strip()
|
|
196
|
+
data = json.loads(raw) if raw else {}
|
|
197
|
+
data.pop(world, None)
|
|
198
|
+
open(p, 'w', encoding='utf-8').write(json.dumps(data, ensure_ascii=False, indent=2) + '\n')
|
|
199
|
+
PY
|
|
200
|
+
ind 'config.json 里关掉 coodoc'
|
|
201
|
+
$SUDO python3 - "$CONFIG" "$WORLD" <<'PY'
|
|
202
|
+
import json, os, sys
|
|
203
|
+
p, world = sys.argv[1], sys.argv[2]
|
|
204
|
+
if os.path.exists(p):
|
|
205
|
+
raw = open(p, encoding='utf-8').read().strip()
|
|
206
|
+
data = json.loads(raw) if raw else {}
|
|
207
|
+
w = data.get('worlds') or {}
|
|
208
|
+
if isinstance(w.get(world), dict):
|
|
209
|
+
w[world]['enabled'] = False
|
|
210
|
+
data['worlds'] = w
|
|
211
|
+
open(p, 'w', encoding='utf-8').write(json.dumps(data, ensure_ascii=False, indent=2) + '\n')
|
|
212
|
+
PY
|
|
213
|
+
ind '删掉工具说明替身表'
|
|
214
|
+
[ -f "$STUB" ] && $SUDO rm -f "$STUB"
|
|
215
|
+
ind '把引擎改回来'
|
|
216
|
+
for f in "$PREFIX" "$LOOP"; do
|
|
217
|
+
[ -f "$f.bak-coodoc" ] && $SUDO cp -f "$f.bak-coodoc" "$f"
|
|
218
|
+
done
|
|
219
|
+
say ''
|
|
220
|
+
say '卸完了。重新打开 Coopanion 生效。'
|
|
221
|
+
exit 0
|
|
222
|
+
fi
|
|
223
|
+
|
|
224
|
+
for f in "$PREFIX" "$LOOP"; do
|
|
225
|
+
[ -f "$f" ] || { say "找不到:$f"; exit 1; }
|
|
226
|
+
done
|
|
227
|
+
[ -f "$SRC/index.js" ] || { say "找不到扩展源码:$SRC"; exit 1; }
|
|
228
|
+
|
|
229
|
+
# 装之前先备份引擎
|
|
230
|
+
for f in "$PREFIX" "$LOOP"; do
|
|
231
|
+
if [ ! -f "$f.bak-coodoc" ]; then
|
|
232
|
+
if [ "$DRY" = 1 ]; then say " [dry] 备份 $(basename "$f") → .bak-coodoc"
|
|
233
|
+
else ind "备份 $(basename "$f") → .bak-coodoc"; $SUDO cp -f "$f" "$f.bak-coodoc"; fi
|
|
234
|
+
fi
|
|
235
|
+
done
|
|
236
|
+
|
|
237
|
+
# 1. 扩展本体
|
|
238
|
+
if [ "$DRY" = 1 ]; then
|
|
239
|
+
say " [dry] 扩展落位到 $NODE_MOD($MODE)"
|
|
240
|
+
else
|
|
241
|
+
ind "扩展落位到 $EXT_ROOT/node_modules($MODE)"
|
|
242
|
+
$SUDO mkdir -p "$EXT_ROOT/node_modules"
|
|
243
|
+
# 重装时先把攒下来的东西挪走:docs/ 是抄好的说明书,state.json 记着谁留手边
|
|
244
|
+
KEEP=''
|
|
245
|
+
if [ -e "$NODE_MOD" ] && [ ! -L "$NODE_MOD" ]; then
|
|
246
|
+
KEEP="$(mktemp -d)"
|
|
247
|
+
for n in docs state.json catalog.json; do
|
|
248
|
+
[ -e "$NODE_MOD/$n" ] && $SUDO mv "$NODE_MOD/$n" "$KEEP/$n"
|
|
249
|
+
done
|
|
250
|
+
$SUDO chown -R "$(id -u):$(id -g)" "$KEEP" 2>/dev/null || true
|
|
251
|
+
fi
|
|
252
|
+
[ -e "$NODE_MOD" ] || [ -L "$NODE_MOD" ] && $SUDO rm -rf "$NODE_MOD" || true
|
|
253
|
+
if [ "$MODE" = 'link' ]; then
|
|
254
|
+
$SUDO ln -s "$SRC" "$NODE_MOD"
|
|
255
|
+
else
|
|
256
|
+
$SUDO mkdir -p "$NODE_MOD"
|
|
257
|
+
for n in index.js package.json ENV_PROMPT.md describe.json icon.svg README.md CHANGELOG.md; do
|
|
258
|
+
[ -f "$SRC/$n" ] && $SUDO cp -f "$SRC/$n" "$NODE_MOD/$n"
|
|
259
|
+
done
|
|
260
|
+
if [ -f "$SRC/install/install.sh" ]; then
|
|
261
|
+
$SUDO mkdir -p "$NODE_MOD/install"
|
|
262
|
+
$SUDO cp -f "$SRC/install/"* "$NODE_MOD/install/"
|
|
263
|
+
fi
|
|
264
|
+
fi
|
|
265
|
+
if [ -n "$KEEP" ]; then
|
|
266
|
+
for n in docs state.json catalog.json; do
|
|
267
|
+
[ -e "$KEEP/$n" ] || continue
|
|
268
|
+
$SUDO rm -rf "$NODE_MOD/$n"
|
|
269
|
+
$SUDO mv "$KEEP/$n" "$NODE_MOD/$n"
|
|
270
|
+
done
|
|
271
|
+
rm -rf "$KEEP"
|
|
272
|
+
fi
|
|
273
|
+
fi
|
|
274
|
+
|
|
275
|
+
# 2. 注册:归属 + 依赖 + 开关
|
|
276
|
+
if [ "$DRY" = 1 ]; then
|
|
277
|
+
say ' [dry] 写 world-owners.json / package.json / config.json'
|
|
278
|
+
else
|
|
279
|
+
ind 'world-owners.json 认领 coodoc'
|
|
280
|
+
$SUDO python3 - "$OWNERS" "$WORLD" "$PKG" <<'PY'
|
|
281
|
+
import json, os, sys
|
|
282
|
+
p, world, pkg = sys.argv[1], sys.argv[2], sys.argv[3]
|
|
283
|
+
raw = open(p, encoding='utf-8').read().strip() if os.path.exists(p) else ''
|
|
284
|
+
data = json.loads(raw) if raw else {}
|
|
285
|
+
data[world] = pkg
|
|
286
|
+
open(p, 'w', encoding='utf-8').write(json.dumps(data, ensure_ascii=False, indent=2) + '\n')
|
|
287
|
+
PY
|
|
288
|
+
if [ "$MODE" = 'link' ]; then
|
|
289
|
+
ind 'package.json 记下源码位置'
|
|
290
|
+
$SUDO python3 - "$EXT_PKG" "$PKG" "$SRC" <<'PY'
|
|
291
|
+
import json, os, sys
|
|
292
|
+
p, pkg, src = sys.argv[1], sys.argv[2], sys.argv[3]
|
|
293
|
+
raw = open(p, encoding='utf-8').read().strip() if os.path.exists(p) else ''
|
|
294
|
+
data = json.loads(raw) if raw else {'name': 'extensions', 'private': True}
|
|
295
|
+
data.setdefault('dependencies', {})[pkg] = 'link:' + src.replace('\\', '/')
|
|
296
|
+
open(p, 'w', encoding='utf-8').write(json.dumps(data, ensure_ascii=False, indent=2) + '\n')
|
|
297
|
+
PY
|
|
298
|
+
fi
|
|
299
|
+
ind 'config.json 里开启 coodoc'
|
|
300
|
+
$SUDO mkdir -p "$DEPLOY"
|
|
301
|
+
$SUDO python3 - "$CONFIG" "$WORLD" <<'PY'
|
|
302
|
+
import json, os, sys
|
|
303
|
+
p, world = sys.argv[1], sys.argv[2]
|
|
304
|
+
raw = open(p, encoding='utf-8').read().strip() if os.path.exists(p) else ''
|
|
305
|
+
data = json.loads(raw) if raw else {}
|
|
306
|
+
w = data.get('worlds')
|
|
307
|
+
if not isinstance(w, dict):
|
|
308
|
+
w = {}
|
|
309
|
+
w[world] = {'enabled': True, 'readChars': 2400}
|
|
310
|
+
data['worlds'] = w
|
|
311
|
+
open(p, 'w', encoding='utf-8').write(json.dumps(data, ensure_ascii=False, indent=2) + '\n')
|
|
312
|
+
PY
|
|
313
|
+
fi
|
|
314
|
+
|
|
315
|
+
# 3. 引擎补丁
|
|
316
|
+
if [ "$SKIP_PATCH" = 1 ]; then
|
|
317
|
+
say ' 跳过引擎补丁(那就只能撤提示词,撤不了工具说明)'
|
|
318
|
+
elif [ "$DRY" = 1 ]; then
|
|
319
|
+
say ' [dry] prefix.ts 加替身表读取;loop.ts 换成替身 description'
|
|
320
|
+
else
|
|
321
|
+
ind 'prefix.ts:加替身表读取'
|
|
322
|
+
$SUDO python3 - "$PREFIX" <<'PY'
|
|
323
|
+
import sys
|
|
324
|
+
p = sys.argv[1]
|
|
325
|
+
t = open(p, encoding='utf-8').read()
|
|
326
|
+
if 'export function toolStubMap' in t:
|
|
327
|
+
print(' 已经有了,跳过')
|
|
328
|
+
sys.exit(0)
|
|
329
|
+
if 'statSync' not in t:
|
|
330
|
+
t = t.replace('import { existsSync', 'import { existsSync, statSync', 1)
|
|
331
|
+
if 'readTextFile' not in t:
|
|
332
|
+
t = t.rstrip() + "\nimport { readTextFile } from './util.ts';\n"
|
|
333
|
+
snippet = '''
|
|
334
|
+
interface StubCache {
|
|
335
|
+
stamp: string;
|
|
336
|
+
map: Map<string, string>;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
let stubCache: StubCache | null = null;
|
|
340
|
+
|
|
341
|
+
export function toolStubMap(dirs: EnvPromptDirs | undefined): Map<string, string> {
|
|
342
|
+
const dir = dirs?.deploymentDir;
|
|
343
|
+
if (!dir) return new Map();
|
|
344
|
+
const path = join(dir, 'docs-tool-stub.json');
|
|
345
|
+
let stamp: string;
|
|
346
|
+
try {
|
|
347
|
+
const st = statSync(path);
|
|
348
|
+
stamp = `${path}:${st.mtimeMs}:${st.size}`;
|
|
349
|
+
} catch {
|
|
350
|
+
stubCache = null;
|
|
351
|
+
return new Map();
|
|
352
|
+
}
|
|
353
|
+
if (stubCache && stubCache.stamp === stamp) return stubCache.map;
|
|
354
|
+
const map = new Map<string, string>();
|
|
355
|
+
try {
|
|
356
|
+
const parsed: unknown = JSON.parse(readTextFile(path));
|
|
357
|
+
if (parsed && typeof parsed === 'object') {
|
|
358
|
+
for (const [name, text] of Object.entries(parsed as Record<string, unknown>)) {
|
|
359
|
+
const s = String(text ?? '').trim();
|
|
360
|
+
if (name && s) map.set(name, s);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
} catch {
|
|
364
|
+
// 文件写坏了就当没有,工具照原样发
|
|
365
|
+
}
|
|
366
|
+
stubCache = { stamp, map };
|
|
367
|
+
return map;
|
|
368
|
+
}
|
|
369
|
+
'''
|
|
370
|
+
open(p, 'w', encoding='utf-8').write(t.rstrip() + '\n\n' + snippet)
|
|
371
|
+
PY
|
|
372
|
+
|
|
373
|
+
ind 'loop.ts:导入 toolStubMap'
|
|
374
|
+
$SUDO python3 - "$LOOP" <<'PY'
|
|
375
|
+
import re, sys
|
|
376
|
+
p = sys.argv[1]
|
|
377
|
+
t = open(p, encoding='utf-8').read()
|
|
378
|
+
if 'toolStubMap' in t:
|
|
379
|
+
print(' 已经有了,跳过')
|
|
380
|
+
sys.exit(0)
|
|
381
|
+
def add(m):
|
|
382
|
+
inner = m.group(1).strip()
|
|
383
|
+
return "import {" + inner + ", toolStubMap} from './prefix.ts'"
|
|
384
|
+
t2, n = re.subn(r"import \{([^}]*)\} from '\./prefix\.ts'", add, t, count=1)
|
|
385
|
+
if n == 0:
|
|
386
|
+
print(' 没找到 ./prefix.ts 的 import,请手动加 toolStubMap', file=sys.stderr)
|
|
387
|
+
open(p, 'w', encoding='utf-8').write(t2)
|
|
388
|
+
PY
|
|
389
|
+
|
|
390
|
+
ind 'loop.ts:assembleTools 里换成替身说明'
|
|
391
|
+
$SUDO python3 - "$LOOP" <<'PY'
|
|
392
|
+
import sys
|
|
393
|
+
p = sys.argv[1]
|
|
394
|
+
t = open(p, encoding='utf-8').read()
|
|
395
|
+
if 'const stubs = toolStubMap' in t:
|
|
396
|
+
print(' 已经有了,跳过')
|
|
397
|
+
sys.exit(0)
|
|
398
|
+
anchor = 'const { decl, log } = this.d;'
|
|
399
|
+
hits = t.count(anchor)
|
|
400
|
+
if hits != 1:
|
|
401
|
+
print(f' 锚点出现 {hits} 次,不敢乱插,请手动改(看技术说明「手动打补丁」)', file=sys.stderr)
|
|
402
|
+
sys.exit(3)
|
|
403
|
+
t = t.replace(anchor, anchor + "\n // 说明书库收进库里的工具:description 换成「一句场合 + 文件路径」,工具照旧可调。\n const stubs = toolStubMap(this.d.dirs);", 1)
|
|
404
|
+
old = 'defs.push(def)'
|
|
405
|
+
if old in t:
|
|
406
|
+
t = t.replace(old, "const stub = stubs.get(def.name);\n defs.push(stub ? { ...def, description: stub } : def)", 1)
|
|
407
|
+
else:
|
|
408
|
+
print(' push 那行没认出来,请手动把 defs.push(def) 换成带 stub 的写法', file=sys.stderr)
|
|
409
|
+
open(p, 'w', encoding='utf-8').write(t)
|
|
410
|
+
PY
|
|
411
|
+
fi
|
|
412
|
+
|
|
413
|
+
# Mac:改过 .app 里的文件会把签名封条弄坏,重签一次 ad-hoc,不然可能打不开。
|
|
414
|
+
if [ "$(uname -s)" = 'Darwin' ]; then
|
|
415
|
+
case "$COOPANION" in
|
|
416
|
+
*.app)
|
|
417
|
+
ind '重新签名(改过 app 里的文件)'
|
|
418
|
+
if $SUDO codesign --force --deep -s - "$COOPANION" >/dev/null 2>&1; then
|
|
419
|
+
say ' 签好了'
|
|
420
|
+
else
|
|
421
|
+
say " 重签没成;打不开的话手动跑:sudo codesign --force --deep -s - \"$COOPANION\""
|
|
422
|
+
fi
|
|
423
|
+
;;
|
|
424
|
+
esac
|
|
425
|
+
fi
|
|
426
|
+
|
|
427
|
+
say ''
|
|
428
|
+
say '装好了。下一步:'
|
|
429
|
+
say ' 1. 完全退出 Coopanion(托盘 / 菜单栏也要退),再打开'
|
|
430
|
+
say ' 2. 打开后它会自己扫一遍已装扩展,文档落在扩展目录的 docs/ 下'
|
|
431
|
+
say ' 3. 默认一本都不收,全留在手边;想收哪本,在 Coo 里说一声,或让它调 coodoc_run(cold)'
|
|
432
|
+
say ''
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cortico-world-coo-doc",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "说明书库:把已装且启用扩展的环境提示词和工具说明抄成 docs/ 下的文件,系统提示词里只留一句「什么时候用」加路径;哪几本留在手边、哪几本收进库里,由机器人在对话里自己切(hot / cold,可临时留几天、到期自动收回)。附带每 5
|
|
5
|
+
"description": "说明书库:把已装且启用扩展的环境提示词和工具说明抄成 docs/ 下的文件,系统提示词里只留一句「什么时候用」加路径;哪几本留在手边、哪几本收进库里,由机器人在对话里自己切(hot / cold,可临时留几天、到期自动收回)。附带每 5 分钟自动扫一遍新装扩展、目录与常用标记、按节读取和关键词定位、模板型提示词的扫描快照与实时值提醒;Windows 的 install.ps1、macOS 与 Linux 的 install.sh 随包发出,可选地给引擎打补丁,把工具说明也缩成一句加路径。",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"main": "./index.js",
|
|
8
8
|
"files": [
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"README.md",
|
|
12
12
|
"CHANGELOG.md",
|
|
13
13
|
"describe.json",
|
|
14
|
-
"icon.svg"
|
|
14
|
+
"icon.svg",
|
|
15
|
+
"install"
|
|
15
16
|
],
|
|
16
17
|
"engines": {
|
|
17
18
|
"node": ">=22"
|