prepare
Validates a browser request and builds the call to forward. router() uses it; call it
yourself to serve the gateway from a framework other than Hono. It checks the key, then the origin.
Everything the browser could use to widen its own access is refused or replaced here: method must
be one of start, chat, log, or end (default chat), the conversation comes from a signed
handle, and config_url is the gateway’s. The one field forwarded is user_meta_data, after the
checks described under ChatGateway. A chat message over 8 KiB of UTF-8 is refused
with 413 before a conversation is created or a turn is counted.
prepare() doesn’t check the size of the request body. router() limits it to 64 KiB
before parsing; apply a limit of your own when you call prepare() directly.
A log or end request needs a handle. A start or chat without one creates a conversation,
which counts against maxNewConversations; a chat counts against maxTurns.
Parameters
body
The browser’s request body: { method?, handle?, message?, user_meta_data? }.
opts
The request’s credentials.
opts.origin
The Origin header.
opts.key
The presented publishable key.
Returns
[string, Record<string, unknown>, string | null] — a tuple of [method, params, mintedHandle].
method is the service method to call: create_conversation for start, chat for chat,
chat_log for log, and end_conversation for end. params are its parameters. mintedHandle
is the new handle when this call created the conversation, and null otherwise; return it to the
browser.
Send each method the way router() does:
Never relay a chat_log response as it comes back. The full log carries the system prompt and
tool traffic, which the browser must not see.
Throws
GatewayRejection when the request is refused: 401 for a bad key, 403 for a disallowed origin
or a bad handle, 400 for a malformed request, 413 for an oversized message or metadata, and
429 when a cap was reached.