checkpoint.ts
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import path from 'node:path';
import fs from 'node:fs';
import { SqliteSaver } from '@langchain/langgraph-checkpoint-sqlite';
import { logger } from './logger';
import { promptUserChoice } from '../cli/prompts';
/**
* Check if a checkpoint exists for the given thread_id
*/
async function checkpointExists(checkpointer: SqliteSaver, threadId: string): Promise<boolean> {
try {
const checkpoints = checkpointer.list({ configurable: { thread_id: threadId } }, { limit: 1 });
const firstCheckpoint = await checkpoints.next();
return !firstCheckpoint.done && firstCheckpoint.value !== undefined;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
logger.printTestLog(`Error checking checkpoint: ${errorMessage}`);
return false;
}
}
/**
* Clear checkpoint for the given thread_id
*/
async function clearCheckpoint(checkpointer: SqliteSaver, threadId: string): Promise<void> {
try {
// Delete all checkpoints for this thread_id
await checkpointer.deleteThread(threadId);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
logger.printWarnLog(`Failed to clear checkpoint: ${errorMessage}`);
}
}
/**
* Check and prompt user for checkpoint decision
* Returns true if user wants to resume, false if start fresh, undefined if no checkpoint exists
*/
export async function promptCheckpointChoice(checkpointer: SqliteSaver, threadId: string): Promise<boolean | undefined> {
const hasCheckpoint = await checkpointExists(checkpointer, threadId);
if (!hasCheckpoint) {
return undefined;
}
const choice = await promptUserChoice();
return choice === 'resume';
}
/**
* Clear checkpoint for the given thread_id (exported for external use)
*/
export async function clearThreadCheckpoint(checkpointer: SqliteSaver, threadId: string): Promise<void> {
await clearCheckpoint(checkpointer, threadId);
}
/**
* Initialize SqliteSaver with the database path
*/
export function initializeSqliteSaver(dbPath: string): SqliteSaver {
// Ensure the directory exists
const dbDir = path.dirname(dbPath);
if (!fs.existsSync(dbDir)) {
fs.mkdirSync(dbDir, { recursive: true });
}
// SqliteSaver.fromConnString already sets up the database
const checkpointer = SqliteSaver.fromConnString(dbPath);
return checkpointer;
}