Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.
JavaScript agent-orchestrationagentic-workflowai-agentsai-codingclaude-codecode-reviewcodebuddycodexcoding-agentscursordeveloper-toolshuman-on-the-loopllmmcpmulti-agentsdlcself-hostedsopworkflow-automationworkflow-engine
refactor(knowledge): split ParseDistilled front matter parsing (#407)
fx-kurumic8f54a921e7e
diff --git a/internal/knowledge/parse_distilled_test.go b/internal/knowledge/parse_distilled_test.gonew file mode 100644index 00000000..e3d79c2b--- /dev/null+++ b/internal/knowledge/parse_distilled_test.go@@ -0,0 +1,25 @@+package knowledge++import "testing"++func TestParseDistilledEdgeCases(t *testing.T) {+ cases := []struct {+ name string+ in string+ want Distilled+ }{+ {"empty", "", Distilled{}},+ {"no front matter no heading", "plain body", Distilled{Body: "plain body"}},+ {"crlf front matter", "---\r\ntitle: T\r\n---\r\nbody", Distilled{Title: "T", Body: "body"}},+ {"unclosed front matter falls back to heading", "---\ntitle: T\n# H\n", Distilled{Title: "H", Body: "---\ntitle: T\n# H\n"}},+ {"empty title uses heading", "---\nsource: s\n---\n\n\n# 标题\n正文", Distilled{Title: "标题", Source: "s", Body: "# 标题\n正文"}},+ {"unknown keys and junk lines ignored", "---\nTitle: 'Q'\nauthor: x\nnot a key\nPATH: a/b.md\n---\nbody", Distilled{Title: "Q", Path: "a/b.md", Body: "body"}},+ {"later key wins", "---\ntitle: a\ntitle: b\n---\n", Distilled{Title: "b"}},+ {"heading with extra hashes", "x\n ### 深层标题 \n", Distilled{Title: "深层标题", Body: "x\n ### 深层标题 \n"}},+ }+ for _, tc := range cases {+ if got := ParseDistilled(tc.in); got != tc.want {+ t.Errorf("%s: got %#v want %#v", tc.name, got, tc.want)+ }+ }+}diff --git a/internal/knowledge/rules.go b/internal/knowledge/rules.goindex 7ba84a27..a75cd3c8 100644--- a/internal/knowledge/rules.go+++ b/internal/knowledge/rules.go@@ -84,40 +84,62 @@ var frontMatterLine = regexp.MustCompile(`^([A-Za-z_]+)\s*:\s*(.*)$`)func ParseDistilled(content string) Distilled {text := strings.ReplaceAll(content, "\r\n", "\n")out := Distilled{Body: text}- if strings.HasPrefix(text, "---\n") {- if end := strings.Index(text[4:], "\n---"); end >= 0 {- header := text[4 : 4+end]- rest := text[4+end+4:]- out.Body = strings.TrimLeft(strings.TrimPrefix(rest, "\n"), "\n")- for _, line := range strings.Split(header, "\n") {- m := frontMatterLine.FindStringSubmatch(strings.TrimSpace(line))- if m == nil {- continue- }- value := strings.Trim(strings.TrimSpace(m[2]), `"'`)- switch strings.ToLower(m[1]) {- case "title":- out.Title = value- case "source":- out.Source = value- case "path":- out.Path = value- }- }- }+ if header, body, ok := splitDistilledFrontMatter(text); ok {+ out.Body = body+ applyDistilledHeader(&out, header)}if out.Title == "" {- for _, line := range strings.Split(out.Body, "\n") {- line = strings.TrimSpace(line)- if strings.HasPrefix(line, "#") {- out.Title = strings.TrimSpace(strings.TrimLeft(line, "#"))- break- }- }+ out.Title = firstMarkdownHeading(out.Body)}return out}+// splitDistilledFrontMatter separates a leading "---" front matter block from+// the body. ok is false when text has no closed front matter.+func splitDistilledFrontMatter(text string) (header, body string, ok bool) {+ if !strings.HasPrefix(text, "---\n") {+ return "", text, false+ }+ end := strings.Index(text[4:], "\n---")+ if end < 0 {+ return "", text, false+ }+ rest := text[4+end+4:]+ return text[4 : 4+end], strings.TrimLeft(strings.TrimPrefix(rest, "\n"), "\n"), true+}++// applyDistilledHeader copies the recognised title/source/path keys of a+// front matter header into out; other lines are ignored.+func applyDistilledHeader(out *Distilled, header string) {+ for _, line := range strings.Split(header, "\n") {+ m := frontMatterLine.FindStringSubmatch(strings.TrimSpace(line))+ if m == nil {+ continue+ }+ value := strings.Trim(strings.TrimSpace(m[2]), `"'`)+ switch strings.ToLower(m[1]) {+ case "title":+ out.Title = value+ case "source":+ out.Source = value+ case "path":+ out.Path = value+ }+ }+}++// firstMarkdownHeading returns the text of the first "#" heading in body, or+// "" when there is none.+func firstMarkdownHeading(body string) string {+ for _, line := range strings.Split(body, "\n") {+ line = strings.TrimSpace(line)+ if strings.HasPrefix(line, "#") {+ return strings.TrimSpace(strings.TrimLeft(line, "#"))+ }+ }+ return ""+}+var unsafePathChars = regexp.MustCompile(`[^\p{L}\p{N}._/-]+`)// NormalizeDocPath keeps upstream write paths repository-relative Markdown.