import type { KaijiBotConfig } from "../../config/config.js";
import { cancelFlowByIdForOwner } from "../../tasks/task-executor.js";
import type { TaskFlowRecord, JsonValue } from "../../tasks/task-flow-registry.types.js";
import type { TaskDeliveryStatus, TaskDeliveryState, TaskNotifyPolicy, TaskRecord, TaskRegistrySummary, TaskRuntime } from "../../tasks/task-registry.types.js";
import type { KaijiBotPluginToolContext } from "../types.js";
export type ManagedTaskFlowRecord = TaskFlowRecord & {
    syncMode: "managed";
    controllerId: string;
};
export type ManagedTaskFlowMutationErrorCode = "not_found" | "not_managed" | "revision_conflict";
export type ManagedTaskFlowMutationResult = {
    applied: true;
    flow: ManagedTaskFlowRecord;
} | {
    applied: false;
    code: ManagedTaskFlowMutationErrorCode;
    current?: TaskFlowRecord;
};
export type BoundTaskFlowTaskRunResult = {
    created: true;
    flow: ManagedTaskFlowRecord;
    task: TaskRecord;
} | {
    created: false;
    reason: string;
    found: boolean;
    flow?: TaskFlowRecord;
};
export type BoundTaskFlowCancelResult = Awaited<ReturnType<typeof cancelFlowByIdForOwner>>;
export type BoundTaskFlowRuntime = {
    readonly sessionKey: string;
    readonly requesterOrigin?: TaskDeliveryState["requesterOrigin"];
    createManaged: (params: {
        controllerId: string;
        goal: string;
        status?: ManagedTaskFlowRecord["status"];
        notifyPolicy?: TaskNotifyPolicy;
        currentStep?: string | null;
        stateJson?: JsonValue | null;
        waitJson?: JsonValue | null;
        cancelRequestedAt?: number | null;
        createdAt?: number;
        updatedAt?: number;
        endedAt?: number | null;
    }) => ManagedTaskFlowRecord;
    get: (flowId: string) => TaskFlowRecord | undefined;
    list: () => TaskFlowRecord[];
    findLatest: () => TaskFlowRecord | undefined;
    resolve: (token: string) => TaskFlowRecord | undefined;
    getTaskSummary: (flowId: string) => TaskRegistrySummary | undefined;
    setWaiting: (params: {
        flowId: string;
        expectedRevision: number;
        currentStep?: string | null;
        stateJson?: JsonValue | null;
        waitJson?: JsonValue | null;
        blockedTaskId?: string | null;
        blockedSummary?: string | null;
        updatedAt?: number;
    }) => ManagedTaskFlowMutationResult;
    resume: (params: {
        flowId: string;
        expectedRevision: number;
        status?: Extract<ManagedTaskFlowRecord["status"], "queued" | "running">;
        currentStep?: string | null;
        stateJson?: JsonValue | null;
        updatedAt?: number;
    }) => ManagedTaskFlowMutationResult;
    finish: (params: {
        flowId: string;
        expectedRevision: number;
        stateJson?: JsonValue | null;
        updatedAt?: number;
        endedAt?: number;
    }) => ManagedTaskFlowMutationResult;
    fail: (params: {
        flowId: string;
        expectedRevision: number;
        stateJson?: JsonValue | null;
        blockedTaskId?: string | null;
        blockedSummary?: string | null;
        updatedAt?: number;
        endedAt?: number;
    }) => ManagedTaskFlowMutationResult;
    requestCancel: (params: {
        flowId: string;
        expectedRevision: number;
        cancelRequestedAt?: number;
    }) => ManagedTaskFlowMutationResult;
    cancel: (params: {
        flowId: string;
        cfg: KaijiBotConfig;
    }) => Promise<BoundTaskFlowCancelResult>;
    runTask: (params: {
        flowId: string;
        runtime: TaskRuntime;
        sourceId?: string;
        childSessionKey?: string;
        parentTaskId?: string;
        agentId?: string;
        runId?: string;
        label?: string;
        task: string;
        preferMetadata?: boolean;
        notifyPolicy?: TaskNotifyPolicy;
        deliveryStatus?: TaskDeliveryStatus;
        status?: "queued" | "running";
        startedAt?: number;
        lastEventAt?: number;
        progressSummary?: string | null;
    }) => BoundTaskFlowTaskRunResult;
};
export type PluginRuntimeTaskFlow = {
    bindSession: (params: {
        sessionKey: string;
        requesterOrigin?: TaskDeliveryState["requesterOrigin"];
    }) => BoundTaskFlowRuntime;
    fromToolContext: (ctx: Pick<KaijiBotPluginToolContext, "sessionKey" | "deliveryContext">) => BoundTaskFlowRuntime;
};
export declare function createRuntimeTaskFlow(): PluginRuntimeTaskFlow;
