mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-03 02:59:07 +08:00
refacto: reasoning cache implementation and update import paths
This commit is contained in:
parent
9a72a8df2c
commit
1723d72b29
@ -38,7 +38,7 @@ import type {
|
|||||||
import { loggerService } from '@logger'
|
import { loggerService } from '@logger'
|
||||||
import { type FinishReason, type LanguageModelUsage, type TextStreamPart, type ToolSet } from 'ai'
|
import { type FinishReason, type LanguageModelUsage, type TextStreamPart, type ToolSet } from 'ai'
|
||||||
|
|
||||||
import { googleReasoningCache, openRouterReasoningCache } from '../../services/CacheService'
|
import { googleReasoningCache, openRouterReasoningCache } from '../services/reasoning-cache'
|
||||||
|
|
||||||
const logger = loggerService.withContext('AiSdkToAnthropicSSE')
|
const logger = loggerService.withContext('AiSdkToAnthropicSSE')
|
||||||
|
|
||||||
|
|||||||
45
src/main/apiServer/services/reasoning-cache.ts
Normal file
45
src/main/apiServer/services/reasoning-cache.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/**
|
||||||
|
* Reasoning Cache Service
|
||||||
|
*
|
||||||
|
* Manages reasoning-related caching for AI providers that support thinking/reasoning modes.
|
||||||
|
* This includes Google Gemini's thought signatures and OpenRouter's reasoning details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type { ReasoningDetailUnion } from '@main/apiServer/adapters/openrouter'
|
||||||
|
import { CacheService } from '@main/services/CacheService'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface for reasoning cache
|
||||||
|
*/
|
||||||
|
export interface IReasoningCache<T> {
|
||||||
|
set(key: string, value: T): void
|
||||||
|
get(key: string): T | undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cache duration: 30 minutes
|
||||||
|
* Reasoning data is typically only needed within a short conversation context
|
||||||
|
*/
|
||||||
|
const REASONING_CACHE_DURATION = 30 * 60 * 1000
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Google Gemini reasoning cache
|
||||||
|
*
|
||||||
|
* Stores thought signatures for Gemini 3 models to handle multi-turn conversations
|
||||||
|
* where the model needs to maintain thinking context across tool calls.
|
||||||
|
*/
|
||||||
|
export const googleReasoningCache: IReasoningCache<string> = {
|
||||||
|
set: (key, value) => CacheService.set(`google-reasoning:${key}`, value, REASONING_CACHE_DURATION),
|
||||||
|
get: (key) => CacheService.get(`google-reasoning:${key}`) || undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OpenRouter reasoning cache
|
||||||
|
*
|
||||||
|
* Stores reasoning details from OpenRouter responses to preserve thinking tokens
|
||||||
|
* and reasoning metadata across the conversation flow.
|
||||||
|
*/
|
||||||
|
export const openRouterReasoningCache: IReasoningCache<ReasoningDetailUnion[]> = {
|
||||||
|
set: (key, value) => CacheService.set(`openrouter-reasoning:${key}`, value, REASONING_CACHE_DURATION),
|
||||||
|
get: (key) => CacheService.get(`openrouter-reasoning:${key}`) || undefined
|
||||||
|
}
|
||||||
@ -42,7 +42,7 @@ import { net } from 'electron'
|
|||||||
import type { Response } from 'express'
|
import type { Response } from 'express'
|
||||||
import * as z from 'zod'
|
import * as z from 'zod'
|
||||||
|
|
||||||
import { googleReasoningCache, openRouterReasoningCache } from '../../services/CacheService'
|
import { googleReasoningCache, openRouterReasoningCache } from './reasoning-cache'
|
||||||
|
|
||||||
const logger = loggerService.withContext('UnifiedMessagesService')
|
const logger = loggerService.withContext('UnifiedMessagesService')
|
||||||
|
|
||||||
|
|||||||
@ -1,19 +1,9 @@
|
|||||||
import type { ReasoningDetailUnion } from '@main/apiServer/adapters/openrouter'
|
|
||||||
|
|
||||||
interface CacheItem<T> {
|
interface CacheItem<T> {
|
||||||
data: T
|
data: T
|
||||||
timestamp: number
|
timestamp: number
|
||||||
duration: number
|
duration: number
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface for reasoning cache
|
|
||||||
*/
|
|
||||||
export interface IReasoningCache<T> {
|
|
||||||
set(key: string, value: T): void
|
|
||||||
get(key: string): T | undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
export class CacheService {
|
export class CacheService {
|
||||||
private static cache: Map<string, CacheItem<any>> = new Map()
|
private static cache: Map<string, CacheItem<any>> = new Map()
|
||||||
|
|
||||||
@ -82,14 +72,3 @@ export class CacheService {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Singleton cache instances using CacheService
|
|
||||||
export const googleReasoningCache: IReasoningCache<string> = {
|
|
||||||
set: (key, value) => CacheService.set(`google-reasoning:${key}`, value, 30 * 60 * 1000),
|
|
||||||
get: (key) => CacheService.get(`google-reasoning:${key}`) || undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
export const openRouterReasoningCache: IReasoningCache<ReasoningDetailUnion[]> = {
|
|
||||||
set: (key, value) => CacheService.set(`openrouter-reasoning:${key}`, value, 30 * 60 * 1000),
|
|
||||||
get: (key) => CacheService.get(`openrouter-reasoning:${key}`) || undefined
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user