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