Skip to content

Commit 018a0d6

Browse files
authored
Merge pull request #263 from Opencode-DCP/fix/simplify-injection-timing
fix: simplify injection timing to wait for assistant turn
2 parents d2b9a6d + e3d74f3 commit 018a0d6

File tree

2 files changed

+8
-44
lines changed

2 files changed

+8
-44
lines changed

lib/messages/inject.ts

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
buildToolIdList,
99
createSyntheticAssistantMessageWithToolPart,
1010
isIgnoredUserMessage,
11-
hasReasoningInCurrentAssistantTurn,
1211
} from "./utils"
1312
import { getFilePathFromParameters, isProtectedFilePath } from "../protected-file-patterns"
1413
import { getLastUserMessage } from "../shared-utils"
@@ -139,31 +138,16 @@ export const insertPruneToolContext = (
139138
return
140139
}
141140

142-
const userInfo = lastUserMessage.info as UserMessage
143-
const providerID = userInfo.model.providerID
144-
const modelID = userInfo.model.modelID
145-
const isGitHubCopilot =
146-
providerID === "github-copilot" || providerID === "github-copilot-enterprise"
147-
148-
// TODO: This can probably be improved further to only trigger for the appropriate thinking settings
149-
// This setting is also potentially only necessary for claude subscription, API seems to not need this
150-
// validation. See more here: https://platform.claude.com/docs/en/build-with-claude/extended-thinking
151-
const isAnthropic = modelID.includes("claude")
152-
153-
if (isGitHubCopilot) {
154-
const lastMessage = messages[messages.length - 1]
155-
if (lastMessage?.info?.role === "user" && !isIgnoredUserMessage(lastMessage)) {
156-
return
157-
}
158-
}
159-
160-
// Anthropic extended thinking models require a thinking block at the start of its turn
161-
if (isAnthropic) {
162-
if (!hasReasoningInCurrentAssistantTurn(messages)) {
163-
return
164-
}
141+
// Never inject immediately following a user message - wait until assistant has started its turn
142+
// This avoids interfering with model reasoning/thinking phases
143+
// TODO: This can be skipped if there is a good way to check if the model has reasoning,
144+
// can't find a good way to do this yet
145+
const lastMessage = messages[messages.length - 1]
146+
if (lastMessage?.info?.role === "user" && !isIgnoredUserMessage(lastMessage)) {
147+
return
165148
}
166149

150+
const userInfo = lastUserMessage.info as UserMessage
167151
const variant = state.variant ?? userInfo.variant
168152
messages.push(
169153
createSyntheticAssistantMessageWithToolPart(lastUserMessage, prunableToolsContent, variant),

lib/messages/utils.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -207,23 +207,3 @@ export const isIgnoredUserMessage = (message: WithParts): boolean => {
207207

208208
return true
209209
}
210-
211-
export const hasReasoningInCurrentAssistantTurn = (messages: WithParts[]): boolean => {
212-
for (let i = messages.length - 1; i >= 0; i--) {
213-
const message = messages[i]
214-
if (message.info?.role === "user") {
215-
if (isIgnoredUserMessage(message)) {
216-
continue
217-
}
218-
return false
219-
}
220-
if (message.info?.role === "assistant" && message.parts) {
221-
for (const part of message.parts) {
222-
if (part.type === "reasoning") {
223-
return true
224-
}
225-
}
226-
}
227-
}
228-
return false
229-
}

0 commit comments

Comments
 (0)