Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix context logic
perf3ct9834e77bb4ea
diff --git a/src/services/llm/context_extractor.ts b/src/services/llm/context_extractor.tsindex d12fcd97c7..41517035f9 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':- case 'mindMap':- case 'relationMap':- case 'geoMap':if (mime === 'application/json') {try {// Parse JSON contentconst 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);+ // 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');- break;- }+ formattedContent += 'Canvas content:\n' + texts.join('\n');+ } else {+ formattedContent += '[Empty canvas]';}- 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;- };+ }+ 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);- if (jsonContent.root) {- formattedContent += 'Mind map content:\n' + extractMindMapNodes(jsonContent.root).join('\n');- break;+ // Extract node text from mind map+ const extractMindMapNodes = (node: any): string[] => {+ let texts: string[] = [];+ if (node.text) {+ texts.push(node.text);}- }- 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 (node.children && Array.isArray(node.children)) {+ for (const child of node.children) {+ texts = texts.concat(extractMindMapNodes(child));+ }}+ return texts;+ };- 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('; ');- }+ 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);- formattedContent += result;- break;+ // 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('; ');}- else if (type === 'geoMap') {- let result = 'Geographic map content:\n';- if (jsonContent.markers && Array.isArray(jsonContent.markers)) {+ 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);++ let result = 'Geographic map content:\n';++ 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':