@iota-uz/sdk 0.4.20 → 0.4.22

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.
Files changed (40) hide show
  1. package/README.md +20 -0
  2. package/dist/applet/core.cjs +8 -4
  3. package/dist/applet/core.cjs.map +1 -1
  4. package/dist/applet/core.mjs +8 -4
  5. package/dist/applet/core.mjs.map +1 -1
  6. package/dist/applet/devtools.cjs +24 -8
  7. package/dist/applet/devtools.cjs.map +1 -1
  8. package/dist/applet/devtools.mjs +24 -8
  9. package/dist/applet/devtools.mjs.map +1 -1
  10. package/dist/applet/host.cjs +47 -16
  11. package/dist/applet/host.cjs.map +1 -1
  12. package/dist/applet/host.mjs +47 -16
  13. package/dist/applet/host.mjs.map +1 -1
  14. package/dist/applet/vite.cjs +12 -4
  15. package/dist/applet/vite.cjs.map +1 -1
  16. package/dist/applet/vite.mjs +12 -4
  17. package/dist/applet/vite.mjs.map +1 -1
  18. package/dist/applet-runtime/index.cjs.map +1 -1
  19. package/dist/applet-runtime/index.mjs.map +1 -1
  20. package/dist/bichat/index.cjs +6971 -2646
  21. package/dist/bichat/index.cjs.map +1 -1
  22. package/dist/bichat/index.css +11 -1
  23. package/dist/bichat/index.css.map +1 -1
  24. package/dist/bichat/index.d.cts +453 -157
  25. package/dist/bichat/index.d.ts +453 -157
  26. package/dist/bichat/index.mjs +6969 -2652
  27. package/dist/bichat/index.mjs.map +1 -1
  28. package/dist/bichat/styles.css +17 -4
  29. package/dist/bichat/tailwind.cjs.map +1 -1
  30. package/dist/bichat/tailwind.mjs.map +1 -1
  31. package/dist/index.cjs +73 -26
  32. package/dist/index.cjs.map +1 -1
  33. package/dist/index.mjs +73 -26
  34. package/dist/index.mjs.map +1 -1
  35. package/package.json +10 -3
  36. package/tailwind/compiled.css +1 -1
  37. package/tailwind/create-config.cjs +50 -2
  38. package/tailwind/iota.css +6 -6
  39. package/tailwind/sdk-content.cjs +177 -0
  40. package/tailwind/sdk-theme.cjs +12 -0
package/README.md CHANGED
@@ -50,10 +50,14 @@ From a repo that contains an applet (e.g. iota-sdk or EAI):
50
50
  applet doctor # environment and config diagnostics
51
51
  applet rpc gen --name <applet-name>
52
52
  applet rpc check --name <applet-name>
53
+ applet rpc watch --name <applet-name>
53
54
  applet deps check
55
+ applet sdk link --sdk-root ../../applets
56
+ applet sdk unlink
54
57
  applet check # deps + RPC drift for all applets
55
58
  applet schema export --name <applet>
56
59
  applet dev # start dev environment (all configured applets)
60
+ applet dev --rpc-watch # include RPC codegen watch during dev
57
61
  applet build [name] # build production bundle
58
62
  applet list # list configured applets
59
63
  applet secrets set --name <applet> --key OPENAI_API_KEY --value ...
@@ -63,6 +67,22 @@ applet secrets delete --name <applet> --key OPENAI_API_KEY
63
67
 
64
68
  - **Specific version:** `go install github.com/iota-uz/applets/cmd/applet@v0.4.4`
65
69
  - **Shell completion:** `applet completion bash`, `applet completion zsh`, or `applet completion fish` — see `applet completion --help` for install instructions.
70
+ - **Local SDK overrides:** `applet sdk link` writes local-only settings to `.applets/local.env` (gitignored) and avoids committing link overrides into package manifests/lockfiles.
71
+
72
+ ---
73
+
74
+ ## DX Migration Notes (Breaking)
75
+
76
+ - `applets/` is the canonical source of `@iota-uz/sdk`; `iota-sdk/package.json` is no longer the publish source.
77
+ - Local SDK iteration should use `applet sdk link --sdk-root ../../applets` and `applet sdk unlink` instead of committing `pnpm` overrides/workspace links.
78
+ - `applet dev` now detects `go.work` dependencies and automatically watches/restarts critical processes when dependency code changes.
79
+ - Keep `.applets/local.env` local-only. It is intentionally gitignored and must not be committed.
80
+
81
+ ### Release flow
82
+
83
+ 1. Publish SDK changes from `applets/` (bump `applets/package.json` version and release).
84
+ 2. Upgrade consumers (`eai/back`, applet web packages, etc.) to the published version.
85
+ 3. Commit only version upgrades in consumer repos; never commit local-link overrides.
66
86
 
67
87
  ---
68
88
 
