* feat(ai): add Cloudflare AI Gateway as a provider
Routes through Cloudflare's Unified API (`/compat`) for Workers AI and
Anthropic models, and through the provider-specific `/openai` subpath
for OpenAI models so reasoning models (gpt-5.x, o-series) can hit
`/v1/responses` natively. Once `/compat` adds Responses-API support,
the OpenAI subpath can be folded back in.
Catalog layout:
workers-ai/@cf/... -> openai-completions, gateway/.../compat
anthropic/... -> openai-completions, gateway/.../compat
<native-id> -> openai-responses, gateway/.../openai
(gpt-5.1, claude-... no, sorry: gpt-5.x and o-series only;
prefix stripped because the OpenAI SDK posts native ids)
Touches:
packages/ai/src/types.ts add cloudflare-ai-gateway to KnownProvider
packages/ai/src/env-api-keys.ts map to CLOUDFLARE_API_KEY
packages/ai/src/providers/cloudflare.ts add CLOUDFLARE_AI_GATEWAY_COMPAT_BASE_URL
and CLOUDFLARE_AI_GATEWAY_OPENAI_BASE_URL
packages/ai/src/providers/openai-responses.ts one-line dispatch through resolveCloudflareBaseUrl
(matches what openai-completions.ts already does)
packages/ai/scripts/generate-models.ts branch openai/* vs workers-ai/anthropic/*
packages/ai/src/models.generated.ts spliced 34 entries
packages/ai/test/stream.test.ts 3 e2e blocks (one per upstream)
packages/coding-agent/* defaultModelPerProvider, login, env docs,
README, providers.md
Verified end-to-end against a real Cloudflare account with unified
billing: 9/9 e2e tests pass across all three upstreams (Workers AI
Kimi K2.6, OpenAI gpt-5.1 reasoning, Anthropic claude-sonnet-4-5).
* refactor(ai): move AI Gateway User-Agent and per-route session-affinity flag to catalog
Mirrors the same per-model metadata refactor done for Workers AI in the
parent branch. All cloudflare-ai-gateway entries get the User-Agent
header. Only workers-ai/* gateway entries set
`compat.sendSessionAffinityHeaders: true` because the gateway
forwards that header to the underlying Workers AI runtime; anthropic/*
upstream and openai/* (openai-responses) don't use it.
packages/ai/scripts/generate-models.ts: emit headers (always) and
per-upstream compat (workers-ai only) on each cloudflare-ai-gateway
entry.
packages/ai/src/models.generated.ts: re-spliced 35 entries with
headers + conditional compat.
Behavior unchanged - 9/9 e2e tests pass across all three upstream
families.
* fix(ai): align AI Gateway with telemetry-aware UA helper
Adapts to badlogic/pi-mono#3851's follow-up fix ("honor telemetry for
Cloudflare attribution headers", fbb5eed) which moved the
'User-Agent: pi-coding-agent' header out of per-model catalog metadata
and into a centralized telemetry-honoring helper
(coding-agent/src/core/sdk.ts:getAttributionHeaders).
- packages/coding-agent/src/core/sdk.ts: extend the cloudflare branch of
getAttributionHeaders to also match cloudflare-ai-gateway and
gateway.ai.cloudflare.com.
- packages/ai/scripts/generate-models.ts and src/models.generated.ts:
drop 'headers' from the 35 cloudflare-ai-gateway entries (constant
CLOUDFLARE_STATIC_HEADERS no longer exists). Per-route
compat.sendSessionAffinityHeaders is unchanged.
End-to-end behavior unchanged: 9/9 tests still pass across all three
upstream families (Workers AI, Anthropic, OpenAI Responses).
---------
Co-authored-by: Mario Zechner <badlogicgames@gmail.com>
281 lines
8.9 KiB
TypeScript
281 lines
8.9 KiB
TypeScript
import OpenAI from "openai";
|
|
import type { ResponseCreateParamsStreaming } from "openai/resources/responses/responses.js";
|
|
import { getEnvApiKey } from "../env-api-keys.js";
|
|
import { supportsXhigh } from "../models.js";
|
|
import type {
|
|
Api,
|
|
AssistantMessage,
|
|
CacheRetention,
|
|
Context,
|
|
Model,
|
|
OpenAIResponsesCompat,
|
|
SimpleStreamOptions,
|
|
StreamFunction,
|
|
StreamOptions,
|
|
Usage,
|
|
} from "../types.js";
|
|
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
import { headersToRecord } from "../utils/headers.js";
|
|
import { isCloudflareProvider, resolveCloudflareBaseUrl } from "./cloudflare.js";
|
|
import { buildCopilotDynamicHeaders, hasCopilotVisionInput } from "./github-copilot-headers.js";
|
|
import { convertResponsesMessages, convertResponsesTools, processResponsesStream } from "./openai-responses-shared.js";
|
|
import { buildBaseOptions, clampReasoning } from "./simple-options.js";
|
|
|
|
const OPENAI_TOOL_CALL_PROVIDERS = new Set(["openai", "openai-codex", "opencode"]);
|
|
|
|
/**
|
|
* Resolve cache retention preference.
|
|
* Defaults to "short" and uses PI_CACHE_RETENTION for backward compatibility.
|
|
*/
|
|
function resolveCacheRetention(cacheRetention?: CacheRetention): CacheRetention {
|
|
if (cacheRetention) {
|
|
return cacheRetention;
|
|
}
|
|
if (typeof process !== "undefined" && process.env.PI_CACHE_RETENTION === "long") {
|
|
return "long";
|
|
}
|
|
return "short";
|
|
}
|
|
|
|
function getCompat(model: Model<"openai-responses">): Required<OpenAIResponsesCompat> {
|
|
return {
|
|
sendSessionIdHeader: model.compat?.sendSessionIdHeader ?? true,
|
|
supportsLongCacheRetention: model.compat?.supportsLongCacheRetention ?? true,
|
|
};
|
|
}
|
|
|
|
function getPromptCacheRetention(
|
|
compat: Required<OpenAIResponsesCompat>,
|
|
cacheRetention: CacheRetention,
|
|
): "24h" | undefined {
|
|
return cacheRetention === "long" && compat.supportsLongCacheRetention ? "24h" : undefined;
|
|
}
|
|
|
|
// OpenAI Responses-specific options
|
|
export interface OpenAIResponsesOptions extends StreamOptions {
|
|
reasoningEffort?: "minimal" | "low" | "medium" | "high" | "xhigh";
|
|
reasoningSummary?: "auto" | "detailed" | "concise" | null;
|
|
serviceTier?: ResponseCreateParamsStreaming["service_tier"];
|
|
}
|
|
|
|
/**
|
|
* Generate function for OpenAI Responses API
|
|
*/
|
|
export const streamOpenAIResponses: StreamFunction<"openai-responses", OpenAIResponsesOptions> = (
|
|
model: Model<"openai-responses">,
|
|
context: Context,
|
|
options?: OpenAIResponsesOptions,
|
|
): AssistantMessageEventStream => {
|
|
const stream = new AssistantMessageEventStream();
|
|
|
|
// Start async processing
|
|
(async () => {
|
|
const output: AssistantMessage = {
|
|
role: "assistant",
|
|
content: [],
|
|
api: model.api as Api,
|
|
provider: model.provider,
|
|
model: model.id,
|
|
usage: {
|
|
input: 0,
|
|
output: 0,
|
|
cacheRead: 0,
|
|
cacheWrite: 0,
|
|
totalTokens: 0,
|
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
},
|
|
stopReason: "stop",
|
|
timestamp: Date.now(),
|
|
};
|
|
|
|
try {
|
|
// Create OpenAI client
|
|
const apiKey = options?.apiKey || getEnvApiKey(model.provider) || "";
|
|
const cacheRetention = resolveCacheRetention(options?.cacheRetention);
|
|
const cacheSessionId = cacheRetention === "none" ? undefined : options?.sessionId;
|
|
const client = createClient(model, context, apiKey, options?.headers, cacheSessionId);
|
|
let params = buildParams(model, context, options);
|
|
const nextParams = await options?.onPayload?.(params, model);
|
|
if (nextParams !== undefined) {
|
|
params = nextParams as ResponseCreateParamsStreaming;
|
|
}
|
|
const requestOptions = {
|
|
...(options?.signal ? { signal: options.signal } : {}),
|
|
...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),
|
|
...(options?.maxRetries !== undefined ? { maxRetries: options.maxRetries } : {}),
|
|
};
|
|
const { data: openaiStream, response } = await client.responses.create(params, requestOptions).withResponse();
|
|
await options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);
|
|
stream.push({ type: "start", partial: output });
|
|
|
|
await processResponsesStream(openaiStream, output, stream, model, {
|
|
serviceTier: options?.serviceTier,
|
|
applyServiceTierPricing: (usage, serviceTier) => applyServiceTierPricing(usage, serviceTier, model),
|
|
});
|
|
|
|
if (options?.signal?.aborted) {
|
|
throw new Error("Request was aborted");
|
|
}
|
|
|
|
if (output.stopReason === "aborted" || output.stopReason === "error") {
|
|
throw new Error("An unknown error occurred");
|
|
}
|
|
|
|
stream.push({ type: "done", reason: output.stopReason, message: output });
|
|
stream.end();
|
|
} catch (error) {
|
|
for (const block of output.content) {
|
|
delete (block as { index?: number }).index;
|
|
// partialJson is only a streaming scratch buffer; never persist it.
|
|
delete (block as { partialJson?: string }).partialJson;
|
|
}
|
|
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
|
|
stream.push({ type: "error", reason: output.stopReason, error: output });
|
|
stream.end();
|
|
}
|
|
})();
|
|
|
|
return stream;
|
|
};
|
|
|
|
export const streamSimpleOpenAIResponses: StreamFunction<"openai-responses", SimpleStreamOptions> = (
|
|
model: Model<"openai-responses">,
|
|
context: Context,
|
|
options?: SimpleStreamOptions,
|
|
): AssistantMessageEventStream => {
|
|
const apiKey = options?.apiKey || getEnvApiKey(model.provider);
|
|
if (!apiKey) {
|
|
throw new Error(`No API key for provider: ${model.provider}`);
|
|
}
|
|
|
|
const base = buildBaseOptions(model, options, apiKey);
|
|
const reasoningEffort = supportsXhigh(model) ? options?.reasoning : clampReasoning(options?.reasoning);
|
|
|
|
return streamOpenAIResponses(model, context, {
|
|
...base,
|
|
reasoningEffort,
|
|
} satisfies OpenAIResponsesOptions);
|
|
};
|
|
|
|
function createClient(
|
|
model: Model<"openai-responses">,
|
|
context: Context,
|
|
apiKey?: string,
|
|
optionsHeaders?: Record<string, string>,
|
|
sessionId?: string,
|
|
) {
|
|
if (!apiKey) {
|
|
if (!process.env.OPENAI_API_KEY) {
|
|
throw new Error(
|
|
"OpenAI API key is required. Set OPENAI_API_KEY environment variable or pass it as an argument.",
|
|
);
|
|
}
|
|
apiKey = process.env.OPENAI_API_KEY;
|
|
}
|
|
|
|
const compat = getCompat(model);
|
|
const headers = { ...model.headers };
|
|
if (model.provider === "github-copilot") {
|
|
const hasImages = hasCopilotVisionInput(context.messages);
|
|
const copilotHeaders = buildCopilotDynamicHeaders({
|
|
messages: context.messages,
|
|
hasImages,
|
|
});
|
|
Object.assign(headers, copilotHeaders);
|
|
}
|
|
|
|
if (sessionId) {
|
|
if (compat.sendSessionIdHeader) {
|
|
headers.session_id = sessionId;
|
|
}
|
|
headers["x-client-request-id"] = sessionId;
|
|
}
|
|
|
|
// Merge options headers last so they can override defaults
|
|
if (optionsHeaders) {
|
|
Object.assign(headers, optionsHeaders);
|
|
}
|
|
|
|
return new OpenAI({
|
|
apiKey,
|
|
baseURL: isCloudflareProvider(model.provider) ? resolveCloudflareBaseUrl(model) : model.baseUrl,
|
|
dangerouslyAllowBrowser: true,
|
|
defaultHeaders: headers,
|
|
});
|
|
}
|
|
|
|
function buildParams(model: Model<"openai-responses">, context: Context, options?: OpenAIResponsesOptions) {
|
|
const messages = convertResponsesMessages(model, context, OPENAI_TOOL_CALL_PROVIDERS);
|
|
|
|
const cacheRetention = resolveCacheRetention(options?.cacheRetention);
|
|
const compat = getCompat(model);
|
|
const params: ResponseCreateParamsStreaming = {
|
|
model: model.id,
|
|
input: messages,
|
|
stream: true,
|
|
prompt_cache_key: cacheRetention === "none" ? undefined : options?.sessionId,
|
|
prompt_cache_retention: getPromptCacheRetention(compat, cacheRetention),
|
|
store: false,
|
|
};
|
|
|
|
if (options?.maxTokens) {
|
|
params.max_output_tokens = options?.maxTokens;
|
|
}
|
|
|
|
if (options?.temperature !== undefined) {
|
|
params.temperature = options?.temperature;
|
|
}
|
|
|
|
if (options?.serviceTier !== undefined) {
|
|
params.service_tier = options.serviceTier;
|
|
}
|
|
|
|
if (context.tools && context.tools.length > 0) {
|
|
params.tools = convertResponsesTools(context.tools);
|
|
}
|
|
|
|
if (model.reasoning) {
|
|
if (options?.reasoningEffort || options?.reasoningSummary) {
|
|
params.reasoning = {
|
|
effort: options?.reasoningEffort || "medium",
|
|
summary: options?.reasoningSummary || "auto",
|
|
};
|
|
params.include = ["reasoning.encrypted_content"];
|
|
} else if (model.provider !== "github-copilot") {
|
|
params.reasoning = { effort: "none" };
|
|
}
|
|
}
|
|
|
|
return params;
|
|
}
|
|
|
|
function getServiceTierCostMultiplier(
|
|
model: Pick<Model<"openai-responses">, "id">,
|
|
serviceTier: ResponseCreateParamsStreaming["service_tier"] | undefined,
|
|
): number {
|
|
switch (serviceTier) {
|
|
case "flex":
|
|
return 0.5;
|
|
case "priority":
|
|
return model.id === "gpt-5.5" ? 2.5 : 2;
|
|
default:
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
function applyServiceTierPricing(
|
|
usage: Usage,
|
|
serviceTier: ResponseCreateParamsStreaming["service_tier"] | undefined,
|
|
model: Pick<Model<"openai-responses">, "id">,
|
|
) {
|
|
const multiplier = getServiceTierCostMultiplier(model, serviceTier);
|
|
if (multiplier === 1) return;
|
|
|
|
usage.cost.input *= multiplier;
|
|
usage.cost.output *= multiplier;
|
|
usage.cost.cacheRead *= multiplier;
|
|
usage.cost.cacheWrite *= multiplier;
|
|
usage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
|
|
}
|