Skip to content
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

core[minor]: Allow injection of AsyncLocalStorage #4498

Merged
merged 8 commits into from
Feb 24, 2024
Merged

Conversation

jacoblee93
Copy link
Collaborator

@jacoblee93 jacoblee93 commented Feb 23, 2024

AsyncLocalStorage a very useful API that is available in many environments but not all. We want to allow projects like LangGraph to take advantage of its convenience at the cost of supporting only a subset of environments.

The idea would be (before instantiating any RunnableLambda):

import { AsyncLocalStorageProviderSingleton } from "@langchain/core/singletons";
import { AsyncLocalStorage } from "node:async_hooks";

AsyncLocalStorageProviderSingleton.setClass(AsyncLocalStorage);

This PR also allows nested RunnableLambdas to automatically fetch config from the next level up, enabling more convenient tracing.

@nfcampos @bracesproul @dqbd

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Feb 23, 2024
Copy link

vercel bot commented Feb 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ❌ Failed (Inspect) Feb 24, 2024 1:31am
langchainjs-docs ✅ Ready (Inspect) Visit Preview Feb 24, 2024 1:31am

@jacoblee93
Copy link
Collaborator Author

jacoblee93 commented Feb 23, 2024

I actually think it might be better to instantiate a singleton instance instead of a class - don't think there are any downsides to sharing an instance?

Going to change to that to remove janky race condition around instantiating a global variable in the constructor.

run: (store: any, callback: () => any) => any;
}

export class MockAsyncLocalStorage implements AsyncLocalStorageInterface {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure Mock is the right word to use here? To me if I see this I'll assume it should be used in tests, and not as the default value.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a mock though because it's not doing anything? Any better names?

@jacoblee93
Copy link
Collaborator Author

@nfcampos how's this look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:improvement Medium size change to existing code to handle new use-cases size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants