Skip to content
Draft
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
26 changes: 17 additions & 9 deletions packages/opencode/src/provider/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,11 @@ function normalizeMessages(
}
if (!Array.isArray(msg.content)) return msg
const filtered = msg.content.filter((part) => {
if (part.type === "text" || part.type === "reasoning") {
return part.text !== ""
if (part.type === "text") return part.text !== ""
if (part.type === "reasoning") {
// Keep reasoning parts that carry providerOptions (e.g. redacted_thinking
// blocks which have empty text but contain redactedData that must be preserved)
return part.text !== "" || part.providerOptions != null
}
return true
})
Expand Down Expand Up @@ -213,6 +216,7 @@ function normalizeMessages(
const parts = msg.content
const first = parts.findIndex((part) => part.type === "tool-call")
if (first === -1) return [msg]
if (parts.some((part) => part.type === "reasoning")) return [msg]
if (!parts.slice(first).some((part) => part.type !== "tool-call")) return [msg]
return [
{ ...msg, content: parts.filter((part) => part.type !== "tool-call") },
Expand Down Expand Up @@ -358,13 +362,17 @@ function applyCaching(msgs: ModelMessage[], model: Provider.Model): ModelMessage
const shouldUseContentOptions = !useMessageLevelOptions && Array.isArray(msg.content) && msg.content.length > 0

if (shouldUseContentOptions) {
const lastContent = msg.content[msg.content.length - 1]
if (
lastContent &&
typeof lastContent === "object" &&
lastContent.type !== "tool-approval-request" &&
lastContent.type !== "tool-approval-response"
) {
const lastContent = [...msg.content]
.reverse()
.find(
(part) =>
part &&
typeof part === "object" &&
part.type !== "tool-approval-request" &&
part.type !== "tool-approval-response" &&
part.type !== "reasoning",
) as any
if (lastContent) {
lastContent.providerOptions = mergeDeep(lastContent.providerOptions ?? {}, providerOptions)
continue
}
Expand Down
3 changes: 2 additions & 1 deletion packages/opencode/src/session/compaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ export const layer: Layer.Layer<
yield* plugin.trigger("experimental.chat.messages.transform", {}, { messages: msgs })
const modelMessages = yield* MessageV2.toModelMessagesEffect(msgs, model, {
stripMedia: true,
stripReasoning: true,
toolOutputMaxChars: TOOL_OUTPUT_MAX_CHARS,
})
const ctx = yield* InstanceState.context
Expand Down Expand Up @@ -449,7 +450,7 @@ export const layer: Layer.Layer<
tools: {},
system: [],
messages: [
...modelMessages,
...(modelMessages ?? []),
{
role: "user",
content: [{ type: "text", text: nextPrompt }],
Expand Down
7 changes: 4 additions & 3 deletions packages/opencode/src/session/message-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ function providerMeta(metadata: Record<string, any> | undefined) {
export const toModelMessagesEffect = Effect.fnUntraced(function* (
input: WithParts[],
model: Provider.Model,
options?: { stripMedia?: boolean; toolOutputMaxChars?: number },
options?: { stripMedia?: boolean; stripReasoning?: boolean; toolOutputMaxChars?: number },
) {
const result: UIMessage[] = []
const toolNames = new Set<string>()
Expand Down Expand Up @@ -956,6 +956,7 @@ export const toModelMessagesEffect = Effect.fnUntraced(function* (
})
}
if (part.type === "reasoning") {
if (options?.stripReasoning) continue
if (differentModel) {
if (part.text.trim().length > 0)
assistantMessage.parts.push({
Expand Down Expand Up @@ -1012,9 +1013,9 @@ export const toModelMessagesEffect = Effect.fnUntraced(function* (
export function toModelMessages(
input: WithParts[],
model: Provider.Model,
options?: { stripMedia?: boolean; toolOutputMaxChars?: number },
options?: { stripMedia?: boolean; stripReasoning?: boolean; toolOutputMaxChars?: number },
): Promise<ModelMessage[]> {
return Effect.runPromise(toModelMessagesEffect(input, model, options).pipe(Effect.provide(EffectLogger.layer)))
return Effect.runPromise(toModelMessagesEffect(input, model, options).pipe(Effect.provide(EffectLogger.layer), Effect.map((msgs) => msgs ?? [])))
}

export function page(input: { sessionID: SessionID; limit: number; before?: string }) {
Expand Down
4 changes: 2 additions & 2 deletions packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export const layer = Layer.effect(
model: mdl,
sessionID: input.session.id,
retries: 2,
messages: [{ role: "user", content: "Generate a title for this conversation:\n" }, ...msgs],
messages: [{ role: "user", content: "Generate a title for this conversation:\n" }, ...(msgs ?? [])],
})
.pipe(
Stream.filter((e): e is Extract<LLM.Event, { type: "text-delta" }> => e.type === "text-delta"),
Expand Down Expand Up @@ -1581,7 +1581,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the
sessionID,
parentSessionID: session.parentID,
system,
messages: [...modelMsgs, ...(isLastStep ? [{ role: "assistant" as const, content: MAX_STEPS }] : [])],
messages: [...(modelMsgs ?? []), ...(isLastStep ? [{ role: "assistant" as const, content: MAX_STEPS }] : [])],
tools,
model,
toolChoice: format.type === "json_schema" ? "required" : undefined,
Expand Down
43 changes: 43 additions & 0 deletions packages/opencode/test/session/message-v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,49 @@ describe("session.message-v2.toModelMessage", () => {
])
})

test("converts reasoning to text when assistant model differs", async () => {
const userID = "m-user"
const assistantID = "m-assistant"

const input: MessageV2.WithParts[] = [
{
info: userInfo(userID),
parts: [
{
...basePart(userID, "u1"),
type: "text",
text: "think about this",
},
] as MessageV2.Part[],
},
{
info: assistantInfo(assistantID, userID, undefined, { providerID: "other", modelID: "other" }),
parts: [
{
...basePart(assistantID, "a1"),
type: "reasoning",
text: "reasoning trace",
time: { start: 0 },
metadata: { anthropic: { signature: "sig-abc" } },
},
] as MessageV2.Part[],
} as MessageV2.WithParts,
]

expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([
{
role: "user",
content: [{ type: "text", text: "think about this" }],
},
{
role: "assistant",
content: [
{ type: "text", text: "reasoning trace" },
],
},
])
})

test("replaces compacted tool output with placeholder", async () => {
const userID = "m-user"
const assistantID = "m-assistant"
Expand Down
Loading