feat: port source-first ingestion to os repo

- make source the canonical field across os routes, tools, ui, and mcp
- align fresh-install schema, search, and embedding flows with source-first ingestion

Generated with Claude Code
This commit is contained in:
“BeeRad”
2026-03-20 12:36:26 +11:00
parent 41f8498d4a
commit 28e570696c
34 changed files with 588 additions and 521 deletions
+2 -4
View File
@@ -57,10 +57,8 @@ export async function POST(request: NextRequest) {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title,
// Give the description generator and UI a readable summary plus a short excerpt.
notes: `Imported PDF document: ${file.name} (${extraction.metadata.pages} pages, ${Math.round(extraction.metadata.text_length / 1000)}k characters).\n\nPreview:\n${extraction.chunk.slice(0, 1200)}`,
// Full extracted text goes in chunk for universal chunking/embedding
chunk: extraction.chunk,
description: `PDF document imported from ${file.name}${extraction.metadata.pages} pages of extracted text. Useful as a searchable local source.`,
source: extraction.chunk,
metadata: {
source: 'pdf_upload',
original_filename: file.name,
+5 -4
View File
@@ -134,7 +134,7 @@ function createRAHServer(): McpServer {
id: node.id,
title: node.title,
description: node.description ?? null,
notes: node.notes ?? null,
source: node.source ?? node.notes ?? null,
link: node.link ?? null,
dimensions: node.dimensions || [],
metadata: node.metadata || {},
@@ -263,21 +263,22 @@ function createRAHServer(): McpServer {
description: 'Create a new node in the knowledge graph.',
inputSchema: {
title: z.string().min(1).max(160).describe('Node title'),
content: z.string().max(20000).optional().describe('Node content/notes'),
content: z.string().max(20000).optional().describe('Legacy content field; mapped to source'),
source: z.string().max(50000).optional().describe('Full source text'),
link: z.string().url().optional().describe('Source URL'),
description: z.string().max(2000).optional().describe('Short description'),
dimensions: z.array(z.string()).min(1).max(5).describe('Categories/tags (at least 1)'),
metadata: z.record(z.any()).optional().describe('Additional metadata'),
},
},
async ({ title, content, link, description, dimensions, metadata }) => {
async ({ title, content, source, link, description, dimensions, metadata }) => {
// Call the nodes API internally
const response = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000'}/api/nodes`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title: title.trim(),
content: content?.trim(),
source: source?.trim() || content?.trim(),
link: link?.trim(),
description: description?.trim(),
dimensions,
@@ -85,7 +85,7 @@ export async function POST(
// Generate new description using the description service
const newDescription = await generateDescription({
title: node.title,
notes: node.notes || undefined,
notes: node.source || node.description || undefined,
link: node.link || undefined,
metadata: enrichedMetadata,
+8 -10
View File
@@ -85,13 +85,15 @@ export async function PUT(
updates.dimensions = normalizeDimensions(body.dimensions, 5);
}
const incomingChunk = typeof body.chunk === 'string' ? body.chunk : undefined;
const incomingNotes = typeof body.notes === 'string' ? body.notes : undefined;
const existingChunk = existingNode.chunk ?? '';
delete updates.notes;
delete updates.chunk;
if (incomingChunk !== undefined) {
const trimmedIncoming = incomingChunk.trim();
const trimmedExisting = existingChunk.trim();
const incomingSource = typeof body.source === 'string' ? body.source : undefined;
const existingSource = existingNode.source ?? '';
if (incomingSource !== undefined) {
const trimmedIncoming = incomingSource.trim();
const trimmedExisting = existingSource.trim();
if (!trimmedIncoming) {
updates.chunk_status = null;
@@ -101,10 +103,6 @@ export async function PUT(
} else {
delete updates.chunk_status;
}
} else if (!existingChunk.trim() && hasSufficientContent(incomingNotes)) {
updates.chunk = incomingNotes;
updates.chunk_status = 'not_chunked';
shouldQueueEmbed = true;
}
const node = await nodeService.updateNode(nodeId, updates);
+7 -21
View File
@@ -92,15 +92,15 @@ export async function POST(request: NextRequest) {
// Sanitize title (strip extraction artifacts)
body.title = sanitizeTitle(body.title);
const rawNotes = typeof body.notes === 'string' ? body.notes : null;
const rawChunk = typeof body.chunk === 'string' ? body.chunk : null;
const rawSource = typeof body.source === 'string' ? body.source.trim() : null;
const eventDate = typeof body.event_date === 'string' ? body.event_date : null;
// Process provided dimensions first (needed for description generation)
const trimmedProvidedDimensions = normalizeDimensions(body.dimensions, 5);
// Use provided description if present, otherwise auto-generate
let nodeDescription: string | undefined = typeof body.description === 'string' && body.description.trim()
const isUserSuppliedDescription = typeof body.description === 'string' && body.description.trim().length > 0;
let nodeDescription: string | undefined = isUserSuppliedDescription
? body.description.trim().slice(0, 280)
: undefined;
@@ -108,7 +108,7 @@ export async function POST(request: NextRequest) {
try {
nodeDescription = await generateDescription({
title: body.title,
notes: rawNotes || rawChunk?.slice(0, 2000) || undefined,
notes: rawSource?.slice(0, 2000) || undefined,
link: body.link || undefined,
metadata: body.metadata,
dimensions: trimmedProvidedDimensions
@@ -127,7 +127,6 @@ export async function POST(request: NextRequest) {
const descriptionError = validateExplicitDescription(finalDescription);
if (descriptionError) {
const isUserSuppliedDescription = typeof body.description === 'string' && body.description.trim().length > 0;
if (isUserSuppliedDescription) {
return NextResponse.json({
success: false,
@@ -148,33 +147,20 @@ export async function POST(request: NextRequest) {
// Use only provided dimensions (no auto-assignment)
const finalDimensions = trimmedProvidedDimensions;
let chunkToStore = rawChunk;
const sourceToStore = rawSource || [body.title, nodeDescription].filter(Boolean).join('\n\n').trim() || null;
let chunkStatus: Node['chunk_status'];
if (chunkToStore && chunkToStore.trim().length > 0) {
if (sourceToStore && sourceToStore.trim().length > 0) {
chunkStatus = 'not_chunked';
} else {
// Build chunk from all available notes if not provided
// This ensures every node gets at least one chunk for search
const fallbackContent = [body.title, nodeDescription, rawNotes]
.filter(Boolean)
.join('\n\n')
.trim();
if (fallbackContent) {
chunkToStore = fallbackContent;
chunkStatus = 'not_chunked';
}
}
const node = await nodeService.createNode({
title: body.title,
description: finalDescription,
notes: rawNotes ?? undefined,
source: sourceToStore ?? undefined,
event_date: eventDate ?? undefined,
link: body.link,
dimensions: finalDimensions,
chunk: chunkToStore ?? undefined,
chunk_status: chunkStatus,
metadata: body.metadata || {}
});