dsh-budget 0.1.0
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 +26 -0
- package/LICENSE +201 -0
- package/README.es.md +140 -0
- package/README.hi.md +140 -0
- package/README.md +140 -0
- package/README.pt.md +140 -0
- package/README.zh.md +140 -0
- package/SECURITY.md +37 -0
- package/THIRD_PARTY_NOTICES.md +23 -0
- package/cordis.patch.yml +73 -0
- package/lib/client.js +4893 -0
- package/lib/client.js.map +1 -0
- package/lib/index.js +1225 -0
- package/lib/typert.host.js +26 -0
- package/lib/types/aggregate/usage.d.ts +112 -0
- package/lib/types/aggregate/usage.d.ts.map +1 -0
- package/lib/types/client/BudgetTab.d.ts +18 -0
- package/lib/types/client/BudgetTab.d.ts.map +1 -0
- package/lib/types/client/index.d.ts +35 -0
- package/lib/types/client/index.d.ts.map +1 -0
- package/lib/types/client/locales.d.ts +42 -0
- package/lib/types/client/locales.d.ts.map +1 -0
- package/lib/types/client/present.d.ts +19 -0
- package/lib/types/client/present.d.ts.map +1 -0
- package/lib/types/client/remote.d.ts +270 -0
- package/lib/types/client/remote.d.ts.map +1 -0
- package/lib/types/client/styles.d.ts +12 -0
- package/lib/types/client/styles.d.ts.map +1 -0
- package/lib/types/command.d.ts +51 -0
- package/lib/types/command.d.ts.map +1 -0
- package/lib/types/config.d.ts +132 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/estimate/carbon.d.ts +128 -0
- package/lib/types/estimate/carbon.d.ts.map +1 -0
- package/lib/types/estimate/cost.d.ts +71 -0
- package/lib/types/estimate/cost.d.ts.map +1 -0
- package/lib/types/estimate/latency-stats.d.ts +111 -0
- package/lib/types/estimate/latency-stats.d.ts.map +1 -0
- package/lib/types/estimate/models.d.ts +29 -0
- package/lib/types/estimate/models.d.ts.map +1 -0
- package/lib/types/estimate/prices.d.ts +85 -0
- package/lib/types/estimate/prices.d.ts.map +1 -0
- package/lib/types/estimate/sanitize.d.ts +56 -0
- package/lib/types/estimate/sanitize.d.ts.map +1 -0
- package/lib/types/events.d.ts +57 -0
- package/lib/types/events.d.ts.map +1 -0
- package/lib/types/governance.d.ts +67 -0
- package/lib/types/governance.d.ts.map +1 -0
- package/lib/types/index.d.ts +50 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/service.d.ts +67 -0
- package/lib/types/service.d.ts.map +1 -0
- package/lib/types/typert.host.d.ts +258 -0
- package/lib/types/typert.host.d.ts.map +1 -0
- package/lib/types/wire.d.ts +629 -0
- package/lib/types/wire.d.ts.map +1 -0
- package/lib/wire-DVO8yw7L.js +4219 -0
- package/package.json +164 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Config schema and resolution for `dsh-budget`. Every tunable is a validated
|
|
3
|
+
* {@link Config} field changeable from cordis.yml; `resolveConfig` re-judges
|
|
4
|
+
* every default and bound so programmatic construction that bypasses
|
|
5
|
+
* Schemastery normalization still fails loud (the explicit-resolve contract).
|
|
6
|
+
*
|
|
7
|
+
* @module dsh-budget/config
|
|
8
|
+
*/
|
|
9
|
+
import z from '@deepseek-ai/schemastery';
|
|
10
|
+
/** Display currency: code, units per USD, and decimal places. */
|
|
11
|
+
export interface CurrencyConfig {
|
|
12
|
+
code?: string;
|
|
13
|
+
rate?: number;
|
|
14
|
+
decimals?: number;
|
|
15
|
+
}
|
|
16
|
+
/** Carbon footprint estimation. */
|
|
17
|
+
export interface CarbonConfig {
|
|
18
|
+
enabled?: boolean;
|
|
19
|
+
region?: 'global' | 'us' | 'eu' | 'china' | 'india' | 'uk' | 'france' | 'iceland';
|
|
20
|
+
pue?: number;
|
|
21
|
+
energyKwhPerToken?: number;
|
|
22
|
+
}
|
|
23
|
+
/** Per-model latency statistics. */
|
|
24
|
+
export interface LatencyConfig {
|
|
25
|
+
enabled?: boolean;
|
|
26
|
+
windowSize?: number;
|
|
27
|
+
}
|
|
28
|
+
/** Budget caps per scope; undefined = unlimited. */
|
|
29
|
+
export interface BudgetsConfig {
|
|
30
|
+
session?: number;
|
|
31
|
+
daily?: number;
|
|
32
|
+
monthly?: number;
|
|
33
|
+
}
|
|
34
|
+
/** Raw plugin config — every field optional; {@link resolveConfig} supplies the defaults. */
|
|
35
|
+
export interface Config {
|
|
36
|
+
/** Per-model USD prices per 1M tokens, merged over the built-in table. */
|
|
37
|
+
prices?: Record<string, {
|
|
38
|
+
input: number;
|
|
39
|
+
output: number;
|
|
40
|
+
cacheRead?: number;
|
|
41
|
+
cacheWrite?: number;
|
|
42
|
+
}>;
|
|
43
|
+
/** Fallback price for models absent from both tables. */
|
|
44
|
+
defaultPrice?: {
|
|
45
|
+
input: number;
|
|
46
|
+
output: number;
|
|
47
|
+
cacheRead?: number;
|
|
48
|
+
cacheWrite?: number;
|
|
49
|
+
};
|
|
50
|
+
/** Budget caps in USD per scope; omit a scope for unlimited. */
|
|
51
|
+
budgets?: BudgetsConfig;
|
|
52
|
+
/** Alert once usage reaches this fraction of a cap (0..1). */
|
|
53
|
+
warnRatio?: number;
|
|
54
|
+
/** Behavior after a cap is crossed: alert | block | degrade. */
|
|
55
|
+
overLimit?: 'alert' | 'block' | 'degrade';
|
|
56
|
+
/** Degradation map: model id -> cheaper model id of the SAME provider. */
|
|
57
|
+
degradation?: Record<string, string>;
|
|
58
|
+
/** Optional webhook URL for threshold alerts (POST JSON). */
|
|
59
|
+
webhookUrl?: string;
|
|
60
|
+
/** Webhook request timeout in milliseconds. */
|
|
61
|
+
webhookTimeoutMs?: number;
|
|
62
|
+
/** Master switch for threshold alerts. */
|
|
63
|
+
alertsEnabled?: boolean;
|
|
64
|
+
/** Minimum milliseconds between two alerts of the same scope. */
|
|
65
|
+
alertCooldownMs?: number;
|
|
66
|
+
/** Browser desktop notifications for new alerts. */
|
|
67
|
+
desktopNotifications?: boolean;
|
|
68
|
+
/** Settings budget tab polling interval in milliseconds. */
|
|
69
|
+
refreshIntervalMs?: number;
|
|
70
|
+
/** Carbon footprint estimation. */
|
|
71
|
+
carbon?: CarbonConfig;
|
|
72
|
+
/** Per-model latency statistics. */
|
|
73
|
+
latency?: LatencyConfig;
|
|
74
|
+
/** Display currency. */
|
|
75
|
+
currency?: CurrencyConfig;
|
|
76
|
+
/** /budget command output language: en | zh. */
|
|
77
|
+
outputLanguage?: 'en' | 'zh';
|
|
78
|
+
/** Days of per-day usage history kept in the panel snapshot. */
|
|
79
|
+
historyDays?: number;
|
|
80
|
+
}
|
|
81
|
+
/** Fully resolved config. */
|
|
82
|
+
export interface ResolvedConfig {
|
|
83
|
+
readonly prices: Record<string, {
|
|
84
|
+
input: number;
|
|
85
|
+
output: number;
|
|
86
|
+
cacheRead?: number;
|
|
87
|
+
cacheWrite?: number;
|
|
88
|
+
}>;
|
|
89
|
+
readonly defaultPrice: {
|
|
90
|
+
input: number;
|
|
91
|
+
output: number;
|
|
92
|
+
cacheRead?: number;
|
|
93
|
+
cacheWrite?: number;
|
|
94
|
+
};
|
|
95
|
+
readonly budgets: BudgetsConfig;
|
|
96
|
+
readonly warnRatio: number;
|
|
97
|
+
readonly overLimit: 'alert' | 'block' | 'degrade';
|
|
98
|
+
readonly degradation: Record<string, string>;
|
|
99
|
+
readonly webhookUrl: string | undefined;
|
|
100
|
+
readonly webhookTimeoutMs: number;
|
|
101
|
+
readonly alertsEnabled: boolean;
|
|
102
|
+
readonly alertCooldownMs: number;
|
|
103
|
+
readonly desktopNotifications: boolean;
|
|
104
|
+
readonly refreshIntervalMs: number;
|
|
105
|
+
readonly carbon: {
|
|
106
|
+
enabled: boolean;
|
|
107
|
+
region: string;
|
|
108
|
+
pue: number;
|
|
109
|
+
energyKwhPerToken: number;
|
|
110
|
+
};
|
|
111
|
+
readonly latency: {
|
|
112
|
+
enabled: boolean;
|
|
113
|
+
windowSize: number;
|
|
114
|
+
};
|
|
115
|
+
readonly currency: {
|
|
116
|
+
code: string;
|
|
117
|
+
rate: number;
|
|
118
|
+
decimals: number;
|
|
119
|
+
};
|
|
120
|
+
readonly outputLanguage: 'en' | 'zh';
|
|
121
|
+
readonly historyDays: number;
|
|
122
|
+
}
|
|
123
|
+
/** Schemastery schema: the loader validates and fills defaults before `apply`. */
|
|
124
|
+
export declare const Config: z<Config>;
|
|
125
|
+
/**
|
|
126
|
+
* Resolve raw config to the runtime policy, re-validating defaults and bounds.
|
|
127
|
+
*
|
|
128
|
+
* @param config - raw loader config; `undefined` for a bare row.
|
|
129
|
+
* @returns the frozen resolved config.
|
|
130
|
+
*/
|
|
131
|
+
export declare function resolveConfig(config: Config | undefined): ResolvedConfig;
|
|
132
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,CAAC,MAAM,0BAA0B,CAAA;AAExC,iEAAiE;AACjE,MAAM,WAAW,cAAc;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED,mCAAmC;AACnC,MAAM,WAAW,YAAY;IAC3B,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,MAAM,CAAC,EAAE,QAAQ,GAAG,IAAI,GAAG,IAAI,GAAG,OAAO,GAAG,OAAO,GAAG,IAAI,GAAG,QAAQ,GAAG,SAAS,CAAA;IACjF,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,iBAAiB,CAAC,EAAE,MAAM,CAAA;CAC3B;AAED,oCAAoC;AACpC,MAAM,WAAW,aAAa;IAC5B,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,oDAAoD;AACpD,MAAM,WAAW,aAAa;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB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E,MAAM,CAAA;KAAE,CAAA;IAC1D,QAAQ,CAAC,QAAQ,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAA;IACnE,QAAQ,CAAC,cAAc,EAAE,IAAI,GAAG,IAAI,CAAA;IACpC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;CAC7B;AAKD,kFAAkF;AAClF,eAAO,MAAM,MAAM,EAAE,CAAC,CAAC,MAAM,CA4C3B,CAAA;AAOF;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,cAAc,CAgGxE"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Port of the AI-Carbon-Footprint-Calculator data and formulas
|
|
3
|
+
* (`upstream/AI-Carbon-Footprint-Calculator/src/ai_carbon_footprint/data.py`
|
|
4
|
+
* + `core.py` + `comparisons.py`, commit d8d52b5, Apache-2.0): GPU TDP table,
|
|
5
|
+
* regional grid carbon intensity (kg CO2e/kWh), PUE constants, the
|
|
6
|
+
* GPU-hours → energy → CO2 formula, and the equivalence comparisons.
|
|
7
|
+
*
|
|
8
|
+
* The plugin bridges LLM tokens onto this model through
|
|
9
|
+
* {@link tokenCarbon}: IT energy = tokens × kWh/token, total energy = IT × PUE,
|
|
10
|
+
* CO2 = total energy × regional intensity. `energyKwhPerToken` is configurable
|
|
11
|
+
* (`config.carbon.energyKwhPerToken`); its default derivation is documented in
|
|
12
|
+
* cordis.patch.yml.
|
|
13
|
+
*
|
|
14
|
+
* @module dsh-budget/estimate/carbon
|
|
15
|
+
*/
|
|
16
|
+
/** GPU TDP specifications, verbatim from upstream data.py. */
|
|
17
|
+
export interface GpuSpec {
|
|
18
|
+
/** Thermal design power in watts. */
|
|
19
|
+
tdp: number;
|
|
20
|
+
/** Display name. */
|
|
21
|
+
name: string;
|
|
22
|
+
/** Upstream category tag. */
|
|
23
|
+
category: string;
|
|
24
|
+
}
|
|
25
|
+
/** The upstream GPU table (17 entries), verbatim. */
|
|
26
|
+
export declare const GPU_SPECS: Readonly<Record<string, GpuSpec>>;
|
|
27
|
+
/** Regional grid carbon intensity in kg CO2e per kWh, verbatim from upstream. */
|
|
28
|
+
export declare const CARBON_INTENSITY: Readonly<Record<string, number>>;
|
|
29
|
+
/** Upstream default power usage effectiveness. */
|
|
30
|
+
export declare const DEFAULT_PUE = 1.58;
|
|
31
|
+
/** Upstream "efficient" PUE reference. */
|
|
32
|
+
export declare const EFFICIENT_PUE = 1.2;
|
|
33
|
+
/** Upstream "hyperscale" PUE reference. */
|
|
34
|
+
export declare const HYPERSCALE_PUE = 1.1;
|
|
35
|
+
/** One GPU-hours workload's carbon result. */
|
|
36
|
+
export interface CarbonResult {
|
|
37
|
+
/** GPU display name. */
|
|
38
|
+
gpuName: string;
|
|
39
|
+
/** GPU table key. */
|
|
40
|
+
gpuModel: string;
|
|
41
|
+
/** GPU count. */
|
|
42
|
+
numGpus: number;
|
|
43
|
+
/** Runtime in hours. */
|
|
44
|
+
hours: number;
|
|
45
|
+
/** Utilization factor (0..1]. */
|
|
46
|
+
utilization: number;
|
|
47
|
+
/** Effective PUE. */
|
|
48
|
+
pue: number;
|
|
49
|
+
/** Electricity region key. */
|
|
50
|
+
region: string;
|
|
51
|
+
/** IT energy in kWh (before PUE). */
|
|
52
|
+
energyKwh: number;
|
|
53
|
+
/** Total energy in kWh (after PUE). */
|
|
54
|
+
totalEnergyKwh: number;
|
|
55
|
+
/** Emissions in kg CO2e. */
|
|
56
|
+
co2Kg: number;
|
|
57
|
+
/** Regional intensity used, kg CO2e/kWh. */
|
|
58
|
+
carbonIntensity: number;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Compute the carbon footprint of a GPU-hours workload.
|
|
62
|
+
*
|
|
63
|
+
* @param gpuModel - GPU table key.
|
|
64
|
+
* @param hours - runtime in hours.
|
|
65
|
+
* @param numGpus - GPU count (default 1).
|
|
66
|
+
* @param pue - power usage effectiveness (default {@link DEFAULT_PUE}).
|
|
67
|
+
* @param region - electricity region key (default `global`).
|
|
68
|
+
* @param utilization - GPU utilization in (0, 1] (default 1).
|
|
69
|
+
* @returns the calculation result.
|
|
70
|
+
* @throws on an unknown GPU, an unknown region, or an out-of-range utilization.
|
|
71
|
+
*/
|
|
72
|
+
export declare function calculateCarbonFootprint(gpuModel: string, hours: number, numGpus?: number, pue?: number, region?: string, utilization?: number): CarbonResult;
|
|
73
|
+
/** One real-world equivalence reference (upstream `co2_kg` comparison table). */
|
|
74
|
+
export interface CarbonComparison {
|
|
75
|
+
/** Human-readable name. */
|
|
76
|
+
name: string;
|
|
77
|
+
/** Emoji marker from the upstream table. */
|
|
78
|
+
emoji: string;
|
|
79
|
+
/** Reference emissions in kg CO2e. */
|
|
80
|
+
co2Kg: number;
|
|
81
|
+
}
|
|
82
|
+
/** The upstream comparison references, verbatim from comparisons.py. */
|
|
83
|
+
export declare const COMPARISONS: Readonly<Record<string, CarbonComparison>>;
|
|
84
|
+
/** One computed equivalence: how many reference units a CO2 amount equals. */
|
|
85
|
+
export interface CarbonEquivalence {
|
|
86
|
+
/** Reference name. */
|
|
87
|
+
name: string;
|
|
88
|
+
/** Reference emoji. */
|
|
89
|
+
emoji: string;
|
|
90
|
+
/** `co2Kg / reference`. */
|
|
91
|
+
equivalent: number;
|
|
92
|
+
/** Reference emissions in kg CO2e. */
|
|
93
|
+
unitCo2: number;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Pick the top-3 most relevant real-world equivalences for an emissions
|
|
97
|
+
* amount, upstream style: only references within 0.1×–10× qualify, sorted by
|
|
98
|
+
* closeness to 1×.
|
|
99
|
+
*
|
|
100
|
+
* @param co2Kg - emissions in kg CO2e.
|
|
101
|
+
* @returns up to three equivalences, closest first.
|
|
102
|
+
*/
|
|
103
|
+
export declare function getComparisons(co2Kg: number): CarbonEquivalence[];
|
|
104
|
+
/** Token-bridge carbon result for one token volume. */
|
|
105
|
+
export interface TokenCarbonResult {
|
|
106
|
+
/** IT energy in kWh (tokens × kWh/token). */
|
|
107
|
+
energyKwh: number;
|
|
108
|
+
/** Total energy in kWh (IT × PUE). */
|
|
109
|
+
totalEnergyKwh: number;
|
|
110
|
+
/** Emissions in kg CO2e. */
|
|
111
|
+
co2Kg: number;
|
|
112
|
+
/** Regional intensity used, kg CO2e/kWh. */
|
|
113
|
+
carbonIntensity: number;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Estimate the carbon footprint of a token volume (the plugin's token→carbon
|
|
117
|
+
* bridge): energy = tokens × kWh/token, total = energy × PUE,
|
|
118
|
+
* CO2 = total × regional intensity.
|
|
119
|
+
*
|
|
120
|
+
* @param tokens - total tokens processed (all buckets).
|
|
121
|
+
* @param energyKwhPerToken - IT energy per token in kWh.
|
|
122
|
+
* @param pue - power usage effectiveness.
|
|
123
|
+
* @param region - electricity region key.
|
|
124
|
+
* @returns the estimation result.
|
|
125
|
+
* @throws on an unknown region.
|
|
126
|
+
*/
|
|
127
|
+
export declare function tokenCarbon(tokens: number, energyKwhPerToken: number, pue: number, region: string): TokenCarbonResult;
|
|
128
|
+
//# sourceMappingURL=carbon.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"carbon.d.ts","sourceRoot":"","sources":["../../../src/estimate/carbon.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,8DAA8D;AAC9D,MAAM,WAAW,OAAO;IACtB,qCAAqC;IACrC,GAAG,EAAE,MAAM,CAAA;IACX,oBAAoB;IACpB,IAAI,EAAE,MAAM,CAAA;IACZ,6BAA6B;IAC7B,QAAQ,EAAE,MAAM,CAAA;CACjB;AAED,qDAAqD;AACrD,eAAO,MAAM,SAAS,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAkBtD,CAAA;AAEF,iFAAiF;AACjF,eAAO,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,CAAC,CAMjE,CAAA;AAEF,8EAA8E;AAC9E,MAAM,WAAW,iBAAiB;IAChC,sBAAsB;IACtB,IAAI,EAAE,MAAM,CAAA;IACZ,uBAAuB;IACvB,KAAK,EAAE,MAAM,CAAA;IACb,2BAA2B;IAC3B,UAAU,EAAE,MAAM,CAAA;IAClB,sCAAsC;IACtC,OAAO,EAAE,MAAM,CAAA;CAChB;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,iBAAiB,EAAE,CAUjE;AAED,uDAAuD;AACvD,MAAM,WAAW,iBAAiB;IAChC,6CAA6C;IAC7C,SAAS,EAAE,MAAM,CAAA;IACjB,sCAAsC;IACtC,cAAc,EAAE,MAAM,CAAA;IACtB,4BAA4B;IAC5B,KAAK,EAAE,MAAM,CAAA;IACb,4CAA4C;IAC5C,eAAe,EAAE,MAAM,CAAA;CACxB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,MAAM,EACd,iBAAiB,EAAE,MAAM,EACzB,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,MAAM,GACb,iBAAiB,CAMnB"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Port of the LLM-Cost-Estimator-CN cost formulas
|
|
3
|
+
* (`upstream/LLM-Cost-Estimator-CN/src/llm_cost_estimator_cn/core.py` +
|
|
4
|
+
* `utils.py`, commit aa6cc2f, Apache-2.0): per-1k-token pricing, cheapest-first
|
|
5
|
+
* sorting, the ¥ currency formatter and the percentage-diff helper.
|
|
6
|
+
*
|
|
7
|
+
* @module dsh-budget/estimate/cost
|
|
8
|
+
*/
|
|
9
|
+
/** A price entry used by {@link calculateCosts}. */
|
|
10
|
+
export interface CostPriceEntry {
|
|
11
|
+
/** Input price per 1000 tokens. */
|
|
12
|
+
inputPricePer1k: number;
|
|
13
|
+
/** Output price per 1000 tokens. */
|
|
14
|
+
outputPricePer1k: number;
|
|
15
|
+
}
|
|
16
|
+
/** One model's cost estimate, as produced by the upstream calculator. */
|
|
17
|
+
export interface CostEstimate {
|
|
18
|
+
/** Model table key. */
|
|
19
|
+
modelId: string;
|
|
20
|
+
/** Provider display name. */
|
|
21
|
+
provider: string;
|
|
22
|
+
/** Model display name. */
|
|
23
|
+
modelName: string;
|
|
24
|
+
/** Input cost for the requested tokens. */
|
|
25
|
+
inputCost: number;
|
|
26
|
+
/** Output cost for the requested tokens. */
|
|
27
|
+
outputCost: number;
|
|
28
|
+
/** `inputCost + outputCost`. */
|
|
29
|
+
totalCost: number;
|
|
30
|
+
}
|
|
31
|
+
/** Entry shape the upstream calculator iterates (id → price + display fields). */
|
|
32
|
+
export interface CostTableEntry extends CostPriceEntry {
|
|
33
|
+
/** Provider display name. */
|
|
34
|
+
provider: string;
|
|
35
|
+
/** Model display name. */
|
|
36
|
+
name: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Calculate every model's cost for the given token counts, cheapest first.
|
|
40
|
+
*
|
|
41
|
+
* @param models - id → price/display entry table.
|
|
42
|
+
* @param inputTokens - number of input tokens.
|
|
43
|
+
* @param outputTokens - number of output tokens.
|
|
44
|
+
* @returns the estimates sorted by ascending `totalCost`.
|
|
45
|
+
*/
|
|
46
|
+
export declare function calculateCosts(models: Readonly<Record<string, CostTableEntry>>, inputTokens: number, outputTokens: number): CostEstimate[];
|
|
47
|
+
/**
|
|
48
|
+
* Estimate every built-in upstream model's cost, cheapest first.
|
|
49
|
+
*
|
|
50
|
+
* @param inputTokens - number of input tokens.
|
|
51
|
+
* @param outputTokens - number of output tokens.
|
|
52
|
+
* @returns the estimates sorted by ascending `totalCost`.
|
|
53
|
+
*/
|
|
54
|
+
export declare function calculateUpstreamCosts(inputTokens: number, outputTokens: number): CostEstimate[];
|
|
55
|
+
/**
|
|
56
|
+
* Format an amount with the upstream ¥ style (4 decimal places).
|
|
57
|
+
*
|
|
58
|
+
* @param amount - numeric amount.
|
|
59
|
+
* @returns `¥` plus the amount fixed to 4 decimals.
|
|
60
|
+
*/
|
|
61
|
+
export declare function formatCurrencyCny(amount: number): string;
|
|
62
|
+
/**
|
|
63
|
+
* Compute the percentage difference against a baseline, upstream style.
|
|
64
|
+
*
|
|
65
|
+
* @param value - the value to compare.
|
|
66
|
+
* @param baseline - the reference value.
|
|
67
|
+
* @returns `N/A` for a zero baseline, a literal `基准` marker when equal,
|
|
68
|
+
* otherwise a signed percentage with one decimal.
|
|
69
|
+
*/
|
|
70
|
+
export declare function percentageDiff(value: number, baseline: number): string;
|
|
71
|
+
//# sourceMappingURL=cost.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cost.d.ts","sourceRoot":"","sources":["../../../src/estimate/cost.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,oDAAoD;AACpD,MAAM,WAAW,cAAc;IAC7B,mCAAmC;IACnC,eAAe,EAAE,MAAM,CAAA;IACvB,oCAAoC;IACpC,gBAAgB,EAAE,MAAM,CAAA;CACzB;AAED,yEAAyE;AACzE,MAAM,WAAW,YAAY;IAC3B,uBAAuB;IACvB,OAAO,EAAE,MAAM,CAAA;IACf,6BAA6B;IAC7B,QAAQ,EAAE,MAAM,CAAA;IAChB,0BAA0B;IAC1B,SAAS,EAAE,MAAM,CAAA;IACjB,2CAA2C;IAC3C,SAAS,EAAE,MAAM,CAAA;IACjB,4CAA4C;IAC5C,UAAU,EAAE,MAAM,CAAA;IAClB,gCAAgC;IAChC,SAAS,EAAE,MAAM,CAAA;CAClB;AAED,kFAAkF;AAClF,MAAM,WAAW,cAAe,SAAQ,cAAc;IACpD,6BAA6B;IAC7B,QAAQ,EAAE,MAAM,CAAA;IAChB,0BAA0B;IAC1B,IAAI,EAAE,MAAM,CAAA;CACb;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAC5B,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC,EAChD,WAAW,EAAE,MAAM,EACnB,YAAY,EAAE,MAAM,GACnB,YAAY,EAAE,CAgBhB;AAED;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,WAAW,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,YAAY,EAAE,CAEhG;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAExD;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAKtE"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Port of the Mode-Latency-Benchmark provider table and request builders
|
|
3
|
+
* (`upstream/Mode-Latency-Benchmark/src/model_latency_benchmark/core.py`,
|
|
4
|
+
* commit 8123838, Apache-2.0), plus the percentile statistics the plugin
|
|
5
|
+
* aggregates from measured call durations.
|
|
6
|
+
*
|
|
7
|
+
* The upstream tool probes providers over HTTP; the plugin reuses its
|
|
8
|
+
* `BenchmarkResult` vocabulary (ttft / total time / tokens / error) and adds
|
|
9
|
+
* `latencyStats` for per-model percentile aggregation over the llm/stream
|
|
10
|
+
* wrapper.
|
|
11
|
+
*
|
|
12
|
+
* @module dsh-budget/estimate/latency-stats
|
|
13
|
+
*/
|
|
14
|
+
/** Authentication style of one upstream provider config. */
|
|
15
|
+
export type ProviderAuthType = 'bearer' | 'query_param';
|
|
16
|
+
/** One provider config entry, verbatim from upstream core.py. */
|
|
17
|
+
export interface ProviderSpec {
|
|
18
|
+
/** Display name. */
|
|
19
|
+
name: string;
|
|
20
|
+
/** HTTP endpoint. */
|
|
21
|
+
endpoint: string;
|
|
22
|
+
/** Environment variable carrying the API key. */
|
|
23
|
+
envKey: string;
|
|
24
|
+
/** Default model id. */
|
|
25
|
+
model: string;
|
|
26
|
+
/** Authentication style. */
|
|
27
|
+
authType: ProviderAuthType;
|
|
28
|
+
/** Terminal color tag. */
|
|
29
|
+
color: string;
|
|
30
|
+
/** Terminal icon. */
|
|
31
|
+
icon: string;
|
|
32
|
+
}
|
|
33
|
+
/** The upstream provider table (4 entries), verbatim. */
|
|
34
|
+
export declare const PROVIDERS: Readonly<Record<string, ProviderSpec>>;
|
|
35
|
+
/** One benchmark outcome (upstream BenchmarkResult vocabulary). */
|
|
36
|
+
export interface BenchmarkResult {
|
|
37
|
+
/** Provider table key. */
|
|
38
|
+
providerId: string;
|
|
39
|
+
/** Provider display name. */
|
|
40
|
+
providerName: string;
|
|
41
|
+
/** Time to first token in seconds; null when never reached. */
|
|
42
|
+
ttft: number | null;
|
|
43
|
+
/** Total response time in seconds; null on failure. */
|
|
44
|
+
totalTime: number | null;
|
|
45
|
+
/** Chunk/token count observed. */
|
|
46
|
+
tokens: number;
|
|
47
|
+
/** Failure detail; null on success. */
|
|
48
|
+
error: string | null;
|
|
49
|
+
/** Whether the run completed. */
|
|
50
|
+
success: boolean;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Build the request headers for one provider, upstream style.
|
|
54
|
+
*
|
|
55
|
+
* @param provider - provider config (reads `authType` only).
|
|
56
|
+
* @param apiKey - provider API key (used only for bearer auth).
|
|
57
|
+
* @returns the header map.
|
|
58
|
+
*/
|
|
59
|
+
export declare function buildRequestHeaders(provider: Readonly<ProviderSpec>, apiKey: string): Record<string, string>;
|
|
60
|
+
/**
|
|
61
|
+
* Build the request body for one provider, upstream style.
|
|
62
|
+
*
|
|
63
|
+
* @param provider - provider config.
|
|
64
|
+
* @param prompt - the user prompt.
|
|
65
|
+
* @returns the JSON body object.
|
|
66
|
+
*/
|
|
67
|
+
export declare function buildRequestBody(provider: Readonly<ProviderSpec>, prompt: string): Record<string, unknown>;
|
|
68
|
+
/**
|
|
69
|
+
* Build the request URL for one provider, upstream style (query-param auth
|
|
70
|
+
* appends the key).
|
|
71
|
+
*
|
|
72
|
+
* @param provider - provider config.
|
|
73
|
+
* @param apiKey - provider API key (used only for query-param auth).
|
|
74
|
+
* @returns the request URL.
|
|
75
|
+
*/
|
|
76
|
+
export declare function buildRequestUrl(provider: Readonly<ProviderSpec>, apiKey: string): string;
|
|
77
|
+
/** Percentile statistics over one duration sample window (milliseconds). */
|
|
78
|
+
export interface LatencyStats {
|
|
79
|
+
/** Sample count. */
|
|
80
|
+
count: number;
|
|
81
|
+
/** Arithmetic mean (ms). */
|
|
82
|
+
mean: number;
|
|
83
|
+
/** Minimum (ms). */
|
|
84
|
+
min: number;
|
|
85
|
+
/** Maximum (ms). */
|
|
86
|
+
max: number;
|
|
87
|
+
/** 50th percentile (ms). */
|
|
88
|
+
p50: number;
|
|
89
|
+
/** 95th percentile (ms). */
|
|
90
|
+
p95: number;
|
|
91
|
+
/** 99th percentile (ms). */
|
|
92
|
+
p99: number;
|
|
93
|
+
/** Population standard deviation (ms). */
|
|
94
|
+
stdev: number;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Aggregate latency statistics over a sample window. Deterministic and pure:
|
|
98
|
+
* callers own the sample retention policy.
|
|
99
|
+
*
|
|
100
|
+
* @param samples - durations in milliseconds (any order, may be empty).
|
|
101
|
+
* @returns the statistics; every field is 0 for an empty window.
|
|
102
|
+
*/
|
|
103
|
+
export declare function latencyStats(samples: readonly number[]): LatencyStats;
|
|
104
|
+
/**
|
|
105
|
+
* Format a millisecond duration the way the upstream tool displays it.
|
|
106
|
+
*
|
|
107
|
+
* @param ms - duration in milliseconds.
|
|
108
|
+
* @returns `123ms` for sub-second values, `1.23s` otherwise.
|
|
109
|
+
*/
|
|
110
|
+
export declare function formatMs(ms: number): string;
|
|
111
|
+
//# sourceMappingURL=latency-stats.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"latency-stats.d.ts","sourceRoot":"","sources":["../../../src/estimate/latency-stats.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,4DAA4D;AAC5D,MAAM,MAAM,gBAAgB,GAAG,QAAQ,GAAG,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,QAAQ,EAAE,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAGxF;AAED,4EAA4E;AAC5E,MAAM,WAAW,YAAY;IAC3B,oBAAoB;IACpB,KAAK,EAAE,MAAM,CAAA;IACb,4BAA4B;IAC5B,IAAI,EAAE,MAAM,CAAA;IACZ,oBAAoB;IACpB,GAAG,EAAE,MAAM,CAAA;IACX,oBAAoB;IACpB,GAAG,EAAE,MAAM,CAAA;IACX,4BAA4B;IAC5B,GAAG,EAAE,MAAM,CAAA;IACX,4BAA4B;IAC5B,GAAG,EAAE,MAAM,CAAA;IACX,4BAA4B;IAC5B,GAAG,EAAE,MAAM,CAAA;IACX,0CAA0C;IAC1C,KAAK,EAAE,MAAM,CAAA;CACd;AAoBD;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,YAAY,CAkBrE;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAG3C"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Port of the LLM-Cost-Estimator-CN price table
|
|
3
|
+
* (`upstream/LLM-Cost-Estimator-CN/src/llm_cost_estimator_cn/data/models.json`,
|
|
4
|
+
* commit aa6cc2f, Apache-2.0). Values are kept VERBATIM: CNY per 1k tokens.
|
|
5
|
+
*
|
|
6
|
+
* This table is the fixture source for the ported cost formulas in
|
|
7
|
+
* {@link ./cost.ts}; the plugin's operational USD-per-1M price table lives in
|
|
8
|
+
* {@link ./prices.ts} and is maintained separately.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-budget/estimate/models
|
|
11
|
+
*/
|
|
12
|
+
/** One entry of the upstream Chinese-model price table (CNY per 1k tokens). */
|
|
13
|
+
export interface UpstreamCostModelEntry {
|
|
14
|
+
/** Upstream provider display name. */
|
|
15
|
+
provider: string;
|
|
16
|
+
/** Upstream model display name. */
|
|
17
|
+
name: string;
|
|
18
|
+
/** Input price, CNY per 1000 tokens. */
|
|
19
|
+
inputPricePer1k: number;
|
|
20
|
+
/** Output price, CNY per 1000 tokens. */
|
|
21
|
+
outputPricePer1k: number;
|
|
22
|
+
/** Upstream price currency code. */
|
|
23
|
+
currency: string;
|
|
24
|
+
/** Upstream free-form note. */
|
|
25
|
+
notes?: string;
|
|
26
|
+
}
|
|
27
|
+
/** The upstream price table, verbatim from models.json. */
|
|
28
|
+
export declare const UPSTREAM_MODELS: Readonly<Record<string, UpstreamCostModelEntry>>;
|
|
29
|
+
//# sourceMappingURL=models.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"models.d.ts","sourceRoot":"","sources":["../../../src/estimate/models.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,+EAA+E;AAC/E,MAAM,WAAW,sBAAsB;IACrC,sCAAsC;IACtC,QAAQ,EAAE,MAAM,CAAA;IAChB,mCAAmC;IACnC,IAAI,EAAE,MAAM,CAAA;IACZ,wCAAwC;IACxC,eAAe,EAAE,MAAM,CAAA;IACvB,yCAAyC;IACzC,gBAAgB,EAAE,MAAM,CAAA;IACxB,oCAAoC;IACpC,QAAQ,EAAE,MAAM,CAAA;IAChB,+BAA+B;IAC/B,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED,2DAA2D;AAC3D,eAAO,MAAM,eAAe,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,sBAAsB,CAAC,CAyD3E,CAAA"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The plugin's operational price table: USD per 1M tokens, merged with
|
|
3
|
+
* `config.prices` at load. Entries marked `source: 'vendor'` were authored
|
|
4
|
+
* from vendor pricing pages at porting time; entries marked
|
|
5
|
+
* `source: 'upstream-cny'` were converted from the ported
|
|
6
|
+
* LLM-Cost-Estimator-CN table (CNY per 1k tokens) with the fixed rate
|
|
7
|
+
* {@link UPSTREAM_CNY_PER_USD} captured at porting time. Prices drift — treat
|
|
8
|
+
* this table as a starting point and override entries via `config.prices`.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-budget/estimate/prices
|
|
11
|
+
*/
|
|
12
|
+
/** Where a built-in entry's numbers came from. */
|
|
13
|
+
export type PriceSource = 'vendor' | 'upstream-cny';
|
|
14
|
+
/** USD price per 1M tokens. Cache fields follow the harness TokenUsage split. */
|
|
15
|
+
export interface PriceEntry {
|
|
16
|
+
/** Uncached input tokens. */
|
|
17
|
+
input: number;
|
|
18
|
+
/** Output tokens. */
|
|
19
|
+
output: number;
|
|
20
|
+
/** Cached (hit) input tokens; defaults to `input` when absent. */
|
|
21
|
+
cacheRead?: number;
|
|
22
|
+
/** Cache-miss (written) input tokens; defaults to `input` when absent. */
|
|
23
|
+
cacheWrite?: number;
|
|
24
|
+
/** Provenance metadata for built-in entries. */
|
|
25
|
+
source?: PriceSource;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Fixed CNY→USD rate used when converting the upstream CNY-per-1k table into
|
|
29
|
+
* USD-per-1M entries, captured at porting time. It is a conversion constant
|
|
30
|
+
* of the shipped DATA, not a runtime exchange rate; override any converted
|
|
31
|
+
* entry via `config.prices` when vendor USD pricing is available.
|
|
32
|
+
*/
|
|
33
|
+
export declare const UPSTREAM_CNY_PER_USD = 7.2;
|
|
34
|
+
/**
|
|
35
|
+
* Convert an upstream CNY-per-1k price into USD per 1M tokens.
|
|
36
|
+
*
|
|
37
|
+
* @param cnyPer1k - price in CNY per 1000 tokens.
|
|
38
|
+
* @returns the price in USD per 1,000,000 tokens.
|
|
39
|
+
*/
|
|
40
|
+
export declare function cnyPer1kToUsdPer1m(cnyPer1k: number): number;
|
|
41
|
+
/** The built-in price table (USD per 1M tokens). */
|
|
42
|
+
export declare const BUILTIN_PRICES: Readonly<Record<string, PriceEntry>>;
|
|
43
|
+
/**
|
|
44
|
+
* Merge the user table over the built-in table (per-model override).
|
|
45
|
+
*
|
|
46
|
+
* @param custom - `config.prices` entries.
|
|
47
|
+
* @returns the merged table; custom entries win per model id.
|
|
48
|
+
*/
|
|
49
|
+
export declare function mergePrices(custom: Readonly<Record<string, PriceEntry>>): Record<string, PriceEntry>;
|
|
50
|
+
/**
|
|
51
|
+
* Resolve the price for one exact route: `${provider}/${model}` first, then
|
|
52
|
+
* the bare model id, then the fallback.
|
|
53
|
+
*
|
|
54
|
+
* @param table - merged price table.
|
|
55
|
+
* @param fallback - price for models absent from the table.
|
|
56
|
+
* @param provider - registered provider route.
|
|
57
|
+
* @param model - model id.
|
|
58
|
+
* @returns the effective price entry (never undefined).
|
|
59
|
+
*/
|
|
60
|
+
export declare function priceFor(table: Readonly<Record<string, PriceEntry>>, fallback: Readonly<PriceEntry>, provider: string, model: string): PriceEntry;
|
|
61
|
+
/** Cost breakdown for one usage record. */
|
|
62
|
+
export interface UsageCost {
|
|
63
|
+
/** Uncached input cost (USD). */
|
|
64
|
+
inputCost: number;
|
|
65
|
+
/** Output cost (USD). */
|
|
66
|
+
outputCost: number;
|
|
67
|
+
/** Cache-hit input cost (USD). */
|
|
68
|
+
cacheReadCost: number;
|
|
69
|
+
/** Cache-miss input cost (USD). */
|
|
70
|
+
cacheWriteCost: number;
|
|
71
|
+
/** Sum of all four buckets (USD). */
|
|
72
|
+
totalCost: number;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Price one disjoint token usage record.
|
|
76
|
+
*
|
|
77
|
+
* @param price - effective price entry.
|
|
78
|
+
* @param inputTokens - uncached input tokens.
|
|
79
|
+
* @param outputTokens - output tokens.
|
|
80
|
+
* @param cacheReadTokens - cache-hit tokens (0 when absent).
|
|
81
|
+
* @param cacheWriteTokens - cache-miss tokens (0 when absent).
|
|
82
|
+
* @returns the USD cost breakdown.
|
|
83
|
+
*/
|
|
84
|
+
export declare function estimateUsageCost(price: Readonly<PriceEntry>, inputTokens: number, outputTokens: number, cacheReadTokens: number, cacheWriteTokens: number): UsageCost;
|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AA;IACjB,yBAAyB;IACzB,UAAU,EAAE,MAAM,CAAA;IAClB,kCAAkC;IAClC,aAAa,EAAE,MAAM,CAAA;IACrB,mCAAmC;IACnC,cAAc,EAAE,MAAM,CAAA;IACtB,qCAAqC;IACrC,SAAS,EAAE,MAAM,CAAA;CAClB;AAED;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,QAAQ,CAAC,UAAU,CAAC,EAC3B,WAAW,EAAE,MAAM,EACnB,YAAY,EAAE,MAAM,EACpB,eAAe,EAAE,MAAM,EACvB,gBAAgB,EAAE,MAAM,GACvB,SAAS,CAMX"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sanitization and formatting pure functions. Every display/log surface of
|
|
3
|
+
* dsh-budget goes through these: webhook URLs, alert text, token counts and
|
|
4
|
+
* money amounts. Never log or render a raw config or wire value.
|
|
5
|
+
*
|
|
6
|
+
* @module dsh-budget/estimate/sanitize
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Make arbitrary text safe for one-line display and logs: strip control and
|
|
10
|
+
* invisible characters, collapse whitespace runs, truncate with an ellipsis.
|
|
11
|
+
*
|
|
12
|
+
* @param input - raw text (possibly hostile).
|
|
13
|
+
* @param maxLength - output length cap (default 200).
|
|
14
|
+
* @returns the sanitized text.
|
|
15
|
+
*/
|
|
16
|
+
export declare function sanitizeText(input: string, maxLength?: number): string;
|
|
17
|
+
/**
|
|
18
|
+
* Redact credentials from a URL for display and logs: the userinfo component
|
|
19
|
+
* (`user:pass@`) is replaced, the rest is kept, and overlong URLs are cut.
|
|
20
|
+
* Query parameters are preserved verbatim — a webhook URL's own secret query
|
|
21
|
+
* tokens are the operator's responsibility to rotate, but the userinfo is
|
|
22
|
+
* always redacted.
|
|
23
|
+
*
|
|
24
|
+
* @param input - raw URL (possibly hostile).
|
|
25
|
+
* @param maxLength - output length cap (default 512).
|
|
26
|
+
* @returns the redacted URL, or '' when it cannot be parsed.
|
|
27
|
+
*/
|
|
28
|
+
export declare function sanitizeUrl(input: string, maxLength?: number): string;
|
|
29
|
+
/** Display-currency options for {@link formatMoney}. */
|
|
30
|
+
export interface MoneyFormat {
|
|
31
|
+
/** Currency code placed before the amount (display only). */
|
|
32
|
+
code: string;
|
|
33
|
+
/** Display-currency units per 1 USD (1 = USD). */
|
|
34
|
+
rate: number;
|
|
35
|
+
/** Decimal places. */
|
|
36
|
+
decimals: number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Format a USD amount in the configured display currency, deterministically
|
|
40
|
+
* (no locale-dependent grouping): converted by `rate`, rounded to `decimals`
|
|
41
|
+
* places, grouped with commas, prefixed with the currency code.
|
|
42
|
+
*
|
|
43
|
+
* @param usd - amount in USD.
|
|
44
|
+
* @param format - display options.
|
|
45
|
+
* @returns e.g. `USD 1,234.57`; non-finite input renders as `USD 0.00`.
|
|
46
|
+
*/
|
|
47
|
+
export declare function formatMoney(usd: number, format: Readonly<MoneyFormat>): string;
|
|
48
|
+
/**
|
|
49
|
+
* Format a token count compactly: `999` stays as-is, `1234` → `1.23k`,
|
|
50
|
+
* `4_500_000` → `4.50M`.
|
|
51
|
+
*
|
|
52
|
+
* @param tokens - non-negative token count.
|
|
53
|
+
* @returns the compact string.
|
|
54
|
+
*/
|
|
55
|
+
export declare function formatTokens(tokens: number): string;
|
|
56
|
+
//# sourceMappingURL=sanitize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sanitize.d.ts","sourceRoot":"","sources":["../../../src/estimate/sanitize.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAKH;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,SAAM,GAAG,MAAM,CAKnE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,SAAM,GAAG,MAAM,CAclE;AAED,wDAAwD;AACxD,MAAM,WAAW,WAAW;IAC1B,6DAA6D;IAC7D,IAAI,EAAE,MAAM,CAAA;IACZ,kDAAkD;IAClD,IAAI,EAAE,MAAM,CAAA;IACZ,sBAAsB;IACtB,QAAQ,EAAE,MAAM,CAAA;CACjB;AAcD;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,WAAW,CAAC,GAAG,MAAM,CAW9E;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAKnD"}
|