Bump github.com/samcharles93/ai-sdk v0.1.40 -> v0.1.41. ai-sdk now sends max_completion_tokens for reasoning models and omits temperature when the model rejects it, so the max_tokens workarounds are no longer needed: the chat-turn bound is set directly and MCP sampling loses its reasoning branch. Classic models keep their existing bounds.
59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
package agentworker
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/samcharles93/ai-sdk/core"
|
|
"github.com/samcharles93/ai-sdk/runtime"
|
|
|
|
"github.com/samcharles93/archie-core/internal/agentexec/modelloop"
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/tools/mcp"
|
|
)
|
|
|
|
// taskSamplingModel returns the task's builder model, or "".
|
|
func taskSamplingModel(cfg config.TaskConfig) string {
|
|
return strings.TrimSpace(cfg.Models["builder"])
|
|
}
|
|
|
|
// taskSamplingHandler answers MCP sampling requests with the task's model.
|
|
// Without a runtime or model the request is refused.
|
|
func taskSamplingHandler(llm *runtime.Runtime, cfg config.TaskConfig) mcp.SamplingHandler {
|
|
return func(ctx context.Context, req mcp.SamplingRequest) (mcp.SamplingResult, error) {
|
|
if llm == nil {
|
|
return mcp.SamplingResult{}, errors.New("mcp sampling: no model runtime is configured")
|
|
}
|
|
model := taskSamplingModel(cfg)
|
|
if model == "" {
|
|
return mcp.SamplingResult{}, errors.New("mcp sampling: no builder model is configured")
|
|
}
|
|
messages, err := modelloop.SamplingMessages(req)
|
|
if err != nil {
|
|
return mcp.SamplingResult{}, err
|
|
}
|
|
limits := cfg.ModelLimits[model]
|
|
options := core.GenerateOptions{
|
|
Messages: messages,
|
|
System: req.SystemPrompt,
|
|
MaxSteps: 1,
|
|
MaxTokens: modelloop.SamplingMaxTokens(req.MaxTokens, limits.MaxOutputTokens),
|
|
}
|
|
if req.Temperature != nil {
|
|
options.Temperature = float32(*req.Temperature)
|
|
}
|
|
result, err := llm.Chat(ctx, model, options)
|
|
if err != nil {
|
|
return mcp.SamplingResult{}, fmt.Errorf("mcp sampling: %w", err)
|
|
}
|
|
return mcp.SamplingResult{
|
|
Model: model,
|
|
Role: "assistant",
|
|
Content: mcp.SamplingContent{Type: "text", Text: result.Text},
|
|
StopReason: string(result.FinishReason),
|
|
}, nil
|
|
}
|
|
}
|