feat: port holistic node refinement contract

This commit is contained in:
“BeeRad”
2026-04-11 21:37:52 +10:00
parent 35f9ecf89c
commit 3ae46245ec
119 changed files with 6596 additions and 10982 deletions
@@ -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
View File
@@ -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 ?? '';