mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-26 23:28:43 +00:00
fix(ai): keep the prompt cache when switching effort on Copilot GPT
This commit is contained in:
parent
8ec08f4913
commit
8191a3b889
6 changed files with 70 additions and 5 deletions
|
|
@ -30,7 +30,7 @@ import {
|
|||
withUntypedFrames,
|
||||
} from "./shared.js"
|
||||
import { classifyProviderFailure } from "../provider-error.js"
|
||||
import { effortUpdate } from "../effort-updates.js"
|
||||
import { effortUpdate, resolveEffortUpdates } from "../effort-updates.js"
|
||||
import { toolsUpdate } from "../tool-updates.js"
|
||||
import { OpenResponsesOptions } from "./utils/open-responses-options.js"
|
||||
import { Lifecycle } from "./utils/lifecycle.js"
|
||||
|
|
@ -894,9 +894,10 @@ export const fromRequestWithAdapter = Effect.fn("OpenResponses.fromRequestWithAd
|
|||
const definitions = ProviderShared.flattenTools(request.tools)
|
||||
const options = OpenResponsesOptions.resolve(request)
|
||||
const custom = OpenAIFreeform.tools(request)
|
||||
const updates = resolveEffortUpdates(request, options.reasoningEffort)
|
||||
return {
|
||||
...(yield* lowerConversation(request, adapter)),
|
||||
...lowerGeneration(request, options),
|
||||
...(yield* lowerConversation(updates.request, adapter)),
|
||||
...lowerGeneration(request, { ...options, reasoningEffort: updates.effort }),
|
||||
tools: definitions.length === 0 ? undefined : yield* lowerTools(definitions, adapter, custom),
|
||||
tool_choice:
|
||||
allowedToolChoice(options, custom) ??
|
||||
|
|
@ -1629,6 +1630,8 @@ export const protocol = Protocol.make({
|
|||
step: (state: ParserState, event: Event) => step(state, normalize(state, event)),
|
||||
terminal,
|
||||
},
|
||||
// Not every Open Responses server accepts `configuration_update`, so only models that opt in get it.
|
||||
supportsEffortUpdates: (request) => request.model.capabilities?.reasoning?.updates === true,
|
||||
})
|
||||
|
||||
export const httpTransport = HttpTransport.sseJson.with<OpenResponsesBody>()
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import { AnthropicMessages } from "../src/protocols/anthropic-messages.js"
|
|||
import { OpenAIResponses } from "../src/protocols/openai-responses.js"
|
||||
import { Gemini } from "../src/protocols/gemini.js"
|
||||
import { AmazonBedrockMantle, GoogleVertexMessages, OpenAI } from "../src/providers.js"
|
||||
import { OpenAICompatibleResponses } from "../src/protocols/openai-compatible-responses.js"
|
||||
import { applyEffortUpdates } from "../src/effort-updates.js"
|
||||
import { it, testEffect } from "./lib/effect.js"
|
||||
import { dynamicResponse } from "./lib/http.js"
|
||||
|
|
@ -572,3 +573,40 @@ describe("OpenAI Responses effort updates", () => {
|
|||
}),
|
||||
)
|
||||
})
|
||||
|
||||
describe("Open Responses effort updates", () => {
|
||||
const compatible = (updates?: boolean) =>
|
||||
OpenAICompatibleResponses.route
|
||||
.with({
|
||||
provider: "github-copilot",
|
||||
endpoint: { baseURL: "https://api.githubcopilot.test" },
|
||||
auth: Auth.bearer("test"),
|
||||
})
|
||||
.model({ id: "gpt-6.2-sol", capabilities: updates === undefined ? undefined : { reasoning: { updates } } })
|
||||
|
||||
it.effect("lowers markers to configuration_update items and freezes reasoning.effort for opted-in models", () =>
|
||||
Effect.gen(function* () {
|
||||
const prepared = yield* compileRequest(
|
||||
ai.request({ model: compatible(true), messages: conversation, providerOptions: { reasoningEffort: "low" } }),
|
||||
)
|
||||
|
||||
expect(prepared.body.reasoning).toEqual({ effort: "high" })
|
||||
expect(prepared.body.input).toEqual([
|
||||
{ type: "message", role: "user", content: [{ type: "input_text", text: "Before." }] },
|
||||
{ type: "configuration_update", reasoning: { effort: "low" } },
|
||||
{ type: "message", role: "user", content: [{ type: "input_text", text: "After." }] },
|
||||
])
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("strips markers unless the model opts in", () =>
|
||||
Effect.gen(function* () {
|
||||
const prepared = yield* compileRequest(
|
||||
ai.request({ model: compatible(), messages: conversation, providerOptions: { reasoningEffort: "low" } }),
|
||||
)
|
||||
|
||||
expect(updates(prepared.body)).toEqual([])
|
||||
expect(prepared.body.reasoning).toEqual({ effort: "low" })
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -187,6 +187,7 @@ function build(id: Model.ID, remote: UsableModel, baseURL: string, previous?: Mo
|
|||
settings: Provider.mergeOverlay(previous?.settings, { baseURL: route.baseURL }),
|
||||
headers: previous?.headers,
|
||||
body: previous?.body,
|
||||
compatibility: route.effortUpdates ? { supportsEffortUpdates: true } : undefined,
|
||||
capabilities: {
|
||||
tools: remote.capabilities.supports.tool_calls,
|
||||
input,
|
||||
|
|
@ -230,6 +231,8 @@ export function routeFor(modelID: string, advertised: Endpoint | undefined, root
|
|||
return {
|
||||
endpoint: selected,
|
||||
package: PACKAGES[selected],
|
||||
// Like OpenAI's own Responses API, Copilot takes mid-conversation `configuration_update` items from GPT-6 on.
|
||||
effortUpdates: selected === "responses" && gptVersion(modelID) >= 6,
|
||||
// Copilot serves Anthropic Messages under /v1 and its OpenAI APIs at the root.
|
||||
baseURL: selected === "messages" ? `${root}/v1` : root,
|
||||
}
|
||||
|
|
@ -244,12 +247,16 @@ function endpoint(supported: ReadonlyArray<string> | undefined): Endpoint | unde
|
|||
// Without advertised endpoints, Copilot serves GPT-5 and later (except GPT-5 mini), Grok and MAI Code
|
||||
// only on /responses, and Gemini and the rest on /chat/completions.
|
||||
function fallbackEndpoint(modelID: string): Endpoint {
|
||||
const gpt = /^gpt-(\d+)/.exec(modelID)
|
||||
if (gpt !== null && Number(gpt[1]) >= 5 && !modelID.startsWith("gpt-5-mini")) return "responses"
|
||||
if (gptVersion(modelID) >= 5 && !modelID.startsWith("gpt-5-mini")) return "responses"
|
||||
if (modelID.startsWith("grok-") || modelID.startsWith("mai-code-")) return "responses"
|
||||
return "chat"
|
||||
}
|
||||
|
||||
function gptVersion(modelID: string) {
|
||||
const gpt = /^gpt-(\d+)/.exec(modelID)
|
||||
return gpt === null ? 0 : Number(gpt[1])
|
||||
}
|
||||
|
||||
function variants(remote: UsableModel, messages: boolean): Model.Info["variants"] {
|
||||
const efforts = remote.capabilities.supports.reasoning_effort ?? []
|
||||
if (!messages && efforts.length) {
|
||||
|
|
|
|||
|
|
@ -294,6 +294,7 @@ export const GithubCopilotPlugin = define({
|
|||
)
|
||||
model.package = route.package
|
||||
model.settings = Provider.mergeOverlay(model.settings, { baseURL: route.baseURL })
|
||||
if (route.effortUpdates) model.compatibility = { ...model.compatibility, supportsEffortUpdates: true }
|
||||
})
|
||||
}
|
||||
})
|
||||
|
|
|
|||
|
|
@ -165,3 +165,12 @@ test("prices cache reads from either token price spelling", async () => {
|
|||
await server.stop(true)
|
||||
}
|
||||
})
|
||||
|
||||
test("sends effort switches as configuration updates only for GPT-6 and later on Responses", () => {
|
||||
const root = "https://api.githubcopilot.test"
|
||||
expect(CopilotModels.routeFor("gpt-6.2-sol", "responses", root).effortUpdates).toBe(true)
|
||||
expect(CopilotModels.routeFor("gpt-6-luna", undefined, root).effortUpdates).toBe(true)
|
||||
expect(CopilotModels.routeFor("gpt-5.6-luna", "responses", root).effortUpdates).toBe(false)
|
||||
expect(CopilotModels.routeFor("gpt-6-luna", "chat", root).effortUpdates).toBe(false)
|
||||
expect(CopilotModels.routeFor("grok-4.6", "responses", root).effortUpdates).toBe(false)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -590,6 +590,7 @@ describe("GithubCopilotPlugin", () => {
|
|||
"gpt-4.1": "@opencode/ai/providers/openai",
|
||||
"gpt-5": "@opencode/ai/providers/openai-compatible",
|
||||
"gpt-5.1-codex": "@opencode/ai/providers/openai-compatible",
|
||||
"gpt-6-luna": "@opencode/ai/providers/openai",
|
||||
"gpt-5-mini": "@opencode/ai/providers/openai-compatible",
|
||||
"gpt-4o": "@opencode/ai/providers/openai-compatible",
|
||||
"grok-4.5": "@opencode/ai/providers/openai-compatible",
|
||||
|
|
@ -635,6 +636,12 @@ describe("GithubCopilotPlugin", () => {
|
|||
required(yield* models.get(Provider.ID.githubCopilot, Model.ID.make("gemini-3.8-flash"))),
|
||||
)
|
||||
expect(resolved.route.id).toBe("openai-compatible-chat")
|
||||
|
||||
const luna = yield* ModelResolver.fromCatalogModel(
|
||||
required(yield* models.get(Provider.ID.githubCopilot, Model.ID.make("gpt-6-luna"))),
|
||||
)
|
||||
expect(luna.route.id).toBe("openai-compatible-responses")
|
||||
expect(luna.capabilities?.reasoning?.updates).toBe(true)
|
||||
}),
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue