You need to sign in or sign up before continuing.
parse-config.js 4.93 KB
/**
 * 文档解析服务配置
 *
 * @description 配置 markitdown 和 AI 服务的访问信息
 * @module scripts/parse-config
 * @author Claude Code
 * @created 2026-02-14
 */

/**
 * markitdown 服务配置
 *
 * @description markitdown 是一个将 PDF/DOCX/PPTX/XLSX 等多种格式转换为 Markdown 的工具
 *
 * 安装方式:
 * 1. Python: pip install markitdown
 * 2. Docker: docker pull ghcr.io/onurtemiz/markitdown
 * 3. HTTP API: 部署 markitdown 服务
 *
 * 使用方式:
 * - CLI: markitdown input.docx output.md
 * - Docker: docker run --rm -v $(pwd):/app markitdown input.docx output.md
 * - HTTP: POST /convert with file upload
 */
export const MARKITDOWN_CONFIG = {
  // markitdown 服务类型
  // - 'cli': 使用命令行工具(需要本地安装 Python)
  // - 'docker': 使用 Docker 容器
  // - 'http': 使用 HTTP API
  // - 'disabled': 禁用,使用本地库
  type: 'cli',

  // CLI 配置
  cli: {
    command: 'markitdown', // 命令行工具路径
    timeout: 30000 // 超时时间(毫秒)
  },

  // Docker 配置
  docker: {
    image: 'ghcr.io/onurtemiz/markitdown',
    timeout: 30000
  },

  // HTTP API 配置
  http: {
    url: process.env.MARKITDOWN_URL || 'http://localhost:8000/convert',
    timeout: 30000
  }
}

/**
 * AI 解析服务配置
 *
 * @description 配置用于智能解析文档内容的 AI 服务
 *
 * 支持的 AI 服务:
 * - 'openai': OpenAI GPT-4/GPT-3.5
 * - 'anthropic': Anthropic Claude
 * - 'openrouter': OpenRouter(聚合服务)
 * - 'disabled': 禁用 AI 解析
 */
export const AI_SERVICE_CONFIG = {
  // AI 服务类型
  type: process.env.AI_SERVICE_TYPE || 'disabled',

  // OpenAI 配置
  openai: {
    apiKey: process.env.OPENAI_API_KEY || '',
    baseURL: process.env.OPENAI_BASE_URL || 'https://api.openai.com/v1',
    model: process.env.OPENAI_MODEL || 'gpt-4-turbo',
    timeout: 60000
  },

  // Anthropic 配置
  anthropic: {
    apiKey: process.env.ANTHROPIC_API_KEY || '',
    baseURL: process.env.ANTHROPIC_BASE_URL || 'https://api.anthropic.com/v1',
    model: process.env.ANTHROPIC_MODEL || 'claude-3-sonnet-20240229',
    timeout: 60000
  },

  // OpenRouter 配置
  openrouter: {
    apiKey: process.env.OPENROUTER_API_KEY || '',
    baseURL: process.env.OPENROUTER_BASE_URL || 'https://openrouter.ai/api/v1',
    model: process.env.OPENROUTER_MODEL || 'anthropic/claude-3-sonnet',
    timeout: 60000
  }
}

/**
 * 检查 markitdown 服务是否可用
 *
 * @returns {Promise<boolean>} 是否可用
 */
export async function checkMarkitdownAvailable() {
  const { type } = MARKITDOWN_CONFIG

  if (type === 'disabled') {
    return false
  }

  if (type === 'cli') {
    // 检查命令是否存在
    const { exec } = require('child_process')
    return new Promise((resolve) => {
      exec(`${MARKITDOWN_CONFIG.cli.command} --version`, (error) => {
        resolve(!error)
      })
    })
  }

  if (type === 'http') {
    // 检查 HTTP 服务是否可访问
    try {
      const response = await fetch(MARKITDOWN_CONFIG.http.url, {
        method: 'HEAD',
        timeout: 5000
      })
      return response.ok
    } catch {
      return false
    }
  }

  if (type === 'docker') {
    // 检查 Docker 是否可用
    const { exec } = require('child_process')
    return new Promise((resolve) => {
      exec('docker --version', (error) => {
        resolve(!error)
      })
    })
  }

  return false
}

/**
 * 检查 AI 服务是否配置
 *
 * @returns {boolean} 是否已配置 API Key
 */
export function checkAIServiceConfigured() {
  const { type } = AI_SERVICE_CONFIG

  if (type === 'disabled') {
    return false
  }

  if (type === 'openai') {
    return !!AI_SERVICE_CONFIG.openai.apiKey
  }

  if (type === 'anthropic') {
    return !!AI_SERVICE_CONFIG.anthropic.apiKey
  }

  if (type === 'openrouter') {
    return !!AI_SERVICE_CONFIG.openrouter.apiKey
  }

  return false
}

/**
 * 打印配置状态
 */
export function printConfigStatus() {
  console.log('\n🔧 文档解析服务配置状态:')
  console.log('─'.repeat(50))

  // markitdown 状态
  const markitdownType = MARKITDOWN_CONFIG.type
  console.log(`📄 markitdown: ${markitdownType === 'disabled' ? '❌ 未配置' : '✅ ' + markitdownType}`)

  // AI 服务状态
  const aiType = AI_SERVICE_CONFIG.type
  console.log(`🤖 AI 服务: ${aiType === 'disabled' ? '❌ 未配置' : '✅ ' + aiType}`)

  if (aiType !== 'disabled') {
    const configured = checkAIServiceConfigured()
    console.log(`   API Key: ${configured ? '✅ 已配置' : '❌ 未配置'}`)
  }

  console.log('─'.repeat(50))
  console.log('')
  console.log('💡 配置提示:')
  console.log('   1. 使用 markitdown: 安装 Python 并运行 "pip install markitdown"')
  console.log('   2. 配置 AI 服务: 设置环境变量(.env 文件)')
  console.log('      - OPENAI_API_KEY: OpenAI API Key')
  console.log('      - ANTHROPIC_API_KEY: Anthropic API Key')
  console.log('      - AI_SERVICE_TYPE: openai | anthropic | openrouter')
  console.log('')
}