import type { OutputStream } from '../../platform/index.js'; import type { RunningExecution } from '../../core/running-executions.js'; import type { ThreadRecord, AgentSlotId, AgentSlotConfig, ThreadTemplate, RunThreadOptions } from '../../core/types/thread-types.js'; interface ThreadRunResult { thread: ThreadRecord; /** Final output from the last completed step (for Slack display) */ finalOutput: string | null; /** Aggregated cost across all steps */ totalCostUsd: number; /** Total number of turns across all steps */ totalNumTurns: number; /** The result object from the last agent call (for backward compat with handleAgentSuccess) */ lastAgentResult: any; /** The execution ID from the last completed step — used by handleAgentSuccess in the wrapper. */ executionId: string | null; } /** Outer-scope state shared across the whole runThread() lifecycle. */ interface ThreadContext { thread: ThreadRecord; template: ThreadTemplate | null; meta: ThreadRecord['metadata']; stream: OutputStream; lastAgentResult: any; totalNumTurns: number; /** Set when a step was interrupted by an API rate limit while the throttle is active. * Pauses the thread (status='rate_limited') instead of completing/failing it. */ rateLimited?: boolean; } /** Per-step config built once by buildStepConfig — fully populated, no placeholders. */ interface StepContext { agentSlotId: AgentSlotId; agentConfig: AgentSlotConfig; isFirstStep: boolean; multiAgent: boolean; /** Stage this step runs. Null for single-stage agents (no `stages` map declared). */ stage: string | null; prompt: string; sessionId: string | null; sessionKey: string | null; sessionName: string; profileName: string; execution: { id: string; [k: string]: any; }; /** Always set in buildStepConfig — never an empty placeholder. */ stepStartTime: string; } /** Per-step callbacks resolved from opts/vm by setupStepCallbacks. */ interface StepCallbacks { onAssistantMessage: ((text: string) => void) | null | undefined; onProgress: ((progress: any) => void) | null; onToolUse: ((name: string, input: any) => void) | null; } type StepInfo = Pick; /** Validate thread, load template/metadata, init the aggregating OutputStream. */ declare function initThreadContext(threadId: string, opts: RunThreadOptions): ThreadContext; /** Resolve next step, post boundary notifications, update the status message. * Returns null if the loop should break (cancelled, no next step). */ declare function resolveAndNotifyStep(threadId: string, ctx: ThreadContext, opts: RunThreadOptions): Promise; /** Build prompt, resolve session config, profile, register execution, generate session name + start time. * Returns a fully-populated StepContext — no placeholder fields. */ declare function buildStepConfig(threadId: string, stepInfo: StepInfo, ctx: ThreadContext, opts: RunThreadOptions): Promise; /** Resolve onAssistantMessage/onProgress callbacks and mark slot as running. * Returns the callbacks instead of mutating the StepContext. */ declare function setupStepCallbacks(threadId: string, stepCtx: StepContext, ctx: ThreadContext, opts: RunThreadOptions): StepCallbacks; /** Run the agent, manage handle, await result; fail execution + rethrow on error. * Returns the agent result. */ declare function executeAndAwaitAgent(threadId: string, stepCtx: StepContext, callbacks: StepCallbacks, ctx: ThreadContext, opts: RunThreadOptions): Promise; /** Record step result, register session, finalize execution; update aggregate counters. */ declare function recordStepOutcome(threadId: string, stepCtx: StepContext, result: any, ctx: ThreadContext, opts: RunThreadOptions): Promise; /** Decide whether the loop continues; run onTransition hook when transitioning. * Returns false to break the loop. */ declare function evaluateAndTransition(threadId: string, stepCtx: StepContext, ctx: ThreadContext, opts: RunThreadOptions): Promise; /** Read final artifact, flush VM, build the run result. */ declare function finalizeThread(threadId: string, ctx: ThreadContext): Promise; /** Terminate a thread by agent abort and, BEFORE onEnd hooks run, hand the owning task to the * caller's onAbort so it can reach a terminal (blocked) state in time (DR-0015 problem 2). * No-op on the task side for non-dispatch threads (metadata has no taskId) or when the caller * did not inject onAbort. Exported for unit testing. */ export declare function finalizeAbortedThread(threadId: string, meta: ThreadRecord['metadata'], reason: string | null, opts: Pick): Promise; declare function runThread(threadId: string, opts: RunThreadOptions): Promise; declare function continueThread(threadId: string, userMessage: string, opts: RunThreadOptions): Promise; /** Re-enter a thread paused by an API rate limit (status==='rate_limited'). Like resumeThread, * the userMessage is NOT overwritten — the thread re-runs its interrupted step from the original * prompt/contract. Called by resume-dispatcher when the rate-limit window resets. */ declare function resumeRateLimitedThread(threadId: string, opts: RunThreadOptions): Promise; /** Re-enter a parent thread that was suspended via [WAIT_CHILDREN]. Unlike continueThread, * the userMessage is NOT overwritten — the original contract stays in {{input}}; the child * results arrive through metadata.pendingMessages (injected by the prompt builder). */ declare function resumeThread(threadId: string, opts: RunThreadOptions): Promise; declare function buildThreadSummary(result: ThreadRunResult): string; declare function cancelActiveThread(channel: string): boolean; declare function getActiveHandle(channel: string): RunningExecution | null; export { runThread, continueThread, resumeThread, resumeRateLimitedThread, buildThreadSummary, cancelActiveThread, getActiveHandle, initThreadContext, resolveAndNotifyStep, buildStepConfig, setupStepCallbacks, executeAndAwaitAgent, recordStepOutcome, evaluateAndTransition, finalizeThread, }; export type { ThreadRunResult, ThreadContext, StepContext, StepCallbacks, StepInfo };