dsh-compaction-weave 0.0.0-stage → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +103 -0
- package/LICENSE +21 -0
- package/README.en.md +145 -0
- package/README.md +148 -2
- package/cordis.patch.yml +19 -0
- package/docs/architecture.md +84 -0
- package/docs/benchmarks/published-0.3.2-rc.1-tests.json +576 -0
- package/docs/benchmarks/rc2-documentation.json +41 -0
- package/docs/benchmarks/rc2-linux-tests.json +590 -0
- package/docs/benchmarks/rc2-live-and-cost.json +746 -0
- package/docs/benchmarks/rc3-host-compatibility.json +2314 -0
- package/docs/benchmarks/rc4-tests.json +617 -0
- package/docs/benchmarks/review35-compression.json +117 -0
- package/docs/benchmarks/v0.3.2-live-four-way.json +3495 -0
- package/docs/benchmarks/v0.3.2-tests.json +590 -0
- package/docs/configuration.md +131 -0
- package/docs/development.md +95 -0
- package/docs/en/architecture.md +75 -0
- package/docs/en/configuration.md +131 -0
- package/docs/en/development.md +92 -0
- package/docs/en/feedback.md +47 -0
- package/docs/en/principles.md +235 -0
- package/docs/en/testing.md +368 -0
- package/docs/en/troubleshooting.md +60 -0
- package/docs/feedback.md +47 -0
- package/docs/images/principles-context.en.svg +77 -0
- package/docs/images/principles-context.zh.svg +77 -0
- package/docs/images/principles-pipeline.en.svg +93 -0
- package/docs/images/principles-pipeline.zh.svg +93 -0
- package/docs/images/principles-recovery.en.svg +46 -0
- package/docs/images/principles-recovery.zh.svg +46 -0
- package/docs/principles.md +235 -0
- package/docs/test-results.json +634 -0
- package/docs/testing.md +358 -0
- package/docs/troubleshooting.md +60 -0
- package/lib/abort.js +13 -0
- package/lib/action-state.js +60 -0
- package/lib/atomic-replace.js +18 -0
- package/lib/attachments.js +76 -0
- package/lib/audit-reject.js +161 -0
- package/lib/audit.js +939 -0
- package/lib/cache-policy.js +71 -0
- package/lib/client.js +696 -0
- package/lib/command-compact.js +38 -0
- package/lib/command-heartbeat.js +61 -0
- package/lib/config.js +596 -0
- package/lib/constraint-scope.js +37 -0
- package/lib/continuation-budget.js +69 -0
- package/lib/continuation-pending.js +57 -0
- package/lib/continuation-progress.js +46 -0
- package/lib/continuation.js +287 -0
- package/lib/engine-entry.js +103 -0
- package/lib/engine.js +2485 -0
- package/lib/failure-diagnostics.js +220 -0
- package/lib/history-source.js +74 -0
- package/lib/history.js +712 -0
- package/lib/idle-compaction.js +64 -0
- package/lib/index.js +611 -0
- package/lib/instruction-source.js +163 -0
- package/lib/journal.js +164 -0
- package/lib/manual-error.js +179 -0
- package/lib/memory-policy.js +383 -0
- package/lib/meter.js +387 -0
- package/lib/observation-mask.js +390 -0
- package/lib/proactive.js +108 -0
- package/lib/progress-facts.js +347 -0
- package/lib/progress-state.js +30 -0
- package/lib/prompt.js +194 -0
- package/lib/reconstruct.js +72 -0
- package/lib/reference-input.js +107 -0
- package/lib/region.js +972 -0
- package/lib/source-provenance.js +58 -0
- package/lib/source-recovery.js +22 -0
- package/lib/source-refs.js +137 -0
- package/lib/state.js +46 -0
- package/lib/storage-path.js +42 -0
- package/lib/summary-budget.js +187 -0
- package/lib/summary-config.js +308 -0
- package/lib/summary-progress.js +56 -0
- package/lib/summary-retry.js +27 -0
- package/lib/summary-timeout.js +68 -0
- package/lib/summary.js +476 -0
- package/lib/tools.js +341 -0
- package/lib/transaction-host.js +252 -0
- package/lib/transaction.js +285 -0
- package/lib/validate.js +503 -0
- package/lib/version.js +3 -0
- package/lib/window.js +120 -0
- package/lib/work-report.js +55 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +71 -4
package/lib/meter.js
ADDED
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
// 最小 StreamChunk 折叠器。
|
|
2
|
+
//
|
|
3
|
+
// 官方用的是 `@deepseek-ai/dsh-llm` 里的 `BlockAssembler`,但那个包在本机
|
|
4
|
+
// 解析不到(本地插件是 junction 装的,真实路径下没有 node_modules),所以
|
|
5
|
+
// 这里自带一份。
|
|
6
|
+
//
|
|
7
|
+
// ⚠️ 不能因为"摘要只要纯文本"就只认 `text-delta`。宿主的 `BlockAssembler`
|
|
8
|
+
// (`dsh-llm/lib/index.js:954-1025`、`dsh-llm/lib/types/assembler.js:34-108`)
|
|
9
|
+
// 对同一个 index 有两套文本来源,**block-end 携带的 block 是终态、优先于
|
|
10
|
+
// 已累积的 delta**:
|
|
11
|
+
//
|
|
12
|
+
// case 'block-end': {
|
|
13
|
+
// const partial = this.ensure(chunk.index, chunk.block.type);
|
|
14
|
+
// if (partial.block) return; // 首次关闭生效,后来的重关闭忽略
|
|
15
|
+
// partial.block = chunk.block;
|
|
16
|
+
// return;
|
|
17
|
+
// }
|
|
18
|
+
// assemble(partial) { if (partial.block) return partial.block; ... }
|
|
19
|
+
//
|
|
20
|
+
// 也就是说存在两种合法流:
|
|
21
|
+
// ① 只有 block-start + block-end(block.text 是完整文本,没有任何 delta)
|
|
22
|
+
// ② delta 是草稿、block-end 的 block.text 才是定稿(覆盖 delta)
|
|
23
|
+
//
|
|
24
|
+
// 只折叠 delta 会分别导致"有完整文本却报空摘要"和"提交了草稿"。所以这里
|
|
25
|
+
// 按 index 维护 partial,关闭的块取终态、未关闭的文本块才折叠 delta,
|
|
26
|
+
// 与宿主逐条对齐。
|
|
27
|
+
//
|
|
28
|
+
// 协议依据 docs/subsystems/llm-streaming.md:
|
|
29
|
+
// - chunk 类型:text-delta / reasoning-delta / tool-arg-delta / block-start /
|
|
30
|
+
// block-end / usage / finish
|
|
31
|
+
// - usage 必须在 finish 之前;finish 之后不得再有 chunk
|
|
32
|
+
// - 失败有两条合法路径:stream() 直接 throw,或结束流时发 finish{kind:'error'|'aborted'}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 没有 `index` 的 chunk 共用的 partial 键。
|
|
36
|
+
* 用 Symbol 而不是数字,避免和真实 index 撞键。
|
|
37
|
+
*/
|
|
38
|
+
const ANONYMOUS_BLOCK = Symbol('anonymous-block');
|
|
39
|
+
|
|
40
|
+
export class SummaryAssembler {
|
|
41
|
+
constructor() {
|
|
42
|
+
/**
|
|
43
|
+
* index → { blockType, text, block }
|
|
44
|
+
* `block` 一旦被 block-end 填上就是终态,后续同 index 的 delta 一律忽略。
|
|
45
|
+
* @type {Map<number, {blockType:string, text:string, block:any}>}
|
|
46
|
+
*/
|
|
47
|
+
this._partials = new Map();
|
|
48
|
+
/** 流里出现过的 index,按首次出现顺序。 @type {number[]} */
|
|
49
|
+
this._order = [];
|
|
50
|
+
/** @type {string[]} */
|
|
51
|
+
this._reasoning = [];
|
|
52
|
+
/** @type {number} */
|
|
53
|
+
this._reasoningChars = 0;
|
|
54
|
+
/** @type {{inputTokens?:number, outputTokens?:number, cacheReadTokens?:number, cacheWriteTokens?:number}|null} */
|
|
55
|
+
this.usage = null;
|
|
56
|
+
/** @type {{kind:string, failure?:unknown}|null} */
|
|
57
|
+
this.finish = null;
|
|
58
|
+
this._chunkCount = 0;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @param {any} chunk */
|
|
62
|
+
push(chunk) {
|
|
63
|
+
if (!chunk || typeof chunk !== 'object') return;
|
|
64
|
+
this._chunkCount += 1;
|
|
65
|
+
const change = { textChanged: false, reasoningChanged: false };
|
|
66
|
+
|
|
67
|
+
switch (chunk.type) {
|
|
68
|
+
case 'block-start': {
|
|
69
|
+
this._ensure(chunk.index, chunk.blockType);
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
case 'text-delta':
|
|
73
|
+
case 'reasoning-delta': {
|
|
74
|
+
const text = chunk.text ?? chunk.delta ?? chunk.content;
|
|
75
|
+
const isText = chunk.type === 'text-delta';
|
|
76
|
+
const partial = this._ensure(chunk.index, isText ? 'text' : 'reasoning');
|
|
77
|
+
// 已被 block-end 关闭:迟到的 delta 属于畸形流,忽略(宿主同样处理)。
|
|
78
|
+
if (partial.block) break;
|
|
79
|
+
if (typeof text === 'string' && text.length > 0) {
|
|
80
|
+
partial.text += text;
|
|
81
|
+
if (isText) change.textChanged = true;
|
|
82
|
+
else change.reasoningChanged = true;
|
|
83
|
+
}
|
|
84
|
+
if (!isText && typeof text === 'string') {
|
|
85
|
+
this._reasoning.push(text);
|
|
86
|
+
this._reasoningChars += text.length;
|
|
87
|
+
}
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
case 'block-end': {
|
|
91
|
+
const block = chunk.block;
|
|
92
|
+
if (!block || typeof block !== 'object' || typeof block.type !== 'string') break;
|
|
93
|
+
const partial = this._ensure(chunk.index, block.type);
|
|
94
|
+
// 首次关闭生效;重关闭忽略,避免后来的畸形块覆盖已定稿内容。
|
|
95
|
+
if (partial.block) break;
|
|
96
|
+
if (typeof block.text === 'string' && block.text.length > 0 && block.text !== partial.text) {
|
|
97
|
+
if (block.type === 'text') change.textChanged = true;
|
|
98
|
+
if (block.type === 'reasoning') change.reasoningChanged = true;
|
|
99
|
+
}
|
|
100
|
+
partial.block = block;
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
case 'usage': {
|
|
104
|
+
// ⚠️ 这里只能放**已定义的数值字段**。宿主 `session.append` 用
|
|
105
|
+
// `snapshotJsonValue` 做无损 JSON 校验(`dsh-session/lib/index.js:1523`),
|
|
106
|
+
// 它不像 `JSON.stringify` 那样把 undefined 丢掉,而是直接判定
|
|
107
|
+
// "non-JSON-serializable data" 并抛错。而宿主自己的 `mapUsage`
|
|
108
|
+
// (`dsh-llm-pi-ai/lib/index.js:1439-1446`)恰恰在缓存为 0 时会
|
|
109
|
+
// **省略** `cacheReadTokens` / `cacheWriteTokens` —— 也就是说,
|
|
110
|
+
// 一个完全合法的 usage 经过无条件四字段重建后,就会让压缩在提交阶段
|
|
111
|
+
// 失败,只留下 compaction/start 和一个报错的 compaction/end。
|
|
112
|
+
const u = chunk.usage ?? chunk;
|
|
113
|
+
this.usage = compactUsage({
|
|
114
|
+
inputTokens: pickNumber(u.inputTokens, u.input_tokens, u.promptTokens),
|
|
115
|
+
outputTokens: pickNumber(u.outputTokens, u.output_tokens, u.completionTokens),
|
|
116
|
+
totalTokens: pickNumber(u.totalTokens, u.total_tokens),
|
|
117
|
+
cacheReadTokens: pickNumber(u.cacheReadTokens, u.cache_read_input_tokens),
|
|
118
|
+
cacheWriteTokens: pickNumber(u.cacheWriteTokens, u.cache_creation_input_tokens),
|
|
119
|
+
});
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
case 'finish': {
|
|
123
|
+
// finish 的形态在不同适配器/版本里有三种:
|
|
124
|
+
// { type:'finish', kind:'max-tokens' }
|
|
125
|
+
// { type:'finish', reason:{ kind:'max-tokens', failure? } }
|
|
126
|
+
// { type:'finish', finishReason:'max-tokens' }
|
|
127
|
+
const reason = chunk.reason;
|
|
128
|
+
const kind =
|
|
129
|
+
typeof chunk.kind === 'string'
|
|
130
|
+
? chunk.kind
|
|
131
|
+
: typeof reason === 'string'
|
|
132
|
+
? reason
|
|
133
|
+
: typeof reason?.kind === 'string'
|
|
134
|
+
? reason.kind
|
|
135
|
+
: typeof chunk.finishReason === 'string'
|
|
136
|
+
? chunk.finishReason
|
|
137
|
+
: 'stop';
|
|
138
|
+
const failure = chunk.failure ?? chunk.error ?? reason?.failure ?? reason?.error;
|
|
139
|
+
this.finish = { kind, failure };
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
// tool-call-delta / tool-arg-delta:摘要不需要工具调用回放,忽略。
|
|
143
|
+
default:
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
return change;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 取(或建)某个 index 的 partial,与宿主 `ensure` 同义。
|
|
151
|
+
*
|
|
152
|
+
* ⚠️ 缺 `index` 的 chunk 必须落进**同一个** partial,不能每条都新建。
|
|
153
|
+
* 宿主用 `partials.get(chunk.index)`:`undefined` 也是个普通键,
|
|
154
|
+
* 所有没带 index 的 delta 都累加进那一份。这里若拿"当前序号"当键,
|
|
155
|
+
* 两个连续的 `{type:'text-delta', text:'你'}` / `{text:'好'}` 会变成
|
|
156
|
+
* 两个块,拼出来是 `你\n好` —— 一份被无声切成两半的正文。
|
|
157
|
+
* @param {any} index
|
|
158
|
+
* @param {string} blockType
|
|
159
|
+
*/
|
|
160
|
+
_ensure(index, blockType) {
|
|
161
|
+
const key = typeof index === 'number' ? index : ANONYMOUS_BLOCK;
|
|
162
|
+
let partial = this._partials.get(key);
|
|
163
|
+
if (!partial) {
|
|
164
|
+
partial = { blockType, text: '', block: null };
|
|
165
|
+
this._partials.set(key, partial);
|
|
166
|
+
this._order.push(key);
|
|
167
|
+
}
|
|
168
|
+
return partial;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 把单个 partial 折叠成块,与宿主 `assemble` 同义:
|
|
173
|
+
* 已关闭的取终态 block,未关闭的文本块用累积 delta。
|
|
174
|
+
* @param {{blockType:string, text:string, block:any}} partial
|
|
175
|
+
*/
|
|
176
|
+
_assemble(partial) {
|
|
177
|
+
if (partial.block) return partial.block;
|
|
178
|
+
switch (partial.blockType) {
|
|
179
|
+
case 'text':
|
|
180
|
+
return { type: 'text', text: partial.text };
|
|
181
|
+
case 'reasoning':
|
|
182
|
+
return { type: 'reasoning', text: partial.text };
|
|
183
|
+
default:
|
|
184
|
+
// 未关闭的非文本块(tool-call 等):摘要用不到,返回 null 由调用方跳过。
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** 是否已经有可见正文。 */
|
|
190
|
+
get hasText() {
|
|
191
|
+
return this.blocks().some(
|
|
192
|
+
(block) => block.type === 'text' && typeof block.text === 'string' && block.text.trim().length > 0,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 纯文本正文,按流顺序拼接。
|
|
198
|
+
*
|
|
199
|
+
* ⚠️ 块之间必须用换行连接,与 `blocksToText` 完全一致。
|
|
200
|
+
* 这里曾经用空串 `join('')`:模型把九个 Markdown 小节各放在一个 text 块里、
|
|
201
|
+
* 每块末尾不带换行时,拼出来就是 `## 1. …## 2. …## 3. …` 一整行 ——
|
|
202
|
+
* 提交前的行首标题校验只会看到第 1 节,另外 8 节全被判成"缺失",
|
|
203
|
+
* 一份完全合法的摘要被拒绝。生成、校验、审计、存储必须共用**同一个**
|
|
204
|
+
* 文本表示,否则"校验通过"和"存进去的东西"根本不是同一份。
|
|
205
|
+
*/
|
|
206
|
+
get text() {
|
|
207
|
+
return blocksToText(this.blocks());
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
get reasoningChars() {
|
|
211
|
+
return this._reasoningChars;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
get chunkCount() {
|
|
215
|
+
return this._chunkCount;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 折成 ContentBlock[],按流顺序,**保留所有可组装的块类型**。
|
|
220
|
+
*
|
|
221
|
+
* ⚠️ 这里不能只留 text。摘要内容政策要做两件不同的事:
|
|
222
|
+
* 图片块必须像宿主一样**显式拒绝**(`UNSUPPORTED_CONTENT`),
|
|
223
|
+
* 而"先过滤掉再检查"等于把该报的错吞了 —— 模型输出了图片,
|
|
224
|
+
* 插件却当成纯文本摘要提交成功,用户拿到一份缺内容的检查点。
|
|
225
|
+
* 所以过滤交给调用方(`summaryText`)做,这里如实反映流里有什么。
|
|
226
|
+
* 只有"未关闭且不属于 text/reasoning"的块才返回 null(tool-call 之类)。
|
|
227
|
+
*/
|
|
228
|
+
blocks() {
|
|
229
|
+
const out = [];
|
|
230
|
+
for (const key of this._order) {
|
|
231
|
+
const partial = this._partials.get(key);
|
|
232
|
+
if (!partial) continue;
|
|
233
|
+
const block = this._assemble(partial);
|
|
234
|
+
if (!block) continue;
|
|
235
|
+
out.push(block);
|
|
236
|
+
}
|
|
237
|
+
return out;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* 按官方 `finishError` 的语义把异常 finish 转成 throw。
|
|
242
|
+
* @throws {Error}
|
|
243
|
+
*/
|
|
244
|
+
throwIfFailed() {
|
|
245
|
+
const finish = this.finish;
|
|
246
|
+
if (finish === null) return;
|
|
247
|
+
if (finish.kind === 'error' || finish.kind === 'aborted') {
|
|
248
|
+
const failure = finish.failure;
|
|
249
|
+
const message =
|
|
250
|
+
(failure && typeof failure === 'object' && typeof failure.message === 'string'
|
|
251
|
+
? failure.message
|
|
252
|
+
: typeof failure === 'string'
|
|
253
|
+
? failure
|
|
254
|
+
: null) ?? `summarization stream ended with ${finish.kind}`;
|
|
255
|
+
const error = new Error(message);
|
|
256
|
+
error.code =
|
|
257
|
+
(failure && typeof failure === 'object' && typeof failure.code === 'string'
|
|
258
|
+
? failure.code
|
|
259
|
+
: finish.kind === 'aborted'
|
|
260
|
+
? 'ABORTED'
|
|
261
|
+
: 'STREAM_ERROR');
|
|
262
|
+
if (failure !== undefined) error.cause = failure;
|
|
263
|
+
throw error;
|
|
264
|
+
}
|
|
265
|
+
if (finish.kind === 'max-tokens') {
|
|
266
|
+
const error = new Error('summarization truncated at the token cap (incomplete checkpoint)');
|
|
267
|
+
error.code = 'MAX_TOKENS';
|
|
268
|
+
throw error;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function pickNumber(...values) {
|
|
274
|
+
for (const value of values) {
|
|
275
|
+
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
|
276
|
+
}
|
|
277
|
+
return undefined;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* 去掉值为 `undefined` 的键,得到一个**可被宿主无损 JSON 快照**接受的 usage 对象。
|
|
282
|
+
*
|
|
283
|
+
* `session.append` 走的是 `snapshotJsonValue`,它把 `undefined` 视为
|
|
284
|
+
* 不可序列化(而不是像 `JSON.stringify` 那样跳过),所以任何值为 undefined
|
|
285
|
+
* 的字段都必须在这里剔除。全部字段都缺失时返回 `null`,让调用方整段省略。
|
|
286
|
+
*
|
|
287
|
+
* @param {{inputTokens?:number, outputTokens?:number, totalTokens?:number, cacheReadTokens?:number, cacheWriteTokens?:number}} raw
|
|
288
|
+
* @returns {{inputTokens?:number, outputTokens?:number, totalTokens?:number, cacheReadTokens?:number, cacheWriteTokens?:number}|null}
|
|
289
|
+
*/
|
|
290
|
+
export function compactUsage(raw) {
|
|
291
|
+
if (!raw || typeof raw !== 'object') return null;
|
|
292
|
+
const out = {};
|
|
293
|
+
let count = 0;
|
|
294
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
295
|
+
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
296
|
+
out[key] = value;
|
|
297
|
+
count += 1;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return count === 0 ? null : out;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* 粗估 token:CJK 按 1 字 ≈ 1 token,其余按 4 字符 ≈ 1 token。
|
|
305
|
+
* 只用于"摘要是否比原文小"这类相对判断,不用于绝对预算。
|
|
306
|
+
*
|
|
307
|
+
* ⚠️ 这不是真实分词。宿主 `tokenMeter.estimateMessage` 同样是固定 4 字符/token
|
|
308
|
+
* 加结构开销(`dsh-token-meter/lib/index.js:615-667`),两者都只是估算。
|
|
309
|
+
* 任何"请求一定落在窗口内"的结论都不能建立在这个数上。
|
|
310
|
+
*
|
|
311
|
+
* @param {string} text
|
|
312
|
+
*/
|
|
313
|
+
export function approxTokens(text) {
|
|
314
|
+
if (typeof text !== 'string' || text.length === 0) return 0;
|
|
315
|
+
let cjk = 0;
|
|
316
|
+
let other = 0;
|
|
317
|
+
for (const ch of text) {
|
|
318
|
+
const code = ch.codePointAt(0);
|
|
319
|
+
if (
|
|
320
|
+
(code >= 0x4e00 && code <= 0x9fff) ||
|
|
321
|
+
(code >= 0x3400 && code <= 0x4dbf) ||
|
|
322
|
+
(code >= 0x3040 && code <= 0x30ff) ||
|
|
323
|
+
(code >= 0xac00 && code <= 0xd7af)
|
|
324
|
+
) {
|
|
325
|
+
cjk += 1;
|
|
326
|
+
} else {
|
|
327
|
+
other += 1;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return Math.ceil(cjk + other / 4);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* 从 ContentBlock[] 里抽纯文本。
|
|
335
|
+
* @param {readonly any[]|undefined} blocks
|
|
336
|
+
*/
|
|
337
|
+
export function blocksToText(blocks) {
|
|
338
|
+
if (!Array.isArray(blocks)) return '';
|
|
339
|
+
const parts = [];
|
|
340
|
+
for (const block of blocks) {
|
|
341
|
+
if (!block || typeof block !== 'object') continue;
|
|
342
|
+
if (block.type === 'text' && typeof block.text === 'string') parts.push(block.text);
|
|
343
|
+
}
|
|
344
|
+
return parts.join('\n');
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* 官方 `summaryText` 的等价实现:**只拒绝图片**,其余块一律滤成 text。
|
|
349
|
+
*
|
|
350
|
+
* 宿主实现(`dsh-compaction-basic/lib/index.js:370-374`):
|
|
351
|
+
* ```js
|
|
352
|
+
* function summaryText(blocks) {
|
|
353
|
+
* if (contentHasImage(blocks)) throw new LlmError("compaction summary cannot contain image output", "UNSUPPORTED_CONTENT");
|
|
354
|
+
* return blocks.filter((block) => block.type === "text");
|
|
355
|
+
* }
|
|
356
|
+
* ```
|
|
357
|
+
* `contentHasImage` 只判 `block.type === "image"`(`dsh-llm/lib/index.js:593-595`)。
|
|
358
|
+
*
|
|
359
|
+
* ⚠️ 不能顺手把 reasoning 一起拒掉。开思考的模型先吐 `thinking_start/delta/end`
|
|
360
|
+
* (pi-ai 映射成 reasoning 块)再吐正文是**正常流形态**,不是畸形流:
|
|
361
|
+
* 宿主会接受这种摘要,而这里一旦报 `UNSUPPORTED_CONTENT`,
|
|
362
|
+
* 三级目标链会连着三个都失败,用户看到的是"摘要模型全部失败",
|
|
363
|
+
* 实际每个模型都好好答完了。内部推理不进检查点是对的(它不该被存下来),
|
|
364
|
+
* 但"不存"和"当作不支持内容拒绝"是两件完全不同的事。
|
|
365
|
+
*
|
|
366
|
+
* @param {readonly any[]} blocks
|
|
367
|
+
* @returns {readonly {type:'text', text:string}[]}
|
|
368
|
+
*/
|
|
369
|
+
export function summaryText(blocks) {
|
|
370
|
+
if (!Array.isArray(blocks)) {
|
|
371
|
+
const error = new Error('summarization produced no text summary content');
|
|
372
|
+
error.code = 'EMPTY_SUMMARY';
|
|
373
|
+
throw error;
|
|
374
|
+
}
|
|
375
|
+
if (blocks.some((block) => block && typeof block === 'object' && block.type === 'image')) {
|
|
376
|
+
const error = new Error('compaction summary cannot contain image output');
|
|
377
|
+
error.code = 'UNSUPPORTED_CONTENT';
|
|
378
|
+
throw error;
|
|
379
|
+
}
|
|
380
|
+
const text = blocksToText(blocks);
|
|
381
|
+
if (text.trim().length === 0) {
|
|
382
|
+
const error = new Error('summarization produced no text summary content');
|
|
383
|
+
error.code = 'EMPTY_SUMMARY';
|
|
384
|
+
throw error;
|
|
385
|
+
}
|
|
386
|
+
return [{ type: 'text', text }];
|
|
387
|
+
}
|