Skip to content

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

8.2 KB
Raw Blame
/** Structured system-prompt document and deterministic mutation/rendering operations. */

import type { PromptCacheSystemPromptBlockSpan } from "@vetta/ai";
import type {
	SystemPromptBlock,
	SystemPromptBlockType,
	SystemPromptOperation,
} from "../public-api/sdk/sdk-system-prompt-contract.js";

export type {
	SystemPromptBlock,
	SystemPromptBlockPatch,
	SystemPromptBlockType,
	SystemPromptContribution,
	SystemPromptOperation,
} from "../public-api/sdk/sdk-system-prompt-contract.js";

export interface SystemPromptDraft {
	blocks: SystemPromptBlock[];
	metadata: {
		cwd: string;
		dateTime: string;
		promptBudgetTokens?: number;
	};
}

export interface SystemPromptBlockDiagnostics {
	readonly id: string;
	readonly type: SystemPromptBlockType;
	readonly source: SystemPromptBlock["source"];
	readonly charCount: number;
	readonly estimatedTokens: number;
}

export interface SystemPromptDiagnostics {
	readonly charCount: number;
	readonly estimatedTokens: number;
	readonly enabledBlockCount: number;
	readonly promptBudgetTokens?: number;
	readonly overBudget: boolean;
	readonly blocks: readonly SystemPromptBlockDiagnostics[];
}

/**
 * 稳定段与易变段的 priority 分界。
 *
 * 核心块以该优先级推断默认 cacheability。插件块必须显式声明 stable,否则一律视为 volatile。
 */
export const STABLE_SYSTEM_PROMPT_PRIORITY = 800;

export interface CompiledSystemPrompt {
	readonly content: string;
	/**
	 * `content` 中稳定前缀的字符长度,满足 `content.slice(0, stableLength)` 恰好等于
	 * 排序后连续 stable 块按同一顺序 join("\n\n") 的结果。首个 volatile 块之后不再跨段缓存。
	 *
	 * 块间分隔符 `"\n\n"` 归属其后的块,因此两段都非空时 `content.slice(stableLength)` 以 `"\n\n"` 开头,
	 * 两段拼回必然等于 `content`。全部块落在同一段时取 `content.length` 或 `0`。
	 */
	readonly stableLength: number;
	/** Ordered block spans for privacy-safe cache change diagnostics. */
	readonly promptCacheBlocks: readonly PromptCacheSystemPromptBlockSpan[];
	readonly diagnostics: SystemPromptDiagnostics;
}

export function coreBlock(
	id: string,
	type: SystemPromptBlockType,
	content: string,
	priority: number,
): SystemPromptBlock {
	return {
		id,
		type,
		source: { kind: "core" },
		content,
		priority,
		enabled: content.length > 0,
		cacheability: priority < STABLE_SYSTEM_PROMPT_PRIORITY ? "stable" : "volatile",
	};
}

export function applySystemPromptOperation(
	draft: SystemPromptDraft,
	pluginId: string,
	operation: SystemPromptOperation,
): void {
	assertSystemPromptDraft(draft);
	switch (operation.type) {
		case "addBlock": {
			assertPluginBlockIdAvailable(draft, operation.block.id);
			draft.blocks.push({
				...operation.block,
				source: { kind: "plugin", pluginId },
			});
			return;
		}
		case "replaceBlock": {
			const index = draft.blocks.findIndex((block) => block.id === operation.blockId);
			if (index < 0 && isCoreBlockId(operation.blockId)) {
				throw new Error(`Cannot replace missing core system prompt block: ${operation.blockId}`);
			}
			const nextBlock: SystemPromptBlock = {
				...operation.block,
				id: operation.blockId,
				source: { kind: "plugin", pluginId },
			};
			if (index >= 0) {
				draft.blocks[index] = nextBlock;
			} else {
				draft.blocks.push(nextBlock);
			}
			return;
		}
		case "updateBlock": {
			const block = draft.blocks.find((candidate) => candidate.id === operation.blockId);
			if (block) {
				const { type, content, priority, enabled, cacheability } = operation.patch;
				if (type !== undefined) block.type = type;
				if (content !== undefined) block.content = content;
				if (priority !== undefined) block.priority = priority;
				if (enabled !== undefined) block.enabled = enabled;
				if (cacheability !== undefined) block.cacheability = cacheability;
			}
			return;
		}
		case "removeBlock":
			draft.blocks = draft.blocks.filter((block) => block.id !== operation.blockId);
			return;
		case "setBlockEnabled": {
			const block = draft.blocks.find((candidate) => candidate.id === operation.blockId);
			if (block) {
				block.enabled = operation.enabled;
			}
			return;
		}
	}
}

