dsh-context-compression-improved 0.5.6 → 0.5.8
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/.gitattributes +2 -2
- package/.githooks/pre-push +37 -37
- package/.github/workflows/ci.yml +39 -39
- package/CHANGELOG.ja.md +169 -169
- package/CHANGELOG.ko.md +167 -167
- package/CHANGELOG.md +323 -304
- package/CHANGELOG.zh.md +152 -152
- package/CONTRIBUTING.md +22 -22
- package/LICENSE +21 -21
- package/README.ja.md +85 -85
- package/README.ko.md +85 -85
- package/README.md +94 -94
- package/README.zh.md +85 -85
- package/SECURITY.md +18 -18
- package/THIRD_PARTY_NOTICES.md +14 -14
- package/docs/compat-0.1.5-report.md +77 -77
- package/docs/installation.ja.md +78 -78
- package/docs/installation.ko.md +78 -78
- package/docs/installation.md +103 -103
- package/docs/installation.zh.md +100 -100
- package/docs/repair-log.md +657 -657
- package/eslint.config.js +30 -30
- package/package.json +163 -164
- package/packages/selector/LICENSE +21 -21
- package/packages/selector/README.md +26 -26
- package/packages/selector/README.zh.md +24 -24
- package/packages/selector/THIRD_PARTY_NOTICES.md +32 -32
- package/packages/selector/cordis.patch.yml +27 -27
- package/packages/selector/docs/history-tool-call-working-set-spec.md +112 -112
- package/packages/selector/docs/native-tool-result-selector-spec.md +34 -34
- package/packages/selector/docs/subagent-cache-reuse-spec.md +46 -46
- package/packages/selector/dsh.plugin.json +16 -16
- package/packages/selector/lib/client.d.ts +59 -0
- package/packages/selector/lib/client.js +1738 -10
- package/packages/selector/lib/{advisor-state.js → config.js} +816 -65
- package/packages/selector/lib/index.d.ts +13 -1
- package/packages/selector/lib/index.js +295 -3
- package/packages/selector/lib/pruner.d.ts +65 -0
- package/packages/selector/lib/pruner.js +497 -262
- package/packages/selector/lib/style.css +88 -46
- package/packages/selector/package.json +114 -114
- package/packages/selector/screenshots.json +3 -3
- package/packages/selector/src/client/CompressionProfileControls.tsx +293 -226
- package/packages/selector/src/client/CompressionProfileSelector.module.css +199 -170
- package/packages/selector/src/client/CompressionProfileSelector.tsx +85 -83
- package/packages/selector/src/client/CustomPolicyEditor.tsx +216 -216
- package/packages/selector/src/client/EstimatorControls.tsx +277 -277
- package/packages/selector/src/client/decode.ts +57 -49
- package/packages/selector/src/client/index.ts +240 -149
- package/packages/selector/src/client/locales/de.ts +141 -0
- package/packages/selector/src/client/locales/es.ts +141 -0
- package/packages/selector/src/client/locales/fr.ts +141 -0
- package/packages/selector/src/client/locales/it.ts +141 -0
- package/packages/selector/src/client/locales/ja.ts +141 -0
- package/packages/selector/src/client/locales/ko.ts +141 -0
- package/packages/selector/src/client/locales/ru.ts +141 -0
- package/packages/selector/src/client/locales.ts +282 -196
- package/packages/selector/src/client/monitor-fab.ts +336 -0
- package/packages/selector/src/client/preset-options.ts +88 -88
- package/packages/selector/src/client/savings-card.tsx +150 -0
- package/packages/selector/src/client/settings-section.tsx +140 -124
- package/packages/selector/src/css-modules.d.ts +6 -6
- package/packages/selector/src/deepseek-v4-tokenizer.ts +205 -205
- package/packages/selector/src/estimator-catalog.ts +104 -104
- package/packages/selector/src/index.ts +673 -463
- package/packages/selector/src/invariant.ts +113 -113
- package/packages/selector/src/preset-overlay.ts +626 -626
- package/packages/selector/src/profiles.ts +431 -394
- package/packages/selector/src/pruner/content.ts +201 -201
- package/packages/selector/src/pruner/session.ts +94 -94
- package/packages/selector/src/pruner/state.ts +57 -50
- package/packages/selector/src/pruner/tuning.ts +23 -23
- package/packages/selector/src/pruner/types.ts +78 -78
- package/packages/selector/src/pruner.ts +2736 -2402
- package/packages/selector/src/runtime/adaptive-cost.ts +205 -205
- package/packages/selector/src/runtime/audit.ts +303 -286
- package/packages/selector/src/runtime/config.ts +756 -706
- package/packages/selector/src/runtime/custom-policy.ts +278 -278
- package/packages/selector/src/runtime/deepseek-official-pricing.ts +298 -298
- package/packages/selector/src/runtime/deepseek-v4-vision-tokens.ts +254 -254
- package/packages/selector/src/runtime/measurement.ts +436 -436
- package/packages/selector/src/runtime/monitor.ts +235 -0
- package/packages/selector/src/runtime/reducers.ts +1674 -1674
- package/packages/selector/src/runtime/retrieve.ts +479 -457
- package/packages/selector/src/runtime/savings.ts +337 -0
- package/packages/selector/src/runtime/session-events.ts +30 -30
- package/packages/selector/src/runtime/summary-command.ts +127 -0
- package/packages/selector/src/runtime/tail-trim.ts +166 -166
- package/packages/selector/src/runtime/token-count.ts +72 -72
- package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -188
- package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -149
- package/packages/selector/src/runtime/tokenpilot/advisor.ts +419 -419
- package/packages/selector/src/runtime/tokenpilot/benefit.ts +200 -200
- package/packages/selector/src/runtime/tokenpilot/dedup.ts +81 -81
- package/packages/selector/src/runtime/tokenpilot/estimator.ts +151 -151
- package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -0
- package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -0
- package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -0
- package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -0
- package/packages/selector/src/runtime/tokenpilot/locator.ts +128 -128
- package/packages/selector/src/runtime/tokenpilot/read-state.ts +98 -98
- package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +303 -303
- package/packages/selector/src/runtime/toolclass.ts +103 -103
- package/packages/selector/src/runtime/types.ts +365 -346
- package/packages/selector/src/runtime/value.ts +48 -48
- package/packages/selector/tests/advisor-report.host.spec.ts +223 -223
- package/packages/selector/tests/auto-compact.client.spec.tsx +214 -213
- package/packages/selector/tests/built/client-artifact.spec.ts +59 -59
- package/packages/selector/tests/cache-prefix-audit.spec.ts +123 -123
- package/packages/selector/tests/code-skeleton.client.spec.ts +91 -90
- package/packages/selector/tests/custom-contract.client.spec.ts +205 -204
- package/packages/selector/tests/estimator-catalog.spec.ts +70 -70
- package/packages/selector/tests/estimator-channel.client.spec.tsx +253 -252
- package/packages/selector/tests/estimator-route-registration.host.spec.ts +194 -188
- package/packages/selector/tests/host-preset-overlay.host.spec.ts +206 -206
- package/packages/selector/tests/monitor-fab.client.spec.ts +138 -0
- package/packages/selector/tests/preset-options-write.client.spec.ts +207 -206
- package/packages/selector/tests/preset-overlay-loader.e2e.host.spec.ts +201 -201
- package/packages/selector/tests/preset-overlay.host.spec.ts +246 -246
- package/packages/selector/tests/profiles.client.spec.tsx +436 -434
- package/packages/selector/tests/public/package-contract.client.spec.ts +78 -78
- package/packages/selector/tests/runtime/adaptive-cost.spec.ts +167 -167
- package/packages/selector/tests/runtime/advice-never-withholds.host.spec.ts +232 -232
- package/packages/selector/tests/runtime/advisor-invariant.spec.ts +272 -272
- package/packages/selector/tests/runtime/advisor.spec.ts +226 -226
- package/packages/selector/tests/runtime/audit.spec.ts +275 -274
- package/packages/selector/tests/runtime/auto-compact-config.spec.ts +523 -523
- package/packages/selector/tests/runtime/char-basis.spec.ts +30 -30
- package/packages/selector/tests/runtime/code-skeleton.spec.ts +152 -152
- package/packages/selector/tests/runtime/deepseek-official-pricing.spec.ts +186 -186
- package/packages/selector/tests/runtime/deepseek-v4-tokenizer.spec.ts +122 -122
- package/packages/selector/tests/runtime/deepseek-v4-vision-tokens.spec.ts +122 -122
- package/packages/selector/tests/runtime/deprecated-preset-options.spec.ts +96 -96
- package/packages/selector/tests/runtime/fixtures/profile-baseline.json +273 -273
- package/packages/selector/tests/runtime/fixtures/tokenizer-golden.json +106 -106
- package/packages/selector/tests/runtime/fixtures/vision-golden.json +459 -459
- package/packages/selector/tests/runtime/frequency-longstrings.spec.ts +74 -74
- package/packages/selector/tests/runtime/html-reducer.spec.ts +212 -212
- package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -0
- package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -0
- package/packages/selector/tests/runtime/line-mapping.spec.ts +153 -153
- package/packages/selector/tests/runtime/monitor.spec.ts +301 -0
- package/packages/selector/tests/runtime/prose-reducers.spec.ts +133 -133
- package/packages/selector/tests/runtime/public/public-runtime.spec.ts +2724 -2724
- package/packages/selector/tests/runtime/read-input-cap.spec.ts +33 -33
- package/packages/selector/tests/runtime/savings.spec.ts +120 -0
- package/packages/selector/tests/runtime/search-reducer.spec.ts +110 -110
- package/packages/selector/tests/runtime/session-events.spec.ts +27 -27
- package/packages/selector/tests/runtime/sidechannel.spec.ts +241 -241
- package/packages/selector/tests/runtime/summary-command.spec.ts +154 -0
- package/packages/selector/tests/runtime/toc-and-bundled.spec.ts +159 -159
- package/packages/selector/tests/runtime/tokenizer-golden.spec.ts +53 -53
- package/packages/selector/tests/runtime/tokenpilot/benefit.spec.ts +217 -217
- package/packages/selector/tests/runtime/tokenpilot/dedup.spec.ts +52 -52
- package/packages/selector/tests/runtime/tokenpilot/estimator.spec.ts +79 -79
- package/packages/selector/tests/runtime/tokenpilot/locator.spec.ts +76 -76
- package/packages/selector/tests/runtime/tokenpilot/profile-baseline.spec.ts +116 -116
- package/packages/selector/tests/runtime/tokenpilot/read-state.spec.ts +82 -82
- package/packages/selector/tests/runtime/toolclass.spec.ts +156 -156
- package/packages/selector/tests/runtime/value.spec.ts +23 -23
- package/packages/selector/tests/settings-seat.client.spec.ts +130 -118
- package/packages/selector/tests/standing-generation.host.spec.ts +697 -697
- package/packages/selector/tests/subagent-cache-reuse.host.spec.ts +253 -253
- package/packages/selector/tests/support/cache-prefix-audit.ts +105 -105
- package/packages/selector/tests/support/mock-adapter.ts +37 -37
- package/packages/selector/tests/support/ui-primitives.tsx +34 -34
- package/packages/selector/tsconfig.json +11 -11
- package/packages/selector/tsdown.client.config.ts +102 -102
- package/packages/selector/tsdown.config.ts +20 -20
- package/pnpm-workspace.yaml +66 -66
- package/scripts/capture-profile-baseline.mjs +42 -42
- package/scripts/generate-tokenizer-fixtures.py +81 -81
- package/scripts/generate-vision-fixtures.py +208 -208
- package/scripts/packed-components-smoke.mjs +732 -732
- package/scripts/packed-install-e2e.mjs +1273 -1273
- package/scripts/toolclass-corpus-replay.mjs +281 -281
- package/scripts/verify-release.mjs +318 -317
- package/tests/TEST_INVENTORY.md +41 -41
- package/tsconfig.base.json +18 -18
- package/tsconfig.json +7 -7
- package/tsconfig.tests.json +15 -15
- package/vitest.built.config.ts +9 -9
- package/vitest.config.ts +43 -43
|
@@ -1,457 +1,479 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Current-session recovery tool for context-compression placeholders.
|
|
3
|
-
*
|
|
4
|
-
* It reads the immutable append-only event cited by a `session://.../event/N`
|
|
5
|
-
* reference. The tool never reads the replacement surface as the source of
|
|
6
|
-
* truth and never crosses into another Session.
|
|
7
|
-
*
|
|
8
|
-
* @module dsh-context-compression-improved-runtime/retrieve
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type { Context } from '@deepseek-ai/cordis'
|
|
12
|
-
import z from '@deepseek-ai/schemastery'
|
|
13
|
-
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
14
|
-
import type { ContentBlock } from '@deepseek-ai/dsh-llm'
|
|
15
|
-
import type {} from '@deepseek-ai/dsh-system-prompt'
|
|
16
|
-
import {
|
|
17
|
-
parseTailTrimRef,
|
|
18
|
-
validatePublishedTailTrim,
|
|
19
|
-
type PublishedTailTrim,
|
|
20
|
-
} from './tail-trim.ts'
|
|
21
|
-
import { sessionEvents } from './session-events.ts'
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
export const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
const
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
+ '
|
|
52
|
-
+ '
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* @param
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
ctx.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
args.
|
|
102
|
-
|
|
103
|
-
args.
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
]
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
function
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
for (const
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
yield
|
|
306
|
-
return
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
)
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Current-session recovery tool for context-compression placeholders.
|
|
3
|
+
*
|
|
4
|
+
* It reads the immutable append-only event cited by a `session://.../event/N`
|
|
5
|
+
* reference. The tool never reads the replacement surface as the source of
|
|
6
|
+
* truth and never crosses into another Session.
|
|
7
|
+
*
|
|
8
|
+
* @module dsh-context-compression-improved-runtime/retrieve
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
12
|
+
import z from '@deepseek-ai/schemastery'
|
|
13
|
+
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
14
|
+
import type { ContentBlock } from '@deepseek-ai/dsh-llm'
|
|
15
|
+
import type {} from '@deepseek-ai/dsh-system-prompt'
|
|
16
|
+
import {
|
|
17
|
+
parseTailTrimRef,
|
|
18
|
+
validatePublishedTailTrim,
|
|
19
|
+
type PublishedTailTrim,
|
|
20
|
+
} from './tail-trim.ts'
|
|
21
|
+
import { sessionEvents } from './session-events.ts'
|
|
22
|
+
import { getSavingsLedger } from './savings.ts'
|
|
23
|
+
|
|
24
|
+
export const name = 'context-compression-retrieve'
|
|
25
|
+
export const inject = ['tools', 'systemPrompt']
|
|
26
|
+
|
|
27
|
+
/** Maximum text returned by one recovery call. */
|
|
28
|
+
export interface Config {
|
|
29
|
+
/** Hard response bound in Unicode code points. Defaults to 50000. */
|
|
30
|
+
maxChars?: number
|
|
31
|
+
/** Maximum source text inspected by one call. Defaults to 250000. */
|
|
32
|
+
maxScanChars?: number
|
|
33
|
+
/** Maximum query length in Unicode code points. Defaults to 256. */
|
|
34
|
+
maxQueryChars?: number
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Loader schema for the recovery tool's bounded runtime configuration. */
|
|
38
|
+
export const Config: z<Config> = z.object({
|
|
39
|
+
maxChars: z.number().step(1).min(1).default(50_000),
|
|
40
|
+
maxScanChars: z.number().step(1).min(1).default(250_000),
|
|
41
|
+
maxQueryChars: z.number().step(1).min(1).default(256),
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const REF_PATTERN = /^session:\/\/([^/]+)\/event\/(\d+)$/
|
|
45
|
+
const MAX_LINES = 1_000
|
|
46
|
+
const DEFAULT_MAX_CHARS = 50_000
|
|
47
|
+
const DEFAULT_MAX_SCAN_CHARS = 250_000
|
|
48
|
+
const DEFAULT_MAX_QUERY_CHARS = 256
|
|
49
|
+
const TRUNCATION_MARKER = '\n[context_compression_retrieve output truncated; reported lines describe the selected source range]\n'
|
|
50
|
+
const PROMPT = 'When a compacted tool result contains a session://<session-id>/event/<seq> reference, '
|
|
51
|
+
+ 'or TailTrim contains a session://<session-id>/tailtrim/<seq> reference, use context_compression_retrieve with that exact ref '
|
|
52
|
+
+ 'and a narrow line range or query if the omitted evidence is necessary. '
|
|
53
|
+
+ 'The returned event content comes from the append-only session log, which is the source of truth.'
|
|
54
|
+
|
|
55
|
+
const OUTPUT = {
|
|
56
|
+
schema: { type: 'string' as const },
|
|
57
|
+
render: (_args: unknown, value: string) => [{ type: 'text' as const, text: value }],
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Register the current-session recovery tool and its stable guidance.
|
|
62
|
+
*
|
|
63
|
+
* @param ctx Plugin context providing the tool registry and system prompt.
|
|
64
|
+
* @param config Optional response, scan, and query bounds.
|
|
65
|
+
*/
|
|
66
|
+
export function installContextCompressionRetrieve(ctx: Context, config: Config = {}): void {
|
|
67
|
+
const maxChars = resolvePositiveInteger('maxChars', config.maxChars, DEFAULT_MAX_CHARS)
|
|
68
|
+
const maxScanChars = resolvePositiveInteger(
|
|
69
|
+
'maxScanChars', config.maxScanChars, DEFAULT_MAX_SCAN_CHARS,
|
|
70
|
+
)
|
|
71
|
+
const maxQueryChars = resolvePositiveInteger(
|
|
72
|
+
'maxQueryChars', config.maxQueryChars, DEFAULT_MAX_QUERY_CHARS,
|
|
73
|
+
)
|
|
74
|
+
ctx.systemPrompt.section({ name: 'tool:context-compression-retrieve', order: 114, text: PROMPT })
|
|
75
|
+
ctx.tools.register(defineTool({
|
|
76
|
+
name: 'context_compression_retrieve',
|
|
77
|
+
description: 'Recover exact content from one compacted tool result or TailTrim group using its current-session session:// reference.',
|
|
78
|
+
parameters: {
|
|
79
|
+
ref: { type: 'string', required: true, description: 'Exact session://<current-session-id>/event/<seq> or /tailtrim/<seq> reference from a placeholder.' },
|
|
80
|
+
query: { type: 'string', description: 'Optional case-insensitive text to search for inside the original result.' },
|
|
81
|
+
start_line: { type: 'integer', description: 'Optional 1-based first line for a direct slice. Defaults to 1.' },
|
|
82
|
+
max_lines: { type: 'integer', description: 'Maximum lines to return. Defaults to 200; maximum 1000.' },
|
|
83
|
+
},
|
|
84
|
+
output: OUTPUT,
|
|
85
|
+
isConcurrencySafe: () => true,
|
|
86
|
+
execute(args, exec) {
|
|
87
|
+
if (exec.agent === undefined) throw new Error('context_compression_retrieve requires an agent session')
|
|
88
|
+
const match = REF_PATTERN.exec(args.ref)
|
|
89
|
+
const tailTrimRef = parseTailTrimRef(args.ref)
|
|
90
|
+
if (match === null && tailTrimRef === null) {
|
|
91
|
+
throw new Error('context_compression_retrieve: ref must be session://<session-id>/(event|tailtrim)/<seq>')
|
|
92
|
+
}
|
|
93
|
+
const sessionId = match?.[1] ?? tailTrimRef?.sessionId
|
|
94
|
+
if (sessionId !== String(exec.agent.id)) {
|
|
95
|
+
throw new Error('context_compression_retrieve: a compression reference may only read the caller\'s current session')
|
|
96
|
+
}
|
|
97
|
+
if (tailTrimRef !== null) {
|
|
98
|
+
const sessionId = String(exec.agent.id)
|
|
99
|
+
return Promise.resolve(recoverTailTrim(
|
|
100
|
+
exec.agent.session,
|
|
101
|
+
args.ref,
|
|
102
|
+
tailTrimRef.manifestSeq,
|
|
103
|
+
args.query,
|
|
104
|
+
args.start_line,
|
|
105
|
+
args.max_lines,
|
|
106
|
+
{ maxChars, maxScanChars, maxQueryChars },
|
|
107
|
+
)).then((text: string) => {
|
|
108
|
+
// 节省统计:回读内容重新进入上下文 = 对该组原声称节省的抵消
|
|
109
|
+
// (chars/4 估算口径;retrieve 不持有精确 counter 身份,分列不混算)。
|
|
110
|
+
getSavingsLedger().recordOffset({
|
|
111
|
+
sessionId,
|
|
112
|
+
component: 'tailtrim-retrieve',
|
|
113
|
+
tokens: Math.ceil(codePointLength(text) / 4),
|
|
114
|
+
basis: 'characters',
|
|
115
|
+
note: `tailtrim/${tailTrimRef.manifestSeq} 回读`,
|
|
116
|
+
})
|
|
117
|
+
return text
|
|
118
|
+
})
|
|
119
|
+
}
|
|
120
|
+
const seq = Number(match?.[2])
|
|
121
|
+
const event = sessionEvents(exec.agent.session)[seq]
|
|
122
|
+
if (event?.type !== 'tool/result') {
|
|
123
|
+
throw new Error(`context_compression_retrieve: event ${String(seq)} is not a tool/result in the current session`)
|
|
124
|
+
}
|
|
125
|
+
const maxLines = resolveMaxLines(args.max_lines)
|
|
126
|
+
const scan = scanBlocks(event.data.message.content[0].content, maxScanChars)
|
|
127
|
+
const scannedLines = splitScannedLines(scan)
|
|
128
|
+
const lines = scannedLines.lines
|
|
129
|
+
const query = args.query
|
|
130
|
+
if (query !== undefined && exceedsCodePointLimit(query, maxQueryChars)) {
|
|
131
|
+
throw new Error(
|
|
132
|
+
`context_compression_retrieve: query must be at most ${String(maxQueryChars)} Unicode code points`,
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
const selected = query === undefined || query === ''
|
|
136
|
+
? directSlice(lines, args.start_line ?? 1, maxLines, scan.complete, scannedLines.partialTail)
|
|
137
|
+
: querySlice(lines, query, maxLines, scan.complete, scannedLines.partialTail)
|
|
138
|
+
const total = scan.complete ? String(lines.length) : `at least ${String(lines.length)}`
|
|
139
|
+
const header = [
|
|
140
|
+
`source: ${args.ref}`,
|
|
141
|
+
`tool_call_id: ${event.data.message.source.callId}`,
|
|
142
|
+
`status: ${event.data.message.content[0].isError === true ? 'error' : 'completed'}`,
|
|
143
|
+
`lines: ${String(selected.start)}-${String(selected.end)} of ${total}`,
|
|
144
|
+
scan.complete ? '' : 'note: source scan limit reached; later lines were not inspected',
|
|
145
|
+
selected.partialLine === undefined
|
|
146
|
+
? ''
|
|
147
|
+
: `note: line ${String(selected.partialLine)} is a partial prefix ending at the source scan limit`,
|
|
148
|
+
selected.omitted
|
|
149
|
+
? query === undefined || query === ''
|
|
150
|
+
? 'note: additional source lines were omitted'
|
|
151
|
+
: 'note: additional matching or neighboring lines were omitted'
|
|
152
|
+
: '',
|
|
153
|
+
'--- original tool result ---',
|
|
154
|
+
].filter(Boolean).join('\n')
|
|
155
|
+
const output = `${header}\n${selected.text}`
|
|
156
|
+
const bounded = boundCodePoints(output, maxChars)
|
|
157
|
+
// 节省统计:retrieve 产物重新进入上下文 = 抵消(chars/4 估算口径)。
|
|
158
|
+
getSavingsLedger().recordOffset({
|
|
159
|
+
sessionId: String(exec.agent.id),
|
|
160
|
+
component: 'retrieve',
|
|
161
|
+
tokens: Math.ceil(codePointLength(bounded) / 4),
|
|
162
|
+
basis: 'characters',
|
|
163
|
+
note: `event/${String(seq)} 回读`,
|
|
164
|
+
})
|
|
165
|
+
return Promise.resolve(bounded)
|
|
166
|
+
},
|
|
167
|
+
}))
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function recoverTailTrim(
|
|
171
|
+
session: Parameters<typeof validatePublishedTailTrim>[0],
|
|
172
|
+
ref: string,
|
|
173
|
+
manifestSeq: number,
|
|
174
|
+
query: string | undefined,
|
|
175
|
+
startLine: number | undefined,
|
|
176
|
+
requestedMaxLines: number | undefined,
|
|
177
|
+
bounds: { readonly maxChars: number; readonly maxScanChars: number; readonly maxQueryChars: number },
|
|
178
|
+
): string {
|
|
179
|
+
const published = validatePublishedTailTrim(session, manifestSeq)
|
|
180
|
+
if (published === null || published.ref !== ref) {
|
|
181
|
+
throw new Error('context_compression_retrieve: ref is not a valid published TailTrim group')
|
|
182
|
+
}
|
|
183
|
+
if (query !== undefined && exceedsCodePointLimit(query, bounds.maxQueryChars)) {
|
|
184
|
+
throw new Error(
|
|
185
|
+
`context_compression_retrieve: query must be at most ${String(bounds.maxQueryChars)} Unicode code points`,
|
|
186
|
+
)
|
|
187
|
+
}
|
|
188
|
+
const fixedHeader = [
|
|
189
|
+
`source: ${ref}`,
|
|
190
|
+
'kind: tailtrim-group',
|
|
191
|
+
`records: ${String(published.roots.length)}`,
|
|
192
|
+
'--- original tool group (jsonl) ---',
|
|
193
|
+
].join('\n')
|
|
194
|
+
const scanBudget = Math.max(0, bounds.maxScanChars - codePointLength(`${fixedHeader}\n`))
|
|
195
|
+
const scan = consumeChunks(renderGroupRecordChunks(published.roots), scanBudget)
|
|
196
|
+
const scannedLines = splitScannedLines(scan)
|
|
197
|
+
const maxLines = resolveMaxLines(requestedMaxLines)
|
|
198
|
+
const selected = query === undefined || query === ''
|
|
199
|
+
? directSlice(
|
|
200
|
+
scannedLines.lines,
|
|
201
|
+
startLine ?? 1,
|
|
202
|
+
maxLines,
|
|
203
|
+
scan.complete,
|
|
204
|
+
scannedLines.partialTail,
|
|
205
|
+
)
|
|
206
|
+
: querySlice(
|
|
207
|
+
scannedLines.lines,
|
|
208
|
+
query,
|
|
209
|
+
maxLines,
|
|
210
|
+
scan.complete,
|
|
211
|
+
scannedLines.partialTail,
|
|
212
|
+
)
|
|
213
|
+
const header = [
|
|
214
|
+
fixedHeader.split('\n').slice(0, 3).join('\n'),
|
|
215
|
+
scan.complete ? '' : 'note: source scan limit reached; later records were not inspected',
|
|
216
|
+
selected.omitted ? 'note: additional group records were omitted' : '',
|
|
217
|
+
'--- original tool group (jsonl) ---',
|
|
218
|
+
].filter(Boolean).join('\n')
|
|
219
|
+
return boundCodePoints(`${header}\n${selected.text}`, bounds.maxChars)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
interface ScanResult {
|
|
223
|
+
readonly text: string
|
|
224
|
+
readonly complete: boolean
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
interface ScannedLines {
|
|
228
|
+
readonly lines: readonly string[]
|
|
229
|
+
readonly partialTail: boolean
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
interface Selection {
|
|
233
|
+
readonly text: string
|
|
234
|
+
readonly start: number
|
|
235
|
+
readonly end: number
|
|
236
|
+
readonly omitted: boolean
|
|
237
|
+
readonly partialLine?: number
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function resolvePositiveInteger(name: string, value: number | undefined, fallback: number): number {
|
|
241
|
+
const resolved = value ?? fallback
|
|
242
|
+
if (!Number.isSafeInteger(resolved) || resolved <= 0) {
|
|
243
|
+
throw new TypeError(`tool-context-retrieve: ${name} must be a positive safe integer`)
|
|
244
|
+
}
|
|
245
|
+
return resolved
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function resolveMaxLines(value: number | undefined): number {
|
|
249
|
+
const resolved = value ?? 200
|
|
250
|
+
if (!Number.isSafeInteger(resolved) || resolved < 1 || resolved > MAX_LINES) {
|
|
251
|
+
throw new Error(`context_compression_retrieve: max_lines must be an integer from 1 to ${String(MAX_LINES)}`)
|
|
252
|
+
}
|
|
253
|
+
return resolved
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function scanBlocks(blocks: readonly ContentBlock[], maxChars: number): ScanResult {
|
|
257
|
+
return consumeChunks(renderBlockChunks(blocks), maxChars)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function* renderBlockChunks(blocks: readonly ContentBlock[]): Generator<string> {
|
|
261
|
+
let first = true
|
|
262
|
+
for (const block of blocks) {
|
|
263
|
+
if (!first) yield '\n'
|
|
264
|
+
first = false
|
|
265
|
+
if (block.type === 'text') yield block.text
|
|
266
|
+
else yield* jsonTokens(block)
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function* renderGroupRecordChunks(roots: PublishedTailTrim['roots']): Generator<string> {
|
|
271
|
+
let first = true
|
|
272
|
+
for (const root of roots) {
|
|
273
|
+
if (!first) yield '\n'
|
|
274
|
+
first = false
|
|
275
|
+
const message = root.data.message
|
|
276
|
+
yield* jsonTokens({
|
|
277
|
+
seq: root.seq,
|
|
278
|
+
type: root.type,
|
|
279
|
+
message: {
|
|
280
|
+
id: message.id,
|
|
281
|
+
role: message.role,
|
|
282
|
+
content: message.content,
|
|
283
|
+
source: message.source,
|
|
284
|
+
},
|
|
285
|
+
})
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function* jsonTokens(value: unknown): Generator<string> {
|
|
290
|
+
if (value === null) {
|
|
291
|
+
yield 'null'
|
|
292
|
+
return
|
|
293
|
+
}
|
|
294
|
+
switch (typeof value) {
|
|
295
|
+
case 'string':
|
|
296
|
+
yield '"'
|
|
297
|
+
for (const point of value) {
|
|
298
|
+
const quoted = jsonScalar(point)
|
|
299
|
+
yield quoted.slice(1, -1)
|
|
300
|
+
}
|
|
301
|
+
yield '"'
|
|
302
|
+
return
|
|
303
|
+
case 'number':
|
|
304
|
+
case 'boolean':
|
|
305
|
+
yield jsonScalar(value)
|
|
306
|
+
return
|
|
307
|
+
case 'object': {
|
|
308
|
+
if (Array.isArray(value)) {
|
|
309
|
+
yield '['
|
|
310
|
+
for (let index = 0; index < value.length; index++) {
|
|
311
|
+
if (index > 0) yield ','
|
|
312
|
+
yield* jsonTokens(value[index])
|
|
313
|
+
}
|
|
314
|
+
yield ']'
|
|
315
|
+
return
|
|
316
|
+
}
|
|
317
|
+
yield '{'
|
|
318
|
+
let first = true
|
|
319
|
+
for (const key in value) {
|
|
320
|
+
if (!Object.hasOwn(value, key)) continue
|
|
321
|
+
if (!first) yield ','
|
|
322
|
+
first = false
|
|
323
|
+
yield* jsonTokens(key)
|
|
324
|
+
yield ':'
|
|
325
|
+
yield* jsonTokens((value as Record<string, unknown>)[key])
|
|
326
|
+
}
|
|
327
|
+
yield '}'
|
|
328
|
+
return
|
|
329
|
+
}
|
|
330
|
+
default:
|
|
331
|
+
throw new TypeError('context_compression_retrieve: source content is not JSON-serializable')
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function jsonScalar(value: string | number | boolean): string {
|
|
336
|
+
return JSON.stringify(value)
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function consumeChunks(chunks: Iterable<string>, maxChars: number): ScanResult {
|
|
340
|
+
const output: string[] = []
|
|
341
|
+
let remaining = maxChars
|
|
342
|
+
for (const chunk of chunks) {
|
|
343
|
+
const prefix = codePointPrefix(chunk, remaining)
|
|
344
|
+
output.push(prefix.text)
|
|
345
|
+
remaining -= prefix.count
|
|
346
|
+
if (!prefix.complete) return { text: output.join(''), complete: false }
|
|
347
|
+
}
|
|
348
|
+
return { text: output.join(''), complete: true }
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function splitScannedLines(scan: ScanResult): ScannedLines {
|
|
352
|
+
const lines = scan.text.split('\n')
|
|
353
|
+
const partialTail = !scan.complete && !scan.text.endsWith('\n')
|
|
354
|
+
if (scan.text.endsWith('\n')) lines.pop()
|
|
355
|
+
return { lines, partialTail }
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function directSlice(
|
|
359
|
+
lines: readonly string[],
|
|
360
|
+
startLine: number,
|
|
361
|
+
maxLines: number,
|
|
362
|
+
scanComplete: boolean,
|
|
363
|
+
partialTail: boolean,
|
|
364
|
+
): Selection {
|
|
365
|
+
if (!Number.isSafeInteger(startLine) || startLine < 1) {
|
|
366
|
+
throw new Error('context_compression_retrieve: start_line must be a positive safe integer')
|
|
367
|
+
}
|
|
368
|
+
if (startLine > lines.length) {
|
|
369
|
+
const reason = scanComplete ? 'outside the source line range' : 'beyond the source scan limit'
|
|
370
|
+
throw new Error(`context_compression_retrieve: start_line ${String(startLine)} is ${reason}`)
|
|
371
|
+
}
|
|
372
|
+
const startIndex = startLine - 1
|
|
373
|
+
const selected = lines.slice(startIndex, startIndex + maxLines)
|
|
374
|
+
return {
|
|
375
|
+
text: selected.join('\n'),
|
|
376
|
+
start: startIndex + 1,
|
|
377
|
+
end: startIndex + selected.length,
|
|
378
|
+
omitted: startIndex > 0 || startIndex + selected.length < lines.length || !scanComplete,
|
|
379
|
+
...partialTail && startIndex + selected.length === lines.length
|
|
380
|
+
? { partialLine: lines.length }
|
|
381
|
+
: {},
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function querySlice(
|
|
386
|
+
lines: readonly string[],
|
|
387
|
+
query: string,
|
|
388
|
+
maxLines: number,
|
|
389
|
+
scanComplete: boolean,
|
|
390
|
+
partialTail: boolean,
|
|
391
|
+
): Selection {
|
|
392
|
+
const needle = query.toLowerCase()
|
|
393
|
+
const chosen = new Set<number>()
|
|
394
|
+
let matched = false
|
|
395
|
+
let omitted = !scanComplete
|
|
396
|
+
for (let index = 0; index < lines.length; index++) {
|
|
397
|
+
const line = lines[index]
|
|
398
|
+
if (line === undefined || !line.toLowerCase().includes(needle)) continue
|
|
399
|
+
matched = true
|
|
400
|
+
for (let row = Math.max(0, index - 2); row <= Math.min(lines.length - 1, index + 2); row++) {
|
|
401
|
+
if (chosen.has(row)) continue
|
|
402
|
+
if (chosen.size >= maxLines) {
|
|
403
|
+
omitted = true
|
|
404
|
+
continue
|
|
405
|
+
}
|
|
406
|
+
chosen.add(row)
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
if (!matched) {
|
|
410
|
+
return {
|
|
411
|
+
text: scanComplete ? '[no matches]' : '[no matches within source scan limit]',
|
|
412
|
+
start: 0,
|
|
413
|
+
end: 0,
|
|
414
|
+
omitted,
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
const ordered = [...chosen].sort((a, b) => a - b)
|
|
418
|
+
const rendered: string[] = []
|
|
419
|
+
let previous = -2
|
|
420
|
+
let start = 0
|
|
421
|
+
let end = 0
|
|
422
|
+
for (const index of ordered) {
|
|
423
|
+
const line = lines[index]
|
|
424
|
+
if (line === undefined) continue
|
|
425
|
+
if (index > previous + 1) rendered.push('...')
|
|
426
|
+
rendered.push(`${String(index + 1)}: ${line}`)
|
|
427
|
+
if (start === 0) start = index + 1
|
|
428
|
+
end = index + 1
|
|
429
|
+
previous = index
|
|
430
|
+
}
|
|
431
|
+
return {
|
|
432
|
+
text: rendered.join('\n'),
|
|
433
|
+
start,
|
|
434
|
+
end,
|
|
435
|
+
omitted,
|
|
436
|
+
...partialTail && ordered.includes(lines.length - 1)
|
|
437
|
+
? { partialLine: lines.length }
|
|
438
|
+
: {},
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function boundCodePoints(text: string, maxChars: number): string {
|
|
443
|
+
const bounded = codePointPrefix(text, maxChars)
|
|
444
|
+
if (bounded.complete) return text
|
|
445
|
+
const marker = codePointPrefix(TRUNCATION_MARKER, maxChars)
|
|
446
|
+
if (!marker.complete) return marker.text
|
|
447
|
+
const body = codePointPrefix(text, maxChars - marker.count)
|
|
448
|
+
return body.text + marker.text
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function codePointPrefix(text: string, maxChars: number): {
|
|
452
|
+
readonly text: string
|
|
453
|
+
readonly count: number
|
|
454
|
+
readonly complete: boolean
|
|
455
|
+
} {
|
|
456
|
+
const output: string[] = []
|
|
457
|
+
let count = 0
|
|
458
|
+
for (const point of text) {
|
|
459
|
+
if (count >= maxChars) return { text: output.join(''), count, complete: false }
|
|
460
|
+
output.push(point)
|
|
461
|
+
count++
|
|
462
|
+
}
|
|
463
|
+
return { text: output.join(''), count, complete: true }
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function exceedsCodePointLimit(text: string, limit: number): boolean {
|
|
467
|
+
let count = 0
|
|
468
|
+
for (const _point of text) {
|
|
469
|
+
count++
|
|
470
|
+
if (count > limit) return true
|
|
471
|
+
}
|
|
472
|
+
return false
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function codePointLength(text: string): number {
|
|
476
|
+
let count = 0
|
|
477
|
+
for (const _point of text) count++
|
|
478
|
+
return count
|
|
479
|
+
}
|