dsh-context-compression-improved 0.8.0-beta.2 → 0.8.0-beta.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 +16 -0
- package/eslint.config.js +30 -30
- package/package.json +163 -163
- package/packages/selector/dsh.plugin.json +16 -16
- package/packages/selector/lib/client.d.ts +20 -0
- package/packages/selector/lib/client.js +433 -17
- package/packages/selector/lib/index.js +213 -2
- package/packages/selector/lib/{advisor-state.js → intent-gate.js} +593 -3
- package/packages/selector/lib/profiles.d.ts +9 -0
- package/packages/selector/lib/pruner.d.ts +59 -0
- package/packages/selector/lib/pruner.js +494 -262
- package/packages/selector/lib/style.css +42 -0
- package/packages/selector/src/client/CompressionProfileControls.tsx +34 -0
- package/packages/selector/src/client/CompressionProfileSelector.module.css +199 -170
- package/packages/selector/src/client/CompressionProfileSelector.tsx +1 -0
- package/packages/selector/src/client/decode.ts +53 -49
- package/packages/selector/src/client/index.ts +4 -0
- package/packages/selector/src/client/locales/de.ts +20 -0
- package/packages/selector/src/client/locales/es.ts +20 -0
- package/packages/selector/src/client/locales/fr.ts +20 -0
- package/packages/selector/src/client/locales/it.ts +20 -0
- package/packages/selector/src/client/locales/ja.ts +20 -0
- package/packages/selector/src/client/locales/ko.ts +20 -0
- package/packages/selector/src/client/locales/ru.ts +20 -0
- package/packages/selector/src/client/locales.ts +236 -196
- package/packages/selector/src/client/savings-card.tsx +150 -0
- package/packages/selector/src/client/settings-section.tsx +133 -124
- package/packages/selector/src/index.ts +554 -411
- package/packages/selector/src/profiles.ts +414 -394
- package/packages/selector/src/pruner/state.ts +57 -50
- package/packages/selector/src/pruner.ts +2745 -2421
- package/packages/selector/src/runtime/audit.ts +303 -286
- package/packages/selector/src/runtime/config.ts +731 -706
- package/packages/selector/src/runtime/retrieve.ts +479 -457
- package/packages/selector/src/runtime/savings.ts +337 -0
- package/packages/selector/src/runtime/summary-command.ts +127 -0
- 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/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/types.ts +358 -346
- package/packages/selector/tests/auto-compact.client.spec.tsx +214 -213
- package/packages/selector/tests/code-skeleton.client.spec.ts +94 -93
- package/packages/selector/tests/custom-contract.client.spec.ts +223 -222
- package/packages/selector/tests/estimator-channel.client.spec.tsx +253 -252
- package/packages/selector/tests/estimator-route-registration.host.spec.ts +192 -188
- package/packages/selector/tests/preset-options-write.client.spec.ts +188 -187
- package/packages/selector/tests/profiles.client.spec.tsx +436 -434
- package/packages/selector/tests/runtime/audit.spec.ts +275 -274
- 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/public/public-runtime.spec.ts +2734 -2731
- package/packages/selector/tests/runtime/savings.spec.ts +120 -0
- package/packages/selector/tests/runtime/summary-command.spec.ts +154 -0
- package/packages/selector/tests/settings-seat.client.spec.ts +1 -1
- package/scripts/packed-install-e2e.mjs +1 -1
- package/scripts/verify-release.mjs +2 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { I as priceOfficialDeepSeekUsage, R as getSavingsLedger, a as getLastIntentFold, i as getAdvisorState, n as INTENT_GATE_GROWTH_TOKENS, o as getObservedIntentEnabled, p as setSummaryOverride, s as getSummaryOverride, t as INTENT_GATE_FLOOR_FRACTION, v as DEFAULT_CONTEXT_COMPRESSION_SETTINGS } from "./intent-gate.js";
|
|
2
2
|
import z from "@deepseek-ai/schemastery";
|
|
3
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
3
|
import { createHash } from "node:crypto";
|
|
4
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
5
5
|
import { chmod, mkdtemp, readFile, rename, rm, stat, utimes, writeFile } from "node:fs/promises";
|
|
6
6
|
import { tmpdir } from "node:os";
|
|
7
7
|
import { isAbsolute, join } from "node:path";
|
|
@@ -75,6 +75,96 @@ async function buildEstimatorCatalog(deps) {
|
|
|
75
75
|
};
|
|
76
76
|
}
|
|
77
77
|
//#endregion
|
|
78
|
+
//#region src/runtime/summary-command.ts
|
|
79
|
+
const SUMMARY_COMMAND_NAME = "ctx-summary";
|
|
80
|
+
const USAGE = "Usage: /ctx-summary off|on|status";
|
|
81
|
+
/** Parse the raw argument string; `undefined` = unrecognized (usage error). */
|
|
82
|
+
function parseSummaryCommandArgs(rawInput) {
|
|
83
|
+
const token = rawInput.trim().toLowerCase();
|
|
84
|
+
if (token === "off" || token === "on" || token === "status") return token;
|
|
85
|
+
}
|
|
86
|
+
function describeFold(sessionId) {
|
|
87
|
+
const fold = getLastIntentFold(sessionId);
|
|
88
|
+
if (fold === void 0) return "none";
|
|
89
|
+
return `turn ${fold.turn}, seq ${fold.startSeq}..${fold.endSeq}`;
|
|
90
|
+
}
|
|
91
|
+
/** Human-facing status line; observability only, never a decision input. */
|
|
92
|
+
function formatSummaryStatus(sessionId) {
|
|
93
|
+
const override = getSummaryOverride(sessionId);
|
|
94
|
+
const enabled = getObservedIntentEnabled(sessionId);
|
|
95
|
+
const enabledText = enabled === void 0 ? "unknown (gate not evaluated yet)" : enabled ? "yes" : "no";
|
|
96
|
+
return [
|
|
97
|
+
`ctx-summary status:`,
|
|
98
|
+
` override: ${override ?? "default (settings-driven)"}`,
|
|
99
|
+
` intentSummary enabled (settings): ${enabledText}`,
|
|
100
|
+
` gate: floor >${Math.round(INTENT_GATE_FLOOR_FRACTION * 100)}% of window AND growth >${INTENT_GATE_GROWTH_TOKENS} tokens since last fold`,
|
|
101
|
+
` last fold: ${describeFold(sessionId)}`
|
|
102
|
+
].join("\n");
|
|
103
|
+
}
|
|
104
|
+
/** Resolve the invoking agent's session id (`''` when the host exposes none). */
|
|
105
|
+
function sessionIdOf(agent) {
|
|
106
|
+
const session = agent?.session;
|
|
107
|
+
if (session === void 0 || session.id === void 0 || session.id === null) return "";
|
|
108
|
+
return String(session.id);
|
|
109
|
+
}
|
|
110
|
+
/** Best-effort `CommandDefinitionId` brand; `undefined` on stripped hosts (registered without it). */
|
|
111
|
+
async function loadCommandDefinitionId() {
|
|
112
|
+
try {
|
|
113
|
+
return (await import("@deepseek-ai/dsh-commands")).CommandDefinitionId;
|
|
114
|
+
} catch {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Mount `/ctx-summary` for every composed human-command adapter. Never
|
|
120
|
+
* throws: the command is a convenience surface, and a host without the
|
|
121
|
+
* commands service (or with an incompatible registry shape) simply runs
|
|
122
|
+
* without it.
|
|
123
|
+
*/
|
|
124
|
+
function registerSummaryCommand(ctx) {
|
|
125
|
+
ctx.inject(["commands"], (injected) => {
|
|
126
|
+
const commands = injected.commands;
|
|
127
|
+
if (commands === void 0 || typeof commands.register !== "function") return;
|
|
128
|
+
(async () => {
|
|
129
|
+
try {
|
|
130
|
+
const definitionId = await loadCommandDefinitionId();
|
|
131
|
+
await commands.register({
|
|
132
|
+
name: SUMMARY_COMMAND_NAME,
|
|
133
|
+
description: "Turn-tail intent summary: temporarily disable/resume or inspect status",
|
|
134
|
+
...definitionId === void 0 ? {} : { definitionId: definitionId("dsh-context-compression-improved/ctx-summary") },
|
|
135
|
+
handler: async (invocation) => {
|
|
136
|
+
const args = parseSummaryCommandArgs(typeof invocation.rawInput === "string" ? invocation.rawInput : "");
|
|
137
|
+
if (args === void 0) return {
|
|
138
|
+
kind: "error",
|
|
139
|
+
text: USAGE
|
|
140
|
+
};
|
|
141
|
+
const sessionId = sessionIdOf(invocation.agent);
|
|
142
|
+
if (args === "status") {
|
|
143
|
+
if (sessionId === "") return {
|
|
144
|
+
kind: "error",
|
|
145
|
+
text: "ctx-summary: no session is attached to this invocation"
|
|
146
|
+
};
|
|
147
|
+
return {
|
|
148
|
+
kind: "success",
|
|
149
|
+
text: formatSummaryStatus(sessionId)
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
if (sessionId === "") return {
|
|
153
|
+
kind: "error",
|
|
154
|
+
text: "ctx-summary: no session is attached to this invocation"
|
|
155
|
+
};
|
|
156
|
+
setSummaryOverride(sessionId, args);
|
|
157
|
+
return {
|
|
158
|
+
kind: "success",
|
|
159
|
+
text: args === "off" ? "ctx-summary: turn-tail intent summary paused for this session (until process restart or `/ctx-summary on`)" : "ctx-summary: turn-tail intent summary resumed for this session (numeric gates still apply)"
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
} catch {}
|
|
164
|
+
})();
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
//#endregion
|
|
78
168
|
//#region src/preset-overlay.ts
|
|
79
169
|
/** Plugin-owned, reversible compression overlays for native agent presets. */
|
|
80
170
|
/**
|
|
@@ -504,6 +594,7 @@ function restoreMethod(presets, snapshot) {
|
|
|
504
594
|
//#region src/index.ts
|
|
505
595
|
const ESTIMATOR_CATALOG_ROUTES = ["/endpoint/dsh-context-compression-improved/estimator-catalog", "/api/dsh-context-compression-improved/estimator-catalog"];
|
|
506
596
|
const ADVISOR_REPORT_ROUTES = ["/endpoint/dsh-context-compression-improved/advisor-report", "/api/dsh-context-compression-improved/advisor-report"];
|
|
597
|
+
const SAVINGS_ROUTES = ["/endpoint/dsh-context-compression-improved/savings", "/api/dsh-context-compression-improved/savings"];
|
|
507
598
|
function sessionFor(readService, sessionId) {
|
|
508
599
|
const agents = readService("agents");
|
|
509
600
|
return typeof agents?.get === "function" ? agents.get(sessionId)?.session : void 0;
|
|
@@ -719,6 +810,124 @@ function registerEstimatorCatalogRoute(ctx) {
|
|
|
719
810
|
log("warn", "context-compression webServer not active yet — estimator catalog route pending: %s", ESTIMATOR_CATALOG_ROUTES.join(", "));
|
|
720
811
|
}
|
|
721
812
|
/**
|
|
813
|
+
* 金额估算(monitor 口径的 token/金额估算,结合官方牌价):
|
|
814
|
+
* - actualCost:本进程累计真实 usage 的官方牌价(exact/range);
|
|
815
|
+
* - estimatedSavedCost:净节省(精确口径)按 cache-miss 输入价折算——
|
|
816
|
+
* 基线假设"这些 token 不压缩就要全价进上下文",标注估算。
|
|
817
|
+
* 仅官方 deepseek-official 路由可解;第三方/未知模型 fail-closed 为 undefined。
|
|
818
|
+
*/
|
|
819
|
+
function estimateSavingsPricing(readService, snapshot) {
|
|
820
|
+
if (snapshot.usage.requests === 0 && snapshot.net.exact === 0) return void 0;
|
|
821
|
+
const selection = readService("agentDefaultModel")?.currentSelection?.();
|
|
822
|
+
const provider = typeof selection?.provider === "string" ? selection.provider : "";
|
|
823
|
+
const modelId = typeof selection?.model === "string" ? selection.model : "";
|
|
824
|
+
if (provider !== "deepseek-official") return void 0;
|
|
825
|
+
const now = /* @__PURE__ */ new Date();
|
|
826
|
+
const startedAt = new Date(snapshot.startedAt);
|
|
827
|
+
const base = {
|
|
828
|
+
provider,
|
|
829
|
+
baseUrlClass: "official-public",
|
|
830
|
+
apiRoute: "chat-completions",
|
|
831
|
+
modelId,
|
|
832
|
+
currency: "USD",
|
|
833
|
+
startedAt,
|
|
834
|
+
completedAt: now.getTime() > startedAt.getTime() ? now : new Date(startedAt.getTime() + 1)
|
|
835
|
+
};
|
|
836
|
+
const actual = priceOfficialDeepSeekUsage({
|
|
837
|
+
...base,
|
|
838
|
+
usage: {
|
|
839
|
+
cacheReadTokens: Math.max(0, Math.round(snapshot.usage.cacheReadTokens)),
|
|
840
|
+
cacheMissTokens: Math.max(0, Math.round(snapshot.usage.inputTokens)),
|
|
841
|
+
outputTokens: Math.max(0, Math.round(snapshot.usage.outputTokens))
|
|
842
|
+
}
|
|
843
|
+
});
|
|
844
|
+
const saved = snapshot.net.exact > 0 ? priceOfficialDeepSeekUsage({
|
|
845
|
+
...base,
|
|
846
|
+
usage: {
|
|
847
|
+
cacheReadTokens: 0,
|
|
848
|
+
cacheMissTokens: Math.round(snapshot.net.exact),
|
|
849
|
+
outputTokens: 0
|
|
850
|
+
}
|
|
851
|
+
}) : void 0;
|
|
852
|
+
const decimalOf = (cost) => cost.kind === "exact" ? cost.decimal : cost.kind === "range" ? cost.minimum.decimal : void 0;
|
|
853
|
+
if (actual.kind === "unpriced" && (saved === void 0 || saved.kind === "unpriced")) return void 0;
|
|
854
|
+
return {
|
|
855
|
+
currency: "USD",
|
|
856
|
+
...actual.kind === "unpriced" ? {} : { actualCost: decimalOf(actual) },
|
|
857
|
+
...saved === void 0 || saved.kind === "unpriced" ? {} : { estimatedSavedCost: decimalOf(saved) }
|
|
858
|
+
};
|
|
859
|
+
}
|
|
860
|
+
/**
|
|
861
|
+
* Read-only savings snapshot route (`GET .../savings?sessionId=…`): gross saved,
|
|
862
|
+
* offsets (negative savings — compressed content later re-read in full), and net,
|
|
863
|
+
* with exact-tokenizer and chars/4 bases reported separately and never merged.
|
|
864
|
+
* Registration mirrors the estimator-catalog route (webServer gate alone, dual
|
|
865
|
+
* channels for both service arrival orders, single guarded registration).
|
|
866
|
+
*/
|
|
867
|
+
function registerSavingsRoute(ctx) {
|
|
868
|
+
const readService = (name) => {
|
|
869
|
+
try {
|
|
870
|
+
return ctx.get(name);
|
|
871
|
+
} catch {
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
};
|
|
875
|
+
const log = (level, message, ...args) => {
|
|
876
|
+
console[level](message, ...args);
|
|
877
|
+
};
|
|
878
|
+
let registered = false;
|
|
879
|
+
const register = (webServer, channel) => {
|
|
880
|
+
if (registered) return;
|
|
881
|
+
const handler = (req, res) => {
|
|
882
|
+
const resTyped = res;
|
|
883
|
+
if (typeof resTyped?.writeHead !== "function" || typeof resTyped?.end !== "function") return;
|
|
884
|
+
let sessionId;
|
|
885
|
+
try {
|
|
886
|
+
const raw = new URL(String(req?.url ?? "/"), "http://localhost").searchParams.get("sessionId");
|
|
887
|
+
if (typeof raw === "string" && raw.length > 0 && raw.length <= 512) sessionId = raw;
|
|
888
|
+
} catch {}
|
|
889
|
+
const snapshot = getSavingsLedger().snapshot(sessionId);
|
|
890
|
+
const pricing = estimateSavingsPricing(readService, snapshot);
|
|
891
|
+
resTyped.writeHead(200, {
|
|
892
|
+
"content-type": "application/json; charset=utf-8",
|
|
893
|
+
"cache-control": "no-cache"
|
|
894
|
+
});
|
|
895
|
+
resTyped.end(JSON.stringify({
|
|
896
|
+
ok: true,
|
|
897
|
+
...snapshot,
|
|
898
|
+
...pricing === void 0 ? {} : { pricing }
|
|
899
|
+
}));
|
|
900
|
+
};
|
|
901
|
+
try {
|
|
902
|
+
const disposers = SAVINGS_ROUTES.map((path) => webServer.register({
|
|
903
|
+
kind: "exact",
|
|
904
|
+
path,
|
|
905
|
+
handler
|
|
906
|
+
})).filter((off) => typeof off === "function");
|
|
907
|
+
registered = true;
|
|
908
|
+
ctx.effect(() => () => {
|
|
909
|
+
for (const off of disposers) off();
|
|
910
|
+
}, "contextCompressionSelector.savings route");
|
|
911
|
+
log("info", "context-compression savings route registered (%s): %s", channel, SAVINGS_ROUTES.join(", "));
|
|
912
|
+
} catch (error) {
|
|
913
|
+
log("warn", "context-compression savings route registration failed (%s): %o", channel, error);
|
|
914
|
+
}
|
|
915
|
+
};
|
|
916
|
+
const active = asWebServer(readService("webServer"));
|
|
917
|
+
if (active !== void 0) {
|
|
918
|
+
register(active, "direct");
|
|
919
|
+
if (registered) return;
|
|
920
|
+
}
|
|
921
|
+
ctx.inject(["webServer"], (injected) => {
|
|
922
|
+
const webServer = asWebServer(injected.webServer);
|
|
923
|
+
if (webServer === void 0) {
|
|
924
|
+
log("warn", "context-compression webServer exposes no register() — savings route not registered");
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
register(webServer, "inject");
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
/**
|
|
722
931
|
* Resolve one possibly-volatile field: a live ref on 0.1.7+, a plain value
|
|
723
932
|
* otherwise (0.1.7 hands `.volatile()` fields to `apply()` as live refs).
|
|
724
933
|
*/
|
|
@@ -741,7 +950,9 @@ const Config = z.object({
|
|
|
741
950
|
function apply(ctx, config = {}) {
|
|
742
951
|
try {
|
|
743
952
|
if (config.estimatorCatalogRoute === true) registerEstimatorCatalogRoute(ctx);
|
|
953
|
+
registerSavingsRoute(ctx);
|
|
744
954
|
if (config.advisorReportRoute === true) registerAdvisorReportRoute(ctx);
|
|
955
|
+
registerSummaryCommand(ctx);
|
|
745
956
|
if (config.presetOverlay !== true) return;
|
|
746
957
|
ctx.inject(["agentPresets"], (presetsCtx) => {
|
|
747
958
|
const installation = decorateAgentPresets(presetsCtx.agentPresets, {
|