Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

feat(ai): proxy OpenCode API paths for iframe embedding

rcourtman038b57ee4300
diff --git a/frontend-modern/src/components/AI/AIChat.tsx b/frontend-modern/src/components/AI/AIChat.tsx
index 77cad13961..edad0df140 100644
--- a/frontend-modern/src/components/AI/AIChat.tsx
+++ b/frontend-modern/src/components/AI/AIChat.tsx
@@ -1,19 +1,15 @@
-import { Component, Show, lazy, Suspense } from 'solid-js';
+import { Component, Show, createEffect, onMount, onCleanup } from 'solid-js';
import { aiChatStore } from '@/stores/aiChat';
-// Lazy load the Chat component for better initial load performance
-const Chat = lazy(() => import('./Chat'));
-
interface AIChatProps {
onClose: () => void;
}
/**
- * AIChat component - Slide-out panel for AI Assistant.
+ * AIChat component - Embeds OpenCode's web UI in a slide-out panel.
*
- * Uses the custom Chat component which communicates with OpenCode
- * through Pulse's backend API, providing:
- * - Streaming chat responses
+ * OpenCode provides the full chat experience including:
+ * - Message display and streaming
* - Tool execution visualization
* - Session management
* - Model selection
@@ -22,6 +18,29 @@ interface AIChatProps {
*/
export const AIChat: Component<AIChatProps> = (props) => {
const isOpen = () => aiChatStore.isOpen;
+ let iframeRef: HTMLIFrameElement | undefined;
+
+ // Focus iframe when panel opens for keyboard navigation
+ createEffect(() => {
+ if (isOpen() && iframeRef) {
+ setTimeout(() => iframeRef?.focus(), 100);
+ }
+ });
+
+ // Handle escape key to close panel
+ const handleKeyDown = (e: KeyboardEvent) => {
+ if (e.key === 'Escape' && isOpen()) {
+ props.onClose();
+ }
+ };
+
+ onMount(() => {
+ document.addEventListener('keydown', handleKeyDown);
+ });
+
+ onCleanup(() => {
+ document.removeEventListener('keydown', handleKeyDown);
+ });
return (
<div
@@ -30,15 +49,79 @@ export const AIChat: Component<AIChatProps> = (props) => {
}`}
>
<Show when={isOpen()}>
- <Suspense
- fallback={
- <div class="flex-1 flex items-center justify-center">
- <div class="animate-spin rounded-full h-8 w-8 border-b-2 border-purple-600" />
+ {/* Header */}
+ <div class="flex items-center justify-between px-4 py-3 border-b border-gray-200 dark:border-gray-700 bg-gradient-to-r from-purple-50 to-pink-50 dark:from-purple-900/20 dark:to-pink-900/20">
+ <div class="flex items-center gap-3">
+ <div class="p-2 bg-purple-100 dark:bg-purple-900/40 rounded-lg">
+ <svg
+ class="w-5 h-5 text-purple-600 dark:text-purple-300"
+ fill="none"
+ stroke="currentColor"
+ viewBox="0 0 24 24"
+ >
+ <path
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ stroke-width="1.8"
+ d="M9.75 3.104v5.714a2.25 2.25 0 01-.659 1.591L5 14.5M9.75 3.104c-.251.023-.501.05-.75.082m.75-.082a24.301 24.301 0 014.5 0m0 0v5.714c0 .597.237 1.17.659 1.591L19.8 15.3M14.25 3.104c.251.023.501.05.75.082M19.8 15.3l-1.57.393A9.065 9.065 0 0112 15a9.065 9.065 0 00-6.23.693L5 14.5m14.8.8l1.402 1.402c1.232 1.232.65 3.318-1.067 3.611l-2.576.43a18.003 18.003 0 01-5.118 0l-2.576-.43c-1.717-.293-2.299-2.379-1.067-3.611L5 14.5"
+ />
+ </svg>
+ </div>
+ <div>
+ <h2 class="text-sm font-semibold text-gray-900 dark:text-gray-100">
+ AI Assistant
+ </h2>
+ <p class="text-xs text-gray-500 dark:text-gray-400">
+ Powered by OpenCode
+ </p>
</div>
- }
- >
- <Chat onClose={props.onClose} />
- </Suspense>
+ </div>
+ <div class="flex items-center gap-2">
+ {/* Open in new tab */}
+ <a
+ href="/opencode/"
+ target="_blank"
+ rel="noopener noreferrer"
+ class="p-2 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-800"
+ title="Open in new tab"
+ >
+ <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
+ <path
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ stroke-width="2"
+ d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"
+ />
+ </svg>
+ </a>
+ {/* Collapse panel */}
+ <button
+ onClick={props.onClose}
+ class="p-2 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-800"
+ title="Collapse panel (Esc)"
+ >
+ <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
+ <path
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ stroke-width="2"
+ d="M13 5l7 7-7 7M6 5l7 7-7 7"
+ />
+ </svg>
+ </button>
+ </div>
+ </div>
+
+ {/* OpenCode iframe */}
+ <div class="flex-1 relative">
+ <iframe
+ ref={iframeRef}
+ src="/opencode/"
+ class="absolute inset-0 w-full h-full border-0"
+ title="AI Assistant"
+ allow="clipboard-read; clipboard-write"
+ />
+ </div>
</Show>
</div>
);
diff --git a/frontend-modern/src/components/AI/Chat/ThinkingBlock.tsx b/frontend-modern/src/components/AI/Chat/ThinkingBlock.tsx
index 8c89748140..3bf110fdc2 100644
--- a/frontend-modern/src/components/AI/Chat/ThinkingBlock.tsx
+++ b/frontend-modern/src/components/AI/Chat/ThinkingBlock.tsx
@@ -1,4 +1,4 @@
-import { Component } from 'solid-js';
+import { Component, createSignal } from 'solid-js';
import { sanitizeThinking } from '../aiChatUtils';
interface ThinkingBlockProps {
@@ -7,22 +7,55 @@ interface ThinkingBlockProps {
}
export const ThinkingBlock: Component<ThinkingBlockProps> = (props) => {
+ const [expanded, setExpanded] = createSignal(false);
+
const truncated = () => {
- const max = props.maxLength ?? 500;
+ const max = props.maxLength ?? 300;
const text = props.content;
- return text.length > max ? text.substring(0, max) + '...' : text;
+ return text.length > max && !expanded() ? text.substring(0, max) + '...' : text;
+ };
+
+ const needsExpansion = () => {
+ const max = props.maxLength ?? 300;
+ return props.content.length > max;
};
return (
- <div class="px-3 py-2 text-xs bg-gradient-to-r from-blue-50 to-indigo-50 dark:from-blue-900/20 dark:to-indigo-900/20 text-gray-700 dark:text-gray-300 rounded-lg border-l-3 border-blue-400 dark:border-blue-500 whitespace-pre-wrap">
- <div class="flex items-center gap-1.5 mb-1.5 text-blue-600 dark:text-blue-400 font-medium">
- <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
- <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z" />
+ <div class="rounded-lg overflow-hidden border border-blue-200 dark:border-blue-800 bg-gradient-to-r from-blue-50 to-indigo-50 dark:from-blue-900/20 dark:to-indigo-900/20">
+ {/* Header - clickable to toggle */}
+ <button
+ type="button"
+ onClick={() => setExpanded(!expanded())}
+ class="w-full px-3 py-2 flex items-center gap-2 text-left hover:bg-blue-100/50 dark:hover:bg-blue-800/30 transition-colors"
+ >
+ <div class="p-1 rounded bg-blue-100 dark:bg-blue-800/50">
+ <svg class="w-3 h-3 text-blue-600 dark:text-blue-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z" />
+ </svg>
+ </div>
+ <span class="text-[10px] font-medium uppercase tracking-wider text-blue-600 dark:text-blue-400">
+ Thinking
+ </span>
+ <span class="flex-1" />
+ <svg
+ class={`w-4 h-4 text-blue-500 transition-transform ${expanded() ? 'rotate-180' : ''}`}
+ fill="none"
+ stroke="currentColor"
+ viewBox="0 0 24 24"
+ >
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7" />
</svg>
- <span class="text-[10px] uppercase tracking-wider">Thinking</span>
- </div>
- <div class="text-gray-600 dark:text-gray-400 leading-relaxed">
- {sanitizeThinking(truncated())}
+ </button>
+
+ {/* Content */}
+ <div
+ class={`px-3 overflow-hidden transition-all duration-200 ${
+ expanded() ? 'max-h-96 py-2' : needsExpansion() ? 'max-h-20 py-2' : 'max-h-40 py-2'
+ }`}
+ >
+ <div class="text-xs text-gray-600 dark:text-gray-400 leading-relaxed whitespace-pre-wrap overflow-y-auto max-h-80">
+ {sanitizeThinking(truncated())}
+ </div>
</div>
</div>
);
diff --git a/frontend-modern/vite.config.ts b/frontend-modern/vite.config.ts
index 473d71f915..a6f2ab258a 100644
--- a/frontend-modern/vite.config.ts
+++ b/frontend-modern/vite.config.ts
@@ -224,6 +224,67 @@ export default defineConfig({
target: backendUrl,
changeOrigin: true,
},
+ // OpenCode API proxies - when OpenCode is embedded in iframe, its frontend
+ // makes requests to window.location.origin. We proxy these to the backend
+ // which forwards them to OpenCode's actual backend.
+ '/global': {
+ target: backendUrl,
+ changeOrigin: true,
+ },
+ '/session': {
+ target: backendUrl,
+ changeOrigin: true,
+ ws: true, // WebSocket support for session events
+ },
+ '/tui': {
+ target: backendUrl,
+ changeOrigin: true,
+ },
+ '/config': {
+ target: backendUrl,
+ changeOrigin: true,
+ },
+ '/file': {
+ target: backendUrl,
+ changeOrigin: true,
+ },
+ '/find': {
+ target: backendUrl,
+ changeOrigin: true,
+ },
+ '/instance': {
+ target: backendUrl,
+ changeOrigin: true,
+ },
+ '/mcp': {
+ target: backendUrl,
+ changeOrigin: true,
+ },
+ '/permission': {
+ target: backendUrl,
+ changeOrigin: true,
+ },
+ '/project': {
+ target: backendUrl,
+ changeOrigin: true,
+ },
+ '/provider': {
+ target: backendUrl,
+ changeOrigin: true,
+ },
+ '/pty': {
+ target: backendUrl,
+ changeOrigin: true,
+ ws: true, // WebSocket support for PTY
+ },
+ '/question': {
+ target: backendUrl,
+ changeOrigin: true,
+ },
+ '/experimental': {
+ target: backendUrl,
+ changeOrigin: true,
+ },
// OpenCode Web UI proxy - serves OpenCode's built-in interface
'/opencode': {
target: backendUrl,
diff --git a/internal/api/ai_handler.go b/internal/api/ai_handler.go
index a7f87ffd03..9704f5d65a 100644
--- a/internal/api/ai_handler.go
+++ b/internal/api/ai_handler.go
@@ -479,3 +479,43 @@ func (h *AIHandler) HandleOpenCodeUI(w http.ResponseWriter, r *http.Request) {
// Serve the proxied request
proxy.ServeHTTP(w, r)
}
+
+// HandleOpenCodeAPI proxies OpenCode's API requests
+// When OpenCode is embedded in an iframe, its frontend makes requests to window.location.origin
+// which is Pulse. This handler proxies those requests to OpenCode's actual backend.
+func (h *AIHandler) HandleOpenCodeAPI(w http.ResponseWriter, r *http.Request) {
+ if !h.IsRunning() {
+ http.Error(w, "AI is not running", http.StatusServiceUnavailable)
+ return
+ }
+
+ baseURL := h.service.GetBaseURL()
+ if baseURL == "" {
+ http.Error(w, "OpenCode URL not available", http.StatusServiceUnavailable)
+ return
+ }
+
+ target, err := url.Parse(baseURL)
+ if err != nil {
+ http.Error(w, "Invalid OpenCode URL", http.StatusInternalServerError)
+ return
+ }
+
+ // Create reverse proxy - no path modification needed
+ proxy := httputil.NewSingleHostReverseProxy(target)
+
+ originalDirector := proxy.Director
+ proxy.Director = func(req *http.Request) {
+ originalDirector(req)
+ // Keep the path as-is (no stripping)
+ req.Host = target.Host
+ }
+
+ // Handle WebSocket upgrades (for /pty/ and other real-time endpoints)
+ if r.Header.Get("Upgrade") == "websocket" {
+ proxy.ServeHTTP(w, r)
+ return
+ }
+
+ proxy.ServeHTTP(w, r)
+}
diff --git a/internal/api/router.go b/internal/api/router.go
index 2646a5905a..d2130e96c9 100644
--- a/internal/api/router.go
+++ b/internal/api/router.go
@@ -1409,6 +1409,29 @@ func (r *Router) setupRoutes() {
http.Redirect(w, req, "/opencode/", http.StatusMovedPermanently)
}))
+ // OpenCode API proxy - these routes are used by OpenCode's frontend
+ // When embedded in iframe, OpenCode's JS makes requests to window.location.origin
+ // We proxy these to OpenCode's backend so the iframe works correctly
+ openCodeAPIPaths := []string{
+ "/global/",
+ "/session/",
+ "/tui/",
+ "/config/",
+ "/file/",
+ "/find/",
+ "/instance/",
+ "/mcp/",
+ "/permission/",
+ "/project/",
+ "/provider/",
+ "/pty/",
+ "/question/",
+ "/experimental/",
+ }
+ for _, path := range openCodeAPIPaths {
+ r.mux.HandleFunc(path, RequireAuth(r.config, r.aiHandler.HandleOpenCodeAPI))
+ }
+
// Agent WebSocket for AI command execution
r.mux.HandleFunc("/api/agent/ws", r.handleAgentWebSocket)
@@ -2425,6 +2448,21 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
strings.HasPrefix(req.URL.Path, "/socket.io/") ||
strings.HasPrefix(req.URL.Path, "/download/") ||
strings.HasPrefix(req.URL.Path, "/opencode") ||
+ // OpenCode API paths - proxied to OpenCode backend for iframe embedding
+ strings.HasPrefix(req.URL.Path, "/global/") ||
+ strings.HasPrefix(req.URL.Path, "/session/") ||
+ strings.HasPrefix(req.URL.Path, "/tui/") ||
+ strings.HasPrefix(req.URL.Path, "/config/") ||
+ strings.HasPrefix(req.URL.Path, "/file/") ||
+ strings.HasPrefix(req.URL.Path, "/find/") ||
+ strings.HasPrefix(req.URL.Path, "/instance/") ||
+ strings.HasPrefix(req.URL.Path, "/mcp/") ||
+ strings.HasPrefix(req.URL.Path, "/permission/") ||
+ strings.HasPrefix(req.URL.Path, "/project/") ||
+ strings.HasPrefix(req.URL.Path, "/provider/") ||
+ strings.HasPrefix(req.URL.Path, "/pty/") ||
+ strings.HasPrefix(req.URL.Path, "/question/") ||
+ strings.HasPrefix(req.URL.Path, "/experimental/") ||
req.URL.Path == "/simple-stats" ||
req.URL.Path == "/install-docker-agent.sh" ||
req.URL.Path == "/install-container-agent.sh" ||