@tanstack/ai-client 0.29.1 → 0.30.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/README.md +11 -15
- package/dist/esm/chat-client.js +1 -0
- package/dist/esm/chat-client.js.map +1 -1
- package/dist/esm/devtools-noop.d.ts +2 -0
- package/dist/esm/devtools-noop.js +2 -0
- package/dist/esm/devtools-noop.js.map +1 -1
- package/dist/esm/devtools.d.ts +11 -1
- package/dist/esm/devtools.js +95 -0
- package/dist/esm/devtools.js.map +1 -1
- package/dist/esm/ui/selectors.d.ts +11 -0
- package/dist/esm/ui/selectors.js +106 -0
- package/dist/esm/ui/selectors.js.map +1 -0
- package/dist/esm/ui/types.d.ts +103 -0
- package/dist/esm/ui.d.ts +3 -0
- package/dist/esm/ui.js +2 -0
- package/package.json +7 -3
- package/src/chat-client.ts +7 -0
- package/src/devtools-noop.ts +2 -0
- package/src/devtools.ts +182 -0
- package/src/ui/selectors.ts +178 -0
- package/src/ui/types.ts +243 -0
- package/src/ui.ts +37 -0
package/src/ui/types.ts
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AnyClientTool,
|
|
3
|
+
InferSchemaType,
|
|
4
|
+
InferToolInput,
|
|
5
|
+
InferToolOutput,
|
|
6
|
+
InterruptDefinition,
|
|
7
|
+
SchemaInput,
|
|
8
|
+
} from '@tanstack/ai/client'
|
|
9
|
+
import type {
|
|
10
|
+
ChatInterrupt,
|
|
11
|
+
MessagePart,
|
|
12
|
+
RegisteredGenericInterrupt,
|
|
13
|
+
StructuredOutputPart,
|
|
14
|
+
ToolApprovalInterrupt,
|
|
15
|
+
ToolCallPart,
|
|
16
|
+
ToolResultPart,
|
|
17
|
+
UIMessage,
|
|
18
|
+
} from '../types'
|
|
19
|
+
|
|
20
|
+
export type ChatUIPartKey =
|
|
21
|
+
| 'text'
|
|
22
|
+
| 'image'
|
|
23
|
+
| 'audio'
|
|
24
|
+
| 'video'
|
|
25
|
+
| 'document'
|
|
26
|
+
| 'thinking'
|
|
27
|
+
| 'toolCall'
|
|
28
|
+
| 'toolResult'
|
|
29
|
+
| 'structuredOutput'
|
|
30
|
+
| 'uiResource'
|
|
31
|
+
|
|
32
|
+
export type ChatUIPartTypeByKey = {
|
|
33
|
+
text: 'text'
|
|
34
|
+
image: 'image'
|
|
35
|
+
audio: 'audio'
|
|
36
|
+
video: 'video'
|
|
37
|
+
document: 'document'
|
|
38
|
+
thinking: 'thinking'
|
|
39
|
+
toolCall: 'tool-call'
|
|
40
|
+
toolResult: 'tool-result'
|
|
41
|
+
structuredOutput: 'structured-output'
|
|
42
|
+
uiResource: 'ui-resource'
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type ChatUIPartOf<
|
|
46
|
+
TOptions,
|
|
47
|
+
TKey extends ChatUIPartKey = ChatUIPartKey,
|
|
48
|
+
> = TKey extends ChatUIPartKey
|
|
49
|
+
? Extract<
|
|
50
|
+
MessagePart<ChatUIToolsOf<TOptions>, ChatUIData<TOptions>>,
|
|
51
|
+
{ type: ChatUIPartTypeByKey[TKey] }
|
|
52
|
+
>
|
|
53
|
+
: never
|
|
54
|
+
|
|
55
|
+
export type ChatUIToolsOf<TOptions> = TOptions extends {
|
|
56
|
+
tools: infer TTools
|
|
57
|
+
}
|
|
58
|
+
? TTools extends ReadonlyArray<AnyClientTool>
|
|
59
|
+
? TTools
|
|
60
|
+
: any
|
|
61
|
+
: any
|
|
62
|
+
|
|
63
|
+
export type ChatUIInterruptsOf<TOptions> = TOptions extends {
|
|
64
|
+
interrupts: infer TInterrupts
|
|
65
|
+
}
|
|
66
|
+
? TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>>
|
|
67
|
+
? TInterrupts
|
|
68
|
+
: readonly []
|
|
69
|
+
: readonly []
|
|
70
|
+
|
|
71
|
+
export type ChatUISchemaOf<TOptions> = TOptions extends {
|
|
72
|
+
outputSchema: infer TSchema
|
|
73
|
+
}
|
|
74
|
+
? TSchema extends SchemaInput
|
|
75
|
+
? TSchema
|
|
76
|
+
: undefined
|
|
77
|
+
: undefined
|
|
78
|
+
|
|
79
|
+
export type ChatUIData<TOptions> = TOptions extends {
|
|
80
|
+
outputSchema: infer TSchema
|
|
81
|
+
}
|
|
82
|
+
? TSchema extends SchemaInput
|
|
83
|
+
? InferSchemaType<TSchema>
|
|
84
|
+
: unknown
|
|
85
|
+
: unknown
|
|
86
|
+
|
|
87
|
+
export type ChatUIToolName<TOptions> =
|
|
88
|
+
ChatUIToolsOf<TOptions>[number] extends infer TTool
|
|
89
|
+
? TTool extends AnyClientTool
|
|
90
|
+
? TTool['name']
|
|
91
|
+
: string
|
|
92
|
+
: string
|
|
93
|
+
|
|
94
|
+
export type ChatUIRegisteredInterruptId<TOptions> =
|
|
95
|
+
ChatUIInterruptsOf<TOptions>[number] extends infer TDefinition
|
|
96
|
+
? TDefinition extends InterruptDefinition<infer TId, any, any, any>
|
|
97
|
+
? TId
|
|
98
|
+
: string
|
|
99
|
+
: string
|
|
100
|
+
|
|
101
|
+
export type ChatUIHasNamedTools<TOptions> = [ChatUIToolName<TOptions>] extends [
|
|
102
|
+
never,
|
|
103
|
+
]
|
|
104
|
+
? false
|
|
105
|
+
: [string] extends [ChatUIToolName<TOptions>]
|
|
106
|
+
? false
|
|
107
|
+
: true
|
|
108
|
+
|
|
109
|
+
export type ChatUIHasNamedInterrupts<TOptions> = [
|
|
110
|
+
ChatUIRegisteredInterruptId<TOptions>,
|
|
111
|
+
] extends [never]
|
|
112
|
+
? false
|
|
113
|
+
: [string] extends [ChatUIRegisteredInterruptId<TOptions>]
|
|
114
|
+
? false
|
|
115
|
+
: true
|
|
116
|
+
|
|
117
|
+
export type ChatUINamedInterruptId<TOptions> = Exclude<
|
|
118
|
+
ChatUIRegisteredInterruptId<TOptions>,
|
|
119
|
+
'fallback'
|
|
120
|
+
>
|
|
121
|
+
|
|
122
|
+
export type ChatUIInterruptName<TOptions> =
|
|
123
|
+
| (ChatUIHasNamedTools<TOptions> extends true
|
|
124
|
+
? ChatUIToolName<TOptions>
|
|
125
|
+
: never)
|
|
126
|
+
| (ChatUIHasNamedInterrupts<TOptions> extends true
|
|
127
|
+
? ChatUINamedInterruptId<TOptions>
|
|
128
|
+
: never)
|
|
129
|
+
|
|
130
|
+
type ToolByName<TOptions, TName> = Extract<
|
|
131
|
+
ChatUIToolsOf<TOptions> extends ReadonlyArray<infer TTool> ? TTool : never,
|
|
132
|
+
{ name: TName }
|
|
133
|
+
>
|
|
134
|
+
|
|
135
|
+
export type ChatUIInterrupt = ChatInterrupt | ToolApprovalInterrupt
|
|
136
|
+
|
|
137
|
+
export type ChatUIToolApproval<
|
|
138
|
+
TOptions,
|
|
139
|
+
TName extends ChatUIToolName<TOptions> = ChatUIToolName<TOptions>,
|
|
140
|
+
> = Extract<
|
|
141
|
+
ChatInterrupt<ChatUIToolsOf<TOptions>, ChatUIInterruptsOf<TOptions>>,
|
|
142
|
+
{ kind: 'tool-approval'; toolName: TName }
|
|
143
|
+
>
|
|
144
|
+
|
|
145
|
+
export type ChatUIToolPart<
|
|
146
|
+
TOptions,
|
|
147
|
+
TName extends ChatUIToolName<TOptions> = ChatUIToolName<TOptions>,
|
|
148
|
+
> = {
|
|
149
|
+
key: 'toolCall'
|
|
150
|
+
part: Extract<ToolCallPart<ChatUIToolsOf<TOptions>>, { name: TName }>
|
|
151
|
+
result?: ToolResultPart
|
|
152
|
+
interrupt?: Extract<
|
|
153
|
+
ChatInterrupt<ChatUIToolsOf<TOptions>, ChatUIInterruptsOf<TOptions>>,
|
|
154
|
+
{ kind: 'tool-approval'; toolName: TName }
|
|
155
|
+
>
|
|
156
|
+
input?: InferToolInput<ToolByName<TOptions, TName>>
|
|
157
|
+
output?: InferToolOutput<ToolByName<TOptions, TName>>
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export type RegisteredUIInterrupt<
|
|
161
|
+
TOptions,
|
|
162
|
+
TId extends ChatUIRegisteredInterruptId<TOptions> =
|
|
163
|
+
ChatUIRegisteredInterruptId<TOptions>,
|
|
164
|
+
> = Extract<
|
|
165
|
+
RegisteredGenericInterrupt<ChatUIInterruptsOf<TOptions>>,
|
|
166
|
+
{ definitionId: TId }
|
|
167
|
+
>
|
|
168
|
+
|
|
169
|
+
export type ChatUIInterruptOf<
|
|
170
|
+
TOptions,
|
|
171
|
+
TName extends ChatUIInterruptName<TOptions> = never,
|
|
172
|
+
> = [TName] extends [never]
|
|
173
|
+
? ChatUIInterrupt
|
|
174
|
+
: TName extends (
|
|
175
|
+
ChatUIHasNamedTools<TOptions> extends true
|
|
176
|
+
? ChatUIToolName<TOptions>
|
|
177
|
+
: never
|
|
178
|
+
)
|
|
179
|
+
? ChatUIToolApproval<TOptions, TName>
|
|
180
|
+
: TName extends ChatUINamedInterruptId<TOptions>
|
|
181
|
+
? RegisteredUIInterrupt<TOptions, TName>
|
|
182
|
+
: ChatUIInterrupt
|
|
183
|
+
|
|
184
|
+
export type ChatUISelectedToolPart = {
|
|
185
|
+
key: 'toolCall'
|
|
186
|
+
part: ToolCallPart
|
|
187
|
+
result?: ToolResultPart
|
|
188
|
+
interrupt?: ChatUIInterrupt
|
|
189
|
+
input?: unknown
|
|
190
|
+
output?: unknown
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export type ChatUISelectedResultPart = {
|
|
194
|
+
key: 'toolResult'
|
|
195
|
+
part: ToolResultPart
|
|
196
|
+
matched: boolean
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export type ChatUISelectedGenericPart = {
|
|
200
|
+
key: Exclude<ChatUIPartKey, 'toolCall' | 'toolResult'>
|
|
201
|
+
part: MessagePart
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export type ChatUISelectedPart =
|
|
205
|
+
| ChatUISelectedToolPart
|
|
206
|
+
| ChatUISelectedResultPart
|
|
207
|
+
| ChatUISelectedGenericPart
|
|
208
|
+
|
|
209
|
+
export type ChatUISelectedPartOf<
|
|
210
|
+
TOptions,
|
|
211
|
+
TKey extends ChatUIPartKey = ChatUIPartKey,
|
|
212
|
+
> = TKey extends 'toolCall'
|
|
213
|
+
? ChatUIToolPart<TOptions>
|
|
214
|
+
: TKey extends 'toolResult'
|
|
215
|
+
? ChatUISelectedResultPart
|
|
216
|
+
: {
|
|
217
|
+
key: TKey
|
|
218
|
+
part: ChatUIPartOf<TOptions, TKey>
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export type ChatUISelectedMessage = {
|
|
222
|
+
message: UIMessage
|
|
223
|
+
parts: Array<ChatUISelectedPart>
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export type ChatUISelection = {
|
|
227
|
+
messages: Array<ChatUISelectedMessage>
|
|
228
|
+
interrupts: Array<ChatUIInterrupt>
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export type ChatUISelectInput = {
|
|
232
|
+
messages: ReadonlyArray<UIMessage>
|
|
233
|
+
interrupts?: ReadonlyArray<ChatUIInterrupt>
|
|
234
|
+
inlineToolNames?: ReadonlyArray<string>
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export type ChatUIMessages<TOptions> = Array<
|
|
238
|
+
UIMessage<ChatUIToolsOf<TOptions>, ChatUIData<TOptions>>
|
|
239
|
+
>
|
|
240
|
+
|
|
241
|
+
export type ChatUIStructuredPart<TOptions> = StructuredOutputPart<
|
|
242
|
+
ChatUIData<TOptions>
|
|
243
|
+
>
|
package/src/ui.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export { partTypeToKey, selectChatUI } from './ui/selectors'
|
|
2
|
+
export {
|
|
3
|
+
automaticPartsForMessage,
|
|
4
|
+
collectInlineToolNames,
|
|
5
|
+
resolveInterruptComponent,
|
|
6
|
+
selectMessageUI,
|
|
7
|
+
} from './ui/selectors'
|
|
8
|
+
export type {
|
|
9
|
+
ChatUIData,
|
|
10
|
+
ChatUIHasNamedInterrupts,
|
|
11
|
+
ChatUIHasNamedTools,
|
|
12
|
+
ChatUIInterrupt,
|
|
13
|
+
ChatUIInterruptName,
|
|
14
|
+
ChatUIInterruptOf,
|
|
15
|
+
ChatUIInterruptsOf,
|
|
16
|
+
ChatUINamedInterruptId,
|
|
17
|
+
ChatUIMessages,
|
|
18
|
+
ChatUIPartKey,
|
|
19
|
+
ChatUIPartOf,
|
|
20
|
+
ChatUIPartTypeByKey,
|
|
21
|
+
ChatUIRegisteredInterruptId,
|
|
22
|
+
ChatUISchemaOf,
|
|
23
|
+
ChatUISelectInput,
|
|
24
|
+
ChatUISelectedGenericPart,
|
|
25
|
+
ChatUISelectedMessage,
|
|
26
|
+
ChatUISelectedPart,
|
|
27
|
+
ChatUISelectedPartOf,
|
|
28
|
+
ChatUISelectedResultPart,
|
|
29
|
+
ChatUISelectedToolPart,
|
|
30
|
+
ChatUISelection,
|
|
31
|
+
ChatUIStructuredPart,
|
|
32
|
+
ChatUIToolApproval,
|
|
33
|
+
ChatUIToolName,
|
|
34
|
+
ChatUIToolPart,
|
|
35
|
+
ChatUIToolsOf,
|
|
36
|
+
RegisteredUIInterrupt,
|
|
37
|
+
} from './ui/types'
|