export function applySystemPromptOperations(
	draft: SystemPromptDraft,
	pluginId: string,
	operations: readonly SystemPromptOperation[],
): SystemPromptDraft {
	const nextDraft: SystemPromptDraft = {
		blocks: draft.blocks.map((block) => ({ ...block, source: { ...block.source } })),
		metadata: { ...draft.metadata },
	};
	for (const operation of operations) {
		applySystemPromptOperation(nextDraft, pluginId, operation);
	}
	assertSystemPromptDraft(nextDraft);
	return nextDraft;
}

export function compileSystemPromptDraft(draft: SystemPromptDraft): CompiledSystemPrompt {
	assertSystemPromptDraft(draft);
	const enabledBlocks = draft.blocks
		.filter((block) => block.enabled && block.content.length > 0)
		.sort((a, b) => a.priority - b.priority || a.id.localeCompare(b.id));
	const content = enabledBlocks.map((block) => block.content).join("\n\n");
	const stableBlockCount = firstVolatileBlockIndex(enabledBlocks);
	const stableLength = enabledBlocks
		.slice(0, stableBlockCount)
		.map((block) => block.content)
		.join("\n\n").length;
	let blockEnd = 0;
	const promptCacheBlocks = enabledBlocks.map<PromptCacheSystemPromptBlockSpan>((block, index) => {
		const start = blockEnd + (index === 0 ? 0 : 2);
		blockEnd = start + block.content.length;
		return {
			id: block.id,
			start,
			length: block.content.length,
			cacheability: index < stableBlockCount ? "stable" : "volatile",
		};
	});
	const estimatedTokens = estimateTextTokens(content);
	return {
		content,
		stableLength,
		promptCacheBlocks,
		diagnostics: {
			charCount: content.length,
			estimatedTokens,
			enabledBlockCount: enabledBlocks.length,
			promptBudgetTokens: draft.metadata.promptBudgetTokens,
			overBudget:
				draft.metadata.promptBudgetTokens !== undefined && estimatedTokens > draft.metadata.promptBudgetTokens,
			blocks: enabledBlocks.map((block) => ({
				id: block.id,
				type: block.type,
				source: { ...block.source },
				charCount: block.content.length,
				estimatedTokens: estimateTextTokens(block.content),
			})),
		},
	};
}

function firstVolatileBlockIndex(blocks: readonly SystemPromptBlock[]): number {
	const index = blocks.findIndex((block) => !isCacheStable(block));
	return index < 0 ? blocks.length : index;
}

function isCacheStable(block: SystemPromptBlock): boolean {
	if (block.cacheability !== undefined) return block.cacheability === "stable";
	return block.source.kind === "core" && block.priority < STABLE_SYSTEM_PROMPT_PRIORITY;
}

export function renderSystemPromptDraft(draft: SystemPromptDraft): string {
	return compileSystemPromptDraft(draft).content;
}

export function assertSystemPromptDraft(draft: SystemPromptDraft): void {
	if (
		draft.metadata.promptBudgetTokens !== undefined &&
		(!Number.isFinite(draft.metadata.promptBudgetTokens) || draft.metadata.promptBudgetTokens <= 0)
	) {
		throw new Error("System prompt token budget must be a positive finite number");
	}
	const ids = new Set<string>();
	for (const block of draft.blocks) {
		if (!block.id.trim()) throw new Error("System prompt block id cannot be empty");
		if (ids.has(block.id)) throw new Error(`Duplicate system prompt block id: ${block.id}`);
		if (!Number.isFinite(block.priority)) {
			throw new Error(`System prompt block priority must be finite: ${block.id}`);
		}
		if (block.source.kind === "core") {
			if (!isCoreBlockId(block.id) || block.source.pluginId !== undefined) {
				throw new Error(`Invalid core system prompt block provenance: ${block.id}`);
			}
		} else if (!block.source.pluginId?.trim()) {
			throw new Error(`Plugin system prompt block is missing pluginId: ${block.id}`);
		}
		ids.add(block.id);
	}
}

function estimateTextTokens(content: string): number {
	return Math.ceil(content.length / 4);
}

function assertPluginBlockIdAvailable(draft: SystemPromptDraft, blockId: string): void {
	if (isCoreBlockId(blockId)) throw new Error(`Plugin cannot add reserved core system prompt block: ${blockId}`);
	if (draft.blocks.some(({ id }) => id === blockId)) {
		throw new Error(`Duplicate system prompt block id: ${blockId}`);
	}
}

function isCoreBlockId(blockId: string): boolean {
	return blockId.startsWith("core.");
}