Open History to see the last commit
1import { Component, ElementRef, effect, viewChild } from '@angular/core'
2import { FormsModule } from '@angular/forms'
3import { fetchServerSentEvents, injectChat } from '@tanstack/ai-angular'
4import { clientTools } from '@tanstack/ai-client'
5import { chatTools } from './lib/chat-tools'
6
7@Component({
8 selector: 'app-root',
9 standalone: true,
10 imports: [FormsModule],
11 template: `
12 <div class="app-shell">
13 <header class="topbar">
14 <div class="brand">
15 <span class="brand__mark">TanStack <em>AI</em></span>
16 <span class="brand__tag">Angular Chat</span>
17 </div>
18 <span class="status-pill">
19 <span class="status-dot"></span>
20 gpt-5.5 · streaming
21 </span>
22 </header>
23
24 <main class="convo" #scroller>
25 <div class="convo__inner">
26 @if (chat.messages().length === 0) {
27 <section class="hero">
28 <p class="hero__eyebrow">Powered by signals · 5 client tools</p>
29 <h2 class="hero__title">What can I help you <em>ship</em>?</h2>
30 <p class="hero__sub">
31 A streaming chat built with injectChat and the OpenAI adapter.
32 The model can call browser-side tools — try one below.
33 </p>
34 <div class="chips">
35 @for (s of suggestions; track s) {
36 <button class="chip" type="button" (click)="pick(s)">
37 {{ s }}
38 </button>
39 }
40 </div>
41 </section>
42 }
43
44 @for (message of chat.messages(); track message.id) {
45 @if (isRenderable(message)) {
46 <div
47 class="msg"
48 [class]="
49 message.role === 'user' ? 'msg--user' : 'msg--assistant'
50 "
51 >
52 @if (message.role !== 'user') {
53 <span class="avatar">AI</span>
54 }
55 <div class="msg__stack">
56 @for (part of message.parts; track $index) {
57 @if (part.type === 'text' && part.content) {
58 <div class="bubble">{{ part.content }}</div>
59 } @else if (part.type === 'tool-call') {
60 <div
61 class="tool"
62 [class.tool--running]="
63 part.state !== 'complete' && part.state !== 'error'
64 "
65 >
66 <span class="tool__glyph">⚙</span>
67 <span class="tool__name">{{ part.name }}</span>
68 @if (formatArgs(part.arguments)) {
69 <code class="tool__chip">{{
70 formatArgs(part.arguments)
71 }}</code>
72 }
73 @if (hasOutput(part.output)) {
74 <span class="tool__arrow">→</span>
75 <code class="tool__chip tool__chip--out">{{
76 formatOutput(part.output)
77 }}</code>
78 }
79 </div>
80 }
81 }
82 </div>
83 </div>
84 }
85 }
86
87 @if (chat.isLoading()) {
88 <div class="thinking">
89 <span class="avatar">AI</span>
90 <span class="thinking__dots"><i></i><i></i><i></i></span>
91 <span class="thinking__label">Thinking</span>
92 </div>
93 }
94 </div>
95 </main>
96
97 <footer class="composer">
98 <form class="composer__inner" (submit)="send($event)">
99 <div class="composer__bar">
100 <input
101 class="composer__input"
102 [(ngModel)]="draft"
103 name="draft"
104 [disabled]="chat.isLoading()"
105 placeholder="Ask something, or try a tool…"
106 autocomplete="off"
107 />
108 @if (chat.isLoading()) {
109 <button
110 type="button"
111 class="stop-btn"
112 aria-label="Stop"
113 (click)="chat.stop()"
114 >
115 <svg
116 width="14"
117 height="14"
118 viewBox="0 0 14 14"
119 aria-hidden="true"
120 >
121 <rect width="14" height="14" rx="3" fill="currentColor" />
122 </svg>
123 </button>
124 } @else {
125 <button
126 type="submit"
127 class="send-btn"
128 aria-label="Send"
129 [disabled]="draft.trim().length === 0"
130 >
131 <svg
132 width="18"
133 height="18"
134 viewBox="0 0 24 24"
135 aria-hidden="true"
136 >
137 <path
138 d="M12 19V5M12 5l-6 6M12 5l6 6"
139 fill="none"
140 stroke="currentColor"
141 stroke-width="2.2"
142 stroke-linecap="round"
143 stroke-linejoin="round"
144 />
145 </svg>
146 </button>
147 }
148 </div>
149 <p class="composer__hint">
150 TanStack AI · Angular · responses are generated and may be
151 inaccurate
152 </p>
153 </form>
154 </footer>
155 </div>
156 `,
157})
158export class AppComponent {
159 draft = ''
160 scroller = viewChild<ElementRef<HTMLElement>>('scroller')
161
162 suggestions = [
163 'What time is it right now?',
164 'Roll two dice',
165 "What's the weather in Tokyo?",
166 'What is 42 × 7?',
167 ]
168
169 chat = injectChat({
170 connection: fetchServerSentEvents('/api/chat'),
171 tools: clientTools(...chatTools),
172 })
173
174 constructor() {
175 // Auto-scroll to the latest message as the conversation streams in.
176 effect(() => {
177 this.chat.messages()
178 this.chat.isLoading()
179 const el = this.scroller()?.nativeElement
180 if (el) {
181 queueMicrotask(() => {
182 el.scrollTop = el.scrollHeight
183 })
184 }
185 })
186 }
187
188 /** A message is worth rendering if it has visible text or a tool call. */
189 isRenderable(message: {
190 parts: ReadonlyArray<{ type: string; content?: unknown }>
191 }): boolean {
192 return message.parts.some(
193 (part) =>
194 (part.type === 'text' && !!part.content) || part.type === 'tool-call',
195 )
196 }
197
198 /** Compact, readable tool arguments — hides empty `{}`. */
199 formatArgs(args: string): string {
200 try {
201 const parsed: unknown = JSON.parse(args)
202 const compact = JSON.stringify(parsed)
203 return compact === '{}' ? '' : compact
204 } catch {
205 return ''
206 }
207 }
208
209 hasOutput(output: unknown): boolean {
210 return output !== undefined && output !== null && output !== ''
211 }
212
213 /** Render a tool's output as a compact string. */
214 formatOutput(output: unknown): string {
215 if (typeof output === 'string') return output
216 try {
217 return JSON.stringify(output)
218 } catch {
219 return String(output)
220 }
221 }
222
223 pick(text: string) {
224 this.draft = text
225 this.send()
226 }
227
228 send(event?: Event) {
229 event?.preventDefault()
230 const text = this.draft.trim()
231 if (!text) return
232 this.draft = ''
233 void this.chat.sendMessage(text)
234 }
235}