feat: port holistic node refinement contract
This commit is contained in:
@@ -4,7 +4,14 @@ import { generateDescription } from '@/services/database/descriptionService';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
type NodeMetadata = { source?: string; channel_name?: string; author?: string; site_name?: string; type?: string } & Record<string, unknown>;
|
||||
type NodeMetadata = {
|
||||
source?: string;
|
||||
channel_name?: string;
|
||||
author?: string;
|
||||
site_name?: string;
|
||||
type?: string;
|
||||
source_metadata?: Record<string, unknown>;
|
||||
} & Record<string, unknown>;
|
||||
|
||||
function parseMetadata(raw: unknown): NodeMetadata | undefined {
|
||||
if (!raw) return undefined;
|
||||
@@ -27,9 +34,14 @@ async function enrichYoutubeMetadataIfMissing(link: string, metadata: NodeMetada
|
||||
if (!url.includes('youtube.com') && !url.includes('youtu.be')) return metadata;
|
||||
|
||||
const existing = metadata || {};
|
||||
const existingSourceMetadata = typeof existing.source_metadata === 'object' && existing.source_metadata
|
||||
? existing.source_metadata
|
||||
: {};
|
||||
const hasCreatorHint = Boolean(
|
||||
(typeof existing.author === 'string' && existing.author.trim()) ||
|
||||
(typeof existing.channel_name === 'string' && existing.channel_name.trim())
|
||||
(typeof existing.channel_name === 'string' && existing.channel_name.trim()) ||
|
||||
(typeof existingSourceMetadata.author === 'string' && existingSourceMetadata.author.trim()) ||
|
||||
(typeof existingSourceMetadata.channel_name === 'string' && existingSourceMetadata.channel_name.trim())
|
||||
);
|
||||
if (hasCreatorHint) return existing;
|
||||
|
||||
@@ -44,9 +56,20 @@ async function enrichYoutubeMetadataIfMissing(link: string, metadata: NodeMetada
|
||||
|
||||
return {
|
||||
...existing,
|
||||
source: typeof existing.source === 'string' && existing.source.trim().length > 0 ? existing.source : 'youtube',
|
||||
channel_name: typeof existing.channel_name === 'string' && existing.channel_name.trim().length > 0 ? existing.channel_name : authorName,
|
||||
site_name: typeof existing.site_name === 'string' && existing.site_name.trim().length > 0 ? existing.site_name : (providerName || 'YouTube'),
|
||||
type: typeof existing.type === 'string' && existing.type.trim().length > 0 ? existing.type : 'youtube',
|
||||
source_metadata: {
|
||||
...existingSourceMetadata,
|
||||
channel_name: typeof existing.channel_name === 'string' && existing.channel_name.trim().length > 0
|
||||
? existing.channel_name
|
||||
: (typeof existingSourceMetadata.channel_name === 'string' && existingSourceMetadata.channel_name.trim().length > 0
|
||||
? existingSourceMetadata.channel_name
|
||||
: authorName),
|
||||
site_name: typeof existing.site_name === 'string' && existing.site_name.trim().length > 0
|
||||
? existing.site_name
|
||||
: (typeof existingSourceMetadata.site_name === 'string' && existingSourceMetadata.site_name.trim().length > 0
|
||||
? existingSourceMetadata.site_name
|
||||
: (providerName || 'YouTube')),
|
||||
},
|
||||
};
|
||||
} catch {
|
||||
return existing;
|
||||
@@ -88,8 +111,6 @@ export async function POST(
|
||||
source: node.source || node.description || undefined,
|
||||
link: node.link || undefined,
|
||||
metadata: enrichedMetadata,
|
||||
|
||||
dimensions: node.dimensions || []
|
||||
});
|
||||
|
||||
// Update the node with the new description
|
||||
|
||||
+15
-20
@@ -2,8 +2,7 @@ import { NextRequest, NextResponse } from 'next/server';
|
||||
import { contextService, nodeService } from '@/services/database';
|
||||
import { autoEmbedQueue } from '@/services/embedding/autoEmbedQueue';
|
||||
import { hasSufficientContent } from '@/services/embedding/constants';
|
||||
import { coerceDescriptionForStorage, normalizeDimensions } from '@/services/database/quality';
|
||||
import { formatUnknownDimensionsError, getUnknownDimensions } from '@/services/database/dimensionValidation';
|
||||
import { coerceDescriptionForStorage } from '@/services/database/quality';
|
||||
import { normalizeNodeLink } from '@/utils/nodeLink';
|
||||
import { mergeNodeMetadata } from '@/services/nodes/metadata';
|
||||
|
||||
@@ -94,26 +93,26 @@ export async function PUT(
|
||||
});
|
||||
}
|
||||
|
||||
if (Array.isArray(body.dimensions)) {
|
||||
updates.dimensions = normalizeDimensions(body.dimensions, 5);
|
||||
const unknownDimensions = getUnknownDimensions(updates.dimensions as string[]);
|
||||
if (unknownDimensions.length > 0) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: formatUnknownDimensionsError(unknownDimensions)
|
||||
}, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
delete updates.notes;
|
||||
delete updates.chunk;
|
||||
|
||||
if (Object.prototype.hasOwnProperty.call(body, 'context_id') || Object.prototype.hasOwnProperty.call(body, 'context_name')) {
|
||||
if (body.metadata !== undefined) {
|
||||
updates.metadata = mergeNodeMetadata(existingNode.metadata, body.metadata);
|
||||
}
|
||||
|
||||
if (Object.prototype.hasOwnProperty.call(body, 'context_name')) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: 'context_name is only supported on node creation. Use context_id for updates.'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
if (Object.prototype.hasOwnProperty.call(body, 'context_id')) {
|
||||
try {
|
||||
updates.context_id = await contextService.resolveContextId({
|
||||
const resolvedContextId = await contextService.resolveContextId({
|
||||
context_id: body.context_id,
|
||||
context_name: body.context_name,
|
||||
});
|
||||
updates.context_id = resolvedContextId;
|
||||
} catch (error) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
@@ -122,10 +121,6 @@ export async function PUT(
|
||||
}
|
||||
}
|
||||
|
||||
if (body.metadata !== undefined) {
|
||||
updates.metadata = mergeNodeMetadata(existingNode.metadata, body.metadata);
|
||||
}
|
||||
|
||||
const incomingSource = typeof body.source === 'string' ? body.source : undefined;
|
||||
const existingSource = existingNode.source ?? '';
|
||||
|
||||
|
||||
+14
-54
@@ -4,11 +4,9 @@ import { Node, NodeFilters } from '@/types/database';
|
||||
import { autoEmbedQueue } from '@/services/embedding/autoEmbedQueue';
|
||||
import { generateDescription } from '@/services/database/descriptionService';
|
||||
import { scheduleAutoEdgeCreation } from '@/services/agents/autoEdge';
|
||||
import { coerceDescriptionForStorage, normalizeDimensions } from '@/services/database/quality';
|
||||
import { formatUnknownDimensionsError, getUnknownDimensions } from '@/services/database/dimensionValidation';
|
||||
import { coerceDescriptionForStorage } from '@/services/database/quality';
|
||||
import { normalizeNodeLink } from '@/utils/nodeLink';
|
||||
import { buildCanonicalNodeMetadata } from '@/services/nodes/metadata';
|
||||
import { inferBestContextIdForNode } from '@/services/context/contextAssignment';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
@@ -30,18 +28,6 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
}
|
||||
|
||||
// Handle dimensions parameter (comma-separated)
|
||||
const dimensionsParam = searchParams.get('dimensions');
|
||||
if (dimensionsParam) {
|
||||
filters.dimensions = dimensionsParam.split(',').map(dim => dim.trim()).filter(Boolean);
|
||||
}
|
||||
|
||||
// Handle dimensionsMatch parameter (any|all)
|
||||
const dimensionsMatchParam = searchParams.get('dimensionsMatch');
|
||||
if (dimensionsMatchParam === 'all') {
|
||||
filters.dimensionsMatch = 'all';
|
||||
}
|
||||
|
||||
// Handle sortBy parameter (sortBy=edges|updated|created)
|
||||
const sortByParam = searchParams.get('sortBy');
|
||||
if (sortByParam === 'edges' || sortByParam === 'updated' || sortByParam === 'created' || sortByParam === 'event_date') {
|
||||
@@ -115,16 +101,6 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
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);
|
||||
const unknownDimensions = getUnknownDimensions(trimmedProvidedDimensions);
|
||||
if (unknownDimensions.length > 0) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: formatUnknownDimensionsError(unknownDimensions)
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
// Use provided description if present, otherwise auto-generate
|
||||
const isUserSuppliedDescription = typeof body.description === 'string' && body.description.trim().length > 0;
|
||||
let nodeDescription: string | undefined = isUserSuppliedDescription
|
||||
@@ -138,7 +114,6 @@ export async function POST(request: NextRequest) {
|
||||
source: rawSource?.slice(0, 2000) || undefined,
|
||||
link: normalizedLink || undefined,
|
||||
metadata: body.metadata,
|
||||
dimensions: trimmedProvidedDimensions
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error generating description:', error);
|
||||
@@ -160,8 +135,6 @@ export async function POST(request: NextRequest) {
|
||||
console.warn(`[DescriptionQuality] Weak description for node "${body.title}": "${finalDescription}"`);
|
||||
}
|
||||
|
||||
// Use only provided dimensions (no auto-assignment)
|
||||
const finalDimensions = trimmedProvidedDimensions;
|
||||
const sourceToStore = rawSource || [body.title, nodeDescription].filter(Boolean).join('\n\n').trim() || null;
|
||||
let chunkStatus: Node['chunk_status'];
|
||||
|
||||
@@ -169,6 +142,13 @@ export async function POST(request: NextRequest) {
|
||||
chunkStatus = 'not_chunked';
|
||||
}
|
||||
|
||||
const inferredType =
|
||||
typeof body.metadata?.type === 'string'
|
||||
? body.metadata.type
|
||||
: typeof body.metadata?.source === 'string'
|
||||
? body.metadata.source
|
||||
: undefined;
|
||||
|
||||
let resolvedContextId: number | null | undefined;
|
||||
try {
|
||||
resolvedContextId = await contextService.resolveContextId({
|
||||
@@ -176,25 +156,10 @@ export async function POST(request: NextRequest) {
|
||||
context_name: body.context_name,
|
||||
});
|
||||
} catch (error) {
|
||||
console.warn('[nodes.create] Invalid explicit context input, falling back to inheritance/inference:', error);
|
||||
resolvedContextId = undefined;
|
||||
}
|
||||
|
||||
if (resolvedContextId === undefined && typeof body.active_context_id === 'number' && Number.isInteger(body.active_context_id) && body.active_context_id > 0) {
|
||||
const inherited = await contextService.getContextById(body.active_context_id);
|
||||
if (inherited) {
|
||||
resolvedContextId = inherited.id;
|
||||
}
|
||||
}
|
||||
|
||||
if (resolvedContextId == null) {
|
||||
resolvedContextId = await inferBestContextIdForNode({
|
||||
title: body.title,
|
||||
description: finalDescription,
|
||||
source: sourceToStore,
|
||||
dimensions: finalDimensions,
|
||||
metadata: body.metadata,
|
||||
});
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Invalid context input'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
const node = await nodeService.createNode({
|
||||
@@ -203,16 +168,11 @@ export async function POST(request: NextRequest) {
|
||||
source: sourceToStore ?? undefined,
|
||||
event_date: eventDate ?? undefined,
|
||||
link: normalizedLink ?? undefined,
|
||||
dimensions: finalDimensions,
|
||||
chunk_status: chunkStatus,
|
||||
context_id: resolvedContextId,
|
||||
metadata: buildCanonicalNodeMetadata({
|
||||
metadata: body.metadata || {},
|
||||
type: typeof body.metadata?.type === 'string'
|
||||
? body.metadata.type
|
||||
: typeof body.metadata?.source === 'string'
|
||||
? body.metadata.source
|
||||
: undefined,
|
||||
type: inferredType,
|
||||
state: body.metadata?.state === 'processed' ? 'processed' : 'not_processed',
|
||||
})
|
||||
});
|
||||
@@ -229,7 +189,7 @@ export async function POST(request: NextRequest) {
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: node,
|
||||
message: `Node created successfully with dimensions: ${finalDimensions.join(', ')}`
|
||||
message: `Node created successfully`
|
||||
}, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('Error creating node:', error);
|
||||
|
||||
@@ -29,7 +29,6 @@ export async function GET(request: NextRequest) {
|
||||
const results = nodes.map(node => ({
|
||||
id: node.id,
|
||||
title: node.title,
|
||||
dimensions: node.dimensions
|
||||
}));
|
||||
|
||||
return NextResponse.json({
|
||||
@@ -46,4 +45,4 @@ export async function GET(request: NextRequest) {
|
||||
error: error instanceof Error ? error.message : 'Failed to search nodes'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user