dsh-context-compression-improved 0.1.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.
@@ -0,0 +1,136 @@
1
+ import { createUserMessage } from "@deepseek-ai/dsh-llm";
2
+ //#region src/runtime/session-events.ts
3
+ /**
4
+ * Read one immutable Session event snapshot.
5
+ *
6
+ * Harness rc.2 exposed `events`; Alpha 5 replaced it with the public
7
+ * `snapshotEvents()` method. Prefer the new API when present and retain the
8
+ * old accessor as the compatibility fallback for already-supported hosts.
9
+ */
10
+ function sessionEvents(session) {
11
+ const snapshot = session.snapshotEvents;
12
+ return typeof snapshot === "function" ? snapshot.call(session) : session.events;
13
+ }
14
+ //#endregion
15
+ //#region src/runtime/tail-trim.ts
16
+ /** Plugin-owned TailTrim publication protocol over official Session events. */
17
+ const TAIL_TRIM_REF_PATTERN = /^session:\/\/([^/]+)\/tailtrim\/(\d+)$/;
18
+ const MAX_ROOTS = 64;
19
+ const MAX_STUB_CODE_POINTS = 1024;
20
+ /** Build a same-session reference keyed by the standard prune event. */
21
+ function tailTrimRef(sessionId, manifestSeq) {
22
+ return `session://${sessionId}/tailtrim/${String(manifestSeq)}`;
23
+ }
24
+ /** Parse one exact TailTrim reference. */
25
+ function parseTailTrimRef(ref) {
26
+ const match = TAIL_TRIM_REF_PATTERN.exec(ref);
27
+ if (match === null) return null;
28
+ const manifestSeq = Number(match[2]);
29
+ if (!Number.isSafeInteger(manifestSeq) || manifestSeq < 0) return null;
30
+ return {
31
+ sessionId: match[1] ?? "",
32
+ manifestSeq
33
+ };
34
+ }
35
+ /** Build the fixed bounded model-visible TailTrim stub. */
36
+ function tailTrimStub(ref, toolNames, sourceEventSeqs) {
37
+ const stub = [
38
+ "[TailTrim: completed tool-call group]",
39
+ `ref: ${ref}`,
40
+ `tools: ${toolNames.join(", ")}`,
41
+ `source_event_seqs: ${sourceEventSeqs.join(", ")}`,
42
+ "use context_compression_retrieve with this TailTrim ref if needed"
43
+ ].join("\n");
44
+ return Array.from(stub).length <= MAX_STUB_CODE_POINTS ? stub : null;
45
+ }
46
+ /** Wrap a TailTrim stub in the one user message used for range replacement. */
47
+ function tailTrimMessage(stub) {
48
+ return createUserMessage({
49
+ content: [{
50
+ type: "text",
51
+ text: stub
52
+ }],
53
+ source: {
54
+ kind: "plugin",
55
+ plugin: "dsh-context-compression-improved-runtime"
56
+ }
57
+ });
58
+ }
59
+ /** Validate the standard prune, adjacent replacement, append roots and stub. */
60
+ function validatePublishedTailTrim(session, manifestSeq) {
61
+ const events = sessionEvents(session);
62
+ const manifest = events[manifestSeq];
63
+ if (manifest?.type !== "compaction/prune" || manifest.data.shadowedSeqs.length < 2 || manifest.data.shadowedSeqs.length > MAX_ROOTS || manifest.data.shadowedSeqs[0] !== manifest.data.shadowedRange.start || manifest.data.shadowedSeqs.at(-1) !== manifest.data.shadowedRange.end || new Set(manifest.data.shadowedSeqs).size !== manifest.data.shadowedSeqs.length) return null;
64
+ const replacement = events[manifestSeq + 1];
65
+ if (replacement?.type !== "user/message" || replacement.seq !== manifest.seq + 1 || replacement.data.source.kind !== "plugin" || replacement.data.source.plugin !== "dsh-context-compression-improved-runtime" || replacement.surfaceOp === void 0 || replacement.surfaceOp === "append" || replacement.surfaceOp.start !== manifest.data.shadowedRange.start || replacement.surfaceOp.end !== manifest.data.shadowedRange.end || !sameNumbers(replacement.sourceEventSeqs, [manifest.seq, ...manifest.data.shadowedSeqs]) || replacement.data.content.length !== 1 || replacement.data.content[0]?.type !== "text") return null;
66
+ const tracedRoots = manifest.data.shadowedSeqs.map((seq) => uniqueAppendRoot(session, seq, manifestSeq));
67
+ if (tracedRoots.some((root) => root === null)) return null;
68
+ const sourceEventSeqs = tracedRoots;
69
+ if (new Set(sourceEventSeqs).size !== sourceEventSeqs.length) return null;
70
+ const roots = sourceEventSeqs.map((seq) => events[seq]);
71
+ if (roots.some((event) => event === void 0)) return null;
72
+ const typedRoots = roots;
73
+ if (!validRootGroup(typedRoots, manifestSeq)) return null;
74
+ const assistant = typedRoots[0];
75
+ if (assistant?.type !== "assistant/message") return null;
76
+ const toolNames = assistant.data.message.content.map((block) => {
77
+ if (block.type !== "tool-call") throw new Error("unreachable");
78
+ return block.name;
79
+ });
80
+ const ref = tailTrimRef(String(session.id), manifestSeq);
81
+ const stub = tailTrimStub(ref, toolNames, sourceEventSeqs);
82
+ if (stub === null || replacement.data.content[0].text !== stub) return null;
83
+ return {
84
+ manifest,
85
+ replacement,
86
+ roots: typedRoots,
87
+ toolNames,
88
+ ref,
89
+ stub
90
+ };
91
+ }
92
+ function uniqueAppendRoot(session, seq, beforeSeq) {
93
+ const events = sessionEvents(session);
94
+ const pending = [{
95
+ seq,
96
+ depth: 0
97
+ }];
98
+ const visited = /* @__PURE__ */ new Set();
99
+ const roots = /* @__PURE__ */ new Set();
100
+ while (pending.length > 0) {
101
+ const next = pending.pop();
102
+ if (next === void 0 || next.depth > MAX_ROOTS || visited.has(next.seq)) continue;
103
+ if (!Number.isSafeInteger(next.seq) || next.seq < 0 || next.seq >= beforeSeq) return null;
104
+ visited.add(next.seq);
105
+ if (visited.size > MAX_ROOTS) return null;
106
+ const event = events[next.seq];
107
+ if (event === void 0 || event.type !== "assistant/message" && event.type !== "tool/result") return null;
108
+ if (event.surfaceOp === "append") roots.add(event.seq);
109
+ else if (typeof event.surfaceOp === "object") {
110
+ const sources = event.sourceEventSeqs;
111
+ if (sources === void 0 || sources.length === 0) return null;
112
+ for (const source of sources) pending.push({
113
+ seq: source,
114
+ depth: next.depth + 1
115
+ });
116
+ } else return null;
117
+ if (roots.size > 1) return null;
118
+ }
119
+ return roots.size === 1 ? [...roots][0] ?? null : null;
120
+ }
121
+ function validRootGroup(roots, manifestSeq) {
122
+ const assistant = roots[0];
123
+ if (assistant?.type !== "assistant/message" || assistant.seq >= manifestSeq || assistant.surfaceOp !== "append" || assistant.data.interrupted === true || assistant.data.message.content.length === 0 || assistant.data.message.content.some((block) => block.type !== "tool-call")) return false;
124
+ const callIds = assistant.data.message.content.map((call) => call.id);
125
+ if (new Set(callIds).size !== callIds.length || roots.length !== callIds.length + 1) return false;
126
+ for (const [index, root] of roots.slice(1).entries()) {
127
+ if (root.type !== "tool/result" || root.seq >= manifestSeq || root.surfaceOp !== "append") return false;
128
+ if (root.data.message.content[0].isError === true || root.data.error !== void 0 || root.data.turn !== assistant.data.turn || root.data.step !== assistant.data.step || String(root.data.message.source.callId) !== String(callIds[index])) return false;
129
+ }
130
+ return true;
131
+ }
132
+ function sameNumbers(left, right) {
133
+ return left !== void 0 && left.length === right.length && left.every((value, index) => value === right[index]);
134
+ }
135
+ //#endregion
136
+ export { validatePublishedTailTrim as a, tailTrimStub as i, tailTrimMessage as n, sessionEvents as o, tailTrimRef as r, parseTailTrimRef as t };
package/package.json ADDED
@@ -0,0 +1,115 @@
1
+ {
2
+ "name": "dsh-context-compression-improved",
3
+ "version": "0.1.1",
4
+ "description": "One-install context-compression selector bundle for DeepSeek Harness",
5
+ "type": "module",
6
+ "main": "lib/index.js",
7
+ "types": "lib/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./lib/index.d.ts",
11
+ "default": "./lib/index.js"
12
+ },
13
+ "./invariant": {
14
+ "types": "./lib/invariant.d.ts",
15
+ "default": "./lib/invariant.js"
16
+ },
17
+ "./pruner": {
18
+ "types": "./lib/pruner.d.ts",
19
+ "default": "./lib/pruner.js"
20
+ },
21
+ "./client": {
22
+ "types": "./lib/client.d.ts",
23
+ "default": "./lib/client.js"
24
+ },
25
+ "./package.json": "./package.json"
26
+ },
27
+ "files": [
28
+ "lib/**/*.js",
29
+ "lib/**/*.d.ts",
30
+ "cordis.patch.yml",
31
+ "dsh.plugin.json",
32
+ "screenshots.json",
33
+ "assets",
34
+ "README.md",
35
+ "README.zh.md",
36
+ "LICENSE",
37
+ "THIRD_PARTY_NOTICES.md"
38
+ ],
39
+ "dsh": {
40
+ "client": {
41
+ "inject": [
42
+ "@deepseek-ai/dsh-client-locale",
43
+ "@deepseek-ai/dsh-client-runtime",
44
+ "@deepseek-ai/dsh-client-ui-settings",
45
+ "@deepseek-ai/dsh-client-ui-workspace"
46
+ ],
47
+ "platform": "web"
48
+ },
49
+ "bundle": {
50
+ "patch": "./cordis.patch.yml"
51
+ }
52
+ },
53
+ "scripts": {
54
+ "bundle": "tsdown --config tsdown.config.ts && tsdown --config tsdown.client.config.ts",
55
+ "prepack": "pnpm run bundle",
56
+ "typecheck": "tsc --noEmit -p tsconfig.json",
57
+ "test": "vitest run --root ../.. --config vitest.config.ts --project runtime --project selector-host --project selector-client",
58
+ "pack:dry-run": "npm pack --dry-run"
59
+ },
60
+ "repository": {
61
+ "type": "git",
62
+ "url": "git+https://github.com/drscrewdriver/dsh-context-compression-improved.git",
63
+ "directory": "packages/selector"
64
+ },
65
+ "homepage": "https://github.com/drscrewdriver/dsh-context-compression-improved#readme",
66
+ "bugs": {
67
+ "url": "https://github.com/drscrewdriver/dsh-context-compression-improved/issues"
68
+ },
69
+ "author": "WilliamShi666",
70
+ "engines": {
71
+ "node": "^22.19.0 || >=24.0.0",
72
+ "dsh": ">=0.1.1-rc.2 <0.2.0-0"
73
+ },
74
+ "keywords": [
75
+ "deepseek",
76
+ "harness",
77
+ "context",
78
+ "compression",
79
+ "plugin"
80
+ ],
81
+ "license": "MIT",
82
+ "publishConfig": {
83
+ "access": "public",
84
+ "tag": "latest"
85
+ },
86
+ "dependencies": {
87
+ "@huggingface/tokenizers": "0.1.3",
88
+ "js-yaml": "^4.3.2"
89
+ },
90
+ "peerDependencies": {
91
+ "@deepseek-ai/cordis": ">=4.0.1 <5",
92
+ "@deepseek-ai/cordis-plugin-include": ">=1.0.6 <2",
93
+ "@deepseek-ai/cordis-plugin-loader": ">=1.0.2 <2",
94
+ "@deepseek-ai/dsh-agent": ">=0.1.1-rc.2 <0.2.0",
95
+ "@deepseek-ai/dsh-agent-presets": ">=0.1.1-rc.2 <0.2.0",
96
+ "@deepseek-ai/dsh-command-compact": ">=0.1.1-rc.2 <0.2.0",
97
+ "@deepseek-ai/dsh-compaction": ">=0.1.1-rc.2 <0.2.0",
98
+ "@deepseek-ai/dsh-compaction-basic": ">=0.1.1-rc.2 <0.2.0",
99
+ "@deepseek-ai/dsh-client-locale": ">=0.1.1-rc.2 <0.2.0",
100
+ "@deepseek-ai/dsh-client-runtime": ">=0.1.1-rc.2 <0.2.0",
101
+ "@deepseek-ai/dsh-client-ui-primitives": ">=0.1.1-rc.2 <0.2.0",
102
+ "@deepseek-ai/dsh-client-ui-settings": ">=0.1.1-rc.2 <0.2.0",
103
+ "@deepseek-ai/dsh-client-ui-slots": ">=0.1.1-rc.2 <0.2.0",
104
+ "@deepseek-ai/dsh-client-ui-workspace": ">=0.1.1-rc.2 <0.2.0",
105
+ "@deepseek-ai/dsh-invariants": ">=0.1.1-rc.2 <0.2.0",
106
+ "@deepseek-ai/dsh-llm": ">=0.1.1-rc.2 <0.2.0",
107
+ "@deepseek-ai/dsh-session": ">=0.1.1-rc.2 <0.2.0",
108
+ "@deepseek-ai/dsh-settings": ">=0.1.1-rc.2 <0.2.0",
109
+ "@deepseek-ai/dsh-system-prompt": ">=0.1.1-rc.2 <0.2.0",
110
+ "@deepseek-ai/dsh-token-meter": ">=0.1.1-rc.2 <0.2.0",
111
+ "@deepseek-ai/dsh-tools": ">=0.1.1-rc.2 <0.2.0",
112
+ "@deepseek-ai/schemastery": ">=3.18.1 <4",
113
+ "react": ">=18 <19"
114
+ }
115
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "screenshots": [
3
+ "assets/screenshots/context-compression-selector-settings.png",
4
+ "assets/screenshots/context-compression-selector-profiles.jpg"
5
+ ]
6
+ }