Part of [CODAGT-1430](https://linear.app/codercom/issue/CODAGT-1430). Stacked on #31383. We had two rules for which media a model can take. `AcceptsFilePartMediaType` filtered user attachments, whilst `ToolResultMediaOmission` filtered tool results with its own lists, and only let images through on Anthropic. Now that fantasy converts tool result media like user files, the two should agree, so we'll keep one rule. `chatprovider.Model` now carries the configured provider type, and `AcceptsFilePartMediaType` reads a table keyed on the route, i.e. the configured provider plus the transport. `replaceUnsupportedMedia` applies it to user file parts and tool result media alike, including the pending tail of the turn prompt. As a result, tool result PDFs now reach Anthropic, and the 5 MiB inline image cap only applies to images. Azure OpenAI Chat Completions rejects file content parts with a 400 (`This model does not support file content types`, see https://github.com/Azure/azure-sdk-for-net/issues/51873), and Gemini's OpenAI-compatible endpoint ignores them. chatd reaches both through the openai-compat client, so the rule rejects PDFs there, and the model gets a text note instead of a failed request or a silently dropped file. Unsupported tool result media now gets the same note fantasy writes ("The tool returned `<type>` content, which cannot be displayed."), so the model sees one wording whichever layer drops it. I've also added a Tool results section to the MCP servers docs, and fixed the File attachments docs, which said every rejected attachment becomes text. Only text files do; a rejected PDF becomes a note that names it. I've also tested this end to end on Bedrock. I ran chatd from this branch with an Anthropic provider pointed at our AI gateway's Bedrock route (InvokeModel, us-east-2), plus an MCP tool that returns a PDF with a code word inside. Claude Haiku 5.5 and Claude Opus 5.5 (adaptive thinking) both read the code word from the tool result, and read it again on the next turn, when the PDF is replayed from history. The request carries the PDF as a `document` block inside `tool_result`, the same body chatd sends to Anthropic.
81 lines
2.9 KiB
Go
81 lines
2.9 KiB
Go
package chatprovider
|
|
|
|
import (
|
|
"charm.land/fantasy"
|
|
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatopenai"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
// Model pairs a language model client with the facts resolved when it was
|
|
// built. Its fields are unexported and nothing sets transport except NewModel,
|
|
// which derives it from the client, so no caller can choose a transport that
|
|
// disagrees with the client it wraps.
|
|
type Model struct {
|
|
lm fantasy.LanguageModel
|
|
transport chatopenai.Transport
|
|
// configuredProvider is the AI provider type the request is routed to.
|
|
// Empty means the client's own provider.
|
|
configuredProvider string
|
|
}
|
|
|
|
// NewModel pairs an already-built client with the transport resolved from that
|
|
// client's own identity. Callers must pass the config the client was built
|
|
// with, the one degree of freedom Model cannot police. ModelFromConfig is the
|
|
// only production caller.
|
|
func NewModel(lm fantasy.LanguageModel, openAIConfig *codersdk.ChatModelOpenAIConfig) Model {
|
|
if lm == nil {
|
|
// The invalid zero value lets callers report a nil client as an
|
|
// error instead of dereferencing it here.
|
|
return Model{}
|
|
}
|
|
return Model{
|
|
lm: lm,
|
|
transport: chatopenai.TransportFor(lm.Provider(), lm.Model(), openAIResponsesAPIOverride(openAIConfig)),
|
|
}
|
|
}
|
|
|
|
func (m Model) LanguageModel() fantasy.LanguageModel { return m.lm }
|
|
|
|
func (m Model) Provider() string { return m.lm.Provider() }
|
|
|
|
func (m Model) ModelID() string { return m.lm.Model() }
|
|
|
|
func (m Model) Transport() chatopenai.Transport { return m.transport }
|
|
|
|
// WithConfiguredProvider records the AI provider type that requests reach
|
|
// through this client. Several provider types share one client, and the
|
|
// provider type decides which media the route accepts.
|
|
func (m Model) WithConfiguredProvider(provider string) Model {
|
|
m.configuredProvider = provider
|
|
return m
|
|
}
|
|
|
|
// RouteProvider returns the configured provider type, or the client's own
|
|
// provider when none was set.
|
|
func (m Model) RouteProvider() string {
|
|
if m.configuredProvider != "" {
|
|
return NormalizeProvider(m.configuredProvider)
|
|
}
|
|
return NormalizeProvider(m.Provider())
|
|
}
|
|
|
|
// Valid reports whether m wraps a client rather than being the zero value.
|
|
func (m Model) Valid() bool { return m.lm != nil }
|
|
|
|
// WithLanguageModel replaces the wrapped client and keeps the resolved
|
|
// transport, because decorators such as debug recording do not change what the
|
|
// client speaks. It panics on an invalid receiver or a replacement that
|
|
// reports a different identity, because both would pair the kept transport
|
|
// with a client it was not resolved from.
|
|
func (m Model) WithLanguageModel(lm fantasy.LanguageModel) Model {
|
|
if !m.Valid() {
|
|
panic("chatprovider: WithLanguageModel on an invalid Model")
|
|
}
|
|
if lm == nil || lm.Provider() != m.lm.Provider() || lm.Model() != m.lm.Model() {
|
|
panic("chatprovider: WithLanguageModel replacement changes the model identity")
|
|
}
|
|
m.lm = lm
|
|
return m
|
|
}
|