@tanstack/ai-angular 0.7.3 → 0.8.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/dist/fesm2022/tanstack-ai-angular-ui.mjs +408 -0
- package/dist/fesm2022/tanstack-ai-angular-ui.mjs.map +1 -0
- package/dist/types/tanstack-ai-angular-ui.d.ts +162 -0
- package/package.json +12 -6
- package/ui/ng-package.json +5 -0
- package/ui/src/create-chat-hook.ts +79 -0
- package/ui/src/create-ui.ts +519 -0
- package/ui/src/index.ts +21 -0
- package/ui/src/tokens.ts +47 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-angular",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Angular signals integration for TanStack AI streaming chat, structured outputs, and media generation.",
|
|
5
5
|
"author": "",
|
|
6
6
|
"license": "MIT",
|
|
@@ -33,6 +33,10 @@
|
|
|
33
33
|
},
|
|
34
34
|
"./package.json": {
|
|
35
35
|
"default": "./package.json"
|
|
36
|
+
},
|
|
37
|
+
"./ui": {
|
|
38
|
+
"types": "./dist/types/tanstack-ai-angular-ui.d.ts",
|
|
39
|
+
"default": "./dist/fesm2022/tanstack-ai-angular-ui.mjs"
|
|
36
40
|
}
|
|
37
41
|
},
|
|
38
42
|
"sideEffects": false,
|
|
@@ -41,15 +45,17 @@
|
|
|
41
45
|
},
|
|
42
46
|
"files": [
|
|
43
47
|
"dist",
|
|
44
|
-
"src"
|
|
48
|
+
"src",
|
|
49
|
+
"ui"
|
|
45
50
|
],
|
|
46
51
|
"dependencies": {
|
|
47
52
|
"tslib": "^2.8.1",
|
|
48
|
-
"@tanstack/ai-client": "^0.
|
|
53
|
+
"@tanstack/ai-client": "^0.30.0"
|
|
49
54
|
},
|
|
50
55
|
"peerDependencies": {
|
|
56
|
+
"@angular/common": ">=21.0.0",
|
|
51
57
|
"@angular/core": ">=21.0.0",
|
|
52
|
-
"@tanstack/ai": "^0.
|
|
58
|
+
"@tanstack/ai": "^0.52.1"
|
|
53
59
|
},
|
|
54
60
|
"devDependencies": {
|
|
55
61
|
"@analogjs/vite-plugin-angular": "^2.6.2",
|
|
@@ -66,11 +72,11 @@
|
|
|
66
72
|
"vitest": "^4.1.10",
|
|
67
73
|
"zod": "^4.2.0",
|
|
68
74
|
"zone.js": "^0.15.0",
|
|
69
|
-
"@tanstack/ai": "0.
|
|
75
|
+
"@tanstack/ai": "0.52.1"
|
|
70
76
|
},
|
|
71
77
|
"scripts": {
|
|
72
78
|
"clean": "premove ./build ./dist",
|
|
73
|
-
"test:oxlint": "oxlint src --type-aware",
|
|
79
|
+
"test:oxlint": "oxlint src ui/src --type-aware",
|
|
74
80
|
"test:lib": "vitest run",
|
|
75
81
|
"test:lib:dev": "vitest",
|
|
76
82
|
"test:types": "tsc",
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { assertInInjectionContext } from '@angular/core'
|
|
2
|
+
import type { Type } from '@angular/core'
|
|
3
|
+
import { injectChat } from '@tanstack/ai-angular'
|
|
4
|
+
import type { InjectChatOptions } from '@tanstack/ai-angular'
|
|
5
|
+
import { createChatUI } from './create-ui'
|
|
6
|
+
import type { ChatUIFactoryConfig, ChatUIHost, InputProps } from './create-ui'
|
|
7
|
+
import type {
|
|
8
|
+
ChatUIInterruptsOf,
|
|
9
|
+
ChatUISchemaOf,
|
|
10
|
+
ChatUIToolsOf,
|
|
11
|
+
} from '@tanstack/ai-client/ui'
|
|
12
|
+
import type { InferredClientContext } from '@tanstack/ai-client'
|
|
13
|
+
|
|
14
|
+
type HeadlessOptions<TOptions> = InjectChatOptions<
|
|
15
|
+
ChatUIToolsOf<TOptions>,
|
|
16
|
+
ChatUISchemaOf<TOptions>,
|
|
17
|
+
InferredClientContext<ChatUIToolsOf<TOptions>>,
|
|
18
|
+
ChatUIInterruptsOf<TOptions>
|
|
19
|
+
>
|
|
20
|
+
|
|
21
|
+
type ChatInstanceOverrides<TOptions> = {
|
|
22
|
+
threadId?: string
|
|
23
|
+
live?: HeadlessOptions<TOptions>['live']
|
|
24
|
+
forwardedProps?: HeadlessOptions<TOptions>['forwardedProps']
|
|
25
|
+
body?: HeadlessOptions<TOptions>['body']
|
|
26
|
+
initialMessages?: HeadlessOptions<TOptions>['initialMessages']
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Bind chat options and Angular widgets once at module scope.
|
|
31
|
+
*
|
|
32
|
+
* This matches Form `injectForm` and the React `createChatHook` factory:
|
|
33
|
+
* widgets register here. The returned `injectAppChat` creates a chat
|
|
34
|
+
* instance in an injection context. Render `<ai-chat [chat]="chat" />`
|
|
35
|
+
* with the factory `Chat` component.
|
|
36
|
+
*/
|
|
37
|
+
export function createChatHook<
|
|
38
|
+
const TOptions,
|
|
39
|
+
TInput extends Type<unknown> | undefined = Type<unknown> | undefined,
|
|
40
|
+
>({
|
|
41
|
+
options,
|
|
42
|
+
...chatComponents
|
|
43
|
+
}: {
|
|
44
|
+
options: TOptions
|
|
45
|
+
} & ChatUIFactoryConfig<NoInfer<TOptions>, TInput>) {
|
|
46
|
+
const ui = createChatUI(
|
|
47
|
+
options,
|
|
48
|
+
chatComponents as ChatUIFactoryConfig<NoInfer<TOptions>, TInput>,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
function injectAppChat(overrides?: ChatInstanceOverrides<TOptions>) {
|
|
52
|
+
assertInInjectionContext(injectAppChat)
|
|
53
|
+
const chat = (
|
|
54
|
+
overrides
|
|
55
|
+
? injectChat({
|
|
56
|
+
...(options as HeadlessOptions<TOptions>),
|
|
57
|
+
...overrides,
|
|
58
|
+
})
|
|
59
|
+
: injectChat(options as HeadlessOptions<TOptions>)
|
|
60
|
+
) as ChatUIHost<TOptions>
|
|
61
|
+
return chat
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
injectAppChat,
|
|
66
|
+
injectChatContext: ui.injectChatContext,
|
|
67
|
+
Chat: ui.Chat,
|
|
68
|
+
ChatProvider: ui.ChatProvider,
|
|
69
|
+
} as {
|
|
70
|
+
injectAppChat: (
|
|
71
|
+
overrides?: ChatInstanceOverrides<TOptions>,
|
|
72
|
+
) => ChatUIHost<TOptions>
|
|
73
|
+
injectChatContext: () => ChatUIHost<TOptions>
|
|
74
|
+
Chat: Type<unknown>
|
|
75
|
+
ChatProvider: Type<unknown>
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export type { InputProps }
|
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ChangeDetectionStrategy,
|
|
3
|
+
Component,
|
|
4
|
+
Injector,
|
|
5
|
+
computed,
|
|
6
|
+
effect,
|
|
7
|
+
inject,
|
|
8
|
+
input,
|
|
9
|
+
untracked,
|
|
10
|
+
} from '@angular/core'
|
|
11
|
+
import { NgComponentOutlet } from '@angular/common'
|
|
12
|
+
import type { Type } from '@angular/core'
|
|
13
|
+
import {
|
|
14
|
+
automaticPartsForMessage,
|
|
15
|
+
collectInlineToolNames,
|
|
16
|
+
resolveInterruptComponent,
|
|
17
|
+
selectChatUI,
|
|
18
|
+
selectMessageUI,
|
|
19
|
+
} from '@tanstack/ai-client/ui'
|
|
20
|
+
import type {
|
|
21
|
+
ChatUIHasNamedInterrupts,
|
|
22
|
+
ChatUIHasNamedTools,
|
|
23
|
+
ChatUIInterrupt,
|
|
24
|
+
ChatUIInterruptName,
|
|
25
|
+
ChatUIInterruptOf,
|
|
26
|
+
ChatUIInterruptsOf,
|
|
27
|
+
ChatUIMessages,
|
|
28
|
+
ChatUINamedInterruptId,
|
|
29
|
+
ChatUIPartKey,
|
|
30
|
+
ChatUIPartOf,
|
|
31
|
+
ChatUISchemaOf,
|
|
32
|
+
ChatUISelectedPart,
|
|
33
|
+
ChatUIToolApproval,
|
|
34
|
+
ChatUIToolName,
|
|
35
|
+
ChatUIToolsOf,
|
|
36
|
+
} from '@tanstack/ai-client/ui'
|
|
37
|
+
import type {
|
|
38
|
+
InferredClientContext,
|
|
39
|
+
QueuedMessage,
|
|
40
|
+
ToolCallPart,
|
|
41
|
+
ToolResultPart,
|
|
42
|
+
} from '@tanstack/ai-client'
|
|
43
|
+
import type { InjectChatResult } from '@tanstack/ai-angular'
|
|
44
|
+
import { ChatHostRef, defaultChatUITokens } from './tokens'
|
|
45
|
+
import type { ChatUITokens } from './tokens'
|
|
46
|
+
|
|
47
|
+
export type ChatUIHost<TOptions = unknown> = InjectChatResult<
|
|
48
|
+
ChatUIToolsOf<TOptions>,
|
|
49
|
+
ChatUISchemaOf<TOptions>,
|
|
50
|
+
ChatUIInterruptsOf<TOptions>
|
|
51
|
+
>
|
|
52
|
+
|
|
53
|
+
export type ChatUIQueueItem = QueuedMessage & {
|
|
54
|
+
cancelQueued: () => void
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type LayoutProps<
|
|
58
|
+
TOptions,
|
|
59
|
+
TInput extends Type<unknown> | undefined = Type<unknown>,
|
|
60
|
+
> = {
|
|
61
|
+
Messages: Type<unknown>
|
|
62
|
+
Interrupts: Type<unknown>
|
|
63
|
+
Queue: Type<unknown>
|
|
64
|
+
readonly __ui?: TOptions
|
|
65
|
+
} & (TInput extends Type<unknown> ? { Input: Type<unknown> } : {})
|
|
66
|
+
|
|
67
|
+
export type MessageProps<TOptions> = {
|
|
68
|
+
message: ChatUIMessages<TOptions>[number]
|
|
69
|
+
Parts: Type<unknown>
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type InputProps<TOptions> = {
|
|
73
|
+
readonly __ui?: TOptions
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export type QueueProps<TOptions> = {
|
|
77
|
+
item: ChatUIQueueItem
|
|
78
|
+
readonly __ui?: TOptions
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export type PartProps<TOptions, TKey extends ChatUIPartKey = ChatUIPartKey> = {
|
|
82
|
+
part: ChatUIPartOf<TOptions, TKey>
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export type ToolProps<
|
|
86
|
+
TOptions,
|
|
87
|
+
TName extends ChatUIToolName<TOptions> = ChatUIToolName<TOptions>,
|
|
88
|
+
> = {
|
|
89
|
+
part: Extract<ToolCallPart<ChatUIToolsOf<TOptions>>, { name: TName }>
|
|
90
|
+
result?: ToolResultPart
|
|
91
|
+
interrupt?: ChatUIToolApproval<TOptions, TName>
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export type InterruptProps<
|
|
95
|
+
TOptions,
|
|
96
|
+
TName extends ChatUIInterruptName<TOptions> = never,
|
|
97
|
+
> = {
|
|
98
|
+
interrupt: ChatUIInterruptOf<TOptions, TName>
|
|
99
|
+
readonly __ui?: TOptions
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
type GenericInterruptComponents<TOptions> =
|
|
103
|
+
ChatUIHasNamedInterrupts<TOptions> extends true
|
|
104
|
+
? {
|
|
105
|
+
[K in ChatUINamedInterruptId<TOptions>]: Type<unknown>
|
|
106
|
+
} & {
|
|
107
|
+
fallback?: Type<unknown>
|
|
108
|
+
}
|
|
109
|
+
: {
|
|
110
|
+
fallback?: Type<unknown>
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
type ToolApprovalMap<TOptions> = {
|
|
114
|
+
[K in ChatUIToolName<TOptions>]?: Type<unknown>
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export type ChatUIChromeComponents<
|
|
118
|
+
TOptions,
|
|
119
|
+
TInput extends Type<unknown> | undefined = Type<unknown>,
|
|
120
|
+
> = {
|
|
121
|
+
layout: Type<unknown>
|
|
122
|
+
message: Type<unknown>
|
|
123
|
+
input?: TInput
|
|
124
|
+
queue?: Type<unknown>
|
|
125
|
+
readonly __ui?: TOptions
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export type ChatUIPartsComponents<TOptions> = {
|
|
129
|
+
[K in ChatUIPartKey]?: Type<unknown>
|
|
130
|
+
} & {
|
|
131
|
+
fallback?: Type<unknown>
|
|
132
|
+
readonly __ui?: TOptions
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export type ChatUIInterruptsComponents<TOptions> = {
|
|
136
|
+
tools?: ToolApprovalMap<TOptions>
|
|
137
|
+
generic: GenericInterruptComponents<TOptions>
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export type ChatUIComponents<
|
|
141
|
+
TOptions,
|
|
142
|
+
TInput extends Type<unknown> | undefined = Type<unknown>,
|
|
143
|
+
> = {
|
|
144
|
+
components: ChatUIChromeComponents<TOptions, TInput>
|
|
145
|
+
partsComponents: ChatUIPartsComponents<TOptions>
|
|
146
|
+
} & (ChatUIHasNamedTools<TOptions> extends true
|
|
147
|
+
? {
|
|
148
|
+
toolsComponents: {
|
|
149
|
+
[K in ChatUIToolName<TOptions>]: Type<unknown>
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
: {
|
|
153
|
+
toolsComponents?: {
|
|
154
|
+
[K in ChatUIToolName<TOptions>]?: Type<unknown>
|
|
155
|
+
}
|
|
156
|
+
}) &
|
|
157
|
+
(ChatUIHasNamedInterrupts<TOptions> extends true
|
|
158
|
+
? { interruptsComponents: ChatUIInterruptsComponents<TOptions> }
|
|
159
|
+
: {
|
|
160
|
+
interruptsComponents?: {
|
|
161
|
+
tools?: ToolApprovalMap<TOptions>
|
|
162
|
+
generic?: GenericInterruptComponents<TOptions>
|
|
163
|
+
}
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
export type ChatUIContextConfig = {
|
|
167
|
+
chatRef?: ChatUITokens['chatRef']
|
|
168
|
+
part?: ChatUITokens['part']
|
|
169
|
+
interrupt?: ChatUITokens['interrupt']
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export type ChatUIFactoryConfig<
|
|
173
|
+
TOptions,
|
|
174
|
+
TInput extends Type<unknown> | undefined = Type<unknown>,
|
|
175
|
+
> = ChatUIComponents<TOptions, TInput> & {
|
|
176
|
+
context?: ChatUIContextConfig
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function createWarnOnce() {
|
|
180
|
+
const seen = new Set<string>()
|
|
181
|
+
return (key: string, message: string) => {
|
|
182
|
+
if (process.env.NODE_ENV === 'production') return
|
|
183
|
+
if (seen.has(key)) return
|
|
184
|
+
seen.add(key)
|
|
185
|
+
console.warn(message)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Bind chat options and Angular widgets once at module scope.
|
|
191
|
+
*
|
|
192
|
+
* Layout, message, input, queue, parts, tools, and interrupts are
|
|
193
|
+
* standalone component classes. The factory builds `Chat`, `Messages`,
|
|
194
|
+
* `Interrupts`, and `Queue` as Angular components that inject the active
|
|
195
|
+
* chat through DI.
|
|
196
|
+
*/
|
|
197
|
+
export function createChatUI<
|
|
198
|
+
const TOptions,
|
|
199
|
+
TInput extends Type<unknown> | undefined = Type<unknown> | undefined,
|
|
200
|
+
>(options: TOptions, config: ChatUIFactoryConfig<NoInfer<TOptions>, TInput>) {
|
|
201
|
+
void options
|
|
202
|
+
const {
|
|
203
|
+
context: contextOption,
|
|
204
|
+
components,
|
|
205
|
+
partsComponents: parts,
|
|
206
|
+
toolsComponents: tools,
|
|
207
|
+
interruptsComponents: interrupts,
|
|
208
|
+
} = config as ChatUIFactoryConfig<TOptions, TInput> & {
|
|
209
|
+
toolsComponents?: Record<string, Type<unknown> | undefined>
|
|
210
|
+
interruptsComponents?: {
|
|
211
|
+
tools?: Record<string, Type<unknown> | undefined>
|
|
212
|
+
generic?: Record<string, Type<unknown> | undefined>
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
const {
|
|
216
|
+
layout: Layout,
|
|
217
|
+
message: MessageComponent,
|
|
218
|
+
input: InputComponent,
|
|
219
|
+
queue: QueueItemComponent,
|
|
220
|
+
} = components
|
|
221
|
+
const warn = createWarnOnce()
|
|
222
|
+
const tokens = {
|
|
223
|
+
chatRef: contextOption?.chatRef ?? defaultChatUITokens.chatRef,
|
|
224
|
+
part: contextOption?.part ?? defaultChatUITokens.part,
|
|
225
|
+
interrupt: contextOption?.interrupt ?? defaultChatUITokens.interrupt,
|
|
226
|
+
}
|
|
227
|
+
const inlineToolNames = collectInlineToolNames(
|
|
228
|
+
interrupts?.tools as Record<string, unknown> | undefined,
|
|
229
|
+
Object.keys(tools ?? {}),
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
function injectChatContext() {
|
|
233
|
+
const ref = inject(tokens.chatRef)
|
|
234
|
+
const chat = ref.host()
|
|
235
|
+
if (!chat) {
|
|
236
|
+
throw new Error(
|
|
237
|
+
'`injectChatContext` must be used within `Chat` or `ChatProvider`.',
|
|
238
|
+
)
|
|
239
|
+
}
|
|
240
|
+
return chat as ChatUIHost<TOptions>
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
@Component({
|
|
244
|
+
selector: 'ai-chat-missing-input',
|
|
245
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
246
|
+
template: '',
|
|
247
|
+
})
|
|
248
|
+
class MissingInput {
|
|
249
|
+
constructor() {
|
|
250
|
+
warn(
|
|
251
|
+
'input',
|
|
252
|
+
'[tanstack-ai-ui] Rendered Input but no `input` component is registered.',
|
|
253
|
+
)
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
@Component({
|
|
258
|
+
selector: 'ai-chat-queue',
|
|
259
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
260
|
+
imports: [NgComponentOutlet],
|
|
261
|
+
template: `
|
|
262
|
+
@if (itemComponent) {
|
|
263
|
+
@for (row of rows(); track row.item.id) {
|
|
264
|
+
<ng-container
|
|
265
|
+
[ngComponentOutlet]="itemComponent"
|
|
266
|
+
[ngComponentOutletInputs]="{ item: row.item }"
|
|
267
|
+
/>
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
`,
|
|
271
|
+
})
|
|
272
|
+
class Queue {
|
|
273
|
+
readonly ref = inject(tokens.chatRef)
|
|
274
|
+
readonly itemComponent = QueueItemComponent
|
|
275
|
+
readonly rows = computed(() => {
|
|
276
|
+
const chat = this.ref.host()
|
|
277
|
+
if (!chat || !QueueItemComponent) return []
|
|
278
|
+
const items = chat.queue()
|
|
279
|
+
return items.map((item) => ({
|
|
280
|
+
item: {
|
|
281
|
+
...item,
|
|
282
|
+
cancelQueued: () => {
|
|
283
|
+
chat.cancelQueued(item.id)
|
|
284
|
+
},
|
|
285
|
+
},
|
|
286
|
+
}))
|
|
287
|
+
})
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
@Component({
|
|
291
|
+
selector: 'ai-chat-parts',
|
|
292
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
293
|
+
imports: [NgComponentOutlet],
|
|
294
|
+
template: `
|
|
295
|
+
@for (selected of selectedParts(); track $index) {
|
|
296
|
+
<ng-container
|
|
297
|
+
[ngComponentOutlet]="outletFor(selected)"
|
|
298
|
+
[ngComponentOutletInputs]="inputsFor(selected)"
|
|
299
|
+
[ngComponentOutletInjector]="injectorFor(selected)"
|
|
300
|
+
/>
|
|
301
|
+
}
|
|
302
|
+
`,
|
|
303
|
+
})
|
|
304
|
+
class Parts {
|
|
305
|
+
readonly injector = inject(Injector)
|
|
306
|
+
readonly ref = inject(tokens.chatRef)
|
|
307
|
+
message = input.required<ChatUIMessages<TOptions>[number]>()
|
|
308
|
+
|
|
309
|
+
readonly selectedParts = computed(() => {
|
|
310
|
+
const chat = this.ref.host()
|
|
311
|
+
return automaticPartsForMessage(
|
|
312
|
+
selectMessageUI(this.message(), {
|
|
313
|
+
interrupts: (chat?.interrupts() ??
|
|
314
|
+
[]) as ReadonlyArray<ChatUIInterrupt>,
|
|
315
|
+
inlineToolNames,
|
|
316
|
+
}),
|
|
317
|
+
)
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
outletFor(selected: ChatUISelectedPart) {
|
|
321
|
+
if (selected.key === 'toolCall') {
|
|
322
|
+
const name = selected.part.name
|
|
323
|
+
const Tool = tools?.[name as ChatUIToolName<TOptions>]
|
|
324
|
+
if (!Tool) {
|
|
325
|
+
warn(
|
|
326
|
+
`tool:${name}`,
|
|
327
|
+
`[tanstack-ai-ui] Missing tools.${name} component`,
|
|
328
|
+
)
|
|
329
|
+
return null
|
|
330
|
+
}
|
|
331
|
+
return Tool
|
|
332
|
+
}
|
|
333
|
+
const PartComponent = (parts[selected.key] ?? parts.fallback) as
|
|
334
|
+
| Type<unknown>
|
|
335
|
+
| undefined
|
|
336
|
+
if (!PartComponent) {
|
|
337
|
+
warn(
|
|
338
|
+
`part:${selected.key}`,
|
|
339
|
+
`[tanstack-ai-ui] Missing parts.${selected.key} component`,
|
|
340
|
+
)
|
|
341
|
+
return null
|
|
342
|
+
}
|
|
343
|
+
return PartComponent
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
inputsFor(selected: ChatUISelectedPart) {
|
|
347
|
+
if (selected.key === 'toolCall') {
|
|
348
|
+
return {
|
|
349
|
+
part: selected.part,
|
|
350
|
+
result: selected.result,
|
|
351
|
+
interrupt: selected.interrupt,
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return { part: selected.part }
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
injectorFor(selected: ChatUISelectedPart) {
|
|
358
|
+
return Injector.create({
|
|
359
|
+
parent: this.injector,
|
|
360
|
+
providers: [{ provide: tokens.part, useValue: selected }],
|
|
361
|
+
})
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
@Component({
|
|
366
|
+
selector: 'ai-chat-messages',
|
|
367
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
368
|
+
imports: [NgComponentOutlet],
|
|
369
|
+
template: `
|
|
370
|
+
@for (message of messages(); track message.id) {
|
|
371
|
+
<ng-container
|
|
372
|
+
[ngComponentOutlet]="messageComponent"
|
|
373
|
+
[ngComponentOutletInputs]="{
|
|
374
|
+
message,
|
|
375
|
+
Parts,
|
|
376
|
+
}"
|
|
377
|
+
/>
|
|
378
|
+
}
|
|
379
|
+
`,
|
|
380
|
+
})
|
|
381
|
+
class Messages {
|
|
382
|
+
readonly ref = inject(tokens.chatRef)
|
|
383
|
+
readonly messageComponent = MessageComponent
|
|
384
|
+
readonly Parts = Parts
|
|
385
|
+
readonly messages = computed(() => {
|
|
386
|
+
const chat = this.ref.host()
|
|
387
|
+
return (chat?.messages() ?? []) as ChatUIMessages<TOptions>
|
|
388
|
+
})
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
@Component({
|
|
392
|
+
selector: 'ai-chat-interrupts',
|
|
393
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
394
|
+
imports: [NgComponentOutlet],
|
|
395
|
+
template: `
|
|
396
|
+
@for (interrupt of interrupts(); track interrupt.id) {
|
|
397
|
+
<ng-container
|
|
398
|
+
[ngComponentOutlet]="outletFor(interrupt)"
|
|
399
|
+
[ngComponentOutletInputs]="{ interrupt }"
|
|
400
|
+
[ngComponentOutletInjector]="injectorFor(interrupt)"
|
|
401
|
+
/>
|
|
402
|
+
}
|
|
403
|
+
`,
|
|
404
|
+
})
|
|
405
|
+
class Interrupts {
|
|
406
|
+
readonly injector = inject(Injector)
|
|
407
|
+
readonly ref = inject(tokens.chatRef)
|
|
408
|
+
readonly interrupts = computed(() => {
|
|
409
|
+
const chat = this.ref.host()
|
|
410
|
+
const selected = selectChatUI({
|
|
411
|
+
messages: (chat?.messages() ?? []) as ChatUIMessages<TOptions>,
|
|
412
|
+
interrupts: (chat?.interrupts() ??
|
|
413
|
+
[]) as ReadonlyArray<ChatUIInterrupt>,
|
|
414
|
+
inlineToolNames,
|
|
415
|
+
})
|
|
416
|
+
return selected.interrupts
|
|
417
|
+
})
|
|
418
|
+
|
|
419
|
+
outletFor(interrupt: ChatUIInterrupt) {
|
|
420
|
+
const InterruptComponent = resolveInterruptComponent(
|
|
421
|
+
interrupt,
|
|
422
|
+
interrupts,
|
|
423
|
+
) as Type<unknown> | undefined
|
|
424
|
+
if (!InterruptComponent) {
|
|
425
|
+
warn(
|
|
426
|
+
`interrupt:${interrupt.id}`,
|
|
427
|
+
`[tanstack-ai-ui] Missing interrupt component for ${interrupt.kind}`,
|
|
428
|
+
)
|
|
429
|
+
return null
|
|
430
|
+
}
|
|
431
|
+
return InterruptComponent
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
injectorFor(interrupt: ChatUIInterrupt) {
|
|
435
|
+
return Injector.create({
|
|
436
|
+
parent: this.injector,
|
|
437
|
+
providers: [{ provide: tokens.interrupt, useValue: interrupt }],
|
|
438
|
+
})
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const layoutInputs = {
|
|
443
|
+
Messages,
|
|
444
|
+
Interrupts,
|
|
445
|
+
Queue,
|
|
446
|
+
Input: (InputComponent ?? MissingInput) as Type<unknown>,
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
@Component({
|
|
450
|
+
selector: 'ai-chat',
|
|
451
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
452
|
+
imports: [NgComponentOutlet],
|
|
453
|
+
providers: [{ provide: tokens.chatRef, useClass: ChatHostRef }],
|
|
454
|
+
template: `
|
|
455
|
+
<ng-container
|
|
456
|
+
[ngComponentOutlet]="layout"
|
|
457
|
+
[ngComponentOutletInputs]="layoutInputs"
|
|
458
|
+
/>
|
|
459
|
+
`,
|
|
460
|
+
})
|
|
461
|
+
class Chat {
|
|
462
|
+
chat = input.required<ChatUIHost<TOptions>>()
|
|
463
|
+
readonly layout = Layout
|
|
464
|
+
readonly layoutInputs = layoutInputs
|
|
465
|
+
readonly ref = inject(tokens.chatRef)
|
|
466
|
+
|
|
467
|
+
constructor() {
|
|
468
|
+
effect(() => {
|
|
469
|
+
const next = this.chat()
|
|
470
|
+
untracked(() => this.ref.host.set(next as InjectChatResult))
|
|
471
|
+
})
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
@Component({
|
|
476
|
+
selector: 'ai-chat-provider',
|
|
477
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
478
|
+
providers: [{ provide: tokens.chatRef, useClass: ChatHostRef }],
|
|
479
|
+
template: `<ng-content />`,
|
|
480
|
+
})
|
|
481
|
+
class ChatProvider {
|
|
482
|
+
chat = input.required<ChatUIHost<TOptions>>()
|
|
483
|
+
readonly ref = inject(tokens.chatRef)
|
|
484
|
+
|
|
485
|
+
constructor() {
|
|
486
|
+
effect(() => {
|
|
487
|
+
const next = this.chat()
|
|
488
|
+
untracked(() => this.ref.host.set(next as InjectChatResult))
|
|
489
|
+
})
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
return {
|
|
494
|
+
Chat,
|
|
495
|
+
ChatProvider,
|
|
496
|
+
Messages,
|
|
497
|
+
Interrupts,
|
|
498
|
+
Queue,
|
|
499
|
+
Parts,
|
|
500
|
+
injectChatContext,
|
|
501
|
+
Input: InputComponent,
|
|
502
|
+
} as ChatUIKit<TOptions, TInput>
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
export type ChatUIKit<
|
|
506
|
+
TOptions,
|
|
507
|
+
TInput extends Type<unknown> | undefined = Type<unknown>,
|
|
508
|
+
> = {
|
|
509
|
+
Chat: Type<unknown>
|
|
510
|
+
ChatProvider: Type<unknown>
|
|
511
|
+
Messages: Type<unknown>
|
|
512
|
+
Interrupts: Type<unknown>
|
|
513
|
+
Queue: Type<unknown>
|
|
514
|
+
Parts: Type<unknown>
|
|
515
|
+
injectChatContext: () => ChatUIHost<TOptions>
|
|
516
|
+
Input: TInput
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
export type { InferredClientContext }
|
package/ui/src/index.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export {
|
|
2
|
+
createChatUI,
|
|
3
|
+
type ChatUIComponents,
|
|
4
|
+
type ChatUIFactoryConfig,
|
|
5
|
+
type ChatUIHost,
|
|
6
|
+
type ChatUIKit,
|
|
7
|
+
type ChatUIQueueItem,
|
|
8
|
+
type InputProps,
|
|
9
|
+
type InterruptProps,
|
|
10
|
+
type LayoutProps,
|
|
11
|
+
type MessageProps,
|
|
12
|
+
type PartProps,
|
|
13
|
+
type QueueProps,
|
|
14
|
+
type ToolProps,
|
|
15
|
+
} from './create-ui'
|
|
16
|
+
export { createChatHook } from './create-chat-hook'
|
|
17
|
+
export {
|
|
18
|
+
createChatHookContexts,
|
|
19
|
+
ChatHostRef,
|
|
20
|
+
type ChatUITokens,
|
|
21
|
+
} from './tokens'
|