dsh-context-compression-improved 0.8.0-beta.3 → 0.8.0-beta.5
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 +20 -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 +28 -0
- package/packages/selector/lib/client.js +631 -7
- package/packages/selector/lib/{advisor-state.js → config.js} +170 -65
- package/packages/selector/lib/index.d.ts +13 -1
- package/packages/selector/lib/index.js +306 -50
- package/packages/selector/lib/profiles.d.ts +15 -0
- package/packages/selector/lib/pruner.d.ts +65 -0
- package/packages/selector/lib/pruner.js +408 -5
- package/packages/selector/src/client/CompressionProfileControls.tsx +67 -0
- package/packages/selector/src/client/CompressionProfileSelector.tsx +2 -0
- package/packages/selector/src/client/decode.ts +57 -49
- package/packages/selector/src/client/index.ts +41 -0
- package/packages/selector/src/client/locales/de.ts +26 -0
- package/packages/selector/src/client/locales/es.ts +26 -0
- package/packages/selector/src/client/locales/fr.ts +26 -0
- package/packages/selector/src/client/locales/it.ts +26 -0
- package/packages/selector/src/client/locales/ja.ts +26 -0
- package/packages/selector/src/client/locales/ko.ts +26 -0
- package/packages/selector/src/client/locales/ru.ts +26 -0
- package/packages/selector/src/client/locales.ts +52 -0
- package/packages/selector/src/client/monitor-fab.ts +307 -0
- package/packages/selector/src/client/settings-section.tsx +16 -2
- package/packages/selector/src/index.ts +133 -60
- package/packages/selector/src/profiles.ts +431 -394
- package/packages/selector/src/pruner/state.ts +57 -50
- package/packages/selector/src/pruner.ts +241 -1
- package/packages/selector/src/runtime/audit.ts +303 -286
- package/packages/selector/src/runtime/config.ts +756 -706
- package/packages/selector/src/runtime/monitor.ts +149 -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 +365 -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 +8 -6
- package/packages/selector/tests/monitor-fab.client.spec.ts +117 -0
- 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/monitor.spec.ts +236 -0
- package/packages/selector/tests/runtime/public/public-runtime.spec.ts +2734 -2731
- 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
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compression monitor surface for the floating panel (FAB, task: cci-monitor-fab).
|
|
3
|
+
*
|
|
4
|
+
* Three pieces, one module:
|
|
5
|
+
* - `buildMonitorSnapshot`: the savings snapshot (reused verbatim from the
|
|
6
|
+
* ledger — no second accounting) plus the turn-tail intent-summary block
|
|
7
|
+
* (override / observed flag / gate constants / last fold);
|
|
8
|
+
* - `applySessionOverride`: the `/ctx-summary` override state machine,
|
|
9
|
+
* addressed by session id — the panel buttons drive the same state the
|
|
10
|
+
* command drives, never a second one;
|
|
11
|
+
* - `estimateSavingsPricing`: moved here from the entry (the savings route
|
|
12
|
+
* and the monitor route share it; keeping it next to the monitor payload
|
|
13
|
+
* avoids a runtime → entry import cycle).
|
|
14
|
+
*
|
|
15
|
+
* The monitor route itself stays in the entry (`registerMonitorRoute`),
|
|
16
|
+
* mirroring the savings/estimator registrars: all webServer wiring lives in
|
|
17
|
+
* one place, the data plane lives here.
|
|
18
|
+
*
|
|
19
|
+
* Contract discipline: the published `/savings` route contract is frozen
|
|
20
|
+
* (estimator-route spec encodes it) — the monitor aggregates but never
|
|
21
|
+
* reshapes it; new fields attach under `intent` / `sessionScope`.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/** Current host model-group selection, when the service exposes one. */
|
|
25
|
+
export interface AgentDefaultModelLike {
|
|
26
|
+
currentSelection?: () => { provider?: unknown, model?: unknown } | undefined
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
import { priceOfficialDeepSeekUsage } from './deepseek-official-pricing.ts'
|
|
30
|
+
import { getSavingsLedger, type SavingsLedger, type SavingsSnapshot } from './savings.ts'
|
|
31
|
+
import {
|
|
32
|
+
getLastIntentFold,
|
|
33
|
+
getObservedIntentEnabled,
|
|
34
|
+
getSummaryOverride,
|
|
35
|
+
setSummaryOverride,
|
|
36
|
+
type IntentFoldRecord,
|
|
37
|
+
} from './tokenpilot/advisor-state.ts'
|
|
38
|
+
import { INTENT_GATE_FLOOR_FRACTION, INTENT_GATE_GROWTH_TOKENS } from './tokenpilot/intent-gate.ts'
|
|
39
|
+
|
|
40
|
+
export interface MonitorIntentBlock {
|
|
41
|
+
/** Session-scoped override; absent = settings-driven. */
|
|
42
|
+
override: 'on' | 'off' | undefined
|
|
43
|
+
/** Settings flag as last observed by the gate; absent = gate not evaluated yet. */
|
|
44
|
+
observedEnabled: boolean | undefined
|
|
45
|
+
gate: { floorFraction: number, growthTokens: number }
|
|
46
|
+
lastFold: IntentFoldRecord | undefined
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export type MonitorSnapshot = SavingsSnapshot & {
|
|
50
|
+
intent: MonitorIntentBlock
|
|
51
|
+
/** The session the snapshot was filtered to; `null` = all-sessions aggregate. */
|
|
52
|
+
sessionScope: string | null
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Savings snapshot plus the intent-summary control block. Ledger is
|
|
57
|
+
* injectable so tests seed a private instance instead of the process
|
|
58
|
+
* singleton.
|
|
59
|
+
*/
|
|
60
|
+
export function buildMonitorSnapshot(
|
|
61
|
+
sessionId?: string,
|
|
62
|
+
ledger: SavingsLedger = getSavingsLedger(),
|
|
63
|
+
): MonitorSnapshot {
|
|
64
|
+
const override = getSummaryOverride(sessionId ?? '')
|
|
65
|
+
return {
|
|
66
|
+
...ledger.snapshot(sessionId),
|
|
67
|
+
intent: {
|
|
68
|
+
override,
|
|
69
|
+
observedEnabled: getObservedIntentEnabled(sessionId ?? ''),
|
|
70
|
+
gate: { floorFraction: INTENT_GATE_FLOOR_FRACTION, growthTokens: INTENT_GATE_GROWTH_TOKENS },
|
|
71
|
+
lastFold: getLastIntentFold(sessionId ?? ''),
|
|
72
|
+
},
|
|
73
|
+
sessionScope: sessionId ?? null,
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export type SessionOverrideAction = 'on' | 'off' | 'clear'
|
|
78
|
+
|
|
79
|
+
export function isSessionOverrideAction(value: unknown): value is SessionOverrideAction {
|
|
80
|
+
return value === 'on' || value === 'off' || value === 'clear'
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Drive the `/ctx-summary` override state machine: `on`/`off` pin the
|
|
85
|
+
* session, `clear` returns it to settings-driven. The resulting override is
|
|
86
|
+
* handed back so the route can echo the post-action truth.
|
|
87
|
+
*/
|
|
88
|
+
export function applySessionOverride(
|
|
89
|
+
sessionId: string,
|
|
90
|
+
action: SessionOverrideAction,
|
|
91
|
+
): { override: 'on' | 'off' | undefined } {
|
|
92
|
+
setSummaryOverride(sessionId, action === 'clear' ? undefined : action)
|
|
93
|
+
return { override: getSummaryOverride(sessionId) }
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 金额估算(monitor 口径的 token/金额估算,结合官方牌价):
|
|
98
|
+
* - actualCost:本进程累计真实 usage 的官方牌价(exact/range);
|
|
99
|
+
* - estimatedSavedCost:净节省(精确口径)按 cache-miss 输入价折算——
|
|
100
|
+
* 基线假设"这些 token 不压缩就要全价进上下文",标注估算。
|
|
101
|
+
* 仅官方 deepseek-official 路由可解;第三方/未知模型 fail-closed 为 undefined。
|
|
102
|
+
* (自入口迁入: savings 路由与 monitor 路由共享,避免 runtime → entry 回环。)
|
|
103
|
+
*/
|
|
104
|
+
export function estimateSavingsPricing(readService: (name: string) => unknown, snapshot: {
|
|
105
|
+
startedAt: string
|
|
106
|
+
net: { exact: number; estimated: number }
|
|
107
|
+
usage: { requests: number; inputTokens: number; outputTokens: number; cacheReadTokens: number; cacheWriteTokens: number }
|
|
108
|
+
}): { currency: string; actualCost?: string | undefined; estimatedSavedCost?: string | undefined } | undefined {
|
|
109
|
+
if (snapshot.usage.requests === 0 && snapshot.net.exact === 0) return undefined
|
|
110
|
+
const defaults = readService('agentDefaultModel') as AgentDefaultModelLike | undefined
|
|
111
|
+
const selection = defaults?.currentSelection?.()
|
|
112
|
+
const provider = typeof selection?.provider === 'string' ? selection.provider : ''
|
|
113
|
+
const modelId = typeof selection?.model === 'string' ? selection.model : ''
|
|
114
|
+
if (provider !== 'deepseek-official') return undefined
|
|
115
|
+
const now = new Date()
|
|
116
|
+
const startedAt = new Date(snapshot.startedAt)
|
|
117
|
+
const completedAt = now.getTime() > startedAt.getTime() ? now : new Date(startedAt.getTime() + 1)
|
|
118
|
+
const base = {
|
|
119
|
+
provider,
|
|
120
|
+
baseUrlClass: 'official-public',
|
|
121
|
+
apiRoute: 'chat-completions',
|
|
122
|
+
modelId,
|
|
123
|
+
currency: 'USD',
|
|
124
|
+
startedAt,
|
|
125
|
+
completedAt,
|
|
126
|
+
} as const
|
|
127
|
+
const actual = priceOfficialDeepSeekUsage({
|
|
128
|
+
...base,
|
|
129
|
+
usage: {
|
|
130
|
+
cacheReadTokens: Math.max(0, Math.round(snapshot.usage.cacheReadTokens)),
|
|
131
|
+
cacheMissTokens: Math.max(0, Math.round(snapshot.usage.inputTokens)),
|
|
132
|
+
outputTokens: Math.max(0, Math.round(snapshot.usage.outputTokens)),
|
|
133
|
+
},
|
|
134
|
+
})
|
|
135
|
+
const saved = snapshot.net.exact > 0
|
|
136
|
+
? priceOfficialDeepSeekUsage({
|
|
137
|
+
...base,
|
|
138
|
+
usage: { cacheReadTokens: 0, cacheMissTokens: Math.round(snapshot.net.exact), outputTokens: 0 },
|
|
139
|
+
})
|
|
140
|
+
: undefined
|
|
141
|
+
const decimalOf = (cost: typeof actual): string | undefined =>
|
|
142
|
+
cost.kind === 'exact' ? cost.decimal : cost.kind === 'range' ? cost.minimum.decimal : undefined
|
|
143
|
+
if (actual.kind === 'unpriced' && (saved === undefined || saved.kind === 'unpriced')) return undefined
|
|
144
|
+
return {
|
|
145
|
+
currency: 'USD',
|
|
146
|
+
...(actual.kind === 'unpriced' ? {} : { actualCost: decimalOf(actual) }),
|
|
147
|
+
...(saved === undefined || saved.kind === 'unpriced' ? {} : { estimatedSavedCost: decimalOf(saved) }),
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/ctx-summary off|on|status` — in-session temporary override for the
|
|
3
|
+
* turn-tail intent-summary gate.
|
|
4
|
+
*
|
|
5
|
+
* The override is session-scoped by design: settings are session-frozen
|
|
6
|
+
* (changes only affect newly observed sessions), so "temporarily disable and
|
|
7
|
+
* resume" can only be expressed as runtime state. Overrides die with the
|
|
8
|
+
* process and never leak across sessions. `off` wins over the settings gate;
|
|
9
|
+
* `on` never bypasses the numeric growth gates — it only lifts the
|
|
10
|
+
* settings/override veto.
|
|
11
|
+
*
|
|
12
|
+
* Registration mirrors the host's own `/compact` command: optional
|
|
13
|
+
* `commands`-service injection (a host without the registry simply never
|
|
14
|
+
* mounts the command), dynamic `CommandDefinitionId` brand import with a
|
|
15
|
+
* plain fallback so a stripped host cannot break plugin load.
|
|
16
|
+
*/
|
|
17
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
18
|
+
import {
|
|
19
|
+
getLastIntentFold,
|
|
20
|
+
getObservedIntentEnabled,
|
|
21
|
+
getSummaryOverride,
|
|
22
|
+
setSummaryOverride,
|
|
23
|
+
} from './tokenpilot/advisor-state.ts'
|
|
24
|
+
import { INTENT_GATE_FLOOR_FRACTION, INTENT_GATE_GROWTH_TOKENS } from './tokenpilot/intent-gate.ts'
|
|
25
|
+
|
|
26
|
+
export const SUMMARY_COMMAND_NAME = 'ctx-summary'
|
|
27
|
+
|
|
28
|
+
const USAGE = 'Usage: /ctx-summary off|on|status'
|
|
29
|
+
|
|
30
|
+
export type SummaryCommandArgs = 'off' | 'on' | 'status'
|
|
31
|
+
|
|
32
|
+
/** Parse the raw argument string; `undefined` = unrecognized (usage error). */
|
|
33
|
+
export function parseSummaryCommandArgs(rawInput: string): SummaryCommandArgs | undefined {
|
|
34
|
+
const token = rawInput.trim().toLowerCase()
|
|
35
|
+
if (token === 'off' || token === 'on' || token === 'status') return token
|
|
36
|
+
return undefined
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function describeFold(sessionId: string): string {
|
|
40
|
+
const fold = getLastIntentFold(sessionId)
|
|
41
|
+
if (fold === undefined) return 'none'
|
|
42
|
+
return `turn ${fold.turn}, seq ${fold.startSeq}..${fold.endSeq}`
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Human-facing status line; observability only, never a decision input. */
|
|
46
|
+
export function formatSummaryStatus(sessionId: string): string {
|
|
47
|
+
const override = getSummaryOverride(sessionId)
|
|
48
|
+
const enabled = getObservedIntentEnabled(sessionId)
|
|
49
|
+
const enabledText = enabled === undefined ? 'unknown (gate not evaluated yet)' : enabled ? 'yes' : 'no'
|
|
50
|
+
return [
|
|
51
|
+
`ctx-summary status:`,
|
|
52
|
+
` override: ${override ?? 'default (settings-driven)'}`,
|
|
53
|
+
` intentSummary enabled (settings): ${enabledText}`,
|
|
54
|
+
` gate: floor >${Math.round(INTENT_GATE_FLOOR_FRACTION * 100)}% of window AND growth >${INTENT_GATE_GROWTH_TOKENS} tokens since last fold`,
|
|
55
|
+
` last fold: ${describeFold(sessionId)}`,
|
|
56
|
+
].join('\n')
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Resolve the invoking agent's session id (`''` when the host exposes none). */
|
|
60
|
+
function sessionIdOf(agent: unknown): string {
|
|
61
|
+
const session = (agent as { session?: { id?: unknown } } | undefined)?.session
|
|
62
|
+
if (session === undefined || session.id === undefined || session.id === null) return ''
|
|
63
|
+
return String(session.id)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
type CommandsServiceLike = {
|
|
67
|
+
register: (definition: {
|
|
68
|
+
definitionId?: unknown
|
|
69
|
+
name: string
|
|
70
|
+
description: string
|
|
71
|
+
handler: (invocation: { agent?: unknown, rawInput?: unknown }) => Promise<{ kind: 'success' | 'error', text: string }>
|
|
72
|
+
}) => unknown
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Best-effort `CommandDefinitionId` brand; `undefined` on stripped hosts (registered without it). */
|
|
76
|
+
async function loadCommandDefinitionId(): Promise<((id: string) => unknown) | undefined> {
|
|
77
|
+
try {
|
|
78
|
+
const mod = (await import('@deepseek-ai/dsh-commands')) as { CommandDefinitionId?: (id: string) => unknown }
|
|
79
|
+
return mod.CommandDefinitionId
|
|
80
|
+
} catch {
|
|
81
|
+
return undefined
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Mount `/ctx-summary` for every composed human-command adapter. Never
|
|
87
|
+
* throws: the command is a convenience surface, and a host without the
|
|
88
|
+
* commands service (or with an incompatible registry shape) simply runs
|
|
89
|
+
* without it.
|
|
90
|
+
*/
|
|
91
|
+
export function registerSummaryCommand(ctx: Context): void {
|
|
92
|
+
ctx.inject(['commands'], (injected) => {
|
|
93
|
+
const commands = (injected as { commands?: unknown }).commands as CommandsServiceLike | undefined
|
|
94
|
+
if (commands === undefined || typeof commands.register !== 'function') return
|
|
95
|
+
void (async () => {
|
|
96
|
+
try {
|
|
97
|
+
const definitionId = await loadCommandDefinitionId()
|
|
98
|
+
await commands.register({
|
|
99
|
+
name: SUMMARY_COMMAND_NAME,
|
|
100
|
+
description: 'Turn-tail intent summary: temporarily disable/resume or inspect status',
|
|
101
|
+
...(definitionId === undefined ? {} : { definitionId: definitionId('dsh-context-compression-improved/ctx-summary') }),
|
|
102
|
+
handler: async (invocation) => {
|
|
103
|
+
const rawInput = typeof invocation.rawInput === 'string' ? invocation.rawInput : ''
|
|
104
|
+
const args = parseSummaryCommandArgs(rawInput)
|
|
105
|
+
if (args === undefined) return { kind: 'error' as const, text: USAGE }
|
|
106
|
+
const sessionId = sessionIdOf(invocation.agent)
|
|
107
|
+
if (args === 'status') {
|
|
108
|
+
if (sessionId === '') return { kind: 'error' as const, text: 'ctx-summary: no session is attached to this invocation' }
|
|
109
|
+
return { kind: 'success' as const, text: formatSummaryStatus(sessionId) }
|
|
110
|
+
}
|
|
111
|
+
if (sessionId === '') return { kind: 'error' as const, text: 'ctx-summary: no session is attached to this invocation' }
|
|
112
|
+
setSummaryOverride(sessionId, args)
|
|
113
|
+
return {
|
|
114
|
+
kind: 'success' as const,
|
|
115
|
+
text: args === 'off'
|
|
116
|
+
? 'ctx-summary: turn-tail intent summary paused for this session (until process restart or `/ctx-summary on`)'
|
|
117
|
+
: 'ctx-summary: turn-tail intent summary resumed for this session (numeric gates still apply)',
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
})
|
|
121
|
+
} catch {
|
|
122
|
+
// Registry rejected the definition (shape drift, closed union): the
|
|
123
|
+
// plugin keeps working, only the convenience command is absent.
|
|
124
|
+
}
|
|
125
|
+
})()
|
|
126
|
+
})
|
|
127
|
+
}
|