solutions: Solutions Guide engine (catalog, rules, wizard state, native config, knowledge index, CLI)
New nested module yetano.dev/yetano/core/solutions: loads the catalog in assets/solutions on top of the Big Picture, evaluates questions and conditions, resolves packages/modules/capabilities, generates native configuration from typed fragments with per-value origins, and builds architecture graph, plan, lock file and handbook. Knowledge index with yetano: references, search and lint (incl. manual sections). CLI with yet's conventions (solution, task, knowledge, open), standalone as yetano-solutions. No execution, no network. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
586e76889c
commit
2481a0dd27
48 files changed
+10304
No files matched your search
@@ -0,0 +1,34 @@
|
||||
package knowledge
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"yetano.dev/yetano/core/solutions/catalog"
|
||||
)
|
||||
|
||||
func assetsIndex(t *testing.T) *Index {
|
||||
t.Helper()
|
||||
dir, _ := filepath.Abs("../../../assets/solutions")
|
||||
if _, err := os.Stat(filepath.Join(dir, "catalog.yaml")); err != nil {
|
||||
t.Skip("assets/solutions not present")
|
||||
}
|
||||
c, err := catalog.Load(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return Build(c)
|
||||
}
|
||||
|
||||
func TestAssetsLint(t *testing.T) {
|
||||
ix := assetsIndex(t)
|
||||
for _, i := range Lint(ix) {
|
||||
if i.Severity == "error" {
|
||||
t.Error(i)
|
||||
} else {
|
||||
t.Log(i)
|
||||
}
|
||||
}
|
||||
t.Logf("%d entries, %d manual topics, missing sources: %v", len(ix.Entries), len(ix.Topics), ix.Missing)
|
||||
}
|
||||
@@ -0,0 +1,439 @@
|
||||
// Package knowledge is the Knowledge Engine: one local index over the
|
||||
// catalog, the Big Picture and the existing manuals, with reference
|
||||
// resolution per client, backlinks, search, lint and coverage. The index
|
||||
// is derived data: it is rebuilt from the sources and never edited.
|
||||
package knowledge
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"yetano.dev/yetano/core/solutions/catalog"
|
||||
"yetano.dev/yetano/core/solutions/ref"
|
||||
)
|
||||
|
||||
// Entry is one searchable, linkable thing.
|
||||
type Entry struct {
|
||||
Ref string `json:"ref"`
|
||||
Kind string `json:"kind"`
|
||||
Title string `json:"title"`
|
||||
Summary string `json:"summary,omitempty"`
|
||||
Package string `json:"package,omitempty"`
|
||||
Category string `json:"category,omitempty"`
|
||||
Tags []string `json:"tags,omitempty"`
|
||||
Synonyms []string `json:"synonyms,omitempty"`
|
||||
Capabilities []string `json:"capabilities,omitempty"`
|
||||
Status string `json:"status,omitempty"`
|
||||
File string `json:"file,omitempty"` // local file (manuals)
|
||||
Line int `json:"line,omitempty"`
|
||||
Body string `json:"-"` // searchable text
|
||||
Translations catalog.Translations `json:"-"`
|
||||
}
|
||||
|
||||
// Section is a heading of a manual topic.
|
||||
type Section struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Level int `json:"level"`
|
||||
Start int `json:"start"` // first line (1-based)
|
||||
End int `json:"end"` // last line
|
||||
}
|
||||
|
||||
// Topic is one manual document.
|
||||
type Topic struct {
|
||||
Source string `json:"source"`
|
||||
Slug string `json:"slug"`
|
||||
Title string `json:"title"`
|
||||
File string `json:"file"`
|
||||
Sections []Section `json:"sections"`
|
||||
lines []string
|
||||
}
|
||||
|
||||
// Index is the knowledge index of one catalog.
|
||||
type Index struct {
|
||||
Cat *catalog.Catalog
|
||||
Entries []*Entry
|
||||
byRef map[string]*Entry
|
||||
Topics map[string]*Topic // "<source>/<slug>"
|
||||
Backlinks map[string][]string // target ref -> refs that point to it
|
||||
// Missing lists knowledge sources whose files are not checked out.
|
||||
Missing []string
|
||||
}
|
||||
|
||||
// Build indexes the catalog, the Big Picture and the manuals that are
|
||||
// available locally under the catalog's sourcesRoot.
|
||||
func Build(c *catalog.Catalog) *Index {
|
||||
ix := &Index{Cat: c, byRef: map[string]*Entry{}, Topics: map[string]*Topic{}, Backlinks: map[string][]string{}}
|
||||
ix.catalogEntries()
|
||||
ix.bigPictureEntries()
|
||||
ix.manuals()
|
||||
ix.links()
|
||||
sort.Slice(ix.Entries, func(i, j int) bool { return ix.Entries[i].Ref < ix.Entries[j].Ref })
|
||||
return ix
|
||||
}
|
||||
|
||||
func (ix *Index) add(e *Entry) {
|
||||
if _, dup := ix.byRef[e.Ref]; dup {
|
||||
return
|
||||
}
|
||||
ix.byRef[e.Ref] = e
|
||||
ix.Entries = append(ix.Entries, e)
|
||||
}
|
||||
|
||||
// Entry returns the entry for a reference target (no section/version).
|
||||
func (ix *Index) Entry(r string) *Entry { return ix.byRef[r] }
|
||||
|
||||
func (ix *Index) catalogEntries() {
|
||||
c := ix.Cat
|
||||
for _, id := range catalog.SortedIDs(c.Solutions) {
|
||||
ix.add(solutionEntry(c, c.Solutions[id], ref.Solution))
|
||||
}
|
||||
for _, id := range catalog.SortedIDs(c.Tasks) {
|
||||
ix.add(solutionEntry(c, c.Tasks[id], ref.Task))
|
||||
}
|
||||
for _, id := range catalog.SortedIDs(c.Packages) {
|
||||
p := c.Packages[id]
|
||||
e := &Entry{Ref: ref.For(ref.Package, id), Kind: ref.Package, Title: id, Package: id, Summary: p.Meta.Summary}
|
||||
if bp := c.BigPicture; bp != nil {
|
||||
if pr, ok := bp.Products[p.Spec.BigPicture]; ok {
|
||||
e.Summary = firstNonEmpty(e.Summary, pr.Summary)
|
||||
e.Body = strings.Join(pr.Tasks, "\n")
|
||||
}
|
||||
}
|
||||
for _, m := range p.Spec.Modules {
|
||||
e.Capabilities = append(e.Capabilities, m.Provides...)
|
||||
e.Body += "\n" + m.Title + " " + m.Program
|
||||
ix.add(&Entry{Ref: ref.For(ref.Module, id, m.ID), Kind: ref.Module, Title: firstNonEmpty(m.Program, m.ID) + ": " + m.Title,
|
||||
Summary: m.Description, Package: id, Capabilities: m.Provides, Status: string(m.Support)})
|
||||
}
|
||||
if p.Spec.Microsite != "" {
|
||||
ix.add(&Entry{Ref: ref.For(ref.Microsite, id), Kind: ref.Microsite, Title: id + " microsite", Package: id, Summary: e.Summary})
|
||||
}
|
||||
ix.add(e)
|
||||
}
|
||||
for _, id := range catalog.SortedIDs(c.Capabilities) {
|
||||
cp := c.Capabilities[id]
|
||||
ix.add(&Entry{Ref: ref.For(ref.Capability, id), Kind: ref.Capability, Title: cp.Title, Summary: cp.Description, Capabilities: []string{id}, Translations: cp.Translations})
|
||||
}
|
||||
for _, id := range catalog.SortedIDs(c.Profiles) {
|
||||
p := c.Profiles[id]
|
||||
ix.add(&Entry{Ref: ref.For(ref.Profile, p.Meta.Package, p.Meta.ID), Kind: ref.Profile, Title: firstNonEmpty(p.Meta.Title, p.Meta.ID),
|
||||
Summary: p.Meta.Description, Package: p.Meta.Package})
|
||||
}
|
||||
for _, id := range catalog.SortedIDs(c.Examples) {
|
||||
x := c.Examples[id]
|
||||
ix.add(&Entry{Ref: ref.For(ref.Example, x.Spec.Solution, x.Meta.ID), Kind: ref.Example, Title: x.Meta.Title, Summary: x.Meta.Summary,
|
||||
Translations: x.Meta.Translations, Body: x.Spec.Scenario})
|
||||
}
|
||||
}
|
||||
|
||||
func solutionEntry(c *catalog.Catalog, s *catalog.Solution, kind string) *Entry {
|
||||
e := &Entry{Ref: ref.For(kind, s.Meta.ID), Kind: kind, Title: s.Meta.Title, Summary: s.Meta.Summary, Category: s.Meta.Category,
|
||||
Tags: s.Meta.Tags, Synonyms: s.Meta.Synonyms, Status: string(s.Meta.Status), Translations: s.Meta.Translations}
|
||||
if bp := c.BigPicture; bp != nil {
|
||||
if g, ok := bp.Stories[s.Spec.BigPicture]; ok {
|
||||
e.Title = firstNonEmpty(e.Title, g.Title)
|
||||
e.Summary = firstNonEmpty(e.Summary, g.Intent)
|
||||
e.Body = g.Intent + "\n" + g.Outcome
|
||||
}
|
||||
}
|
||||
e.Body += "\n" + strings.Join(s.Spec.Goals, "\n") + "\n" + strings.Join(s.Spec.Scenarios, "\n")
|
||||
for _, q := range s.Spec.Questions {
|
||||
e.Body += "\n" + q.Title
|
||||
for _, o := range q.Options {
|
||||
e.Body += "\n" + o.Title
|
||||
}
|
||||
}
|
||||
e.Capabilities = append(append([]string{}, s.Spec.Capabilities.Required...), s.Spec.Capabilities.Recommended...)
|
||||
for _, p := range s.Spec.Packages {
|
||||
e.Body += "\n" + p.ID
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
func (ix *Index) bigPictureEntries() {
|
||||
bp := ix.Cat.BigPicture
|
||||
if bp == nil {
|
||||
return
|
||||
}
|
||||
for _, id := range catalog.SortedIDs(bp.Topics) {
|
||||
t := bp.Topics[id]
|
||||
ix.add(&Entry{Ref: ref.For(ref.BigPicture, id), Kind: ref.BigPicture, Title: t.Title, Summary: t.Summary})
|
||||
}
|
||||
for _, id := range catalog.SortedIDs(bp.Stories) {
|
||||
s := bp.Stories[id]
|
||||
ix.add(&Entry{Ref: ref.For(ref.BigPicture, id), Kind: ref.BigPicture, Title: s.Title, Summary: s.Intent, Body: s.Outcome + "\n" + s.Explanation})
|
||||
}
|
||||
for _, id := range catalog.SortedIDs(bp.Products) {
|
||||
p := bp.Products[id]
|
||||
ix.add(&Entry{Ref: ref.For(ref.BigPicture, id), Kind: ref.BigPicture, Title: p.Name + " (Big Picture)", Summary: p.Summary})
|
||||
}
|
||||
}
|
||||
|
||||
// ---- manuals ---------------------------------------------------------------
|
||||
|
||||
// SourceDir is where a knowledge source's files are, or "" if unknown.
|
||||
func (ix *Index) SourceDir(k *catalog.KnowledgeSource) string {
|
||||
if ix.Cat.Spec.SourcesRoot == "" || ix.Cat.Root == "" {
|
||||
return ""
|
||||
}
|
||||
root := ix.Cat.Spec.SourcesRoot
|
||||
if !filepath.IsAbs(root) {
|
||||
root = filepath.Join(ix.Cat.Root, root)
|
||||
}
|
||||
return filepath.Join(root, filepath.FromSlash(k.Spec.Repository), filepath.FromSlash(k.Spec.Root))
|
||||
}
|
||||
|
||||
// MaxManualSize bounds one manual file.
|
||||
const MaxManualSize = 2 << 20
|
||||
|
||||
func (ix *Index) manuals() {
|
||||
for _, id := range catalog.SortedIDs(ix.Cat.Sources) {
|
||||
k := ix.Cat.Sources[id]
|
||||
dir := ix.SourceDir(k)
|
||||
if dir == "" {
|
||||
ix.Missing = append(ix.Missing, id)
|
||||
continue
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "README.md")); err != nil {
|
||||
ix.Missing = append(ix.Missing, id)
|
||||
continue
|
||||
}
|
||||
files := []string{"README.md"}
|
||||
docs := filepath.Join(dir, "docs")
|
||||
_ = filepath.WalkDir(docs, func(p string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
rel, _ := filepath.Rel(dir, p)
|
||||
rel = filepath.ToSlash(rel)
|
||||
for _, ex := range k.Spec.Exclude {
|
||||
if rel == ex || strings.HasPrefix(rel, ex+"/") {
|
||||
if d.IsDir() {
|
||||
return fs.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if d.IsDir() && strings.HasPrefix(d.Name(), ".") {
|
||||
return fs.SkipDir
|
||||
}
|
||||
if !d.IsDir() && strings.HasSuffix(p, ".md") {
|
||||
files = append(files, rel)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
sort.Strings(files)
|
||||
for _, f := range files {
|
||||
b, err := os.ReadFile(filepath.Join(dir, filepath.FromSlash(f)))
|
||||
if err != nil || len(b) > MaxManualSize {
|
||||
continue
|
||||
}
|
||||
t := parseTopic(id, TopicSlug(f), filepath.Join(dir, filepath.FromSlash(f)), b)
|
||||
ix.Topics[id+"/"+t.Slug] = t
|
||||
caps := []string{}
|
||||
for _, st := range k.Spec.Topics {
|
||||
if st.Slug == t.Slug {
|
||||
caps = st.Capabilities
|
||||
}
|
||||
}
|
||||
r := ref.For(ref.Manual, id, t.Slug)
|
||||
ix.add(&Entry{Ref: r, Kind: ref.Manual, Title: t.Title, Summary: firstLine(t), Package: k.Spec.Package,
|
||||
Capabilities: caps, File: t.File, Body: strings.Join(t.lines, "\n")})
|
||||
for _, s := range t.Sections[1:] {
|
||||
ix.add(&Entry{Ref: r + "#" + s.ID, Kind: "section", Title: t.Title + " › " + s.Title, Package: k.Spec.Package,
|
||||
File: t.File, Line: s.Start, Body: strings.Join(t.lines[s.Start-1:s.End], "\n")})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TopicSlug follows core/manual: README.md -> readme, docs/x.md -> x,
|
||||
// docs/x/README.md -> x.
|
||||
func TopicSlug(rel string) string {
|
||||
if rel == "README.md" {
|
||||
return "readme"
|
||||
}
|
||||
s := strings.TrimSuffix(strings.TrimPrefix(rel, "docs/"), ".md")
|
||||
s = strings.TrimSuffix(s, "/README")
|
||||
return strings.ReplaceAll(s, "/", ".")
|
||||
}
|
||||
|
||||
func parseTopic(source, slug, file string, b []byte) *Topic {
|
||||
t := &Topic{Source: source, Slug: slug, File: file}
|
||||
sc := bufio.NewScanner(bytes.NewReader(b))
|
||||
sc.Buffer(make([]byte, 64<<10), MaxManualSize)
|
||||
fence := false
|
||||
seen := map[string]int{}
|
||||
for sc.Scan() {
|
||||
line := sc.Text()
|
||||
t.lines = append(t.lines, line)
|
||||
trimmed := strings.TrimSpace(line)
|
||||
if strings.HasPrefix(trimmed, "```") || strings.HasPrefix(trimmed, "~~~") {
|
||||
fence = !fence
|
||||
continue
|
||||
}
|
||||
if fence || !strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
level := len(line) - len(strings.TrimLeft(line, "#"))
|
||||
title := strings.TrimSpace(line[level:])
|
||||
if level > 6 || title == "" {
|
||||
continue
|
||||
}
|
||||
id := ref.Slug(title)
|
||||
if n := seen[id]; n > 0 {
|
||||
id = fmt.Sprintf("%s-%d", id, n)
|
||||
}
|
||||
seen[ref.Slug(title)]++
|
||||
if len(t.Sections) > 0 {
|
||||
t.Sections[len(t.Sections)-1].End = len(t.lines) - 1
|
||||
}
|
||||
t.Sections = append(t.Sections, Section{ID: id, Title: title, Level: level, Start: len(t.lines)})
|
||||
if t.Title == "" && level == 1 {
|
||||
t.Title = title
|
||||
}
|
||||
}
|
||||
if len(t.Sections) == 0 {
|
||||
t.Sections = []Section{{ID: "", Title: slug, Level: 1, Start: 1}}
|
||||
}
|
||||
t.Sections[len(t.Sections)-1].End = len(t.lines)
|
||||
if t.Title == "" {
|
||||
t.Title = slug
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func firstLine(t *Topic) string {
|
||||
for _, l := range t.lines {
|
||||
l = strings.TrimSpace(l)
|
||||
if l != "" && !strings.HasPrefix(l, "#") && !strings.HasPrefix(l, "```") && !strings.HasPrefix(l, "|") && !strings.HasPrefix(l, "<") {
|
||||
if len(l) > 200 {
|
||||
l = l[:200] + "…"
|
||||
}
|
||||
return l
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func firstNonEmpty(a ...string) string {
|
||||
for _, s := range a {
|
||||
if s != "" {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// links collects every reference the catalog makes, for backlinks (REL-01/02).
|
||||
func (ix *Index) links() {
|
||||
c := ix.Cat
|
||||
link := func(from string, to string) {
|
||||
r, err := ref.Parse(to)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
t := r.Target()
|
||||
for _, x := range ix.Backlinks[t] {
|
||||
if x == from {
|
||||
return
|
||||
}
|
||||
}
|
||||
ix.Backlinks[t] = append(ix.Backlinks[t], from)
|
||||
}
|
||||
for _, m := range []map[string]*catalog.Solution{c.Solutions, c.Tasks} {
|
||||
for _, id := range catalog.SortedIDs(m) {
|
||||
s := m[id]
|
||||
kind := ref.Solution
|
||||
if s.IsTask() {
|
||||
kind = ref.Task
|
||||
}
|
||||
from := ref.For(kind, id)
|
||||
for _, r := range RefsOf(s) {
|
||||
link(from, r)
|
||||
}
|
||||
for _, p := range s.Spec.Packages {
|
||||
link(from, ref.For(ref.Package, p.ID))
|
||||
}
|
||||
for _, cp := range append(append([]string{}, s.Spec.Capabilities.Required...), s.Spec.Capabilities.Recommended...) {
|
||||
link(from, ref.For(ref.Capability, cp))
|
||||
}
|
||||
if s.Spec.BigPicture != "" {
|
||||
link(from, ref.For(ref.BigPicture, s.Spec.BigPicture))
|
||||
}
|
||||
for _, r := range s.Spec.Related {
|
||||
k := ref.Solution
|
||||
if c.Tasks[r] != nil {
|
||||
k = ref.Task
|
||||
}
|
||||
link(from, ref.For(k, r))
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, id := range catalog.SortedIDs(c.Packages) {
|
||||
p := c.Packages[id]
|
||||
from := ref.For(ref.Package, id)
|
||||
for _, m := range p.Spec.Modules {
|
||||
for _, cp := range m.Provides {
|
||||
link(ref.For(ref.Module, id, m.ID), ref.For(ref.Capability, cp))
|
||||
link(from, ref.For(ref.Capability, cp))
|
||||
}
|
||||
}
|
||||
if p.Spec.BigPicture != "" {
|
||||
link(from, ref.For(ref.BigPicture, p.Spec.BigPicture))
|
||||
}
|
||||
}
|
||||
for _, id := range catalog.SortedIDs(c.Profiles) {
|
||||
p := c.Profiles[id]
|
||||
for _, r := range p.Spec.References {
|
||||
link(ref.For(ref.Profile, p.Meta.Package, p.Meta.ID), r)
|
||||
}
|
||||
link(ref.For(ref.Profile, p.Meta.Package, p.Meta.ID), ref.For(ref.Package, p.Meta.Package))
|
||||
}
|
||||
for _, id := range catalog.SortedIDs(c.Examples) {
|
||||
x := c.Examples[id]
|
||||
k := ref.Solution
|
||||
if c.Tasks[x.Spec.Solution] != nil {
|
||||
k = ref.Task
|
||||
}
|
||||
link(ref.For(ref.Example, x.Spec.Solution, x.Meta.ID), ref.For(k, x.Spec.Solution))
|
||||
}
|
||||
for k := range ix.Backlinks {
|
||||
sort.Strings(ix.Backlinks[k])
|
||||
}
|
||||
}
|
||||
|
||||
// RefsOf lists every yetano: reference a solution or task contains.
|
||||
func RefsOf(s *catalog.Solution) []string {
|
||||
var out []string
|
||||
out = append(out, s.Spec.References.All()...)
|
||||
for _, q := range s.Spec.Questions {
|
||||
out = append(out, q.References...)
|
||||
for _, o := range q.Options {
|
||||
out = append(out, o.References...)
|
||||
}
|
||||
}
|
||||
for _, st := range s.Spec.Steps {
|
||||
out = append(out, st.References...)
|
||||
}
|
||||
for _, c := range s.Spec.Requirements {
|
||||
out = append(out, c.References...)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Backlinks of a reference (its target), sorted.
|
||||
func (ix *Index) BacklinksOf(r string) []string {
|
||||
p, err := ref.Parse(r)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return ix.Backlinks[p.Target()]
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
package knowledge
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"yetano.dev/yetano/core/solutions/catalog"
|
||||
"yetano.dev/yetano/core/solutions/ref"
|
||||
)
|
||||
|
||||
// Lint checks what catalog.Validate cannot: every yetano: reference
|
||||
// resolves (manual topics and sections included), and catalog-wide
|
||||
// advice (QS-03). It returns catalog.Validate's issues plus its own.
|
||||
func Lint(ix *Index) []catalog.Issue {
|
||||
c := ix.Cat
|
||||
issues := c.Validate()
|
||||
check := func(e *catalog.Entity, owner, r string) {
|
||||
res, err := ix.Resolve(r)
|
||||
switch {
|
||||
case err != nil:
|
||||
issues = append(issues, catalog.Issue{Severity: "error", File: e.File, Line: e.Line, ID: owner, Code: "reference", Message: err.Error()})
|
||||
case !res.Exists:
|
||||
issues = append(issues, catalog.Issue{Severity: "error", File: e.File, Line: e.Line, ID: owner, Code: "reference", Message: fmt.Sprintf("%s does not resolve", r)})
|
||||
case !res.SectionExists:
|
||||
issues = append(issues, catalog.Issue{Severity: "error", File: e.File, Line: e.Line, ID: owner, Code: "reference-section", Message: fmt.Sprintf("%s: the manual has no such section (renamed heading?)", r)})
|
||||
}
|
||||
}
|
||||
for _, m := range []map[string]*catalog.Solution{c.Solutions, c.Tasks} {
|
||||
for _, id := range catalog.SortedIDs(m) {
|
||||
s := m[id]
|
||||
for _, r := range RefsOf(s) {
|
||||
check(&s.Entity, id, r)
|
||||
}
|
||||
if s.Kind == catalog.KindSolution && len(c.Examples) > 0 {
|
||||
has := false
|
||||
for _, x := range c.Examples {
|
||||
has = has || x.Spec.Solution == id
|
||||
}
|
||||
if !has {
|
||||
issues = append(issues, catalog.Issue{Severity: "warning", File: s.File, ID: id, Code: "no-example", Message: "a solution should have at least one example (EX-01)"})
|
||||
}
|
||||
}
|
||||
for _, q := range s.Spec.Questions {
|
||||
if q.Type == "select" && q.Decide == "" && q.Level != catalog.LevelAdvanced && len(q.Options) > 2 {
|
||||
issues = append(issues, catalog.Issue{Severity: "warning", File: s.File, ID: id + "." + q.ID, Code: "no-decide", Message: "a choice between several options should explain it (decide, HELP-01)"})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, id := range catalog.SortedIDs(c.Profiles) {
|
||||
p := c.Profiles[id]
|
||||
for _, r := range p.Spec.References {
|
||||
check(&p.Entity, id, r)
|
||||
}
|
||||
}
|
||||
for _, id := range ix.Missing {
|
||||
issues = append(issues, catalog.Issue{Severity: "warning", ID: id, Code: "source-missing", Message: "documents not checked out under sourcesRoot; its references are not checked"})
|
||||
}
|
||||
return issues
|
||||
}
|
||||
|
||||
// Coverage is the knowledge coverage report (COV-01), computed from data.
|
||||
type Coverage struct {
|
||||
Packages int `json:"packages"`
|
||||
Metrics []Metric `json:"metrics"`
|
||||
GoalsWithout []string `json:"goalsWithoutSolution"`
|
||||
ProgramsWithout []string `json:"bigPictureProgramsWithoutPackage"`
|
||||
ByPackage map[string]PackageCoverage `json:"byPackage"`
|
||||
}
|
||||
|
||||
// Metric is one percentage.
|
||||
type Metric struct {
|
||||
Name string `json:"name"`
|
||||
Have int `json:"have"`
|
||||
Total int `json:"total"`
|
||||
Pct float64 `json:"percent"`
|
||||
}
|
||||
|
||||
// PackageCoverage is what exists for one package.
|
||||
type PackageCoverage struct {
|
||||
Manual bool `json:"manual"`
|
||||
Microsite bool `json:"microsite"`
|
||||
Provides int `json:"capabilities"`
|
||||
Profiles int `json:"profiles"`
|
||||
Solutions int `json:"solutions"`
|
||||
Tasks int `json:"tasks"`
|
||||
}
|
||||
|
||||
// ComputeCoverage builds the report.
|
||||
func ComputeCoverage(ix *Index) Coverage {
|
||||
c := ix.Cat
|
||||
cov := Coverage{ByPackage: map[string]PackageCoverage{}}
|
||||
for _, id := range catalog.SortedIDs(c.Packages) {
|
||||
p := c.Packages[id]
|
||||
pc := PackageCoverage{Microsite: p.Spec.Microsite != ""}
|
||||
for _, m := range p.Spec.Modules {
|
||||
pc.Provides += len(m.Provides)
|
||||
}
|
||||
for _, k := range c.Sources {
|
||||
pc.Manual = pc.Manual || k.Spec.Package == id
|
||||
}
|
||||
for _, pr := range c.Profiles {
|
||||
if pr.Meta.Package == id {
|
||||
pc.Profiles++
|
||||
}
|
||||
}
|
||||
for _, s := range c.Solutions {
|
||||
for _, pu := range s.Spec.Packages {
|
||||
if pu.ID == id {
|
||||
pc.Solutions++
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, s := range c.Tasks {
|
||||
for _, pu := range s.Spec.Packages {
|
||||
if pu.ID == id {
|
||||
pc.Tasks++
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
cov.ByPackage[id] = pc
|
||||
}
|
||||
cov.Packages = len(c.Packages)
|
||||
count := func(name string, f func(PackageCoverage) bool) {
|
||||
m := Metric{Name: name, Total: len(cov.ByPackage)}
|
||||
for _, pc := range cov.ByPackage {
|
||||
if f(pc) {
|
||||
m.Have++
|
||||
}
|
||||
}
|
||||
cov.Metrics = append(cov.Metrics, pct(m))
|
||||
}
|
||||
count("Packages with a manual source", func(p PackageCoverage) bool { return p.Manual })
|
||||
count("Packages with a microsite", func(p PackageCoverage) bool { return p.Microsite })
|
||||
count("Packages with capabilities", func(p PackageCoverage) bool { return p.Provides > 0 })
|
||||
count("Packages with profiles", func(p PackageCoverage) bool { return p.Profiles > 0 })
|
||||
count("Packages used by a solution", func(p PackageCoverage) bool { return p.Solutions > 0 })
|
||||
count("Packages with a task", func(p PackageCoverage) bool { return p.Tasks > 0 })
|
||||
// references
|
||||
refs, ok := 0, 0
|
||||
for _, m := range []map[string]*catalog.Solution{c.Solutions, c.Tasks} {
|
||||
for _, s := range m {
|
||||
for _, r := range RefsOf(s) {
|
||||
refs++
|
||||
if res, err := ix.Resolve(r); err == nil && res.Exists && res.SectionExists {
|
||||
ok++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
cov.Metrics = append(cov.Metrics, pct(Metric{Name: "Valid references", Have: ok, Total: refs}))
|
||||
tested := 0
|
||||
for _, x := range c.Examples {
|
||||
if x.Spec.Tested {
|
||||
tested++
|
||||
}
|
||||
}
|
||||
cov.Metrics = append(cov.Metrics, pct(Metric{Name: "Examples marked tested", Have: tested, Total: len(c.Examples)}))
|
||||
// translations of solution titles
|
||||
tr, total := 0, 0
|
||||
for _, loc := range c.Spec.Locales {
|
||||
if loc == "en" {
|
||||
continue
|
||||
}
|
||||
for _, m := range []map[string]*catalog.Solution{c.Solutions, c.Tasks} {
|
||||
for _, s := range m {
|
||||
total++
|
||||
if s.Meta.Translations[loc]["title"] != "" {
|
||||
tr++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
cov.Metrics = append(cov.Metrics, pct(Metric{Name: "Solution titles translated", Have: tr, Total: total}))
|
||||
if bp := c.BigPicture; bp != nil {
|
||||
used := map[string]bool{}
|
||||
for _, s := range c.Solutions {
|
||||
used[s.Spec.BigPicture] = true
|
||||
}
|
||||
goals := 0
|
||||
for _, id := range catalog.SortedIDs(bp.Stories) {
|
||||
if used[id] {
|
||||
goals++
|
||||
} else {
|
||||
cov.GoalsWithout = append(cov.GoalsWithout, id)
|
||||
}
|
||||
}
|
||||
cov.Metrics = append(cov.Metrics, pct(Metric{Name: "Big Picture goals with a solution", Have: goals, Total: len(bp.Stories)}))
|
||||
pk := map[string]bool{}
|
||||
for _, p := range c.Packages {
|
||||
pk[p.Spec.BigPicture] = true
|
||||
}
|
||||
for _, id := range catalog.SortedIDs(bp.Products) {
|
||||
if !pk[id] {
|
||||
cov.ProgramsWithout = append(cov.ProgramsWithout, strings.TrimPrefix(id, "product."))
|
||||
}
|
||||
}
|
||||
cov.Metrics = append(cov.Metrics, pct(Metric{Name: "Big Picture programs in the catalog", Have: len(bp.Products) - len(cov.ProgramsWithout), Total: len(bp.Products)}))
|
||||
}
|
||||
sort.SliceStable(cov.Metrics, func(i, j int) bool { return false })
|
||||
return cov
|
||||
}
|
||||
|
||||
func pct(m Metric) Metric {
|
||||
if m.Total > 0 {
|
||||
m.Pct = float64(int(float64(m.Have)*1000/float64(m.Total)+0.5)) / 10
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
var _ = ref.Solution
|
||||
@@ -0,0 +1,193 @@
|
||||
package knowledge
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"yetano.dev/yetano/core/solutions/ref"
|
||||
)
|
||||
|
||||
// Resolution says what a reference points at and how each client opens
|
||||
// it (REF-02). Exists is false for targets the index does not know.
|
||||
type Resolution struct {
|
||||
Ref string `json:"ref"`
|
||||
Kind string `json:"kind"`
|
||||
Title string `json:"title,omitempty"`
|
||||
Summary string `json:"summary,omitempty"`
|
||||
Exists bool `json:"exists"`
|
||||
// SectionExists is false when the ref names a section that is not there.
|
||||
SectionExists bool `json:"sectionExists"`
|
||||
Web string `json:"web,omitempty"` // URL on yetano.app (only for pages that exist)
|
||||
CLI string `json:"cli,omitempty"` // command that shows it in a terminal
|
||||
File string `json:"file,omitempty"` // local Markdown file (manuals)
|
||||
Line int `json:"line,omitempty"`
|
||||
View string `json:"view"` // how a UI shows it: wizard | reader | graph | package | code | list
|
||||
Note string `json:"note,omitempty"`
|
||||
}
|
||||
|
||||
// Resolve resolves a reference against the index.
|
||||
func (ix *Index) Resolve(s string) (Resolution, error) {
|
||||
r, err := ref.Parse(s)
|
||||
if err != nil {
|
||||
return Resolution{}, err
|
||||
}
|
||||
res := Resolution{Ref: s, Kind: r.Kind, SectionExists: r.Section == ""}
|
||||
web := strings.TrimRight(ix.Cat.Spec.WebBase, "/") + "/"
|
||||
if e := ix.byRef[r.Target()]; e != nil {
|
||||
res.Exists, res.Title, res.Summary = true, e.Title, e.Summary
|
||||
}
|
||||
switch r.Kind {
|
||||
case ref.Solution, ref.Task:
|
||||
res.View = "wizard"
|
||||
cmd := "solution"
|
||||
if r.Kind == ref.Task {
|
||||
cmd = "task"
|
||||
}
|
||||
res.CLI = fmt.Sprintf("yet %s show %s", cmd, r.Path[0])
|
||||
case ref.Package:
|
||||
res.View = "package"
|
||||
res.CLI = "yet solution package " + r.Path[0]
|
||||
if p := ix.Cat.Packages[r.Path[0]]; p != nil && p.Spec.Microsite != "" {
|
||||
res.Web = web + p.Spec.Microsite
|
||||
}
|
||||
case ref.Module:
|
||||
res.View = "package"
|
||||
res.CLI = "yet solution package " + r.Path[0]
|
||||
case ref.Microsite:
|
||||
res.View = "package"
|
||||
if p := ix.Cat.Packages[r.Path[0]]; p != nil && p.Spec.Microsite != "" {
|
||||
res.Web = web + p.Spec.Microsite
|
||||
res.Exists = true
|
||||
}
|
||||
case ref.Capability:
|
||||
res.View = "list"
|
||||
res.CLI = "yet knowledge show " + s
|
||||
case ref.Profile, ref.Example:
|
||||
res.View = "code"
|
||||
res.CLI = "yet knowledge show " + s
|
||||
case ref.BigPicture:
|
||||
res.View = "graph"
|
||||
if bp := ix.Cat.BigPicture; bp != nil && bp.Has(r.Path[0]) {
|
||||
res.Exists = true
|
||||
res.Web = web + bp.WebPath + "#" + bp.Fragment(r.Path[0])
|
||||
}
|
||||
case ref.Manual:
|
||||
res.View = "reader"
|
||||
source := r.Path[0]
|
||||
slug := strings.Join(r.Path[1:], ".")
|
||||
k := ix.Cat.Sources[source]
|
||||
if k == nil {
|
||||
res.Note = "unknown knowledge source " + source
|
||||
break
|
||||
}
|
||||
realSlug := strings.ReplaceAll(slug, ".", "/")
|
||||
if k.Spec.Program != "" {
|
||||
res.CLI = fmt.Sprintf("%s manual %s", k.Spec.Program, realSlug)
|
||||
} else {
|
||||
res.CLI = "yet knowledge show " + s
|
||||
}
|
||||
if k.Spec.Web != "" {
|
||||
res.Web = k.Spec.Web
|
||||
}
|
||||
t := ix.Topics[source+"/"+slug]
|
||||
if t == nil {
|
||||
if ix.missing(source) {
|
||||
res.Note = "the documents of " + source + " are not available locally; the reference is not checked"
|
||||
res.Exists = true // cannot tell; do not report as broken
|
||||
}
|
||||
break
|
||||
}
|
||||
res.Exists, res.Title, res.File = true, t.Title, t.File
|
||||
if r.Section != "" {
|
||||
for _, sec := range t.Sections {
|
||||
if sec.ID == r.Section {
|
||||
res.SectionExists, res.Title, res.Line = true, t.Title+" › "+sec.Title, sec.Start
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if res.Web != "" {
|
||||
if _, err := url.Parse(res.Web); err != nil {
|
||||
res.Web = ""
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (ix *Index) missing(source string) bool {
|
||||
for _, m := range ix.Missing {
|
||||
if m == source {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Read returns the Markdown of a manual reference: the whole topic, or
|
||||
// just the section (with its subsections).
|
||||
func (ix *Index) Read(s string) (title, markdown string, err error) {
|
||||
r, err := ref.Parse(s)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
if r.Kind != ref.Manual {
|
||||
return "", "", fmt.Errorf("%s is not a manual reference", s)
|
||||
}
|
||||
t := ix.Topics[r.Path[0]+"/"+strings.Join(r.Path[1:], ".")]
|
||||
if t == nil {
|
||||
return "", "", fmt.Errorf("manual %s is not available locally", s)
|
||||
}
|
||||
if r.Section == "" {
|
||||
return t.Title, strings.Join(t.lines, "\n") + "\n", nil
|
||||
}
|
||||
for i, sec := range t.Sections {
|
||||
if sec.ID != r.Section {
|
||||
continue
|
||||
}
|
||||
end := len(t.lines)
|
||||
for _, next := range t.Sections[i+1:] {
|
||||
if next.Level <= sec.Level {
|
||||
end = next.Start - 1
|
||||
break
|
||||
}
|
||||
}
|
||||
return sec.Title, strings.Join(t.lines[sec.Start-1:end], "\n") + "\n", nil
|
||||
}
|
||||
return "", "", fmt.Errorf("%s: no section %q", s, r.Section)
|
||||
}
|
||||
|
||||
// Excerpt implements engine.ManualReader.
|
||||
func (ix *Index) Excerpt(s string, maxLines int) (string, string, bool) {
|
||||
title, md, err := ix.Read(s)
|
||||
if err != nil {
|
||||
return "", "", false
|
||||
}
|
||||
lines := strings.Split(strings.TrimRight(md, "\n"), "\n")
|
||||
if len(lines) > 0 && strings.HasPrefix(lines[0], "#") {
|
||||
lines = lines[1:]
|
||||
}
|
||||
for len(lines) > 0 && strings.TrimSpace(lines[0]) == "" {
|
||||
lines = lines[1:]
|
||||
}
|
||||
if len(lines) > maxLines {
|
||||
lines = append(lines[:maxLines], "…")
|
||||
}
|
||||
return title, strings.Join(lines, "\n"), true
|
||||
}
|
||||
|
||||
// Link implements engine.ManualReader: a web URL, else the CLI command.
|
||||
func (ix *Index) Link(s string) string {
|
||||
res, err := ix.Resolve(s)
|
||||
if err != nil {
|
||||
return "`" + s + "`"
|
||||
}
|
||||
label := firstNonEmpty(res.Title, s)
|
||||
switch {
|
||||
case res.Web != "" && res.Kind != ref.Manual:
|
||||
return fmt.Sprintf("[%s](%s)", label, res.Web)
|
||||
case res.CLI != "":
|
||||
return fmt.Sprintf("%s (`%s`)", label, res.CLI)
|
||||
}
|
||||
return "`" + s + "`"
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
package knowledge
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"yetano.dev/yetano/core/solutions/ref"
|
||||
)
|
||||
|
||||
// Hit is one search result.
|
||||
type Hit struct {
|
||||
Ref string `json:"ref"`
|
||||
Kind string `json:"kind"`
|
||||
Title string `json:"title"`
|
||||
Summary string `json:"summary,omitempty"`
|
||||
Score float64 `json:"score"`
|
||||
Status string `json:"status,omitempty"`
|
||||
}
|
||||
|
||||
// Group collects the hits of one kind (SEARCH-01).
|
||||
type Group struct {
|
||||
Kind string `json:"kind"`
|
||||
Title string `json:"title"`
|
||||
Hits []Hit `json:"hits"`
|
||||
}
|
||||
|
||||
// SearchOptions narrow a search.
|
||||
type SearchOptions struct {
|
||||
Kinds []string // only these kinds (solution, task, package, manual, ...)
|
||||
Limit int // per group; 0 = 10
|
||||
Locale string
|
||||
Package string
|
||||
}
|
||||
|
||||
// Group order: use cases first (SEARCH-04).
|
||||
var groupOrder = []struct{ kind, title string }{
|
||||
{ref.Solution, "Solutions"}, {ref.Task, "Tasks"}, {ref.Example, "Examples"},
|
||||
{ref.Package, "Packages"}, {ref.Module, "Modules"}, {ref.Capability, "Capabilities"},
|
||||
{ref.Manual, "Manuals"}, {"section", "Manual sections"}, {ref.Microsite, "Microsites"},
|
||||
{ref.BigPicture, "Big Picture"}, {ref.Profile, "Profiles"},
|
||||
}
|
||||
|
||||
var stop = map[string]bool{"i": true, "a": true, "an": true, "the": true, "to": true, "want": true, "my": true, "own": true,
|
||||
"with": true, "for": true, "of": true, "and": true, "run": false, "ich": true, "will": true, "einen": true, "ein": true, "mit": true, "möchte": true}
|
||||
|
||||
// Tokens splits text into lower-case search words.
|
||||
func Tokens(s string) []string {
|
||||
f := strings.FieldsFunc(strings.ToLower(s), func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '.' })
|
||||
var out []string
|
||||
for _, w := range f {
|
||||
w = strings.Trim(w, ".")
|
||||
if w != "" && !stop[w] {
|
||||
out = append(out, w)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Search runs a deterministic full-text search over the index: titles,
|
||||
// IDs, tags and synonyms weigh most, then summaries, then body text.
|
||||
// No embeddings and no network (SEARCH-05).
|
||||
func (ix *Index) Search(q string, opt SearchOptions) []Group {
|
||||
words := Tokens(q)
|
||||
if len(words) == 0 {
|
||||
return nil
|
||||
}
|
||||
limit := opt.Limit
|
||||
if limit <= 0 {
|
||||
limit = 10
|
||||
}
|
||||
allowed := map[string]bool{}
|
||||
for _, k := range opt.Kinds {
|
||||
allowed[k] = true
|
||||
}
|
||||
byKind := map[string][]Hit{}
|
||||
for _, e := range ix.Entries {
|
||||
if len(allowed) > 0 && !allowed[e.Kind] {
|
||||
continue
|
||||
}
|
||||
if opt.Package != "" && e.Package != opt.Package {
|
||||
continue
|
||||
}
|
||||
title := e.Title
|
||||
summary := e.Summary
|
||||
if opt.Locale != "" && e.Translations != nil {
|
||||
if m, ok := e.Translations[opt.Locale]; ok {
|
||||
title = firstNonEmpty(m["title"], title)
|
||||
summary = firstNonEmpty(m["summary"], summary)
|
||||
}
|
||||
}
|
||||
fields := []struct {
|
||||
text string
|
||||
weight float64
|
||||
}{
|
||||
{title + " " + e.Title, 6}, {e.Ref, 4}, {strings.Join(e.Tags, " ") + " " + strings.Join(e.Synonyms, " "), 5},
|
||||
{strings.Join(e.Capabilities, " ") + " " + e.Package, 3}, {summary + " " + e.Summary, 2}, {e.Body, 0.5},
|
||||
}
|
||||
score := 0.0
|
||||
matched := 0
|
||||
phrase := strings.ToLower(strings.Join(words, " "))
|
||||
for _, w := range words {
|
||||
best := 0.0
|
||||
for _, f := range fields {
|
||||
lt := strings.ToLower(f.text)
|
||||
if strings.Contains(lt, w) {
|
||||
s := f.weight
|
||||
if hasWord(lt, w) {
|
||||
s *= 1.5
|
||||
}
|
||||
if s > best {
|
||||
best = s
|
||||
}
|
||||
}
|
||||
}
|
||||
if best > 0 {
|
||||
matched++
|
||||
}
|
||||
score += best
|
||||
}
|
||||
if matched == 0 {
|
||||
continue
|
||||
}
|
||||
score *= float64(matched) / float64(len(words))
|
||||
if strings.Contains(strings.ToLower(title+" "+strings.Join(e.Synonyms, " ")), phrase) && len(words) > 1 {
|
||||
score += 5
|
||||
}
|
||||
switch e.Kind {
|
||||
case ref.Solution, ref.Task:
|
||||
score *= 1.5
|
||||
case "section":
|
||||
score *= 0.6
|
||||
}
|
||||
byKind[e.Kind] = append(byKind[e.Kind], Hit{Ref: e.Ref, Kind: e.Kind, Title: title, Summary: summary, Score: round(score), Status: e.Status})
|
||||
}
|
||||
var out []Group
|
||||
for _, g := range groupOrder {
|
||||
hits := byKind[g.kind]
|
||||
if len(hits) == 0 {
|
||||
continue
|
||||
}
|
||||
sort.SliceStable(hits, func(i, j int) bool {
|
||||
if hits[i].Score != hits[j].Score {
|
||||
return hits[i].Score > hits[j].Score
|
||||
}
|
||||
return hits[i].Ref < hits[j].Ref
|
||||
})
|
||||
if len(hits) > limit {
|
||||
hits = hits[:limit]
|
||||
}
|
||||
out = append(out, Group{Kind: g.kind, Title: g.title, Hits: hits})
|
||||
}
|
||||
// groups with the best hit first, but solutions and tasks keep the lead
|
||||
sort.SliceStable(out, func(i, j int) bool {
|
||||
pi, pj := lead(out[i].Kind), lead(out[j].Kind)
|
||||
if pi != pj {
|
||||
return pi > pj
|
||||
}
|
||||
return out[i].Hits[0].Score > out[j].Hits[0].Score
|
||||
})
|
||||
return out
|
||||
}
|
||||
|
||||
func lead(kind string) int {
|
||||
if kind == ref.Solution || kind == ref.Task {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func hasWord(text, w string) bool {
|
||||
for _, t := range Tokens(text) {
|
||||
if t == w {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func round(f float64) float64 { return float64(int(f*100+0.5)) / 100 }
|
||||
Reference in new issue
Block a user