-
Notifications
You must be signed in to change notification settings - Fork 0
feat: overhaul document viewer page (smart summary, badges, flattened evidence, flowing source text) #492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: overhaul document viewer page (smart summary, badges, flattened evidence, flowing source text) #492
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // Badge derivation for the document viewer's high-yield summary card. | ||
| // | ||
| // Framework-free (mirrors medication-badges.ts): data in, ClinicalBadge-shaped | ||
| // items out. Two inputs feed the cluster — safety-relevant document labels | ||
| // (Risk / Medication / Clinical action) and phrases detected in the stored | ||
| // summary text itself ("narrow therapeutic index", "Schedule 8", …). Tone | ||
| // rules follow the badge governance in docs/clinical-badge-system-guide.md: | ||
| // danger is reserved for true contraindications; regulatory/caution signals | ||
| // (S8, high-risk, toxicity) are warnings. | ||
|
|
||
| import { buildSmartDocumentTags, type SmartDocumentTagGroup } from "@/lib/document-tags"; | ||
| import { sortBySemanticTonePriority, type SemanticIconKey, type SemanticTone } from "@/lib/semantic-tone"; | ||
| import type { DocumentLabel } from "@/lib/types"; | ||
|
|
||
| export type DocumentSummaryBadge = { | ||
| id: string; | ||
| label: string; | ||
| tone: SemanticTone; | ||
| iconKey?: SemanticIconKey; | ||
| }; | ||
|
|
||
| type DocumentLabelLike = Pick<DocumentLabel, "label" | "label_type" | "source" | "confidence" | "metadata">; | ||
|
|
||
| // Canonical tone for each smart-tag group. Shared with DocumentTagCloud so the | ||
| // tag chips and the summary badge cluster speak the same colour language. | ||
| export const documentTagGroupTone: Record<SmartDocumentTagGroup, SemanticTone> = { | ||
| Risk: "warning", | ||
| Medication: "clinical", | ||
| "Clinical action": "clinical", | ||
| Workflow: "info", | ||
| Manual: "success", | ||
| Site: "neutral", | ||
| Topic: "neutral", | ||
| Population: "neutral", | ||
| Setting: "neutral", | ||
| Service: "neutral", | ||
| "Document type": "neutral", | ||
| "Care phase": "neutral", | ||
| "Document intent": "neutral", | ||
| "Content feature": "neutral", | ||
| }; | ||
|
|
||
| // Groups whose labels are important enough to promote into the badge cluster; | ||
| // the rest stay in the browse-by-tag cloud. | ||
| const badgeWorthyGroups = new Set<SmartDocumentTagGroup>(["Risk", "Medication", "Clinical action"]); | ||
|
|
||
| type SummaryPhraseRule = { | ||
| id: string; | ||
| pattern: RegExp; | ||
| label: string; | ||
| tone: SemanticTone; | ||
| iconKey?: SemanticIconKey; | ||
| }; | ||
|
|
||
| // Phrase catalogue over the stored summary text. Every rule here is also | ||
| // registered in SEMANTIC_FLAG_CATALOGUE (document domain) so the | ||
| // /reference/colour-coding legend stays complete. | ||
| const summaryPhraseRules: SummaryPhraseRule[] = [ | ||
| { id: "summary-contraindication", pattern: /contraindicat/i, label: "Contraindications", tone: "danger" }, | ||
|
BigSimmo marked this conversation as resolved.
|
||
| { | ||
| id: "summary-narrow-therapeutic-index", | ||
| pattern: /narrow therapeutic (?:index|window|range)/i, | ||
| label: "Narrow therapeutic index", | ||
| tone: "warning", | ||
| }, | ||
| { | ||
| id: "summary-high-risk-medication", | ||
| pattern: /high[- ]?risk medication/i, | ||
| label: "High-risk medication", | ||
| tone: "warning", | ||
| }, | ||
| { | ||
| id: "summary-controlled-drug", | ||
| pattern: /\bschedule\s*8\b|\bS8\b/i, | ||
| label: "Schedule 8", | ||
| tone: "warning", | ||
| iconKey: "controlled", | ||
| }, | ||
| { id: "summary-toxicity", pattern: /\btoxic(?:ity)?\b/i, label: "Toxicity risk", tone: "warning" }, | ||
| { | ||
| id: "summary-escalation", | ||
| pattern: /\b(?:escalat\w*|urgent(?:ly)? review\w*|emergency)\b/i, | ||
| label: "Escalation criteria", | ||
| tone: "warning", | ||
| }, | ||
| { | ||
| id: "summary-pregnancy", | ||
| pattern: /\b(?:pregnan\w*|lactation|breastfeed\w*)\b/i, | ||
| label: "Pregnancy & lactation", | ||
| tone: "warning", | ||
| }, | ||
| { | ||
| id: "summary-monitoring", | ||
| pattern: /\b(?:monitor(?:ing|ed)?|serum levels?|blood tests?|fbc|anc|ecg|qtc)\b/i, | ||
| label: "Monitoring required", | ||
| tone: "info", | ||
| }, | ||
| ]; | ||
|
|
||
| export function buildDocumentSummaryBadges({ | ||
| labels, | ||
| summaryText, | ||
| limit = 8, | ||
| }: { | ||
| labels?: DocumentLabelLike[] | null; | ||
| summaryText?: string | null; | ||
| limit?: number; | ||
| }): DocumentSummaryBadge[] { | ||
| const badges: DocumentSummaryBadge[] = []; | ||
| const seenIds = new Set<string>(); | ||
| const seenLabels = new Set<string>(); | ||
|
|
||
| const push = (badge: DocumentSummaryBadge) => { | ||
| const labelKey = badge.label.toLowerCase(); | ||
| if (seenIds.has(badge.id) || seenLabels.has(labelKey)) return; | ||
| seenIds.add(badge.id); | ||
| seenLabels.add(labelKey); | ||
| badges.push(badge); | ||
| }; | ||
|
|
||
| for (const tag of buildSmartDocumentTags(labels, { includeManualGroup: false })) { | ||
| if (!badgeWorthyGroups.has(tag.group)) continue; | ||
| push({ id: `label-${tag.key}`, label: tag.label, tone: documentTagGroupTone[tag.group] }); | ||
| } | ||
|
|
||
| if (summaryText) { | ||
| for (const rule of summaryPhraseRules) { | ||
| if (!rule.pattern.test(summaryText)) continue; | ||
| push({ id: rule.id, label: rule.label, tone: rule.tone, iconKey: rule.iconKey }); | ||
| } | ||
| } | ||
|
|
||
| return sortBySemanticTonePriority(badges).slice(0, Math.max(0, limit)); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.