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
4 changes: 3 additions & 1 deletion lib/messages/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const SUMMARY_ID_HASH_LENGTH = 16
const DCP_BLOCK_ID_TAG_REGEX = /(<dcp-message-id(?=[\s>])[^>]*>)b\d+(<\/dcp-message-id>)/g
const DCP_PAIRED_TAG_REGEX = /<dcp[^>]*>[\s\S]*?<\/dcp[^>]*>/gi
const DCP_UNPAIRED_TAG_REGEX = /<\/?dcp[^>]*>/gi
const HALLUCINATED_PARAMETER_SUFFIX_REGEX = /\nm\d+<\/parameter>\s*$/

const generateStableId = (prefix: string, seed: string): string => {
const hash = createHash("sha256").update(seed).digest("hex").slice(0, SUMMARY_ID_HASH_LENGTH)
Expand Down Expand Up @@ -163,7 +164,8 @@ export const replaceBlockIdsWithBlocked = (text: string): string => {
}

export const stripHallucinationsFromString = (text: string): string => {
return text.replace(DCP_PAIRED_TAG_REGEX, "").replace(DCP_UNPAIRED_TAG_REGEX, "")
const withoutHallucinatedParameter = text.replace(HALLUCINATED_PARAMETER_SUFFIX_REGEX, "")
return withoutHallucinatedParameter.replace(DCP_PAIRED_TAG_REGEX, "").replace(DCP_UNPAIRED_TAG_REGEX, "")
}

export const stripHallucinations = (messages: WithParts[]): void => {
Expand Down
7 changes: 7 additions & 0 deletions tests/message-priority.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,13 @@ test("hallucination stripping does not affect non-dcp tags", async () => {
)
})

test("hallucination stripping removes trailing mXXXX</parameter> artifact (issue #555)", () => {
assert.equal(
stripHallucinationsFromString("Total: maybe 20 lines changed.\n\nm0340</parameter>\n\n"),
"Total: maybe 20 lines changed.\n\n",
)
})

test("injectMessageIds skips empty assistant messages to avoid prefill (issue #463)", () => {
const sessionID = "ses_empty_assistant"
const messages: WithParts[] = [
Expand Down
Loading