@aiworkbench/vibe-bridge 0.0.2 → 0.0.4
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/adapters/api.d.ts +6 -0
- package/dist/adapters/api.d.ts.map +1 -0
- package/dist/adapters/auth.d.ts +12 -0
- package/dist/adapters/auth.d.ts.map +1 -0
- package/dist/adapters/events.d.ts +3 -0
- package/dist/adapters/events.d.ts.map +1 -0
- package/dist/adapters/navigation.d.ts +6 -0
- package/dist/adapters/navigation.d.ts.map +1 -0
- package/dist/adapters/storage.d.ts +3 -0
- package/dist/adapters/storage.d.ts.map +1 -0
- package/dist/adapters/theme.d.ts +3 -0
- package/dist/adapters/theme.d.ts.map +1 -0
- package/dist/adapters/toast.d.ts +11 -0
- package/dist/adapters/toast.d.ts.map +1 -0
- package/dist/components/VibeErrorBoundary.d.ts +17 -0
- package/dist/components/VibeErrorBoundary.d.ts.map +1 -0
- package/dist/components/VibeHost.d.ts +3 -0
- package/dist/components/VibeHost.d.ts.map +1 -0
- package/dist/core/event-bus.d.ts +10 -0
- package/dist/core/event-bus.d.ts.map +1 -0
- package/dist/core/loader.d.ts +3 -0
- package/dist/core/loader.d.ts.map +1 -0
- package/dist/core/permissions.d.ts +3 -0
- package/dist/core/permissions.d.ts.map +1 -0
- package/dist/core/types.d.ts +40 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/hooks/useVibeBridge.d.ts +9 -0
- package/dist/hooks/useVibeBridge.d.ts.map +1 -0
- package/dist/hooks/useVibeEvents.d.ts +2 -0
- package/dist/hooks/useVibeEvents.d.ts.map +1 -0
- package/dist/hooks/useVibeLoader.d.ts +3 -0
- package/dist/hooks/useVibeLoader.d.ts.map +1 -0
- package/{src/index.ts → dist/index.d.ts} +2 -15
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +450 -0
- package/package.json +5 -5
- package/src/adapters/api.ts +0 -24
- package/src/adapters/auth.ts +0 -28
- package/src/adapters/events.ts +0 -16
- package/src/adapters/navigation.ts +0 -21
- package/src/adapters/storage.ts +0 -27
- package/src/adapters/theme.ts +0 -24
- package/src/adapters/toast.ts +0 -34
- package/src/components/VibeErrorBoundary.tsx +0 -36
- package/src/components/VibeHost.tsx +0 -104
- package/src/core/event-bus.ts +0 -60
- package/src/core/loader.ts +0 -57
- package/src/core/permissions.ts +0 -41
- package/src/core/types.ts +0 -54
- package/src/hooks/useVibeBridge.ts +0 -49
- package/src/hooks/useVibeEvents.ts +0 -13
- package/src/hooks/useVibeLoader.ts +0 -42
package/dist/index.js
ADDED
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
// src/components/VibeHost.tsx
|
|
2
|
+
import { useRef, useEffect as useEffect2, useCallback } from "react";
|
|
3
|
+
|
|
4
|
+
// src/hooks/useVibeLoader.ts
|
|
5
|
+
import { useState, useEffect } from "react";
|
|
6
|
+
|
|
7
|
+
// src/core/loader.ts
|
|
8
|
+
var loadedScripts = new Set;
|
|
9
|
+
var ALLOWED_PROTOCOLS = new Set(["https:", "http:"]);
|
|
10
|
+
function validateScriptSrc(src) {
|
|
11
|
+
let url;
|
|
12
|
+
try {
|
|
13
|
+
url = new URL(src, window.location.href);
|
|
14
|
+
} catch {
|
|
15
|
+
return `Invalid vibe app script URL: ${src}`;
|
|
16
|
+
}
|
|
17
|
+
if (!ALLOWED_PROTOCOLS.has(url.protocol)) {
|
|
18
|
+
return `Refused to load vibe app script with disallowed protocol "${url.protocol}": ${src}`;
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
function loadScript(src) {
|
|
23
|
+
const validationError = validateScriptSrc(src);
|
|
24
|
+
if (validationError) {
|
|
25
|
+
return Promise.reject(new Error(validationError));
|
|
26
|
+
}
|
|
27
|
+
if (loadedScripts.has(src)) {
|
|
28
|
+
return Promise.resolve();
|
|
29
|
+
}
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
const existing = document.querySelector(`script[src="${CSS.escape(src)}"]`);
|
|
32
|
+
if (existing) {
|
|
33
|
+
loadedScripts.add(src);
|
|
34
|
+
resolve();
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const script = document.createElement("script");
|
|
38
|
+
script.type = "module";
|
|
39
|
+
script.src = src;
|
|
40
|
+
script.onload = () => {
|
|
41
|
+
loadedScripts.add(src);
|
|
42
|
+
resolve();
|
|
43
|
+
};
|
|
44
|
+
script.onerror = () => {
|
|
45
|
+
reject(new Error(`Failed to load vibe app script: ${src}`));
|
|
46
|
+
};
|
|
47
|
+
document.head.appendChild(script);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
function waitForElement(tagName) {
|
|
51
|
+
if (customElements.get(tagName)) {
|
|
52
|
+
return Promise.resolve();
|
|
53
|
+
}
|
|
54
|
+
return customElements.whenDefined(tagName).then(() => {
|
|
55
|
+
return;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// src/hooks/useVibeLoader.ts
|
|
60
|
+
function useVibeLoader(src, tagName) {
|
|
61
|
+
const [status, setStatus] = useState("loading");
|
|
62
|
+
const [error, setError] = useState(null);
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
let cancelled = false;
|
|
65
|
+
setStatus("loading");
|
|
66
|
+
setError(null);
|
|
67
|
+
loadScript(src).then(() => waitForElement(tagName)).then(() => {
|
|
68
|
+
if (!cancelled) {
|
|
69
|
+
setStatus("ready");
|
|
70
|
+
}
|
|
71
|
+
}).catch((err) => {
|
|
72
|
+
if (!cancelled) {
|
|
73
|
+
const e = err instanceof Error ? err : new Error(`Failed to load vibe app: ${tagName}`);
|
|
74
|
+
setError(e);
|
|
75
|
+
setStatus("error");
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
return () => {
|
|
79
|
+
cancelled = true;
|
|
80
|
+
};
|
|
81
|
+
}, [src, tagName]);
|
|
82
|
+
return { status, error };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/hooks/useVibeBridge.ts
|
|
86
|
+
import { useMemo } from "react";
|
|
87
|
+
import { useSession } from "next-auth/react";
|
|
88
|
+
import { useRouter } from "next/navigation";
|
|
89
|
+
import { toast } from "sonner";
|
|
90
|
+
|
|
91
|
+
// src/adapters/auth.ts
|
|
92
|
+
function createAuthAdapter(deps) {
|
|
93
|
+
return {
|
|
94
|
+
getUser() {
|
|
95
|
+
const user = deps.session?.user;
|
|
96
|
+
return {
|
|
97
|
+
id: user?.email ?? "unknown",
|
|
98
|
+
name: user?.name ?? "Unknown User",
|
|
99
|
+
email: user?.email ?? undefined
|
|
100
|
+
};
|
|
101
|
+
},
|
|
102
|
+
async getToken() {
|
|
103
|
+
const token = deps.session?.idToken;
|
|
104
|
+
if (!token) {
|
|
105
|
+
throw new Error("No authentication token available.");
|
|
106
|
+
}
|
|
107
|
+
return token;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// src/adapters/api.ts
|
|
113
|
+
function createApiAdapter(deps) {
|
|
114
|
+
return {
|
|
115
|
+
async fetch(endpoint, options) {
|
|
116
|
+
const url = endpoint.startsWith("/api/") ? endpoint : `/api/${endpoint.replace(/^\//, "")}`;
|
|
117
|
+
const token = await deps.getToken();
|
|
118
|
+
const headers = new Headers(options?.headers);
|
|
119
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
120
|
+
return globalThis.fetch(url, {
|
|
121
|
+
...options,
|
|
122
|
+
headers
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// src/adapters/navigation.ts
|
|
129
|
+
function createNavigationAdapter(deps) {
|
|
130
|
+
return {
|
|
131
|
+
navigate(path) {
|
|
132
|
+
if (/^https?:\/\//i.test(path) || /^javascript:/i.test(path)) {
|
|
133
|
+
throw new Error(`Navigation to external URLs is not allowed: ${path}`);
|
|
134
|
+
}
|
|
135
|
+
deps.push(path);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// src/adapters/theme.ts
|
|
141
|
+
function createThemeAdapter() {
|
|
142
|
+
return {
|
|
143
|
+
current() {
|
|
144
|
+
const html = document.documentElement;
|
|
145
|
+
if (html.classList.contains("dark"))
|
|
146
|
+
return "dark";
|
|
147
|
+
if (html.classList.contains("light"))
|
|
148
|
+
return "light";
|
|
149
|
+
const dataTheme = html.getAttribute("data-theme");
|
|
150
|
+
if (dataTheme === "dark")
|
|
151
|
+
return "dark";
|
|
152
|
+
if (dataTheme === "light")
|
|
153
|
+
return "light";
|
|
154
|
+
if (globalThis.matchMedia?.("(prefers-color-scheme: dark)").matches) {
|
|
155
|
+
return "dark";
|
|
156
|
+
}
|
|
157
|
+
return "light";
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// src/adapters/toast.ts
|
|
163
|
+
function createToastAdapter(deps) {
|
|
164
|
+
return {
|
|
165
|
+
show(message, options) {
|
|
166
|
+
const type = options?.type ?? "info";
|
|
167
|
+
switch (type) {
|
|
168
|
+
case "success":
|
|
169
|
+
deps.toast.success(message);
|
|
170
|
+
break;
|
|
171
|
+
case "error":
|
|
172
|
+
deps.toast.error(message);
|
|
173
|
+
break;
|
|
174
|
+
case "info":
|
|
175
|
+
deps.toast.info(message);
|
|
176
|
+
break;
|
|
177
|
+
default:
|
|
178
|
+
deps.toast(message);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// src/adapters/storage.ts
|
|
185
|
+
function createStorageAdapter(appId) {
|
|
186
|
+
const prefix = `vibe:${appId}:`;
|
|
187
|
+
return {
|
|
188
|
+
async get(key) {
|
|
189
|
+
return localStorage.getItem(`${prefix}${key}`);
|
|
190
|
+
},
|
|
191
|
+
async set(key, value) {
|
|
192
|
+
localStorage.setItem(`${prefix}${key}`, value);
|
|
193
|
+
},
|
|
194
|
+
async remove(key) {
|
|
195
|
+
localStorage.removeItem(`${prefix}${key}`);
|
|
196
|
+
},
|
|
197
|
+
async keys() {
|
|
198
|
+
const result = [];
|
|
199
|
+
for (let i = 0;i < localStorage.length; i++) {
|
|
200
|
+
const k = localStorage.key(i);
|
|
201
|
+
if (k?.startsWith(prefix)) {
|
|
202
|
+
result.push(k.slice(prefix.length));
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return result;
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// src/core/event-bus.ts
|
|
211
|
+
var VIBE_EVENT_PREFIX = "vibe:";
|
|
212
|
+
var sharedBus = null;
|
|
213
|
+
function getEventBus() {
|
|
214
|
+
if (!sharedBus) {
|
|
215
|
+
sharedBus = new EventTarget;
|
|
216
|
+
}
|
|
217
|
+
return sharedBus;
|
|
218
|
+
}
|
|
219
|
+
function prefixedEventName(event) {
|
|
220
|
+
if (event.startsWith(VIBE_EVENT_PREFIX)) {
|
|
221
|
+
return event;
|
|
222
|
+
}
|
|
223
|
+
return `${VIBE_EVENT_PREFIX}${event}`;
|
|
224
|
+
}
|
|
225
|
+
function emitEvent(event, payload, sourceAppId) {
|
|
226
|
+
const bus = getEventBus();
|
|
227
|
+
const detail = { payload, sourceAppId };
|
|
228
|
+
bus.dispatchEvent(new CustomEvent(prefixedEventName(event), { detail }));
|
|
229
|
+
}
|
|
230
|
+
function onEvent(event, handler) {
|
|
231
|
+
const bus = getEventBus();
|
|
232
|
+
const prefixed = prefixedEventName(event);
|
|
233
|
+
const listener = (e) => {
|
|
234
|
+
handler(e.detail);
|
|
235
|
+
};
|
|
236
|
+
bus.addEventListener(prefixed, listener);
|
|
237
|
+
return () => bus.removeEventListener(prefixed, listener);
|
|
238
|
+
}
|
|
239
|
+
function onceEvent(event, handler) {
|
|
240
|
+
const bus = getEventBus();
|
|
241
|
+
const prefixed = prefixedEventName(event);
|
|
242
|
+
const listener = (e) => {
|
|
243
|
+
handler(e.detail);
|
|
244
|
+
};
|
|
245
|
+
bus.addEventListener(prefixed, listener, { once: true });
|
|
246
|
+
return () => bus.removeEventListener(prefixed, listener);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// src/adapters/events.ts
|
|
250
|
+
function createEventsAdapter(appId) {
|
|
251
|
+
return {
|
|
252
|
+
emit(event, payload) {
|
|
253
|
+
emitEvent(event, payload ?? {}, appId);
|
|
254
|
+
},
|
|
255
|
+
on(event, handler) {
|
|
256
|
+
return onEvent(event, (detail) => handler(detail.payload));
|
|
257
|
+
},
|
|
258
|
+
once(event, handler) {
|
|
259
|
+
return onceEvent(event, (detail) => handler(detail.payload));
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// src/core/permissions.ts
|
|
265
|
+
function createDeniedProxy(capability) {
|
|
266
|
+
return new Proxy({}, {
|
|
267
|
+
get(_target, prop) {
|
|
268
|
+
if (typeof prop === "symbol")
|
|
269
|
+
return;
|
|
270
|
+
throw new Error(`Access denied: ${capability}.${prop} is not permitted. Add '${capability}' to your manifest.json permissions.`);
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
function filterByPermissions(bridge, permissions) {
|
|
275
|
+
const allCapabilities = [
|
|
276
|
+
"auth",
|
|
277
|
+
"api",
|
|
278
|
+
"navigation",
|
|
279
|
+
"theme",
|
|
280
|
+
"toast",
|
|
281
|
+
"storage",
|
|
282
|
+
"events"
|
|
283
|
+
];
|
|
284
|
+
const filtered = {};
|
|
285
|
+
for (const cap of allCapabilities) {
|
|
286
|
+
if (permissions.includes(cap)) {
|
|
287
|
+
filtered[cap] = bridge[cap];
|
|
288
|
+
} else {
|
|
289
|
+
filtered[cap] = createDeniedProxy(cap);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return filtered;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// src/hooks/useVibeBridge.ts
|
|
296
|
+
function useVibeBridge(options) {
|
|
297
|
+
const { appId, permissions, overrides } = options;
|
|
298
|
+
const { data: session } = useSession();
|
|
299
|
+
const router = useRouter();
|
|
300
|
+
return useMemo(() => {
|
|
301
|
+
const authAdapter = overrides?.auth ?? createAuthAdapter({ session: session ?? null });
|
|
302
|
+
const fullBridge = {
|
|
303
|
+
auth: authAdapter,
|
|
304
|
+
api: overrides?.api ?? createApiAdapter({ getToken: () => authAdapter.getToken() }),
|
|
305
|
+
navigation: overrides?.navigation ?? createNavigationAdapter({ push: (path) => router.push(path) }),
|
|
306
|
+
theme: overrides?.theme ?? createThemeAdapter(),
|
|
307
|
+
toast: overrides?.toast ?? createToastAdapter({ toast }),
|
|
308
|
+
storage: overrides?.storage ?? createStorageAdapter(appId),
|
|
309
|
+
events: overrides?.events ?? createEventsAdapter(appId)
|
|
310
|
+
};
|
|
311
|
+
return filterByPermissions(fullBridge, permissions);
|
|
312
|
+
}, [session, router, appId, permissions, overrides]);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// src/components/VibeErrorBoundary.tsx
|
|
316
|
+
import { Component } from "react";
|
|
317
|
+
|
|
318
|
+
class VibeErrorBoundary extends Component {
|
|
319
|
+
constructor(props) {
|
|
320
|
+
super(props);
|
|
321
|
+
this.state = { error: null };
|
|
322
|
+
}
|
|
323
|
+
static getDerivedStateFromError(error) {
|
|
324
|
+
return { error };
|
|
325
|
+
}
|
|
326
|
+
componentDidCatch(error, _info) {
|
|
327
|
+
this.props.onError?.(error);
|
|
328
|
+
}
|
|
329
|
+
render() {
|
|
330
|
+
if (this.state.error) {
|
|
331
|
+
return this.props.fallback(this.state.error);
|
|
332
|
+
}
|
|
333
|
+
return this.props.children;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// src/components/VibeHost.tsx
|
|
338
|
+
import { jsxDEV, Fragment } from "react/jsx-dev-runtime";
|
|
339
|
+
function VibeHostInner({
|
|
340
|
+
manifest,
|
|
341
|
+
src,
|
|
342
|
+
fallback,
|
|
343
|
+
errorFallback,
|
|
344
|
+
adapters,
|
|
345
|
+
onReady,
|
|
346
|
+
onError,
|
|
347
|
+
className,
|
|
348
|
+
style
|
|
349
|
+
}) {
|
|
350
|
+
const containerRef = useRef(null);
|
|
351
|
+
const elementRef = useRef(null);
|
|
352
|
+
const readyFiredRef = useRef(false);
|
|
353
|
+
const { status, error: loadError } = useVibeLoader(src, manifest.id);
|
|
354
|
+
const bridge = useVibeBridge({
|
|
355
|
+
appId: manifest.id,
|
|
356
|
+
permissions: manifest.permissions,
|
|
357
|
+
overrides: adapters
|
|
358
|
+
});
|
|
359
|
+
useEffect2(() => {
|
|
360
|
+
if (loadError) {
|
|
361
|
+
onError?.(loadError);
|
|
362
|
+
}
|
|
363
|
+
}, [loadError, onError]);
|
|
364
|
+
useEffect2(() => {
|
|
365
|
+
const container = containerRef.current;
|
|
366
|
+
if (status !== "ready" || !container)
|
|
367
|
+
return;
|
|
368
|
+
const el = document.createElement(manifest.id);
|
|
369
|
+
el.bridge = bridge;
|
|
370
|
+
container.appendChild(el);
|
|
371
|
+
elementRef.current = el;
|
|
372
|
+
if (!readyFiredRef.current) {
|
|
373
|
+
readyFiredRef.current = true;
|
|
374
|
+
onReady?.();
|
|
375
|
+
}
|
|
376
|
+
return () => {
|
|
377
|
+
if (container.contains(el)) {
|
|
378
|
+
container.removeChild(el);
|
|
379
|
+
}
|
|
380
|
+
elementRef.current = null;
|
|
381
|
+
};
|
|
382
|
+
}, [status, manifest.id]);
|
|
383
|
+
useEffect2(() => {
|
|
384
|
+
if (elementRef.current) {
|
|
385
|
+
elementRef.current.bridge = bridge;
|
|
386
|
+
}
|
|
387
|
+
}, [bridge]);
|
|
388
|
+
if (status === "error" && loadError) {
|
|
389
|
+
if (errorFallback) {
|
|
390
|
+
return /* @__PURE__ */ jsxDEV(Fragment, {
|
|
391
|
+
children: errorFallback(loadError)
|
|
392
|
+
}, undefined, false, undefined, this);
|
|
393
|
+
}
|
|
394
|
+
return null;
|
|
395
|
+
}
|
|
396
|
+
if (status === "loading") {
|
|
397
|
+
return /* @__PURE__ */ jsxDEV(Fragment, {
|
|
398
|
+
children: fallback ?? null
|
|
399
|
+
}, undefined, false, undefined, this);
|
|
400
|
+
}
|
|
401
|
+
return /* @__PURE__ */ jsxDEV("div", {
|
|
402
|
+
ref: containerRef,
|
|
403
|
+
className,
|
|
404
|
+
style
|
|
405
|
+
}, undefined, false, undefined, this);
|
|
406
|
+
}
|
|
407
|
+
function VibeHost(props) {
|
|
408
|
+
const defaultErrorFallback = useCallback((error) => {
|
|
409
|
+
props.onError?.(error);
|
|
410
|
+
if (props.errorFallback) {
|
|
411
|
+
return /* @__PURE__ */ jsxDEV(Fragment, {
|
|
412
|
+
children: props.errorFallback(error)
|
|
413
|
+
}, undefined, false, undefined, this);
|
|
414
|
+
}
|
|
415
|
+
return null;
|
|
416
|
+
}, [props.onError, props.errorFallback]);
|
|
417
|
+
return /* @__PURE__ */ jsxDEV(VibeErrorBoundary, {
|
|
418
|
+
fallback: defaultErrorFallback,
|
|
419
|
+
onError: props.onError,
|
|
420
|
+
children: /* @__PURE__ */ jsxDEV(VibeHostInner, {
|
|
421
|
+
...props
|
|
422
|
+
}, undefined, false, undefined, this)
|
|
423
|
+
}, undefined, false, undefined, this);
|
|
424
|
+
}
|
|
425
|
+
// src/hooks/useVibeEvents.ts
|
|
426
|
+
import { useEffect as useEffect3 } from "react";
|
|
427
|
+
function useVibeEvents(event, handler) {
|
|
428
|
+
useEffect3(() => {
|
|
429
|
+
return onEvent(event, (detail) => {
|
|
430
|
+
handler(detail.payload, detail.sourceAppId);
|
|
431
|
+
});
|
|
432
|
+
}, [event, handler]);
|
|
433
|
+
}
|
|
434
|
+
export {
|
|
435
|
+
useVibeLoader,
|
|
436
|
+
useVibeEvents,
|
|
437
|
+
useVibeBridge,
|
|
438
|
+
getEventBus,
|
|
439
|
+
filterByPermissions,
|
|
440
|
+
emitEvent,
|
|
441
|
+
createToastAdapter,
|
|
442
|
+
createThemeAdapter,
|
|
443
|
+
createStorageAdapter,
|
|
444
|
+
createNavigationAdapter,
|
|
445
|
+
createEventsAdapter,
|
|
446
|
+
createAuthAdapter,
|
|
447
|
+
createApiAdapter,
|
|
448
|
+
VibeHost,
|
|
449
|
+
VibeErrorBoundary
|
|
450
|
+
};
|
package/package.json
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiworkbench/vibe-bridge",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"publishConfig": { "access": "public" },
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
-
"bun": "./src/index.ts",
|
|
11
10
|
"import": "./dist/index.js",
|
|
12
11
|
"types": "./dist/index.d.ts"
|
|
13
12
|
}
|
|
@@ -16,11 +15,12 @@
|
|
|
16
15
|
"build": "bun build ./src/index.ts --outdir ./dist --target=browser --external react --external react-dom --external next --external next-auth --external next-auth/react --external next/navigation --external sonner && tsc --emitDeclarationOnly",
|
|
17
16
|
"dev": "tsc --watch --emitDeclarationOnly",
|
|
18
17
|
"type-check": "tsc --noEmit",
|
|
19
|
-
"clean": "rm -rf dist"
|
|
18
|
+
"clean": "rm -rf dist",
|
|
19
|
+
"prepublishOnly": "bun run build"
|
|
20
20
|
},
|
|
21
|
-
"files": ["dist"
|
|
21
|
+
"files": ["dist"],
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@aiworkbench/vibe-types": "^0.0.
|
|
23
|
+
"@aiworkbench/vibe-types": "^0.0.4"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"react": "^19.0.0",
|
package/src/adapters/api.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { ApiBridge } from "@aiworkbench/vibe-types";
|
|
2
|
-
|
|
3
|
-
export interface ApiAdapterDeps {
|
|
4
|
-
getToken: () => Promise<string>;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function createApiAdapter(deps: ApiAdapterDeps): ApiBridge {
|
|
8
|
-
return {
|
|
9
|
-
async fetch(endpoint: string, options?: RequestInit): Promise<Response> {
|
|
10
|
-
const url = endpoint.startsWith("/api/")
|
|
11
|
-
? endpoint
|
|
12
|
-
: `/api/${endpoint.replace(/^\//, "")}`;
|
|
13
|
-
|
|
14
|
-
const token = await deps.getToken();
|
|
15
|
-
const headers = new Headers(options?.headers);
|
|
16
|
-
headers.set("Authorization", `Bearer ${token}`);
|
|
17
|
-
|
|
18
|
-
return globalThis.fetch(url, {
|
|
19
|
-
...options,
|
|
20
|
-
headers,
|
|
21
|
-
});
|
|
22
|
-
},
|
|
23
|
-
};
|
|
24
|
-
}
|
package/src/adapters/auth.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { AuthBridge, BridgeUser } from "@aiworkbench/vibe-types";
|
|
2
|
-
|
|
3
|
-
export interface AuthAdapterDeps {
|
|
4
|
-
session: {
|
|
5
|
-
user?: { name?: string | null; email?: string | null } | null;
|
|
6
|
-
idToken?: string;
|
|
7
|
-
} | null;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function createAuthAdapter(deps: AuthAdapterDeps): AuthBridge {
|
|
11
|
-
return {
|
|
12
|
-
getUser(): BridgeUser {
|
|
13
|
-
const user = deps.session?.user;
|
|
14
|
-
return {
|
|
15
|
-
id: user?.email ?? "unknown",
|
|
16
|
-
name: user?.name ?? "Unknown User",
|
|
17
|
-
email: user?.email ?? undefined,
|
|
18
|
-
};
|
|
19
|
-
},
|
|
20
|
-
async getToken(): Promise<string> {
|
|
21
|
-
const token = deps.session?.idToken;
|
|
22
|
-
if (!token) {
|
|
23
|
-
throw new Error("No authentication token available.");
|
|
24
|
-
}
|
|
25
|
-
return token;
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
|
-
}
|
package/src/adapters/events.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { EventsBridge, EventPayload } from "@aiworkbench/vibe-types";
|
|
2
|
-
import { emitEvent, onEvent, onceEvent } from "../core/event-bus";
|
|
3
|
-
|
|
4
|
-
export function createEventsAdapter(appId: string): EventsBridge {
|
|
5
|
-
return {
|
|
6
|
-
emit(event: string, payload?: EventPayload): void {
|
|
7
|
-
emitEvent(event, payload ?? {}, appId);
|
|
8
|
-
},
|
|
9
|
-
on(event: string, handler: (payload: EventPayload) => void): () => void {
|
|
10
|
-
return onEvent(event, (detail) => handler(detail.payload));
|
|
11
|
-
},
|
|
12
|
-
once(event: string, handler: (payload: EventPayload) => void): () => void {
|
|
13
|
-
return onceEvent(event, (detail) => handler(detail.payload));
|
|
14
|
-
},
|
|
15
|
-
};
|
|
16
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { NavigationBridge } from "@aiworkbench/vibe-types";
|
|
2
|
-
|
|
3
|
-
export interface NavigationAdapterDeps {
|
|
4
|
-
push: (path: string) => void;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function createNavigationAdapter(
|
|
8
|
-
deps: NavigationAdapterDeps,
|
|
9
|
-
): NavigationBridge {
|
|
10
|
-
return {
|
|
11
|
-
navigate(path: string): void {
|
|
12
|
-
// Block external URLs and javascript: protocol
|
|
13
|
-
if (/^https?:\/\//i.test(path) || /^javascript:/i.test(path)) {
|
|
14
|
-
throw new Error(
|
|
15
|
-
`Navigation to external URLs is not allowed: ${path}`,
|
|
16
|
-
);
|
|
17
|
-
}
|
|
18
|
-
deps.push(path);
|
|
19
|
-
},
|
|
20
|
-
};
|
|
21
|
-
}
|
package/src/adapters/storage.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { StorageBridge } from "@aiworkbench/vibe-types";
|
|
2
|
-
|
|
3
|
-
export function createStorageAdapter(appId: string): StorageBridge {
|
|
4
|
-
const prefix = `vibe:${appId}:`;
|
|
5
|
-
|
|
6
|
-
return {
|
|
7
|
-
async get(key: string): Promise<string | null> {
|
|
8
|
-
return localStorage.getItem(`${prefix}${key}`);
|
|
9
|
-
},
|
|
10
|
-
async set(key: string, value: string): Promise<void> {
|
|
11
|
-
localStorage.setItem(`${prefix}${key}`, value);
|
|
12
|
-
},
|
|
13
|
-
async remove(key: string): Promise<void> {
|
|
14
|
-
localStorage.removeItem(`${prefix}${key}`);
|
|
15
|
-
},
|
|
16
|
-
async keys(): Promise<string[]> {
|
|
17
|
-
const result: string[] = [];
|
|
18
|
-
for (let i = 0; i < localStorage.length; i++) {
|
|
19
|
-
const k = localStorage.key(i);
|
|
20
|
-
if (k?.startsWith(prefix)) {
|
|
21
|
-
result.push(k.slice(prefix.length));
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return result;
|
|
25
|
-
},
|
|
26
|
-
};
|
|
27
|
-
}
|
package/src/adapters/theme.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { ThemeBridge } from "@aiworkbench/vibe-types";
|
|
2
|
-
|
|
3
|
-
export function createThemeAdapter(): ThemeBridge {
|
|
4
|
-
return {
|
|
5
|
-
current(): "light" | "dark" {
|
|
6
|
-
// Check <html> class for Tailwind dark mode convention
|
|
7
|
-
const html = document.documentElement;
|
|
8
|
-
if (html.classList.contains("dark")) return "dark";
|
|
9
|
-
if (html.classList.contains("light")) return "light";
|
|
10
|
-
|
|
11
|
-
// Check data-theme attribute
|
|
12
|
-
const dataTheme = html.getAttribute("data-theme");
|
|
13
|
-
if (dataTheme === "dark") return "dark";
|
|
14
|
-
if (dataTheme === "light") return "light";
|
|
15
|
-
|
|
16
|
-
// Fallback to prefers-color-scheme media query
|
|
17
|
-
if (globalThis.matchMedia?.("(prefers-color-scheme: dark)").matches) {
|
|
18
|
-
return "dark";
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return "light";
|
|
22
|
-
},
|
|
23
|
-
};
|
|
24
|
-
}
|
package/src/adapters/toast.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import type { ToastBridge } from "@aiworkbench/vibe-types";
|
|
2
|
-
|
|
3
|
-
export interface ToastAdapterDeps {
|
|
4
|
-
toast: {
|
|
5
|
-
(message: string): void;
|
|
6
|
-
success: (message: string) => void;
|
|
7
|
-
error: (message: string) => void;
|
|
8
|
-
info: (message: string) => void;
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function createToastAdapter(deps: ToastAdapterDeps): ToastBridge {
|
|
13
|
-
return {
|
|
14
|
-
show(
|
|
15
|
-
message: string,
|
|
16
|
-
options?: { type?: "info" | "success" | "error" },
|
|
17
|
-
): void {
|
|
18
|
-
const type = options?.type ?? "info";
|
|
19
|
-
switch (type) {
|
|
20
|
-
case "success":
|
|
21
|
-
deps.toast.success(message);
|
|
22
|
-
break;
|
|
23
|
-
case "error":
|
|
24
|
-
deps.toast.error(message);
|
|
25
|
-
break;
|
|
26
|
-
case "info":
|
|
27
|
-
deps.toast.info(message);
|
|
28
|
-
break;
|
|
29
|
-
default:
|
|
30
|
-
deps.toast(message);
|
|
31
|
-
}
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { Component, type ReactNode, type ErrorInfo } from "react";
|
|
2
|
-
|
|
3
|
-
interface VibeErrorBoundaryProps {
|
|
4
|
-
fallback: (error: Error) => ReactNode;
|
|
5
|
-
onError?: (error: Error) => void;
|
|
6
|
-
children: ReactNode;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
interface VibeErrorBoundaryState {
|
|
10
|
-
error: Error | null;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export class VibeErrorBoundary extends Component<
|
|
14
|
-
VibeErrorBoundaryProps,
|
|
15
|
-
VibeErrorBoundaryState
|
|
16
|
-
> {
|
|
17
|
-
constructor(props: VibeErrorBoundaryProps) {
|
|
18
|
-
super(props);
|
|
19
|
-
this.state = { error: null };
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
static getDerivedStateFromError(error: Error): VibeErrorBoundaryState {
|
|
23
|
-
return { error };
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
componentDidCatch(error: Error, _info: ErrorInfo): void {
|
|
27
|
-
this.props.onError?.(error);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
render(): ReactNode {
|
|
31
|
-
if (this.state.error) {
|
|
32
|
-
return this.props.fallback(this.state.error);
|
|
33
|
-
}
|
|
34
|
-
return this.props.children;
|
|
35
|
-
}
|
|
36
|
-
}
|