Skip to content

chore(langgraph): browser testing and explicit config passing #999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
7 changes: 6 additions & 1 deletion libs/langgraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"lint": "yarn lint:eslint && yarn lint:dpdm",
"lint:fix": "yarn lint:eslint --fix && yarn lint:dpdm",
"prepack": "yarn build",
"test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%",
"test": "NODE_OPTIONS=--experimental-vm-modules vitest --config vitest.config.js --test-timeout 30000 --maxWorkers=50%",
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts",
"test:single": "NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000",
"test:int": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.int\\.test.ts --testTimeout 100000 --maxWorkers=50%",
Expand Down Expand Up @@ -50,11 +50,13 @@
"@langchain/scripts": ">=0.1.3 <0.2.0",
"@swc/core": "^1.3.90",
"@swc/jest": "^0.2.29",
"@testing-library/dom": "^10.4.0",
"@tsconfig/recommended": "^1.0.3",
"@types/pg": "^8",
"@types/uuid": "^10",
"@typescript-eslint/eslint-plugin": "^6.12.0",
"@typescript-eslint/parser": "^6.12.0",
"@vitest/browser": "^3.0.8",
"@xenova/transformers": "^2.17.2",
"cheerio": "1.0.0-rc.12",
"dotenv": "^16.3.1",
Expand All @@ -70,12 +72,15 @@
"jest-environment-node": "^29.6.4",
"langchain": "^0.3.13",
"pg": "^8.13.0",
"playwright": "^1.51.0",
"prettier": "^2.8.3",
"release-it": "^17.6.0",
"rollup": "^4.34.7",
"ts-jest": "^29.1.0",
"tsx": "^4.7.0",
"typescript": "^4.9.5 || ^5.4.5",
"vite-plugin-node-polyfills": "^0.23.0",
"vitest": "^3.0.8",
"zod-to-json-schema": "^3.22.4"
},
"publishConfig": {
Expand Down
8 changes: 5 additions & 3 deletions libs/langgraph/src/func/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
BaseStore,
} from "@langchain/langgraph-checkpoint";
import { AsyncLocalStorageProviderSingleton } from "@langchain/core/singletons";
import { RunnableConfig } from "@langchain/core/runnables";
import { Pregel } from "../pregel/index.js";
import { PregelNode } from "../pregel/read.js";
import {
Expand Down Expand Up @@ -376,8 +377,9 @@ entrypoint.final = function final<ValueT, SaveT>({
* const newCount = (previousState?.counter ?? 0) + 1;
* ```
*/
export function getPreviousState<StateT>(): StateT {
const config: LangGraphRunnableConfig =
AsyncLocalStorageProviderSingleton.getRunnableConfig();
export function getPreviousState<StateT>(c?: RunnableConfig): StateT {
const config =
c ??
(AsyncLocalStorageProviderSingleton.getRunnableConfig() as LangGraphRunnableConfig);
return config.configurable?.[CONFIG_KEY_PREVIOUS_STATE] as StateT;
}
8 changes: 5 additions & 3 deletions libs/langgraph/src/interrupt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ import { PregelScratchpad } from "./pregel/types.js";
* @throws {GraphInterrupt} When no resume value is available
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function interrupt<I = unknown, R = any>(value: I): R {
const config: RunnableConfig | undefined =
AsyncLocalStorageProviderSingleton.getRunnableConfig();
export function interrupt<I = unknown, R = any>(
value: I,
c?: RunnableConfig
): R {
const config = c ?? AsyncLocalStorageProviderSingleton.getRunnableConfig();
if (!config) {
throw new Error("Called interrupt() outside the context of a graph.");
}
Expand Down
8 changes: 5 additions & 3 deletions libs/langgraph/src/pregel/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,11 @@ export function call<ArgsT extends unknown[], OutputT>(
{ func, name, retry }: CallWrapperOptions<ArgsT, OutputT>,
...args: ArgsT
): Promise<OutputT> {
const config =
AsyncLocalStorageProviderSingleton.getRunnableConfig() as RunnableConfig;
if (typeof config.configurable?.[CONFIG_KEY_CALL] === "function") {
let config = args[args.length - 1] as RunnableConfig;
config =
config ??
(AsyncLocalStorageProviderSingleton.getRunnableConfig() as RunnableConfig);
if (typeof config?.configurable?.[CONFIG_KEY_CALL] === "function") {
return config.configurable[CONFIG_KEY_CALL](func, name, args, {
retry,
callbacks: config.callbacks,
Expand Down
5 changes: 2 additions & 3 deletions libs/langgraph/src/pregel/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,8 @@ export function getConfig(): LangGraphRunnableConfig {
*
* @returns the input for the currently executing task
*/
export function getCurrentTaskInput<T = unknown>(): T {
const config: LangGraphRunnableConfig =
AsyncLocalStorageProviderSingleton.getRunnableConfig();
export function getCurrentTaskInput<T = unknown>(c?: RunnableConfig): T {
const config = c ?? AsyncLocalStorageProviderSingleton.getRunnableConfig();
if (config === undefined) {
throw new Error(
"Config not retrievable. This is likely because you are running in an environment without support for AsyncLocalStorage."
Expand Down
12 changes: 8 additions & 4 deletions libs/langgraph/src/setup/async_local_storage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { AsyncLocalStorageProviderSingleton } from "@langchain/core/singletons";
import { AsyncLocalStorage } from "node:async_hooks";

export function initializeAsyncLocalStorageSingleton() {
AsyncLocalStorageProviderSingleton.initializeGlobalInstance(
new AsyncLocalStorage()
);
if (typeof require !== "undefined") {
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
const { AsyncLocalStorage } = require("node:async_hooks");

AsyncLocalStorageProviderSingleton.initializeGlobalInstance(
new AsyncLocalStorage()
);
}
}
2 changes: 1 addition & 1 deletion libs/langgraph/src/tests/channels.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from "@jest/globals";
import { describe, it, expect } from "vitest";
import { AnyValue } from "../channels/any_value.js";
import { EphemeralValue } from "../channels/ephemeral_value.js";
import { LastValue } from "../channels/last_value.js";
Expand Down
2 changes: 1 addition & 1 deletion libs/langgraph/src/tests/diagrams.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, expect } from "@jest/globals";
import { test, expect } from "vitest";
import { createReactAgent } from "../prebuilt/index.js";
import { FakeSearchTool, FakeToolCallingChatModel } from "./utils.js";
import { Annotation, StateGraph } from "../web.js";
Expand Down
2 changes: 1 addition & 1 deletion libs/langgraph/src/tests/errors.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable prefer-template */
import { it, expect } from "@jest/globals";
import { it, expect } from "vitest";
import {
Annotation,
END,
Expand Down
Loading