@logictan/dsh-verbatim-compaction 0.13.0 → 0.13.2
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/cordis.patch.yml +2 -0
- package/lib/index.js +43 -19
- package/lib/purpose.js +25 -0
- package/lib/types/index.d.ts +12 -1
- package/lib/types/purpose.d.ts +27 -0
- package/package.json +12 -3
package/cordis.patch.yml
CHANGED
package/lib/index.js
CHANGED
|
@@ -31,8 +31,34 @@
|
|
|
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 { getLiveConfig } from './purpose.js';
|
|
34
36
|
/** Cordis companion plugin name. Must equal this plugin's `cordis.patch.yml` row id. */
|
|
35
37
|
export const name = 'verbatim-compaction';
|
|
38
|
+
/** A provider/model override takes effect only as a complete pair. */
|
|
39
|
+
export function pairedRouteOf(config) {
|
|
40
|
+
const provider = config?.provider?.get().trim();
|
|
41
|
+
const model = config?.model?.get().trim();
|
|
42
|
+
return provider !== undefined && provider !== '' && model !== undefined && model !== ''
|
|
43
|
+
? { provider, model }
|
|
44
|
+
: undefined;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Apply the optional override; without a configured effort the cheapest usable
|
|
48
|
+
* effort still wins, because a route's own default is typically the priciest.
|
|
49
|
+
*/
|
|
50
|
+
export function applyPurposeOverride(options, config, cheapest) {
|
|
51
|
+
const route = pairedRouteOf(config);
|
|
52
|
+
const configured = config?.reasoningEffort?.get().trim();
|
|
53
|
+
const effort = configured !== undefined && configured !== '' ? configured : cheapest;
|
|
54
|
+
if (route === undefined && effort === undefined)
|
|
55
|
+
return options;
|
|
56
|
+
return {
|
|
57
|
+
...options,
|
|
58
|
+
...(route === undefined ? {} : { provider: route.provider, model: route.model }),
|
|
59
|
+
...(effort === undefined ? {} : { reasoningEffort: ReasoningEffortId(effort) }),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
36
62
|
/**
|
|
37
63
|
* Absolute ceiling for one digest, in the meter's fixed 4-chars-per-token
|
|
38
64
|
* heuristic.
|
|
@@ -1242,7 +1268,7 @@ export function buildExtractionMaterial(turn, prose) {
|
|
|
1242
1268
|
* @param signal - cancellation forwarded to the adapter.
|
|
1243
1269
|
* @returns the folded paragraph, or undefined when the call produced nothing.
|
|
1244
1270
|
*/
|
|
1245
|
-
async function condenseOnce(ctx, agent, material, signal) {
|
|
1271
|
+
async function condenseOnce(ctx, agent, material, signal, override) {
|
|
1246
1272
|
const llm = ctx.llm;
|
|
1247
1273
|
if (llm === undefined) {
|
|
1248
1274
|
ctx.logger?.warn('verbatim-compaction: no llm service, condensation skipped');
|
|
@@ -1261,16 +1287,15 @@ async function condenseOnce(ctx, agent, material, signal) {
|
|
|
1261
1287
|
const cheapest = cheapestEffort(info?.reasoning?.efforts);
|
|
1262
1288
|
const chunks = [];
|
|
1263
1289
|
try {
|
|
1264
|
-
const stream = llm.stream({
|
|
1290
|
+
const stream = llm.stream(applyPurposeOverride({
|
|
1265
1291
|
provider,
|
|
1266
1292
|
model,
|
|
1267
1293
|
messages: [{ role: 'user', content: [{ type: 'text', text: `${material}\n\n${CONDENSE_INSTRUCTION}` }] }],
|
|
1268
1294
|
maxTokens: CONDENSE_TOKENS,
|
|
1269
1295
|
sessionId: agent.session.id,
|
|
1270
1296
|
purpose: 'compaction',
|
|
1271
|
-
...(cheapest === undefined ? {} : { reasoningEffort: cheapest }),
|
|
1272
1297
|
...(signal === undefined ? {} : { signal }),
|
|
1273
|
-
});
|
|
1298
|
+
}, override, cheapest));
|
|
1274
1299
|
for await (const chunk of stream) {
|
|
1275
1300
|
const delta = chunk;
|
|
1276
1301
|
if (delta.type === 'text-delta' && typeof delta.text === 'string')
|
|
@@ -1290,11 +1315,11 @@ async function condenseOnce(ctx, agent, material, signal) {
|
|
|
1290
1315
|
}
|
|
1291
1316
|
return text;
|
|
1292
1317
|
}
|
|
1293
|
-
async function extractBatch(ctx, agent, batch, signal) {
|
|
1318
|
+
async function extractBatch(ctx, agent, batch, signal, override) {
|
|
1294
1319
|
const out = new Map();
|
|
1295
1320
|
if (batch.length === 0)
|
|
1296
1321
|
return out;
|
|
1297
|
-
const attempt = await extractOnce(ctx, agent, batch, signal);
|
|
1322
|
+
const attempt = await extractOnce(ctx, agent, batch, signal, override);
|
|
1298
1323
|
if (!attempt.truncated) {
|
|
1299
1324
|
if (attempt.blocks.size === 0) {
|
|
1300
1325
|
ctx.logger?.warn(`verbatim-compaction: extraction returned no usable JSON for ${batch.length} slot(s) `
|
|
@@ -1316,7 +1341,7 @@ async function extractBatch(ctx, agent, batch, signal) {
|
|
|
1316
1341
|
out.set(key, block);
|
|
1317
1342
|
const middle = Math.ceil(batch.length / 2);
|
|
1318
1343
|
for (const half of [batch.slice(0, middle), batch.slice(middle)]) {
|
|
1319
|
-
for (const [key, block] of await extractBatch(ctx, agent, half, signal))
|
|
1344
|
+
for (const [key, block] of await extractBatch(ctx, agent, half, signal, override))
|
|
1320
1345
|
out.set(key, block);
|
|
1321
1346
|
}
|
|
1322
1347
|
return out;
|
|
@@ -1376,7 +1401,7 @@ function cheapestEffort(efforts) {
|
|
|
1376
1401
|
* @param signal - cancellation forwarded to the adapter.
|
|
1377
1402
|
* @returns the parsed blocks and whether the model stopped at the token cap.
|
|
1378
1403
|
*/
|
|
1379
|
-
async function extractOnce(ctx, agent, batch, signal) {
|
|
1404
|
+
async function extractOnce(ctx, agent, batch, signal, override) {
|
|
1380
1405
|
const out = new Map();
|
|
1381
1406
|
const llm = ctx.llm;
|
|
1382
1407
|
if (llm === undefined)
|
|
@@ -1410,7 +1435,7 @@ async function extractOnce(ctx, agent, batch, signal) {
|
|
|
1410
1435
|
const chunks = [];
|
|
1411
1436
|
let truncated = false;
|
|
1412
1437
|
try {
|
|
1413
|
-
const stream = llm.stream({
|
|
1438
|
+
const stream = llm.stream(applyPurposeOverride({
|
|
1414
1439
|
provider,
|
|
1415
1440
|
model,
|
|
1416
1441
|
messages: [
|
|
@@ -1419,9 +1444,8 @@ async function extractOnce(ctx, agent, batch, signal) {
|
|
|
1419
1444
|
maxTokens: EXTRACT_TOKENS_PER_TURN * parts.length + EXTRACT_THINKING_RESERVE_TOKENS,
|
|
1420
1445
|
sessionId: agent.session.id,
|
|
1421
1446
|
purpose: 'compaction',
|
|
1422
|
-
...(cheapest === undefined ? {} : { reasoningEffort: cheapest }),
|
|
1423
1447
|
...(signal === undefined ? {} : { signal }),
|
|
1424
|
-
});
|
|
1448
|
+
}, override, cheapest));
|
|
1425
1449
|
for await (const chunk of stream) {
|
|
1426
1450
|
const delta = chunk;
|
|
1427
1451
|
if (delta.type === 'text-delta' && typeof delta.text === 'string')
|
|
@@ -1504,12 +1528,12 @@ export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
|
1504
1528
|
// size independent of history length; the guard bound only exists for the
|
|
1505
1529
|
// degenerate case of a span smaller than the ceiling.
|
|
1506
1530
|
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);
|
|
1531
|
+
const extracted = await this.extractAll(source, agent, signal, getLiveConfig());
|
|
1508
1532
|
// Fold the oldest turns up only when the plain digest overflows. The fold is an
|
|
1509
1533
|
// LLM call, so a digest that already fits must not pay for one.
|
|
1510
1534
|
let condensed;
|
|
1511
1535
|
if (renderDigest(source, budget, extracted).length > budget * 4) {
|
|
1512
|
-
condensed = await this.condenseOldest(source, extracted, agent, budget, signal);
|
|
1536
|
+
condensed = await this.condenseOldest(source, extracted, agent, budget, signal, getLiveConfig());
|
|
1513
1537
|
}
|
|
1514
1538
|
// The fold path returns its digest whole, so it can still overflow when every
|
|
1515
1539
|
// group has been folded and the user lines alone are too long. The budget is a
|
|
@@ -1553,7 +1577,7 @@ export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
|
1553
1577
|
* @param signal - cancellation forwarded to every call.
|
|
1554
1578
|
* @returns condensed blocks keyed by the `turnKey` of the first covered section.
|
|
1555
1579
|
*/
|
|
1556
|
-
async condenseOldest(source, extracted, agent, budget, signal) {
|
|
1580
|
+
async condenseOldest(source, extracted, agent, budget, signal, override) {
|
|
1557
1581
|
const sections = withExtractions(buildSections(source), extracted);
|
|
1558
1582
|
const budgetChars = budget * 4;
|
|
1559
1583
|
const out = new Map();
|
|
@@ -1570,7 +1594,7 @@ export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
|
1570
1594
|
const group = sections.slice(start, start + CONDENSE_GROUP);
|
|
1571
1595
|
if (group.length === 0)
|
|
1572
1596
|
continue;
|
|
1573
|
-
const text = await this.condenseGroup(group, agent, signal);
|
|
1597
|
+
const text = await this.condenseGroup(group, agent, signal, override);
|
|
1574
1598
|
// A failed group is skipped rather than retried: the next group still buys
|
|
1575
1599
|
// room, and retrying the same material against a failing model only spends
|
|
1576
1600
|
// calls. The remaining turns keep their own blocks either way.
|
|
@@ -1590,7 +1614,7 @@ export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
|
1590
1614
|
* @param signal - cancellation forwarded to the call.
|
|
1591
1615
|
* @returns the folded paragraph, or undefined when the call produced nothing.
|
|
1592
1616
|
*/
|
|
1593
|
-
async condenseGroup(group, agent, signal) {
|
|
1617
|
+
async condenseGroup(group, agent, signal, override) {
|
|
1594
1618
|
// Both sources, because a turn has one or the other: extraction succeeds and
|
|
1595
1619
|
// the block replaces the prose, or it fails and the verbatim prose is what the
|
|
1596
1620
|
// digest shows. Reading only `extracted` meant a run of turns whose extraction
|
|
@@ -1621,7 +1645,7 @@ export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
|
1621
1645
|
const cached = this.condensedCache.get(key);
|
|
1622
1646
|
if (cached !== undefined)
|
|
1623
1647
|
return cached;
|
|
1624
|
-
const text = await condenseOnce(this.ctx, agent, material, signal);
|
|
1648
|
+
const text = await condenseOnce(this.ctx, agent, material, signal, override);
|
|
1625
1649
|
if (text === undefined)
|
|
1626
1650
|
return undefined;
|
|
1627
1651
|
this.condensedCache.set(key, text);
|
|
@@ -1648,7 +1672,7 @@ export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
|
1648
1672
|
* @param signal - cancellation forwarded to every call.
|
|
1649
1673
|
* @returns turn number to extracted block, omitting turns that yielded none.
|
|
1650
1674
|
*/
|
|
1651
|
-
async extractAll(source, agent, signal) {
|
|
1675
|
+
async extractAll(source, agent, signal, override) {
|
|
1652
1676
|
// Walk the same sections the renderer will use, so a key addresses the exact
|
|
1653
1677
|
// slot the block will be written into. Grouping by turn number instead lost
|
|
1654
1678
|
// blocks whenever the log reused a number.
|
|
@@ -1712,7 +1736,7 @@ export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
|
1712
1736
|
if (signal?.aborted === true)
|
|
1713
1737
|
break;
|
|
1714
1738
|
const batch = missing.slice(index, index + TURNS_PER_CALL);
|
|
1715
|
-
const blocks = await extractBatch(this.ctx, agent, batch, signal);
|
|
1739
|
+
const blocks = await extractBatch(this.ctx, agent, batch, signal, override);
|
|
1716
1740
|
for (const slot of batch) {
|
|
1717
1741
|
const block = blocks.get(slot.key);
|
|
1718
1742
|
if (block === undefined)
|
package/lib/purpose.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import Schema from '@deepseek-ai/schemastery';
|
|
2
|
+
/** Settings namespace for the optional model override used by compaction. */
|
|
3
|
+
export const name = 'verbatim-compaction-purpose';
|
|
4
|
+
/** The three editable scalars; empty strings mean "follow the chat model". */
|
|
5
|
+
export const Config = Schema.object({
|
|
6
|
+
provider: Schema.string().default('').volatile(),
|
|
7
|
+
model: Schema.string().default('').volatile(),
|
|
8
|
+
reasoningEffort: Schema.string().default('').volatile(),
|
|
9
|
+
});
|
|
10
|
+
/**
|
|
11
|
+
* The compaction engine runs inside isolated preset groups, while this
|
|
12
|
+
* settings row lives in the root realm. A module-level live reference is the
|
|
13
|
+
* one channel both halves can see without registering a second service.
|
|
14
|
+
*/
|
|
15
|
+
let liveConfig;
|
|
16
|
+
export function setLiveConfig(config) {
|
|
17
|
+
liveConfig = config;
|
|
18
|
+
}
|
|
19
|
+
export function getLiveConfig() {
|
|
20
|
+
return liveConfig;
|
|
21
|
+
}
|
|
22
|
+
/** Root-realm settings row. It registers no service. */
|
|
23
|
+
export function apply(_ctx, config) {
|
|
24
|
+
setLiveConfig(config);
|
|
25
|
+
}
|
package/lib/types/index.d.ts
CHANGED
|
@@ -32,10 +32,21 @@
|
|
|
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
36
|
import type { Session, SessionEvent, SessionSeq } from '@deepseek-ai/dsh-session';
|
|
37
|
+
import { type PurposeConfig } from './purpose.js';
|
|
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
|
+
/** A provider/model override takes effect only as a complete pair. */
|
|
41
|
+
export declare function pairedRouteOf(config: PurposeConfig | undefined): {
|
|
42
|
+
provider: string;
|
|
43
|
+
model: string;
|
|
44
|
+
} | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Apply the optional override; without a configured effort the cheapest usable
|
|
47
|
+
* effort still wins, because a route's own default is typically the priciest.
|
|
48
|
+
*/
|
|
49
|
+
export declare function applyPurposeOverride<T extends object>(options: T, config: PurposeConfig | undefined, cheapest: string | undefined): T;
|
|
39
50
|
/**
|
|
40
51
|
* Absolute ceiling for one digest, in the meter's fixed 4-chars-per-token
|
|
41
52
|
* heuristic.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import Schema from '@deepseek-ai/schemastery';
|
|
2
|
+
/** Settings namespace for the optional model override used by compaction. */
|
|
3
|
+
export declare const name = "verbatim-compaction-purpose";
|
|
4
|
+
/** A live reference Cordis hands over for a volatile Config field. */
|
|
5
|
+
export interface ConfigRef<T> {
|
|
6
|
+
get(): T;
|
|
7
|
+
}
|
|
8
|
+
/** The three editable scalars; empty strings mean "follow the chat model". */
|
|
9
|
+
export declare const Config: Schema<Schemastery.ObjectS<NoInfer<{
|
|
10
|
+
provider: Schema<string, string, "volatile-defined">;
|
|
11
|
+
model: Schema<string, string, "volatile-defined">;
|
|
12
|
+
reasoningEffort: Schema<string, string, "volatile-defined">;
|
|
13
|
+
}>>, Schemastery.ObjectT<NoInfer<{
|
|
14
|
+
provider: Schema<string, string, "volatile-defined">;
|
|
15
|
+
model: Schema<string, string, "volatile-defined">;
|
|
16
|
+
reasoningEffort: Schema<string, string, "volatile-defined">;
|
|
17
|
+
}>>, "plain">;
|
|
18
|
+
/** The shape of the live configuration Cordis passes to the plugin. */
|
|
19
|
+
export interface PurposeConfig {
|
|
20
|
+
provider?: ConfigRef<string>;
|
|
21
|
+
model?: ConfigRef<string>;
|
|
22
|
+
reasoningEffort?: ConfigRef<string>;
|
|
23
|
+
}
|
|
24
|
+
export declare function setLiveConfig(config: PurposeConfig): void;
|
|
25
|
+
export declare function getLiveConfig(): PurposeConfig | undefined;
|
|
26
|
+
/** Root-realm settings row. It registers no service. */
|
|
27
|
+
export declare function apply(_ctx: unknown, config: PurposeConfig): void;
|
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.2",
|
|
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",
|
|
@@ -10,7 +10,11 @@
|
|
|
10
10
|
"types": "./lib/types/index.d.ts",
|
|
11
11
|
"default": "./lib/index.js"
|
|
12
12
|
},
|
|
13
|
-
"./package.json": "./package.json"
|
|
13
|
+
"./package.json": "./package.json",
|
|
14
|
+
"./purpose": {
|
|
15
|
+
"types": "./lib/types/purpose.d.ts",
|
|
16
|
+
"default": "./lib/purpose.js"
|
|
17
|
+
}
|
|
14
18
|
},
|
|
15
19
|
"dsh": {
|
|
16
20
|
"bundle": {
|
|
@@ -45,6 +49,9 @@
|
|
|
45
49
|
"url": "git+https://github.com/dale0525/dsh-plugins.git",
|
|
46
50
|
"directory": "packages/dsh-verbatim-compaction"
|
|
47
51
|
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@deepseek-ai/schemastery": "^3.18.5"
|
|
54
|
+
},
|
|
48
55
|
"peerDependencies": {
|
|
49
56
|
"@deepseek-ai/cordis": "^4.0.2",
|
|
50
57
|
"@deepseek-ai/dsh-agent": ">=0.1.6-alpha.1 <0.2.0 || ^0.2.1-rc.2",
|
|
@@ -52,7 +59,8 @@
|
|
|
52
59
|
"@deepseek-ai/dsh-compaction-basic": ">=0.1.6-alpha.1 <0.2.0 || ^0.2.1-rc.2",
|
|
53
60
|
"@deepseek-ai/dsh-llm": ">=0.1.6-alpha.1 <0.2.0 || ^0.2.1-rc.2",
|
|
54
61
|
"@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"
|
|
62
|
+
"@deepseek-ai/dsh-token-meter": ">=0.1.6-alpha.1 <0.2.0 || ^0.2.1-rc.2",
|
|
63
|
+
"@deepseek-ai/schemastery": "^3.18.0 || ^3.18.5-alpha.1"
|
|
56
64
|
},
|
|
57
65
|
"devDependencies": {
|
|
58
66
|
"@deepseek-ai/cordis": "^4.0.5",
|
|
@@ -62,6 +70,7 @@
|
|
|
62
70
|
"@deepseek-ai/dsh-llm": "0.2.1-rc.2",
|
|
63
71
|
"@deepseek-ai/dsh-session": "0.2.1-rc.2",
|
|
64
72
|
"@deepseek-ai/dsh-token-meter": "0.2.1-rc.2",
|
|
73
|
+
"@deepseek-ai/schemastery": "^3.18.5",
|
|
65
74
|
"typescript": "^5.9.0",
|
|
66
75
|
"@types/node": "^22.20.0"
|
|
67
76
|
}
|