Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
46 changes: 37 additions & 9 deletions open-sse/config/forwardingKeywordRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,34 @@ function normalizeTagRule(rule: unknown): ForwardingTagRule | null {
};
}

function mergeKeywordRules(
defaults: ForwardingKeywordRule[],
overrides: ForwardingKeywordRule[]
): ForwardingKeywordRule[] {
if (overrides.length === 0) return defaults;

const merged = new Map(defaults.map((rule) => [rule.match, rule]));
for (const override of overrides) {
merged.set(override.match, override);
}

return Array.from(merged.values());
}

function mergeTagRules(
defaults: ForwardingTagRule[],
overrides: ForwardingTagRule[]
): ForwardingTagRule[] {
if (overrides.length === 0) return defaults;

const merged = new Map(defaults.map((rule) => [`${rule.open}::${rule.close}`, rule]));
for (const override of overrides) {
merged.set(`${override.open}::${override.close}`, override);
}

return Array.from(merged.values());
}

export function normalizeForwardingKeywordConfig(value: unknown): ForwardingKeywordConfig {
const normalized = cloneForwardingKeywordConfig(DEFAULT_FORWARDING_KEYWORD_CONFIG);
if (!value || typeof value !== "object") return normalized;
Expand All @@ -95,20 +123,20 @@ export function normalizeForwardingKeywordConfig(value: unknown): ForwardingKeyw
if (!rawLane || typeof rawLane !== "object") continue;

const rawLaneConfig = rawLane as Record<string, unknown>;
const toolNames = Array.isArray(rawLaneConfig.toolNames)
const toolNameOverrides = Array.isArray(rawLaneConfig.toolNames)
? rawLaneConfig.toolNames.map(normalizeKeywordRule).filter(Boolean)
: normalized[lane].toolNames;
const text = Array.isArray(rawLaneConfig.text)
: [];
const textOverrides = Array.isArray(rawLaneConfig.text)
? rawLaneConfig.text.map(normalizeKeywordRule).filter(Boolean)
: normalized[lane].text;
const tags = Array.isArray(rawLaneConfig.tags)
: [];
const tagOverrides = Array.isArray(rawLaneConfig.tags)
? rawLaneConfig.tags.map(normalizeTagRule).filter(Boolean)
: normalized[lane].tags;
: [];

normalized[lane] = {
toolNames,
text,
tags,
toolNames: mergeKeywordRules(normalized[lane].toolNames, toolNameOverrides),
text: mergeKeywordRules(normalized[lane].text, textOverrides),
tags: mergeTagRules(normalized[lane].tags, tagOverrides),
};
}

Expand Down
27 changes: 25 additions & 2 deletions src/app/api/settings/__tests__/forwarding-keywords.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe("/api/settings/forwarding-keywords", () => {
expect(json.config["claude-oauth-prefixed"].toolNames[0].replace).toBe("bg_out");
});

it("preserves intentionally empty rule arrays", async () => {
it("keeps default rewrite rules when empty arrays are saved", async () => {
const body = {
"claude-oauth-prefixed": {
toolNames: [],
Expand All @@ -84,7 +84,30 @@ describe("/api/settings/forwarding-keywords", () => {
const res = await PUT(createPutRequest(body));
expect(res.status).toBe(200);
const json = await res.json();
expect(json.config["claude-oauth-prefixed"]).toEqual(body["claude-oauth-prefixed"]);
expect(json.config["claude-oauth-prefixed"]).toEqual(
getDefaultForwardingKeywordConfig()["claude-oauth-prefixed"]
);
});

it("overrides matching defaults without disabling the rest", async () => {
const body = {
"claude-oauth-prefixed": {
toolNames: [{ match: "background_output", replace: "bg_out" }],
text: [],
tags: [],
},
};

const res = await PUT(createPutRequest(body));
expect(res.status).toBe(200);
const json = await res.json();
expect(json.config["claude-oauth-prefixed"].toolNames).toEqual([
{ match: "background_output", replace: "bg_out" },
{ match: "background_cancel", replace: "background_stop" },
]);
expect(json.config["claude-oauth-prefixed"].text).toEqual(
getDefaultForwardingKeywordConfig()["claude-oauth-prefixed"].text
);
});

it("rejects whitespace-only keyword matches", async () => {
Expand Down
72 changes: 70 additions & 2 deletions tests/unit/translator-openai-to-claude.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,54 @@ test("Forwarding keyword normalization rejects blank match and tag boundaries",

assert.deepEqual(normalized["claude-oauth-prefixed"].toolNames, [
{ match: "background_output", replace: "background_result" },
{ match: "background_cancel", replace: "background_stop" },
]);
assert.deepEqual(normalized["claude-oauth-prefixed"].text, [
{ match: "background_output", replace: "background_result" },
{ match: "background_cancel", replace: "background_stop" },
]);
assert.deepEqual(normalized["claude-oauth-prefixed"].tags, [
{
open: "<directories>",
openReplacement: "directories:\n",
close: "</directories>",
closeReplacement: "",
},
]);
});

test("Forwarding keyword normalization overlays saved rules onto defaults", () => {
const normalized = normalizeForwardingKeywordConfig({
"claude-oauth-prefixed": {
toolNames: [{ match: "background_output", replace: "bg_out" }],
text: [],
tags: [
{
open: "<directories>",
openReplacement: "dirs:\n",
close: "</directories>",
closeReplacement: "",
},
],
},
});

assert.deepEqual(normalized["claude-oauth-prefixed"].toolNames, [
{ match: "background_output", replace: "bg_out" },
{ match: "background_cancel", replace: "background_stop" },
]);
assert.deepEqual(normalized["claude-oauth-prefixed"].text, [
{ match: "background_output", replace: "background_result" },
{ match: "background_cancel", replace: "background_stop" },
]);
assert.deepEqual(normalized["claude-oauth-prefixed"].tags, [
{
open: "<directories>",
openReplacement: "dirs:\n",
close: "</directories>",
closeReplacement: "",
},
]);
assert.deepEqual(normalized["claude-oauth-prefixed"].text, []);
assert.deepEqual(normalized["claude-oauth-prefixed"].tags, []);
});

test("Forwarding keyword settings reset to defaults only after successful settings load", () => {
Expand All @@ -507,6 +552,29 @@ test("Forwarding keyword settings reset to defaults only after successful settin
);
});

test("Forwarding keyword settings keep defaults active when saved config has empty arrays", () => {
applyForwardingKeywordSettings({
forwardingKeywordRules: {
"claude-oauth-prefixed": {
toolNames: [],
text: [],
tags: [],
},
},
});

const rewrittenText = rewriteForwardedTextForLane(
"claude-oauth-prefixed",
"background_output <directories>src/</directories>"
);

assert.equal(
rewriteForwardedToolNameForLane("claude-oauth-prefixed", "background_cancel"),
"background_stop"
);
assert.equal(rewrittenText, "background_result directories:\nsrc/");
});

test("OpenAI-compatible -> Claude -> OpenAI-compatible preserves original tool names", () => {
const request = openaiToClaudeRequest(
"claude-4-sonnet",
Expand Down
Loading