Skip to content

Open-source, local-first AI agent for real work.

2.6 KB
Raw Blame
export type BackgroundCommandStatus = "running" | "completed" | "failed" | "killed";
export type BackgroundCommandStopReason = "caller" | "agent" | "dispose";

/**
 * `command`: a shell command the service spawned itself.
 * `detached-process`: processes a command left running in its process group after the shell
 * exited (`cmd &`, `nohup`, `disown`). The service adopts them so they stay visible and stoppable;
 * they are not stopped when the session is disposed.
 */
export type BackgroundCommandKind = "command" | "detached-process";

export interface BackgroundCommandSnapshot {
	readonly id: string;
	readonly command: string;
	readonly cwd: string;
	readonly status: BackgroundCommandStatus;
	readonly outputFile: string;
	readonly exitCode: number | undefined;
	readonly startedAt: number;
	readonly endedAt?: number;
	readonly toolCallId?: string;
	readonly tail: string;
	readonly endedBy?: BackgroundCommandStopReason;
	/** Absent means `command`. */
	readonly kind?: BackgroundCommandKind;
	/** Process group that a `detached-process` task tracks. */
	readonly processGroupId?: number;
	/** Set on a command whose leftover processes were adopted as this `detached-process` task. */
	readonly detachedTaskId?: string;
}

export type BackgroundCommandEvent =
	| { readonly type: "task_started"; readonly task: BackgroundCommandSnapshot }
	| { readonly type: "task_output"; readonly task: BackgroundCommandSnapshot }
	| { readonly type: "task_ended"; readonly task: BackgroundCommandSnapshot }
	| { readonly type: "tasks_cleared" };

export interface SpawnBackgroundCommandOptions {
	readonly command: string;
	readonly cwd: string;
	readonly env: Readonly<Record<string, string | undefined>>;
	readonly toolCallId?: string;
	readonly notifyOnlyIfPromoted?: boolean;
}

export interface ReadBackgroundCommandOutputOptions {
	readonly fromStart: boolean;
	readonly advanceCursor: boolean;
}

export interface BackgroundCommandService {
	spawn(options: SpawnBackgroundCommandOptions): BackgroundCommandSnapshot;
	subscribe(listener: (event: BackgroundCommandEvent) => void): () => void;
	subscribeNotifications(listener: (task: BackgroundCommandSnapshot) => void): () => void;
	wait(
		taskId: string,
		options: { readonly maxMs: number; readonly signal?: AbortSignal },
	): Promise<{ readonly stillRunning: boolean; readonly snapshot: BackgroundCommandSnapshot }>;
	get(taskId: string): BackgroundCommandSnapshot | undefined;
	list(): readonly BackgroundCommandSnapshot[];
	clearFinished(): number;
	readOutput(taskId: string, options: ReadBackgroundCommandOutputOptions): string;
	stop(taskId: string, reason?: BackgroundCommandStopReason): boolean;
	dispose(): void;
	shutdown(): Promise<void>;
}