Open History to see the last commit
1<div align="center">
2 <picture>
3 <source
4 media="(prefers-color-scheme: dark)"
5 srcset="https://tanstack.com/api/readme/ai.png?framework=angular&theme=dark"
6 />
7 <source
8 media="(prefers-color-scheme: light)"
9 srcset="https://tanstack.com/api/readme/ai.png?framework=angular"
10 />
11 <img
12 src="https://tanstack.com/api/readme/ai.png?framework=angular"
13 alt="TanStack Angular AI"
14 width="900"
15 />
16 </picture>
17</div>
18
19<br />
20
21<div align="center">
22 <a href="https://npmjs.com/package/@tanstack/ai-angular" target="_parent">
23 <img alt="NPM downloads" src="https://img.shields.io/npm/dm/@tanstack/ai-angular.svg" />
24 </a>
25 <a href="https://github.com/TanStack/ai" target="_parent">
26 <img alt="GitHub stars" src="https://img.shields.io/github/stars/TanStack/ai.svg?style=social&label=Star" />
27 </a>
28 <a href="https://github.com/TanStack/ai/releases" target="_parent">
29 <img alt="Release" src="https://img.shields.io/github/v/release/tanstack/ai" />
30 </a>
31 <a href="https://bundlephobia.com/result?p=@tanstack/ai-angular@latest" target="_parent">
32 <img alt="Bundle size" src="https://badgen.net/bundlephobia/minzip/@tanstack/ai-angular@latest" />
33 </a>
34 <a href="https://twitter.com/tan_stack">
35 <img alt="Follow @TanStack" src="https://img.shields.io/twitter/follow/tan_stack.svg?style=social" />
36 </a>
37</div>
38
39# TanStack AI — Angular
40
41Angular signal-based bindings for TanStack AI — streaming chat, tool-calling agents, and media generation built on Angular's native reactivity model.
42
43## <a href="https://tanstack.com/ai">Read the docs -></a>
44
45## Install
46
47```bash
48pnpm add @tanstack/ai-angular @tanstack/ai
49```
50
51```bash
52npm install @tanstack/ai-angular @tanstack/ai
53```
54
55## Minimal Usage
56
57> **Important:** All `inject*` functions use Angular's dependency injection system and **must** be called within an Angular injection context — a component or directive class field initializer, the constructor, or inside `runInInjectionContext`. Calling them outside an injection context will throw a runtime error.
58
59The example below shows a standalone component that streams chat messages from a server endpoint via SSE:
60
61```typescript
62import { Component } from '@angular/core'
63import { CommonModule } from '@angular/common'
64import { injectChat } from '@tanstack/ai-angular'
65import { fetchServerSentEvents } from '@tanstack/ai-client'
66
67@Component({
68 selector: 'app-chat',
69 standalone: true,
70 imports: [CommonModule],
71 template: `
72 <ul>
73 @for (message of chat.messages(); track message.id) {
74 <li>{{ message.role }}: {{ message.content }}</li>
75 }
76 </ul>
77 <input #input placeholder="Type a message..." />
78 <button (click)="chat.sendMessage(input.value); input.value = ''">
79 Send
80 </button>
81 @if (chat.isLoading()) {
82 <p>Thinking...</p>
83 }
84 `,
85})
86export class ChatComponent {
87 // injectChat is called in a field initializer — this is a valid injection context.
88 chat = injectChat({
89 connection: fetchServerSentEvents('/api/chat'),
90 })
91}
92```
93
94All state is exposed as Angular `Signal`s. Read them by calling them as functions:
95
96| Signal | Type | Description |
97| ------------------------- | -------------------- | ------------------------------------------- |
98| `chat.messages()` | `UIMessage[]` | Current message list |
99| `chat.isLoading()` | `boolean` | Whether a response is streaming |
100| `chat.error()` | `Error \| undefined` | Last error, if any |
101| `chat.status()` | `ChatClientState` | `'ready'`, `'streaming'`, `'error'`, ... |
102| `chat.isSubscribed()` | `boolean` | Whether a live (SSE push) session is active |
103| `chat.connectionStatus()` | `ConnectionStatus` | Transport connection status |
104
105Available methods on the return value: `sendMessage`, `append`, `reload`, `stop`, `clear`, `setMessages`, `addToolResult`, `addToolApprovalResponse`.
106
107## Server endpoint
108
109The client pairs with any endpoint that returns a TanStack AI SSE stream:
110
111```typescript
112import { chat, toServerSentEventsResponse } from '@tanstack/ai'
113import { openaiText } from '@tanstack/ai-openai'
114
115export async function POST(request: Request) {
116 const body = await request.json()
117
118 const stream = chat({
119 adapter: openaiText('gpt-5.5'),
120 messages: body.messages,
121 })
122
123 return toServerSentEventsResponse(stream)
124}
125```
126
127## Available Functions
128
129| Function | Description |
130| ---------------------- | ---------------------------------------------------------------- |
131| `injectChat` | Streaming chat with messages, tool calls, and structured outputs |
132| `injectGeneration` | Generic generation client (streaming or one-shot) |
133| `injectGenerateImage` | Image generation |
134| `injectGenerateAudio` | Audio generation |
135| `injectGenerateVideo` | Video generation |
136| `injectGenerateSpeech` | Text-to-speech |
137| `injectSummarize` | Summarization |
138| `injectTranscription` | Audio transcription |
139
140All generation functions return signals (`result`, `isLoading`, `error`, `status`) and methods (`generate`, `stop`, `reset`).
141
142## Injection Context
143
144Angular's DI system requires that `inject()` is called during component construction. Every `inject*` function in this package calls `inject()` internally. Valid call sites:
145
146```typescript
147// Field initializer (recommended)
148export class MyComponent {
149 chat = injectChat({ connection: fetchServerSentEvents('/api/chat') })
150}
151
152// Constructor
153export class MyComponent {
154 chat: ReturnType<typeof injectChat>
155 constructor() {
156 this.chat = injectChat({ connection: fetchServerSentEvents('/api/chat') })
157 }
158}
159
160// Inside runInInjectionContext
161const chat = runInInjectionContext(injector, () =>
162 injectChat({ connection: fetchServerSentEvents('/api/chat') }),
163)
164```
165
166## Get Involved
167
168- Read the [docs](https://tanstack.com/ai).
169- Participate in [GitHub discussions](https://github.com/TanStack/ai/discussions).
170- Chat with the community on [Discord](https://discord.com/invite/WrRKjPJ).
171- See [CONTRIBUTING.md](https://github.com/TanStack/ai/blob/main/CONTRIBUTING.md) for setup instructions.
172- [Become a sponsor](https://github.com/sponsors/tannerlinsley/).