parse-config.js
4.93 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
/**
* 文档解析服务配置
*
* @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('')
}