Skip to content

feat: AI-assisted knowledge graph auto-linking — entity extraction and edge suggestion from notes #285

@d-oit

Description

@d-oit

Problem

The knowledge graph (src/features/graph/) and the editor (src/features/editor/) are completely disconnected from the AI harness. Notes are never analyzed for entity extraction, and there is no automatic node/edge creation based on note content. src/lib/nlp.ts exists but only performs basic tokenization — no named entity recognition (NER) or relationship extraction.

Impact

  • Knowledge graph must be built entirely by hand, making it underutilized
  • Related notes/concepts are not automatically connected
  • No way to discover implicit relationships in the knowledge base

Proposed Implementation

1. LLM-powered entity extraction prompt

// src/lib/ai/entity-extractor.ts
export const ENTITY_EXTRACTION_PROMPT = `
Analyze the following note and extract:
1. Named entities (people, organizations, technologies, concepts, places)
2. Relationships between entities

Respond ONLY with valid JSON in this exact schema:
{
  "entities": [
    { "name": string, "type": "person" | "org" | "tech" | "concept" | "place" | "other", "description": string }
  ],
  "relationships": [
    { "from": string, "to": string, "label": string }
  ]
}

Note content:
{{CONTENT}}
`;

export async function extractEntities(
  content: string,
  provider: ILLMProvider,
  model: string
): Promise<EntityExtractionResult> {
  const prompt = ENTITY_EXTRACTION_PROMPT.replace('{{CONTENT}}', content.slice(0, 2000));
  const response = await provider.chat(
    [{ role: 'user', content: prompt }],
    { model, temperature: 0, maxTokens: 1000 }
  );
  return JSON.parse(response.content);
}

2. Entity suggestion UI

After saving a note, show a dismissible notification:

✨ AI found 3 entities and 2 relationships in this note.
[Add to Graph] [Review] [Dismiss]

The "Review" dialog shows:

  • List of extracted entities with checkboxes
  • List of proposed edges with checkboxes
  • "Add Selected to Graph" button

3. Graph integration service

// src/lib/ai/graph-linker.ts
export async function applyEntitiesToGraph(
  result: EntityExtractionResult,
  graphStore: GraphStore,
  selectedEntities: string[],
  selectedRelationships: string[]
): Promise<void> {
  // Add nodes for selected entities
  for (const entity of result.entities.filter(e => selectedEntities.includes(e.name))) {
    if (!graphStore.hasNode(entity.name)) {
      graphStore.addNode({
        id: crypto.randomUUID(),
        label: entity.name,
        type: entity.type,
        description: entity.description,
      });
    }
  }
  
  // Add edges for selected relationships
  for (const rel of result.relationships.filter(r => selectedRelationships.includes(`${r.from}->${r.to}`))) {
    const fromNode = graphStore.findNodeByLabel(rel.from);
    const toNode = graphStore.findNodeByLabel(rel.to);
    if (fromNode && toNode) {
      graphStore.addEdge({ from: fromNode.id, to: toNode.id, label: rel.label });
    }
  }
}

4. Note-to-graph link

When a graph node is created from a note, store noteId in node metadata so clicking the node opens the source note.

5. Trigger points

  • Manual trigger: "Extract entities" button in note editor toolbar
  • Auto-trigger (optional, configurable): Run after note save with debounce of 3 seconds
  • Batch mode: "Analyze all notes" action in graph sidebar

Acceptance Criteria

  • extractEntities() implemented with structured LLM prompt
  • Entity suggestion UI shown after note save/manual trigger
  • User can review and selectively add entities/relationships to graph
  • Graph nodes created from note entities, with noteId backlink
  • Graph edges created from extracted relationships
  • Works with all configured LLM providers
  • Unit tests for entity extraction with mock LLM responses
  • Integration tests for graph node/edge creation

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions