Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | 24x 24x 327x 327x 19x 19x 327x 24x 21x 21x 21x 16x 5x 24x 24x 22x 22x 24x 34x 24x 29x 29x 29x 29x 24x 15x 14x 14x 24x 12x 12x | /**
* A structured document — svc-ai's `DocumentSpec` (app/documents/spec.py).
*
* It arrives in a ```document fence, put in a reply by svc-ai's
* `render_document` tool (a built-in report skill ends in one). The fence is
* the document; the PDF is a rendering of it, made by svc-core from the
* *stored* message or skill run, never from what the browser holds.
*/
export type DocumentConfidence = "low" | "medium" | "high";
/**
* A stored copy of an image a document's body shows (svc-ai
* `DocumentImage`). svc-ai fetched it once, when the document was made, and
* stored it under the SHA-256 of its bytes — `asset`. The card draws that
* copy from svc-core, never `src`: the image's own site never sees who reads
* the document.
*/
export interface DocumentImage {
/** The image's link in the body, as svc-ai's Markdown parser normalised it. */
src: string;
asset: string;
media_type: "image/png" | "image/jpeg";
width: number;
height: number;
}
export interface DocumentSpec {
title: string;
subtitle?: string;
summary?: string;
key_findings?: string[];
/** Markdown, with `[n]` citations into `sources`. */
body?: string;
sources?: string[];
confidence?: DocumentConfidence;
open_questions?: string[];
/** The stored copies of the images the body shows. */
images?: DocumentImage[];
}
const DOCUMENT_FENCE = /```document[ \t]*\n([\s\S]*?)\n```/g;
/**
* Lift every ```document fence out of `text` before it is rendered.
*
* Each fence becomes ```document\n<n>\n``` and its body is returned at
* index n — the order svc-core numbers them by (`services/documents.py`).
* The Markdown pre-processors (citations, maths) then never see a
* document's JSON, so it reaches the card exactly as it was stored.
*/
export const extractDocuments = (
text: string,
): { text: string; documents: string[] } => {
const documents: string[] = [];
const replaced = text.replace(DOCUMENT_FENCE, (_match, body: string) => {
documents.push(body);
return "```document\n" + (documents.length - 1) + "\n```";
});
return { text: replaced, documents };
};
/** A fence body as a document, or null when it is not one. Only the shape is
* checked here; svc-ai validated it when it was made. */
export const parseDocument = (body: string): DocumentSpec | null => {
try {
const value: unknown = JSON.parse(body);
if (
value &&
typeof value === "object" &&
typeof (value as DocumentSpec).title === "string"
)
return value as DocumentSpec;
} catch {
// Not JSON — drawn as nothing rather than as a broken card.
}
return null;
};
const ASSET_ID = /^[0-9a-f]{64}$/;
const isDocumentImage = (value: unknown): value is DocumentImage => {
const image = value as DocumentImage;
return (
!!image &&
typeof image === "object" &&
typeof image.src === "string" &&
image.src.startsWith("https://") &&
typeof image.asset === "string" &&
ASSET_ID.test(image.asset) &&
typeof image.width === "number" &&
typeof image.height === "number"
);
};
/** A document's stored images that are well formed — a fence is stored data,
* so anything else is drawn as its alt text. */
export const documentImages = (document: DocumentSpec): DocumentImage[] =>
Array.isArray(document.images) ? document.images.filter(isDocumentImage) : [];
/**
* The form an image link is compared in. svc-ai records a link as
* markdown-it normalised it and react-markdown normalises its own copy; both
* percent-encode, so the decoded, NFC form of each is the same link.
*/
export const imageKey = (src: string): string => {
let decoded = src;
try {
decoded = decodeURI(src);
} catch {
// A malformed escape: compare it as written.
}
return decoded.normalize("NFC");
};
/** The stored copy of the image a body links as `src`, if there is one. */
export const findDocumentImage = (
images: DocumentImage[],
src: string | undefined,
): DocumentImage | undefined => {
if (!src) return undefined;
const key = imageKey(src);
return images.find((image) => imageKey(image.src) === key);
};
/**
* What an image that cannot be shown reads as — the PDF says the same
* (svc-ai `documents/pdf.py`, `unavailable_image`).
*/
export const unavailableImage = (alt?: string): string => {
const shown = (alt ?? "").trim();
return shown ? `Image not available: ${shown}` : "Image not available";
};
|