Files
archie/internal/app/archied/chat_turn_model.go
T
sam 4192a9f27c fix(archied): omit the max-token bound on reasoning-class models
The OpenAI chat-completions provider in the pinned ai-sdk serialises a
non-zero output-token bound as `max_tokens`, which reasoning-class models
reject with HTTP 400 ("Use 'max_completion_tokens' instead"). Send no bound
at all for a model the catalog marks reasoning; classic models keep their
bound. Covers the chat turn path and MCP sampling, the two call sites that
set one today.

The reasoning flag rides TurnPrepareContext from the catalog details the
gateway already resolves, so archie-core keeps one notion of "reasoning
model": messaging.ModelDetails.Reasoning. A max_completion_tokens path in
ai-sdk is still needed to ever bound these models; that is filed separately.
2026-10-02 10:25:39 +10:00

217 lines
6.7 KiB
Go

package archied
import (
"context"
"encoding/json"
"fmt"
"github.com/samcharles93/ai-sdk/chat"
"github.com/samcharles93/ai-sdk/core"
"github.com/samcharles93/ai-sdk/runtime"
"github.com/samcharles93/archie-core/internal/agentexec"
"github.com/samcharles93/archie-core/internal/gateway"
"github.com/samcharles93/archie-core/internal/tools"
)
// chatTurnModel adapts the provider-specific ai-sdk runtime to the gateway's
// provider-neutral TurnModel seam. Tool construction remains here because it
// depends on runtime registries and configured execution limits.
type chatTurnModel struct {
// llm resolves the provider runtime at each turn, so a runtime swapped by
// a live model-settings update is the one the turn after it runs on.
llm func() *runtime.Runtime
registry *tools.Registry
maxSteps int
limits agentexec.ToolLimits
// outcomes records each call's result for /status (see sendChatTurn).
outcomes *providerOutcomeRecorder
}
func newChatTurnModel(
llm func() *runtime.Runtime,
registry *tools.Registry,
maxSteps int,
limits agentexec.ToolLimits,
outcomes *providerOutcomeRecorder,
) gateway.TurnModel {
return &chatTurnModel{
llm: llm,
registry: registry,
maxSteps: maxSteps,
limits: limits,
outcomes: outcomes,
}
}
func (m *chatTurnModel) Prepare(
ctx context.Context,
req gateway.TurnPrepareContext,
) (gateway.PreparedTurnModel, error) {
options, err := chatGenerateOptions(ctx, nil, m.registry, m.maxSteps, m.limits, req.Extra, req.ContextWindow)
if err != nil {
return nil, err
}
toolSchema, err := json.Marshal(options.Tools)
if err != nil {
return nil, err
}
return &preparedChatTurnModel{
llm: m.llm(),
model: req.Model,
options: options,
reasoning: req.Reasoning,
toolInfo: toolSummaries(options.Tools),
toolTokens: gateway.EstimateTokens(string(toolSchema)),
outcomes: m.outcomes,
icons: toolIcons(m.registry),
}, nil
}
type preparedChatTurnModel struct {
llm *runtime.Runtime
model string
options core.GenerateOptions
// reasoning is the catalog's class for this model, carried from the
// prepare context so Generate can choose the output-token parameter the
// provider accepts.
reasoning bool
toolInfo []gateway.ToolSummary
toolTokens int
outcomes *providerOutcomeRecorder
// icons maps tool name to the icon that tool registered, for chat
// surfaces to render. A nil map yields "" for every lookup, which is
// the no-icon rendering.
icons map[string]string
}
// toolIcons snapshots the icon each registered tool declares. It is taken at
// prepare time alongside the tool set itself, so a turn renders the icons of
// the tools it was actually given.
func toolIcons(registry *tools.Registry) map[string]string {
if registry == nil {
return nil
}
icons := make(map[string]string)
for _, entry := range registry.All() {
if entry.Emoji != "" {
icons[entry.Name] = entry.Emoji
}
}
return icons
}
func (m *preparedChatTurnModel) ToolSummaries() []gateway.ToolSummary {
return append([]gateway.ToolSummary(nil), m.toolInfo...)
}
// buildTurnMessages maps the gateway's compressed history onto ai-sdk chat
// messages. The turn's inbound media is not part of stored history, so it
// is merged here into the final user message as content parts: the model
// sees the caption text plus each attachment's bytes (or URL), exactly for
// the turn it arrived in.
func buildTurnMessages(request gateway.TurnModelRequest) []chat.Message {
messages := make([]chat.Message, len(request.Messages))
for i, message := range request.Messages {
role := chat.RoleUser
switch message.Role {
case "assistant":
role = chat.RoleAssistant
case "system":
role = chat.RoleSystem
}
messages[i] = chat.Message{Role: role, Content: message.Content}
}
parts := mediaParts(request.Media)
if len(parts) == 0 {
return messages
}
last := len(messages) - 1
// The media belongs to the inbound message, which is always the final
// user entry; if the view ever ends differently, content stays intact
// over silently attaching parts to an unrelated message.
if last < 0 || messages[last].Role != chat.RoleUser {
return messages
}
userMessages := messages[last]
userMessages.Parts = append(chat.Parts{chat.TextPart{Text: userMessages.Content}}, parts...)
userMessages.Content = ""
messages[last] = userMessages
return messages
}
// mediaParts converts inbound media attachments into ai-sdk content
// parts: images get ImagePart, everything else FilePart. An attachment
// carrying neither bytes nor a URL names nothing consumable and is
// dropped -- the turn's text still reaches the model.
func mediaParts(media []gateway.MediaAttachment) []chat.Part {
if len(media) == 0 {
return nil
}
parts := make([]chat.Part, 0, len(media))
for _, att := range media {
if att.Type == "image" {
switch {
case len(att.Data) > 0 && att.MIMEType != "":
parts = append(parts, chat.ImagePart{Data: att.Data, MediaType: att.MIMEType})
case att.URL != "":
parts = append(parts, chat.ImagePart{URL: att.URL})
}
continue
}
part := chat.FilePart{Name: att.FileName, MediaType: att.MIMEType}
if len(att.Data) > 0 {
part.Data = att.Data
} else {
part.URL = att.URL
}
if len(part.Data) == 0 && part.URL == "" {
continue
}
if part.MediaType == "" {
part.MediaType = "application/octet-stream"
}
parts = append(parts, part)
}
if len(parts) == 0 {
return nil
}
return parts
}
func (m *preparedChatTurnModel) ToolSchemaTokens() int {
return m.toolTokens
}
// maxTokensForRequest returns the output-token bound to place on an ai-sdk
// generate request for a model. A reasoning-class model gets no bound at all:
// the OpenAI chat-completions provider serialises a non-zero bound as
// `max_tokens`, which reasoning models reject with HTTP 400 ("Use
// 'max_completion_tokens' instead"), and the pinned ai-sdk release has no
// max_completion_tokens path, so no bound is the only usable form. A classic
// model keeps the caller's bound unchanged.
func maxTokensForRequest(reasoning bool, bound int) int {
if reasoning {
return 0
}
return bound
}
func (m *preparedChatTurnModel) Generate(
ctx context.Context,
request gateway.TurnModelRequest,
stream gateway.TurnStream,
) (string, error) {
options := m.options
options.Messages = buildTurnMessages(request)
// This is one provider response's output allowance, not a turn-
// continuation budget. Tool loops remain free to continue.
options.MaxTokens = maxTokensForRequest(m.reasoning, request.MaxOutputTokens)
// A runtime swapped to nil -- a live update that left no usable provider
// configured -- is a refused turn, not a panic.
if m.llm == nil {
return "", fmt.Errorf("llm chat: no model runtime is configured")
}
return sendChatTurn(ctx, m.llm, m.model, options, stream, m.outcomes, m.icons)
}