@fabianactor/dsh-lil-whale-group 1.8.15 → 1.9.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/lib/code-fp.js +57 -0
- package/lib/config.js +147 -128
- package/lib/index.js +3782 -3708
- package/lib/say-core.js +60 -0
- package/lib/wake-preview-core.js +68 -56
- package/package.json +43 -43
package/lib/code-fp.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// 代码指纹(2026-11-17 r255)。
|
|
2
|
+
//
|
|
3
|
+
// 用途:回答"这个进程里跑的,是不是磁盘上现在这份代码"。
|
|
4
|
+
//
|
|
5
|
+
// 为什么不用 mtime(第二条鱼的探针脚本原先用 mtime 判"本进程是不是新代码",这是我补的一刀):
|
|
6
|
+
// ① mtime 只比一个文件会漏 —— 只改 say-core.js 时,index.js 的 mtime 没变,闸照样放行;
|
|
7
|
+
// ② mtime 会被"不是修改"的事情改 —— git checkout、恢复备份、复制目录都会把 mtime 改回去;
|
|
8
|
+
// ③ 时间戳回答"谁更新",指纹回答"是不是同一份",要问的是后者。
|
|
9
|
+
//
|
|
10
|
+
// 纯函数,不读配置、不碰鱼塘:只读本目录下的运行时代码(含子目录,跳过 node_modules 与点文件)。
|
|
11
|
+
|
|
12
|
+
import { createHash } from 'node:crypto'
|
|
13
|
+
import { readdirSync, readFileSync } from 'node:fs'
|
|
14
|
+
import { dirname, join } from 'node:path'
|
|
15
|
+
import { fileURLToPath } from 'node:url'
|
|
16
|
+
|
|
17
|
+
const DEFAULT_DIR = dirname(fileURLToPath(import.meta.url))
|
|
18
|
+
const CODE_EXT = /\.(cjs|mjs|js|json)$/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 返回 { fp, files }:fp 是 12 位十六进制;读不了时 fp='unknown' 并带 error。
|
|
22
|
+
* 同目录同内容 => 同 fp;任何文件内容变了 => fp 变。
|
|
23
|
+
*/
|
|
24
|
+
export function codeFingerprint(dir = DEFAULT_DIR) {
|
|
25
|
+
const files = []
|
|
26
|
+
try {
|
|
27
|
+
const walk = (current, prefix) => {
|
|
28
|
+
for (const entry of readdirSync(current, { withFileTypes: true })) {
|
|
29
|
+
if (entry.name.startsWith('.') || entry.name === 'node_modules') continue
|
|
30
|
+
const rel = `${prefix}${entry.name}`
|
|
31
|
+
if (entry.isDirectory()) {
|
|
32
|
+
walk(join(current, entry.name), `${rel}/`)
|
|
33
|
+
continue
|
|
34
|
+
}
|
|
35
|
+
if (entry.isFile() && CODE_EXT.test(entry.name)) files.push(rel)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
walk(dir, '')
|
|
39
|
+
files.sort()
|
|
40
|
+
const hash = createHash('sha256')
|
|
41
|
+
for (const rel of files) {
|
|
42
|
+
hash.update(rel)
|
|
43
|
+
hash.update('\0')
|
|
44
|
+
hash.update(readFileSync(join(dir, rel)))
|
|
45
|
+
hash.update('\0')
|
|
46
|
+
}
|
|
47
|
+
return { fp: hash.digest('hex').slice(0, 12), files: files.length }
|
|
48
|
+
} catch (error) {
|
|
49
|
+
return { fp: 'unknown', files: files.length, error: String(error?.message ?? error) }
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// CLI:node plugin\dsh-lil-whale-group\lib\code-fp.js
|
|
54
|
+
if (process.argv[1] && process.argv[1].endsWith('code-fp.js')) {
|
|
55
|
+
const { fp, files, error } = codeFingerprint()
|
|
56
|
+
console.log(error ? `codeFp=unknown files=${files} error=${error}` : `codeFp=${fp} files=${files}`)
|
|
57
|
+
}
|
package/lib/config.js
CHANGED
|
@@ -1,128 +1,147 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 这台机器的假设,全从这里读。
|
|
3
|
-
*
|
|
4
|
-
* 为什么要有这个文件:代码里写死 `<她的家>` 之类的东西,等于"这段程序只在这台机器上
|
|
5
|
-
* 成立"。打包给别人时最麻烦的不是改名,是这些路径 —— 装上以后跑不起来,报的还是一句
|
|
6
|
-
* `ENOENT: no such file or directory`,看不出是谁的问题。
|
|
7
|
-
*
|
|
8
|
-
* 读法(优先级从高到低):
|
|
9
|
-
* 1. 环境变量 `LIL_WHALE_CONFIG` 指向的 JSON 文件
|
|
10
|
-
* 2. 仓库根目录的 `lil-whale.config.json`
|
|
11
|
-
* 3. 一份"按标准布局"推出来的默认值(下面的 DEFAULTS)
|
|
12
|
-
*
|
|
13
|
-
* "标准布局"= 鱼塘根下 `app/` 是仓库、`homes/<键>/` 是鱼的家。所以别人克隆下来、
|
|
14
|
-
* 把鱼塘摆成这个形状,不写配置文件也能跑;只有布局不一样时才需要那份 JSON。
|
|
15
|
-
*
|
|
16
|
-
* 注意仓库根是从**本文件的位置**往上数出来的,不是从进程 cwd 数出来的 —— 宿主的 cwd 是
|
|
17
|
-
* "从哪儿启动的",我们的代码可能被 link 到别的 profile 目录里,cwd 靠不住。
|
|
18
|
-
*
|
|
19
|
-
* 字段命名(2026-11-14 改):每条鱼读的都是**自己**那份配置,所以 `self*` 指"这个进程
|
|
20
|
-
* 服务的那条鱼"。旧的 `her*` / `whale*` 是单鱼时代的名字(第二条鱼出现后 `whaleProfile`
|
|
21
|
-
* 在 fishball 的配置里只能填 `fishball`,读起来是错的),仍然接受,只作兼容别名。
|
|
22
|
-
*/
|
|
23
|
-
import { existsSync, readFileSync, statSync } from 'node:fs'
|
|
24
|
-
import { dirname, join, resolve } from 'node:path'
|
|
25
|
-
import { fileURLToPath } from 'node:url'
|
|
26
|
-
|
|
27
|
-
const HERE = dirname(fileURLToPath(import.meta.url))
|
|
28
|
-
// lib/ → dsh-lil-whale-group/ → plugin/ → 仓库根
|
|
29
|
-
const REPO_ROOT = resolve(HERE, '..', '..', '..')
|
|
30
|
-
|
|
31
|
-
const DEFAULTS = {
|
|
32
|
-
repoRoot: REPO_ROOT,
|
|
33
|
-
// 鱼塘布局(2026-11-13 起):<pond>\app 是仓库,鱼在 <pond>\homes\<键>。
|
|
34
|
-
// 配置丢了也不该指回旧布局(`<repo>\..\lil-whale` 那种),否则"默认值"会把新装的人带进老结构。
|
|
35
|
-
selfHome: resolve(REPO_ROOT, '..', 'homes', 'fish'),
|
|
36
|
-
selfWaker: join(REPO_ROOT, 'scripts', 'wake-fish.ps1'),
|
|
37
|
-
wakeMetrics: join(REPO_ROOT, 'logs', 'wake-metrics.log'),
|
|
38
|
-
pwshExe: process.platform === 'win32' ? 'pwsh.exe' : 'pwsh',
|
|
39
|
-
leeCwd: REPO_ROOT,
|
|
40
|
-
leeProfile: 'web',
|
|
41
|
-
selfProfile: 'fish',
|
|
42
|
-
// 这条鱼的进程开的端口。巡守要推门时敲的就是它;改端口必须同时改这里,否则巡守会去敲一扇没人的门。
|
|
43
|
-
selfPort: 3082,
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** 旧名字 → 新名字。旧配置仍然能读,只是会被换成 `self*`。 */
|
|
47
|
-
const LEGACY_KEYS = {
|
|
48
|
-
herHome: 'selfHome',
|
|
49
|
-
herDshHome: 'selfDshHome',
|
|
50
|
-
herWaker: 'selfWaker',
|
|
51
|
-
herPort: 'selfPort',
|
|
52
|
-
whaleProfile: 'selfProfile',
|
|
53
|
-
whaleCwd: 'selfCwd',
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
let cached = null
|
|
57
|
-
|
|
58
|
-
export function configPath() {
|
|
59
|
-
return process.env.LIL_WHALE_CONFIG || join(REPO_ROOT, 'lil-whale.config.json')
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 这台机器的假设,全从这里读。
|
|
3
|
+
*
|
|
4
|
+
* 为什么要有这个文件:代码里写死 `<她的家>` 之类的东西,等于"这段程序只在这台机器上
|
|
5
|
+
* 成立"。打包给别人时最麻烦的不是改名,是这些路径 —— 装上以后跑不起来,报的还是一句
|
|
6
|
+
* `ENOENT: no such file or directory`,看不出是谁的问题。
|
|
7
|
+
*
|
|
8
|
+
* 读法(优先级从高到低):
|
|
9
|
+
* 1. 环境变量 `LIL_WHALE_CONFIG` 指向的 JSON 文件
|
|
10
|
+
* 2. 仓库根目录的 `lil-whale.config.json`
|
|
11
|
+
* 3. 一份"按标准布局"推出来的默认值(下面的 DEFAULTS)
|
|
12
|
+
*
|
|
13
|
+
* "标准布局"= 鱼塘根下 `app/` 是仓库、`homes/<键>/` 是鱼的家。所以别人克隆下来、
|
|
14
|
+
* 把鱼塘摆成这个形状,不写配置文件也能跑;只有布局不一样时才需要那份 JSON。
|
|
15
|
+
*
|
|
16
|
+
* 注意仓库根是从**本文件的位置**往上数出来的,不是从进程 cwd 数出来的 —— 宿主的 cwd 是
|
|
17
|
+
* "从哪儿启动的",我们的代码可能被 link 到别的 profile 目录里,cwd 靠不住。
|
|
18
|
+
*
|
|
19
|
+
* 字段命名(2026-11-14 改):每条鱼读的都是**自己**那份配置,所以 `self*` 指"这个进程
|
|
20
|
+
* 服务的那条鱼"。旧的 `her*` / `whale*` 是单鱼时代的名字(第二条鱼出现后 `whaleProfile`
|
|
21
|
+
* 在 fishball 的配置里只能填 `fishball`,读起来是错的),仍然接受,只作兼容别名。
|
|
22
|
+
*/
|
|
23
|
+
import { existsSync, readFileSync, statSync } from 'node:fs'
|
|
24
|
+
import { dirname, join, resolve } from 'node:path'
|
|
25
|
+
import { fileURLToPath } from 'node:url'
|
|
26
|
+
|
|
27
|
+
const HERE = dirname(fileURLToPath(import.meta.url))
|
|
28
|
+
// lib/ → dsh-lil-whale-group/ → plugin/ → 仓库根
|
|
29
|
+
const REPO_ROOT = resolve(HERE, '..', '..', '..')
|
|
30
|
+
|
|
31
|
+
const DEFAULTS = {
|
|
32
|
+
repoRoot: REPO_ROOT,
|
|
33
|
+
// 鱼塘布局(2026-11-13 起):<pond>\app 是仓库,鱼在 <pond>\homes\<键>。
|
|
34
|
+
// 配置丢了也不该指回旧布局(`<repo>\..\lil-whale` 那种),否则"默认值"会把新装的人带进老结构。
|
|
35
|
+
selfHome: resolve(REPO_ROOT, '..', 'homes', 'fish'),
|
|
36
|
+
selfWaker: join(REPO_ROOT, 'scripts', 'wake-fish.ps1'),
|
|
37
|
+
wakeMetrics: join(REPO_ROOT, 'logs', 'wake-metrics.log'),
|
|
38
|
+
pwshExe: process.platform === 'win32' ? 'pwsh.exe' : 'pwsh',
|
|
39
|
+
leeCwd: REPO_ROOT,
|
|
40
|
+
leeProfile: 'web',
|
|
41
|
+
selfProfile: 'fish',
|
|
42
|
+
// 这条鱼的进程开的端口。巡守要推门时敲的就是它;改端口必须同时改这里,否则巡守会去敲一扇没人的门。
|
|
43
|
+
selfPort: 3082,
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 旧名字 → 新名字。旧配置仍然能读,只是会被换成 `self*`。 */
|
|
47
|
+
const LEGACY_KEYS = {
|
|
48
|
+
herHome: 'selfHome',
|
|
49
|
+
herDshHome: 'selfDshHome',
|
|
50
|
+
herWaker: 'selfWaker',
|
|
51
|
+
herPort: 'selfPort',
|
|
52
|
+
whaleProfile: 'selfProfile',
|
|
53
|
+
whaleCwd: 'selfCwd',
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let cached = null
|
|
57
|
+
|
|
58
|
+
export function configPath() {
|
|
59
|
+
return process.env.LIL_WHALE_CONFIG || join(REPO_ROOT, 'lil-whale.config.json')
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 配置从哪来。三种状态别混:(r242)
|
|
64
|
+
* env 环境变量给了,文件在
|
|
65
|
+
* env-missing 环境变量给了,文件不在 —— 是**配置错误**,不是"没有配置";
|
|
66
|
+
* 这时绝不改读仓库里那份(那会把测试指到真鱼塘)
|
|
67
|
+
* repo 环境变量没给,仓库根那份在
|
|
68
|
+
* default 都没有,按标准布局推
|
|
69
|
+
*/
|
|
70
|
+
function configSourceOf(path) {
|
|
71
|
+
const fromEnv = Boolean(process.env.LIL_WHALE_CONFIG)
|
|
72
|
+
if (fromEnv) return existsSync(path) ? 'env' : 'env-missing'
|
|
73
|
+
return existsSync(path) ? 'repo' : 'default'
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 派生字段(只由上面几个推出来,不许单独写死)。 */
|
|
77
|
+
function derive(c) {
|
|
78
|
+
// 先把旧名字抬成新名字(新名字优先)。
|
|
79
|
+
const lifted = { ...c }
|
|
80
|
+
for (const [oldKey, newKey] of Object.entries(LEGACY_KEYS)) {
|
|
81
|
+
if (lifted[newKey] === undefined && lifted[oldKey] !== undefined) lifted[newKey] = lifted[oldKey]
|
|
82
|
+
}
|
|
83
|
+
const selfHome = lifted.selfHome
|
|
84
|
+
// 共享区(pool)在她家之外:群、多成员账本都放这儿。
|
|
85
|
+
// 为什么不在她家里:群是共享空间,不是她的私人物品;第二条鱼出现后,
|
|
86
|
+
// 第二条的群文件写进第一条的家就是越界(Fabian 2026-11-12 拍板)。
|
|
87
|
+
// 她家里留 Junction 指回 pool,所以她的路径一个字都不用改。
|
|
88
|
+
// selfHome = <pond>\homes\<键> ⇒ 上两级才是 pond。
|
|
89
|
+
const poolDir = lifted.poolDir || resolve(selfHome, '..', '..', 'pool')
|
|
90
|
+
const out = {
|
|
91
|
+
...lifted,
|
|
92
|
+
poolDir,
|
|
93
|
+
poolStateDir: lifted.poolStateDir || join(poolDir, 'state'),
|
|
94
|
+
// 私聊是「两个人之间的窗口」,不放进群目录:群目录是共享区里公开的那一半(Fabian 2026-11-12 拍板)。
|
|
95
|
+
dmDir: lifted.dmDir || join(poolDir, 'dm'),
|
|
96
|
+
// 名册:谁是谁、叫什么、哪条鱼、端口、爹是谁。多鱼之后这些不许再写死在代码里。
|
|
97
|
+
membersFile: lifted.membersFile || join(poolDir, 'members.json'),
|
|
98
|
+
selfPort: Number(lifted.selfPort) || 3082,
|
|
99
|
+
groupDir: lifted.groupDir || join(poolDir, 'group'),
|
|
100
|
+
selfDshHome: lifted.selfDshHome || join(selfHome, 'dsh-home'),
|
|
101
|
+
selfCwd: lifted.selfCwd || selfHome,
|
|
102
|
+
chatCwd: lifted.chatCwd || dirname(lifted.repoRoot),
|
|
103
|
+
watchmanPidFile: lifted.watchmanPidFile || join(poolDir, 'state', 'watchman.pid'),
|
|
104
|
+
}
|
|
105
|
+
// 兼容别名:旧代码/旧脚本还在用 her* / whale*,给它们留一份只读映射。
|
|
106
|
+
out.herHome = out.selfHome
|
|
107
|
+
out.herDshHome = out.selfDshHome
|
|
108
|
+
out.herWaker = out.selfWaker
|
|
109
|
+
out.herPort = out.selfPort
|
|
110
|
+
out.whaleProfile = out.selfProfile
|
|
111
|
+
out.whaleCwd = out.selfCwd
|
|
112
|
+
return out
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 读一次,缓存起来(进程生命周期内不变 —— 改配置要重启,这是有意的:
|
|
117
|
+
* 热改路径会让"这个进程到底在读哪份文件"变成一个没人答得上来的问题)。
|
|
118
|
+
*/
|
|
119
|
+
export function config() {
|
|
120
|
+
if (cached) return cached
|
|
121
|
+
const path = configPath()
|
|
122
|
+
let raw = {}
|
|
123
|
+
let error = null
|
|
124
|
+
if (existsSync(path)) {
|
|
125
|
+
try {
|
|
126
|
+
raw = JSON.parse(readFileSync(path, 'utf8'))
|
|
127
|
+
} catch (e) {
|
|
128
|
+
// 配置写坏了要说出来,不能安静地用默认值跑 —— 那会让人以为自己改对了
|
|
129
|
+
error = `config 读取失败(${path}):${e.message}`
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
let mtime = null
|
|
133
|
+
try { mtime = statSync(path).mtime.toISOString() } catch { /* 文件不在就没有 mtime */ }
|
|
134
|
+
const source = configSourceOf(path)
|
|
135
|
+
if (source === 'env-missing' && !error) {
|
|
136
|
+
// 环境变量给了 = "我要用这一份";文件不在是配置错误,不能安静地用默认值继续跑。
|
|
137
|
+
error = `LIL_WHALE_CONFIG 指向的文件不存在:${path}`
|
|
138
|
+
}
|
|
139
|
+
cached = { ...derive({ ...DEFAULTS, ...raw }), _path: path, _exists: existsSync(path), _error: error, _mtime: mtime, _source: source }
|
|
140
|
+
return cached
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** 给启动痕迹用的短摘要:哪份配置、这条鱼的家在哪儿。 */
|
|
144
|
+
export function configSummary() {
|
|
145
|
+
const c = config()
|
|
146
|
+
return { path: c._path, source: c._source, exists: c._exists, error: c._error, selfHome: c.selfHome, groupDir: c.groupDir }
|
|
147
|
+
}
|