import { NextRequest, NextResponse } from 'next/server' // 渠道配置接口 interface ChannelConfig { id: string type: 'feishu' | 'wecom' | 'wechat' | 'api' name: string status: 'active' | 'pending' | 'error' config: Record lastChecked?: string error?: string } // 网关地址(卡若AI部署的网关) const GATEWAY_URL = process.env.GATEWAY_URL || 'http://localhost:8000' // GET: 获取渠道状态 export async function GET(request: NextRequest) { const { searchParams } = new URL(request.url) const action = searchParams.get('action') const type = searchParams.get('type') try { // 测试飞书连接 if (action === 'test' && type === 'feishu') { try { const res = await fetch(`${GATEWAY_URL}/feishu/test`, { method: 'GET', headers: { 'Content-Type': 'application/json' } }) const data = await res.json() return NextResponse.json({ success: data.status === 'success', message: data.message, appId: data.app_id }) } catch (e) { return NextResponse.json({ success: false, message: '网关连接失败,请确认卡若AI网关已启动', error: String(e) }) } } // 获取飞书群列表 if (action === 'chats' && type === 'feishu') { try { const res = await fetch(`${GATEWAY_URL}/feishu/chats?refresh=true`) const data = await res.json() return NextResponse.json({ success: data.success, chats: data.chats || [], count: data.count || 0 }) } catch (e) { return NextResponse.json({ success: false, message: '获取群列表失败', chats: [] }) } } // 获取所有渠道状态 const channels: ChannelConfig[] = [ { id: 'feishu-1', type: 'feishu', name: '飞书机器人', status: 'pending', config: { webhookUrl: `${GATEWAY_URL}/feishu/webhook`, testUrl: `${GATEWAY_URL}/feishu/test` } }, { id: 'wecom-1', type: 'wecom', name: '企业微信应用', status: 'pending', config: { webhookUrl: `${GATEWAY_URL}/wecom/callback` } }, { id: 'api-1', type: 'api', name: 'REST API', status: 'active', config: { endpoint: '/api/ai-chat', method: 'POST' } } ] return NextResponse.json({ success: true, channels, gateway: GATEWAY_URL }) } catch (error) { return NextResponse.json({ success: false, error: String(error) }, { status: 500 }) } } // POST: 发送消息到渠道 export async function POST(request: NextRequest) { try { const body = await request.json() const { action, type, ...params } = body // 发送消息到飞书群 if (action === 'send' && type === 'feishu') { const { chatName, content } = params if (!chatName || !content) { return NextResponse.json({ success: false, error: '缺少参数: chatName, content' }, { status: 400 }) } try { const res = await fetch(`${GATEWAY_URL}/feishu/send_message`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ chat_name: chatName, content: content }) }) const data = await res.json() return NextResponse.json(data) } catch (e) { return NextResponse.json({ success: false, message: '发送失败', error: String(e) }) } } // 发送会议纪要 if (action === 'send_minutes' && type === 'feishu') { const { minutesUrl, chatName, useLlm = true } = params try { const res = await fetch(`${GATEWAY_URL}/feishu/send_minutes`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ minutes_url: minutesUrl, chat_name: chatName, use_llm: useLlm }) }) const data = await res.json() return NextResponse.json(data) } catch (e) { return NextResponse.json({ success: false, message: '发送会议纪要失败', error: String(e) }) } } return NextResponse.json({ success: false, error: '未知操作' }, { status: 400 }) } catch (error) { return NextResponse.json({ success: false, error: String(error) }, { status: 500 }) } }