@jerry_chzy/dsh-token-usage 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.i18n.yaml +6 -0
- package/README.md +162 -2
- package/README.zh.md +163 -0
- package/cordis.patch.yml +7 -0
- package/lib/client.js +311 -0
- package/lib/index.js +891 -0
- package/lib/types/backfill.d.ts +110 -0
- package/lib/types/client/TokenUsageDock.d.ts +28 -0
- package/lib/types/client/index.d.ts +30 -0
- package/lib/types/client/locales.d.ts +42 -0
- package/lib/types/command.d.ts +75 -0
- package/lib/types/format.d.ts +30 -0
- package/lib/types/index.d.ts +86 -0
- package/lib/types/pricing.d.ts +92 -0
- package/lib/types/projection.d.ts +52 -0
- package/package.json +53 -3
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* First-install history backfill: fold every archived session's durable usage
|
|
3
|
+
* into peak/off-peak money and write one `TOKEN_COST.md` per session working
|
|
4
|
+
* directory.
|
|
5
|
+
*
|
|
6
|
+
* The pass is a side effect of the plugin's fiber, not of its activation: it
|
|
7
|
+
* runs once per process start against the archived corpus, is idempotent
|
|
8
|
+
* through a marker comment carrying the fold version, the config hash, and the
|
|
9
|
+
* folded session watermarks, and never writes outside a session header's own
|
|
10
|
+
* `cwd`. A deployment without `ctx.sessionQuery` mounted, or a session without
|
|
11
|
+
* a working directory, is skipped with a log line rather than a guess.
|
|
12
|
+
*
|
|
13
|
+
* @module @deepseek-ai/dsh-client-ui-token-usage/backfill
|
|
14
|
+
*/
|
|
15
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
16
|
+
import type { SessionEvent, SessionId } from '@deepseek-ai/dsh-session';
|
|
17
|
+
import type { TokenUsageProjection } from '@deepseek-ai/dsh-token-meter/client';
|
|
18
|
+
/** Name of the report written into each session working directory. */
|
|
19
|
+
export declare const BACKFILL_FILE_NAME = "TOKEN_COST.md";
|
|
20
|
+
/** One price pair as `[offPeak, peak]`, in CNY per million tokens. */
|
|
21
|
+
export type PricePair = readonly [offPeak: number, peak: number];
|
|
22
|
+
/** One model route's peak and off-peak prices, in CNY per million tokens. */
|
|
23
|
+
export interface BackfillPrice {
|
|
24
|
+
/** Uncached prompt tokens, billed with cache-write tokens. */
|
|
25
|
+
inputMiss: PricePair;
|
|
26
|
+
/** Prompt tokens served from the cache. */
|
|
27
|
+
inputHit: PricePair;
|
|
28
|
+
/** Completion tokens, reasoning included. */
|
|
29
|
+
output: PricePair;
|
|
30
|
+
}
|
|
31
|
+
/** One peak-rate window in Beijing time. */
|
|
32
|
+
export interface PeakWindow {
|
|
33
|
+
/** Weekdays the window covers, `0` = Sunday through `6` = Saturday. */
|
|
34
|
+
days: number[];
|
|
35
|
+
/** Inclusive `HH:MM` start. */
|
|
36
|
+
start: string;
|
|
37
|
+
/** Exclusive `HH:MM` end. */
|
|
38
|
+
end: string;
|
|
39
|
+
}
|
|
40
|
+
/** Advisory peak windows: Beijing-time weekdays 09:00–12:00 and 14:00–18:00. */
|
|
41
|
+
export declare const DEFAULT_PEAK_HOURS: readonly PeakWindow[];
|
|
42
|
+
/**
|
|
43
|
+
* Advisory DeepSeek price table for the backfill (CNY per million tokens,
|
|
44
|
+
* `[offPeak, peak]`). Deployments override it through `backfill.pricing`; a
|
|
45
|
+
* route absent from the resolved table is reported as needing a price instead
|
|
46
|
+
* of being guessed at.
|
|
47
|
+
*/
|
|
48
|
+
export declare const DEFAULT_BACKFILL_PRICING: Readonly<Record<string, BackfillPrice>>;
|
|
49
|
+
/** One archived session's durable usage, folded and split by rate period. */
|
|
50
|
+
export interface SessionCostSample {
|
|
51
|
+
/** Session the usage was recorded under. */
|
|
52
|
+
readonly sessionId: SessionId;
|
|
53
|
+
/** Every route (`provider/model`) the log records, in first-seen order. */
|
|
54
|
+
readonly routes: readonly string[];
|
|
55
|
+
/** Route in force at the log's last header event, or null when it has none. */
|
|
56
|
+
readonly route: string | null;
|
|
57
|
+
/** Usage recorded inside a peak window. */
|
|
58
|
+
readonly peak: TokenUsageProjection;
|
|
59
|
+
/** Usage recorded outside every peak window. */
|
|
60
|
+
readonly offPeak: TokenUsageProjection;
|
|
61
|
+
/** Highest event seq folded. */
|
|
62
|
+
readonly lastSeq: number;
|
|
63
|
+
}
|
|
64
|
+
/** One backfill pass's resolved configuration. */
|
|
65
|
+
export interface BackfillRunOptions {
|
|
66
|
+
/** Prices in CNY per million tokens, keyed by model-id prefix. */
|
|
67
|
+
readonly pricing: Readonly<Record<string, BackfillPrice>>;
|
|
68
|
+
/** Peak windows in Beijing time. */
|
|
69
|
+
readonly peakHours: readonly PeakWindow[];
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Whether one instant falls inside a Beijing-time peak window.
|
|
73
|
+
* @param time - event timestamp in Unix epoch milliseconds.
|
|
74
|
+
* @param windows - peak windows stated in Beijing time.
|
|
75
|
+
* @returns true when some window covers the instant's weekday and clock time.
|
|
76
|
+
*/
|
|
77
|
+
export declare function isPeakInstant(time: number, windows: readonly PeakWindow[]): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Fold one session log's durable usage into peak/off-peak buckets and routes.
|
|
80
|
+
*
|
|
81
|
+
* A settlement is either an `assistant/message` or a failed `assistant/attempt`;
|
|
82
|
+
* both report usage, and a stream-embedded sample counts even when the event's
|
|
83
|
+
* own `usage` field is absent. A second sample for the same turn and step
|
|
84
|
+
* replaces the first instead of adding to it, matching the `tokenUsage`
|
|
85
|
+
* projection: the provider reports one attempt's usage, and a re-settled
|
|
86
|
+
* attempt must not bill twice. `llm/retry-started` closes that replacement slot,
|
|
87
|
+
* so the sample after a retry adds to the failed attempt it followed — those
|
|
88
|
+
* tokens were billed too. Each sample lands in the period its own event
|
|
89
|
+
* timestamp falls in, so a conversation spanning the peak boundary is priced at
|
|
90
|
+
* both rates.
|
|
91
|
+
* @param sessionId - session the log belongs to.
|
|
92
|
+
* @param events - the session's complete raw log, in seq order.
|
|
93
|
+
* @param windows - peak windows stated in Beijing time.
|
|
94
|
+
* @returns the folded routes, period buckets, and highest folded seq.
|
|
95
|
+
*/
|
|
96
|
+
export declare function foldSessionUsage(sessionId: SessionId, events: readonly SessionEvent[], windows: readonly PeakWindow[]): SessionCostSample;
|
|
97
|
+
/**
|
|
98
|
+
* Run one backfill pass over the archived session corpus.
|
|
99
|
+
*
|
|
100
|
+
* The session-query service is optional: an unmounted one logs and returns
|
|
101
|
+
* instead of failing the plugin. Each session is read and folded inside its
|
|
102
|
+
* own failure scope, so one unreadable or malformed log costs only that
|
|
103
|
+
* session's row. Nothing is written outside a session header's own `cwd`;
|
|
104
|
+
* a session without one is logged and skipped.
|
|
105
|
+
* @param ctx - the host context owning the logger and the optional service.
|
|
106
|
+
* @param options - the resolved backfill configuration.
|
|
107
|
+
* @param signal - aborts the pass when the plugin's fiber is disposed.
|
|
108
|
+
*/
|
|
109
|
+
export declare function runCostBackfill(ctx: Context, options: BackfillRunOptions, signal: AbortSignal): Promise<void>;
|
|
110
|
+
//# sourceMappingURL=backfill.d.ts.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The composer-dock token-usage readout: one line naming each non-zero
|
|
3
|
+
* billing bucket of the CURRENT conversation plus the configured cost
|
|
4
|
+
* estimate.
|
|
5
|
+
*
|
|
6
|
+
* Every number comes from the durable `tokenUsage` projection, so paging and
|
|
7
|
+
* compaction cannot change it, and the slot's session scope is what makes the
|
|
8
|
+
* counters per-conversation: switching sessions switches the projection the
|
|
9
|
+
* readout reads. The model id feeding the price lookup comes from the durable
|
|
10
|
+
* `modelSelection` projection's last-used selection, and the price table from
|
|
11
|
+
* the host-registered `tokenPricing` projection. When the host's history
|
|
12
|
+
* backfill found routes it cannot price, the `tokenCostBackfill` projection
|
|
13
|
+
* adds a notice pointing at the generated report.
|
|
14
|
+
*
|
|
15
|
+
* @module @deepseek-ai/dsh-client-ui-token-usage/TokenUsageDock
|
|
16
|
+
*/
|
|
17
|
+
import type { PropsLocale, PropsRuntime } from '@deepseek-ai/dsh-client-ui-slots';
|
|
18
|
+
import { NS } from './locales.ts';
|
|
19
|
+
/** Composed props of one composer-dock entry. */
|
|
20
|
+
export type TokenUsageDockProps = PropsRuntime<'conversation.composer.dock'> & PropsLocale<typeof NS>;
|
|
21
|
+
/**
|
|
22
|
+
* One conversation's classified token readout; renders nothing before its
|
|
23
|
+
* first billed step or before the pricing projection arrives.
|
|
24
|
+
* @param props - the slot's standard session shares and this namespace's locale seat.
|
|
25
|
+
* @returns the readout row, or null when the session has no billed usage.
|
|
26
|
+
*/
|
|
27
|
+
export declare function TokenUsageDock({ useProjection, t }: TokenUsageDockProps): import("react").JSX.Element | null;
|
|
28
|
+
//# sourceMappingURL=TokenUsageDock.d.ts.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token-usage readout plugin, browser half: one `conversation.composer.dock`
|
|
3
|
+
* entry that classifies the CURRENT conversation's durable token usage into
|
|
4
|
+
* its four disjoint billing buckets and prices them with the host-registered
|
|
5
|
+
* `tokenPricing` table.
|
|
6
|
+
*
|
|
7
|
+
* The plugin reads only framework hooks: `tokenUsage` (the exact whole-log
|
|
8
|
+
* buckets), `modelSelection` (the route that produced them), and
|
|
9
|
+
* `tokenPricing` (the deployment's price table). It owns no state, issues no
|
|
10
|
+
* RPC, and adds no model-visible content.
|
|
11
|
+
*
|
|
12
|
+
* @module @deepseek-ai/dsh-client-ui-token-usage
|
|
13
|
+
*/
|
|
14
|
+
import type { Context as ClientContext } from '@deepseek-ai/cordis';
|
|
15
|
+
import { type TokenUsageKey } from './locales.ts';
|
|
16
|
+
export type { TokenUsageKey } from './locales.ts';
|
|
17
|
+
declare module '@deepseek-ai/dsh-client-ui-slots' {
|
|
18
|
+
interface LocaleNamespaceMap {
|
|
19
|
+
/** The composer-dock token-usage readout's copy. */
|
|
20
|
+
tokenUsage: TokenUsageKey;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** Required services for the readout: slot contribution and its dictionaries. */
|
|
24
|
+
export declare const inject: string[];
|
|
25
|
+
/**
|
|
26
|
+
* Client plugin body: register the dictionaries and the composer-dock entry.
|
|
27
|
+
* @param ctx - client root context.
|
|
28
|
+
*/
|
|
29
|
+
export declare function apply(ctx: ClientContext): void;
|
|
30
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** `tokenUsage` namespace dictionaries: the composer-dock readout's copy. */
|
|
2
|
+
/** Dictionary namespace owned by this plugin. */
|
|
3
|
+
export declare const NS = "tokenUsage";
|
|
4
|
+
/** Simplified Chinese dictionary (the key-set source of truth). */
|
|
5
|
+
export declare const zh: {
|
|
6
|
+
'count.thousand': string;
|
|
7
|
+
'count.million': string;
|
|
8
|
+
'label.total': string;
|
|
9
|
+
'label.context': string;
|
|
10
|
+
'label.input': string;
|
|
11
|
+
'label.output': string;
|
|
12
|
+
'label.cacheRead': string;
|
|
13
|
+
'label.cacheWrite': string;
|
|
14
|
+
'context.usage': string;
|
|
15
|
+
'cost.estimate': string;
|
|
16
|
+
'backfill.notice': string;
|
|
17
|
+
'backfill.noticeNoPath': string;
|
|
18
|
+
'aria.readout': string;
|
|
19
|
+
'aria.context': string;
|
|
20
|
+
'aria.cost': string;
|
|
21
|
+
};
|
|
22
|
+
/** The `tokenUsage` namespace key union. */
|
|
23
|
+
export type TokenUsageKey = keyof typeof zh;
|
|
24
|
+
/** English dictionary, checked complete against the zh key set. */
|
|
25
|
+
export declare const en: {
|
|
26
|
+
'count.thousand': string;
|
|
27
|
+
'count.million': string;
|
|
28
|
+
'label.total': string;
|
|
29
|
+
'label.context': string;
|
|
30
|
+
'label.input': string;
|
|
31
|
+
'label.output': string;
|
|
32
|
+
'label.cacheRead': string;
|
|
33
|
+
'label.cacheWrite': string;
|
|
34
|
+
'context.usage': string;
|
|
35
|
+
'cost.estimate': string;
|
|
36
|
+
'backfill.notice': string;
|
|
37
|
+
'backfill.noticeNoPath': string;
|
|
38
|
+
'aria.readout': string;
|
|
39
|
+
'aria.context': string;
|
|
40
|
+
'aria.cost': string;
|
|
41
|
+
};
|
|
42
|
+
//# sourceMappingURL=locales.d.ts.map
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `/usage` report on two surfaces: a registered slash command for clients
|
|
3
|
+
* that execute commands, and a model-boundary fallback for clients that can only
|
|
4
|
+
* send messages.
|
|
5
|
+
*
|
|
6
|
+
* Neither surface calls the provider. The fallback returns a stream with no usage
|
|
7
|
+
* sample, so the loop logs the answer without settling anything: the token
|
|
8
|
+
* meter's context occupancy reads the newest settlement, and a synthetic zero
|
|
9
|
+
* would read as an empty context.
|
|
10
|
+
* @module @deepseek-ai/dsh-client-ui-token-usage/command
|
|
11
|
+
*/
|
|
12
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
13
|
+
/** The four disjoint billing buckets, as the token meter projects them. */
|
|
14
|
+
interface UsageState {
|
|
15
|
+
readonly uncachedInputTokens: number;
|
|
16
|
+
readonly cacheReadTokens: number;
|
|
17
|
+
readonly cacheWriteTokens: number;
|
|
18
|
+
readonly outputTokens: number;
|
|
19
|
+
}
|
|
20
|
+
/** Copy of the reply. Deployments that want another language override it. */
|
|
21
|
+
export interface UsageReportLabels {
|
|
22
|
+
readonly total: string;
|
|
23
|
+
readonly input: string;
|
|
24
|
+
readonly cacheRead: string;
|
|
25
|
+
readonly cacheWrite: string;
|
|
26
|
+
readonly output: string;
|
|
27
|
+
readonly context: string;
|
|
28
|
+
}
|
|
29
|
+
/** English defaults, matching the shipped dictionaries. */
|
|
30
|
+
export declare const DEFAULT_REPORT_LABELS: UsageReportLabels;
|
|
31
|
+
/** Figures the reply adds beside the bucket counts. */
|
|
32
|
+
export interface UsageReportFigures {
|
|
33
|
+
/** Estimated cost; omitted when the session's route has no configured price. */
|
|
34
|
+
readonly cost?: number;
|
|
35
|
+
/** Symbol prefixed to the cost figure. */
|
|
36
|
+
readonly currencySymbol: string;
|
|
37
|
+
/** Occupancy of the notified context window; omitted before either side is known. */
|
|
38
|
+
readonly context?: {
|
|
39
|
+
readonly used: number;
|
|
40
|
+
readonly window?: number;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* One-line report of the session's billed usage, cost estimate, and context occupancy.
|
|
45
|
+
* @param usage - the session's four billing buckets.
|
|
46
|
+
* @param labels - copy for each figure.
|
|
47
|
+
* @param figures - cost and context values the reply can add.
|
|
48
|
+
* @returns the reply text.
|
|
49
|
+
*/
|
|
50
|
+
export declare function renderUsageReport(usage: UsageState, labels: UsageReportLabels, figures: UsageReportFigures): string;
|
|
51
|
+
/**
|
|
52
|
+
* Register the usage command on the interactive command surface.
|
|
53
|
+
*
|
|
54
|
+
* The registration waits for the commands service through its own fiber, so a
|
|
55
|
+
* profile without a command surface (headless, ACP) keeps the readout and the
|
|
56
|
+
* backfill without holding this plugin pending.
|
|
57
|
+
* @param ctx - plugin context owning the command registration.
|
|
58
|
+
* @param options - registered command name and reply copy.
|
|
59
|
+
*/
|
|
60
|
+
export declare function registerUsageCommand(ctx: Context, options: {
|
|
61
|
+
readonly name: string;
|
|
62
|
+
readonly labels: UsageReportLabels;
|
|
63
|
+
}): void;
|
|
64
|
+
/**
|
|
65
|
+
* Answer the trigger at the model boundary, for clients that can only send
|
|
66
|
+
* messages rather than execute commands.
|
|
67
|
+
* @param ctx - plugin context owning the request middleware and the projections.
|
|
68
|
+
* @param options - trigger line and reply copy.
|
|
69
|
+
*/
|
|
70
|
+
export declare function installUsageCommand(ctx: Context, options: {
|
|
71
|
+
readonly trigger: string;
|
|
72
|
+
readonly labels: UsageReportLabels;
|
|
73
|
+
}): void;
|
|
74
|
+
export {};
|
|
75
|
+
//# sourceMappingURL=command.d.ts.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compact numeric formatting shared by the browser readout and the host's local
|
|
3
|
+
* `/usage` reply, so both render the same figure from the same rule.
|
|
4
|
+
* @module @deepseek-ai/dsh-client-ui-token-usage/format
|
|
5
|
+
*/
|
|
6
|
+
/** Suffix renderers for the compact scale. */
|
|
7
|
+
export interface CountUnits {
|
|
8
|
+
/** Render a thousands-scaled value: `12.3` becomes `12.3K`. */
|
|
9
|
+
readonly thousand: (scaled: string) => string;
|
|
10
|
+
/** Render a millions-scaled value: `1.2` becomes `1.2M`. */
|
|
11
|
+
readonly million: (scaled: string) => string;
|
|
12
|
+
}
|
|
13
|
+
/** Units for host-authored text, which has no locale seat to render through. */
|
|
14
|
+
export declare const ASCII_UNITS: CountUnits;
|
|
15
|
+
/**
|
|
16
|
+
* Compact token count: 517 / 12.3K / 1.2M.
|
|
17
|
+
* @param value - non-negative token count.
|
|
18
|
+
* @param units - suffix renderers, owned by the caller's locale or fixed text.
|
|
19
|
+
* @returns compact display string.
|
|
20
|
+
*/
|
|
21
|
+
export declare function formatTokens(value: number, units: CountUnits): string;
|
|
22
|
+
/**
|
|
23
|
+
* Cost with enough precision to stay non-zero on cheap sessions: two decimals
|
|
24
|
+
* once the figure is a yuan, three below it, and trailing-zero-trimmed
|
|
25
|
+
* micro-precision for sub-fen amounts.
|
|
26
|
+
* @param value - non-negative cost amount.
|
|
27
|
+
* @returns display amount that never rounds a real cost to bare zero.
|
|
28
|
+
*/
|
|
29
|
+
export declare function formatAmount(value: number): string;
|
|
30
|
+
//# sourceMappingURL=format.d.ts.map
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token-usage readout plugin, node half: registers the deployment's
|
|
3
|
+
* `tokenPricing` session projection so the browser readout can price durable
|
|
4
|
+
* usage, plus the `tokenCostBackfill` projection and the one-time history
|
|
5
|
+
* backfill that writes a `TOKEN_COST.md` per session working directory. It also
|
|
6
|
+
* registers the `/usage` command, which answers from session state without a
|
|
7
|
+
* provider request or a usage settlement. The plugin owns no session state.
|
|
8
|
+
*
|
|
9
|
+
* @module @deepseek-ai/dsh-client-ui-token-usage
|
|
10
|
+
*/
|
|
11
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
12
|
+
import z from '@deepseek-ai/schemastery';
|
|
13
|
+
import { type BackfillPrice, type PeakWindow } from './backfill.ts';
|
|
14
|
+
import { type ModelPrice } from './pricing.ts';
|
|
15
|
+
import { type UsageReportLabels } from './command.ts';
|
|
16
|
+
export type { BackfillPrice, PeakWindow, PricePair } from './backfill.ts';
|
|
17
|
+
export type { ModelPrice, PricingProjection, TokenCostBackfillProjection } from './pricing.ts';
|
|
18
|
+
/** First-install history backfill of the archived session corpus. */
|
|
19
|
+
export interface BackfillConfig {
|
|
20
|
+
/**
|
|
21
|
+
* Whether the plugin backfills archived sessions and publishes backfill
|
|
22
|
+
* coverage. `false` leaves the browser notice unregistered too.
|
|
23
|
+
* @default true
|
|
24
|
+
*/
|
|
25
|
+
enabled?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Prices in CNY per million tokens, keyed by model-id prefix (the longest
|
|
28
|
+
* matching prefix wins). Each pair is `[offPeak, peak]`. Defaults to the
|
|
29
|
+
* advisory DeepSeek table, whose off-peak column is half its peak column; a
|
|
30
|
+
* route absent from the table is reported as needing a price.
|
|
31
|
+
*/
|
|
32
|
+
pricing?: Record<string, BackfillPrice>;
|
|
33
|
+
/**
|
|
34
|
+
* Peak windows in Beijing time, replacing the defaults entirely. Defaults
|
|
35
|
+
* are the weekday windows 09:00–12:00 and 14:00–18:00.
|
|
36
|
+
*/
|
|
37
|
+
peakHours?: PeakWindow[];
|
|
38
|
+
}
|
|
39
|
+
/** The `/usage` command answered from session state instead of the provider. */
|
|
40
|
+
export interface CommandConfig {
|
|
41
|
+
/**
|
|
42
|
+
* Whether `/usage` is served at all: the registered command where the client
|
|
43
|
+
* executes commands, and the model-boundary fallback where it can only send a
|
|
44
|
+
* message. Neither calls the provider or settles usage, so it is on by default.
|
|
45
|
+
* @default true
|
|
46
|
+
*/
|
|
47
|
+
enabled?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Command name without the leading slash; the message fallback matches
|
|
50
|
+
* `/<name>` exactly.
|
|
51
|
+
* @default 'usage'
|
|
52
|
+
*/
|
|
53
|
+
name?: string;
|
|
54
|
+
/** Copy for each figure; deployments that want another language override it. */
|
|
55
|
+
labels?: Partial<UsageReportLabels>;
|
|
56
|
+
}
|
|
57
|
+
/** Token-usage readout runtime configuration. */
|
|
58
|
+
export interface Config {
|
|
59
|
+
/**
|
|
60
|
+
* Prices in CNY per million tokens, keyed by model-id prefix (the longest
|
|
61
|
+
* matching prefix wins). Defaults to the advisory DeepSeek official table;
|
|
62
|
+
* a model absent from the table shows tokens without a cost figure.
|
|
63
|
+
*/
|
|
64
|
+
pricing?: Record<string, ModelPrice>;
|
|
65
|
+
/** Symbol prefixed to the cost figure. */
|
|
66
|
+
currencySymbol?: string;
|
|
67
|
+
/** History backfill; the defaults run it against the advisory price table. */
|
|
68
|
+
backfill?: BackfillConfig;
|
|
69
|
+
/** Local trigger line that answers with the readout without calling the provider. */
|
|
70
|
+
command?: CommandConfig;
|
|
71
|
+
}
|
|
72
|
+
/** Validated readout configuration. */
|
|
73
|
+
export declare const Config: z<Config>;
|
|
74
|
+
/** The projection registry is the plugin's whole purpose; without it the fiber stays pending. */
|
|
75
|
+
export declare const inject: string[];
|
|
76
|
+
/**
|
|
77
|
+
* Register the deployment pricing table and, unless disabled, the backfill
|
|
78
|
+
* coverage projection plus the one-pass history backfill. Both registrations
|
|
79
|
+
* are effects on this plugin's fiber, so unloading removes the keys for every
|
|
80
|
+
* session; the backfill's own effect owns an abort controller that cancels the
|
|
81
|
+
* pass on unload, and a pass failure is logged rather than propagated.
|
|
82
|
+
* @param ctx - registrant context carrying the projection registry.
|
|
83
|
+
* @param config - validated plugin config (schema defaults applied).
|
|
84
|
+
*/
|
|
85
|
+
export declare function apply(ctx: Context, config?: Config): void;
|
|
86
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cost estimation for one conversation's durable token usage.
|
|
3
|
+
*
|
|
4
|
+
* The estimate converts the `tokenUsage` projection's four disjoint billing
|
|
5
|
+
* buckets into money with a deployment-supplied price table. It is a
|
|
6
|
+
* reference figure, not a bill: prices change, peak/off-peak rates exist, and
|
|
7
|
+
* the provider's own statement is authoritative.
|
|
8
|
+
*
|
|
9
|
+
* The table reaches the browser as the `tokenPricing` session projection
|
|
10
|
+
* because browser plugin rows carry no `config`; the host half registers the
|
|
11
|
+
* configured table and every session projects the same deployment constant.
|
|
12
|
+
* The module also owns the `tokenCostBackfill` projection's value — the
|
|
13
|
+
* browser notice's view of which routes the backfill price table missed.
|
|
14
|
+
*
|
|
15
|
+
* @module @deepseek-ai/dsh-client-ui-token-usage/pricing
|
|
16
|
+
*/
|
|
17
|
+
import type { TokenUsageProjection } from '@deepseek-ai/dsh-token-meter/client';
|
|
18
|
+
/** Price of one model route in CNY per million tokens. */
|
|
19
|
+
export interface ModelPrice {
|
|
20
|
+
/** Prompt tokens that miss the cache (and cache-write tokens). */
|
|
21
|
+
inputMiss: number;
|
|
22
|
+
/** Prompt tokens served from the cache. */
|
|
23
|
+
inputHit: number;
|
|
24
|
+
/** Completion tokens, reasoning included. */
|
|
25
|
+
output: number;
|
|
26
|
+
}
|
|
27
|
+
/** Deployment price table and currency delivered to the browser through `tokenPricing`. */
|
|
28
|
+
export interface PricingProjection {
|
|
29
|
+
/** Prices keyed by model-id prefix. */
|
|
30
|
+
readonly prices: Readonly<Record<string, ModelPrice>>;
|
|
31
|
+
/** Symbol prefixed to the cost figure. */
|
|
32
|
+
readonly currencySymbol: string;
|
|
33
|
+
}
|
|
34
|
+
/** What the historical-cost backfill found for one session, read by the composer notice. */
|
|
35
|
+
export interface TokenCostBackfillProjection {
|
|
36
|
+
/** Whether the session used at least one route the backfill price table prices. */
|
|
37
|
+
readonly hasPricedUsage: boolean;
|
|
38
|
+
/** Routes (`provider/model`) the price table does not cover, deduplicated in first-seen order. */
|
|
39
|
+
readonly unpricedRoutes: readonly string[];
|
|
40
|
+
/** Report path under the session working directory, or null when the header records none. */
|
|
41
|
+
readonly filePath: string | null;
|
|
42
|
+
}
|
|
43
|
+
declare module '@deepseek-ai/dsh-session-projection/types' {
|
|
44
|
+
interface SessionProjectionMap {
|
|
45
|
+
/** Deployment token price table for the composer readout's cost estimate. */
|
|
46
|
+
tokenPricing: PricingProjection;
|
|
47
|
+
/** Historical-cost backfill coverage for the composer pricing notice. */
|
|
48
|
+
tokenCostBackfill: TokenCostBackfillProjection;
|
|
49
|
+
}
|
|
50
|
+
interface SessionProjectionStateMap {
|
|
51
|
+
tokenPricing: PricingProjection;
|
|
52
|
+
tokenCostBackfill: TokenCostBackfillProjection;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Advisory DeepSeek official price table (CNY per million tokens, peak rate;
|
|
57
|
+
* off-peak official rates are half). Deployments override it through the
|
|
58
|
+
* plugin's `pricing` config; an unlisted model shows no cost figure rather
|
|
59
|
+
* than a guessed one.
|
|
60
|
+
*/
|
|
61
|
+
export declare const DEFAULT_PRICING: Readonly<Record<string, ModelPrice>>;
|
|
62
|
+
/**
|
|
63
|
+
* Resolve a model id against a price table by longest matching prefix.
|
|
64
|
+
*
|
|
65
|
+
* Prefixes, not exact ids, because adapters append route suffixes (vision,
|
|
66
|
+
* dated snapshots) that share the base model's price. The value type is open
|
|
67
|
+
* because the readout's per-token price and the backfill's peak/off-peak pairs
|
|
68
|
+
* both key off the same prefix rule.
|
|
69
|
+
* @param model - the session's last-used model id.
|
|
70
|
+
* @param table - prices keyed by model-id prefix.
|
|
71
|
+
* @returns the matched entry, or undefined when no prefix matches.
|
|
72
|
+
*/
|
|
73
|
+
export declare function priceForModel<T>(model: string, table: Readonly<Record<string, T>>): T | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* Estimate one conversation's cost in CNY.
|
|
76
|
+
*
|
|
77
|
+
* Prompt tokens that missed the cache (uncached input plus cache writes) bill
|
|
78
|
+
* at the miss rate; cache-hit prompt tokens bill at the hit rate; completion
|
|
79
|
+
* tokens bill at the output rate. The result is exact arithmetic over the
|
|
80
|
+
* projected buckets — the estimate error is entirely the price table's.
|
|
81
|
+
* @param usage - the session's four disjoint billing buckets.
|
|
82
|
+
* @param price - the matched model price.
|
|
83
|
+
* @returns estimated cost in CNY.
|
|
84
|
+
*/
|
|
85
|
+
export declare function estimateCostCny(usage: TokenUsageProjection, price: ModelPrice): number;
|
|
86
|
+
/**
|
|
87
|
+
* Total billed tokens across the projection's four disjoint buckets.
|
|
88
|
+
* @param usage - the session's four disjoint billing buckets.
|
|
89
|
+
* @returns prompt-side billed input plus output.
|
|
90
|
+
*/
|
|
91
|
+
export declare function billedTokens(usage: TokenUsageProjection): number;
|
|
92
|
+
//# sourceMappingURL=pricing.d.ts.map
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `tokenPricing` and `tokenCostBackfill` session-projection units: the
|
|
3
|
+
* deployment's token price table plus the coverage its history backfill has
|
|
4
|
+
* over this session's routes, projected to the browser so the composer readout
|
|
5
|
+
* can price durable usage and point at the routes it cannot price.
|
|
6
|
+
*
|
|
7
|
+
* Browser plugin rows carry no `config`, so a host-side projection is the
|
|
8
|
+
* delivery path for a client-side readout's pricing. The pricing unit is a
|
|
9
|
+
* constant: it ignores every event, returns the same state reference, and
|
|
10
|
+
* therefore publishes one value per session. The backfill unit folds
|
|
11
|
+
* `request/header` events into the set of routes the backfill price table
|
|
12
|
+
* misses, and derives the report path from the session header's working
|
|
13
|
+
* directory.
|
|
14
|
+
*
|
|
15
|
+
* @module @deepseek-ai/dsh-client-ui-token-usage/projection
|
|
16
|
+
*/
|
|
17
|
+
import { z } from 'zod';
|
|
18
|
+
import type { ProjectionDefinition } from '@deepseek-ai/dsh-session-projection';
|
|
19
|
+
import { type BackfillPrice } from './backfill.ts';
|
|
20
|
+
import { type ModelPrice, type PricingProjection, type TokenCostBackfillProjection } from './pricing.ts';
|
|
21
|
+
/**
|
|
22
|
+
* Build the pricing projection unit for one resolved deployment table.
|
|
23
|
+
* @param pricing - prices keyed by model-id prefix.
|
|
24
|
+
* @param currencySymbol - symbol prefixed to the readout's cost figure.
|
|
25
|
+
* @returns the unit definition the host registers on `ctx.sessionProjections`.
|
|
26
|
+
*/
|
|
27
|
+
export declare function createPricingProjectionDefinition(pricing: Readonly<Record<string, ModelPrice>>, currencySymbol: string): Omit<ProjectionDefinition<'tokenPricing', PricingProjection>, 'wire'> & {
|
|
28
|
+
/** This unit always wires a browser view, so registration may rely on it. */
|
|
29
|
+
wire: {
|
|
30
|
+
viewSchema: z.ZodType<PricingProjection>;
|
|
31
|
+
view: (state: PricingProjection) => PricingProjection;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Build the backfill-coverage projection unit for one resolved price table.
|
|
36
|
+
*
|
|
37
|
+
* Only `request/header` can introduce a route, so every other event returns the
|
|
38
|
+
* same state reference and publishes nothing. The report path comes from the
|
|
39
|
+
* session header rather than from the running process: a session created in
|
|
40
|
+
* another directory must point at that directory's file, and a header without
|
|
41
|
+
* a working directory points nowhere.
|
|
42
|
+
* @param pricing - backfill prices keyed by model-id prefix.
|
|
43
|
+
* @returns the unit definition the host registers on `ctx.sessionProjections`.
|
|
44
|
+
*/
|
|
45
|
+
export declare function createBackfillProjectionDefinition(pricing: Readonly<Record<string, BackfillPrice>>): Omit<ProjectionDefinition<'tokenCostBackfill', TokenCostBackfillProjection>, 'wire'> & {
|
|
46
|
+
/** This unit always wires a browser view, so registration may rely on it. */
|
|
47
|
+
wire: {
|
|
48
|
+
viewSchema: z.ZodType<TokenCostBackfillProjection>;
|
|
49
|
+
view: (state: TokenCostBackfillProjection) => TokenCostBackfillProjection;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
//# sourceMappingURL=projection.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jerry_chzy/dsh-token-usage",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"description": "Composer-dock token-usage readout: the session's four disjoint billing buckets plus a configurable cost estimate",
|
|
4
|
+
"version": "0.1.1",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"types": "lib/types/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/types/index.d.ts",
|
|
11
|
+
"default": "./lib/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./client": {
|
|
14
|
+
"types": "./lib/types/client/index.d.ts",
|
|
15
|
+
"default": "./lib/client.js"
|
|
16
|
+
},
|
|
17
|
+
"./src/*": "./src/*",
|
|
18
|
+
"./package.json": "./package.json"
|
|
19
|
+
},
|
|
20
|
+
"dsh": {
|
|
21
|
+
"bundle": {
|
|
22
|
+
"patch": "./cordis.patch.yml"
|
|
23
|
+
},
|
|
24
|
+
"client": {
|
|
25
|
+
"inject": [
|
|
26
|
+
"@deepseek-ai/dsh-api-session-controller",
|
|
27
|
+
"@deepseek-ai/dsh-client-locale",
|
|
28
|
+
"@deepseek-ai/dsh-client-ui-conversation",
|
|
29
|
+
"@deepseek-ai/dsh-client-ui-session"
|
|
30
|
+
],
|
|
31
|
+
"platform": "web"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/deepseek-ai/deepseek-harness.git",
|
|
38
|
+
"directory": "packages/client/ui-token-usage"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@deepseek-ai/cordis": "^4.0.2"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@deepseek-ai/schemastery": "^3.18.2",
|
|
48
|
+
"zod": "^4.4.3"
|
|
49
|
+
},
|
|
50
|
+
"files": [
|
|
51
|
+
"cordis.patch.yml",
|
|
52
|
+
"lib/index.js",
|
|
53
|
+
"lib/client.js",
|
|
54
|
+
"lib/types/**/*.d.ts"
|
|
55
|
+
]
|
|
6
56
|
}
|