feat(ai): support message-anchored tool loading (#6474)
This adds cache-friendly dynamic tool loading anchored to tool results. Purely additive active-tool changes are recorded with `addedToolNames`, allowing supported Anthropic and OpenAI Responses models to load tool definitions at the point they become available instead of placing them in the cached prompt prefix. It retains safe fallback behavior for unsupported models and non-additive changes but it will wipe caches.
This commit is contained in:
@@ -9,7 +9,14 @@ import {
|
||||
CLOUDFLARE_AI_GATEWAY_OPENAI_BASE_URL,
|
||||
CLOUDFLARE_WORKERS_AI_BASE_URL,
|
||||
} from "../src/api/cloudflare.ts";
|
||||
import type { AnthropicMessagesCompat, Api, KnownProvider, Model, OpenAICompletionsCompat } from "../src/types.ts";
|
||||
import type {
|
||||
AnthropicMessagesCompat,
|
||||
Api,
|
||||
KnownProvider,
|
||||
Model,
|
||||
OpenAICompletionsCompat,
|
||||
OpenAIResponsesCompat,
|
||||
} from "../src/types.ts";
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
@@ -194,6 +201,15 @@ const ANT_LING_RING_THINKING_LEVEL_MAP = {
|
||||
} as const;
|
||||
|
||||
const MODELS_DEV_OPENAI_UNSUPPORTED_MODEL_IDS = new Set(["gpt-5.6"]);
|
||||
const OPENAI_TOOL_SEARCH_MODEL_IDS = new Set([
|
||||
"gpt-5.4",
|
||||
"gpt-5.4-mini",
|
||||
"gpt-5.4-pro",
|
||||
"gpt-5.5",
|
||||
"gpt-5.6-sol",
|
||||
"gpt-5.6-terra",
|
||||
"gpt-5.6-luna",
|
||||
]);
|
||||
const OPENAI_LONG_CONTEXT_INPUT_THRESHOLD = 272000;
|
||||
const OPENAI_SHORT_CONTEXT_CAPPED_MODEL_IDS = new Set([
|
||||
"gpt-5.4",
|
||||
@@ -482,6 +498,16 @@ function applyOpenAICompletionsCompatMetadata(model: Model<Api>): void {
|
||||
}
|
||||
}
|
||||
|
||||
function applyOpenAIToolSearchMetadata(model: Model<Api>): void {
|
||||
const isOpenAIResponses = model.provider === "openai" && model.api === "openai-responses";
|
||||
const isOpenAICodex = model.provider === "openai-codex" && model.api === "openai-codex-responses";
|
||||
if (!(isOpenAIResponses || isOpenAICodex) || !OPENAI_TOOL_SEARCH_MODEL_IDS.has(model.id)) return;
|
||||
model.compat = {
|
||||
...(model.compat as OpenAIResponsesCompat | undefined),
|
||||
supportsToolSearch: true,
|
||||
};
|
||||
}
|
||||
|
||||
function isGemini3ProModel(modelId: string): boolean {
|
||||
return /gemini-3(?:\.\d+)?-pro/.test(modelId.toLowerCase());
|
||||
}
|
||||
@@ -2178,6 +2204,7 @@ async function generateModels() {
|
||||
for (const model of allModels) {
|
||||
applyThinkingLevelMetadata(model);
|
||||
applyOpenAICompletionsCompatMetadata(model);
|
||||
applyOpenAIToolSearchMetadata(model);
|
||||
}
|
||||
|
||||
// Group by provider and deduplicate by model ID
|
||||
|
||||
Reference in New Issue
Block a user