67 lines
2.0 KiB
Go
67 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const quillSystem = `You are Quill crow, the owner's assistant and point of contact in a nest of AI crows.
|
|
Answer the owner in one or two plain sentences. You are text-only: no web, no files, no tools.`
|
|
|
|
const quillHistoryCap = 20
|
|
|
|
// quillHistory builds a block of the most recent nest messages (excluding the
|
|
// requesting message), formatted as "[author|kind] text" with text truncated
|
|
// to 200 runes. Returns a single line when no earlier messages exist.
|
|
func quillHistory(all []Message, excludeID int) string {
|
|
var filtered []Message
|
|
for _, m := range all {
|
|
if m.ID != excludeID {
|
|
filtered = append(filtered, m)
|
|
}
|
|
}
|
|
if len(filtered) == 0 {
|
|
return "The nest has no earlier messages."
|
|
}
|
|
if len(filtered) > quillHistoryCap {
|
|
filtered = filtered[len(filtered)-quillHistoryCap:]
|
|
}
|
|
var sb strings.Builder
|
|
for _, m := range filtered {
|
|
text := []rune(m.Text)
|
|
if len(text) > 200 {
|
|
text = text[:200]
|
|
}
|
|
sb.WriteString(fmt.Sprintf("[%s|%s] %s\n", m.Author, m.Kind, string(text)))
|
|
}
|
|
return strings.TrimRight(sb.String(), "\n")
|
|
}
|
|
|
|
// respondQuill answers a message addressed to Quill, the assistant service
|
|
// role (CHATROOM_SPEC section 6). D2: reads the nest for status before
|
|
// composing its reply.
|
|
func (s *Server) respondQuill(nest Nest, req Message) {
|
|
budget := &Budget{name: "quill-reply", limit: s.p.G.TokenBudgetPerItem}
|
|
system := systemFor("Quill", quillSystem, false)
|
|
history := quillHistory(s.msgs.All(nest.ID), req.ID)
|
|
prompt := history + "\n---\nFROM: " + req.Author + "\nMESSAGE: " + req.Text
|
|
answer, err := s.p.Flock.ask(s.ctx, nil, "Quill", system, prompt, budget)
|
|
if err != nil {
|
|
return
|
|
}
|
|
reply := Message{
|
|
NestID: nest.ID,
|
|
Kind: "deliver",
|
|
Author: "Quill",
|
|
Text: strings.TrimSpace(answer),
|
|
To: req.Author,
|
|
RefersTo: req.MsgID(),
|
|
TaggedBy: "harness",
|
|
Timestamp: time.Now().UTC().Format(time.RFC3339),
|
|
}
|
|
if record, err := s.msgs.Append(reply); err == nil {
|
|
s.emitNestMessage(record)
|
|
}
|
|
}
|