@agentforge/core 0.9.0 → 0.9.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/dist/index.cjs +322 -322
- package/dist/index.d.cts +854 -854
- package/dist/index.d.ts +854 -854
- package/dist/index.js +322 -322
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2134,7 +2134,86 @@ function composeGraphs(parentGraph, subgraph, options) {
|
|
|
2134
2134
|
return parentGraph;
|
|
2135
2135
|
}
|
|
2136
2136
|
|
|
2137
|
-
// src/langgraph/
|
|
2137
|
+
// src/langgraph/middleware/compose.ts
|
|
2138
|
+
function compose(...middleware) {
|
|
2139
|
+
return (node) => {
|
|
2140
|
+
return middleware.reduceRight(
|
|
2141
|
+
(wrappedNode, mw) => mw(wrappedNode),
|
|
2142
|
+
node
|
|
2143
|
+
);
|
|
2144
|
+
};
|
|
2145
|
+
}
|
|
2146
|
+
function composeWithOptions(options, ...middleware) {
|
|
2147
|
+
const { reverse = false, name, catchErrors = true } = options;
|
|
2148
|
+
return (node) => {
|
|
2149
|
+
const orderedMiddleware = reverse ? [...middleware].reverse() : middleware;
|
|
2150
|
+
let wrappedNode = orderedMiddleware.reduceRight(
|
|
2151
|
+
(wrappedNode2, mw) => mw(wrappedNode2),
|
|
2152
|
+
node
|
|
2153
|
+
);
|
|
2154
|
+
if (catchErrors) {
|
|
2155
|
+
const originalNode = wrappedNode;
|
|
2156
|
+
wrappedNode = async (state) => {
|
|
2157
|
+
try {
|
|
2158
|
+
return await Promise.resolve(originalNode(state));
|
|
2159
|
+
} catch (error) {
|
|
2160
|
+
const enhancedError = error instanceof Error ? error : new Error(String(error));
|
|
2161
|
+
if (name) {
|
|
2162
|
+
enhancedError.message = `[${name}] ${enhancedError.message}`;
|
|
2163
|
+
}
|
|
2164
|
+
throw enhancedError;
|
|
2165
|
+
}
|
|
2166
|
+
};
|
|
2167
|
+
}
|
|
2168
|
+
return wrappedNode;
|
|
2169
|
+
};
|
|
2170
|
+
}
|
|
2171
|
+
var MiddlewareChain = class {
|
|
2172
|
+
middleware = [];
|
|
2173
|
+
options = {};
|
|
2174
|
+
/**
|
|
2175
|
+
* Add middleware to the chain.
|
|
2176
|
+
*/
|
|
2177
|
+
use(middleware) {
|
|
2178
|
+
this.middleware.push(middleware);
|
|
2179
|
+
return this;
|
|
2180
|
+
}
|
|
2181
|
+
/**
|
|
2182
|
+
* Set composition options.
|
|
2183
|
+
*/
|
|
2184
|
+
withOptions(options) {
|
|
2185
|
+
this.options = { ...this.options, ...options };
|
|
2186
|
+
return this;
|
|
2187
|
+
}
|
|
2188
|
+
/**
|
|
2189
|
+
* Build the middleware chain and apply it to a node.
|
|
2190
|
+
*/
|
|
2191
|
+
build(node) {
|
|
2192
|
+
if (this.middleware.length === 0) {
|
|
2193
|
+
return node;
|
|
2194
|
+
}
|
|
2195
|
+
return composeWithOptions(this.options, ...this.middleware)(node);
|
|
2196
|
+
}
|
|
2197
|
+
/**
|
|
2198
|
+
* Get the number of middleware in the chain.
|
|
2199
|
+
*/
|
|
2200
|
+
get length() {
|
|
2201
|
+
return this.middleware.length;
|
|
2202
|
+
}
|
|
2203
|
+
};
|
|
2204
|
+
function chain() {
|
|
2205
|
+
return new MiddlewareChain();
|
|
2206
|
+
}
|
|
2207
|
+
function createMiddlewareContext() {
|
|
2208
|
+
return {
|
|
2209
|
+
executionId: `exec_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
|
2210
|
+
startTime: Date.now(),
|
|
2211
|
+
data: {},
|
|
2212
|
+
middlewareStack: []
|
|
2213
|
+
};
|
|
2214
|
+
}
|
|
2215
|
+
|
|
2216
|
+
// src/langgraph/middleware/retry.ts
|
|
2138
2217
|
function calculateDelay(attempt, strategy, initialDelay, maxDelay) {
|
|
2139
2218
|
let delay;
|
|
2140
2219
|
switch (strategy) {
|
|
@@ -2186,7 +2265,7 @@ function withRetry(node, options = {}) {
|
|
|
2186
2265
|
};
|
|
2187
2266
|
}
|
|
2188
2267
|
|
|
2189
|
-
// src/langgraph/
|
|
2268
|
+
// src/langgraph/middleware/error-handler.ts
|
|
2190
2269
|
function withErrorHandler(node, options) {
|
|
2191
2270
|
const { onError, logError, rethrow = false } = options;
|
|
2192
2271
|
return async (state) => {
|
|
@@ -2206,7 +2285,7 @@ function withErrorHandler(node, options) {
|
|
|
2206
2285
|
};
|
|
2207
2286
|
}
|
|
2208
2287
|
|
|
2209
|
-
// src/langgraph/
|
|
2288
|
+
// src/langgraph/middleware/timeout.ts
|
|
2210
2289
|
var TimeoutError = class extends Error {
|
|
2211
2290
|
constructor(timeout2) {
|
|
2212
2291
|
super(`Node execution timed out after ${timeout2}ms`);
|
|
@@ -2238,150 +2317,6 @@ function withTimeout(node, options) {
|
|
|
2238
2317
|
};
|
|
2239
2318
|
}
|
|
2240
2319
|
|
|
2241
|
-
// src/langgraph/persistence/checkpointer.ts
|
|
2242
|
-
import { MemorySaver } from "@langchain/langgraph";
|
|
2243
|
-
function createMemoryCheckpointer(options) {
|
|
2244
|
-
return new MemorySaver();
|
|
2245
|
-
}
|
|
2246
|
-
async function createSqliteCheckpointer(options = {}) {
|
|
2247
|
-
const { path = ":memory:", autoMigrate = true } = options;
|
|
2248
|
-
try {
|
|
2249
|
-
const { SqliteSaver } = await import("@langchain/langgraph-checkpoint-sqlite");
|
|
2250
|
-
const checkpointer = SqliteSaver.fromConnString(path);
|
|
2251
|
-
if (autoMigrate) {
|
|
2252
|
-
await checkpointer.setup();
|
|
2253
|
-
}
|
|
2254
|
-
return checkpointer;
|
|
2255
|
-
} catch (error) {
|
|
2256
|
-
if (error instanceof Error && error.message.includes("Cannot find module")) {
|
|
2257
|
-
throw new Error(
|
|
2258
|
-
"SQLite checkpointer requires @langchain/langgraph-checkpoint-sqlite to be installed. Install it with: npm install @langchain/langgraph-checkpoint-sqlite"
|
|
2259
|
-
);
|
|
2260
|
-
}
|
|
2261
|
-
throw error;
|
|
2262
|
-
}
|
|
2263
|
-
}
|
|
2264
|
-
function isMemoryCheckpointer(checkpointer) {
|
|
2265
|
-
return checkpointer instanceof MemorySaver;
|
|
2266
|
-
}
|
|
2267
|
-
|
|
2268
|
-
// src/langgraph/persistence/thread.ts
|
|
2269
|
-
import { randomUUID } from "crypto";
|
|
2270
|
-
function generateThreadId(seed) {
|
|
2271
|
-
if (seed && seed.length > 0) {
|
|
2272
|
-
const hash = Buffer.from(seed).toString("base64").replace(/[^a-zA-Z0-9]/g, "");
|
|
2273
|
-
return `thread-${hash}`;
|
|
2274
|
-
}
|
|
2275
|
-
return randomUUID();
|
|
2276
|
-
}
|
|
2277
|
-
function createThreadConfig(config = {}) {
|
|
2278
|
-
const { threadId = generateThreadId(), checkpointId, checkpointNamespace, metadata } = config;
|
|
2279
|
-
const runnableConfig = {
|
|
2280
|
-
configurable: {
|
|
2281
|
-
thread_id: threadId
|
|
2282
|
-
}
|
|
2283
|
-
};
|
|
2284
|
-
if (checkpointId) {
|
|
2285
|
-
runnableConfig.configurable.checkpoint_id = checkpointId;
|
|
2286
|
-
}
|
|
2287
|
-
if (checkpointNamespace) {
|
|
2288
|
-
runnableConfig.configurable.checkpoint_ns = checkpointNamespace;
|
|
2289
|
-
}
|
|
2290
|
-
if (metadata) {
|
|
2291
|
-
runnableConfig.metadata = metadata;
|
|
2292
|
-
}
|
|
2293
|
-
return runnableConfig;
|
|
2294
|
-
}
|
|
2295
|
-
function createConversationConfig(config) {
|
|
2296
|
-
const { userId, sessionId, metadata = {} } = config;
|
|
2297
|
-
const threadId = sessionId ? generateThreadId(`${userId}-${sessionId}`) : generateThreadId(userId);
|
|
2298
|
-
return createThreadConfig({
|
|
2299
|
-
threadId,
|
|
2300
|
-
metadata: {
|
|
2301
|
-
...metadata,
|
|
2302
|
-
userId,
|
|
2303
|
-
sessionId
|
|
2304
|
-
}
|
|
2305
|
-
});
|
|
2306
|
-
}
|
|
2307
|
-
|
|
2308
|
-
// src/langgraph/persistence/utils.ts
|
|
2309
|
-
async function getCheckpointHistory(checkpointer, options) {
|
|
2310
|
-
const { threadId, limit = 10, before } = options;
|
|
2311
|
-
const config = {
|
|
2312
|
-
configurable: {
|
|
2313
|
-
thread_id: threadId
|
|
2314
|
-
}
|
|
2315
|
-
};
|
|
2316
|
-
if (before) {
|
|
2317
|
-
config.configurable.checkpoint_id = before;
|
|
2318
|
-
}
|
|
2319
|
-
const checkpoints = [];
|
|
2320
|
-
for await (const checkpoint of checkpointer.list(config, { limit })) {
|
|
2321
|
-
checkpoints.push(checkpoint);
|
|
2322
|
-
}
|
|
2323
|
-
return checkpoints;
|
|
2324
|
-
}
|
|
2325
|
-
async function getLatestCheckpoint(checkpointer, options) {
|
|
2326
|
-
const { threadId } = options;
|
|
2327
|
-
const config = {
|
|
2328
|
-
configurable: {
|
|
2329
|
-
thread_id: threadId
|
|
2330
|
-
}
|
|
2331
|
-
};
|
|
2332
|
-
const tuple = await checkpointer.getTuple(config);
|
|
2333
|
-
return tuple || null;
|
|
2334
|
-
}
|
|
2335
|
-
async function clearThread(checkpointer, options) {
|
|
2336
|
-
const { threadId } = options;
|
|
2337
|
-
const checkpoints = await getCheckpointHistory(checkpointer, {
|
|
2338
|
-
threadId,
|
|
2339
|
-
limit: 1e3
|
|
2340
|
-
// Get all checkpoints
|
|
2341
|
-
});
|
|
2342
|
-
if (checkpoints.length > 0) {
|
|
2343
|
-
throw new Error(
|
|
2344
|
-
"Clearing threads is not supported by the current checkpointer implementation. Consider using a new thread ID instead."
|
|
2345
|
-
);
|
|
2346
|
-
}
|
|
2347
|
-
}
|
|
2348
|
-
|
|
2349
|
-
// src/langgraph/observability/langsmith.ts
|
|
2350
|
-
var globalConfig = null;
|
|
2351
|
-
function configureLangSmith(config) {
|
|
2352
|
-
globalConfig = config;
|
|
2353
|
-
if (config.apiKey) {
|
|
2354
|
-
process.env.LANGSMITH_API_KEY = config.apiKey;
|
|
2355
|
-
}
|
|
2356
|
-
if (config.projectName) {
|
|
2357
|
-
process.env.LANGSMITH_PROJECT = config.projectName;
|
|
2358
|
-
}
|
|
2359
|
-
if (config.tracingEnabled !== void 0) {
|
|
2360
|
-
process.env.LANGSMITH_TRACING = config.tracingEnabled ? "true" : "false";
|
|
2361
|
-
} else if (config.apiKey) {
|
|
2362
|
-
process.env.LANGSMITH_TRACING = "true";
|
|
2363
|
-
}
|
|
2364
|
-
if (config.endpoint) {
|
|
2365
|
-
process.env.LANGSMITH_ENDPOINT = config.endpoint;
|
|
2366
|
-
}
|
|
2367
|
-
}
|
|
2368
|
-
function getLangSmithConfig() {
|
|
2369
|
-
return globalConfig;
|
|
2370
|
-
}
|
|
2371
|
-
function isTracingEnabled() {
|
|
2372
|
-
return process.env.LANGSMITH_TRACING === "true";
|
|
2373
|
-
}
|
|
2374
|
-
function withTracing(node, options) {
|
|
2375
|
-
const { name, metadata, tags, runName } = options;
|
|
2376
|
-
return async (state) => {
|
|
2377
|
-
if (isTracingEnabled()) {
|
|
2378
|
-
const result = await Promise.resolve(node(state));
|
|
2379
|
-
return result;
|
|
2380
|
-
}
|
|
2381
|
-
return await Promise.resolve(node(state));
|
|
2382
|
-
};
|
|
2383
|
-
}
|
|
2384
|
-
|
|
2385
2320
|
// src/langgraph/observability/metrics.ts
|
|
2386
2321
|
var MetricType = /* @__PURE__ */ ((MetricType2) => {
|
|
2387
2322
|
MetricType2["COUNTER"] = "counter";
|
|
@@ -2490,189 +2425,39 @@ function withMetrics(node, options) {
|
|
|
2490
2425
|
};
|
|
2491
2426
|
}
|
|
2492
2427
|
|
|
2493
|
-
// src/langgraph/observability/
|
|
2494
|
-
var
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
cause;
|
|
2500
|
-
timestamp;
|
|
2501
|
-
constructor(message, context = {}) {
|
|
2502
|
-
super(message);
|
|
2503
|
-
this.name = "AgentError";
|
|
2504
|
-
this.code = context.code;
|
|
2505
|
-
this.node = context.node;
|
|
2506
|
-
this.state = context.state;
|
|
2507
|
-
this.metadata = context.metadata;
|
|
2508
|
-
this.cause = context.cause;
|
|
2509
|
-
this.timestamp = Date.now();
|
|
2510
|
-
if (Error.captureStackTrace) {
|
|
2511
|
-
Error.captureStackTrace(this, _AgentError);
|
|
2512
|
-
}
|
|
2428
|
+
// src/langgraph/observability/langsmith.ts
|
|
2429
|
+
var globalConfig = null;
|
|
2430
|
+
function configureLangSmith(config) {
|
|
2431
|
+
globalConfig = config;
|
|
2432
|
+
if (config.apiKey) {
|
|
2433
|
+
process.env.LANGSMITH_API_KEY = config.apiKey;
|
|
2513
2434
|
}
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
*/
|
|
2517
|
-
toJSON() {
|
|
2518
|
-
return {
|
|
2519
|
-
name: this.name,
|
|
2520
|
-
message: this.message,
|
|
2521
|
-
code: this.code,
|
|
2522
|
-
node: this.node,
|
|
2523
|
-
state: this.state,
|
|
2524
|
-
metadata: this.metadata,
|
|
2525
|
-
timestamp: this.timestamp,
|
|
2526
|
-
stack: this.stack,
|
|
2527
|
-
cause: this.cause ? {
|
|
2528
|
-
name: this.cause.name,
|
|
2529
|
-
message: this.cause.message,
|
|
2530
|
-
stack: this.cause.stack
|
|
2531
|
-
} : void 0
|
|
2532
|
-
};
|
|
2533
|
-
}
|
|
2534
|
-
/**
|
|
2535
|
-
* Get a human-readable string representation
|
|
2536
|
-
*/
|
|
2537
|
-
toString() {
|
|
2538
|
-
const parts = [`${this.name}: ${this.message}`];
|
|
2539
|
-
if (this.code) {
|
|
2540
|
-
parts.push(`Code: ${this.code}`);
|
|
2541
|
-
}
|
|
2542
|
-
if (this.node) {
|
|
2543
|
-
parts.push(`Node: ${this.node}`);
|
|
2544
|
-
}
|
|
2545
|
-
if (this.cause) {
|
|
2546
|
-
parts.push(`Caused by: ${this.cause.message}`);
|
|
2547
|
-
}
|
|
2548
|
-
return parts.join("\n");
|
|
2549
|
-
}
|
|
2550
|
-
};
|
|
2551
|
-
var ErrorReporterImpl = class {
|
|
2552
|
-
options;
|
|
2553
|
-
constructor(options) {
|
|
2554
|
-
this.options = {
|
|
2555
|
-
onError: options.onError,
|
|
2556
|
-
includeStackTrace: options.includeStackTrace ?? true,
|
|
2557
|
-
includeState: options.includeState ?? false,
|
|
2558
|
-
rethrow: options.rethrow ?? true
|
|
2559
|
-
};
|
|
2560
|
-
}
|
|
2561
|
-
wrap(node, nodeName) {
|
|
2562
|
-
return async (state) => {
|
|
2563
|
-
try {
|
|
2564
|
-
return await Promise.resolve(node(state));
|
|
2565
|
-
} catch (error) {
|
|
2566
|
-
const agentError = this.toAgentError(error, {
|
|
2567
|
-
node: nodeName,
|
|
2568
|
-
state: this.options.includeState ? state : void 0
|
|
2569
|
-
});
|
|
2570
|
-
await this.report(agentError);
|
|
2571
|
-
if (this.options.rethrow) {
|
|
2572
|
-
throw agentError;
|
|
2573
|
-
}
|
|
2574
|
-
return state;
|
|
2575
|
-
}
|
|
2576
|
-
};
|
|
2435
|
+
if (config.projectName) {
|
|
2436
|
+
process.env.LANGSMITH_PROJECT = config.projectName;
|
|
2577
2437
|
}
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
} catch (reportError) {
|
|
2583
|
-
console.error("Error reporting failed:", reportError);
|
|
2584
|
-
}
|
|
2438
|
+
if (config.tracingEnabled !== void 0) {
|
|
2439
|
+
process.env.LANGSMITH_TRACING = config.tracingEnabled ? "true" : "false";
|
|
2440
|
+
} else if (config.apiKey) {
|
|
2441
|
+
process.env.LANGSMITH_TRACING = "true";
|
|
2585
2442
|
}
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
return error;
|
|
2589
|
-
}
|
|
2590
|
-
return new AgentError(error.message, {
|
|
2591
|
-
...context,
|
|
2592
|
-
cause: error
|
|
2593
|
-
});
|
|
2443
|
+
if (config.endpoint) {
|
|
2444
|
+
process.env.LANGSMITH_ENDPOINT = config.endpoint;
|
|
2594
2445
|
}
|
|
2595
|
-
};
|
|
2596
|
-
function createErrorReporter(options) {
|
|
2597
|
-
return new ErrorReporterImpl(options);
|
|
2598
2446
|
}
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
function compose(...middleware) {
|
|
2602
|
-
return (node) => {
|
|
2603
|
-
return middleware.reduceRight(
|
|
2604
|
-
(wrappedNode, mw) => mw(wrappedNode),
|
|
2605
|
-
node
|
|
2606
|
-
);
|
|
2607
|
-
};
|
|
2447
|
+
function getLangSmithConfig() {
|
|
2448
|
+
return globalConfig;
|
|
2608
2449
|
}
|
|
2609
|
-
function
|
|
2610
|
-
|
|
2611
|
-
return (node) => {
|
|
2612
|
-
const orderedMiddleware = reverse ? [...middleware].reverse() : middleware;
|
|
2613
|
-
let wrappedNode = orderedMiddleware.reduceRight(
|
|
2614
|
-
(wrappedNode2, mw) => mw(wrappedNode2),
|
|
2615
|
-
node
|
|
2616
|
-
);
|
|
2617
|
-
if (catchErrors) {
|
|
2618
|
-
const originalNode = wrappedNode;
|
|
2619
|
-
wrappedNode = async (state) => {
|
|
2620
|
-
try {
|
|
2621
|
-
return await Promise.resolve(originalNode(state));
|
|
2622
|
-
} catch (error) {
|
|
2623
|
-
const enhancedError = error instanceof Error ? error : new Error(String(error));
|
|
2624
|
-
if (name) {
|
|
2625
|
-
enhancedError.message = `[${name}] ${enhancedError.message}`;
|
|
2626
|
-
}
|
|
2627
|
-
throw enhancedError;
|
|
2628
|
-
}
|
|
2629
|
-
};
|
|
2630
|
-
}
|
|
2631
|
-
return wrappedNode;
|
|
2632
|
-
};
|
|
2450
|
+
function isTracingEnabled() {
|
|
2451
|
+
return process.env.LANGSMITH_TRACING === "true";
|
|
2633
2452
|
}
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
use(middleware) {
|
|
2641
|
-
this.middleware.push(middleware);
|
|
2642
|
-
return this;
|
|
2643
|
-
}
|
|
2644
|
-
/**
|
|
2645
|
-
* Set composition options.
|
|
2646
|
-
*/
|
|
2647
|
-
withOptions(options) {
|
|
2648
|
-
this.options = { ...this.options, ...options };
|
|
2649
|
-
return this;
|
|
2650
|
-
}
|
|
2651
|
-
/**
|
|
2652
|
-
* Build the middleware chain and apply it to a node.
|
|
2653
|
-
*/
|
|
2654
|
-
build(node) {
|
|
2655
|
-
if (this.middleware.length === 0) {
|
|
2656
|
-
return node;
|
|
2453
|
+
function withTracing(node, options) {
|
|
2454
|
+
const { name, metadata, tags, runName } = options;
|
|
2455
|
+
return async (state) => {
|
|
2456
|
+
if (isTracingEnabled()) {
|
|
2457
|
+
const result = await Promise.resolve(node(state));
|
|
2458
|
+
return result;
|
|
2657
2459
|
}
|
|
2658
|
-
return
|
|
2659
|
-
}
|
|
2660
|
-
/**
|
|
2661
|
-
* Get the number of middleware in the chain.
|
|
2662
|
-
*/
|
|
2663
|
-
get length() {
|
|
2664
|
-
return this.middleware.length;
|
|
2665
|
-
}
|
|
2666
|
-
};
|
|
2667
|
-
function chain() {
|
|
2668
|
-
return new MiddlewareChain();
|
|
2669
|
-
}
|
|
2670
|
-
function createMiddlewareContext() {
|
|
2671
|
-
return {
|
|
2672
|
-
executionId: `exec_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
|
2673
|
-
startTime: Date.now(),
|
|
2674
|
-
data: {},
|
|
2675
|
-
middlewareStack: []
|
|
2460
|
+
return await Promise.resolve(node(state));
|
|
2676
2461
|
};
|
|
2677
2462
|
}
|
|
2678
2463
|
|
|
@@ -2874,6 +2659,221 @@ var presets = {
|
|
|
2874
2659
|
testing
|
|
2875
2660
|
};
|
|
2876
2661
|
|
|
2662
|
+
// src/langgraph/persistence/checkpointer.ts
|
|
2663
|
+
import { MemorySaver } from "@langchain/langgraph";
|
|
2664
|
+
function createMemoryCheckpointer(options) {
|
|
2665
|
+
return new MemorySaver();
|
|
2666
|
+
}
|
|
2667
|
+
async function createSqliteCheckpointer(options = {}) {
|
|
2668
|
+
const { path = ":memory:", autoMigrate = true } = options;
|
|
2669
|
+
try {
|
|
2670
|
+
const { SqliteSaver } = await import("@langchain/langgraph-checkpoint-sqlite");
|
|
2671
|
+
const checkpointer = SqliteSaver.fromConnString(path);
|
|
2672
|
+
if (autoMigrate) {
|
|
2673
|
+
await checkpointer.setup();
|
|
2674
|
+
}
|
|
2675
|
+
return checkpointer;
|
|
2676
|
+
} catch (error) {
|
|
2677
|
+
if (error instanceof Error && error.message.includes("Cannot find module")) {
|
|
2678
|
+
throw new Error(
|
|
2679
|
+
"SQLite checkpointer requires @langchain/langgraph-checkpoint-sqlite to be installed. Install it with: npm install @langchain/langgraph-checkpoint-sqlite"
|
|
2680
|
+
);
|
|
2681
|
+
}
|
|
2682
|
+
throw error;
|
|
2683
|
+
}
|
|
2684
|
+
}
|
|
2685
|
+
function isMemoryCheckpointer(checkpointer) {
|
|
2686
|
+
return checkpointer instanceof MemorySaver;
|
|
2687
|
+
}
|
|
2688
|
+
|
|
2689
|
+
// src/langgraph/persistence/thread.ts
|
|
2690
|
+
import { randomUUID } from "crypto";
|
|
2691
|
+
function generateThreadId(seed) {
|
|
2692
|
+
if (seed && seed.length > 0) {
|
|
2693
|
+
const hash = Buffer.from(seed).toString("base64").replace(/[^a-zA-Z0-9]/g, "");
|
|
2694
|
+
return `thread-${hash}`;
|
|
2695
|
+
}
|
|
2696
|
+
return randomUUID();
|
|
2697
|
+
}
|
|
2698
|
+
function createThreadConfig(config = {}) {
|
|
2699
|
+
const { threadId = generateThreadId(), checkpointId, checkpointNamespace, metadata } = config;
|
|
2700
|
+
const runnableConfig = {
|
|
2701
|
+
configurable: {
|
|
2702
|
+
thread_id: threadId
|
|
2703
|
+
}
|
|
2704
|
+
};
|
|
2705
|
+
if (checkpointId) {
|
|
2706
|
+
runnableConfig.configurable.checkpoint_id = checkpointId;
|
|
2707
|
+
}
|
|
2708
|
+
if (checkpointNamespace) {
|
|
2709
|
+
runnableConfig.configurable.checkpoint_ns = checkpointNamespace;
|
|
2710
|
+
}
|
|
2711
|
+
if (metadata) {
|
|
2712
|
+
runnableConfig.metadata = metadata;
|
|
2713
|
+
}
|
|
2714
|
+
return runnableConfig;
|
|
2715
|
+
}
|
|
2716
|
+
function createConversationConfig(config) {
|
|
2717
|
+
const { userId, sessionId, metadata = {} } = config;
|
|
2718
|
+
const threadId = sessionId ? generateThreadId(`${userId}-${sessionId}`) : generateThreadId(userId);
|
|
2719
|
+
return createThreadConfig({
|
|
2720
|
+
threadId,
|
|
2721
|
+
metadata: {
|
|
2722
|
+
...metadata,
|
|
2723
|
+
userId,
|
|
2724
|
+
sessionId
|
|
2725
|
+
}
|
|
2726
|
+
});
|
|
2727
|
+
}
|
|
2728
|
+
|
|
2729
|
+
// src/langgraph/persistence/utils.ts
|
|
2730
|
+
async function getCheckpointHistory(checkpointer, options) {
|
|
2731
|
+
const { threadId, limit = 10, before } = options;
|
|
2732
|
+
const config = {
|
|
2733
|
+
configurable: {
|
|
2734
|
+
thread_id: threadId
|
|
2735
|
+
}
|
|
2736
|
+
};
|
|
2737
|
+
if (before) {
|
|
2738
|
+
config.configurable.checkpoint_id = before;
|
|
2739
|
+
}
|
|
2740
|
+
const checkpoints = [];
|
|
2741
|
+
for await (const checkpoint of checkpointer.list(config, { limit })) {
|
|
2742
|
+
checkpoints.push(checkpoint);
|
|
2743
|
+
}
|
|
2744
|
+
return checkpoints;
|
|
2745
|
+
}
|
|
2746
|
+
async function getLatestCheckpoint(checkpointer, options) {
|
|
2747
|
+
const { threadId } = options;
|
|
2748
|
+
const config = {
|
|
2749
|
+
configurable: {
|
|
2750
|
+
thread_id: threadId
|
|
2751
|
+
}
|
|
2752
|
+
};
|
|
2753
|
+
const tuple = await checkpointer.getTuple(config);
|
|
2754
|
+
return tuple || null;
|
|
2755
|
+
}
|
|
2756
|
+
async function clearThread(checkpointer, options) {
|
|
2757
|
+
const { threadId } = options;
|
|
2758
|
+
const checkpoints = await getCheckpointHistory(checkpointer, {
|
|
2759
|
+
threadId,
|
|
2760
|
+
limit: 1e3
|
|
2761
|
+
// Get all checkpoints
|
|
2762
|
+
});
|
|
2763
|
+
if (checkpoints.length > 0) {
|
|
2764
|
+
throw new Error(
|
|
2765
|
+
"Clearing threads is not supported by the current checkpointer implementation. Consider using a new thread ID instead."
|
|
2766
|
+
);
|
|
2767
|
+
}
|
|
2768
|
+
}
|
|
2769
|
+
|
|
2770
|
+
// src/langgraph/observability/errors.ts
|
|
2771
|
+
var AgentError = class _AgentError extends Error {
|
|
2772
|
+
code;
|
|
2773
|
+
node;
|
|
2774
|
+
state;
|
|
2775
|
+
metadata;
|
|
2776
|
+
cause;
|
|
2777
|
+
timestamp;
|
|
2778
|
+
constructor(message, context = {}) {
|
|
2779
|
+
super(message);
|
|
2780
|
+
this.name = "AgentError";
|
|
2781
|
+
this.code = context.code;
|
|
2782
|
+
this.node = context.node;
|
|
2783
|
+
this.state = context.state;
|
|
2784
|
+
this.metadata = context.metadata;
|
|
2785
|
+
this.cause = context.cause;
|
|
2786
|
+
this.timestamp = Date.now();
|
|
2787
|
+
if (Error.captureStackTrace) {
|
|
2788
|
+
Error.captureStackTrace(this, _AgentError);
|
|
2789
|
+
}
|
|
2790
|
+
}
|
|
2791
|
+
/**
|
|
2792
|
+
* Convert error to JSON for logging/reporting
|
|
2793
|
+
*/
|
|
2794
|
+
toJSON() {
|
|
2795
|
+
return {
|
|
2796
|
+
name: this.name,
|
|
2797
|
+
message: this.message,
|
|
2798
|
+
code: this.code,
|
|
2799
|
+
node: this.node,
|
|
2800
|
+
state: this.state,
|
|
2801
|
+
metadata: this.metadata,
|
|
2802
|
+
timestamp: this.timestamp,
|
|
2803
|
+
stack: this.stack,
|
|
2804
|
+
cause: this.cause ? {
|
|
2805
|
+
name: this.cause.name,
|
|
2806
|
+
message: this.cause.message,
|
|
2807
|
+
stack: this.cause.stack
|
|
2808
|
+
} : void 0
|
|
2809
|
+
};
|
|
2810
|
+
}
|
|
2811
|
+
/**
|
|
2812
|
+
* Get a human-readable string representation
|
|
2813
|
+
*/
|
|
2814
|
+
toString() {
|
|
2815
|
+
const parts = [`${this.name}: ${this.message}`];
|
|
2816
|
+
if (this.code) {
|
|
2817
|
+
parts.push(`Code: ${this.code}`);
|
|
2818
|
+
}
|
|
2819
|
+
if (this.node) {
|
|
2820
|
+
parts.push(`Node: ${this.node}`);
|
|
2821
|
+
}
|
|
2822
|
+
if (this.cause) {
|
|
2823
|
+
parts.push(`Caused by: ${this.cause.message}`);
|
|
2824
|
+
}
|
|
2825
|
+
return parts.join("\n");
|
|
2826
|
+
}
|
|
2827
|
+
};
|
|
2828
|
+
var ErrorReporterImpl = class {
|
|
2829
|
+
options;
|
|
2830
|
+
constructor(options) {
|
|
2831
|
+
this.options = {
|
|
2832
|
+
onError: options.onError,
|
|
2833
|
+
includeStackTrace: options.includeStackTrace ?? true,
|
|
2834
|
+
includeState: options.includeState ?? false,
|
|
2835
|
+
rethrow: options.rethrow ?? true
|
|
2836
|
+
};
|
|
2837
|
+
}
|
|
2838
|
+
wrap(node, nodeName) {
|
|
2839
|
+
return async (state) => {
|
|
2840
|
+
try {
|
|
2841
|
+
return await Promise.resolve(node(state));
|
|
2842
|
+
} catch (error) {
|
|
2843
|
+
const agentError = this.toAgentError(error, {
|
|
2844
|
+
node: nodeName,
|
|
2845
|
+
state: this.options.includeState ? state : void 0
|
|
2846
|
+
});
|
|
2847
|
+
await this.report(agentError);
|
|
2848
|
+
if (this.options.rethrow) {
|
|
2849
|
+
throw agentError;
|
|
2850
|
+
}
|
|
2851
|
+
return state;
|
|
2852
|
+
}
|
|
2853
|
+
};
|
|
2854
|
+
}
|
|
2855
|
+
async report(error, context) {
|
|
2856
|
+
const agentError = this.toAgentError(error, context);
|
|
2857
|
+
try {
|
|
2858
|
+
await Promise.resolve(this.options.onError(agentError));
|
|
2859
|
+
} catch (reportError) {
|
|
2860
|
+
console.error("Error reporting failed:", reportError);
|
|
2861
|
+
}
|
|
2862
|
+
}
|
|
2863
|
+
toAgentError(error, context) {
|
|
2864
|
+
if (error instanceof AgentError) {
|
|
2865
|
+
return error;
|
|
2866
|
+
}
|
|
2867
|
+
return new AgentError(error.message, {
|
|
2868
|
+
...context,
|
|
2869
|
+
cause: error
|
|
2870
|
+
});
|
|
2871
|
+
}
|
|
2872
|
+
};
|
|
2873
|
+
function createErrorReporter(options) {
|
|
2874
|
+
return new ErrorReporterImpl(options);
|
|
2875
|
+
}
|
|
2876
|
+
|
|
2877
2877
|
// src/langgraph/interrupts/utils.ts
|
|
2878
2878
|
function createHumanRequestInterrupt(request) {
|
|
2879
2879
|
return {
|