@dabuliu_wsy/dsh-im 0.0.0-stage → 0.3.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 +204 -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 +213 -0
- package/dist/core/im.js +1640 -0
- package/dist/core/index.d.ts +34 -0
- package/dist/core/index.js +34 -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/trash.d.ts +110 -0
- package/dist/core/trash.js +56 -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 +296 -0
- package/dist/host/agent-session.js +964 -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/group-files.d.ts +26 -0
- package/dist/host/group-files.js +142 -0
- package/dist/host/im-api.d.ts +408 -0
- package/dist/host/im-api.js +1188 -0
- package/dist/host/index.d.ts +103 -0
- package/dist/host/index.js +509 -0
- package/dist/host/media-type.d.ts +10 -0
- package/dist/host/media-type.js +39 -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/dist/host/trash-store.d.ts +32 -0
- package/dist/host/trash-store.js +316 -0
- package/dsh-plugin.json +25 -0
- package/lib/client.inner.cjs +3933 -0
- package/lib/client.js +3941 -0
- package/package.json +86 -6
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 会议共识状态(consensus)构建器。
|
|
3
|
+
*
|
|
4
|
+
* 职责边界:
|
|
5
|
+
* - 输入是**已抽取的事实**(带事件序号与来源消息引用)和**评审过的聚类草稿**;
|
|
6
|
+
* - 只做契约校验、物化、版本与渲染,不调模型、不发明内容;
|
|
7
|
+
* - 用户条目只能进 A/C,角色条目只能进 B/D;
|
|
8
|
+
* - 能力/环境自述必须归 D,从结构上阻止自证循环;
|
|
9
|
+
* - 草稿必须覆盖全部事实,零丢失;
|
|
10
|
+
* - 输出版本由内容 hash 派生,幂等可回放。
|
|
11
|
+
*/
|
|
12
|
+
import { isTransientClaim } from './facts.js';
|
|
13
|
+
import { stableHash } from './receipt.js';
|
|
14
|
+
// 能力自述的判定在 facts 层(摘要 L2 / 角色状态 / 共识共用同一条规则),这里保留 re-export 兼容旧引用。
|
|
15
|
+
export { isTransientClaim } from './facts.js';
|
|
16
|
+
export const CONSENSUS_LANES = ['A', 'B', 'C', 'D'];
|
|
17
|
+
export const CONSENSUS_KINDS = [
|
|
18
|
+
'约束', '变更', '决定', '验收', '判定', '分工', '承诺', '约定', '立场', '任务', '能力自述', '事实',
|
|
19
|
+
];
|
|
20
|
+
const KIND_ALIASES = {
|
|
21
|
+
约束: '约束', constraint: '约束',
|
|
22
|
+
变更: '变更', change: '变更',
|
|
23
|
+
决定: '决定', decision: '决定', 决策: '决定',
|
|
24
|
+
验收: '验收', acceptance: '验收',
|
|
25
|
+
判定: '判定', verdict: '判定',
|
|
26
|
+
分工: '分工', division: '分工',
|
|
27
|
+
承诺: '承诺', commitment: '承诺',
|
|
28
|
+
约定: '约定', agreement: '约定',
|
|
29
|
+
立场: '立场', stance: '立场',
|
|
30
|
+
任务: '任务', task: '任务', todo: '任务',
|
|
31
|
+
能力自述: '能力自述', capability: '能力自述',
|
|
32
|
+
事实: '事实', fact: '事实',
|
|
33
|
+
};
|
|
34
|
+
/** 把中英文别名收敛到唯一分类;未知返回 undefined(由调用方报错,不做静默降级)。 */
|
|
35
|
+
export function normalizeConsensusKind(value) {
|
|
36
|
+
const key = (value ?? '').trim();
|
|
37
|
+
if (!key)
|
|
38
|
+
return undefined;
|
|
39
|
+
return KIND_ALIASES[key] ?? KIND_ALIASES[key.toLowerCase()];
|
|
40
|
+
}
|
|
41
|
+
export const CONSENSUS_STATUSES = [
|
|
42
|
+
'candidate', 'active', 'applied', 'done', 'resolved', 'superseded', 'expired', 'archived',
|
|
43
|
+
];
|
|
44
|
+
export const CONSENSUS_STATUS_LABEL = {
|
|
45
|
+
candidate: '待确认',
|
|
46
|
+
active: '有效',
|
|
47
|
+
applied: '已落实',
|
|
48
|
+
done: '已完成',
|
|
49
|
+
resolved: '已解决',
|
|
50
|
+
superseded: '已被取代',
|
|
51
|
+
expired: '已过期',
|
|
52
|
+
archived: '已归档',
|
|
53
|
+
};
|
|
54
|
+
/** 当前权威层(会进 prompt 主体);candidate 单独进「待确认」块。 */
|
|
55
|
+
export const AUTHORITATIVE_STATUSES = ['active', 'applied', 'done'];
|
|
56
|
+
const ALLOWED_STATUSES = {
|
|
57
|
+
A: ['active', 'applied', 'candidate'],
|
|
58
|
+
B: ['active', 'candidate', 'resolved', 'superseded', 'expired'],
|
|
59
|
+
C: ['active', 'done', 'candidate'],
|
|
60
|
+
D: ['archived'],
|
|
61
|
+
};
|
|
62
|
+
export const CONSENSUS_SCHEMA = 'consensus-v1';
|
|
63
|
+
export const CONSENSUS_MAX_TEXT = 160;
|
|
64
|
+
export const DEFAULT_PROMPT_BUDGET = 1800;
|
|
65
|
+
export const ECHO_MARKERS = [
|
|
66
|
+
'[当前消息]',
|
|
67
|
+
'[最近群聊]',
|
|
68
|
+
'[本轮群聊摘要]',
|
|
69
|
+
'[你之前的发言要点]',
|
|
70
|
+
'收到:',
|
|
71
|
+
'开发预览',
|
|
72
|
+
];
|
|
73
|
+
function isUserOwner(owner) {
|
|
74
|
+
return owner === '用户' || owner === '你';
|
|
75
|
+
}
|
|
76
|
+
function containsEcho(text) {
|
|
77
|
+
return ECHO_MARKERS.some((marker) => text.includes(marker));
|
|
78
|
+
}
|
|
79
|
+
function refsOf(factIds, factById) {
|
|
80
|
+
return factIds.map((factId) => {
|
|
81
|
+
const fact = factById.get(factId);
|
|
82
|
+
return {
|
|
83
|
+
factId,
|
|
84
|
+
factEventSeq: fact.eventSeq,
|
|
85
|
+
sourceMessageId: fact.sourceMessageId ?? null,
|
|
86
|
+
sourceMessageSeq: fact.sourceMessageSeq ?? null,
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/** 去掉正文里自己套的“名字:”前缀(模型改写常把发言人名字写进正文)。 */
|
|
91
|
+
function stripOwnerPrefix(text, owner) {
|
|
92
|
+
const name = owner.trim();
|
|
93
|
+
if (!name)
|
|
94
|
+
return text;
|
|
95
|
+
const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
96
|
+
const pattern = new RegExp(`^(?:\\*\\*|【|「|“|")?${escaped}(?:\\*\\*|】|」|”|")?\\s*[::]\\s*`, 'u');
|
|
97
|
+
let out = text;
|
|
98
|
+
for (let layer = 0; layer < 3; layer++) {
|
|
99
|
+
const next = out.replace(pattern, '');
|
|
100
|
+
if (next === out)
|
|
101
|
+
break;
|
|
102
|
+
out = next;
|
|
103
|
+
}
|
|
104
|
+
return out.trim();
|
|
105
|
+
}
|
|
106
|
+
function normalizeClusterText(text, factIds, factById) {
|
|
107
|
+
const owners = [...new Set(factIds
|
|
108
|
+
.map((factId) => factById.get(factId)?.owner)
|
|
109
|
+
.filter((owner) => Boolean(owner)))];
|
|
110
|
+
let out = (text ?? '').replace(/\s+/g, ' ').trim();
|
|
111
|
+
for (const owner of owners)
|
|
112
|
+
out = stripOwnerPrefix(out, owner);
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
function defaultStatusFor(lane) {
|
|
116
|
+
return lane === 'D' ? 'archived' : 'active';
|
|
117
|
+
}
|
|
118
|
+
function resolveDraftStatus(cluster, errors) {
|
|
119
|
+
const fallback = defaultStatusFor(cluster.lane);
|
|
120
|
+
if (cluster.status === undefined)
|
|
121
|
+
return fallback;
|
|
122
|
+
if (!CONSENSUS_STATUSES.includes(cluster.status)) {
|
|
123
|
+
errors.push(`未知 status:${cluster.key} -> ${String(cluster.status)}`);
|
|
124
|
+
return fallback;
|
|
125
|
+
}
|
|
126
|
+
if (!ALLOWED_STATUSES[cluster.lane].includes(cluster.status)) {
|
|
127
|
+
errors.push(`status 与 lane 不匹配:${cluster.key}(${cluster.lane} 不允许 ${cluster.status})`);
|
|
128
|
+
return fallback;
|
|
129
|
+
}
|
|
130
|
+
if (cluster.status === 'superseded' && !cluster.supersededByKey?.trim()) {
|
|
131
|
+
errors.push(`superseded 缺 supersededByKey:${cluster.key}`);
|
|
132
|
+
}
|
|
133
|
+
return cluster.status;
|
|
134
|
+
}
|
|
135
|
+
export function buildConsensusState(factsInput, draft, options, now = Date.now()) {
|
|
136
|
+
const errors = [];
|
|
137
|
+
const facts = [...factsInput];
|
|
138
|
+
const factById = new Map();
|
|
139
|
+
for (const fact of facts) {
|
|
140
|
+
if (factById.has(fact.factId))
|
|
141
|
+
errors.push(`事实重复:${fact.factId}`);
|
|
142
|
+
factById.set(fact.factId, fact);
|
|
143
|
+
if (!Number.isFinite(fact.eventSeq) || fact.eventSeq <= 0)
|
|
144
|
+
errors.push(`eventSeq 非法:${fact.factId}`);
|
|
145
|
+
if (!fact.text?.trim())
|
|
146
|
+
errors.push(`事实正文为空:${fact.factId}`);
|
|
147
|
+
}
|
|
148
|
+
if (facts.length === 0)
|
|
149
|
+
errors.push('facts 为空');
|
|
150
|
+
const draftClusters = [...draft.clusters];
|
|
151
|
+
if (draftClusters.length === 0 && (draft.derived?.length ?? 0) === 0)
|
|
152
|
+
errors.push('草稿没有簇');
|
|
153
|
+
const used = new Map();
|
|
154
|
+
const keys = new Set();
|
|
155
|
+
const statusByKey = new Map();
|
|
156
|
+
for (const cluster of draftClusters) {
|
|
157
|
+
const key = cluster.key?.trim() ?? '';
|
|
158
|
+
if (!key)
|
|
159
|
+
errors.push('簇缺少 key');
|
|
160
|
+
else if (keys.has(key))
|
|
161
|
+
errors.push(`簇 key 重复:${key}`);
|
|
162
|
+
else
|
|
163
|
+
keys.add(key);
|
|
164
|
+
if (!CONSENSUS_LANES.includes(cluster.lane))
|
|
165
|
+
errors.push(`lane 非法:${key}`);
|
|
166
|
+
const kind = normalizeConsensusKind(cluster.kind);
|
|
167
|
+
if (!kind)
|
|
168
|
+
errors.push(`未知 kind:${key}(${String(cluster.kind)})`);
|
|
169
|
+
statusByKey.set(key, resolveDraftStatus(cluster, errors));
|
|
170
|
+
const text = normalizeClusterText(cluster.text ?? '', cluster.factIds ?? [], factById);
|
|
171
|
+
if (!text)
|
|
172
|
+
errors.push(`簇正文为空:${key}`);
|
|
173
|
+
else if (text.length > CONSENSUS_MAX_TEXT)
|
|
174
|
+
errors.push(`簇正文超长:${key}(${text.length} > ${CONSENSUS_MAX_TEXT})`);
|
|
175
|
+
if (containsEcho(text))
|
|
176
|
+
errors.push(`簇正文混入 prompt 回声:${key}`);
|
|
177
|
+
if (!Array.isArray(cluster.factIds) || cluster.factIds.length === 0) {
|
|
178
|
+
errors.push(`簇没有 factIds:${key}`);
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
for (const factId of cluster.factIds) {
|
|
182
|
+
const fact = factById.get(factId);
|
|
183
|
+
if (!fact) {
|
|
184
|
+
errors.push(`未知 factId:${key} -> ${factId}`);
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
const previous = used.get(factId);
|
|
188
|
+
if (previous)
|
|
189
|
+
errors.push(`事实重复分配:${factId}(${previous} / ${key})`);
|
|
190
|
+
else
|
|
191
|
+
used.set(factId, key);
|
|
192
|
+
const allowed = isUserOwner(fact.owner) ? ['A', 'C'] : ['B', 'D'];
|
|
193
|
+
if (!allowed.includes(cluster.lane))
|
|
194
|
+
errors.push(`来源错栏:${factId}(${fact.owner})不能进 ${cluster.lane}`);
|
|
195
|
+
if (!isUserOwner(fact.owner) && cluster.lane !== 'D' && isTransientClaim(fact.text)) {
|
|
196
|
+
errors.push(`能力/环境自述必须归档:${factId}(当前 ${cluster.lane},应为 D)`);
|
|
197
|
+
}
|
|
198
|
+
if (containsEcho(fact.text))
|
|
199
|
+
errors.push(`事实混入 prompt 回声:${factId}`);
|
|
200
|
+
if (options.mode === 'new') {
|
|
201
|
+
if (!fact.sourceMessageId)
|
|
202
|
+
errors.push(`缺 sourceMessageId:${factId}`);
|
|
203
|
+
else if (options.knownMessageIds && !options.knownMessageIds.includes(fact.sourceMessageId)) {
|
|
204
|
+
errors.push(`sourceMessageId 不存在:${factId}`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
for (const cluster of draftClusters) {
|
|
210
|
+
const key = cluster.key.trim();
|
|
211
|
+
if (statusByKey.get(key) !== 'superseded')
|
|
212
|
+
continue;
|
|
213
|
+
const target = cluster.supersededByKey?.trim() ?? '';
|
|
214
|
+
if (!target)
|
|
215
|
+
continue;
|
|
216
|
+
if (target === key)
|
|
217
|
+
errors.push(`supersededByKey 不能指向自己:${key}`);
|
|
218
|
+
else if (!statusByKey.has(target))
|
|
219
|
+
errors.push(`supersededByKey 不存在:${key} -> ${target}`);
|
|
220
|
+
else if (statusByKey.get(target) === 'superseded')
|
|
221
|
+
errors.push(`supersededByKey 指向另一个已取代条目:${key} -> ${target}`);
|
|
222
|
+
}
|
|
223
|
+
for (const fact of facts) {
|
|
224
|
+
if (!used.has(fact.factId))
|
|
225
|
+
errors.push(`丢失:${fact.factId}(${fact.text.slice(0, 24)})`);
|
|
226
|
+
}
|
|
227
|
+
const draftDerived = [...(draft.derived ?? [])];
|
|
228
|
+
const clusterByKey = new Map(draftClusters.map((cluster) => [cluster.key, cluster]));
|
|
229
|
+
for (const item of draftDerived) {
|
|
230
|
+
const key = item.key?.trim() ?? '';
|
|
231
|
+
if (!key)
|
|
232
|
+
errors.push('派生簇缺少 key');
|
|
233
|
+
else if (keys.has(key))
|
|
234
|
+
errors.push(`簇 key 重复:${key}`);
|
|
235
|
+
else
|
|
236
|
+
keys.add(key);
|
|
237
|
+
if (item.lane !== 'C')
|
|
238
|
+
errors.push(`派生簇 lane 必须为 C:${key}`);
|
|
239
|
+
const source = clusterByKey.get(item.fromKey);
|
|
240
|
+
if (!source)
|
|
241
|
+
errors.push(`派生来源不存在:${key} -> ${item.fromKey}`);
|
|
242
|
+
else if (!item.factIds.every((factId) => source.factIds.includes(factId))) {
|
|
243
|
+
errors.push(`派生 factIds 不在来源簇:${key}`);
|
|
244
|
+
}
|
|
245
|
+
if (!normalizeConsensusKind(item.kind))
|
|
246
|
+
errors.push(`未知 kind:${key}(${String(item.kind)})`);
|
|
247
|
+
const text = normalizeClusterText(item.text ?? '', item.factIds ?? [], factById);
|
|
248
|
+
if (!text)
|
|
249
|
+
errors.push(`派生簇正文为空:${key}`);
|
|
250
|
+
else if (text.length > CONSENSUS_MAX_TEXT)
|
|
251
|
+
errors.push(`派生簇正文超长:${key}`);
|
|
252
|
+
}
|
|
253
|
+
if (errors.length > 0)
|
|
254
|
+
return { ok: false, errors };
|
|
255
|
+
const clusters = draftClusters.map((cluster) => {
|
|
256
|
+
const key = cluster.key.trim();
|
|
257
|
+
const text = normalizeClusterText(cluster.text, cluster.factIds, factById);
|
|
258
|
+
return {
|
|
259
|
+
key,
|
|
260
|
+
lane: cluster.lane,
|
|
261
|
+
kind: normalizeConsensusKind(cluster.kind) ?? '事实',
|
|
262
|
+
text,
|
|
263
|
+
factIds: [...cluster.factIds],
|
|
264
|
+
refs: refsOf(cluster.factIds, factById),
|
|
265
|
+
status: statusByKey.get(key) ?? defaultStatusFor(cluster.lane),
|
|
266
|
+
...(cluster.supersededByKey?.trim() ? { supersededByKey: cluster.supersededByKey.trim() } : {}),
|
|
267
|
+
chars: text.length,
|
|
268
|
+
};
|
|
269
|
+
});
|
|
270
|
+
const derived = draftDerived.map((item) => ({
|
|
271
|
+
key: item.key.trim(),
|
|
272
|
+
lane: 'C',
|
|
273
|
+
kind: normalizeConsensusKind(item.kind) ?? '事实',
|
|
274
|
+
text: normalizeClusterText(item.text, item.factIds, factById),
|
|
275
|
+
fromKey: item.fromKey,
|
|
276
|
+
factIds: [...item.factIds],
|
|
277
|
+
refs: refsOf(item.factIds, factById),
|
|
278
|
+
}));
|
|
279
|
+
const lanes = CONSENSUS_LANES.reduce((counts, lane) => {
|
|
280
|
+
counts[lane] = clusters.filter((cluster) => cluster.lane === lane).length;
|
|
281
|
+
return counts;
|
|
282
|
+
}, {});
|
|
283
|
+
const archivedFacts = clusters
|
|
284
|
+
.filter((cluster) => cluster.lane === 'D')
|
|
285
|
+
.reduce((total, cluster) => total + cluster.factIds.length, 0);
|
|
286
|
+
const mergedFacts = clusters.reduce((total, cluster) => total + cluster.factIds.length - 1, 0);
|
|
287
|
+
const statusCounts = CONSENSUS_STATUSES.reduce((counts, status) => {
|
|
288
|
+
counts[status] = clusters.filter((cluster) => cluster.status === status).length;
|
|
289
|
+
return counts;
|
|
290
|
+
}, {});
|
|
291
|
+
const missingFactIds = facts.filter((fact) => !fact.sourceMessageId).map((fact) => fact.factId);
|
|
292
|
+
const signature = [
|
|
293
|
+
CONSENSUS_SCHEMA,
|
|
294
|
+
options.mode,
|
|
295
|
+
facts.map((fact) => `${fact.factId}@${fact.eventSeq}`).sort().join('|'),
|
|
296
|
+
clusters
|
|
297
|
+
.map((cluster) => `${cluster.key}:${cluster.lane}:${cluster.kind}:${cluster.status}:${[...cluster.factIds].sort().join(',')}:${cluster.text}`)
|
|
298
|
+
.sort()
|
|
299
|
+
.join('|'),
|
|
300
|
+
derived
|
|
301
|
+
.map((cluster) => `${cluster.key}:${cluster.fromKey}:${[...cluster.factIds].sort().join(',')}:${cluster.text}`)
|
|
302
|
+
.sort()
|
|
303
|
+
.join('|'),
|
|
304
|
+
].join('#');
|
|
305
|
+
const hash = stableHash(signature).split(':').at(-1) ?? '0';
|
|
306
|
+
return {
|
|
307
|
+
ok: true,
|
|
308
|
+
errors: [],
|
|
309
|
+
state: {
|
|
310
|
+
schema: CONSENSUS_SCHEMA,
|
|
311
|
+
version: `${CONSENSUS_SCHEMA}:${hash}`,
|
|
312
|
+
builtAt: now,
|
|
313
|
+
mode: options.mode,
|
|
314
|
+
clusters,
|
|
315
|
+
derived,
|
|
316
|
+
metrics: {
|
|
317
|
+
facts: facts.length,
|
|
318
|
+
clusters: clusters.length,
|
|
319
|
+
derived: derived.length,
|
|
320
|
+
lanes,
|
|
321
|
+
statusCounts,
|
|
322
|
+
archivedFacts,
|
|
323
|
+
mergedFacts,
|
|
324
|
+
},
|
|
325
|
+
provenance: {
|
|
326
|
+
expected: facts.length,
|
|
327
|
+
resolved: facts.length - missingFactIds.length,
|
|
328
|
+
missingFactIds,
|
|
329
|
+
},
|
|
330
|
+
},
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
const SECTION_TITLES = {
|
|
334
|
+
A: '[用户指令]',
|
|
335
|
+
B: '[群共识(结论,非用户指令)]',
|
|
336
|
+
C: '[任务]',
|
|
337
|
+
candidate: '[待确认(非指令,仅供核对)]',
|
|
338
|
+
};
|
|
339
|
+
const NOTE_RESERVE = 28;
|
|
340
|
+
function promptLine(cluster) {
|
|
341
|
+
const refs = [...new Set(cluster.refs.map((ref) => `#${ref.factEventSeq}`))].join(' ');
|
|
342
|
+
return refs ? `- ${cluster.text}(${refs})` : `- ${cluster.text}`;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* 渲染注入 prompt 的共识块:A(用户指令)优先,其次任务,最后群共识;D 永不注入。
|
|
346
|
+
* 超预算时整行丢弃并显式注明“另有 N 条见任务板/交接单”,不静默截断。
|
|
347
|
+
*/
|
|
348
|
+
export function renderConsensusPrompt(state, options = {}) {
|
|
349
|
+
const budget = Math.max(200, options.charBudget ?? DEFAULT_PROMPT_BUDGET);
|
|
350
|
+
const sections = [
|
|
351
|
+
{ lane: 'A', entries: state.clusters.filter((cluster) => cluster.lane === 'A' && AUTHORITATIVE_STATUSES.includes(cluster.status)) },
|
|
352
|
+
{ lane: 'C', entries: state.clusters.filter((cluster) => cluster.lane === 'C' && AUTHORITATIVE_STATUSES.includes(cluster.status)) },
|
|
353
|
+
{
|
|
354
|
+
lane: 'B',
|
|
355
|
+
entries: state.clusters
|
|
356
|
+
.filter((cluster) => cluster.lane === 'B' && AUTHORITATIVE_STATUSES.includes(cluster.status))
|
|
357
|
+
.sort((left, right) => {
|
|
358
|
+
const leftSeq = Math.max(...left.refs.map((ref) => ref.factEventSeq), 0);
|
|
359
|
+
const rightSeq = Math.max(...right.refs.map((ref) => ref.factEventSeq), 0);
|
|
360
|
+
return rightSeq - leftSeq;
|
|
361
|
+
}),
|
|
362
|
+
},
|
|
363
|
+
{ lane: 'candidate', entries: state.clusters.filter((cluster) => cluster.status === 'candidate') },
|
|
364
|
+
];
|
|
365
|
+
const lines = [];
|
|
366
|
+
const included = [];
|
|
367
|
+
let dropped = 0;
|
|
368
|
+
for (const section of sections) {
|
|
369
|
+
if (section.entries.length === 0)
|
|
370
|
+
continue;
|
|
371
|
+
const title = SECTION_TITLES[section.lane];
|
|
372
|
+
if (lines.join('\n').length + title.length + 1 > budget - NOTE_RESERVE) {
|
|
373
|
+
dropped += section.entries.length;
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
lines.push(title);
|
|
377
|
+
for (const entry of section.entries) {
|
|
378
|
+
const line = promptLine(entry);
|
|
379
|
+
const current = lines.join('\n').length;
|
|
380
|
+
if (current + line.length + 1 > budget - NOTE_RESERVE) {
|
|
381
|
+
dropped += 1;
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
lines.push(line);
|
|
385
|
+
included.push(entry.key);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
const note = dropped > 0 ? `(另有 ${dropped} 条见任务板/交接单)` : '';
|
|
389
|
+
let block = lines.join('\n');
|
|
390
|
+
if (block && note)
|
|
391
|
+
block += '\n';
|
|
392
|
+
block += note;
|
|
393
|
+
return { block, chars: block.length, included, dropped };
|
|
394
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 群级上下文预算(按群里**最小上下文**统一压)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么按最小而不是按发言者各自:同一轮里不同成员如果看到不同长度的上下文,
|
|
5
|
+
* 会各说各话(1M 角色看到 150k tokens,256k 角色只看到 64k)。统一预算后,
|
|
6
|
+
* 同一轮所有人共享同一份"压缩后的上下文",一致性优先;1M 角色想看得更多,
|
|
7
|
+
* 用 memory_search / memory_expand 按需拉原文(推压缩 + 拉原文)。
|
|
8
|
+
*
|
|
9
|
+
* 预算 = min(
|
|
10
|
+
* 每个在场成员模型: min(上下文 × 档位份额, 档位绝对上限)
|
|
11
|
+
* )
|
|
12
|
+
* 再按固定份额分给四块:窗口 60% / 摘要 18% / 事实 10% / 记忆 12%。
|
|
13
|
+
* 全确定性:同一组输入永远算出同一预算,可回放、可 golden 对照。
|
|
14
|
+
*/
|
|
15
|
+
import { type BudgetTier } from './budget-plan.ts';
|
|
16
|
+
/** 四块份额;窗口用不完时记忆块上限内的余量回流窗口。 */
|
|
17
|
+
export declare const CONTEXT_BLOCK_SHARES: {
|
|
18
|
+
readonly window: 0.6;
|
|
19
|
+
readonly summary: 0.18;
|
|
20
|
+
readonly facts: 0.1;
|
|
21
|
+
readonly memory: 0.12;
|
|
22
|
+
};
|
|
23
|
+
/** 摘要/记忆块的字符硬上限(防单块吃穿预算;数值来自实测成本与可读性)。 */
|
|
24
|
+
export declare const MAX_SUMMARY_BLOCK_CHARS = 32000;
|
|
25
|
+
export declare const MAX_MEMORY_BLOCK_CHARS = 8000;
|
|
26
|
+
export declare const MIN_MEMORY_BLOCK_CHARS = 1400;
|
|
27
|
+
export declare const MIN_SUMMARY_BLOCK_CHARS = 2000;
|
|
28
|
+
export declare const MAX_WINDOW_BLOCK_CHARS = 400000;
|
|
29
|
+
export declare const MIN_WINDOW_BLOCK_CHARS = 4000;
|
|
30
|
+
export interface GroupContextEntry {
|
|
31
|
+
personaId: string;
|
|
32
|
+
model?: string;
|
|
33
|
+
contextWindow?: number;
|
|
34
|
+
}
|
|
35
|
+
export interface GroupTokensDecision {
|
|
36
|
+
/** 群级 token 预算(= 最小成员窗口 × 份额,再夹档位上限)。 */
|
|
37
|
+
tokens: number;
|
|
38
|
+
tier: BudgetTier;
|
|
39
|
+
/** 决定预算的那个成员(最小窗口);没有任何窗口信息时 undefined。 */
|
|
40
|
+
smallest?: {
|
|
41
|
+
personaId: string;
|
|
42
|
+
model?: string;
|
|
43
|
+
contextWindow: number;
|
|
44
|
+
};
|
|
45
|
+
/** 有窗口信息的成员数(0 = 调用方应退回旧估算)。 */
|
|
46
|
+
knownWindows: number;
|
|
47
|
+
clampedBy: 'member-window' | 'tier-cap';
|
|
48
|
+
}
|
|
49
|
+
/** 群级 token 预算:取在场成员里最小上下文。 */
|
|
50
|
+
export declare function groupContextTokens(entries: readonly GroupContextEntry[], tier?: unknown): GroupTokensDecision;
|
|
51
|
+
export interface ContextAllocation {
|
|
52
|
+
tokens: number;
|
|
53
|
+
ratio: number;
|
|
54
|
+
windowChars: number;
|
|
55
|
+
summaryChars: number;
|
|
56
|
+
factsCount: number;
|
|
57
|
+
memoryChars: number;
|
|
58
|
+
notes: string[];
|
|
59
|
+
}
|
|
60
|
+
/** 把群级 token 预算按份额换成各块的字符/条数预算(确定性)。 */
|
|
61
|
+
export declare function allocateContextBudget(tokens: number, options?: {
|
|
62
|
+
ratio?: number;
|
|
63
|
+
factsMax?: number;
|
|
64
|
+
}): ContextAllocation;
|
|
65
|
+
export interface GroupContextBudget extends GroupTokensDecision, ContextAllocation {
|
|
66
|
+
}
|
|
67
|
+
/** 一次性算出群级预算 + 四块分配;没有任何窗口信息时返回 undefined(调用方回退)。 */
|
|
68
|
+
export declare function groupContextBudget(entries: readonly GroupContextEntry[], options?: {
|
|
69
|
+
tier?: unknown;
|
|
70
|
+
ratio?: number;
|
|
71
|
+
}): GroupContextBudget | undefined;
|
|
72
|
+
/** 群级预算的输入来源(客户端注入;prompt 组装与摘要生成必须用同一份输入)。 */
|
|
73
|
+
export interface GroupBudgetPolicy {
|
|
74
|
+
/** economy / standard / deep。 */
|
|
75
|
+
tier?: unknown;
|
|
76
|
+
/** chars/token(内容先验或校准;不传则 allocate 用 DEFAULT_RATIO)。 */
|
|
77
|
+
ratio?: number;
|
|
78
|
+
/** 按角色查模型上下文窗口(群里取在场成员最小值)。 */
|
|
79
|
+
contextWindowForPersona?: (personaId: string) => number | undefined;
|
|
80
|
+
/** 按角色查模型 id(写进 groupMinModel,便于回执定位)。 */
|
|
81
|
+
modelForPersona?: (personaId: string) => string | undefined;
|
|
82
|
+
/** 当前发言角色自己的窗口:没有按角色查表时用它兜底(与旧 runtime 行为一致)。 */
|
|
83
|
+
fallback?: {
|
|
84
|
+
personaId: string;
|
|
85
|
+
contextWindow?: number;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/** 在场成员(排除禁言)→ 群级预算条目;按 personaId 排序保证同一群签名稳定。 */
|
|
89
|
+
export declare function groupEntriesForThread(thread: Pick<import('./types.ts').Thread, 'members'> | {
|
|
90
|
+
members?: readonly {
|
|
91
|
+
personaId: string;
|
|
92
|
+
activity: string;
|
|
93
|
+
}[];
|
|
94
|
+
}, policy?: GroupBudgetPolicy): GroupContextEntry[];
|
|
95
|
+
/** 群级预算(thread 版本);没有成员的窗口信息时返回 undefined(调用方回退旧估算)。 */
|
|
96
|
+
export declare function groupBudgetForThread(thread: Pick<import('./types.ts').Thread, 'members'> | {
|
|
97
|
+
members?: readonly {
|
|
98
|
+
personaId: string;
|
|
99
|
+
activity: string;
|
|
100
|
+
}[];
|
|
101
|
+
}, policy?: GroupBudgetPolicy): GroupContextBudget | undefined;
|
|
102
|
+
/** 预算输入签名:只有成员集合 / 模型窗口 / 档位变化才会变(消息条数、时间不参与)。 */
|
|
103
|
+
export declare function groupBudgetSignature(entries: readonly GroupContextEntry[], tier: unknown): string;
|
|
104
|
+
/** 回执里的预算快照(结构化、可 diff;不含正文/时间)。 */
|
|
105
|
+
export declare const CONTEXT_BUDGET_VERSION = "context-budget-v1";
|
|
106
|
+
export interface ContextBudgetReceipt {
|
|
107
|
+
version: string;
|
|
108
|
+
source: 'group' | 'fallback';
|
|
109
|
+
tier: BudgetTier;
|
|
110
|
+
tokens: number | null;
|
|
111
|
+
ratio: number;
|
|
112
|
+
windowChars: number;
|
|
113
|
+
summaryChars: number;
|
|
114
|
+
digestChars: number;
|
|
115
|
+
chapterChars: number;
|
|
116
|
+
memoryChars: number;
|
|
117
|
+
factsCount: number;
|
|
118
|
+
groupMinModel?: {
|
|
119
|
+
personaId: string;
|
|
120
|
+
model?: string;
|
|
121
|
+
contextWindow: number;
|
|
122
|
+
};
|
|
123
|
+
/** 预算输入签名(同签名 = 同一预算;粘滞保证同签名不重算)。 */
|
|
124
|
+
signature: string;
|
|
125
|
+
}
|
|
126
|
+
export interface BudgetChangedEvent {
|
|
127
|
+
from: Pick<ContextBudgetReceipt, 'tokens' | 'tier' | 'windowChars' | 'summaryChars' | 'groupMinModel'>;
|
|
128
|
+
to: Pick<ContextBudgetReceipt, 'tokens' | 'tier' | 'windowChars' | 'summaryChars' | 'groupMinModel'>;
|
|
129
|
+
reason: 'members' | 'model' | 'tier' | 'ratio';
|
|
130
|
+
}
|
|
131
|
+
export declare function budgetViewOf(budget: ContextAllocation | GroupContextBudget, input: {
|
|
132
|
+
tier?: unknown;
|
|
133
|
+
source?: 'group' | 'fallback';
|
|
134
|
+
signature?: string;
|
|
135
|
+
}): ContextBudgetReceipt;
|
|
136
|
+
/**
|
|
137
|
+
* 预算变更事件:**只有显式输入变化**(成员集合 / 模型窗口 / 档位 / ratio)才会返回。
|
|
138
|
+
* 消息条数、时间、轮次都不在判据里;同输入必然返回 undefined(单测锁死)。
|
|
139
|
+
*/
|
|
140
|
+
export declare function budgetChangeOf(input: {
|
|
141
|
+
previous: ContextBudgetReceipt | undefined;
|
|
142
|
+
next: ContextBudgetReceipt;
|
|
143
|
+
previousInputs?: {
|
|
144
|
+
entries: readonly GroupContextEntry[];
|
|
145
|
+
tier: unknown;
|
|
146
|
+
ratio: number;
|
|
147
|
+
};
|
|
148
|
+
nextInputs: {
|
|
149
|
+
entries: readonly GroupContextEntry[];
|
|
150
|
+
tier: unknown;
|
|
151
|
+
ratio: number;
|
|
152
|
+
};
|
|
153
|
+
}): BudgetChangedEvent | undefined;
|