@tanstack/ai 0.17.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/dist/esm/activities/chat/index.d.ts +12 -3
  2. package/dist/esm/activities/chat/index.js +10 -5
  3. package/dist/esm/activities/chat/index.js.map +1 -1
  4. package/dist/esm/activities/chat/messages.js +26 -2
  5. package/dist/esm/activities/chat/messages.js.map +1 -1
  6. package/dist/esm/activities/chat/middleware/compose.js +1 -1
  7. package/dist/esm/activities/chat/middleware/compose.js.map +1 -1
  8. package/dist/esm/activities/chat/middleware/types.d.ts +12 -1
  9. package/dist/esm/activities/chat/tools/schema-converter.js +5 -0
  10. package/dist/esm/activities/chat/tools/schema-converter.js.map +1 -1
  11. package/dist/esm/index.d.ts +3 -0
  12. package/dist/esm/index.js +6 -0
  13. package/dist/esm/index.js.map +1 -1
  14. package/dist/esm/types.d.ts +29 -8
  15. package/dist/esm/utilities/ag-ui-wire.d.ts +44 -0
  16. package/dist/esm/utilities/ag-ui-wire.js +96 -0
  17. package/dist/esm/utilities/ag-ui-wire.js.map +1 -0
  18. package/dist/esm/utilities/chat-params.d.ts +80 -0
  19. package/dist/esm/utilities/chat-params.js +96 -0
  20. package/dist/esm/utilities/chat-params.js.map +1 -0
  21. package/package.json +3 -3
  22. package/skills/ai-core/ag-ui-protocol/SKILL.md +46 -3
  23. package/src/activities/chat/index.ts +33 -9
  24. package/src/activities/chat/messages.ts +44 -4
  25. package/src/activities/chat/middleware/compose.ts +1 -1
  26. package/src/activities/chat/middleware/types.ts +12 -1
  27. package/src/activities/chat/tools/schema-converter.ts +14 -0
  28. package/src/index.ts +11 -0
  29. package/src/types.ts +39 -8
  30. package/src/utilities/ag-ui-wire.ts +182 -0
  31. package/src/utilities/chat-params.ts +199 -0
