Skip to content

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

9.3 KB
Raw Blame
import type { ImageContent, TextContent } from "@vetta/ai";
import type { RuntimeActiveSessionHost, RuntimeHostSession } from "@vetta/runtime-core";
import type { SessionContextRecord } from "@vetta/runtime-core/kernel";
import { createCodingAgentExtensionSessionViewFromSource } from "../../adapters/extensions/runtime-session-view-adapter.js";
import type { CodingAgentRuntimeSessionOptions } from "../../composition/contracts/index.js";
import type {
	CodingAgentBashOperations,
	CodingAgentBashResult,
	CodingAgentNewSessionOptions,
	CodingAgentTreeNavigationOptions,
	CodingAgentTreeNavigationResult,
} from "../../public-api/sdk/sdk-session-contract.js";
import type { CodingAgentSdkActiveSessionCapabilityPort } from "./runtime-contracts.js";

export interface CodingAgentSdkBashPort {
	execute(
		session: RuntimeHostSession,
		command: string,
		onChunk?: (chunk: string) => void,
		options?: { readonly excludeFromContext?: boolean; readonly operations?: CodingAgentBashOperations },
	): Promise<CodingAgentBashResult>;
	record(
		session: RuntimeHostSession,
		command: string,
		result: CodingAgentBashResult,
		options?: { readonly excludeFromContext?: boolean },
	): Promise<void>;
	abort(): void;
	readonly isRunning: boolean;
	hasPending(sessionId: string): boolean;
	quiesce(session: RuntimeHostSession): Promise<void>;
	dispose(): Promise<void>;
}

export interface CodingAgentSdkTreeNavigationPort {
	navigateTree(targetId: string, options?: CodingAgentTreeNavigationOptions): Promise<CodingAgentTreeNavigationResult>;
	abortBranchSummary(): void;
}

export interface CodingAgentSdkActiveSessionCapabilityHostOptions {
	readonly sessionHost: Pick<
		RuntimeActiveSessionHost<CodingAgentRuntimeSessionOptions, RuntimeHostSession>,
		| "fork"
		| "newSession"
		| "readSession"
		| "runActiveSessionMutation"
		| "startActiveSessionOperation"
		| "switchSession"
	>;
	readonly createSessionSetupInitializer?: (
		setup: NonNullable<CodingAgentNewSessionOptions["setup"]>,
	) => NonNullable<
		Parameters<RuntimeActiveSessionHost<CodingAgentRuntimeSessionOptions, RuntimeHostSession>["newSession"]>[0]
	>["seedInitializer"];
	readonly treeNavigation?: CodingAgentSdkTreeNavigationPort;
	readonly bash?: CodingAgentSdkBashPort;
}

/** SDK 活动会话命令到 Runtime 历史、上下文和身份事务端口的适配器。 */
export class CodingAgentSdkActiveSessionCapabilityHost implements CodingAgentSdkActiveSessionCapabilityPort {
	constructor(private readonly options: CodingAgentSdkActiveSessionCapabilityHostOptions) {}

	getSessionBranch() {
		return createCodingAgentExtensionSessionViewFromSource(this.readSession()).getBranch();
	}

	sendCustomMessage<T = unknown>(
		message: {
			readonly customType: string;
			readonly content: string | readonly (TextContent | ImageContent)[];
			readonly display: boolean;
			readonly details?: T;
		},
		options?: { readonly triggerTurn?: boolean; readonly deliverAs?: "steer" | "followUp" | "nextTurn" },
	): Promise<void> {
		return this.options.sessionHost.startActiveSessionOperation(async (session) => {
			const mode = resolveCustomMessageDeliveryMode(session, options);
			const record: SessionContextRecord = {
				type: message.customType,
				content: normalizeContent(message.content),
				modelVisible: true,
				display: message.display,
				metadata: message.details,
				timestamp: Date.now(),
			};
			await session.deliverContext([record], mode);
		});
	}

	sendUserMessage(
		content: string | readonly (TextContent | ImageContent)[],
		options?: { readonly deliverAs?: "steer" | "followUp" },
	): Promise<void> {
		const normalized = normalizeUserContent(content);
		return this.options.sessionHost.startActiveSessionOperation(async (session) => {
			await session.prompt({
				text: normalized.text,
				images: normalized.images,
				streamingBehavior: options?.deliverAs,
			});
		});
	}

	async newSession(options?: CodingAgentNewSessionOptions): Promise<boolean> {
		const seedInitializer = options?.setup ? this.options.createSessionSetupInitializer?.(options.setup) : undefined;
		if (options?.setup && !seedInitializer) {
			throw new Error("SDK session setup capability is unavailable");
		}
		const result = await this.options.sessionHost.newSession({
			...(options?.parentSession ? { parentSession: options.parentSession } : {}),
			...(seedInitializer ? { seedInitializer } : {}),
		});
		return !result.cancelled;
	}

	abortBranchSummary(): void {
		this.options.treeNavigation?.abortBranchSummary();
	}

