mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
25 lines
1.4 KiB
TypeScript
25 lines
1.4 KiB
TypeScript
// Changing the top-level reasoning effort invalidates the provider prompt cache. Protocols with a native
|
|
// per-message update keep it frozen and lower `Message.effort(...)` markers instead; other routes strip them.
|
|
import { type LLMRequest, type EffortPart, type Message } from "./schema/messages.js"
|
|
|
|
export const effortUpdate = (message: Message): EffortPart | undefined => {
|
|
if (message.role !== "system" || message.content.length !== 1) return undefined
|
|
const part = message.content[0]
|
|
return part.type === "effort" ? part : undefined
|
|
}
|
|
|
|
export const stripEffortUpdates = (request: LLMRequest) => {
|
|
const messages = request.messages.filter((message) => effortUpdate(message) === undefined)
|
|
return messages.length === request.messages.length ? request : { ...request, messages }
|
|
}
|
|
|
|
export const applyEffortUpdates = (request: LLMRequest): LLMRequest =>
|
|
request.model.route.supportsEffortUpdates?.(request) ? request : stripEffortUpdates(request)
|
|
|
|
// Reverted or forked history can leave the last marker disagreeing with the requested effort.
|
|
export const resolveEffortUpdates = (request: LLMRequest, current: string | undefined) => {
|
|
const updates = request.messages.flatMap((message) => effortUpdate(message) ?? [])
|
|
if (updates.length === 0) return { request, effort: current }
|
|
if (updates.at(-1)?.effort !== current) return { request: stripEffortUpdates(request), effort: current }
|
|
return { request, effort: updates[0]?.previous }
|
|
}
|