import{AsyncLocalStorage}from"node:async_hooks";import{resolveKey}from"#context/key.js";const EVE_CONTEXT_STORAGE_KEY=Symbol.for(`eve.context-storage`);var ContextContainer=class{_durableValues=new Map;_virtualValues=new Map;get(e){return this._virtualValues.has(e.name)?this._virtualValues.get(e.name):this._durableValues.get(e.name)}require(e){if(!this.has(e))throw Error(`Context key "${e.name}" is not set.`);return this.get(e)}has(e){return this._virtualValues.has(e.name)||this._durableValues.has(e.name)}set(e,t){let n=typeof t==`function`?t(this.get(e)):t;return this._durableValues.set(e.name,n),n}ensure(e,t){return this.has(e)?this.require(e):this.set(e,t())}clearVirtualContext(){this._virtualValues.clear()}setVirtualContext(e,t){this._virtualValues.set(e.name,t)}*entries(){for(let[e,n]of this._durableValues){let r=resolveKey(e);r!==void 0&&(yield[r,n])}}};const globalContextStorage=globalThis;globalContextStorage[EVE_CONTEXT_STORAGE_KEY]===void 0&&(globalContextStorage[EVE_CONTEXT_STORAGE_KEY]=new AsyncLocalStorage);const contextStorage=globalContextStorage[EVE_CONTEXT_STORAGE_KEY];function loadContext(){let e=contextStorage.getStore();if(e===void 0)throw Error(`No active Eve context. Call this function only from authored runtime code such as tools, steps, and model callbacks.`);return e}export{ContextContainer,contextStorage,loadContext};