Skip to content

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

5.7 KB
Raw Blame
import type { ConversationDocument, RuntimeDocumentParticipantContext } from "@vetta/runtime-core";
import { selectConversationDocumentEntries } from "@vetta/runtime-core";
import type { StoredSessionEvent } from "@vetta/runtime-core/kernel";
import type {
	CodingAgentTodoRuntime as CodingAgentTodoRuntimePort,
	TodoItem,
	TodoLockSource,
	TodoSnapshot,
	TodoSnapshotEnvelope,
	TodoUpdateListener,
} from "./contracts.js";
import { parseTodoSnapshot, TODO_SNAPSHOT_TYPE } from "./todo-snapshot.js";
import { TodoState } from "./todo-state.js";

export interface CodingAgentTodoRuntimeOptions {
	readonly state?: TodoState;
	readonly createEntryId?: () => string;
	readonly now?: () => number;
}

/**
 * 一个 Session 内 Todo 的唯一状态所有者。
 *
 * Tool、Continuation 与宿主 Controller 都读取同一个 TodoStore;
 * Runtime Core 仅通过通用 Conversation Document participant 保存 custom entry。
 */
export class CodingAgentTodoRuntime implements CodingAgentTodoRuntimePort {
	private readonly state: TodoState;
	private readonly createEntryId: () => string;
	private readonly now: () => number;
	private documentContext: RuntimeDocumentParticipantContext | undefined;
	private unsubscribe: (() => void) | undefined;
	private persistenceTail: Promise<void> = Promise.resolve();
	private latestPersistence: Promise<void> = Promise.resolve();
	private readonly pendingSnapshots: TodoSnapshotEnvelope[] = [];
	private activeTurn = false;
	private restoring = false;
	private disposed = false;

	constructor(options: CodingAgentTodoRuntimeOptions = {}) {
		this.state = options.state ?? new TodoState();
		this.createEntryId = options.createEntryId ?? createTodoEntryId;
		this.now = options.now ?? Date.now;
	}

	getAll(): ReturnType<TodoState["getAll"]> {
		return this.state.getAll();
	}

	isLocked(): boolean {
		return this.state.isLocked();
	}

	getLockSource(): TodoLockSource | null {
		return this.state.getLockSource();
	}

	createMany(contents: string[]) {
		return this.state.createMany(contents);
	}

	update(id: number, status: "pending" | "in_progress" | "done") {
		return this.state.update(id, status);
	}

	subscribe(listener: TodoUpdateListener): () => void {
		return this.state.subscribe(listener);
	}

	initializeTodoItems(contents: readonly string[], lockSource?: TodoLockSource): void {
		this.state.createMany([...contents]);
		if (lockSource) this.state.lock(lockSource);
	}

	readItems(): readonly TodoItem[] {
		return this.state.getAll().map((item) => ({ ...item }));
	}

	clear(): boolean {
		if (this.state.isLocked() || this.state.getAll().length === 0) return false;
		this.state.clear();
		return true;
	}

	initialize(document: ConversationDocument, context: RuntimeDocumentParticipantContext): void {
		if (this.documentContext) {
			throw new Error("Coding Agent Todo Runtime is already initialized");
		}
		this.documentContext = context;
		const snapshot = latestTodoSnapshot(document);
		if (snapshot) this.restore(snapshot);
		this.unsubscribe = this.state.subscribe(() => {
			if (!this.restoring) this.captureSnapshot();
		});
		if (!snapshot && (this.state.getAll().length > 0 || this.state.isLocked())) {
			this.captureSnapshot();
		}
	}

	onDocumentChanged(document: ConversationDocument): void {
		// Turn 内的改动要等 toolResult 或 Turn 结束才落盘;此时 Document 里还是旧快照,
		// 直接回灌会把尚未持久化的本地状态改回去(例如续跑提醒反复复活已完成的 Todo)。
		if (this.pendingSnapshots.length > 0) return;
		const snapshot = latestTodoSnapshot(document);
		this.restore(snapshot ?? []);
	}

	async onSessionEvent(event: StoredSessionEvent): Promise<void> {
		if (event.type === "turn.started") {
			this.activeTurn = true;
			return;
		}
		if (event.type === "message.appended" && event.message.role === "toolResult") {
			this.schedulePendingSnapshots();
			await this.latestPersistence;
			return;
		}
		if (event.type === "turn.completed" || event.type === "turn.cancelled" || event.type === "turn.failed") {
			this.activeTurn = false;
			this.schedulePendingSnapshots();
			await this.latestPersistence;
		}
	}

	async flush(): Promise<void> {
		if (!this.activeTurn) this.schedulePendingSnapshots();
		await this.latestPersistence;
	}

	async dispose(): Promise<void> {
		if (this.disposed) return;
		this.disposed = true;
		this.unsubscribe?.();
		this.unsubscribe = undefined;
		this.activeTurn = false;
		await this.flush();
	}

	private captureSnapshot(): void {
		this.pendingSnapshots.push({
			items: this.state.getAll().map((item) => ({ ...item })),
			lockedBy: this.state.getLockSource(),
		});
		if (!this.activeTurn) this.schedulePendingSnapshots();
	}

	private schedulePendingSnapshots(): void {
		const context = this.documentContext;
		if (!context) return;
		for (const snapshot of this.pendingSnapshots.splice(0)) {
			const operation = this.persistenceTail.then(() =>
				context.appendCustomEntry({
					entryId: this.createEntryId(),
					customType: TODO_SNAPSHOT_TYPE,
					data: snapshot,
					timestamp: new Date(this.now()).toISOString(),
				}),
			);
			this.latestPersistence = operation;
			this.persistenceTail = operation.catch(() => undefined);
		}
	}

	private restore(snapshot: TodoSnapshot): void {
		this.restoring = true;
		try {
			this.state.restoreFromSnapshot(snapshot);
		} finally {
			this.restoring = false;
		}
	}
}

function createTodoEntryId(): string {
	return globalThis.crypto.randomUUID();
}

function latestTodoSnapshot(document: ConversationDocument): TodoSnapshot | undefined {
	for (const entry of [...selectConversationDocumentEntries(document)].reverse()) {
		if (entry.type !== "custom" || entry.customType !== TODO_SNAPSHOT_TYPE) continue;
		return parseTodoSnapshot(entry.data, entry.id);
	}
	return undefined;
}