UNPKG

agents

Version:

A home for your AI agents

113 lines (111 loc) • 4.68 kB
import { _ as LifecycleJobContext, o as MemoryLimitContext, v as LifecycleJobOutcome, x as Connection } from "./capability-runner-uFOdSANC.js"; import { t as Lifecycle } from "./durable-object-lifecycle-CRCA6O0W.js"; import { AsyncLocalStorage } from "node:async_hooks"; import { DurableObject } from "cloudflare:workers"; //#region src/lifecycle/current-agent.d.ts /** * A Durable Object that has installed the Agents SDK Lifecycle. * * Pass a more specific Lifecycle Object type to {@link getCurrentAgent} when * shared host code needs APIs implemented by a particular object. */ interface LifecycleObject< Env extends object = Cloudflare.Env, Props extends object = object > extends DurableObject<Env> { readonly lifecycle: Lifecycle<Env, Props>; onStart?(props?: Props): void | Promise<void>; onRequest?(request: Request): Response | Promise<Response>; /** * Choose the response for a request or WebSocket upgrade whose handling * threw: startup, a capability's request or upgrade hook, or `onRequest`. * * Lifecycle logs `error` before calling this hook. Return a `Response` to * send it, or nothing to send the default: `500 Internal Server Error` for * a request, and for an upgrade a socket that closes with 1011. Neither * default carries the error. If this hook throws, Lifecycle logs that * error and also sends the default. In both cases, if the original thrown * value is not an `Error`, Lifecycle rethrows it instead. For an upgrade, * return either an HTTP error response or a `101` response with your own * `WebSocketPair`. * * `error` is whatever was thrown, so narrow it before reading it. Its * message and stack can reveal internals, so avoid sending them to callers * you do not trust. `request` may already have had its body read. */ onFetchError?( request: Request, error: unknown ): Response | void | Promise<Response | void>; onAlarm?(): void | Promise<void>; onJob?( context: LifecycleJobContext ): LifecycleJobOutcome | void | Promise<LifecycleJobOutcome | void>; /** * Host domain policy applied when the alarm memory-limit circuit breaker * records a strike (#1825), after every capability's `onMemoryLimit` hook. * * Lifecycle dispatches host hooks structurally through its internal host * cast, so TS visibility is the host's to choose: a framework host whose * implementation is internal machinery declares the hook `protected` (the * pattern — see `AIChatAgent`/`Think`) and keeps the real work in a * `private _cf_`-prefixed method, without falling out of this contract at * runtime. */ onAlarmMemoryLimit?(context: MemoryLimitContext): void | Promise<void>; } /** Values associated with the currently executing Lifecycle host. */ type AgentContextStore = { /** Lifecycle host selected for this invocation. */ agent: unknown /** WebSocket connection selected for this invocation, when applicable. */; connection: | Connection | undefined /** HTTP request selected for this invocation, when applicable. */; request: | Request | undefined /** Extension-owned value selected for this invocation, when applicable. */; email: unknown; }; /** Values returned by {@link getCurrentAgent}. */ type CurrentAgentContext< Host extends DurableObject = LifecycleObject, Email = unknown > = { agent: Host | undefined; connection: Connection | undefined; request: Request | undefined; email: Email | undefined; }; /** * Shared invocation context for Lifecycle, Agent, AIChatAgent, and Think. * * @internal Importing or relying on this symbol will break your code in a * future release. Use {@link getCurrentAgent} to read the public context. */ declare const __DO_NOT_USE_WILL_BREAK__agentContext: AsyncLocalStorage<AgentContextStore>; /** * Return the current Agent or Lifecycle Object and invocation-specific values. * * Lifecycle host startup and alarm hooks receive the current object with no * request. Request hooks additionally receive the request being handled. * Lifecycle-managed WebSocket hooks receive their connection and, during * connect, its upgrade request. Agent extensions may also establish context * for email, chat turns, callable methods, and detached work. Capability hooks * do not run in this ambient context. */ declare function getCurrentAgent< Host extends DurableObject = LifecycleObject >(): CurrentAgentContext<Host>; //#endregion export { getCurrentAgent as a, __DO_NOT_USE_WILL_BREAK__agentContext as i, CurrentAgentContext as n, LifecycleObject as r, AgentContextStore as t }; //# sourceMappingURL=current-agent-DShbQHDO.d.ts.map