@brixel_ai/artifact-sdk 1.0.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/LICENSE +21 -0
- package/README.md +298 -0
- package/dist/index.d.mts +443 -0
- package/dist/index.d.ts +443 -0
- package/dist/index.js +461 -0
- package/dist/index.mjs +428 -0
- package/package.json +67 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 BrixelAI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
# @brixel_ai/artifact-sdk
|
|
2
|
+
|
|
3
|
+
SDK for building Brixel Artifacts - interactive React components that integrate with Brixel workflows.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @brixel_ai/artifact-sdk
|
|
9
|
+
# or
|
|
10
|
+
yarn add @brixel_ai/artifact-sdk
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @brixel_ai/artifact-sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
import { useBrixelArtifact } from "@brixel_ai/artifact-sdk";
|
|
19
|
+
|
|
20
|
+
// Define your input/output types
|
|
21
|
+
interface Inputs {
|
|
22
|
+
title: string;
|
|
23
|
+
options: string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface Output {
|
|
27
|
+
selectedOption: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function MyUITask() {
|
|
31
|
+
const { inputs, complete, cancel, context, status } = useBrixelArtifact<Inputs, Output>();
|
|
32
|
+
|
|
33
|
+
// Loading state while waiting for INIT from host
|
|
34
|
+
if (!inputs) {
|
|
35
|
+
return <div>Loading...</div>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<div>
|
|
40
|
+
<h1>{inputs.title}</h1>
|
|
41
|
+
{inputs.options.map((option) => (
|
|
42
|
+
<button key={option} onClick={() => complete({ selectedOption: option })}>
|
|
43
|
+
{option}
|
|
44
|
+
</button>
|
|
45
|
+
))}
|
|
46
|
+
<button onClick={() => cancel("User cancelled")}>Cancel</button>
|
|
47
|
+
</div>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## API Reference
|
|
53
|
+
|
|
54
|
+
### `useBrixelArtifact<TInputs, TOutput>(options?)`
|
|
55
|
+
|
|
56
|
+
Main hook for building UI Tasks.
|
|
57
|
+
|
|
58
|
+
#### Returns
|
|
59
|
+
|
|
60
|
+
| Property | Type | Description |
|
|
61
|
+
|----------|------|-------------|
|
|
62
|
+
| `inputs` | `TInputs \| null` | Input data from the host |
|
|
63
|
+
| `context` | `BrixelContext \| null` | Execution context (run, theme, locale, etc.) |
|
|
64
|
+
| `status` | `TaskStatus` | Current status: `"initializing"`, `"ready"`, `"completed"`, `"cancelled"`, `"error"` |
|
|
65
|
+
| `renderMode` | `RenderMode \| null` | `"display"` or `"interaction"` |
|
|
66
|
+
| `runId` | `string \| null` | Unique run identifier |
|
|
67
|
+
| `complete` | `(output: TOutput) => void` | Complete the task with output |
|
|
68
|
+
| `cancel` | `(reason?: string) => void` | Cancel the task |
|
|
69
|
+
| `setHeight` | `(height: number \| "auto") => void` | Request iframe resize |
|
|
70
|
+
| `log` | `(level, message, data?) => void` | Send log to host |
|
|
71
|
+
| `isEmbedded` | `boolean` | Whether running inside Brixel iframe |
|
|
72
|
+
|
|
73
|
+
#### Options
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
interface UseBrixelTaskOptions {
|
|
77
|
+
targetOrigin?: string; // PostMessage target origin (default: "*")
|
|
78
|
+
onInputsUpdate?: (inputs) => void; // Callback when inputs change
|
|
79
|
+
onDestroy?: () => void; // Callback before cleanup
|
|
80
|
+
debug?: boolean; // Enable debug logging
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Development Mode
|
|
85
|
+
|
|
86
|
+
Test your UI Task locally without Brixel:
|
|
87
|
+
|
|
88
|
+
```tsx
|
|
89
|
+
import { simulateBrixelInit } from "@brixel_ai/artifact-sdk";
|
|
90
|
+
|
|
91
|
+
// In your main.tsx or App.tsx
|
|
92
|
+
if (import.meta.env.DEV) {
|
|
93
|
+
simulateBrixelInit({
|
|
94
|
+
title: "Test Survey",
|
|
95
|
+
options: ["Option A", "Option B", "Option C"],
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Mock Host for Testing
|
|
101
|
+
|
|
102
|
+
```tsx
|
|
103
|
+
import { createMockBrixelHost } from "@brixel_ai/artifact-sdk";
|
|
104
|
+
|
|
105
|
+
const host = createMockBrixelHost({
|
|
106
|
+
onComplete: (output) => console.debug("Completed:", output),
|
|
107
|
+
onCancel: (reason) => console.debug("Cancelled:", reason),
|
|
108
|
+
onResize: (height) => console.debug("Resize:", height),
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// Send init
|
|
112
|
+
host.init({ title: "Test" });
|
|
113
|
+
|
|
114
|
+
// Later: cleanup
|
|
115
|
+
host.destroy();
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Render Modes
|
|
119
|
+
|
|
120
|
+
### Display Mode
|
|
121
|
+
|
|
122
|
+
For UI Tasks that only show information without requiring user interaction:
|
|
123
|
+
|
|
124
|
+
```tsx
|
|
125
|
+
function DisplayTask() {
|
|
126
|
+
const { inputs, context } = useBrixelArtifact<{ message: string }, void>();
|
|
127
|
+
|
|
128
|
+
if (!inputs) return null;
|
|
129
|
+
|
|
130
|
+
return <div className="notification">{inputs.message}</div>;
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Interaction Mode
|
|
135
|
+
|
|
136
|
+
For UI Tasks that require user input and block the workflow:
|
|
137
|
+
|
|
138
|
+
```tsx
|
|
139
|
+
function InteractionTask() {
|
|
140
|
+
const { inputs, complete, cancel } = useBrixelArtifact<FormInputs, FormOutput>();
|
|
141
|
+
|
|
142
|
+
const handleSubmit = (data: FormOutput) => {
|
|
143
|
+
complete(data); // This unblocks the workflow
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// ...
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Context Object
|
|
151
|
+
|
|
152
|
+
The `context` object provides information about the execution environment:
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
interface BrixelContext {
|
|
156
|
+
runId: string;
|
|
157
|
+
stepId?: string;
|
|
158
|
+
userId?: string;
|
|
159
|
+
organizationId?: string;
|
|
160
|
+
theme: "light" | "dark" | "system";
|
|
161
|
+
locale: string;
|
|
162
|
+
conversationId?: string;
|
|
163
|
+
apiToken?: string;
|
|
164
|
+
apiBaseUrl?: string;
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Executing Other UI Tasks
|
|
169
|
+
|
|
170
|
+
The SDK allows UI Tasks to execute other UI Tasks programmatically using the `executeTask` function.
|
|
171
|
+
|
|
172
|
+
### Basic Usage
|
|
173
|
+
|
|
174
|
+
```tsx
|
|
175
|
+
import { useBrixelArtifact } from "@brixel_ai/artifact-sdk";
|
|
176
|
+
|
|
177
|
+
function MyUITask() {
|
|
178
|
+
const { executeTask } = useBrixelArtifact();
|
|
179
|
+
|
|
180
|
+
const handleExecuteTask = async () => {
|
|
181
|
+
const result = await executeTask({
|
|
182
|
+
taskUuid: "78c2482f-b47d-461c-9fd0-509476687be9",
|
|
183
|
+
inputs: { name: "value" },
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
if (result.success) {
|
|
187
|
+
console.debug("Task executed:", result.data);
|
|
188
|
+
} else {
|
|
189
|
+
console.error("Error:", result.error);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
return <button onClick={handleExecuteTask}>Execute Task</button>;
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### Authentication
|
|
198
|
+
|
|
199
|
+
The `executeTask` function supports two authentication methods (in priority order):
|
|
200
|
+
|
|
201
|
+
1. **API Token via postMessage** (RECOMMENDED): The parent interface passes the token via the INIT message
|
|
202
|
+
2. **Cookies fallback**: Uses `credentials: 'include'` if no token provided
|
|
203
|
+
|
|
204
|
+
#### Passing Token from Parent
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
// In parent application (console.brixel.ai)
|
|
208
|
+
const authToken = getCookieValue('token');
|
|
209
|
+
|
|
210
|
+
iframe.contentWindow.postMessage({
|
|
211
|
+
type: 'BRIXEL_INIT',
|
|
212
|
+
payload: {
|
|
213
|
+
runId: 'run-123',
|
|
214
|
+
inputs: { /* ... */ },
|
|
215
|
+
context: {
|
|
216
|
+
apiToken: authToken,
|
|
217
|
+
// ... other context fields
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
The UI Task automatically receives this token and uses it for `executeTask` calls:
|
|
224
|
+
|
|
225
|
+
```tsx
|
|
226
|
+
const { executeTask, context } = useBrixelArtifact();
|
|
227
|
+
|
|
228
|
+
// Token from context is automatically used
|
|
229
|
+
await executeTask({
|
|
230
|
+
taskUuid: "task-uuid",
|
|
231
|
+
inputs: {}
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### API URL Auto-Detection
|
|
237
|
+
|
|
238
|
+
The SDK automatically detects your environment and uses the appropriate API:
|
|
239
|
+
|
|
240
|
+
- **Development** (localhost): `http://localhost:8000/backoffice/ui-components`
|
|
241
|
+
- **Production**: `https://api.brixel.ai/backoffice/ui-components`
|
|
242
|
+
|
|
243
|
+
When running on localhost, you'll see:
|
|
244
|
+
```
|
|
245
|
+
[Brixel SDK] Using API URL: http://localhost:8000/backoffice/ui-components
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
See [DEVELOPMENT.md](./DEVELOPMENT.md) for details on local development setup.
|
|
249
|
+
|
|
250
|
+
## PostMessage Protocol
|
|
251
|
+
|
|
252
|
+
The SDK handles the following message types automatically:
|
|
253
|
+
|
|
254
|
+
### Host → Iframe
|
|
255
|
+
|
|
256
|
+
- `BRIXEL_INIT`: Initialize with inputs and context
|
|
257
|
+
- `BRIXEL_UPDATE_INPUTS`: Update inputs during execution
|
|
258
|
+
- `BRIXEL_UPDATE_THEME`: Update theme (`"light" | "dark" | "system"`)
|
|
259
|
+
- `BRIXEL_UPDATE_LOCALE`: Update locale (e.g. `"fr-FR"`)
|
|
260
|
+
- `BRIXEL_DESTROY`: Cleanup signal
|
|
261
|
+
|
|
262
|
+
### Iframe → Host
|
|
263
|
+
|
|
264
|
+
- `BRIXEL_READY`: Iframe is ready to receive INIT
|
|
265
|
+
- `BRIXEL_COMPLETE`: Task completed with output
|
|
266
|
+
- `BRIXEL_CANCEL`: Task cancelled
|
|
267
|
+
- `BRIXEL_RESIZE`: Request height change
|
|
268
|
+
- `BRIXEL_ERROR`: Error occurred
|
|
269
|
+
- `BRIXEL_LOG`: Debug log message
|
|
270
|
+
|
|
271
|
+
## Building for Production
|
|
272
|
+
|
|
273
|
+
```bash
|
|
274
|
+
npm run build
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
This produces:
|
|
278
|
+
- `dist/index.js` - CommonJS build
|
|
279
|
+
- `dist/index.mjs` - ES Module build
|
|
280
|
+
- `dist/index.d.ts` - TypeScript declarations
|
|
281
|
+
|
|
282
|
+
### Packaging locally without `npm link`
|
|
283
|
+
|
|
284
|
+
To avoid duplicating React in consuming apps, test the SDK as an npm tarball (same shape as a publish):
|
|
285
|
+
|
|
286
|
+
```bash
|
|
287
|
+
# in the SDK repo
|
|
288
|
+
npm run pack:local
|
|
289
|
+
|
|
290
|
+
# in a consumer app (adjust version if needed)
|
|
291
|
+
npm install ../brixel-artifact-sdk/dist-tarballs/brixel-artifact-sdk-1.0.0.tgz
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
This keeps `node_modules` out of the package and prevents hook errors caused by multiple React copies. Prefer this over `npm link`.
|
|
295
|
+
|
|
296
|
+
## License
|
|
297
|
+
|
|
298
|
+
MIT
|