@logictan/dsh-verbatim-compaction 0.13.0 → 0.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +65 -19
- package/lib/types/index.d.ts +37 -1
- package/package.json +7 -2
package/lib/index.js
CHANGED
|
@@ -31,8 +31,40 @@
|
|
|
31
31
|
* @module @logictan/dsh-verbatim-compaction
|
|
32
32
|
*/
|
|
33
33
|
import { BasicCompactionEngine } from '@deepseek-ai/dsh-compaction-basic';
|
|
34
|
+
import { ReasoningEffortId } from '@deepseek-ai/dsh-llm';
|
|
35
|
+
import Schema from '@deepseek-ai/schemastery';
|
|
34
36
|
/** Cordis companion plugin name. Must equal this plugin's `cordis.patch.yml` row id. */
|
|
35
37
|
export const name = 'verbatim-compaction';
|
|
38
|
+
/** Three editable scalars; empty strings mean "follow the chat model". */
|
|
39
|
+
export const OVERRIDE_FIELDS = {
|
|
40
|
+
provider: Schema.string().default('').volatile(),
|
|
41
|
+
model: Schema.string().default('').volatile(),
|
|
42
|
+
reasoningEffort: Schema.string().default('').volatile(),
|
|
43
|
+
};
|
|
44
|
+
/** A provider/model override takes effect only as a complete pair. */
|
|
45
|
+
export function pairedRouteOf(config) {
|
|
46
|
+
const provider = config?.provider?.get().trim();
|
|
47
|
+
const model = config?.model?.get().trim();
|
|
48
|
+
return provider !== undefined && provider !== '' && model !== undefined && model !== ''
|
|
49
|
+
? { provider, model }
|
|
50
|
+
: undefined;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Apply the optional override; without a configured effort the cheapest usable
|
|
54
|
+
* effort still wins, because a route's own default is typically the priciest.
|
|
55
|
+
*/
|
|
56
|
+
export function applyPurposeOverride(options, config, cheapest) {
|
|
57
|
+
const route = pairedRouteOf(config);
|
|
58
|
+
const configured = config?.reasoningEffort?.get().trim();
|
|
59
|
+
const effort = configured !== undefined && configured !== '' ? configured : cheapest;
|
|
60
|
+
if (route === undefined && effort === undefined)
|
|
61
|
+
return options;
|
|
62
|
+
return {
|
|
63
|
+
...options,
|
|
64
|
+
...(route === undefined ? {} : { provider: route.provider, model: route.model }),
|
|
65
|
+
...(effort === undefined ? {} : { reasoningEffort: ReasoningEffortId(effort) }),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
36
68
|
/**
|
|
37
69
|
* Absolute ceiling for one digest, in the meter's fixed 4-chars-per-token
|
|
38
70
|
* heuristic.
|
|
@@ -1242,7 +1274,7 @@ export function buildExtractionMaterial(turn, prose) {
|
|
|
1242
1274
|
* @param signal - cancellation forwarded to the adapter.
|
|
1243
1275
|
* @returns the folded paragraph, or undefined when the call produced nothing.
|
|
1244
1276
|
*/
|
|
1245
|
-
async function condenseOnce(ctx, agent, material, signal) {
|
|
1277
|
+
async function condenseOnce(ctx, agent, material, signal, override) {
|
|
1246
1278
|
const llm = ctx.llm;
|
|
1247
1279
|
if (llm === undefined) {
|
|
1248
1280
|
ctx.logger?.warn('verbatim-compaction: no llm service, condensation skipped');
|
|
@@ -1261,16 +1293,15 @@ async function condenseOnce(ctx, agent, material, signal) {
|
|
|
1261
1293
|
const cheapest = cheapestEffort(info?.reasoning?.efforts);
|
|
1262
1294
|
const chunks = [];
|
|
1263
1295
|
try {
|
|
1264
|
-
const stream = llm.stream({
|
|
1296
|
+
const stream = llm.stream(applyPurposeOverride({
|
|
1265
1297
|
provider,
|
|
1266
1298
|
model,
|
|
1267
1299
|
messages: [{ role: 'user', content: [{ type: 'text', text: `${material}\n\n${CONDENSE_INSTRUCTION}` }] }],
|
|
1268
1300
|
maxTokens: CONDENSE_TOKENS,
|
|
1269
1301
|
sessionId: agent.session.id,
|
|
1270
1302
|
purpose: 'compaction',
|
|
1271
|
-
...(cheapest === undefined ? {} : { reasoningEffort: cheapest }),
|
|
1272
1303
|
...(signal === undefined ? {} : { signal }),
|
|
1273
|
-
});
|
|
1304
|
+
}, override, cheapest));
|
|
1274
1305
|
for await (const chunk of stream) {
|
|
1275
1306
|
const delta = chunk;
|
|
1276
1307
|
if (delta.type === 'text-delta' && typeof delta.text === 'string')
|
|
@@ -1290,11 +1321,11 @@ async function condenseOnce(ctx, agent, material, signal) {
|
|
|
1290
1321
|
}
|
|
1291
1322
|
return text;
|
|
1292
1323
|
}
|
|
1293
|
-
async function extractBatch(ctx, agent, batch, signal) {
|
|
1324
|
+
async function extractBatch(ctx, agent, batch, signal, override) {
|
|
1294
1325
|
const out = new Map();
|
|
1295
1326
|
if (batch.length === 0)
|
|
1296
1327
|
return out;
|
|
1297
|
-
const attempt = await extractOnce(ctx, agent, batch, signal);
|
|
1328
|
+
const attempt = await extractOnce(ctx, agent, batch, signal, override);
|
|
1298
1329
|
if (!attempt.truncated) {
|
|
1299
1330
|
if (attempt.blocks.size === 0) {
|
|
1300
1331
|
ctx.logger?.warn(`verbatim-compaction: extraction returned no usable JSON for ${batch.length} slot(s) `
|
|
@@ -1316,7 +1347,7 @@ async function extractBatch(ctx, agent, batch, signal) {
|
|
|
1316
1347
|
out.set(key, block);
|
|
1317
1348
|
const middle = Math.ceil(batch.length / 2);
|
|
1318
1349
|
for (const half of [batch.slice(0, middle), batch.slice(middle)]) {
|
|
1319
|
-
for (const [key, block] of await extractBatch(ctx, agent, half, signal))
|
|
1350
|
+
for (const [key, block] of await extractBatch(ctx, agent, half, signal, override))
|
|
1320
1351
|
out.set(key, block);
|
|
1321
1352
|
}
|
|
1322
1353
|
return out;
|
|
@@ -1376,7 +1407,7 @@ function cheapestEffort(efforts) {
|
|
|
1376
1407
|
* @param signal - cancellation forwarded to the adapter.
|
|
1377
1408
|
* @returns the parsed blocks and whether the model stopped at the token cap.
|
|
1378
1409
|
*/
|
|
1379
|
-
async function extractOnce(ctx, agent, batch, signal) {
|
|
1410
|
+
async function extractOnce(ctx, agent, batch, signal, override) {
|
|
1380
1411
|
const out = new Map();
|
|
1381
1412
|
const llm = ctx.llm;
|
|
1382
1413
|
if (llm === undefined)
|
|
@@ -1410,7 +1441,7 @@ async function extractOnce(ctx, agent, batch, signal) {
|
|
|
1410
1441
|
const chunks = [];
|
|
1411
1442
|
let truncated = false;
|
|
1412
1443
|
try {
|
|
1413
|
-
const stream = llm.stream({
|
|
1444
|
+
const stream = llm.stream(applyPurposeOverride({
|
|
1414
1445
|
provider,
|
|
1415
1446
|
model,
|
|
1416
1447
|
messages: [
|
|
@@ -1419,9 +1450,8 @@ async function extractOnce(ctx, agent, batch, signal) {
|
|
|
1419
1450
|
maxTokens: EXTRACT_TOKENS_PER_TURN * parts.length + EXTRACT_THINKING_RESERVE_TOKENS,
|
|
1420
1451
|
sessionId: agent.session.id,
|
|
1421
1452
|
purpose: 'compaction',
|
|
1422
|
-
...(cheapest === undefined ? {} : { reasoningEffort: cheapest }),
|
|
1423
1453
|
...(signal === undefined ? {} : { signal }),
|
|
1424
|
-
});
|
|
1454
|
+
}, override, cheapest));
|
|
1425
1455
|
for await (const chunk of stream) {
|
|
1426
1456
|
const delta = chunk;
|
|
1427
1457
|
if (delta.type === 'text-delta' && typeof delta.text === 'string')
|
|
@@ -1449,6 +1479,22 @@ async function extractOnce(ctx, agent, batch, signal) {
|
|
|
1449
1479
|
* subclass cannot diverge from the host's transaction semantics.
|
|
1450
1480
|
*/
|
|
1451
1481
|
export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
1482
|
+
/**
|
|
1483
|
+
* The Loader reads Config from the default-exported class, and the base
|
|
1484
|
+
* constructor rejects unknown keys, so the base schema is merged instead of
|
|
1485
|
+
* replaced. Only the three new fields are volatile: existing keys stay
|
|
1486
|
+
* ordinary patch-level configuration.
|
|
1487
|
+
*/
|
|
1488
|
+
static Config = Schema.object({
|
|
1489
|
+
...BasicCompactionEngine.Config.dict,
|
|
1490
|
+
...OVERRIDE_FIELDS,
|
|
1491
|
+
});
|
|
1492
|
+
purposeConfig;
|
|
1493
|
+
constructor(ctx, config) {
|
|
1494
|
+
const { provider, model, reasoningEffort, ...base } = config ?? {};
|
|
1495
|
+
super(ctx, base);
|
|
1496
|
+
this.purposeConfig = { provider, model, reasoningEffort };
|
|
1497
|
+
}
|
|
1452
1498
|
/**
|
|
1453
1499
|
* Extraction results, keyed by a hash of the material they were derived from.
|
|
1454
1500
|
*
|
|
@@ -1504,12 +1550,12 @@ export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
|
1504
1550
|
// size independent of history length; the guard bound only exists for the
|
|
1505
1551
|
// degenerate case of a span smaller than the ceiling.
|
|
1506
1552
|
const budget = Math.max(0, Math.min(DIGEST_TOKEN_CEILING, shadowedPrice - FRAME_TOKENS - SHRINK_MARGIN_TOKENS));
|
|
1507
|
-
const extracted = await this.extractAll(source, agent, signal);
|
|
1553
|
+
const extracted = await this.extractAll(source, agent, signal, this.purposeConfig);
|
|
1508
1554
|
// Fold the oldest turns up only when the plain digest overflows. The fold is an
|
|
1509
1555
|
// LLM call, so a digest that already fits must not pay for one.
|
|
1510
1556
|
let condensed;
|
|
1511
1557
|
if (renderDigest(source, budget, extracted).length > budget * 4) {
|
|
1512
|
-
condensed = await this.condenseOldest(source, extracted, agent, budget, signal);
|
|
1558
|
+
condensed = await this.condenseOldest(source, extracted, agent, budget, signal, this.purposeConfig);
|
|
1513
1559
|
}
|
|
1514
1560
|
// The fold path returns its digest whole, so it can still overflow when every
|
|
1515
1561
|
// group has been folded and the user lines alone are too long. The budget is a
|
|
@@ -1553,7 +1599,7 @@ export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
|
1553
1599
|
* @param signal - cancellation forwarded to every call.
|
|
1554
1600
|
* @returns condensed blocks keyed by the `turnKey` of the first covered section.
|
|
1555
1601
|
*/
|
|
1556
|
-
async condenseOldest(source, extracted, agent, budget, signal) {
|
|
1602
|
+
async condenseOldest(source, extracted, agent, budget, signal, override) {
|
|
1557
1603
|
const sections = withExtractions(buildSections(source), extracted);
|
|
1558
1604
|
const budgetChars = budget * 4;
|
|
1559
1605
|
const out = new Map();
|
|
@@ -1570,7 +1616,7 @@ export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
|
1570
1616
|
const group = sections.slice(start, start + CONDENSE_GROUP);
|
|
1571
1617
|
if (group.length === 0)
|
|
1572
1618
|
continue;
|
|
1573
|
-
const text = await this.condenseGroup(group, agent, signal);
|
|
1619
|
+
const text = await this.condenseGroup(group, agent, signal, override);
|
|
1574
1620
|
// A failed group is skipped rather than retried: the next group still buys
|
|
1575
1621
|
// room, and retrying the same material against a failing model only spends
|
|
1576
1622
|
// calls. The remaining turns keep their own blocks either way.
|
|
@@ -1590,7 +1636,7 @@ export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
|
1590
1636
|
* @param signal - cancellation forwarded to the call.
|
|
1591
1637
|
* @returns the folded paragraph, or undefined when the call produced nothing.
|
|
1592
1638
|
*/
|
|
1593
|
-
async condenseGroup(group, agent, signal) {
|
|
1639
|
+
async condenseGroup(group, agent, signal, override) {
|
|
1594
1640
|
// Both sources, because a turn has one or the other: extraction succeeds and
|
|
1595
1641
|
// the block replaces the prose, or it fails and the verbatim prose is what the
|
|
1596
1642
|
// digest shows. Reading only `extracted` meant a run of turns whose extraction
|
|
@@ -1621,7 +1667,7 @@ export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
|
1621
1667
|
const cached = this.condensedCache.get(key);
|
|
1622
1668
|
if (cached !== undefined)
|
|
1623
1669
|
return cached;
|
|
1624
|
-
const text = await condenseOnce(this.ctx, agent, material, signal);
|
|
1670
|
+
const text = await condenseOnce(this.ctx, agent, material, signal, override);
|
|
1625
1671
|
if (text === undefined)
|
|
1626
1672
|
return undefined;
|
|
1627
1673
|
this.condensedCache.set(key, text);
|
|
@@ -1648,7 +1694,7 @@ export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
|
1648
1694
|
* @param signal - cancellation forwarded to every call.
|
|
1649
1695
|
* @returns turn number to extracted block, omitting turns that yielded none.
|
|
1650
1696
|
*/
|
|
1651
|
-
async extractAll(source, agent, signal) {
|
|
1697
|
+
async extractAll(source, agent, signal, override) {
|
|
1652
1698
|
// Walk the same sections the renderer will use, so a key addresses the exact
|
|
1653
1699
|
// slot the block will be written into. Grouping by turn number instead lost
|
|
1654
1700
|
// blocks whenever the log reused a number.
|
|
@@ -1712,7 +1758,7 @@ export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
|
1712
1758
|
if (signal?.aborted === true)
|
|
1713
1759
|
break;
|
|
1714
1760
|
const batch = missing.slice(index, index + TURNS_PER_CALL);
|
|
1715
|
-
const blocks = await extractBatch(this.ctx, agent, batch, signal);
|
|
1761
|
+
const blocks = await extractBatch(this.ctx, agent, batch, signal, override);
|
|
1716
1762
|
for (const slot of batch) {
|
|
1717
1763
|
const block = blocks.get(slot.key);
|
|
1718
1764
|
if (block === undefined)
|
package/lib/types/index.d.ts
CHANGED
|
@@ -32,10 +32,37 @@
|
|
|
32
32
|
*/
|
|
33
33
|
import { BasicCompactionEngine } from '@deepseek-ai/dsh-compaction-basic';
|
|
34
34
|
import type { Agent } from '@deepseek-ai/dsh-agent';
|
|
35
|
-
import type
|
|
35
|
+
import { type ContentBlock, type Message } from '@deepseek-ai/dsh-llm';
|
|
36
|
+
import Schema from '@deepseek-ai/schemastery';
|
|
36
37
|
import type { Session, SessionEvent, SessionSeq } from '@deepseek-ai/dsh-session';
|
|
37
38
|
/** Cordis companion plugin name. Must equal this plugin's `cordis.patch.yml` row id. */
|
|
38
39
|
export declare const name = "verbatim-compaction";
|
|
40
|
+
/** Live reference Cordis hands over for a volatile Config field. */
|
|
41
|
+
interface ConfigRef<T> {
|
|
42
|
+
get(): T;
|
|
43
|
+
}
|
|
44
|
+
/** Optional model/effort override for compaction's two LLM calls. */
|
|
45
|
+
export interface PurposeConfig {
|
|
46
|
+
provider?: ConfigRef<string>;
|
|
47
|
+
model?: ConfigRef<string>;
|
|
48
|
+
reasoningEffort?: ConfigRef<string>;
|
|
49
|
+
}
|
|
50
|
+
/** Three editable scalars; empty strings mean "follow the chat model". */
|
|
51
|
+
export declare const OVERRIDE_FIELDS: {
|
|
52
|
+
provider: Schema<string, string, "volatile-defined">;
|
|
53
|
+
model: Schema<string, string, "volatile-defined">;
|
|
54
|
+
reasoningEffort: Schema<string, string, "volatile-defined">;
|
|
55
|
+
};
|
|
56
|
+
/** A provider/model override takes effect only as a complete pair. */
|
|
57
|
+
export declare function pairedRouteOf(config: PurposeConfig | undefined): {
|
|
58
|
+
provider: string;
|
|
59
|
+
model: string;
|
|
60
|
+
} | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* Apply the optional override; without a configured effort the cheapest usable
|
|
63
|
+
* effort still wins, because a route's own default is typically the priciest.
|
|
64
|
+
*/
|
|
65
|
+
export declare function applyPurposeOverride<T extends object>(options: T, config: PurposeConfig | undefined, cheapest: string | undefined): T;
|
|
39
66
|
/**
|
|
40
67
|
* Absolute ceiling for one digest, in the meter's fixed 4-chars-per-token
|
|
41
68
|
* heuristic.
|
|
@@ -331,6 +358,15 @@ export declare function buildExtractionMaterial(turn: readonly SessionEvent[], p
|
|
|
331
358
|
* subclass cannot diverge from the host's transaction semantics.
|
|
332
359
|
*/
|
|
333
360
|
export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
361
|
+
/**
|
|
362
|
+
* The Loader reads Config from the default-exported class, and the base
|
|
363
|
+
* constructor rejects unknown keys, so the base schema is merged instead of
|
|
364
|
+
* replaced. Only the three new fields are volatile: existing keys stay
|
|
365
|
+
* ordinary patch-level configuration.
|
|
366
|
+
*/
|
|
367
|
+
static Config: typeof BasicCompactionEngine.Config;
|
|
368
|
+
private readonly purposeConfig;
|
|
369
|
+
constructor(ctx: ConstructorParameters<typeof BasicCompactionEngine>[0], config: any);
|
|
334
370
|
/**
|
|
335
371
|
* Extraction results, keyed by a hash of the material they were derived from.
|
|
336
372
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logictan/dsh-verbatim-compaction",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.1",
|
|
4
4
|
"description": "Deterministic compaction backend that keeps conversation prose verbatim in the checkpoint instead of re-summarizing it: user and assistant text survive word for word, tool results collapse to one-line pointers, reasoning is never read, and no model call is made.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -45,6 +45,9 @@
|
|
|
45
45
|
"url": "git+https://github.com/dale0525/dsh-plugins.git",
|
|
46
46
|
"directory": "packages/dsh-verbatim-compaction"
|
|
47
47
|
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@deepseek-ai/schemastery": "^3.18.5"
|
|
50
|
+
},
|
|
48
51
|
"peerDependencies": {
|
|
49
52
|
"@deepseek-ai/cordis": "^4.0.2",
|
|
50
53
|
"@deepseek-ai/dsh-agent": ">=0.1.6-alpha.1 <0.2.0 || ^0.2.1-rc.2",
|
|
@@ -52,7 +55,8 @@
|
|
|
52
55
|
"@deepseek-ai/dsh-compaction-basic": ">=0.1.6-alpha.1 <0.2.0 || ^0.2.1-rc.2",
|
|
53
56
|
"@deepseek-ai/dsh-llm": ">=0.1.6-alpha.1 <0.2.0 || ^0.2.1-rc.2",
|
|
54
57
|
"@deepseek-ai/dsh-session": ">=0.1.6-alpha.1 <0.2.0 || ^0.2.1-rc.2",
|
|
55
|
-
"@deepseek-ai/dsh-token-meter": ">=0.1.6-alpha.1 <0.2.0 || ^0.2.1-rc.2"
|
|
58
|
+
"@deepseek-ai/dsh-token-meter": ">=0.1.6-alpha.1 <0.2.0 || ^0.2.1-rc.2",
|
|
59
|
+
"@deepseek-ai/schemastery": "^3.18.0 || ^3.18.5-alpha.1"
|
|
56
60
|
},
|
|
57
61
|
"devDependencies": {
|
|
58
62
|
"@deepseek-ai/cordis": "^4.0.5",
|
|
@@ -62,6 +66,7 @@
|
|
|
62
66
|
"@deepseek-ai/dsh-llm": "0.2.1-rc.2",
|
|
63
67
|
"@deepseek-ai/dsh-session": "0.2.1-rc.2",
|
|
64
68
|
"@deepseek-ai/dsh-token-meter": "0.2.1-rc.2",
|
|
69
|
+
"@deepseek-ai/schemastery": "^3.18.5",
|
|
65
70
|
"typescript": "^5.9.0",
|
|
66
71
|
"@types/node": "^22.20.0"
|
|
67
72
|
}
|