mirror of
https://github.com/HeyPuter/puter.git
synced 2026-11-25 07:09:11 +00:00
fix(ai): record no usage for a provider count that isn't a positive number (#4723)
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
d8bb1b3570
commit
2798d721e0
2 files changed
+38
-7
No files matched your search
@@ -154,6 +154,28 @@ describe('AI cost factor', () => {
|
||||
expect(result.total).toBe(1040 + 2080);
|
||||
});
|
||||
|
||||
it('records nothing for a count that is not a positive number', async () => {
|
||||
const recorded = vi.spyOn(metering, 'batchIncrementUsages');
|
||||
|
||||
await scoped.utilRecordUsageObject(
|
||||
{
|
||||
input_tokens: 100,
|
||||
output_tokens: '5',
|
||||
cached_tokens: -1,
|
||||
reasoning_tokens: NaN,
|
||||
audio_tokens: 1e308,
|
||||
} as unknown as Record<string, number>,
|
||||
actor,
|
||||
'claude:sonnet',
|
||||
);
|
||||
|
||||
const amounts = recorded.mock.calls[0]![1].map(
|
||||
({ usageAmount }) => usageAmount,
|
||||
);
|
||||
recorded.mockRestore();
|
||||
expect(amounts).toEqual([100, 0, 0, 0, 0]);
|
||||
});
|
||||
|
||||
it('records a usage object as it stood when recording began', async () => {
|
||||
listen(2);
|
||||
const recorded = vi.spyOn(metering, 'batchIncrementUsages');
|
||||
|
||||
@@ -154,8 +154,7 @@ const PENDING_WRITES_KEY = 'aiPendingUsageWrites';
|
||||
const trackWrite = (write: Promise<UsageByType>): Promise<UsageByType> => {
|
||||
if (!Context.current()) return write;
|
||||
let pending = Context.get(PENDING_WRITES_KEY) as
|
||||
| Set<Promise<void>>
|
||||
| undefined;
|
||||
Set<Promise<void>> | undefined;
|
||||
if (!pending) {
|
||||
pending = new Set();
|
||||
Context.set(PENDING_WRITES_KEY, pending);
|
||||
@@ -180,8 +179,7 @@ const outlastingWrites = (hold: CreditHold): CreditHold => {
|
||||
...hold,
|
||||
release: async () => {
|
||||
const pending = Context.get(PENDING_WRITES_KEY) as
|
||||
| Set<Promise<void>>
|
||||
| undefined;
|
||||
Set<Promise<void>> | undefined;
|
||||
if (!pending?.size) return hold.release();
|
||||
void Promise.all([...pending])
|
||||
.then(() => hold.release())
|
||||
@@ -296,11 +294,23 @@ export function withAiCostFactor(
|
||||
};
|
||||
|
||||
const utilRecordUsageObject = <T extends Record<string, number>>(
|
||||
trackedUsageObject: T,
|
||||
reportedUsageObject: T,
|
||||
actor: Actor,
|
||||
modelPrefix: string,
|
||||
costsOverrides?: Partial<Record<keyof T, number>>,
|
||||
): Promise<UsageByType> => {
|
||||
// Counts come from the provider's answer. One that isn't a positive
|
||||
// number records nothing, instead of the ledger's one-unit fallback.
|
||||
const trackedUsageObject = Object.fromEntries(
|
||||
Object.entries(reportedUsageObject).map(([key, amount]) => [
|
||||
key,
|
||||
typeof amount === 'number' &&
|
||||
amount > 0 &&
|
||||
amount <= Number.MAX_SAFE_INTEGER
|
||||
? amount
|
||||
: 0,
|
||||
]),
|
||||
) as T;
|
||||
noteRequestCosts(
|
||||
Object.entries(trackedUsageObject).map(([key, usageAmount]) => ({
|
||||
usageType: key,
|
||||
@@ -316,8 +326,7 @@ export function withAiCostFactor(
|
||||
modelPrefix,
|
||||
costsOverrides,
|
||||
);
|
||||
// Recorded after the lookup; callers may add keys to theirs meanwhile.
|
||||
const usageObject = { ...trackedUsageObject };
|
||||
const usageObject = trackedUsageObject;
|
||||
const scaleOverrides = (factor: number) =>
|
||||
factor === 1
|
||||
? costsOverrides
|
||||
|
||||
Reference in new issue
Block a user