From 9834e77bb4eaee9502624ea71e7323dcc4047745 Mon Sep 17 00:00:00 2001 From: perf3ct Date: Mon, 10 Mar 2025 20:04:49 +0000 Subject: [PATCH] fix context logic --- src/services/llm/context_extractor.ts | 174 ++++++++++++++++---------- 1 file changed, 106 insertions(+), 68 deletions(-) diff --git a/src/services/llm/context_extractor.ts b/src/services/llm/context_extractor.ts index d12fcd97c..41517035f 100644 --- a/src/services/llm/context_extractor.ts +++ b/src/services/llm/context_extractor.ts @@ -103,94 +103,132 @@ export class ContextExtractor { formattedContent += '```\n' + content + '\n```'; break; case 'canvas': + if (mime === 'application/json') { + try { + // Parse JSON content + const jsonContent = JSON.parse(content); + + // Extract text elements from canvas + if (jsonContent.elements && Array.isArray(jsonContent.elements)) { + const texts = jsonContent.elements + .filter((element: any) => element.type === 'text' && element.text) + .map((element: any) => element.text); + + formattedContent += 'Canvas content:\n' + texts.join('\n'); + } else { + formattedContent += '[Empty canvas]'; + } + } + catch (e: any) { + formattedContent += `[Error parsing canvas content: ${e.message}]`; + } + } else { + formattedContent += '[Canvas content]'; + } + break; + case 'mindMap': + if (mime === 'application/json') { + try { + // Parse JSON content + const jsonContent = JSON.parse(content); + + // Extract node text from mind map + const extractMindMapNodes = (node: any): string[] => { + let texts: string[] = []; + if (node.text) { + texts.push(node.text); + } + if (node.children && Array.isArray(node.children)) { + for (const child of node.children) { + texts = texts.concat(extractMindMapNodes(child)); + } + } + return texts; + }; + + if (jsonContent.root) { + formattedContent += 'Mind map content:\n' + extractMindMapNodes(jsonContent.root).join('\n'); + } else { + formattedContent += '[Empty mind map]'; + } + } + catch (e: any) { + formattedContent += `[Error parsing mind map content: ${e.message}]`; + } + } else { + formattedContent += '[Mind map content]'; + } + break; + case 'relationMap': + if (mime === 'application/json') { + try { + // Parse JSON content + const jsonContent = JSON.parse(content); + + // Extract relation map entities and connections + let result = 'Relation map content:\n'; + + if (jsonContent.notes && Array.isArray(jsonContent.notes)) { + result += 'Notes: ' + jsonContent.notes + .map((note: any) => note.title || note.name) + .filter(Boolean) + .join(', ') + '\n'; + } + + if (jsonContent.relations && Array.isArray(jsonContent.relations)) { + result += 'Relations: ' + jsonContent.relations + .map((rel: any) => { + const sourceNote = jsonContent.notes.find((n: any) => n.noteId === rel.sourceNoteId); + const targetNote = jsonContent.notes.find((n: any) => n.noteId === rel.targetNoteId); + const source = sourceNote ? (sourceNote.title || sourceNote.name) : 'unknown'; + const target = targetNote ? (targetNote.title || targetNote.name) : 'unknown'; + return `${source} → ${rel.name || ''} → ${target}`; + }) + .join('; '); + } + + formattedContent += result; + } + catch (e: any) { + formattedContent += `[Error parsing relation map content: ${e.message}]`; + } + } else { + formattedContent += '[Relation map content]'; + } + break; + case 'geoMap': if (mime === 'application/json') { try { // Parse JSON content const jsonContent = JSON.parse(content); - if (type === 'canvas') { - // Extract text elements from canvas - if (jsonContent.elements && Array.isArray(jsonContent.elements)) { - const texts = jsonContent.elements - .filter((element: any) => element.type === 'text' && element.text) - .map((element: any) => element.text); + let result = 'Geographic map content:\n'; - formattedContent += 'Canvas content:\n' + texts.join('\n'); - break; - } - } - else if (type === 'mindMap') { - // Extract node text from mind map - const extractMindMapNodes = (node: any): string[] => { - let texts: string[] = []; - if (node.text) { - texts.push(node.text); - } - if (node.children && Array.isArray(node.children)) { - for (const child of node.children) { - texts = texts.concat(extractMindMapNodes(child)); - } - } - return texts; - }; - - if (jsonContent.root) { - formattedContent += 'Mind map content:\n' + extractMindMapNodes(jsonContent.root).join('\n'); - break; - } - } - else if (type === 'relationMap') { - // Extract relation map entities and connections - let result = 'Relation map content:\n'; - - if (jsonContent.notes && Array.isArray(jsonContent.notes)) { - result += 'Notes: ' + jsonContent.notes - .map((note: any) => note.title || note.name) - .filter(Boolean) - .join(', ') + '\n'; - } - - if (jsonContent.relations && Array.isArray(jsonContent.relations)) { - result += 'Relations: ' + jsonContent.relations - .map((rel: any) => { - const sourceNote = jsonContent.notes.find((n: any) => n.noteId === rel.sourceNoteId); - const targetNote = jsonContent.notes.find((n: any) => n.noteId === rel.targetNoteId); - const source = sourceNote ? (sourceNote.title || sourceNote.name) : 'unknown'; - const target = targetNote ? (targetNote.title || targetNote.name) : 'unknown'; - return `${source} → ${rel.name || ''} → ${target}`; - }) - .join('; '); - } - - formattedContent += result; - break; - } - else if (type === 'geoMap') { - let result = 'Geographic map content:\n'; - - if (jsonContent.markers && Array.isArray(jsonContent.markers)) { + if (jsonContent.markers && Array.isArray(jsonContent.markers)) { + if (jsonContent.markers.length > 0) { result += jsonContent.markers .map((marker: any) => { return `Location: ${marker.title || ''} (${marker.lat}, ${marker.lng})${marker.description ? ' - ' + marker.description : ''}`; }) .join('\n'); + } else { + result += 'Empty geographic map'; } - - formattedContent += result || 'Empty geographic map'; - break; + } else { + result += 'Empty geographic map'; } + + formattedContent += result; } catch (e: any) { - formattedContent += `[Error parsing ${type} content: ${e.message}]`; - break; + formattedContent += `[Error parsing geographic map content: ${e.message}]`; } + } else { + formattedContent += '[Geographic map content]'; } - - // If JSON parsing or specific handling failed, use default handling - formattedContent += `[${type} content]`; break; case 'mermaid':