All files / src/server/routes info-collection.ts

0% Statements 0/113
0% Branches 0/1
0% Functions 0/1
0% Lines 0/113

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184                                                                                                                                                                                                                                                                                                                                                                               
import type express from 'express'
import { pluginLogger } from '@dalidev/core'
import { logger } from '../../utils/logger-adapter'
 
// 信息收集会话接口
interface InfoCollectionSession {
  sessionId: string
  data: {
    workspaceRoot: string
    context: string
    prompt?: string
    schema: Array<{
      id: string
      label: string
      type: string
      required?: boolean
      defaultValue?: any
      placeholder?: string
      description?: string
      options?: Array<{ value: string | number, label: string }>
      [key: string]: any
    }>
    title?: string
    description?: string
  }
  status: 'waiting' | 'completed' | 'cancelled'
  timestamp: number
  result?: {
    formData: Record<string, any>
    prompt: string
    fileMap: Record<string, string> // 文件占位符到实际路径的映射
    skipped?: boolean
  }
}
 
// 内存存储
 
export function registerInfoCollectionRoutes(app: express.Express) {
  const infoCollectionSessions = new Map<string, InfoCollectionSession>()
  // 创建或更新信息收集会话数据
  app.post('/api/info-collection/session/:sessionId', (req, res) => {
    try {
      const { sessionId } = req.params
      const { data, status, timestamp } = req.body
      logger.info(`创建/更新信息收集会话到内存`, { sessionId, data, status, timestamp })
      if (!sessionId) {
        return res.status(400).json({ success: false, error: 'sessionId is required' })
      }
 
      // 创建或更新会话到内存
      const session: InfoCollectionSession = {
        sessionId,
        data: data || {},
        status: status || 'waiting',
        timestamp: timestamp || Date.now(),
      }
 
      infoCollectionSessions.set(sessionId, session)
 
      logger.info(`创建/更新信息收集会话到内存`, { sessionId, infoCollectionSessions })
      res.json({ success: true, message: `Session ${sessionId} created/updated`, data: session })
    }
    catch (error) {
      pluginLogger.error('Server', '创建/更新信息收集会话失败', { error })
      res.status(500).json({ success: false, error: error instanceof Error ? error.message : String(error) })
    }
  })
 
  // 获取信息收集会话数据
  app.get('/api/info-collection/session', (req, res) => {
    try {
      const { sessionId } = req.query
      if (!sessionId) {
        return res.status(400).json({ success: false, error: 'sessionId is required' })
      }
 
      const session = infoCollectionSessions.get(sessionId as string)
      if (!session) {
        return res.status(400).json({ success: false, error: `Session ${sessionId} not found` })
      }
 
      logger.info(`获取信息收集会话数据`, { sessionId, session })
      res.json({ success: true, data: session })
    }
    catch (error) {
      pluginLogger.error('Server', '获取信息收集会话数据失败', { error })
      res.status(400).json({ success: false, error: error instanceof Error ? error.message : String(error) })
    }
  })
 
  // 提交信息收集数据
  app.post('/api/info-collection/submit', (req, res) => {
    try {
      const { sessionId, formData, prompt, skipped } = req.body
 
      if (!sessionId) {
        return res.status(400).json({ success: false, error: 'sessionId  are required' })
      }
 
      // 处理文件上传(如果有的话)
      const fileMap: Record<string, string> = {}
 
      // TODO: 处理上传的文件,将它们保存到合适的位置
      // 这里先简单处理,实际实现时需要处理 multipart/form-data
 
      const session = infoCollectionSessions.get(sessionId)
      if (!session) {
        return res.status(200).json({ success: false, error: `Session ${sessionId} not found` })
      }
 
      // 更新会话状态
      session.status = 'completed'
      session.result = {
        formData: typeof formData === 'string' ? JSON.parse(formData) : formData,
        prompt: prompt || '',
        fileMap,
        skipped: skipped === 'true' || skipped === true,
      }
      session.timestamp = Date.now()
 
      infoCollectionSessions.set(sessionId, session)
 
      pluginLogger.info('Server', `完成信息收集会话`, { sessionId, skipped: session.result.skipped })
      res.json({ success: true, message: 'Info collection completed', data: session })
    }
    catch (error) {
      pluginLogger.error('Server', '完成信息收集会话失败', { error })
      res.status(200).json({ success: false, error: error instanceof Error ? error.message : String(error) })
    }
  })
 
  // 取消信息收集会话
  app.post('/api/info-collection/cancel', (req, res) => {
    try {
      const { sessionId } = req.body
 
      if (!sessionId) {
        return res.status(400).json({ success: false, error: 'sessionId is required' })
      }
 
      const session = infoCollectionSessions.get(sessionId)
      if (!session) {
        return res.status(404).json({ success: false, error: `Session ${sessionId} not found` })
      }
 
      // 更新会话状态
      session.status = 'cancelled'
      session.timestamp = Date.now()
 
      infoCollectionSessions.set(sessionId, session)
 
      pluginLogger.info('Server', `取消信息收集会话`, { sessionId })
      res.json({ success: true, message: 'Info collection cancelled', data: session })
    }
    catch (error) {
      pluginLogger.error('Server', '取消信息收集会话失败', { error })
      res.status(200).json({ success: false, error: error instanceof Error ? error.message : String(error) })
    }
  })
 
  // 清理过期会话
  app.post('/api/info-collection/cleanup', (_req, res) => {
    try {
      const now = Date.now()
      const expireTime = 24 * 60 * 60 * 1000 // 24小时
 
      let cleanedCount = 0
      for (const [sessionId, session] of infoCollectionSessions.entries()) {
        if (now - session.timestamp > expireTime) {
          infoCollectionSessions.delete(sessionId)
          cleanedCount++
        }
      }
 
      pluginLogger.info('Server', `清理过期信息收集会话`, { cleanedCount })
      res.json({ success: true, message: `Cleaned ${cleanedCount} expired sessions` })
    }
    catch (error) {
      pluginLogger.error('Server', '清理过期信息收集会话失败', { error })
      res.status(500).json({ success: false, error: error instanceof Error ? error.message : String(error) })
    }
  })
}