Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

### 🔧 Bug Fixes

- **fix(github):** drop a trailing assistant prefill before dispatching to GitHub Copilot chat to avoid 400 errors. (thanks @baslr)

- **settings (model aliases — self-heal after restart):** the Settings → Routing page showed "No exact-match aliases configured" after a server restart even though the aliases were persisted in the DB. Aliases are held in a module-local `_customAliases` map in `modelDeprecation.ts` that the boot path hydrates, but Next.js compiles the app-route module graph separately from the startup graph (the same webpack chunk-splitting class as #5312), so the `GET /api/settings/model-aliases` handler read a different, un-hydrated copy. The handler now self-heals: when its in-memory alias map is empty it reads `settings.modelAliases` from the DB (via the existing `getSettings()` db module — no raw SQL in the route) and repopulates the map, so the UI reflects the persisted aliases on the first GET after a restart. Regression guard: `tests/unit/model-aliases-settings-route-selfheal.test.ts`. ([#5777](https://github.com/diegosouzapw/OmniRoute/pull/5777) — thanks [@jleonar2](https://github.com/jleonar2))

- **providers (grok-cli token auto-refresh):** grok-cli OAuth tokens were never proactively refreshed before their real expiry. `mapTokens` hardcoded `expiresIn: 21600` (6 h) regardless of the token's actual lifetime, so the persisted `expiresAt` was always "now + 6 h" and the proactive `tokenHealthCheck` sweep (refresh when `expiresAt - now < 5 min`) fired 6 h after import instead of shortly before the token really expired. `mapTokens` now computes `expiresIn` from the authoritative `expires_at` field in `~/.grok/auth.json` (ISO → epoch-seconds) with a fallback to the JWT `exp` claim (payload-only decode, no signature trust); the hardcoded `21600` is kept only when neither is present. Regression guards: 3 new cases in `tests/unit/grok-cli-oauth.test.ts` (JWT `exp`, JSON `expires_at`, and the fallback). ([#5775](https://github.com/diegosouzapw/OmniRoute/pull/5775) — thanks [@Chewji9875](https://github.com/Chewji9875))
Expand Down
25 changes: 25 additions & 0 deletions open-sse/executors/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ export class GithubExecutor extends BaseExecutor {
);
}

// GitHub Copilot's /chat/completions endpoint rejects a conversation that ends
// with an assistant message: "This model does not support assistant message
// prefill. The conversation must end with a user message." (HTTP 400). Anthropic
// clients such as newest Claude Desktop send a trailing assistant turn as a
// prefill seed — the Anthropic API honors it, but Copilot does not. Drop it here,
// scoped to the GitHub executor only (the shared translator/contextManager and
// other providers that DO honor prefill are untouched).
// Port of 9router#2143 (author: Manuel <baslr@users.noreply.github.com>).
if (Array.isArray(modifiedBody.messages)) {
modifiedBody.messages = this.dropTrailingAssistantPrefill(modifiedBody.messages);
}

// Config-driven strip of params unsupported by the target provider/model.
// For GitHub Copilot this removes Claude-style `thinking` and
// `reasoning_effort` for Claude models that reject them upstream
Expand Down Expand Up @@ -188,6 +200,19 @@ export class GithubExecutor extends BaseExecutor {
return { ...msg, content: cleanContent.length > 0 ? cleanContent : null };
}

// Remove trailing assistant message(s). GitHub Copilot's /chat/completions endpoint
// can't honor an assistant prefill and 400s unless the conversation ends with a
// non-assistant (user/tool) message. Never empties the array — an assistant-only
// conversation keeps its last message. No-op (same array reference) when the
// conversation already ends with a non-assistant message.
// Port of 9router#2143 (author: Manuel <baslr@users.noreply.github.com>).
dropTrailingAssistantPrefill(messages: any): any {
if (!Array.isArray(messages) || messages.length === 0) return messages;
let end = messages.length;
while (end > 1 && messages[end - 1]?.role === "assistant") end--;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The check for the assistant role is case-sensitive (messages[end - 1]?.role === "assistant"). However, some clients may send roles in mixed-case or uppercase (e.g., "Assistant" or "ASSISTANT"). To ensure robustness and prevent unexpected HTTP 400 errors from GitHub Copilot, the role check should be case-insensitive, similar to how it is handled in transformRequest.

Suggested change
while (end > 1 && messages[end - 1]?.role === "assistant") end--;
while (end > 1 && typeof messages[end - 1]?.role === "string" && messages[end - 1].role.toLowerCase() === "assistant") end--;

return end === messages.length ? messages : messages.slice(0, end);
}

