From 5b29ed23500d6de18a5d55b224154695c339657f Mon Sep 17 00:00:00 2001 From: shrinivas-sn Date: Mon, 12 Oct 2026 15:37:39 +0530 Subject: [PATCH 1/2] fix(ai): classify replies from latest assistant turn --- .../domainClassifier.contextual.unit.test.js | 30 +++++++++++++++++++ backend/utils/domainClassifier.js | 4 +-- 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 backend/tests/domainClassifier.contextual.unit.test.js diff --git a/backend/tests/domainClassifier.contextual.unit.test.js b/backend/tests/domainClassifier.contextual.unit.test.js new file mode 100644 index 00000000..e46afa0f --- /dev/null +++ b/backend/tests/domainClassifier.contextual.unit.test.js @@ -0,0 +1,30 @@ +import { describe, it, expect } from "vitest"; +import { isContextualResponse } from "../utils/domainClassifier.js"; + +describe("isContextualResponse", () => { + const firstExchange = [ + { role: "user", text: "Help me prepare for an interview" }, + { role: "model", text: "Would you like to practice a question?" }, + ]; + + it("accepts a short reply when the previous assistant message ends the history", () => { + expect(isContextualResponse("yes", firstExchange)).toBe(true); + expect(isContextualResponse("continue", [ + ...firstExchange, + { role: "user", text: "Explain the answer" }, + { role: "model", text: "Should we try another?" }, + ])).toBe(true); + }); + + it("does not use an older assistant reply when history ends with a user message", () => { + expect(isContextualResponse("yes", [ + ...firstExchange, + { role: "user", text: "What is the weather?" }, + ])).toBe(false); + }); + + it("does not treat a reply as contextual without a completed exchange", () => { + expect(isContextualResponse("yes", [])).toBe(false); + expect(isContextualResponse("yes", [{ role: "model", text: "Ready?" }])).toBe(false); + }); +}); diff --git a/backend/utils/domainClassifier.js b/backend/utils/domainClassifier.js index 976ed79e..46169e2a 100644 --- a/backend/utils/domainClassifier.js +++ b/backend/utils/domainClassifier.js @@ -62,9 +62,9 @@ const isContextualResponse = (prompt, history) => { const trimmed = prompt.trim(); if (contextualRegex.test(trimmed)) { - // Only contextual if there's a previous assistant message + // The current prompt is separate from history; its last entry must be the assistant. if (history && history.length > 1) { - const lastMessage = history[history.length - 2]; + const lastMessage = history[history.length - 1]; if (lastMessage && lastMessage.role === "model") { return true; } From 75fd7927d975bf77cf14ed4f7780b16fe8635321 Mon Sep 17 00:00:00 2001 From: shrinivas-sn Date: Wed, 11 Nov 2026 14:07:22 +0530 Subject: [PATCH 2/2] fix(ai): allow contextual reply when history has single assistant message (#2365) --- backend/tests/domainClassifier.contextual.unit.test.js | 4 ++-- backend/utils/domainClassifier.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/tests/domainClassifier.contextual.unit.test.js b/backend/tests/domainClassifier.contextual.unit.test.js index e46afa0f..3e5ec24e 100644 --- a/backend/tests/domainClassifier.contextual.unit.test.js +++ b/backend/tests/domainClassifier.contextual.unit.test.js @@ -9,6 +9,7 @@ describe("isContextualResponse", () => { it("accepts a short reply when the previous assistant message ends the history", () => { expect(isContextualResponse("yes", firstExchange)).toBe(true); + expect(isContextualResponse("yes", [{ role: "model", text: "Ready?" }])).toBe(true); expect(isContextualResponse("continue", [ ...firstExchange, { role: "user", text: "Explain the answer" }, @@ -23,8 +24,7 @@ describe("isContextualResponse", () => { ])).toBe(false); }); - it("does not treat a reply as contextual without a completed exchange", () => { + it("does not treat a reply as contextual with empty history", () => { expect(isContextualResponse("yes", [])).toBe(false); - expect(isContextualResponse("yes", [{ role: "model", text: "Ready?" }])).toBe(false); }); }); diff --git a/backend/utils/domainClassifier.js b/backend/utils/domainClassifier.js index 46169e2a..510bdc1b 100644 --- a/backend/utils/domainClassifier.js +++ b/backend/utils/domainClassifier.js @@ -63,7 +63,7 @@ const isContextualResponse = (prompt, history) => { if (contextualRegex.test(trimmed)) { // The current prompt is separate from history; its last entry must be the assistant. - if (history && history.length > 1) { + if (history && history.length > 0) { const lastMessage = history[history.length - 1]; if (lastMessage && lastMessage.role === "model") { return true;