@langgraph-js/pure-graph 1.4.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 CHANGED
@@ -27,6 +27,25 @@ yarn add @langgraph-js/pure-graph
27
27
 
28
28
  ### Next.js Example
29
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
+
30
49
  To integrate Pure Graph into a Next.js project, follow these steps:
31
50
 
32
51
  1. **Create a Route Handler**
@@ -37,7 +56,7 @@ To integrate Pure Graph into a Next.js project, follow these steps:
37
56
  // app/api/langgraph/[...path]/route.ts
38
57
  import { GET, POST, DELETE } from '@langgraph-js/pure-graph/dist/adapter/nextjs/router.js';
39
58
  import { registerGraph } from '@langgraph-js/pure-graph';
40
- import { graph } from './path/to/your/graph'; // Replace with your graph implementation
59
+ import { graph } from '../../../agent/graph-name/graph';
41
60
 
42
61
  registerGraph('test', graph);
43
62
 
@@ -65,7 +84,7 @@ To integrate Pure Graph into a Hono.js project, follow these steps:
65
84
  ```js
66
85
  // app.js
67
86
  import { registerGraph } from '@langgraph-js/pure-graph';
68
- import { graph } from './path/to/your/graph'; // Replace with your graph implementation
87
+ import { graph } from './agent/graph-name/graph';
69
88
  import { Hono } from 'hono';
70
89
  import LangGraphApp from '@langgraph-js/pure-graph/dist/adapter/hono/index';
71
90
 
@@ -93,9 +112,146 @@ Here are the environment variables you need to configure:
93
112
 
94
113
  - `SQLITE_DATABASE_URI`: Path to your SQLite database (e.g., `./.langgraph_api/chat.db`).
95
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).
96
116
  - `CHECKPOINT_TYPE`: Type of checkpoint storage (optional, defaults to memory; options: `postgres`, `redis`, `shallow/redis`).
97
117
  - `REDIS_URL`: URL for Redis (required if using Redis checkpoint or message queue).
98
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)
254
+
99
255
  ## API Endpoints
100
256
 
101
257
  ### Assistants
package/dist/global.d.ts CHANGED
@@ -2,6 +2,6 @@ import type { SqliteSaver } from './storage/sqlite/checkpoint.js';
2
2
  import type { PostgresSaver } from '@langchain/langgraph-checkpoint-postgres';
3
3
  export declare class LangGraphGlobal {
4
4
  static globalMessageQueue: import("./queue/stream_queue.js").StreamQueueManager<import("./queue/stream_queue.js").BaseStreamQueueInterface>;
5
- static globalCheckPointer: import("@langchain/langgraph-checkpoint-redis").RedisSaver | import("@langchain/langgraph-checkpoint-redis/shallow").ShallowRedisSaver | PostgresSaver | SqliteSaver | import("@langchain/langgraph-checkpoint").MemorySaver;
5
+ static globalCheckPointer: SqliteSaver | PostgresSaver | import("@langchain/langgraph-checkpoint-redis").RedisSaver | import("@langchain/langgraph-checkpoint-redis/shallow").ShallowRedisSaver | import("@langchain/langgraph-checkpoint").MemorySaver;
6
6
  static globalThreadsManager: import("./storage/pg/threads.js").PostgresThreadsManager<unknown> | import("./storage/sqlite/threads.js").SQLiteThreadsManager<unknown> | import("./storage/memory/threads.js").MemoryThreadsManager<unknown>;
7
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;
@@ -4,7 +4,7 @@ import { MemoryThreadsManager } from './memory/threads';
4
4
  import type { SqliteSaver as SqliteSaverType } from './sqlite/checkpoint';
5
5
  import type { PostgresSaver } from '@langchain/langgraph-checkpoint-postgres';
6
6
  import { SQLiteThreadsManager } from './sqlite/threads';
7
- export declare const createCheckPointer: () => Promise<import("@langchain/langgraph-checkpoint-redis").RedisSaver | import("@langchain/langgraph-checkpoint-redis/shallow").ShallowRedisSaver | PostgresSaver | SqliteSaverType | MemorySaver>;
7
+ export declare const createCheckPointer: () => Promise<SqliteSaverType | PostgresSaver | import("@langchain/langgraph-checkpoint-redis").RedisSaver | import("@langchain/langgraph-checkpoint-redis/shallow").ShallowRedisSaver | MemorySaver>;
8
8
  export declare const createMessageQueue: () => Promise<StreamQueueManager<BaseStreamQueueInterface>>;
9
9
  export declare const createThreadManager: (config: {
10
10
  checkpointer?: SqliteSaverType | PostgresSaver;
@@ -8,7 +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.log('Using redis as checkpoint');
11
+ console.debug('LG | Using redis as checkpoint');
12
12
  const { RedisSaver } = await import('@langchain/langgraph-checkpoint-redis');
13
13
  return await RedisSaver.fromUrl(process.env.REDIS_URL, {
14
14
  defaultTTL: 60, // TTL in minutes
@@ -16,18 +16,18 @@ export const createCheckPointer = async () => {
16
16
  });
17
17
  }
18
18
  if (process.env.CHECKPOINT_TYPE === 'shallow/redis') {
19
- console.log('Using shallow redis as checkpoint');
19
+ console.debug('LG | Using shallow redis as checkpoint');
20
20
  const { ShallowRedisSaver } = await import('@langchain/langgraph-checkpoint-redis/shallow');
21
21
  return await ShallowRedisSaver.fromUrl(process.env.REDIS_URL);
22
22
  }
23
23
  }
24
24
  if (process.env.DATABASE_URL) {
25
- console.log('Using postgres as checkpoint');
25
+ console.debug('LG | Using postgres as checkpoint');
26
26
  const { createPGCheckpoint } = await import('./pg/checkpoint');
27
27
  return createPGCheckpoint();
28
28
  }
29
29
  if (process.env.SQLITE_DATABASE_URI) {
30
- console.log('Using sqlite as checkpoint');
30
+ console.debug('LG | Using sqlite as checkpoint');
31
31
  const { SqliteSaver } = await import('./sqlite/checkpoint');
32
32
  const db = SqliteSaver.fromConnString(process.env.SQLITE_DATABASE_URI);
33
33
  return db;
@@ -37,7 +37,7 @@ export const createCheckPointer = async () => {
37
37
  export const createMessageQueue = async () => {
38
38
  let q;
39
39
  if (process.env.REDIS_URL) {
40
- console.log('Using redis as stream queue');
40
+ console.debug('LG | Using redis as stream queue');
41
41
  const { RedisStreamQueue } = await import('./redis/queue');
42
42
  q = RedisStreamQueue;
43
43
  }
@@ -49,10 +49,16 @@ export const createMessageQueue = async () => {
49
49
  export const createThreadManager = async (config) => {
50
50
  if (process.env.DATABASE_URL && config.checkpointer) {
51
51
  const { PostgresThreadsManager } = await import('./pg/threads');
52
- return new PostgresThreadsManager(config.checkpointer);
52
+ const threadsManager = new PostgresThreadsManager(config.checkpointer);
53
+ if (process.env.DATABASE_INIT === 'true') {
54
+ await threadsManager.setup();
55
+ }
56
+ return threadsManager;
53
57
  }
54
58
  if (process.env.SQLITE_DATABASE_URI && config.checkpointer) {
55
- return new SQLiteThreadsManager(config.checkpointer);
59
+ const threadsManager = new SQLiteThreadsManager(config.checkpointer);
60
+ await threadsManager.setup();
61
+ return threadsManager;
56
62
  }
57
63
  return new MemoryThreadsManager();
58
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)) {
@@ -2,7 +2,7 @@ import { PostgresSaver } from '@langchain/langgraph-checkpoint-postgres';
2
2
  export const createPGCheckpoint = async () => {
3
3
  const checkpointer = PostgresSaver.fromConnString(process.env.DATABASE_URL);
4
4
  if (process.env.DATABASE_INIT === 'true') {
5
- console.log('Initializing postgres checkpoint');
5
+ console.debug('LG | Initializing postgres checkpoint');
6
6
  await checkpointer.setup();
7
7
  }
8
8
  return checkpointer;
@@ -5,7 +5,7 @@ export declare class PostgresThreadsManager<ValuesType = unknown> implements Bas
5
5
  private pool;
6
6
  private isSetup;
7
7
  constructor(checkpointer: PostgresSaver);
8
- private setup;
8
+ setup(): Promise<void>;
9
9
  create(payload?: {
10
10
  metadata?: Metadata;
11
11
  threadId?: string;
@@ -6,7 +6,6 @@ export class PostgresThreadsManager {
6
6
  constructor(checkpointer) {
7
7
  // 访问 PostgresSaver 的 pool 属性(虽然是 private,但在运行时可以访问)
8
8
  this.pool = checkpointer.pool;
9
- this.setup();
10
9
  }
11
10
  async setup() {
12
11
  if (this.isSetup) {
@@ -1,7 +1,7 @@
1
1
  let Database;
2
2
  /** @ts-ignore */
3
3
  if (globalThis.Bun) {
4
- console.log('Using Bun Sqlite, pid:', process.pid);
4
+ console.debug('LG | Using Bun Sqlite, pid:', process.pid);
5
5
  const BunSqlite = await import('bun:sqlite');
6
6
  /** @ts-ignore */
7
7
  Database = BunSqlite.default;
@@ -6,7 +6,7 @@ export declare class SQLiteThreadsManager<ValuesType = unknown> implements BaseT
6
6
  db: DatabaseType;
7
7
  private isSetup;
8
8
  constructor(checkpointer: SqliteSaver);
9
- private setup;
9
+ setup(): Promise<void>;
10
10
  create(payload?: {
11
11
  metadata?: Metadata;
12
12
  threadId?: string;
@@ -5,9 +5,8 @@ export class SQLiteThreadsManager {
5
5
  isSetup = false;
6
6
  constructor(checkpointer) {
7
7
  this.db = checkpointer.db;
8
- this.setup();
9
8
  }
10
- setup() {
9
+ async setup() {
11
10
  if (this.isSetup) {
12
11
  return;
13
12
  }
@@ -1,5 +1,6 @@
1
1
  import { Command, Config, Metadata, OnConflictBehavior, Run, RunStatus, SortOrder, Thread, ThreadSortBy, ThreadStatus } from '@langgraph-js/sdk';
2
2
  export interface BaseThreadsManager<ValuesType = unknown> {
3
+ setup(): Promise<void>;
3
4
  create(payload?: {
4
5
  metadata?: Metadata;
5
6
  threadId?: string;