|
| 1 | +package agents |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/url" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | +) |
| 9 | + |
| 10 | +type kbCitationList struct { |
| 11 | + mu sync.Mutex |
| 12 | + citations []KBCitation |
| 13 | +} |
| 14 | + |
| 15 | +func (l *kbCitationList) AddKBCitations(citations []KBCitation) { |
| 16 | + if len(citations) == 0 { |
| 17 | + return |
| 18 | + } |
| 19 | + l.mu.Lock() |
| 20 | + defer l.mu.Unlock() |
| 21 | + l.citations = append(l.citations, citations...) |
| 22 | +} |
| 23 | + |
| 24 | +func (l *kbCitationList) Citations() []KBCitation { |
| 25 | + l.mu.Lock() |
| 26 | + defer l.mu.Unlock() |
| 27 | + out := make([]KBCitation, len(l.citations)) |
| 28 | + copy(out, l.citations) |
| 29 | + return out |
| 30 | +} |
| 31 | + |
| 32 | +// AppendKBCitations appends a markdown Sources block for KB citations. |
| 33 | +func AppendKBCitations(response, collection, userID string, citations []KBCitation) string { |
| 34 | + if strings.TrimSpace(response) == "" || len(citations) == 0 { |
| 35 | + return response |
| 36 | + } |
| 37 | + |
| 38 | + var lines []string |
| 39 | + seen := make(map[string]struct{}) |
| 40 | + for _, citation := range citations { |
| 41 | + key := strings.TrimSpace(citation.EntryKey) |
| 42 | + if key == "" { |
| 43 | + key = strings.TrimSpace(citation.FileName) |
| 44 | + } |
| 45 | + if key == "" { |
| 46 | + continue |
| 47 | + } |
| 48 | + if _, ok := seen[key]; ok { |
| 49 | + continue |
| 50 | + } |
| 51 | + seen[key] = struct{}{} |
| 52 | + |
| 53 | + displayName := kbCitationDisplayName(citation) |
| 54 | + if displayName == "" { |
| 55 | + continue |
| 56 | + } |
| 57 | + |
| 58 | + sourceURL := kbCitationRawFileURL(collection, citation.EntryKey, userID) |
| 59 | + number := len(lines) + 1 |
| 60 | + if sourceURL == "" { |
| 61 | + lines = append(lines, fmt.Sprintf("[%d] %s", number, displayName)) |
| 62 | + continue |
| 63 | + } |
| 64 | + lines = append(lines, fmt.Sprintf("[%d] [%s](%s)", number, escapeMarkdownLinkText(displayName), sourceURL)) |
| 65 | + } |
| 66 | + |
| 67 | + if len(lines) == 0 { |
| 68 | + return response |
| 69 | + } |
| 70 | + |
| 71 | + var sb strings.Builder |
| 72 | + sb.WriteString(strings.TrimRight(response, "\n")) |
| 73 | + sb.WriteString("\n\nSources:\n") |
| 74 | + for _, line := range lines { |
| 75 | + sb.WriteString(line) |
| 76 | + sb.WriteString("\n") |
| 77 | + } |
| 78 | + return strings.TrimRight(sb.String(), "\n") |
| 79 | +} |
| 80 | + |
| 81 | +func kbCitationDisplayName(citation KBCitation) string { |
| 82 | + if fileName := strings.TrimSpace(citation.FileName); fileName != "" { |
| 83 | + return fileName |
| 84 | + } |
| 85 | + |
| 86 | + segments := strings.Split(strings.Trim(strings.TrimSpace(citation.EntryKey), "/"), "/") |
| 87 | + for i := len(segments) - 1; i >= 0; i-- { |
| 88 | + if segment := strings.TrimSpace(segments[i]); segment != "" { |
| 89 | + return segment |
| 90 | + } |
| 91 | + } |
| 92 | + return "" |
| 93 | +} |
| 94 | + |
| 95 | +func kbCitationRawFileURL(collection, entryKey, userID string) string { |
| 96 | + collection = strings.TrimSpace(collection) |
| 97 | + entryKey = strings.Trim(strings.TrimSpace(entryKey), "/") |
| 98 | + if collection == "" || entryKey == "" { |
| 99 | + return "" |
| 100 | + } |
| 101 | + |
| 102 | + var escapedEntrySegments []string |
| 103 | + for _, segment := range strings.Split(entryKey, "/") { |
| 104 | + if segment == "" { |
| 105 | + continue |
| 106 | + } |
| 107 | + escapedEntrySegments = append(escapedEntrySegments, url.PathEscape(segment)) |
| 108 | + } |
| 109 | + if len(escapedEntrySegments) == 0 { |
| 110 | + return "" |
| 111 | + } |
| 112 | + |
| 113 | + sourceURL := "/api/agents/collections/" + url.PathEscape(collection) + "/entries-raw/" + strings.Join(escapedEntrySegments, "/") |
| 114 | + if userID != "" { |
| 115 | + query := url.Values{} |
| 116 | + query.Set("user_id", userID) |
| 117 | + sourceURL += "?" + query.Encode() |
| 118 | + } |
| 119 | + return sourceURL |
| 120 | +} |
| 121 | + |
| 122 | +func escapeMarkdownLinkText(text string) string { |
| 123 | + text = strings.ReplaceAll(text, `\`, `\\`) |
| 124 | + text = strings.ReplaceAll(text, "[", `\[`) |
| 125 | + text = strings.ReplaceAll(text, "]", `\]`) |
| 126 | + return text |
| 127 | +} |
0 commit comments