UNPKG

@copilotkit/runtime

Version:

<img src="https://github.com/user-attachments/assets/0a6b64d9-e193-4940-a3f6-60334ac34084" alt="banner" style="border-radius: 12px; border: 2px solid #d6d4fa;" />

101 lines (100 loc) 3.03 kB
import "reflect-metadata"; import { CopilotRuntimeLike } from "./runtime.mjs"; import { MaybePromise } from "@copilotkit/shared"; //#region src/v2/runtime/core/hooks.d.ts type RouteInfo = { method: "agent/run"; agentId: string; } | { method: "agent/connect"; agentId: string; } | { method: "agent/stop"; agentId: string; threadId: string; } | { method: "info"; } | { method: "transcribe"; } | { method: "threads/list"; } | { method: "threads/subscribe"; } | { method: "threads/update"; threadId: string; } | { method: "threads/archive"; threadId: string; } | { method: "threads/messages"; threadId: string; } | { method: "threads/events"; threadId: string; } | { method: "threads/state"; threadId: string; } | { method: "threads/clear"; } | { method: "cpk-debug-events"; }; interface HookContext { /** The incoming Fetch Request (possibly modified by prior hooks). */ request: Request; /** The resolved URL pathname. */ path: string; /** The CopilotRuntimeLike instance. */ runtime: CopilotRuntimeLike; } interface HandlerHookContext extends HookContext { /** The resolved route information. */ route: RouteInfo; } interface ResponseHookContext extends HookContext { /** The Response produced by the handler. */ response: Response; /** The resolved route information. */ route: RouteInfo; } interface ErrorHookContext extends HookContext { /** The error that occurred. */ error: unknown; /** The route info, if routing had already succeeded. */ route?: RouteInfo; } interface CopilotRuntimeHooks { /** * Called at the start of every request, before routing. * Use to validate auth, attach headers, initialize correlation IDs, etc. * * Return a modified Request to replace the original, or void to continue. * Throw a Response to short-circuit with an early response. */ onRequest?: (ctx: HookContext) => MaybePromise<Request | void>; /** * Called after routing is resolved but before the handler executes. * Receives the resolved route info (method, agentId, threadId). * * Use to do route-specific authorization, attach headers for agent calls, etc. * Return a modified Request or void. * Throw a Response to short-circuit. */ onBeforeHandler?: (ctx: HandlerHookContext) => MaybePromise<Request | void>; /** * Called after the handler produces a Response, before it's sent to the client. * Use to set cookies, add debugging headers, log, etc. * * Return a modified Response to replace the original, or void. */ onResponse?: (ctx: ResponseHookContext) => MaybePromise<Response | void>; /** * Called when an error occurs during request processing. * Return a Response to override the default error response, or void to use the default. */ onError?: (ctx: ErrorHookContext) => MaybePromise<Response | void>; } //#endregion export { CopilotRuntimeHooks, ErrorHookContext, HandlerHookContext, HookContext, ResponseHookContext, RouteInfo }; //# sourceMappingURL=hooks.d.mts.map