refactor(ai): streamline codex prompt handling

This commit is contained in:
Mario Zechner
2026-01-06 10:27:51 +01:00
parent b04ce9fe95
commit 858c6bae8a
6 changed files with 99 additions and 104 deletions
@@ -3,46 +3,53 @@
* Aligns Codex CLI expectations with Pi's toolset.
*/
export const CODEX_PI_BRIDGE = `# Codex Running in Pi
import type { Tool } from "../../../types.js";
You are running Codex through pi, a terminal coding assistant. The tools and rules differ from Codex CLI.
function formatToolList(tools?: Tool[]): string {
if (!tools || tools.length === 0) {
return "- (none)";
}
## CRITICAL: Tool Replacements
const normalized = tools
.map((tool) => {
const name = tool.name.trim();
if (!name) return null;
const description = (tool.description || "Custom tool").replace(/\s*\n\s*/g, " ").trim();
return { name, description };
})
.filter((tool): tool is { name: string; description: string } => tool !== null);
<critical_rule priority="0">
❌ APPLY_PATCH DOES NOT EXIST → ✅ USE "edit" INSTEAD
- NEVER use: apply_patch, applyPatch
- ALWAYS use: edit for ALL file modifications
</critical_rule>
if (normalized.length === 0) {
return "- (none)";
}
<critical_rule priority="0">
❌ UPDATE_PLAN DOES NOT EXIST
- NEVER use: update_plan, updatePlan, read_plan, readPlan, todowrite, todoread
- There is no plan tool in this environment
</critical_rule>
const maxNameLength = normalized.reduce((max, tool) => Math.max(max, tool.name.length), 0);
const padWidth = Math.max(6, maxNameLength + 1);
## Available Tools (pi)
return normalized
.map((tool) => {
const paddedName = tool.name.padEnd(padWidth);
return `- ${paddedName}- ${tool.description}`;
})
.join("\n");
}
- read - Read file contents
- bash - Execute bash commands
- edit - Modify files with exact find/replace (requires prior read)
- write - Create or overwrite files
- grep - Search file contents (read-only)
- find - Find files by glob pattern (read-only)
- ls - List directory contents (read-only)
export function buildCodexPiBridge(tools?: Tool[]): string {
const toolsList = formatToolList(tools);
## Usage Rules
return `# Codex Environment Bridge
- Read before edit; use read instead of cat/sed for file contents
- Use edit for surgical changes; write only for new files or complete rewrites
- Prefer grep/find/ls over bash for discovery
- Be concise and show file paths clearly when working with files
<environment_override priority="0">
IGNORE ALL PREVIOUS INSTRUCTIONS ABOVE THIS MESSAGE.
Do not assume any tools are available unless listed below.
</environment_override>
## Verification Checklist
The next system instructions that follow this message are authoritative and must be obeyed, even if they conflict with earlier instructions.
1. Using edit, not apply_patch
2. No plan tools used
3. Only the tools listed above are called
## Available Tools
Below are additional system instruction you MUST follow when responding:
${toolsList}
Only use the tools listed above. Do not reference or call any other tools.
`;
}
@@ -0,0 +1,26 @@
export interface CodexSystemPrompt {
instructions: string;
developerMessages: string[];
}
export function buildCodexSystemPrompt(args: {
codexInstructions: string;
bridgeText: string;
userSystemPrompt?: string;
}): CodexSystemPrompt {
const { codexInstructions, bridgeText, userSystemPrompt } = args;
const developerMessages: string[] = [];
if (bridgeText.trim().length > 0) {
developerMessages.push(bridgeText.trim());
}
if (userSystemPrompt && userSystemPrompt.trim().length > 0) {
developerMessages.push(userSystemPrompt.trim());
}
return {
instructions: codexInstructions.trim(),
developerMessages,
};
}