From 2d9f5f696abf0aad60e97403f100e619114e5239 Mon Sep 17 00:00:00 2001 From: i1hwan Date: Sat, 11 Apr 2026 20:40:23 +0900 Subject: [PATCH] fix(settings): preserve default forwarding keyword rewrites Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- open-sse/config/forwardingKeywordRules.ts | 46 +++++++++--- .../__tests__/forwarding-keywords.test.ts | 27 ++++++- .../unit/translator-openai-to-claude.test.mjs | 72 ++++++++++++++++++- 3 files changed, 132 insertions(+), 13 deletions(-) diff --git a/open-sse/config/forwardingKeywordRules.ts b/open-sse/config/forwardingKeywordRules.ts index f1e593017271..96199577591c 100644 --- a/open-sse/config/forwardingKeywordRules.ts +++ b/open-sse/config/forwardingKeywordRules.ts @@ -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; @@ -95,20 +123,20 @@ export function normalizeForwardingKeywordConfig(value: unknown): ForwardingKeyw if (!rawLane || typeof rawLane !== "object") continue; const rawLaneConfig = rawLane as Record; - 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), }; } diff --git a/src/app/api/settings/__tests__/forwarding-keywords.test.ts b/src/app/api/settings/__tests__/forwarding-keywords.test.ts index 8e95ec2a2e52..5e1686db7379 100644 --- a/src/app/api/settings/__tests__/forwarding-keywords.test.ts +++ b/src/app/api/settings/__tests__/forwarding-keywords.test.ts @@ -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: [], @@ -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 () => { diff --git a/tests/unit/translator-openai-to-claude.test.mjs b/tests/unit/translator-openai-to-claude.test.mjs index b867eb1c4c0f..d04bc78612ad 100644 --- a/tests/unit/translator-openai-to-claude.test.mjs +++ b/tests/unit/translator-openai-to-claude.test.mjs @@ -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: "", + openReplacement: "directories:\n", + close: "", + 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: "", + openReplacement: "dirs:\n", + close: "", + 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: "", + openReplacement: "dirs:\n", + close: "", + 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", () => { @@ -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 src/" + ); + + 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",