fix(web): add prompt cache affinity to canvas agent (#74)
Co-authored-by: Tiantianr <260026737+Tiantianr@users.noreply.github.com>
This commit is contained in:
17
web/src/lib/openai-prompt-cache.ts
Normal file
17
web/src/lib/openai-prompt-cache.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
export const OPENAI_PROMPT_CACHE_KEY_MAX_LENGTH = 64;
|
||||
|
||||
export function clampOpenAIPromptCacheKey(value?: string) {
|
||||
const key = value?.trim();
|
||||
if (!key) return undefined;
|
||||
return Array.from(key).slice(0, OPENAI_PROMPT_CACHE_KEY_MAX_LENGTH).join("");
|
||||
}
|
||||
|
||||
export function canvasAgentPromptCacheKey(sessionId: string) {
|
||||
const value = sessionId.trim();
|
||||
return value ? clampOpenAIPromptCacheKey(`canvas-agent:${value}`) : undefined;
|
||||
}
|
||||
|
||||
export function withOpenAIPromptCacheKey<T extends Record<string, unknown>>(body: T, value?: string): T & { prompt_cache_key?: string } {
|
||||
const key = clampOpenAIPromptCacheKey(value);
|
||||
return key ? { ...body, prompt_cache_key: key } : body;
|
||||
}
|
||||
Reference in New Issue
Block a user