@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/dist/index.js
ADDED
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createExecuteTask: () => createExecuteTask,
|
|
24
|
+
createMockBrixelHost: () => createMockBrixelHost,
|
|
25
|
+
executeTask: () => executeTask,
|
|
26
|
+
listenToUITaskMessages: () => listenToUITaskMessages,
|
|
27
|
+
mockContext: () => mockContext,
|
|
28
|
+
simulateBrixelInit: () => simulateBrixelInit,
|
|
29
|
+
useBrixelArtifact: () => useBrixelArtifact
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(index_exports);
|
|
32
|
+
|
|
33
|
+
// src/useBrixelArtifact.ts
|
|
34
|
+
var import_react = require("react");
|
|
35
|
+
|
|
36
|
+
// src/executeTask.ts
|
|
37
|
+
function getApiBaseUrl() {
|
|
38
|
+
if (typeof window !== "undefined") {
|
|
39
|
+
const hostname = window.location.hostname;
|
|
40
|
+
if (hostname === "localhost" || hostname === "127.0.0.1") {
|
|
41
|
+
return "http://localhost:8000/backoffice/ui-components";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return "https://api.brixel.ai/backoffice/ui-components";
|
|
45
|
+
}
|
|
46
|
+
async function executeTask(params) {
|
|
47
|
+
const { taskUuid, inputs, conversationId, apiToken, apiBaseUrl } = params;
|
|
48
|
+
const baseUrl = apiBaseUrl || getApiBaseUrl();
|
|
49
|
+
if (typeof window !== "undefined" && (window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1")) {
|
|
50
|
+
console.debug("[Brixel SDK] Using API URL:", baseUrl);
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
const headers = {
|
|
54
|
+
"Content-Type": "application/json"
|
|
55
|
+
};
|
|
56
|
+
if (apiToken) {
|
|
57
|
+
headers["Authorization"] = `Bearer ${apiToken}`;
|
|
58
|
+
}
|
|
59
|
+
if (conversationId) {
|
|
60
|
+
headers["x-conversation-id"] = conversationId;
|
|
61
|
+
}
|
|
62
|
+
const response = await fetch(`${baseUrl}/execute_task`, {
|
|
63
|
+
method: "POST",
|
|
64
|
+
headers,
|
|
65
|
+
// Include cookies as fallback if no explicit token provided
|
|
66
|
+
credentials: apiToken ? "same-origin" : "include",
|
|
67
|
+
body: JSON.stringify({
|
|
68
|
+
task_uuid: taskUuid,
|
|
69
|
+
inputs
|
|
70
|
+
})
|
|
71
|
+
});
|
|
72
|
+
const responseText = await response.text();
|
|
73
|
+
let data = null;
|
|
74
|
+
if (responseText) {
|
|
75
|
+
try {
|
|
76
|
+
data = JSON.parse(responseText);
|
|
77
|
+
} catch {
|
|
78
|
+
data = { raw: responseText };
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (!response.ok) {
|
|
82
|
+
return {
|
|
83
|
+
success: false,
|
|
84
|
+
error: {
|
|
85
|
+
code: data?.code || `HTTP_${response.status}`,
|
|
86
|
+
message: data?.message || `Request failed with status ${response.status}`,
|
|
87
|
+
details: data?.details || data
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
success: true,
|
|
93
|
+
data: data ?? void 0
|
|
94
|
+
};
|
|
95
|
+
} catch (error) {
|
|
96
|
+
return {
|
|
97
|
+
success: false,
|
|
98
|
+
error: {
|
|
99
|
+
code: "NETWORK_ERROR",
|
|
100
|
+
message: error instanceof Error ? error.message : "Unknown error occurred",
|
|
101
|
+
details: error
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function createExecuteTask(contextAuth) {
|
|
107
|
+
return (params) => {
|
|
108
|
+
return executeTask({
|
|
109
|
+
...params,
|
|
110
|
+
apiToken: contextAuth?.apiToken,
|
|
111
|
+
conversationId: contextAuth?.conversationId,
|
|
112
|
+
apiBaseUrl: contextAuth?.apiBaseUrl
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// src/useBrixelArtifact.ts
|
|
118
|
+
var SDK_VERSION = "1.0.0";
|
|
119
|
+
function isInIframe() {
|
|
120
|
+
try {
|
|
121
|
+
return window.self !== window.top;
|
|
122
|
+
} catch {
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function useBrixelArtifact(options = {}) {
|
|
127
|
+
const { targetOrigin = "*", onInputsUpdate, onDestroy, debug = false } = options;
|
|
128
|
+
const [inputs, setInputs] = (0, import_react.useState)(null);
|
|
129
|
+
const [context, setContext] = (0, import_react.useState)(null);
|
|
130
|
+
const [status, setStatus] = (0, import_react.useState)("initializing");
|
|
131
|
+
const [renderMode, setRenderMode] = (0, import_react.useState)(null);
|
|
132
|
+
const [runId, setRunId] = (0, import_react.useState)(null);
|
|
133
|
+
const isEmbedded = (0, import_react.useRef)(isInIframe());
|
|
134
|
+
const parentWindow = (0, import_react.useRef)(null);
|
|
135
|
+
const hasCompleted = (0, import_react.useRef)(false);
|
|
136
|
+
const debugLog = (0, import_react.useCallback)(
|
|
137
|
+
(message, data) => {
|
|
138
|
+
if (debug) {
|
|
139
|
+
console.debug(`[BrixelSDK] ${message}`, data ?? "");
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
[debug]
|
|
143
|
+
);
|
|
144
|
+
const postToParent = (0, import_react.useCallback)(
|
|
145
|
+
(message) => {
|
|
146
|
+
if (parentWindow.current) {
|
|
147
|
+
debugLog("Sending message to parent:", message);
|
|
148
|
+
parentWindow.current.postMessage(message, targetOrigin);
|
|
149
|
+
} else {
|
|
150
|
+
debugLog("Cannot send message - no parent window");
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
[targetOrigin, debugLog]
|
|
154
|
+
);
|
|
155
|
+
const complete = (0, import_react.useCallback)(
|
|
156
|
+
(output) => {
|
|
157
|
+
if (hasCompleted.current) {
|
|
158
|
+
debugLog("Already completed, ignoring duplicate complete call");
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (!runId) {
|
|
162
|
+
console.error("[BrixelSDK] Cannot complete - no runId");
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
hasCompleted.current = true;
|
|
166
|
+
setStatus("completed");
|
|
167
|
+
postToParent({
|
|
168
|
+
type: "BRIXEL_COMPLETE",
|
|
169
|
+
payload: { runId, output }
|
|
170
|
+
});
|
|
171
|
+
debugLog("Task completed with output:", output);
|
|
172
|
+
},
|
|
173
|
+
[runId, postToParent, debugLog]
|
|
174
|
+
);
|
|
175
|
+
const cancel = (0, import_react.useCallback)(
|
|
176
|
+
(reason) => {
|
|
177
|
+
if (hasCompleted.current) {
|
|
178
|
+
debugLog("Already completed, ignoring cancel call");
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (!runId) {
|
|
182
|
+
console.error("[BrixelSDK] Cannot cancel - no runId");
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
hasCompleted.current = true;
|
|
186
|
+
setStatus("cancelled");
|
|
187
|
+
postToParent({
|
|
188
|
+
type: "BRIXEL_CANCEL",
|
|
189
|
+
payload: { runId, reason }
|
|
190
|
+
});
|
|
191
|
+
debugLog("Task cancelled:", reason);
|
|
192
|
+
},
|
|
193
|
+
[runId, postToParent, debugLog]
|
|
194
|
+
);
|
|
195
|
+
const setHeight = (0, import_react.useCallback)(
|
|
196
|
+
(height) => {
|
|
197
|
+
if (!runId) return;
|
|
198
|
+
postToParent({
|
|
199
|
+
type: "BRIXEL_RESIZE",
|
|
200
|
+
payload: { runId, height }
|
|
201
|
+
});
|
|
202
|
+
debugLog("Resize requested:", height);
|
|
203
|
+
},
|
|
204
|
+
[runId, postToParent, debugLog]
|
|
205
|
+
);
|
|
206
|
+
const log = (0, import_react.useCallback)(
|
|
207
|
+
(level, message, data) => {
|
|
208
|
+
if (!runId) return;
|
|
209
|
+
postToParent({
|
|
210
|
+
type: "BRIXEL_LOG",
|
|
211
|
+
payload: { runId, level, message, data }
|
|
212
|
+
});
|
|
213
|
+
},
|
|
214
|
+
[runId, postToParent]
|
|
215
|
+
);
|
|
216
|
+
(0, import_react.useEffect)(() => {
|
|
217
|
+
parentWindow.current = isEmbedded.current ? window.parent : window;
|
|
218
|
+
if (!isEmbedded.current) {
|
|
219
|
+
debugLog("Not in iframe, SDK will work in standalone mode (dev tools compatible)");
|
|
220
|
+
}
|
|
221
|
+
const handleMessage = (event) => {
|
|
222
|
+
const message = event.data;
|
|
223
|
+
if (!message || typeof message !== "object" || !message.type) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (!message.type.startsWith("BRIXEL_")) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
debugLog("Received message:", message);
|
|
230
|
+
switch (message.type) {
|
|
231
|
+
case "BRIXEL_INIT": {
|
|
232
|
+
const { runId: newRunId, inputs: newInputs, context: newContext, renderMode: mode } = message.payload;
|
|
233
|
+
setRunId(newRunId);
|
|
234
|
+
setInputs(newInputs);
|
|
235
|
+
setContext(newContext);
|
|
236
|
+
setRenderMode(mode);
|
|
237
|
+
setStatus("ready");
|
|
238
|
+
hasCompleted.current = false;
|
|
239
|
+
debugLog("Initialized with:", { runId: newRunId, inputs: newInputs, context: newContext });
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
case "BRIXEL_UPDATE_INPUTS": {
|
|
243
|
+
const { inputs: updatedInputs } = message.payload;
|
|
244
|
+
setInputs((prev) => prev ? { ...prev, ...updatedInputs } : updatedInputs);
|
|
245
|
+
onInputsUpdate?.(updatedInputs);
|
|
246
|
+
debugLog("Inputs updated:", updatedInputs);
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
case "BRIXEL_DESTROY": {
|
|
250
|
+
onDestroy?.();
|
|
251
|
+
debugLog("Destroy received");
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
case "BRIXEL_UPDATE_THEME": {
|
|
255
|
+
const { theme } = message.payload;
|
|
256
|
+
setContext((prev) => prev ? { ...prev, theme } : prev);
|
|
257
|
+
debugLog("Theme updated:", theme);
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
case "BRIXEL_UPDATE_LOCALE": {
|
|
261
|
+
const { locale } = message.payload;
|
|
262
|
+
setContext((prev) => prev ? { ...prev, locale } : prev);
|
|
263
|
+
debugLog("Locale updated:", locale);
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
window.addEventListener("message", handleMessage);
|
|
269
|
+
postToParent({
|
|
270
|
+
type: "BRIXEL_READY",
|
|
271
|
+
payload: { version: SDK_VERSION }
|
|
272
|
+
});
|
|
273
|
+
debugLog("SDK initialized, READY sent");
|
|
274
|
+
return () => {
|
|
275
|
+
window.removeEventListener("message", handleMessage);
|
|
276
|
+
};
|
|
277
|
+
}, [debugLog, postToParent, onInputsUpdate, onDestroy]);
|
|
278
|
+
(0, import_react.useEffect)(() => {
|
|
279
|
+
if (!runId || !isEmbedded.current) return;
|
|
280
|
+
const resizeObserver = new ResizeObserver((entries) => {
|
|
281
|
+
for (const entry of entries) {
|
|
282
|
+
const height = entry.contentRect.height;
|
|
283
|
+
setHeight(height);
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
resizeObserver.observe(document.body);
|
|
287
|
+
return () => {
|
|
288
|
+
resizeObserver.disconnect();
|
|
289
|
+
};
|
|
290
|
+
}, [runId, setHeight]);
|
|
291
|
+
const executeTask2 = (0, import_react.useCallback)(
|
|
292
|
+
createExecuteTask({
|
|
293
|
+
apiToken: context?.apiToken,
|
|
294
|
+
conversationId: context?.conversationId,
|
|
295
|
+
apiBaseUrl: context?.apiBaseUrl
|
|
296
|
+
}),
|
|
297
|
+
[context?.apiToken, context?.conversationId, context?.apiBaseUrl]
|
|
298
|
+
);
|
|
299
|
+
return {
|
|
300
|
+
inputs,
|
|
301
|
+
context,
|
|
302
|
+
status,
|
|
303
|
+
renderMode,
|
|
304
|
+
runId,
|
|
305
|
+
complete,
|
|
306
|
+
cancel,
|
|
307
|
+
setHeight,
|
|
308
|
+
log,
|
|
309
|
+
isEmbedded: isEmbedded.current,
|
|
310
|
+
executeTask: executeTask2
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// src/devTools.ts
|
|
315
|
+
var mockContext = {
|
|
316
|
+
runId: "dev-run-001",
|
|
317
|
+
stepId: "dev-step-001",
|
|
318
|
+
userId: "dev-user-001",
|
|
319
|
+
organizationId: "dev-org-001",
|
|
320
|
+
theme: "light",
|
|
321
|
+
locale: "en-US",
|
|
322
|
+
conversationId: "dev-conversation-001"
|
|
323
|
+
};
|
|
324
|
+
function simulateBrixelInit(inputs, options = {}) {
|
|
325
|
+
const { runId = "dev-run-001", renderMode = "interaction", context = {}, delay = 100 } = options;
|
|
326
|
+
const message = {
|
|
327
|
+
type: "BRIXEL_INIT",
|
|
328
|
+
payload: {
|
|
329
|
+
runId,
|
|
330
|
+
inputs,
|
|
331
|
+
context: { ...mockContext, ...context, runId },
|
|
332
|
+
renderMode
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
setTimeout(() => {
|
|
336
|
+
window.postMessage(message, "*");
|
|
337
|
+
console.debug("[BrixelDevTools] Simulated INIT message sent:", message);
|
|
338
|
+
}, delay);
|
|
339
|
+
}
|
|
340
|
+
function listenToUITaskMessages(callback) {
|
|
341
|
+
const handleMessage = (event) => {
|
|
342
|
+
const message = event.data;
|
|
343
|
+
if (message && typeof message === "object" && message.type?.startsWith("BRIXEL_")) {
|
|
344
|
+
callback(message);
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
window.addEventListener("message", handleMessage);
|
|
348
|
+
return () => {
|
|
349
|
+
window.removeEventListener("message", handleMessage);
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
function createMockBrixelHost(options) {
|
|
353
|
+
const { onReady, onComplete, onCancel, onResize, onLog, onError } = options;
|
|
354
|
+
let currentRunId = null;
|
|
355
|
+
const handleMessage = (event) => {
|
|
356
|
+
const message = event.data;
|
|
357
|
+
if (!message || typeof message !== "object" || !message.type?.startsWith("BRIXEL_")) {
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
switch (message.type) {
|
|
361
|
+
case "BRIXEL_READY":
|
|
362
|
+
onReady?.(message.payload?.version);
|
|
363
|
+
break;
|
|
364
|
+
case "BRIXEL_COMPLETE":
|
|
365
|
+
onComplete?.(message.payload?.output);
|
|
366
|
+
break;
|
|
367
|
+
case "BRIXEL_CANCEL":
|
|
368
|
+
onCancel?.(message.payload?.reason);
|
|
369
|
+
break;
|
|
370
|
+
case "BRIXEL_RESIZE":
|
|
371
|
+
onResize?.(message.payload?.height);
|
|
372
|
+
break;
|
|
373
|
+
case "BRIXEL_LOG":
|
|
374
|
+
onLog?.(message.payload?.level, message.payload?.message, message.payload?.data);
|
|
375
|
+
break;
|
|
376
|
+
case "BRIXEL_ERROR":
|
|
377
|
+
onError?.(message.payload?.error);
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
window.addEventListener("message", handleMessage);
|
|
382
|
+
return {
|
|
383
|
+
init(inputs, runId = "mock-run-001", renderMode = "interaction") {
|
|
384
|
+
currentRunId = runId;
|
|
385
|
+
window.postMessage(
|
|
386
|
+
{
|
|
387
|
+
type: "BRIXEL_INIT",
|
|
388
|
+
payload: {
|
|
389
|
+
runId,
|
|
390
|
+
inputs,
|
|
391
|
+
context: { ...mockContext, runId },
|
|
392
|
+
renderMode
|
|
393
|
+
}
|
|
394
|
+
},
|
|
395
|
+
"*"
|
|
396
|
+
);
|
|
397
|
+
},
|
|
398
|
+
updateInputs(inputs) {
|
|
399
|
+
if (!currentRunId) {
|
|
400
|
+
console.warn("[MockHost] Cannot update inputs - no active run");
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
window.postMessage(
|
|
404
|
+
{
|
|
405
|
+
type: "BRIXEL_UPDATE_INPUTS",
|
|
406
|
+
payload: { runId: currentRunId, inputs }
|
|
407
|
+
},
|
|
408
|
+
"*"
|
|
409
|
+
);
|
|
410
|
+
},
|
|
411
|
+
destroy() {
|
|
412
|
+
if (currentRunId) {
|
|
413
|
+
window.postMessage(
|
|
414
|
+
{
|
|
415
|
+
type: "BRIXEL_DESTROY",
|
|
416
|
+
payload: { runId: currentRunId }
|
|
417
|
+
},
|
|
418
|
+
"*"
|
|
419
|
+
);
|
|
420
|
+
}
|
|
421
|
+
window.removeEventListener("message", handleMessage);
|
|
422
|
+
currentRunId = null;
|
|
423
|
+
},
|
|
424
|
+
updateTheme(theme) {
|
|
425
|
+
if (!currentRunId) {
|
|
426
|
+
console.warn("[MockHost] Cannot update theme - no active run");
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
window.postMessage(
|
|
430
|
+
{
|
|
431
|
+
type: "BRIXEL_UPDATE_THEME",
|
|
432
|
+
payload: { runId: currentRunId, theme }
|
|
433
|
+
},
|
|
434
|
+
"*"
|
|
435
|
+
);
|
|
436
|
+
},
|
|
437
|
+
updateLocale(locale) {
|
|
438
|
+
if (!currentRunId) {
|
|
439
|
+
console.warn("[MockHost] Cannot update locale - no active run");
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
window.postMessage(
|
|
443
|
+
{
|
|
444
|
+
type: "BRIXEL_UPDATE_LOCALE",
|
|
445
|
+
payload: { runId: currentRunId, locale }
|
|
446
|
+
},
|
|
447
|
+
"*"
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
453
|
+
0 && (module.exports = {
|
|
454
|
+
createExecuteTask,
|
|
455
|
+
createMockBrixelHost,
|
|
456
|
+
executeTask,
|
|
457
|
+
listenToUITaskMessages,
|
|
458
|
+
mockContext,
|
|
459
|
+
simulateBrixelInit,
|
|
460
|
+
useBrixelArtifact
|
|
461
|
+
});
|