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

angular.md

107 lines3.3 KB

1---
2title: Angular Chat UI
3id: typed-headless-ui-angular
4order: 8
5description: "Build a typed, headless Angular chat UI with createChatHook. Widgets are standalone components. Chat state is signals."
6keywords:
7 - tanstack ai
8 - createChatHook
9 - angular
10 - injectAppChat
11 - headless ui
12 - ToolProps
13---
14​
15Install `@tanstack/ai-angular`. Import the UI factory from `@tanstack/ai-angular/ui`. Call `createChatHook({ options, ...components })` once at module scope.
16​
17This is not a React port. Widgets are standalone Angular components. Chat state from `injectAppChat()` is signals. Layout slots are component classes. Your layout template uses `NgComponentOutlet` to render `Messages`, `Interrupts`, `Queue`, and `Input`.
18​
19The factory returns `injectAppChat`, `injectChatContext`, and `Chat`. Call `injectAppChat()` in a component injection context. Bind the instance with `<ai-chat [chat]="chat" />`.
20​
21The factory needs a `toolsComponents` entry for every tool name in `chatOptions`. It also needs an `interruptsComponents.generic` entry for every interrupt id. `generic.fallback` is optional.
22​
23The server route matches the [React page](./react). The [chat UI recipes](./recipes/index) show the same option groups. The code there is React. On Angular, each widget is a component class with `input()`.
24​
25## Client
26​
27```ts
28import { Component, input } from '@angular/core'
29import { NgComponentOutlet } from '@angular/common'
30import type { Type } from '@angular/core'
31import { fetchServerSentEvents } from '@tanstack/ai-client'
32import { createChatHook } from '@tanstack/ai-angular/ui'
33import { toolDefinition } from '@tanstack/ai'
34import { z } from 'zod'
35​
36const getWeather = toolDefinition({
37 name: 'getWeather',
38 description: 'Look up weather',
39 inputSchema: z.object({ city: z.string() }),
40 outputSchema: z.object({ temperature: z.number() }),
41}).client()
42​
43const chatOptions = {
44 connection: fetchServerSentEvents('/api/chat'),
45 tools: [getWeather],
47​
48@Component({
49 selector: 'app-chat-layout',
50 imports: [NgComponentOutlet],
51 template: `
52 <ng-container [ngComponentOutlet]="Messages()" />
53 @if (Input(); as inputCmp) {
54 <ng-container [ngComponentOutlet]="inputCmp" />
55 }
56 `,
57})
58export class ChatLayout {
59 Messages = input.required<Type<unknown>>()
60 Interrupts = input.required<Type<unknown>>()
61 Queue = input.required<Type<unknown>>()
62 Input = input<Type<unknown>>()
64​
65@Component({
66 selector: 'app-chat-message',
67 imports: [NgComponentOutlet],
68 template: `
69 <article>
70 <ng-container
71 [ngComponentOutlet]="Parts()"
72 [ngComponentOutletInputs]="{ message: message() }"
73 />
74 </article>
75 `,
76})
77export class ChatMessage {
78 message = input.required<unknown>()
79 Parts = input.required<Type<unknown>>()
81​
82@Component({
83 selector: 'app-weather-tool',
84 template: `<strong>{{ part().input?.city }}</strong>`,
85})
86export class WeatherTool {
87 part = input.required<{ input?: { city?: string } }>()
88 result = input<unknown>()
89 interrupt = input<unknown>()
91​
92const { injectAppChat, Chat } = createChatHook({
93 options: chatOptions,
94 components: { layout: ChatLayout, message: ChatMessage },
95 partsComponents: { fallback: ChatMessage },
96 toolsComponents: { getWeather: WeatherTool },
97})
98​
99@Component({
100 selector: 'app-chat-screen',
101 imports: [Chat],
102 template: `<ai-chat [chat]="chat" />`,
103})
104export class ChatScreen {
105 chat = injectAppChat()
107```