Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions libs/checkpoint-mysql/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ------------------LangSmith tracing------------------
LANGCHAIN_TRACING_V2=true
LANGCHAIN_ENDPOINT="https://api.smith.langchain.com"
LANGCHAIN_API_KEY=
LANGCHAIN_PROJECT=
# -----------------------------------------------------
68 changes: 68 additions & 0 deletions libs/checkpoint-mysql/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module.exports = {
extends: [
"airbnb-base",
"eslint:recommended",
"prettier",
"plugin:@typescript-eslint/recommended",
],
parserOptions: {
ecmaVersion: 12,
parser: "@typescript-eslint/parser",
project: "./tsconfig.json",
sourceType: "module",
},
plugins: ["@typescript-eslint", "no-instanceof"],
ignorePatterns: [
".eslintrc.cjs",
"scripts",
"node_modules",
"dist",
"dist-cjs",
"*.js",
"*.cjs",
"*.d.ts",
],
rules: {
"no-process-env": 2,
"no-instanceof/no-instanceof": 2,
"@typescript-eslint/explicit-module-boundary-types": 0,
"@typescript-eslint/no-empty-function": 0,
"@typescript-eslint/no-shadow": 0,
"@typescript-eslint/no-empty-interface": 0,
"@typescript-eslint/no-use-before-define": ["error", "nofunc"],
"@typescript-eslint/no-unused-vars": ["warn", { args: "none" }],
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-misused-promises": "error",
"arrow-body-style": 0,
camelcase: 0,
"class-methods-use-this": 0,
"import/extensions": [2, "ignorePackages"],
"import/no-extraneous-dependencies": [
"error",
{ devDependencies: ["**/*.test.ts"] },
],
"import/no-unresolved": 0,
"import/prefer-default-export": 0,
"keyword-spacing": "error",
"max-classes-per-file": 0,
"max-len": 0,
"no-await-in-loop": 0,
"no-bitwise": 0,
"no-console": 0,
"no-empty-function": 0,
"no-restricted-syntax": 0,
"no-shadow": 0,
"no-continue": 0,
"no-void": 0,
"no-underscore-dangle": 0,
"no-use-before-define": 0,
"no-useless-constructor": 0,
"no-return-await": 0,
"consistent-return": 0,
"no-else-return": 0,
"func-names": 0,
"no-lonely-if": 0,
"prefer-rest-params": 0,
"new-cap": ["error", { properties: false, capIsNew: false }],
},
};
7 changes: 7 additions & 0 deletions libs/checkpoint-mysql/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
index.cjs
index.js
index.d.ts
index.d.cts
node_modules
dist
.yarn
19 changes: 19 additions & 0 deletions libs/checkpoint-mysql/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "https://json.schemastore.org/prettierrc",
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": false,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"trailingComma": "es5",
"bracketSpacing": true,
"arrowParens": "always",
"requirePragma": false,
"insertPragma": false,
"proseWrap": "preserve",
"htmlWhitespaceSensitivity": "css",
"vueIndentScriptAndStyle": false,
"endOfLine": "lf"
}
13 changes: 13 additions & 0 deletions libs/checkpoint-mysql/.release-it.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"github": {
"release": true,
"autoGenerate": true,
"tokenRef": "GITHUB_TOKEN_RELEASE"
},
"npm": {
"publish": true,
"versionArgs": [
"--workspaces-update=false"
]
}
}
21 changes: 21 additions & 0 deletions libs/checkpoint-mysql/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2024 LangChain

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
101 changes: 101 additions & 0 deletions libs/checkpoint-mysql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# @langchain/langgraph-checkpoint-mysql

