@tanstack/ai-react 0.27.3 → 0.28.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.
package/src/types.ts CHANGED
@@ -27,6 +27,8 @@ import type {
27
27
  SendMessageOptions,
28
28
  UIMessage,
29
29
  WhenBusy,
30
+ SubagentClientAgent,
31
+ SubagentHandles,
30
32
  } from '@tanstack/ai-client'
31
33
 
32
34
  // Re-export types from ai-client
@@ -86,6 +88,7 @@ export type UseChatOptions<
86
88
  TContext = InferredClientContext<TTools>,
87
89
  TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>> =
88
90
  readonly [],
91
+ TSubagents extends ReadonlyArray<SubagentClientAgent> | undefined = undefined,
89
92
  > = DistributedOmit<
90
93
  ChatClientOptions<TTools, TContext, TInterrupts>,
91
94
  | 'onMessagesChange'
@@ -100,6 +103,7 @@ export type UseChatOptions<
100
103
  | 'onRunIdChange'
101
104
  | 'context'
102
105
  | 'devtools'
106
+ | 'subagents'
103
107
  > & {
104
108
  /** Display options for TanStack AI Devtools. */
105
109
  devtools?: AIDevtoolsDisplayOptions
@@ -115,6 +119,12 @@ export type UseChatOptions<
115
119
  * against the schema passed to `chat({ outputSchema })` on the server route.
116
120
  */
117
121
  outputSchema?: TSchema
122
+ /**
123
+ * Agents from `defineAgent`. This types `messages` parts and `subagents`.
124
+ * The hook does not call `run` and does not send the agents to the server.
125
+ * Pass the same array you give to `chat({ subagents: { agents } })`.
126
+ */
127
+ subagents?: TSubagents
118
128
  } & ClientContextOptionFromTools<TTools, TContext>
119
129
 
120
130
  /**
@@ -127,10 +137,12 @@ export type UseChatReturn<
127
137
  TSchema extends SchemaInput | undefined = undefined,
128
138
  TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>> =
129
139
  readonly [],
140
+ TSubagents extends ReadonlyArray<SubagentClientAgent> | undefined = undefined,
130
141
  > = BaseUseChatReturn<
131
142
  TTools,
132
143
  TSchema extends SchemaInput ? InferSchemaType<TSchema> : unknown,
133
- TInterrupts
144
+ TInterrupts,
145
+ TSubagents
134
146
  > &
135
147
  (TSchema extends SchemaInput
136
148
  ? {
@@ -155,13 +167,23 @@ interface BaseUseChatReturn<
155
167
  TData = unknown,
156
168
  TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>> =
157
169
  readonly [],
170
+ TSubagents extends ReadonlyArray<SubagentClientAgent> | undefined = undefined,
158
171
  > {
159
172
  /**
160
173
  * Current messages in the conversation. When `outputSchema` is supplied,
161
174
  * `messages[i].parts.find(p => p.type === 'structured-output')` is typed
162
175
  * with the schema's inferred shape — `data: T`, `partial: DeepPartial<T>`.
176
+ * When `subagents` is supplied, a `type: 'subagent'` part narrows on
177
+ * `subagent.name`, and that child's `messages` use the agent's tools.
178
+ */
179
+ messages: Array<UIMessage<TTools, TData, TSubagents>>
180
+
181
+ /**
182
+ * Live child-agent invocations, nested cards included. Each entry is the
183
+ * same object as the matching `part.subagent`, including `stop()`.
184
+ * When `subagents` is supplied, `name` is one of those agent names.
163
185
  */
164
- messages: Array<UIMessage<TTools, TData>>
186
+ subagents: Array<SubagentHandles<TSubagents>>
165
187
 
166
188
  /**
167
189
  * Send a message and get a response.
package/src/ui.ts CHANGED
@@ -15,6 +15,8 @@ export {
15
15
  type MessageProps,
16
16
  type PartProps,
17
17
  type QueueProps,
18
+ type SubagentPartsProps,
19
+ type SubagentProps,
18
20
  type ToolProps,
19
21
  } from './chat-ui/create-ui'
20
22
  export { createChatHook } from './chat-ui/create-chat-hook'
package/src/use-chat.ts CHANGED
@@ -40,9 +40,11 @@ export function useChat<
40
40
  const TInterrupts extends ReadonlyArray<
41
41
  InterruptDefinition<any, any, any, any>
42
42
  > = readonly [],
43
+ const TSubagents extends ReadonlyArray<{ name: string }> | undefined =
44
+ undefined,
43
45
  >(
44
- options: UseChatOptions<TTools, TSchema, TContext, TInterrupts>,
45
- ): UseChatReturn<TTools, TSchema, TInterrupts> {
46
+ options: UseChatOptions<TTools, TSchema, TContext, TInterrupts, TSubagents>,
47
+ ): UseChatReturn<TTools, TSchema, TInterrupts, TSubagents> {
46
48
  // The hook's identity is its `threadId`. Reload with the same `threadId`
47
49
  // restores the same conversation. `hookId` is only a React recreation key
48
50
  // when no `threadId` is given. It is never sent on the wire.
@@ -94,7 +96,9 @@ export function useChat<
94
96
 
95
97
  // Track current options in a ref to avoid recreating client when options change
96
98
  const optionsRef =
97
- useRef<UseChatOptions<TTools, TSchema, TContext, TInterrupts>>(options)
99
+ useRef<UseChatOptions<TTools, TSchema, TContext, TInterrupts, TSubagents>>(
100
+ options,
101
+ )
98
102
  optionsRef.current = options
99
103
 
100
104
  const syncResumeState = useCallback((target: ChatClient | null) => {
@@ -609,12 +613,15 @@ export function useChat<
609
613
  return activeStructuredPart.data as Final
610
614
  }, [activeStructuredPart])
611
615
 
616
+ const subagents = useMemo(() => client.getSubagents(), [renderedMessages])
617
+
612
618
  // The runtime shape unconditionally exposes partial/final; the public
613
619
  // return type hides them when no outputSchema was supplied. TS can't
614
620
  // structurally narrow across that conditional, so the `as` is the seam.
615
621
  // oxlint-disable-next-line eslint-js/no-restricted-syntax -- hook return shape diverges from generic UseChatReturn<TTools, TSchema> due to conditional type on TSchema; TS can't structurally narrow
616
622
  return {
617
623
  messages: renderedMessages,
624
+ subagents,
618
625
  sendMessage,
619
626
  append,
620
627
  reload,
@@ -645,5 +652,5 @@ export function useChat<
645
652
  resumeInterrupts,
646
653
  partial,
647
654
  final,
648
- } as unknown as UseChatReturn<TTools, TSchema, TInterrupts>
655
+ } as unknown as UseChatReturn<TTools, TSchema, TInterrupts, TSubagents>
649
656
  }