chore: simplify codex prompt handling

This commit is contained in:
Mario Zechner 2026-01-17 21:53:01 +01:00
parent 94bd7f69fd
commit 4068bc556a
8 changed files with 33 additions and 87 deletions

View file

@ -1,13 +0,0 @@
/**
* Static Pi instructions for OpenAI Codex.
* This string is whitelisted by OpenAI and must not change.
*/
export const PI_STATIC_INSTRUCTIONS = `You are pi, an expert coding assistant. You help users with coding tasks by reading files, executing commands, editing code, and writing new files.
Pi specific Documentation:
- Main documentation: pi-internal://README.md
- Additional docs: pi-internal://docs
- Examples: pi-internal://examples (extensions, custom tools, SDK)
- When asked to create: custom models/providers (README.md), extensions (docs/extensions.md, examples/extensions/), themes (docs/theme.md), skills (docs/skills.md), TUI components (docs/tui.md - has copy-paste patterns)
- Always read the doc, examples, AND follow .md cross-references before implementing
`;

View file

@ -1,4 +1,3 @@
export * from "./constants.js";
export * from "./models.js";
export * from "./providers/anthropic.js";
export * from "./providers/google.js";

View file

@ -4,7 +4,6 @@ import type {
ResponseOutputMessage,
ResponseReasoningItem,
} from "openai/resources/responses/responses.js";
import { PI_STATIC_INSTRUCTIONS } from "../constants.js";
import { calculateCost } from "../models.js";
import { getEnvApiKey } from "../stream.js";
import type {
@ -215,22 +214,14 @@ function buildRequestBody(
context: Context,
options?: OpenAICodexResponsesOptions,
): RequestBody {
const systemPrompt = buildSystemPrompt(context.systemPrompt);
const messages = convertMessages(model, context);
// Prepend developer messages
const developerMessages = systemPrompt.developerMessages.map((text) => ({
type: "message",
role: "developer",
content: [{ type: "input_text", text }],
}));
const body: RequestBody = {
model: model.id,
store: false,
stream: true,
instructions: systemPrompt.instructions,
input: [...developerMessages, ...messages],
instructions: context.systemPrompt,
input: messages,
text: { verbosity: options?.textVerbosity || "medium" },
include: ["reasoning.encrypted_content"],
prompt_cache_key: options?.sessionId,
@ -262,23 +253,6 @@ function buildRequestBody(
return body;
}
function buildSystemPrompt(userSystemPrompt?: string): { instructions: string; developerMessages: string[] } {
// PI_STATIC_INSTRUCTIONS is whitelisted and must be in the instructions field.
// User's system prompt goes in developer messages, with the static prefix stripped.
const staticPrefix = PI_STATIC_INSTRUCTIONS.trim();
const developerMessages: string[] = [];
if (userSystemPrompt?.trim()) {
let dynamicPart = userSystemPrompt.trim();
if (dynamicPart.startsWith(staticPrefix)) {
dynamicPart = dynamicPart.slice(staticPrefix.length).trim();
}
if (dynamicPart) developerMessages.push(dynamicPart);
}
return { instructions: staticPrefix, developerMessages };
}
function clampReasoningEffort(modelId: string, effort: string): string {
const id = modelId.includes("/") ? modelId.split("/").pop()! : modelId;
if (id.startsWith("gpt-5.2") && effort === "minimal") return "low";