mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-28 19:14:53 +00:00
fix(ai): replay Muse commentary after a tool call without its phase (#65774)
This commit is contained in:
parent
2c97d2de9b
commit
3161ddcbfb
2 changed files with 88 additions and 2 deletions
|
|
@ -653,10 +653,17 @@ const lowerToolResultOutput = Effect.fnUntraced(function* (
|
|||
|
||||
const DEFAULT_EFFORT = "medium"
|
||||
|
||||
// A call's output only follows the whole assistant turn, and Meta's Muse models, directly or through a gateway,
|
||||
// reject a commentary message replayed while a call is waiting for it. Text after a call keeps its place without
|
||||
// the phase there; other providers accept it and keep the phase.
|
||||
const rejectsPendingCommentary = (request: LLMRequest) =>
|
||||
request.model.provider === "meta" || /(?:^|\/)muse-/i.test(request.model.id)
|
||||
|
||||
export const lowerMessages = Effect.fnUntraced(function* (request: LLMRequest, adapter: ProviderAdapter) {
|
||||
const input: OpenResponsesInputItem[] = []
|
||||
const providerMetadataKey = metadataKey(request.model)
|
||||
const custom = OpenAIFreeform.tools(request)
|
||||
const pendingCommentaryRejected = rejectsPendingCommentary(request)
|
||||
const customTool = (part: ToolCallPart | ToolResultPart) =>
|
||||
part.namespace === undefined ? custom.get(part.name) : undefined
|
||||
|
||||
|
|
@ -701,6 +708,7 @@ export const lowerMessages = Effect.fnUntraced(function* (request: LLMRequest, a
|
|||
const content: TextPart[] = []
|
||||
const reasoningItems: Record<string, OpenResponsesReasoningInput> = {}
|
||||
const hostedToolItems = new Set<string>()
|
||||
let calledTool = false
|
||||
const flushText = () => {
|
||||
if (content.length === 0) return
|
||||
const groups = content.reduce<
|
||||
|
|
@ -709,7 +717,8 @@ export const lowerMessages = Effect.fnUntraced(function* (request: LLMRequest, a
|
|||
const partMetadata = part.providerMetadata?.[providerMetadataKey]
|
||||
const id = itemID(part.providerMetadata, providerMetadataKey) ?? metadata?.itemId
|
||||
const partPhase = messagePhase(partMetadata?.phase)
|
||||
const phase = partPhase === undefined ? metadata?.phase : partPhase
|
||||
const declared = partPhase === undefined ? metadata?.phase : partPhase
|
||||
const phase = calledTool && declared === "commentary" && pendingCommentaryRejected ? undefined : declared
|
||||
const group = groups.at(-1)
|
||||
if (group && group.id === id && group.phase === phase) group.parts.push(part)
|
||||
else groups.push({ id, phase, parts: [part] })
|
||||
|
|
@ -761,6 +770,7 @@ export const lowerMessages = Effect.fnUntraced(function* (request: LLMRequest, a
|
|||
flushText()
|
||||
if (part.providerExecuted === true) continue
|
||||
input.push(lowerToolCall(part, providerMetadataKey, customTool(part)))
|
||||
calledTool = true
|
||||
continue
|
||||
}
|
||||
if (part.type === "tool-result" && part.providerExecuted === true) {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { describe, expect } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { ai, LLMEvent, Message } from "../../src/effect.js"
|
||||
import { OpenAI } from "../../src/providers.js"
|
||||
import { Meta, OpenAI } from "../../src/providers.js"
|
||||
import { configure } from "../../src/providers/openai-compatible-responses.js"
|
||||
import { compileRequest, LLMClient } from "../../src/route/client.js"
|
||||
import { it } from "../lib/effect.js"
|
||||
|
|
@ -118,3 +118,79 @@ for (const model of [
|
|||
)
|
||||
})
|
||||
}
|
||||
|
||||
describe("Responses replay of commentary after a tool call in the same turn", () => {
|
||||
const compatible = configure({ apiKey: "test-key", baseURL: "https://responses.example.test/v1" })
|
||||
const call = (key: string, id: string, input: Record<string, unknown>) => ({
|
||||
type: "tool-call" as const,
|
||||
id,
|
||||
name: "execute",
|
||||
input,
|
||||
providerMetadata: { [key]: { itemId: `fc_${id}` } },
|
||||
})
|
||||
const commentary = (key: string, id: string, text: string) => ({
|
||||
type: "text" as const,
|
||||
text,
|
||||
providerMetadata: { [key]: { itemId: id, phase: "commentary" } },
|
||||
})
|
||||
|
||||
for (const [model, keepsPhase] of [
|
||||
[Meta.configure({ apiKey: "test-key" }).responses("muse-ember-1.0"), false],
|
||||
[OpenAI.configure({ apiKey: "test-key" }).responses("muse-ember-1.0"), false],
|
||||
[compatible.model("meta/muse-spark-1.3"), false],
|
||||
[OpenAI.configure({ apiKey: "test-key" }).responses("gpt-6.1"), true],
|
||||
[compatible.model("example-model"), true],
|
||||
] as const) {
|
||||
it.effect(`${keepsPhase ? "keeps" : "drops"} the phase for ${model.route.protocol} ${model.id}`, () =>
|
||||
Effect.gen(function* () {
|
||||
const key = model.route.providerMetadataKey ?? "openresponses"
|
||||
const prepared = yield* compileRequest(
|
||||
ai.request({
|
||||
model,
|
||||
messages: [
|
||||
Message.user("Check the fillet"),
|
||||
Message.make({
|
||||
role: "assistant",
|
||||
content: [
|
||||
commentary(key, "msg_before", "Reading the schematic."),
|
||||
call(key, "call_a", {}),
|
||||
commentary(key, "msg_after", "Checking the fillet."),
|
||||
call(key, "call_b", { code: "1" }),
|
||||
],
|
||||
}),
|
||||
Message.make({
|
||||
role: "tool",
|
||||
content: [
|
||||
{ type: "tool-result", id: "call_a", name: "execute", result: { type: "error", value: "Bad input" } },
|
||||
{ type: "tool-result", id: "call_b", name: "execute", result: { type: "text", value: "1" } },
|
||||
],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
)
|
||||
expect(prepared.body.input.slice(1)).toEqual([
|
||||
{
|
||||
type: "message",
|
||||
role: "assistant",
|
||||
id: "msg_before",
|
||||
phase: "commentary",
|
||||
status: "completed",
|
||||
content: [{ type: "output_text", text: "Reading the schematic." }],
|
||||
},
|
||||
{ type: "function_call", id: "fc_call_a", call_id: "call_a", name: "execute", arguments: "{}" },
|
||||
{
|
||||
type: "message",
|
||||
role: "assistant",
|
||||
id: "msg_after",
|
||||
...(keepsPhase ? { phase: "commentary" } : {}),
|
||||
status: "completed",
|
||||
content: [{ type: "output_text", text: "Checking the fillet." }],
|
||||
},
|
||||
{ type: "function_call", id: "fc_call_b", call_id: "call_b", name: "execute", arguments: '{"code":"1"}' },
|
||||
{ type: "function_call_output", call_id: "call_a", output: "Bad input" },
|
||||
{ type: "function_call_output", call_id: "call_b", output: "1" },
|
||||
])
|
||||
}),
|
||||
)
|
||||
}
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue