mirror of
https://github.com/bitbaum/chatkit.git
synced 2026-12-04 04:47:19 +01:00
"Who produces gallium?" now carries a ranked bar chart, not a list. chatkit
stays dependency-free: it defines the data contract (`ChartBlock`, mirroring
@bitbaum/chartkit's props for line, bars, share, timeline, tiles) and a
renderer slot (`renderers={{ chart }}` on ChatScreen, ChatThread, ChatMessage,
Markdown and RichAnswerBlock); the app injects the drawing.
- Parser: each kind validated field by field (bounded lists, finite numbers,
safe links). A malformed block is rejected by the parser and degrades to
its caption and a table of the safe rows; without a renderer, or when one
returns null, chatkit draws that table itself. A chart is never a blank.
- Instruction: when to draw (ranked 3+ rows -> bars, shares -> share, 3+
points over time -> line, 3+ dated events -> timeline, 2-4 figures ->
tiles), figures only from tool results, hrefs copied from the result.
- Copy and Listen read the figures as lines, never the wire JSON.
- Tests: node tests per kind, the fallback and the renderer paths; the
browser test renders a bars block through a stub renderer and a line block
through the fallback in demo/screen.html at 320/390/1280, with the
broken-word scan and no horizontal overflow.
Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
115 lines
3.6 KiB
TypeScript
115 lines
3.6 KiB
TypeScript
/**
|
||
* Charts in answers (0.15). chatkit defines the DATA contract and a renderer
|
||
* slot; the drawing is injected by the app (`renderers={{ chart }}` on
|
||
* `ChatThread`, `ChatScreen`, `ChatMessage`, `Markdown` or `RichAnswerBlock`).
|
||
* Substrata hands the block to `@bitbaum/chartkit`; the shapes here mirror
|
||
* chartkit's props exactly, so a block spreads into the matching component:
|
||
*
|
||
* chart: (b) => b.chart === "bars" ? <BarChart {...b} /> : …
|
||
*
|
||
* Without a renderer, or when one returns null, chatkit draws the caption and
|
||
* a table of the data itself — a chart is never a blank.
|
||
*/
|
||
export type ChartPoint = {
|
||
/** A year, a date or a number along the x axis. */
|
||
x: string | number;
|
||
y: number;
|
||
href?: string;
|
||
note?: string;
|
||
};
|
||
export type ChartSeries = {
|
||
id: string;
|
||
label: string;
|
||
points: ChartPoint[];
|
||
};
|
||
export type ChartRow = {
|
||
label: string;
|
||
value: number;
|
||
href?: string;
|
||
/** Percent of the whole, 0–100. */
|
||
share?: number;
|
||
note?: string;
|
||
estimated?: boolean;
|
||
};
|
||
export type ShareRow = {
|
||
label: string;
|
||
share: number;
|
||
href?: string;
|
||
};
|
||
export type ChartEvent = {
|
||
date: string;
|
||
label: string;
|
||
href?: string;
|
||
tone?: "up" | "down" | "neutral";
|
||
kind?: string;
|
||
};
|
||
export type ChartTile = {
|
||
label: string;
|
||
value: string | number;
|
||
href?: string;
|
||
note?: string;
|
||
};
|
||
/** One of chartkit's five figures, as data. */
|
||
export type ChartBlock = {
|
||
type: "chart";
|
||
chart: "line";
|
||
caption: string;
|
||
unit?: string;
|
||
series: ChartSeries[];
|
||
} | {
|
||
type: "chart";
|
||
chart: "bars";
|
||
caption: string;
|
||
unit?: string;
|
||
rows: ChartRow[];
|
||
/** The label of the row to draw in the accent (the reader's country, the answer). */
|
||
highlight?: string;
|
||
} | {
|
||
type: "chart";
|
||
chart: "share";
|
||
caption: string;
|
||
rows: ShareRow[];
|
||
/** The share left over, 0–100, drawn as "Other". */
|
||
other?: number;
|
||
} | {
|
||
type: "chart";
|
||
chart: "timeline";
|
||
caption: string;
|
||
events: ChartEvent[];
|
||
} | {
|
||
type: "chart";
|
||
chart: "tiles";
|
||
caption: string;
|
||
tiles: ChartTile[];
|
||
};
|
||
export type ChartKind = ChartBlock["chart"];
|
||
export declare const CHART_KINDS: readonly ["line", "bars", "share", "timeline", "tiles"];
|
||
/** Same-origin paths and web URLs only; a link in a chart is a link, never markup. */
|
||
export declare function safeChartHref(value: unknown): value is string;
|
||
/**
|
||
* A chart block, validated field by field against chartkit's shapes: bounded
|
||
* lists, finite numbers, safe links. Anything else is null — the caller then
|
||
* degrades it with `chartFallback` rather than dropping it.
|
||
*/
|
||
export declare function parseChartBlock(b: unknown): ChartBlock | null;
|
||
/** The data of a chart as a table: what chatkit draws without a renderer,
|
||
* and what Copy and Listen read. */
|
||
export type ChartTable = {
|
||
caption: string;
|
||
head: string[];
|
||
rows: {
|
||
cells: string[];
|
||
href?: string;
|
||
}[];
|
||
};
|
||
/**
|
||
* A table of whatever a chart block holds — valid or not. A malformed block
|
||
* (a share of 140, a link with a script in it, forty rows) still has a caption
|
||
* and some rows a reader can use, so the answer shows those instead of a hole.
|
||
* Only the safe parts travel: string labels, finite numbers, safe links.
|
||
* Partial JSON, or anything that is not a chart with a caption, is null.
|
||
*/
|
||
export declare function chartFallback(raw: string | unknown): ChartTable | null;
|
||
/** Copy, share and voice get the figures as lines, never the wire JSON. */
|
||
export declare function chartText(block: ChartBlock): string;
|
||
//# sourceMappingURL=chart.d.ts.map
|