@@ -0,0 +1,182 @@
1
+ import type { ContentPart, MessagePart, TextPart, UIMessage } from '../types'
2
+
3
+ type AGUITextInputContent = { type: 'text'; text: string }
4
+ type AGUIInputContent =
5
+ | AGUITextInputContent
6
+ | (ContentPart & { type: 'image' | 'audio' | 'video' | 'document' })
7
+
8
+ type AGUIToolCallMirror = {
9
+ id: string
10
+ type: 'function'
11
+ function: { name: string; arguments: string }
12
+ }
13
+
14
+ type AGUIToolMessage = {
15
+ role: 'tool'
16
+ id: string
17
+ toolCallId: string
18
+ content: string
19
+ error?: string
20
+ }
21
+
22
+ type AGUIReasoningMessage = {
23
+ role: 'reasoning'
24
+ id: string
25
+ content: string
26
+ }
27
+
28
+ type WireAnchorMessage = UIMessage & {
29
+ content?: string | Array<AGUIInputContent>
30
+ toolCalls?: Array<AGUIToolCallMirror>
31
+ }
32
+
33
+ export type WireMessage =
34
+ | WireAnchorMessage
35
+ | AGUIToolMessage
36
+ | AGUIReasoningMessage
37
+
38
+ /**
39
+ * Serialize TanStack `UIMessage`s into the AG-UI `RunAgentInput.messages`
40
+ * wire shape. Each anchor (system/user/assistant) carries the canonical
41
+ * `parts` array verbatim plus AG-UI mirror fields (`content`, `toolCalls`)
42
+ * so AG-UI Zod parsing succeeds. Tool results and thinking parts on
43
+ * assistant messages are additionally emitted as fan-out
44
+ * `{role:'tool',...}` and `{role:'reasoning',...}` entries for strict
45
+ * AG-UI server consumers.
46
+ */
47
+ export function uiMessagesToWire(
48
+ messages: Array<UIMessage>,
49
+ ): Array<WireMessage> {
50
+ const wire: Array<WireMessage> = []
51
+
52
+ for (const msg of messages) {
53
+ // Defensive: if parts is missing (ModelMessage-shaped input), pass through as-is.
54
+ // UIMessage always has parts; ModelMessage uses content directly.
55
+ const parts: ReadonlyArray<MessagePart> =
56
+ (msg.parts as ReadonlyArray<MessagePart> | undefined) ?? []
57
+
58
+ if (msg.role === 'system') {
59
+ wire.push({
60
+ ...msg,
61
+ content:
62
+ parts.length > 0
63
+ ? collectText(parts)
64
+ : ((msg as unknown as { content?: string }).content ?? ''),
65
+ })
66
+ continue
67
+ }
68
+
69
+ if (msg.role === 'user') {
70
+ wire.push({
71
+ ...msg,
72
+ content:
73
+ parts.length > 0
74
+ ? collectUserContent(parts)
75
+ : ((msg as unknown as { content?: string }).content ?? ''),
76
+ })
77
+ continue
78
+ }
79
+
80
+ // assistant: emit reasoning fan-outs first, then anchor, then tool fan-outs
81
+ for (const part of parts) {
82
+ if (part.type === 'thinking') {
83
+ wire.push({
84
+ role: 'reasoning',
85
+ id: deriveReasoningId(msg.id, part),
86
+ content: part.content,
87
+ })
88
+ }
89
+ }
90
+
91
+ const text = collectText(parts)
92
+ const toolCalls = collectToolCalls(parts)
93
+ wire.push({
94
+ ...msg,
95
+ ...(text !== '' && { content: text }),
96
+ ...(toolCalls && { toolCalls }),
97
+ })
98
+
99
+ for (const part of parts) {
100
+ if (part.type === 'tool-result') {
101
+ wire.push({
102
+ role: 'tool',
103
+ id: deriveToolMessageId(part.toolCallId),
104
+ toolCallId: part.toolCallId,
105
+ content: part.content,
106
+ ...(part.error !== undefined && { error: part.error }),
107
+ })
108
+ }
109
+ }
110
+ }
111
+
112
+ return wire
113
+ }
114
+
115
+ function collectText(parts: ReadonlyArray<MessagePart>): string {
116
+ return parts
117
+ .filter((p): p is TextPart => p.type === 'text')
118
+ .map((p) => p.content)
119
+ .join('')
120
+ }
121
+
122
+ function collectUserContent(
123
+ parts: ReadonlyArray<MessagePart>,
124
+ ): string | Array<AGUIInputContent> {
125
+ const hasMultimodal = parts.some(
126
+ (p) =>
127
+ p.type === 'image' ||
128
+ p.type === 'audio' ||
129
+ p.type === 'video' ||
130
+ p.type === 'document',
131
+ )
132
+ if (!hasMultimodal) {
133
+ return collectText(parts)
134
+ }
135
+ const out: Array<AGUIInputContent> = []
136
+ for (const p of parts) {
137
+ if (p.type === 'text') {
138
+ out.push({ type: 'text', text: p.content })
139
+ } else if (
140
+ p.type === 'image' ||
141
+ p.type === 'audio' ||
142
+ p.type === 'video' ||
143
+ p.type === 'document'
144
+ ) {
145
+ out.push(p as AGUIInputContent)
146
+ }
147
+ }
148
+ return out
149
+ }
150
+
151
+ function collectToolCalls(
152
+ parts: ReadonlyArray<MessagePart>,
153
+ ): Array<AGUIToolCallMirror> | undefined {
154
+ const calls: Array<AGUIToolCallMirror> = []
155
+ for (const p of parts) {
156
+ if (p.type === 'tool-call') {
157
+ calls.push({
158
+ id: p.id,
159
+ type: 'function',
160
+ function: { name: p.name, arguments: p.arguments },
161
+ })
162
+ }
163
+ }
164
+ return calls.length > 0 ? calls : undefined
165
+ }
166
+
167
+ function deriveReasoningId(messageId: string, part: MessagePart): string {
168
+ return `${messageId}-reasoning-${(part as { id?: string }).id ?? hashContent((part as { content: string }).content)}`
169
+ }
170
+
171
+ function deriveToolMessageId(toolCallId: string): string {
172
+ return `tool-${toolCallId}`
173
+ }
174
+
175
+ function hashContent(s: string): string {
176
+ // Cheap deterministic id suffix; collisions are tolerable since
177
+ // reasoning ids only matter for AG-UI server consumers, not for our
178
+ // own server's dedup logic (which keys on toolCallId, not reasoning id).
179
+ let h = 0
180
+ for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) | 0
181
+ return Math.abs(h).toString(36)
182
+ }
@@ -0,0 +1,199 @@
1
+ import { AGUIError, RunAgentInputSchema } from '@ag-ui/core'
2
+ import type { Context as AGUIContext } from '@ag-ui/core'
3
+ import type { JSONSchema, ModelMessage, Tool, UIMessage } from '../types'
4
+
5
+ const KNOWN_PART_TYPES = new Set([
6
+ 'text',
7
+ 'image',
8
+ 'audio',
9
+ 'video',
10
+ 'document',
11
+ 'tool-call',
12
+ 'tool-result',
13
+ 'thinking',
14
+ ])
15
+
16
+ function isValidParts(value: unknown): value is Array<{ type: string }> {
17
+ if (!Array.isArray(value)) return false
18
+ for (const p of value) {
19
+ if (!p || typeof p !== 'object') return false
20
+ const type = (p as { type?: unknown }).type
21
+ if (typeof type !== 'string' || !KNOWN_PART_TYPES.has(type)) return false
22
+ }
23
+ return true
24
+ }
25
+
26
+ /**
27
+ * Parse and validate an HTTP request body as an AG-UI `RunAgentInput`.
28
+ *
29
+ * Returns a spread-friendly object whose `messages` field is suitable for
30
+ * passing directly to `chat({ messages })`. The existing
31
+ * `convertMessagesToModelMessages` handles AG-UI fan-out dedup and
32
+ * reasoning/activity/developer-role normalization internally.
33
+ *
34
+ * @throws An error with a migration-pointing message when the body does
35
+ * not conform to AG-UI 0.0.52 `RunAgentInputSchema`. Surface this as a
36
+ * 400 Bad Request to the client.
37
+ */
38
+ export function chatParamsFromRequestBody(body: unknown): Promise<{
39
+ messages: Array<UIMessage | ModelMessage>
40
+ threadId: string
41
+ runId: string
42
+ parentRunId?: string
43
+ tools: Array<{ name: string; description: string; parameters: JSONSchema }>
44
+ forwardedProps: Record<string, unknown>
45
+ state: unknown
46
+ context: Array<AGUIContext>
47
+ }> {
48
+ const parseResult = RunAgentInputSchema.safeParse(body)
49
+ if (!parseResult.success) {
50
+ return Promise.reject(
51
+ new AGUIError(
52
+ `Request body is not a valid AG-UI RunAgentInput. ` +
53
+ `If you're upgrading from a previous @tanstack/ai-client release, ` +
54
+ `see docs/migration/ag-ui-compliance.md. ` +
55
+ `Validation errors: ${parseResult.error.message}`,
56
+ ),
57
+ )
58
+ }
59
+
60
+ const parsed = parseResult.data
61
+
62
+ // AG-UI Zod uses `.strip()` so extra fields like `parts` on messages are
63
+ // dropped during parse. We re-attach them from the original body so the
64
+ // existing UIMessage path inside `chat()` can use them directly.
65
+ const rawMessages =
66
+ (body as { messages?: Array<Record<string, unknown>> }).messages ?? []
67
+ const messages = parsed.messages.map((m, i) => {
68
+ const raw = rawMessages[i]
69
+ if (
70
+ raw &&
71
+ typeof raw === 'object' &&
72
+ 'parts' in raw &&
73
+ isValidParts(raw.parts)
74
+ ) {
75
+ return { ...m, parts: raw.parts } as UIMessage | ModelMessage
76
+ }
77
+ return m as ModelMessage
78
+ })
79
+
80
+ return Promise.resolve({
81
+ messages,
82
+ threadId: parsed.threadId,
83
+ runId: parsed.runId,
84
+ parentRunId: parsed.parentRunId,
85
+ tools: parsed.tools as Array<{
86
+ name: string
87
+ description: string
88
+ parameters: JSONSchema
89
+ }>,
90
+ forwardedProps: (parsed.forwardedProps ?? {}) as Record<string, unknown>,
91
+ state: parsed.state,
92
+ context: parsed.context,
93
+ })
94
+ }
95
+
96
+ /**
97
+ * Read an HTTP `Request`, parse its JSON body, and validate it as an
98
+ * AG-UI `RunAgentInput` — collapsing the standard `req.json()` +
99
+ * `chatParamsFromRequestBody(...)` pair into a single call.
100
+ *
101
+ * On a malformed body or invalid AG-UI shape, this **throws a
102
+ * `Response`** with status 400 and a migration-pointing message in the
103
+ * body. Frameworks that natively handle thrown `Response` objects
104
+ * (TanStack Start, SolidStart, Remix, React Router 7) will return the
105
+ * 400 to the client automatically, so the handler reduces to:
106
+ *
107
+ * ```ts
108
+ * export async function POST(req: Request) {
109
+ * const params = await chatParamsFromRequest(req)
110
+ * // ...use params
111
+ * }
112
+ * ```
113
+ *
114
+ * In frameworks that do not auto-handle thrown `Response` objects
115
+ * (Next.js Route Handlers, SvelteKit, Hono, raw Node), wrap the call
116
+ * with try/catch and return the caught Response yourself, or use
117
+ * `chatParamsFromRequestBody` directly with your own JSON-parsing.
118
+ *
119
+ * @throws {Response} 400 on malformed JSON or invalid AG-UI shape.
120
+ */
121
+ export async function chatParamsFromRequest(
122
+ req: Request,
123
+ ): Promise<Awaited<ReturnType<typeof chatParamsFromRequestBody>>> {
124
+ let body: unknown
125
+ try {
126
+ body = await req.json()
127
+ } catch (cause) {
128
+ // Preserve the underlying error on the thrown Response for
129
+ // server-side observability without leaking it to the client.
130
+ const res = new Response(
131
+ 'Invalid AG-UI request body. See docs/migration/ag-ui-compliance.md.',
132
+ { status: 400 },
133
+ )
134
+ ;(res as unknown as { cause?: unknown }).cause = cause
135
+ throw res
136
+ }
137
+ try {
138
+ return await chatParamsFromRequestBody(body)
139
+ } catch (cause) {
140
+ // Generic public message — avoid echoing Zod paths (which can contain
141
+ // user payload fragments) or internal validator strings to the client.
142
+ // The original AGUIError is attached as `cause` so server logs can
143
+ // surface it without exposing it to remote callers.
144
+ const res = new Response(
145
+ 'Invalid AG-UI request body. See docs/migration/ag-ui-compliance.md.',
146
+ { status: 400 },
147
+ )
148
+ ;(res as unknown as { cause?: unknown }).cause = cause
149
+ throw res
150
+ }
151
+ }
152
+
153
+ /**
154
+ * Merge a server-side tool array with the AG-UI client-declared tools
155
+ * received in the request body.
156
+ *
157
+ * Rules:
158
+ * - Server tools win on name collision. The client's declaration is
159
+ * ignored if the server already has a tool with that name. The client's
160
+ * UI-side handler still fires when the streamed tool-result event comes
161
+ * through (see `chat-client.ts` `onToolCall`), giving the
162
+ * "after server execution the client also handles" semantic for free.
163
+ * - Client-only tools (name not in `serverTools`) become no-execute
164
+ * entries: the runtime's existing `ClientToolRequest` path handles
165
+ * them — server emits a tool-call request, client executes via its
166
+ * registered handler, client posts back the result.
167
+ *
168
+ * @param serverTools - The server's tool array (e.g. from
169
+ * `[myToolDef.server(...)]`). Pass directly to `chat({ tools })`.
170
+ * @param clientTools - The `tools` array received from
171
+ * `chatParamsFromRequest(...)` / `chatParamsFromRequestBody(...)`.
172
+ * @returns A merged array suitable for `chat({ tools })`.
173
+ */
174
+ export function mergeAgentTools(
175
+ serverTools: ReadonlyArray<Tool>,
176
+ clientTools: ReadonlyArray<{
177
+ name: string
178
+ description: string
179
+ parameters: JSONSchema
180
+ }>,
181
+ ): Array<Tool> {
182
+ const seen = new Set(serverTools.map((t) => t.name))
183
+ const merged: Array<Tool> = [...serverTools]
184
+ for (const ct of clientTools) {
185
+ if (seen.has(ct.name)) {
186
+ // Server wins on name collision.
187
+ continue
188
+ }
189
+ seen.add(ct.name)
190
+ merged.push({
191
+ name: ct.name,
192
+ description: ct.description,
193
+ inputSchema: ct.parameters,
194
+ // No `execute` — runtime treats this as a client-side tool and
195
+ // emits ClientToolRequest events.
196
+ } as Tool)
197
+ }
198
+ return merged
199
+ }