refactor(llm): share cache retention resolution · openclaw/openclaw@536c8a8
vincentkoc
·
2026-06-22
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -61,27 +61,14 @@ import {
|
61 | 61 | ANTHROPIC_OMITTED_REASONING_TEXT, |
62 | 62 | findActiveAnthropicToolTurnAssistantIndex, |
63 | 63 | } from "./anthropic-thinking-replay.js"; |
| 64 | +import { resolveCacheRetention } from "./cache-retention.js"; |
64 | 65 | import { resolveCloudflareBaseUrl } from "./cloudflare.js"; |
65 | 66 | import { buildCopilotDynamicHeaders, hasCopilotVisionInput } from "./github-copilot-headers.js"; |
66 | 67 | import { adjustMaxTokensForThinking, buildBaseOptions } from "./simple-options.js"; |
67 | 68 | import { transformMessages } from "./transform-messages.js"; |
68 | 69 | |
69 | 70 | const ANTHROPIC_CACHE_CONTROL_LIMIT = 4; |
70 | 71 | |
71 | | -/** |
72 | | - * Resolve cache retention preference. |
73 | | - * Defaults to "short" and uses OPENCLAW_CACHE_RETENTION for backward compatibility. |
74 | | - */ |
75 | | -function resolveCacheRetention(cacheRetention?: CacheRetention): CacheRetention { |
76 | | -if (cacheRetention) { |
77 | | -return cacheRetention; |
78 | | -} |
79 | | -if (typeof process !== "undefined" && process.env.OPENCLAW_CACHE_RETENTION === "long") { |
80 | | -return "long"; |
81 | | -} |
82 | | -return "short"; |
83 | | -} |
84 | | - |
85 | 72 | function getCacheControl( |
86 | 73 | model: Model<"anthropic-messages">, |
87 | 74 | cacheRetention?: CacheRetention, |
|
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +import type { CacheRetention } from "../types.js"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Resolve cache retention preference. |
| 5 | + * Defaults to "short" and uses OPENCLAW_CACHE_RETENTION for backward compatibility. |
| 6 | + */ |
| 7 | +export function resolveCacheRetention(cacheRetention?: CacheRetention): CacheRetention { |
| 8 | +if (cacheRetention) { |
| 9 | +return cacheRetention; |
| 10 | +} |
| 11 | +if (typeof process !== "undefined" && process.env.OPENCLAW_CACHE_RETENTION === "long") { |
| 12 | +return "long"; |
| 13 | +} |
| 14 | +return "short"; |
| 15 | +} |
| Original file line number | Diff line number | Diff line change |
|---|
@@ -46,6 +46,7 @@ import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
46 | 46 | import { headersToRecord } from "../utils/headers.js"; |
47 | 47 | import { parseStreamingJson } from "../utils/json-parse.js"; |
48 | 48 | import { sanitizeSurrogates } from "../utils/sanitize-unicode.js"; |
| 49 | +import { resolveCacheRetention } from "./cache-retention.js"; |
49 | 50 | import { isCloudflareProvider, resolveCloudflareBaseUrl } from "./cloudflare.js"; |
50 | 51 | import { buildCopilotDynamicHeaders, hasCopilotVisionInput } from "./github-copilot-headers.js"; |
51 | 52 | import { clampOpenAIPromptCacheKey } from "./openai-prompt-cache.js"; |
@@ -118,16 +119,6 @@ type ChatCompletionToolWithCacheControl = OpenAI.Chat.Completions.ChatCompletion
|
118 | 119 | cache_control?: OpenAICompatCacheControl; |
119 | 120 | }; |
120 | 121 | |
121 | | -function resolveCacheRetention(cacheRetention?: CacheRetention): CacheRetention { |
122 | | -if (cacheRetention) { |
123 | | -return cacheRetention; |
124 | | -} |
125 | | -if (typeof process !== "undefined" && process.env.OPENCLAW_CACHE_RETENTION === "long") { |
126 | | -return "long"; |
127 | | -} |
128 | | -return "short"; |
129 | | -} |
130 | | - |
131 | 122 | export const streamOpenAICompletions: StreamFunction< |
132 | 123 | "openai-completions", |
133 | 124 | OpenAICompletionsOptions |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -13,6 +13,7 @@ import type {
|
13 | 13 | Usage, |
14 | 14 | } from "../types.js"; |
15 | 15 | import { AssistantMessageEventStream } from "../utils/event-stream.js"; |
| 16 | +import { resolveCacheRetention } from "./cache-retention.js"; |
16 | 17 | import { isCloudflareProvider, resolveCloudflareBaseUrl } from "./cloudflare.js"; |
17 | 18 | import { buildCopilotDynamicHeaders, hasCopilotVisionInput } from "./github-copilot-headers.js"; |
18 | 19 | import { clampOpenAIPromptCacheKey } from "./openai-prompt-cache.js"; |
@@ -27,20 +28,6 @@ import { buildBaseOptions } from "./simple-options.js";
|
27 | 28 | |
28 | 29 | const OPENAI_TOOL_CALL_PROVIDERS = new Set(["openai", "opencode"]); |
29 | 30 | |
30 | | -/** |
31 | | - * Resolve cache retention preference. |
32 | | - * Defaults to "short" and uses OPENCLAW_CACHE_RETENTION for backward compatibility. |
33 | | - */ |
34 | | -function resolveCacheRetention(cacheRetention?: CacheRetention): CacheRetention { |
35 | | -if (cacheRetention) { |
36 | | -return cacheRetention; |
37 | | -} |
38 | | -if (typeof process !== "undefined" && process.env.OPENCLAW_CACHE_RETENTION === "long") { |
39 | | -return "long"; |
40 | | -} |
41 | | -return "short"; |
42 | | -} |
43 | | - |
44 | 31 | function getCompat(model: Model<"openai-responses">): Required<OpenAIResponsesCompat> { |
45 | 32 | return { |
46 | 33 | sendSessionIdHeader: model.compat?.sendSessionIdHeader ?? true, |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。