|
| 1 | +import { VercelKV, createClient } from "@vercel/kv"; |
| 2 | + |
| 3 | +import { RunnableConfig } from "@langchain/core/runnables"; |
| 4 | +import { |
| 5 | + BaseCheckpointSaver, |
| 6 | + Checkpoint, |
| 7 | + CheckpointMetadata, |
| 8 | + CheckpointTuple, |
| 9 | + SerializerProtocol, |
| 10 | +} from "@langchain/langgraph/web"; |
| 11 | + |
| 12 | +// snake_case is used to match Python implementation |
| 13 | +interface KVRow { |
| 14 | + checkpoint: string; |
| 15 | + metadata: string; |
| 16 | +} |
| 17 | + |
| 18 | +interface KVConfig { |
| 19 | + url: string; |
| 20 | + token: string; |
| 21 | +} |
| 22 | + |
| 23 | +export class VercelKVSaver extends BaseCheckpointSaver { |
| 24 | + private kv: VercelKV; |
| 25 | + |
| 26 | + constructor(config: KVConfig, serde?: SerializerProtocol<unknown>) { |
| 27 | + super(serde); |
| 28 | + this.kv = createClient(config); |
| 29 | + } |
| 30 | + |
| 31 | + async getTuple(config: RunnableConfig): Promise<CheckpointTuple | undefined> { |
| 32 | + const thread_id = config.configurable?.thread_id; |
| 33 | + const checkpoint_id = config.configurable?.checkpoint_id; |
| 34 | + |
| 35 | + if (!thread_id) { |
| 36 | + return undefined; |
| 37 | + } |
| 38 | + |
| 39 | + const key = checkpoint_id |
| 40 | + ? `${thread_id}:${checkpoint_id}` |
| 41 | + : `${thread_id}:last`; |
| 42 | + |
| 43 | + const row: KVRow | null = await this.kv.get(key); |
| 44 | + |
| 45 | + if (!row) { |
| 46 | + return undefined; |
| 47 | + } |
| 48 | + |
| 49 | + const [checkpoint, metadata] = await Promise.all([ |
| 50 | + this.serde.parse(row.checkpoint), |
| 51 | + this.serde.parse(row.metadata), |
| 52 | + ]); |
| 53 | + |
| 54 | + return { |
| 55 | + checkpoint: checkpoint as Checkpoint, |
| 56 | + metadata: metadata as CheckpointMetadata, |
| 57 | + config: checkpoint_id |
| 58 | + ? config |
| 59 | + : { |
| 60 | + configurable: { |
| 61 | + thread_id, |
| 62 | + checkpoint_id: (checkpoint as Checkpoint).id, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + async *list( |
| 69 | + config: RunnableConfig, |
| 70 | + limit?: number, |
| 71 | + before?: RunnableConfig |
| 72 | + ): AsyncGenerator<CheckpointTuple> { |
| 73 | + const thread_id: string = config.configurable?.thread_id; |
| 74 | + |
| 75 | + // LUA script to get keys excluding those starting with "last" |
| 76 | + const luaScript = ` |
| 77 | + local prefix = ARGV[1] |
| 78 | + local cursor = '0' |
| 79 | + local result = {} |
| 80 | + repeat |
| 81 | + local scanResult = redis.call('SCAN', cursor, 'MATCH', prefix .. '*', 'COUNT', 1000) |
| 82 | + cursor = scanResult[1] |
| 83 | + local keys = scanResult[2] |
| 84 | + for _, key in ipairs(keys) do |
| 85 | + if key:sub(-5) ~= ':last' then |
| 86 | + table.insert(result, key) |
| 87 | + end |
| 88 | + end |
| 89 | + until cursor == '0' |
| 90 | + return result |
| 91 | + `; |
| 92 | + |
| 93 | + // Execute the LUA script with the thread_id as an argument |
| 94 | + const keys: string[] = await this.kv.eval(luaScript, [], [thread_id]); |
| 95 | + |
| 96 | + const filteredKeys = keys.filter((key: string) => { |
| 97 | + const [, checkpoint_id] = key.split(":"); |
| 98 | + |
| 99 | + return !before || checkpoint_id < before?.configurable?.checkpoint_id; |
| 100 | + }); |
| 101 | + |
| 102 | + const sortedKeys = filteredKeys |
| 103 | + .sort((a: string, b: string) => b.localeCompare(a)) |
| 104 | + .slice(0, limit); |
| 105 | + |
| 106 | + const rows: (KVRow | null)[] = await this.kv.mget(...sortedKeys); |
| 107 | + for (const row of rows) { |
| 108 | + if (row) { |
| 109 | + const [checkpoint, metadata] = await Promise.all([ |
| 110 | + this.serde.parse(row.checkpoint), |
| 111 | + this.serde.parse(row.metadata), |
| 112 | + ]); |
| 113 | + |
| 114 | + yield { |
| 115 | + config: { |
| 116 | + configurable: { |
| 117 | + thread_id, |
| 118 | + checkpoint_id: (checkpoint as Checkpoint).id, |
| 119 | + }, |
| 120 | + }, |
| 121 | + checkpoint: checkpoint as Checkpoint, |
| 122 | + metadata: metadata as CheckpointMetadata, |
| 123 | + }; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + async put( |
| 129 | + config: RunnableConfig, |
| 130 | + checkpoint: Checkpoint, |
| 131 | + metadata: CheckpointMetadata |
| 132 | + ): Promise<RunnableConfig> { |
| 133 | + const thread_id = config.configurable?.thread_id; |
| 134 | + |
| 135 | + if (!thread_id || !checkpoint.id) { |
| 136 | + throw new Error("Thread ID and Checkpoint ID must be defined"); |
| 137 | + } |
| 138 | + |
| 139 | + const row: KVRow = { |
| 140 | + checkpoint: this.serde.stringify(checkpoint), |
| 141 | + metadata: this.serde.stringify(metadata), |
| 142 | + }; |
| 143 | + |
| 144 | + // LUA script to set checkpoint data atomically" |
| 145 | + const luaScript = ` |
| 146 | + local thread_id = ARGV[1] |
| 147 | + local checkpoint_id = ARGV[2] |
| 148 | + local row = ARGV[3] |
| 149 | +
|
| 150 | + redis.call('SET', thread_id .. ':' .. checkpoint_id, row) |
| 151 | + redis.call('SET', thread_id .. ':last', row) |
| 152 | + `; |
| 153 | + |
| 154 | + // Save the checkpoint and the last checkpoint |
| 155 | + await this.kv.eval(luaScript, [], [thread_id, checkpoint.id, row]); |
| 156 | + |
| 157 | + return { |
| 158 | + configurable: { |
| 159 | + thread_id, |
| 160 | + checkpoint_id: checkpoint.id, |
| 161 | + }, |
| 162 | + }; |
| 163 | + } |
| 164 | +} |
0 commit comments