@laoyuehanni/dsh-token-usage 0.4.6 → 0.4.8
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/README.md +7 -5
- package/README.zh.md +7 -5
- package/docs/pricing.md +61 -4
- package/lib/aux-request.d.ts +62 -0
- package/lib/aux-request.d.ts.map +1 -0
- package/lib/aux-request.js +80 -0
- package/lib/aux-request.js.map +1 -0
- package/lib/client/CostPill.d.ts.map +1 -1
- package/lib/client/CostPill.js +18 -13
- package/lib/client/CostPill.js.map +1 -1
- package/lib/client/PricingAssociations.d.ts +38 -0
- package/lib/client/PricingAssociations.d.ts.map +1 -0
- package/lib/client/PricingAssociations.js +211 -0
- package/lib/client/PricingAssociations.js.map +1 -0
- package/lib/client/PricingOverviewDialog.d.ts +2 -13
- package/lib/client/PricingOverviewDialog.d.ts.map +1 -1
- package/lib/client/PricingOverviewDialog.js +12 -39
- package/lib/client/PricingOverviewDialog.js.map +1 -1
- package/lib/client/PricingStatus.d.ts +12 -0
- package/lib/client/PricingStatus.d.ts.map +1 -0
- package/lib/client/PricingStatus.js +13 -0
- package/lib/client/PricingStatus.js.map +1 -0
- package/lib/client/QuotaButton.js +17 -5
- package/lib/client/QuotaButton.js.map +1 -1
- package/lib/client/SessionTable.d.ts +3 -2
- package/lib/client/SessionTable.d.ts.map +1 -1
- package/lib/client/SessionTable.js +3 -2
- package/lib/client/SessionTable.js.map +1 -1
- package/lib/client/TokenUsageCard.d.ts.map +1 -1
- package/lib/client/TokenUsageCard.js +51 -3
- package/lib/client/TokenUsageCard.js.map +1 -1
- package/lib/client/TokenUsageSection.d.ts +26 -0
- package/lib/client/TokenUsageSection.d.ts.map +1 -1
- package/lib/client/TokenUsageSection.js +103 -53
- package/lib/client/TokenUsageSection.js.map +1 -1
- package/lib/client/UsageView.d.ts.map +1 -1
- package/lib/client/UsageView.js +46 -42
- package/lib/client/UsageView.js.map +1 -1
- package/lib/client/locales.d.ts +69 -0
- package/lib/client/locales.d.ts.map +1 -1
- package/lib/client/locales.js +138 -0
- package/lib/client/locales.js.map +1 -1
- package/lib/client/pricing-associations.d.ts +13 -0
- package/lib/client/pricing-associations.d.ts.map +1 -0
- package/lib/client/pricing-associations.js +51 -0
- package/lib/client/pricing-associations.js.map +1 -0
- package/lib/client/pricing-search.d.ts +5 -0
- package/lib/client/pricing-search.d.ts.map +1 -0
- package/lib/client/pricing-search.js +27 -0
- package/lib/client/pricing-search.js.map +1 -0
- package/lib/client/use-cost-inflate.d.ts.map +1 -1
- package/lib/client/use-cost-inflate.js +6 -1
- package/lib/client/use-cost-inflate.js.map +1 -1
- package/lib/client.js +2809 -1584
- package/lib/client.js.map +1 -1
- package/lib/index.d.ts +6 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +171 -40
- package/lib/index.js.map +1 -1
- package/lib/migrate.d.ts +9 -7
- package/lib/migrate.d.ts.map +1 -1
- package/lib/migrate.js +43 -8
- package/lib/migrate.js.map +1 -1
- package/lib/model-resolution.d.ts +15 -0
- package/lib/model-resolution.d.ts.map +1 -0
- package/lib/model-resolution.js +124 -0
- package/lib/model-resolution.js.map +1 -0
- package/lib/pricing-association-service.d.ts +20 -0
- package/lib/pricing-association-service.d.ts.map +1 -0
- package/lib/pricing-association-service.js +104 -0
- package/lib/pricing-association-service.js.map +1 -0
- package/lib/pricing-associations-route.d.ts +6 -0
- package/lib/pricing-associations-route.d.ts.map +1 -0
- package/lib/pricing-associations-route.js +71 -0
- package/lib/pricing-associations-route.js.map +1 -0
- package/lib/pricing-associations.d.ts +42 -0
- package/lib/pricing-associations.d.ts.map +1 -0
- package/lib/pricing-associations.js +133 -0
- package/lib/pricing-associations.js.map +1 -0
- package/lib/pricing.d.ts +37 -32
- package/lib/pricing.d.ts.map +1 -1
- package/lib/pricing.js +197 -63
- package/lib/pricing.js.map +1 -1
- package/lib/quota/adapters/command-code.d.ts +33 -0
- package/lib/quota/adapters/command-code.d.ts.map +1 -0
- package/lib/quota/adapters/command-code.js +170 -0
- package/lib/quota/adapters/command-code.js.map +1 -0
- package/lib/quota/adapters/deepseek-account.d.ts +24 -0
- package/lib/quota/adapters/deepseek-account.d.ts.map +1 -0
- package/lib/quota/adapters/deepseek-account.js +91 -0
- package/lib/quota/adapters/deepseek-account.js.map +1 -0
- package/lib/quota/adapters/deepseek-balance.d.ts +7 -3
- package/lib/quota/adapters/deepseek-balance.d.ts.map +1 -1
- package/lib/quota/adapters/deepseek-balance.js +9 -3
- package/lib/quota/adapters/deepseek-balance.js.map +1 -1
- package/lib/quota/credentials.d.ts +3 -2
- package/lib/quota/credentials.d.ts.map +1 -1
- package/lib/quota/credentials.js +5 -3
- package/lib/quota/credentials.js.map +1 -1
- package/lib/quota/quota-service.d.ts +3 -0
- package/lib/quota/quota-service.d.ts.map +1 -1
- package/lib/quota/quota-service.js +2 -1
- package/lib/quota/quota-service.js.map +1 -1
- package/lib/quota/registry.d.ts.map +1 -1
- package/lib/quota/registry.js +6 -0
- package/lib/quota/registry.js.map +1 -1
- package/lib/quota/types.d.ts +23 -1
- package/lib/quota/types.d.ts.map +1 -1
- package/lib/quota/types.js.map +1 -1
- package/lib/record-cache.d.ts +5 -0
- package/lib/record-cache.d.ts.map +1 -1
- package/lib/record-cache.js +9 -1
- package/lib/record-cache.js.map +1 -1
- package/lib/rollup.d.ts +17 -6
- package/lib/rollup.d.ts.map +1 -1
- package/lib/rollup.js +26 -7
- package/lib/rollup.js.map +1 -1
- package/lib/stats-route.d.ts +4 -1
- package/lib/stats-route.d.ts.map +1 -1
- package/lib/stats-route.js +65 -21
- package/lib/stats-route.js.map +1 -1
- package/lib/stats.d.ts +22 -8
- package/lib/stats.d.ts.map +1 -1
- package/lib/stats.js +244 -57
- package/lib/stats.js.map +1 -1
- package/lib/usage-record.d.ts +34 -6
- package/lib/usage-record.d.ts.map +1 -1
- package/lib/usage-record.js +20 -4
- package/lib/usage-record.js.map +1 -1
- package/lib/wire.d.ts +136 -11
- package/lib/wire.d.ts.map +1 -1
- package/lib/wire.js +2 -0
- package/lib/wire.js.map +1 -1
- package/package.json +12 -10
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/** Durable name references; prices and usage records never live in this file. */
|
|
2
|
+
import { mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { join, resolve } from 'node:path';
|
|
4
|
+
import { modelKey } from "./model-resolution.js";
|
|
5
|
+
export const ASSOCIATIONS_FILE = 'pricing-associations.json';
|
|
6
|
+
const TMP_FILE = `${ASSOCIATIONS_FILE}.tmp`;
|
|
7
|
+
export class AssociationError extends Error {
|
|
8
|
+
code;
|
|
9
|
+
statusCode;
|
|
10
|
+
constructor(code, statusCode = 409) {
|
|
11
|
+
super(code);
|
|
12
|
+
this.code = code;
|
|
13
|
+
this.statusCode = statusCode;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** Compare complete names without trimming or dropping namespace components. */
|
|
17
|
+
export function isAssociationName(value) {
|
|
18
|
+
return typeof value === 'string' && value !== '' && !/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/u.test(value);
|
|
19
|
+
}
|
|
20
|
+
export function exactFields(value, fields) {
|
|
21
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
22
|
+
&& Object.keys(value).length === fields.length
|
|
23
|
+
&& fields.every(field => Object.hasOwn(value, field));
|
|
24
|
+
}
|
|
25
|
+
export function coerceAssociations(value) {
|
|
26
|
+
if (!exactFields(value, ['version', 'revision', 'associations']) || value.version !== 1
|
|
27
|
+
|| !Number.isSafeInteger(value.revision) || value.revision < 0 || !Array.isArray(value.associations))
|
|
28
|
+
return null;
|
|
29
|
+
const keys = new Set();
|
|
30
|
+
const associations = [];
|
|
31
|
+
for (const row of value.associations) {
|
|
32
|
+
if (!exactFields(row, ['sourceModel', 'targetModelId'])
|
|
33
|
+
|| !isAssociationName(row.sourceModel) || !isAssociationName(row.targetModelId))
|
|
34
|
+
return null;
|
|
35
|
+
const key = modelKey(row.sourceModel);
|
|
36
|
+
if (keys.has(key))
|
|
37
|
+
return null;
|
|
38
|
+
keys.add(key);
|
|
39
|
+
associations.push({ sourceModel: row.sourceModel, targetModelId: row.targetModelId });
|
|
40
|
+
}
|
|
41
|
+
return { version: 1, revision: value.revision, associations };
|
|
42
|
+
}
|
|
43
|
+
/** Missing is empty; malformed or unreadable durable choices are never erased. */
|
|
44
|
+
export async function readAssociations(dir) {
|
|
45
|
+
let source;
|
|
46
|
+
try {
|
|
47
|
+
source = await readFile(join(dir, ASSOCIATIONS_FILE), 'utf8');
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
if (error.code === 'ENOENT') {
|
|
51
|
+
return { state: 'ready', source: null, file: { version: 1, revision: 0, associations: [] } };
|
|
52
|
+
}
|
|
53
|
+
return { state: 'unavailable', source: null, error: 'unreadable' };
|
|
54
|
+
}
|
|
55
|
+
let value;
|
|
56
|
+
try {
|
|
57
|
+
value = JSON.parse(source);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return { state: 'unavailable', source, error: 'invalid-json' };
|
|
61
|
+
}
|
|
62
|
+
const file = coerceAssociations(value);
|
|
63
|
+
return file === null ? { state: 'unavailable', source, error: 'invalid-file' } : { state: 'ready', source, file };
|
|
64
|
+
}
|
|
65
|
+
const queues = new Map();
|
|
66
|
+
export function pricingDirectoryKey(dir) {
|
|
67
|
+
const absolute = resolve(dir);
|
|
68
|
+
return process.platform === 'win32' ? absolute.toLowerCase() : absolute;
|
|
69
|
+
}
|
|
70
|
+
function queueOf(dir) {
|
|
71
|
+
const key = pricingDirectoryKey(dir);
|
|
72
|
+
let state = queues.get(key);
|
|
73
|
+
if (state === undefined) {
|
|
74
|
+
state = { tail: Promise.resolve(), paused: false, epoch: 0 };
|
|
75
|
+
queues.set(key, state);
|
|
76
|
+
}
|
|
77
|
+
return state;
|
|
78
|
+
}
|
|
79
|
+
/** Snapshot reads and feed/association commits share one process-local queue. */
|
|
80
|
+
export function withPricingDirectoryRead(dir, run) {
|
|
81
|
+
const state = queueOf(dir);
|
|
82
|
+
const task = state.tail.catch(() => undefined).then(run);
|
|
83
|
+
state.tail = task.catch(() => undefined);
|
|
84
|
+
return task;
|
|
85
|
+
}
|
|
86
|
+
export function withPricingDirectoryWrite(dir, run) {
|
|
87
|
+
if (queueOf(dir).paused)
|
|
88
|
+
return Promise.reject(new AssociationError('migration-in-progress'));
|
|
89
|
+
return withPricingDirectoryRead(dir, run);
|
|
90
|
+
}
|
|
91
|
+
export function associationWritesPaused(dir) { return queueOf(dir).paused; }
|
|
92
|
+
export function pricingDirectoryEpoch(dir) { return queueOf(dir).epoch; }
|
|
93
|
+
/** Block new commits to both directories, drain already accepted writes,
|
|
94
|
+
* and keep them paused through copying, switching and source cleanup. */
|
|
95
|
+
export async function withPricingDirectoryMigration(oldDir, newDir, run) {
|
|
96
|
+
const states = [...new Set([queueOf(oldDir), queueOf(newDir)])];
|
|
97
|
+
if (states.some(state => state.paused))
|
|
98
|
+
throw new AssociationError('migration-in-progress');
|
|
99
|
+
for (const state of states) {
|
|
100
|
+
state.paused = true;
|
|
101
|
+
state.epoch += 1;
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
await Promise.all(states.map(state => state.tail));
|
|
105
|
+
return await run();
|
|
106
|
+
}
|
|
107
|
+
finally {
|
|
108
|
+
for (const state of states)
|
|
109
|
+
state.paused = false;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/** Caller holds the directory queue. Verify both the directory epoch and
|
|
113
|
+
* original bytes immediately before replacing durable user choices. */
|
|
114
|
+
export async function writeAssociations(dir, expected, file, verifyDirectory) {
|
|
115
|
+
const path = join(dir, ASSOCIATIONS_FILE), tmp = join(dir, TMP_FILE);
|
|
116
|
+
await mkdir(dir, { recursive: true });
|
|
117
|
+
try {
|
|
118
|
+
await writeFile(tmp, JSON.stringify(file) + '\n', 'utf8');
|
|
119
|
+
const source = await readFile(path, 'utf8').catch((error) => {
|
|
120
|
+
if (error.code === 'ENOENT')
|
|
121
|
+
return null;
|
|
122
|
+
throw error;
|
|
123
|
+
});
|
|
124
|
+
verifyDirectory();
|
|
125
|
+
if (source !== expected.source)
|
|
126
|
+
throw new AssociationError('revision-conflict');
|
|
127
|
+
await rename(tmp, path);
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
await unlink(tmp).catch(() => undefined);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=pricing-associations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pricing-associations.js","sourceRoot":"","sources":["../src/pricing-associations.ts"],"names":[],"mappings":"AAAA,iFAAiF;AACjF,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAC7E,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AAGhD,MAAM,CAAC,MAAM,iBAAiB,GAAG,2BAA2B,CAAA;AAC5D,MAAM,QAAQ,GAAG,GAAG,iBAAiB,MAAM,CAAA;AAY3C,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IACpB;IAAuB;IAA5C,YAAqB,IAAY,EAAW,aAAa,GAAG;QAC1D,KAAK,CAAC,IAAI,CAAC,CAAA;QADQ,SAAI,GAAJ,IAAI,CAAQ;QAAW,eAAU,GAAV,UAAU,CAAM;IAE5D,CAAC;CACF;AAED,gFAAgF;AAChF,MAAM,UAAU,iBAAiB,CAAC,KAAc;IAC9C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,EAAE,IAAI,CAAC,2CAA2C,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;AAC9G,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,KAAc,EAAE,MAAyB;IACnE,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,K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2B;IAE3B,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,EAAE,iBAAiB,CAAC,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAA;IACpE,MAAM,KAAK,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAA;IACrC,IAAI,CAAC;QACH,MAAM,SAAS,CAAC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,IAAI,EAAE,MAAM,CAAC,CAAA;QACzD,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;YACnE,IAAK,KAA+B,CAAC,IAAI,KAAK,QAAQ;gBAAE,OAAO,IAAI,CAAA;YACnE,MAAM,KAAK,CAAA;QACb,CAAC,CAAC,CAAA;QACF,eAAe,EAAE,CAAA;QACjB,IAAI,MAAM,KAAK,QAAQ,CAAC,MAAM;YAAE,MAAM,IAAI,gBAAgB,CAAC,mBAAmB,CAAC,CAAA;QAC/E,MAAM,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,CAAA;IACzB,CAAC;YAAS,CAAC;QAAC,MAAM,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;IAAC,CAAC;AACxD,CAAC"}
|
package/lib/pricing.d.ts
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* price rules (¥ per million tokens) backing the cost figures of the stats
|
|
4
4
|
* page. The single source is the cloud feed mirrored on every dsh startup
|
|
5
5
|
* into `<data dir>/pricing.ccsa.json` (the same model-price-table source
|
|
6
|
-
* cc-switch-analyzer pulls); an absent or malformed mirror
|
|
7
|
-
*
|
|
6
|
+
* cc-switch-analyzer pulls); an absent or malformed mirror reports costs
|
|
7
|
+
* unavailable while usage remains readable. Pricing
|
|
8
8
|
* data used to layer a hand-edited `pricing.json` on top, but that path was
|
|
9
9
|
* removed (see docs/decisions/implemented/2026-09-05-abolish-manual-pricing-json.md):
|
|
10
10
|
* no UI entry point, wholesale-replace semantics silently drop time rules
|
|
@@ -14,14 +14,15 @@
|
|
|
14
14
|
* time-rule container first, then context tier, then peak slot (a slot may be
|
|
15
15
|
* restricted to ISO weekdays via `daysOfWeek`, matched on the request's local
|
|
16
16
|
* day) — and the aggregation keeps rows per (day, model, rate identity), so
|
|
17
|
-
*
|
|
18
|
-
*
|
|
17
|
+
* a pricing-context change rebuilds stored identities from raw history.
|
|
18
|
+
* Context size is approximated by the request's input-side tokens (input +
|
|
19
19
|
* cacheRead + cacheWrite): the log does not carry the raw context size.
|
|
20
20
|
*
|
|
21
21
|
* @module token-usage/pricing
|
|
22
22
|
*/
|
|
23
|
+
import { type AssociationSnapshot } from './pricing-associations.ts';
|
|
23
24
|
import { type LoggerLike } from './log.ts';
|
|
24
|
-
import type { ModelPricing, ModelRates, PricingOverviewModel, PricingTable, RateKey, UsageTotals } from './wire.ts';
|
|
25
|
+
import type { ModelPricing, ModelRates, ModelResolution, PricingOverviewModel, PricingTable, PricingUnavailableReason, PricingAssociationStatus, PricingSyncResult, RateKey, UsageTotals } from './wire.ts';
|
|
25
26
|
/** The cloud-feed mirror written on every startup. */
|
|
26
27
|
export declare const PRICING_CCSA_FILE = "pricing.ccsa.json";
|
|
27
28
|
/**
|
|
@@ -107,7 +108,7 @@ export interface CloudPricingModel extends CloudRates {
|
|
|
107
108
|
family?: string;
|
|
108
109
|
contextTiers?: CloudTier[];
|
|
109
110
|
timeRules?: CloudTimeRule[];
|
|
110
|
-
|
|
111
|
+
aliasPatterns?: string[];
|
|
111
112
|
}
|
|
112
113
|
/** The cloud feed envelope. */
|
|
113
114
|
export interface CloudPricingData {
|
|
@@ -128,25 +129,20 @@ export interface CloudPricingData {
|
|
|
128
129
|
*/
|
|
129
130
|
export declare function coerceCloudPricing(value: unknown): CloudPricingData | null;
|
|
130
131
|
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
* first, ids last, so a model id always wins a collision with another
|
|
134
|
-
* model's alias.
|
|
132
|
+
* Seed model IDs and resolve observed regex/provider names on demand.
|
|
133
|
+
* Patterns cannot be expanded into a finite list of dictionary keys.
|
|
135
134
|
* @param feed - the coerced cloud feed.
|
|
136
|
-
* @returns
|
|
135
|
+
* @returns a table keyed by model IDs and successfully looked-up names.
|
|
137
136
|
*/
|
|
138
137
|
export declare function cloudToTable(feed: CloudPricingData): PricingTable;
|
|
139
138
|
/** One model row's identity for alias → modelId folding. */
|
|
140
139
|
export interface CanonicalModelRef {
|
|
141
140
|
modelId: string;
|
|
142
|
-
|
|
141
|
+
aliasPatterns?: readonly string[];
|
|
143
142
|
}
|
|
144
143
|
/**
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
* aliases first (first writer wins), model ids last (always overwrite).
|
|
148
|
-
* Names that do not appear in the feed are absent; callers fall back
|
|
149
|
-
* to the raw name.
|
|
144
|
+
* Resolve names lazily through the same ID/regex chain as pricing.
|
|
145
|
+
* Only IDs and observed matches are enumerated; ambiguous names stay absent.
|
|
150
146
|
* @param models - the feed's model rows (or the overview projection).
|
|
151
147
|
* @returns name → modelId; unpriced names are not keys.
|
|
152
148
|
*/
|
|
@@ -197,16 +193,32 @@ export declare function ratesForKey(rates: ModelRates, key: RateKey): ModelPrici
|
|
|
197
193
|
export declare function costOf(totals: UsageTotals, prices: ModelPricing): number;
|
|
198
194
|
/**
|
|
199
195
|
* The synced cloud table of one data directory plus the alias → modelId
|
|
200
|
-
*
|
|
201
|
-
*
|
|
196
|
+
* lookup, compiled identity resolver, exchange rate and history fingerprint
|
|
197
|
+
* from one validated feed and association snapshot.
|
|
202
198
|
* @param dir - the plugin's data directory.
|
|
203
199
|
* @param logger - diagnostic sink (defaults to console).
|
|
204
200
|
* @returns the flat table and the canonical map (both empty when absent).
|
|
205
201
|
*/
|
|
206
|
-
export
|
|
202
|
+
export interface PricingContext {
|
|
203
|
+
feedVersion?: number;
|
|
204
|
+
associations: AssociationSnapshot;
|
|
205
|
+
associationStatuses: PricingAssociationStatus[];
|
|
206
|
+
unavailableReason?: PricingUnavailableReason;
|
|
207
207
|
table: PricingTable;
|
|
208
208
|
canonical: Map<string, string>;
|
|
209
|
-
|
|
209
|
+
available: boolean;
|
|
210
|
+
fingerprint: string;
|
|
211
|
+
usdExchangeRate: number;
|
|
212
|
+
models: PricingOverviewModel[];
|
|
213
|
+
resolveModel(name: string): ModelResolution;
|
|
214
|
+
resolveAutomatic(name: string): ModelResolution;
|
|
215
|
+
ratesForModel(name: string): ModelRates | undefined;
|
|
216
|
+
}
|
|
217
|
+
/** One immutable feed snapshot; its indexes are private and resolutions lazy. */
|
|
218
|
+
export declare function pricingContextOf(feed: CloudPricingData | null, unavailableReason?: PricingUnavailableReason, associations?: AssociationSnapshot): PricingContext;
|
|
219
|
+
export declare function readPricingContext(dir: string, logger?: LoggerLike): Promise<PricingContext>;
|
|
220
|
+
/** Internal seam for association mutations that already hold the directory queue. */
|
|
221
|
+
export declare function readPricingContextLocked(dir: string, logger?: LoggerLike): Promise<PricingContext>;
|
|
210
222
|
/**
|
|
211
223
|
* Load the pricing table of one data directory: the synced cloud mirror
|
|
212
224
|
* flattened through {@link cloudToTable}. Absent or malformed mirrors read
|
|
@@ -230,9 +242,8 @@ export declare function readUsdExchangeRate(dir: string, logger?: LoggerLike): P
|
|
|
230
242
|
/**
|
|
231
243
|
* The cloud feed of one data directory in its model-row shape (not flattened
|
|
232
244
|
* through {@link cloudToTable}): one row per model carrying the modelId, the
|
|
233
|
-
* alias
|
|
234
|
-
*
|
|
235
|
-
* one row per alias-key. Absent, malformed, or non-RMB mirrors read as an
|
|
245
|
+
* alias patterns, upstream family, and full rule set — one row per model.
|
|
246
|
+
* Absent, malformed, or non-RMB mirrors read as an
|
|
236
247
|
* empty list, matching {@link readPricingTable}'s stay-alive semantics.
|
|
237
248
|
* @param dir - the plugin's data directory.
|
|
238
249
|
* @param logger - diagnostic sink (defaults to console).
|
|
@@ -240,13 +251,7 @@ export declare function readUsdExchangeRate(dir: string, logger?: LoggerLike): P
|
|
|
240
251
|
*/
|
|
241
252
|
export declare function readPricingOverview(dir: string, logger?: LoggerLike): Promise<PricingOverviewModel[]>;
|
|
242
253
|
/** The outcome of one successful cloud sync, for the command's reply. */
|
|
243
|
-
export
|
|
244
|
-
version: number;
|
|
245
|
-
updatedAt: number;
|
|
246
|
-
models: number;
|
|
247
|
-
aliases: number;
|
|
248
|
-
usdExchangeRate: number;
|
|
249
|
-
}
|
|
254
|
+
export type CloudSyncResult = PricingSyncResult;
|
|
250
255
|
/**
|
|
251
256
|
* Fetch the cloud pricing feed and mirror it verbatim into
|
|
252
257
|
* `<data dir>/pricing.ccsa.json` (atomically: temp file + rename). The raw
|
|
@@ -255,6 +260,6 @@ export interface CloudSyncResult {
|
|
|
255
260
|
* @param url - the feed URL (the config override or the default gitee feed).
|
|
256
261
|
* @returns the sync summary.
|
|
257
262
|
*/
|
|
258
|
-
export declare function syncCloudPricing(dir: string, url: string): Promise<CloudSyncResult>;
|
|
263
|
+
export declare function syncCloudPricing(dir: string, url: string, isCurrent?: () => boolean): Promise<CloudSyncResult>;
|
|
259
264
|
export {};
|
|
260
265
|
//# sourceMappingURL=pricing.d.ts.map
|
package/lib/pricing.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pricing.d.ts","sourceRoot":"","sources":["../src/pricing.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;
|
|
1
|
+
{"version":3,"file":"pricing.d.ts","sourceRoot":"","sources":["../src/pricing.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAMH,OAAO,EAA2I,KAAK,mBAAmB,EAAE,MAAM,2BAA2B,CAAA;AAC7M,OAAO,EAAiB,KAAK,UAAU,EAAE,MAAM,UAAU,CAAA;AACzD,OAAO,KAAK,EAGV,YAAY,EACZ,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