Fix X-Initiator header logic for GitHub Copilot

Check last message role instead of any message in history.
This matches the original correct implementation from PR #200.

fixes #209
This commit is contained in:
Mario Zechner 2025-12-19 05:08:28 +01:00
parent 13b8af1f36
commit 575dcb2676
5 changed files with 18 additions and 14 deletions

View file

@ -302,9 +302,11 @@ function createClient(model: Model<"openai-completions">, context: Context, apiK
const headers = { ...model.headers };
if (model.provider === "github-copilot") {
// Copilot expects X-Initiator to indicate whether the request is user-initiated
// or agent-initiated. It's an agent call if ANY message in history has assistant/tool role.
// or agent-initiated (e.g. follow-up after assistant/tool messages). If there is
// no prior message, default to user-initiated.
const messages = context.messages || [];
const isAgentCall = messages.some((msg) => msg.role === "assistant" || msg.role === "toolResult");
const lastMessage = messages[messages.length - 1];
const isAgentCall = lastMessage ? lastMessage.role !== "user" : false;
headers["X-Initiator"] = isAgentCall ? "agent" : "user";
headers["Openai-Intent"] = "conversation-edits";
}

View file

@ -310,9 +310,11 @@ function createClient(model: Model<"openai-responses">, context: Context, apiKey
const headers = { ...model.headers };
if (model.provider === "github-copilot") {
// Copilot expects X-Initiator to indicate whether the request is user-initiated
// or agent-initiated. It's an agent call if ANY message in history has assistant/tool role.
// or agent-initiated (e.g. follow-up after assistant/tool messages). If there is
// no prior message, default to user-initiated.
const messages = context.messages || [];
const isAgentCall = messages.some((msg) => msg.role === "assistant" || msg.role === "toolResult");
const lastMessage = messages[messages.length - 1];
const isAgentCall = lastMessage ? lastMessage.role !== "user" : false;
headers["X-Initiator"] = isAgentCall ? "agent" : "user";
headers["Openai-Intent"] = "conversation-edits";
}