Member Junction
    Preparing search index...

    A BaseDecision driver for Cloudflare's Clef decision models (@cf/cloudflare/clef and @cf/cloudflare/clef-flash) on Workers AI. They speak the System One decisions format (the one TypeSafe's Jev speaks), so the wire mapping comes from BaseSystemOneDecision; this class supplies the Workers AI endpoint, the account ID and the token, and unwraps Cloudflare's v4 response envelope.

    Credentials. Workers AI needs an account ID in the URL and an API token. The API key is either "<accountId>:<apiToken>", or the token alone with the account ID in the CLOUDFLARE_ACCOUNT_ID environment variable. A key that names an account wins over the variable. An AI Credential's values arrive as JSON: its apiKey is read the same way, and an accountId field wins over both.

    Endpoint. https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/run, followed by the model. To go through an AI Gateway instead, set CLOUDFLARE_WORKERS_AI_BASE_URL, give the credential an endpoint, or pass a base URL to the constructor (the constructor wins, then the credential, then the variable): the URL the model is appended to, such as https://gateway.ai.cloudflare.com/v1/{account_id}/my-gateway/workers-ai. {account_id} is replaced with the account ID; a base URL without it needs none.

    Model. DecisionParams.Model is the model-vendor row's APIName (@cf/cloudflare/clef), which is the URL's last part; the body's model field is its last segment (clef).

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    ACCOUNT_ID_ENV_VAR: "CLOUDFLARE_ACCOUNT_ID" = 'CLOUDFLARE_ACCOUNT_ID'

    The environment variable the account ID is read from when the API key does not carry one.

    BASE_URL_ENV_VAR: "CLOUDFLARE_WORKERS_AI_BASE_URL" = 'CLOUDFLARE_WORKERS_AI_BASE_URL'

    The environment variable that overrides the base URL, for an AI Gateway.

    DEFAULT_BASE_URL: "https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/run" = ...

    The Workers AI base URL the model is appended to, with the account ID placeholder.

    DEFAULT_MODEL: "@cf/cloudflare/clef" = '@cf/cloudflare/clef'

    The model used when DecisionParams.Model is empty.

    Accessors

    • get apiKey(): string

      Only sub-classes can access the API key

      Returns string

    Methods

    • Executes a typed decision request across one or more questions about a given state. This method wraps driver execution with input validation, output verification, execution timing, and structured error handling. It never throws.

      Parameters

      • params: DecisionParams

        Decision request parameters including state, questions, and model.

      Returns Promise<DecisionResult>

      A Promise resolving to a DecisionResult.

    • The error type for a non-2xx status whose ErrorAnalyzer reading is wrong for this API, or undefined (the default) to leave it to ErrorAnalyzer, which reads the message and the status. A type returned here is attached to the thrown error as a Retriable error that allows failover: another candidate may answer what this API refused.

      Parameters

      • _status: number

        The HTTP status.

      Returns AIErrorType

    • Sends the request. This is the driver's only network call; a test double overrides it to stand in for the network.

      Parameters

      • url: string

        The endpoint from GetEndpointURL.

      • init: RequestInit

        The POST: method, headers, JSON body and the caller's cancellation signal.

      Returns Promise<Response>

    • Validates input DecisionParams against the decision contract.

      Parameters

      Returns string[]

      An array of error descriptions, empty if valid.