From 4e40fa3e4782e2c3a3326e5fcabab013bfd36816 Mon Sep 17 00:00:00 2001 From: 0xbbjoker <0xbbjoker@proton.me> Date: Mon, 8 Sep 2025 13:34:13 +0200 Subject: [PATCH] fix: knowledge panel loading --- package.json | 2 +- src/frontend/ui/knowledge-tab.tsx | 47 +++++++++++++++++++++++-------- src/routes.ts | 43 ++++++++++++++++++++-------- 3 files changed, 69 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index b771b51..1288390 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@elizaos/plugin-knowledge", "description": "Plugin for Knowledge", - "version": "1.2.2", + "version": "1.2.3", "type": "module", "main": "dist/index.js", "module": "dist/index.js", diff --git a/src/frontend/ui/knowledge-tab.tsx b/src/frontend/ui/knowledge-tab.tsx index 5198f69..04f5895 100644 --- a/src/frontend/ui/knowledge-tab.tsx +++ b/src/frontend/ui/knowledge-tab.tsx @@ -26,6 +26,16 @@ import { Card } from './card'; import { Input } from './input'; import { MemoryGraph } from './memory-graph'; +// Declare global window extension for TypeScript +declare global { + interface Window { + ELIZA_CONFIG?: { + agentId: string; + apiBase: string; + }; + } +} + // Local utility function instead of importing from client const cn = (...classes: (string | undefined | null | false)[]) => { return classes.filter(Boolean).join(' '); @@ -238,18 +248,28 @@ const getCorrectMimeType = (file: File): string => { return file.type || 'application/octet-stream'; }; +// Get the API base path from the injected configuration +const getApiBase = () => { + // Check if we have the ELIZA_CONFIG from the server + if (window.ELIZA_CONFIG?.apiBase) { + return window.ELIZA_CONFIG.apiBase; + } + return '/api'; +}; + const apiClient = { getKnowledgeDocuments: async ( agentId: UUID, options?: { limit?: number; before?: number; includeEmbedding?: boolean } ) => { const params = new URLSearchParams(); - params.append('agentId', agentId); + // Don't append agentId to params if it's already in the URL path if (options?.limit) params.append('limit', options.limit.toString()); if (options?.before) params.append('before', options.before.toString()); if (options?.includeEmbedding) params.append('includeEmbedding', 'true'); - const response = await fetch(`/api/documents?${params.toString()}`); + const apiBase = getApiBase(); + const response = await fetch(`${apiBase}/documents?${params.toString()}`); if (!response.ok) { const errorText = await response.text(); throw new Error(`Failed to fetch knowledge documents: ${response.status} ${errorText}`); @@ -273,7 +293,8 @@ const apiClient = { if (options?.documentId) params.append('documentId', options.documentId); if (options?.documentsOnly) params.append('documentsOnly', 'true'); - const response = await fetch(`/api/knowledges?${params.toString()}`); + const apiBase = getApiBase(); + const response = await fetch(`${apiBase}/knowledges?${params.toString()}`); if (!response.ok) { const errorText = await response.text(); throw new Error(`Failed to fetch knowledge chunks: ${response.status} ${errorText}`); @@ -285,7 +306,8 @@ const apiClient = { const params = new URLSearchParams(); params.append('agentId', agentId); - const response = await fetch(`/api/documents/${knowledgeId}?${params.toString()}`, { + const apiBase = getApiBase(); + const response = await fetch(`${apiBase}/documents/${knowledgeId}?${params.toString()}`, { method: 'DELETE', }); if (!response.ok) { @@ -307,7 +329,8 @@ const apiClient = { } formData.append('agentId', agentId); - const response = await fetch(`/api/documents`, { + const apiBase = getApiBase(); + const response = await fetch(`${apiBase}/documents`, { method: 'POST', body: formData, }); @@ -330,7 +353,8 @@ const apiClient = { params.append('threshold', threshold.toString()); params.append('limit', limit.toString()); - const response = await fetch(`/api/search?${params.toString()}`); + const apiBase = getApiBase(); + const response = await fetch(`${apiBase}/search?${params.toString()}`); if (!response.ok) { const errorText = await response.text(); throw new Error(`Failed to search knowledge: ${response.status} ${errorText}`); @@ -673,7 +697,8 @@ export function KnowledgeTab({ agentId }: { agentId: UUID }) { setUrlError(null); try { - const result = await fetch(`/api/documents`, { + const apiBase = getApiBase(); + const result = await fetch(`${apiBase}/documents`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -729,7 +754,8 @@ export function KnowledgeTab({ agentId }: { agentId: UUID }) { } formData.append('agentId', agentId); - const response = await fetch('/api/documents', { + const apiBase = getApiBase(); + const response = await fetch(`${apiBase}/documents`, { method: 'POST', body: formData, }); @@ -1124,9 +1150,8 @@ export function KnowledgeTab({ agentId }: { agentId: UUID }) { return (