agents
Version:
A home for your AI agents
353 lines (351 loc) • 12.6 kB
TypeScript
import {
_ as DynamicRouteId,
a as CloudflareAIError$1,
b as WorkersAIModelId,
d as CatalogModelId,
f as CloudflareEmbeddingModelId,
g as CloudflareTranscriptionModelId,
h as CloudflareSpeechModelId,
i as CloudflareAIAttempt,
l as ModelOptions$1,
m as CloudflareRerankingModelId,
o as CloudflareAIErrorCode,
p as CloudflareImageModelId,
s as AISettings,
t as Transport$1,
u as ProviderGatewaySettings
} from "./transport-5moD2pW5.js";
import {
APICallError,
EmbeddingModelV4,
ImageModelV4,
LanguageModelV4,
LanguageModelV4CallOptions,
LanguageModelV4GenerateResult,
LanguageModelV4StreamResult,
RerankingModelV4,
SpeechModelV4,
TranscriptionModelV4
} from "@ai-sdk/provider";
//#region src/models/ai-sdk/settings.d.ts
/**
* A language model id: a Workers AI id, an AI Gateway catalog id
* (`<author>/<model>`), or a dynamic route (`dynamic/<route>`). All three run
* on `env.AI.run`.
*/
type CloudflareLanguageModelId =
| WorkersAIModelId
| DynamicRouteId
| (CatalogModelId & {});
/**
* One leg of a fallback chain: a language model id, or any AI SDK language
* model — which is routed through AI Gateway like `ai(model)`.
*/
type FallbackLeg = CloudflareLanguageModelId | LanguageModelV4;
/**
* Per-model and per-call options: the framework-neutral options, with the
* fallback leg type this module accepts.
*/
type ModelOptions = ModelOptions$1<FallbackLeg>;
/**
* Which key authenticates a vendor model object routed through AI Gateway.
*/
interface VendorKeyOptions {
/**
* `"gateway"` (the default) strips the key the vendor's provider put on the
* request, so the gateway supplies one: Unified Billing, or a BYOK key
* stored on the gateway. `"request"` forwards that key to the vendor, for a
* key you hold yourself, e.g. `createAnthropic({ apiKey: env.ANTHROPIC_KEY })`.
*
* It belongs to the model it is set on, so a fallback leg keeps its own.
*/
vendorKey?: "gateway" | "request";
}
/**
* The options accepted next to a model object — `ai(anthropic("…"), { … })`.
* {@link ModelOptions} plus {@link VendorKeyOptions.vendorKey}; the
* Workers-AI-only knobs (`reasoningEffort`, `chatTemplateKwargs`) are ignored
* with a warning.
*/
type GatewayModelOptions = ModelOptions & VendorKeyOptions;
/**
* The options `ai.routed()` takes: the gateway options, `headers`,
* `sessionAffinity` and `vendorKey`.
*
* A routed embedding, image, speech or transcription model is the vendor's
* own, and only its transport is ours — there is no chain to fall back through
* and no Workers AI reasoning knob to apply. They are absent from the type
* rather than dropped at runtime, so the mistake is a compile error naming the
* option.
*/
type RoutedModelOptions = Omit<
ModelOptions,
"fallback" | "reasoningEffort" | "chatTemplateKwargs"
> &
VendorKeyOptions;
/**
* The options the Workers-AI-only methods take — `ai.embedding`, `ai.image`,
* `ai.speech`, `ai.transcription`, `ai.reranking`.
*
* Their fallback legs are ids: those models run through the Workers AI run
* path, which resolves a leg by id, so a model object would type-check and
* then be silently dropped.
*/
type ModalityOptions = ModelOptions$1<WorkersAIModelId>;
//#endregion
//#region src/models/ai-sdk/errors.d.ts
/**
* Error thrown for every failed Cloudflare AI call, whether it came back as a
* non-2xx `Response` or as a thrown Workers AI binding error.
*
* Extends the AI SDK's `APICallError` so `ai`'s built-in retry loop honours
* {@link CloudflareAIError.isRetryable} without any extra wiring.
*/
declare class CloudflareAIError extends APICallError {
/** HTTP status, when the failure carried one. */
readonly status: number | undefined;
/** Coarse classification used for routing and fallback decisions. */
readonly code: CloudflareAIErrorCode;
/** `cf-aig-log-id` of the failed request, when the response carried one. */
readonly logId: string | undefined;
/** The model id that produced this error. */
readonly model: string;
/** Every leg tried, when this error ends a fallback chain. */
readonly attempts: CloudflareAIAttempt[] | undefined;
constructor(options: {
message: string;
model: string;
url: string;
requestBodyValues: unknown;
status?: number;
code: CloudflareAIErrorCode;
logId?: string;
responseHeaders?: Record<string, string>;
responseBody?: string;
isRetryable?: boolean;
data?: unknown;
cause?: unknown;
attempts?: CloudflareAIAttempt[];
});
/** Lifts the framework-neutral core error into the AI SDK error type. */
static fromCore(error: CloudflareAIError$1): CloudflareAIError;
}
//#endregion
//#region src/models/ai-sdk/transport.d.ts
/**
* The transport this module uses: the run path for a model id, and the
* provider-native path for a vendor's own model object, through the binding
* or over HTTP.
*/
type Transport = Transport$1;
//#endregion
//#region src/models/ai-sdk/models/language.d.ts
/**
* Typed observability attached to every result under
* `providerMetadata.cloudflare`, so consumers never have to walk response
* objects looking for a gateway log id.
*/
interface CloudflareMetadata {
/** The model that actually answered — the fallback leg, when one was used. */
model: string;
/** The gateway id the request went through. Absent when none handled it. */
gateway?: string;
/**
* The gateway provider slug, for a vendor's model object sent to the
* provider-native path. Absent for a model id.
*/
provider?: string;
/** `cf-aig-log-id`, or the binding's last log id. */
logId?: string;
/** `cf-aig-event-id` — correlates the requests of one gateway event. */
eventId?: string;
/** `cf-aig-request-id` — the gateway's id for this request. */
requestId?: string;
/** `cf-aig-trace-id` — the trace this request belongs to. */
traceId?: string;
/** `cf-aig-run-id`, when the gateway sends one. */
runId?: string;
/** `cf-aig-cache-status`, e.g. `HIT` or `MISS`. */
cacheStatus?: string;
/** `cf-aig-step` — which step of a gateway route answered. */
step?: number;
}
/** Everything a language model needs from the provider that created it. */
interface LanguageModelConfig {
transport: Transport;
/**
* The provider-wide gateway defaults, in any of the spellings `createAI`
* takes: a bare id, a `gateway` object, or the whole settings object with
* the flat gateway keys on it.
*/
providerGateway: string | ProviderGatewaySettings | undefined;
}
//#endregion
//#region src/models/ai-sdk/models/gateway.d.ts
/**
* A `LanguageModelV4` that is the user's own vendor model, routed through AI
* Gateway. Generation, parsing, tool handling and error shapes stay the
* vendor's; only the transport is Cloudflare's.
*/
declare class GatewayLanguageModel implements LanguageModelV4 {
#private;
readonly specificationVersion: "v4";
constructor(
model: LanguageModelV4,
options: GatewayModelOptions | undefined,
config: LanguageModelConfig
);
/** The vendor's own provider name — `anthropic.messages`, `openai.responses`. */
get provider(): string;
/** The vendor's own id spelling, which the gateway requires verbatim. */
get modelId(): string;
get supportedUrls(): LanguageModelV4["supportedUrls"];
/** The model this wraps, for callers that need the vendor object back. */
get model(): LanguageModelV4;
doGenerate(
options: LanguageModelV4CallOptions
): Promise<LanguageModelV4GenerateResult>;
doStream(
options: LanguageModelV4CallOptions
): Promise<LanguageModelV4StreamResult>;
}
//#endregion
//#region src/models/ai-sdk/index.d.ts
/**
* The provider `createAI` returns. Call it with a model id — a Workers AI id,
* an AI Gateway catalog id, or a dynamic route — or with a model another
* provider built.
*
* It satisfies `ProviderV4`, so it drops into
* `createProviderRegistry({ cloudflare: ai })` and into
* `globalThis.AI_SDK_DEFAULT_PROVIDER`, where the AI SDK resolves a bare
* `model: "@cf/…"` or `"openai/gpt-5.5"` string through it.
*/
interface AI {
/**
* A language model: `@cf/…` (Workers AI), `<author>/<model>` (the AI
* Gateway catalog, in its spelling), or `dynamic/<route>`.
*/
(modelId: CloudflareLanguageModelId, options?: ModelOptions): LanguageModelV4;
/**
* Any AI SDK v4 language model, sent to AI Gateway's provider-native path
* with its own request and parser.
*/
(model: LanguageModelV4, options?: GatewayModelOptions): LanguageModelV4;
/** The AI SDK provider specification this implements. */
readonly specificationVersion: "v4";
/** Same as calling the provider directly. */
languageModel(
modelId: CloudflareLanguageModelId,
options?: ModelOptions
): LanguageModelV4;
languageModel(
model: LanguageModelV4,
options?: GatewayModelOptions
): LanguageModelV4;
/** The `ProviderV4` shape: a registry resolves models by string alone. */
languageModel(modelId: string, options?: ModelOptions): LanguageModelV4;
/**
* Any other v4 model a vendor built — embedding, image, speech,
* transcription — sent to AI Gateway's provider-native path with its own
* code intact.
*/
routed<T extends object>(model: T, options?: RoutedModelOptions): T;
/** A text-embedding model from the Workers AI catalog. */
embedding(
modelId: CloudflareEmbeddingModelId,
options?: ModalityOptions
): EmbeddingModelV4;
/** Alias of {@link AI.embedding}, the `ProviderV4` method name. */
embeddingModel(
modelId: CloudflareEmbeddingModelId,
options?: ModalityOptions
): EmbeddingModelV4;
/**
* Alias of {@link AI.embedding}.
*
* @deprecated `embeddingModel` is the `ProviderV4` name; this is the older
* `ProviderV2`/`ProviderV3` spelling, kept for one release.
*/
textEmbeddingModel(
modelId: CloudflareEmbeddingModelId,
options?: ModalityOptions
): EmbeddingModelV4;
/** A text-to-image model from the Workers AI catalog. */
image(
modelId: CloudflareImageModelId,
options?: ModalityOptions
): ImageModelV4;
/** Alias of {@link AI.image}, the `ProviderV4` method name. */
imageModel(
modelId: CloudflareImageModelId,
options?: ModalityOptions
): ImageModelV4;
/** A speech-recognition model from the Workers AI catalog. */
transcription(
modelId: CloudflareTranscriptionModelId,
options?: ModalityOptions
): TranscriptionModelV4;
/** Alias of {@link AI.transcription}, the `ProviderV4` method name. */
transcriptionModel(
modelId: CloudflareTranscriptionModelId,
options?: ModalityOptions
): TranscriptionModelV4;
/** A text-to-speech model from the Workers AI catalog. */
speech(
modelId: CloudflareSpeechModelId,
options?: ModalityOptions
): SpeechModelV4;
/** Alias of {@link AI.speech}, the `ProviderV4` method name. */
speechModel(
modelId: CloudflareSpeechModelId,
options?: ModalityOptions
): SpeechModelV4;
/** A reranking model from the Workers AI catalog. */
reranking(
modelId: CloudflareRerankingModelId,
options?: ModalityOptions
): RerankingModelV4;
/** Alias of {@link AI.reranking}, the `ProviderV4` method name. */
rerankingModel(
modelId: CloudflareRerankingModelId,
options?: ModalityOptions
): RerankingModelV4;
}
/**
* Creates a provider over Workers AI and the AI Gateway catalog.
*
* A Workers AI id goes to Workers AI and a catalog id or dynamic route to AI
* Gateway's `/run`, both on `env.AI.run`; a vendor's model object goes to AI
* Gateway's provider-native path. With `{ accountId, apiToken }` instead of
* `binding`, the same requests go to the REST API and to
* `gateway.ai.cloudflare.com`. Gateway
* options are flat (`id`, `cacheTtl`, …) or nested under `gateway`; per-model
* options and per-call `providerOptions.cloudflare` override them in that
* order.
*
* ```ts
* const ai = createAI({ binding: env.AI, id: "prod", cacheTtl: 60 });
*
* ai("@cf/zai-org/glm-4.7-flash");
* ai("anthropic/claude-opus-4.8", { fallback: ["openai/gpt-5.5"] });
* ai.embedding("@cf/baai/bge-base-en-v1.5");
* ```
*/
declare function createAI(settings: AISettings): AI;
//#endregion
export {
CloudflareAIError as a,
GatewayModelOptions as c,
RoutedModelOptions as d,
VendorKeyOptions as f,
CloudflareMetadata as i,
ModalityOptions as l,
createAI as n,
CloudflareLanguageModelId as o,
GatewayLanguageModel as r,
FallbackLeg as s,
AI as t,
ModelOptions as u
};
//# sourceMappingURL=index-8k4qsX1T.d.ts.map