@aiworkbench/vibe-bridge 0.0.1
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/package.json +42 -0
- package/src/adapters/api.ts +24 -0
- package/src/adapters/auth.ts +28 -0
- package/src/adapters/events.ts +16 -0
- package/src/adapters/navigation.ts +21 -0
- package/src/adapters/storage.ts +27 -0
- package/src/adapters/theme.ts +24 -0
- package/src/adapters/toast.ts +34 -0
- package/src/components/VibeErrorBoundary.tsx +36 -0
- package/src/components/VibeHost.tsx +104 -0
- package/src/core/event-bus.ts +60 -0
- package/src/core/loader.ts +57 -0
- package/src/core/permissions.ts +41 -0
- package/src/core/types.ts +54 -0
- package/src/hooks/useVibeBridge.ts +49 -0
- package/src/hooks/useVibeEvents.ts +13 -0
- package/src/hooks/useVibeLoader.ts +42 -0
- package/src/index.ts +33 -0
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aiworkbench/vibe-bridge",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"publishConfig": { "access": "public" },
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"bun": "./src/index.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"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
|
+
"dev": "tsc --watch --emitDeclarationOnly",
|
|
18
|
+
"type-check": "tsc --noEmit",
|
|
19
|
+
"clean": "rm -rf dist"
|
|
20
|
+
},
|
|
21
|
+
"files": ["dist", "src"],
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@aiworkbench/vibe-types": "workspace:*"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"react": "^19.0.0",
|
|
27
|
+
"react-dom": "^19.0.0",
|
|
28
|
+
"next": ">=14.0.0",
|
|
29
|
+
"next-auth": "^4.0.0",
|
|
30
|
+
"sonner": "^2.0.0"
|
|
31
|
+
},
|
|
32
|
+
"peerDependenciesMeta": {
|
|
33
|
+
"sonner": {
|
|
34
|
+
"optional": true
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/react": "^19.2.10",
|
|
39
|
+
"@types/react-dom": "^19.2.3",
|
|
40
|
+
"sonner": "^2.0.0"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { useRef, useEffect, useCallback } from "react";
|
|
2
|
+
import type { Bridge } from "@aiworkbench/vibe-types";
|
|
3
|
+
import { useVibeLoader } from "../hooks/useVibeLoader";
|
|
4
|
+
import { useVibeBridge } from "../hooks/useVibeBridge";
|
|
5
|
+
import { VibeErrorBoundary } from "./VibeErrorBoundary";
|
|
6
|
+
import type { VibeHostProps } from "../core/types";
|
|
7
|
+
|
|
8
|
+
interface VibeElementInstance extends HTMLElement {
|
|
9
|
+
bridge: Bridge;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function VibeHostInner({
|
|
13
|
+
manifest,
|
|
14
|
+
src,
|
|
15
|
+
fallback,
|
|
16
|
+
errorFallback,
|
|
17
|
+
adapters,
|
|
18
|
+
onReady,
|
|
19
|
+
onError,
|
|
20
|
+
className,
|
|
21
|
+
style,
|
|
22
|
+
}: VibeHostProps) {
|
|
23
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
24
|
+
const elementRef = useRef<VibeElementInstance | null>(null);
|
|
25
|
+
const readyFiredRef = useRef(false);
|
|
26
|
+
|
|
27
|
+
const { status, error: loadError } = useVibeLoader(src, manifest.id);
|
|
28
|
+
|
|
29
|
+
const bridge = useVibeBridge({
|
|
30
|
+
appId: manifest.id,
|
|
31
|
+
permissions: manifest.permissions,
|
|
32
|
+
overrides: adapters,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Handle load errors
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
if (loadError) {
|
|
38
|
+
onError?.(loadError);
|
|
39
|
+
}
|
|
40
|
+
}, [loadError, onError]);
|
|
41
|
+
|
|
42
|
+
// Mount the custom element once it's defined
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
const container = containerRef.current;
|
|
45
|
+
if (status !== "ready" || !container) return;
|
|
46
|
+
|
|
47
|
+
// Create and mount the custom element
|
|
48
|
+
const el = document.createElement(manifest.id) as VibeElementInstance;
|
|
49
|
+
el.bridge = bridge;
|
|
50
|
+
container.appendChild(el);
|
|
51
|
+
elementRef.current = el;
|
|
52
|
+
|
|
53
|
+
if (!readyFiredRef.current) {
|
|
54
|
+
readyFiredRef.current = true;
|
|
55
|
+
onReady?.();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return () => {
|
|
59
|
+
if (container.contains(el)) {
|
|
60
|
+
container.removeChild(el);
|
|
61
|
+
}
|
|
62
|
+
elementRef.current = null;
|
|
63
|
+
};
|
|
64
|
+
}, [status, manifest.id]);
|
|
65
|
+
|
|
66
|
+
// Re-inject bridge when it changes (session/theme updates)
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
if (elementRef.current) {
|
|
69
|
+
elementRef.current.bridge = bridge;
|
|
70
|
+
}
|
|
71
|
+
}, [bridge]);
|
|
72
|
+
|
|
73
|
+
if (status === "error" && loadError) {
|
|
74
|
+
if (errorFallback) {
|
|
75
|
+
return <>{errorFallback(loadError)}</>;
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (status === "loading") {
|
|
81
|
+
return <>{fallback ?? null}</>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return <div ref={containerRef} className={className} style={style} />;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function VibeHost(props: VibeHostProps) {
|
|
88
|
+
const defaultErrorFallback = useCallback(
|
|
89
|
+
(error: Error) => {
|
|
90
|
+
props.onError?.(error);
|
|
91
|
+
if (props.errorFallback) {
|
|
92
|
+
return <>{props.errorFallback(error)}</>;
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
},
|
|
96
|
+
[props.onError, props.errorFallback],
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
return (
|
|
100
|
+
<VibeErrorBoundary fallback={defaultErrorFallback} onError={props.onError}>
|
|
101
|
+
<VibeHostInner {...props} />
|
|
102
|
+
</VibeErrorBoundary>
|
|
103
|
+
);
|
|
104
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const VIBE_EVENT_PREFIX = "vibe:";
|
|
2
|
+
|
|
3
|
+
export interface VibeEventDetail {
|
|
4
|
+
payload: Record<string, unknown>;
|
|
5
|
+
sourceAppId: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
let sharedBus: EventTarget | null = null;
|
|
9
|
+
|
|
10
|
+
export function getEventBus(): EventTarget {
|
|
11
|
+
if (!sharedBus) {
|
|
12
|
+
sharedBus = new EventTarget();
|
|
13
|
+
}
|
|
14
|
+
return sharedBus;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function prefixedEventName(event: string): string {
|
|
18
|
+
if (event.startsWith(VIBE_EVENT_PREFIX)) {
|
|
19
|
+
return event;
|
|
20
|
+
}
|
|
21
|
+
return `${VIBE_EVENT_PREFIX}${event}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function emitEvent(
|
|
25
|
+
event: string,
|
|
26
|
+
payload: Record<string, unknown>,
|
|
27
|
+
sourceAppId: string,
|
|
28
|
+
): void {
|
|
29
|
+
const bus = getEventBus();
|
|
30
|
+
const detail: VibeEventDetail = { payload, sourceAppId };
|
|
31
|
+
bus.dispatchEvent(
|
|
32
|
+
new CustomEvent(prefixedEventName(event), { detail }),
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function onEvent(
|
|
37
|
+
event: string,
|
|
38
|
+
handler: (detail: VibeEventDetail) => void,
|
|
39
|
+
): () => void {
|
|
40
|
+
const bus = getEventBus();
|
|
41
|
+
const prefixed = prefixedEventName(event);
|
|
42
|
+
const listener = (e: Event) => {
|
|
43
|
+
handler((e as CustomEvent<VibeEventDetail>).detail);
|
|
44
|
+
};
|
|
45
|
+
bus.addEventListener(prefixed, listener);
|
|
46
|
+
return () => bus.removeEventListener(prefixed, listener);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function onceEvent(
|
|
50
|
+
event: string,
|
|
51
|
+
handler: (detail: VibeEventDetail) => void,
|
|
52
|
+
): () => void {
|
|
53
|
+
const bus = getEventBus();
|
|
54
|
+
const prefixed = prefixedEventName(event);
|
|
55
|
+
const listener = (e: Event) => {
|
|
56
|
+
handler((e as CustomEvent<VibeEventDetail>).detail);
|
|
57
|
+
};
|
|
58
|
+
bus.addEventListener(prefixed, listener, { once: true });
|
|
59
|
+
return () => bus.removeEventListener(prefixed, listener);
|
|
60
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const loadedScripts = new Set<string>();
|
|
2
|
+
|
|
3
|
+
const ALLOWED_PROTOCOLS = new Set(["https:", "http:"]);
|
|
4
|
+
|
|
5
|
+
function validateScriptSrc(src: string): string | null {
|
|
6
|
+
let url: URL;
|
|
7
|
+
try {
|
|
8
|
+
url = new URL(src, window.location.href);
|
|
9
|
+
} catch {
|
|
10
|
+
return `Invalid vibe app script URL: ${src}`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (!ALLOWED_PROTOCOLS.has(url.protocol)) {
|
|
14
|
+
return `Refused to load vibe app script with disallowed protocol "${url.protocol}": ${src}`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function loadScript(src: string): Promise<void> {
|
|
21
|
+
const validationError = validateScriptSrc(src);
|
|
22
|
+
if (validationError) {
|
|
23
|
+
return Promise.reject(new Error(validationError));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (loadedScripts.has(src)) {
|
|
27
|
+
return Promise.resolve();
|
|
28
|
+
}
|
|
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
|
+
|
|
38
|
+
const script = document.createElement("script");
|
|
39
|
+
script.type = "module";
|
|
40
|
+
script.src = src;
|
|
41
|
+
script.onload = () => {
|
|
42
|
+
loadedScripts.add(src);
|
|
43
|
+
resolve();
|
|
44
|
+
};
|
|
45
|
+
script.onerror = () => {
|
|
46
|
+
reject(new Error(`Failed to load vibe app script: ${src}`));
|
|
47
|
+
};
|
|
48
|
+
document.head.appendChild(script);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function waitForElement(tagName: string): Promise<void> {
|
|
53
|
+
if (customElements.get(tagName)) {
|
|
54
|
+
return Promise.resolve();
|
|
55
|
+
}
|
|
56
|
+
return customElements.whenDefined(tagName).then(() => undefined);
|
|
57
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Bridge, BridgeCapability } from "@aiworkbench/vibe-types";
|
|
2
|
+
|
|
3
|
+
function createDeniedProxy(capability: string): unknown {
|
|
4
|
+
return new Proxy(
|
|
5
|
+
{},
|
|
6
|
+
{
|
|
7
|
+
get(_target, prop) {
|
|
8
|
+
if (typeof prop === "symbol") return undefined;
|
|
9
|
+
throw new Error(
|
|
10
|
+
`Access denied: ${capability}.${prop} is not permitted. Add '${capability}' to your manifest.json permissions.`,
|
|
11
|
+
);
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function filterByPermissions(
|
|
18
|
+
bridge: Bridge,
|
|
19
|
+
permissions: BridgeCapability[],
|
|
20
|
+
): Bridge {
|
|
21
|
+
const allCapabilities: BridgeCapability[] = [
|
|
22
|
+
"auth",
|
|
23
|
+
"api",
|
|
24
|
+
"navigation",
|
|
25
|
+
"theme",
|
|
26
|
+
"toast",
|
|
27
|
+
"storage",
|
|
28
|
+
"events",
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
const filtered = {} as Record<string, unknown>;
|
|
32
|
+
for (const cap of allCapabilities) {
|
|
33
|
+
if (permissions.includes(cap)) {
|
|
34
|
+
filtered[cap] = bridge[cap];
|
|
35
|
+
} else {
|
|
36
|
+
filtered[cap] = createDeniedProxy(cap);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return filtered as unknown as Bridge;
|
|
41
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Bridge,
|
|
3
|
+
VibeManifest,
|
|
4
|
+
AuthBridge,
|
|
5
|
+
ApiBridge,
|
|
6
|
+
NavigationBridge,
|
|
7
|
+
ThemeBridge,
|
|
8
|
+
ToastBridge,
|
|
9
|
+
StorageBridge,
|
|
10
|
+
EventsBridge,
|
|
11
|
+
} from "@aiworkbench/vibe-types";
|
|
12
|
+
import type { ReactNode, CSSProperties } from "react";
|
|
13
|
+
|
|
14
|
+
export interface VibeHostProps {
|
|
15
|
+
/** The mini-app manifest describing its id, permissions, etc. */
|
|
16
|
+
manifest: VibeManifest;
|
|
17
|
+
/** URL to the built JS bundle for the mini-app. */
|
|
18
|
+
src: string;
|
|
19
|
+
/** Shown while the script loads and custom element registers. */
|
|
20
|
+
fallback?: ReactNode;
|
|
21
|
+
/** Shown when the script fails to load or the element fails to mount. */
|
|
22
|
+
errorFallback?: (error: Error) => ReactNode;
|
|
23
|
+
/** Optional overrides per capability adapter. */
|
|
24
|
+
adapters?: Partial<AdapterOverrides>;
|
|
25
|
+
/** Fires when the custom element is mounted and bridge is injected. */
|
|
26
|
+
onReady?: () => void;
|
|
27
|
+
/** Fires on load or mount failure. */
|
|
28
|
+
onError?: (error: Error) => void;
|
|
29
|
+
className?: string;
|
|
30
|
+
style?: CSSProperties;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface AdapterOverrides {
|
|
34
|
+
auth: AuthBridge;
|
|
35
|
+
api: ApiBridge;
|
|
36
|
+
navigation: NavigationBridge;
|
|
37
|
+
theme: ThemeBridge;
|
|
38
|
+
toast: ToastBridge;
|
|
39
|
+
storage: StorageBridge;
|
|
40
|
+
events: EventsBridge;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type LoaderStatus = "loading" | "ready" | "error";
|
|
44
|
+
|
|
45
|
+
export interface UseVibeLoaderResult {
|
|
46
|
+
status: LoaderStatus;
|
|
47
|
+
error: Error | null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface UseVibeBridgeOptions {
|
|
51
|
+
appId: string;
|
|
52
|
+
permissions: Bridge extends infer B ? (keyof B)[] : never;
|
|
53
|
+
overrides?: Partial<AdapterOverrides>;
|
|
54
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
2
|
+
import type { Bridge, BridgeCapability } from "@aiworkbench/vibe-types";
|
|
3
|
+
import { useSession } from "next-auth/react";
|
|
4
|
+
import { useRouter } from "next/navigation";
|
|
5
|
+
import { toast } from "sonner";
|
|
6
|
+
import { createAuthAdapter } from "../adapters/auth";
|
|
7
|
+
import { createApiAdapter } from "../adapters/api";
|
|
8
|
+
import { createNavigationAdapter } from "../adapters/navigation";
|
|
9
|
+
import { createThemeAdapter } from "../adapters/theme";
|
|
10
|
+
import { createToastAdapter } from "../adapters/toast";
|
|
11
|
+
import { createStorageAdapter } from "../adapters/storage";
|
|
12
|
+
import { createEventsAdapter } from "../adapters/events";
|
|
13
|
+
import { filterByPermissions } from "../core/permissions";
|
|
14
|
+
import type { AdapterOverrides } from "../core/types";
|
|
15
|
+
|
|
16
|
+
export interface UseVibeBridgeOptions {
|
|
17
|
+
appId: string;
|
|
18
|
+
permissions: BridgeCapability[];
|
|
19
|
+
overrides?: Partial<AdapterOverrides>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function useVibeBridge(options: UseVibeBridgeOptions): Bridge {
|
|
23
|
+
const { appId, permissions, overrides } = options;
|
|
24
|
+
const { data: session } = useSession();
|
|
25
|
+
const router = useRouter();
|
|
26
|
+
|
|
27
|
+
return useMemo(() => {
|
|
28
|
+
const authAdapter =
|
|
29
|
+
overrides?.auth ?? createAuthAdapter({ session: session ?? null });
|
|
30
|
+
|
|
31
|
+
const fullBridge: Bridge = {
|
|
32
|
+
auth: authAdapter,
|
|
33
|
+
api:
|
|
34
|
+
overrides?.api ??
|
|
35
|
+
createApiAdapter({ getToken: () => authAdapter.getToken() }),
|
|
36
|
+
navigation:
|
|
37
|
+
overrides?.navigation ??
|
|
38
|
+
createNavigationAdapter({ push: (path) => router.push(path) }),
|
|
39
|
+
theme: overrides?.theme ?? createThemeAdapter(),
|
|
40
|
+
toast:
|
|
41
|
+
overrides?.toast ??
|
|
42
|
+
createToastAdapter({ toast }),
|
|
43
|
+
storage: overrides?.storage ?? createStorageAdapter(appId),
|
|
44
|
+
events: overrides?.events ?? createEventsAdapter(appId),
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
return filterByPermissions(fullBridge, permissions);
|
|
48
|
+
}, [session, router, appId, permissions, overrides]);
|
|
49
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
import { onEvent, type VibeEventDetail } from "../core/event-bus";
|
|
3
|
+
|
|
4
|
+
export function useVibeEvents(
|
|
5
|
+
event: string,
|
|
6
|
+
handler: (payload: Record<string, unknown>, sourceAppId: string) => void,
|
|
7
|
+
): void {
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
return onEvent(event, (detail: VibeEventDetail) => {
|
|
10
|
+
handler(detail.payload, detail.sourceAppId);
|
|
11
|
+
});
|
|
12
|
+
}, [event, handler]);
|
|
13
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { useState, useEffect } from "react";
|
|
2
|
+
import { loadScript, waitForElement } from "../core/loader";
|
|
3
|
+
import type { LoaderStatus, UseVibeLoaderResult } from "../core/types";
|
|
4
|
+
|
|
5
|
+
export function useVibeLoader(
|
|
6
|
+
src: string,
|
|
7
|
+
tagName: string,
|
|
8
|
+
): UseVibeLoaderResult {
|
|
9
|
+
const [status, setStatus] = useState<LoaderStatus>("loading");
|
|
10
|
+
const [error, setError] = useState<Error | null>(null);
|
|
11
|
+
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
let cancelled = false;
|
|
14
|
+
|
|
15
|
+
setStatus("loading");
|
|
16
|
+
setError(null);
|
|
17
|
+
|
|
18
|
+
loadScript(src)
|
|
19
|
+
.then(() => waitForElement(tagName))
|
|
20
|
+
.then(() => {
|
|
21
|
+
if (!cancelled) {
|
|
22
|
+
setStatus("ready");
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
.catch((err: unknown) => {
|
|
26
|
+
if (!cancelled) {
|
|
27
|
+
const e =
|
|
28
|
+
err instanceof Error
|
|
29
|
+
? err
|
|
30
|
+
: new Error(`Failed to load vibe app: ${tagName}`);
|
|
31
|
+
setError(e);
|
|
32
|
+
setStatus("error");
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return () => {
|
|
37
|
+
cancelled = true;
|
|
38
|
+
};
|
|
39
|
+
}, [src, tagName]);
|
|
40
|
+
|
|
41
|
+
return { status, error };
|
|
42
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Components
|
|
2
|
+
export { VibeHost } from "./components/VibeHost";
|
|
3
|
+
export { VibeErrorBoundary } from "./components/VibeErrorBoundary";
|
|
4
|
+
|
|
5
|
+
// Hooks
|
|
6
|
+
export { useVibeBridge } from "./hooks/useVibeBridge";
|
|
7
|
+
export { useVibeLoader } from "./hooks/useVibeLoader";
|
|
8
|
+
export { useVibeEvents } from "./hooks/useVibeEvents";
|
|
9
|
+
|
|
10
|
+
// Adapter factories
|
|
11
|
+
export { createAuthAdapter } from "./adapters/auth";
|
|
12
|
+
export { createApiAdapter } from "./adapters/api";
|
|
13
|
+
export { createNavigationAdapter } from "./adapters/navigation";
|
|
14
|
+
export { createThemeAdapter } from "./adapters/theme";
|
|
15
|
+
export { createToastAdapter } from "./adapters/toast";
|
|
16
|
+
export { createStorageAdapter } from "./adapters/storage";
|
|
17
|
+
export { createEventsAdapter } from "./adapters/events";
|
|
18
|
+
|
|
19
|
+
// Core utilities
|
|
20
|
+
export { filterByPermissions } from "./core/permissions";
|
|
21
|
+
export { getEventBus, emitEvent } from "./core/event-bus";
|
|
22
|
+
|
|
23
|
+
// Types
|
|
24
|
+
export type {
|
|
25
|
+
VibeHostProps,
|
|
26
|
+
AdapterOverrides,
|
|
27
|
+
LoaderStatus,
|
|
28
|
+
UseVibeLoaderResult,
|
|
29
|
+
} from "./core/types";
|
|
30
|
+
export type { AuthAdapterDeps } from "./adapters/auth";
|
|
31
|
+
export type { ApiAdapterDeps } from "./adapters/api";
|
|
32
|
+
export type { NavigationAdapterDeps } from "./adapters/navigation";
|
|
33
|
+
export type { ToastAdapterDeps } from "./adapters/toast";
|