Skip to content

Commit 8c1abc4

Browse files
committed
fix(adapters): frame AgentRouter first messages to pass the language filter
AgentRouter's gateway applies a language filter to the first user message and hard-fails non-English prompts with 400 content-blocked (#2074) — the 400 surfaced mid-session as a hard failure for any Portuguese/Spanish/etc. first prompt routed to an AgentRouter-backed provider. Prepend an explicit English instruction frame to the first user message when the provider's baseUrl resolves to AgentRouter. The frame tells the model to respond in the appropriate language, so the original request and the output language are preserved while the boundary filter passes. The helper is idempotent (marker check) and handles string content, structured content, and content with no text part; applied at the single request-build site so raw requests and tool-call ids are untouched.
1 parent e97fb26 commit 8c1abc4

2 files changed

Lines changed: 109 additions & 0 deletions

File tree

‎src/adapters/anthropic.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,53 @@ function normalizeAnthropicInputSchema(schema: unknown): Record<string, unknown>
816816
return normalized;
817817
}
818818

819+
/**
820+
* AgentRouter's gateway applies a language filter to the FIRST user message
821+
* content and hard-fails non-English prompts with 400 content-blocked
822+
* (#2074). Prepending an explicit English instruction frame lets the filter
823+
* pass while the model still answers in the user's language — the frame says
824+
* "respond in the appropriate language", it does not force English output.
825+
*/
826+
export const AGR_PREAMBLE_MARKER =
827+
"[Instruction: Process the user request below and respond in the appropriate language.]";
828+
829+
export function isAgentRouterBaseUrl(baseUrl: string): boolean {
830+
try {
831+
return new URL(baseUrl).hostname.includes("agentrouter");
832+
} catch {
833+
return false;
834+
}
835+
}
836+
837+
/**
838+
* Prepend the AgentRouter language frame to the first user message, once.
839+
* Idempotent: a message already carrying the marker is left untouched (retries
840+
* and replays must not stack frames).
841+
*/
842+
export function applyAgrLanguagePreamble(messages: unknown[]): void {
843+
const firstUser = messages.find(
844+
m => typeof m === "object" && m !== null && (m as { role?: string }).role === "user",
845+
) as { content?: unknown } | undefined;
846+
if (!firstUser) return;
847+
848+
if (typeof firstUser.content === "string") {
849+
if (!firstUser.content.includes(AGR_PREAMBLE_MARKER)) {
850+
firstUser.content = `${AGR_PREAMBLE_MARKER}\n\n${firstUser.content}`;
851+
}
852+
} else if (Array.isArray(firstUser.content)) {
853+
const textPart = firstUser.content.find(
854+
p => typeof p === "object" && p !== null && (p as { type?: string }).type === "text",
855+
) as { text?: string } | undefined;
856+
if (textPart && typeof textPart.text === "string") {
857+
if (!textPart.text.includes(AGR_PREAMBLE_MARKER)) {
858+
textPart.text = `${AGR_PREAMBLE_MARKER}\n\n${textPart.text}`;
859+
}
860+
} else {
861+
firstUser.content.unshift({ type: "text", text: AGR_PREAMBLE_MARKER });
862+
}
863+
}
864+
}
865+
819866
export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetention?: "none" | "short" | "long"): ProviderAdapter {
820867
const isOAuth = provider.authMode === "oauth";
821868
const toolNames = buildToolNameTransforms(provider);
@@ -833,6 +880,9 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
833880
}
834881

835882
const { system, messages } = messagesToAnthropicFormat(parsed, toolNames);
883+
if (isAgentRouterBaseUrl(provider.baseUrl)) {
884+
applyAgrLanguagePreamble(messages);
885+
}
836886
// Primary image layer: resize/re-encode to fit Anthropic limits without dropping
837887
// (anthropic-image-normalize.ts); the guard below remains the deterministic backstop.
838888
// imageTierBias > 0 = upstream-413 tightened retry (030): start every image one tier lower.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { describe, expect, test } from "bun:test";
2+
import {
3+
AGR_PREAMBLE_MARKER,
4+
applyAgrLanguagePreamble,
5+
isAgentRouterBaseUrl,
6+
} from "../src/adapters/anthropic";
7+
8+
describe("AgentRouter language preamble (#2074)", () => {
9+
test("detects AgentRouter base URLs by hostname", () => {
10+
expect(isAgentRouterBaseUrl("https://agentrouter.org")).toBe(true);
11+
expect(isAgentRouterBaseUrl("https://agentrouter.org/v1")).toBe(true);
12+
expect(isAgentRouterBaseUrl("https://api.anthropic.com")).toBe(false);
13+
expect(isAgentRouterBaseUrl("not a url")).toBe(false);
14+
});
15+
16+
test("prepends the frame to a string first user message", () => {
17+
const messages = [
18+
{ role: "user", content: "responda apenas: OK" },
19+
{ role: "assistant", content: "OK" },
20+
];
21+
applyAgrLanguagePreamble(messages);
22+
const first = messages[0] as { content: string };
23+
expect(first.content.startsWith(AGR_PREAMBLE_MARKER)).toBe(true);
24+
expect(first.content).toContain("responda apenas: OK");
25+
});
26+
27+
test("prepends into the first text part of structured content", () => {
28+
const messages = [
29+
{ role: "user", content: [{ type: "image", source: {} }, { type: "text", text: "hola" }] },
30+
];
31+
applyAgrLanguagePreamble(messages);
32+
const parts = (messages[0] as { content: Array<{ type: string; text?: string }> }).content;
33+
const text = parts.find(p => p.type === "text");
34+
expect(text?.text?.startsWith(AGR_PREAMBLE_MARKER)).toBe(true);
35+
expect(text?.text).toContain("hola");
36+
});
37+
38+
test("inserts a text part when the first message has none", () => {
39+
const messages = [{ role: "user", content: [{ type: "image", source: {} }] }];
40+
applyAgrLanguagePreamble(messages);
41+
const parts = (messages[0] as { content: Array<{ type: string; text?: string }> }).content;
42+
expect(parts[0]?.type).toBe("text");
43+
expect(parts[0]?.text).toContain(AGR_PREAMBLE_MARKER);
44+
});
45+
46+
test("is idempotent — replays do not stack frames", () => {
47+
const messages = [{ role: "user", content: "bonjour" }];
48+
applyAgrLanguagePreamble(messages);
49+
applyAgrLanguagePreamble(messages);
50+
const content = (messages[0] as { content: string }).content;
51+
expect(content.split(AGR_PREAMBLE_MARKER).length - 1).toBe(1);
52+
});
53+
54+
test("no user message is a no-op", () => {
55+
const messages = [{ role: "assistant", content: "hi" }];
56+
applyAgrLanguagePreamble(messages);
57+
expect(messages).toEqual([{ role: "assistant", content: "hi" }]);
58+
});
59+
});

0 commit comments

Comments
 (0)