merge: show and export the extracted passages
Browse files at this commit4 changed files +322 -13
- src/components/notes/rag-output-document.ts +68 -0
- src/components/notes/rag-output-drawer.test.tsx +174 -12
- src/components/notes/rag-output-drawer.tsx +46 -1
- src/components/notes/rag-output-messages.ts +34 -0
src/components/notes/rag-output-document.tsmodified+68 -0
| @@ -18,6 +18,8 @@ const RENDERED_KEYS: Record<string, true> = { | |||
| 18 | files: true, | 18 | files: true, |
| 19 | failed: true, | 19 | failed: true, |
| 20 | course_note: true, | 20 | course_note: true, |
| 21 | + passages: true, | ||
| 22 | + passages_truncated: true, | ||
| 21 | }; | 23 | }; |
| 22 | 24 | ||
| 23 | function asRecord(value: unknown): Record<string, unknown> | null { | 25 | function asRecord(value: unknown): Record<string, unknown> | null { |
| @@ -78,11 +80,61 @@ export type RagOutputDocument = { | |||
| 78 | /** Stored attachment ids whose name the payload does not carry. */ | 80 | /** Stored attachment ids whose name the payload does not carry. */ |
| 79 | unnamedSources: number; | 81 | unnamedSources: number; |
| 80 | failed: string[]; | 82 | failed: string[]; |
| 83 | + /** The indexed chunks the service handed back, in the order it wrote them. */ | ||
| 84 | + passages: RagOutputPassage[]; | ||
| 85 | + /** The service dropped passages above its response budget. */ | ||
| 86 | + passagesTruncated: boolean; | ||
| 87 | + /** The payload's own `chunks` — the total it indexed — when it recorded one. */ | ||
| 88 | + chunksTotal: number | null; | ||
| 81 | /** Payload entries with no dedicated rendering, in payload order. */ | 89 | /** Payload entries with no dedicated rendering, in payload order. */ |
| 82 | fields: Array<{ key: string; value: unknown }>; | 90 | fields: Array<{ key: string; value: unknown }>; |
| 83 | raw: unknown; | 91 | raw: unknown; |
| 84 | }; | 92 | }; |
| 85 | 93 | ||
| 94 | +/** One indexed chunk as the service returned it — the extraction, readable. */ | ||
| 95 | +export type RagOutputPassage = { | ||
| 96 | + /** The attachment the passage came from, by name; null when unresolvable. */ | ||
| 97 | + sourceName: string | null; | ||
| 98 | + /** The passage came from the note's own text, not from an attachment. */ | ||
| 99 | + fromNote: boolean; | ||
| 100 | + /** The chunk's own text, verbatim. */ | ||
| 101 | + text: string; | ||
| 102 | + pageStart: number | null; | ||
| 103 | + pageEnd: number | null; | ||
| 104 | +}; | ||
| 105 | + | ||
| 106 | +/** The note key the answer stamps its own body chunks with — the payload's own | ||
| 107 | + * `course_note` first, the stored row's link as the fallback. */ | ||
| 108 | +function payloadNoteKey(record: Record<string, unknown> | null, output: RagOutput): string { | ||
| 109 | + if (typeof record?.course_note === "string" && record.course_note) return record.course_note; | ||
| 110 | + return typeof output.course_note === "string" ? output.course_note : ""; | ||
| 111 | +} | ||
| 112 | + | ||
| 113 | +/** The passages the service recorded, dropping entries with no readable text. */ | ||
| 114 | +function payloadPassages( | ||
| 115 | + record: Record<string, unknown> | null, | ||
| 116 | + names: Map<string, string>, | ||
| 117 | + noteId: string, | ||
| 118 | +): RagOutputPassage[] { | ||
| 119 | + if (!Array.isArray(record?.passages)) return []; | ||
| 120 | + const pages = (value: unknown) => (typeof value === "number" && Number.isInteger(value) ? value : null); | ||
| 121 | + const passages: RagOutputPassage[] = []; | ||
| 122 | + for (const entry of record.passages) { | ||
| 123 | + const passage = asRecord(entry); | ||
| 124 | + if (typeof passage?.text !== "string" || !passage.text.trim()) continue; | ||
| 125 | + const docId = typeof passage.doc_id === "string" ? passage.doc_id : ""; | ||
| 126 | + passages.push({ | ||
| 127 | + sourceName: (docId ? names.get(docId) : undefined) ?? null, | ||
| 128 | + // The service stamps the note's own chunks with the note key, not a file id. | ||
| 129 | + fromNote: docId !== "" && docId === noteId, | ||
| 130 | + text: passage.text, | ||
| 131 | + pageStart: pages(passage.page_start), | ||
| 132 | + pageEnd: pages(passage.page_end), | ||
| 133 | + }); | ||
| 134 | + } | ||
| 135 | + return passages; | ||
| 136 | +} | ||
| 137 | + | ||
| 86 | /** | 138 | /** |
| 87 | * Read one stored AI output into everything the drawer shows. The payload is | 139 | * Read one stored AI output into everything the drawer shows. The payload is |
| 88 | * the service's own object served unread, so every read here is defensive: a | 140 | * the service's own object served unread, so every read here is defensive: a |
| @@ -120,6 +172,9 @@ export function readRagOutputDocument(output: RagOutput): RagOutputDocument { | |||
| 120 | sourceNames, | 172 | sourceNames, |
| 121 | unnamedSources, | 173 | unnamedSources, |
| 122 | failed, | 174 | failed, |
| 175 | + passages: payloadPassages(record, names, payloadNoteKey(record, output)), | ||
| 176 | + passagesTruncated: record?.passages_truncated === true, | ||
| 177 | + chunksTotal: typeof record?.chunks === "number" && Number.isFinite(record.chunks) ? record.chunks : null, | ||
| 123 | fields: record | 178 | fields: record |
| 124 | ? Object.entries(record) | 179 | ? Object.entries(record) |
| 125 | .filter(([key]) => !RENDERED_KEYS[key]) | 180 | .filter(([key]) => !RENDERED_KEYS[key]) |
| @@ -135,6 +190,12 @@ export type RagOutputMarkdownLabels = { | |||
| 135 | sources: string; | 190 | sources: string; |
| 136 | failed: string; | 191 | failed: string; |
| 137 | otherFields: string; | 192 | otherFields: string; |
| 193 | + /** Heading of the extracted-text section. */ | ||
| 194 | + passages: string; | ||
| 195 | + /** One passage's heading: source name and page range, never a raw id. */ | ||
| 196 | + passageHeader: (passage: RagOutputPassage) => string; | ||
| 197 | + /** Honest note when the service dropped passages; "" when it did not. */ | ||
| 198 | + passagesTruncated: string; | ||
| 138 | }; | 199 | }; |
| 139 | 200 | ||
| 140 | /** | 201 | /** |
| @@ -170,6 +231,13 @@ export function buildRagOutputMarkdown(input: { | |||
| 170 | for (const name of doc.failed) lines.push(`- ${name}`); | 231 | for (const name of doc.failed) lines.push(`- ${name}`); |
| 171 | lines.push(""); | 232 | lines.push(""); |
| 172 | } | 233 | } |
| 234 | + if (doc.passages.length > 0) { | ||
| 235 | + lines.push(`## ${labels.passages}`, ""); | ||
| 236 | + for (const passage of doc.passages) { | ||
| 237 | + lines.push(`**${labels.passageHeader(passage)}**`, "", passage.text, ""); | ||
| 238 | + } | ||
| 239 | + if (doc.passagesTruncated && labels.passagesTruncated) lines.push(labels.passagesTruncated, ""); | ||
| 240 | + } | ||
| 173 | if (doc.fields.length > 0) { | 241 | if (doc.fields.length > 0) { |
| 174 | lines.push(`## ${labels.otherFields}`, ""); | 242 | lines.push(`## ${labels.otherFields}`, ""); |
| 175 | for (const field of doc.fields) lines.push(`- **${field.key}:** ${fieldValueText(field.value)}`); | 243 | for (const field of doc.fields) lines.push(`- **${field.key}:** ${fieldValueText(field.value)}`); |
src/components/notes/rag-output-drawer.test.tsxmodified+174 -12
| @@ -1,9 +1,14 @@ | |||
| 1 | -import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library"; | 1 | +import { cleanup, fireEvent, render, screen, waitFor, within } from "@solidjs/testing-library"; |
| 2 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | 2 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | import type { RagOutput } from "@/api/client"; | 3 | import type { RagOutput } from "@/api/client"; |
| 4 | import { RagOutputDrawer } from "@/components/notes/rag-output-drawer"; | 4 | import { RagOutputDrawer } from "@/components/notes/rag-output-drawer"; |
| 5 | -import { buildRagOutputMarkdown, readRagOutputDocument } from "@/components/notes/rag-output-document"; | 5 | +import { |
| 6 | -import { ragOutputMessage } from "@/components/notes/rag-output-messages"; | 6 | + buildRagOutputMarkdown, |
| 7 | + readRagOutputDocument, | ||
| 8 | + type RagOutputDocument, | ||
| 9 | + type RagOutputMarkdownLabels, | ||
| 10 | +} from "@/components/notes/rag-output-document"; | ||
| 11 | +import { ragOutputMessage, ragOutputPassagePages } from "@/components/notes/rag-output-messages"; | ||
| 7 | import { RagOutputsPanel } from "@/components/notes/rag-outputs-panel"; | 12 | import { RagOutputsPanel } from "@/components/notes/rag-outputs-panel"; |
| 8 | import { formatDateTime } from "@/lib/format"; | 13 | import { formatDateTime } from "@/lib/format"; |
| 9 | import type { NoteFileSource } from "@/lib/note-source"; | 14 | import type { NoteFileSource } from "@/lib/note-source"; |
| @@ -44,24 +49,75 @@ const HEALTHY: RagOutput = { | |||
| 44 | }, | 49 | }, |
| 45 | }; | 50 | }; |
| 46 | 51 | ||
| 47 | -const LABELS = { | 52 | +const PASSAGE_TEXT_1 = "Hücre zarı seçici geçirgendir; madde alışverişini düzenler.\nZar proteinleri taşımayı üstlenir."; |
| 48 | - date: ragOutputMessage("en", "date"), | 53 | +const PASSAGE_TEXT_2 = "Mitokondri, oksijenli solunumla ATP üretir."; |
| 49 | - note: ragOutputMessage("en", "note"), | 54 | + |
| 50 | - sources: ragOutputMessage("en", "sources"), | 55 | +/** HEALTHY's payload with extra keys — the shape the service evolved into. */ |
| 51 | - failed: ragOutputMessage("en", "failedTitle"), | 56 | +function withPassages(extra: Record<string, unknown>): RagOutput { |
| 52 | - otherFields: ragOutputMessage("en", "otherFields"), | 57 | + return { ...HEALTHY, payload: { ...(HEALTHY.payload as Record<string, unknown>), ...extra } }; |
| 53 | -}; | 58 | +} |
| 59 | + | ||
| 60 | +const WITH_PASSAGES = withPassages({ | ||
| 61 | + passages: [ | ||
| 62 | + { chunk_id: "chunk-1", doc_id: "doc-ccc3", text: PASSAGE_TEXT_1, page_start: 3, page_end: 4 }, | ||
| 63 | + { chunk_id: "chunk-2", doc_id: "doc-ddd4", text: PASSAGE_TEXT_2, page_start: 5, page_end: 5 }, | ||
| 64 | + ], | ||
| 65 | + passages_truncated: false, | ||
| 66 | +}); | ||
| 67 | + | ||
| 68 | +const WITH_TRUNCATED = withPassages({ | ||
| 69 | + passages: [ | ||
| 70 | + { chunk_id: "chunk-1", doc_id: "doc-ccc3", text: PASSAGE_TEXT_1, page_start: 3, page_end: 4 }, | ||
| 71 | + { chunk_id: "chunk-2", doc_id: "doc-ddd4", text: PASSAGE_TEXT_2, page_start: 5, page_end: 5 }, | ||
| 72 | + ], | ||
| 73 | + passages_truncated: true, | ||
| 74 | +}); | ||
| 75 | + | ||
| 76 | +/** The service stamps the note's own body chunks with the note key. */ | ||
| 77 | +const WITH_NOTE_PASSAGE = withPassages({ | ||
| 78 | + passages: [ | ||
| 79 | + { chunk_id: "01NOTE:01NOTE:c0", doc_id: "note-1", text: PASSAGE_TEXT_2, page_start: null, page_end: null }, | ||
| 80 | + ], | ||
| 81 | +}); | ||
| 82 | + | ||
| 83 | +function markdownLabels(document: RagOutputDocument): RagOutputMarkdownLabels { | ||
| 84 | + return { | ||
| 85 | + date: ragOutputMessage("en", "date"), | ||
| 86 | + note: ragOutputMessage("en", "note"), | ||
| 87 | + sources: ragOutputMessage("en", "sources"), | ||
| 88 | + failed: ragOutputMessage("en", "failedTitle"), | ||
| 89 | + otherFields: ragOutputMessage("en", "otherFields"), | ||
| 90 | + passages: ragOutputMessage("en", "passages"), | ||
| 91 | + passageHeader: (passage) => { | ||
| 92 | + const source = passage.sourceName | ||
| 93 | + ?? (passage.fromNote ? NOTE_TITLE : ragOutputMessage("en", "unnamedSource")); | ||
| 94 | + const pages = ragOutputPassagePages("en", passage.pageStart, passage.pageEnd); | ||
| 95 | + return pages ? `${source} · ${pages}` : source; | ||
| 96 | + }, | ||
| 97 | + passagesTruncated: document.passagesTruncated | ||
| 98 | + ? document.chunksTotal != null | ||
| 99 | + ? ragOutputMessage("en", "passagesTruncated", { shown: document.passages.length, total: document.chunksTotal }) | ||
| 100 | + : ragOutputMessage("en", "passagesTruncatedNoTotal", { shown: document.passages.length }) | ||
| 101 | + : "", | ||
| 102 | + }; | ||
| 103 | +} | ||
| 54 | 104 | ||
| 55 | function expectedMarkdown(output: RagOutput) { | 105 | function expectedMarkdown(output: RagOutput) { |
| 106 | + const document = readRagOutputDocument(output); | ||
| 56 | return buildRagOutputMarkdown({ | 107 | return buildRagOutputMarkdown({ |
| 57 | title: NOTE_TITLE, | 108 | title: NOTE_TITLE, |
| 58 | date: formatDateTime(output.generated_at, "en"), | 109 | date: formatDateTime(output.generated_at, "en"), |
| 59 | noteTitle: NOTE_TITLE, | 110 | noteTitle: NOTE_TITLE, |
| 60 | - document: readRagOutputDocument(output), | 111 | + document, |
| 61 | - labels: LABELS, | 112 | + labels: markdownLabels(document), |
| 62 | }); | 113 | }); |
| 63 | } | 114 | } |
| 64 | 115 | ||
| 116 | +/** The drawer's extracted-text section, so raw-record assertions stay scoped. */ | ||
| 117 | +function passagesSection(): HTMLElement { | ||
| 118 | + return screen.getByText("Extracted text").closest("div") as HTMLElement; | ||
| 119 | +} | ||
| 120 | + | ||
| 65 | function stubSource(output: RagOutput): NoteFileSource { | 121 | function stubSource(output: RagOutput): NoteFileSource { |
| 66 | return { | 122 | return { |
| 67 | maxFiles: 10, | 123 | maxFiles: 10, |
| @@ -112,6 +168,7 @@ describe("RagOutputDrawer", () => { | |||
| 112 | 168 | ||
| 113 | afterEach(() => { | 169 | afterEach(() => { |
| 114 | cleanup(); | 170 | cleanup(); |
| 171 | + localStorage.removeItem("hezarfen.locale"); | ||
| 115 | vi.clearAllMocks(); | 172 | vi.clearAllMocks(); |
| 116 | }); | 173 | }); |
| 117 | 174 | ||
| @@ -189,4 +246,109 @@ describe("RagOutputDrawer", () => { | |||
| 189 | expect(screen.getByText("Unnamed attachment")).toBeTruthy(); | 246 | expect(screen.getByText("Unnamed attachment")).toBeTruthy(); |
| 190 | expect(screen.queryByText(/file-zzz9/)).toBeNull(); | 247 | expect(screen.queryByText(/file-zzz9/)).toBeNull(); |
| 191 | }); | 248 | }); |
| 249 | + | ||
| 250 | + it("shows the extracted passages with their source files, page ranges and line breaks", async () => { | ||
| 251 | + renderDrawer(WITH_PASSAGES); | ||
| 252 | + | ||
| 253 | + await waitFor(() => expect(screen.getByText("Extracted text")).toBeTruthy()); | ||
| 254 | + expect(screen.getByText("hücre-notu.pdf · pp. 3-4")).toBeTruthy(); | ||
| 255 | + expect(screen.getByText("organeller.pdf · p. 5")).toBeTruthy(); | ||
| 256 | + // The section renders the passages — it does not dump their ids. | ||
| 257 | + const section = passagesSection(); | ||
| 258 | + const first = within(section).getByText(/Hücre zarı seçici geçirgendir/); | ||
| 259 | + expect(first.textContent).toBe(PASSAGE_TEXT_1); | ||
| 260 | + expect(first.className).toContain("whitespace-pre-wrap"); | ||
| 261 | + expect(within(section).getByText(/Mitokondri, oksijenli solunumla/)).toBeTruthy(); | ||
| 262 | + expect(within(section).queryByText(/chunk-/)).toBeNull(); | ||
| 263 | + expect(within(section).queryByText(/doc-/)).toBeNull(); | ||
| 264 | + }); | ||
| 265 | + | ||
| 266 | + it("exports the same passages, in the same order, into the Markdown document", () => { | ||
| 267 | + const markdown = expectedMarkdown(WITH_PASSAGES); | ||
| 268 | + | ||
| 269 | + expect(markdown).toContain("## Extracted text"); | ||
| 270 | + expect(markdown).toContain("**hücre-notu.pdf · pp. 3-4**"); | ||
| 271 | + expect(markdown).toContain("**organeller.pdf · p. 5**"); | ||
| 272 | + const first = markdown.indexOf(PASSAGE_TEXT_1); | ||
| 273 | + const second = markdown.indexOf(PASSAGE_TEXT_2); | ||
| 274 | + expect(first).toBeGreaterThan(-1); | ||
| 275 | + expect(second).toBeGreaterThan(first); | ||
| 276 | + expect(markdown.indexOf("## Extracted text")).toBeLessThan(markdown.indexOf("## Other fields")); | ||
| 277 | + expect(markdown).not.toContain("chunk-1"); | ||
| 278 | + expect(markdown).not.toContain("doc-ccc3"); | ||
| 279 | + expect(markdown).not.toContain("passages_truncated"); | ||
| 280 | + }); | ||
| 281 | + | ||
| 282 | + it("copies the passages exactly as exported, truncation note included", async () => { | ||
| 283 | + renderDrawer(WITH_TRUNCATED); | ||
| 284 | + | ||
| 285 | + fireEvent.click(screen.getByRole("button", { name: "Copy" })); | ||
| 286 | + | ||
| 287 | + await waitFor(() => expect(writeText).toHaveBeenCalledTimes(1)); | ||
| 288 | + expect(writeText).toHaveBeenCalledWith(expectedMarkdown(WITH_TRUNCATED)); | ||
| 289 | + }); | ||
| 290 | + | ||
| 291 | + it("renders no extracted-text section for outputs stored before passages existed", async () => { | ||
| 292 | + renderDrawer(HEALTHY); | ||
| 293 | + | ||
| 294 | + await waitFor(() => expect(screen.getByText(SUMMARY)).toBeTruthy()); | ||
| 295 | + expect(screen.queryByText("Extracted text")).toBeNull(); | ||
| 296 | + }); | ||
| 297 | + | ||
| 298 | + it("survives an ill-typed passages value without rendering a section", async () => { | ||
| 299 | + renderDrawer(withPassages({ passages: "bozuk" })); | ||
| 300 | + | ||
| 301 | + await waitFor(() => expect(screen.getByText(SUMMARY)).toBeTruthy()); | ||
| 302 | + expect(screen.queryByText("Extracted text")).toBeNull(); | ||
| 303 | + }); | ||
| 304 | + | ||
| 305 | + it("says honestly how many passages were extracted when the list is capped", async () => { | ||
| 306 | + renderDrawer(WITH_TRUNCATED); | ||
| 307 | + | ||
| 308 | + await waitFor(() => expect(screen.getByText("Extracted text")).toBeTruthy()); | ||
| 309 | + expect( | ||
| 310 | + screen.getByText(ragOutputMessage("en", "passagesTruncated", { shown: 2, total: 18 })), | ||
| 311 | + ).toBeTruthy(); | ||
| 312 | + }); | ||
| 313 | + | ||
| 314 | + it("renders the Turkish section title and truncation sentence", async () => { | ||
| 315 | + localStorage.setItem("hezarfen.locale", "tr"); | ||
| 316 | + renderDrawer(WITH_TRUNCATED); | ||
| 317 | + | ||
| 318 | + await waitFor(() => expect(screen.getByText("Çıkarılan metin")).toBeTruthy()); | ||
| 319 | + expect( | ||
| 320 | + screen.getByText(ragOutputMessage("tr", "passagesTruncated", { shown: 2, total: 18 })), | ||
| 321 | + ).toBeTruthy(); | ||
| 322 | + }); | ||
| 323 | + | ||
| 324 | + it("never shows a raw attachment id for a passage whose source cannot be resolved", async () => { | ||
| 325 | + renderDrawer( | ||
| 326 | + withPassages({ | ||
| 327 | + passages: [ | ||
| 328 | + { chunk_id: "chunk-9", doc_id: "doc-zzz9", text: PASSAGE_TEXT_2, page_start: null, page_end: null }, | ||
| 329 | + ], | ||
| 330 | + }), | ||
| 331 | + ); | ||
| 332 | + | ||
| 333 | + await waitFor(() => expect(screen.getByText("Extracted text")).toBeTruthy()); | ||
| 334 | + expect(screen.getByText("Unnamed attachment")).toBeTruthy(); | ||
| 335 | + const section = passagesSection(); | ||
| 336 | + expect(within(section).getByText(/Mitokondri, oksijenli solunumla/)).toBeTruthy(); | ||
| 337 | + expect(within(section).queryByText(/doc-zzz9/)).toBeNull(); | ||
| 338 | + expect(within(section).queryByText(/chunk-9/)).toBeNull(); | ||
| 339 | + }); | ||
| 340 | + | ||
| 341 | + it("labels a note-body passage with the note title, never the note key", async () => { | ||
| 342 | + renderDrawer(WITH_NOTE_PASSAGE); | ||
| 343 | + | ||
| 344 | + await waitFor(() => expect(screen.getByText("Extracted text")).toBeTruthy()); | ||
| 345 | + const section = passagesSection(); | ||
| 346 | + expect(within(section).getByText(NOTE_TITLE)).toBeTruthy(); | ||
| 347 | + expect(within(section).queryByText(/note-1/)).toBeNull(); | ||
| 348 | + | ||
| 349 | + // The export carries the same label the view shows. | ||
| 350 | + fireEvent.click(screen.getByRole("button", { name: "Copy" })); | ||
| 351 | + await waitFor(() => expect(writeText).toHaveBeenCalledTimes(1)); | ||
| 352 | + expect(writeText).toHaveBeenCalledWith(expectedMarkdown(WITH_NOTE_PASSAGE)); | ||
| 353 | + }); | ||
| 192 | }); | 354 | }); |
src/components/notes/rag-output-drawer.tsxmodified+46 -1
| @@ -9,8 +9,14 @@ import { | |||
| 9 | fieldValueText, | 9 | fieldValueText, |
| 10 | ragOutputFileName, | 10 | ragOutputFileName, |
| 11 | readRagOutputDocument, | 11 | readRagOutputDocument, |
| 12 | + type RagOutputDocument, | ||
| 13 | + type RagOutputPassage, | ||
| 12 | } from "@/components/notes/rag-output-document"; | 14 | } from "@/components/notes/rag-output-document"; |
| 13 | -import { ragOutputMessage, type RagOutputMessageKey } from "@/components/notes/rag-output-messages"; | 15 | +import { |
| 16 | + ragOutputMessage, | ||
| 17 | + ragOutputPassagePages, | ||
| 18 | + type RagOutputMessageKey, | ||
| 19 | +} from "@/components/notes/rag-output-messages"; | ||
| 14 | import { formatDateTime } from "@/lib/format"; | 20 | import { formatDateTime } from "@/lib/format"; |
| 15 | import { usePreferences } from "@/stores/preferences-context"; | 21 | import { usePreferences } from "@/stores/preferences-context"; |
| 16 | 22 | ||
| @@ -58,6 +64,20 @@ export function RagOutputDrawer(props: { | |||
| 58 | 64 | ||
| 59 | const doc = () => (props.output ? readRagOutputDocument(props.output) : null); | 65 | const doc = () => (props.output ? readRagOutputDocument(props.output) : null); |
| 60 | const noteTitle = () => props.noteTitle?.trim() ?? ""; | 66 | const noteTitle = () => props.noteTitle?.trim() ?? ""; |
| 67 | + /** One passage's heading: the source file by name, its page range if any. */ | ||
| 68 | + const passageHeader = (passage: RagOutputPassage) => { | ||
| 69 | + const source = passage.sourceName | ||
| 70 | + ?? (passage.fromNote ? noteTitle() || rt("noteText") : rt("unnamedSource")); | ||
| 71 | + const pages = ragOutputPassagePages(locale(), passage.pageStart, passage.pageEnd); | ||
| 72 | + return pages ? `${source} · ${pages}` : source; | ||
| 73 | + }; | ||
| 74 | + /** The honest note when the service's cap means more passages exist. */ | ||
| 75 | + const passagesNote = (read: RagOutputDocument) => { | ||
| 76 | + if (!read.passagesTruncated) return ""; | ||
| 77 | + return read.chunksTotal != null | ||
| 78 | + ? rt("passagesTruncated", { shown: read.passages.length, total: read.chunksTotal }) | ||
| 79 | + : rt("passagesTruncatedNoTotal", { shown: read.passages.length }); | ||
| 80 | + }; | ||
| 61 | const markdown = () => { | 81 | const markdown = () => { |
| 62 | const output = props.output; | 82 | const output = props.output; |
| 63 | const read = doc(); | 83 | const read = doc(); |
| @@ -73,6 +93,9 @@ export function RagOutputDrawer(props: { | |||
| 73 | sources: rt("sources"), | 93 | sources: rt("sources"), |
| 74 | failed: rt("failedTitle"), | 94 | failed: rt("failedTitle"), |
| 75 | otherFields: rt("otherFields"), | 95 | otherFields: rt("otherFields"), |
| 96 | + passages: rt("passages"), | ||
| 97 | + passageHeader, | ||
| 98 | + passagesTruncated: passagesNote(read), | ||
| 76 | }, | 99 | }, |
| 77 | }); | 100 | }); |
| 78 | }; | 101 | }; |
| @@ -200,6 +223,28 @@ export function RagOutputDrawer(props: { | |||
| 200 | </Show> | 223 | </Show> |
| 201 | </Show> | 224 | </Show> |
| 202 | 225 | ||
| 226 | + <Show when={read().passages.length > 0}> | ||
| 227 | + <div class="space-y-2"> | ||
| 228 | + <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">{rt("passages")}</p> | ||
| 229 | + <ul class="space-y-3"> | ||
| 230 | + <For each={read().passages}> | ||
| 231 | + {(passage) => ( | ||
| 232 | + <li class="rounded-lg border border-border/60 p-3"> | ||
| 233 | + <p class="text-xs text-muted-foreground">{passageHeader(passage)}</p> | ||
| 234 | + {/* Scrolls past its cap on screen; print flows it out in full. */} | ||
| 235 | + <div class="mt-1.5 max-h-60 overflow-y-auto print:max-h-none"> | ||
| 236 | + <p class="whitespace-pre-wrap break-words text-sm leading-6">{passage.text}</p> | ||
| 237 | + </div> | ||
| 238 | + </li> | ||
| 239 | + )} | ||
| 240 | + </For> | ||
| 241 | + </ul> | ||
| 242 | + <Show when={passagesNote(read())}> | ||
| 243 | + <p class="text-xs text-muted-foreground">{passagesNote(read())}</p> | ||
| 244 | + </Show> | ||
| 245 | + </div> | ||
| 246 | + </Show> | ||
| 247 | + | ||
| 203 | <Show when={read().fields.length > 0}> | 248 | <Show when={read().fields.length > 0}> |
| 204 | <div class="space-y-1.5"> | 249 | <div class="space-y-1.5"> |
| 205 | <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"> | 250 | <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"> |
src/components/notes/rag-output-messages.tsmodified+34 -0
| @@ -23,6 +23,12 @@ export type RagOutputMessageKey = | |||
| 23 | | "failedLine" | 23 | | "failedLine" |
| 24 | | "otherFields" | 24 | | "otherFields" |
| 25 | | "chunks" | 25 | | "chunks" |
| 26 | + | "passages" | ||
| 27 | + | "passagePagesSingle" | ||
| 28 | + | "passagePagesRange" | ||
| 29 | + | "passagesTruncated" | ||
| 30 | + | "passagesTruncatedNoTotal" | ||
| 31 | + | "noteText" | ||
| 26 | | "technical" | 32 | | "technical" |
| 27 | | "copy" | 33 | | "copy" |
| 28 | | "copied" | 34 | | "copied" |
| @@ -43,6 +49,12 @@ const en: Record<RagOutputMessageKey, string> = { | |||
| 43 | "failedLine": "Not processed: {names}", | 49 | "failedLine": "Not processed: {names}", |
| 44 | "otherFields": "Other fields", | 50 | "otherFields": "Other fields", |
| 45 | "chunks": "Text chunks", | 51 | "chunks": "Text chunks", |
| 52 | + "passages": "Extracted text", | ||
| 53 | + "passagePagesSingle": "p. {page}", | ||
| 54 | + "passagePagesRange": "pp. {start}-{end}", | ||
| 55 | + "passagesTruncated": "{total} passages were extracted in total; the first {shown} are listed here.", | ||
| 56 | + "passagesTruncatedNoTotal": "Only the first {shown} extracted passages are listed here.", | ||
| 57 | + "noteText": "Note text", | ||
| 46 | "technical": "Technical details", | 58 | "technical": "Technical details", |
| 47 | "copy": "Copy", | 59 | "copy": "Copy", |
| 48 | "copied": "Copied to the clipboard", | 60 | "copied": "Copied to the clipboard", |
| @@ -64,6 +76,12 @@ const tr: Record<RagOutputMessageKey, string> = { | |||
| 64 | "failedLine": "İşlenemeyenler: {names}", | 76 | "failedLine": "İşlenemeyenler: {names}", |
| 65 | "otherFields": "Diğer alanlar", | 77 | "otherFields": "Diğer alanlar", |
| 66 | "chunks": "Metin parçaları", | 78 | "chunks": "Metin parçaları", |
| 79 | + "passages": "Çıkarılan metin", | ||
| 80 | + "passagePagesSingle": "s. {page}", | ||
| 81 | + "passagePagesRange": "s. {start}-{end}", | ||
| 82 | + "passagesTruncated": "Toplam {total} metin parçası çıkarıldı; burada ilk {shown} tanesi gösteriliyor.", | ||
| 83 | + "passagesTruncatedNoTotal": "Çıkarılan parçaların yalnızca ilk {shown} tanesi gösteriliyor.", | ||
| 84 | + "noteText": "Not metni", | ||
| 67 | "technical": "Teknik ayrıntı", | 85 | "technical": "Teknik ayrıntı", |
| 68 | "copy": "Kopyala", | 86 | "copy": "Kopyala", |
| 69 | "copied": "Panoya kopyalandı", | 87 | "copied": "Panoya kopyalandı", |
| @@ -79,3 +97,19 @@ export function ragOutputMessage( | |||
| 79 | ): string { | 97 | ): string { |
| 80 | return formatMessage((locale === "tr" ? tr : en)[key], vars); | 98 | return formatMessage((locale === "tr" ? tr : en)[key], vars); |
| 81 | } | 99 | } |
| 100 | + | ||
| 101 | +/** | ||
| 102 | + * One passage's page range (`s. 3-4`), or an empty string when the service | ||
| 103 | + * recorded no page — the label is then left off entirely. | ||
| 104 | + */ | ||
| 105 | +export function ragOutputPassagePages( | ||
| 106 | + locale: Locale, | ||
| 107 | + pageStart: number | null, | ||
| 108 | + pageEnd: number | null, | ||
| 109 | +): string { | ||
| 110 | + if (pageStart == null) return ""; | ||
| 111 | + if (pageEnd == null || pageEnd <= pageStart) { | ||
| 112 | + return ragOutputMessage(locale, "passagePagesSingle", { page: pageStart }); | ||
| 113 | + } | ||
| 114 | + return ragOutputMessage(locale, "passagePagesRange", { start: pageStart, end: pageEnd }); | ||
| 115 | +} | ||