bunker.ai
const reply = await bunker.ai.chat([ { role: 'system', content: 'Summarize in 3 bullet points.' }, { role: 'user', content: text },]);reply.text;
const short = await bunker.ai.chat('Suggest a name for a lunch club'); // a string is one user message
for await (const chunk of bunker.ai.stream(messages)) out.textContent += chunk;
const data = await bunker.ai.json(messages, { schema: { type: 'object', properties: { sentiment: { enum: ['positive', 'neutral', 'negative'] } } },});- Uses the provider and the page model your admin chose (for example Claude Haiku or GPT-5 mini). Keys stay on the server.
- Calls count against the viewer’s and the app’s daily allowance. Call AI on a button press, not on every keystroke.
- Handle
quota_exceededandunavailable(no provider set up) gracefully. jsonresolves to the parsed value. With aschema, the answer follows it. Bunker retries once if the model’s JSON doesn’t parse, then throwsinvalid.- Breaking out of a
for awaitloop overstreamcancels the request. - Every call is audited with metadata (who, app, model, tokens). Full prompts are logged only if the admin turned that on.
Request body
Section titled “Request body”| Field | |
|---|---|
messages |
[{ role: 'user' | 'assistant' | 'system', content }] |
prompt |
A single question, instead of messages |
system |
Instructions |
json |
true to answer as JSON |
schema |
A JSON Schema the answer must follow (implies json) |
maxTokens |
Longest answer (default 1024, capped by the admin’s per-request limit) |
stream |
true for server-sent events: text chunks, then done |
The reply is { text, stopReason, model, usage }. JSON answers add json (the parsed value), or json: null with jsonError if the model still did not return valid JSON.