@@ -181,10 +181,11 @@ function useStreaming() {
181
181
  setIsStreaming(true);
182
182
  const controller = new AbortController();
183
183
  abortControllerRef.current = controller;
184
- if (signal) {
185
- signal.addEventListener("abort", () => {
186
- controller.abort();
187
- });
184
+ const onExternalAbort = signal ? () => {
185
+ controller.abort();
186
+ } : void 0;
187
+ if (signal && onExternalAbort) {
188
+ signal.addEventListener("abort", onExternalAbort);
188
189
  }
189
190
  try {
190
191
  for await (const chunk of generator) {
@@ -199,6 +200,9 @@ function useStreaming() {
199
200
  }
200
201
  throw error;
201
202
  } finally {
203
+ if (signal && onExternalAbort) {
204
+ signal.removeEventListener("abort", onExternalAbort);
205
+ }
202
206
  setIsStreaming(false);
203
207
  abortControllerRef.current = null;
204
208
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../ui/src/applet-core/context/AppletContext.tsx","../../ui/src/applet-core/context/ConfigProvider.tsx","../../ui/src/applet-core/hooks/useAppletContext.ts","../../ui/src/applet-core/hooks/useConfig.ts","../../ui/src/applet-core/hooks/useUser.ts","../../ui/src/applet-core/hooks/usePermissions.ts","../../ui/src/applet-core/hooks/useTranslation.ts","../../ui/src/applet-core/hooks/useSession.ts","../../ui/src/applet-core/hooks/useRoute.ts","../../ui/src/applet-core/hooks/useStreaming.ts","../../ui/src/applet-core/hooks/useAppletRuntime.ts"],"names":["createContext","useContext","jsx","useAppletContext","useMemo","useState","useRef","useCallback"],"mappings":";;;;;;AAOA,IAAM,aAAA,GAAgBA,oBAAqC,IAAI,CAAA;AAE/D,IAAM,gBAA6C,CAAC,MAAA,EAAQ,UAAU,QAAA,EAAU,QAAA,EAAU,SAAS,SAAS,CAAA;AAE5G,SAAS,sBAAA,CAAuB,OAAgB,SAAA,EAAmC;AACjF,EAAA,IAAI,CAAC,KAAA,IAAS,OAAO,KAAA,KAAU,QAAA,EAAU;AACvC,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,EAAG,SAAS,CAAA,0BAAA,EAA6B,OAAO,KAAK,CAAA,CAAE,CAAA;AAAA,EACzE;AACA,EAAA,MAAM,GAAA,GAAM,KAAA;AACZ,EAAA,MAA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AmB,QAAQ,KAAA,EAAM;AAAA,IACnC;AACA,IAAA,cAAA,CAAe,KAAK,CAAA;AAAA,EACtB,CAAA,EAAG,EAAE,CAAA;AAEL,EAAA,MAAM,KAAA,GAAQA,kBAAY,MAAM;AAC9B,IAAA,kBAAA,CAAmB,OAAA,GAAU,IAAA;AAC7B,IAAA,cAAA,CAAe,KAAK,CAAA;AAAA,EACtB,CAAA,EAAG,EAAE,CAAA;AAEL,EAAA,OAAO;AAAA,IACL,WAAA;AAAA,IACA,aAAA;AAAA,IACA,MAAA;AAAA,IACA;AAAA,GACF;AACF;;;AC1EO,SAAS,gBAAA,GAAkC;AAChD,EAAA,MAAM,SAAS,SAAA,EAAU;AAEzB,EAAA,MAAM,QAAA,GAAW,OAAO,QAAA,IAAY,EAAA;AACpC,EAAA,MAAM,kBAAA,GAAqB,SAAS,QAAA,CAAS,GAAG,IAAI,QAAA,CAAS,KAAA,CAAM,CAAA,EAAG,EAAE,CAAA,GAAI,QAAA;AAC5E,EAAA,MAAM,cAAA,GAAiB,MAAA,CAAO,cAAA,IAAkB,CAAA,EAAG,sBAAsB,EAAE,CAAA,OAAA,CAAA;AAC3E,EAAA,MAAM,cAAc,MAAA,CAAO,aAAA;AAC3B,EAAA,MAAM,YAAY,MAAA,CAAO,SAAA;AAEzB,EAAA,OAAO,EAAE,QAAA,EAAU,kBAAA,EAAoB,cAAA,EAAgB,aAAa,SAAA,EAAU;AAChF","file":"core.cjs","sourcesContent":["import { createContext, useContext, ReactNode } from 'react'\nimport type { InitialContext } from '../types'\n\n/**\n * AppletContext provides access to the global context injected by the backend.\n * The context is read from window.__*_CONTEXT__ (configured via windowKey).\n */\nconst AppletContext = createContext<InitialContext | null>(null)\n\nconst REQUIRED_KEYS: Array<keyof InitialContext> = ['user', 'tenant', 'locale', 'config', 'route', 'session']\n\nfunction validateInitialContext(value: unknown, windowKey: string): InitialContext {\n if (!value || typeof value !== 'object') {\n throw new Error(`${windowKey}: expected an object, got ${typeof value}`)\n }\n const obj = value as Record<string, unknown>\n const missing = REQUIRED_KEYS.filter((k) => !(k in obj))\n if (missing.length > 0) {\n throw new Error(`${windowKey}: missing required keys: ${missing.join(', ')}`)\n }\n\n // Deep validation for nested required fields\n const user = obj.user as Record<string, unknown> | undefined\n if (!user || typeof user !== 'object') {\n throw new Error(`${windowKey}.user: expected an object, got ${typeof user}`)\n }\n if (typeof user.id !== 'number') {\n throw new Error(`${windowKey}.user.id: expected number, got ${typeof user.id}`)\n }\n if (!Array.isArray(user.permissions)) {\n throw new Error(`${windowKey}.user.permissions: expected array, got ${typeof user.permissions}`)\n }\n\n const tenant = obj.tenant as Record<string, unknown> | undefined\n if (!tenant || typeof tenant !== 'object') {\n throw new Error(`${windowKey}.tenant: expected an object, got ${typeof tenant}`)\n }\n if (typeof tenant.id !== 'string') {\n throw new Error(`${windowKey}.tenant.id: expected string, got ${typeof tenant.id}`)\n }\n\n const session = obj.session as Record<string, unknown> | undefined\n if (!session || typeof session !== 'object') {\n throw new Error(`${windowKey}.session: expected an object, got ${typeof session}`)\n }\n if (typeof session.csrfToken !== 'string') {\n throw new Error(`${windowKey}.session.csrfToken: expected string, got ${typeof session.csrfToken}`)\n }\n if (typeof session.expiresAt !== 'number') {\n throw new Error(`${windowKey}.session.expiresAt: expected number, got ${typeof session.expiresAt}`)\n }\n if (typeof session.refreshURL !== 'string') {\n throw new Error(`${windowKey}.session.refreshURL: expected string, got ${typeof session.refreshURL}`)\n }\n\n const config = obj.config\n if (!config || typeof config !== 'object') {\n throw new Error(`${windowKey}.config: expected object, got ${typeof config}`)\n }\n\n return value as InitialContext\n}\n\nexport interface AppletProviderProps {\n children: ReactNode\n windowKey: string\n context?: InitialContext\n}\n\n/**\n * AppletProvider reads context from window global and provides it to hooks.\n *\n * Usage:\n * <AppletProvider windowKey=\"__APPLET_CONTEXT__\">\n * <App />\n * </AppletProvider>\n */\nexport function AppletProvider({ children, windowKey, context }: AppletProviderProps) {\n // Use provided context or read from window global\n const raw = context ?? (window as unknown as Record<string, unknown>)[windowKey]\n\n if (!raw) {\n throw new Error(`${windowKey} not found on window. Ensure backend context injection is working.`)\n }\n\n const initialContext = validateInitialContext(raw, windowKey)\n\n return (\n <AppletContext.Provider value={initialContext}>\n {children}\n </AppletContext.Provider>\n )\n}\n\n/**\n * useAppletContext provides access to the full applet context.\n * Use specialized hooks (useUser, useConfig, etc.) for specific context parts.\n */\nexport function useAppletContext<T = InitialContext>(): T {\n const context = useContext(AppletContext)\n if (!context) {\n throw new Error('useAppletContext must be used within AppletProvider')\n }\n return context as T\n}\n","import { createContext, useContext, ReactNode } from 'react'\nimport type { InitialContext } from '../types'\n\n/**\n * ConfigProvider is an alternative to AppletProvider that accepts context via props\n * instead of reading from window global. Useful for testing and server-side rendering.\n */\n\nconst ConfigContext = createContext<InitialContext | null>(null)\n\nexport interface ConfigProviderProps {\n children: ReactNode\n config: InitialContext\n}\n\n/**\n * ConfigProvider accepts context configuration via props.\n *\n * Usage:\n * <ConfigProvider config={initialContext}>\n * <App />\n * </ConfigProvider>\n */\nexport function ConfigProvider({ children, config }: ConfigProviderProps) {\n return (\n <ConfigContext.Provider value={config}>\n {children}\n </ConfigContext.Provider>\n )\n}\n\n/**\n * useConfigContext provides access to the applet context when using ConfigProvider.\n */\nexport function useConfigContext<T = InitialContext>(): T {\n const context = useContext(ConfigContext)\n if (!context) {\n throw new Error('useConfigContext must be used within ConfigProvider')\n }\n return context as T\n}\n","import type { InitialContext } from '../types'\n\n/**\n * useAppletContext provides direct access to the window global context.\n * This is a standalone version that doesn't require AppletProvider.\n *\n * Usage:\n * const context = useAppletContext('__APPLET_CONTEXT__')\n *\n * Note: Prefer using AppletProvider + context hooks for better type safety\n * and testability. Use this hook only when provider setup is not possible.\n */\nexport function useAppletContext<T = InitialContext>(windowKey: string): T {\n const context = (window as unknown as Record<string, unknown>)[windowKey]\n\n if (!context) {\n throw new Error(`${windowKey} not found on window. Ensure backend context injection is working.`)\n }\n\n return context as T\n}\n","import { useAppletContext } from '../context/AppletContext'\nimport type { AppConfig } from '../types'\n\n/**\n * useConfig provides access to applet configuration (endpoints, etc.)\n *\n * Usage:\n * const { graphQLEndpoint, streamEndpoint } = useConfig()\n */\nexport function useConfig(): AppConfig {\n const { config } = useAppletContext()\n return config\n}\n","import { useAppletContext } from '../context/AppletContext'\nimport type { UserContext } from '../types'\n\n/**\n * useUser provides access to current user information.\n *\n * Usage:\n * const { id, email, firstName, lastName, permissions } = useUser()\n */\nexport function useUser(): UserContext {\n const { user } = useAppletContext()\n return user\n}\n","import { useAppletContext } from '../context/AppletContext'\nimport type { PermissionsHook } from '../types'\n\n/**\n * usePermissions provides permission checking utilities.\n * All user permissions are automatically passed from backend.\n *\n * Usage:\n * const { hasPermission, hasAnyPermission } = usePermissions()\n *\n * if (hasPermission('BiChat.Access')) {\n * // User has bichat access\n * }\n *\n * if (hasAnyPermission('finance.view', 'finance.edit')) {\n * // User has at least one of these permissions\n * }\n */\nexport function usePermissions(): PermissionsHook {\n const { user } = useAppletContext()\n\n const hasPermission = (permission: string): boolean => {\n return user.permissions.includes(permission)\n }\n\n const hasAnyPermission = (...permissions: string[]): boolean => {\n return permissions.some(p => user.permissions.includes(p))\n }\n\n return {\n hasPermission,\n hasAnyPermission,\n permissions: user.permissions\n }\n}\n","import { useAppletContext } from '../context/AppletContext'\nimport type { TranslationHook } from '../types'\n\n/**\n * useTranslation provides i18n translation utilities.\n * All translations are automatically passed from backend locale bundle.\n *\n * Usage:\n * const { t, language } = useTranslation()\n *\n * // Simple translation\n * t('BiChat.Title') // Returns translated text\n *\n * // Translation with interpolation\n * t('Common.WelcomeMessage', { name: 'John' })\n * // If translation is \"Welcome {name}!\" -> Returns \"Welcome John!\"\n *\n * React uses same keys as Go backend:\n * Go: pageCtx.T(\"BiChat.Title\")\n * React: t(\"BiChat.Title\")\n */\nexport function useTranslation(): TranslationHook {\n const { locale } = useAppletContext()\n\n const t = (key: string, params?: Record<string, unknown>): string => {\n let text = locale.translations[key] || key\n\n // Simple interpolation: \"Hello {name}\" with {name: \"World\"}\n if (params) {\n Object.entries(params).forEach(([k, v]) => {\n text = text.replace(new RegExp(`\\\\{${k}\\\\}`, 'g'), String(v))\n })\n }\n\n return text\n }\n\n return {\n t,\n language: locale.language\n }\n}\n","import { useMemo } from 'react'\nimport { useAppletContext } from '../context/AppletContext'\nimport type { SessionHook } from '../types'\n\nexport interface UseSessionOptions {\n loginPath?: string\n}\n\n/**\n * useSession provides session and authentication handling utilities.\n *\n * Usage:\n * const { isExpiringSoon, refreshSession, csrfToken } = useSession()\n *\n * // Check if session is expiring soon (5 min buffer)\n * if (isExpiringSoon) {\n * await refreshSession()\n * }\n *\n * // Include CSRF token in requests\n * fetch('/api/endpoint', {\n * headers: { 'X-CSRF-Token': csrfToken }\n * })\n */\nexport function useSession(options?: UseSessionOptions): SessionHook {\n const { session } = useAppletContext()\n const loginPath = options?.loginPath ?? '/login'\n\n // Check if session is expiring soon (5 minute buffer)\n const isExpiringSoon = useMemo(() => {\n const bufferMs = 5 * 60 * 1000 // 5 minutes\n return session.expiresAt - Date.now() < bufferMs\n }, [session.expiresAt])\n\n const refreshSession = async (): Promise<void> => {\n const response = await fetch(session.refreshURL, {\n method: 'POST',\n headers: {\n 'X-CSRF-Token': session.csrfToken\n }\n })\n\n if (!response.ok) {\n // Session refresh failed - redirect to login with return URL\n const returnUrl = encodeURIComponent(window.location.pathname)\n window.location.href = `${loginPath}?redirect=${returnUrl}`\n return\n }\n\n // Dispatch event for CSRF token update\n const newToken = response.headers.get('X-CSRF-Token')\n if (newToken) {\n window.dispatchEvent(\n new CustomEvent('iota:csrf-refresh', {\n detail: { token: newToken }\n })\n )\n }\n }\n\n return {\n isExpiringSoon,\n refreshSession,\n csrfToken: session.csrfToken,\n expiresAt: session.expiresAt\n }\n}\n","import { useAppletContext } from '../context/AppletContext'\nimport type { RouteContext } from '../types'\n\n/**\n * useRoute provides access to the current route context.\n * Route context is initialized from the backend and includes path, params, and query.\n *\n * Usage:\n * const { path, params, query } = useRoute()\n *\n * // Example values:\n * // path: \"/sessions/123\"\n * // params: { id: \"123\" }\n * // query: { tab: \"history\" }\n */\nexport function useRoute(): RouteContext {\n const { route } = useAppletContext()\n return route\n}\n","import { useState, useRef, useCallback } from 'react'\nimport type { StreamingHook } from '../types'\n\n/**\n * useStreaming provides SSE (Server-Sent Events) streaming utilities with cancellation support.\n *\n * Usage:\n * const { isStreaming, processStream, cancel, reset } = useStreaming()\n *\n * // Process async generator stream\n * await processStream(messageStream, (chunk) => {\n * console.log('Received:', chunk)\n * })\n *\n * // Cancel ongoing stream\n * cancel()\n *\n * // Reset state after stream completion\n * reset()\n */\nexport function useStreaming(): StreamingHook {\n const [isStreaming, setIsStreaming] = useState(false)\n const abortControllerRef = useRef<AbortController | null>(null)\n\n const processStream = useCallback(\n async <T,>(\n generator: AsyncGenerator<T>,\n onChunk: (chunk: T) => void,\n signal?: AbortSignal\n ): Promise<void> => {\n setIsStreaming(true)\n\n // Create abort controller if not provided\n const controller = new AbortController()\n abortControllerRef.current = controller\n\n // Listen to external signal if provided\n if (signal) {\n signal.addEventListener('abort', () => {\n controller.abort()\n })\n }\n\n try {\n for await (const chunk of generator) {\n // Check if stream was cancelled\n if (controller.signal.aborted) {\n break\n }\n\n onChunk(chunk)\n }\n } catch (error) {\n // Stream was cancelled or errored\n if (controller.signal.aborted) {\n // Cancellation is expected, don't throw\n return\n }\n throw error\n } finally {\n setIsStreaming(false)\n abortControllerRef.current = null\n }\n },\n []\n )\n\n const cancel = useCallback(() => {\n if (abortControllerRef.current) {\n abortControllerRef.current.abort()\n }\n setIsStreaming(false)\n }, [])\n\n const reset = useCallback(() => {\n abortControllerRef.current = null\n setIsStreaming(false)\n }, [])\n\n return {\n isStreaming,\n processStream,\n cancel,\n reset\n }\n}\n","import { useConfig } from './useConfig'\n\nexport type ShellMode = 'embedded' | 'standalone'\n\nexport interface AppletRuntime {\n basePath: string\n assetsBasePath: string\n rpcEndpoint?: string\n shellMode?: ShellMode\n}\n\nexport function useAppletRuntime(): AppletRuntime {\n const config = useConfig()\n\n const basePath = config.basePath ?? ''\n const normalizedBasePath = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath\n const assetsBasePath = config.assetsBasePath ?? `${normalizedBasePath || ''}/assets`\n const rpcEndpoint = config.rpcUIEndpoint\n const shellMode = config.shellMode\n\n return { basePath: normalizedBasePath, assetsBasePath, rpcEndpoint, shellMode }\n}\n"]}
1
+ {"version":3,"sources":["../../ui/src/applet-core/context/AppletContext.tsx","../../ui/src/applet-core/context/ConfigProvider.tsx","../../ui/src/applet-core/hooks/useAppletContext.ts","../../ui/src/applet-core/hooks/useConfig.ts","../../ui/src/applet-core/hooks/useUser.ts","../../ui/src/applet-core/hooks/usePermissions.ts","../../ui/src/applet-core/hooks/useTranslation.ts","../../ui/src/applet-core/hooks/useSession.ts","../../ui/src/applet-core/hooks/useRoute.ts","../../ui/src/applet-core/hooks/useStreaming.ts","../../ui/src/applet-core/hooks/useAppletRuntime.ts"],"names":["createContext","useContext","jsx","useAppletContext","useMemo","useState","useRef","useCallback"],"mappings":";;;;;;AAOA,IAAM,aAAA,GAAgBA,oBAAqC,IAAI,CAAA;AAE/D,IAAM,gBAA6C,CAAC,MAAA,EAAQ,UAAU,QAAA,EAAU,QAAA,EAAU,SAAS,SAAS,CAAA;AAE5G,SAAS,sBAAA,CAAuB,OAAgB,SAAA,EAAmC;AACjF,EAAA,IAAI,CAAC,KAAA,IAAS,OAAO,KAAA,KAAU,QAAA,EAAU;AACvC,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,EAAG,SAAS,CAAA,0BAAA,EAA6B,OAAO,KAAK,CAAA,CAAE,CAAA;AAAA,EACzE;AACA,EAAA,MAAM,GAAA,GAAM,KAAA;AACZ,EAAA,MAAM,UAAU,aAAA,CAAc,MAAA,CAAO,CAAC,CAAA,KAAM,EAAE,KAAK,GAAA,CAAI,CAAA;AACvD,EAAA,IAAI,OAAA,CAAQ,SAAS,CAAA,EAAG;AACtB,IAAA,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EAAS;AAC9B,MAAA,kBAAA,CAAmB,QAAQ,KAAA,EAAM;AAAA,IACnC;AACA,IAAA,cAAA,CAAe,KAAK,CAAA;AAAA,EACtB,CAAA,EAAG,EAAE,CAAA;AAEL,EAAA,MAAM,KAAA,GAAQA,kBAAY,MAAM;AAC9B,IAAA,kBAAA,CAAmB,OAAA,GAAU,IAAA;AAC7B,IAAA,cAAA,CAAe,KAAK,CAAA;AAAA,EACtB,CAAA,EAAG,EAAE,CAAA;AAEL,EAAA,OAAO;AAAA,IACL,WAAA;AAAA,IACA,aAAA;AAAA,IACA,MAAA;AAAA,IACA;AAAA,GACF;AACF;;;AC5EO,SAAS,gBAAA,GAAkC;AAChD,EAAA,MAAM,SAAS,SAAA,EAAU;AAEzB,EAAA,MAAM,QAAA,GAAW,OAAO,QAAA,IAAY,EAAA;AACpC,EAAA,MAAM,kBAAA,GAAqB,SAAS,QAAA,CAAS,GAAG,IAAI,QAAA,CAAS,KAAA,CAAM,CAAA,EAAG,EAAE,CAAA,GAAI,QAAA;AAC5E,EAAA,MAAM,cAAA,GAAiB,MAAA,CAAO,cAAA,IAAkB,CAAA,EAAG,sBAAsB,EAAE,CAAA,OAAA,CAAA;AAC3E,EAAA,MAAM,cAAc,MAAA,CAAO,aAAA;AAC3B,EAAA,MAAM,YAAY,MAAA,CAAO,SAAA;AAEzB,EAAA,OAAO,EAAE,QAAA,EAAU,kBAAA,EAAoB,cAAA,EAAgB,aAAa,SAAA,EAAU;AAChF","file":"core.cjs","sourcesContent":["import { createContext, useContext, ReactNode } from 'react';\nimport type { InitialContext } from '../types';\n\n/**\n * AppletContext provides access to the global context injected by the backend.\n * The context is read from window.__*_CONTEXT__ (configured via windowKey).\n */\nconst AppletContext = createContext<InitialContext | null>(null);\n\nconst REQUIRED_KEYS: Array<keyof InitialContext> = ['user', 'tenant', 'locale', 'config', 'route', 'session'];\n\nfunction validateInitialContext(value: unknown, windowKey: string): InitialContext {\n if (!value || typeof value !== 'object') {\n throw new Error(`${windowKey}: expected an object, got ${typeof value}`);\n }\n const obj = value as Record<string, unknown>;\n const missing = REQUIRED_KEYS.filter((k) => !(k in obj));\n if (missing.length > 0) {\n throw new Error(`${windowKey}: missing required keys: ${missing.join(', ')}`);\n }\n\n // Deep validation for nested required fields\n const user = obj.user as Record<string, unknown> | undefined;\n if (!user || typeof user !== 'object') {\n throw new Error(`${windowKey}.user: expected an object, got ${typeof user}`);\n }\n if (typeof user.id !== 'number') {\n throw new Error(`${windowKey}.user.id: expected number, got ${typeof user.id}`);\n }\n if (!Array.isArray(user.permissions)) {\n throw new Error(`${windowKey}.user.permissions: expected array, got ${typeof user.permissions}`);\n }\n\n const tenant = obj.tenant as Record<string, unknown> | undefined;\n if (!tenant || typeof tenant !== 'object') {\n throw new Error(`${windowKey}.tenant: expected an object, got ${typeof tenant}`);\n }\n if (typeof tenant.id !== 'string') {\n throw new Error(`${windowKey}.tenant.id: expected string, got ${typeof tenant.id}`);\n }\n\n const session = obj.session as Record<string, unknown> | undefined;\n if (!session || typeof session !== 'object') {\n throw new Error(`${windowKey}.session: expected an object, got ${typeof session}`);\n }\n if (typeof session.csrfToken !== 'string') {\n throw new Error(`${windowKey}.session.csrfToken: expected string, got ${typeof session.csrfToken}`);\n }\n if (typeof session.expiresAt !== 'number') {\n throw new Error(`${windowKey}.session.expiresAt: expected number, got ${typeof session.expiresAt}`);\n }\n if (typeof session.refreshURL !== 'string') {\n throw new Error(`${windowKey}.session.refreshURL: expected string, got ${typeof session.refreshURL}`);\n }\n\n const config = obj.config;\n if (!config || typeof config !== 'object') {\n throw new Error(`${windowKey}.config: expected object, got ${typeof config}`);\n }\n\n return value as InitialContext;\n}\n\nexport interface AppletProviderProps {\n children: ReactNode\n windowKey: string\n context?: InitialContext\n}\n\n/**\n * AppletProvider reads context from window global and provides it to hooks.\n *\n * Usage:\n * <AppletProvider windowKey=\"__APPLET_CONTEXT__\">\n * <App />\n * </AppletProvider>\n */\nexport function AppletProvider({ children, windowKey, context }: AppletProviderProps) {\n // Use provided context or read from window global\n const raw = context ?? (window as unknown as Record<string, unknown>)[windowKey];\n\n if (!raw) {\n throw new Error(`${windowKey} not found on window. Ensure backend context injection is working.`);\n }\n\n const initialContext = validateInitialContext(raw, windowKey);\n\n return (\n <AppletContext.Provider value={initialContext}>\n {children}\n </AppletContext.Provider>\n );\n}\n\n/**\n * useAppletContext provides access to the full applet context.\n * Use specialized hooks (useUser, useConfig, etc.) for specific context parts.\n */\nexport function useAppletContext<T = InitialContext>(): T {\n const context = useContext(AppletContext);\n if (!context) {\n throw new Error('useAppletContext must be used within AppletProvider');\n }\n return context as T;\n}\n","import { createContext, useContext, ReactNode } from 'react';\nimport type { InitialContext } from '../types';\n\n/**\n * ConfigProvider is an alternative to AppletProvider that accepts context via props\n * instead of reading from window global. Useful for testing and server-side rendering.\n */\n\nconst ConfigContext = createContext<InitialContext | null>(null);\n\nexport interface ConfigProviderProps {\n children: ReactNode\n config: InitialContext\n}\n\n/**\n * ConfigProvider accepts context configuration via props.\n *\n * Usage:\n * <ConfigProvider config={initialContext}>\n * <App />\n * </ConfigProvider>\n */\nexport function ConfigProvider({ children, config }: ConfigProviderProps) {\n return (\n <ConfigContext.Provider value={config}>\n {children}\n </ConfigContext.Provider>\n );\n}\n\n/**\n * useConfigContext provides access to the applet context when using ConfigProvider.\n */\nexport function useConfigContext<T = InitialContext>(): T {\n const context = useContext(ConfigContext);\n if (!context) {\n throw new Error('useConfigContext must be used within ConfigProvider');\n }\n return context as T;\n}\n","import type { InitialContext } from '../types';\n\n/**\n * useAppletContext provides direct access to the window global context.\n * This is a standalone version that doesn't require AppletProvider.\n *\n * Usage:\n * const context = useAppletContext('__APPLET_CONTEXT__')\n *\n * Note: Prefer using AppletProvider + context hooks for better type safety\n * and testability. Use this hook only when provider setup is not possible.\n */\nexport function useAppletContext<T = InitialContext>(windowKey: string): T {\n const context = (window as unknown as Record<string, unknown>)[windowKey];\n\n if (!context) {\n throw new Error(`${windowKey} not found on window. Ensure backend context injection is working.`);\n }\n\n return context as T;\n}\n","import { useAppletContext } from '../context/AppletContext';\nimport type { AppConfig } from '../types';\n\n/**\n * useConfig provides access to applet configuration (endpoints, etc.)\n *\n * Usage:\n * const { graphQLEndpoint, streamEndpoint } = useConfig()\n */\nexport function useConfig(): AppConfig {\n const { config } = useAppletContext();\n return config;\n}\n","import { useAppletContext } from '../context/AppletContext';\nimport type { UserContext } from '../types';\n\n/**\n * useUser provides access to current user information.\n *\n * Usage:\n * const { id, email, firstName, lastName, permissions } = useUser()\n */\nexport function useUser(): UserContext {\n const { user } = useAppletContext();\n return user;\n}\n","import { useAppletContext } from '../context/AppletContext';\nimport type { PermissionsHook } from '../types';\n\n/**\n * usePermissions provides permission checking utilities.\n * All user permissions are automatically passed from backend.\n *\n * Usage:\n * const { hasPermission, hasAnyPermission } = usePermissions()\n *\n * if (hasPermission('BiChat.Access')) {\n * // User has bichat access\n * }\n *\n * if (hasAnyPermission('finance.view', 'finance.edit')) {\n * // User has at least one of these permissions\n * }\n */\nexport function usePermissions(): PermissionsHook {\n const { user } = useAppletContext();\n\n const hasPermission = (permission: string): boolean => {\n return user.permissions.includes(permission);\n };\n\n const hasAnyPermission = (...permissions: string[]): boolean => {\n return permissions.some(p => user.permissions.includes(p));\n };\n\n return {\n hasPermission,\n hasAnyPermission,\n permissions: user.permissions\n };\n}\n","import { useAppletContext } from '../context/AppletContext';\nimport type { TranslationHook } from '../types';\n\n/**\n * useTranslation provides i18n translation utilities.\n * All translations are automatically passed from backend locale bundle.\n *\n * Usage:\n * const { t, language } = useTranslation()\n *\n * // Simple translation\n * t('BiChat.Title') // Returns translated text\n *\n * // Translation with interpolation\n * t('Common.WelcomeMessage', { name: 'John' })\n * // If translation is \"Welcome {name}!\" -> Returns \"Welcome John!\"\n *\n * React uses same keys as Go backend:\n * Go: pageCtx.T(\"BiChat.Title\")\n * React: t(\"BiChat.Title\")\n */\nexport function useTranslation(): TranslationHook {\n const { locale } = useAppletContext();\n\n const t = (key: string, params?: Record<string, unknown>): string => {\n let text = locale.translations[key] || key;\n\n // Simple interpolation: \"Hello {name}\" with {name: \"World\"}\n if (params) {\n Object.entries(params).forEach(([k, v]) => {\n text = text.replace(new RegExp(`\\\\{${k}\\\\}`, 'g'), String(v));\n });\n }\n\n return text;\n };\n\n return {\n t,\n language: locale.language\n };\n}\n","import { useMemo } from 'react';\nimport { useAppletContext } from '../context/AppletContext';\nimport type { SessionHook } from '../types';\n\nexport interface UseSessionOptions {\n loginPath?: string\n}\n\n/**\n * useSession provides session and authentication handling utilities.\n *\n * Usage:\n * const { isExpiringSoon, refreshSession, csrfToken } = useSession()\n *\n * // Check if session is expiring soon (5 min buffer)\n * if (isExpiringSoon) {\n * await refreshSession()\n * }\n *\n * // Include CSRF token in requests\n * fetch('/api/endpoint', {\n * headers: { 'X-CSRF-Token': csrfToken }\n * })\n */\nexport function useSession(options?: UseSessionOptions): SessionHook {\n const { session } = useAppletContext();\n const loginPath = options?.loginPath ?? '/login';\n\n // Check if session is expiring soon (5 minute buffer)\n const isExpiringSoon = useMemo(() => {\n const bufferMs = 5 * 60 * 1000; // 5 minutes\n return session.expiresAt - Date.now() < bufferMs;\n }, [session.expiresAt]);\n\n const refreshSession = async (): Promise<void> => {\n const response = await fetch(session.refreshURL, {\n method: 'POST',\n headers: {\n 'X-CSRF-Token': session.csrfToken\n }\n });\n\n if (!response.ok) {\n // Session refresh failed - redirect to login with return URL\n const returnUrl = encodeURIComponent(window.location.pathname);\n window.location.href = `${loginPath}?redirect=${returnUrl}`;\n return;\n }\n\n // Dispatch event for CSRF token update\n const newToken = response.headers.get('X-CSRF-Token');\n if (newToken) {\n window.dispatchEvent(\n new CustomEvent('iota:csrf-refresh', {\n detail: { token: newToken }\n })\n );\n }\n };\n\n return {\n isExpiringSoon,\n refreshSession,\n csrfToken: session.csrfToken,\n expiresAt: session.expiresAt\n };\n}\n","import { useAppletContext } from '../context/AppletContext';\nimport type { RouteContext } from '../types';\n\n/**\n * useRoute provides access to the current route context.\n * Route context is initialized from the backend and includes path, params, and query.\n *\n * Usage:\n * const { path, params, query } = useRoute()\n *\n * // Example values:\n * // path: \"/sessions/123\"\n * // params: { id: \"123\" }\n * // query: { tab: \"history\" }\n */\nexport function useRoute(): RouteContext {\n const { route } = useAppletContext();\n return route;\n}\n","import { useState, useRef, useCallback } from 'react';\nimport type { StreamingHook } from '../types';\n\n/**\n * useStreaming provides SSE (Server-Sent Events) streaming utilities with cancellation support.\n *\n * Usage:\n * const { isStreaming, processStream, cancel, reset } = useStreaming()\n *\n * // Process async generator stream\n * await processStream(messageStream, (chunk) => {\n * console.log('Received:', chunk)\n * })\n *\n * // Cancel ongoing stream\n * cancel()\n *\n * // Reset state after stream completion\n * reset()\n */\nexport function useStreaming(): StreamingHook {\n const [isStreaming, setIsStreaming] = useState(false);\n const abortControllerRef = useRef<AbortController | null>(null);\n\n const processStream = useCallback(\n async <T,>(\n generator: AsyncGenerator<T>,\n onChunk: (chunk: T) => void,\n signal?: AbortSignal\n ): Promise<void> => {\n setIsStreaming(true);\n\n // Create abort controller if not provided\n const controller = new AbortController();\n abortControllerRef.current = controller;\n\n // Listen to external signal if provided\n const onExternalAbort = signal ? () => { controller.abort(); } : undefined;\n if (signal && onExternalAbort) {\n signal.addEventListener('abort', onExternalAbort);\n }\n\n try {\n for await (const chunk of generator) {\n // Check if stream was cancelled\n if (controller.signal.aborted) {\n break;\n }\n\n onChunk(chunk);\n }\n } catch (error) {\n // Stream was cancelled or errored\n if (controller.signal.aborted) {\n // Cancellation is expected, don't throw\n return;\n }\n throw error;\n } finally {\n if (signal && onExternalAbort) {\n signal.removeEventListener('abort', onExternalAbort);\n }\n setIsStreaming(false);\n abortControllerRef.current = null;\n }\n },\n []\n );\n\n const cancel = useCallback(() => {\n if (abortControllerRef.current) {\n abortControllerRef.current.abort();\n }\n setIsStreaming(false);\n }, []);\n\n const reset = useCallback(() => {\n abortControllerRef.current = null;\n setIsStreaming(false);\n }, []);\n\n return {\n isStreaming,\n processStream,\n cancel,\n reset\n };\n}\n","import { useConfig } from './useConfig';\n\nexport type ShellMode = 'embedded' | 'standalone'\n\nexport interface AppletRuntime {\n basePath: string\n assetsBasePath: string\n rpcEndpoint?: string\n shellMode?: ShellMode\n}\n\nexport function useAppletRuntime(): AppletRuntime {\n const config = useConfig();\n\n const basePath = config.basePath ?? '';\n const normalizedBasePath = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath;\n const assetsBasePath = config.assetsBasePath ?? `${normalizedBasePath || ''}/assets`;\n const rpcEndpoint = config.rpcUIEndpoint;\n const shellMode = config.shellMode;\n\n return { basePath: normalizedBasePath, assetsBasePath, rpcEndpoint, shellMode };\n}\n"]}
@@ -179,10 +179,11 @@ function useStreaming() {
179
179
  setIsStreaming(true);
180
180
  const controller = new AbortController();
181
181
  abortControllerRef.current = controller;
182
- if (signal) {
183
- signal.addEventListener("abort", () => {
184
- controller.abort();
185
- });
182
+ const onExternalAbort = signal ? () => {
183
+ controller.abort();
184
+ } : void 0;
185
+ if (signal && onExternalAbort) {
186
+ signal.addEventListener("abort", onExternalAbort);
186
187
  }
187
188
  try {
188
189
  for await (const chunk of generator) {
@@ -197,6 +198,9 @@ function useStreaming() {
197
198
  }
198
199
  throw error;
199
200
  } finally {
201
+ if (signal && onExternalAbort) {
202
+ signal.removeEventListener("abort", onExternalAbort);
203
+ }
200
204
  setIsStreaming(false);
201
205
  abortControllerRef.current = null;
202
206
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../ui/src/applet-core/context/AppletContext.tsx","../../ui/src/applet-core/context/ConfigProvider.tsx","../../ui/src/applet-core/hooks/useAppletContext.ts","../../ui/src/applet-core/hooks/useConfig.ts","../../ui/src/applet-core/hooks/useUser.ts","../../ui/src/applet-core/hooks/usePermissions.ts","../../ui/src/applet-core/hooks/useTranslation.ts","../../ui/src/applet-core/hooks/useSession.ts","../../ui/src/applet-core/hooks/useRoute.ts","../../ui/src/applet-core/hooks/useStreaming.ts","../../ui/src/applet-core/hooks/useAppletRunt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AA,CAAO,SAAA;AAEzB,EAAA,OAAO,EAAE,QAAA,EAAU,kBAAA,EAAoB,cAAA,EAAgB,aAAa,SAAA,EAAU;AAChF","file":"core.mjs","sourcesContent":["import { createContext, useContext, ReactNode } from 'react'\nimport type { InitialContext } from '../types'\n\n/**\n * AppletContext provides access to the global context injected by the backend.\n * The context is read from window.__*_CONTEXT__ (configured via windowKey).\n */\nconst AppletContext = createContext<InitialContext | null>(null)\n\nconst REQUIRED_KEYS: Array<keyof InitialContext> = ['user', 'tenant', 'locale', 'config', 'route', 'session']\n\nfunction validateInitialContext(value: unknown, windowKey: string): InitialContext {\n if (!value || typeof value !== 'object') {\n throw new Error(`${windowKey}: expected an object, got ${typeof value}`)\n }\n const obj = value as Record<string, unknown>\n const missing = REQUIRED_KEYS.filter((k) => !(k in obj))\n if (missing.length > 0) {\n throw new Error(`${windowKey}: missing required keys: ${missing.join(', ')}`)\n }\n\n // Deep validation for nested required fields\n const user = obj.user as Record<string, unknown> | undefined\n if (!user || typeof user !== 'object') {\n throw new Error(`${windowKey}.user: expected an object, got ${typeof user}`)\n }\n if (typeof user.id !== 'number') {\n throw new Error(`${windowKey}.user.id: expected number, got ${typeof user.id}`)\n }\n if (!Array.isArray(user.permissions)) {\n throw new Error(`${windowKey}.user.permissions: expected array, got ${typeof user.permissions}`)\n }\n\n const tenant = obj.tenant as Record<string, unknown> | undefined\n if (!tenant || typeof tenant !== 'object') {\n throw new Error(`${windowKey}.tenant: expected an object, got ${typeof tenant}`)\n }\n if (typeof tenant.id !== 'string') {\n throw new Error(`${windowKey}.tenant.id: expected string, got ${typeof tenant.id}`)\n }\n\n const session = obj.session as Record<string, unknown> | undefined\n if (!session || typeof session !== 'object') {\n throw new Error(`${windowKey}.session: expected an object, got ${typeof session}`)\n }\n if (typeof session.csrfToken !== 'string') {\n throw new Error(`${windowKey}.session.csrfToken: expected string, got ${typeof session.csrfToken}`)\n }\n if (typeof session.expiresAt !== 'number') {\n throw new Error(`${windowKey}.session.expiresAt: expected number, got ${typeof session.expiresAt}`)\n }\n if (typeof session.refreshURL !== 'string') {\n throw new Error(`${windowKey}.session.refreshURL: expected string, got ${typeof session.refreshURL}`)\n }\n\n const config = obj.config\n if (!config || typeof config !== 'object') {\n throw new Error(`${windowKey}.config: expected object, got ${typeof config}`)\n }\n\n return value as InitialContext\n}\n\nexport interface AppletProviderProps {\n children: ReactNode\n windowKey: string\n context?: InitialContext\n}\n\n/**\n * AppletProvider reads context from window global and provides it to hooks.\n *\n * Usage:\n * <AppletProvider windowKey=\"__APPLET_CONTEXT__\">\n * <App />\n * </AppletProvider>\n */\nexport function AppletProvider({ children, windowKey, context }: AppletProviderProps) {\n // Use provided context or read from window global\n const raw = context ?? (window as unknown as Record<string, unknown>)[windowKey]\n\n if (!raw) {\n throw new Error(`${windowKey} not found on window. Ensure backend context injection is working.`)\n }\n\n const initialContext = validateInitialContext(raw, windowKey)\n\n return (\n <AppletContext.Provider value={initialContext}>\n {children}\n </AppletContext.Provider>\n )\n}\n\n/**\n * useAppletContext provides access to the full applet context.\n * Use specialized hooks (useUser, useConfig, etc.) for specific context parts.\n */\nexport function useAppletContext<T = InitialContext>(): T {\n const context = useContext(AppletContext)\n if (!context) {\n throw new Error('useAppletContext must be used within AppletProvider')\n }\n return context as T\n}\n","import { createContext, useContext, ReactNode } from 'react'\nimport type { InitialContext } from '../types'\n\n/**\n * ConfigProvider is an alternative to AppletProvider that accepts context via props\n * instead of reading from window global. Useful for testing and server-side rendering.\n */\n\nconst ConfigContext = createContext<InitialContext | null>(null)\n\nexport interface ConfigProviderProps {\n children: ReactNode\n config: InitialContext\n}\n\n/**\n * ConfigProvider accepts context configuration via props.\n *\n * Usage:\n * <ConfigProvider config={initialContext}>\n * <App />\n * </ConfigProvider>\n */\nexport function ConfigProvider({ children, config }: ConfigProviderProps) {\n return (\n <ConfigContext.Provider value={config}>\n {children}\n </ConfigContext.Provider>\n )\n}\n\n/**\n * useConfigContext provides access to the applet context when using ConfigProvider.\n */\nexport function useConfigContext<T = InitialContext>(): T {\n const context = useContext(ConfigContext)\n if (!context) {\n throw new Error('useConfigContext must be used within ConfigProvider')\n }\n return context as T\n}\n","import type { InitialContext } from '../types'\n\n/**\n * useAppletContext provides direct access to the window global context.\n * This is a standalone version that doesn't require AppletProvider.\n *\n * Usage:\n * const context = useAppletContext('__APPLET_CONTEXT__')\n *\n * Note: Prefer using AppletProvider + context hooks for better type safety\n * and testability. Use this hook only when provider setup is not possible.\n */\nexport function useAppletContext<T = InitialContext>(windowKey: string): T {\n const context = (window as unknown as Record<string, unknown>)[windowKey]\n\n if (!context) {\n throw new Error(`${windowKey} not found on window. Ensure backend context injection is working.`)\n }\n\n return context as T\n}\n","import { useAppletContext } from '../context/AppletContext'\nimport type { AppConfig } from '../types'\n\n/**\n * useConfig provides access to applet configuration (endpoints, etc.)\n *\n * Usage:\n * const { graphQLEndpoint, streamEndpoint } = useConfig()\n */\nexport function useConfig(): AppConfig {\n const { config } = useAppletContext()\n return config\n}\n","import { useAppletContext } from '../context/AppletContext'\nimport type { UserContext } from '../types'\n\n/**\n * useUser provides access to current user information.\n *\n * Usage:\n * const { id, email, firstName, lastName, permissions } = useUser()\n */\nexport function useUser(): UserContext {\n const { user } = useAppletContext()\n return user\n}\n","import { useAppletContext } from '../context/AppletContext'\nimport type { PermissionsHook } from '../types'\n\n/**\n * usePermissions provides permission checking utilities.\n * All user permissions are automatically passed from backend.\n *\n * Usage:\n * const { hasPermission, hasAnyPermission } = usePermissions()\n *\n * if (hasPermission('BiChat.Access')) {\n * // User has bichat access\n * }\n *\n * if (hasAnyPermission('finance.view', 'finance.edit')) {\n * // User has at least one of these permissions\n * }\n */\nexport function usePermissions(): PermissionsHook {\n const { user } = useAppletContext()\n\n const hasPermission = (permission: string): boolean => {\n return user.permissions.includes(permission)\n }\n\n const hasAnyPermission = (...permissions: string[]): boolean => {\n return permissions.some(p => user.permissions.includes(p))\n }\n\n return {\n hasPermission,\n hasAnyPermission,\n permissions: user.permissions\n }\n}\n","import { useAppletContext } from '../context/AppletContext'\nimport type { TranslationHook } from '../types'\n\n/**\n * useTranslation provides i18n translation utilities.\n * All translations are automatically passed from backend locale bundle.\n *\n * Usage:\n * const { t, language } = useTranslation()\n *\n * // Simple translation\n * t('BiChat.Title') // Returns translated text\n *\n * // Translation with interpolation\n * t('Common.WelcomeMessage', { name: 'John' })\n * // If translation is \"Welcome {name}!\" -> Returns \"Welcome John!\"\n *\n * React uses same keys as Go backend:\n * Go: pageCtx.T(\"BiChat.Title\")\n * React: t(\"BiChat.Title\")\n */\nexport function useTranslation(): TranslationHook {\n const { locale } = useAppletContext()\n\n const t = (key: string, params?: Record<string, unknown>): string => {\n let text = locale.translations[key] || key\n\n // Simple interpolation: \"Hello {name}\" with {name: \"World\"}\n if (params) {\n Object.entries(params).forEach(([k, v]) => {\n text = text.replace(new RegExp(`\\\\{${k}\\\\}`, 'g'), String(v))\n })\n }\n\n return text\n }\n\n return {\n t,\n language: locale.language\n }\n}\n","import { useMemo } from 'react'\nimport { useAppletContext } from '../context/AppletContext'\nimport type { SessionHook } from '../types'\n\nexport interface UseSessionOptions {\n loginPath?: string\n}\n\n/**\n * useSession provides session and authentication handling utilities.\n *\n * Usage:\n * const { isExpiringSoon, refreshSession, csrfToken } = useSession()\n *\n * // Check if session is expiring soon (5 min buffer)\n * if (isExpiringSoon) {\n * await refreshSession()\n * }\n *\n * // Include CSRF token in requests\n * fetch('/api/endpoint', {\n * headers: { 'X-CSRF-Token': csrfToken }\n * })\n */\nexport function useSession(options?: UseSessionOptions): SessionHook {\n const { session } = useAppletContext()\n const loginPath = options?.loginPath ?? '/login'\n\n // Check if session is expiring soon (5 minute buffer)\n const isExpiringSoon = useMemo(() => {\n const bufferMs = 5 * 60 * 1000 // 5 minutes\n return session.expiresAt - Date.now() < bufferMs\n }, [session.expiresAt])\n\n const refreshSession = async (): Promise<void> => {\n const response = await fetch(session.refreshURL, {\n method: 'POST',\n headers: {\n 'X-CSRF-Token': session.csrfToken\n }\n })\n\n if (!response.ok) {\n // Session refresh failed - redirect to login with return URL\n const returnUrl = encodeURIComponent(window.location.pathname)\n window.location.href = `${loginPath}?redirect=${returnUrl}`\n return\n }\n\n // Dispatch event for CSRF token update\n const newToken = response.headers.get('X-CSRF-Token')\n if (newToken) {\n window.dispatchEvent(\n new CustomEvent('iota:csrf-refresh', {\n detail: { token: newToken }\n })\n )\n }\n }\n\n return {\n isExpiringSoon,\n refreshSession,\n csrfToken: session.csrfToken,\n expiresAt: session.expiresAt\n }\n}\n","import { useAppletContext } from '../context/AppletContext'\nimport type { RouteContext } from '../types'\n\n/**\n * useRoute provides access to the current route context.\n * Route context is initialized from the backend and includes path, params, and query.\n *\n * Usage:\n * const { path, params, query } = useRoute()\n *\n * // Example values:\n * // path: \"/sessions/123\"\n * // params: { id: \"123\" }\n * // query: { tab: \"history\" }\n */\nexport function useRoute(): RouteContext {\n const { route } = useAppletContext()\n return route\n}\n","import { useState, useRef, useCallback } from 'react'\nimport type { StreamingHook } from '../types'\n\n/**\n * useStreaming provides SSE (Server-Sent Events) streaming utilities with cancellation support.\n *\n * Usage:\n * const { isStreaming, processStream, cancel, reset } = useStreaming()\n *\n * // Process async generator stream\n * await processStream(messageStream, (chunk) => {\n * console.log('Received:', chunk)\n * })\n *\n * // Cancel ongoing stream\n * cancel()\n *\n * // Reset state after stream completion\n * reset()\n */\nexport function useStreaming(): StreamingHook {\n const [isStreaming, setIsStreaming] = useState(false)\n const abortControllerRef = useRef<AbortController | null>(null)\n\n const processStream = useCallback(\n async <T,>(\n generator: AsyncGenerator<T>,\n onChunk: (chunk: T) => void,\n signal?: AbortSignal\n ): Promise<void> => {\n setIsStreaming(true)\n\n // Create abort controller if not provided\n const controller = new AbortController()\n abortControllerRef.current = controller\n\n // Listen to external signal if provided\n if (signal) {\n signal.addEventListener('abort', () => {\n controller.abort()\n })\n }\n\n try {\n for await (const chunk of generator) {\n // Check if stream was cancelled\n if (controller.signal.aborted) {\n break\n }\n\n onChunk(chunk)\n }\n } catch (error) {\n // Stream was cancelled or errored\n if (controller.signal.aborted) {\n // Cancellation is expected, don't throw\n return\n }\n throw error\n } finally {\n setIsStreaming(false)\n abortControllerRef.current = null\n }\n },\n []\n )\n\n const cancel = useCallback(() => {\n if (abortControllerRef.current) {\n abortControllerRef.current.abort()\n }\n setIsStreaming(false)\n }, [])\n\n const reset = useCallback(() => {\n abortControllerRef.current = null\n setIsStreaming(false)\n }, [])\n\n return {\n isStreaming,\n processStream,\n cancel,\n reset\n }\n}\n","import { useConfig } from './useConfig'\n\nexport type ShellMode = 'embedded' | 'standalone'\n\nexport interface AppletRuntime {\n basePath: string\n assetsBasePath: string\n rpcEndpoint?: string\n shellMode?: ShellMode\n}\n\nexport function useAppletRuntime(): AppletRuntime {\n const config = useConfig()\n\n const basePath = config.basePath ?? ''\n const normalizedBasePath = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath\n const assetsBasePath = config.assetsBasePath ?? `${normalizedBasePath || ''}/assets`\n const rpcEndpoint = config.rpcUIEndpoint\n const shellMode = config.shellMode\n\n return { basePath: normalizedBasePath, assetsBasePath, rpcEndpoint, shellMode }\n}\n"]}
1
+ {"version":3,"sources":["../../ui/src/applet-core/context/AppletContext.tsx","../../ui/src/applet-core/context/ConfigProvider.tsx","../../ui/src/applet-core/hooks/useAppletContext.ts","../../ui/src/applet-core/hooks/useConfig.ts","../../ui/src/applet-core/hooks/useUser.ts","../../ui/src/applet-core/hooks/usePermissions.ts","../../ui/src/applet-core/hooks/useTranslation.ts","../../ui/src/applet-core/hooks/useSession.ts","../../ui/src/applet-core/hooks/useRoute.ts","../../ui/src/applet-core/hooks/useStreaming.ts","../../ui/src/applet-core/hooks/useAppletRuntime.ts"],"names":["createContext","jsx","useContext","useAppletContext"],"mappings":";;;;AAOA,IAAM,aAAA,GAAgB,cAAqC,IAAI,CAAA;AAE/D,IAAM,gBAA6C,CAAC,MAAA,EAAQ,UAAU,QAAA,EAAU,QAAA,EAAU,SAAS,SAAS,CAAA;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SAAA;AAEzB,EAAA,OAAO,EAAE,QAAA,EAAU,kBAAA,EAAoB,cAAA,EAAgB,aAAa,SAAA,EAAU;AAChF","file":"core.mjs","sourcesContent":["import { createContext, useContext, ReactNode } from 'react';\nimport type { InitialContext } from '../types';\n\n/**\n * AppletContext provides access to the global context injected by the backend.\n * The context is read from window.__*_CONTEXT__ (configured via windowKey).\n */\nconst AppletContext = createContext<InitialContext | null>(null);\n\nconst REQUIRED_KEYS: Array<keyof InitialContext> = ['user', 'tenant', 'locale', 'config', 'route', 'session'];\n\nfunction validateInitialContext(value: unknown, windowKey: string): InitialContext {\n if (!value || typeof value !== 'object') {\n throw new Error(`${windowKey}: expected an object, got ${typeof value}`);\n }\n const obj = value as Record<string, unknown>;\n const missing = REQUIRED_KEYS.filter((k) => !(k in obj));\n if (missing.length > 0) {\n throw new Error(`${windowKey}: missing required keys: ${missing.join(', ')}`);\n }\n\n // Deep validation for nested required fields\n const user = obj.user as Record<string, unknown> | undefined;\n if (!user || typeof user !== 'object') {\n throw new Error(`${windowKey}.user: expected an object, got ${typeof user}`);\n }\n if (typeof user.id !== 'number') {\n throw new Error(`${windowKey}.user.id: expected number, got ${typeof user.id}`);\n }\n if (!Array.isArray(user.permissions)) {\n throw new Error(`${windowKey}.user.permissions: expected array, got ${typeof user.permissions}`);\n }\n\n const tenant = obj.tenant as Record<string, unknown> | undefined;\n if (!tenant || typeof tenant !== 'object') {\n throw new Error(`${windowKey}.tenant: expected an object, got ${typeof tenant}`);\n }\n if (typeof tenant.id !== 'string') {\n throw new Error(`${windowKey}.tenant.id: expected string, got ${typeof tenant.id}`);\n }\n\n const session = obj.session as Record<string, unknown> | undefined;\n if (!session || typeof session !== 'object') {\n throw new Error(`${windowKey}.session: expected an object, got ${typeof session}`);\n }\n if (typeof session.csrfToken !== 'string') {\n throw new Error(`${windowKey}.session.csrfToken: expected string, got ${typeof session.csrfToken}`);\n }\n if (typeof session.expiresAt !== 'number') {\n throw new Error(`${windowKey}.session.expiresAt: expected number, got ${typeof session.expiresAt}`);\n }\n if (typeof session.refreshURL !== 'string') {\n throw new Error(`${windowKey}.session.refreshURL: expected string, got ${typeof session.refreshURL}`);\n }\n\n const config = obj.config;\n if (!config || typeof config !== 'object') {\n throw new Error(`${windowKey}.config: expected object, got ${typeof config}`);\n }\n\n return value as InitialContext;\n}\n\nexport interface AppletProviderProps {\n children: ReactNode\n windowKey: string\n context?: InitialContext\n}\n\n/**\n * AppletProvider reads context from window global and provides it to hooks.\n *\n * Usage:\n * <AppletProvider windowKey=\"__APPLET_CONTEXT__\">\n * <App />\n * </AppletProvider>\n */\nexport function AppletProvider({ children, windowKey, context }: AppletProviderProps) {\n // Use provided context or read from window global\n const raw = context ?? (window as unknown as Record<string, unknown>)[windowKey];\n\n if (!raw) {\n throw new Error(`${windowKey} not found on window. Ensure backend context injection is working.`);\n }\n\n const initialContext = validateInitialContext(raw, windowKey);\n\n return (\n <AppletContext.Provider value={initialContext}>\n {children}\n </AppletContext.Provider>\n );\n}\n\n/**\n * useAppletContext provides access to the full applet context.\n * Use specialized hooks (useUser, useConfig, etc.) for specific context parts.\n */\nexport function useAppletContext<T = InitialContext>(): T {\n const context = useContext(AppletContext);\n if (!context) {\n throw new Error('useAppletContext must be used within AppletProvider');\n }\n return context as T;\n}\n","import { createContext, useContext, ReactNode } from 'react';\nimport type { InitialContext } from '../types';\n\n/**\n * ConfigProvider is an alternative to AppletProvider that accepts context via props\n * instead of reading from window global. Useful for testing and server-side rendering.\n */\n\nconst ConfigContext = createContext<InitialContext | null>(null);\n\nexport interface ConfigProviderProps {\n children: ReactNode\n config: InitialContext\n}\n\n/**\n * ConfigProvider accepts context configuration via props.\n *\n * Usage:\n * <ConfigProvider config={initialContext}>\n * <App />\n * </ConfigProvider>\n */\nexport function ConfigProvider({ children, config }: ConfigProviderProps) {\n return (\n <ConfigContext.Provider value={config}>\n {children}\n </ConfigContext.Provider>\n );\n}\n\n/**\n * useConfigContext provides access to the applet context when using ConfigProvider.\n */\nexport function useConfigContext<T = InitialContext>(): T {\n const context = useContext(ConfigContext);\n if (!context) {\n throw new Error('useConfigContext must be used within ConfigProvider');\n }\n return context as T;\n}\n","import type { InitialContext } from '../types';\n\n/**\n * useAppletContext provides direct access to the window global context.\n * This is a standalone version that doesn't require AppletProvider.\n *\n * Usage:\n * const context = useAppletContext('__APPLET_CONTEXT__')\n *\n * Note: Prefer using AppletProvider + context hooks for better type safety\n * and testability. Use this hook only when provider setup is not possible.\n */\nexport function useAppletContext<T = InitialContext>(windowKey: string): T {\n const context = (window as unknown as Record<string, unknown>)[windowKey];\n\n if (!context) {\n throw new Error(`${windowKey} not found on window. Ensure backend context injection is working.`);\n }\n\n return context as T;\n}\n","import { useAppletContext } from '../context/AppletContext';\nimport type { AppConfig } from '../types';\n\n/**\n * useConfig provides access to applet configuration (endpoints, etc.)\n *\n * Usage:\n * const { graphQLEndpoint, streamEndpoint } = useConfig()\n */\nexport function useConfig(): AppConfig {\n const { config } = useAppletContext();\n return config;\n}\n","import { useAppletContext } from '../context/AppletContext';\nimport type { UserContext } from '../types';\n\n/**\n * useUser provides access to current user information.\n *\n * Usage:\n * const { id, email, firstName, lastName, permissions } = useUser()\n */\nexport function useUser(): UserContext {\n const { user } = useAppletContext();\n return user;\n}\n","import { useAppletContext } from '../context/AppletContext';\nimport type { PermissionsHook } from '../types';\n\n/**\n * usePermissions provides permission checking utilities.\n * All user permissions are automatically passed from backend.\n *\n * Usage:\n * const { hasPermission, hasAnyPermission } = usePermissions()\n *\n * if (hasPermission('BiChat.Access')) {\n * // User has bichat access\n * }\n *\n * if (hasAnyPermission('finance.view', 'finance.edit')) {\n * // User has at least one of these permissions\n * }\n */\nexport function usePermissions(): PermissionsHook {\n const { user } = useAppletContext();\n\n const hasPermission = (permission: string): boolean => {\n return user.permissions.includes(permission);\n };\n\n const hasAnyPermission = (...permissions: string[]): boolean => {\n return permissions.some(p => user.permissions.includes(p));\n };\n\n return {\n hasPermission,\n hasAnyPermission,\n permissions: user.permissions\n };\n}\n","import { useAppletContext } from '../context/AppletContext';\nimport type { TranslationHook } from '../types';\n\n/**\n * useTranslation provides i18n translation utilities.\n * All translations are automatically passed from backend locale bundle.\n *\n * Usage:\n * const { t, language } = useTranslation()\n *\n * // Simple translation\n * t('BiChat.Title') // Returns translated text\n *\n * // Translation with interpolation\n * t('Common.WelcomeMessage', { name: 'John' })\n * // If translation is \"Welcome {name}!\" -> Returns \"Welcome John!\"\n *\n * React uses same keys as Go backend:\n * Go: pageCtx.T(\"BiChat.Title\")\n * React: t(\"BiChat.Title\")\n */\nexport function useTranslation(): TranslationHook {\n const { locale } = useAppletContext();\n\n const t = (key: string, params?: Record<string, unknown>): string => {\n let text = locale.translations[key] || key;\n\n // Simple interpolation: \"Hello {name}\" with {name: \"World\"}\n if (params) {\n Object.entries(params).forEach(([k, v]) => {\n text = text.replace(new RegExp(`\\\\{${k}\\\\}`, 'g'), String(v));\n });\n }\n\n return text;\n };\n\n return {\n t,\n language: locale.language\n };\n}\n","import { useMemo } from 'react';\nimport { useAppletContext } from '../context/AppletContext';\nimport type { SessionHook } from '../types';\n\nexport interface UseSessionOptions {\n loginPath?: string\n}\n\n/**\n * useSession provides session and authentication handling utilities.\n *\n * Usage:\n * const { isExpiringSoon, refreshSession, csrfToken } = useSession()\n *\n * // Check if session is expiring soon (5 min buffer)\n * if (isExpiringSoon) {\n * await refreshSession()\n * }\n *\n * // Include CSRF token in requests\n * fetch('/api/endpoint', {\n * headers: { 'X-CSRF-Token': csrfToken }\n * })\n */\nexport function useSession(options?: UseSessionOptions): SessionHook {\n const { session } = useAppletContext();\n const loginPath = options?.loginPath ?? '/login';\n\n // Check if session is expiring soon (5 minute buffer)\n const isExpiringSoon = useMemo(() => {\n const bufferMs = 5 * 60 * 1000; // 5 minutes\n return session.expiresAt - Date.now() < bufferMs;\n }, [session.expiresAt]);\n\n const refreshSession = async (): Promise<void> => {\n const response = await fetch(session.refreshURL, {\n method: 'POST',\n headers: {\n 'X-CSRF-Token': session.csrfToken\n }\n });\n\n if (!response.ok) {\n // Session refresh failed - redirect to login with return URL\n const returnUrl = encodeURIComponent(window.location.pathname);\n window.location.href = `${loginPath}?redirect=${returnUrl}`;\n return;\n }\n\n // Dispatch event for CSRF token update\n const newToken = response.headers.get('X-CSRF-Token');\n if (newToken) {\n window.dispatchEvent(\n new CustomEvent('iota:csrf-refresh', {\n detail: { token: newToken }\n })\n );\n }\n };\n\n return {\n isExpiringSoon,\n refreshSession,\n csrfToken: session.csrfToken,\n expiresAt: session.expiresAt\n };\n}\n","import { useAppletContext } from '../context/AppletContext';\nimport type { RouteContext } from '../types';\n\n/**\n * useRoute provides access to the current route context.\n * Route context is initialized from the backend and includes path, params, and query.\n *\n * Usage:\n * const { path, params, query } = useRoute()\n *\n * // Example values:\n * // path: \"/sessions/123\"\n * // params: { id: \"123\" }\n * // query: { tab: \"history\" }\n */\nexport function useRoute(): RouteContext {\n const { route } = useAppletContext();\n return route;\n}\n","import { useState, useRef, useCallback } from 'react';\nimport type { StreamingHook } from '../types';\n\n/**\n * useStreaming provides SSE (Server-Sent Events) streaming utilities with cancellation support.\n *\n * Usage:\n * const { isStreaming, processStream, cancel, reset } = useStreaming()\n *\n * // Process async generator stream\n * await processStream(messageStream, (chunk) => {\n * console.log('Received:', chunk)\n * })\n *\n * // Cancel ongoing stream\n * cancel()\n *\n * // Reset state after stream completion\n * reset()\n */\nexport function useStreaming(): StreamingHook {\n const [isStreaming, setIsStreaming] = useState(false);\n const abortControllerRef = useRef<AbortController | null>(null);\n\n const processStream = useCallback(\n async <T,>(\n generator: AsyncGenerator<T>,\n onChunk: (chunk: T) => void,\n signal?: AbortSignal\n ): Promise<void> => {\n setIsStreaming(true);\n\n // Create abort controller if not provided\n const controller = new AbortController();\n abortControllerRef.current = controller;\n\n // Listen to external signal if provided\n const onExternalAbort = signal ? () => { controller.abort(); } : undefined;\n if (signal && onExternalAbort) {\n signal.addEventListener('abort', onExternalAbort);\n }\n\n try {\n for await (const chunk of generator) {\n // Check if stream was cancelled\n if (controller.signal.aborted) {\n break;\n }\n\n onChunk(chunk);\n }\n } catch (error) {\n // Stream was cancelled or errored\n if (controller.signal.aborted) {\n // Cancellation is expected, don't throw\n return;\n }\n throw error;\n } finally {\n if (signal && onExternalAbort) {\n signal.removeEventListener('abort', onExternalAbort);\n }\n setIsStreaming(false);\n abortControllerRef.current = null;\n }\n },\n []\n );\n\n const cancel = useCallback(() => {\n if (abortControllerRef.current) {\n abortControllerRef.current.abort();\n }\n setIsStreaming(false);\n }, []);\n\n const reset = useCallback(() => {\n abortControllerRef.current = null;\n setIsStreaming(false);\n }, []);\n\n return {\n isStreaming,\n processStream,\n cancel,\n reset\n };\n}\n","import { useConfig } from './useConfig';\n\nexport type ShellMode = 'embedded' | 'standalone'\n\nexport interface AppletRuntime {\n basePath: string\n assetsBasePath: string\n rpcEndpoint?: string\n shellMode?: ShellMode\n}\n\nexport function useAppletRuntime(): AppletRuntime {\n const config = useConfig();\n\n const basePath = config.basePath ?? '';\n const normalizedBasePath = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath;\n const assetsBasePath = config.assetsBasePath ?? `${normalizedBasePath || ''}/assets`;\n const rpcEndpoint = config.rpcUIEndpoint;\n const shellMode = config.shellMode;\n\n return { basePath: normalizedBasePath, assetsBasePath, rpcEndpoint, shellMode };\n}\n"]}
@@ -5,9 +5,13 @@ var jsxRuntime = require('react/jsx-runtime');
5
5
 
6
6
  // ui/src/applet-devtools/enabled.ts
7
7
  function shouldEnableAppletDevtools() {
8
- if (typeof window === "undefined") return false;
8
+ if (typeof window === "undefined") {
9
+ return false;
10
+ }
9
11
  const url = new URL(window.location.href);
10
- if (url.searchParams.get("appletDebug") === "1") return true;
12
+ if (url.searchParams.get("appletDebug") === "1") {
13
+ return true;
14
+ }
11
15
  try {
12
16
  return window.localStorage.getItem("iotaAppletDevtools") === "1";
13
17
  } catch {
@@ -45,9 +49,13 @@ function AppletDevtoolsOverlay() {
45
49
  const [rpcEvents, setRPCEvents] = react.useState([]);
46
50
  react.useEffect(() => {
47
51
  const onEvent = (e) => {
48
- if (!(e instanceof CustomEvent)) return;
52
+ if (!(e instanceof CustomEvent)) {
53
+ return;
54
+ }
49
55
  const detail = e.detail;
50
- if (!detail) return;
56
+ if (!detail) {
57
+ return;
58
+ }
51
59
  setRPCEvents((prev) => [detail, ...prev].slice(0, 50));
52
60
  };
53
61
  window.addEventListener("iota:applet-rpc", onEvent);
@@ -115,11 +123,19 @@ function serializeError(err) {
115
123
  name: err.name,
116
124
  message: err.message
117
125
  };
118
- if (err.stack) out.stack = err.stack;
126
+ if (err.stack) {
127
+ out.stack = err.stack;
128
+ }
119
129
  const errRecord = err;
120
- if ("code" in err && typeof errRecord.code === "string") out.code = errRecord.code;
121
- if ("details" in err) out.details = errRecord.details;
122
- if ("cause" in err) out.cause = errRecord.cause;
130
+ if ("code" in err && typeof errRecord.code === "string") {
131
+ out.code = errRecord.code;
132
+ }
133
+ if ("details" in err) {
134
+ out.details = errRecord.details;
135
+ }
136
+ if ("cause" in err) {
137
+ out.cause = errRecord.cause;
138
+ }
123
139
  return out;
124
140
  }
125
141
  return err;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../ui/src/applet-devtools/enabled.ts","../../ui/src/applet-core/context/AppletContext.tsx","../../ui/src/applet-core/hooks/useConfig.ts","../../ui/src/applet-core/hooks/useAppletRuntime.ts","../../ui/src/applet-devtools/overlay.tsx"],"names":["createContext","useContext","useState","useEffect","useMemo","jsxs","jsx"],"mappings":";;;;;;AAAO,SAAS,0BAAA,GAAsC;AACpD,EAAA,IAAI,OAAO,MAAA,KAAW,WAAA,EAAa,OAAO,KAAA;AAE1C,EAAA,MAAM,GAAA,GAAM,IAAI,GAAA,CAAI,MAAA,CAAO,SAAS,IAAI,CAAA;AACxC,EAAA,IAAI,IAAI,YAAA,CAAa,GAAA,CAAI,aAAa,CAAA,KAAM,KAAK,OAAO,IAAA;AAExD,EAAA,IAAI;AACF,IAAA,OAAO,MAAA,CAAO,YAA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A,EAAI,KAAA,EAAO,EAAE,MAAA,EAAQ,SAAA,EAAW,OAAA,EAAS,GAAA,EAAK,UAAA,EAAY,UAAA,EAAY,SAAA,EAAW,YAAA,EAAa,EAC5F,QAAA,EAAA,IAAA,CAAK,SAAA,CAAU,cAAA,CAAe,EAAA,CAAG,KAAK,CAAA,IAAK,EAAC,EAAG,IAAA,EAAM,CAAC,CAAA,EACzD,CAAA,GACE;AAAA,WAAA,EAAA,EAZI,CAAA,EAAG,GAAG,EAAE,CAAA,CAAA,EAAI,GAAG,MAAM,CAAA,CAa/B,CACD,CAAA,EACH;AAAA,SAAA,EAEJ;AAAA;AAAA;AAAA,GACF;AAEJ;AAEA,SAAS,eAAe,GAAA,EAAuB;AAC7C,EAAA,IAAI,eAAe,KAAA,EAAO;AACxB,IAAA,MAAM,GAAA,GAA+B;AAAA,MACnC,MAAM,GAAA,CAAI,IAAA;AAAA,MACV,SAAS,GAAA,CAAI;AAAA,KACf;AACA,IAAA,IAAI,GAAA,CAAI,KAAA,EAAO,GAAA,CAAI,KAAA,GAAQ,GAAA,CAAI,KAAA;AAC/B,IAAA,MAAM,SAAA,GAAY,GAAA;AAClB,IAAA,IAAI,MAAA,IAAU,OAAO,OAAO,SAAA,CAAU,SAAS,QAAA,EAAU,GAAA,CAAI,OAAO,SAAA,CAAU,IAAA;AAC9E,IAAA,IAAI,SAAA,IAAa,GAAA,EAAK,GAAA,CAAI,OAAA,GAAU,SAAA,CAAU,OAAA;AAC9C,IAAA,IAAI,OAAA,IAAW,GAAA,EAAK,GAAA,CAAI,KAAA,GAAQ,SAAA,CAAU,KAAA;AAC1C,IAAA,OAAO,GAAA;AAAA,EACT;AACA,EAAA,OAAO,GAAA;AACT","file":"devtools.cjs","sourcesContent":["export function shouldEnableAppletDevtools(): boolean {\n if (typeof window === 'undefined') return false\n\n const url = new URL(window.location.href)\n if (url.searchParams.get('appletDebug') === '1') return true\n\n try {\n return window.localStorage.getItem('iotaAppletDevtools') === '1'\n } catch {\n return false\n }\n}\n\n","import { createContext, useContext, ReactNode } from 'react'\nimport type { InitialContext } from '../types'\n\n/**\n * AppletContext provides access to the global context injected by the backend.\n * The context is read from window.__*_CONTEXT__ (configured via windowKey).\n */\nconst AppletContext = createContext<InitialContext | null>(null)\n\nconst REQUIRED_KEYS: Array<keyof InitialContext> = ['user', 'tenant', 'locale', 'config', 'route', 'session']\n\nfunction validateInitialContext(value: unknown, windowKey: string): InitialContext {\n if (!value || typeof value !== 'object') {\n throw new Error(`${windowKey}: expected an object, got ${typeof value}`)\n }\n const obj = value as Record<string, unknown>\n const missing = REQUIRED_KEYS.filter((k) => !(k in obj))\n if (missing.length > 0) {\n throw new Error(`${windowKey}: missing required keys: ${missing.join(', ')}`)\n }\n\n // Deep validation for nested required fields\n const user = obj.user as Record<string, unknown> | undefined\n if (!user || typeof user !== 'object') {\n throw new Error(`${windowKey}.user: expected an object, got ${typeof user}`)\n }\n if (typeof user.id !== 'number') {\n throw new Error(`${windowKey}.user.id: expected number, got ${typeof user.id}`)\n }\n if (!Array.isArray(user.permissions)) {\n throw new Error(`${windowKey}.user.permissions: expected array, got ${typeof user.permissions}`)\n }\n\n const tenant = obj.tenant as Record<string, unknown> | undefined\n if (!tenant || typeof tenant !== 'object') {\n throw new Error(`${windowKey}.tenant: expected an object, got ${typeof tenant}`)\n }\n if (typeof tenant.id !== 'string') {\n throw new Error(`${windowKey}.tenant.id: expected string, got ${typeof tenant.id}`)\n }\n\n const session = obj.session as Record<string, unknown> | undefined\n if (!session || typeof session !== 'object') {\n throw new Error(`${windowKey}.session: expected an object, got ${typeof session}`)\n }\n if (typeof session.csrfToken !== 'string') {\n throw new Error(`${windowKey}.session.csrfToken: expected string, got ${typeof session.csrfToken}`)\n }\n if (typeof session.expiresAt !== 'number') {\n throw new Error(`${windowKey}.session.expiresAt: expected number, got ${typeof session.expiresAt}`)\n }\n if (typeof session.refreshURL !== 'string') {\n throw new Error(`${windowKey}.session.refreshURL: expected string, got ${typeof session.refreshURL}`)\n }\n\n const config = obj.config\n if (!config || typeof config !== 'object') {\n throw new Error(`${windowKey}.config: expected object, got ${typeof config}`)\n }\n\n return value as InitialContext\n}\n\nexport interface AppletProviderProps {\n children: ReactNode\n windowKey: string\n context?: InitialContext\n}\n\n/**\n * AppletProvider reads context from window global and provides it to hooks.\n *\n * Usage:\n * <AppletProvider windowKey=\"__APPLET_CONTEXT__\">\n * <App />\n * </AppletProvider>\n */\nexport function AppletProvider({ children, windowKey, context }: AppletProviderProps) {\n // Use provided context or read from window global\n const raw = context ?? (window as unknown as Record<string, unknown>)[windowKey]\n\n if (!raw) {\n throw new Error(`${windowKey} not found on window. Ensure backend context injection is working.`)\n }\n\n const initialContext = validateInitialContext(raw, windowKey)\n\n return (\n <AppletContext.Provider value={initialContext}>\n {children}\n </AppletContext.Provider>\n )\n}\n\n/**\n * useAppletContext provides access to the full applet context.\n * Use specialized hooks (useUser, useConfig, etc.) for specific context parts.\n */\nexport function useAppletContext<T = InitialContext>(): T {\n const context = useContext(AppletContext)\n if (!context) {\n throw new Error('useAppletContext must be used within AppletProvider')\n }\n return context as T\n}\n","import { useAppletContext } from '../context/AppletContext'\nimport type { AppConfig } from '../types'\n\n/**\n * useConfig provides access to applet configuration (endpoints, etc.)\n *\n * Usage:\n * const { graphQLEndpoint, streamEndpoint } = useConfig()\n */\nexport function useConfig(): AppConfig {\n const { config } = useAppletContext()\n return config\n}\n","import { useConfig } from './useConfig'\n\nexport type ShellMode = 'embedded' | 'standalone'\n\nexport interface AppletRuntime {\n basePath: string\n assetsBasePath: string\n rpcEndpoint?: string\n shellMode?: ShellMode\n}\n\nexport function useAppletRuntime(): AppletRuntime {\n const config = useConfig()\n\n const basePath = config.basePath ?? ''\n const normalizedBasePath = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath\n const assetsBasePath = config.assetsBasePath ?? `${normalizedBasePath || ''}/assets`\n const rpcEndpoint = config.rpcUIEndpoint\n const shellMode = config.shellMode\n\n return { basePath: normalizedBasePath, assetsBasePath, rpcEndpoint, shellMode }\n}\n","import { useEffect, useMemo, useState } from 'react'\nimport { useAppletContext } from '../applet-core/context/AppletContext'\nimport { useAppletRuntime } from '../applet-core/hooks/useAppletRuntime'\n\ntype RPCEvent = {\n id: string\n method: string\n status: 'start' | 'success' | 'error'\n durationMs?: number\n error?: unknown\n}\n\nexport function AppletDevtoolsOverlay() {\n const ctx = useAppletContext()\n const runtime = useAppletRuntime()\n const [rpcEvents, setRPCEvents] = useState<RPCEvent[]>([])\n\n useEffect(() => {\n const onEvent = (e: Event) => {\n if (!(e instanceof CustomEvent)) return\n const detail = (e as CustomEvent<RPCEvent>).detail\n if (!detail) return\n setRPCEvents((prev) => [detail, ...prev].slice(0, 50))\n }\n window.addEventListener('iota:applet-rpc', onEvent as EventListener)\n return () => window.removeEventListener('iota:applet-rpc', onEvent as EventListener)\n }, [])\n\n const summary = useMemo(() => {\n return {\n basePath: runtime.basePath,\n assetsBasePath: runtime.assetsBasePath,\n rpcEndpoint: runtime.rpcEndpoint,\n shellMode: runtime.shellMode,\n route: ctx.route,\n user: { id: ctx.user.id, email: ctx.user.email },\n tenant: ctx.tenant,\n }\n }, [ctx.route, ctx.tenant, ctx.user.email, ctx.user.id, runtime.assetsBasePath, runtime.basePath, runtime.rpcEndpoint, runtime.shellMode])\n\n return (\n <div\n style={{\n position: 'fixed',\n right: 12,\n bottom: 12,\n width: 420,\n maxHeight: '60vh',\n overflow: 'auto',\n background: 'rgba(17, 24, 39, 0.92)',\n color: '#E5E7EB',\n border: '1px solid rgba(255,255,255,0.12)',\n borderRadius: 10,\n padding: 12,\n fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, \"Liberation Mono\", \"Courier New\", monospace',\n fontSize: 12,\n zIndex: 2147483647,\n }}\n >\n <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 8 }}>\n <div style={{ fontWeight: 700 }}>Applet Devtools</div>\n <div style={{ opacity: 0.7 }}>{runtime.shellMode ?? 'unknown'}</div>\n </div>\n\n <div style={{ marginBottom: 10 }}>\n <div style={{ opacity: 0.85, marginBottom: 4 }}>Context</div>\n <pre style={{ margin: 0, whiteSpace: 'pre-wrap', wordBreak: 'break-word' }}>{JSON.stringify(summary, null, 2)}</pre>\n </div>\n\n <div>\n <div style={{ opacity: 0.85, marginBottom: 4 }}>RPC</div>\n {rpcEvents.length === 0 ? (\n <div style={{ opacity: 0.7 }}>No calls yet</div>\n ) : (\n <div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>\n {rpcEvents.map((ev) => (\n <div key={`${ev.id}:${ev.status}`} style={{ padding: 8, border: '1px solid rgba(255,255,255,0.08)', borderRadius: 8 }}>\n <div style={{ display: 'flex', justifyContent: 'space-between' }}>\n <div>{ev.method}</div>\n <div style={{ opacity: 0.8 }}>\n {ev.status}\n {typeof ev.durationMs === 'number' ? ` (${ev.durationMs}ms)` : ''}\n </div>\n </div>\n {ev.status === 'error' ? (\n <pre style={{ margin: '6px 0 0', opacity: 0.8, whiteSpace: 'pre-wrap', wordBreak: 'break-word' }}>\n {JSON.stringify(serializeError(ev.error) ?? {}, null, 2)}\n </pre>\n ) : null}\n </div>\n ))}\n </div>\n )}\n </div>\n </div>\n )\n}\n\nfunction serializeError(err: unknown): unknown {\n if (err instanceof Error) {\n const out: Record<string, unknown> = {\n name: err.name,\n message: err.message,\n }\n if (err.stack) out.stack = err.stack\n const errRecord = err as unknown as Record<string, unknown>\n if ('code' in err && typeof errRecord.code === 'string') out.code = errRecord.code\n if ('details' in err) out.details = errRecord.details\n if ('cause' in err) out.cause = errRecord.cause\n return out\n }\n return err\n}\n"]}
1
+ {"version":3,"sources":["../../ui/src/applet-devtools/enabled.ts","../../ui/src/applet-core/context/AppletContext.tsx","../../ui/src/applet-core/hooks/useConfig.ts","../../ui/src/applet-core/hooks/useAppletRuntime.ts","../../ui/src/applet-devtools/overlay.tsx"],"names":["createContext","useContext","useState","useEffect","useMemo","jsxs","jsx"],"mappings":";;;;;;AAAO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AA,CAAU,IAAA;AAAA,IAAK;AACpF,IAAA,IAAI,aAAa,GAAA,EAAK;AAAC,MAAA,GAAA,CAAI,UAAU,SAAA,CAAU,OAAA;AAAA,IAAQ;AACvD,IAAA,IAAI,WAAW,GAAA,EAAK;AAAC,MAAA,GAAA,CAAI,QAAQ,SAAA,CAAU,KAAA;AAAA,IAAM;AACjD,IAAA,OAAO,GAAA;AAAA,EACT;AACA,EAAA,OAAO,GAAA;AACT","file":"devtools.cjs","sourcesContent":["export function shouldEnableAppletDevtools(): boolean {\n if (typeof window === 'undefined') {return false;}\n\n const url = new URL(window.location.href);\n if (url.searchParams.get('appletDebug') === '1') {return true;}\n\n try {\n return window.localStorage.getItem('iotaAppletDevtools') === '1';\n } catch {\n return false;\n }\n}\n\n","import { createContext, useContext, ReactNode } from 'react';\nimport type { InitialContext } from '../types';\n\n/**\n * AppletContext provides access to the global context injected by the backend.\n * The context is read from window.__*_CONTEXT__ (configured via windowKey).\n */\nconst AppletContext = createContext<InitialContext | null>(null);\n\nconst REQUIRED_KEYS: Array<keyof InitialContext> = ['user', 'tenant', 'locale', 'config', 'route', 'session'];\n\nfunction validateInitialContext(value: unknown, windowKey: string): InitialContext {\n if (!value || typeof value !== 'object') {\n throw new Error(`${windowKey}: expected an object, got ${typeof value}`);\n }\n const obj = value as Record<string, unknown>;\n const missing = REQUIRED_KEYS.filter((k) => !(k in obj));\n if (missing.length > 0) {\n throw new Error(`${windowKey}: missing required keys: ${missing.join(', ')}`);\n }\n\n // Deep validation for nested required fields\n const user = obj.user as Record<string, unknown> | undefined;\n if (!user || typeof user !== 'object') {\n throw new Error(`${windowKey}.user: expected an object, got ${typeof user}`);\n }\n if (typeof user.id !== 'number') {\n throw new Error(`${windowKey}.user.id: expected number, got ${typeof user.id}`);\n }\n if (!Array.isArray(user.permissions)) {\n throw new Error(`${windowKey}.user.permissions: expected array, got ${typeof user.permissions}`);\n }\n\n const tenant = obj.tenant as Record<string, unknown> | undefined;\n if (!tenant || typeof tenant !== 'object') {\n throw new Error(`${windowKey}.tenant: expected an object, got ${typeof tenant}`);\n }\n if (typeof tenant.id !== 'string') {\n throw new Error(`${windowKey}.tenant.id: expected string, got ${typeof tenant.id}`);\n }\n\n const session = obj.session as Record<string, unknown> | undefined;\n if (!session || typeof session !== 'object') {\n throw new Error(`${windowKey}.session: expected an object, got ${typeof session}`);\n }\n if (typeof session.csrfToken !== 'string') {\n throw new Error(`${windowKey}.session.csrfToken: expected string, got ${typeof session.csrfToken}`);\n }\n if (typeof session.expiresAt !== 'number') {\n throw new Error(`${windowKey}.session.expiresAt: expected number, got ${typeof session.expiresAt}`);\n }\n if (typeof session.refreshURL !== 'string') {\n throw new Error(`${windowKey}.session.refreshURL: expected string, got ${typeof session.refreshURL}`);\n }\n\n const config = obj.config;\n if (!config || typeof config !== 'object') {\n throw new Error(`${windowKey}.config: expected object, got ${typeof config}`);\n }\n\n return value as InitialContext;\n}\n\nexport interface AppletProviderProps {\n children: ReactNode\n windowKey: string\n context?: InitialContext\n}\n\n/**\n * AppletProvider reads context from window global and provides it to hooks.\n *\n * Usage:\n * <AppletProvider windowKey=\"__APPLET_CONTEXT__\">\n * <App />\n * </AppletProvider>\n */\nexport function AppletProvider({ children, windowKey, context }: AppletProviderProps) {\n // Use provided context or read from window global\n const raw = context ?? (window as unknown as Record<string, unknown>)[windowKey];\n\n if (!raw) {\n throw new Error(`${windowKey} not found on window. Ensure backend context injection is working.`);\n }\n\n const initialContext = validateInitialContext(raw, windowKey);\n\n return (\n <AppletContext.Provider value={initialContext}>\n {children}\n </AppletContext.Provider>\n );\n}\n\n/**\n * useAppletContext provides access to the full applet context.\n * Use specialized hooks (useUser, useConfig, etc.) for specific context parts.\n */\nexport function useAppletContext<T = InitialContext>(): T {\n const context = useContext(AppletContext);\n if (!context) {\n throw new Error('useAppletContext must be used within AppletProvider');\n }\n return context as T;\n}\n","import { useAppletContext } from '../context/AppletContext';\nimport type { AppConfig } from '../types';\n\n/**\n * useConfig provides access to applet configuration (endpoints, etc.)\n *\n * Usage:\n * const { graphQLEndpoint, streamEndpoint } = useConfig()\n */\nexport function useConfig(): AppConfig {\n const { config } = useAppletContext();\n return config;\n}\n","import { useConfig } from './useConfig';\n\nexport type ShellMode = 'embedded' | 'standalone'\n\nexport interface AppletRuntime {\n basePath: string\n assetsBasePath: string\n rpcEndpoint?: string\n shellMode?: ShellMode\n}\n\nexport function useAppletRuntime(): AppletRuntime {\n const config = useConfig();\n\n const basePath = config.basePath ?? '';\n const normalizedBasePath = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath;\n const assetsBasePath = config.assetsBasePath ?? `${normalizedBasePath || ''}/assets`;\n const rpcEndpoint = config.rpcUIEndpoint;\n const shellMode = config.shellMode;\n\n return { basePath: normalizedBasePath, assetsBasePath, rpcEndpoint, shellMode };\n}\n","import { useEffect, useMemo, useState } from 'react';\nimport { useAppletContext } from '../applet-core/context/AppletContext';\nimport { useAppletRuntime } from '../applet-core/hooks/useAppletRuntime';\n\ntype RPCEvent = {\n id: string\n method: string\n status: 'start' | 'success' | 'error'\n durationMs?: number\n error?: unknown\n}\n\nexport function AppletDevtoolsOverlay() {\n const ctx = useAppletContext();\n const runtime = useAppletRuntime();\n const [rpcEvents, setRPCEvents] = useState<RPCEvent[]>([]);\n\n useEffect(() => {\n const onEvent = (e: Event) => {\n if (!(e instanceof CustomEvent)) {return;}\n const detail = (e as CustomEvent<RPCEvent>).detail;\n if (!detail) {return;}\n setRPCEvents((prev) => [detail, ...prev].slice(0, 50));\n };\n window.addEventListener('iota:applet-rpc', onEvent as EventListener);\n return () => window.removeEventListener('iota:applet-rpc', onEvent as EventListener);\n }, []);\n\n const summary = useMemo(() => {\n return {\n basePath: runtime.basePath,\n assetsBasePath: runtime.assetsBasePath,\n rpcEndpoint: runtime.rpcEndpoint,\n shellMode: runtime.shellMode,\n route: ctx.route,\n user: { id: ctx.user.id, email: ctx.user.email },\n tenant: ctx.tenant,\n };\n }, [ctx.route, ctx.tenant, ctx.user.email, ctx.user.id, runtime.assetsBasePath, runtime.basePath, runtime.rpcEndpoint, runtime.shellMode]);\n\n return (\n <div\n style={{\n position: 'fixed',\n right: 12,\n bottom: 12,\n width: 420,\n maxHeight: '60vh',\n overflow: 'auto',\n background: 'rgba(17, 24, 39, 0.92)',\n color: '#E5E7EB',\n border: '1px solid rgba(255,255,255,0.12)',\n borderRadius: 10,\n padding: 12,\n fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, \"Liberation Mono\", \"Courier New\", monospace',\n fontSize: 12,\n zIndex: 2147483647,\n }}\n >\n <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 8 }}>\n <div style={{ fontWeight: 700 }}>Applet Devtools</div>\n <div style={{ opacity: 0.7 }}>{runtime.shellMode ?? 'unknown'}</div>\n </div>\n\n <div style={{ marginBottom: 10 }}>\n <div style={{ opacity: 0.85, marginBottom: 4 }}>Context</div>\n <pre style={{ margin: 0, whiteSpace: 'pre-wrap', wordBreak: 'break-word' }}>{JSON.stringify(summary, null, 2)}</pre>\n </div>\n\n <div>\n <div style={{ opacity: 0.85, marginBottom: 4 }}>RPC</div>\n {rpcEvents.length === 0 ? (\n <div style={{ opacity: 0.7 }}>No calls yet</div>\n ) : (\n <div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>\n {rpcEvents.map((ev) => (\n <div key={`${ev.id}:${ev.status}`} style={{ padding: 8, border: '1px solid rgba(255,255,255,0.08)', borderRadius: 8 }}>\n <div style={{ display: 'flex', justifyContent: 'space-between' }}>\n <div>{ev.method}</div>\n <div style={{ opacity: 0.8 }}>\n {ev.status}\n {typeof ev.durationMs === 'number' ? ` (${ev.durationMs}ms)` : ''}\n </div>\n </div>\n {ev.status === 'error' ? (\n <pre style={{ margin: '6px 0 0', opacity: 0.8, whiteSpace: 'pre-wrap', wordBreak: 'break-word' }}>\n {JSON.stringify(serializeError(ev.error) ?? {}, null, 2)}\n </pre>\n ) : null}\n </div>\n ))}\n </div>\n )}\n </div>\n </div>\n );\n}\n\nfunction serializeError(err: unknown): unknown {\n if (err instanceof Error) {\n const out: Record<string, unknown> = {\n name: err.name,\n message: err.message,\n };\n if (err.stack) {out.stack = err.stack;}\n const errRecord = err as unknown as Record<string, unknown>;\n if ('code' in err && typeof errRecord.code === 'string') {out.code = errRecord.code;}\n if ('details' in err) {out.details = errRecord.details;}\n if ('cause' in err) {out.cause = errRecord.cause;}\n return out;\n }\n return err;\n}\n"]}
@@ -3,9 +3,13 @@ import { jsxs, jsx } from 'react/jsx-runtime';
3
3
 
4
4
  // ui/src/applet-devtools/enabled.ts
5
5
  function shouldEnableAppletDevtools() {
6
- if (typeof window === "undefined") return false;
6
+ if (typeof window === "undefined") {
7
+ return false;
8
+ }
7
9
  const url = new URL(window.location.href);
8
- if (url.searchParams.get("appletDebug") === "1") return true;
10
+ if (url.searchParams.get("appletDebug") === "1") {
11
+ return true;
12
+ }
9
13
  try {
10
14
  return window.localStorage.getItem("iotaAppletDevtools") === "1";
11
15
  } catch {
@@ -43,9 +47,13 @@ function AppletDevtoolsOverlay() {
43
47
  const [rpcEvents, setRPCEvents] = useState([]);
44
48
  useEffect(() => {
45
49
  const onEvent = (e) => {
46
- if (!(e instanceof CustomEvent)) return;
50
+ if (!(e instanceof CustomEvent)) {
51
+ return;
52
+ }
47
53
  const detail = e.detail;
48
- if (!detail) return;
54
+ if (!detail) {
55
+ return;
56
+ }
49
57
  setRPCEvents((prev) => [detail, ...prev].slice(0, 50));
50
58
  };
51
59
  window.addEventListener("iota:applet-rpc", onEvent);
@@ -113,11 +121,19 @@ function serializeError(err) {
113
121
  name: err.name,
114
122
  message: err.message
115
123
  };
116
- if (err.stack) out.stack = err.stack;
124
+ if (err.stack) {
125
+ out.stack = err.stack;
126
+ }
117
127
  const errRecord = err;
118
- if ("code" in err && typeof errRecord.code === "string") out.code = errRecord.code;
119
- if ("details" in err) out.details = errRecord.details;
120
- if ("cause" in err) out.cause = errRecord.cause;
128
+ if ("code" in err && typeof errRecord.code === "string") {
129
+ out.code = errRecord.code;
130
+ }
131
+ if ("details" in err) {
132
+ out.details = errRecord.details;
133
+ }
134
+ if ("cause" in err) {
135
+ out.cause = errRecord.cause;
136
+ }
121
137
  return out;
122
138
  }
123
139
  return err;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../ui/src/applet-devtools/enabled.ts","../../ui/src/applet-core/context/AppletContext.tsx","../../ui/src/applet-core/hooks/useConfig.ts","../../ui/src/applet-core/hooks/useAppletRuntime.ts","../../ui/src/applet-devtools/overlay.tsx"],"names":["jsx"],"mappings":";;;;AAAO,SAAS,0BAAA,GAAsC;AACpD,EAAA,IAAI,OAAO,MAAA,KAAW,WAAA,EAAa,OAAO,KAAA;AAE1C,EAAA,MAAM,GAAA,GAAM,IAAI,GAAA,CAAI,MAAA,CAAO,SAAS,IAAI,CAAA;AACxC,EAAA,IAAI,IAAI,YAAA,CAAa,GAAA,CAAI,aAAa,CAAA,KAAM,KAAK,OAAO,IAAA;AAExD,EAAA,IAAI;AACF,IAAA,OAAO,MAAA,CAAO,YAAA,CAAa,OAAA,CAAQ,oBAAoB,CAAA,KAAM,GAAA;AAAA,EAC/D,CAAA,CAAA,MAAQ;AACN,IAAA,OAAO,KAAA;AAAA,EACT;AACF;ACJA,IAAM,aAAA,GAAgB,cAAqC,IAAI,CAAA;AA2FxD,SAAS,gBAAA,GAA0C;AACxD,EAAA,MAAM,OAAA,GAAU,WAAW,aAAa,CAAA;AACxC,EAAA,IAAI,CAAC,OAAA,EAAS;AACZ,IAAA,MAAM,IAAI,MAAM,qDAAqD,CAAA;AAAA,EACvE;AACA,EAAA,OAAO,OAAA;AACT;;;AC/FO,SAAS,SAAA,GAAuB;AACrC,EAAA,MAAM,EAAE,MAAA,EAAO,GAAI,gBAAA,EAAiB;AACpC,EAAA,OAAO,MAAA;AACT;;;ACDO,SAAS,gBAAA,GAAkC;AAChD,EAAA,MAAM,SAAS,SAAA,EAAU;AAEzB,EAAA,MAAM,QAAA,GAAW,OA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A,KACf;AACA,IAAA,IAAI,GAAA,CAAI,KAAA,EAAO,GAAA,CAAI,KAAA,GAAQ,GAAA,CAAI,KAAA;AAC/B,IAAA,MAAM,SAAA,GAAY,GAAA;AAClB,IAAA,IAAI,MAAA,IAAU,OAAO,OAAO,SAAA,CAAU,SAAS,QAAA,EAAU,GAAA,CAAI,OAAO,SAAA,CAAU,IAAA;AAC9E,IAAA,IAAI,SAAA,IAAa,GAAA,EAAK,GAAA,CAAI,OAAA,GAAU,SAAA,CAAU,OAAA;AAC9C,IAAA,IAAI,OAAA,IAAW,GAAA,EAAK,GAAA,CAAI,KAAA,GAAQ,SAAA,CAAU,KAAA;AAC1C,IAAA,OAAO,GAAA;AAAA,EACT;AACA,EAAA,OAAO,GAAA;AACT","file":"devtools.mjs","sourcesContent":["export function shouldEnableAppletDevtools(): boolean {\n if (typeof window === 'undefined') return false\n\n const url = new URL(window.location.href)\n if (url.searchParams.get('appletDebug') === '1') return true\n\n try {\n return window.localStorage.getItem('iotaAppletDevtools') === '1'\n } catch {\n return false\n }\n}\n\n","import { createContext, useContext, ReactNode } from 'react'\nimport type { InitialContext } from '../types'\n\n/**\n * AppletContext provides access to the global context injected by the backend.\n * The context is read from window.__*_CONTEXT__ (configured via windowKey).\n */\nconst AppletContext = createContext<InitialContext | null>(null)\n\nconst REQUIRED_KEYS: Array<keyof InitialContext> = ['user', 'tenant', 'locale', 'config', 'route', 'session']\n\nfunction validateInitialContext(value: unknown, windowKey: string): InitialContext {\n if (!value || typeof value !== 'object') {\n throw new Error(`${windowKey}: expected an object, got ${typeof value}`)\n }\n const obj = value as Record<string, unknown>\n const missing = REQUIRED_KEYS.filter((k) => !(k in obj))\n if (missing.length > 0) {\n throw new Error(`${windowKey}: missing required keys: ${missing.join(', ')}`)\n }\n\n // Deep validation for nested required fields\n const user = obj.user as Record<string, unknown> | undefined\n if (!user || typeof user !== 'object') {\n throw new Error(`${windowKey}.user: expected an object, got ${typeof user}`)\n }\n if (typeof user.id !== 'number') {\n throw new Error(`${windowKey}.user.id: expected number, got ${typeof user.id}`)\n }\n if (!Array.isArray(user.permissions)) {\n throw new Error(`${windowKey}.user.permissions: expected array, got ${typeof user.permissions}`)\n }\n\n const tenant = obj.tenant as Record<string, unknown> | undefined\n if (!tenant || typeof tenant !== 'object') {\n throw new Error(`${windowKey}.tenant: expected an object, got ${typeof tenant}`)\n }\n if (typeof tenant.id !== 'string') {\n throw new Error(`${windowKey}.tenant.id: expected string, got ${typeof tenant.id}`)\n }\n\n const session = obj.session as Record<string, unknown> | undefined\n if (!session || typeof session !== 'object') {\n throw new Error(`${windowKey}.session: expected an object, got ${typeof session}`)\n }\n if (typeof session.csrfToken !== 'string') {\n throw new Error(`${windowKey}.session.csrfToken: expected string, got ${typeof session.csrfToken}`)\n }\n if (typeof session.expiresAt !== 'number') {\n throw new Error(`${windowKey}.session.expiresAt: expected number, got ${typeof session.expiresAt}`)\n }\n if (typeof session.refreshURL !== 'string') {\n throw new Error(`${windowKey}.session.refreshURL: expected string, got ${typeof session.refreshURL}`)\n }\n\n const config = obj.config\n if (!config || typeof config !== 'object') {\n throw new Error(`${windowKey}.config: expected object, got ${typeof config}`)\n }\n\n return value as InitialContext\n}\n\nexport interface AppletProviderProps {\n children: ReactNode\n windowKey: string\n context?: InitialContext\n}\n\n/**\n * AppletProvider reads context from window global and provides it to hooks.\n *\n * Usage:\n * <AppletProvider windowKey=\"__APPLET_CONTEXT__\">\n * <App />\n * </AppletProvider>\n */\nexport function AppletProvider({ children, windowKey, context }: AppletProviderProps) {\n // Use provided context or read from window global\n const raw = context ?? (window as unknown as Record<string, unknown>)[windowKey]\n\n if (!raw) {\n throw new Error(`${windowKey} not found on window. Ensure backend context injection is working.`)\n }\n\n const initialContext = validateInitialContext(raw, windowKey)\n\n return (\n <AppletContext.Provider value={initialContext}>\n {children}\n </AppletContext.Provider>\n )\n}\n\n/**\n * useAppletContext provides access to the full applet context.\n * Use specialized hooks (useUser, useConfig, etc.) for specific context parts.\n */\nexport function useAppletContext<T = InitialContext>(): T {\n const context = useContext(AppletContext)\n if (!context) {\n throw new Error('useAppletContext must be used within AppletProvider')\n }\n return context as T\n}\n","import { useAppletContext } from '../context/AppletContext'\nimport type { AppConfig } from '../types'\n\n/**\n * useConfig provides access to applet configuration (endpoints, etc.)\n *\n * Usage:\n * const { graphQLEndpoint, streamEndpoint } = useConfig()\n */\nexport function useConfig(): AppConfig {\n const { config } = useAppletContext()\n return config\n}\n","import { useConfig } from './useConfig'\n\nexport type ShellMode = 'embedded' | 'standalone'\n\nexport interface AppletRuntime {\n basePath: string\n assetsBasePath: string\n rpcEndpoint?: string\n shellMode?: ShellMode\n}\n\nexport function useAppletRuntime(): AppletRuntime {\n const config = useConfig()\n\n const basePath = config.basePath ?? ''\n const normalizedBasePath = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath\n const assetsBasePath = config.assetsBasePath ?? `${normalizedBasePath || ''}/assets`\n const rpcEndpoint = config.rpcUIEndpoint\n const shellMode = config.shellMode\n\n return { basePath: normalizedBasePath, assetsBasePath, rpcEndpoint, shellMode }\n}\n","import { useEffect, useMemo, useState } from 'react'\nimport { useAppletContext } from '../applet-core/context/AppletContext'\nimport { useAppletRuntime } from '../applet-core/hooks/useAppletRuntime'\n\ntype RPCEvent = {\n id: string\n method: string\n status: 'start' | 'success' | 'error'\n durationMs?: number\n error?: unknown\n}\n\nexport function AppletDevtoolsOverlay() {\n const ctx = useAppletContext()\n const runtime = useAppletRuntime()\n const [rpcEvents, setRPCEvents] = useState<RPCEvent[]>([])\n\n useEffect(() => {\n const onEvent = (e: Event) => {\n if (!(e instanceof CustomEvent)) return\n const detail = (e as CustomEvent<RPCEvent>).detail\n if (!detail) return\n setRPCEvents((prev) => [detail, ...prev].slice(0, 50))\n }\n window.addEventListener('iota:applet-rpc', onEvent as EventListener)\n return () => window.removeEventListener('iota:applet-rpc', onEvent as EventListener)\n }, [])\n\n const summary = useMemo(() => {\n return {\n basePath: runtime.basePath,\n assetsBasePath: runtime.assetsBasePath,\n rpcEndpoint: runtime.rpcEndpoint,\n shellMode: runtime.shellMode,\n route: ctx.route,\n user: { id: ctx.user.id, email: ctx.user.email },\n tenant: ctx.tenant,\n }\n }, [ctx.route, ctx.tenant, ctx.user.email, ctx.user.id, runtime.assetsBasePath, runtime.basePath, runtime.rpcEndpoint, runtime.shellMode])\n\n return (\n <div\n style={{\n position: 'fixed',\n right: 12,\n bottom: 12,\n width: 420,\n maxHeight: '60vh',\n overflow: 'auto',\n background: 'rgba(17, 24, 39, 0.92)',\n color: '#E5E7EB',\n border: '1px solid rgba(255,255,255,0.12)',\n borderRadius: 10,\n padding: 12,\n fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, \"Liberation Mono\", \"Courier New\", monospace',\n fontSize: 12,\n zIndex: 2147483647,\n }}\n >\n <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 8 }}>\n <div style={{ fontWeight: 700 }}>Applet Devtools</div>\n <div style={{ opacity: 0.7 }}>{runtime.shellMode ?? 'unknown'}</div>\n </div>\n\n <div style={{ marginBottom: 10 }}>\n <div style={{ opacity: 0.85, marginBottom: 4 }}>Context</div>\n <pre style={{ margin: 0, whiteSpace: 'pre-wrap', wordBreak: 'break-word' }}>{JSON.stringify(summary, null, 2)}</pre>\n </div>\n\n <div>\n <div style={{ opacity: 0.85, marginBottom: 4 }}>RPC</div>\n {rpcEvents.length === 0 ? (\n <div style={{ opacity: 0.7 }}>No calls yet</div>\n ) : (\n <div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>\n {rpcEvents.map((ev) => (\n <div key={`${ev.id}:${ev.status}`} style={{ padding: 8, border: '1px solid rgba(255,255,255,0.08)', borderRadius: 8 }}>\n <div style={{ display: 'flex', justifyContent: 'space-between' }}>\n <div>{ev.method}</div>\n <div style={{ opacity: 0.8 }}>\n {ev.status}\n {typeof ev.durationMs === 'number' ? ` (${ev.durationMs}ms)` : ''}\n </div>\n </div>\n {ev.status === 'error' ? (\n <pre style={{ margin: '6px 0 0', opacity: 0.8, whiteSpace: 'pre-wrap', wordBreak: 'break-word' }}>\n {JSON.stringify(serializeError(ev.error) ?? {}, null, 2)}\n </pre>\n ) : null}\n </div>\n ))}\n </div>\n )}\n </div>\n </div>\n )\n}\n\nfunction serializeError(err: unknown): unknown {\n if (err instanceof Error) {\n const out: Record<string, unknown> = {\n name: err.name,\n message: err.message,\n }\n if (err.stack) out.stack = err.stack\n const errRecord = err as unknown as Record<string, unknown>\n if ('code' in err && typeof errRecord.code === 'string') out.code = errRecord.code\n if ('details' in err) out.details = errRecord.details\n if ('cause' in err) out.cause = errRecord.cause\n return out\n }\n return err\n}\n"]}
1
+ {"version":3,"sources":["../../ui/src/applet-devtools/enabled.ts","../../ui/src/applet-core/context/AppletContext.tsx","../../ui/src/applet-core/hooks/useConfig.ts","../../ui/src/applet-core/hooks/useAppletRuntime.ts","../../ui/src/applet-devtools/overlay.tsx"],"names":["jsx"],"mappings":";;;;AAAO,SAAS,0BAAA,GAAsC;AACpD,EAAA,IAAI,OAAO,WAAW,WAAA,EAAa;AAAC,IAAA,OAAO,KAAA;AAAA,EAAM;AAEjD,EAAA,MAAM,GAAA,GAAM,IAAI,GAAA,CAAI,MAAA,CAAO,SAAS,IAAI,CAAA;AACxC,EAAA,IAAI,GAAA,CAAI,YAAA,CAAa,GAAA,CAAI,aAAa,MAAM,GAAA,EAAK;AAAC,IAAA,OAAO,IAAA;AAAA,EAAK;AAE9D,EAAA,IAAI;AACF,IAAA,OAAO,MAAA,CAAO,YAAA,CAAa,OAAA,CAAQ,oBAAoB,CAAA,KAAM,GAAA;AAAA,EAC/D,CAAA,CAAA,MAAQ;AACN,IAAA,OAAO,KAAA;AAAA,EACT;AACF;ACJA,IAAM,aAAA,GAAgB,cAAqC,IAAI,CAAA;AA2FxD,SAAS,gBAAA,GAA0C;AACxD,EAAA,MAAM,OAAA,GAAU,WAAW,aAAa,CAAA;AACxC,EAAA,IAAI,CAAC,OAAA,EAAS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CAAA,EACzD,CAAA,GACE;AAAA,WAAA,EAAA,EAZI,CAAA,EAAG,GAAG,EAAE,CAAA,CAAA,EAAI,GAAG,MAAM,CAAA,CAa/B,CACD,CAAA,EACH;AAAA,SAAA,EAEJ;AAAA;AAAA;AAAA,GACF;AAEJ;AAEA,SAAS,eAAe,GAAA,EAAuB;AAC7C,EAAA,IAAI,eAAe,KAAA,EAAO;AACxB,IAAA,MAAM,GAAA,GAA+B;AAAA,MACnC,MAAM,GAAA,CAAI,IAAA;AAAA,MACV,SAAS,GAAA,CAAI;AAAA,KACf;AACA,IAAA,IAAI,IAAI,KAAA,EAAO;AAAC,MAAA,GAAA,CAAI,QAAQ,GAAA,CAAI,KAAA;AAAA,IAAM;AACtC,IAAA,MAAM,SAAA,GAAY,GAAA;AAClB,IAAA,IAAI,MAAA,IAAU,GAAA,IAAO,OAAO,SAAA,CAAU,SAAS,QAAA,EAAU;AAAC,MAAA,GAAA,CAAI,OAAO,SAAA,CAAU,IAAA;AAAA,IAAK;AACpF,IAAA,IAAI,aAAa,GAAA,EAAK;AAAC,MAAA,GAAA,CAAI,UAAU,SAAA,CAAU,OAAA;AAAA,IAAQ;AACvD,IAAA,IAAI,WAAW,GAAA,EAAK;AAAC,MAAA,GAAA,CAAI,QAAQ,SAAA,CAAU,KAAA;AAAA,IAAM;AACjD,IAAA,OAAO,GAAA;AAAA,EACT;AACA,EAAA,OAAO,GAAA;AACT","file":"devtools.mjs","sourcesContent":["export function shouldEnableAppletDevtools(): boolean {\n if (typeof window === 'undefined') {return false;}\n\n const url = new URL(window.location.href);\n if (url.searchParams.get('appletDebug') === '1') {return true;}\n\n try {\n return window.localStorage.getItem('iotaAppletDevtools') === '1';\n } catch {\n return false;\n }\n}\n\n","import { createContext, useContext, ReactNode } from 'react';\nimport type { InitialContext } from '../types';\n\n/**\n * AppletContext provides access to the global context injected by the backend.\n * The context is read from window.__*_CONTEXT__ (configured via windowKey).\n */\nconst AppletContext = createContext<InitialContext | null>(null);\n\nconst REQUIRED_KEYS: Array<keyof InitialContext> = ['user', 'tenant', 'locale', 'config', 'route', 'session'];\n\nfunction validateInitialContext(value: unknown, windowKey: string): InitialContext {\n if (!value || typeof value !== 'object') {\n throw new Error(`${windowKey}: expected an object, got ${typeof value}`);\n }\n const obj = value as Record<string, unknown>;\n const missing = REQUIRED_KEYS.filter((k) => !(k in obj));\n if (missing.length > 0) {\n throw new Error(`${windowKey}: missing required keys: ${missing.join(', ')}`);\n }\n\n // Deep validation for nested required fields\n const user = obj.user as Record<string, unknown> | undefined;\n if (!user || typeof user !== 'object') {\n throw new Error(`${windowKey}.user: expected an object, got ${typeof user}`);\n }\n if (typeof user.id !== 'number') {\n throw new Error(`${windowKey}.user.id: expected number, got ${typeof user.id}`);\n }\n if (!Array.isArray(user.permissions)) {\n throw new Error(`${windowKey}.user.permissions: expected array, got ${typeof user.permissions}`);\n }\n\n const tenant = obj.tenant as Record<string, unknown> | undefined;\n if (!tenant || typeof tenant !== 'object') {\n throw new Error(`${windowKey}.tenant: expected an object, got ${typeof tenant}`);\n }\n if (typeof tenant.id !== 'string') {\n throw new Error(`${windowKey}.tenant.id: expected string, got ${typeof tenant.id}`);\n }\n\n const session = obj.session as Record<string, unknown> | undefined;\n if (!session || typeof session !== 'object') {\n throw new Error(`${windowKey}.session: expected an object, got ${typeof session}`);\n }\n if (typeof session.csrfToken !== 'string') {\n throw new Error(`${windowKey}.session.csrfToken: expected string, got ${typeof session.csrfToken}`);\n }\n if (typeof session.expiresAt !== 'number') {\n throw new Error(`${windowKey}.session.expiresAt: expected number, got ${typeof session.expiresAt}`);\n }\n if (typeof session.refreshURL !== 'string') {\n throw new Error(`${windowKey}.session.refreshURL: expected string, got ${typeof session.refreshURL}`);\n }\n\n const config = obj.config;\n if (!config || typeof config !== 'object') {\n throw new Error(`${windowKey}.config: expected object, got ${typeof config}`);\n }\n\n return value as InitialContext;\n}\n\nexport interface AppletProviderProps {\n children: ReactNode\n windowKey: string\n context?: InitialContext\n}\n\n/**\n * AppletProvider reads context from window global and provides it to hooks.\n *\n * Usage:\n * <AppletProvider windowKey=\"__APPLET_CONTEXT__\">\n * <App />\n * </AppletProvider>\n */\nexport function AppletProvider({ children, windowKey, context }: AppletProviderProps) {\n // Use provided context or read from window global\n const raw = context ?? (window as unknown as Record<string, unknown>)[windowKey];\n\n if (!raw) {\n throw new Error(`${windowKey} not found on window. Ensure backend context injection is working.`);\n }\n\n const initialContext = validateInitialContext(raw, windowKey);\n\n return (\n <AppletContext.Provider value={initialContext}>\n {children}\n </AppletContext.Provider>\n );\n}\n\n/**\n * useAppletContext provides access to the full applet context.\n * Use specialized hooks (useUser, useConfig, etc.) for specific context parts.\n */\nexport function useAppletContext<T = InitialContext>(): T {\n const context = useContext(AppletContext);\n if (!context) {\n throw new Error('useAppletContext must be used within AppletProvider');\n }\n return context as T;\n}\n","import { useAppletContext } from '../context/AppletContext';\nimport type { AppConfig } from '../types';\n\n/**\n * useConfig provides access to applet configuration (endpoints, etc.)\n *\n * Usage:\n * const { graphQLEndpoint, streamEndpoint } = useConfig()\n */\nexport function useConfig(): AppConfig {\n const { config } = useAppletContext();\n return config;\n}\n","import { useConfig } from './useConfig';\n\nexport type ShellMode = 'embedded' | 'standalone'\n\nexport interface AppletRuntime {\n basePath: string\n assetsBasePath: string\n rpcEndpoint?: string\n shellMode?: ShellMode\n}\n\nexport function useAppletRuntime(): AppletRuntime {\n const config = useConfig();\n\n const basePath = config.basePath ?? '';\n const normalizedBasePath = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath;\n const assetsBasePath = config.assetsBasePath ?? `${normalizedBasePath || ''}/assets`;\n const rpcEndpoint = config.rpcUIEndpoint;\n const shellMode = config.shellMode;\n\n return { basePath: normalizedBasePath, assetsBasePath, rpcEndpoint, shellMode };\n}\n","import { useEffect, useMemo, useState } from 'react';\nimport { useAppletContext } from '../applet-core/context/AppletContext';\nimport { useAppletRuntime } from '../applet-core/hooks/useAppletRuntime';\n\ntype RPCEvent = {\n id: string\n method: string\n status: 'start' | 'success' | 'error'\n durationMs?: number\n error?: unknown\n}\n\nexport function AppletDevtoolsOverlay() {\n const ctx = useAppletContext();\n const runtime = useAppletRuntime();\n const [rpcEvents, setRPCEvents] = useState<RPCEvent[]>([]);\n\n useEffect(() => {\n const onEvent = (e: Event) => {\n if (!(e instanceof CustomEvent)) {return;}\n const detail = (e as CustomEvent<RPCEvent>).detail;\n if (!detail) {return;}\n setRPCEvents((prev) => [detail, ...prev].slice(0, 50));\n };\n window.addEventListener('iota:applet-rpc', onEvent as EventListener);\n return () => window.removeEventListener('iota:applet-rpc', onEvent as EventListener);\n }, []);\n\n const summary = useMemo(() => {\n return {\n basePath: runtime.basePath,\n assetsBasePath: runtime.assetsBasePath,\n rpcEndpoint: runtime.rpcEndpoint,\n shellMode: runtime.shellMode,\n route: ctx.route,\n user: { id: ctx.user.id, email: ctx.user.email },\n tenant: ctx.tenant,\n };\n }, [ctx.route, ctx.tenant, ctx.user.email, ctx.user.id, runtime.assetsBasePath, runtime.basePath, runtime.rpcEndpoint, runtime.shellMode]);\n\n return (\n <div\n style={{\n position: 'fixed',\n right: 12,\n bottom: 12,\n width: 420,\n maxHeight: '60vh',\n overflow: 'auto',\n background: 'rgba(17, 24, 39, 0.92)',\n color: '#E5E7EB',\n border: '1px solid rgba(255,255,255,0.12)',\n borderRadius: 10,\n padding: 12,\n fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, \"Liberation Mono\", \"Courier New\", monospace',\n fontSize: 12,\n zIndex: 2147483647,\n }}\n >\n <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 8 }}>\n <div style={{ fontWeight: 700 }}>Applet Devtools</div>\n <div style={{ opacity: 0.7 }}>{runtime.shellMode ?? 'unknown'}</div>\n </div>\n\n <div style={{ marginBottom: 10 }}>\n <div style={{ opacity: 0.85, marginBottom: 4 }}>Context</div>\n <pre style={{ margin: 0, whiteSpace: 'pre-wrap', wordBreak: 'break-word' }}>{JSON.stringify(summary, null, 2)}</pre>\n </div>\n\n <div>\n <div style={{ opacity: 0.85, marginBottom: 4 }}>RPC</div>\n {rpcEvents.length === 0 ? (\n <div style={{ opacity: 0.7 }}>No calls yet</div>\n ) : (\n <div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>\n {rpcEvents.map((ev) => (\n <div key={`${ev.id}:${ev.status}`} style={{ padding: 8, border: '1px solid rgba(255,255,255,0.08)', borderRadius: 8 }}>\n <div style={{ display: 'flex', justifyContent: 'space-between' }}>\n <div>{ev.method}</div>\n <div style={{ opacity: 0.8 }}>\n {ev.status}\n {typeof ev.durationMs === 'number' ? ` (${ev.durationMs}ms)` : ''}\n </div>\n </div>\n {ev.status === 'error' ? (\n <pre style={{ margin: '6px 0 0', opacity: 0.8, whiteSpace: 'pre-wrap', wordBreak: 'break-word' }}>\n {JSON.stringify(serializeError(ev.error) ?? {}, null, 2)}\n </pre>\n ) : null}\n </div>\n ))}\n </div>\n )}\n </div>\n </div>\n );\n}\n\nfunction serializeError(err: unknown): unknown {\n if (err instanceof Error) {\n const out: Record<string, unknown> = {\n name: err.name,\n message: err.message,\n };\n if (err.stack) {out.stack = err.stack;}\n const errRecord = err as unknown as Record<string, unknown>;\n if ('code' in err && typeof errRecord.code === 'string') {out.code = errRecord.code;}\n if ('details' in err) {out.details = errRecord.details;}\n if ('cause' in err) {out.cause = errRecord.cause;}\n return out;\n }\n return err;\n}\n"]}