# Conflicts: # docs/core-data-structures/session.i18n.yaml # packages/client/ui-conversation/README.i18n.yaml # packages/client/ui-conversation/README.md # packages/llm/token-meter/README.i18n.yaml # packages/llm/token-meter/README.md # packages/llm/token-meter/README.zh.md # packages/llm/token-meter/src/projection.ts
78 lines
3.2 KiB
TypeScript
78 lines
3.2 KiB
TypeScript
/**
|
|
* Pure client-safe token-projection vocabulary.
|
|
*
|
|
* @module @deepseek-ai/dsh-token-meter/projection
|
|
*/
|
|
|
|
/**
|
|
* Durable cumulative provider usage for a complete session log.
|
|
*
|
|
* The four buckets are disjoint. In particular, reasoning tokens are already
|
|
* included in `outputTokens` and are not accumulated again.
|
|
*/
|
|
export interface TokenUsageProjection {
|
|
uncachedInputTokens: number
|
|
outputTokens: number
|
|
cacheReadTokens: number
|
|
cacheWriteTokens: number
|
|
}
|
|
|
|
/**
|
|
* Approximate context occupancy for a status display.
|
|
*
|
|
* The fields, when present, are deliberately NOT one atomic request
|
|
* observation: each is a last-wins record of a different moment. Switching
|
|
* models can therefore pair a fresh capacity with the previous route's
|
|
* pressure until the next request reports usage. This is an intentional trade
|
|
* — the value is a user-facing reference, not a billing or gating input. See
|
|
* the token-meter README for the full rationale.
|
|
*/
|
|
export interface ContextPressureProjection {
|
|
/**
|
|
* Provider-reported prompt size of the most recent request: uncached input
|
|
* plus cache reads and writes. Response output is excluded, so this does not
|
|
* grow as the current turn streams. Absent until a provider reports usage.
|
|
*/
|
|
pressureTokens?: number
|
|
/**
|
|
* What the NEXT request's prompt would cost: {@link pressureTokens} plus the
|
|
* heuristic repricing of everything the surface gained or lost since that
|
|
* sample. Only the delta is estimated, so the figure stays anchored to the
|
|
* provider while still reacting the moment a compaction shadows a span —
|
|
* which `pressureTokens` alone cannot do, since compaction reports no usage
|
|
* of its own. Absent until a provider reports usage.
|
|
*/
|
|
projectedTokens?: number
|
|
/** Newest recorded route capacity; absent when no adapter advertised one. */
|
|
contextWindow?: number
|
|
}
|
|
|
|
/**
|
|
* Heuristic composition of the next request's context: what the prompt is
|
|
* made of, not what it costs. All three figures use the meter's fixed
|
|
* density estimate, so they will not sum to the provider-anchored
|
|
* `projectedTokens`: the estimator systematically underprices CJK text and
|
|
* JSON schemas, which is exactly the error the anchoring in
|
|
* {@link ContextPressureProjection.projectedTokens} keeps out of the occupancy
|
|
* figure. Present these as approximations of composition, never as a total.
|
|
*/
|
|
export interface ContextBreakdownProjection {
|
|
/** Heuristic tokens of the newest request envelope's system prompt; 0 before any request. */
|
|
systemTokens: number
|
|
/** Heuristic tokens of the newest request envelope's tool schemas; 0 before any request. */
|
|
toolsTokens: number
|
|
/** Heuristic tokens of the current model-visible conversation surface. */
|
|
messageTokens: number
|
|
}
|
|
|
|
declare module '@deepseek-ai/dsh-session-projection/types' {
|
|
interface SessionProjectionMap {
|
|
/** Provider-reported usage accumulated across the complete durable log. */
|
|
tokenUsage: TokenUsageProjection
|
|
/** Newest request pressure paired with the newest known route capacity. */
|
|
contextPressure: ContextPressureProjection
|
|
/** Heuristic system/tools/message composition of the next request. */
|
|
contextBreakdown: ContextBreakdownProjection
|
|
}
|
|
}
|