feat: sync runtime search and schema quality updates from app repo

- port retrieval, validation, and eval improvements relevant to os
- align prompts and dimensions with the flat single-agent model
- replace the old eval suite with the focused core scenarios

Generated with Codex
This commit is contained in:
“BeeRad”
2026-03-15 14:55:45 +11:00
parent 053c163e31
commit 4c75df101f
57 changed files with 1809 additions and 534 deletions
+38 -18
View File
@@ -1,20 +1,22 @@
import { tool } from 'ai';
import { z } from 'zod';
import { getInternalApiBaseUrl } from '@/services/runtime/apiBase';
import { normalizeDimensions, validateExplicitDescription } from '@/services/database/quality';
export const updateNodeTool = tool({
description: 'Update node fields',
description: 'Update node fields. Description is REQUIRED on every update and must explicitly state WHAT this is + WHY it matters.',
inputSchema: z.object({
id: z.number().describe('The ID of the node to update'),
updates: z.object({
title: z.string().optional().describe('New title'),
description: z.string().max(280).optional().describe('New description (overwrites existing). WHAT this is + WHY it matters. No "discusses/explores".'),
notes: z.string().optional().describe('New notes (appended to existing)'),
notes: z.string().optional().describe('User notes/analysis to append. USE THIS for workflow outputs, briefs, research notes, etc.'),
description: z.string().max(280).describe('REQUIRED on every update. Explicitly state WHAT this is + WHY it matters. No "discusses/explores".'),
link: z.string().optional().describe('New link'),
event_date: z.string().optional().describe('ISO date string for time-anchored nodes'),
event_date: z.string().optional().describe('When the thing actually happened (ISO 8601). Not when it was added to the graph.'),
dimensions: z.array(z.string()).optional().describe('New dimension tags - completely replaces existing dimensions'),
chunk: z.string().optional().describe('New chunk content'),
chunk: z.string().optional().describe('DO NOT USE - raw source text that triggers re-embedding. Only for source corrections.'),
metadata: z.record(z.any()).optional().describe('New metadata - completely replaces existing metadata')
}).describe('Object containing the fields to update')
}).describe('Object containing the fields to update. For adding notes/analysis, always use "notes" field.')
}),
execute: async ({ id, updates }) => {
try {
@@ -26,24 +28,40 @@ export const updateNodeTool = tool({
};
}
// FORCE APPEND for content field - fetch existing and append new content
if (!updates.description) {
return {
success: false,
error: 'Every node update requires an explicit description (WHAT this is + WHY it matters).',
data: null
};
}
const descriptionError = validateExplicitDescription(updates.description);
if (descriptionError) {
return {
success: false,
error: descriptionError,
data: null
};
}
// FORCE APPEND for notes field - fetch existing and append new notes
if (updates.notes) {
const fetchResponse = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000'}/api/nodes/${id}`);
const fetchResponse = await fetch(`${getInternalApiBaseUrl()}/api/nodes/${id}`);
if (fetchResponse.ok) {
const { node } = await fetchResponse.json();
const existingNotes = (node?.notes || '').trim();
const newNotes = updates.notes.trim();
// Skip if new content is identical to existing (model sent duplicate)
// Skip if new notes are identical to existing (model sent duplicate)
if (existingNotes === newNotes) {
console.log(`[updateNode] ERROR - new content identical to existing (${existingNotes.length} chars). Model should NOT call updateNode again.`);
console.log(`[updateNode] ERROR - new notes identical to existing (${existingNotes.length} chars). Model should NOT call updateNode again.`);
return {
success: false,
error: 'Notes already up to date - do not call updateNode again. Move to next step.',
data: null
};
}
// Detect if adding a section that already exists (e.g., ## Integration Analysis)
const newSectionMatch = newNotes.match(/^##\s+(.+)$/m);
if (newSectionMatch && existingNotes) {
@@ -57,12 +75,12 @@ export const updateNodeTool = tool({
};
}
}
// Detect if model included existing content + new content
// Detect if model included existing notes + new notes
if (existingNotes && newNotes.startsWith(existingNotes)) {
// Extract only the new part
const actualNewNotes = newNotes.substring(existingNotes.length).trim();
console.log(`[updateNode] Model included existing content - extracting new part only (${actualNewNotes.length} chars)`);
console.log(`[updateNode] Model included existing notes - extracting new part only (${actualNewNotes.length} chars)`);
const separator = existingNotes.endsWith('\n\n') ? '' : '\n\n';
updates.notes = `${existingNotes}${separator}${actualNewNotes}`;
} else if (existingNotes) {
@@ -71,15 +89,17 @@ export const updateNodeTool = tool({
updates.notes = `${existingNotes}${separator}${newNotes}`;
console.log(`[updateNode] Appended notes: ${existingNotes.length} + ${newNotes.length} = ${updates.notes.length} chars`);
} else {
console.log(`[updateNode] No existing content, using new content as-is (${newNotes.length} chars)`);
console.log(`[updateNode] No existing notes, using new notes as-is (${newNotes.length} chars)`);
}
}
}
// No dimension validation - user has full control over dimensions
if (Array.isArray(updates.dimensions)) {
updates.dimensions = normalizeDimensions(updates.dimensions, 5);
}
// Call the nodes API endpoint
const response = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000'}/api/nodes/${id}`, {
const response = await fetch(`${getInternalApiBaseUrl()}/api/nodes/${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(updates)