MCPcopy
hub / github.com/TanStack/ai / injectChat

Function injectChat

packages/ai-angular/src/inject-chat.ts:37–255  ·  view source on GitHub ↗
(
  options: InjectChatOptions<
    TTools,
    TSchema,
    TContext
  > = {} as InjectChatOptions<TTools, TSchema, TContext>,
)

Source from the content-addressed store, hash-verified

35let nextId = 0
36
37export function injectChat<
38 TTools extends ReadonlyArray<AnyClientTool> = any,
39 TSchema extends SchemaInput | undefined = undefined,
40 TContext = InferredClientContext<TTools>,
41>(
42 options: InjectChatOptions<
43 TTools,
44 TSchema,
45 TContext
46 > = {} as InjectChatOptions<TTools, TSchema, TContext>,
47): InjectChatResult<TTools, TSchema> {
48 assertInInjectionContext(injectChat)
49
50 type Partial = DeepPartial<InferSchemaType<NonNullable<TSchema>>>
51 type Final = InferSchemaType<NonNullable<TSchema>>
52
53 const destroyRef = inject(DestroyRef)
54 const injector = inject(Injector)
55 const clientId = options.id || `injectChat-${nextId++}`
56
57 const messages = signal<Array<UIMessage<TTools>>>(
58 options.initialMessages || [],
59 )
60 const isLoading = signal(false)
61 const error = signal<Error | undefined>(undefined)
62 const status = signal<ChatClientState>('ready')
63 const isSubscribed = signal(false)
64 const connectionStatus = signal<ConnectionStatus>('disconnected')
65 const sessionGenerating = signal(false)
66
67 // Reactive option sources. Plain values become constant computeds.
68 const bodySource =
69 options.body !== undefined ? toReactive(options.body) : undefined
70 const forwardedPropsSource =
71 options.forwardedProps !== undefined
72 ? toReactive(options.forwardedProps)
73 : undefined
74 const contextSource =
75 options.context !== undefined ? toReactive(options.context) : undefined
76 const liveSource =
77 options.live !== undefined ? toReactive(options.live) : undefined
78
79 const transport = options.connection
80 ? { connection: options.connection }
81 : { fetcher: options.fetcher }
82
83 const client = new ChatClient<TTools, TContext>({
84 devtoolsBridgeFactory: createChatDevtoolsBridge,
85 ...transport,
86 id: clientId,
87 ...(options.initialMessages !== undefined && {
88 initialMessages: options.initialMessages,
89 }),
90 ...(options.persistence !== undefined && {
91 persistence: options.persistence,
92 }),
93 ...(bodySource !== undefined && { body: bodySource() }),
94 ...(options.threadId !== undefined && { threadId: options.threadId }),

Callers 3

HostComponentClass · 0.90
StructuredHostClass · 0.90
AppComponentClass · 0.90

Calls 10

getMessagesMethod · 0.95
updateOptionsMethod · 0.95
subscribeMethod · 0.95
unsubscribeMethod · 0.95
mountDevtoolsMethod · 0.95
stopMethod · 0.95
disposeMethod · 0.95
toReactiveFunction · 0.90
messagesFunction · 0.85
setMethod · 0.65

Tested by

no test coverage detected