@magemetrics/ai 0.0.52 → 0.0.54-RC2
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 +228 -0
- package/dist/react/ai.css +1 -0
- package/dist/react/ai.js +11442 -10884
- package/dist/react/index.d.ts +2922 -597
- package/dist/styles.css +1 -1
- package/dist/web-component/ai.css +1 -0
- package/dist/web-component/web-component.es.js +45039 -40783
- package/package.json +19 -7
package/README.md
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
# @magemetrics/ai
|
|
2
|
+
|
|
3
|
+
This package provides a set of React components and a web component to integrate MageMetrics AI functionalities into your application.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
To install the package, run the following command:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @magemetrics/ai
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
You also need to include the styles in your application:
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
import "@magemetrics/ai/styles.css";
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## React Usage
|
|
20
|
+
|
|
21
|
+
### Quick Start: `ManagedModal`
|
|
22
|
+
|
|
23
|
+
The easiest way to get started is by using the `ManagedModal` component. It provides a fully managed experience, including a launcher button and a conversation view, and it doesn't require a context provider.
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
import { ManagedModal } from "@magemetrics/ai/react";
|
|
27
|
+
|
|
28
|
+
const YourComponent = () => {
|
|
29
|
+
return (
|
|
30
|
+
<ManagedModal
|
|
31
|
+
externalJwt={YOUR_EXTERNAL_JWT}
|
|
32
|
+
apiKey={YOUR_API_KEY}
|
|
33
|
+
apiUrl={YOUR_API_URL}
|
|
34
|
+
display="contents"
|
|
35
|
+
startOptions={{
|
|
36
|
+
showRecentChats: true,
|
|
37
|
+
showRecommendations: true,
|
|
38
|
+
showBackdrop: true,
|
|
39
|
+
}}
|
|
40
|
+
/>
|
|
41
|
+
);
|
|
42
|
+
};
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
**Props:**
|
|
46
|
+
|
|
47
|
+
- `externalJwt` (string): The external JWT for authentication.
|
|
48
|
+
- `apiKey` (string): Your MageMetrics API key.
|
|
49
|
+
- `apiUrl` (string): The URL of the MageMetrics API.
|
|
50
|
+
- `display` ('string'): The display style of the modal.
|
|
51
|
+
- `startOptions` (object): Options to configure the initial view of the modal.
|
|
52
|
+
- `modal` (object): Options to control the modal visibility and behavior.
|
|
53
|
+
|
|
54
|
+
### Advanced Usage
|
|
55
|
+
|
|
56
|
+
For more advanced use cases, you can use the `MageMetricsContextProvider` in combination with the `StandaloneConversationModal` and other components. This gives you more control over the state and behavior of the library.
|
|
57
|
+
|
|
58
|
+
#### `MageMetricsContextProvider`
|
|
59
|
+
|
|
60
|
+
This is the main provider that enables all the functionalities of the library. You need to wrap your application with it.
|
|
61
|
+
|
|
62
|
+
```tsx
|
|
63
|
+
import { MageMetricsContextProvider } from "@magemetrics/ai/react";
|
|
64
|
+
|
|
65
|
+
const App = () => {
|
|
66
|
+
return (
|
|
67
|
+
<MageMetricsContextProvider
|
|
68
|
+
externalJwt={YOUR_EXTERNAL_JWT}
|
|
69
|
+
apiKey={YOUR_API_KEY}
|
|
70
|
+
apiUrl={YOUR_API_URL}
|
|
71
|
+
>
|
|
72
|
+
{/* Your application */}
|
|
73
|
+
</MageMetricsContextProvider>
|
|
74
|
+
);
|
|
75
|
+
};
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**Props:**
|
|
79
|
+
|
|
80
|
+
- `externalJwt` (string): The external JWT for authentication.
|
|
81
|
+
- `apiKey` (string): Your MageMetrics API key.
|
|
82
|
+
- `apiUrl` (string): The URL of the MageMetrics API.
|
|
83
|
+
- `experimental_components` (Components): An optional object to override the default components.
|
|
84
|
+
|
|
85
|
+
#### `StandaloneConversationModal`
|
|
86
|
+
|
|
87
|
+
This component provides a standalone conversation modal that you can control with your own state. It must be used within a `MageMetricsContextProvider`.
|
|
88
|
+
|
|
89
|
+
```tsx
|
|
90
|
+
import { StandaloneConversationModal } from "@magemetrics/ai/react";
|
|
91
|
+
import { useState } from "react";
|
|
92
|
+
|
|
93
|
+
const YourComponent = () => {
|
|
94
|
+
const [isOpened, setIsOpened] = useState(false);
|
|
95
|
+
const [flowId, setFlowId] = useState<string | null>(null);
|
|
96
|
+
|
|
97
|
+
// Logic to get or create a flowId
|
|
98
|
+
|
|
99
|
+
return (
|
|
100
|
+
<div>
|
|
101
|
+
<button onClick={() => setIsOpened(true)}>Open Modal</button>
|
|
102
|
+
{flowId && (
|
|
103
|
+
<StandaloneConversationModal
|
|
104
|
+
opened={isOpened}
|
|
105
|
+
onOpenChange={setIsOpened}
|
|
106
|
+
flowId={flowId}
|
|
107
|
+
/>
|
|
108
|
+
)}
|
|
109
|
+
</div>
|
|
110
|
+
);
|
|
111
|
+
};
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**Props:**
|
|
115
|
+
|
|
116
|
+
- `opened` (boolean): Controls the visibility of the modal.
|
|
117
|
+
- `onOpenChange` (function): A callback function that is called when the modal is closed.
|
|
118
|
+
- `flowId` (string): The ID of the conversation flow to display.
|
|
119
|
+
|
|
120
|
+
### Other Exports
|
|
121
|
+
|
|
122
|
+
#### `DataTable`
|
|
123
|
+
|
|
124
|
+
This component is used to render data tables within the conversation. It's typically used when customizing the `dataReportTable` component.
|
|
125
|
+
|
|
126
|
+
```tsx
|
|
127
|
+
import { DataTable, type DataReportMessageProps } from "@magemetrics/ai/react";
|
|
128
|
+
|
|
129
|
+
const CustomDataTable = (props: DataReportMessageProps) => {
|
|
130
|
+
return <DataTable {...props} />;
|
|
131
|
+
};
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
#### `ApiService`
|
|
135
|
+
|
|
136
|
+
This class provides methods to interact with the MageMetrics API. It must be used within a `MageMetricsContextProvider`.
|
|
137
|
+
|
|
138
|
+
```tsx
|
|
139
|
+
import { ApiService } from "@magemetrics/ai/react";
|
|
140
|
+
|
|
141
|
+
const client = new ApiService(YOUR_API_URL);
|
|
142
|
+
|
|
143
|
+
// Get recent flows
|
|
144
|
+
const recentFlows = await client.getRecentFlows({ limit: 10 });
|
|
145
|
+
|
|
146
|
+
// Start a new flow
|
|
147
|
+
const newFlowId = await client.startFlow("your prompt");
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Customization
|
|
151
|
+
|
|
152
|
+
You can customize the components by passing an `experimental_components` object to the `MageMetricsContextProvider`.
|
|
153
|
+
|
|
154
|
+
```tsx
|
|
155
|
+
import {
|
|
156
|
+
MageMetricsContextProvider,
|
|
157
|
+
type Components,
|
|
158
|
+
type DataReportMessageProps,
|
|
159
|
+
DataTable,
|
|
160
|
+
} from "@magemetrics/ai/react";
|
|
161
|
+
|
|
162
|
+
const CustomComponent = (props: DataReportMessageProps) => {
|
|
163
|
+
return <DataTable {...props} />;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const components: Components = {
|
|
167
|
+
dataTableCells: {
|
|
168
|
+
empty: () => "N/A",
|
|
169
|
+
renderTypes: {
|
|
170
|
+
url: (props: unknown) => {
|
|
171
|
+
if (typeof props !== "string") return null;
|
|
172
|
+
return <a href={props}>CUSTOM LINK</a>;
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
dataReportTable: (props) => {
|
|
177
|
+
return <CustomComponent {...props} />;
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const App = () => {
|
|
182
|
+
return (
|
|
183
|
+
<MageMetricsContextProvider
|
|
184
|
+
// ... other props
|
|
185
|
+
experimental_components={components}
|
|
186
|
+
>
|
|
187
|
+
{/* Your application */}
|
|
188
|
+
</MageMetricsContextProvider>
|
|
189
|
+
);
|
|
190
|
+
};
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## Web Component Usage
|
|
194
|
+
|
|
195
|
+
You can also use the library as a web component.
|
|
196
|
+
|
|
197
|
+
### Registration
|
|
198
|
+
|
|
199
|
+
First, you need to register the web component in your application:
|
|
200
|
+
|
|
201
|
+
```javascript
|
|
202
|
+
import { createMagemetrics } from "@magemetrics/ai/web-component";
|
|
203
|
+
|
|
204
|
+
createMagemetrics();
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Usage
|
|
208
|
+
|
|
209
|
+
Then, you can use the `magemetrics-ai` custom element in your HTML:
|
|
210
|
+
|
|
211
|
+
```html
|
|
212
|
+
<magemetrics-ai
|
|
213
|
+
apiUrl="YOUR_API_URL"
|
|
214
|
+
apiKey="YOUR_API_KEY"
|
|
215
|
+
externalJWT="YOUR_EXTERNAL_JWT"
|
|
216
|
+
startOptions='{"showRecentChats":true,"showRecommendations":true,"showBackdrop":true}'
|
|
217
|
+
></magemetrics-ai>
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
**Attributes:**
|
|
221
|
+
|
|
222
|
+
- `apiUrl` (string): The URL of the MageMetrics API.
|
|
223
|
+
- `apiKey` (string): Your MageMetrics API key.
|
|
224
|
+
- `externalJWT` (string): The external JWT for authentication.
|
|
225
|
+
- `display` (string): The display style of the modal.
|
|
226
|
+
- `persist` (string): Persist the state of the component.
|
|
227
|
+
- `modal` (json): Options to control the modal visibility and behavior.
|
|
228
|
+
- `startOptions` (json): Options to configure the initial view of the modal.
|