roadmap phase 1 complete

This commit is contained in:
Storme-bit
2026-04-27 03:10:39 -07:00
parent 9fe8e568cf
commit 1a97b19280
19 changed files with 759 additions and 281 deletions

View File

@@ -4,18 +4,23 @@ const { parseRow } = require ('@nexusai/shared')
/******* Entities ********/
// Upsert an entity - insert or update if (name, type) already exists
function upsertEntity(name, type, notes = null, metadata = null) {
function upsertEntity(name, type, notes = null, metadata = null, source = 'extraction') {
const db = getDB();
const stmt = db.prepare(`
INSERT INTO entities (name, type, notes, metadata)
VALUES (?, ?, ?, ?)
ON CONFLICT(name, type) DO UPDATE SET
notes = COALESCE(entities.notes, excluded.notes),
metadata = excluded.metadata,
updated_at = unixepoch()
`);
const result = stmt.run(name, type, notes, metadata ? JSON.stringify(metadata) : null);
const stmt = db.prepare(`
INSERT INTO entities (name, type, notes, metadata, source)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(name, type) DO UPDATE SET
-- First extraction wins: notes are never overwritten once set.
-- Revisit during Memory Consolidation Lifecycle (Phase 2) — once entity
-- quality scoring exists, a higher-confidence extraction should be able
-- to replace stale notes rather than being silently dropped.
notes = COALESCE(entities.notes, excluded.notes),
metadata = excluded.metadata,
mention_count = entities.mention_count + 1,
last_seen_at = unixepoch(),
updated_at = unixepoch()
`);
stmt.run(name, type, notes, metadata ? JSON.stringify(metadata) : null, source);
return getEntityByNameType(name, type);
}
@@ -40,15 +45,17 @@ function deleteEntity(id) {
/********* Relationships *********/
// Upsert a relationship, insert or ignore if (from_id, to_id, label) already exists
function upsertRelationship(fromId, toId, label, metadata = null){
function upsertRelationship(fromId, toId, label, notes = null, metadata = null) {
const db = getDB();
const stmt = db.prepare(`
INSERT INTO relationships (from_id, to_id, label, metadata)
VALUES (?, ?, ?, ?)
ON CONFLICT(from_id, to_id, label) DO NOTHING
INSERT INTO relationships (from_id, to_id, label, notes, metadata)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(from_id, to_id, label) DO UPDATE SET
mention_count = relationships.mention_count + 1,
-- First extraction wins for notes — same policy as entities.
notes = COALESCE(relationships.notes, excluded.notes)
`);
const result = stmt.run(fromId, toId, label, metadata ?JSON.stringify(metadata) : null);
stmt.run(fromId, toId, label, notes, metadata ? JSON.stringify(metadata) : null);
return getRelationship(fromId, toId, label);
}
@@ -69,7 +76,7 @@ function getEntityByNameType(name, type) {
}
// Retrive all relationships originating from a given entity
function getRelationshipsByEntity(entityId) {
function getOutboundRelationships(entityId) {
const db = getDB();
return db.prepare(`SELECT * FROM relationships WHERE from_id = ?`).all(entityId).map(parseRow);
}
@@ -81,14 +88,23 @@ function deleteRelationship(fromId, toId, label) {
db.prepare(`DELETE FROM relationships WHERE from_id = ? AND to_id = ? AND label = ?`).run(fromId, toId, label);
}
function linkEntityToEpisode(entityId, episodeId) {
const db = getDB();
db.prepare(`
INSERT OR IGNORE INTO entity_episodes (entity_id, episode_id)
VALUES (?, ?)
`).run(entityId, episodeId);
}
module.exports = {
upsertEntity,
getEntity,
getEntitiesByType,
getEntityByNameType,
deleteEntity,
linkEntityToEpisode,
upsertRelationship,
getRelationship,
getRelationshipsByEntity,
getOutboundRelationships,
deleteRelationship
}