Skip to content

Commit dc16a0d

Browse files
committed
refactor: simplify hallucination stripping
1 parent 5767be1 commit dc16a0d

5 files changed

Lines changed: 10 additions & 23 deletions

File tree

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const plugin: Plugin = (async (ctx) => {
6767
hostPermissions,
6868
) as any,
6969
"chat.message": createChatMessageHandler(state, logger, config, hostPermissions),
70-
"experimental.text.complete": createTextCompleteHandler(state, config),
70+
"experimental.text.complete": createTextCompleteHandler(),
7171
"command.execute.before": createCommandExecuteHandler(
7272
ctx.client,
7373
state,

lib/hooks.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export function createChatMessageTransformHandler(
103103
return
104104
}
105105

106-
stripHallucinations(output.messages, state, config)
106+
stripHallucinations(output.messages)
107107
cacheSystemPromptTokens(state, output.messages)
108108
assignMessageRefs(state, output.messages)
109109
syncCompressionBlocks(state, logger, output.messages)
@@ -255,15 +255,11 @@ export function createCommandExecuteHandler(
255255
}
256256
}
257257

258-
export function createTextCompleteHandler(state: SessionState, config: PluginConfig) {
258+
export function createTextCompleteHandler() {
259259
return async (
260260
_input: { sessionID: string; messageID: string; partID: string },
261261
output: { text: string },
262262
) => {
263-
if (compressPermission(state, config) === "deny") {
264-
return
265-
}
266-
267263
output.text = stripHallucinationsFromString(output.text)
268264
}
269265
}

lib/messages/utils.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { createHash } from "node:crypto"
22
import type { PluginConfig } from "../config"
3-
import { compressPermission } from "../shared-utils"
43
import { isMessageCompacted } from "../shared-utils"
54
import type { SessionState, WithParts } from "../state"
65
import type { UserMessage } from "@opencode-ai/sdk/v2"
@@ -188,15 +187,7 @@ export const stripHallucinationsFromString = (text: string): string => {
188187
return text.replace(DCP_PAIRED_TAG_REGEX, "").replace(DCP_UNPAIRED_TAG_REGEX, "")
189188
}
190189

191-
export const stripHallucinations = (
192-
messages: WithParts[],
193-
state: SessionState,
194-
config: PluginConfig,
195-
): void => {
196-
if (compressPermission(state, config) === "deny") {
197-
return
198-
}
199-
190+
export const stripHallucinations = (messages: WithParts[]): void => {
200191
for (const message of messages) {
201192
for (const part of message.parts) {
202193
if (part.type === "text" && typeof part.text === "string") {

tests/hooks-permission.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function buildMessage(id: string, role: "user" | "assistant", text: string): Wit
8080
}
8181
}
8282

83-
test("chat message transform leaves messages untouched when compress is denied", async () => {
83+
test("chat message transform strips hallucinated tags even when compress is denied", async () => {
8484
const state = createSessionState()
8585
const logger = new Logger(false)
8686
const config = buildConfig("deny")
@@ -104,7 +104,7 @@ test("chat message transform leaves messages untouched when compress is denied",
104104
await handler({}, output)
105105

106106
assert.equal(output.messages[0]?.parts[0]?.type, "text")
107-
assert.equal((output.messages[0]?.parts[0] as any).text, "alpha <dcp>beta</dcp> omega")
107+
assert.equal((output.messages[0]?.parts[0] as any).text, "alpha omega")
108108
})
109109

110110
test("command execute exits after effective permission resolves to deny", async () => {
@@ -144,11 +144,11 @@ test("chat message hook caches variant even when effective permission is denied"
144144
assert.equal(state.variant, "danger")
145145
})
146146

147-
test("text complete leaves output untouched when compress is denied", async () => {
147+
test("text complete strips hallucinated metadata tags", async () => {
148148
const output = { text: "alpha <dcp>beta</dcp> omega" }
149-
const handler = createTextCompleteHandler(createSessionState(), buildConfig("deny"))
149+
const handler = createTextCompleteHandler()
150150

151151
await handler({ sessionID: "session-1", messageID: "message-1", partID: "part-1" }, output)
152152

153-
assert.equal(output.text, "alpha <dcp>beta</dcp> omega")
153+
assert.equal(output.text, "alpha omega")
154154
})

tests/message-priority.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ test("hallucination stripping removes all dcp-prefixed XML tags including varian
776776

777777
assert.equal(stripHallucinationsFromString(text), "alphaomega")
778778

779-
const handler = createTextCompleteHandler(createSessionState(), buildConfig())
779+
const handler = createTextCompleteHandler()
780780
const output = { text }
781781
await handler({ sessionID: "session", messageID: "message", partID: "part" }, output)
782782
assert.equal(output.text, "alphaomega")

0 commit comments

Comments
 (0)