Files
ra-h-os/app/api/edges/route.ts
T
“BeeRad”andClaude Opus 4.5 2cbc950fd4 sync: Idea Genealogy + Quick Capture Overhaul
From private repo:

Idea Genealogy:
- Required edge explanations across UI/tools/API/MCP
- Inferred edge schema (category/type/confidence)
- Added part_of relationship type
- Connection UI chips (Made by / Part of / Came from / Related)

Quick Capture Overhaul:
- Jina.ai fallback for JS-rendered sites (Twitter, SPAs)
- Simplified UI: removed mode buttons, auto-detect input type
- Renamed to "Add Stuff", moved to top-right
- Auto-edge creation from description entities
- Increased content summary length (500→1500 chars)

Description Generation Fix:
- Simplified prompt to "what is this"
- Added link/dimensions context
- Better creator attribution

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-14 17:08:26 +11:00

114 lines
3.4 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { edgeService } from '@/services/database';
export const runtime = 'nodejs';
export async function GET() {
try {
const edges = await edgeService.getEdges();
return NextResponse.json({
success: true,
data: edges,
count: edges.length
});
} catch (error) {
console.error('Error fetching edges:', error);
return NextResponse.json({
success: false,
error: error instanceof Error ? error.message : 'Failed to fetch edges'
}, { status: 500 });
}
}
export async function POST(request: NextRequest) {
try {
const body = await request.json();
// Validate required fields
if (!body.from_node_id || !body.to_node_id || typeof body.explanation !== 'string') {
return NextResponse.json({
success: false,
error: 'Missing required fields: from_node_id, to_node_id, and explanation are required'
}, { status: 400 });
}
// Validate node IDs are numbers
if (isNaN(parseInt(body.from_node_id)) || isNaN(parseInt(body.to_node_id))) {
return NextResponse.json({
success: false,
error: 'Invalid node IDs: must be valid numbers'
}, { status: 400 });
}
// Set default source if not provided
if (!body.source) {
body.source = 'user';
}
// Validate source value
if (!['user', 'ai_similarity', 'helper_name'].includes(body.source)) {
return NextResponse.json({
success: false,
error: 'Invalid source: must be user, ai_similarity, or helper_name'
}, { status: 400 });
}
const fromId = parseInt(body.from_node_id);
const toId = parseInt(body.to_node_id);
const explanation = String(body.explanation || '').trim();
if (!explanation) {
return NextResponse.json({
success: false,
error: 'explanation is required and cannot be empty'
}, { status: 400 });
}
const skipInference = Boolean(body.skip_inference);
const createdVia = (() => {
const raw = typeof body.created_via === 'string' ? body.created_via : '';
if (['ui', 'agent', 'mcp', 'workflow', 'quicklink'].includes(raw)) return raw as any;
return 'ui' as const;
})();
// Idempotency: prevent duplicate edges between same pair
try {
const exists = await edgeService.edgeExists(fromId, toId);
if (exists) {
return NextResponse.json({
success: true,
data: { from_node_id: fromId, to_node_id: toId },
message: `Edge already exists between nodes ${fromId} and ${toId}`
}, { status: 200 });
}
} catch (e) {
// Non-fatal: continue with creation if existence check fails
console.warn('edgeExists check failed; proceeding to create:', e);
}
const edge = await edgeService.createEdge({
from_node_id: fromId,
to_node_id: toId,
explanation,
created_via: createdVia,
source: body.source,
skip_inference: skipInference
});
return NextResponse.json({
success: true,
data: edge,
message: `Edge created successfully between nodes ${edge.from_node_id} and ${edge.to_node_id}`
}, { status: 201 });
} catch (error) {
console.error('Error creating edge:', error);
return NextResponse.json({
success: false,
error: error instanceof Error ? error.message : 'Failed to create edge'
}, { status: 500 });
}
}