hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import type { RagCitation, RagStudyScope } from "@/api/client"; |
| 2 | |
| 3 | /** Longest range label sent with a study request. */ |
| 4 | const LABEL_MAX = 120; |
| 5 | |
| 6 | /** |
| 7 | * The study range behind one answer: the subject most of its citations come |
| 8 | * from, and exactly the passages (span ids) and pages they cite there. A |
| 9 | * study request covers one corpus, so citations from another subject are |
| 10 | * left out rather than mixed in. Null when no citation names a subject — the |
| 11 | * service needs `ders`, and guessing one would study the wrong material. |
| 12 | */ |
| 13 | export function scopeFromCitations(citations: RagCitation[], label?: string): RagStudyScope | null { |
| 14 | const bySubject = new Map<string, RagCitation[]>(); |
| 15 | for (const citation of citations) { |
| 16 | const ders = citation.ders?.trim(); |
| 17 | if (!ders || citation.span_ids.length === 0) continue; |
| 18 | bySubject.set(ders, [...(bySubject.get(ders) ?? []), citation]); |
| 19 | } |
| 20 | let best: [string, RagCitation[]] | null = null; |
| 21 | for (const entry of bySubject) if (!best || entry[1].length > best[1].length) best = entry; |
| 22 | if (!best) return null; |
| 23 | const [ders, cited] = best; |
| 24 | const trimmed = label?.trim(); |
| 25 | return { |
| 26 | ders, |
| 27 | span_ids: [...new Set(cited.flatMap((citation) => citation.span_ids))], |
| 28 | pages: [...new Set(cited.flatMap((citation) => citation.pages))].sort((a, b) => a - b), |
| 29 | scope_label: trimmed ? trimmed.slice(0, LABEL_MAX) : null, |
| 30 | }; |
| 31 | } |