@polka-codes/core 0.9.91 → 0.9.92
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/dist/_tsup-dts-rollup.d.ts +22 -1
- package/dist/index.js +49 -3
- package/package.json +1 -1
|
@@ -1724,6 +1724,13 @@ export { providerConfigSchema }
|
|
|
1724
1724
|
export { providerConfigSchema as providerConfigSchema_alias_1 }
|
|
1725
1725
|
export { providerConfigSchema as providerConfigSchema_alias_2 }
|
|
1726
1726
|
|
|
1727
|
+
declare type ProviderMetadataEntry = {
|
|
1728
|
+
provider: string;
|
|
1729
|
+
model: string;
|
|
1730
|
+
metadata: any;
|
|
1731
|
+
timestamp: number;
|
|
1732
|
+
};
|
|
1733
|
+
|
|
1727
1734
|
declare const providerModelSchema: z.ZodObject<{
|
|
1728
1735
|
provider: z.ZodOptional<z.ZodString>;
|
|
1729
1736
|
model: z.ZodOptional<z.ZodString>;
|
|
@@ -2641,7 +2648,9 @@ declare class UsageMeter {
|
|
|
2641
2648
|
modelInfo?: ModelInfo;
|
|
2642
2649
|
}): void;
|
|
2643
2650
|
/** Override the running totals (e.g., restore from saved state). */
|
|
2644
|
-
setUsage(newUsage: Partial<Totals
|
|
2651
|
+
setUsage(newUsage: Partial<Totals>, options?: {
|
|
2652
|
+
clearMetadata?: boolean;
|
|
2653
|
+
}): void;
|
|
2645
2654
|
/** Manually bump the message count (useful if you record some messages without token info). */
|
|
2646
2655
|
incrementMessageCount(n?: number): void;
|
|
2647
2656
|
/** Reset the running totals. */
|
|
@@ -2664,6 +2673,18 @@ declare class UsageMeter {
|
|
|
2664
2673
|
cost: number;
|
|
2665
2674
|
messageCount: number;
|
|
2666
2675
|
};
|
|
2676
|
+
/** Getter for provider metadata entries (immutable copy). */
|
|
2677
|
+
get providerMetadata(): ProviderMetadataEntry[];
|
|
2678
|
+
/** Calculate cache statistics from stored metadata entries. */
|
|
2679
|
+
get cacheStats(): {
|
|
2680
|
+
totalCachedTokens: number;
|
|
2681
|
+
totalRequests: number;
|
|
2682
|
+
requestsWithCache: number;
|
|
2683
|
+
cacheHitRate: number;
|
|
2684
|
+
entries: ProviderMetadataEntry[];
|
|
2685
|
+
};
|
|
2686
|
+
/** Clear stored provider metadata entries. */
|
|
2687
|
+
clearProviderMetadata(): void;
|
|
2667
2688
|
/** Merge another UsageMeter's totals into this one. */
|
|
2668
2689
|
merge(other: UsageMeter): void;
|
|
2669
2690
|
getUsageText(): string;
|
package/dist/index.js
CHANGED
|
@@ -166,12 +166,12 @@ import { z as z2 } from "zod";
|
|
|
166
166
|
var memoryConfigSchema = z2.object({
|
|
167
167
|
enabled: z2.boolean().optional().default(true),
|
|
168
168
|
type: z2.enum(["sqlite", "memory"]).optional().default("sqlite"),
|
|
169
|
-
path: z2.string().optional().default("~/.config/
|
|
169
|
+
path: z2.string().optional().default("~/.config/polkacodes/memory/memory.sqlite")
|
|
170
170
|
}).strict().optional();
|
|
171
171
|
var DEFAULT_MEMORY_CONFIG = {
|
|
172
172
|
enabled: true,
|
|
173
173
|
type: "sqlite",
|
|
174
|
-
path: "~/.config/
|
|
174
|
+
path: "~/.config/polkacodes/memory/memory.sqlite"
|
|
175
175
|
};
|
|
176
176
|
function resolveHomePath(path) {
|
|
177
177
|
if (path.startsWith("~")) {
|
|
@@ -2269,6 +2269,7 @@ var writeToFile_default = {
|
|
|
2269
2269
|
// src/UsageMeter.ts
|
|
2270
2270
|
var UsageMeter = class {
|
|
2271
2271
|
#totals = { input: 0, output: 0, cachedRead: 0, cost: 0, messageCount: 0 };
|
|
2272
|
+
#providerMetadataEntries = [];
|
|
2272
2273
|
#modelInfos;
|
|
2273
2274
|
#maxMessages;
|
|
2274
2275
|
#maxCost;
|
|
@@ -2352,14 +2353,26 @@ var UsageMeter = class {
|
|
|
2352
2353
|
this.#totals.cachedRead += result.cachedRead || 0;
|
|
2353
2354
|
this.#totals.cost += result.cost || 0;
|
|
2354
2355
|
this.#totals.messageCount += 1;
|
|
2356
|
+
if (resp.providerMetadata && Object.keys(resp.providerMetadata).length > 0) {
|
|
2357
|
+
const providerKey = Object.keys(resp.providerMetadata)[0];
|
|
2358
|
+
this.#providerMetadataEntries.push({
|
|
2359
|
+
provider: providerKey || llm.provider,
|
|
2360
|
+
model: llm.modelId,
|
|
2361
|
+
metadata: resp.providerMetadata[providerKey] || resp.providerMetadata,
|
|
2362
|
+
timestamp: Date.now()
|
|
2363
|
+
});
|
|
2364
|
+
}
|
|
2355
2365
|
}
|
|
2356
2366
|
/** Override the running totals (e.g., restore from saved state). */
|
|
2357
|
-
setUsage(newUsage) {
|
|
2367
|
+
setUsage(newUsage, options = {}) {
|
|
2358
2368
|
if (newUsage.input != null) this.#totals.input = newUsage.input;
|
|
2359
2369
|
if (newUsage.output != null) this.#totals.output = newUsage.output;
|
|
2360
2370
|
if (newUsage.cachedRead != null) this.#totals.cachedRead = newUsage.cachedRead;
|
|
2361
2371
|
if (newUsage.cost != null) this.#totals.cost = newUsage.cost;
|
|
2362
2372
|
if (newUsage.messageCount != null) this.#totals.messageCount = newUsage.messageCount;
|
|
2373
|
+
if (options.clearMetadata) {
|
|
2374
|
+
this.#providerMetadataEntries = [];
|
|
2375
|
+
}
|
|
2363
2376
|
}
|
|
2364
2377
|
/** Manually bump the message count (useful if you record some messages without token info). */
|
|
2365
2378
|
incrementMessageCount(n = 1) {
|
|
@@ -2368,6 +2381,7 @@ var UsageMeter = class {
|
|
|
2368
2381
|
/** Reset the running totals. */
|
|
2369
2382
|
resetUsage() {
|
|
2370
2383
|
this.#totals = { input: 0, output: 0, cachedRead: 0, cost: 0, messageCount: 0 };
|
|
2384
|
+
this.#providerMetadataEntries = [];
|
|
2371
2385
|
}
|
|
2372
2386
|
/** Return true once either messages or cost exceed the configured caps. */
|
|
2373
2387
|
isLimitExceeded() {
|
|
@@ -2394,6 +2408,37 @@ var UsageMeter = class {
|
|
|
2394
2408
|
get usage() {
|
|
2395
2409
|
return { ...this.#totals };
|
|
2396
2410
|
}
|
|
2411
|
+
/** Getter for provider metadata entries (immutable copy). */
|
|
2412
|
+
get providerMetadata() {
|
|
2413
|
+
return [...this.#providerMetadataEntries];
|
|
2414
|
+
}
|
|
2415
|
+
/** Calculate cache statistics from stored metadata entries. */
|
|
2416
|
+
get cacheStats() {
|
|
2417
|
+
const entries = this.#providerMetadataEntries;
|
|
2418
|
+
const totalRequests = entries.length;
|
|
2419
|
+
let totalCachedTokens = 0;
|
|
2420
|
+
let requestsWithCache = 0;
|
|
2421
|
+
for (const entry of entries) {
|
|
2422
|
+
const metadata = entry.metadata;
|
|
2423
|
+
const cachedTokens = metadata.cachedPromptTokens ?? metadata.cacheReadTokens ?? metadata.prompt_cache_hit_tokens ?? 0;
|
|
2424
|
+
if (cachedTokens > 0) {
|
|
2425
|
+
totalCachedTokens += cachedTokens;
|
|
2426
|
+
requestsWithCache++;
|
|
2427
|
+
}
|
|
2428
|
+
}
|
|
2429
|
+
const cacheHitRate = totalRequests > 0 ? requestsWithCache / totalRequests : 0;
|
|
2430
|
+
return {
|
|
2431
|
+
totalCachedTokens,
|
|
2432
|
+
totalRequests,
|
|
2433
|
+
requestsWithCache,
|
|
2434
|
+
cacheHitRate,
|
|
2435
|
+
entries: [...this.#providerMetadataEntries]
|
|
2436
|
+
};
|
|
2437
|
+
}
|
|
2438
|
+
/** Clear stored provider metadata entries. */
|
|
2439
|
+
clearProviderMetadata() {
|
|
2440
|
+
this.#providerMetadataEntries = [];
|
|
2441
|
+
}
|
|
2397
2442
|
/** Merge another UsageMeter's totals into this one. */
|
|
2398
2443
|
merge(other) {
|
|
2399
2444
|
const otherUsage = other.usage;
|
|
@@ -2402,6 +2447,7 @@ var UsageMeter = class {
|
|
|
2402
2447
|
this.#totals.cachedRead += otherUsage.cachedRead;
|
|
2403
2448
|
this.#totals.cost += otherUsage.cost;
|
|
2404
2449
|
this.#totals.messageCount += otherUsage.messageCount;
|
|
2450
|
+
this.#providerMetadataEntries.push(...other.providerMetadata);
|
|
2405
2451
|
}
|
|
2406
2452
|
getUsageText() {
|
|
2407
2453
|
const u = this.usage;
|