package archiegateway import ( "context" "fmt" "log/slog" "time" "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/curator" "github.com/samcharles93/archie-core/internal/domain/usage" "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/infrastructure/controlplanestore" infraMemory "github.com/samcharles93/archie-core/internal/infrastructure/memory" "github.com/samcharles93/archie-core/internal/infrastructure/sessioncurator" "github.com/samcharles93/archie-core/internal/infrastructure/skillcurator" "github.com/samcharles93/archie-core/internal/infrastructure/toolbuilder" ) func (b *server) setupCurators(ctx context.Context) bool { log := b.log.With("component", "curator") model, err := b.chatModels.ModelFor(ctx) if err != nil { log.WarnContext(ctx, "curators disabled: chat model unavailable; configure a chat model and restart the gateway", "err", err) return false } skillsRoot := b.cfg.SkillsDir if skillsRoot == "" { skillsRoot = b.cfg.WorkDir } b.curatorRegistry = curator.NewRegistry(curator.Registrar{ Log: log.With("component", "curator"), Events: curatorEventSink{b.bus}, Tools: toolbuilder.New(b.toolReg), MemoryEngines: b.memEngines, Skills: skillcurator.NewStore(skillsRoot), Conversations: sessioncurator.NewAdapter(b.chatSessionStore, b.cfg.BotUser), LLM: curatorLLMRunner{llm: b.chatLLM, outcomes: b.providerOutcomes, usage: b.usage}, Model: model, }) if err := b.curatorRegistry.Register(skillcurator.New(skillcurator.DefaultInterval)); err != nil { log.ErrorContext(ctx, "skill curator registration failed", "err", err) } if err := b.curatorRegistry.Register(sessioncurator.New(sessioncurator.DefaultInterval, infraMemory.EngineName)); err != nil { log.ErrorContext(ctx, "session-memory curator registration failed", "err", err) } b.curatorRuntime = curator.NewRuntime(b.curatorRegistry, curator.RuntimeConfig{}) curator.WakeOnPrimaryInput(ctx, b.bus, b.curatorRuntime, events.KindTurnCompleted) rt := b.curatorRuntime b.addCleanup(shutdownCuratorRuntime(rt, log)) reg := b.curatorRegistry b.addCleanup(shutdownCuratorRegistry(reg, log)) return true } //nolint:contextcheck // shutdown runs after the parent context is cancelled func shutdownCuratorRuntime(rt *curator.Runtime, log *slog.Logger) func() { return func() { stopCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() if err := rt.Stop(stopCtx); err != nil { log.Error("curator runtime shutdown", "err", err) } } } //nolint:contextcheck // shutdown runs after the parent context is cancelled func shutdownCuratorRegistry(reg *curator.Registry, log *slog.Logger) func() { return func() { stopCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() if err := reg.Stop(stopCtx); err != nil { log.Error("curator registry shutdown", "err", err) } } } // curatorEventSink adapts the in-process event bus to the curator family's // narrow emission contract. Bus publish is non-blocking with bounded, // dropping per-subscriber buffers, so curator activity can never // backpressure the daemon or a chat turn. type curatorEventSink struct { b *events.Bus } func (s curatorEventSink) Emit(kind, detail string, data map[string]any) { s.b.Publish(context.Background(), events.Event{Kind: kind, Detail: detail, Data: data}) } // curatorLLMRunner adapts the shared ai-sdk runtime to the curator // family's narrow LLMRunner contract: one model reference, plain // messages, and the declared tool set -- no streaming. Tools are built // through the same agentexec path a chat turn uses, so a curator's // declared set is converted to a runnable core.ToolSet rather than a // parallel catalogue. type curatorLLMRunner struct { // llm resolves the runtime at each call, so a live model-settings update // that swaps it wholesale is the one the next curator run reads; a // snapshot taken at construction would keep curators on the providers // boot started with. llm func() *runtime.Runtime // outcomes records this call for /status alongside sendChatTurn's. A // curator call is a model call this process made, and it does not pass // through sendChatTurn, so without this a daemon whose only recent model // traffic was curator work reports "no calls attempted yet" while the // provider is demonstrably reachable -- or worse, keeps showing a much // older chat outcome as current. outcomes *providerOutcomeRecorder usage *usageSink } func (r curatorLLMRunner) Chat(ctx context.Context, req curator.ChatRequest) (curator.ChatResult, error) { // modelloop.NewRuntime returns nil when no provider is configured, and a // nil *runtime.Runtime panics on the first method call. A curator asking // for a completion on a daemon with no providers is a misconfiguration, // not a reason to take the process down. if r.llm == nil { return curator.ChatResult{}, fmt.Errorf("curator chat: no model runtime is configured") } llm := r.llm() if llm == nil { return curator.ChatResult{}, fmt.Errorf("curator chat: no model runtime is configured") } msgs := make([]chat.Message, 0, len(req.Messages)) for _, m := range req.Messages { msgs = append(msgs, chat.Message{Role: chat.Role(m.Role), Content: m.Content}) } toolSet, err := modelloop.BuildToolSetFrom(req.Tools, modelloop.ToolSetOptions{}) if err != nil { r.outcomes.record(req.Model, err) return curator.ChatResult{}, err } res, err := llm.Chat(ctx, req.Model, core.GenerateOptions{ Messages: msgs, Tools: toolSet, MaxSteps: max(req.MaxSteps, 1), MaxParallelToolCalls: modelloop.MaxParallelToolCalls, }) r.outcomes.record(req.Model, err) r.usage.record(ctx, usage.SourceCurator, req.Model, res.TotalUsage) if err != nil { return curator.ChatResult{}, err } calls := make([]curator.ToolCall, 0, len(res.ToolCalls)) for _, call := range res.ToolCalls { calls = append(calls, curator.ToolCall{Name: call.ToolName, Input: call.Input}) } return curator.ChatResult{Text: res.Text, ToolCalls: calls}, nil } func (b *server) applyCurators(ctx context.Context, defs []config.CuratorDefinition) error { definitions := make([]curator.Definition, 0, len(defs)) for _, def := range defs { definitions = append(definitions, controlplanestore.CuratorDefinition(def)) } return b.curatorRuntime.ReplaceDefinitions(ctx, definitions) } func (b *server) watchCurators(ctx context.Context, version int64) error { updates, err := b.controlPlane.WatchCurators(ctx, version) if err != nil { return err } watchStatus(ctx, b.applyStatus.Report, b.log, controlplanestore.CuratorsKind, "curators", version, updates, b.controlPlane.WatchCurators, func(update controlplanestore.AppliedCurators) int64 { return update.Version }, func(update controlplanestore.AppliedCurators) error { return update.Err }, func(update controlplanestore.AppliedCurators) error { return b.applyCurators(ctx, update.Definitions) }) return nil } func (b *server) startCurators(ctx context.Context) error { // Curators wake on this process's turns and read its conversations. if !b.setupCurators(ctx) { return nil } if err := b.curatorRuntime.Start(ctx); err != nil { b.log.ErrorContext(ctx, "curator runtime startup", "err", err) } defs, version, err := b.controlPlane.Curators(ctx) if err == nil { err = b.applyCurators(ctx, defs) } b.applyStatus.Report(ctx, controlplanestore.CuratorsKind, version, err) if err != nil { b.log.ErrorContext(ctx, "curator definitions apply failed", "err", err) } if err := b.watchCurators(ctx, version); err != nil { return fmt.Errorf("watch curators: %w", err) } return nil }