@langgraph-js/pure-graph 1.3.0 → 1.4.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 +180 -16
- package/dist/adapter/nextjs/router.js +6 -6
- package/dist/global.d.ts +3 -2
- package/dist/global.js +3 -0
- package/dist/storage/index.d.ts +4 -3
- package/dist/storage/index.js +21 -3
- package/dist/storage/memory/threads.d.ts +1 -0
- package/dist/storage/memory/threads.js +3 -0
- package/dist/storage/pg/checkpoint.d.ts +2 -0
- package/dist/storage/pg/checkpoint.js +9 -0
- package/dist/storage/pg/threads.d.ts +43 -0
- package/dist/storage/pg/threads.js +303 -0
- package/dist/storage/sqlite/DB.js +1 -1
- package/dist/storage/sqlite/threads.d.ts +1 -1
- package/dist/storage/sqlite/threads.js +1 -2
- package/dist/threads/index.d.ts +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +38 -5
- package/.prettierrc +0 -11
- package/bun.lock +0 -209
- package/dist/adapter/hono/zod.d.ts +0 -203
- package/dist/adapter/hono/zod.js +0 -43
- package/dist/adapter/nextjs/zod.d.ts +0 -203
- package/dist/adapter/nextjs/zod.js +0 -60
- package/examples/nextjs/README.md +0 -36
- package/examples/nextjs/app/api/langgraph/[...path]/route.ts +0 -10
- package/examples/nextjs/app/favicon.ico +0 -0
- package/examples/nextjs/app/globals.css +0 -26
- package/examples/nextjs/app/layout.tsx +0 -34
- package/examples/nextjs/app/page.tsx +0 -211
- package/examples/nextjs/next.config.ts +0 -26
- package/examples/nextjs/package.json +0 -24
- package/examples/nextjs/postcss.config.mjs +0 -5
- package/examples/nextjs/tsconfig.json +0 -27
- package/packages/agent-graph/demo.json +0 -35
- package/packages/agent-graph/package.json +0 -18
- package/packages/agent-graph/src/index.ts +0 -47
- package/packages/agent-graph/src/tools/tavily.ts +0 -9
- package/packages/agent-graph/src/tools.ts +0 -38
- package/packages/agent-graph/src/types.ts +0 -42
- package/pnpm-workspace.yaml +0 -4
- package/src/adapter/hono/assistants.ts +0 -24
- package/src/adapter/hono/endpoint.ts +0 -3
- package/src/adapter/hono/index.ts +0 -14
- package/src/adapter/hono/runs.ts +0 -92
- package/src/adapter/hono/threads.ts +0 -37
- package/src/adapter/nextjs/endpoint.ts +0 -2
- package/src/adapter/nextjs/index.ts +0 -2
- package/src/adapter/nextjs/router.ts +0 -206
- package/src/adapter/nextjs/zod.ts +0 -66
- package/src/adapter/zod.ts +0 -144
- package/src/createEndpoint.ts +0 -116
- package/src/e.d.ts +0 -3
- package/src/global.ts +0 -11
- package/src/graph/stream.ts +0 -263
- package/src/graph/stringify.ts +0 -219
- package/src/index.ts +0 -6
- package/src/queue/JsonPlusSerializer.ts +0 -143
- package/src/queue/event_message.ts +0 -30
- package/src/queue/stream_queue.ts +0 -237
- package/src/storage/index.ts +0 -52
- package/src/storage/memory/checkpoint.ts +0 -2
- package/src/storage/memory/queue.ts +0 -91
- package/src/storage/memory/threads.ts +0 -183
- package/src/storage/redis/queue.ts +0 -148
- package/src/storage/sqlite/DB.ts +0 -16
- package/src/storage/sqlite/checkpoint.ts +0 -502
- package/src/storage/sqlite/threads.ts +0 -405
- package/src/storage/sqlite/type.ts +0 -12
- package/src/threads/index.ts +0 -37
- package/src/types.ts +0 -118
- package/src/utils/createEntrypointGraph.ts +0 -20
- package/src/utils/getGraph.ts +0 -44
- package/src/utils/getLangGraphCommand.ts +0 -21
- package/test/graph/entrypoint.ts +0 -21
- package/test/graph/index.ts +0 -60
- package/test/hono.ts +0 -15
- package/tsconfig.json +0 -20
package/README.md
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
# Pure Graph
|
|
2
2
|
|
|
3
|
-
Pure Graph is a library that provides a standard LangGraph endpoint for integrating into various frameworks like NextJS and Hono.js. This document will guide you on how to use Pure Graph in your projects.
|
|
3
|
+
Pure Graph is a library that provides a standard LangGraph endpoint for integrating into various frameworks like NextJS and Hono.js. It supports multiple storage backends (SQLite, PostgreSQL, Redis) and message queues. This document will guide you on how to use Pure Graph in your projects.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Multiple Storage Backends**: Support for SQLite, PostgreSQL, Redis, and in-memory storage
|
|
8
|
+
- **Message Queue**: Redis-based stream queue with TTL support
|
|
9
|
+
- **Thread Management**: Comprehensive thread lifecycle management with status tracking
|
|
10
|
+
- **Framework Integration**: Native support for Next.js and Hono.js frameworks
|
|
4
11
|
|
|
5
12
|
## Installation
|
|
6
13
|
|
|
@@ -20,6 +27,25 @@ yarn add @langgraph-js/pure-graph
|
|
|
20
27
|
|
|
21
28
|
### Next.js Example
|
|
22
29
|
|
|
30
|
+
```text
|
|
31
|
+
my-nextjs-app/
|
|
32
|
+
├── app/
|
|
33
|
+
│ ├── api/
|
|
34
|
+
│ │ └── langgraph/
|
|
35
|
+
│ │ └── [...path]/
|
|
36
|
+
│ │ └── route.ts # LangGraph API endpoint
|
|
37
|
+
│ ├── layout.tsx # Root layout
|
|
38
|
+
│ └── page.tsx # Your page
|
|
39
|
+
├── agent/
|
|
40
|
+
│ └── graph-name/
|
|
41
|
+
│ ├── graph.ts # Main graph implementation
|
|
42
|
+
│ ├── state.ts # Graph state definitions
|
|
43
|
+
│ └── prompt.ts # Prompt templates
|
|
44
|
+
├── .env.local # Environment variables
|
|
45
|
+
├── package.json # Dependencies and scripts
|
|
46
|
+
└── tsconfig.json # TypeScript configuration
|
|
47
|
+
```
|
|
48
|
+
|
|
23
49
|
To integrate Pure Graph into a Next.js project, follow these steps:
|
|
24
50
|
|
|
25
51
|
1. **Create a Route Handler**
|
|
@@ -30,7 +56,7 @@ To integrate Pure Graph into a Next.js project, follow these steps:
|
|
|
30
56
|
// app/api/langgraph/[...path]/route.ts
|
|
31
57
|
import { GET, POST, DELETE } from '@langgraph-js/pure-graph/dist/adapter/nextjs/router.js';
|
|
32
58
|
import { registerGraph } from '@langgraph-js/pure-graph';
|
|
33
|
-
import { graph } from '
|
|
59
|
+
import { graph } from '../../../agent/graph-name/graph';
|
|
34
60
|
|
|
35
61
|
registerGraph('test', graph);
|
|
36
62
|
|
|
@@ -58,7 +84,7 @@ To integrate Pure Graph into a Hono.js project, follow these steps:
|
|
|
58
84
|
```js
|
|
59
85
|
// app.js
|
|
60
86
|
import { registerGraph } from '@langgraph-js/pure-graph';
|
|
61
|
-
import { graph } from './
|
|
87
|
+
import { graph } from './agent/graph-name/graph';
|
|
62
88
|
import { Hono } from 'hono';
|
|
63
89
|
import LangGraphApp from '@langgraph-js/pure-graph/dist/adapter/hono/index';
|
|
64
90
|
|
|
@@ -84,27 +110,165 @@ To integrate Pure Graph into a Hono.js project, follow these steps:
|
|
|
84
110
|
|
|
85
111
|
Here are the environment variables you need to configure:
|
|
86
112
|
|
|
87
|
-
-
|
|
88
|
-
-
|
|
89
|
-
-
|
|
113
|
+
- `SQLITE_DATABASE_URI`: Path to your SQLite database (e.g., `./.langgraph_api/chat.db`).
|
|
114
|
+
- `DATABASE_URL`: PostgreSQL connection string (required for PostgreSQL checkpoint storage).
|
|
115
|
+
- `DATABASE_INIT`: Set to `true` for initial PostgreSQL database setup (required only on first run with PostgreSQL).
|
|
116
|
+
- `CHECKPOINT_TYPE`: Type of checkpoint storage (optional, defaults to memory; options: `postgres`, `redis`, `shallow/redis`).
|
|
117
|
+
- `REDIS_URL`: URL for Redis (required if using Redis checkpoint or message queue).
|
|
118
|
+
|
|
119
|
+
## Persistence Configuration
|
|
120
|
+
|
|
121
|
+
Pure Graph supports multiple storage backends for persisting graph state, checkpoints, and thread data. Choose the appropriate storage type based on your requirements for scalability, persistence, and performance.
|
|
122
|
+
|
|
123
|
+
### Memory Storage (Default)
|
|
124
|
+
|
|
125
|
+
**Best for**: Development, testing, or stateless applications.
|
|
126
|
+
|
|
127
|
+
**Configuration**:
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
# No additional configuration required - this is the default
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
**Characteristics**:
|
|
134
|
+
|
|
135
|
+
- Fastest performance
|
|
136
|
+
- No persistence across restarts
|
|
137
|
+
- Data is lost when the application stops
|
|
138
|
+
- Suitable for development and testing
|
|
139
|
+
|
|
140
|
+
### SQLite Storage
|
|
141
|
+
|
|
142
|
+
**Best for**: Single-server applications, development, or small-scale production.
|
|
143
|
+
|
|
144
|
+
**Configuration**:
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
SQLITE_DATABASE_URI=./.langgraph_api/chat.db
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
**Setup**:
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
# Create the database directory
|
|
154
|
+
mkdir -p .langgraph_api
|
|
155
|
+
|
|
156
|
+
# The database file will be created automatically on first run
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
**Characteristics**:
|
|
160
|
+
|
|
161
|
+
- File-based database
|
|
162
|
+
- Good performance for moderate workloads
|
|
163
|
+
- ACID compliant
|
|
164
|
+
- Single-writer limitation
|
|
165
|
+
|
|
166
|
+
### PostgreSQL Storage
|
|
167
|
+
|
|
168
|
+
**Best for**: Production applications requiring high reliability and scalability.
|
|
169
|
+
|
|
170
|
+
**Configuration**:
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
DATABASE_URL=postgresql://username:password@localhost:5432/langgraph_db
|
|
174
|
+
DATABASE_INIT=true # Only needed for initial setup
|
|
175
|
+
CHECKPOINT_TYPE=postgres
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
**Setup**:
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
# First run with DATABASE_INIT=true to create tables
|
|
182
|
+
export DATABASE_INIT=true
|
|
183
|
+
# Run your application once to initialize the database
|
|
184
|
+
|
|
185
|
+
# Remove DATABASE_INIT for subsequent runs
|
|
186
|
+
unset DATABASE_INIT
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
**Characteristics**:
|
|
190
|
+
|
|
191
|
+
- Full ACID compliance
|
|
192
|
+
- Concurrent access support
|
|
193
|
+
- Scalable for high-throughput applications
|
|
194
|
+
- Requires PostgreSQL server setup
|
|
195
|
+
|
|
196
|
+
### Redis Storage
|
|
197
|
+
|
|
198
|
+
Pure Graph supports two Redis checkpoint modes:
|
|
199
|
+
|
|
200
|
+
#### Full Redis Checkpoint
|
|
201
|
+
|
|
202
|
+
**Best for**: High-performance caching with full persistence.
|
|
203
|
+
|
|
204
|
+
**Configuration**:
|
|
205
|
+
|
|
206
|
+
```bash
|
|
207
|
+
REDIS_URL=redis://localhost:6379
|
|
208
|
+
CHECKPOINT_TYPE=redis
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
#### Shallow Redis Checkpoint
|
|
212
|
+
|
|
213
|
+
**Best for**: Memory-efficient Redis usage with lighter persistence.
|
|
214
|
+
|
|
215
|
+
**Configuration**:
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
REDIS_URL=redis://localhost:6379
|
|
219
|
+
CHECKPOINT_TYPE=shallow/redis
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
**Characteristics**:
|
|
223
|
+
|
|
224
|
+
- High performance
|
|
225
|
+
- TTL-based automatic cleanup
|
|
226
|
+
- Distributed caching capabilities
|
|
227
|
+
- Requires Redis server setup
|
|
228
|
+
|
|
229
|
+
### Redis Message Queue
|
|
230
|
+
|
|
231
|
+
When using Redis, message queues are automatically enabled for better performance:
|
|
232
|
+
|
|
233
|
+
**Configuration**:
|
|
234
|
+
|
|
235
|
+
```bash
|
|
236
|
+
REDIS_URL=redis://localhost:6379
|
|
237
|
+
# Message queues will use Redis automatically when REDIS_URL is set
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
**Characteristics**:
|
|
241
|
+
|
|
242
|
+
- Automatic TTL management (300 seconds)
|
|
243
|
+
- Improved streaming performance
|
|
244
|
+
- Better resource utilization
|
|
245
|
+
|
|
246
|
+
### Configuration Priority
|
|
247
|
+
|
|
248
|
+
Storage backends are selected in this priority order:
|
|
249
|
+
|
|
250
|
+
1. **Redis** (if `REDIS_URL` set and `CHECKPOINT_TYPE` matches)
|
|
251
|
+
2. **PostgreSQL** (if `DATABASE_URL` set)
|
|
252
|
+
3. **SQLite** (if `SQLITE_DATABASE_URI` set)
|
|
253
|
+
4. **Memory** (fallback default)
|
|
90
254
|
|
|
91
255
|
## API Endpoints
|
|
92
256
|
|
|
93
257
|
### Assistants
|
|
94
258
|
|
|
95
|
-
-
|
|
96
|
-
-
|
|
259
|
+
- **GET /assistants**: Search for assistants.
|
|
260
|
+
- **GET /assistants/{assistantId}**: Retrieve a specific assistant graph.
|
|
97
261
|
|
|
98
262
|
### Threads
|
|
99
263
|
|
|
100
|
-
-
|
|
101
|
-
-
|
|
102
|
-
-
|
|
103
|
-
-
|
|
264
|
+
- **POST /threads**: Create a new thread.
|
|
265
|
+
- **GET /threads**: Search for threads.
|
|
266
|
+
- **GET /threads/{threadId}**: Retrieve a specific thread.
|
|
267
|
+
- **DELETE /threads/{threadId}**: Delete a specific thread.
|
|
104
268
|
|
|
105
269
|
### Runs
|
|
106
270
|
|
|
107
|
-
-
|
|
108
|
-
-
|
|
109
|
-
-
|
|
110
|
-
-
|
|
271
|
+
- **GET /threads/{threadId}/runs**: List runs in a thread.
|
|
272
|
+
- **POST /threads/{threadId}/runs**: Create a new run.
|
|
273
|
+
- **DELETE /threads/{threadId}/runs/{runId}**: Cancel a specific run.
|
|
274
|
+
- **GET /threads/{threadId}/runs/{runId}/stream**: Stream run data.
|
|
@@ -39,7 +39,7 @@ export async function GET(req) {
|
|
|
39
39
|
const match = pathname.match(/\/assistants\/([^/]+)\/graph$/);
|
|
40
40
|
if (match) {
|
|
41
41
|
const assistant_id = match[1];
|
|
42
|
-
const xrayParam = url.searchParams.get('xray');
|
|
42
|
+
const xrayParam = url.searchParams.get('xray') ?? undefined;
|
|
43
43
|
const queryParams = { xray: xrayParam };
|
|
44
44
|
const { xray } = AssistantGraphQuerySchema.parse(queryParams);
|
|
45
45
|
const data = await client.assistants.getGraph(assistant_id, {
|
|
@@ -62,9 +62,9 @@ export async function GET(req) {
|
|
|
62
62
|
const match = pathname.match(/\/threads\/([0-9a-fA-F-]{36})\/runs$/);
|
|
63
63
|
if (match) {
|
|
64
64
|
const thread_id = match[1];
|
|
65
|
-
const limit = url.searchParams.get('limit');
|
|
66
|
-
const offset = url.searchParams.get('offset');
|
|
67
|
-
const status = url.searchParams.get('status');
|
|
65
|
+
const limit = url.searchParams.get('limit') ?? undefined;
|
|
66
|
+
const offset = url.searchParams.get('offset') ?? undefined;
|
|
67
|
+
const status = url.searchParams.get('status') ?? undefined;
|
|
68
68
|
const queryParams = { limit, offset, status };
|
|
69
69
|
const { limit: parsedLimit, offset: parsedOffset, status: parsedStatus, } = RunListQuerySchema.parse(queryParams);
|
|
70
70
|
const runs = await client.runs.list(thread_id, {
|
|
@@ -148,8 +148,8 @@ export async function POST(req) {
|
|
|
148
148
|
if (match) {
|
|
149
149
|
const thread_id = match[1];
|
|
150
150
|
const run_id = match[2];
|
|
151
|
-
const waitParam = url.searchParams.get('wait');
|
|
152
|
-
const actionParam = url.searchParams.get('action');
|
|
151
|
+
const waitParam = url.searchParams.get('wait') ?? undefined;
|
|
152
|
+
const actionParam = url.searchParams.get('action') ?? undefined;
|
|
153
153
|
const queryParams = {
|
|
154
154
|
wait: waitParam ? waitParam === 'true' : false,
|
|
155
155
|
action: actionParam ?? 'interrupt',
|
package/dist/global.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { SqliteSaver } from './storage/sqlite/checkpoint.js';
|
|
2
|
+
import type { PostgresSaver } from '@langchain/langgraph-checkpoint-postgres';
|
|
2
3
|
export declare class LangGraphGlobal {
|
|
3
4
|
static globalMessageQueue: import("./queue/stream_queue.js").StreamQueueManager<import("./queue/stream_queue.js").BaseStreamQueueInterface>;
|
|
4
|
-
static globalCheckPointer: import("@langchain/langgraph-checkpoint-redis").RedisSaver | import("@langchain/langgraph-checkpoint-redis/shallow").ShallowRedisSaver |
|
|
5
|
-
static globalThreadsManager: import("./storage/sqlite/threads.js").SQLiteThreadsManager<unknown> | import("./storage/memory/threads.js").MemoryThreadsManager<unknown>;
|
|
5
|
+
static globalCheckPointer: SqliteSaver | PostgresSaver | import("@langchain/langgraph-checkpoint-redis").RedisSaver | import("@langchain/langgraph-checkpoint-redis/shallow").ShallowRedisSaver | import("@langchain/langgraph-checkpoint").MemorySaver;
|
|
6
|
+
static globalThreadsManager: import("./storage/pg/threads.js").PostgresThreadsManager<unknown> | import("./storage/sqlite/threads.js").SQLiteThreadsManager<unknown> | import("./storage/memory/threads.js").MemoryThreadsManager<unknown>;
|
|
6
7
|
}
|
package/dist/global.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { createCheckPointer, createMessageQueue, createThreadManager } from './storage/index.js';
|
|
2
2
|
const [globalMessageQueue, globalCheckPointer] = await Promise.all([createMessageQueue(), createCheckPointer()]);
|
|
3
|
+
console.debug('LG | checkpointer created');
|
|
3
4
|
const globalThreadsManager = await createThreadManager({
|
|
4
5
|
checkpointer: globalCheckPointer,
|
|
5
6
|
});
|
|
7
|
+
console.debug('LG | threads manager created');
|
|
8
|
+
console.debug('LG | global init done');
|
|
6
9
|
export class LangGraphGlobal {
|
|
7
10
|
static globalMessageQueue = globalMessageQueue;
|
|
8
11
|
static globalCheckPointer = globalCheckPointer;
|
package/dist/storage/index.d.ts
CHANGED
|
@@ -2,9 +2,10 @@ import { BaseStreamQueueInterface, StreamQueueManager } from '../queue/stream_qu
|
|
|
2
2
|
import { MemorySaver } from './memory/checkpoint';
|
|
3
3
|
import { MemoryThreadsManager } from './memory/threads';
|
|
4
4
|
import type { SqliteSaver as SqliteSaverType } from './sqlite/checkpoint';
|
|
5
|
+
import type { PostgresSaver } from '@langchain/langgraph-checkpoint-postgres';
|
|
5
6
|
import { SQLiteThreadsManager } from './sqlite/threads';
|
|
6
|
-
export declare const createCheckPointer: () => Promise<import("@langchain/langgraph-checkpoint-redis").RedisSaver | import("@langchain/langgraph-checkpoint-redis/shallow").ShallowRedisSaver |
|
|
7
|
+
export declare const createCheckPointer: () => Promise<SqliteSaverType | PostgresSaver | import("@langchain/langgraph-checkpoint-redis").RedisSaver | import("@langchain/langgraph-checkpoint-redis/shallow").ShallowRedisSaver | MemorySaver>;
|
|
7
8
|
export declare const createMessageQueue: () => Promise<StreamQueueManager<BaseStreamQueueInterface>>;
|
|
8
9
|
export declare const createThreadManager: (config: {
|
|
9
|
-
checkpointer?: SqliteSaverType;
|
|
10
|
-
}) => SQLiteThreadsManager<unknown> | MemoryThreadsManager<unknown
|
|
10
|
+
checkpointer?: SqliteSaverType | PostgresSaver;
|
|
11
|
+
}) => Promise<import("./pg/threads").PostgresThreadsManager<unknown> | SQLiteThreadsManager<unknown> | MemoryThreadsManager<unknown>>;
|
package/dist/storage/index.js
CHANGED
|
@@ -8,6 +8,7 @@ export const createCheckPointer = async () => {
|
|
|
8
8
|
if ((process.env.REDIS_URL && process.env.CHECKPOINT_TYPE === 'redis') ||
|
|
9
9
|
process.env.CHECKPOINT_TYPE === 'shallow/redis') {
|
|
10
10
|
if (process.env.CHECKPOINT_TYPE === 'redis') {
|
|
11
|
+
console.debug('LG | Using redis as checkpoint');
|
|
11
12
|
const { RedisSaver } = await import('@langchain/langgraph-checkpoint-redis');
|
|
12
13
|
return await RedisSaver.fromUrl(process.env.REDIS_URL, {
|
|
13
14
|
defaultTTL: 60, // TTL in minutes
|
|
@@ -15,11 +16,18 @@ export const createCheckPointer = async () => {
|
|
|
15
16
|
});
|
|
16
17
|
}
|
|
17
18
|
if (process.env.CHECKPOINT_TYPE === 'shallow/redis') {
|
|
19
|
+
console.debug('LG | Using shallow redis as checkpoint');
|
|
18
20
|
const { ShallowRedisSaver } = await import('@langchain/langgraph-checkpoint-redis/shallow');
|
|
19
21
|
return await ShallowRedisSaver.fromUrl(process.env.REDIS_URL);
|
|
20
22
|
}
|
|
21
23
|
}
|
|
24
|
+
if (process.env.DATABASE_URL) {
|
|
25
|
+
console.debug('LG | Using postgres as checkpoint');
|
|
26
|
+
const { createPGCheckpoint } = await import('./pg/checkpoint');
|
|
27
|
+
return createPGCheckpoint();
|
|
28
|
+
}
|
|
22
29
|
if (process.env.SQLITE_DATABASE_URI) {
|
|
30
|
+
console.debug('LG | Using sqlite as checkpoint');
|
|
23
31
|
const { SqliteSaver } = await import('./sqlite/checkpoint');
|
|
24
32
|
const db = SqliteSaver.fromConnString(process.env.SQLITE_DATABASE_URI);
|
|
25
33
|
return db;
|
|
@@ -29,7 +37,7 @@ export const createCheckPointer = async () => {
|
|
|
29
37
|
export const createMessageQueue = async () => {
|
|
30
38
|
let q;
|
|
31
39
|
if (process.env.REDIS_URL) {
|
|
32
|
-
console.
|
|
40
|
+
console.debug('LG | Using redis as stream queue');
|
|
33
41
|
const { RedisStreamQueue } = await import('./redis/queue');
|
|
34
42
|
q = RedisStreamQueue;
|
|
35
43
|
}
|
|
@@ -38,9 +46,19 @@ export const createMessageQueue = async () => {
|
|
|
38
46
|
}
|
|
39
47
|
return new StreamQueueManager(q);
|
|
40
48
|
};
|
|
41
|
-
export const createThreadManager = (config) => {
|
|
49
|
+
export const createThreadManager = async (config) => {
|
|
50
|
+
if (process.env.DATABASE_URL && config.checkpointer) {
|
|
51
|
+
const { PostgresThreadsManager } = await import('./pg/threads');
|
|
52
|
+
const threadsManager = new PostgresThreadsManager(config.checkpointer);
|
|
53
|
+
if (process.env.DATABASE_INIT === 'true') {
|
|
54
|
+
await threadsManager.setup();
|
|
55
|
+
}
|
|
56
|
+
return threadsManager;
|
|
57
|
+
}
|
|
42
58
|
if (process.env.SQLITE_DATABASE_URI && config.checkpointer) {
|
|
43
|
-
|
|
59
|
+
const threadsManager = new SQLiteThreadsManager(config.checkpointer);
|
|
60
|
+
await threadsManager.setup();
|
|
61
|
+
return threadsManager;
|
|
44
62
|
}
|
|
45
63
|
return new MemoryThreadsManager();
|
|
46
64
|
};
|
|
@@ -2,6 +2,7 @@ import { BaseThreadsManager } from '../../threads/index.js';
|
|
|
2
2
|
import { Command, Config, Metadata, OnConflictBehavior, Run, RunStatus, SortOrder, Thread, ThreadSortBy, ThreadStatus } from '@langgraph-js/sdk';
|
|
3
3
|
export declare class MemoryThreadsManager<ValuesType = unknown> implements BaseThreadsManager<ValuesType> {
|
|
4
4
|
private threads;
|
|
5
|
+
setup(): Promise<void>;
|
|
5
6
|
create(payload?: {
|
|
6
7
|
metadata?: Metadata;
|
|
7
8
|
threadId?: string;
|
|
@@ -2,6 +2,9 @@ import { getGraph } from '../../utils/getGraph.js';
|
|
|
2
2
|
import { serialiseAsDict } from '../../graph/stream.js';
|
|
3
3
|
export class MemoryThreadsManager {
|
|
4
4
|
threads = [];
|
|
5
|
+
async setup() {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
5
8
|
async create(payload) {
|
|
6
9
|
const threadId = payload?.threadId || crypto.randomUUID();
|
|
7
10
|
if (payload?.ifExists === 'raise' && this.threads.some((t) => t.thread_id === threadId)) {
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { PostgresSaver } from '@langchain/langgraph-checkpoint-postgres';
|
|
2
|
+
export const createPGCheckpoint = async () => {
|
|
3
|
+
const checkpointer = PostgresSaver.fromConnString(process.env.DATABASE_URL);
|
|
4
|
+
if (process.env.DATABASE_INIT === 'true') {
|
|
5
|
+
console.debug('LG | Initializing postgres checkpoint');
|
|
6
|
+
await checkpointer.setup();
|
|
7
|
+
}
|
|
8
|
+
return checkpointer;
|
|
9
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { BaseThreadsManager } from '../../threads/index.js';
|
|
2
|
+
import { Command, Config, Metadata, OnConflictBehavior, Run, RunStatus, SortOrder, Thread, ThreadSortBy, ThreadStatus } from '@langgraph-js/sdk';
|
|
3
|
+
import type { PostgresSaver } from '@langchain/langgraph-checkpoint-postgres';
|
|
4
|
+
export declare class PostgresThreadsManager<ValuesType = unknown> implements BaseThreadsManager<ValuesType> {
|
|
5
|
+
private pool;
|
|
6
|
+
private isSetup;
|
|
7
|
+
constructor(checkpointer: PostgresSaver);
|
|
8
|
+
setup(): Promise<void>;
|
|
9
|
+
create(payload?: {
|
|
10
|
+
metadata?: Metadata;
|
|
11
|
+
threadId?: string;
|
|
12
|
+
ifExists?: OnConflictBehavior;
|
|
13
|
+
graphId?: string;
|
|
14
|
+
supersteps?: Array<{
|
|
15
|
+
updates: Array<{
|
|
16
|
+
values: unknown;
|
|
17
|
+
command?: Command;
|
|
18
|
+
asNode: string;
|
|
19
|
+
}>;
|
|
20
|
+
}>;
|
|
21
|
+
}): Promise<Thread<ValuesType>>;
|
|
22
|
+
search(query?: {
|
|
23
|
+
metadata?: Metadata;
|
|
24
|
+
limit?: number;
|
|
25
|
+
offset?: number;
|
|
26
|
+
status?: ThreadStatus;
|
|
27
|
+
sortBy?: ThreadSortBy;
|
|
28
|
+
sortOrder?: SortOrder;
|
|
29
|
+
}): Promise<Thread<ValuesType>[]>;
|
|
30
|
+
get(threadId: string): Promise<Thread<ValuesType>>;
|
|
31
|
+
set(threadId: string, thread: Partial<Thread<ValuesType>>): Promise<void>;
|
|
32
|
+
updateState(threadId: string, thread: Partial<Thread<ValuesType>>): Promise<Pick<Config, 'configurable'>>;
|
|
33
|
+
delete(threadId: string): Promise<void>;
|
|
34
|
+
createRun(threadId: string, assistantId: string, payload?: {
|
|
35
|
+
metadata?: Metadata;
|
|
36
|
+
}): Promise<Run>;
|
|
37
|
+
listRuns(threadId: string, options?: {
|
|
38
|
+
limit?: number;
|
|
39
|
+
offset?: number;
|
|
40
|
+
status?: RunStatus;
|
|
41
|
+
}): Promise<Run[]>;
|
|
42
|
+
updateRun(runId: string, run: Partial<Run>): Promise<void>;
|
|
43
|
+
}
|