@dabuliu_wsy/dsh-im 0.0.0-stage → 0.2.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 +21 -0
- package/README.md +197 -3
- package/cordis.patch.yml +4 -0
- package/dist/core/agent-state.d.ts +38 -0
- package/dist/core/agent-state.js +59 -0
- package/dist/core/arbiter-bridge.d.ts +25 -0
- package/dist/core/arbiter-bridge.js +20 -0
- package/dist/core/arbiter.d.ts +29 -0
- package/dist/core/arbiter.js +59 -0
- package/dist/core/avatar-brands.d.ts +23 -0
- package/dist/core/avatar-brands.js +75 -0
- package/dist/core/avatar-presets.d.ts +21 -0
- package/dist/core/avatar-presets.js +38 -0
- package/dist/core/avatar.d.ts +1 -0
- package/dist/core/avatar.js +18 -0
- package/dist/core/blocked.d.ts +12 -0
- package/dist/core/blocked.js +23 -0
- package/dist/core/budget-plan.d.ts +41 -0
- package/dist/core/budget-plan.js +69 -0
- package/dist/core/calibration-samples.d.ts +77 -0
- package/dist/core/calibration-samples.js +168 -0
- package/dist/core/calibrator.d.ts +57 -0
- package/dist/core/calibrator.js +125 -0
- package/dist/core/chapter-tree.d.ts +128 -0
- package/dist/core/chapter-tree.js +400 -0
- package/dist/core/consensus.d.ts +135 -0
- package/dist/core/consensus.js +394 -0
- package/dist/core/context-budget.d.ts +153 -0
- package/dist/core/context-budget.js +183 -0
- package/dist/core/curator.d.ts +28 -0
- package/dist/core/curator.js +190 -0
- package/dist/core/dual-window.d.ts +86 -0
- package/dist/core/dual-window.js +175 -0
- package/dist/core/events.d.ts +110 -0
- package/dist/core/events.js +117 -0
- package/dist/core/facts.d.ts +114 -0
- package/dist/core/facts.js +400 -0
- package/dist/core/fence.d.ts +34 -0
- package/dist/core/fence.js +129 -0
- package/dist/core/floor.d.ts +18 -0
- package/dist/core/floor.js +80 -0
- package/dist/core/ids.d.ts +11 -0
- package/dist/core/ids.js +29 -0
- package/dist/core/im.d.ts +167 -0
- package/dist/core/im.js +1307 -0
- package/dist/core/index.d.ts +33 -0
- package/dist/core/index.js +33 -0
- package/dist/core/log.d.ts +21 -0
- package/dist/core/log.js +63 -0
- package/dist/core/memory-claims.d.ts +67 -0
- package/dist/core/memory-claims.js +164 -0
- package/dist/core/memory-context.d.ts +121 -0
- package/dist/core/memory-context.js +303 -0
- package/dist/core/memory-visibility.d.ts +58 -0
- package/dist/core/memory-visibility.js +36 -0
- package/dist/core/memory.d.ts +12 -0
- package/dist/core/memory.js +73 -0
- package/dist/core/metrics.d.ts +49 -0
- package/dist/core/metrics.js +89 -0
- package/dist/core/multi-hop.d.ts +116 -0
- package/dist/core/multi-hop.js +278 -0
- package/dist/core/persist.d.ts +21 -0
- package/dist/core/persist.js +94 -0
- package/dist/core/proactive.d.ts +54 -0
- package/dist/core/proactive.js +108 -0
- package/dist/core/prompt.d.ts +82 -0
- package/dist/core/prompt.js +164 -0
- package/dist/core/receipt.d.ts +107 -0
- package/dist/core/receipt.js +86 -0
- package/dist/core/retrieval.d.ts +69 -0
- package/dist/core/retrieval.js +260 -0
- package/dist/core/role-state.d.ts +16 -0
- package/dist/core/role-state.js +65 -0
- package/dist/core/runtime.d.ts +137 -0
- package/dist/core/runtime.js +574 -0
- package/dist/core/summary.d.ts +19 -0
- package/dist/core/summary.js +96 -0
- package/dist/core/text.d.ts +10 -0
- package/dist/core/text.js +45 -0
- package/dist/core/time.d.ts +22 -0
- package/dist/core/time.js +84 -0
- package/dist/core/tool-schema.d.ts +12 -0
- package/dist/core/tool-schema.js +92 -0
- package/dist/core/tools.d.ts +137 -0
- package/dist/core/tools.js +348 -0
- package/dist/core/types.d.ts +400 -0
- package/dist/core/types.js +1 -0
- package/dist/core/window.d.ts +6 -0
- package/dist/core/window.js +7 -0
- package/dist/core/workspace-node.d.ts +28 -0
- package/dist/core/workspace-node.js +232 -0
- package/dist/core/workspace.d.ts +9 -0
- package/dist/core/workspace.js +45 -0
- package/dist/host/agent-session.d.ts +279 -0
- package/dist/host/agent-session.js +935 -0
- package/dist/host/attachment-gc.d.ts +37 -0
- package/dist/host/attachment-gc.js +97 -0
- package/dist/host/calibration-samples-store.d.ts +5 -0
- package/dist/host/calibration-samples-store.js +67 -0
- package/dist/host/capabilities.d.ts +45 -0
- package/dist/host/capabilities.js +67 -0
- package/dist/host/derived-store.d.ts +36 -0
- package/dist/host/derived-store.js +199 -0
- package/dist/host/dsh-llm.d.ts +143 -0
- package/dist/host/dsh-llm.js +395 -0
- package/dist/host/event-store.d.ts +47 -0
- package/dist/host/event-store.js +154 -0
- package/dist/host/fs-atomic.d.ts +1 -0
- package/dist/host/fs-atomic.js +26 -0
- package/dist/host/im-api.d.ts +352 -0
- package/dist/host/im-api.js +1137 -0
- package/dist/host/index.d.ts +103 -0
- package/dist/host/index.js +453 -0
- package/dist/host/memory-index.d.ts +65 -0
- package/dist/host/memory-index.js +181 -0
- package/dist/host/metrics-store.d.ts +7 -0
- package/dist/host/metrics-store.js +104 -0
- package/dist/host/models.d.ts +39 -0
- package/dist/host/models.js +67 -0
- package/dist/host/persona-tools.d.ts +57 -0
- package/dist/host/persona-tools.js +226 -0
- package/dist/host/preflight.d.ts +31 -0
- package/dist/host/preflight.js +29 -0
- package/dist/host/receipt-store.d.ts +26 -0
- package/dist/host/receipt-store.js +68 -0
- package/dist/host/run-check.d.ts +51 -0
- package/dist/host/run-check.js +190 -0
- package/dist/host/state-roots.d.ts +50 -0
- package/dist/host/state-roots.js +163 -0
- package/dist/host/tool-cache.d.ts +25 -0
- package/dist/host/tool-cache.js +90 -0
- package/dist/host/tool-call-budget.d.ts +29 -0
- package/dist/host/tool-call-budget.js +38 -0
- package/dsh-plugin.json +25 -0
- package/lib/client.inner.cjs +3346 -0
- package/lib/client.js +3354 -0
- package/package.json +86 -6
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
import type { ConsensusState } from './consensus.ts';
|
|
2
|
+
export type Activity = 'muted' | 'mention-only' | 'review' | 'high';
|
|
3
|
+
export type ThreadKind = 'dm' | 'triad' | 'group';
|
|
4
|
+
export type MessageKind = 'user' | 'persona' | 'system';
|
|
5
|
+
/** 原生会话一轮要投喂的一行(已带 seq,host 按 seq 去重)。 */
|
|
6
|
+
export interface NativeFrameLine {
|
|
7
|
+
seq: number;
|
|
8
|
+
kind: 'user' | 'persona' | 'system';
|
|
9
|
+
authorName: string;
|
|
10
|
+
text: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 原生会话的增量帧。
|
|
14
|
+
* 静态人设走 systemPrompt 段,这里只带窗口行、当前消息和 L2 事实快照。
|
|
15
|
+
*/
|
|
16
|
+
export interface NativeFrame {
|
|
17
|
+
/** 本轮窗口内的行(已按 seq 升序)。 */
|
|
18
|
+
windowLines: NativeFrameLine[];
|
|
19
|
+
/** 触发本轮的消息(可能已在 windowLines 里,host 按 seq 去重)。 */
|
|
20
|
+
currentLine?: NativeFrameLine;
|
|
21
|
+
/** L2 不可压缩事实。原生会话创建时写入,内容变了会换掉这一段。 */
|
|
22
|
+
factsL2?: string[];
|
|
23
|
+
/** 事实快照截止 seq(写进段首「截止 #N」)。 */
|
|
24
|
+
factsUntilSeq?: number;
|
|
25
|
+
/** Phase 1 handle directory; only sent when memoryContextMode=on (appended to this turn's user text, not the system prefix). */
|
|
26
|
+
memoryBlock?: string;
|
|
27
|
+
/**
|
|
28
|
+
* 双层窗口的冷区上下文(L1 摘要 + 章节目录):只在记忆层 on 时追加到**本轮用户文本**
|
|
29
|
+
* (`memoryBlock` 同模式),绝不写 system 前缀(否则 prompt 缓存全失效)。
|
|
30
|
+
*/
|
|
31
|
+
contextBlock?: string;
|
|
32
|
+
}
|
|
33
|
+
export type RoundMode = 'idle' | 'review';
|
|
34
|
+
/** 主动私聊频率预设:克制 / 标准 / 活跃。 */
|
|
35
|
+
export type ProactiveLevel = 'low' | 'standard' | 'high';
|
|
36
|
+
export interface Persona {
|
|
37
|
+
/** 岗位/头衔,例如 产品经理;name 是这个人叫什么。 */
|
|
38
|
+
post?: string;
|
|
39
|
+
id: string;
|
|
40
|
+
name: string;
|
|
41
|
+
avatar: string;
|
|
42
|
+
avatarUrl?: string;
|
|
43
|
+
personality: string;
|
|
44
|
+
duties: string[];
|
|
45
|
+
origin: 'mine' | 'team';
|
|
46
|
+
provider?: string;
|
|
47
|
+
model: string;
|
|
48
|
+
toolAllow?: string[];
|
|
49
|
+
toolDeny?: string[];
|
|
50
|
+
proactiveDm: boolean;
|
|
51
|
+
/** 主动私聊频率预设(默认 standard)。 */
|
|
52
|
+
proactiveLevel?: ProactiveLevel;
|
|
53
|
+
defaultActivity: Activity;
|
|
54
|
+
}
|
|
55
|
+
export interface ThreadMember {
|
|
56
|
+
personaId: string;
|
|
57
|
+
activity: Activity;
|
|
58
|
+
completedRound: boolean;
|
|
59
|
+
passedRound: boolean;
|
|
60
|
+
}
|
|
61
|
+
export interface CatalogCard {
|
|
62
|
+
threadId: string;
|
|
63
|
+
title: string;
|
|
64
|
+
topic: string;
|
|
65
|
+
shortSummary: string;
|
|
66
|
+
}
|
|
67
|
+
export interface MemoryEntry {
|
|
68
|
+
id: string;
|
|
69
|
+
scope: 'identity' | 'user_global' | 'thread';
|
|
70
|
+
threadId?: string;
|
|
71
|
+
text: string;
|
|
72
|
+
createdAt: number;
|
|
73
|
+
}
|
|
74
|
+
/** 消息里的附件(用户上传;文件/图片)。 */
|
|
75
|
+
export interface MessageAttachment {
|
|
76
|
+
id: string;
|
|
77
|
+
kind: 'image' | 'file';
|
|
78
|
+
name: string;
|
|
79
|
+
mediaType: string;
|
|
80
|
+
bytes: number;
|
|
81
|
+
/** DSH 附件存储的持久引用 id(sha256:...);没有时模型看不到图片,只能看占位。 */
|
|
82
|
+
attachmentId?: string;
|
|
83
|
+
width?: number;
|
|
84
|
+
height?: number;
|
|
85
|
+
/** 文本类文件的截断内容(拼 prompt 用;二进制文件没有)。 */
|
|
86
|
+
text?: string;
|
|
87
|
+
/** 群工作区内的相对路径(用于 UI 预览/下载,如 attachments/xxx.png)。 */
|
|
88
|
+
path?: string;
|
|
89
|
+
/** 是否成功登记到 DSH 附件存储(false 时模型看不到图片内容,只能看占位)。 */
|
|
90
|
+
modelVisible?: boolean;
|
|
91
|
+
/** 声明/扩展名是图片、但内容嗅探不是图片,已降级按文件处理。 */
|
|
92
|
+
downgraded?: boolean;
|
|
93
|
+
}
|
|
94
|
+
/** 模型消息内容块:文本 / 图片(DSH ImageBlock)/ 文件(DSH FileBlock)。 */
|
|
95
|
+
export type ModelContentPart = {
|
|
96
|
+
type: 'text';
|
|
97
|
+
text: string;
|
|
98
|
+
} | {
|
|
99
|
+
type: 'image';
|
|
100
|
+
attachment: {
|
|
101
|
+
attachmentId: string;
|
|
102
|
+
name?: string;
|
|
103
|
+
mediaType?: string;
|
|
104
|
+
width?: number;
|
|
105
|
+
height?: number;
|
|
106
|
+
};
|
|
107
|
+
} | {
|
|
108
|
+
type: 'file';
|
|
109
|
+
attachment: {
|
|
110
|
+
attachmentId: string;
|
|
111
|
+
name?: string;
|
|
112
|
+
bytes?: number;
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
export interface Message {
|
|
116
|
+
id: string;
|
|
117
|
+
threadId: string;
|
|
118
|
+
/** 事件日志里的持久序号(与 events.jsonl 的 seq 对应);老数据由 saveState 回填。 */
|
|
119
|
+
seq?: number;
|
|
120
|
+
kind: MessageKind;
|
|
121
|
+
authorId?: string;
|
|
122
|
+
authorName: string;
|
|
123
|
+
text: string;
|
|
124
|
+
/** 用户随消息上传的附件。 */
|
|
125
|
+
attachments?: MessageAttachment[];
|
|
126
|
+
/** 这条回复生成时“模型看到的最后一条消息 seq”(因果一致性校验用)。 */
|
|
127
|
+
basedOnSeq?: number;
|
|
128
|
+
/** 生成期间有新消息:回复基于旧版本(annotate 策略)。 */
|
|
129
|
+
stale?: boolean;
|
|
130
|
+
interrupted?: boolean;
|
|
131
|
+
pass?: boolean;
|
|
132
|
+
/** 运行时主动发起的私聊(不来自用户提问)。 */
|
|
133
|
+
proactive?: boolean;
|
|
134
|
+
windowReply?: boolean;
|
|
135
|
+
createdAt: number;
|
|
136
|
+
}
|
|
137
|
+
/** 一条发言权仲裁决策:谁、做了什么、为什么、什么时候。 */
|
|
138
|
+
export interface FloorDecision {
|
|
139
|
+
/** 决策针对的角色;pass(全场无人可发)时为空串。 */
|
|
140
|
+
personaId: string;
|
|
141
|
+
action: 'speak' | 'wait' | 'pass';
|
|
142
|
+
reason: string;
|
|
143
|
+
at: number;
|
|
144
|
+
}
|
|
145
|
+
/** 事实的分类(不可压缩项优先:决策/待办/承诺/约束)。 */
|
|
146
|
+
export type FactKind = 'decision' | 'todo' | 'commitment' | 'constraint' | 'stance' | 'fact';
|
|
147
|
+
/** 事实来源决定写入优先级:用户 > 角色显式声明 > LLM 抽取。 */
|
|
148
|
+
export type FactSource = 'user' | 'persona' | 'extractor';
|
|
149
|
+
/** 从事件投影出来的事实(不可变,可重建)。 */
|
|
150
|
+
export interface Fact {
|
|
151
|
+
factId: string;
|
|
152
|
+
kind: FactKind;
|
|
153
|
+
text: string;
|
|
154
|
+
owner?: string;
|
|
155
|
+
source: FactSource;
|
|
156
|
+
confidence: number;
|
|
157
|
+
/** 产生这条事实的事件序号。 */
|
|
158
|
+
seq: number;
|
|
159
|
+
at: number;
|
|
160
|
+
/** 来源消息的事件 id(provenance;旧数据可能缺失)。 */
|
|
161
|
+
sourceMessageId?: string;
|
|
162
|
+
/** 来源消息的持久 seq(provenance;旧数据可能缺失)。 */
|
|
163
|
+
sourceMessageSeq?: number;
|
|
164
|
+
}
|
|
165
|
+
/** 客户端产生、待 host 落成 fact 事件的事实操作(异步抽取 / 任务板完成)。 */
|
|
166
|
+
export interface PendingFactOp {
|
|
167
|
+
op: 'assert' | 'supersede' | 'retract';
|
|
168
|
+
factId: string;
|
|
169
|
+
kind: FactKind;
|
|
170
|
+
text: string;
|
|
171
|
+
source: FactSource;
|
|
172
|
+
owner?: string;
|
|
173
|
+
supersedes?: string;
|
|
174
|
+
/** 来源消息事件 id / seq(provenance;任务板归档等用户操作可不带)。 */
|
|
175
|
+
sourceMessageId?: string;
|
|
176
|
+
sourceMessageSeq?: number;
|
|
177
|
+
at: number;
|
|
178
|
+
}
|
|
179
|
+
/** 一条指标(`metrics.jsonl` 的一行;客户端先攒在 thread.pendingMetrics,saveState 落盘)。 */
|
|
180
|
+
export interface MetricEntry {
|
|
181
|
+
/**
|
|
182
|
+
* 稳定指纹(`core/metrics.ts` 的 `metricFingerprint`):同一条指标在任何进程/任何写入路径下算出同一个 id。
|
|
183
|
+
* 落盘/读取都按它去重——并发 saveState 把同一批 pendingMetrics 写两遍时,行可以重复,指标不会重复计数。
|
|
184
|
+
* 老数据没有这个字段,读侧按「同 kind/at/value/fields」补算指纹去重。
|
|
185
|
+
*/
|
|
186
|
+
id?: string;
|
|
187
|
+
at: number;
|
|
188
|
+
/** 指标类型:llm_call / stale / floor_round / extraction / facts / summary。 */
|
|
189
|
+
kind: string;
|
|
190
|
+
value?: number;
|
|
191
|
+
fields?: Record<string, string | number | boolean>;
|
|
192
|
+
}
|
|
193
|
+
/** 群级摘要投影:L1 = 旧消息压缩,L2 = 事实/决策/待办(不可压缩项)。 */
|
|
194
|
+
export interface SummaryDigest {
|
|
195
|
+
version: string;
|
|
196
|
+
/** 摘要覆盖到的消息 seq;窗口只取它之后的消息。 */
|
|
197
|
+
untilSeq: number;
|
|
198
|
+
l1: string;
|
|
199
|
+
l2: string[];
|
|
200
|
+
factsVersion: string;
|
|
201
|
+
at: number;
|
|
202
|
+
}
|
|
203
|
+
/** 中途某个角色调用失败时,整轮先停在可恢复的暂停态。 */
|
|
204
|
+
export interface BlockedState {
|
|
205
|
+
personaId: string;
|
|
206
|
+
name: string;
|
|
207
|
+
/** 宿主给出的稳定失败码,如 MISSING_CREDENTIAL / RATE_LIMIT。 */
|
|
208
|
+
code?: string;
|
|
209
|
+
message: string;
|
|
210
|
+
at: number;
|
|
211
|
+
}
|
|
212
|
+
export interface Thread {
|
|
213
|
+
/** 角色调用失败后的暂停态;清掉就能继续。 */
|
|
214
|
+
blocked?: BlockedState;
|
|
215
|
+
id: string;
|
|
216
|
+
kind: ThreadKind;
|
|
217
|
+
title: string;
|
|
218
|
+
/**
|
|
219
|
+
* 用户手改过标题(只有三人小窗会用到:默认标题由两个角色名派生,点标题改名后置 true,
|
|
220
|
+
* 之后角色改名不再覆盖;群标题本来就由用户起,不需要这个标记;DM 永远派生)。
|
|
221
|
+
*/
|
|
222
|
+
titleCustom?: boolean;
|
|
223
|
+
topic: string;
|
|
224
|
+
shortSummary: string;
|
|
225
|
+
memberIds: string[];
|
|
226
|
+
members: ThreadMember[];
|
|
227
|
+
projectRoot: string;
|
|
228
|
+
messages: Message[];
|
|
229
|
+
unread: number;
|
|
230
|
+
roundMode: RoundMode;
|
|
231
|
+
speakingPersonaId?: string;
|
|
232
|
+
nextHint?: string;
|
|
233
|
+
/** 本轮发言权仲裁记录(只保留最近若干条;每轮用户新消息开始时清空)。 */
|
|
234
|
+
floorDecisions?: FloorDecision[];
|
|
235
|
+
/** 摘要投影已覆盖到的事件序号;窗口只取它之后的消息(摘要分层落地的接口)。 */
|
|
236
|
+
summaryUntilSeq?: number;
|
|
237
|
+
/** 分层摘要投影(L1 旧消息压缩 + L2 事实/决策/待办);随 state.json 落盘。 */
|
|
238
|
+
summaryState?: SummaryDigest;
|
|
239
|
+
/** 事实投影(saveState 时由事件/消息重建);角色 prompt 会带上。 */
|
|
240
|
+
facts?: Fact[];
|
|
241
|
+
factsVersion?: string;
|
|
242
|
+
/**
|
|
243
|
+
* 共识状态(A 用户指令 / B 群共识 / C 任务 / D 归档)。
|
|
244
|
+
* 开关打开时由 curator + consensus builder 生成;关闭 / 旧数据时为空。
|
|
245
|
+
*/
|
|
246
|
+
consensusState?: ConsensusState;
|
|
247
|
+
/** 最近一次共识生成失败的原因(成功时清空;界面用它提示 + 重试)。 */
|
|
248
|
+
consensusError?: string;
|
|
249
|
+
/** host 侧事实投影已覆盖到的消息 seq:客户端只把比它更新的消息派生事实合并进来(防止已归档事实被重算复活)。 */
|
|
250
|
+
factsUntilSeq?: number;
|
|
251
|
+
/** 本轮客户端异步抽取/任务板操作产生、还没落成事件的事实。 */
|
|
252
|
+
pendingFactOps?: PendingFactOp[];
|
|
253
|
+
/** 成员执行轨迹(最近若干轮;用于右栏“执行”Tab 和成员卡最近动作)。 */
|
|
254
|
+
traces?: TurnTrace[];
|
|
255
|
+
/** 客户端本地看到过、但还没被 host 投影收录的抽取事实(落盘后清空)。 */
|
|
256
|
+
extractedFacts?: Fact[];
|
|
257
|
+
/** 客户端本地已归档的事实 id(防消息规则重算把它们复活;落盘后清空)。 */
|
|
258
|
+
retractedFactIds?: string[];
|
|
259
|
+
/** 待落盘的指标(saveState 写进 groups/<id>/metrics.jsonl 后清空)。 */
|
|
260
|
+
pendingMetrics?: MetricEntry[];
|
|
261
|
+
createdAt: number;
|
|
262
|
+
updatedAt: number;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* 角色状态快照(Phase 1):不是历史,而是“结论”。
|
|
266
|
+
* 全部由 facts 投影视图 + 运行时状态派生,确定性、可重建、零额外模型调用。
|
|
267
|
+
*/
|
|
268
|
+
export interface RoleState {
|
|
269
|
+
personaName: string;
|
|
270
|
+
/** 我答应/认领的事(commitment / todo)。 */
|
|
271
|
+
commitments: string[];
|
|
272
|
+
/** 我做过的决定(decision)。 */
|
|
273
|
+
decisions: string[];
|
|
274
|
+
/** 我坚持的约束(constraint)。 */
|
|
275
|
+
constraints: string[];
|
|
276
|
+
/** 我最近的立场(stance,取最新一条)。 */
|
|
277
|
+
position?: string;
|
|
278
|
+
/** 我在等谁(来自运行时状态机)。 */
|
|
279
|
+
waitingOn?: string;
|
|
280
|
+
/** 我最后看到的消息 seq。 */
|
|
281
|
+
lastSeq: number;
|
|
282
|
+
}
|
|
283
|
+
/** 检索开关三档:off 完全不跑;shadow 跑检索+回执但不进 prompt;on 进 prompt。 */
|
|
284
|
+
export type RetrievalMode = 'off' | 'shadow' | 'on';
|
|
285
|
+
/** 按需检索命中的一条旧事件(Phase 2:确定性检索,不依赖 embedding)。 */
|
|
286
|
+
export interface RetrievalHit {
|
|
287
|
+
seq: number;
|
|
288
|
+
authorName: string;
|
|
289
|
+
kind: MessageKind;
|
|
290
|
+
text: string;
|
|
291
|
+
/** 命中原因(实体命中/被引用/同话题/最近兜底),写进回执便于审计。 */
|
|
292
|
+
reason: string;
|
|
293
|
+
}
|
|
294
|
+
/** 结构化检索查询(回执里可以 diff,不用自然语言字符串)。 */
|
|
295
|
+
export interface RetrievalQuery {
|
|
296
|
+
/** 当前话题段 id(按用户消息边界切,等于该段用户消息 seq)。 */
|
|
297
|
+
topicId: number;
|
|
298
|
+
entities: {
|
|
299
|
+
type: 'mention' | 'name' | 'file' | 'quoted' | 'number';
|
|
300
|
+
value: string;
|
|
301
|
+
}[];
|
|
302
|
+
replyChainRoots: number[];
|
|
303
|
+
params: {
|
|
304
|
+
retrievalVersion: string;
|
|
305
|
+
topicSegVersion: string;
|
|
306
|
+
weights: Record<string, number>;
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
export interface RetrievalResult {
|
|
310
|
+
query: RetrievalQuery;
|
|
311
|
+
/** 最终进 prompt 的命中(受 token 预算/条数约束)。 */
|
|
312
|
+
hits: RetrievalHit[];
|
|
313
|
+
/** 打分后的完整命中 seq 列表(截断 top50,用于审计“为什么没选某条”)。 */
|
|
314
|
+
hitIds: number[];
|
|
315
|
+
/** indexVersion = 检索器版本 + 话题分段版本 + lastSeq,保证可重建。 */
|
|
316
|
+
indexVersion: string;
|
|
317
|
+
}
|
|
318
|
+
export interface SpeakRequest {
|
|
319
|
+
persona: Persona;
|
|
320
|
+
thread: Thread;
|
|
321
|
+
focus: string;
|
|
322
|
+
visibleMessages: Message[];
|
|
323
|
+
identity: string;
|
|
324
|
+
userGlobal: string[];
|
|
325
|
+
threadMemory: string[];
|
|
326
|
+
catalog: CatalogCard[];
|
|
327
|
+
/** 角色状态快照(由 ImService 用 facts + 运行时状态派生)。 */
|
|
328
|
+
roleState?: RoleState;
|
|
329
|
+
/** 群里所有角色名:检索时识别 @/人名实体。 */
|
|
330
|
+
memberNames?: string[];
|
|
331
|
+
/** 本轮检索档位(覆盖 speaker 的默认值):off/shadow/on。 */
|
|
332
|
+
retrievalMode?: RetrievalMode;
|
|
333
|
+
/** 本轮引用句柄协议档位(覆盖 speaker 默认值):off 不跑 / shadow 只写回执 / on 注入。 */
|
|
334
|
+
memoryContextMode?: import('./memory-context.ts').MemoryContextMode;
|
|
335
|
+
/** 主动私聊生成(prompt 会追加“主动私聊模式”说明)。 */
|
|
336
|
+
proactive?: boolean;
|
|
337
|
+
}
|
|
338
|
+
/** 一轮发言里的一步执行(工具调用/最终回复),用于"成员行为"时间线。 */
|
|
339
|
+
export interface TurnStepTrace {
|
|
340
|
+
step: number;
|
|
341
|
+
kind: 'tool' | 'reply';
|
|
342
|
+
name?: string;
|
|
343
|
+
/** 参数/结果只存截断预览,避免把大段文件内容塞进 state。 */
|
|
344
|
+
args?: string;
|
|
345
|
+
result?: string;
|
|
346
|
+
ok?: boolean;
|
|
347
|
+
ms?: number;
|
|
348
|
+
at: number;
|
|
349
|
+
/** 本轮工具调用统计(只在最终 reply 步出现;trace/metrics 审计用)。 */
|
|
350
|
+
toolCalls?: number;
|
|
351
|
+
toolExecuted?: number;
|
|
352
|
+
toolBudgetExhausted?: boolean;
|
|
353
|
+
}
|
|
354
|
+
/** 一轮发言的完整执行轨迹(按 角色×轮 存,最多保留最近若干轮)。 */
|
|
355
|
+
export interface TurnTrace {
|
|
356
|
+
id: string;
|
|
357
|
+
personaId: string;
|
|
358
|
+
personaName: string;
|
|
359
|
+
messageId?: string;
|
|
360
|
+
at: number;
|
|
361
|
+
ms: number;
|
|
362
|
+
steps: TurnStepTrace[];
|
|
363
|
+
}
|
|
364
|
+
export interface SpeakResult {
|
|
365
|
+
text: string;
|
|
366
|
+
pass?: boolean;
|
|
367
|
+
proposeRecallThreadId?: string;
|
|
368
|
+
/** 生成时模型看到的最后一条消息 seq(运行时填;测试/假模型可省略)。 */
|
|
369
|
+
basedOnSeq?: number;
|
|
370
|
+
/** 这一轮的工具执行轨迹(host 返回;假模型可省略)。 */
|
|
371
|
+
trace?: TurnStepTrace[];
|
|
372
|
+
}
|
|
373
|
+
/** 生成期间出现新消息时的处理策略。 */
|
|
374
|
+
export type StalePolicy = 'regenerate' | 'annotate' | 'drop';
|
|
375
|
+
export interface Speaker {
|
|
376
|
+
complete(req: SpeakRequest): Promise<SpeakResult>;
|
|
377
|
+
}
|
|
378
|
+
export interface ChairSelector {
|
|
379
|
+
pickNext(input: {
|
|
380
|
+
candidates: Persona[];
|
|
381
|
+
dutiesLeft: string[];
|
|
382
|
+
latestText: string;
|
|
383
|
+
mentionedIds: string[];
|
|
384
|
+
}): {
|
|
385
|
+
personaId: string;
|
|
386
|
+
focus: string;
|
|
387
|
+
} | null;
|
|
388
|
+
}
|
|
389
|
+
export interface ImState {
|
|
390
|
+
personas: Persona[];
|
|
391
|
+
threads: Thread[];
|
|
392
|
+
memories: MemoryEntry[];
|
|
393
|
+
wakeExcerpts: Record<string, string[]>;
|
|
394
|
+
pendingRecall: {
|
|
395
|
+
threadId: string;
|
|
396
|
+
personaId: string;
|
|
397
|
+
targetThreadId: string;
|
|
398
|
+
} | null;
|
|
399
|
+
lastProactiveAt: Record<string, number>;
|
|
400
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** 群聊上下文窗口:只给“本轮汇总摘要 + 最近 N 条”,避免整段历史灌给每个角色。 */
|
|
2
|
+
export const GROUP_WINDOW_KEEP = 100;
|
|
3
|
+
export function groupWindow(messages, summary, keep = GROUP_WINDOW_KEEP) {
|
|
4
|
+
const sliced = messages.slice(Math.max(0, messages.length - Math.max(1, keep)));
|
|
5
|
+
const trimmed = summary?.trim();
|
|
6
|
+
return trimmed ? { summary: trimmed, recent: sliced } : { recent: sliced };
|
|
7
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ImState } from './types.ts';
|
|
2
|
+
import { type FileSink } from './workspace.ts';
|
|
3
|
+
export { assertSafeThreadId } from './workspace.ts';
|
|
4
|
+
export declare const nodeSink: FileSink;
|
|
5
|
+
export declare function stateFile(projectRoot: string): string;
|
|
6
|
+
export declare function loadState(projectRoot: string): Promise<ImState | null>;
|
|
7
|
+
export interface SaveStateResult {
|
|
8
|
+
path: string;
|
|
9
|
+
/** 派生数据(事件 / facts / 摘要 / 指标)落盘失败、但聊天记录仍已保存的原因;空数组=全成功。 */
|
|
10
|
+
warnings: string[];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 落盘 state.json 并双写派生数据。
|
|
14
|
+
*
|
|
15
|
+
* 同一数据根上的调用**串行**(`saveChains`):并发 PUT 各带一份完整快照,
|
|
16
|
+
* 不排队时旧快照可能在新快照之后落盘(把最后几条消息写回去),派生事件也会互相覆盖。
|
|
17
|
+
* 串行只保证顺序,重复批次的清净由 `metrics-store` 的幂等 id 负责。
|
|
18
|
+
*/
|
|
19
|
+
export declare function saveState(projectRoot: string, state: ImState): Promise<SaveStateResult>;
|
|
20
|
+
/** 写本群的群工作区:<workspace>/.dsh-im/groups/<threadId>/。 */
|
|
21
|
+
export declare function writeGroupFile(projectRoot: string, threadId: string, relPath: string, content: string): Promise<string>;
|
|
22
|
+
/** 写本群工作区的二进制文件(附件用);路径规则与 writeGroupFile 一致。 */
|
|
23
|
+
export declare function writeGroupBuffer(projectRoot: string, threadId: string, relPath: string, data: Uint8Array): Promise<string>;
|
|
24
|
+
/** 读本群工作区的二进制附件。 */
|
|
25
|
+
export declare function readGroupBuffer(projectRoot: string, threadId: string, relPath: string): Promise<Buffer>;
|
|
26
|
+
export declare function listWorkspaceDir(projectRoot: string, threadId: string, rel?: string): Promise<string[]>;
|
|
27
|
+
export declare function readWorkspaceFile(projectRoot: string, threadId: string, rel: string): Promise<string>;
|
|
28
|
+
export declare function searchWorkspace(projectRoot: string, threadId: string, query: string): Promise<string[]>;
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { mkdir, readdir, readFile, stat, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
3
|
+
import { PathFence } from './fence.js';
|
|
4
|
+
import { parseState, stateRelPath } from './persist.js';
|
|
5
|
+
import { assertSafeThreadId, isInsideThreadDir, normalizePath, threadDir } from './workspace.js';
|
|
6
|
+
export { assertSafeThreadId } from './workspace.js';
|
|
7
|
+
import { syncThreadEvents } from '../host/event-store.js';
|
|
8
|
+
import { appendDerivedFactEvents, projectAndWriteFacts, syncPendingFactOps, syncThreadFloorDecisions, } from '../host/derived-store.js';
|
|
9
|
+
import { buildThreadDigest } from './summary.js';
|
|
10
|
+
import { appendMetrics } from '../host/metrics-store.js';
|
|
11
|
+
import { writeFileAtomic } from '../host/fs-atomic.js';
|
|
12
|
+
import { logWarn } from './log.js';
|
|
13
|
+
const SKIP_DIR = new Set(['node_modules', '.git', 'dist', '.vite', '.dsh-im']);
|
|
14
|
+
const MAX_READ = 80_000;
|
|
15
|
+
const MAX_LIST = 200;
|
|
16
|
+
const MAX_SEARCH_FILES = 80;
|
|
17
|
+
const MAX_HITS = 20;
|
|
18
|
+
/** 插件内部状态目录:角色不得读取(state.json、其它群的记录都在里面)。 */
|
|
19
|
+
const INTERNAL_DIR = '.dsh-im';
|
|
20
|
+
function assertNotInternal(root, abs, threadId) {
|
|
21
|
+
assertSafeThreadId(threadId);
|
|
22
|
+
const relPath = relative(resolve(root), resolve(abs)).replaceAll(sep, '/');
|
|
23
|
+
if (relPath !== INTERNAL_DIR && !relPath.startsWith(`${INTERNAL_DIR}/`))
|
|
24
|
+
return;
|
|
25
|
+
// 许读本群的群工作区;其余 .dsh-im 一律拒绝。
|
|
26
|
+
const own = `${INTERNAL_DIR}/groups/${threadId}`;
|
|
27
|
+
if (relPath === own || relPath.startsWith(`${own}/`))
|
|
28
|
+
return;
|
|
29
|
+
throw new Error('denied: .dsh-im 是插件内部状态,角色不能读取;需要资料请用工作区里的项目文件。');
|
|
30
|
+
}
|
|
31
|
+
export const nodeSink = {
|
|
32
|
+
async writeThreadFile(projectRoot, threadId, relativePath, content) {
|
|
33
|
+
const dest = resolve(threadDir(projectRoot, threadId), relativePath);
|
|
34
|
+
if (!isInsideThreadDir(projectRoot, threadId, dest)) {
|
|
35
|
+
throw new Error('write denied: path escapes .dsh-im thread directory');
|
|
36
|
+
}
|
|
37
|
+
await mkdir(dirname(dest), { recursive: true });
|
|
38
|
+
await writeFile(dest, content, 'utf8');
|
|
39
|
+
return dest;
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
export function stateFile(projectRoot) {
|
|
43
|
+
return join(resolve(projectRoot), '.dsh-im', stateRelPath());
|
|
44
|
+
}
|
|
45
|
+
export async function loadState(projectRoot) {
|
|
46
|
+
try {
|
|
47
|
+
return parseState(await readFile(stateFile(projectRoot), 'utf8'));
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
if (err.code === 'ENOENT')
|
|
51
|
+
return null;
|
|
52
|
+
throw err;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const saveChains = new Map();
|
|
56
|
+
/** 队列键:同一个数据根的不同写法(大小写 / 分隔符)必须命中同一把锁。 */
|
|
57
|
+
function saveQueueKey(root) {
|
|
58
|
+
const normalized = resolve(root).replaceAll('\\', '/');
|
|
59
|
+
return process.platform === 'win32' ? normalized.toLowerCase() : normalized;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* 落盘 state.json 并双写派生数据。
|
|
63
|
+
*
|
|
64
|
+
* 同一数据根上的调用**串行**(`saveChains`):并发 PUT 各带一份完整快照,
|
|
65
|
+
* 不排队时旧快照可能在新快照之后落盘(把最后几条消息写回去),派生事件也会互相覆盖。
|
|
66
|
+
* 串行只保证顺序,重复批次的清净由 `metrics-store` 的幂等 id 负责。
|
|
67
|
+
*/
|
|
68
|
+
export function saveState(projectRoot, state) {
|
|
69
|
+
const key = saveQueueKey(projectRoot);
|
|
70
|
+
const previous = saveChains.get(key) ?? Promise.resolve();
|
|
71
|
+
// 前一个失败了也要继续跑下一个:一次失败不能把该根永久卡住。
|
|
72
|
+
const next = previous.then(() => saveStateInner(projectRoot, state), () => saveStateInner(projectRoot, state));
|
|
73
|
+
saveChains.set(key, next.then(() => undefined, () => undefined));
|
|
74
|
+
return next;
|
|
75
|
+
}
|
|
76
|
+
async function saveStateInner(projectRoot, state) {
|
|
77
|
+
const dest = stateFile(projectRoot);
|
|
78
|
+
const warnings = [];
|
|
79
|
+
// 过渡期双写:先补事件日志并回填每条消息的持久 seq,再写 state.json。
|
|
80
|
+
// 事件日志失败不阻塞主写盘(读仍以 state 为准),但必须留下可见的告警,不能静默吞。
|
|
81
|
+
await Promise.all(state.threads.map(async (thread) => {
|
|
82
|
+
try {
|
|
83
|
+
const synced = await syncThreadEvents(projectRoot, thread.id, thread.messages);
|
|
84
|
+
if (synced.seqs.length === thread.messages.length) {
|
|
85
|
+
thread.messages.forEach((message, index) => { message.seq = synced.seqs[index]; });
|
|
86
|
+
}
|
|
87
|
+
// 顺序很重要:先由消息派生 assert(seq 更早),再落客户端操作(retract/supersede),
|
|
88
|
+
// 最后统一投影;反过来的话 retract 会拿到更小的 seq,重载后事实会复活。
|
|
89
|
+
await appendDerivedFactEvents(projectRoot, thread.id, thread.messages);
|
|
90
|
+
await syncPendingFactOps(projectRoot, thread.id, thread.pendingFactOps ?? []);
|
|
91
|
+
const facts = await projectAndWriteFacts(projectRoot, thread.id);
|
|
92
|
+
thread.facts = facts.facts;
|
|
93
|
+
thread.factsVersion = facts.version;
|
|
94
|
+
thread.factsUntilSeq = thread.messages.reduce((max, message) => Math.max(max, message.seq ?? 0), 0);
|
|
95
|
+
thread.pendingFactOps = undefined;
|
|
96
|
+
thread.extractedFacts = undefined;
|
|
97
|
+
thread.retractedFactIds = undefined;
|
|
98
|
+
await syncThreadFloorDecisions(projectRoot, thread.id, thread.floorDecisions ?? []);
|
|
99
|
+
await appendMetrics(projectRoot, thread.id, thread.pendingMetrics ?? []);
|
|
100
|
+
thread.pendingMetrics = undefined;
|
|
101
|
+
const digest = buildThreadDigest(thread, thread.summaryState, { facts: facts.facts });
|
|
102
|
+
thread.summaryState = digest;
|
|
103
|
+
thread.summaryUntilSeq = digest.untilSeq;
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
// 以前这里 catch {} 全吞:事件/facts/摘要/指标写失败时用户以为存好了(掩盖失败)。
|
|
107
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
108
|
+
const message = `群 ${thread.id} 的派生数据落盘失败(聊天记录仍会保存):${reason}`;
|
|
109
|
+
warnings.push(message);
|
|
110
|
+
logWarn('[dsh-im] saveState 派生数据落盘失败', { threadId: thread.id, reason });
|
|
111
|
+
}
|
|
112
|
+
}));
|
|
113
|
+
// 原子写:半截 JSON 会让下次加载直接报错,表现成「数据没了」。
|
|
114
|
+
await writeFileAtomic(dest, JSON.stringify(state, null, 2));
|
|
115
|
+
return { path: dest, warnings };
|
|
116
|
+
}
|
|
117
|
+
function resolveInside(projectRoot, threadId, rel) {
|
|
118
|
+
const root = resolve(projectRoot);
|
|
119
|
+
const dest = resolve(root, rel || '.');
|
|
120
|
+
const fence = new PathFence(normalizePath(root.replace(/\\/g, '/')), threadId);
|
|
121
|
+
assertNotInternal(root, dest, threadId);
|
|
122
|
+
const verdict = fence.decide({ op: 'read', target: dest.replace(/\\/g, '/') });
|
|
123
|
+
if (!verdict.allow)
|
|
124
|
+
throw new Error(verdict.reason);
|
|
125
|
+
return dest;
|
|
126
|
+
}
|
|
127
|
+
/** 写本群的群工作区:<workspace>/.dsh-im/groups/<threadId>/。 */
|
|
128
|
+
export async function writeGroupFile(projectRoot, threadId, relPath, content) {
|
|
129
|
+
assertSafeThreadId(threadId);
|
|
130
|
+
if (!relPath?.trim())
|
|
131
|
+
throw new Error('path required');
|
|
132
|
+
if (typeof content !== 'string')
|
|
133
|
+
throw new Error('content required');
|
|
134
|
+
const root = resolve(projectRoot);
|
|
135
|
+
const dir = join(root, INTERNAL_DIR, 'groups', threadId);
|
|
136
|
+
const dest = resolve(dir, relPath);
|
|
137
|
+
const rel = relative(dir, dest).replaceAll(sep, '/');
|
|
138
|
+
if (!rel || rel.startsWith('..') || isAbsolute(rel)) {
|
|
139
|
+
throw new Error('denied: 只能写在本群工作区目录内');
|
|
140
|
+
}
|
|
141
|
+
await mkdir(dirname(dest), { recursive: true });
|
|
142
|
+
await writeFile(dest, content, 'utf8');
|
|
143
|
+
return dest;
|
|
144
|
+
}
|
|
145
|
+
/** 写本群工作区的二进制文件(附件用);路径规则与 writeGroupFile 一致。 */
|
|
146
|
+
export async function writeGroupBuffer(projectRoot, threadId, relPath, data) {
|
|
147
|
+
assertSafeThreadId(threadId);
|
|
148
|
+
if (!relPath?.trim())
|
|
149
|
+
throw new Error('path required');
|
|
150
|
+
const root = resolve(projectRoot);
|
|
151
|
+
const dir = join(root, INTERNAL_DIR, 'groups', threadId);
|
|
152
|
+
const dest = resolve(dir, relPath);
|
|
153
|
+
const rel = relative(dir, dest).replaceAll(sep, '/');
|
|
154
|
+
if (!rel || rel.startsWith('..') || isAbsolute(rel)) {
|
|
155
|
+
throw new Error('denied: 只能写在本群工作区目录内');
|
|
156
|
+
}
|
|
157
|
+
await mkdir(dirname(dest), { recursive: true });
|
|
158
|
+
await writeFile(dest, data);
|
|
159
|
+
return dest;
|
|
160
|
+
}
|
|
161
|
+
/** 读本群工作区的二进制附件。 */
|
|
162
|
+
export async function readGroupBuffer(projectRoot, threadId, relPath) {
|
|
163
|
+
assertSafeThreadId(threadId);
|
|
164
|
+
if (!relPath?.trim())
|
|
165
|
+
throw new Error('path required');
|
|
166
|
+
const root = resolve(projectRoot);
|
|
167
|
+
const dir = join(root, INTERNAL_DIR, 'groups', threadId);
|
|
168
|
+
const dest = resolve(dir, relPath);
|
|
169
|
+
const rel = relative(dir, dest).replaceAll(sep, '/');
|
|
170
|
+
if (!rel || rel.startsWith('..') || isAbsolute(rel)) {
|
|
171
|
+
throw new Error('denied: 只能读本群工作区目录内的附件');
|
|
172
|
+
}
|
|
173
|
+
return readFile(dest);
|
|
174
|
+
}
|
|
175
|
+
export async function listWorkspaceDir(projectRoot, threadId, rel = '.') {
|
|
176
|
+
const dest = resolveInside(projectRoot, threadId, rel);
|
|
177
|
+
const entries = await readdir(dest, { withFileTypes: true });
|
|
178
|
+
const out = [];
|
|
179
|
+
for (const entry of entries) {
|
|
180
|
+
if (SKIP_DIR.has(entry.name))
|
|
181
|
+
continue;
|
|
182
|
+
out.push(entry.isDirectory() ? `${entry.name}/` : entry.name);
|
|
183
|
+
if (out.length >= MAX_LIST)
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
return out.sort();
|
|
187
|
+
}
|
|
188
|
+
export async function readWorkspaceFile(projectRoot, threadId, rel) {
|
|
189
|
+
if (!rel?.trim())
|
|
190
|
+
throw new Error('path required');
|
|
191
|
+
const dest = resolveInside(projectRoot, threadId, rel);
|
|
192
|
+
const info = await stat(dest);
|
|
193
|
+
if (!info.isFile())
|
|
194
|
+
throw new Error('不是文件');
|
|
195
|
+
const raw = await readFile(dest);
|
|
196
|
+
const text = raw.subarray(0, MAX_READ).toString('utf8');
|
|
197
|
+
return raw.length > MAX_READ ? `${text}\n…(截断,原文件 ${raw.length} 字节)` : text;
|
|
198
|
+
}
|
|
199
|
+
export async function searchWorkspace(projectRoot, threadId, query) {
|
|
200
|
+
const needle = query.trim();
|
|
201
|
+
if (!needle)
|
|
202
|
+
throw new Error('query required');
|
|
203
|
+
const root = resolve(projectRoot);
|
|
204
|
+
const hits = [];
|
|
205
|
+
let files = 0;
|
|
206
|
+
async function walk(dir) {
|
|
207
|
+
if (hits.length >= MAX_HITS || files >= MAX_SEARCH_FILES)
|
|
208
|
+
return;
|
|
209
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
210
|
+
for (const entry of entries) {
|
|
211
|
+
if (hits.length >= MAX_HITS || files >= MAX_SEARCH_FILES)
|
|
212
|
+
return;
|
|
213
|
+
if (SKIP_DIR.has(entry.name))
|
|
214
|
+
continue;
|
|
215
|
+
const full = join(dir, entry.name);
|
|
216
|
+
if (entry.isDirectory()) {
|
|
217
|
+
await walk(full);
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
files += 1;
|
|
221
|
+
const fence = new PathFence(normalizePath(root.replace(/\\/g, '/')), threadId);
|
|
222
|
+
if (!fence.decide({ op: 'read', target: full.replace(/\\/g, '/') }).allow)
|
|
223
|
+
continue;
|
|
224
|
+
const body = (await readFile(full)).subarray(0, MAX_READ).toString('utf8');
|
|
225
|
+
const line = body.split(/\r?\n/).find((row) => row.includes(needle));
|
|
226
|
+
if (line)
|
|
227
|
+
hits.push(`${relative(root, full).replaceAll(sep, '/')}: ${line.trim().slice(0, 160)}`);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
await walk(root);
|
|
231
|
+
return hits;
|
|
232
|
+
}
|