@agentforge/core 0.8.2 → 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.cjs
CHANGED
|
@@ -2290,7 +2290,86 @@ function composeGraphs(parentGraph, subgraph, options) {
|
|
|
2290
2290
|
return parentGraph;
|
|
2291
2291
|
}
|
|
2292
2292
|
|
|
2293
|
-
// src/langgraph/
|
|
2293
|
+
// src/langgraph/middleware/compose.ts
|
|
2294
|
+
function compose(...middleware) {
|
|
2295
|
+
return (node) => {
|
|
2296
|
+
return middleware.reduceRight(
|
|
2297
|
+
(wrappedNode, mw) => mw(wrappedNode),
|
|
2298
|
+
node
|
|
2299
|
+
);
|
|
2300
|
+
};
|
|
2301
|
+
}
|
|
2302
|
+
function composeWithOptions(options, ...middleware) {
|
|
2303
|
+
const { reverse = false, name, catchErrors = true } = options;
|
|
2304
|
+
return (node) => {
|
|
2305
|
+
const orderedMiddleware = reverse ? [...middleware].reverse() : middleware;
|
|
2306
|
+
let wrappedNode = orderedMiddleware.reduceRight(
|
|
2307
|
+
(wrappedNode2, mw) => mw(wrappedNode2),
|
|
2308
|
+
node
|
|
2309
|
+
);
|
|
2310
|
+
if (catchErrors) {
|
|
2311
|
+
const originalNode = wrappedNode;
|
|
2312
|
+
wrappedNode = async (state) => {
|
|
2313
|
+
try {
|
|
2314
|
+
return await Promise.resolve(originalNode(state));
|
|
2315
|
+
} catch (error) {
|
|
2316
|
+
const enhancedError = error instanceof Error ? error : new Error(String(error));
|
|
2317
|
+
if (name) {
|
|
2318
|
+
enhancedError.message = `[${name}] ${enhancedError.message}`;
|
|
2319
|
+
}
|
|
2320
|
+
throw enhancedError;
|
|
2321
|
+
}
|
|
2322
|
+
};
|
|
2323
|
+
}
|
|
2324
|
+
return wrappedNode;
|
|
2325
|
+
};
|
|
2326
|
+
}
|
|
2327
|
+
var MiddlewareChain = class {
|
|
2328
|
+
middleware = [];
|
|
2329
|
+
options = {};
|
|
2330
|
+
/**
|
|
2331
|
+
* Add middleware to the chain.
|
|
2332
|
+
*/
|
|
2333
|
+
use(middleware) {
|
|
2334
|
+
this.middleware.push(middleware);
|
|
2335
|
+
return this;
|
|
2336
|
+
}
|
|
2337
|
+
/**
|
|
2338
|
+
* Set composition options.
|
|
2339
|
+
*/
|
|
2340
|
+
withOptions(options) {
|
|
2341
|
+
this.options = { ...this.options, ...options };
|
|
2342
|
+
return this;
|
|
2343
|
+
}
|
|
2344
|
+
/**
|
|
2345
|
+
* Build the middleware chain and apply it to a node.
|
|
2346
|
+
*/
|
|
2347
|
+
build(node) {
|
|
2348
|
+
if (this.middleware.length === 0) {
|
|
2349
|
+
return node;
|
|
2350
|
+
}
|
|
2351
|
+
return composeWithOptions(this.options, ...this.middleware)(node);
|
|
2352
|
+
}
|
|
2353
|
+
/**
|
|
2354
|
+
* Get the number of middleware in the chain.
|
|
2355
|
+
*/
|
|
2356
|
+
get length() {
|
|
2357
|
+
return this.middleware.length;
|
|
2358
|
+
}
|
|
2359
|
+
};
|
|
2360
|
+
function chain() {
|
|
2361
|
+
return new MiddlewareChain();
|
|
2362
|
+
}
|
|
2363
|
+
function createMiddlewareContext() {
|
|
2364
|
+
return {
|
|
2365
|
+
executionId: `exec_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
|
2366
|
+
startTime: Date.now(),
|
|
2367
|
+
data: {},
|
|
2368
|
+
middlewareStack: []
|
|
2369
|
+
};
|
|
2370
|
+
}
|
|
2371
|
+
|
|
2372
|
+
// src/langgraph/middleware/retry.ts
|
|
2294
2373
|
function calculateDelay(attempt, strategy, initialDelay, maxDelay) {
|
|
2295
2374
|
let delay;
|
|
2296
2375
|
switch (strategy) {
|
|
@@ -2342,7 +2421,7 @@ function withRetry(node, options = {}) {
|
|
|
2342
2421
|
};
|
|
2343
2422
|
}
|
|
2344
2423
|
|
|
2345
|
-
// src/langgraph/
|
|
2424
|
+
// src/langgraph/middleware/error-handler.ts
|
|
2346
2425
|
function withErrorHandler(node, options) {
|
|
2347
2426
|
const { onError, logError, rethrow = false } = options;
|
|
2348
2427
|
return async (state) => {
|
|
@@ -2362,7 +2441,7 @@ function withErrorHandler(node, options) {
|
|
|
2362
2441
|
};
|
|
2363
2442
|
}
|
|
2364
2443
|
|
|
2365
|
-
// src/langgraph/
|
|
2444
|
+
// src/langgraph/middleware/timeout.ts
|
|
2366
2445
|
var TimeoutError = class extends Error {
|
|
2367
2446
|
constructor(timeout2) {
|
|
2368
2447
|
super(`Node execution timed out after ${timeout2}ms`);
|
|
@@ -2394,150 +2473,6 @@ function withTimeout(node, options) {
|
|
|
2394
2473
|
};
|
|
2395
2474
|
}
|
|
2396
2475
|
|
|
2397
|
-
// src/langgraph/persistence/checkpointer.ts
|
|
2398
|
-
var import_langgraph5 = require("@langchain/langgraph");
|
|
2399
|
-
function createMemoryCheckpointer(options) {
|
|
2400
|
-
return new import_langgraph5.MemorySaver();
|
|
2401
|
-
}
|
|
2402
|
-
async function createSqliteCheckpointer(options = {}) {
|
|
2403
|
-
const { path = ":memory:", autoMigrate = true } = options;
|
|
2404
|
-
try {
|
|
2405
|
-
const { SqliteSaver } = await import("@langchain/langgraph-checkpoint-sqlite");
|
|
2406
|
-
const checkpointer = SqliteSaver.fromConnString(path);
|
|
2407
|
-
if (autoMigrate) {
|
|
2408
|
-
await checkpointer.setup();
|
|
2409
|
-
}
|
|
2410
|
-
return checkpointer;
|
|
2411
|
-
} catch (error) {
|
|
2412
|
-
if (error instanceof Error && error.message.includes("Cannot find module")) {
|
|
2413
|
-
throw new Error(
|
|
2414
|
-
"SQLite checkpointer requires @langchain/langgraph-checkpoint-sqlite to be installed. Install it with: npm install @langchain/langgraph-checkpoint-sqlite"
|
|
2415
|
-
);
|
|
2416
|
-
}
|
|
2417
|
-
throw error;
|
|
2418
|
-
}
|
|
2419
|
-
}
|
|
2420
|
-
function isMemoryCheckpointer(checkpointer) {
|
|
2421
|
-
return checkpointer instanceof import_langgraph5.MemorySaver;
|
|
2422
|
-
}
|
|
2423
|
-
|
|
2424
|
-
// src/langgraph/persistence/thread.ts
|
|
2425
|
-
var import_crypto = require("crypto");
|
|
2426
|
-
function generateThreadId(seed) {
|
|
2427
|
-
if (seed && seed.length > 0) {
|
|
2428
|
-
const hash = Buffer.from(seed).toString("base64").replace(/[^a-zA-Z0-9]/g, "");
|
|
2429
|
-
return `thread-${hash}`;
|
|
2430
|
-
}
|
|
2431
|
-
return (0, import_crypto.randomUUID)();
|
|
2432
|
-
}
|
|
2433
|
-
function createThreadConfig(config = {}) {
|
|
2434
|
-
const { threadId = generateThreadId(), checkpointId, checkpointNamespace, metadata } = config;
|
|
2435
|
-
const runnableConfig = {
|
|
2436
|
-
configurable: {
|
|
2437
|
-
thread_id: threadId
|
|
2438
|
-
}
|
|
2439
|
-
};
|
|
2440
|
-
if (checkpointId) {
|
|
2441
|
-
runnableConfig.configurable.checkpoint_id = checkpointId;
|
|
2442
|
-
}
|
|
2443
|
-
if (checkpointNamespace) {
|
|
2444
|
-
runnableConfig.configurable.checkpoint_ns = checkpointNamespace;
|
|
2445
|
-
}
|
|
2446
|
-
if (metadata) {
|
|
2447
|
-
runnableConfig.metadata = metadata;
|
|
2448
|
-
}
|
|
2449
|
-
return runnableConfig;
|
|
2450
|
-
}
|
|
2451
|
-
function createConversationConfig(config) {
|
|
2452
|
-
const { userId, sessionId, metadata = {} } = config;
|
|
2453
|
-
const threadId = sessionId ? generateThreadId(`${userId}-${sessionId}`) : generateThreadId(userId);
|
|
2454
|
-
return createThreadConfig({
|
|
2455
|
-
threadId,
|
|
2456
|
-
metadata: {
|
|
2457
|
-
...metadata,
|
|
2458
|
-
userId,
|
|
2459
|
-
sessionId
|
|
2460
|
-
}
|
|
2461
|
-
});
|
|
2462
|
-
}
|
|
2463
|
-
|
|
2464
|
-
// src/langgraph/persistence/utils.ts
|
|
2465
|
-
async function getCheckpointHistory(checkpointer, options) {
|
|
2466
|
-
const { threadId, limit = 10, before } = options;
|
|
2467
|
-
const config = {
|
|
2468
|
-
configurable: {
|
|
2469
|
-
thread_id: threadId
|
|
2470
|
-
}
|
|
2471
|
-
};
|
|
2472
|
-
if (before) {
|
|
2473
|
-
config.configurable.checkpoint_id = before;
|
|
2474
|
-
}
|
|
2475
|
-
const checkpoints = [];
|
|
2476
|
-
for await (const checkpoint of checkpointer.list(config, { limit })) {
|
|
2477
|
-
checkpoints.push(checkpoint);
|
|
2478
|
-
}
|
|
2479
|
-
return checkpoints;
|
|
2480
|
-
}
|
|
2481
|
-
async function getLatestCheckpoint(checkpointer, options) {
|
|
2482
|
-
const { threadId } = options;
|
|
2483
|
-
const config = {
|
|
2484
|
-
configurable: {
|
|
2485
|
-
thread_id: threadId
|
|
2486
|
-
}
|
|
2487
|
-
};
|
|
2488
|
-
const tuple = await checkpointer.getTuple(config);
|
|
2489
|
-
return tuple || null;
|
|
2490
|
-
}
|
|
2491
|
-
async function clearThread(checkpointer, options) {
|
|
2492
|
-
const { threadId } = options;
|
|
2493
|
-
const checkpoints = await getCheckpointHistory(checkpointer, {
|
|
2494
|
-
threadId,
|
|
2495
|
-
limit: 1e3
|
|
2496
|
-
// Get all checkpoints
|
|
2497
|
-
});
|
|
2498
|
-
if (checkpoints.length > 0) {
|
|
2499
|
-
throw new Error(
|
|
2500
|
-
"Clearing threads is not supported by the current checkpointer implementation. Consider using a new thread ID instead."
|
|
2501
|
-
);
|
|
2502
|
-
}
|
|
2503
|
-
}
|
|
2504
|
-
|
|
2505
|
-
// src/langgraph/observability/langsmith.ts
|
|
2506
|
-
var globalConfig = null;
|
|
2507
|
-
function configureLangSmith(config) {
|
|
2508
|
-
globalConfig = config;
|
|
2509
|
-
if (config.apiKey) {
|
|
2510
|
-
process.env.LANGSMITH_API_KEY = config.apiKey;
|
|
2511
|
-
}
|
|
2512
|
-
if (config.projectName) {
|
|
2513
|
-
process.env.LANGSMITH_PROJECT = config.projectName;
|
|
2514
|
-
}
|
|
2515
|
-
if (config.tracingEnabled !== void 0) {
|
|
2516
|
-
process.env.LANGSMITH_TRACING = config.tracingEnabled ? "true" : "false";
|
|
2517
|
-
} else if (config.apiKey) {
|
|
2518
|
-
process.env.LANGSMITH_TRACING = "true";
|
|
2519
|
-
}
|
|
2520
|
-
if (config.endpoint) {
|
|
2521
|
-
process.env.LANGSMITH_ENDPOINT = config.endpoint;
|
|
2522
|
-
}
|
|
2523
|
-
}
|
|
2524
|
-
function getLangSmithConfig() {
|
|
2525
|
-
return globalConfig;
|
|
2526
|
-
}
|
|
2527
|
-
function isTracingEnabled() {
|
|
2528
|
-
return process.env.LANGSMITH_TRACING === "true";
|
|
2529
|
-
}
|
|
2530
|
-
function withTracing(node, options) {
|
|
2531
|
-
const { name, metadata, tags, runName } = options;
|
|
2532
|
-
return async (state) => {
|
|
2533
|
-
if (isTracingEnabled()) {
|
|
2534
|
-
const result = await Promise.resolve(node(state));
|
|
2535
|
-
return result;
|
|
2536
|
-
}
|
|
2537
|
-
return await Promise.resolve(node(state));
|
|
2538
|
-
};
|
|
2539
|
-
}
|
|
2540
|
-
|
|
2541
2476
|
// src/langgraph/observability/metrics.ts
|
|
2542
2477
|
var MetricType = /* @__PURE__ */ ((MetricType2) => {
|
|
2543
2478
|
MetricType2["COUNTER"] = "counter";
|
|
@@ -2646,189 +2581,39 @@ function withMetrics(node, options) {
|
|
|
2646
2581
|
};
|
|
2647
2582
|
}
|
|
2648
2583
|
|
|
2649
|
-
// src/langgraph/observability/
|
|
2650
|
-
var
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
cause;
|
|
2656
|
-
timestamp;
|
|
2657
|
-
constructor(message, context = {}) {
|
|
2658
|
-
super(message);
|
|
2659
|
-
this.name = "AgentError";
|
|
2660
|
-
this.code = context.code;
|
|
2661
|
-
this.node = context.node;
|
|
2662
|
-
this.state = context.state;
|
|
2663
|
-
this.metadata = context.metadata;
|
|
2664
|
-
this.cause = context.cause;
|
|
2665
|
-
this.timestamp = Date.now();
|
|
2666
|
-
if (Error.captureStackTrace) {
|
|
2667
|
-
Error.captureStackTrace(this, _AgentError);
|
|
2668
|
-
}
|
|
2584
|
+
// src/langgraph/observability/langsmith.ts
|
|
2585
|
+
var globalConfig = null;
|
|
2586
|
+
function configureLangSmith(config) {
|
|
2587
|
+
globalConfig = config;
|
|
2588
|
+
if (config.apiKey) {
|
|
2589
|
+
process.env.LANGSMITH_API_KEY = config.apiKey;
|
|
2669
2590
|
}
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
*/
|
|
2673
|
-
toJSON() {
|
|
2674
|
-
return {
|
|
2675
|
-
name: this.name,
|
|
2676
|
-
message: this.message,
|
|
2677
|
-
code: this.code,
|
|
2678
|
-
node: this.node,
|
|
2679
|
-
state: this.state,
|
|
2680
|
-
metadata: this.metadata,
|
|
2681
|
-
timestamp: this.timestamp,
|
|
2682
|
-
stack: this.stack,
|
|
2683
|
-
cause: this.cause ? {
|
|
2684
|
-
name: this.cause.name,
|
|
2685
|
-
message: this.cause.message,
|
|
2686
|
-
stack: this.cause.stack
|
|
2687
|
-
} : void 0
|
|
2688
|
-
};
|
|
2689
|
-
}
|
|
2690
|
-
/**
|
|
2691
|
-
* Get a human-readable string representation
|
|
2692
|
-
*/
|
|
2693
|
-
toString() {
|
|
2694
|
-
const parts = [`${this.name}: ${this.message}`];
|
|
2695
|
-
if (this.code) {
|
|
2696
|
-
parts.push(`Code: ${this.code}`);
|
|
2697
|
-
}
|
|
2698
|
-
if (this.node) {
|
|
2699
|
-
parts.push(`Node: ${this.node}`);
|
|
2700
|
-
}
|
|
2701
|
-
if (this.cause) {
|
|
2702
|
-
parts.push(`Caused by: ${this.cause.message}`);
|
|
2703
|
-
}
|
|
2704
|
-
return parts.join("\n");
|
|
2705
|
-
}
|
|
2706
|
-
};
|
|
2707
|
-
var ErrorReporterImpl = class {
|
|
2708
|
-
options;
|
|
2709
|
-
constructor(options) {
|
|
2710
|
-
this.options = {
|
|
2711
|
-
onError: options.onError,
|
|
2712
|
-
includeStackTrace: options.includeStackTrace ?? true,
|
|
2713
|
-
includeState: options.includeState ?? false,
|
|
2714
|
-
rethrow: options.rethrow ?? true
|
|
2715
|
-
};
|
|
2716
|
-
}
|
|
2717
|
-
wrap(node, nodeName) {
|
|
2718
|
-
return async (state) => {
|
|
2719
|
-
try {
|
|
2720
|
-
return await Promise.resolve(node(state));
|
|
2721
|
-
} catch (error) {
|
|
2722
|
-
const agentError = this.toAgentError(error, {
|
|
2723
|
-
node: nodeName,
|
|
2724
|
-
state: this.options.includeState ? state : void 0
|
|
2725
|
-
});
|
|
2726
|
-
await this.report(agentError);
|
|
2727
|
-
if (this.options.rethrow) {
|
|
2728
|
-
throw agentError;
|
|
2729
|
-
}
|
|
2730
|
-
return state;
|
|
2731
|
-
}
|
|
2732
|
-
};
|
|
2591
|
+
if (config.projectName) {
|
|
2592
|
+
process.env.LANGSMITH_PROJECT = config.projectName;
|
|
2733
2593
|
}
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
} catch (reportError) {
|
|
2739
|
-
console.error("Error reporting failed:", reportError);
|
|
2740
|
-
}
|
|
2594
|
+
if (config.tracingEnabled !== void 0) {
|
|
2595
|
+
process.env.LANGSMITH_TRACING = config.tracingEnabled ? "true" : "false";
|
|
2596
|
+
} else if (config.apiKey) {
|
|
2597
|
+
process.env.LANGSMITH_TRACING = "true";
|
|
2741
2598
|
}
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
return error;
|
|
2745
|
-
}
|
|
2746
|
-
return new AgentError(error.message, {
|
|
2747
|
-
...context,
|
|
2748
|
-
cause: error
|
|
2749
|
-
});
|
|
2599
|
+
if (config.endpoint) {
|
|
2600
|
+
process.env.LANGSMITH_ENDPOINT = config.endpoint;
|
|
2750
2601
|
}
|
|
2751
|
-
};
|
|
2752
|
-
function createErrorReporter(options) {
|
|
2753
|
-
return new ErrorReporterImpl(options);
|
|
2754
2602
|
}
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
function compose(...middleware) {
|
|
2758
|
-
return (node) => {
|
|
2759
|
-
return middleware.reduceRight(
|
|
2760
|
-
(wrappedNode, mw) => mw(wrappedNode),
|
|
2761
|
-
node
|
|
2762
|
-
);
|
|
2763
|
-
};
|
|
2603
|
+
function getLangSmithConfig() {
|
|
2604
|
+
return globalConfig;
|
|
2764
2605
|
}
|
|
2765
|
-
function
|
|
2766
|
-
|
|
2767
|
-
return (node) => {
|
|
2768
|
-
const orderedMiddleware = reverse ? [...middleware].reverse() : middleware;
|
|
2769
|
-
let wrappedNode = orderedMiddleware.reduceRight(
|
|
2770
|
-
(wrappedNode2, mw) => mw(wrappedNode2),
|
|
2771
|
-
node
|
|
2772
|
-
);
|
|
2773
|
-
if (catchErrors) {
|
|
2774
|
-
const originalNode = wrappedNode;
|
|
2775
|
-
wrappedNode = async (state) => {
|
|
2776
|
-
try {
|
|
2777
|
-
return await Promise.resolve(originalNode(state));
|
|
2778
|
-
} catch (error) {
|
|
2779
|
-
const enhancedError = error instanceof Error ? error : new Error(String(error));
|
|
2780
|
-
if (name) {
|
|
2781
|
-
enhancedError.message = `[${name}] ${enhancedError.message}`;
|
|
2782
|
-
}
|
|
2783
|
-
throw enhancedError;
|
|
2784
|
-
}
|
|
2785
|
-
};
|
|
2786
|
-
}
|
|
2787
|
-
return wrappedNode;
|
|
2788
|
-
};
|
|
2606
|
+
function isTracingEnabled() {
|
|
2607
|
+
return process.env.LANGSMITH_TRACING === "true";
|
|
2789
2608
|
}
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
use(middleware) {
|
|
2797
|
-
this.middleware.push(middleware);
|
|
2798
|
-
return this;
|
|
2799
|
-
}
|
|
2800
|
-
/**
|
|
2801
|
-
* Set composition options.
|
|
2802
|
-
*/
|
|
2803
|
-
withOptions(options) {
|
|
2804
|
-
this.options = { ...this.options, ...options };
|
|
2805
|
-
return this;
|
|
2806
|
-
}
|
|
2807
|
-
/**
|
|
2808
|
-
* Build the middleware chain and apply it to a node.
|
|
2809
|
-
*/
|
|
2810
|
-
build(node) {
|
|
2811
|
-
if (this.middleware.length === 0) {
|
|
2812
|
-
return node;
|
|
2609
|
+
function withTracing(node, options) {
|
|
2610
|
+
const { name, metadata, tags, runName } = options;
|
|
2611
|
+
return async (state) => {
|
|
2612
|
+
if (isTracingEnabled()) {
|
|
2613
|
+
const result = await Promise.resolve(node(state));
|
|
2614
|
+
return result;
|
|
2813
2615
|
}
|
|
2814
|
-
return
|
|
2815
|
-
}
|
|
2816
|
-
/**
|
|
2817
|
-
* Get the number of middleware in the chain.
|
|
2818
|
-
*/
|
|
2819
|
-
get length() {
|
|
2820
|
-
return this.middleware.length;
|
|
2821
|
-
}
|
|
2822
|
-
};
|
|
2823
|
-
function chain() {
|
|
2824
|
-
return new MiddlewareChain();
|
|
2825
|
-
}
|
|
2826
|
-
function createMiddlewareContext() {
|
|
2827
|
-
return {
|
|
2828
|
-
executionId: `exec_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
|
2829
|
-
startTime: Date.now(),
|
|
2830
|
-
data: {},
|
|
2831
|
-
middlewareStack: []
|
|
2616
|
+
return await Promise.resolve(node(state));
|
|
2832
2617
|
};
|
|
2833
2618
|
}
|
|
2834
2619
|
|
|
@@ -3030,6 +2815,221 @@ var presets = {
|
|
|
3030
2815
|
testing
|
|
3031
2816
|
};
|
|
3032
2817
|
|
|
2818
|
+
// src/langgraph/persistence/checkpointer.ts
|
|
2819
|
+
var import_langgraph5 = require("@langchain/langgraph");
|
|
2820
|
+
function createMemoryCheckpointer(options) {
|
|
2821
|
+
return new import_langgraph5.MemorySaver();
|
|
2822
|
+
}
|
|
2823
|
+
async function createSqliteCheckpointer(options = {}) {
|
|
2824
|
+
const { path = ":memory:", autoMigrate = true } = options;
|
|
2825
|
+
try {
|
|
2826
|
+
const { SqliteSaver } = await import("@langchain/langgraph-checkpoint-sqlite");
|
|
2827
|
+
const checkpointer = SqliteSaver.fromConnString(path);
|
|
2828
|
+
if (autoMigrate) {
|
|
2829
|
+
await checkpointer.setup();
|
|
2830
|
+
}
|
|
2831
|
+
return checkpointer;
|
|
2832
|
+
} catch (error) {
|
|
2833
|
+
if (error instanceof Error && error.message.includes("Cannot find module")) {
|
|
2834
|
+
throw new Error(
|
|
2835
|
+
"SQLite checkpointer requires @langchain/langgraph-checkpoint-sqlite to be installed. Install it with: npm install @langchain/langgraph-checkpoint-sqlite"
|
|
2836
|
+
);
|
|
2837
|
+
}
|
|
2838
|
+
throw error;
|
|
2839
|
+
}
|
|
2840
|
+
}
|
|
2841
|
+
function isMemoryCheckpointer(checkpointer) {
|
|
2842
|
+
return checkpointer instanceof import_langgraph5.MemorySaver;
|
|
2843
|
+
}
|
|
2844
|
+
|
|
2845
|
+
// src/langgraph/persistence/thread.ts
|
|
2846
|
+
var import_crypto = require("crypto");
|
|
2847
|
+
function generateThreadId(seed) {
|
|
2848
|
+
if (seed && seed.length > 0) {
|
|
2849
|
+
const hash = Buffer.from(seed).toString("base64").replace(/[^a-zA-Z0-9]/g, "");
|
|
2850
|
+
return `thread-${hash}`;
|
|
2851
|
+
}
|
|
2852
|
+
return (0, import_crypto.randomUUID)();
|
|
2853
|
+
}
|
|
2854
|
+
function createThreadConfig(config = {}) {
|
|
2855
|
+
const { threadId = generateThreadId(), checkpointId, checkpointNamespace, metadata } = config;
|
|
2856
|
+
const runnableConfig = {
|
|
2857
|
+
configurable: {
|
|
2858
|
+
thread_id: threadId
|
|
2859
|
+
}
|
|
2860
|
+
};
|
|
2861
|
+
if (checkpointId) {
|
|
2862
|
+
runnableConfig.configurable.checkpoint_id = checkpointId;
|
|
2863
|
+
}
|
|
2864
|
+
if (checkpointNamespace) {
|
|
2865
|
+
runnableConfig.configurable.checkpoint_ns = checkpointNamespace;
|
|
2866
|
+
}
|
|
2867
|
+
if (metadata) {
|
|
2868
|
+
runnableConfig.metadata = metadata;
|
|
2869
|
+
}
|
|
2870
|
+
return runnableConfig;
|
|
2871
|
+
}
|
|
2872
|
+
function createConversationConfig(config) {
|
|
2873
|
+
const { userId, sessionId, metadata = {} } = config;
|
|
2874
|
+
const threadId = sessionId ? generateThreadId(`${userId}-${sessionId}`) : generateThreadId(userId);
|
|
2875
|
+
return createThreadConfig({
|
|
2876
|
+
threadId,
|
|
2877
|
+
metadata: {
|
|
2878
|
+
...metadata,
|
|
2879
|
+
userId,
|
|
2880
|
+
sessionId
|
|
2881
|
+
}
|
|
2882
|
+
});
|
|
2883
|
+
}
|
|
2884
|
+
|
|
2885
|
+
// src/langgraph/persistence/utils.ts
|
|
2886
|
+
async function getCheckpointHistory(checkpointer, options) {
|
|
2887
|
+
const { threadId, limit = 10, before } = options;
|
|
2888
|
+
const config = {
|
|
2889
|
+
configurable: {
|
|
2890
|
+
thread_id: threadId
|
|
2891
|
+
}
|
|
2892
|
+
};
|
|
2893
|
+
if (before) {
|
|
2894
|
+
config.configurable.checkpoint_id = before;
|
|
2895
|
+
}
|
|
2896
|
+
const checkpoints = [];
|
|
2897
|
+
for await (const checkpoint of checkpointer.list(config, { limit })) {
|
|
2898
|
+
checkpoints.push(checkpoint);
|
|
2899
|
+
}
|
|
2900
|
+
return checkpoints;
|
|
2901
|
+
}
|
|
2902
|
+
async function getLatestCheckpoint(checkpointer, options) {
|
|
2903
|
+
const { threadId } = options;
|
|
2904
|
+
const config = {
|
|
2905
|
+
configurable: {
|
|
2906
|
+
thread_id: threadId
|
|
2907
|
+
}
|
|
2908
|
+
};
|
|
2909
|
+
const tuple = await checkpointer.getTuple(config);
|
|
2910
|
+
return tuple || null;
|
|
2911
|
+
}
|
|
2912
|
+
async function clearThread(checkpointer, options) {
|
|
2913
|
+
const { threadId } = options;
|
|
2914
|
+
const checkpoints = await getCheckpointHistory(checkpointer, {
|
|
2915
|
+
threadId,
|
|
2916
|
+
limit: 1e3
|
|
2917
|
+
// Get all checkpoints
|
|
2918
|
+
});
|
|
2919
|
+
if (checkpoints.length > 0) {
|
|
2920
|
+
throw new Error(
|
|
2921
|
+
"Clearing threads is not supported by the current checkpointer implementation. Consider using a new thread ID instead."
|
|
2922
|
+
);
|
|
2923
|
+
}
|
|
2924
|
+
}
|
|
2925
|
+
|
|
2926
|
+
// src/langgraph/observability/errors.ts
|
|
2927
|
+
var AgentError = class _AgentError extends Error {
|
|
2928
|
+
code;
|
|
2929
|
+
node;
|
|
2930
|
+
state;
|
|
2931
|
+
metadata;
|
|
2932
|
+
cause;
|
|
2933
|
+
timestamp;
|
|
2934
|
+
constructor(message, context = {}) {
|
|
2935
|
+
super(message);
|
|
2936
|
+
this.name = "AgentError";
|
|
2937
|
+
this.code = context.code;
|
|
2938
|
+
this.node = context.node;
|
|
2939
|
+
this.state = context.state;
|
|
2940
|
+
this.metadata = context.metadata;
|
|
2941
|
+
this.cause = context.cause;
|
|
2942
|
+
this.timestamp = Date.now();
|
|
2943
|
+
if (Error.captureStackTrace) {
|
|
2944
|
+
Error.captureStackTrace(this, _AgentError);
|
|
2945
|
+
}
|
|
2946
|
+
}
|
|
2947
|
+
/**
|
|
2948
|
+
* Convert error to JSON for logging/reporting
|
|
2949
|
+
*/
|
|
2950
|
+
toJSON() {
|
|
2951
|
+
return {
|
|
2952
|
+
name: this.name,
|
|
2953
|
+
message: this.message,
|
|
2954
|
+
code: this.code,
|
|
2955
|
+
node: this.node,
|
|
2956
|
+
state: this.state,
|
|
2957
|
+
metadata: this.metadata,
|
|
2958
|
+
timestamp: this.timestamp,
|
|
2959
|
+
stack: this.stack,
|
|
2960
|
+
cause: this.cause ? {
|
|
2961
|
+
name: this.cause.name,
|
|
2962
|
+
message: this.cause.message,
|
|
2963
|
+
stack: this.cause.stack
|
|
2964
|
+
} : void 0
|
|
2965
|
+
};
|
|
2966
|
+
}
|
|
2967
|
+
/**
|
|
2968
|
+
* Get a human-readable string representation
|
|
2969
|
+
*/
|
|
2970
|
+
toString() {
|
|
2971
|
+
const parts = [`${this.name}: ${this.message}`];
|
|
2972
|
+
if (this.code) {
|
|
2973
|
+
parts.push(`Code: ${this.code}`);
|
|
2974
|
+
}
|
|
2975
|
+
if (this.node) {
|
|
2976
|
+
parts.push(`Node: ${this.node}`);
|
|
2977
|
+
}
|
|
2978
|
+
if (this.cause) {
|
|
2979
|
+
parts.push(`Caused by: ${this.cause.message}`);
|
|
2980
|
+
}
|
|
2981
|
+
return parts.join("\n");
|
|
2982
|
+
}
|
|
2983
|
+
};
|
|
2984
|
+
var ErrorReporterImpl = class {
|
|
2985
|
+
options;
|
|
2986
|
+
constructor(options) {
|
|
2987
|
+
this.options = {
|
|
2988
|
+
onError: options.onError,
|
|
2989
|
+
includeStackTrace: options.includeStackTrace ?? true,
|
|
2990
|
+
includeState: options.includeState ?? false,
|
|
2991
|
+
rethrow: options.rethrow ?? true
|
|
2992
|
+
};
|
|
2993
|
+
}
|
|
2994
|
+
wrap(node, nodeName) {
|
|
2995
|
+
return async (state) => {
|
|
2996
|
+
try {
|
|
2997
|
+
return await Promise.resolve(node(state));
|
|
2998
|
+
} catch (error) {
|
|
2999
|
+
const agentError = this.toAgentError(error, {
|
|
3000
|
+
node: nodeName,
|
|
3001
|
+
state: this.options.includeState ? state : void 0
|
|
3002
|
+
});
|
|
3003
|
+
await this.report(agentError);
|
|
3004
|
+
if (this.options.rethrow) {
|
|
3005
|
+
throw agentError;
|
|
3006
|
+
}
|
|
3007
|
+
return state;
|
|
3008
|
+
}
|
|
3009
|
+
};
|
|
3010
|
+
}
|
|
3011
|
+
async report(error, context) {
|
|
3012
|
+
const agentError = this.toAgentError(error, context);
|
|
3013
|
+
try {
|
|
3014
|
+
await Promise.resolve(this.options.onError(agentError));
|
|
3015
|
+
} catch (reportError) {
|
|
3016
|
+
console.error("Error reporting failed:", reportError);
|
|
3017
|
+
}
|
|
3018
|
+
}
|
|
3019
|
+
toAgentError(error, context) {
|
|
3020
|
+
if (error instanceof AgentError) {
|
|
3021
|
+
return error;
|
|
3022
|
+
}
|
|
3023
|
+
return new AgentError(error.message, {
|
|
3024
|
+
...context,
|
|
3025
|
+
cause: error
|
|
3026
|
+
});
|
|
3027
|
+
}
|
|
3028
|
+
};
|
|
3029
|
+
function createErrorReporter(options) {
|
|
3030
|
+
return new ErrorReporterImpl(options);
|
|
3031
|
+
}
|
|
3032
|
+
|
|
3033
3033
|
// src/langgraph/interrupts/utils.ts
|
|
3034
3034
|
function createHumanRequestInterrupt(request) {
|
|
3035
3035
|
return {
|