Passing `ctx` to AI agent tools
I'm currently doing the following inside a convex action:
where I'm using an ALS like so:
so that tools can access the ctx to query or write to the Convex db, e.g.
HOWEVER, I'm running into:
which i could fix by using the
use node
directive, but I'm trying to stay away from it for performance reasons. Any ideas on how do do this with edge runtime packages?3 Replies
The Convex JS runtime doesn't currently support AsyncLocalStorage, so you won't be able to do this there. However only one mutation/query/action is running in any given isolate, so you can just set a global variable with this; you don't need to use AsyncLocalStorage to try to keep the right kind of context to each of the different async threads of execution.
If you need it for a library you're using we want to hear about it, but instead of using it manually for this kind of thing you can just set a global variable. This global state won't be shared between mutation / query / action runs.
Thanks for the suggestion @ballingt !
I'm trying the following:
Action file
Global store file:
When I call the action I get:
Server Error
Uncaught ReferenceError: globalStore is not defined
not very familiar with the best way to manage global stores..Ah are you trying to use a truly global variable? You can do that by setting properties on that just creates a module-level variable that's invalid syntax in strict mode.
globalThis
, globalThis.globalStore
. When you write globalStore = { ... }