Skip to content

Commit 16b86c7

Browse files
committed
refactor: use assistant role for prunable-tools injection
Switch from user role to assistant role message for injecting prunable-tools context. This reduces the likelihood of the model directly addressing the injected content as if responding to user input, improving the conversational experience.
1 parent 936a1b5 commit 16b86c7

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

lib/messages/prune.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import type { Logger } from "../logger"
33
import type { PluginConfig } from "../config"
44
import { loadPrompt } from "../prompt"
55
import { extractParameterKey, buildToolIdList } from "./utils"
6-
import { getLastUserMessage, isMessageCompacted } from "../shared-utils"
7-
import { UserMessage } from "@opencode-ai/sdk"
6+
import { getLastAssistantMessage, isMessageCompacted } from "../shared-utils"
7+
import { AssistantMessage } from "@opencode-ai/sdk"
88

99
const PRUNED_TOOL_INPUT_REPLACEMENT = "[Input removed to save context]"
1010
const PRUNED_TOOL_OUTPUT_REPLACEMENT =
@@ -101,8 +101,8 @@ export const insertPruneToolContext = (
101101
return
102102
}
103103

104-
const lastUserMessage = getLastUserMessage(messages)
105-
if (!lastUserMessage) {
104+
const lastAssistantMessage = getLastAssistantMessage(messages)
105+
if (!lastAssistantMessage) {
106106
return
107107
}
108108

@@ -131,30 +131,33 @@ export const insertPruneToolContext = (
131131
prunableToolsContent = prunableToolsList + nudgeString
132132
}
133133

134-
const userMessage: WithParts = {
134+
const assistantInfo = lastAssistantMessage.info as AssistantMessage
135+
const assistantMessage: WithParts = {
135136
info: {
136137
id: SYNTHETIC_MESSAGE_ID,
137-
sessionID: lastUserMessage.info.sessionID,
138-
role: "user",
138+
sessionID: assistantInfo.sessionID,
139+
role: "assistant",
140+
parentID: assistantInfo.parentID,
141+
modelID: assistantInfo.modelID,
142+
providerID: assistantInfo.providerID,
139143
time: { created: Date.now() },
140-
agent: (lastUserMessage.info as UserMessage).agent || "build",
141-
model: {
142-
providerID: (lastUserMessage.info as UserMessage).model.providerID,
143-
modelID: (lastUserMessage.info as UserMessage).model.modelID,
144-
},
144+
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
145+
cost: 0,
146+
path: assistantInfo.path,
147+
mode: assistantInfo.mode,
145148
},
146149
parts: [
147150
{
148151
id: SYNTHETIC_PART_ID,
149-
sessionID: lastUserMessage.info.sessionID,
152+
sessionID: assistantInfo.sessionID,
150153
messageID: SYNTHETIC_MESSAGE_ID,
151154
type: "text",
152155
text: prunableToolsContent,
153156
},
154157
],
155158
}
156159

157-
messages.push(userMessage)
160+
messages.push(assistantMessage)
158161
}
159162

160163
export const prune = (

lib/shared-utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@ export const getLastUserMessage = (messages: WithParts[]): WithParts | null => {
1313
}
1414
return null
1515
}
16+
17+
export const getLastAssistantMessage = (messages: WithParts[]): WithParts | null => {
18+
for (let i = messages.length - 1; i >= 0; i--) {
19+
const msg = messages[i]
20+
if (msg.info.role === "assistant") {
21+
return msg
22+
}
23+
}
24+
return null
25+
}

0 commit comments

Comments
 (0)