fix(ai): append system prompt to codex bridge message instead of converting to input
Previously the system prompt was converted to an input message in convertMessages, then stripped out by filterPiSystemPrompts. Now the system prompt is passed directly to transformRequestBody and appended after CODEX_PI_BRIDGE in the bridge message.
This commit is contained in:
@@ -123,6 +123,7 @@ export const streamOpenAICodexResponses: StreamFunction<"openai-codex-responses"
|
||||
codexInstructions,
|
||||
codexOptions,
|
||||
options?.codexMode ?? true,
|
||||
context.systemPrompt,
|
||||
);
|
||||
|
||||
const headers = createCodexHeaders(model.headers, accountId, apiKey, transformedBody.prompt_cache_key);
|
||||
@@ -477,14 +478,6 @@ function convertMessages(model: Model<"openai-codex-responses">, context: Contex
|
||||
|
||||
const transformedMessages = transformMessages(context.messages, model);
|
||||
|
||||
if (context.systemPrompt) {
|
||||
const role = model.reasoning ? "developer" : "system";
|
||||
messages.push({
|
||||
role,
|
||||
content: sanitizeSurrogates(context.systemPrompt),
|
||||
});
|
||||
}
|
||||
|
||||
let msgIndex = 0;
|
||||
for (const msg of transformedMessages) {
|
||||
if (msg.role === "user") {
|
||||
|
||||
@@ -43,4 +43,6 @@ You are running Codex through pi, a terminal coding assistant. The tools and rul
|
||||
1. Using edit, not apply_patch
|
||||
2. No plan tools used
|
||||
3. Only the tools listed above are called
|
||||
|
||||
Below are additional system instruction you MUST follow when responding:
|
||||
`;
|
||||
|
||||
@@ -210,45 +210,22 @@ function filterInput(input: InputItem[] | undefined): InputItem[] | undefined {
|
||||
});
|
||||
}
|
||||
|
||||
function getContentText(item: InputItem): string {
|
||||
if (typeof item.content === "string") {
|
||||
return item.content;
|
||||
}
|
||||
if (Array.isArray(item.content)) {
|
||||
return item.content
|
||||
.filter((c) => typeof c === "object" && c !== null && (c as { type?: string }).type === "input_text")
|
||||
.map((c) => (c as { text?: string }).text)
|
||||
.filter((text): text is string => typeof text === "string")
|
||||
.join("\n");
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function isPiSystemPrompt(item: InputItem): boolean {
|
||||
const isSystemRole = item.role === "developer" || item.role === "system";
|
||||
if (!isSystemRole) return false;
|
||||
const contentText = getContentText(item).trim();
|
||||
if (!contentText) return false;
|
||||
return contentText.startsWith(
|
||||
"You are an expert coding assistant. You help users with coding tasks by reading files, executing commands",
|
||||
);
|
||||
}
|
||||
|
||||
async function filterPiSystemPrompts(input: InputItem[] | undefined): Promise<InputItem[] | undefined> {
|
||||
if (!Array.isArray(input)) return input;
|
||||
return input.filter((item) => item.role === "user" || !isPiSystemPrompt(item));
|
||||
}
|
||||
|
||||
function addCodexBridgeMessage(input: InputItem[] | undefined, hasTools: boolean): InputItem[] | undefined {
|
||||
function addCodexBridgeMessage(
|
||||
input: InputItem[] | undefined,
|
||||
hasTools: boolean,
|
||||
systemPrompt?: string,
|
||||
): InputItem[] | undefined {
|
||||
if (!hasTools || !Array.isArray(input)) return input;
|
||||
|
||||
const bridgeText = systemPrompt ? `${CODEX_PI_BRIDGE}\n\n${systemPrompt}` : CODEX_PI_BRIDGE;
|
||||
|
||||
const bridgeMessage: InputItem = {
|
||||
type: "message",
|
||||
role: "developer",
|
||||
content: [
|
||||
{
|
||||
type: "input_text",
|
||||
text: CODEX_PI_BRIDGE,
|
||||
text: bridgeText,
|
||||
},
|
||||
],
|
||||
};
|
||||
@@ -278,6 +255,7 @@ export async function transformRequestBody(
|
||||
codexInstructions: string,
|
||||
options: CodexRequestOptions = {},
|
||||
codexMode = true,
|
||||
systemPrompt?: string,
|
||||
): Promise<RequestBody> {
|
||||
const normalizedModel = normalizeModel(body.model);
|
||||
|
||||
@@ -290,8 +268,7 @@ export async function transformRequestBody(
|
||||
body.input = filterInput(body.input);
|
||||
|
||||
if (codexMode) {
|
||||
body.input = await filterPiSystemPrompts(body.input);
|
||||
body.input = addCodexBridgeMessage(body.input, !!body.tools);
|
||||
body.input = addCodexBridgeMessage(body.input, !!body.tools, systemPrompt);
|
||||
} else {
|
||||
body.input = addToolRemapMessage(body.input, !!body.tools);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user