import { Component, ElementRef, effect, viewChild } from '@angular/core' import { FormsModule } from '@angular/forms' import { fetchServerSentEvents, injectChat } from '@tanstack/ai-angular' import { clientTools } from '@tanstack/ai-client' import { chatTools } from './lib/chat-tools' @Component({ selector: 'app-root', standalone: true, imports: [FormsModule], template: `
TanStack AI Angular Chat
gpt-5.5 · streaming
@if (chat.messages().length === 0) {

Powered by signals · 5 client tools

What can I help you ship?

A streaming chat built with injectChat and the OpenAI adapter. The model can call browser-side tools — try one below.

@for (s of suggestions; track s) { }
} @for (message of chat.messages(); track message.id) { @if (isRenderable(message)) { @if (message.role === 'activity') {
{{ activityType(message) }} @if (activitySteps(message).length > 0) {
    @for (step of activitySteps(message); track $index) {
  1. {{ step.content }}
  2. }
} @else if (activityPayload(message)) { {{ activityPayload(message) }} }
} @else {
@if (message.role !== 'user') { AI }
@for (part of message.parts; track $index) { @if (part.type === 'text' && part.content) {
{{ part.content }}
} @else if (part.type === 'tool-call') {
⚙ {{ part.name }} @if (formatArgs(part.arguments)) { {{ formatArgs(part.arguments) }} } @if (hasOutput(part.output)) { → {{ formatOutput(part.output) }} }
} }
} } } @if (chat.isLoading()) {
AI Thinking
}
`, }) export class AppComponent { draft = '' scroller = viewChild>('scroller') suggestions = [ 'What time is it right now?', 'Roll two dice', "What's the weather in Tokyo?", 'What is 42 × 7?', ] chat = injectChat({ connection: fetchServerSentEvents('/api/chat'), threadId: 'angular-chat', tools: clientTools(...chatTools), }) constructor() { // Auto-scroll to the latest message as the conversation streams in. effect(() => { this.chat.messages() this.chat.isLoading() const el = this.scroller()?.nativeElement if (el) { queueMicrotask(() => { el.scrollTop = el.scrollHeight }) } }) } /** A message is worth rendering if it has visible text, a tool call, or activity. */ isRenderable(message: { role: string parts: ReadonlyArray<{ type: string; content?: unknown }> }): boolean { if (message.role === 'activity') return true return message.parts.some( (part) => (part.type === 'text' && !!part.content) || part.type === 'tool-call', ) } activityType(message: { parts: ReadonlyArray<{ type: string; activityType?: string }> }): string { const part = message.parts.find((item) => item.type === 'activity') return part && typeof part.activityType === 'string' ? part.activityType : 'activity' } activitySteps(message: { parts: ReadonlyArray<{ type: string; content?: unknown }> }): Array<{ content: string; status: string }> { const part = message.parts.find((item) => item.type === 'activity') if (!part || typeof part.content !== 'object' || part.content === null) { return [] } if (!('steps' in part.content) || !Array.isArray(part.content.steps)) { return [] } const steps: Array<{ content: string; status: string }> = [] for (const step of part.content.steps) { if (typeof step !== 'object' || step === null || !('content' in step)) { continue } if (typeof step.content !== 'string') continue const status = 'status' in step && typeof step.status === 'string' ? step.status : '' steps.push({ content: step.content, status }) } return steps } activityPayload(message: { parts: ReadonlyArray<{ type: string; content?: unknown }> }): string { const part = message.parts.find((item) => item.type === 'activity') if (!part || typeof part.content !== 'object' || part.content === null) { return '' } try { return JSON.stringify(part.content) } catch { return '' } } /** Compact, readable tool arguments — hides empty `{}`. */ formatArgs(args: string): string { try { const parsed: unknown = JSON.parse(args) const compact = JSON.stringify(parsed) return compact === '{}' ? '' : compact } catch { return '' } } hasOutput(output: unknown): boolean { return output !== undefined && output !== null && output !== '' } /** Render a tool's output as a compact string. */ formatOutput(output: unknown): string { if (typeof output === 'string') return output try { return JSON.stringify(output) } catch { return String(output) } } pick(text: string) { this.draft = text this.send() } send(event?: Event) { event?.preventDefault() const text = this.draft.trim() if (!text) return this.draft = '' void this.chat.sendMessage(text) } }