pixelize-design-library 3.1.1 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/COMPONENT-GOTCHAS.md +33 -0
- package/DESIGN-GUIDE.md +27 -0
- package/README.md +8 -8
- package/dist/Components/Assistant/assistantModel.d.ts +17 -0
- package/dist/Components/Assistant/assistantModel.js +35 -0
- package/dist/Components/Assistant/assistantStyles.d.ts +27 -0
- package/dist/Components/Assistant/assistantStyles.js +53 -0
- package/dist/Components/Assistant/assistantTypes.d.ts +163 -0
- package/dist/Components/Assistant/assistantTypes.js +2 -0
- package/dist/Components/Assistant/banner/AssistantStatusBanner.d.ts +3 -0
- package/dist/Components/Assistant/banner/AssistantStatusBanner.js +25 -0
- package/dist/Components/Assistant/banner/AssistantStatusBannerProps.d.ts +27 -0
- package/dist/Components/Assistant/banner/AssistantStatusBannerProps.js +2 -0
- package/dist/Components/Assistant/cards/AssistantApprovalCard.d.ts +3 -0
- package/dist/Components/Assistant/cards/AssistantApprovalCard.js +51 -0
- package/dist/Components/Assistant/cards/AssistantApprovalCardProps.d.ts +66 -0
- package/dist/Components/Assistant/cards/AssistantApprovalCardProps.js +2 -0
- package/dist/Components/Assistant/cards/AssistantProposalCard.d.ts +3 -0
- package/dist/Components/Assistant/cards/AssistantProposalCard.js +36 -0
- package/dist/Components/Assistant/cards/AssistantProposalCardProps.d.ts +56 -0
- package/dist/Components/Assistant/cards/AssistantProposalCardProps.js +2 -0
- package/dist/Components/Assistant/cards/CardShell.d.ts +31 -0
- package/dist/Components/Assistant/cards/CardShell.js +32 -0
- package/dist/Components/Assistant/composer/AssistantComposer.d.ts +3 -0
- package/dist/Components/Assistant/composer/AssistantComposer.js +264 -0
- package/dist/Components/Assistant/composer/AssistantComposerProps.d.ts +128 -0
- package/dist/Components/Assistant/composer/AssistantComposerProps.js +2 -0
- package/dist/Components/Assistant/composer/ComposerMenu.d.ts +24 -0
- package/dist/Components/Assistant/composer/ComposerMenu.js +38 -0
- package/dist/Components/Assistant/composer/composerLogic.d.ts +19 -0
- package/dist/Components/Assistant/composer/composerLogic.js +65 -0
- package/dist/Components/Assistant/context/AssistantContextBar.d.ts +3 -0
- package/dist/Components/Assistant/context/AssistantContextBar.js +27 -0
- package/dist/Components/Assistant/context/AssistantContextBarProps.d.ts +34 -0
- package/dist/Components/Assistant/context/AssistantContextBarProps.js +2 -0
- package/dist/Components/Assistant/history/AssistantHistoryList.d.ts +3 -0
- package/dist/Components/Assistant/history/AssistantHistoryList.js +123 -0
- package/dist/Components/Assistant/history/AssistantHistoryListProps.d.ts +83 -0
- package/dist/Components/Assistant/history/AssistantHistoryListProps.js +2 -0
- package/dist/Components/Assistant/history/groupConversations.d.ts +12 -0
- package/dist/Components/Assistant/history/groupConversations.js +55 -0
- package/dist/Components/Assistant/index.d.ts +16 -0
- package/dist/Components/Assistant/index.js +38 -0
- package/dist/Components/Assistant/launcher/AssistantLauncher.d.ts +3 -0
- package/dist/Components/Assistant/launcher/AssistantLauncher.js +45 -0
- package/dist/Components/Assistant/launcher/AssistantLauncherProps.d.ts +37 -0
- package/dist/Components/Assistant/launcher/AssistantLauncherProps.js +2 -0
- package/dist/Components/Assistant/markdown/AssistantMarkdown.d.ts +3 -0
- package/dist/Components/Assistant/markdown/AssistantMarkdown.js +146 -0
- package/dist/Components/Assistant/markdown/AssistantMarkdownProps.d.ts +38 -0
- package/dist/Components/Assistant/markdown/AssistantMarkdownProps.js +2 -0
- package/dist/Components/Assistant/markdown/parseMarkdown.d.ts +74 -0
- package/dist/Components/Assistant/markdown/parseMarkdown.js +328 -0
- package/dist/Components/Assistant/markdown/safeHref.d.ts +21 -0
- package/dist/Components/Assistant/markdown/safeHref.js +28 -0
- package/dist/Components/Assistant/message/AssistantMessage.d.ts +3 -0
- package/dist/Components/Assistant/message/AssistantMessage.js +131 -0
- package/dist/Components/Assistant/message/AssistantMessageProps.d.ts +83 -0
- package/dist/Components/Assistant/message/AssistantMessageProps.js +2 -0
- package/dist/Components/Assistant/message/MessageActions.d.ts +16 -0
- package/dist/Components/Assistant/message/MessageActions.js +23 -0
- package/dist/Components/Assistant/panel/AssistantPanel.d.ts +3 -0
- package/dist/Components/Assistant/panel/AssistantPanel.js +87 -0
- package/dist/Components/Assistant/panel/AssistantPanelProps.d.ts +86 -0
- package/dist/Components/Assistant/panel/AssistantPanelProps.js +2 -0
- package/dist/Components/Assistant/panel/ResizeHandle.d.ts +14 -0
- package/dist/Components/Assistant/panel/ResizeHandle.js +72 -0
- package/dist/Components/Assistant/sources/AssistantSources.d.ts +3 -0
- package/dist/Components/Assistant/sources/AssistantSources.js +63 -0
- package/dist/Components/Assistant/sources/AssistantSourcesProps.d.ts +29 -0
- package/dist/Components/Assistant/sources/AssistantSourcesProps.js +2 -0
- package/dist/Components/Assistant/steps/AssistantToolSteps.d.ts +3 -0
- package/dist/Components/Assistant/steps/AssistantToolSteps.js +41 -0
- package/dist/Components/Assistant/steps/AssistantToolStepsProps.d.ts +37 -0
- package/dist/Components/Assistant/steps/AssistantToolStepsProps.js +2 -0
- package/dist/Components/Assistant/suggestions/AssistantSuggestions.d.ts +3 -0
- package/dist/Components/Assistant/suggestions/AssistantSuggestions.js +18 -0
- package/dist/Components/Assistant/suggestions/AssistantSuggestionsProps.d.ts +15 -0
- package/dist/Components/Assistant/suggestions/AssistantSuggestionsProps.js +2 -0
- package/dist/Components/Assistant/thread/AssistantThread.d.ts +3 -0
- package/dist/Components/Assistant/thread/AssistantThread.js +158 -0
- package/dist/Components/Assistant/thread/AssistantThreadProps.d.ts +39 -0
- package/dist/Components/Assistant/thread/AssistantThreadProps.js +2 -0
- package/dist/Components/Assistant/thread/replyStartScroll.d.ts +8 -0
- package/dist/Components/Assistant/thread/replyStartScroll.js +6 -0
- package/dist/Components/Assistant/typing/AssistantTypingIndicator.d.ts +3 -0
- package/dist/Components/Assistant/typing/AssistantTypingIndicator.js +33 -0
- package/dist/Components/Assistant/typing/AssistantTypingIndicatorProps.d.ts +9 -0
- package/dist/Components/Assistant/typing/AssistantTypingIndicatorProps.js +2 -0
- package/dist/Components/Assistant/welcome/AssistantWelcome.d.ts +3 -0
- package/dist/Components/Assistant/welcome/AssistantWelcome.js +30 -0
- package/dist/Components/Assistant/welcome/AssistantWelcomeProps.d.ts +40 -0
- package/dist/Components/Assistant/welcome/AssistantWelcomeProps.js +2 -0
- package/dist/esm/Components/Assistant/assistantModel.js +29 -0
- package/dist/esm/Components/Assistant/assistantStyles.js +47 -0
- package/dist/esm/Components/Assistant/assistantTypes.js +1 -0
- package/dist/esm/Components/Assistant/banner/AssistantStatusBanner.js +20 -0
- package/dist/esm/Components/Assistant/banner/AssistantStatusBannerProps.js +1 -0
- package/dist/esm/Components/Assistant/cards/AssistantApprovalCard.js +46 -0
- package/dist/esm/Components/Assistant/cards/AssistantApprovalCardProps.js +1 -0
- package/dist/esm/Components/Assistant/cards/AssistantProposalCard.js +31 -0
- package/dist/esm/Components/Assistant/cards/AssistantProposalCardProps.js +1 -0
- package/dist/esm/Components/Assistant/cards/CardShell.js +23 -0
- package/dist/esm/Components/Assistant/composer/AssistantComposer.js +259 -0
- package/dist/esm/Components/Assistant/composer/AssistantComposerProps.js +1 -0
- package/dist/esm/Components/Assistant/composer/ComposerMenu.js +30 -0
- package/dist/esm/Components/Assistant/composer/composerLogic.js +56 -0
- package/dist/esm/Components/Assistant/context/AssistantContextBar.js +22 -0
- package/dist/esm/Components/Assistant/context/AssistantContextBarProps.js +1 -0
- package/dist/esm/Components/Assistant/history/AssistantHistoryList.js +118 -0
- package/dist/esm/Components/Assistant/history/AssistantHistoryListProps.js +1 -0
- package/dist/esm/Components/Assistant/history/groupConversations.js +51 -0
- package/dist/esm/Components/Assistant/index.js +16 -0
- package/dist/esm/Components/Assistant/launcher/AssistantLauncher.js +40 -0
- package/dist/esm/Components/Assistant/launcher/AssistantLauncherProps.js +1 -0
- package/dist/esm/Components/Assistant/markdown/AssistantMarkdown.js +141 -0
- package/dist/esm/Components/Assistant/markdown/AssistantMarkdownProps.js +1 -0
- package/dist/esm/Components/Assistant/markdown/parseMarkdown.js +322 -0
- package/dist/esm/Components/Assistant/markdown/safeHref.js +23 -0
- package/dist/esm/Components/Assistant/message/AssistantMessage.js +126 -0
- package/dist/esm/Components/Assistant/message/AssistantMessageProps.js +1 -0
- package/dist/esm/Components/Assistant/message/MessageActions.js +16 -0
- package/dist/esm/Components/Assistant/panel/AssistantPanel.js +82 -0
- package/dist/esm/Components/Assistant/panel/AssistantPanelProps.js +1 -0
- package/dist/esm/Components/Assistant/panel/ResizeHandle.js +68 -0
- package/dist/esm/Components/Assistant/sources/AssistantSources.js +58 -0
- package/dist/esm/Components/Assistant/sources/AssistantSourcesProps.js +1 -0
- package/dist/esm/Components/Assistant/steps/AssistantToolSteps.js +36 -0
- package/dist/esm/Components/Assistant/steps/AssistantToolStepsProps.js +1 -0
- package/dist/esm/Components/Assistant/suggestions/AssistantSuggestions.js +13 -0
- package/dist/esm/Components/Assistant/suggestions/AssistantSuggestionsProps.js +1 -0
- package/dist/esm/Components/Assistant/thread/AssistantThread.js +153 -0
- package/dist/esm/Components/Assistant/thread/AssistantThreadProps.js +1 -0
- package/dist/esm/Components/Assistant/thread/replyStartScroll.js +2 -0
- package/dist/esm/Components/Assistant/typing/AssistantTypingIndicator.js +28 -0
- package/dist/esm/Components/Assistant/typing/AssistantTypingIndicatorProps.js +1 -0
- package/dist/esm/Components/Assistant/welcome/AssistantWelcome.js +25 -0
- package/dist/esm/Components/Assistant/welcome/AssistantWelcomeProps.js +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +20 -1
- package/package.json +1 -1
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { Fragment, memo, useMemo, } from "react";
|
|
3
|
+
import { Box, Flex, Text, chakra } from "@chakra-ui/react";
|
|
4
|
+
import { CheckSquare, ExternalLink, Square } from "lucide-react";
|
|
5
|
+
import { useCustomTheme } from "../../../Theme/useCustomTheme.js";
|
|
6
|
+
import { focusVisibleStyles } from "../../../Theme/chakra/focusRing.styles.js";
|
|
7
|
+
import { scrollbarStyles } from "../../../Theme/tokens/builders/scrollbar.js";
|
|
8
|
+
import { bandOn } from "../../../Theme/tokens/builders/planeStep.js";
|
|
9
|
+
import { BORDER_WIDTHS } from "../../../Theme/tokens/primitives/borderWidths.js";
|
|
10
|
+
import { RADIUS_ROLE } from "../../../Theme/tokens/primitives/radii.js";
|
|
11
|
+
import { useScrollableRegion } from "../../../Hooks/useScrollableRegion.js";
|
|
12
|
+
import CopyButton from "../../CopyButton/CopyButton.js";
|
|
13
|
+
import VisuallyHidden from "../../VisuallyHidden/VisuallyHidden.js";
|
|
14
|
+
import ScrollbarBox from "../../ScrollbarBox/ScrollbarBox.js";
|
|
15
|
+
import { assistantTokens, StreamingCaret } from "../assistantStyles.js";
|
|
16
|
+
import { parseMarkdown, } from "./parseMarkdown.js";
|
|
17
|
+
import { anchorAttributes, resolveSafeHref } from "./safeHref.js";
|
|
18
|
+
const DEFAULT_LABELS = {
|
|
19
|
+
copyCode: "Copy code",
|
|
20
|
+
copied: "Copied",
|
|
21
|
+
image: (alt) => (alt ? `[image: ${alt}]` : "[image]"),
|
|
22
|
+
newTab: "opens in a new tab",
|
|
23
|
+
table: "Table",
|
|
24
|
+
taskDone: "Done",
|
|
25
|
+
taskOpen: "Not done",
|
|
26
|
+
code: (lang) => (lang ? `${lang} code` : "Code"),
|
|
27
|
+
};
|
|
28
|
+
const renderInline = (nodes, ctx, keyPrefix = "i") => nodes.map((node, i) => {
|
|
29
|
+
const key = `${keyPrefix}-${i}`;
|
|
30
|
+
switch (node.type) {
|
|
31
|
+
case "text":
|
|
32
|
+
return _jsx(Fragment, { children: node.content }, key);
|
|
33
|
+
case "break":
|
|
34
|
+
return _jsx("br", {}, key);
|
|
35
|
+
case "code":
|
|
36
|
+
return (_jsx(Box, { as: "code", fontFamily: "mono", fontSize: "0.9em", px: 1, py: "0.0625em", borderRadius: "base", bg: ctx.tokens.inset, color: ctx.tokens.heading, overflowWrap: "anywhere", children: node.content }, key));
|
|
37
|
+
case "strong":
|
|
38
|
+
return (_jsx(Box, { as: "strong", fontWeight: 600, color: ctx.tokens.heading, children: renderInline(node.children, ctx, key) }, key));
|
|
39
|
+
case "em":
|
|
40
|
+
return (_jsx(Box, { as: "em", fontStyle: "italic", children: renderInline(node.children, ctx, key) }, key));
|
|
41
|
+
case "strike":
|
|
42
|
+
return (_jsx(Box, { as: "del", textDecoration: "line-through", children: renderInline(node.children, ctx, key) }, key));
|
|
43
|
+
case "image":
|
|
44
|
+
return (_jsx(Box, { as: "span", color: ctx.tokens.muted, "data-testid": "assistant-md-image", children: ctx.labels.image(node.alt) }, key));
|
|
45
|
+
case "link": {
|
|
46
|
+
const safe = resolveSafeHref(node.href);
|
|
47
|
+
const children = renderInline(node.children, ctx, key);
|
|
48
|
+
if (!safe) {
|
|
49
|
+
return (_jsx(Box, { as: "span", "data-unsafe-link": "true", children: children }, key));
|
|
50
|
+
}
|
|
51
|
+
return (_jsxs(Box, { as: "a", href: safe.href, ...anchorAttributes(safe), color: ctx.tokens.link, textDecoration: "underline", textUnderlineOffset: "0.15em", borderRadius: "sm", overflowWrap: "anywhere", sx: ctx.focusRing, onClick: (event) => { var _a; return (_a = ctx.onLinkClick) === null || _a === void 0 ? void 0 : _a.call(ctx, safe.href, event); }, children: [children, safe.external ? (_jsxs(_Fragment, { children: [_jsx(Box, { as: ExternalLink, "aria-hidden": "true", display: "inline", boxSize: "0.8em", ml: "0.15em", verticalAlign: "baseline" }), _jsxs(VisuallyHidden, { children: ["(", ctx.labels.newTab, ")"] })] })) : null] }, key));
|
|
52
|
+
}
|
|
53
|
+
default:
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
const HEADING_SIZE = {
|
|
58
|
+
1: "md",
|
|
59
|
+
2: "md",
|
|
60
|
+
3: "sm",
|
|
61
|
+
4: "sm",
|
|
62
|
+
5: "sm",
|
|
63
|
+
6: "sm",
|
|
64
|
+
};
|
|
65
|
+
const CodeBlock = ({ lang, content, ctx, }) => {
|
|
66
|
+
const { colors } = useCustomTheme();
|
|
67
|
+
const region = useScrollableRegion({
|
|
68
|
+
label: ctx.labels.code(lang),
|
|
69
|
+
});
|
|
70
|
+
return (_jsxs(Box, { position: "relative", borderWidth: BORDER_WIDTHS.hairline, borderColor: ctx.tokens.rule, borderRadius: RADIUS_ROLE.media, bg: ctx.tokens.inset, overflow: "hidden", "data-testid": "assistant-md-code", children: [_jsxs(Flex, { align: "center", justify: "space-between", pl: 3, pr: 1, py: 0.5, borderBottomWidth: BORDER_WIDTHS.hairline, borderColor: ctx.tokens.rule, children: [_jsx(Text, { fontSize: "xs", fontFamily: "mono", color: ctx.tokens.muted, children: lang || " " }), _jsx(CopyButton, { value: content, size: "xs", iconSize: 14, tooltip: ctx.labels.copyCode, copiedLabel: ctx.labels.copied })] }), _jsx(Box, { as: "pre", ref: region.ref, ...region.props, m: 0, px: 3, py: 2.5, overflowX: "auto", fontFamily: "mono", fontSize: "xs-plus", lineHeight: 1.6, color: ctx.tokens.heading, sx: { ...scrollbarStyles(colors, "overlay"), ...ctx.focusRing }, children: _jsx("code", { children: content }) })] }));
|
|
71
|
+
};
|
|
72
|
+
const renderBlocks = (blocks, ctx, caret, keyPrefix = "b") => blocks.map((block, i) => {
|
|
73
|
+
const key = `${keyPrefix}-${i}`;
|
|
74
|
+
const isLast = i === blocks.length - 1;
|
|
75
|
+
const withCaret = caret && isLast ? _jsx(StreamingCaret, {}) : null;
|
|
76
|
+
switch (block.type) {
|
|
77
|
+
case "heading": {
|
|
78
|
+
const level = Math.min(6, ctx.headingLevel + block.level - 1);
|
|
79
|
+
return (_jsxs(Text, { as: `h${level}`, fontSize: HEADING_SIZE[block.level], fontWeight: 600, lineHeight: 1.4, color: ctx.tokens.heading, children: [renderInline(block.children, ctx, key), withCaret] }, key));
|
|
80
|
+
}
|
|
81
|
+
case "paragraph":
|
|
82
|
+
return (_jsxs(Text, { as: "p", children: [renderInline(block.children, ctx, key), withCaret] }, key));
|
|
83
|
+
case "code":
|
|
84
|
+
return (_jsxs(Fragment, { children: [_jsx(CodeBlock, { lang: block.lang, content: block.content, ctx: ctx }), withCaret] }, key));
|
|
85
|
+
case "blockquote":
|
|
86
|
+
return (_jsx(Box, { as: "blockquote", pl: 3, borderInlineStartWidth: BORDER_WIDTHS.thick, borderInlineStartColor: ctx.tokens.rule, color: ctx.tokens.muted, className: "assistant-md-flow", children: renderBlocks(block.blocks, ctx, caret && isLast, key) }, key));
|
|
87
|
+
case "list": {
|
|
88
|
+
const isTaskList = block.items.some((item) => item.checked !== null);
|
|
89
|
+
const listProps = {
|
|
90
|
+
pl: isTaskList ? 0 : 5,
|
|
91
|
+
listStyleType: isTaskList ? "none" : undefined,
|
|
92
|
+
sx: { "& > li + li": { mt: 1 } },
|
|
93
|
+
};
|
|
94
|
+
const items = block.items.map((item, j) => {
|
|
95
|
+
const itemKey = `${key}-${j}`;
|
|
96
|
+
const lastItem = caret && isLast && j === block.items.length - 1;
|
|
97
|
+
const body = (_jsxs(_Fragment, { children: [renderInline(item.children, ctx, itemKey), lastItem && item.blocks.length === 0 ? _jsx(StreamingCaret, {}) : null, item.blocks.length ? (_jsx(Box, { mt: 1, className: "assistant-md-flow", children: renderBlocks(item.blocks, ctx, lastItem, itemKey) })) : null] }));
|
|
98
|
+
if (item.checked === null) {
|
|
99
|
+
return _jsx("li", { children: body }, itemKey);
|
|
100
|
+
}
|
|
101
|
+
const Icon = item.checked ? CheckSquare : Square;
|
|
102
|
+
return (_jsxs(Flex, { as: "li", gap: 2, align: "flex-start", children: [_jsx(Box, { as: Icon, "aria-hidden": "true", boxSize: "1em", mt: "0.2em", flexShrink: 0, color: item.checked ? ctx.tokens.link : ctx.tokens.muted }), _jsxs(VisuallyHidden, { children: [item.checked ? ctx.labels.taskDone : ctx.labels.taskOpen, ":", " "] }), _jsx(Box, { minW: 0, textDecoration: item.checked ? "line-through" : undefined, color: item.checked ? ctx.tokens.muted : undefined, children: body })] }, itemKey));
|
|
103
|
+
});
|
|
104
|
+
return block.ordered ? (_jsx(chakra.ol, { start: block.start !== 1 ? block.start : undefined, ...listProps, children: items }, key)) : (_jsx(chakra.ul, { ...listProps, children: items }, key));
|
|
105
|
+
}
|
|
106
|
+
case "rule":
|
|
107
|
+
return (_jsxs(Fragment, { children: [_jsx(Box, { as: "hr", borderTopWidth: BORDER_WIDTHS.hairline, borderColor: ctx.tokens.rule }), withCaret] }, key));
|
|
108
|
+
case "table":
|
|
109
|
+
return (_jsxs(Fragment, { children: [_jsx(Box, { borderWidth: BORDER_WIDTHS.hairline, borderColor: ctx.tokens.rule, borderRadius: RADIUS_ROLE.media, overflow: "hidden", children: _jsx(ScrollbarBox, { overflowX: "auto", overflowY: "hidden", variant: "overlay", "aria-label": ctx.labels.table, children: _jsxs(Box, { as: "table", w: "100%", fontSize: "xs-plus", sx: { borderCollapse: "collapse" }, children: [_jsx("thead", { children: _jsx("tr", { children: block.header.map((cell, c) => {
|
|
110
|
+
var _a;
|
|
111
|
+
return (_jsx(Box, { as: "th", px: 3, py: 1.5, textAlign: (_a = block.align[c]) !== null && _a !== void 0 ? _a : "start", fontWeight: 600, color: ctx.tokens.heading, bg: ctx.tableHeader, borderBottomWidth: BORDER_WIDTHS.hairline, borderColor: ctx.tokens.rule, whiteSpace: "nowrap", children: renderInline(cell, ctx, `${key}-h-${c}`) }, `${key}-h-${c}`));
|
|
112
|
+
}) }) }), _jsx("tbody", { children: block.rows.map((row, r) => (_jsx("tr", { children: row.map((cell, c) => {
|
|
113
|
+
var _a;
|
|
114
|
+
return (_jsx(Box, { as: "td", px: 3, py: 1.5, textAlign: (_a = block.align[c]) !== null && _a !== void 0 ? _a : "start", verticalAlign: "top", borderTopWidth: r === 0 ? 0 : BORDER_WIDTHS.hairline, borderColor: ctx.tokens.rule, children: renderInline(cell, ctx, `${key}-r-${r}-${c}`) }, `${key}-r-${r}-${c}`));
|
|
115
|
+
}) }, `${key}-r-${r}`))) })] }) }) }), withCaret] }, key));
|
|
116
|
+
default:
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
const FLOW = { "& > * + *": { mt: 3 } };
|
|
121
|
+
/**
|
|
122
|
+
* Renders model markdown safely: headings, emphasis, inline and fenced code (with Copy), ordered, unordered
|
|
123
|
+
* and task lists, quotes, pipe tables, rules and links. Never renders HTML or an element that loads a
|
|
124
|
+
* remote resource — an image becomes `[image: alt]` text — and only http(s), mailto and app-relative
|
|
125
|
+
* links become anchors. External links open a new tab with `rel="noopener noreferrer nofollow"`.
|
|
126
|
+
*/
|
|
127
|
+
const AssistantMarkdown = ({ content, onLinkClick, showCaret = false, headingLevel = 3, labels, "data-testid": testId = "assistant-markdown", }) => {
|
|
128
|
+
const { colors } = useCustomTheme();
|
|
129
|
+
const blocks = useMemo(() => parseMarkdown(content), [content]);
|
|
130
|
+
const tokens = assistantTokens(colors);
|
|
131
|
+
const ctx = {
|
|
132
|
+
tokens,
|
|
133
|
+
labels: { ...DEFAULT_LABELS, ...labels },
|
|
134
|
+
headingLevel,
|
|
135
|
+
onLinkClick,
|
|
136
|
+
focusRing: focusVisibleStyles(colors),
|
|
137
|
+
tableHeader: bandOn(colors.backgroundColor.tableHeader, tokens.surface),
|
|
138
|
+
};
|
|
139
|
+
return (_jsxs(Box, { "data-testid": testId, className: "assistant-md-flow", color: tokens.body, fontSize: "sm", lineHeight: 1.6, overflowWrap: "break-word", minW: 0, sx: { ...FLOW, "& .assistant-md-flow": FLOW }, children: [renderBlocks(blocks, ctx, showCaret), showCaret && blocks.length === 0 ? _jsx(StreamingCaret, {}) : null] }));
|
|
140
|
+
};
|
|
141
|
+
export default memo(AssistantMarkdown);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small hand-rolled markdown parser for model output: no dependencies, no HTML, never throws.
|
|
3
|
+
* Ported from Mobile-Components `src/modules/markdown/parseMarkdown.ts` and extended with nesting
|
|
4
|
+
* (lists and quotes), images (as inert alt text), autolinks, escapes, ordered-list starts, table
|
|
5
|
+
* alignment and hard line breaks. Malformed input degrades to literal text; an unclosed fence swallows
|
|
6
|
+
* the rest of the input as code, which is what a half-streamed answer looks like.
|
|
7
|
+
*/
|
|
8
|
+
const MAX_DEPTH = 6;
|
|
9
|
+
const HEADING_RE = /^ {0,3}(#{1,6})(?:\s+(.*?))?\s*#*\s*$/;
|
|
10
|
+
const FENCE_RE = /^ {0,3}(`{3,}|~{3,})\s*([^`\s]*)/;
|
|
11
|
+
const RULE_RE = /^ {0,3}([-*_])(?:\s*\1){2,}\s*$/;
|
|
12
|
+
const QUOTE_RE = /^ {0,3}>\s?(.*)$/;
|
|
13
|
+
const ITEM_RE = /^(\s*)([-*+]|\d{1,9}[.)])\s+(.*)$/;
|
|
14
|
+
const TASK_RE = /^\[([ xX])\]\s+(.*)$/;
|
|
15
|
+
const TABLE_DIVIDER_RE = /^\s*\|?\s*:?-+:?\s*(?:\|\s*:?-+:?\s*)*\|?\s*$/;
|
|
16
|
+
// Order matters: escapes and code spans win over everything; images before links. A URL may hold one
|
|
17
|
+
// level of balanced parentheses (Wikipedia-style).
|
|
18
|
+
const INLINE_RE = new RegExp([
|
|
19
|
+
String.raw `\\([\\\x60*_{}\[\]()#+\-.!~|>])`,
|
|
20
|
+
String.raw `(\x60+)([^\x60\n]|[^\x60\n][\s\S]*?[^\x60\n])\2(?!\x60)`,
|
|
21
|
+
String.raw `!\[([^\]\n]*)\]\(\s*((?:[^()\s]|\([^()\s]*\))*)(?:\s+"[^"\n]*")?\s*\)`,
|
|
22
|
+
String.raw `\[([^\]\n]+)\]\(\s*((?:[^()\s]|\([^()\s]*\))*)(?:\s+"[^"\n]*")?\s*\)`,
|
|
23
|
+
String.raw `<((?:https?:\/\/|mailto:)[^\s<>]+)>`,
|
|
24
|
+
String.raw `(https?:\/\/[^\s<>()\[\]]*[^\s<>()\[\].,;:!?'"*_~])`,
|
|
25
|
+
String.raw `~~(?=\S)([^~\n]+?)~~`,
|
|
26
|
+
String.raw `\*\*(?=\S)([^\n]+?)\*\*`,
|
|
27
|
+
String.raw `(?<![\w])__(?=\S)([^\n]+?)__(?![\w])`,
|
|
28
|
+
String.raw `\*(?=[^\s*])([^*\n]+?)\*`,
|
|
29
|
+
String.raw `(?<![\w])_(?=[^\s_])([^_\n]+?)_(?![\w])`,
|
|
30
|
+
].join("|"), "g");
|
|
31
|
+
const mergeText = (nodes) => nodes.reduce((out, node) => {
|
|
32
|
+
const last = out[out.length - 1];
|
|
33
|
+
if (node.type === "text" && (last === null || last === void 0 ? void 0 : last.type) === "text") {
|
|
34
|
+
out[out.length - 1] = { type: "text", content: last.content + node.content };
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
out.push(node);
|
|
38
|
+
}
|
|
39
|
+
return out;
|
|
40
|
+
}, []);
|
|
41
|
+
const parseSpan = (text, depth) => {
|
|
42
|
+
var _a, _b;
|
|
43
|
+
if (depth > MAX_DEPTH)
|
|
44
|
+
return [{ type: "text", content: text }];
|
|
45
|
+
const nodes = [];
|
|
46
|
+
const re = new RegExp(INLINE_RE.source, "g");
|
|
47
|
+
let last = 0;
|
|
48
|
+
for (let match = re.exec(text); match !== null; match = re.exec(text)) {
|
|
49
|
+
if (match[0].length === 0) {
|
|
50
|
+
re.lastIndex += 1;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (match.index > last)
|
|
54
|
+
nodes.push({ type: "text", content: text.slice(last, match.index) });
|
|
55
|
+
const [, escaped, , code, imageAlt, imageSrc, linkText, linkHref, autolink, bareUrl, strike, bold, boldAlt, italic, italicAlt] = match;
|
|
56
|
+
const inner = (content) => parseSpan(content, depth + 1);
|
|
57
|
+
if (escaped !== undefined)
|
|
58
|
+
nodes.push({ type: "text", content: escaped });
|
|
59
|
+
else if (code !== undefined)
|
|
60
|
+
nodes.push({ type: "code", content: code.trim() || code });
|
|
61
|
+
else if (imageSrc !== undefined)
|
|
62
|
+
nodes.push({ type: "image", alt: (imageAlt !== null && imageAlt !== void 0 ? imageAlt : "").trim() });
|
|
63
|
+
else if (linkText !== undefined)
|
|
64
|
+
nodes.push({ type: "link", href: linkHref !== null && linkHref !== void 0 ? linkHref : "", children: inner(linkText) });
|
|
65
|
+
else if (autolink !== undefined)
|
|
66
|
+
nodes.push({ type: "link", href: autolink, children: [{ type: "text", content: autolink }] });
|
|
67
|
+
else if (bareUrl !== undefined)
|
|
68
|
+
nodes.push({ type: "link", href: bareUrl, children: [{ type: "text", content: bareUrl }] });
|
|
69
|
+
else if (strike !== undefined)
|
|
70
|
+
nodes.push({ type: "strike", children: inner(strike) });
|
|
71
|
+
else if (bold !== undefined || boldAlt !== undefined)
|
|
72
|
+
nodes.push({ type: "strong", children: inner((_a = bold !== null && bold !== void 0 ? bold : boldAlt) !== null && _a !== void 0 ? _a : "") });
|
|
73
|
+
else if (italic !== undefined || italicAlt !== undefined)
|
|
74
|
+
nodes.push({ type: "em", children: inner((_b = italic !== null && italic !== void 0 ? italic : italicAlt) !== null && _b !== void 0 ? _b : "") });
|
|
75
|
+
last = match.index + match[0].length;
|
|
76
|
+
}
|
|
77
|
+
if (last < text.length)
|
|
78
|
+
nodes.push({ type: "text", content: text.slice(last) });
|
|
79
|
+
return mergeText(nodes);
|
|
80
|
+
};
|
|
81
|
+
/** Inline markdown of one paragraph's lines; a line break between lines is kept as a `break` node. */
|
|
82
|
+
export const parseInline = (text, depth = 0) => {
|
|
83
|
+
const lines = text.split("\n");
|
|
84
|
+
return mergeText(lines.flatMap((line, i) => {
|
|
85
|
+
const nodes = parseSpan(line.replace(/\\$/, "").trimEnd(), depth);
|
|
86
|
+
return i < lines.length - 1 ? [...nodes, { type: "break" }] : nodes;
|
|
87
|
+
}));
|
|
88
|
+
};
|
|
89
|
+
const splitCells = (line) => {
|
|
90
|
+
const cells = [];
|
|
91
|
+
let cell = "";
|
|
92
|
+
for (let k = 0; k < line.length; k += 1) {
|
|
93
|
+
const ch = line[k];
|
|
94
|
+
if (ch === "\\" && line[k + 1] === "|") {
|
|
95
|
+
cell += "|";
|
|
96
|
+
k += 1;
|
|
97
|
+
}
|
|
98
|
+
else if (ch === "|") {
|
|
99
|
+
cells.push(cell);
|
|
100
|
+
cell = "";
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
cell += ch;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
cells.push(cell);
|
|
107
|
+
const trimmed = line.trim();
|
|
108
|
+
if (trimmed.startsWith("|"))
|
|
109
|
+
cells.shift();
|
|
110
|
+
if (trimmed.endsWith("|") && !trimmed.endsWith("\\|"))
|
|
111
|
+
cells.pop();
|
|
112
|
+
return cells.map((c) => c.trim());
|
|
113
|
+
};
|
|
114
|
+
const alignOf = (cell) => {
|
|
115
|
+
const left = cell.startsWith(":");
|
|
116
|
+
const right = cell.endsWith(":");
|
|
117
|
+
if (left && right)
|
|
118
|
+
return "center";
|
|
119
|
+
if (right)
|
|
120
|
+
return "right";
|
|
121
|
+
if (left)
|
|
122
|
+
return "left";
|
|
123
|
+
return null;
|
|
124
|
+
};
|
|
125
|
+
const indentOf = (line) => {
|
|
126
|
+
var _a, _b;
|
|
127
|
+
const lead = (_b = (_a = /^\s*/.exec(line)) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : "";
|
|
128
|
+
return lead.replace(/\t/g, " ").length;
|
|
129
|
+
};
|
|
130
|
+
const dedent = (lines) => {
|
|
131
|
+
const widths = lines.filter((l) => l.trim() !== "").map(indentOf);
|
|
132
|
+
const min = widths.length ? Math.min(...widths) : 0;
|
|
133
|
+
return lines.map((l) => l.replace(/\t/g, " ").slice(Math.min(min, indentOf(l))));
|
|
134
|
+
};
|
|
135
|
+
const isBlockStart = (line) => FENCE_RE.test(line) || HEADING_RE.test(line) || RULE_RE.test(line) || QUOTE_RE.test(line) || ITEM_RE.test(line);
|
|
136
|
+
const isOrderedMarker = (marker) => /\d/.test(marker);
|
|
137
|
+
const parseBlocks = (lines, depth) => {
|
|
138
|
+
var _a, _b, _c, _d;
|
|
139
|
+
if (depth > MAX_DEPTH)
|
|
140
|
+
return [{ type: "paragraph", children: [{ type: "text", content: lines.join("\n") }] }];
|
|
141
|
+
const blocks = [];
|
|
142
|
+
let paragraph = [];
|
|
143
|
+
const flush = () => {
|
|
144
|
+
if (paragraph.length === 0)
|
|
145
|
+
return;
|
|
146
|
+
blocks.push({ type: "paragraph", children: parseInline(paragraph.join("\n"), 0) });
|
|
147
|
+
paragraph = [];
|
|
148
|
+
};
|
|
149
|
+
let i = 0;
|
|
150
|
+
while (i < lines.length) {
|
|
151
|
+
const line = lines[i];
|
|
152
|
+
const fence = FENCE_RE.exec(line);
|
|
153
|
+
if (fence) {
|
|
154
|
+
flush();
|
|
155
|
+
const marker = fence[1];
|
|
156
|
+
const body = [];
|
|
157
|
+
i += 1;
|
|
158
|
+
// An unclosed fence swallows the rest rather than failing — a streaming answer looks like this.
|
|
159
|
+
while (i < lines.length && !(lines[i].trim().startsWith(marker[0].repeat(marker.length)) && lines[i].trim().replace(/[`~]/g, "") === "")) {
|
|
160
|
+
body.push(lines[i]);
|
|
161
|
+
i += 1;
|
|
162
|
+
}
|
|
163
|
+
i += 1;
|
|
164
|
+
blocks.push({ type: "code", lang: (_a = fence[2]) !== null && _a !== void 0 ? _a : "", content: body.join("\n") });
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const heading = HEADING_RE.exec(line);
|
|
168
|
+
if (heading) {
|
|
169
|
+
flush();
|
|
170
|
+
blocks.push({
|
|
171
|
+
type: "heading",
|
|
172
|
+
level: heading[1].length,
|
|
173
|
+
children: parseInline((_b = heading[2]) !== null && _b !== void 0 ? _b : "", 0),
|
|
174
|
+
});
|
|
175
|
+
i += 1;
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
if (RULE_RE.test(line)) {
|
|
179
|
+
flush();
|
|
180
|
+
blocks.push({ type: "rule" });
|
|
181
|
+
i += 1;
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
const divider = (_c = lines[i + 1]) !== null && _c !== void 0 ? _c : "";
|
|
185
|
+
if (line.includes("|") && divider.includes("|") && TABLE_DIVIDER_RE.test(divider)) {
|
|
186
|
+
flush();
|
|
187
|
+
const header = splitCells(line).map((c) => parseInline(c, 0));
|
|
188
|
+
const align = splitCells(divider).map(alignOf);
|
|
189
|
+
i += 2;
|
|
190
|
+
const rows = [];
|
|
191
|
+
while (i < lines.length && lines[i].trim() !== "" && lines[i].includes("|")) {
|
|
192
|
+
const cells = splitCells(lines[i]).map((c) => parseInline(c, 0));
|
|
193
|
+
rows.push(header.map((_, col) => { var _a; return (_a = cells[col]) !== null && _a !== void 0 ? _a : []; }));
|
|
194
|
+
i += 1;
|
|
195
|
+
}
|
|
196
|
+
blocks.push({ type: "table", align: header.map((_, col) => { var _a; return (_a = align[col]) !== null && _a !== void 0 ? _a : null; }), header, rows });
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
if (QUOTE_RE.test(line)) {
|
|
200
|
+
flush();
|
|
201
|
+
const quoted = [];
|
|
202
|
+
while (i < lines.length) {
|
|
203
|
+
const quote = QUOTE_RE.exec(lines[i]);
|
|
204
|
+
if (quote)
|
|
205
|
+
quoted.push(quote[1]);
|
|
206
|
+
else if (lines[i].trim() !== "" && !isBlockStart(lines[i]) && quoted.length > 0)
|
|
207
|
+
quoted.push(lines[i]);
|
|
208
|
+
else
|
|
209
|
+
break;
|
|
210
|
+
i += 1;
|
|
211
|
+
}
|
|
212
|
+
blocks.push({ type: "blockquote", blocks: parseBlocks(quoted, depth + 1) });
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
const first = ITEM_RE.exec(line);
|
|
216
|
+
if (first) {
|
|
217
|
+
flush();
|
|
218
|
+
const baseIndent = indentOf(first[1]);
|
|
219
|
+
const ordered = isOrderedMarker(first[2]);
|
|
220
|
+
const items = [];
|
|
221
|
+
while (i < lines.length) {
|
|
222
|
+
const item = ITEM_RE.exec(lines[i]);
|
|
223
|
+
if (!item || isOrderedMarker(item[2]) !== ordered || Math.abs(indentOf(item[1]) - baseIndent) > 1)
|
|
224
|
+
break;
|
|
225
|
+
const task = TASK_RE.exec(item[3]);
|
|
226
|
+
const text = [task ? task[2] : item[3]];
|
|
227
|
+
const nested = [];
|
|
228
|
+
i += 1;
|
|
229
|
+
while (i < lines.length) {
|
|
230
|
+
const next = lines[i];
|
|
231
|
+
if (next.trim() === "") {
|
|
232
|
+
let ahead = i;
|
|
233
|
+
while (ahead < lines.length && lines[ahead].trim() === "")
|
|
234
|
+
ahead += 1;
|
|
235
|
+
if (ahead < lines.length && indentOf(lines[ahead]) > baseIndent + 1) {
|
|
236
|
+
nested.push(...lines.slice(i, ahead));
|
|
237
|
+
i = ahead;
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
if (indentOf(next) > baseIndent + 1) {
|
|
243
|
+
nested.push(next);
|
|
244
|
+
i += 1;
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
if (nested.length === 0 && !isBlockStart(next) && !next.includes("|")) {
|
|
248
|
+
text.push(next.trim());
|
|
249
|
+
i += 1;
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
items.push({
|
|
255
|
+
children: parseInline(text.join("\n"), 0),
|
|
256
|
+
checked: task ? task[1] !== " " : null,
|
|
257
|
+
blocks: nested.length ? parseBlocks(dedent(nested), depth + 1) : [],
|
|
258
|
+
});
|
|
259
|
+
// Blank lines between two items of the same list keep one (loose) list.
|
|
260
|
+
let ahead = i;
|
|
261
|
+
while (ahead < lines.length && lines[ahead].trim() === "")
|
|
262
|
+
ahead += 1;
|
|
263
|
+
const sibling = ITEM_RE.exec((_d = lines[ahead]) !== null && _d !== void 0 ? _d : "");
|
|
264
|
+
if (ahead > i && sibling && isOrderedMarker(sibling[2]) === ordered && Math.abs(indentOf(sibling[1]) - baseIndent) <= 1) {
|
|
265
|
+
i = ahead;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
const start = ordered ? Number.parseInt(first[2], 10) : 1;
|
|
269
|
+
blocks.push({ type: "list", ordered, start: Number.isFinite(start) ? start : 1, items });
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
if (line.trim() === "") {
|
|
273
|
+
flush();
|
|
274
|
+
i += 1;
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
paragraph.push(line.trim());
|
|
278
|
+
i += 1;
|
|
279
|
+
}
|
|
280
|
+
flush();
|
|
281
|
+
return blocks;
|
|
282
|
+
};
|
|
283
|
+
/** Parses markdown into blocks; never throws — on an internal failure the input comes back as one paragraph. */
|
|
284
|
+
export const parseMarkdown = (content) => {
|
|
285
|
+
const source = typeof content === "string" ? content : String(content !== null && content !== void 0 ? content : "");
|
|
286
|
+
try {
|
|
287
|
+
return parseBlocks(source.replace(/\r\n?/g, "\n").split("\n"), 0);
|
|
288
|
+
}
|
|
289
|
+
catch {
|
|
290
|
+
return [{ type: "paragraph", children: [{ type: "text", content: source }] }];
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
const inlineText = (nodes) => nodes
|
|
294
|
+
.map((n) => {
|
|
295
|
+
if (n.type === "text" || n.type === "code")
|
|
296
|
+
return n.content;
|
|
297
|
+
if (n.type === "break")
|
|
298
|
+
return " ";
|
|
299
|
+
if (n.type === "image")
|
|
300
|
+
return n.alt;
|
|
301
|
+
return inlineText(n.children);
|
|
302
|
+
})
|
|
303
|
+
.join("");
|
|
304
|
+
const blockText = (block) => {
|
|
305
|
+
switch (block.type) {
|
|
306
|
+
case "heading":
|
|
307
|
+
case "paragraph":
|
|
308
|
+
return inlineText(block.children);
|
|
309
|
+
case "code":
|
|
310
|
+
return block.content;
|
|
311
|
+
case "blockquote":
|
|
312
|
+
return block.blocks.map(blockText).join(" ");
|
|
313
|
+
case "list":
|
|
314
|
+
return block.items.map((item) => [inlineText(item.children), ...item.blocks.map(blockText)].join(" ")).join(" ");
|
|
315
|
+
case "table":
|
|
316
|
+
return [block.header, ...block.rows].map((row) => row.map(inlineText).join(" ")).join(" ");
|
|
317
|
+
default:
|
|
318
|
+
return "";
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
/** Markdown reduced to readable text (no markers), e.g. for a screen-reader announcement. */
|
|
322
|
+
export const markdownToPlainText = (content) => parseMarkdown(content).map(blockText).filter(Boolean).join(" ");
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// NOTE: an allowlist on the raw prefix — `java\tscript:`, `data:`, `vbscript:` and `//host` never match.
|
|
2
|
+
const ABSOLUTE = /^(https?:\/\/[^\s]+|mailto:[^\s]+)$/i;
|
|
3
|
+
const RELATIVE = /^(\/(?![/\\])[^\s]*|#[^\s]*)$/;
|
|
4
|
+
/**
|
|
5
|
+
* Decides whether a model-emitted href may become an anchor. Only `http:`, `https:`, `mailto:`, an
|
|
6
|
+
* app-relative path (`/…`, never `//host`) and a fragment (`#…`) pass; anything else returns null and
|
|
7
|
+
* the link text renders as plain text. Control characters anywhere reject the link outright.
|
|
8
|
+
*/
|
|
9
|
+
export const resolveSafeHref = (raw) => {
|
|
10
|
+
if (typeof raw !== "string")
|
|
11
|
+
return null;
|
|
12
|
+
const href = raw.trim();
|
|
13
|
+
// eslint-disable-next-line no-control-regex
|
|
14
|
+
if (!href || /[\u0000-\u001f\u007f]/.test(href))
|
|
15
|
+
return null;
|
|
16
|
+
if (ABSOLUTE.test(href))
|
|
17
|
+
return { href, external: /^https?:/i.test(href) };
|
|
18
|
+
if (RELATIVE.test(href))
|
|
19
|
+
return { href, external: false };
|
|
20
|
+
return null;
|
|
21
|
+
};
|
|
22
|
+
/** `target`/`rel` for an anchor built from a safe href. */
|
|
23
|
+
export const anchorAttributes = (safe) => safe.external ? { target: "_blank", rel: "noopener noreferrer nofollow" } : {};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { Fragment, memo, useState } from "react";
|
|
3
|
+
import { Box, Flex, Text } from "@chakra-ui/react";
|
|
4
|
+
import { AlertCircle, AlertTriangle, Info } from "lucide-react";
|
|
5
|
+
import { useCustomTheme } from "../../../Theme/useCustomTheme.js";
|
|
6
|
+
import { resolveFilledSurface } from "../../../Theme/tokens/builders/filledSurface.js";
|
|
7
|
+
import { BORDER_WIDTHS } from "../../../Theme/tokens/primitives/borderWidths.js";
|
|
8
|
+
import { transitionFor } from "../../../Theme/tokens/primitives/motion.js";
|
|
9
|
+
import { RADIUS_ROLE } from "../../../Theme/tokens/primitives/radii.js";
|
|
10
|
+
import Button from "../../Button/Button.js";
|
|
11
|
+
import Tag from "../../Tag/Tag.js";
|
|
12
|
+
import AssistantMarkdown from "../markdown/AssistantMarkdown.js";
|
|
13
|
+
import AssistantToolSteps from "../steps/AssistantToolSteps.js";
|
|
14
|
+
import AssistantProposalCard from "../cards/AssistantProposalCard.js";
|
|
15
|
+
import AssistantApprovalCard from "../cards/AssistantApprovalCard.js";
|
|
16
|
+
import AssistantSources from "../sources/AssistantSources.js";
|
|
17
|
+
import AssistantSuggestions from "../suggestions/AssistantSuggestions.js";
|
|
18
|
+
import AssistantTypingIndicator from "../typing/AssistantTypingIndicator.js";
|
|
19
|
+
import { assistantTokens, assistantTone } from "../assistantStyles.js";
|
|
20
|
+
import { groupParts, messageText } from "../assistantModel.js";
|
|
21
|
+
import { MessageActions } from "./MessageActions.js";
|
|
22
|
+
const DEFAULT_LABELS = {
|
|
23
|
+
user: "You said",
|
|
24
|
+
assistant: "Assistant said",
|
|
25
|
+
actions: "Message actions",
|
|
26
|
+
copy: "Copy",
|
|
27
|
+
copied: "Copied",
|
|
28
|
+
retry: "Retry",
|
|
29
|
+
edit: "Edit",
|
|
30
|
+
helpful: "Good response",
|
|
31
|
+
notHelpful: "Bad response",
|
|
32
|
+
feedbackReasons: "What went wrong?",
|
|
33
|
+
skipReason: "Skip",
|
|
34
|
+
typing: "Assistant is typing",
|
|
35
|
+
aborted: "Response stopped",
|
|
36
|
+
failed: "Something went wrong. Try again.",
|
|
37
|
+
empty: "No response received.",
|
|
38
|
+
result: (name) => `Received ${name} result`,
|
|
39
|
+
};
|
|
40
|
+
const REVEAL = transitionFor(["opacity"]);
|
|
41
|
+
const toolSteps = (parts) => parts.map((p) => { var _a; return ({ id: p.id, label: (_a = p.label) !== null && _a !== void 0 ? _a : p.name, status: p.status, detail: p.detail }); });
|
|
42
|
+
/**
|
|
43
|
+
* One conversation message. The person's text is a right-aligned filled bubble (plain text, never
|
|
44
|
+
* markdown); the assistant's parts stack in a plain left column: markdown, tool steps, results, proposal
|
|
45
|
+
* and approval cards, sources, suggestions, notices and errors. `renderPart` overrides any part.
|
|
46
|
+
*/
|
|
47
|
+
const AssistantMessage = ({ message, renderers, renderPart, showMeta = false, feedbackReasons, actionsVisibility = "hover", onCopy, onRetry, onEdit, onFeedback, onLinkClick, onSuggestion, onSourceSelect, onProposalAction, onApprovalAction, labels, markdownLabels, "data-testid": testId = "assistant-message", }) => {
|
|
48
|
+
var _a;
|
|
49
|
+
const { colors } = useCustomTheme();
|
|
50
|
+
const tokens = assistantTokens(colors);
|
|
51
|
+
const text = { ...DEFAULT_LABELS, ...labels };
|
|
52
|
+
const [menuOpen, setMenuOpen] = useState(false);
|
|
53
|
+
const isUser = message.role === "user";
|
|
54
|
+
const status = (_a = message.status) !== null && _a !== void 0 ? _a : "complete";
|
|
55
|
+
const streaming = status === "streaming";
|
|
56
|
+
const plain = messageText(message);
|
|
57
|
+
const groups = groupParts(isUser ? message.parts.filter((p) => p.type !== "text") : message.parts);
|
|
58
|
+
const lastGroup = groups[groups.length - 1];
|
|
59
|
+
const caretOnText = streaming && (lastGroup === null || lastGroup === void 0 ? void 0 : lastGroup.kind) === "part" && lastGroup.part.type === "text";
|
|
60
|
+
const defaultPart = (part, isLast) => {
|
|
61
|
+
var _a;
|
|
62
|
+
switch (part.type) {
|
|
63
|
+
case "text":
|
|
64
|
+
return (_jsx(AssistantMarkdown, { content: part.text, showCaret: streaming && isLast, onLinkClick: onLinkClick, labels: markdownLabels }));
|
|
65
|
+
case "tool":
|
|
66
|
+
return _jsx(AssistantToolSteps, { steps: toolSteps([part]) });
|
|
67
|
+
case "result": {
|
|
68
|
+
const render = renderers === null || renderers === void 0 ? void 0 : renderers[(_a = part.render) !== null && _a !== void 0 ? _a : part.name];
|
|
69
|
+
if (render)
|
|
70
|
+
return render(part, message);
|
|
71
|
+
return (_jsx(Text, { fontSize: "xs", color: tokens.muted, "data-testid": `${testId}-result-fallback`, children: text.result(part.name) }));
|
|
72
|
+
}
|
|
73
|
+
case "proposal":
|
|
74
|
+
return (_jsx(AssistantProposalCard, { title: part.title, summary: part.summary, fields: part.fields, status: part.status, error: part.error, onPrimary: onProposalAction ? () => onProposalAction(part.id, "apply", message) : undefined, onEdit: onProposalAction ? () => onProposalAction(part.id, "edit", message) : undefined, onDismiss: onProposalAction ? () => onProposalAction(part.id, "dismiss", message) : undefined }));
|
|
75
|
+
case "approval":
|
|
76
|
+
return (_jsx(AssistantApprovalCard, { title: part.title, summary: part.summary, changes: part.changes, risk: part.risk, status: part.status, error: part.error, undoable: part.undoable, onApprove: onApprovalAction ? () => onApprovalAction(part.id, "approve", message) : undefined, onDeny: onApprovalAction ? () => onApprovalAction(part.id, "deny", message) : undefined, onUndo: onApprovalAction ? () => onApprovalAction(part.id, "undo", message) : undefined }));
|
|
77
|
+
case "sources":
|
|
78
|
+
return _jsx(AssistantSources, { sources: part.items, onSelect: onSourceSelect });
|
|
79
|
+
case "suggestions":
|
|
80
|
+
return onSuggestion ? (_jsx(AssistantSuggestions, { suggestions: part.items, onSelect: onSuggestion, isDisabled: streaming })) : null;
|
|
81
|
+
case "notice": {
|
|
82
|
+
const look = assistantTone(colors, part.tone);
|
|
83
|
+
return (_jsxs(Flex, { align: "flex-start", gap: 2, fontSize: "sm", color: look.text, "data-testid": `${testId}-notice`, children: [_jsx(Box, { as: part.tone === "warning" ? AlertTriangle : Info, "aria-hidden": "true", boxSize: 4, mt: 0.5, flexShrink: 0 }), _jsx(Text, { children: part.text })] }));
|
|
84
|
+
}
|
|
85
|
+
case "error":
|
|
86
|
+
return _jsx(ErrorBlock, { message: part.message, onRetry: part.retryable && onRetry ? () => onRetry(message) : undefined, retryLabel: text.retry });
|
|
87
|
+
default:
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const renderOne = (part, isLast) => {
|
|
92
|
+
const custom = renderPart === null || renderPart === void 0 ? void 0 : renderPart(part, message);
|
|
93
|
+
return custom !== undefined ? custom : defaultPart(part, isLast);
|
|
94
|
+
};
|
|
95
|
+
const body = groups.map((group, i) => {
|
|
96
|
+
const isLast = i === groups.length - 1;
|
|
97
|
+
if (group.kind === "tools") {
|
|
98
|
+
const customs = renderPart ? group.parts.map((p) => renderPart(p, message)) : [];
|
|
99
|
+
if (customs.some((c) => c !== undefined)) {
|
|
100
|
+
return (_jsx(Fragment, { children: group.parts.map((p, j) => (_jsx(Fragment, { children: customs[j] !== undefined ? customs[j] : defaultPart(p, false) }, p.id))) }, group.key));
|
|
101
|
+
}
|
|
102
|
+
return _jsx(AssistantToolSteps, { steps: toolSteps(group.parts) }, group.key);
|
|
103
|
+
}
|
|
104
|
+
return _jsx(Fragment, { children: renderOne(group.part, isLast && caretOnText) }, group.key);
|
|
105
|
+
});
|
|
106
|
+
const hasErrorPart = message.parts.some((p) => p.type === "error");
|
|
107
|
+
const showTyping = !isUser && streaming && groups.length === 0;
|
|
108
|
+
const showActions = !streaming && Boolean(plain || onRetry || onEdit || onFeedback);
|
|
109
|
+
const meta = showMeta && message.meta ? [message.meta.model, message.meta.provider].filter(Boolean).join(" · ") : "";
|
|
110
|
+
const bubble = resolveFilledSurface(colors.primary, colors);
|
|
111
|
+
const hoverOnly = actionsVisibility === "hover" && !menuOpen;
|
|
112
|
+
return (_jsxs(Box, { as: "article", "aria-label": isUser ? text.user : text.assistant, "aria-busy": streaming || undefined, display: "flex", flexDirection: "column", alignItems: isUser ? "flex-end" : "stretch", gap: 1, "data-testid": testId, "data-role": message.role, "data-status": status, sx: hoverOnly
|
|
113
|
+
? {
|
|
114
|
+
"@media (hover: hover)": {
|
|
115
|
+
"& [data-message-actions]": { opacity: 0, transition: REVEAL },
|
|
116
|
+
"&:hover [data-message-actions], &:focus-within [data-message-actions]": { opacity: 1 },
|
|
117
|
+
},
|
|
118
|
+
}
|
|
119
|
+
: undefined, children: [isUser ? (_jsxs(_Fragment, { children: [plain ? (_jsx(Box, { maxW: "85%", px: 3.5, py: 2, bg: bubble.fill, color: bubble.color, borderRadius: RADIUS_ROLE.card, borderEndEndRadius: RADIUS_ROLE.option, fontSize: "sm", lineHeight: 1.5, whiteSpace: "pre-wrap", overflowWrap: "anywhere", "data-testid": `${testId}-bubble`, children: plain })) : null, body.length ? _jsx(Flex, { direction: "column", gap: 3, w: "100%", children: body }) : null] })) : (_jsxs(Flex, { direction: "column", gap: 3, minW: 0, children: [showTyping ? _jsx(AssistantTypingIndicator, { label: text.typing }) : body, status === "error" && !hasErrorPart ? (_jsx(ErrorBlock, { message: text.failed, onRetry: onRetry ? () => onRetry(message) : undefined, retryLabel: text.retry })) : null, status === "aborted" ? (_jsx(Text, { fontSize: "xs", color: tokens.muted, fontStyle: "italic", children: text.aborted })) : null, status === "complete" && message.parts.length === 0 ? (_jsx(Text, { fontSize: "sm", color: tokens.muted, children: text.empty })) : null] })), showActions || meta ? (_jsxs(Flex, { align: "center", gap: 2, minH: 6, justify: isUser ? "flex-end" : "flex-start", children: [showActions ? (_jsx(MessageActions, { message: message, text: plain, labels: text, feedbackReasons: feedbackReasons, onCopy: onCopy, onRetry: onRetry, onEdit: onEdit, onFeedback: onFeedback, onMenuOpenChange: setMenuOpen })) : null, meta ? _jsx(Tag, { label: meta, size: "xs", variant: "outline", colorScheme: "gray", "data-testid": `${testId}-meta` }) : null] })) : null] }));
|
|
120
|
+
};
|
|
121
|
+
const ErrorBlock = ({ message, onRetry, retryLabel }) => {
|
|
122
|
+
const { colors } = useCustomTheme();
|
|
123
|
+
const look = assistantTone(colors, "error");
|
|
124
|
+
return (_jsxs(Flex, { role: "alert", align: "flex-start", gap: 2, px: 3, py: 2, bg: look.bg, borderWidth: BORDER_WIDTHS.hairline, borderColor: look.border, borderRadius: RADIUS_ROLE.control, "data-testid": "assistant-message-error", children: [_jsx(Box, { as: AlertCircle, "aria-hidden": "true", boxSize: 4, mt: 0.5, flexShrink: 0, color: look.ink }), _jsx(Text, { flex: "1", fontSize: "sm", color: colors.text[800], children: message }), onRetry ? _jsx(Button, { size: "xs", variant: "outline", colorScheme: "gray", label: retryLabel, onClick: onRetry, flexShrink: 0 }) : null] }));
|
|
125
|
+
};
|
|
126
|
+
export default memo(AssistantMessage);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Flex, Menu, MenuButton, MenuGroup, MenuItem, MenuList, Portal } from "@chakra-ui/react";
|
|
3
|
+
import { Pencil, RotateCcw, ThumbsDown, ThumbsUp } from "lucide-react";
|
|
4
|
+
import CopyButton from "../../CopyButton/CopyButton.js";
|
|
5
|
+
import IconButton from "../../IconButton/IconButton.js";
|
|
6
|
+
import ToolTip from "../../ToolTip/ToolTip.js";
|
|
7
|
+
const ACTION = { size: "xs", variant: "ghost", colorScheme: "gray" };
|
|
8
|
+
/** Copy, retry, edit and feedback for one message; every control is a real button with a name. */
|
|
9
|
+
export const MessageActions = ({ message, text, labels, feedbackReasons, onCopy, onRetry, onEdit, onFeedback, onMenuOpenChange, }) => {
|
|
10
|
+
var _a, _b;
|
|
11
|
+
const isUser = message.role === "user";
|
|
12
|
+
const rating = (_a = message.feedback) !== null && _a !== void 0 ? _a : null;
|
|
13
|
+
const reasons = (_b = feedbackReasons === null || feedbackReasons === void 0 ? void 0 : feedbackReasons.filter(Boolean)) !== null && _b !== void 0 ? _b : [];
|
|
14
|
+
const thumbsDown = !isUser && onFeedback ? (reasons.length && rating !== "down" ? (_jsxs(Menu, { isLazy: true, placement: "bottom-start", onOpen: () => onMenuOpenChange(true), onClose: () => onMenuOpenChange(false), children: [_jsx(MenuButton, { as: IconButton, ...ACTION, "aria-label": labels.notHelpful, "aria-pressed": false, icon: _jsx(ThumbsDown, { size: 14 }) }), _jsx(Portal, { children: _jsx(MenuList, { minW: "12rem", fontSize: "sm", children: _jsxs(MenuGroup, { title: labels.feedbackReasons, children: [reasons.map((reason) => (_jsx(MenuItem, { onClick: () => onFeedback("down", reason, message), children: reason }, reason))), _jsx(MenuItem, { onClick: () => onFeedback("down", undefined, message), children: labels.skipReason })] }) }) })] })) : (_jsx(ToolTip, { label: labels.notHelpful, children: _jsx(IconButton, { ...ACTION, "aria-label": labels.notHelpful, "aria-pressed": rating === "down", icon: _jsx(ThumbsDown, { size: 14, fill: rating === "down" ? "currentColor" : "none" }), onClick: () => onFeedback(rating === "down" ? null : "down", undefined, message) }) }))) : null;
|
|
15
|
+
return (_jsxs(Flex, { role: "group", "aria-label": labels.actions, align: "center", gap: 0.5, "data-message-actions": "", children: [text ? (_jsx(CopyButton, { value: text, size: "xs", iconSize: 14, tooltip: labels.copy, copiedLabel: labels.copied, onCopy: () => onCopy === null || onCopy === void 0 ? void 0 : onCopy(text, message) })) : null, isUser && onEdit ? (_jsx(ToolTip, { label: labels.edit, children: _jsx(IconButton, { ...ACTION, "aria-label": labels.edit, icon: _jsx(Pencil, { size: 14 }), onClick: () => onEdit(message) }) })) : null, !isUser && onRetry ? (_jsx(ToolTip, { label: labels.retry, children: _jsx(IconButton, { ...ACTION, "aria-label": labels.retry, icon: _jsx(RotateCcw, { size: 14 }), onClick: () => onRetry(message) }) })) : null, !isUser && onFeedback ? (_jsx(ToolTip, { label: labels.helpful, children: _jsx(IconButton, { ...ACTION, "aria-label": labels.helpful, "aria-pressed": rating === "up", icon: _jsx(ThumbsUp, { size: 14, fill: rating === "up" ? "currentColor" : "none" }), onClick: () => onFeedback(rating === "up" ? null : "up", undefined, message) }) })) : null, thumbsDown] }));
|
|
16
|
+
};
|