package chat import ( "context" "encoding/json" "encoding/json/jsontext" "errors" "fmt" "strings" "time" "unicode/utf8" "github.com/google/uuid" "github.com/samcharles93/ai-sdk/chat" "github.com/samcharles93/ai-sdk/core" "github.com/samcharles93/archie-core/internal/platform/hub" "github.com/samcharles93/archie-core/internal/platform/prompts" "github.com/samcharles93/archie-core/modules/chat/internal/store" "github.com/samcharles93/archie-core/modules/governance" "github.com/samcharles93/archie-core/modules/memory" ) const ( // maxConcurrentTurns bounds the conversations answered at once. maxConcurrentTurns = 4 // historyTurns is how many earlier turns a turn's model sees. historyTurns = 20 // maxSteps bounds the model calls one turn makes. maxSteps = 16 // turnTimeout ends a turn that never finishes. turnTimeout = 10 * time.Minute // flushEvery is how often streamed text is recorded while it arrives. flushEvery = 150 * time.Millisecond poll = 2 * time.Second ) // Work answers queued turns and settles held actions until ctx ends. Turns a // stopped process left running are failed first, visibly, since the reply they // were streaming is gone. func (m *Module) Work(ctx context.Context) { m.recover(ctx) slots := make(chan struct{}, maxConcurrentTurns) tick := time.NewTicker(poll) defer tick.Stop() for { m.settleActions(ctx) m.startTurns(ctx, slots) select { case <-ctx.Done(): // Wait for the turns in flight to record how they ended. for range maxConcurrentTurns { slots <- struct{}{} } return case <-m.wake: case <-tick.C: } } } // startTurns starts queued turns while there are free slots. func (m *Module) startTurns(ctx context.Context, slots chan struct{}) { for { select { case slots <- struct{}{}: default: return } t, ok, err := m.store.Claim(ctx) if err != nil || !ok { <-slots if err != nil && ctx.Err() == nil { m.log.ErrorContext(ctx, "claim chat turn", "error", err) } return } go func() { defer func() { <-slots; m.poke() }() m.answer(ctx, t) }() } } func (m *Module) poke() { select { case m.wake <- struct{}{}: default: } } func (m *Module) recover(ctx context.Context) { const reason = "Archie restarted before this turn finished. Send it again." ids, err := m.store.Interrupted(ctx, reason) if err != nil { m.log.ErrorContext(ctx, "fail interrupted chat turns", "error", err) return } for _, id := range ids { m.record(ctx, id, kindError, map[string]any{"text": reason}) } } // record stores one streamed event and tells the turn's live readers. func (m *Module) record(ctx context.Context, turnID uuid.UUID, kind string, data any) { raw, err := json.Marshal(data) if err != nil { m.log.ErrorContext(ctx, "encode chat event", "turn", turnID, "kind", kind, "error", err) return } seq, err := m.store.AddEvent(context.WithoutCancel(ctx), turnID, kind, raw) if err != nil { m.log.ErrorContext(ctx, "record chat event", "turn", turnID, "kind", kind, "error", err) return } m.hub.Publish(hub.Note{Topic: topic(turnID), Seq: seq}) } // answer runs one turn to its end and records how it ended. func (m *Module) answer(parent context.Context, t store.Turn) { ctx, cancel := context.WithTimeout(parent, turnTimeout) defer cancel() m.mu.Lock() m.running[t.ID] = cancel m.mu.Unlock() defer func() { m.mu.Lock() delete(m.running, t.ID) m.mu.Unlock() }() reply, reasoning, err := m.generate(ctx, t) done := context.WithoutCancel(ctx) cur, rerr := m.store.Turn(done, m.orgID, t.Owner, t.ID) stopped := rerr == nil && cur.Cancel switch { case stopped: m.finish(done, t.ID, stateCancelled, reply, reasoning, "") m.record(done, t.ID, kindCancelled, map[string]any{"text": reply}) case err != nil: msg := failure(ctx, err) if parent.Err() != nil { msg = "Archie stopped before this turn finished. Send it again." } m.log.ErrorContext(done, "chat turn failed", "turn", t.ID, "error", err) m.finish(done, t.ID, stateFailed, reply, reasoning, msg) m.record(done, t.ID, kindError, map[string]any{"text": msg}) default: m.finish(done, t.ID, stateSucceeded, reply, reasoning, "") m.record(done, t.ID, kindDone, map[string]any{"text": reply}) m.title(done, t, reply) } } func (m *Module) finish(ctx context.Context, id uuid.UUID, state, reply, reasoning, failure string) { if err := m.store.Finish(ctx, id, state, reply, reasoning, failure); err != nil { m.log.ErrorContext(ctx, "finish chat turn", "turn", id, "error", err) } } // failure is what the operator is told about a failed turn. func failure(ctx context.Context, err error) string { if errors.Is(ctx.Err(), context.DeadlineExceeded) { return fmt.Sprintf("The turn ran for %s without finishing and was stopped.", turnTimeout) } return err.Error() } // generate streams the model's answer to t, recording every part as it comes, // and returns the reply and the thinking behind it. func (m *Module) generate(ctx context.Context, t store.Turn) (string, string, error) { d, ok := m.uses() if !ok { return "", "", errors.New("chat isn't ready yet") } ctx, err := m.actingAs(ctx, d, t.Owner) if err != nil { return "", "", err } model, err := d.Settings.ChatModelForAlias(ctx, "") if err != nil { return "", "", fmt.Errorf("no agent model is configured: %w", err) } provider, info, err := d.Settings.AgentProvider(ctx, model) if err != nil { return "", "", fmt.Errorf("agent model %s: %w", model.Model, err) } history, err := m.store.History(ctx, t.SessionID, t.Seq, historyTurns) if err != nil { return "", "", err } tools := m.tools(ctx, d, t) memory := "" if err := m.allowed(ctx, d, governance.ActionRead, governance.KindMemory, ""); err == nil { memory = m.memoryBlock(ctx, d, t.Owner) } instructions, err := d.Settings.Instructions(ctx) if err != nil { return "", "", fmt.Errorf("read the organisation's instructions: %w", err) } system := prompts.ChatSystem(prompts.ChatContext{ Tools: toolSummaries(tools), Channel: "dashboard", Page: page(t), Model: model.Model, SessionID: t.SessionID.String(), Memory: memory, Instructions: instructions, Now: time.Now(), }) stream, err := core.StreamText(ctx, guardedProvider{Provider: provider, m: m, d: d, t: t, offered: tools}, core.GenerateOptions{ Model: model.Model, ModelInfo: info, System: system, Messages: conversation(history, t), Tools: tools, MaxSteps: maxSteps, }) if err != nil { return "", "", fmt.Errorf("start the reply: %w", err) } reply, reasoning, streamErr := m.drain(ctx, t.ID, stream.FullStream) if _, err := stream.FinishReason(); err != nil && streamErr == nil { streamErr = err } return reply, reasoning, streamErr } func page(t store.Turn) string { switch { case t.PageLabel != "" && t.Page != "": return t.PageLabel + " (" + t.Page + ")" default: return t.Page } } // conversation is the turns before t and t itself, as model messages. func conversation(history []store.Turn, t store.Turn) []chat.Message { out := make([]chat.Message, 0, 2*len(history)+1) for _, h := range history { out = append(out, chat.Message{Role: chat.RoleUser, Content: h.Text}) reply := h.Reply switch { case h.State == stateFailed && reply == "": reply = "(this turn failed: " + h.Error + ")" case h.State == stateCancelled && reply == "": reply = "(the operator stopped this turn before a reply)" } if reply != "" { out = append(out, chat.Message{Role: chat.RoleAssistant, Content: reply}) } } return append(out, chat.Message{Role: chat.RoleUser, Content: t.Text}) } func (m *Module) memoryBlock(ctx context.Context, d Deps, owner string) string { notes, err := d.Memory.Notes(ctx, owner, memory.ClassDurableFact) if err != nil { m.log.WarnContext(ctx, "read memory for chat", "error", err) return "" } lines := make([]string, 0, len(notes)) for _, n := range notes { lines = append(lines, "- ["+n.ID.String()+"] "+n.Text) } return strings.Join(lines, "\n") } // pendingCall is a tool call waiting for its result. type pendingCall struct { name, parameters string started time.Time offset int } // drained is what a turn's stream has produced so far. Text and thinking are // buffered between flushes, so a fast stream isn't one row per token. type drained struct { m *Module turnID uuid.UUID reply, reasoning strings.Builder textBuf, thinkBuf strings.Builder runes int pending map[string]pendingCall err error } func (s *drained) flush(ctx context.Context) { if s.thinkBuf.Len() > 0 { s.m.record(ctx, s.turnID, kindReasoning, map[string]any{"text": s.thinkBuf.String()}) s.thinkBuf.Reset() } if s.textBuf.Len() > 0 { s.m.record(ctx, s.turnID, kindDelta, map[string]any{"text": s.textBuf.String()}) s.textBuf.Reset() } } func (s *drained) take(ctx context.Context, part core.StreamPart) { switch part.Type { case core.StreamPartReasoningDelta: s.reasoning.WriteString(part.ReasoningDelta) s.thinkBuf.WriteString(part.ReasoningDelta) case core.StreamPartTextDelta: s.reply.WriteString(part.TextDelta) s.textBuf.WriteString(part.TextDelta) s.runes += utf8.RuneCountInString(part.TextDelta) case core.StreamPartToolCall: if c := part.ToolCall; c != nil { s.pending[c.ToolCallID] = pendingCall{ name: c.ToolName, parameters: summarizeParameters(c.Input), started: time.Now(), offset: s.runes, } } case core.StreamPartToolResult: if r := part.ToolResult; r != nil { s.flush(ctx) call := s.pending[r.ToolCallID] delete(s.pending, r.ToolCallID) s.m.record(ctx, s.turnID, kindTool, toolCall(r, call)) } case core.StreamPartError: if part.Error != nil { s.err = part.Error } case core.StreamPartAbort: if s.err == nil { s.err = context.Canceled } } } // drain consumes the whole stream, recording reasoning, reply text and each // finished tool call. The stream's writes are synchronous, so it is read to // its close whatever happens, or the producer stalls. func (m *Module) drain(ctx context.Context, turnID uuid.UUID, parts <-chan core.StreamPart) (string, string, error) { s := &drained{m: m, turnID: turnID, pending: map[string]pendingCall{}} tick := time.NewTicker(flushEvery) defer tick.Stop() for { select { case <-tick.C: s.flush(ctx) case part, ok := <-parts: if !ok { s.flush(ctx) return s.reply.String(), s.reasoning.String(), s.err } s.take(ctx, part) } } } func toolCall(r *core.ToolResult, call pendingCall) ToolCall { name := r.ToolName if name == "" { name = call.name } out := ToolCall{ ID: r.ToolCallID, Name: name, Parameters: call.parameters, Output: truncateRunes(r.Output, outputMaxRunes), TextOffset: call.offset, } if r.Error != "" { out.Output, out.Failed = truncateRunes(r.Error, outputMaxRunes), true } if !call.started.IsZero() { out.DurationMS = time.Since(call.started).Milliseconds() } return out } // title names an untitled conversation after its first answered exchange. // It is a proposal: a failure leaves the conversation untitled. func (m *Module) title(ctx context.Context, t store.Turn, reply string) { s, err := m.store.Session(ctx, m.orgID, t.Owner, t.SessionID) if err != nil || s.Title != "" { return } d, ok := m.uses() if !ok { return } ctx, cancel := context.WithTimeout(ctx, 30*time.Second) defer cancel() model, err := d.Settings.ChatModelForAlias(ctx, "") if err != nil { return } provider, info, err := d.Settings.AgentProvider(ctx, model) if err != nil { return } res, err := core.GenerateText(ctx, provider, core.GenerateOptions{ Model: model.Model, ModelInfo: info, System: prompts.SessionTitle(), Prompt: "Operator: " + truncateRunes(t.Text, 2000) + "\n\nArchie: " + truncateRunes(reply, 2000), }) if err != nil { m.log.WarnContext(ctx, "title chat session", "session", t.SessionID, "error", err) return } title := strings.Trim(strings.TrimSpace(res.Text), `"'.`) if title == "" || strings.EqualFold(title, "NONE") || utf8.RuneCountInString(title) > 80 { return } if err := m.store.TitleIfUntitled(ctx, m.orgID, t.SessionID, title); err != nil { m.log.WarnContext(ctx, "title chat session", "session", t.SessionID, "error", err) } } // decodeData reads an event's data into v. func decodeData(raw jsontext.Value, v any) error { return json.Unmarshal(raw, v) }