diff --git a/packages/core/src/agent/tokens.test.ts b/packages/core/src/agent/tokens.test.ts new file mode 100644 index 00000000..d15f1d4f --- /dev/null +++ b/packages/core/src/agent/tokens.test.ts @@ -0,0 +1,39 @@ +import { describe, it, expect } from "vitest"; +import { isSilentReplyText, SILENT_REPLY_TOKEN } from "./tokens.js"; + +describe("isSilentReplyText", () => { + it("detects exact NO_REPLY", () => { + expect(isSilentReplyText("NO_REPLY")).toBe(true); + }); + + it("detects NO_REPLY with surrounding whitespace", () => { + expect(isSilentReplyText(" NO_REPLY ")).toBe(true); + expect(isSilentReplyText("\nNO_REPLY\n")).toBe(true); + }); + + it("detects NO_REPLY with trailing punctuation", () => { + expect(isSilentReplyText("NO_REPLY.")).toBe(true); + expect(isSilentReplyText("NO_REPLY.\n")).toBe(true); + }); + + it("detects NO_REPLY at end of text", () => { + expect(isSilentReplyText("I have nothing to report. NO_REPLY")).toBe(true); + }); + + it("returns false for undefined/empty", () => { + expect(isSilentReplyText(undefined)).toBe(false); + expect(isSilentReplyText("")).toBe(false); + }); + + it("returns false for normal text", () => { + expect(isSilentReplyText("Here are the findings")).toBe(false); + }); + + it("returns false for NO_REPLY embedded in a word", () => { + expect(isSilentReplyText("DONO_REPLYX")).toBe(false); + }); + + it("exports SILENT_REPLY_TOKEN as NO_REPLY", () => { + expect(SILENT_REPLY_TOKEN).toBe("NO_REPLY"); + }); +}); diff --git a/packages/core/src/agent/tokens.ts b/packages/core/src/agent/tokens.ts new file mode 100644 index 00000000..2ec4136f --- /dev/null +++ b/packages/core/src/agent/tokens.ts @@ -0,0 +1,17 @@ +export const SILENT_REPLY_TOKEN = "NO_REPLY"; + +function escapeRegExp(value: string): string { + return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); +} + +export function isSilentReplyText( + text: string | undefined, + token: string = SILENT_REPLY_TOKEN, +): boolean { + if (!text) return false; + const escaped = escapeRegExp(token); + const prefix = new RegExp(`^\\s*${escaped}(?=$|\\W)`); + if (prefix.test(text)) return true; + const suffix = new RegExp(`\\b${escaped}\\b\\W*$`); + return suffix.test(text); +}