299 lines
12 KiB
Go
299 lines
12 KiB
Go
package archiegateway
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"path/filepath"
|
|
|
|
"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/modelloop"
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
|
"github.com/samcharles93/archie-core/internal/domain/usage"
|
|
"github.com/samcharles93/archie-core/internal/events"
|
|
"github.com/samcharles93/archie-core/internal/gateway"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
|
"github.com/samcharles93/archie-core/internal/installtype"
|
|
"github.com/samcharles93/archie-core/internal/prompts"
|
|
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
|
"github.com/samcharles93/archie-core/internal/tools"
|
|
)
|
|
|
|
// chatSetup contains the inputs needed to build a chat turn runner and the
|
|
// services that support it. Every field is intentionally explicit so the
|
|
// function signature acts as a contract of what a chat turn depends on.
|
|
type chatSetup struct {
|
|
// Cfg is read once per setup function via Get(); the process is not
|
|
// running yet at this point, so the reload-safe Holder is mostly a
|
|
// formality, but matching the daemon's API keeps callers honest.
|
|
Cfg *config.Holder
|
|
// ToolLimits resolves the tool-output policy at each turn, so a live
|
|
// tool-settings update reaches the next chat turn's tool options rather
|
|
// than the value captured when the runner was built.
|
|
ToolLimits func() modelloop.ToolLimits
|
|
// LLM resolves the provider runtime at each use: a live provider-settings
|
|
// or model-aliases update swaps the runtime wholesale (ai-sdk's
|
|
// Runtime caches the provider instances it built), so the pointer cannot
|
|
// be captured at construction. nil means no model runtime is configured.
|
|
LLM func() *runtime.Runtime
|
|
ChatModels gateway.ModelManager
|
|
// Usage records each model call; nil records nothing.
|
|
Usage *usageSink
|
|
ToolReg *tools.Registry
|
|
Soul gateway.SoulSource
|
|
ChatTasks gateway.TaskCreator
|
|
ChatTaskLister gateway.ChatTaskLister
|
|
ChatTaskLogs gateway.ChatTaskLogReader
|
|
ChatTaskActor gateway.ChatTaskActor
|
|
DefaultChatIdentity string
|
|
// Bus carries primary-input events: a completed
|
|
// chat turn is published here so input-driven curators can wake. Nil
|
|
// disables turn events (tests, minimal setups).
|
|
Bus *events.Bus
|
|
Log *slog.Logger
|
|
// MemoryEngine is the durable-memory read surface (b.memEngines' active
|
|
// engine, resolved by cfg.Memory.Engine). Nil disables the chat <memory>
|
|
// block for every turn runner built from this setup.
|
|
MemoryEngine gateway.MemoryStore
|
|
// MemoryWriter is the same active engine's write surface, used to build the
|
|
// per-turn memory_create/update/delete/list tools. Nil omits those tools.
|
|
MemoryWriter gateway.MemoryWriteStore
|
|
// ProviderOutcomes records the outcome of every chat-model call this
|
|
// process makes, which the /status health source reads back. Nil disables
|
|
// recording (the recorder's methods tolerate that rather than panicking).
|
|
ProviderOutcomes *providerOutcomeRecorder
|
|
// Transcriber is the voice-transcription capability built by
|
|
// setupChatRuntime from this process's own [models]/[providers]. Every turn
|
|
// runner carries it so a speech attachment is transcribed before its
|
|
// message is recorded. Nil disables the capability (no role configured, or
|
|
// the provider credential did not resolve), leaving the frontend's note.
|
|
Transcriber messaging.Transcriber
|
|
// OwnerID is the turn owner shared by every runner in this process, so
|
|
// recovery and persistence agree on which turns are this process's.
|
|
OwnerID string
|
|
}
|
|
|
|
// componentInstallTypeEnricher builds Service.Enrich: it knows how to
|
|
// describe the component kinds this process can say anything true about. Anything else (a check command reporting a component ID this
|
|
// process has no source for) is left alone -- an empty return leaves
|
|
// whatever the check command itself set.
|
|
func componentInstallTypeEnricher(nats config.NATSConfig) func(string) (string, string) {
|
|
return func(componentID string) (installType, reference string) {
|
|
switch componentID {
|
|
case releaseupdate.ComponentDaemon:
|
|
return installtype.Type(), ""
|
|
case releaseupdate.ComponentNATS:
|
|
switch nats.Mode {
|
|
case config.NATSModeEmbedded:
|
|
return config.NATSModeEmbedded, ""
|
|
case config.NATSModeExternal:
|
|
return config.NATSModeExternal, nats.URL
|
|
default:
|
|
return "", ""
|
|
}
|
|
default:
|
|
return "", ""
|
|
}
|
|
}
|
|
}
|
|
|
|
func makeUpdateService(reader controlplanerpc.ResourceReader, cfg config.Config) *releaseupdate.Service {
|
|
updates := controlplanerpc.UpdateService(reader, filepath.Join(cfg.WorkDir, "telegram-update-deferrals.json"))
|
|
updates.Enrich = componentInstallTypeEnricher(cfg.NATS)
|
|
return updates
|
|
}
|
|
|
|
func makeChatLLMResponder(
|
|
ctx context.Context,
|
|
channel string,
|
|
s chatSetup,
|
|
sessionStore gateway.SessionStore,
|
|
router *gateway.Router,
|
|
) (gateway.LLMResponder, gateway.LLMStreamResponder, error) {
|
|
if s.LLM == nil || s.LLM() == nil {
|
|
return nil, nil, nil
|
|
}
|
|
runner, err := newChatTurnRunner(ctx, channel, s, sessionStore, router)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
return runner.Respond, runner.RespondStream, nil
|
|
}
|
|
|
|
func newChatTurnRunner(
|
|
ctx context.Context,
|
|
channel string,
|
|
s chatSetup,
|
|
sessionStore gateway.SessionStore,
|
|
router *gateway.Router,
|
|
) (*gateway.TurnRunner, error) {
|
|
cfg := s.Cfg.Get()
|
|
runner, err := gateway.NewTurnRunner(gateway.TurnRunnerConfig{
|
|
Router: router,
|
|
Sessions: sessionStore,
|
|
Models: s.ChatModels,
|
|
Soul: s.Soul,
|
|
Model: newChatTurnModel(s.LLM, s.ToolReg, cfg.Chat.MaxSteps, s.ToolLimits, s.ProviderOutcomes, s.Usage),
|
|
TaskLister: s.ChatTaskLister,
|
|
Tasks: s.ChatTasks,
|
|
TaskLogs: s.ChatTaskLogs,
|
|
TaskActor: s.ChatTaskActor,
|
|
TaskIdentity: s.DefaultChatIdentity,
|
|
Bus: s.Bus,
|
|
BotUser: cfg.BotUser,
|
|
Channel: channel,
|
|
Operator: cfg.Chat.Operator,
|
|
Workspace: cfg.Chat.Workspace,
|
|
Repos: chatRepoEnv(cfg, s.DefaultChatIdentity),
|
|
MemoryEngine: s.MemoryEngine,
|
|
MemoryWriter: s.MemoryWriter,
|
|
UserIdentity: userIdentityResolver(),
|
|
Transcriber: s.Transcriber,
|
|
OwnerID: s.OwnerID,
|
|
Log: s.Log,
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("chat turn runner for %s: %w", channel, err)
|
|
}
|
|
if err := runner.Recover(ctx); err != nil && s.Log != nil {
|
|
s.Log.ErrorContext(ctx, "recover chat turns", "channel", channel, "err", err)
|
|
}
|
|
return runner, nil
|
|
}
|
|
|
|
// chatRepoEnv renders the repositories the chat agent manages, with their
|
|
// forge host and default branch, into the prompt's <env> block so the agent
|
|
// knows its scope without probing the filesystem. It follows the same
|
|
// identity resolution as chatTaskProfiles: when identities are configured,
|
|
// the one matching the active chat identity wins (falling back to the first
|
|
// identity with repositories, which is what chatTaskProfiles selects as the
|
|
// default); otherwise the single-identity repositories are used.
|
|
func chatRepoEnv(cfg config.Config, identity string) []gateway.RepoEnv {
|
|
repos, forge := cfg.Repos, cfg.Forge
|
|
if len(cfg.Identities) > 0 {
|
|
repos = nil
|
|
for _, id := range cfg.Identities {
|
|
if id.Name == identity {
|
|
repos = id.Repos
|
|
break
|
|
}
|
|
if len(repos) == 0 && len(id.Repos) > 0 {
|
|
repos = id.Repos
|
|
}
|
|
}
|
|
}
|
|
env := make([]gateway.RepoEnv, 0, len(repos))
|
|
for _, repo := range repos {
|
|
env = append(env, gateway.RepoEnv{
|
|
FullName: repo.FullName(),
|
|
Forge: forge.Host,
|
|
DefaultBranch: repo.BaseBranch(),
|
|
})
|
|
}
|
|
return env
|
|
}
|
|
|
|
// sendChatTurn runs one chat-model call and records its outcome for /status'
|
|
// chat-model line. Every chat turn passes through here, so one wrapper covers
|
|
// the whole chat path; curator work is the other model caller in this process
|
|
// and records at its own adapter (curatorLLMRunner). Anything that reaches the
|
|
// runtime without recording leaves /status reporting a stale last-known
|
|
// outcome, which is a /status that lies about the provider.
|
|
func sendChatTurn(ctx context.Context, llm *runtime.Runtime, chatModel string, options core.GenerateOptions, turn gateway.TurnStream, outcomes *providerOutcomeRecorder, usageRecords *usageSink, icons map[string]string) (string, error) {
|
|
text, used, err := runChatTurn(ctx, llm, chatModel, options, turn, icons)
|
|
usageRecords.record(ctx, usage.SourceChat, chatModel, used)
|
|
// record tolerates a nil recorder: a setup built without one (tests, a
|
|
// deployment whose health surface is unwired) still makes its calls.
|
|
outcomes.record(chatModel, err)
|
|
return text, err
|
|
}
|
|
|
|
// runChatTurn returns the reply and the tokens the call used, which a
|
|
// failed call may still report.
|
|
func runChatTurn(ctx context.Context, llm *runtime.Runtime, chatModel string, options core.GenerateOptions, turn gateway.TurnStream, icons map[string]string) (string, chat.Usage, error) {
|
|
if turn == nil {
|
|
result, err := llm.Chat(ctx, chatModel, options)
|
|
if err != nil {
|
|
return "", result.TotalUsage, fmt.Errorf("llm chat: %w", err)
|
|
}
|
|
return result.Text, result.TotalUsage, nil
|
|
}
|
|
stream, err := llm.ChatStream(ctx, chatModel, options)
|
|
if err != nil {
|
|
return "", chat.Usage{}, fmt.Errorf("llm chat stream: %w", err)
|
|
}
|
|
text := gateway.DrainStreamParts(stream.FullStream, turn, icons)
|
|
var used chat.Usage
|
|
if stream.Usage != nil {
|
|
used, _ = stream.Usage()
|
|
}
|
|
if _, err := stream.FinishReason(); err != nil {
|
|
return "", used, fmt.Errorf("llm chat stream: %w", err)
|
|
}
|
|
return text, used, nil
|
|
}
|
|
|
|
// chatTitleGenerator proposes session titles through the chat model. It
|
|
// is a pure proposal: it persists nothing, so a failed or slow title
|
|
// call can never corrupt conversation history or fail the turn. The
|
|
// gateway bounds the call with its own timeout; errors leave the session
|
|
// untitled and are logged here.
|
|
type chatTitleGenerator struct {
|
|
log *slog.Logger
|
|
llm func() *runtime.Runtime
|
|
chatModels gateway.ModelManager
|
|
usage *usageSink
|
|
}
|
|
|
|
// newChatTitleGenerator wires an LLM-backed title generator for a chat
|
|
// setup, or nil when no model is available. Title generation is optional
|
|
// everywhere, so every caller tolerates nil.
|
|
func newChatTitleGenerator(s chatSetup) gateway.TitleGenerator {
|
|
if s.LLM == nil || s.ChatModels == nil {
|
|
return nil
|
|
}
|
|
return &chatTitleGenerator{log: s.Log, llm: s.LLM, chatModels: s.ChatModels, usage: s.Usage}
|
|
}
|
|
|
|
func (g *chatTitleGenerator) GenerateTitle(ctx context.Context, sessionID, message, reply string) (string, error) {
|
|
messages := []chat.Message{
|
|
{Role: chat.RoleSystem, Content: prompts.SessionTitle()},
|
|
{Role: chat.RoleUser, Content: prompts.SessionTitleExchange(message, reply)},
|
|
}
|
|
// No tools: a title is a single completion. The active model is read
|
|
// at call time so a /model switch applies to titles too.
|
|
//
|
|
// Deliberately records no outcome for /status. A title is cosmetic,
|
|
// generated in a detached goroutine under its own 30s bound
|
|
// (gateway.titleGenerationTimeout), and its error is swallowed by the
|
|
// caller. Sharing the process-wide recorder let a title that merely timed
|
|
// out overwrite the chat-model health line with "failed", reporting a
|
|
// broken provider on the strength of a call no user was waiting for.
|
|
// No tool set and no stream, so no tool call can be reported: this turn
|
|
// needs no icons.
|
|
llm := g.llm()
|
|
if llm == nil {
|
|
return "", fmt.Errorf("no model runtime is configured")
|
|
}
|
|
model, err := g.chatModels.ModelFor(ctx)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
text, err := sendChatTurn(ctx, llm, model,
|
|
core.GenerateOptions{Messages: messages, MaxSteps: 1}, nil, nil, g.usage, nil)
|
|
if err != nil {
|
|
if g.log != nil {
|
|
g.log.ErrorContext(ctx, "session title generation failed", "session", sessionID, "err", err)
|
|
}
|
|
return "", fmt.Errorf("generate title for session %s: %w", sessionID, err)
|
|
}
|
|
if g.log != nil {
|
|
g.log.InfoContext(ctx, "session title generated", "session", sessionID)
|
|
}
|
|
return text, nil
|
|
}
|