-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix pending writes and sends and channel versions.
MySQL lacks array functions, so we use JSON arrays as a proxy. Extra compatibility code is needed since MySQL will encode the blobs in each array in base64. MySQL also lacks ORDER BY over its JSON array functions, so sorting needs to happen in the application. Lastly, MySQL lacks some convenient JSON functions for extracting the channel versions from a JSON document, since they are dynamic. We work around that too in modern versions of MySQL.
- Loading branch information
Showing
8 changed files
with
227 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import base64 | ||
import json | ||
from typing import NamedTuple | ||
|
||
# When MySQL returns a blob in a JSON array, it is base64 encoded and a prefix | ||
# of "base64:type251:" attached to it. | ||
MySQLBase64Blob = str | ||
|
||
|
||
def decode_base64_blob(base64_blob: MySQLBase64Blob) -> bytes: | ||
_, data = base64_blob.rsplit(":", 1) | ||
return base64.b64decode(data) | ||
|
||
|
||
class MySQLPendingWrite(NamedTuple): | ||
""" | ||
The pending write tuple we receive from our DB query. | ||
""" | ||
|
||
task_id: str | ||
channel: str | ||
type_: str | ||
blob: MySQLBase64Blob | ||
idx: int | ||
|
||
|
||
def deserialize_pending_writes(value: str) -> list[tuple[str, str, str, bytes]]: | ||
if not value: | ||
return [] | ||
|
||
values = (MySQLPendingWrite(*write) for write in json.loads(value)) | ||
|
||
return [ | ||
(db.task_id, db.channel, db.type_, decode_base64_blob(db.blob)) | ||
for db in sorted(values, key=lambda db: (db.task_id, db.idx)) | ||
] | ||
|
||
|
||
class MySQLPendingSend(NamedTuple): | ||
type_: str | ||
blob: MySQLBase64Blob | ||
idx: int | ||
|
||
|
||
def deserialize_pending_sends(value: str) -> list[tuple[str, bytes]]: | ||
if not value: | ||
return [] | ||
|
||
values = (MySQLPendingSend(*send) for send in json.loads(value)) | ||
|
||
return [ | ||
(db.type_, decode_base64_blob(db.blob)) | ||
for db in sorted(values, key=lambda db: db.idx) | ||
] | ||
|
||
|
||
class MySQLChannelValue(NamedTuple): | ||
channel: str | ||
type_: str | ||
blob: MySQLBase64Blob | ||
|
||
|
||
def deserialize_channel_values(value: str) -> list[tuple[str, str, bytes]]: | ||
if not value: | ||
return [] | ||
|
||
values = (MySQLChannelValue(*channel_value) for channel_value in json.loads(value)) | ||
|
||
return [(db.channel, db.type_, decode_base64_blob(db.blob)) for db in values] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "langgraph-checkpoint-mysql" | ||
version = "1.0.0" | ||
version = "1.0.1" | ||
description = "Library with a MySQL implementation of LangGraph checkpoint saver." | ||
authors = ["Theodore Ni <[email protected]>"] | ||
license = "MIT" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.