eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
68 lines • 2.76 kB
TypeScript
import type { StandardSchemaV1 } from '#compiled/@standard-schema/spec/index.js';
import type { Hook as HookEntity } from '#compiled/@workflow/world/index.js';
import type { Hook, HookOptions } from './create-hook.js';
/**
* A typed hook interface for type-safe hook creation and resumption.
*/
export interface TypedHook<TInput, TOutput> {
/**
* Creates a new hook with the defined output type.
*
* Note: This method is not available in runtime bundles. Use it from workflow contexts only.
*
* @param options - Optional hook configuration
* @returns A Hook that resolves to the defined output type
*/
create(options?: HookOptions): Hook<TOutput>;
/**
* Resumes a hook by sending a payload with the defined input type.
* This is a type-safe wrapper around the `resumeHook` runtime function.
*
* @param token - The unique token identifying the hook
* @param payload - The payload to send; if a `schema` is configured it is validated/transformed before resuming
* @returns Promise resolving to the hook entity
* @throws Error if the hook is not found or if there's an error during the process
*/
resume(token: string, payload: TInput): Promise<HookEntity>;
}
export declare namespace TypedHook {
/**
* Extracts the input type from a {@link TypedHook}
*/
type Input<T extends TypedHook<any, any>> = T extends TypedHook<infer I, any> ? I : never;
}
/**
* Defines a typed hook for type-safe hook creation and resumption.
*
* This helper provides type safety by allowing you to define the input and output types
* for the hook's payload, with optional validation and transformation via a schema.
*
* @param schema - Schema used to validate and transform the input payload before resuming
* @returns An object with `create` and `resume` functions pre-typed with the input and output types
*
* @example
*
* ```ts
* // Define a hook with a specific payload type
* const approvalHook = defineHook<{ approved: boolean; comment: string }>();
*
* // In a workflow
* export async function workflowWithApproval() {
* "use workflow";
*
* const hook = approvalHook.create();
* const result = await hook; // Fully typed as { approved: boolean; comment: string; }
* }
*
* // In an API route
* export async function POST(request: Request) {
* const { token, approved, comment } = await request.json();
* await approvalHook.resume(token, { approved, comment }); // Input type
* return Response.json({ success: true });
* }
* ```
*/
export declare function defineHook<TInput, TOutput = TInput>({ schema, }?: {
schema?: StandardSchemaV1<TInput, TOutput>;
}): TypedHook<TInput, TOutput>;
//# sourceMappingURL=define-hook.d.ts.map