mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-09-11 12:17:53 +02:00
Add history tracking to in-memory, sqlite and aiosqlite checkpointers
- Add Pregel.get_state_history and .aget_state_history methods to get history iterator - Update checkpointer base class with new list and get_tuple methods - Rewrite in-memory checkpointer class to track history - Rewrite sqlite and aiosqlite checkpointers to track history - Add new tests for history tracking
This commit is contained in:
+10
-4
@@ -1,3 +1,5 @@
|
||||
from collections import defaultdict
|
||||
|
||||
from langchain_core.pydantic_v1 import Field
|
||||
|
||||
from langgraph.checkpoint.base import Checkpoint, CheckpointAt, copy_checkpoint
|
||||
@@ -5,15 +7,19 @@ from langgraph.checkpoint.memory import MemorySaver
|
||||
|
||||
|
||||
class MemorySaverAssertImmutable(MemorySaver):
|
||||
storage_for_copies: dict[str, Checkpoint] = Field(default_factory=dict)
|
||||
storage_for_copies: defaultdict[str, dict[str, Checkpoint]] = Field(
|
||||
default_factory=lambda: defaultdict(dict)
|
||||
)
|
||||
|
||||
at = CheckpointAt.END_OF_STEP
|
||||
|
||||
def put(self, config: dict, checkpoint: dict) -> None:
|
||||
def put(self, config: dict, checkpoint: Checkpoint) -> None:
|
||||
# assert checkpoint hasn't been modified since last written
|
||||
thread_id = config["configurable"]["thread_id"]
|
||||
if saved := super().get(config):
|
||||
assert self.storage_for_copies[thread_id] == saved
|
||||
self.storage_for_copies[thread_id] = copy_checkpoint(checkpoint)
|
||||
assert self.storage_for_copies[thread_id][saved["ts"]] == saved
|
||||
self.storage_for_copies[thread_id][checkpoint["ts"]] = copy_checkpoint(
|
||||
checkpoint
|
||||
)
|
||||
# call super to write checkpoint
|
||||
super().put(config, checkpoint)
|
||||
|
||||
Reference in New Issue
Block a user