import { z } from 'zod'; import type { Tool } from '../types/effectTool.js'; import type { StreamPart, TurnHandle } from '../types/stream.js'; import { type SweepRuntime } from '../runtime/durable/sweep.js'; /** * Deferred-work scheduling for proactive (agent-initiated) turns. * * One `Scheduler` contract across the framework: the engagement layer's * broadcast/drip jobs and the runtime's wake turns ride the same interface. * Backends: `createInProcessScheduler` (dev, timer-based), Cloudflare DO * alarms via `@kuralle-agents/cf-agent`, or any queue (BullMQ, Cloud Tasks) * implementing the two methods. */ export interface ScheduledJob { kind: string; payload: Record; } export interface Scheduler { enqueue(job: ScheduledJob, opts?: { delayMs?: number; }): Promise; cancel(jobId: string): Promise; } export type InjectableTimer = { set(fn: () => void, ms: number): unknown; clear(handle: unknown): void; }; /** * Default in-process scheduler (timer-based). For single-process/dev. * Inject a durable adapter (DO alarms, BullMQ, Cloud Tasks) for * multi-process / serverless. */ export declare function createInProcessScheduler(opts: { run: (job: ScheduledJob) => void | Promise; timer?: InjectableTimer; }): Scheduler; export declare const WAKE_JOB_KIND = "kuralle.wake"; export interface WakeOptions { /** Why the agent is waking — composed into the wake note the model sees. */ reason: string; /** Structured context for the wake turn (e.g. the abandoned cart id). */ payload?: Record; } export interface WakeJobPayload extends WakeOptions { sessionId: string; } export declare function wakeJob(wake: WakeJobPayload): ScheduledJob; export declare function isWakeJob(job: ScheduledJob): boolean; export declare const SWEEP_JOB_KIND = "kuralle.sweep"; export declare const DEFAULT_SWEEP_INTERVAL_MS = 30000; export interface SweepJobPayload { intervalMs?: number; } export declare function sweepJob(payload?: SweepJobPayload): ScheduledJob; export declare function isSweepJob(job: ScheduledJob): boolean; /** * Registers both `recoverOrphanedRuns` and `sweepDeadlines` as the handler for * `kuralle.sweep` jobs. * * Exactly one sweeper per RunStore. The run mutex is in-process only; two * schedulers against the same store race recoveries of the same orphan. */ export declare function createSweepJobRunner(runtime: SweepRuntime, opts?: { onError?: (error: unknown, job: ScheduledJob) => void; reschedule?: (job: ScheduledJob) => Promise; }): (job: ScheduledJob) => Promise; /** * Enqueue the first sweep tick on an existing Scheduler. Pair with * `createSweepJobRunner` as that scheduler's `run` callback (and pass * `reschedule` so one-shot backends tick on an interval). * * Exactly one sweeper per RunStore — do not call this twice against the * same store. */ export declare function startRunSweeper(scheduler: Scheduler, opts?: { intervalMs?: number; delayMs?: number; }): Promise; /** What a wake turn produced — handed to the host's delivery function. */ export interface WakeDelivery { sessionId: string; reason: string; payload?: Record; /** Full stream of the wake turn (text, tool events, interactive parts…). */ parts: StreamPart[]; /** Concatenated assistant text of the wake turn. */ text: string; } /** The runtime surface a wake runner needs (satisfied by `Runtime`). */ export interface WakeRunnable { run(opts: { sessionId: string; wake: WakeOptions; }): TurnHandle; } /** * Build the scheduler executor for wake jobs: runs the agent-initiated turn * and hands the produced parts to `deliver` (e.g. the messaging outbound * pipeline — which keeps the send window-safe). Compose with your own job * kinds: `run: (job) => isWakeJob(job) ? runWake(job) : runMine(job)`. */ export declare function createWakeJobRunner(runtime: WakeRunnable, opts: { deliver: (delivery: WakeDelivery) => Promise; onError?: (error: unknown, job: ScheduledJob) => void; }): (job: ScheduledJob) => Promise; declare const scheduleFollowupInput: z.ZodObject<{ delayMinutes: z.ZodNumber; reason: z.ZodString; }, z.core.$strip>; /** * Durable tool letting the agent schedule its own follow-up wake turn * ("I'll check back in an hour"). Safe for `globalTools` — it only schedules; * the wake turn itself goes through the full guard/window pipeline. */ export declare function createScheduleFollowupTool(scheduler: Scheduler): Tool, { scheduled: boolean; jobId: string; inMinutes: number; }>; export {};