mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-21 15:41:17 +00:00
* fix(ai): preserve Catty prompt cache and provider usage * fix(ai): gate explicit caching on the official Anthropic endpoint * fix(ai): resolve provider defaults before enabling prompt caching
29 lines
1.3 KiB
TypeScript
29 lines
1.3 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import { normalizeOpenAIChatCacheUsage } from './openAIChatCacheUsage';
|
|
|
|
test('adapts documented DeepSeek cache usage while preserving the rest of the response', () => {
|
|
const response = { choices: [], usage: {
|
|
prompt_tokens: 1000, prompt_cache_hit_tokens: 900, prompt_cache_miss_tokens: 100,
|
|
prompt_tokens_details: { other: 1 }, completion_tokens: 5,
|
|
} };
|
|
const parsed = JSON.parse(normalizeOpenAIChatCacheUsage(JSON.stringify(response)));
|
|
assert.deepEqual(parsed, {
|
|
...response, usage: { ...response.usage, prompt_tokens_details: { other: 1, cached_tokens: 900 } },
|
|
});
|
|
});
|
|
|
|
test('preserves standard usage and rejects invalid cache counts without breaking stream data', () => {
|
|
for (const hit of [-1, 1.5, '900', 1001, null]) {
|
|
const data = JSON.stringify({ usage: { prompt_tokens: 1000, prompt_cache_hit_tokens: hit } });
|
|
assert.equal(normalizeOpenAIChatCacheUsage(data), data);
|
|
}
|
|
for (const data of ['[DONE]', 'not JSON', '{"usage":null}', '{"type":"message_start"}']) {
|
|
assert.equal(normalizeOpenAIChatCacheUsage(data), data);
|
|
}
|
|
const data = JSON.stringify({ usage: {
|
|
prompt_tokens: 1000, prompt_cache_hit_tokens: 900, prompt_tokens_details: { cached_tokens: 0 },
|
|
} });
|
|
assert.equal(normalizeOpenAIChatCacheUsage(data), data);
|
|
});
|