function toJsonResponse(result, init?): Promise<Response>;Defined in: packages/ai/src/stream-to-response.ts:1620
Send a chat run as one JSON body, for hosts that cannot stream a response. Pair it with the fetchJson connection adapter.
The body is { chunks, offset?, done }:
Pass a ChatResult from chat({ stream: false }) to send it as is. Pass a stream to read it here. Without durability the stream is read to its end. With durability, a fresh run fills the log, and the reply goes out when the run ends or when maxWaitMs passes, whichever is first. The run keeps going after an early reply. A request with a resume offset reads the log instead.
ResponseInit
Response init. For a stream, also abortController, signal, maxWaitMs, durability, and debug.
Promise<Response>
A promise of a JSON Response
// A finished result
export async function POST(request: Request) {
const result = await chat({
adapter: openaiText('gpt-5.5'),
messages: [...],
stream: false,
});
return toJsonResponse(result);
}
// A durable stream that replies at least once a second
export async function POST(request: Request) {
const stream = chat({ adapter: openaiText('gpt-5.5'), messages: [...] });
return toJsonResponse(stream, {
durability: { adapter: memoryStream(request) },
signal: request.signal,
maxWaitMs: 1000,
});
}function toJsonResponse<TOffset>(stream, init?): Promise<Response>;Defined in: packages/ai/src/stream-to-response.ts:1624
Send a chat run as one JSON body, for hosts that cannot stream a response. Pair it with the fetchJson connection adapter.
The body is { chunks, offset?, done }:
Pass a ChatResult from chat({ stream: false }) to send it as is. Pass a stream to read it here. Without durability the stream is read to its end. With durability, a fresh run fills the log, and the reply goes out when the run ends or when maxWaitMs passes, whichever is first. The run keeps going after an early reply. A request with a resume offset reads the log instead.
TOffset extends string = string
AsyncIterable<AGUIEvent>
JsonResponseInit<TOffset>
Response init. For a stream, also abortController, signal, maxWaitMs, durability, and debug.
Promise<Response>
A promise of a JSON Response
// A finished result
export async function POST(request: Request) {
const result = await chat({
adapter: openaiText('gpt-5.5'),
messages: [...],
stream: false,
});
return toJsonResponse(result);
}
// A durable stream that replies at least once a second
export async function POST(request: Request) {
const stream = chat({ adapter: openaiText('gpt-5.5'), messages: [...] });
return toJsonResponse(stream, {
durability: { adapter: memoryStream(request) },
signal: request.signal,
maxWaitMs: 1000,
});
}