@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.
@@ -0,0 +1,47 @@
1
+ import { InjectionToken, inject, signal } from '@angular/core'
2
+ import type { WritableSignal } from '@angular/core'
3
+ import type {
4
+ ChatUIInterrupt,
5
+ ChatUISelectedPart,
6
+ } from '@tanstack/ai-client/ui'
7
+ import type { InjectChatResult } from '@tanstack/ai-angular'
8
+
9
+ /**
10
+ * Holder for the active chat instance. Factory components inject this
11
+ * and read `host()`.
12
+ */
13
+ export class ChatHostRef {
14
+ readonly host: WritableSignal<InjectChatResult | null> = signal(null)
15
+ }
16
+
17
+ export interface ChatUITokens {
18
+ chatRef: InjectionToken<ChatHostRef>
19
+ part: InjectionToken<ChatUISelectedPart>
20
+ interrupt: InjectionToken<ChatUIInterrupt>
21
+ injectChatContext: () => InjectChatResult
22
+ }
23
+
24
+ /**
25
+ * Create isolated tokens for nested chat trees, or for widgets in other
26
+ * files that cannot import the `createChatHook` result.
27
+ */
28
+ export function createChatHookContexts(): ChatUITokens {
29
+ const chatRef = new InjectionToken<ChatHostRef>('ChatUIHostRef')
30
+ const part = new InjectionToken<ChatUISelectedPart>('ChatUIPart')
31
+ const interrupt = new InjectionToken<ChatUIInterrupt>('ChatUIInterrupt')
32
+
33
+ function injectChatContext() {
34
+ const ref = inject(chatRef)
35
+ const chat = ref.host()
36
+ if (!chat) {
37
+ throw new Error(
38
+ '`injectChatContext` must be used within `Chat` or `ChatProvider`.',
39
+ )
40
+ }
41
+ return chat
42
+ }
43
+
44
+ return { chatRef, part, interrupt, injectChatContext }
45
+ }
46
+
47
+ export const defaultChatUITokens = createChatHookContexts()