	executeBash(
		command: string,
		onChunk?: (chunk: string) => void,
		options?: { readonly excludeFromContext?: boolean; readonly operations?: CodingAgentBashOperations },
	): Promise<CodingAgentBashResult> {
		const bash = this.requireBash();
		return this.options.sessionHost.startActiveSessionOperation((session) =>
			bash.execute(session, command, onChunk, options),
		);
	}

	recordBashResult(
		command: string,
		result: CodingAgentBashResult,
		options?: { readonly excludeFromContext?: boolean },
	): Promise<void> {
		const bash = this.requireBash();
		return this.options.sessionHost.runActiveSessionMutation((session) =>
			bash.record(session, command, result, options),
		);
	}

	abortBash(): void {
		this.options.bash?.abort();
	}

	get isBashRunning(): boolean {
		return this.options.bash?.isRunning ?? false;
	}

	get hasPendingBashMessages(): boolean {
		return this.options.bash?.hasPending(this.readSession().sessionId) ?? false;
	}

	async switchSession(sessionPath: string): Promise<boolean> {
		return !(await this.options.sessionHost.switchSession(sessionPath)).cancelled;
	}

	async fork(entryId: string): Promise<{ selectedText: string; cancelled: boolean }> {
		const result = await this.options.sessionHost.fork(entryId);
		return { selectedText: result.text, cancelled: result.cancelled };
	}

	navigateTree(
		targetId: string,
		options?: CodingAgentTreeNavigationOptions,
	): Promise<CodingAgentTreeNavigationResult> {
		if (!this.options.treeNavigation) {
			throw new Error("SDK tree navigation capability is unavailable");
		}
		return this.options.treeNavigation.navigateTree(targetId, options);
	}

	switchBranch(targetId: string): Promise<{ leafId: string }> {
		return this.options.sessionHost.runActiveSessionMutation((session) => session.switchBranch(targetId));
	}

	appendBranchSummary(
		parentId: string | null,
		summary: string,
		details?: unknown,
		fromHook?: boolean,
	): Promise<{ entryId: string }> {
		return this.options.sessionHost.runActiveSessionMutation((session) =>
			session.appendBranchSummary(parentId, summary, details, fromHook),
		);
	}

	deleteMessage(entryId: string): Promise<{ leafId: string | null }> {
		return this.options.sessionHost.runActiveSessionMutation((session) => session.deleteMessage(entryId));
	}

	replaceLastUserMessage(entryId: string): Promise<{ leafId: string | null }> {
		return this.options.sessionHost.runActiveSessionMutation((session) => session.replaceLastUserMessage(entryId));
	}

	exportForkToNewFile(entryId: string): Promise<{ path: string; text: string }> {
		return this.options.sessionHost.runActiveSessionMutation((session) => session.forkSession(entryId));
	}

	getUserMessagesForForking(): readonly { entryId: string; text: string }[] {
		return createCodingAgentExtensionSessionViewFromSource(this.readSession())
			.getEntries()
			.flatMap((entry) => {
				if (entry.type !== "message" || entry.message.role !== "user") return [];
				const text = readContentText(entry.message.content);
				return text ? [{ entryId: entry.id, text }] : [];
			});
	}

	async quiesceIdentity(): Promise<void> {
		const bash = this.options.bash;
		if (bash) await bash.quiesce(this.readSession());
	}

	dispose(): Promise<void> {
		return this.options.bash?.dispose() ?? Promise.resolve();
	}

	private readSession(): RuntimeHostSession {
		return this.options.sessionHost.readSession();
	}

	private requireBash(): CodingAgentSdkBashPort {
		if (!this.options.bash) throw new Error("SDK Bash capability is unavailable");
		return this.options.bash;
	}
}

function resolveCustomMessageDeliveryMode(
	session: RuntimeHostSession,
	options: { readonly triggerTurn?: boolean; readonly deliverAs?: "steer" | "followUp" | "nextTurn" } | undefined,
) {
	if (options?.deliverAs === "nextTurn") return "nextTurn" as const;
	if (session.readState().isStreaming) return options?.deliverAs === "followUp" ? "followUp" : "steer";
	return options?.triggerTurn ? "triggerTurn" : "record";
}

function normalizeContent(content: string | readonly (TextContent | ImageContent)[]): (TextContent | ImageContent)[] {
	return typeof content === "string" ? [{ type: "text", text: content }] : [...content];
}

function normalizeUserContent(content: string | readonly (TextContent | ImageContent)[]): {
	readonly text: string;
	readonly images: ImageContent[] | undefined;
} {
	if (typeof content === "string") return { text: content, images: undefined };
	const text = content
		.filter((part): part is TextContent => part.type === "text")
		.map((part) => part.text)
		.join("\n");
	const images = content.filter((part): part is ImageContent => part.type === "image");
	return { text, images: images.length > 0 ? images : undefined };
}

function readContentText(content: unknown): string {
	if (typeof content === "string") return content;
	if (!Array.isArray(content)) return "";
	return content
		.map((part) =>
			typeof part === "object" && part !== null && typeof Reflect.get(part, "text") === "string"
				? Reflect.get(part, "text")
				: "",
		)
		.join("");
}