@langchain/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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 LangChain, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,321 @@
1
+ # @langchain/angular
2
+
3
+ Angular SDK for building AI-powered applications with [LangChain](https://js.langchain.com/) and [LangGraph](https://langchain-ai.github.io/langgraphjs/). Provides a `useStream` function that manages streaming, state, branching, and interrupts using Angular's Signals API.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @langchain/angular @langchain/core
9
+ ```
10
+
11
+ **Peer dependencies:** `@angular/core` (^18.0.0 || ^19.0.0 || ^20.0.0), `@langchain/core` (^1.0.1)
12
+
13
+ ## Quick Start
14
+
15
+ ```typescript
16
+ import { Component } from "@angular/core";
17
+ import { useStream } from "@langchain/angular";
18
+
19
+ @Component({
20
+ standalone: true,
21
+ template: `
22
+ <div>
23
+ @for (msg of stream.messages(); track msg.id ?? $index) {
24
+ <div>{{ str(msg.content) }}</div>
25
+ }
26
+
27
+ <button
28
+ [disabled]="stream.isLoading()"
29
+ (click)="onSubmit()"
30
+ >
31
+ Send
32
+ </button>
33
+ </div>
34
+ `,
35
+ })
36
+ export class ChatComponent {
37
+ stream = useStream({
38
+ assistantId: "agent",
39
+ apiUrl: "http://localhost:2024",
40
+ });
41
+
42
+ str(v: unknown) {
43
+ return typeof v === "string" ? v : JSON.stringify(v);
44
+ }
45
+
46
+ onSubmit() {
47
+ void this.stream.submit({
48
+ messages: [{ type: "human", content: "Hello!" }],
49
+ });
50
+ }
51
+ }
52
+ ```
53
+
54
+ ## `useStream` Options
55
+
56
+ | Option | Type | Description |
57
+ |---|---|---|
58
+ | `assistantId` | `string` | **Required.** The assistant/graph ID to stream from. |
59
+ | `apiUrl` | `string` | Base URL of the LangGraph API. |
60
+ | `client` | `Client` | Pre-configured `Client` instance (alternative to `apiUrl`). |
61
+ | `messagesKey` | `string` | State key containing messages. Defaults to `"messages"`. |
62
+ | `initialValues` | `StateType` | Initial state values before any stream data arrives. |
63
+ | `fetchStateHistory` | `boolean \| { limit: number }` | Fetch thread history on stream completion. Enables branching. |
64
+ | `throttle` | `boolean \| number` | Throttle state updates for performance. |
65
+ | `onFinish` | `(state, error?) => void` | Called when the stream completes. |
66
+ | `onError` | `(error, state?) => void` | Called on stream errors. |
67
+ | `onThreadId` | `(threadId) => void` | Called when a new thread is created. |
68
+ | `onUpdateEvent` | `(event) => void` | Receive update events from the stream. |
69
+ | `onCustomEvent` | `(event) => void` | Receive custom events from the stream. |
70
+ | `onStop` | `() => void` | Called when the stream is stopped by the user. |
71
+
72
+ ## Return Values
73
+
74
+ All reactive properties are Angular `Signal` or `WritableSignal` values.
75
+
76
+ | Property | Type | Description |
77
+ |---|---|---|
78
+ | `values` | `Signal<StateType>` | Current graph state. |
79
+ | `messages` | `Signal<Message[]>` | Messages from the current state. |
80
+ | `isLoading` | `Signal<boolean>` | Whether a stream is currently active. |
81
+ | `error` | `Signal<unknown>` | The most recent error, if any. |
82
+ | `interrupt` | `Signal<Interrupt \| undefined>` | Current interrupt requiring user input. |
83
+ | `branch` | `WritableSignal<string>` | Active branch identifier. |
84
+ | `submit(values, options?)` | `function` | Submit new input to the graph. When called while a stream is active, the run is created on the server with `multitaskStrategy: "enqueue"` and queued automatically. |
85
+ | `stop()` | `function` | Cancel the active stream. |
86
+ | `setBranch(branch)` | `function` | Switch to a different conversation branch. |
87
+ | `getMessagesMetadata(msg, index?)` | `function` | Get branching and checkpoint metadata for a message. |
88
+ | `switchThread(id)` | `(id: string \| null) => void` | Switch to a different thread. Pass `null` to start a new thread on next submit. |
89
+ | `queue.entries` | `Signal<ReadonlyArray<QueueEntry>>` | Pending server-side runs. Each entry has `id` (server run ID), `values`, `options`, and `createdAt`. |
90
+ | `queue.size` | `Signal<number>` | Number of pending runs on the server. |
91
+ | `queue.cancel(id)` | `(id: string) => Promise<boolean>` | Cancel a pending run on the server by its run ID. |
92
+ | `queue.clear()` | `() => Promise<void>` | Cancel all pending runs on the server. |
93
+
94
+ ## Type Safety
95
+
96
+ Provide your state type as a generic parameter:
97
+
98
+ ```typescript
99
+ import type { Message } from "@langchain/langgraph-sdk";
100
+
101
+ interface MyState {
102
+ messages: Message[];
103
+ context?: string;
104
+ }
105
+
106
+ @Component({ /* ... */ })
107
+ export class ChatComponent {
108
+ stream = useStream<MyState>({
109
+ assistantId: "my-graph",
110
+ apiUrl: "http://localhost:2024",
111
+ });
112
+ }
113
+ ```
114
+
115
+ ### Typed Interrupts
116
+
117
+ ```typescript
118
+ import type { Message } from "@langchain/langgraph-sdk";
119
+
120
+ @Component({ /* ... */ })
121
+ export class ChatComponent {
122
+ stream = useStream<
123
+ { messages: Message[] },
124
+ { InterruptType: { question: string } }
125
+ >({
126
+ assistantId: "my-graph",
127
+ apiUrl: "http://localhost:2024",
128
+ });
129
+
130
+ // this.stream.interrupt() is typed as { question: string } | undefined
131
+ }
132
+ ```
133
+
134
+ ## Handling Interrupts
135
+
136
+ ```typescript
137
+ import { Component } from "@angular/core";
138
+ import type { Message } from "@langchain/langgraph-sdk";
139
+ import { useStream } from "@langchain/angular";
140
+
141
+ @Component({
142
+ standalone: true,
143
+ template: `
144
+ <div>
145
+ @for (msg of stream.messages(); track msg.id ?? $index) {
146
+ <div>{{ str(msg.content) }}</div>
147
+ }
148
+
149
+ @if (stream.interrupt()) {
150
+ <div>
151
+ <p>{{ stream.interrupt()!.value.question }}</p>
152
+ <button (click)="onResume()">Approve</button>
153
+ </div>
154
+ }
155
+
156
+ <button (click)="onSubmit()">Send</button>
157
+ </div>
158
+ `,
159
+ })
160
+ export class ChatComponent {
161
+ stream = useStream<
162
+ { messages: Message[] },
163
+ { InterruptType: { question: string } }
164
+ >({
165
+ assistantId: "agent",
166
+ apiUrl: "http://localhost:2024",
167
+ });
168
+
169
+ str(v: unknown) {
170
+ return typeof v === "string" ? v : JSON.stringify(v);
171
+ }
172
+
173
+ onSubmit() {
174
+ void this.stream.submit({
175
+ messages: [{ type: "human", content: "Hello" }],
176
+ });
177
+ }
178
+
179
+ onResume() {
180
+ void this.stream.submit(null, { command: { resume: "Approved" } });
181
+ }
182
+ }
183
+ ```
184
+
185
+ ## Branching
186
+
187
+ Enable conversation branching with `fetchStateHistory: true`:
188
+
189
+ ```typescript
190
+ import { Component } from "@angular/core";
191
+ import { useStream } from "@langchain/angular";
192
+
193
+ @Component({
194
+ standalone: true,
195
+ template: `
196
+ <div>
197
+ @for (msg of stream.messages(); track msg.id ?? $index) {
198
+ <div>
199
+ <p>{{ str(msg.content) }}</p>
200
+
201
+ @if (getBranchNav(msg, $index); as nav) {
202
+ <button (click)="onPrev(nav)">Previous</button>
203
+ <span>{{ nav.current + 1 }} / {{ nav.total }}</span>
204
+ <button (click)="onNext(nav)">Next</button>
205
+ }
206
+ </div>
207
+ }
208
+
209
+ <button (click)="onSubmit()">Send</button>
210
+ </div>
211
+ `,
212
+ })
213
+ export class ChatComponent {
214
+ stream = useStream({
215
+ assistantId: "agent",
216
+ apiUrl: "http://localhost:2024",
217
+ fetchStateHistory: true,
218
+ });
219
+
220
+ str(v: unknown) {
221
+ return typeof v === "string" ? v : JSON.stringify(v);
222
+ }
223
+
224
+ getBranchNav(msg: any, index: number) {
225
+ const metadata = this.stream.getMessagesMetadata(msg, index);
226
+ const options = metadata?.branchOptions;
227
+ const branch = metadata?.branch;
228
+ if (!options || !branch) return null;
229
+ return {
230
+ options,
231
+ current: options.indexOf(branch),
232
+ total: options.length,
233
+ };
234
+ }
235
+
236
+ onPrev(nav: { options: string[]; current: number }) {
237
+ const prev = nav.options[nav.current - 1];
238
+ if (prev) this.stream.setBranch(prev);
239
+ }
240
+
241
+ onNext(nav: { options: string[]; current: number }) {
242
+ const next = nav.options[nav.current + 1];
243
+ if (next) this.stream.setBranch(next);
244
+ }
245
+
246
+ onSubmit() {
247
+ void this.stream.submit({
248
+ messages: [{ type: "human", content: "Hello" }],
249
+ });
250
+ }
251
+ }
252
+ ```
253
+
254
+ ## Server-Side Queuing
255
+
256
+ When `submit()` is called while a stream is already active, the SDK automatically creates the run on the server with `multitaskStrategy: "enqueue"`. The pending runs are tracked in `queue` and processed in order as each finishes:
257
+
258
+ ```typescript
259
+ import { Component } from "@angular/core";
260
+ import { useStream } from "@langchain/angular";
261
+
262
+ @Component({
263
+ standalone: true,
264
+ template: `
265
+ <div>
266
+ @for (msg of stream.messages(); track msg.id ?? $index) {
267
+ <div>{{ str(msg.content) }}</div>
268
+ }
269
+
270
+ @if (stream.queue.size() > 0) {
271
+ <div>
272
+ <p>{{ stream.queue.size() }} message(s) queued</p>
273
+ <button (click)="onClearQueue()">Clear Queue</button>
274
+ </div>
275
+ }
276
+
277
+ <button
278
+ [disabled]="stream.isLoading()"
279
+ (click)="onSubmit()"
280
+ >
281
+ Send
282
+ </button>
283
+ <button (click)="onNewThread()">New Thread</button>
284
+ </div>
285
+ `,
286
+ })
287
+ export class ChatComponent {
288
+ stream = useStream({
289
+ assistantId: "agent",
290
+ apiUrl: "http://localhost:2024",
291
+ });
292
+
293
+ str(v: unknown) {
294
+ return typeof v === "string" ? v : JSON.stringify(v);
295
+ }
296
+
297
+ onSubmit() {
298
+ void this.stream.submit({
299
+ messages: [{ type: "human", content: "Hello!" }],
300
+ });
301
+ }
302
+
303
+ onClearQueue() {
304
+ void this.stream.queue.clear();
305
+ }
306
+
307
+ onNewThread() {
308
+ this.stream.switchThread(null);
309
+ }
310
+ }
311
+ ```
312
+
313
+ Switching threads via `switchThread()` cancels all pending runs and clears the queue.
314
+
315
+ ## Playground
316
+
317
+ For complete end-to-end examples with full agentic UIs, visit the [LangGraph Playground](https://github.com/langchain-ai/langgraphjs).
318
+
319
+ ## License
320
+
321
+ MIT