orchid-ai 2.0.2 → 2.1.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/README.md +304 -0
- package/orchid-ai.css +517 -0
- package/package.json +1 -1
- package/src/components/ChatWindow.jsx +28 -5
- package/src/components/Message.jsx +285 -14
- package/src/components/visualizations/chartSchema.js +79 -28
- package/src/constants/visualizationInstructions.js +2 -2
- package/src/hooks/useOrchidAiChat.js +93 -18
- package/src/index.d.ts +71 -0
- package/src/index.js +11 -0
- package/src/orchidAiProcessTrace.js +175 -0
- package/src/orchidAiStreamingTitle.js +12 -0
package/README.md
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
# orchid-ai
|
|
2
|
+
|
|
3
|
+
Shared Orchid AI chat UI and visualization components. A source-distributed React component library — no compilation step; the consuming app's bundler handles JSX.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Publishing
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm version patch # or minor / major
|
|
11
|
+
npm publish
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install orchid-ai
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Import the stylesheet once in your app entry point:
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import 'orchid-ai/orchid-ai.css';
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Peer dependencies
|
|
29
|
+
|
|
30
|
+
Your app must provide:
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
react >= 18
|
|
34
|
+
react-dom >= 18
|
|
35
|
+
react-markdown >= 9
|
|
36
|
+
remark-gfm >= 4
|
|
37
|
+
html2canvas >= 1.4 (for chart PNG export)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Quick start
|
|
43
|
+
|
|
44
|
+
```jsx
|
|
45
|
+
import { ChatWindow, ChatInput, useOrchidAiChat } from 'orchid-ai';
|
|
46
|
+
import 'orchid-ai/orchid-ai.css';
|
|
47
|
+
|
|
48
|
+
export default function App() {
|
|
49
|
+
const { messages, loading, statusText, sendMessage } = useOrchidAiChat({
|
|
50
|
+
endpoint: '/api/ai/chat',
|
|
51
|
+
buildBody: (userMessage, history) => ({ message: userMessage, history }),
|
|
52
|
+
getHeaders: () => ({ 'X-CSRF-Token': getCsrfToken() }),
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<div className="ai-chat-container">
|
|
57
|
+
<ChatWindow
|
|
58
|
+
messages={messages}
|
|
59
|
+
loading={loading}
|
|
60
|
+
statusText={statusText}
|
|
61
|
+
aiEnabled={true}
|
|
62
|
+
organisationName="Acme Ltd"
|
|
63
|
+
/>
|
|
64
|
+
<ChatInput onSend={sendMessage} disabled={loading} />
|
|
65
|
+
</div>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## `useOrchidAiChat`
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
const { messages, loading, statusText, sendMessage, clearMessages } =
|
|
76
|
+
useOrchidAiChat(options);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Options
|
|
80
|
+
|
|
81
|
+
| Option | Type | Default | Description |
|
|
82
|
+
|-------------------|------------------------------------------------------------------|---------|-----------------------------------------------------------------|
|
|
83
|
+
| `endpoint` | `string` | — | POST URL the hook fetches on each message |
|
|
84
|
+
| `buildBody` | `(userMessage, history, sendOptions?) => object` | — | Builds the JSON request body |
|
|
85
|
+
| `getHeaders` | `() => Record<string, string>` | — | Returns extra headers (e.g. CSRF token) |
|
|
86
|
+
| `showStatus` | `boolean` | `true` | Set `false` to suppress the live status text entirely |
|
|
87
|
+
| `initialMessages` | `ChatMessage[]` | `[]` | Seed the transcript (e.g. from localStorage) |
|
|
88
|
+
|
|
89
|
+
### Returns
|
|
90
|
+
|
|
91
|
+
| Key | Type | Description |
|
|
92
|
+
|-----------------|----------------------------------------------|----------------------------------------------------------|
|
|
93
|
+
| `messages` | `ChatMessage[]` | Full transcript including streaming assistant messages |
|
|
94
|
+
| `loading` | `boolean` | True while a request is in flight |
|
|
95
|
+
| `statusText` | `string` | Latest `status` event text (e.g. "Looking up data") |
|
|
96
|
+
| `sendMessage` | `(text: string, opts?: SendOptions) => void` | Send a user message |
|
|
97
|
+
| `clearMessages` | `() => void` | Reset the transcript |
|
|
98
|
+
|
|
99
|
+
### `ChatMessage` shape
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
{
|
|
103
|
+
role: 'user' | 'assistant';
|
|
104
|
+
content: string;
|
|
105
|
+
truncated?: boolean;
|
|
106
|
+
isStreaming?: boolean;
|
|
107
|
+
processTrace?: { items: Array<{ type: 'status' | 'text'; value: string }>; defaultCollapsed?: boolean };
|
|
108
|
+
processInterimLive?: string;
|
|
109
|
+
queryContext?: Record<string, unknown>;
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
## Components
|
|
116
|
+
|
|
117
|
+
### `<ChatWindow>`
|
|
118
|
+
|
|
119
|
+
Renders the full message list, empty state, and typing indicator. Wrap it with a `<div className="ai-chat-container">` (sets layout and font).
|
|
120
|
+
|
|
121
|
+
| Prop | Type | Default | Description |
|
|
122
|
+
|-----------------------|-----------------|----------------------|----------------------------------------------------------------------------------------------------|
|
|
123
|
+
| `messages` | `ChatMessage[]` | — | From `useOrchidAiChat` |
|
|
124
|
+
| `loading` | `boolean` | — | Shows typing indicator when true and no streaming message is present |
|
|
125
|
+
| `statusText` | `string` | — | Live status shown above the typing indicator |
|
|
126
|
+
| `aiEnabled` | `boolean` | — | Shows a disabled state with `unavailableMessage` when false |
|
|
127
|
+
| `appName` | `string` | `"Hermes Chat"` | Used in the disabled state heading and as the PDF export filename prefix |
|
|
128
|
+
| `organisationName` | `string` | — | Shown in the empty-state description ("Ask about ...") |
|
|
129
|
+
| `unavailableMessage` | `string` | — | Overrides the default "needs an API key" copy when `aiEnabled` is false |
|
|
130
|
+
| `emptyDescription` | `string` | — | Overrides the default empty-state paragraph |
|
|
131
|
+
| `suggestions` | `string[]` | 3 built-in prompts | Clickable suggestion chips shown in the empty state |
|
|
132
|
+
| `suggestionsDisabled` | `boolean` | `false` | Renders suggestion chips as non-interactive (e.g. while loading) |
|
|
133
|
+
| `onSuggestionClick` | `(text) => void`| — | Called when a suggestion chip is clicked |
|
|
134
|
+
| `showProcessTracePanel`| `boolean` | `true` | Set `false` to show statuses inline next to the typing dots instead of in the collapsible panel |
|
|
135
|
+
| `showQuerySummary` | `boolean` | `false` | Shows a collapsed "Filters used" disclosure on messages that have `queryContext` |
|
|
136
|
+
|
|
137
|
+
### `<ChatInput>`
|
|
138
|
+
|
|
139
|
+
The text input bar with auto-growing textarea and send button.
|
|
140
|
+
|
|
141
|
+
| Prop | Type | Description |
|
|
142
|
+
|-------------------|-----------------|-----------------------------------------------------------------------|
|
|
143
|
+
| `onSend` | `(text) => void`| Called with trimmed text when the user submits |
|
|
144
|
+
| `disabled` | `boolean` | Disables the textarea and send button |
|
|
145
|
+
| `disabledReason` | `string` | When set, shows a clickable overlay and updates the placeholder text |
|
|
146
|
+
| `onDisabledClick` | `() => void` | Called when the user taps the disabled overlay |
|
|
147
|
+
|
|
148
|
+
### `<Message>`
|
|
149
|
+
|
|
150
|
+
Renders a single message bubble. Used internally by `ChatWindow`; import directly if you need a custom layout.
|
|
151
|
+
|
|
152
|
+
| Prop | Type | Description |
|
|
153
|
+
|------------------------|-----------------|---------------------------------------------------------|
|
|
154
|
+
| `role` | `'user' \| 'assistant'` | — |
|
|
155
|
+
| `content` | `string` | Markdown for assistant; plain text for user |
|
|
156
|
+
| `truncated` | `boolean` | Shows a cut-off warning |
|
|
157
|
+
| `exportPrefix` | `string` | Filename prefix for PDF download (default `orchid-ai`) |
|
|
158
|
+
| `isStreaming` | `boolean` | Enables streaming placeholders for open code fences |
|
|
159
|
+
| `streamingStatusText` | `string` | Status text shown above streaming content |
|
|
160
|
+
| `processTrace` | object | See `ChatMessage.processTrace` |
|
|
161
|
+
| `processInterimLive` | `string` | Live interim preamble (streaming only) |
|
|
162
|
+
| `showProcessTracePanel`| `boolean` | Default `true` |
|
|
163
|
+
| `queryContext` | `object` | Filters to display when `showQuerySummary` is true |
|
|
164
|
+
| `showQuerySummary` | `boolean` | Default `false` |
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
## Server-side: SSE protocol
|
|
169
|
+
|
|
170
|
+
The hook handles both streaming (`Content-Type: text/event-stream`) and plain JSON responses. For streaming, emit newline-delimited `data:` events:
|
|
171
|
+
|
|
172
|
+
```
|
|
173
|
+
data: {"type":"status","text":"Looking up data"}\n\n
|
|
174
|
+
data: {"type":"delta","text":"Here are the "}\n\n
|
|
175
|
+
data: {"type":"delta","text":"results..."}\n\n
|
|
176
|
+
data: {"type":"done","response":"Here are the results...","truncated":false}\n\n
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Event types
|
|
180
|
+
|
|
181
|
+
| `type` | Required fields | Optional fields |
|
|
182
|
+
|----------|------------------------------|------------------------------|
|
|
183
|
+
| `status` | `text: string` | — |
|
|
184
|
+
| `delta` | `text: string` | — |
|
|
185
|
+
| `done` | `response: string` | `truncated: boolean`, `queryContext: object` |
|
|
186
|
+
| `error` | `error: string` | — |
|
|
187
|
+
|
|
188
|
+
### Status labels that trigger the Working panel
|
|
189
|
+
|
|
190
|
+
The collapsible "Working" panel appears when statuses matching these patterns are received:
|
|
191
|
+
|
|
192
|
+
- `Looking up…`
|
|
193
|
+
- `Found N…`
|
|
194
|
+
- `Searching the web…`
|
|
195
|
+
- `Searching knowledge base…`
|
|
196
|
+
|
|
197
|
+
The special status `"Compiling response"` (exported as `ORCHID_AI_SSE_STATUS_CLEAR_STREAM`) signals the boundary between interim tool preamble and the final answer — the collector flushes its interim buffer and begins accumulating the reply.
|
|
198
|
+
|
|
199
|
+
Use the exported constants to keep server and client in sync:
|
|
200
|
+
|
|
201
|
+
```js
|
|
202
|
+
import { ORCHID_AI_DEFAULT_STATUS, ORCHID_AI_SSE_STATUS_CLEAR_STREAM } from 'orchid-ai';
|
|
203
|
+
|
|
204
|
+
// ORCHID_AI_DEFAULT_STATUS.thinking → 'Thinking'
|
|
205
|
+
// ORCHID_AI_DEFAULT_STATUS.lookingUpData → 'Looking up data'
|
|
206
|
+
// ORCHID_AI_DEFAULT_STATUS.compilingResponse → 'Compiling response'
|
|
207
|
+
// ORCHID_AI_SSE_STATUS_CLEAR_STREAM → 'Compiling response'
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### Query context
|
|
211
|
+
|
|
212
|
+
Include `queryContext` on the `done` event to let users see what filters the AI applied:
|
|
213
|
+
|
|
214
|
+
```js
|
|
215
|
+
res.write(`data: ${JSON.stringify({
|
|
216
|
+
type: 'done',
|
|
217
|
+
response: finalText,
|
|
218
|
+
queryContext: {
|
|
219
|
+
customerId: 123,
|
|
220
|
+
status: 'active',
|
|
221
|
+
dateFrom: '2024-01-01',
|
|
222
|
+
},
|
|
223
|
+
})}\n\n`);
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
Enable display with `<ChatWindow showQuerySummary={true} />`. Keys are automatically converted from camelCase to Title Case ("customerId" → "Customer ID").
|
|
227
|
+
|
|
228
|
+
---
|
|
229
|
+
|
|
230
|
+
## AI response title
|
|
231
|
+
|
|
232
|
+
The assistant can set the PDF export filename by embedding an HTML comment anywhere in its reply:
|
|
233
|
+
|
|
234
|
+
```
|
|
235
|
+
<!-- title: Monthly Shipment Summary -->
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
The comment is stripped from the rendered content and used as the PDF title slug.
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
## Visualizations
|
|
243
|
+
|
|
244
|
+
The AI embeds charts using a fenced code block with language `orchid-ai-chart` (legacy alias `hemiq-chart` is still parsed):
|
|
245
|
+
|
|
246
|
+
````
|
|
247
|
+
```orchid-ai-chart
|
|
248
|
+
{
|
|
249
|
+
"type": "bar_chart",
|
|
250
|
+
"title": "Shipments by carrier",
|
|
251
|
+
"bars": [
|
|
252
|
+
{ "label": "FedEx", "value": 42 },
|
|
253
|
+
{ "label": "DHL", "value": 31 }
|
|
254
|
+
]
|
|
255
|
+
}
|
|
256
|
+
```
|
|
257
|
+
````
|
|
258
|
+
|
|
259
|
+
The `type` field determines which component renders. All supported types:
|
|
260
|
+
|
|
261
|
+
| `type` | Component | Key fields |
|
|
262
|
+
|---------------------|--------------------|--------------------------------------------------------------|
|
|
263
|
+
| `bar_chart` | `BarChart` | `bars: [{ label, value }]` |
|
|
264
|
+
| `line_chart` | `LineChart` | `xAxis` (label + categories), `yAxis` (label), `series` |
|
|
265
|
+
| `stacked_bar_chart` | `StackedBarChart` | Same as `line_chart` |
|
|
266
|
+
| `grouped_bar_chart` | `GroupedBarChart` | Same as `line_chart` |
|
|
267
|
+
| `dot_chart` | `DotChart` | `series[].points` with numeric `x`, categorical `y` |
|
|
268
|
+
| `histogram` | `HistogramChart` | `bins: [{ start, end, value }]` or `{ range, count }` |
|
|
269
|
+
| `scatter_plot` | `ScatterPlot` | Standard numeric axes + series |
|
|
270
|
+
| `stat_cards` | `StatCards` | `cards: [{ label, value, unit?, subtitle?, trend? }]` |
|
|
271
|
+
| `table` | `DataTable` | `columns` + `rows` |
|
|
272
|
+
| `timeline` | `Timeline` | `items: [{ label, start, end }]` (ISO 8601 dates) |
|
|
273
|
+
|
|
274
|
+
Charts are downloadable as PNG (via `html2canvas`). Each chart block is validated against its schema on render — invalid JSON or schema errors show an error card instead of crashing.
|
|
275
|
+
|
|
276
|
+
### System prompt constant
|
|
277
|
+
|
|
278
|
+
Import `ORCHID_AI_VISUALIZATION_INSTRUCTIONS` to inject the chart format instructions into your AI system prompt:
|
|
279
|
+
|
|
280
|
+
```js
|
|
281
|
+
import { ORCHID_AI_VISUALIZATION_INSTRUCTIONS } from 'orchid-ai';
|
|
282
|
+
|
|
283
|
+
const systemPrompt = `You are a helpful assistant. ${ORCHID_AI_VISUALIZATION_INSTRUCTIONS}`;
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
---
|
|
287
|
+
|
|
288
|
+
## Using visualization components standalone
|
|
289
|
+
|
|
290
|
+
Each chart component can be used outside the chat context:
|
|
291
|
+
|
|
292
|
+
```jsx
|
|
293
|
+
import { BarChart } from 'orchid-ai';
|
|
294
|
+
import 'orchid-ai/orchid-ai.css';
|
|
295
|
+
|
|
296
|
+
<BarChart chart={{
|
|
297
|
+
type: 'bar_chart',
|
|
298
|
+
title: 'Revenue by month',
|
|
299
|
+
bars: [
|
|
300
|
+
{ label: 'Jan', value: 120000 },
|
|
301
|
+
{ label: 'Feb', value: 98000 },
|
|
302
|
+
],
|
|
303
|
+
}} />
|
|
304
|
+
```
|