import type { PaseoApi } from "@getpaseo/client"; import type { ZodType, input as ZodInput, output as ZodOutput } from "zod"; import type { PluginRpcContract, PluginSubscriptionContract, PluginWireInput } from "../rpc.js"; import type { PluginCleanup } from "../contracts.js"; import type { ProviderRegistration } from "./provider.js"; import type { UsageSourceRegistration } from "./usage.js"; import type { PluginLifecycleRegistration } from "./lifecycle.js"; export interface PluginHandlerContext { paseo: PaseoApi; } /** What a subscription handler receives beside its input. */ export interface PluginSubscriptionContext extends PluginHandlerContext { /** * Sends one event to this subscriber. A `Uint8Array` event travels as a binary frame; any other * event is JSON and must not contain a `Uint8Array`. Resolves once the event left the plugin * process. Events pushed after `signal` aborts are dropped. */ push(event: Event): Promise; /** Aborts when the subscriber releases, disconnects, or the plugin stops. */ signal: AbortSignal; } export type PluginSettingsState = | { status: "ready"; revision: string; values: ZodOutput; } | { status: "invalid"; revision: string; error: string; }; export interface PluginSettings { read(): Promise>; subscribe(listener: (state: PluginSettingsState) => void | Promise): PluginCleanup; } export interface PluginServerContext extends PluginLifecycleRegistration { registerSettings( definition: import("../settings.js").SettingsDefinition, ): PluginSettings; /** * Serves a subscription. The handler starts the work and returns; the subscription stays open * until `signal` aborts. A handler that throws refuses the subscription. When it returns a * promise, settling the promise closes the subscription, with the rejection as its error. */ handle( contract: PluginSubscriptionContract, handler: ( input: ZodOutput, context: PluginSubscriptionContext>>, ) => void | Promise, ): void; handle( contract: PluginRpcContract, handler: ( input: ZodOutput, context: PluginHandlerContext, ) => PluginWireInput> | Promise>>, ): void; registerProvider(provider: ProviderRegistration): void; registerUsageSource(source: UsageSourceRegistration): void; } export type PluginServerContribution = (server: PluginServerContext) => PluginCleanup;