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

README.md

183 lines6.7 KB

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 threadId: 'support-42',
91 })
93```
94​
95All state is exposed as Angular `Signal`s. Read them by calling them as functions:
96​
97| Signal | Type | Description |
98| ------------------------- | -------------------- | ------------------------------------------- |
99| `chat.messages()` | `UIMessage[]` | Current message list |
100| `chat.isLoading()` | `boolean` | Whether a response is streaming |
101| `chat.error()` | `Error \| undefined` | Last error, if any |
102| `chat.status()` | `ChatClientState` | `'ready'`, `'streaming'`, `'error'`, ... |
103| `chat.isSubscribed()` | `boolean` | Whether a live (SSE push) session is active |
104| `chat.connectionStatus()` | `ConnectionStatus` | Transport connection status |
105​
106Available methods on the return value: `sendMessage`, `append`, `reload`, `stop`, `clear`, `setMessages`, `addToolResult`.
107​
108## Server endpoint
109​
110The client pairs with any endpoint that returns a TanStack AI SSE stream:
111​
112```typescript
113import { chat, toServerSentEventsResponse } from '@tanstack/ai'
114import { openaiText } from '@tanstack/ai-openai'
115​
116export async function POST(request: Request) {
117 const body = await request.json()
118​
119 const stream = chat({
120 adapter: openaiText('gpt-5.5'),
121 messages: body.messages,
122 threadId: body.threadId,
123 })
124​
125 return toServerSentEventsResponse(stream)
127```
128​
129## Available Functions
130​
131| Function | Description |
132| ---------------------- | ---------------------------------------------------------------- |
133| `injectChat` | Streaming chat with messages, tool calls, and structured outputs |
134| `injectGeneration` | Generic generation client (streaming or one-shot) |
135| `injectGenerateImage` | Image generation |
136| `injectGenerateAudio` | Audio generation |
137| `injectGenerateVideo` | Video generation |
138| `injectGenerateSpeech` | Text-to-speech |
139| `injectSummarize` | Summarization |
140| `injectTranscription` | Audio transcription |
141​
142All generation functions return signals (`result`, `isLoading`, `error`, `status`) and methods (`generate`, `stop`, `reset`).
143​
144## Injection Context
145​
146Angular's DI system requires that `inject()` is called during component construction. Every `inject*` function in this package calls `inject()` internally. Valid call sites:
147​
148```typescript
149// Field initializer (recommended)
150export class MyComponent {
151 chat = injectChat({
152 connection: fetchServerSentEvents('/api/chat'),
153 threadId: 'support-42',
154 })
156​
157// Constructor
158export class MyComponent {
159 chat: ReturnType<typeof injectChat>
160 constructor() {
161 this.chat = injectChat({
162 connection: fetchServerSentEvents('/api/chat'),
163 threadId: 'support-42',
164 })
165 }
167​
168// Inside runInInjectionContext
169const chat = runInInjectionContext(injector, () =>
170 injectChat({
171 connection: fetchServerSentEvents('/api/chat'),
172 threadId: 'support-42',
173 }),
175```
176​
177## Get Involved
178​
179- Read the [docs](https://tanstack.com/ai).
180- Participate in [GitHub discussions](https://github.com/TanStack/ai/discussions).
181- Chat with the community on [Discord](https://discord.com/invite/WrRKjPJ).
182- See [CONTRIBUTING.md](https://github.com/TanStack/ai/blob/main/CONTRIBUTING.md) for setup instructions.
183- [Become a sponsor](https://github.com/sponsors/tannerlinsley/).