Implementation of a [LangGraph.js](https://github.com/langchain-ai/langgraphjs) CheckpointSaver that uses a MySQL DB.

- Database operations are implemented through [Sequelize v6](https://sequelize.org/).
- Support `MySQL >= 5.7`
- Implementation follow the style(including code style / table structure) of [@langchain/langgraph-checkpoint-postgres](https://www.npmjs.com/package/@langchain/langgraph-checkpoint-postgres)
- Inspired by [https://github.com/tjni/langgraph-checkpoint-mysql](https://github.com/tjni/langgraph-checkpoint-mysql)

## Usage

```ts
import { MySQLSaver } from "@langchain/langgraph-checkpoint-mysql";

const writeConfig = {
configurable: {
thread_id: "1",
checkpoint_ns: "",
},
};
const readConfig = {
configurable: {
thread_id: "1",
},
};

// you can optionally pass a configuration object as the second parameter
const checkpointer = MySQLSaver.fromConnString("mysql://...");

// or you can initialize the sequelize instance first
// const sequelize = new Sequelize({
// database: 'testdb',
// username: 'root',
// password: '123456',
// host: '127.0.0.1',
// port: 3306,
// dialect: 'mysql',
// });

// const checkpointer = new MySQLSaver(sequelize);

// You should call .setup() the first time you use the checkpointer:
await checkpointer.setup();

// or you can set the table manually by using the sql in `/src/migration.sql`

const checkpoint = {
v: 1,
ts: "2024-07-31T20:14:19.804150+00:00",
id: "1ef4f797-8335-6428-8001-8a1503f9b875",
channel_values: {
my_key: "meow",
node: "node",
},
channel_versions: {
__start__: 2,
my_key: 3,
"start:node": 3,
node: 3,
},
versions_seen: {
__input__: {},
__start__: {
__start__: 1,
},
node: {
"start:node": 2,
},
},
pending_sends: [],
};

// store checkpoint
await checkpointer.put(writeConfig, checkpoint, {}, {});

// load checkpoint
await checkpointer.get(readConfig);

// list checkpoints
for await (const checkpoint of checkpointer.list(readConfig)) {
console.log(checkpoint);
}
```

## Testing

Spin up testing MySQL

```bash
docker-compose up -d && docker-compose logs -f
```

Then rename the test file `./src/tests/checkpoints.int.test.ts` to `./src/tests/checkpoints.test.ts`

Run the test script

```bash
yarn test

# or yarn test:watch
```
16 changes: 16 additions & 0 deletions libs/checkpoint-mysql/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: "3.8"

services:
postgres:
image: postgres:latest
container_name: langgraphjs-postgres-test
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: testdb
# Enable logging of connections and disconnections
POSTGRES_LOG_CONNECTIONS: "on"
POSTGRES_LOG_DISCONNECTIONS: "on"
POSTGRES_LOG_MIN_MESSAGES: "info" # Logs messages of level INFO and above
ports:
- "5434:5432"
21 changes: 21 additions & 0 deletions libs/checkpoint-mysql/langchain.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";

/**
* @param {string} relativePath
* @returns {string}
*/
function abs(relativePath) {
return resolve(dirname(fileURLToPath(import.meta.url)), relativePath);
}

export const config = {
internals: [/node\:/, /@langchain\/core\//, /async_hooks/],
entrypoints: {
index: "index"
},
tsConfigPath: resolve("./tsconfig.json"),
cjsSource: "./dist-cjs",
cjsDestination: "./dist",
abs,
};
86 changes: 86 additions & 0 deletions libs/checkpoint-mysql/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"name": "@langchain/langgraph-checkpoint-mysql",
"version": "0.0.1",
"description": "LangGraph",
"type": "module",
"engines": {
"node": ">=18"
},
"main": "./index.js",
"types": "./index.d.ts",
"repository": {
"type": "git",
"url": "[email protected]:langchain-ai/langgraphjs.git"
},
"scripts": {
"build": "yarn turbo:command build:internal --filter=@langchain/langgraph-checkpoint-mysql",
"build:internal": "yarn clean && yarn lc_build --create-entrypoints --pre --tree-shaking",
"clean": "rm -rf dist/ dist-cjs/ .turbo/",
"lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/",
"lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
"lint": "yarn lint:eslint && yarn lint:dpdm",
"lint:fix": "yarn lint:eslint --fix && yarn lint:dpdm",
"prepack": "yarn build",
"test": "vitest run",
"test:watch": "vitest watch",
"test:int": "vitest run --mode int",
"format": "prettier --config .prettierrc --write \"src\"",
"format:check": "prettier --config .prettierrc --check \"src\""
},
"author": "LangChain",
"license": "MIT",
"dependencies": {
"mysql2": "^3.14.1",
"sequelize": "^6.37.7"
},
"peerDependencies": {
"@langchain/core": ">=0.2.31 <0.4.0",
"@langchain/langgraph-checkpoint": "~0.0.15"
},
"devDependencies": {
"@langchain/langgraph-checkpoint": "workspace:*",
"@langchain/scripts": ">=0.1.2 <0.2.0",
"@tsconfig/recommended": "^1.0.3",
"@types/pg": "^8.11.8",
"@types/uuid": "^10",
"@typescript-eslint/eslint-plugin": "^6.12.0",
"@typescript-eslint/parser": "^6.12.0",
"dotenv": "^16.3.1",
"dpdm": "^3.12.0",
"eslint": "^8.33.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-no-instanceof": "^1.0.1",
"eslint-plugin-prettier": "^4.2.1",
"prettier": "^2.8.3",
"release-it": "^19.0.2",
"rollup": "^4.37.0",
"tsx": "^4.19.3",
"typescript": "^4.9.5 || ^5.4.5",
"vitest": "^3.1.2"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"exports": {
".": {
"types": {
"import": "./index.d.ts",
"require": "./index.d.cts",
"default": "./index.d.ts"
},
"import": "./index.js",
"require": "./index.cjs"
},
"./package.json": "./package.json"
},
"files": [
"dist/",
"index.cjs",
"index.js",
"index.d.ts",
"index.d.cts"
]
}
Loading