Files
archie/internal/agentexec/sampling.go
T
sam c4457f0556 feat(agentworker): answer MCP sampling from the task's model
A task hosts the same configured MCP servers as a chat turn, but the
worker's provider builder passed no sampling handler, so a task-scoped
server's sampling/createMessage request got a JSON-RPC method-not-found
while the identical request succeeded on the chat path. The worker runs
its own model runtime, so it can answer.

Thread a sampling handler built from the task's builder model into the
worker's MCP provider builder, beside the chat path's handler. Build the
task's model runtime once and share it with the stage runners instead of
creating a second client for sampling.

Move the sampling message mapping and max-token bound to agentexec, next
to the runtime both paths call models through, so a server gets the same
bound on either path. Carry the catalog's reasoning flag in
ModelLimits: a reasoning-class builder model must omit the max-token
bound its provider rejects, exactly as the chat path does.
2026-10-02 12:20:31 +10:00

67 lines
2.4 KiB
Go

package agentexec
import (
"errors"
"fmt"
"github.com/samcharles93/ai-sdk/chat"
protocol "github.com/samcharles93/archie-core/internal/tools/mcp"
)
// DefaultSamplingMaxTokens bounds a server-requested completion when the
// request does not set maxTokens. A sampling request is a delegated
// sub-completion, not a full turn, so one MCP server cannot spend a
// turn-sized output allowance on a single request.
const DefaultSamplingMaxTokens = 4096
// SamplingMessages maps a server's sampling messages onto chat messages. Both
// MCP sampling host paths use it -- the daemon's chat handler and the agent
// worker's task handler -- so the same server is answered the same way
// wherever the client runs. A non-text content block or an unknown role is
// rejected rather than silently dropped: answering from a partial prompt would
// be a wrong answer, not a degraded one.
func SamplingMessages(req protocol.SamplingRequest) ([]chat.Message, error) {
if len(req.Messages) == 0 {
return nil, errors.New("mcp sampling: the request has no messages")
}
messages := make([]chat.Message, 0, len(req.Messages))
for i, message := range req.Messages {
if message.Content.Type != "" && message.Content.Type != "text" {
return nil, fmt.Errorf("mcp sampling: message %d has unsupported content type %q", i, message.Content.Type)
}
var role chat.Role
switch message.Role {
case "user":
role = chat.RoleUser
case "assistant":
role = chat.RoleAssistant
default:
return nil, fmt.Errorf("mcp sampling: message %d has unsupported role %q", i, message.Role)
}
messages = append(messages, chat.Message{Role: role, Content: message.Content.Text})
}
return messages, nil
}
// SamplingMaxTokens bounds a delegated completion to the request's own
// maxTokens, falling back to DefaultSamplingMaxTokens, and never past the
// model's own output ceiling (zero when the ceiling is unknown). A
// reasoning-class model gets no bound at all: its provider rejects the
// `max_tokens` parameter the chat-completions provider emits for a non-zero
// bound. Both the daemon's chat path and the agent worker answer sampling
// with this, so the same server gets the same bound on either path.
func SamplingMaxTokens(requested, modelCeiling int, reasoning bool) int {
maxTokens := requested
if maxTokens <= 0 {
maxTokens = DefaultSamplingMaxTokens
}
if modelCeiling > 0 && maxTokens > modelCeiling {
maxTokens = modelCeiling
}
if reasoning {
return 0
}
return maxTokens
}