Skip to main content

Module http

Module http 

Source
Expand description

The http transport (CONTEXT.md §8, §14).

One OpenAI-compatible chat client over reqwest, with a configurable base URL. It covers every OpenAI-format API worker and (in Phase 4) every conductor backend. Responses are streamed via SSE so deltas reach the event sink as they arrive, and final token usage is captured.

Structs§

ChatMessage
A chat message in the OpenAI format. The tool fields are only set on the host agentic loop: tool_calls on an assistant turn that calls tools, tool_call_id on a tool-role message carrying a call’s result.
ChatRequest
A chat completion request.
ChatResponse
The accumulated result of a (streamed) chat completion.
ChatTurn
One turn of the host agentic loop: either text (the model is done) or a set of tool calls to execute and feed back.
DiscoveredModel
A model offered by a platform’s /v1/models endpoint, with optional cost and context metadata where the platform provides it (e.g. OpenRouter).
FunctionCall
OpenAiClient
An OpenAI-compatible chat client over a configurable base URL.
ToolCall
One tool call in the OpenAI function-calling format — serialized when echoing an assistant turn, deserialized when reading the model’s response.

Traits§

ChatBackend
Marker trait tie-in kept minimal for Phase 2; the API worker adapter wraps this client and implements Worker.

Functions§

cloudflare_account_id_from_base_url
Extract Cloudflare account id from an OpenAI-compat base URL such as https://api.cloudflare.com/client/v4/accounts/{id}/ai/v1.
cloudflare_text_model_fallback
Curated Workers AI text model ids (OpenAI-compat chat). Used when the native models/search API is empty/unavailable so the Models tab still offers real @cf/… ids to add. Keep in sync with https://developers.cloudflare.com/workers-ai/models/
list_cloudflare_workers_ai_models
Cloudflare Workers AI rejects OpenAI-compatible GET …/ai/v1/models (HTTP 405). Use the native REST catalog instead: GET /accounts/{account_id}/ai/models/search.
normalize_base_url
Normalize an OpenAI-compatible base URL. We append our own path (/chat/completions, /models), so a user who pastes a full endpoint URL from a provider’s docs gets it stripped back to the base rather than a doubled path (…/chat/completions/chat/completions → 404).