Demonstrates two harness surfaces the previous examples skipped: - SkillProvider: KB articles in examples/support-agent/kb/ are exposed via runtime.SkillProvider. The runtime auto-registers load_skill and inlines a compact article index into the static (cacheable) system prompt; full bodies load on demand. - PermissionChecker: tickets_update and escalate_to_human are gated on a 'supervisor' flag. FilterToolDefs hides the gated tools from the LLM tool list in non-supervisor mode so it doesn't waste turns inventing calls that would just be denied. Five concrete tools (kb_search, tickets_get, tickets_create, tickets_update, escalate_to_human) plus the auto-registered load_skill. Five seed KB articles cover refund policy, escalation matrix, account recovery, password reset, and API rate limits — chosen to exercise the common support patterns each tool involves. In-memory ticket store (examples/support-agent/store.go) seeded with four tickets in different states. Webhook delivery for escalations is optional via SUPPORT_AGENT_WEBHOOK; when unset the tool prints the escalation banner to stdout, so the demo runs with only ANTHROPIC_API_KEY set. Tests (support_test.go) cover store CRUD, defensive copying, the permission checker's read/write split, and KB indexing/search — all offline, no API needed.
115 lines
3.2 KiB
Go
115 lines
3.2 KiB
Go
// KBSkills implements runtime.SkillProvider on top of the embedded
|
|
// markdown KB articles in ./kb/. The runtime calls FormatIndex once at
|
|
// BuildRuntime time and inlines the result into the static (cacheable)
|
|
// system prompt; full bodies load on demand via the auto-registered
|
|
// load_skill tool, keyed by the slug (filename without extension).
|
|
package main
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"io/fs"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// kbFS is the bundled set of KB articles. Embedded so the example runs
|
|
// from anywhere — no need to chdir to the example directory.
|
|
//
|
|
//go:embed kb/*.md
|
|
var kbFS embed.FS
|
|
|
|
// KBSkills is a SkillProvider backed by kbFS. Articles are loaded once
|
|
// at construction; a real consumer would re-parse on a watcher signal.
|
|
type KBSkills struct {
|
|
bySlug map[string]kbArticle
|
|
slugs []string // sorted, for stable index ordering
|
|
}
|
|
|
|
type kbArticle struct {
|
|
slug string
|
|
summary string // first non-blank, non-heading line of the body
|
|
body string
|
|
}
|
|
|
|
// NewKBSkills loads every kb/*.md, parses out a one-line summary, and
|
|
// returns a ready-to-use SkillProvider. Panics on a malformed embed
|
|
// (build-time failure, not a runtime concern).
|
|
func NewKBSkills() *KBSkills {
|
|
entries, err := fs.ReadDir(kbFS, "kb")
|
|
if err != nil {
|
|
panic(fmt.Sprintf("kb embed: %v", err))
|
|
}
|
|
k := &KBSkills{bySlug: make(map[string]kbArticle, len(entries))}
|
|
for _, e := range entries {
|
|
if e.IsDir() || !strings.HasSuffix(e.Name(), ".md") {
|
|
continue
|
|
}
|
|
raw, err := fs.ReadFile(kbFS, "kb/"+e.Name())
|
|
if err != nil {
|
|
panic(fmt.Sprintf("read kb/%s: %v", e.Name(), err))
|
|
}
|
|
slug := strings.TrimSuffix(e.Name(), ".md")
|
|
body := string(raw)
|
|
k.bySlug[slug] = kbArticle{
|
|
slug: slug,
|
|
summary: extractSummary(body),
|
|
body: body,
|
|
}
|
|
k.slugs = append(k.slugs, slug)
|
|
}
|
|
sort.Strings(k.slugs)
|
|
return k
|
|
}
|
|
|
|
// FormatIndex returns the cacheable index of articles. Kept compact —
|
|
// the index goes into every cached prompt, so we summarise rather than
|
|
// inline bodies.
|
|
func (k *KBSkills) FormatIndex() string {
|
|
var b strings.Builder
|
|
b.WriteString("## Knowledge Base\n\n")
|
|
b.WriteString("Use load_skill(\"<slug>\") to read the full article.\n\n")
|
|
for _, slug := range k.slugs {
|
|
fmt.Fprintf(&b, "- **%s** — %s\n", slug, k.bySlug[slug].summary)
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// Get implements SkillProvider. Returns the raw markdown body.
|
|
func (k *KBSkills) Get(name string) (string, bool) {
|
|
a, ok := k.bySlug[name]
|
|
if !ok {
|
|
return "", false
|
|
}
|
|
return a.body, true
|
|
}
|
|
|
|
// Slugs returns the article slugs (sorted). Used by KBSearchTool.
|
|
func (k *KBSkills) Slugs() []string { return append([]string(nil), k.slugs...) }
|
|
|
|
// Article returns the (summary, body) for a slug. Used by KBSearchTool
|
|
// for snippet preview.
|
|
func (k *KBSkills) Article(slug string) (summary, body string, ok bool) {
|
|
a, ok := k.bySlug[slug]
|
|
if !ok {
|
|
return "", "", false
|
|
}
|
|
return a.summary, a.body, true
|
|
}
|
|
|
|
// extractSummary pulls the first non-heading, non-blank line out of an
|
|
// article. Falls back to the slug-friendly title if nothing matches.
|
|
func extractSummary(body string) string {
|
|
for _, line := range strings.Split(body, "\n") {
|
|
t := strings.TrimSpace(line)
|
|
if t == "" || strings.HasPrefix(t, "#") {
|
|
continue
|
|
}
|
|
if len(t) > 160 {
|
|
t = t[:160] + "..."
|
|
}
|
|
return t
|
|
}
|
|
return "(no summary)"
|
|
}
|