dsh-mobile-plugin 0.5.35 → 0.5.36
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/impl.js +0 -7
- package/lib/mobile/answer-sources.js +223 -0
- package/lib/mobile/approval-memory.js +1 -1
- package/lib/mobile/events.js +0 -20
- package/lib/mobile/extension-data.js +105 -5
- package/lib/mobile/history-derive.js +54 -1
- package/lib/mobile/notification-prefs.js +2 -2
- package/lib/mobile/permissions.js +8 -5
- package/lib/mobile/plugin-market.js +103 -4
- package/lib/mobile/run-timeline.js +317 -0
- package/lib/mobile/session-stop-summary.js +122 -0
- package/lib/mobile/stats-ledger.cjs +45 -0
- package/lib/mobile/stop-summary.js +146 -0
- package/lib/mobile/third-party-extensions.js +86 -0
- package/lib/mobile/tool-result-cache.js +30 -2
- package/lib/panel/index.html +15 -15
- package/lib/relay/lan-proxy.js +20 -11
- package/lib/relay/tunnel.js +28 -12
- package/lib/routes/mobile-api.js +25 -19
- package/lib/shared/derived-cache.js +121 -0
- package/lib/shared/http-util.js +55 -18
- package/package.json +1 -2
- package/config/agent-presets/supervisor/agent.cordis.yml +0 -237
- package/config/agent-presets/supervisor/preset.yml +0 -3
- package/lib/mobile/auto-review-log.js +0 -160
- package/lib/mobile/knowledge-base.js +0 -481
- package/lib/mobile/mail.js +0 -621
- package/lib/mobile/supervisor-broadcast.js +0 -503
- package/lib/mobile/supervisor-memory/index.js +0 -305
- package/lib/mobile/supervisor-runtime/index.js +0 -54
- package/lib/mobile/supervisor-runtime/inject.js +0 -218
- package/lib/mobile/supervisor-runtime/judgment.js +0 -113
- package/lib/mobile/supervisor-tools.js +0 -419
- package/lib/mobile/task-ledger.js +0 -381
|
@@ -1,237 +0,0 @@
|
|
|
1
|
-
# 「总管」的人设:complete: true 让这段文字成为这一档会话**整份**系统提示词
|
|
2
|
-
# (其余身份、后缀、工具说明全部压掉),includeRuntimeContext: false 再关掉可选
|
|
3
|
-
# 运行时上下文。所以下面这段必须自足 —— 工具仍然照常提供,但没有额外的工具说明
|
|
4
|
-
# 文字会追加进来。改这里时记住:这是在改一个 agent 的人格,不是加一条提示。
|
|
5
|
-
- id: persona
|
|
6
|
-
name: '@deepseek-ai/dsh-persona'
|
|
7
|
-
config:
|
|
8
|
-
prefix: |
|
|
9
|
-
你是用户的「总管」——他手机那头一个能说话的对象。你们在一通电话里交流:他看到屏幕,听到你的声音。
|
|
10
|
-
|
|
11
|
-
## 你不干什么
|
|
12
|
-
你不亲自写代码、不跑命令、不替项目干活。真正干活的是各条会话里的对话。你的价值是看得全、说得清、调度得对。
|
|
13
|
-
|
|
14
|
-
## 你怎么说话
|
|
15
|
-
- 口语、短句,像人在电话里说话。一口气能说完;一次别超过两三句。
|
|
16
|
-
- 不念代码、表格、链接、文件路径。要细看的东西留在屏幕上,你只说结论。
|
|
17
|
-
- 用户随时可能打断你。被打断就立刻停嘴,听他说。
|
|
18
|
-
|
|
19
|
-
## 你手上有什么
|
|
20
|
-
- 所有电脑上、所有正在跑的会话:谁在跑、谁跑完了、谁报错、谁在等人回答。
|
|
21
|
-
- 一组工具:列会话、看某条会话的进度、读某条会话的内容、往某条会话发消息、新建会话、中断会话。
|
|
22
|
-
|
|
23
|
-
## 什么时候开口
|
|
24
|
-
- 有事才说,别一条条地蹦。几件事一起发生,先说最要紧的,合成一句话。
|
|
25
|
-
- 没活的时候,用户想聊就陪他聊;不要主动翻出几天前的旧事。
|
|
26
|
-
- 不确定的别硬说。连不上电脑,就直说没连上;不知道就说不知道。
|
|
27
|
-
|
|
28
|
-
## 打断与停活是两件事
|
|
29
|
-
- 用户说「停」「别说了」——那是让你先闭嘴,不是取消任务。活照样在电脑上跑。
|
|
30
|
-
- 只有他明确说「那个停掉」,才去中断那条会话,并且做完告诉他。
|
|
31
|
-
|
|
32
|
-
## 记住的三本账
|
|
33
|
-
- 任务账:他手上还挂着几件事、每件到哪一步、哪个在等人答。每次开口前先过一眼。
|
|
34
|
-
- 会话历史:宿主自己管,会压缩,细节可能丢,接受它。
|
|
35
|
-
- 知识库:他说过、定下来的事,只往里面加,不改旧条目。要删要改,让他在手机上自己来。
|
|
36
|
-
|
|
37
|
-
## 动手之前先对一下
|
|
38
|
-
要开新会话、要往某条会话发东西,先把「我准备让哪条、做什么」跟他一句话说清,他点头再动。他说得含糊就先问一句,别自己猜。
|
|
39
|
-
complete: true
|
|
40
|
-
includeRuntimeContext: false
|
|
41
|
-
- id: agent-instructions
|
|
42
|
-
name: '@deepseek-ai/dsh-agent-instructions'
|
|
43
|
-
config:
|
|
44
|
-
maxBytes: 65536
|
|
45
|
-
- id: tool-bash
|
|
46
|
-
name: '@deepseek-ai/dsh-tool-bash'
|
|
47
|
-
disabled: !!js process.platform === 'win32'
|
|
48
|
-
- id: tool-pwsh
|
|
49
|
-
name: '@deepseek-ai/dsh-tool-pwsh'
|
|
50
|
-
disabled: !!js process.platform !== 'win32'
|
|
51
|
-
- id: tool-fs
|
|
52
|
-
name: '@deepseek-ai/dsh-tool-fs'
|
|
53
|
-
- id: tool-fs-search
|
|
54
|
-
name: '@deepseek-ai/dsh-tool-fs-search'
|
|
55
|
-
config:
|
|
56
|
-
sampleOverCapGlobResults: false
|
|
57
|
-
- id: tool-jobs
|
|
58
|
-
name: '@deepseek-ai/dsh-tool-jobs'
|
|
59
|
-
- id: skill-filesystem
|
|
60
|
-
name: '@deepseek-ai/dsh-skill-filesystem'
|
|
61
|
-
- id: tool-skill
|
|
62
|
-
name: '@deepseek-ai/dsh-tool-skill'
|
|
63
|
-
- id: command-goal
|
|
64
|
-
name: '@deepseek-ai/dsh-command-goal'
|
|
65
|
-
- id: tool-goal
|
|
66
|
-
name: '@deepseek-ai/dsh-tool-goal'
|
|
67
|
-
- id: planning
|
|
68
|
-
name: cordis:group
|
|
69
|
-
group: true
|
|
70
|
-
isolate:
|
|
71
|
-
planMode: true
|
|
72
|
-
config:
|
|
73
|
-
- id: plan-mode
|
|
74
|
-
name: '@deepseek-ai/dsh-plan-mode'
|
|
75
|
-
config:
|
|
76
|
-
section: >
|
|
77
|
-
You are in plan mode. Stay in plan mode until exit_plan_mode
|
|
78
|
-
succeeds or the user switches the session mode. Imperative
|
|
79
|
-
language to implement changes means plan the implementation, not
|
|
80
|
-
execute it. A user's conversational agreement — including an
|
|
81
|
-
answer confirming something you asked — approves nothing and
|
|
82
|
-
does not end plan mode; fold the confirmed decision into the
|
|
83
|
-
plan and submit it through exit_plan_mode.
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
Explore first. Use non-mutating reads, searches, static
|
|
87
|
-
analysis, and checks to ground the plan in the actual
|
|
88
|
-
repository. Do not edit or write files, change configuration,
|
|
89
|
-
run formatters or code generation that rewrites tracked files,
|
|
90
|
-
commit, or otherwise carry out the plan. Prefer existing
|
|
91
|
-
functions and patterns over new machinery.
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
The tool catalog stays the same across modes for request-cache
|
|
95
|
-
stability. These plan-mode rules override any later tool
|
|
96
|
-
description or guidance that suggests using mutation tools;
|
|
97
|
-
those tools remain listed to keep the tool catalog unchanged. Do
|
|
98
|
-
not use todo_write to track this planning phase: it tracks
|
|
99
|
-
implementation after an approved plan, while the plan itself
|
|
100
|
-
belongs in exit_plan_mode.
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
Resolve discoverable facts by inspection. Use ask_user_question
|
|
104
|
-
only for user-owned choices or material ambiguity that
|
|
105
|
-
inspection cannot answer. Do not ask the user where code lives
|
|
106
|
-
or how current behavior works when you can find out.
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
Make the plan decision-complete: state the goal and success
|
|
110
|
-
criteria; group implementation changes by subsystem; identify
|
|
111
|
-
public API, schema, and data-flow changes; cover edge cases,
|
|
112
|
-
failure modes, tests, acceptance criteria, and explicit
|
|
113
|
-
assumptions. Keep it concise enough to review but detailed
|
|
114
|
-
enough that another engineer can implement it without making
|
|
115
|
-
design decisions.
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
When ready, call exit_plan_mode with the complete plan markdown,
|
|
119
|
-
starting with a # title. Make exit_plan_mode the only and final
|
|
120
|
-
tool call in that assistant response: it presents the plan for
|
|
121
|
-
approval, and implementation begins only in a later step after
|
|
122
|
-
approval. Do not paste the final plan as a plain reply or ask
|
|
123
|
-
"should I proceed?" through prose or ask_user_question. If
|
|
124
|
-
review rejects it, incorporate the feedback and present again.
|
|
125
|
-
If the review channel is unavailable or aborted, stay in plan
|
|
126
|
-
mode and ask the user to switch modes manually; do not proceed
|
|
127
|
-
with implementation.
|
|
128
|
-
- id: compaction
|
|
129
|
-
name: cordis:group
|
|
130
|
-
group: true
|
|
131
|
-
isolate:
|
|
132
|
-
compaction: true
|
|
133
|
-
toolResultPruner: true
|
|
134
|
-
config:
|
|
135
|
-
- id: compaction-basic
|
|
136
|
-
name: '@deepseek-ai/dsh-compaction-basic'
|
|
137
|
-
- id: command-compact
|
|
138
|
-
name: '@deepseek-ai/dsh-command-compact'
|
|
139
|
-
- id: tool-result-pruner
|
|
140
|
-
name: '@deepseek-ai/dsh-compaction-tool-result-pruner'
|
|
141
|
-
config:
|
|
142
|
-
thresholdChars: 8192
|
|
143
|
-
headChars: 4096
|
|
144
|
-
tailChars: 1024
|
|
145
|
-
- id: delegation
|
|
146
|
-
name: cordis:group
|
|
147
|
-
group: true
|
|
148
|
-
isolate:
|
|
149
|
-
workflowEngine: true
|
|
150
|
-
config:
|
|
151
|
-
- id: tool-subagent-control
|
|
152
|
-
name: '@deepseek-ai/dsh-tool-subagent-control'
|
|
153
|
-
# 独立的 `list_agents` 入口只在 0.2.1 及更早存在;0.2.2 把它并进了主包,
|
|
154
|
-
# 子路径没了。这一行保留是为了老运行时,启动时宿主会按「这一代认不认这个
|
|
155
|
-
# 子路径」把副本里的这一行整行拿掉(见 lib/host-compat.cjs 的 dropRows)。
|
|
156
|
-
- id: tool-subagent-list-agents
|
|
157
|
-
name: '@deepseek-ai/dsh-tool-subagent-control/list-agents'
|
|
158
|
-
# 子代理挑模型的开关,两代运行时键名不同:0.2.1 及更早读
|
|
159
|
-
# `modelSelectionSettings`,0.2.2 起改成 `modelSelection`(另加
|
|
160
|
-
# `modelDiscovery` 才让模型查得到可选模型)。两个键都写:各自的 schema 只认
|
|
161
|
-
# 自己那个、另一个原样忽略,所以同一份预设挂到哪一代都对。
|
|
162
|
-
- id: tool-subagent
|
|
163
|
-
name: '@deepseek-ai/dsh-tool-subagent'
|
|
164
|
-
config:
|
|
165
|
-
provider: spawn
|
|
166
|
-
toolName: subagent
|
|
167
|
-
modelSelectionSettings: true
|
|
168
|
-
modelSelection: true
|
|
169
|
-
modelDiscovery: true
|
|
170
|
-
backgroundMode: continuable
|
|
171
|
-
- id: tool-subagent-fork
|
|
172
|
-
name: '@deepseek-ai/dsh-tool-subagent'
|
|
173
|
-
config:
|
|
174
|
-
provider: fork
|
|
175
|
-
toolName: subagent_fork
|
|
176
|
-
backgroundMode: continuable
|
|
177
|
-
- id: tool-subagent-codex
|
|
178
|
-
name: '@deepseek-ai/dsh-tool-subagent'
|
|
179
|
-
disabled: true
|
|
180
|
-
config:
|
|
181
|
-
provider: codex
|
|
182
|
-
toolName: subagent_codex
|
|
183
|
-
backgroundMode: one-shot
|
|
184
|
-
maxDepth: provider-managed
|
|
185
|
-
- id: tool-subagent-claude-code
|
|
186
|
-
name: '@deepseek-ai/dsh-tool-subagent'
|
|
187
|
-
disabled: true
|
|
188
|
-
config:
|
|
189
|
-
provider: claude-code
|
|
190
|
-
toolName: subagent_claude_code
|
|
191
|
-
backgroundMode: one-shot
|
|
192
|
-
maxDepth: provider-managed
|
|
193
|
-
# `tool-workflow` is deliberately absent: it injects `workflowEngine`,
|
|
194
|
-
# which only `@deepseek-ai/dsh-workflow-ptc` provides, and that package
|
|
195
|
-
# has no 0.1.1-rc.2 release (its earliest is 0.1.6-alpha.1). The App's
|
|
196
|
-
# workflow surfaces therefore have nothing to run on this host.
|
|
197
|
-
- id: tool-ralph
|
|
198
|
-
name: '@deepseek-ai/dsh-tool-ralph'
|
|
199
|
-
disabled: true
|
|
200
|
-
config:
|
|
201
|
-
subagentProvider: spawn
|
|
202
|
-
maxRounds: 64
|
|
203
|
-
- id: tool-todo
|
|
204
|
-
name: '@deepseek-ai/dsh-tool-todo'
|
|
205
|
-
config:
|
|
206
|
-
allowParallelInProgress: true
|
|
207
|
-
- id: tool-web
|
|
208
|
-
name: '@deepseek-ai/dsh-tool-web'
|
|
209
|
-
config:
|
|
210
|
-
fetch: true
|
|
211
|
-
searchTimeoutMs: 60000
|
|
212
|
-
# Without this row the mobile host's tool catalog has no `ask_user_question`,
|
|
213
|
-
# so the App's question dock could never appear: the model simply reports that
|
|
214
|
-
# the tool is unavailable. The plugin's `lib/mobile/questions.js` listens on
|
|
215
|
-
# the `user-questions/request` hook (0.2.x shape) and answers it — only the
|
|
216
|
-
# tool row was missing.
|
|
217
|
-
#
|
|
218
|
-
# 这里的 `name` 是**占位**:无头宿主启动时会把同代那一份的入口绝对路径写进
|
|
219
|
-
# 一份副本(`host.js` 的 syncPresetDir),挂载的是运行时自己的那一代。这个包的
|
|
220
|
-
# peerDependencies 逐版本紧咬,对不上时 `dsh-app-boot` 会**整行禁用**(不是降级),
|
|
221
|
-
# 而钉死哪一代都不可能覆盖每台机器上装的 DSH —— 判据交给运行期,别在这里写死。
|
|
222
|
-
- id: tool-ask-user
|
|
223
|
-
name: '@deepseek-ai/dsh-tool-ask-user'
|
|
224
|
-
# `present` 是模型**显式声明交付文件**的那一行。桌面 `standard` preset 里一直有它
|
|
225
|
-
# (`@deepseek-ai/dsh-web-app/presets/standard.patch.yml`),手机这份漏了 —— 于是手机
|
|
226
|
-
# 上的模型完全没有「把文件交付给用户」这个动作,只能写完文件什么也不说;App 那边只好
|
|
227
|
-
# 拿 write/edit 成功去反推,结果「每改一次都冒一张卡、用命令行改的却一张都没有」。
|
|
228
|
-
#
|
|
229
|
-
# 它只做两件事:校验路径是会话文件系统里真实存在的普通文件,然后把
|
|
230
|
-
# `deliverables/presented` 事件追加进会话日志。卡片该不该出现,由**模型叫不叫这个工具**
|
|
231
|
-
# 决定,不由「有没有文件被改」决定 —— 这是桌面的口径。
|
|
232
|
-
#
|
|
233
|
-
# 版本同样由运行期决定(见上面 tool-ask-user 那段):0.2.1 的 `present` 要
|
|
234
|
-
# `dsh-agent` 恰好是同一代,对不上时 `dsh-app-boot` 会整行禁用,模型只会说
|
|
235
|
-
# 「没有这个工具」。`name` 是占位,宿主启动时换成本次运行时的入口。
|
|
236
|
-
- id: tool-present
|
|
237
|
-
name: '@deepseek-ai/dsh-tool-present'
|
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 自动审阅档的判定账本:自动放行 / 推卡过哪几次,连同当时看到的事实。
|
|
5
|
-
*
|
|
6
|
-
* ## 为什么落盘,而不是写进会话日志
|
|
7
|
-
*
|
|
8
|
-
* 判定记录天然是「旁路」:它只用于事后翻账(「刚才那条命令怎么没弹卡」),不参与任何派生子
|
|
9
|
-
* 逻辑。可会话日志是宿主的严格结构 —— `dsh-session-persistence` 读盘时遇到不认识的事件类型
|
|
10
|
-
* 且没标 ignorable,会直接拒绝解释整份日志。一条自定义事件写进去,`try/catch` 兜不住,
|
|
11
|
-
* 代价是这条会话重启后可能整份打不开。所以记录落在插件自己的存储里
|
|
12
|
-
* (`<DSH_HOME>/storages/mobile-plugin/auto-review-log.json`),跟着机器走,不进任何一条对话,
|
|
13
|
-
* 宿主怎么读怎么写都碰不到它。
|
|
14
|
-
*
|
|
15
|
-
* ## 条目形状
|
|
16
|
-
*
|
|
17
|
-
* { at, sessionId, action, verdict, decidedBy, targetMode, toolName, callId, fact }
|
|
18
|
-
*
|
|
19
|
-
* - `action`:`allow` / `card`(本档不产 `reject`,见 `auto-review.js` 文件头)。
|
|
20
|
-
* - `verdict`:`within` / `boundary` / `unsure`;`decidedBy` 是谁拍的板
|
|
21
|
-
* (`judge` 判定、`watching` 有人看着、`structure` 越界结构、`facts` 拿不到真实参数)。
|
|
22
|
-
* - `fact`:判定当时看到的事实行,说明「凭什么这么判」。
|
|
23
|
-
*
|
|
24
|
-
* ## 三条边界
|
|
25
|
-
*
|
|
26
|
-
* - **只追加、只留最近 N 条**:这是账,不是状态机;超了挤掉最老的,没人需要它无限长。
|
|
27
|
-
* - **读坏当空、写坏不抛**:文件不在 / 读一半 / 形状不对一律当「还没记过」,绝不抛。
|
|
28
|
-
* 记账坏了不该影响正在跑的判定。
|
|
29
|
-
* - **路径可覆盖仅供测试**:`DSH_MOBILE_AUTO_REVIEW_LOG` 与 task-ledger 的
|
|
30
|
-
* `DSH_MOBILE_TASK_LEDGER` 同一口径,生产不设。
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
const fs = require('node:fs');
|
|
34
|
-
const path = require('node:path');
|
|
35
|
-
|
|
36
|
-
const { dshHome } = require('../shared/paths.js');
|
|
37
|
-
|
|
38
|
-
/** 账本格式版本:将来改结构时据此决定丢弃还是迁移。 */
|
|
39
|
-
const VERSION = 1;
|
|
40
|
-
|
|
41
|
-
/** 最多留最近多少条。判定本身很稀疏(只有没人看时才记一笔),200 条够翻很久。 */
|
|
42
|
-
const MAX_ITEMS = 200;
|
|
43
|
-
|
|
44
|
-
const MAX_TEXT = 400;
|
|
45
|
-
|
|
46
|
-
/** 账本文件。路径每次调用时解析,测试可注入 DSH_HOME 或直接给路径。 */
|
|
47
|
-
function logFile() {
|
|
48
|
-
return process.env.DSH_MOBILE_AUTO_REVIEW_LOG
|
|
49
|
-
|| path.join(dshHome(), 'storages', 'mobile-plugin', 'auto-review-log.json');
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function isRecord(value) {
|
|
53
|
-
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/** 收窄成单行字符串:账本是给人扫一眼的,换行与连续空白在这里没有意义。 */
|
|
57
|
-
function textOf(value, max) {
|
|
58
|
-
if (typeof value !== 'string') return '';
|
|
59
|
-
return value.replace(/\s+/g, ' ').trim().slice(0, max);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** 一条记录的清洗:认不出的字段归空,形状不对整条丢。 */
|
|
63
|
-
function normalizeEntry(raw) {
|
|
64
|
-
if (!isRecord(raw)) return null;
|
|
65
|
-
const at = Number.isFinite(raw.at) && raw.at > 0 ? raw.at : 0;
|
|
66
|
-
const action = textOf(raw.action, 32);
|
|
67
|
-
if (!at || !action) return null;
|
|
68
|
-
return {
|
|
69
|
-
at,
|
|
70
|
-
sessionId: textOf(raw.sessionId, 200),
|
|
71
|
-
action,
|
|
72
|
-
verdict: textOf(raw.verdict, 32),
|
|
73
|
-
decidedBy: textOf(raw.decidedBy, 32),
|
|
74
|
-
targetMode: textOf(raw.targetMode, 32) || null,
|
|
75
|
-
toolName: textOf(raw.toolName, 80) || null,
|
|
76
|
-
callId: textOf(raw.callId, 120) || null,
|
|
77
|
-
fact: textOf(raw.fact, MAX_TEXT),
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function emptyLog() {
|
|
82
|
-
return { version: VERSION, items: [] };
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** 读账。文件不存在 / 读不动 / JSON 坏了 / 形状不对,一律当空,绝不抛。 */
|
|
86
|
-
function readLog() {
|
|
87
|
-
let raw;
|
|
88
|
-
try {
|
|
89
|
-
raw = JSON.parse(fs.readFileSync(logFile(), 'utf8'));
|
|
90
|
-
} catch {
|
|
91
|
-
return emptyLog();
|
|
92
|
-
}
|
|
93
|
-
if (!isRecord(raw) || !Array.isArray(raw.items)) return emptyLog();
|
|
94
|
-
const items = [];
|
|
95
|
-
for (const entry of raw.items) {
|
|
96
|
-
const normalized = normalizeEntry(entry);
|
|
97
|
-
if (normalized) items.push(normalized);
|
|
98
|
-
}
|
|
99
|
-
return { version: VERSION, items };
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* 原子写:先写临时文件再 rename。宿主随时可能被杀,半个 JSON 会让整本账从此读不出来。
|
|
104
|
-
* 写失败安静放弃,返回 false,不抛。
|
|
105
|
-
*/
|
|
106
|
-
function saveLog(log) {
|
|
107
|
-
const file = logFile();
|
|
108
|
-
const next = { version: VERSION, items: log.items };
|
|
109
|
-
try {
|
|
110
|
-
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
111
|
-
const tmp = `${file}.${process.pid}.tmp`;
|
|
112
|
-
fs.writeFileSync(tmp, `${JSON.stringify(next, null, 2)}\n`, { mode: 0o600 });
|
|
113
|
-
fs.renameSync(tmp, file);
|
|
114
|
-
return true;
|
|
115
|
-
} catch {
|
|
116
|
-
return false;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/** 追加一条。返回是否落盘(旁路,调用方不该因此改行为)。 */
|
|
121
|
-
function append(entry) {
|
|
122
|
-
const source = isRecord(entry) ? entry : {};
|
|
123
|
-
const normalized = normalizeEntry({ ...source, at: Date.now() });
|
|
124
|
-
if (!normalized) return false;
|
|
125
|
-
const log = readLog();
|
|
126
|
-
log.items.push(normalized);
|
|
127
|
-
if (log.items.length > MAX_ITEMS) log.items = log.items.slice(log.items.length - MAX_ITEMS);
|
|
128
|
-
return saveLog(log);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* 按会话翻账,最近的在前。不给 `sessionId` 就是全部。
|
|
133
|
-
* `limit` 缺省给一小把(翻账的时机通常是「刚才那一条」)。
|
|
134
|
-
*/
|
|
135
|
-
function list(sessionId, limit) {
|
|
136
|
-
const id = textOf(sessionId, 200);
|
|
137
|
-
const max = Number.isFinite(limit) && limit > 0 ? Math.floor(limit) : 20;
|
|
138
|
-
const items = readLog().items;
|
|
139
|
-
const picked = id ? items.filter((item) => item.sessionId === id) : items;
|
|
140
|
-
return picked.slice(-max).reverse();
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/** 测试用:清掉落盘文件,回到「还没记过」。 */
|
|
144
|
-
function resetForTest() {
|
|
145
|
-
try {
|
|
146
|
-
fs.rmSync(logFile(), { force: true });
|
|
147
|
-
} catch {
|
|
148
|
-
// 本来就没有。
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
module.exports = {
|
|
153
|
-
VERSION,
|
|
154
|
-
MAX_ITEMS,
|
|
155
|
-
logFile,
|
|
156
|
-
readLog,
|
|
157
|
-
append,
|
|
158
|
-
list,
|
|
159
|
-
resetForTest,
|
|
160
|
-
};
|