-
Notifications
You must be signed in to change notification settings - Fork 9
fix: knowledge panel loading #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -513,6 +513,14 @@ async function knowledgePanelHandler(req: any, res: any, runtime: IAgentRuntime) | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| logger.debug(`[Document Processor] 🌐 Serving knowledge panel for agent ${agentId}`); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Extract the plugin API base path from the request path | ||||||||||||||||||||||||||||||||||||||||||
| // Request path will be like: /api/agents/[uuid]/plugins/knowledge/display | ||||||||||||||||||||||||||||||||||||||||||
| // We need: /api/agents/[uuid]/plugins/knowledge | ||||||||||||||||||||||||||||||||||||||||||
| const requestPath = req.originalUrl || req.url || req.path; | ||||||||||||||||||||||||||||||||||||||||||
| const pluginBasePath = requestPath.replace(/\/display.*$/, ''); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| logger.debug(`[Document Processor] 🌐 Plugin base path: ${pluginBasePath}`); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||
| const currentDir = path.dirname(new URL(import.meta.url).pathname); | ||||||||||||||||||||||||||||||||||||||||||
| // Serve the main index.html from Vite's build output | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -522,17 +530,22 @@ async function knowledgePanelHandler(req: any, res: any, runtime: IAgentRuntime) | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if (fs.existsSync(frontendPath)) { | ||||||||||||||||||||||||||||||||||||||||||
| const html = await fs.promises.readFile(frontendPath, 'utf8'); | ||||||||||||||||||||||||||||||||||||||||||
| // Inject config into existing HTML | ||||||||||||||||||||||||||||||||||||||||||
| const injectedHtml = html.replace( | ||||||||||||||||||||||||||||||||||||||||||
| // Inject config into existing HTML with the correct API base path | ||||||||||||||||||||||||||||||||||||||||||
| // Also fix relative asset paths to use absolute paths | ||||||||||||||||||||||||||||||||||||||||||
| let injectedHtml = html.replace( | ||||||||||||||||||||||||||||||||||||||||||
| '<head>', | ||||||||||||||||||||||||||||||||||||||||||
| `<head> | ||||||||||||||||||||||||||||||||||||||||||
| <script> | ||||||||||||||||||||||||||||||||||||||||||
| window.ELIZA_CONFIG = { | ||||||||||||||||||||||||||||||||||||||||||
| agentId: '${agentId}', | ||||||||||||||||||||||||||||||||||||||||||
| apiBase: '/api' | ||||||||||||||||||||||||||||||||||||||||||
| apiBase: '${pluginBasePath}' | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
| </script>` | ||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+533
to
544
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid XSS in injected config by serializing via JSON Injecting request-derived strings into a JS literal is fragile. Serialize instead. - let injectedHtml = html.replace(
- '<head>',
- `<head>
- <script>
- window.ELIZA_CONFIG = {
- agentId: '${agentId}',
- apiBase: '${pluginBasePath}'
- };
- </script>`
- );
+ const elizaConfig = JSON.stringify({ agentId, apiBase: pluginBasePath });
+ let injectedHtml = html.replace(
+ '<head>',
+ `<head><script>window.ELIZA_CONFIG=${elizaConfig};</script>`
+ );📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Fix relative asset paths to be absolute | ||||||||||||||||||||||||||||||||||||||||||
| injectedHtml = injectedHtml.replace(/src="\.\/assets\//g, `src="${pluginBasePath}/assets/`); | ||||||||||||||||||||||||||||||||||||||||||
| injectedHtml = injectedHtml.replace(/href="\.\/assets\//g, `href="${pluginBasePath}/assets/`); | ||||||||||||||||||||||||||||||||||||||||||
| res.writeHead(200, { 'Content-Type': 'text/html' }); | ||||||||||||||||||||||||||||||||||||||||||
| res.end(injectedHtml); | ||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -577,10 +590,10 @@ async function knowledgePanelHandler(req: any, res: any, runtime: IAgentRuntime) | |||||||||||||||||||||||||||||||||||||||||
| <script> | ||||||||||||||||||||||||||||||||||||||||||
| window.ELIZA_CONFIG = { | ||||||||||||||||||||||||||||||||||||||||||
| agentId: '${agentId}', | ||||||||||||||||||||||||||||||||||||||||||
| apiBase: '/api' | ||||||||||||||||||||||||||||||||||||||||||
| apiBase: '${pluginBasePath}' | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
| </script> | ||||||||||||||||||||||||||||||||||||||||||
| <link rel="stylesheet" href="./assets/${cssFile}"> | ||||||||||||||||||||||||||||||||||||||||||
| <link rel="stylesheet" href="${pluginBasePath}/assets/${cssFile}"> | ||||||||||||||||||||||||||||||||||||||||||
| <style> | ||||||||||||||||||||||||||||||||||||||||||
| body { font-family: system-ui, -apple-system, sans-serif; margin: 0; padding: 20px; } | ||||||||||||||||||||||||||||||||||||||||||
| .container { max-width: 1200px; margin: 0 auto; } | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -593,7 +606,7 @@ async function knowledgePanelHandler(req: any, res: any, runtime: IAgentRuntime) | |||||||||||||||||||||||||||||||||||||||||
| <div class="loading">Loading Knowledge Library...</div> | ||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||
| <script type="module" src="./assets/${jsFile}"></script> | ||||||||||||||||||||||||||||||||||||||||||
| <script type="module" src="${pluginBasePath}/assets/${jsFile}"></script> | ||||||||||||||||||||||||||||||||||||||||||
| </body> | ||||||||||||||||||||||||||||||||||||||||||
| </html>`; | ||||||||||||||||||||||||||||||||||||||||||
| res.writeHead(200, { 'Content-Type': 'text/html' }); | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -608,16 +621,24 @@ async function knowledgePanelHandler(req: any, res: any, runtime: IAgentRuntime) | |||||||||||||||||||||||||||||||||||||||||
| // Generic handler to serve static assets from the dist/assets directory | ||||||||||||||||||||||||||||||||||||||||||
| async function frontendAssetHandler(req: any, res: any, runtime: IAgentRuntime) { | ||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||
| logger.debug(`[Document Processor] 🌐 Asset request: ${req.path}`); | ||||||||||||||||||||||||||||||||||||||||||
| // Use originalUrl or url first, fallback to path | ||||||||||||||||||||||||||||||||||||||||||
| const fullPath = req.originalUrl || req.url || req.path; | ||||||||||||||||||||||||||||||||||||||||||
| logger.debug(`[Document Processor] 🌐 Asset request: ${fullPath}`); | ||||||||||||||||||||||||||||||||||||||||||
| const currentDir = path.dirname(new URL(import.meta.url).pathname); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const assetRequestPath = req.path; // This is the full path, e.g., /api/agents/X/plugins/knowledge/assets/file.js | ||||||||||||||||||||||||||||||||||||||||||
| // The path will be like: /api/agents/X/plugins/knowledge/assets/file.js | ||||||||||||||||||||||||||||||||||||||||||
| // We need to extract everything after '/assets/' | ||||||||||||||||||||||||||||||||||||||||||
| const assetsMarker = '/assets/'; | ||||||||||||||||||||||||||||||||||||||||||
| const assetsStartIndex = assetRequestPath.indexOf(assetsMarker); | ||||||||||||||||||||||||||||||||||||||||||
| const assetsStartIndex = fullPath.lastIndexOf(assetsMarker); // Use lastIndexOf to get the last occurrence | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let assetName = null; | ||||||||||||||||||||||||||||||||||||||||||
| if (assetsStartIndex !== -1) { | ||||||||||||||||||||||||||||||||||||||||||
| assetName = assetRequestPath.substring(assetsStartIndex + assetsMarker.length); | ||||||||||||||||||||||||||||||||||||||||||
| assetName = fullPath.substring(assetsStartIndex + assetsMarker.length); | ||||||||||||||||||||||||||||||||||||||||||
| // Remove any query parameters if present | ||||||||||||||||||||||||||||||||||||||||||
| const queryIndex = assetName.indexOf('?'); | ||||||||||||||||||||||||||||||||||||||||||
| if (queryIndex !== -1) { | ||||||||||||||||||||||||||||||||||||||||||
| assetName = assetName.substring(0, queryIndex); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if (!assetName || assetName.includes('..')) { | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -626,7 +647,7 @@ async function frontendAssetHandler(req: any, res: any, runtime: IAgentRuntime) | |||||||||||||||||||||||||||||||||||||||||
| res, | ||||||||||||||||||||||||||||||||||||||||||
| 400, | ||||||||||||||||||||||||||||||||||||||||||
| 'BAD_REQUEST', | ||||||||||||||||||||||||||||||||||||||||||
| `Invalid asset name: '${assetName}' from path ${assetRequestPath}` | ||||||||||||||||||||||||||||||||||||||||||
| `Invalid asset name: '${assetName}' from path ${fullPath}` | ||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: API Parameter Mismatch Causes Filtering Issues
The
getKnowledgeDocumentsAPI call no longer appends theagentIdparameter to its query, but its server-side handler still expects it. This creates an inconsistency with other API calls in the file and may lead to incorrect document filtering or API failures.