Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/ultra/agent-prompt.ts

Raw
export interface AgentPromptIdentity {
	workflow: string;
	phase: string;
	index: number;
}

export interface AgentPromptSource {
	agentId: string;
	task: string;
}

function escapeAttribute(value: string): string {
	return value
		.replaceAll("&", "&")
		.replaceAll('"', """)
		.replaceAll("<", "&lt;")
		.replaceAll(">", "&gt;");
}

function cdata(value: string): string {
	return `<![CDATA[${value.replaceAll("]]>", "]]]]><![CDATA[>")}]]>`;
}

export function quoteAgentOutput(
	content: string,
	sources: AgentPromptSource[],
): string {
	if (sources.length === 0) return content;
	const sourceLines = sources.map(
		(source) =>
			`    <source agent="${escapeAttribute(source.agentId)}" task="${escapeAttribute(source.task)}"/>`,
	);
	return [
		"<quote>",
		...sourceLines,
		`    <content>${cdata(content)}</content>`,
		"</quote>",
	].join("\n");
}

export function assignmentPrompt(
	identity: AgentPromptIdentity,
	assignment: string,
): string {
	const agentId = `${identity.phase}#${identity.index}`;
	return [
		`<agent id="${escapeAttribute(agentId)}" workflow="${escapeAttribute(identity.workflow)}" phase="${escapeAttribute(identity.phase)}" index="${identity.index}">`,
		'  <message from="orchestrator" kind="assignment">',
		assignment,
		"  </message>",
		"</agent>",
	].join("\n");
}

export function resumePrompt(
	agentId: string,
	phase: string,
	message: string,
): string {
	return [
		`<agent id="${escapeAttribute(agentId)}" phase="${escapeAttribute(phase)}">`,
		'  <message from="ultra" kind="resume">',
		message,
		"  </message>",
		"</agent>",
	].join("\n");
}