feat: AI context, Webhooks, Kanban drag-drop, and UI fixes
This commit is contained in:
@@ -58,7 +58,11 @@ export default defineEventHandler(async (event) => {
|
||||
throw createError({ statusCode: 400, message: 'Invalid action' })
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('Gitea sync error:', error)
|
||||
throw createError({ statusCode: 500, message: error?.message || 'Gitea operation failed' })
|
||||
const details = error.data || error.message || error
|
||||
console.error('Gitea sync error details:', details)
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
message: typeof details === 'string' ? details : JSON.stringify(details)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
@@ -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'
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,46 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { prisma } from '~/server/utils/prisma'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const user = requireAuth(event)
|
||||
const body = await readBody(event)
|
||||
const { taskId, canvasId, newState } = body
|
||||
|
||||
if (!taskId || !canvasId || !newState) {
|
||||
throw createError({ statusCode: 400, message: 'taskId, canvasId and newState are required' })
|
||||
}
|
||||
|
||||
// 1. Найти холст
|
||||
const canvas = await prisma.canvas.findUnique({
|
||||
where: { id: canvasId }
|
||||
})
|
||||
|
||||
if (!canvas) {
|
||||
throw createError({ statusCode: 404, message: 'Canvas not found' })
|
||||
}
|
||||
|
||||
// 2. Найти и обновить узел в JSON
|
||||
const nodes = (canvas.nodes as any[]) || []
|
||||
const nodeIdx = nodes.findIndex(n => n.id === taskId)
|
||||
|
||||
if (nodeIdx === -1) {
|
||||
throw createError({ statusCode: 404, message: 'Task node not found in canvas' })
|
||||
}
|
||||
|
||||
// Обновляем состояние
|
||||
nodes[nodeIdx].state = newState
|
||||
|
||||
// Также обновляем дату изменения, если нужно
|
||||
nodes[nodeIdx].updatedAt = new Date().toISOString()
|
||||
|
||||
// 3. Сохранить изменения в БД
|
||||
await prisma.canvas.update({
|
||||
where: { id: canvasId },
|
||||
data: { nodes }
|
||||
})
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `Task ${taskId} moved to ${newState}`
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,31 @@
|
||||
export const generateAIResponse = async (prompt: string, systemPrompt = 'You are a helpful assistant.') => {
|
||||
const config = useRuntimeConfig()
|
||||
const apiKey = config.deepseekApiKey || process.env.DEEPSEEK_API_KEY
|
||||
|
||||
if (!apiKey) {
|
||||
throw new Error('DEEPSEEK_API_KEY is not configured')
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await $fetch('https://api.deepseek.com/v1/chat/completions', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${apiKey}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: {
|
||||
model: 'deepseek-chat',
|
||||
messages: [
|
||||
{ role: 'system', content: systemPrompt },
|
||||
{ role: 'user', content: prompt }
|
||||
],
|
||||
stream: false
|
||||
}
|
||||
}) as any
|
||||
|
||||
return response.choices[0].message.content
|
||||
} catch (error: any) {
|
||||
console.error('DeepSeek AI error:', error.data || error.message)
|
||||
throw new Error(`AI generation failed: ${error.message}`)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user