BuildKnowledgeDigest renders the index card for a knowledge context. budget caps the output in bytes; <=0 takes the default. The model-facing scaffolding is literal English on purpose (prompt text, not UI).
(fc *FileContext, budget int)
| 42 | // caps the output in bytes; <=0 takes the default. The model-facing scaffolding |
| 43 | // is literal English on purpose (prompt text, not UI). |
| 44 | func BuildKnowledgeDigest(fc *FileContext, budget int) string { |
| 45 | if fc == nil { |
| 46 | return "" |
| 47 | } |
| 48 | if budget <= 0 { |
| 49 | budget = defaultDigestBudget |
| 50 | } |
| 51 | sources := aggregateSources(fc) |
| 52 | |
| 53 | var b strings.Builder |
| 54 | fmt.Fprintf(&b, "📚 KNOWLEDGE BASE: %s\n", fc.Name) |
| 55 | if d := strings.TrimSpace(fc.Description); d != "" { |
| 56 | fmt.Fprintf(&b, "Description: %s\n", d) |
| 57 | } |
| 58 | if repo := fc.Metadata[knowledgeMetaRepoURL]; repo != "" { |
| 59 | commit := fc.Metadata[knowledgeMetaCommit] |
| 60 | if len(commit) > 12 { |
| 61 | commit = commit[:12] |
| 62 | } |
| 63 | fmt.Fprintf(&b, "Origin: %s @ %s\n", repo, commit) |
| 64 | } |
| 65 | fmt.Fprintf(&b, "Scale: %d document(s), %d passage(s), ~%s tokens of source material (NOT in context)\n", |
| 66 | len(sources), fc.FileCount, approxTokens(fc.TotalSize)) |
| 67 | b.WriteString("This is an index card only. Relevant passages are auto-retrieved into the ") |
| 68 | b.WriteString("conversation each turn; answer from those passages and cite their source paths. ") |
| 69 | b.WriteString("If coverage looks thin, say what additional section of this index you need.\n") |
| 70 | b.WriteString("\nTable of contents:\n") |
| 71 | |
| 72 | header := b.Len() |
| 73 | writeDigestTree(&b, sources, budget-header) |
| 74 | return b.String() |
| 75 | } |
| 76 | |
| 77 | // aggregateSources folds per-chunk virtual files back into their source |
| 78 | // documents, deterministically ordered by path. |