import { ConversationStore } from './conversations.js'; /** * In-process event bus for the runtime gateway. Anything the daemon * does that an external observer might want to watch (heartbeat fired, * conversation turn appended, boot completed, …) flows through here. * * The HTTP server's `/events` SSE stream subscribes to it and forwards * each event as a `data: \n\n` frame. Tests and embedded usage * subscribe directly via `events.on()`. */ interface BootEvent { type: "boot"; at: string; workspace: string; registered: readonly string[]; } interface HeartbeatFiredEvent { type: "heartbeat-fired"; at: string; agent: string; conversationId: string; prompt: string; reply: string; durationMs: number; } interface HeartbeatErrorEvent { type: "heartbeat-error"; at: string; agent?: string; error: string; } interface ConvTurnEvent { type: "conv-turn-appended"; at: string; conversationId: string; role: "user" | "assistant"; contentPreview: string; } /** * One log line from a supervised tunnel provider (cloudflared today). * Surfaced verbatim so users can debug "why didn't my remote URL come * up" by tailing /events. */ interface RemoteLogEvent { type: "remote-log"; at: string; line: string; } /** * Fired once per successful `config_set` / `PATCH /config` write to * `~/.agentproto/config.json` (config-tools.ts). Never carries a secret * key's value — only the dotted path(s) touched and whether the change * takes effect immediately or needs a restart. */ interface ConfigChangedEvent { type: "config:changed"; at: string; keys: string[]; applied: "hot" | "restart-required"; } type RuntimeEvent = BootEvent | HeartbeatFiredEvent | HeartbeatErrorEvent | ConvTurnEvent | RemoteLogEvent | ConfigChangedEvent; interface RuntimeEvents { on(type: E, handler: (ev: Extract) => void): () => void; onAny(handler: (ev: RuntimeEvent) => void): () => void; emit(ev: RuntimeEvent): void; } declare function createRuntimeEvents(): RuntimeEvents; /** * HEARTBEAT.md autonomy loop. * * @deprecated A third scheduler next to cron and routines, with no known user * (only `projects/agentproto/playground/workspace/HEARTBEAT.md` in the * monorepo). Prefer a cron job that prompts a session (`cron add * --target-session --prompt ...`), or a `ROUTINE.md`: both give pause * and resume, run history and auto-pause on repeated failures, which this loop * lacks. Kept working for existing workspaces; do not build on it. * * A workspace can ship a `HEARTBEAT.md` at its root. Frontmatter: * * schema: heartbeat/v1 * agent: (matches `/.agents//AGENT.md`) * every: "60s" (Ns | Nm | Nh | Nd; cron support TODO) * enabled: true (default true; set false to register-but-skip) * conversation: heartbeat-{date} (templated id; defaults to per-day) * * Body = the prompt body the agent receives each tick. Reread fresh * on every tick so editing HEARTBEAT.md takes effect without restart. * * Ticks resolve the agent via the injected `buildAgent` callback, * call `agent.generate(prompt)`, and append the prompt + reply to a * conversation (default: daily-rolling `heartbeat-YYYY-MM-DD`). * * When the file is missing, frontmatter is invalid, `enabled: false`, * or the resolved agent has no usable model — the runner skips the * tick silently and emits a `heartbeat-error` event. The daemon * stays up. */ interface HeartbeatAgent { generate(prompt: string): Promise<{ text: string; }>; } interface BuildHeartbeatAgent { (id: string): Promise; } interface HeartbeatRunnerOptions { workspace: string; conversations: ConversationStore; events: RuntimeEvents; /** Resolves a heartbeat-runnable agent from its workspace id. */ buildAgent: BuildHeartbeatAgent; /** Filename inside the workspace root. Default `HEARTBEAT.md`. */ filename?: string; /** Skip ticks while this returns false. Default: always true. */ shouldFire?: () => boolean; /** Override clock for tests. */ now?: () => Date; } interface HeartbeatRunner { /** Start the timer loop. Idempotent. */ start(): void; /** Stop the timer loop. Pending ticks finish out. */ stop(): void; /** Force-fire one tick now (used by `POST /heartbeat/tick`). */ fireNow(): Promise; } declare function startHeartbeat(opts: HeartbeatRunnerOptions): HeartbeatRunner; /** * Parse `Ns | Nm | Nh | Nd` into milliseconds. Returns `null` if the * string doesn't match. (AIP-41 supports more units, but the daemon's * heartbeat sticks to coarse durations to keep ticks observable.) */ declare function parseDuration(input: string): number | null; export { type BuildHeartbeatAgent as B, type HeartbeatAgent as H, type RuntimeEvent as R, type HeartbeatRunner as a, type RuntimeEvents as b, createRuntimeEvents as c, type HeartbeatRunnerOptions as d, parseDuration as p, startHeartbeat as s };