|
| 1 | +import assert from "node:assert/strict" |
| 2 | +import test from "node:test" |
| 3 | +import type { PluginConfig } from "../lib/config" |
| 4 | +import { createTextCompleteHandler, createToolExecuteAfterHandler } from "../lib/hooks" |
| 5 | +import { injectMessageIds } from "../lib/messages/inject/inject" |
| 6 | +import { sanitizeVisibleOutput } from "../lib/messages/utils" |
| 7 | +import { createSessionState, type WithParts } from "../lib/state" |
| 8 | + |
| 9 | +function buildConfig(): PluginConfig { |
| 10 | + return { |
| 11 | + enabled: true, |
| 12 | + debug: false, |
| 13 | + pruneNotification: "off", |
| 14 | + pruneNotificationType: "chat", |
| 15 | + commands: { |
| 16 | + enabled: true, |
| 17 | + protectedTools: [], |
| 18 | + }, |
| 19 | + manualMode: { |
| 20 | + enabled: false, |
| 21 | + automaticStrategies: true, |
| 22 | + }, |
| 23 | + turnProtection: { |
| 24 | + enabled: false, |
| 25 | + turns: 4, |
| 26 | + }, |
| 27 | + experimental: { |
| 28 | + allowSubAgents: true, |
| 29 | + customPrompts: false, |
| 30 | + }, |
| 31 | + protectedFilePatterns: [], |
| 32 | + compress: { |
| 33 | + permission: "allow", |
| 34 | + showCompression: false, |
| 35 | + maxContextLimit: 150000, |
| 36 | + minContextLimit: 50000, |
| 37 | + nudgeFrequency: 5, |
| 38 | + iterationNudgeThreshold: 15, |
| 39 | + nudgeForce: "soft", |
| 40 | + flatSchema: false, |
| 41 | + protectedTools: [], |
| 42 | + protectUserMessages: false, |
| 43 | + }, |
| 44 | + strategies: { |
| 45 | + deduplication: { |
| 46 | + enabled: true, |
| 47 | + protectedTools: [], |
| 48 | + }, |
| 49 | + supersedeWrites: { |
| 50 | + enabled: true, |
| 51 | + }, |
| 52 | + purgeErrors: { |
| 53 | + enabled: true, |
| 54 | + turns: 4, |
| 55 | + protectedTools: [], |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +test("sanitizeVisibleOutput strips DCP metadata and trailing blank lines", () => { |
| 62 | + const result = sanitizeVisibleOutput(`bun install |
| 63 | +<dcp-message-id>m0045</dcp-message-id> |
| 64 | +
|
| 65 | +
|
| 66 | +<dcp-system-reminder>hidden</dcp-system-reminder> |
| 67 | +`) |
| 68 | + |
| 69 | + assert.equal(result, "bun install") |
| 70 | +}) |
| 71 | + |
| 72 | +test("tool.execute.after strips DCP metadata from visible tool output", async () => { |
| 73 | + const handler = createToolExecuteAfterHandler() |
| 74 | + const output = { |
| 75 | + title: `bash |
| 76 | +<dcp-message-id>m0045</dcp-message-id>`, |
| 77 | + output: `bun install v1.3.10 |
| 78 | +<dcp-message-id>m0045</dcp-message-id>`, |
| 79 | + metadata: {}, |
| 80 | + } |
| 81 | + |
| 82 | + await handler({ tool: "bash", sessionID: "ses_1", callID: "call_1" }, output) |
| 83 | + |
| 84 | + assert.equal(output.title, "bash") |
| 85 | + assert.equal(output.output, "bun install v1.3.10") |
| 86 | +}) |
| 87 | + |
| 88 | +test("experimental.text.complete strips DCP metadata from visible assistant text", async () => { |
| 89 | + const handler = createTextCompleteHandler() |
| 90 | + const output = { |
| 91 | + text: `done |
| 92 | +<dcp-message-id>m0045</dcp-message-id> |
| 93 | +<dcp-system-reminder>hidden</dcp-system-reminder>`, |
| 94 | + } |
| 95 | + |
| 96 | + await handler({ sessionID: "ses_1", messageID: "msg_1", partID: "part_1" }, output) |
| 97 | + |
| 98 | + assert.equal(output.text, "done") |
| 99 | +}) |
| 100 | + |
| 101 | +test("injectMessageIds keeps assistant tool output clean and inserts a synthetic text part", () => { |
| 102 | + const state = createSessionState() |
| 103 | + state.messageIds.byRawId.set("assistant-1", "m0045") |
| 104 | + |
| 105 | + const messages: WithParts[] = [ |
| 106 | + { |
| 107 | + info: { |
| 108 | + id: "assistant-1", |
| 109 | + role: "assistant", |
| 110 | + sessionID: "ses_1", |
| 111 | + agent: "assistant", |
| 112 | + time: { created: 1 }, |
| 113 | + } as WithParts["info"], |
| 114 | + parts: [ |
| 115 | + { |
| 116 | + id: "tool-part-1", |
| 117 | + sessionID: "ses_1", |
| 118 | + messageID: "assistant-1", |
| 119 | + type: "tool", |
| 120 | + callID: "call_1", |
| 121 | + tool: "bash", |
| 122 | + state: { |
| 123 | + status: "completed", |
| 124 | + input: {}, |
| 125 | + title: "bash", |
| 126 | + output: "bun install v1.3.10", |
| 127 | + metadata: {}, |
| 128 | + time: { start: 1, end: 2 }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + ], |
| 132 | + }, |
| 133 | + ] |
| 134 | + |
| 135 | + injectMessageIds(state, buildConfig(), messages) |
| 136 | + |
| 137 | + assert.equal(messages[0].parts[0].type, "text") |
| 138 | + assert.equal(messages[0].parts[0].synthetic, true) |
| 139 | + assert.match(messages[0].parts[0].text, /<dcp-message-id>m0045<\/dcp-message-id>/) |
| 140 | + assert.equal(messages[0].parts[1].type, "tool") |
| 141 | + assert.equal(messages[0].parts[1].state.status, "completed") |
| 142 | + assert.equal(messages[0].parts[1].state.output, "bun install v1.3.10") |
| 143 | +}) |
0 commit comments