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

inject-chat.ts

339 lines11.0 KB

1import { ChatClient } from '@tanstack/ai-client'
2import { createChatDevtoolsBridge } from '@tanstack/ai-client/devtools'
3import {
4 DestroyRef,
5 Injector,
6 afterNextRender,
7 assertInInjectionContext,
8 computed,
9 effect,
10 inject,
11 signal,
12} from '@angular/core'
13import { toReactive } from './internal/to-reactive'
14import type {
15 AnyClientTool,
16 InferSchemaType,
17 ModelMessage,
18 RunAgentResumeItem,
19 SchemaInput,
20 StreamChunk,
21} from '@tanstack/ai'
22import type {
23 ChatClientState,
24 ChatInterrupt,
25 ChatInterruptState,
26 ChatResumeState,
27 ConnectionStatus,
28 InferredClientContext,
29 QueuedMessage,
30 SendMessageOptions,
31 StructuredOutputPart,
32} from '@tanstack/ai-client'
33import type {
34 DeepPartial,
35 InjectChatOptions,
36 InjectChatResult,
37 MultimodalContent,
38 UIMessage,
39} from './types'
40​
41const EMPTY_INTERRUPTS = Object.freeze([])
42const EMPTY_INTERRUPT_ERRORS = Object.freeze([])
43​
44let nextId = 0
45​
46export function injectChat<
47 const TTools extends ReadonlyArray<AnyClientTool> = any,
48 TSchema extends SchemaInput | undefined = undefined,
49 TContext = InferredClientContext<TTools>,
50>(
51 options: InjectChatOptions<
52 TTools,
53 TSchema,
54 TContext
55 > = {} as InjectChatOptions<TTools, TSchema, TContext>,
56): InjectChatResult<TTools, TSchema> {
57 assertInInjectionContext(injectChat)
58​
59 type Partial = DeepPartial<InferSchemaType<NonNullable<TSchema>>>
60 type Final = InferSchemaType<NonNullable<TSchema>>
61​
62 const destroyRef = inject(DestroyRef)
63 const injector = inject(Injector)
64 const clientId = options.id || `injectChat-${nextId++}`
65​
66 const messages = signal<Array<UIMessage<TTools>>>(
67 options.initialMessages || [],
68 )
69 const isLoading = signal(false)
70 const error = signal<Error | undefined>(undefined)
71 const status = signal<ChatClientState>('ready')
72 const isSubscribed = signal(false)
73 const connectionStatus = signal<ConnectionStatus>('disconnected')
74 const sessionGenerating = signal(false)
75 const queue = signal<Array<QueuedMessage>>([])
76 const runId = signal<string | null>(null)
77 const interruptState = signal<ChatInterruptState<TTools>>({
78 interrupts: EMPTY_INTERRUPTS,
79 pendingInterrupts: EMPTY_INTERRUPTS,
80 interruptErrors: EMPTY_INTERRUPT_ERRORS,
81 resuming: false,
82 })
83​
84 // Reactive option sources. Plain values become constant computeds.
85 const bodySource =
86 options.body !== undefined ? toReactive(options.body) : undefined
87 const forwardedPropsSource =
88 options.forwardedProps !== undefined
89 ? toReactive(options.forwardedProps)
90 : undefined
91 const contextSource =
92 options.context !== undefined ? toReactive(options.context) : undefined
93 const liveSource =
94 options.live !== undefined ? toReactive(options.live) : undefined
95​
96 const transport = options.connection
97 ? { connection: options.connection }
98 : { fetcher: options.fetcher }
99​
100 const client = new ChatClient<TTools, TContext>({
101 devtoolsBridgeFactory: createChatDevtoolsBridge,
102 ...transport,
103 id: clientId,
104 ...(options.initialMessages !== undefined && {
105 initialMessages: options.initialMessages,
106 }),
107 ...(options.persistence !== undefined && {
108 persistence: options.persistence,
109 }),
110 ...(options.initialResumeSnapshot !== undefined && {
111 initialResumeSnapshot: options.initialResumeSnapshot,
112 }),
113 ...(bodySource !== undefined && { body: bodySource() }),
114 ...(options.threadId !== undefined && { threadId: options.threadId }),
115 ...(forwardedPropsSource !== undefined && {
116 forwardedProps: forwardedPropsSource(),
117 }),
118 ...(contextSource !== undefined && { context: contextSource() }),
119 devtools: {
120 ...options.devtools,
121 framework: 'angular',
122 hookName: 'injectChat',
123 outputKind: options.outputSchema ? 'structured' : 'chat',
124 },
125 onResponse: (response) => options.onResponse?.(response),
126 onChunk: (chunk: StreamChunk) => options.onChunk?.(chunk),
127 onFinish: (message) => options.onFinish?.(message),
128 onError: (err) => options.onError?.(err),
129 onRunIdChange: (nextRunId) => runId.set(nextRunId),
130 // No `onResumeStateChange`: the run identity is surfaced as the `runId`
131 // signal (via `onRunIdChange`) and pending interrupts arrive through
132 // `onInterruptStateChange`, so there is nothing left for it to do — and it
133 // is not a public option here, matching the other framework packages.
134 onInterruptStateChange: (nextInterruptState) => {
135 interruptState.set(nextInterruptState)
136 options.onInterruptStateChange?.(nextInterruptState)
137 },
138 tools: options.tools,
139 onCustomEvent: (eventType, data, context) =>
140 options.onCustomEvent?.(eventType, data, context),
141 ...(options.streamProcessor !== undefined && {
142 streamProcessor: options.streamProcessor,
143 }),
144 onMessagesChange: (m: Array<UIMessage<TTools>>) => messages.set(m),
145 onLoadingChange: (v: boolean) => isLoading.set(v),
146 onStatusChange: (v: ChatClientState) => status.set(v),
147 onErrorChange: (v: Error | undefined) => error.set(v),
148 onSubscriptionChange: (v: boolean) => isSubscribed.set(v),
149 onConnectionStatusChange: (v: ConnectionStatus) => connectionStatus.set(v),
150 onSessionGeneratingChange: (v: boolean) => sessionGenerating.set(v),
151 ...(options.queue !== undefined && { queue: options.queue }),
152 onQueueChange: (nextQueue: Array<QueuedMessage>) => queue.set(nextQueue),
153 })
154​
155 messages.set(client.getMessages())
156 interruptState.set(client.getInterruptState())
157​
158 // START TAILING HERE, not in the constructor. A client is idle until something
159 // attaches it, so a client that gets built and thrown away never opens a
160 // connection — an unreachable stream would hold one of the browser's ~6
161 // connections per origin until the page reloaded. `inject*` runs in an injection
162 // context tied to the consumer's lifetime, and `destroyRef.onDestroy` below is the
163 // matching `detach`.
164 client.attach()
165​
166 // Sync reactive body / forwardedProps / context to the client.
167 if (bodySource || forwardedPropsSource || contextSource) {
168 effect(
169 () => {
170 const newBody = bodySource?.()
171 const newForwardedProps = forwardedPropsSource?.()
172 const newContext = contextSource?.()
173 client.updateOptions({
174 ...(newBody !== undefined && { body: newBody }),
175 ...(newForwardedProps !== undefined && {
176 forwardedProps: newForwardedProps,
177 }),
178 ...(newContext !== undefined && { context: newContext }),
179 })
180 },
181 { injector },
182 )
183 }
184​
185 // Subscribe / unsubscribe based on reactive `live`.
186 if (liveSource) {
187 effect(
188 () => {
189 if (liveSource()) {
190 client.subscribe()
191 } else {
192 client.unsubscribe()
193 }
194 },
195 { injector },
196 )
197 }
198​
199 afterNextRender(
200 () => {
201 client.mountDevtools()
202 // Delivery-durability resume is transparent: the resumable SSE
203 // connection adapter reattaches via the browser's native Last-Event-ID
204 // on reconnect. No client-side auto-resume wiring is needed.
205 },
206 { injector },
207 )
208​
209 destroyRef.onDestroy(() => {
210 // Release the connection first: the counterpart of the `attach` above.
211 client.detach()
212 if (liveSource?.()) {
213 client.unsubscribe()
214 } else {
215 client.stop()
216 }
217 client.dispose()
218 })
219​
220 // Active structured-output part = the one on the assistant message after the
221 // latest user message. Ported from ai-vue/src/use-chat.ts.
222 const activeStructuredPart = computed<StructuredOutputPart | null>(() => {
223 const list = messages()
224 let lastUserIndex = -1
225 for (let i = list.length - 1; i >= 0; i--) {
226 if (list[i]?.role === 'user') {
227 lastUserIndex = i
228 break
229 }
230 }
231 if (lastUserIndex === -1) return null
232 for (let i = list.length - 1; i > lastUserIndex; i--) {
233 const m = list[i]
234 if (m?.role !== 'assistant') continue
235 const part = m.parts.find(
236 (p): p is StructuredOutputPart => p.type === 'structured-output',
237 )
238 if (part) return part
239 }
240 return null
241 })
242​
243 const partial = computed<Partial>(() => {
244 const part = activeStructuredPart()
245 if (!part) return {} as Partial
246 const v = part.partial ?? part.data
247 return (v ?? {}) as Partial
248 })
249​
250 const final = computed<Final | null>(() => {
251 const part = activeStructuredPart()
252 if (!part || part.status !== 'complete') return null
253 return part.data as Final
254 })
255​
256 const sendMessage = async (
257 content: string | MultimodalContent,
258 sendOptions?: SendMessageOptions,
259 ) => {
260 await client.sendMessage(content, undefined, sendOptions)
261 }
262 const cancelQueued = (id: string) => client.cancelQueued(id)
263 const append = async (message: ModelMessage | UIMessage<TTools>) => {
264 await client.append(message)
265 }
266 const reload = async () => {
267 await client.reload()
268 }
269 const stop = () => client.stop()
270 const clear = () => client.clear()
271 const setMessages = (m: Array<UIMessage<TTools>>) =>
272 client.setMessagesManually(m)
273 const addToolResult = async (result: {
274 toolCallId: string
275 tool: string
276 output: any
277 state?: 'output-available' | 'output-error'
278 errorText?: string
279 }) => {
280 await client.addToolResult(result)
281 }
282 const addToolApprovalResponse = async (response: {
283 id: string
284 approved: boolean
285 }) => {
286 await client.addToolApprovalResponse(response)
287 }
288 const interrupts = computed(() => interruptState().interrupts)
289 const pendingInterrupts = computed(() => interruptState().interrupts)
290 const interruptErrors = computed(() => interruptState().interruptErrors)
291 const resuming = computed(() => interruptState().resuming)
292 const resolveInterrupts = (
293 resolution: boolean | ((interrupt: ChatInterrupt<TTools>) => undefined),
294 ) => {
295 if (typeof resolution === 'boolean') {
296 client.resolveInterrupts(resolution)
297 } else {
298 client.resolveInterrupts(resolution)
299 }
300 }
301 const cancelInterrupts = () => client.cancelInterrupts()
302 const retryInterrupts = () => client.retryInterrupts()
303 const resumeInterruptsUnsafe = (
304 resumeItems: Array<RunAgentResumeItem>,
305 state?: ChatResumeState,
306 ) => client.resumeInterruptsUnsafe(resumeItems, state)
307​
308 // oxlint-disable-next-line eslint-js/no-restricted-syntax -- return shape diverges from conditional InjectChatResult<TTools, TSchema>; TS can't structurally narrow the TSchema-gated partial/final signals
309 return {
310 messages: messages.asReadonly(),
311 sendMessage,
312 queue: queue.asReadonly(),
313 cancelQueued,
314 append,
315 reload,
316 stop,
317 isLoading: isLoading.asReadonly(),
318 error: error.asReadonly(),
319 status: status.asReadonly(),
320 isSubscribed: isSubscribed.asReadonly(),
321 connectionStatus: connectionStatus.asReadonly(),
322 sessionGenerating: sessionGenerating.asReadonly(),
323 setMessages,
324 clear,
325 addToolResult,
326 addToolApprovalResponse,
327 runId: runId.asReadonly(),
328 interrupts,
329 pendingInterrupts,
330 interruptErrors,
331 resuming,
332 resolveInterrupts,
333 cancelInterrupts,
334 retryInterrupts,
335 resumeInterruptsUnsafe,
336 partial,
337 final,
338 } as unknown as InjectChatResult<TTools, TSchema>