diff --git a/open-sse/executors/perplexity-web/protocol.ts b/open-sse/executors/perplexity-web/protocol.ts index 197ee7be79..9c8de0f8c0 100644 --- a/open-sse/executors/perplexity-web/protocol.ts +++ b/open-sse/executors/perplexity-web/protocol.ts @@ -408,7 +408,12 @@ function searchHintEnabled(): boolean { } export function buildQuery(parsed: ParsedMessages, followUpUuid: string | null): string { - if (followUpUuid) return parsed.currentMsg; + if (followUpUuid) { + const sys = parsed.systemMsg.trim(); + const hint = searchHintEnabled() ? `\n\n${SEARCH_HINT}` : ""; + const contract = sys ? `${sys}${hint}` : ""; + return contract ? `${contract}\n\n${parsed.currentMsg}` : parsed.currentMsg; + } const obj: Record = {}; if (parsed.systemMsg.trim()) { diff --git a/tests/unit/12104-perplexity-web-followup-contract.test.ts b/tests/unit/12104-perplexity-web-followup-contract.test.ts new file mode 100644 index 0000000000..a1ca0c87f9 --- /dev/null +++ b/tests/unit/12104-perplexity-web-followup-contract.test.ts @@ -0,0 +1,29 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { buildQuery } from "../../open-sse/executors/perplexity-web/protocol.ts"; + +test("PR #3: buildQuery preserves system contract on follow-up requests", () => { + const parsed = { + systemMsg: "You are an expert coder. contract", + history: [], + currentMsg: "How do I reverse a string in JS?" + }; + + // Turn 1: followUpUuid is null + const turn1Raw = buildQuery(parsed, null); + const turn1Obj = JSON.parse(turn1Raw); + assert.ok(turn1Obj.instructions !== undefined, "Turn 1 JSON should contain instructions"); + assert.equal(turn1Obj.instructions[0], "You are an expert coder. contract"); + + // Turn 2: follow-up request + const parsedTurn2 = { + systemMsg: "You are an expert coder. contract", + history: [{ role: "user", content: "How do I reverse a string in JS?" }, { role: "assistant", content: "Use .reverse()" }], + currentMsg: "What about Python?" + }; + const turn2Output = buildQuery(parsedTurn2, "abc-123"); + + // Since we append systemMsg to currentMsg for follow-ups (Approach B) + assert.ok(turn2Output.includes("contract"), "Follow-up query should preserve the tool contract"); + assert.ok(turn2Output.includes("What about Python?"), "Follow-up query should include the current message"); +});