async execute(input: ExecuteInput) {
const result = await super.execute(input);
if (!result || !result.response) return result;
Expand Down
119 changes: 119 additions & 0 deletions tests/unit/executor-github-prefill-sanitize.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import test from "node:test";
import assert from "node:assert/strict";

import { GithubExecutor } from "../../open-sse/executors/github.ts";

// GitHub Copilot's /chat/completions endpoint rejects a conversation that ends with an
// assistant message: "This model does not support assistant message prefill. The
// conversation must end with a user message." Anthropic clients (e.g. newest Claude
// Desktop) send a trailing assistant turn as a prefill seed — the Anthropic API honors
// it, but Copilot 400s. GithubExecutor.dropTrailingAssistantPrefill() strips the
// trailing assistant message(s) before dispatch, scoped to the github executor only.
// Port of 9router#2143 (author: Manuel <baslr@users.noreply.github.com>).

test("dropTrailingAssistantPrefill drops a single trailing assistant message", () => {
const executor = new GithubExecutor();
const messages = [
{ role: "user", content: "Hi" },
{ role: "assistant", content: "Here is the answer:" },
];

const out = executor.dropTrailingAssistantPrefill(messages);

assert.equal(out.length, 1);
assert.equal(out[0].role, "user");
});

test("dropTrailingAssistantPrefill drops multiple consecutive trailing assistant messages", () => {
const executor = new GithubExecutor();
const messages = [
{ role: "user", content: "Hi" },
{ role: "assistant", content: "one" },
{ role: "assistant", content: "two" },
];

const out = executor.dropTrailingAssistantPrefill(messages);

assert.equal(out.length, 1);
assert.equal(out[0].role, "user");
});

test("dropTrailingAssistantPrefill is a no-op (same reference) when the conversation ends with a user message", () => {
const executor = new GithubExecutor();
const messages = [
{ role: "user", content: "Hi" },
{ role: "assistant", content: "Hello" },
{ role: "user", content: "More" },
];

const out = executor.dropTrailingAssistantPrefill(messages);

assert.equal(out, messages, "must return the same array reference when nothing changes");
assert.equal(out.length, 3);
});

test("dropTrailingAssistantPrefill is a no-op when the conversation ends with a tool message", () => {
const executor = new GithubExecutor();
const messages = [
{ role: "user", content: "Hi" },
{ role: "assistant", content: null, tool_calls: [{ id: "x" }] },
{ role: "tool", tool_call_id: "x", content: "result" },
];

const out = executor.dropTrailingAssistantPrefill(messages);

assert.equal(out, messages, "must return the same array reference when nothing changes");
assert.equal(out.length, 3);
assert.equal(out[2].role, "tool");
});

test("dropTrailingAssistantPrefill never empties an assistant-only conversation", () => {
const executor = new GithubExecutor();
const messages = [{ role: "assistant", content: "only" }];

const out = executor.dropTrailingAssistantPrefill(messages);

assert.equal(out.length, 1, "must keep at least one message");
assert.equal(out[0].role, "assistant");
});

test("dropTrailingAssistantPrefill is null/empty safe", () => {
const executor = new GithubExecutor();

assert.deepEqual(executor.dropTrailingAssistantPrefill([]), []);
assert.equal(executor.dropTrailingAssistantPrefill(undefined), undefined);
assert.equal(executor.dropTrailingAssistantPrefill(null), null);
});

test("GithubExecutor.transformRequest drops the trailing assistant prefill end-to-end", () => {
const executor = new GithubExecutor();
const body = {
model: "claude-sonnet-4.6",
messages: [
{ role: "user", content: "Hi" },
{ role: "assistant", content: "Here is the answer:" },
],
};

const out = executor.transformRequest("claude-sonnet-4.6", body, false, {});

assert.equal(out.messages.length, 1);
assert.equal(out.messages[0].role, "user");
});

test("GithubExecutor.transformRequest leaves a user-terminated conversation untouched end-to-end", () => {
const executor = new GithubExecutor();
const body = {
model: "claude-sonnet-4.6",
messages: [
{ role: "user", content: "Hi" },
{ role: "assistant", content: "Hello" },
{ role: "user", content: "More" },
],
};

const out = executor.transformRequest("claude-sonnet-4.6", body, false, {});

assert.equal(out.messages.length, 3);
assert.equal(out.messages[2].role, "user");
});
10 changes: 10 additions & 0 deletions tests/unit/executor-github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ test("GithubExecutor.transformRequest injects JSON response instructions for Cla
reasoning_text: "internal",
reasoning_content: "internal",
},
// Trailing user turn: dropTrailingAssistantPrefill (9router#2143) strips a
// conversation that ends in "assistant", which would otherwise remove the very
// message this test inspects below. Keep the array ending in "user" so this test
// stays focused on response_format injection + reasoning-field stripping.
{ role: "user", content: "thanks" },
],
};

Expand Down Expand Up @@ -225,6 +230,11 @@ test("GithubExecutor.transformRequest leaves string content and missing content
role: "assistant",
tool_calls: [{ id: "c1", type: "function", function: { name: "f", arguments: "{}" } }],
},
// Trailing tool response: dropTrailingAssistantPrefill (9router#2143) strips a
// conversation that ends in "assistant", which would otherwise remove the very
// tool_calls message this test inspects below. A real tool round-trip ends in
// "tool", not "assistant" — model that shape instead.
{ role: "tool", tool_call_id: "c1", content: "result" },
],
};
const result = executor.transformRequest("claude-sonnet-4.6", body, true, {});
Expand Down
Loading