feat: AI context, Webhooks, Kanban drag-drop, and UI fixes

This commit is contained in:
2026-04-29 17:43:18 +05:00
parent 89b3340253
commit bc2f99408d
29 changed files with 991 additions and 52 deletions
+96
View File
@@ -0,0 +1,96 @@
import { requireAuth } from '~/server/utils/auth'
import { prisma } from '~/server/utils/prisma'
import { generateAIResponse } from '~/server/utils/ai'
export default defineEventHandler(async (event) => {
const user = requireAuth(event)
const nodeId = getRouterParam(event, 'id')
const body = await readBody(event)
const { canvasId, context } = body
if (!nodeId || !canvasId) {
throw createError({ statusCode: 400, message: 'Node ID and Canvas ID are required' })
}
// 1. Найти холст и убедиться в правах доступа
const canvas = await prisma.canvas.findUnique({
where: { id: canvasId }
})
if (!canvas) {
throw createError({ statusCode: 404, message: 'Canvas not found' })
}
// Проверка прав (владелец или админ)
if (canvas.ownerId !== user.id && user.role !== 'admin') {
throw createError({ statusCode: 403, message: 'Access denied' })
}
// 2. Найти конкретный узел в JSON холста
const nodes = (canvas.nodes as any[]) || []
const nodeIdx = nodes.findIndex(n => n.id === nodeId)
if (nodeIdx === -1) {
throw createError({ statusCode: 404, message: 'Node not found in canvas' })
}
const node = nodes[nodeIdx]
try {
let result = ''
if (node.type === 'ai') {
// Формируем промпт с учетом контекста от соседей
let fullPrompt = node.prompt || node.text || 'Analyze the provided context.'
if (context && context.length > 0) {
const contextText = context.map((c: any) => `[Node ${c.id}]: ${c.text}`).join('\n\n')
fullPrompt = `CONTEXT INFORMATION:\n${contextText}\n\nUSER REQUEST/INSTRUCTION:\n${fullPrompt}`
}
console.log(`[AI Execute] Generating response with context for node ${nodeId}...`)
result = await generateAIResponse(fullPrompt)
// Сохраняем результат в текст узла
nodes[nodeIdx].text = result
} else if (node.type === 'webhook') {
const url = node.url
const method = node.method || 'GET'
if (!url) throw new Error('Webhook URL is not configured')
console.log(`[Webhook Execute] Calling ${method} ${url}...`)
const response = await $fetch<any>(url, {
method,
...(method === 'POST' ? { body: { context, timestamp: new Date() } } : {})
})
result = typeof response === 'string' ? response : JSON.stringify(response, null, 2)
nodes[nodeIdx].text = `### Response from ${url}\n\n\`\`\`json\n${result}\n\`\`\``
} else {
throw createError({ statusCode: 400, message: `Execution not supported for type: ${node.type}` })
}
// 3. Сохраняем обновленный холст
await prisma.canvas.update({
where: { id: canvasId },
data: { nodes }
})
return {
success: true,
result,
message: 'Node executed and canvas updated'
}
} catch (error: any) {
console.error('Node execution error:', error)
throw createError({
statusCode: 500,
message: error.message || 'Execution failed'
})
}
})