Chat returned only the final answer, so a wrong tool choice, a hallucinated action, or a footgun like `systemctl stop` against `restart` was invisible from the conversation. Only text deltas were threaded through the turn, and tool calls executed inside Generate with nowhere to report. Replace the delta callback with gateway.TurnStream, which carries text fragments and completed tool calls on one ordered path from the model loop to the channel. The sequence is the point: a tool line that overtakes the sentence it interrupted describes a turn that never happened. Text-only callers use gateway.DeltaFunc and are unaffected. A tool is reported on its result, never on its call -- the call part carries no outcome yet, so reporting both would list every tool twice, once with nothing to say. Telegram renders it behind [chat.telegram] show_tool_calls, off by default: this is debugging visibility, and it narrates every internal step to provide it. The dashboard always shows it, being the operator's own observability surface; its tool lines are live-only and are not persisted with the conversation. The Telegram live reply changes mechanism, and with it what a frame MEANS. It used to stream through sendMessageDraft: drafts are ephemeral 30-second previews, so a tool call longer than that outlived the preview, and successive frames were observed concatenated with their cursors intact. It now sends one real message and edits it. An update used to be "the next preview of the answer"; it is now "the whole answer as it currently stands, replacing what is there". The cursor is appended at render time and never stored, so exactly one exists and always at the end, and a frame too long for one message is cut to its tail rather than rejected outright. A stopped or failed turn now edits the cursor away instead of leaving it blinking on a message nothing will finish. That cleanup runs on a live context because turnCtx is already dead, and it runs on both failure branches: ai-sdk raises an aborted stream as ErrAborted with ctx.Err() formatted in rather than wrapped, so errors.Is cannot be trusted to tell a /stop from a fault. Closes #467
98 lines
2.4 KiB
Go
98 lines
2.4 KiB
Go
package main
|
|
|
|
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
|
|
}
|
|
|
|
func newChatTurnModel(
|
|
llm *runtime.Runtime,
|
|
registry *tools.Registry,
|
|
maxSteps int,
|
|
limits agentexec.ToolLimits,
|
|
) gateway.TurnModel {
|
|
return &chatTurnModel{
|
|
llm: llm,
|
|
registry: registry,
|
|
maxSteps: maxSteps,
|
|
limits: limits,
|
|
}
|
|
}
|
|
|
|
func (m *chatTurnModel) Prepare(
|
|
ctx context.Context,
|
|
model string,
|
|
extra []tools.ToolEntry,
|
|
) (gateway.PreparedTurnModel, error) {
|
|
options, err := chatGenerateOptions(ctx, nil, m.registry, m.maxSteps, m.limits, extra)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
toolSchema, err := json.Marshal(options.Tools)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &preparedChatTurnModel{
|
|
llm: m.llm,
|
|
model: model,
|
|
options: options,
|
|
toolInfo: toolSummaries(options.Tools),
|
|
toolTokens: gateway.EstimateTokens(string(toolSchema)),
|
|
}, nil
|
|
}
|
|
|
|
type preparedChatTurnModel struct {
|
|
llm *runtime.Runtime
|
|
model string
|
|
options core.GenerateOptions
|
|
toolInfo []gateway.ToolSummary
|
|
toolTokens int
|
|
}
|
|
|
|
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}
|
|
}
|
|
options.MaxTokens = request.MaxTokens
|
|
return sendChatTurn(ctx, m.llm, m.model, options, stream)
|
|
}
|