Files
adnify/tests/main/usageConversion.test.ts
adnaan 4da1e487c8 feat(llm): refactor prompt caching with capability detection and cache analysis
- Replace PromptCache with new CacheAnalysis and CacheStrategies modules for improved cache management
- Add capability detection system to LLMConfig for provider-specific features (OpenAI reasoning, Google thinking mode, prompt cache retention)
- Implement cache source tracking (provider-reported vs derived/estimated) in token usage reporting
- Add StablePrefixAnalysis and TextOnlyPrefixAnalysis for intelligent message prefix caching
- Introduce CacheStrategies with provider-specific implementations (OpenAI, Google, Anthropic)
- Update RequestCache, RequestExecution, and RequestSettings to use new cache strategy system
- Enhance StreamingService, StructuredService, and SyncService with improved cache handling
- Add ThinkingStrategy for managing reasoning token budgets across providers
- Update MessageConverter to support new cache analysis capabilities
- Add ProviderCompatibility checks for cache feature support
- Extend ContextStatsContent UI to display cache source information
- Add llmCapabilities test suite for provider feature detection
- Update agent loop and stream handling for new cache strategy integration
- Improve token usage conversion in IPC layer with cache source metadata
- Consolidate LLM configuration types and persistence logic
2026-04-30 21:51:42 +08:00

87 lines
2.2 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { convertUsage } from '@main/services/llm/types'
describe('convertUsage', () => {
it('prefers AI SDK standard cache fields when available', () => {
const usage = {
inputTokens: 100,
outputTokens: 20,
totalTokens: 120,
inputTokenDetails: {
cacheReadTokens: 60,
cacheWriteTokens: 40,
},
outputTokenDetails: {
reasoningTokens: 5,
},
} as any
expect(convertUsage(usage)).toEqual({
inputTokens: 100,
outputTokens: 20,
totalTokens: 120,
cachedInputTokens: 60,
cacheWriteTokens: 40,
reasoningTokens: 5,
cacheReadSource: 'provider-reported',
cacheWriteSource: 'provider-reported',
})
})
it('falls back to anthropic raw cache fields', () => {
const usage = {
inputTokens: 180,
outputTokens: 20,
totalTokens: 200,
inputTokenDetails: {
cacheReadTokens: undefined,
cacheWriteTokens: undefined,
},
raw: {
cache_read_input_tokens: 70,
cache_creation_input_tokens: 30,
},
} as any
expect(convertUsage(usage)).toEqual({
inputTokens: 180,
outputTokens: 20,
totalTokens: 200,
cachedInputTokens: 70,
cacheWriteTokens: 30,
reasoningTokens: undefined,
cacheReadSource: 'provider-reported',
cacheWriteSource: 'provider-reported',
})
})
it('falls back to openai and google raw cache read fields', () => {
const openAIUsage = {
inputTokens: 120,
outputTokens: 30,
totalTokens: 150,
inputTokenDetails: {},
raw: {
input_tokens_details: {
cached_tokens: 50,
},
},
} as any
const googleUsage = {
inputTokens: 140,
outputTokens: 25,
totalTokens: 165,
inputTokenDetails: {},
raw: {
cachedContentTokenCount: 45,
},
} as any
expect(convertUsage(openAIUsage).cachedInputTokens).toBe(50)
expect(convertUsage(openAIUsage).cacheWriteTokens).toBe(0)
expect(convertUsage(googleUsage).cachedInputTokens).toBe(45)
expect(convertUsage(googleUsage).cacheWriteTokens).toBe(0)
})
})