fix(provider): select prompt cache keys by SDK (#38424)
This commit is contained in:
@@ -58,6 +58,28 @@ function sdkKey(npm: string): string | undefined {
|
||||
return "vertex"
|
||||
case "@ai-sdk/google":
|
||||
return "google"
|
||||
case "@ai-sdk/alibaba":
|
||||
return "alibaba"
|
||||
case "@ai-sdk/cerebras":
|
||||
return "cerebras"
|
||||
case "@ai-sdk/cohere":
|
||||
return "cohere"
|
||||
case "@ai-sdk/deepinfra":
|
||||
return "deepinfra"
|
||||
case "@ai-sdk/groq":
|
||||
return "groq"
|
||||
case "@ai-sdk/mistral":
|
||||
return "mistral"
|
||||
case "@ai-sdk/perplexity":
|
||||
return "perplexity"
|
||||
case "@ai-sdk/togetherai":
|
||||
return "togetherai"
|
||||
case "@ai-sdk/vercel":
|
||||
return "vercel"
|
||||
case "@ai-sdk/xai":
|
||||
return "xai"
|
||||
case "venice-ai-sdk-provider":
|
||||
return "venice"
|
||||
case "@ai-sdk/gateway":
|
||||
return "gateway"
|
||||
case "@openrouter/ai-sdk-provider":
|
||||
@@ -442,6 +464,9 @@ function mapProviderOptions(
|
||||
export function message(msgs: ModelMessage[], model: Provider.Model, options: Record<string, unknown>) {
|
||||
msgs = unsupportedParts(msgs, model)
|
||||
msgs = normalizeMessages(msgs, model, options)
|
||||
const usesAnthropicAutomaticCaching =
|
||||
options.cacheControl !== undefined &&
|
||||
(model.api.npm === "@ai-sdk/anthropic" || model.api.npm === "@ai-sdk/google-vertex/anthropic")
|
||||
if (
|
||||
(model.providerID === "anthropic" ||
|
||||
model.providerID === "google-vertex-anthropic" ||
|
||||
@@ -451,7 +476,8 @@ export function message(msgs: ModelMessage[], model: Provider.Model, options: Re
|
||||
model.id.includes("claude") ||
|
||||
model.api.npm === "@ai-sdk/anthropic" ||
|
||||
model.api.npm === "@ai-sdk/alibaba") &&
|
||||
model.api.npm !== "@ai-sdk/gateway"
|
||||
model.api.npm !== "@ai-sdk/gateway" &&
|
||||
!usesAnthropicAutomaticCaching
|
||||
) {
|
||||
msgs = applyCaching(msgs, model)
|
||||
}
|
||||
@@ -1137,7 +1163,6 @@ export function options(input: {
|
||||
|
||||
if (input.model.api.npm === "@ai-sdk/azure") {
|
||||
result["store"] = false
|
||||
result["promptCacheKey"] = input.sessionID
|
||||
}
|
||||
|
||||
if (input.model.api.npm === "@openrouter/ai-sdk-provider" || input.model.api.npm === "@llmgateway/ai-sdk-provider") {
|
||||
@@ -1166,16 +1191,6 @@ export function options(input: {
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
input.providerOptions?.setCacheKey !== false &&
|
||||
(input.model.providerID === "openai" ||
|
||||
input.model.api.npm === "@ai-sdk/openai" ||
|
||||
input.model.api.npm === "@ai-sdk/xai" ||
|
||||
input.providerOptions?.setCacheKey)
|
||||
) {
|
||||
result["promptCacheKey"] = input.sessionID
|
||||
}
|
||||
|
||||
if (input.model.providerID === "meta" && input.model.api.npm === "@ai-sdk/openai") {
|
||||
result["reasoningSummary"] = "auto"
|
||||
result["include"] = INCLUDE_ENCRYPTED_REASONING
|
||||
@@ -1224,6 +1239,25 @@ export function options(input: {
|
||||
result["enable_thinking"] = true
|
||||
}
|
||||
|
||||
if (input.providerOptions?.setCacheKey !== false) {
|
||||
if (input.model.api.npm === "@ai-sdk/deepinfra" || input.model.api.npm === "@ai-sdk/cerebras") {
|
||||
result["prompt_cache_key"] = input.sessionID
|
||||
} else if (
|
||||
input.model.api.npm === "@ai-sdk/openai" ||
|
||||
input.model.api.npm === "@ai-sdk/azure" ||
|
||||
input.model.api.npm === "@ai-sdk/xai" ||
|
||||
input.model.api.npm === "@ai-sdk/mistral" ||
|
||||
input.model.api.npm === "venice-ai-sdk-provider" ||
|
||||
input.providerOptions?.setCacheKey === true
|
||||
) {
|
||||
result["promptCacheKey"] = input.sessionID
|
||||
}
|
||||
}
|
||||
|
||||
if (input.model.api.npm === "@ai-sdk/gateway") {
|
||||
result["gateway"] = { caching: "auto" }
|
||||
}
|
||||
|
||||
if (input.model.api.npm === "@ai-sdk/azure" && input.model.api.id.includes("gpt-5.5")) {
|
||||
result["reasoningSummary"] = "auto"
|
||||
return result
|
||||
@@ -1256,26 +1290,13 @@ export function options(input: {
|
||||
result["textVerbosity"] = "low"
|
||||
}
|
||||
|
||||
if (input.model.providerID.startsWith("opencode")) {
|
||||
if (input.model.providerID.startsWith("opencode") && input.providerOptions?.setCacheKey !== false) {
|
||||
result["promptCacheKey"] = input.sessionID
|
||||
result["include"] = INCLUDE_ENCRYPTED_REASONING
|
||||
result["reasoningSummary"] = "auto"
|
||||
}
|
||||
}
|
||||
|
||||
if (input.model.providerID === "venice") {
|
||||
result["promptCacheKey"] = input.sessionID
|
||||
}
|
||||
|
||||
if (input.model.providerID === "openrouter") {
|
||||
result["prompt_cache_key"] = input.sessionID
|
||||
}
|
||||
if (input.model.api.npm === "@ai-sdk/gateway") {
|
||||
result["gateway"] = {
|
||||
caching: "auto",
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user