A tool call rendered as a label line plus a ```text fence. Telegram draws a fenced block as a full code widget (language header bar, copy button, panel), and the block parser inserts a spacer paragraph before every block, so one call cost four rendered elements. Three calls filled the screen to show three word counts. Every toolPreview path already summarises to a single line, so the fence was never wrapping more than one. Drop it and put the preview on the header line. Tool lines now join with a hard break rather than a bare newline: a soft break would space-join them into one run-on paragraph, and a blank line would put the spacer back. Three calls are now three consecutive blocks. Two hardcoded values go with it. The status word is printed only when the outcome is not success, since every line otherwise read "done" and distinguished nothing. The icon is no longer a literal in the renderer: each builtin declares its own on tools.Tool (not on Schema, which is the model's contract), the provider carries it into ToolEntry, and the turn snapshots the registry's icons alongside its tool set. A tool that declares none renders without one rather than borrowing a generic wrench. Carries a dependency upgrade (go.mod, go.sum, tools/go.mod, tools/go.sum).
126 lines
3.5 KiB
Go
126 lines
3.5 KiB
Go
package archied
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"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 *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 *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,
|
|
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
|
|
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...)
|
|
}
|
|
|
|
func (m *preparedChatTurnModel) ToolSchemaTokens() int {
|
|
return m.toolTokens
|
|
}
|
|
|
|
func (m *preparedChatTurnModel) Generate(
|
|
ctx context.Context,
|
|
request gateway.TurnModelRequest,
|
|
stream gateway.TurnStream,
|
|
) (string, error) {
|
|
options := m.options
|
|
options.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
|
|
}
|
|
options.Messages[i] = chat.Message{Role: role, Content: message.Content}
|
|
}
|
|
// This is one provider response's output allowance, not a turn-
|
|
// continuation budget. Tool loops remain free to continue.
|
|
options.MaxTokens = request.MaxOutputTokens
|
|
return sendChatTurn(ctx, m.llm, m.model, options, stream, m.outcomes, m.icons)
|
|
}
|