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 @if (message.role === 'activity') {
47 <div class="activity">
48 <span class="activity__type">{{
49 activityType(message)
50 }}</span>
51 @if (activitySteps(message).length > 0) {
52 <ol class="activity__steps">
53 @for (step of activitySteps(message); track $index) {
54 <li
55 class="activity__step"
56 [class.activity__step--done]="
57 step.status === 'completed'
58 "
59 [class.activity__step--run]="
60 step.status === 'in_progress'
61 "
62 >
63 {{ step.content }}
64 </li>
65 }
66 </ol>
67 } @else if (activityPayload(message)) {
68 <code class="activity__payload">{{
69 activityPayload(message)
70 }}</code>
71 }
72 </div>
73 } @else {
74 <div
75 class="msg"
76 [class]="
77 message.role === 'user' ? 'msg--user' : 'msg--assistant'
78 "
79 >
80 @if (message.role !== 'user') {
81 <span class="avatar">AI</span>
82 }
83 <div class="msg__stack">
84 @for (part of message.parts; track $index) {
85 @if (part.type === 'text' && part.content) {
86 <div class="bubble">{{ part.content }}</div>
87 } @else if (part.type === 'tool-call') {
88 <div
89 class="tool"
90 [class.tool--running]="
91 part.state !== 'complete' && part.state !== 'error'
92 "
93 >
94 <span class="tool__glyph">⚙</span>
95 <span class="tool__name">{{ part.name }}</span>
96 @if (formatArgs(part.arguments)) {
97 <code class="tool__chip">{{
98 formatArgs(part.arguments)
99 }}</code>
100 }
101 @if (hasOutput(part.output)) {
102 <span class="tool__arrow">→</span>
103 <code class="tool__chip tool__chip--out">{{
104 formatOutput(part.output)
105 }}</code>
106 }
107 </div>
108 }
109 }
110 </div>
111 </div>
112 }
113 }
114 }
115
116 @if (chat.isLoading()) {
117 <div class="thinking">
118 <span class="avatar">AI</span>
119 <span class="thinking__dots"><i></i><i></i><i></i></span>
120 <span class="thinking__label">Thinking</span>
121 </div>
122 }
123 </div>
124 </main>
125
126 <footer class="composer">
127 <form class="composer__inner" (submit)="send($event)">
128 <div class="composer__bar">
129 <input
130 class="composer__input"
131 [(ngModel)]="draft"
132 name="draft"
133 [disabled]="chat.isLoading()"
134 placeholder="Ask something, or try a tool…"
135 autocomplete="off"
136 />
137 @if (chat.isLoading()) {
138 <button
139 type="button"
140 class="stop-btn"
141 aria-label="Stop"
142 (click)="chat.stop()"
143 >
144 <svg
145 width="14"
146 height="14"
147 viewBox="0 0 14 14"
148 aria-hidden="true"
149 >
150 <rect width="14" height="14" rx="3" fill="currentColor" />
151 </svg>
152 </button>
153 } @else {
154 <button
155 type="submit"
156 class="send-btn"
157 aria-label="Send"
158 [disabled]="draft.trim().length === 0"
159 >
160 <svg
161 width="18"
162 height="18"
163 viewBox="0 0 24 24"
164 aria-hidden="true"
165 >
166 <path
167 d="M12 19V5M12 5l-6 6M12 5l6 6"
168 fill="none"
169 stroke="currentColor"
170 stroke-width="2.2"
171 stroke-linecap="round"
172 stroke-linejoin="round"
173 />
174 </svg>
175 </button>
176 }
177 </div>
178 <p class="composer__hint">
179 TanStack AI · Angular · responses are generated and may be
180 inaccurate
181 </p>
182 </form>
183 </footer>
184 </div>
185 `,
186})
187export class AppComponent {
188 draft = ''
189 scroller = viewChild<ElementRef<HTMLElement>>('scroller')
190
191 suggestions = [
192 'What time is it right now?',
193 'Roll two dice',
194 "What's the weather in Tokyo?",
195 'What is 42 × 7?',
196 ]
197
198 chat = injectChat({
199 connection: fetchServerSentEvents('/api/chat'),
200 threadId: 'angular-chat',
201 tools: clientTools(...chatTools),
202 })
203
204 constructor() {
205 // Auto-scroll to the latest message as the conversation streams in.
206 effect(() => {
207 this.chat.messages()
208 this.chat.isLoading()
209 const el = this.scroller()?.nativeElement
210 if (el) {
211 queueMicrotask(() => {
212 el.scrollTop = el.scrollHeight
213 })
214 }
215 })
216 }
217
218 /** A message is worth rendering if it has visible text, a tool call, or activity. */
219 isRenderable(message: {
220 role: string
221 parts: ReadonlyArray<{ type: string; content?: unknown }>
222 }): boolean {
223 if (message.role === 'activity') return true
224 return message.parts.some(
225 (part) =>
226 (part.type === 'text' && !!part.content) || part.type === 'tool-call',
227 )
228 }
229
230 activityType(message: {
231 parts: ReadonlyArray<{ type: string; activityType?: string }>
232 }): string {
233 const part = message.parts.find((item) => item.type === 'activity')
234 return part && typeof part.activityType === 'string'
235 ? part.activityType
236 : 'activity'
237 }
238
239 activitySteps(message: {
240 parts: ReadonlyArray<{ type: string; content?: unknown }>
241 }): Array<{ content: string; status: string }> {
242 const part = message.parts.find((item) => item.type === 'activity')
243 if (!part || typeof part.content !== 'object' || part.content === null) {
244 return []
245 }
246 if (!('steps' in part.content) || !Array.isArray(part.content.steps)) {
247 return []
248 }
249 const steps: Array<{ content: string; status: string }> = []
250 for (const step of part.content.steps) {
251 if (typeof step !== 'object' || step === null || !('content' in step)) {
252 continue
253 }
254 if (typeof step.content !== 'string') continue
255 const status =
256 'status' in step && typeof step.status === 'string' ? step.status : ''
257 steps.push({ content: step.content, status })
258 }
259 return steps
260 }
261
262 activityPayload(message: {
263 parts: ReadonlyArray<{ type: string; content?: unknown }>
264 }): string {
265 const part = message.parts.find((item) => item.type === 'activity')
266 if (!part || typeof part.content !== 'object' || part.content === null) {
267 return ''
268 }
269 try {
270 return JSON.stringify(part.content)
271 } catch {
272 return ''
273 }
274 }
275
276 /** Compact, readable tool arguments — hides empty `{}`. */
277 formatArgs(args: string): string {
278 try {
279 const parsed: unknown = JSON.parse(args)
280 const compact = JSON.stringify(parsed)
281 return compact === '{}' ? '' : compact
282 } catch {
283 return ''
284 }
285 }
286
287 hasOutput(output: unknown): boolean {
288 return output !== undefined && output !== null && output !== ''
289 }
290
291 /** Render a tool's output as a compact string. */
292 formatOutput(output: unknown): string {
293 if (typeof output === 'string') return output
294 try {
295 return JSON.stringify(output)
296 } catch {
297 return String(output)
298 }
299 }
300
301 pick(text: string) {
302 this.draft = text
303 this.send()
304 }
305
306 send(event?: Event) {
307 event?.preventDefault()
308 const text = this.draft.trim()
309 if (!text) return
310 this.draft = ''
311 void this.chat.sendMessage(text)
312 }
313}