Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

chatParamsFromRequest.md

76 lines2.1 KB

1---
2id: chatParamsFromRequest
3title: chatParamsFromRequest
4---
5​
6# Function: chatParamsFromRequest()
7​
8```ts
9function chatParamsFromRequest(req): Promise<{
10 context: object[];
11 forwardedProps: Record<string, unknown>;
12 messages: (
13 | ModelMessage<
14 | string
15 | ContentPart<unknown, unknown, unknown, unknown, unknown>[]
16 | null>
17 | UIMessage<unknown>)[];
18 parentRunId?: string;
19 runId: string;
20 state: unknown;
21 threadId: string;
22 tools: object[];
23}>;
24```
25​
26Defined in: [packages/typescript/ai/src/utilities/chat-params.ts:121](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/utilities/chat-params.ts#L121)
27​
28Read an HTTP `Request`, parse its JSON body, and validate it as an
29AG-UI `RunAgentInput` — collapsing the standard `req.json()` +
30`chatParamsFromRequestBody(...)` pair into a single call.
31​
32On a malformed body or invalid AG-UI shape, this **throws a
33`Response`** with status 400 and a migration-pointing message in the
34body. Frameworks that natively handle thrown `Response` objects
35(TanStack Start, SolidStart, Remix, React Router 7) will return the
36400 to the client automatically, so the handler reduces to:
37​
38```ts
39export async function POST(req: Request) {
40 const params = await chatParamsFromRequest(req)
41 // ...use params
43```
44​
45In frameworks that do not auto-handle thrown `Response` objects
46(Next.js Route Handlers, SvelteKit, Hono, raw Node), wrap the call
47with try/catch and return the caught Response yourself, or use
48`chatParamsFromRequestBody` directly with your own JSON-parsing.
49​
50## Parameters
51​
52### req
53​
54`Request`
55​
56## Returns
57​
58`Promise`\<\{
59 `context`: `object`[];
60 `forwardedProps`: `Record`\<`string`, `unknown`\>;
61 `messages`: (
62 \| [`ModelMessage`](../interfaces/ModelMessage.md)\<
63 \| `string`
64 \| [`ContentPart`](../type-aliases/ContentPart.md)\<`unknown`, `unknown`, `unknown`, `unknown`, `unknown`\>[]
65 \| `null`\>
66 \| [`UIMessage`](../interfaces/UIMessage.md)\<`unknown`\>)[];
67 `parentRunId?`: `string`;
68 `runId`: `string`;
69 `state`: `unknown`;
70 `threadId`: `string`;
71 `tools`: `object`[];
72\}\>
73​
74## Throws
75​
76400 on malformed JSON or invalid AG-UI shape.