@hsafa/ui-sdk 0.5.4 → 0.6.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/dist/index.cjs +8 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +110 -17
- package/dist/index.d.ts +110 -17
- package/dist/index.js +8 -15
- package/dist/index.js.map +1 -1
- package/docs/XMARKDOWN_USAGE.md +196 -0
- package/package.json +20 -19
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
# XMarkdown Usage Guide
|
|
2
|
+
|
|
3
|
+
The `XMarkdownRenderer` component provides comprehensive markdown rendering with support for Mermaid diagrams, custom HTML components, and data charts.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
### 1. GitHub Flavored Markdown (GFM)
|
|
8
|
+
All standard markdown features work out of the box:
|
|
9
|
+
- **Bold**, *italic*, `inline code`
|
|
10
|
+
- [Links](https://example.com)
|
|
11
|
+
- Tables
|
|
12
|
+
- Task lists
|
|
13
|
+
- Code blocks with syntax highlighting
|
|
14
|
+
|
|
15
|
+
### 2. Mermaid Diagrams
|
|
16
|
+
|
|
17
|
+
Mermaid diagrams are automatically rendered when wrapped in fenced code blocks:
|
|
18
|
+
|
|
19
|
+
````markdown
|
|
20
|
+
```mermaid
|
|
21
|
+
graph TD
|
|
22
|
+
User --> Chat
|
|
23
|
+
Chat --> LLM
|
|
24
|
+
LLM --> Response
|
|
25
|
+
```
|
|
26
|
+
````
|
|
27
|
+
|
|
28
|
+
Supported diagram types:
|
|
29
|
+
- `flowchart` (graph TD/LR)
|
|
30
|
+
- `sequenceDiagram`
|
|
31
|
+
- `classDiagram`
|
|
32
|
+
- `stateDiagram-v2`
|
|
33
|
+
- `erDiagram`
|
|
34
|
+
- `gantt`
|
|
35
|
+
- `pie`
|
|
36
|
+
- `gitgraph`
|
|
37
|
+
|
|
38
|
+
### 3. Custom HTML Components
|
|
39
|
+
|
|
40
|
+
You can embed custom React components directly in markdown using HTML-like tags:
|
|
41
|
+
|
|
42
|
+
```markdown
|
|
43
|
+
<welcome
|
|
44
|
+
data-icon="https://example.com/icon.png"
|
|
45
|
+
title="Hello, I'm your AI Assistant"
|
|
46
|
+
data-description="I can help you with various tasks"
|
|
47
|
+
></welcome>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
#### Creating Custom Components
|
|
51
|
+
|
|
52
|
+
```tsx
|
|
53
|
+
import { WelcomeCard } from '@hsafa/ui-sdk/markdown-examples/WelcomeCard';
|
|
54
|
+
|
|
55
|
+
<XMarkdownRenderer
|
|
56
|
+
content={markdown}
|
|
57
|
+
customComponents={{
|
|
58
|
+
welcome: WelcomeCard,
|
|
59
|
+
'user-card': UserCard,
|
|
60
|
+
// ... more custom components
|
|
61
|
+
}}
|
|
62
|
+
/>
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Component props:
|
|
66
|
+
- `domNode` - Parsed HTML node with `attribs` (attributes)
|
|
67
|
+
- `streamStatus` - 'loading' | 'done' (for streaming awareness)
|
|
68
|
+
- Standard HTML attributes
|
|
69
|
+
|
|
70
|
+
### 4. Data Charts
|
|
71
|
+
|
|
72
|
+
Embed chart specifications directly in markdown:
|
|
73
|
+
|
|
74
|
+
```markdown
|
|
75
|
+
<data-chart
|
|
76
|
+
data-source="sales_2024"
|
|
77
|
+
data-spec='{
|
|
78
|
+
"type": "line",
|
|
79
|
+
"x": "date",
|
|
80
|
+
"y": "revenue",
|
|
81
|
+
"smooth": true
|
|
82
|
+
}'
|
|
83
|
+
></data-chart>
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
The DataChart component:
|
|
87
|
+
- Shows loading skeleton during streaming
|
|
88
|
+
- Parses JSON spec when complete
|
|
89
|
+
- Can be integrated with any charting library (ECharts, AntV, Chart.js, etc.)
|
|
90
|
+
|
|
91
|
+
## Streaming Support
|
|
92
|
+
|
|
93
|
+
XMarkdownRenderer is optimized for LLM streaming:
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
const [content, setContent] = useState('');
|
|
97
|
+
const [isStreaming, setIsStreaming] = useState(true);
|
|
98
|
+
|
|
99
|
+
// As chunks arrive from LLM
|
|
100
|
+
stream.on('chunk', (chunk) => {
|
|
101
|
+
setContent(prev => prev + chunk);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
stream.on('end', () => {
|
|
105
|
+
setIsStreaming(false);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
return (
|
|
109
|
+
<XMarkdownRenderer
|
|
110
|
+
content={content}
|
|
111
|
+
streaming={isStreaming}
|
|
112
|
+
/>
|
|
113
|
+
);
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Features during streaming:
|
|
117
|
+
- Progressive rendering with fade-in animation
|
|
118
|
+
- Skeleton placeholders for incomplete components
|
|
119
|
+
- Automatic finalization when streaming completes
|
|
120
|
+
|
|
121
|
+
## Complete Example
|
|
122
|
+
|
|
123
|
+
```tsx
|
|
124
|
+
import { XMarkdownRenderer } from '@hsafa/ui-sdk';
|
|
125
|
+
import { WelcomeCard } from '@hsafa/ui-sdk/markdown-examples/WelcomeCard';
|
|
126
|
+
|
|
127
|
+
const markdown = `
|
|
128
|
+
# System Architecture
|
|
129
|
+
|
|
130
|
+
Here's how our system works:
|
|
131
|
+
|
|
132
|
+
\`\`\`mermaid
|
|
133
|
+
sequenceDiagram
|
|
134
|
+
User->>WebApp: HTTP request
|
|
135
|
+
WebApp->>API: REST call
|
|
136
|
+
API->>DB: Query
|
|
137
|
+
DB-->>API: Result
|
|
138
|
+
API-->>WebApp: JSON response
|
|
139
|
+
WebApp-->>User: Rendered page
|
|
140
|
+
\`\`\`
|
|
141
|
+
|
|
142
|
+
<welcome
|
|
143
|
+
data-icon="/logo.png"
|
|
144
|
+
title="Welcome to Our Platform"
|
|
145
|
+
data-description="Built with modern technology"
|
|
146
|
+
></welcome>
|
|
147
|
+
|
|
148
|
+
## Sales Data
|
|
149
|
+
|
|
150
|
+
<data-chart
|
|
151
|
+
data-source="sales_last_7_days"
|
|
152
|
+
data-spec='{
|
|
153
|
+
"type": "bar",
|
|
154
|
+
"x": "date",
|
|
155
|
+
"y": "revenue"
|
|
156
|
+
}'
|
|
157
|
+
></data-chart>
|
|
158
|
+
`;
|
|
159
|
+
|
|
160
|
+
export function ChatMessage({ isStreaming }: { isStreaming: boolean }) {
|
|
161
|
+
return (
|
|
162
|
+
<XMarkdownRenderer
|
|
163
|
+
content={markdown}
|
|
164
|
+
theme="dark"
|
|
165
|
+
streaming={isStreaming}
|
|
166
|
+
customComponents={{
|
|
167
|
+
welcome: WelcomeCard,
|
|
168
|
+
}}
|
|
169
|
+
/>
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Security
|
|
175
|
+
|
|
176
|
+
XMarkdownRenderer uses DOMPurify for HTML sanitization:
|
|
177
|
+
- No `dangerouslySetInnerHTML`
|
|
178
|
+
- XSS protection by default
|
|
179
|
+
- Configurable allowed tags and attributes
|
|
180
|
+
- Safe for rendering LLM-generated content
|
|
181
|
+
|
|
182
|
+
## Prompting LLMs
|
|
183
|
+
|
|
184
|
+
To get LLMs to use these features, include instructions like:
|
|
185
|
+
|
|
186
|
+
```
|
|
187
|
+
MARKDOWN & DIAGRAM CAPABILITIES:
|
|
188
|
+
Your responses are rendered with @ant-design/x-markdown, which supports:
|
|
189
|
+
- GitHub Flavored Markdown (GFM): tables, task lists, strikethrough, autolinks
|
|
190
|
+
- Mermaid diagrams: Use when visuals aid clarity (schemas, flows, architectures)
|
|
191
|
+
- Custom components: <welcome>, <data-chart>, etc.
|
|
192
|
+
- Streaming support: Your responses render progressively
|
|
193
|
+
|
|
194
|
+
For Mermaid diagrams, wrap them in fenced code blocks with "mermaid" language tag.
|
|
195
|
+
For charts, use <data-chart data-source="..." data-spec='{...}'></data-chart>
|
|
196
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hsafa/ui-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "React SDK for integrating AI agents built with HSAFA AI Agent Studio",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -27,21 +27,6 @@
|
|
|
27
27
|
"publishConfig": {
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
|
-
"scripts": {
|
|
31
|
-
"build": "tsup",
|
|
32
|
-
"dev": "tsup --watch",
|
|
33
|
-
"test": "vitest",
|
|
34
|
-
"test:ui": "vitest --ui",
|
|
35
|
-
"storybook": "storybook dev -p 6006",
|
|
36
|
-
"build-storybook": "storybook build",
|
|
37
|
-
"lint": "eslint src --ext .ts,.tsx",
|
|
38
|
-
"type-check": "tsc --noEmit",
|
|
39
|
-
"docs:api": "typedoc",
|
|
40
|
-
"docs:dev": "pnpm storybook",
|
|
41
|
-
"docs:build": "pnpm build-storybook && pnpm docs:api",
|
|
42
|
-
"prepublishOnly": "pnpm build && pnpm test",
|
|
43
|
-
"publish:npm": "npm publish --access public"
|
|
44
|
-
},
|
|
45
30
|
"peerDependencies": {
|
|
46
31
|
"@tabler/icons-react": ">=3",
|
|
47
32
|
"react": ">=18",
|
|
@@ -110,9 +95,25 @@
|
|
|
110
95
|
},
|
|
111
96
|
"dependencies": {
|
|
112
97
|
"@ai-sdk/react": "beta",
|
|
98
|
+
"@ant-design/x": "^2.1.0",
|
|
99
|
+
"@ant-design/x-markdown": "^2.1.0",
|
|
113
100
|
"ai": "beta",
|
|
101
|
+
"fast-json-patch": "^3.1.1",
|
|
114
102
|
"lucide-react": "^0.509.0",
|
|
115
|
-
"mermaid": "^11.
|
|
116
|
-
|
|
103
|
+
"mermaid": "^11.12.0"
|
|
104
|
+
},
|
|
105
|
+
"scripts": {
|
|
106
|
+
"build": "tsup",
|
|
107
|
+
"dev": "tsup --watch",
|
|
108
|
+
"test": "vitest",
|
|
109
|
+
"test:ui": "vitest --ui",
|
|
110
|
+
"storybook": "storybook dev -p 6006",
|
|
111
|
+
"build-storybook": "storybook build",
|
|
112
|
+
"lint": "eslint src --ext .ts,.tsx",
|
|
113
|
+
"type-check": "tsc --noEmit",
|
|
114
|
+
"docs:api": "typedoc",
|
|
115
|
+
"docs:dev": "pnpm storybook",
|
|
116
|
+
"docs:build": "pnpm build-storybook && pnpm docs:api",
|
|
117
|
+
"publish:npm": "npm publish --access public"
|
|
117
118
|
}
|
|
118
|
-
}
|
|
119
|
+
}
|