forked from ThinkInAIXYZ/deepchat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
290 lines (265 loc) · 10.5 KB
/
index.ts
File metadata and controls
290 lines (265 loc) · 10.5 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { app } from 'electron'
import { presenter } from '@/presenter'
import { IDeeplinkPresenter, MCPServerConfig } from '@shared/presenter'
import path from 'path'
import { DEEPLINK_EVENTS, WINDOW_EVENTS } from '@/events'
import { eventBus } from '@/eventbus'
interface MCPInstallConfig {
mcpServers: Record<
string,
{
command?: string
args?: string[]
env?: Record<string, string> | string
descriptions?: string
icons?: string
autoApprove?: string[]
disable?: boolean
url?: string
type?: 'sse' | 'stdio' | 'http'
}
>
}
/**
* DeepLink 处理器类
* 负责处理 deepchat:// 协议的链接
* deepchat://start 唤起应用,进入到默认的新会话界面
* deepchat://start?msg=你好 唤起应用,进入新会话界面,并且带上默认消息
* deepchat://start?msg=你好&model=deepseek-chat 唤起应用,进入新会话界面,并且带上默认消息,model先进行完全匹配,选中第一个命中的。没有命中的就进行模糊匹配,只要包含这个字段的第一个返回,如果都没有就忽略用默认
* deepchat://mcp/install?json=base64JSONData 通过json数据直接安装mcp
*/
export class DeeplinkPresenter implements IDeeplinkPresenter {
private startupUrl: string | null = null
init(): void {
// 注册协议处理器
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient('deepchat', process.execPath, [
path.resolve(process.argv[1])
])
}
} else {
app.setAsDefaultProtocolClient('deepchat')
}
// 处理 macOS 上协议被调用的情况
app.on('open-url', (event, url) => {
event.preventDefault()
if (!app.isReady()) {
console.log('应用程序尚未就绪,保存 URL:', url)
this.startupUrl = url
} else {
console.log('应用程序已就绪,直接处理 URL:', url)
this.handleDeepLink(url)
}
})
// 监听窗口准备好的事件
eventBus.on(WINDOW_EVENTS.READY_TO_SHOW, () => {
console.log('窗口准备就绪,检查是否有待处理的 URL')
if (this.startupUrl) {
console.log('处理启动时保存的 URL:', this.startupUrl)
this.handleDeepLink(this.startupUrl)
this.startupUrl = null
}
})
// 处理 Windows 上协议被调用的情况
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.quit()
} else {
app.on('second-instance', (_event, commandLine) => {
// 用户尝试运行第二个实例,我们应该聚焦到我们的窗口
if (presenter.windowPresenter.mainWindow) {
if (presenter.windowPresenter.mainWindow.isMinimized()) {
presenter.windowPresenter.mainWindow.restore()
}
presenter.windowPresenter.mainWindow.show()
presenter.windowPresenter.mainWindow.focus()
}
if (process.platform === 'win32') {
// 在 Windows 上,命令行参数包含协议 URL
const deepLinkUrl = commandLine.find((arg) => arg.startsWith('deepchat://'))
if (deepLinkUrl) {
if (!app.isReady()) {
console.log('Windows: 应用程序尚未就绪,保存 URL:', deepLinkUrl)
this.startupUrl = deepLinkUrl
} else {
console.log('Windows: 应用程序已就绪,直接处理 URL:', deepLinkUrl)
this.handleDeepLink(deepLinkUrl)
}
}
}
})
}
}
async handleDeepLink(url: string): Promise<void> {
console.log('收到 DeepLink:', url)
try {
const urlObj = new URL(url)
if (urlObj.protocol !== 'deepchat:') {
console.error('不支持的协议:', urlObj.protocol)
return
}
// 从 hostname 获取命令
const command = urlObj.hostname
// 处理不同的命令
if (command === 'start') {
await this.handleStart(urlObj.searchParams)
} else if (command === 'mcp') {
// 处理 mcp/install 命令
const subCommand = urlObj.pathname.slice(1) // 移除开头的斜杠
if (subCommand === 'install') {
await this.handleMcpInstall(urlObj.searchParams)
} else {
console.warn('未知的 MCP 子命令:', subCommand)
}
} else {
console.warn('未知的 DeepLink 命令:', command)
}
} catch (error) {
console.error('处理 DeepLink 时出错:', error)
}
}
async handleStart(params: URLSearchParams): Promise<void> {
console.log('处理 start 命令,参数:', Object.fromEntries(params.entries()))
let msg = params.get('msg')
if (!msg) {
return
}
msg = decodeURIComponent(msg)
// 如果有模型参数,尝试设置
let modelId = params.get('model')
if (modelId && modelId.trim() !== '') {
modelId = decodeURIComponent(modelId)
}
let systemPrompt = params.get('system')
if (systemPrompt && systemPrompt.trim() !== '') {
systemPrompt = decodeURIComponent(systemPrompt)
} else {
systemPrompt = ''
}
// 如果用户增加了yolo=1或者yolo=true,则自动发送消息
const yolo = params.get('yolo')
const autoSend = yolo && yolo.trim() !== ''
console.log('msg:', msg)
console.log('modelId:', modelId)
console.log('systemPrompt:', systemPrompt)
console.log('autoSend:', autoSend)
eventBus.emit(DEEPLINK_EVENTS.START, { msg, modelId, systemPrompt, autoSend })
}
async handleMcpInstall(params: URLSearchParams): Promise<void> {
console.log('处理 mcp/install 命令,参数:', Object.fromEntries(params.entries()))
// 获取 JSON 数据
const jsonBase64 = params.get('code')
if (!jsonBase64) {
console.error("缺少 'code' 参数")
return
}
try {
// 解码 Base64 并解析 JSON
const jsonString = Buffer.from(jsonBase64, 'base64').toString('utf-8')
const mcpConfig = JSON.parse(jsonString) as MCPInstallConfig
// 检查 MCP 配置是否有效
if (!mcpConfig || !mcpConfig.mcpServers) {
console.error('无效的 MCP 配置:缺少 mcpServers 字段')
return
}
// 遍历并安装所有 MCP 服务器
for (const [serverName, serverConfig] of Object.entries(mcpConfig.mcpServers)) {
let determinedType: 'sse' | 'stdio' | null = null
const determinedCommand: string | undefined = serverConfig.command
const determinedUrl: string | undefined = serverConfig.url
// 1. Check explicit type
if (serverConfig.type) {
if (serverConfig.type === 'stdio' || serverConfig.type === 'sse') {
determinedType = serverConfig.type
// Validate required fields based on explicit type
if (determinedType === 'stdio' && !determinedCommand) {
console.error(`服务器 ${serverName} 类型为 'stdio' 但缺少必需的 'command' 字段`)
continue
}
if (determinedType === 'sse' && !determinedUrl) {
console.error(`服务器 ${serverName} 类型为 'sse' 但缺少必需的 'url' 字段`)
continue
}
} else {
console.error(
`服务器 ${serverName} 提供了无效的 'type' 值: ${serverConfig.type},应为 'stdio' 或 'sse'`
)
continue
}
} else {
// 2. Infer type if not provided
const hasCommand = !!determinedCommand && determinedCommand.trim() !== ''
const hasUrl = !!determinedUrl && determinedUrl.trim() !== ''
if (hasCommand && hasUrl) {
console.error(
`服务器 ${serverName} 同时提供了 'command' 和 'url' 字段,但未指定 'type'。请明确指定 'type' 为 'stdio' 或 'sse'。`
)
continue
} else if (hasCommand) {
determinedType = 'stdio'
} else if (hasUrl) {
determinedType = 'sse'
} else {
console.error(
`服务器 ${serverName} 必须提供 'command' (用于 stdio) 或 'url' (用于 sse) 字段之一`
)
continue
}
}
// Safeguard check (should not be reached if logic is correct)
if (!determinedType) {
console.error(`无法确定服务器 ${serverName} 的类型 ('stdio' 或 'sse')`)
continue
}
// Set default values based on determined type
const defaultConfig: Partial<MCPServerConfig> = {
env: {},
descriptions: `${serverName} MCP 服务`,
icons: determinedType === 'stdio' ? '🔌' : '🌐', // Different default icons
autoApprove: ['all'],
disable: false,
args: [],
baseUrl: '',
command: '',
type: determinedType
}
// Merge configuration
const finalConfig: MCPServerConfig = {
env: {
...(typeof defaultConfig.env === 'string'
? JSON.parse(defaultConfig.env)
: defaultConfig.env),
...(typeof serverConfig.env === 'string'
? JSON.parse(serverConfig.env)
: serverConfig.env)
},
// env: { ...defaultConfig.env, ...serverConfig.env },
descriptions: serverConfig.descriptions || defaultConfig.descriptions!,
icons: serverConfig.icons || defaultConfig.icons!,
autoApprove: serverConfig.autoApprove || defaultConfig.autoApprove!,
disable: serverConfig.disable ?? defaultConfig.disable!,
args: serverConfig.args || defaultConfig.args!,
type: determinedType, // Use the determined type
// Set command or baseUrl based on type, prioritizing provided values
command: determinedType === 'stdio' ? determinedCommand! : defaultConfig.command!,
baseUrl: determinedType === 'sse' ? determinedUrl! : defaultConfig.baseUrl!
}
// 安装 MCP 服务器
console.log(`准备安装 MCP 服务器: ${serverName} (类型: ${determinedType})`, finalConfig)
const resultServerConfig = {
mcpServers: {
[serverName]: finalConfig
}
}
// 如果配置中指定了该服务器为默认服务器,则添加到默认服务器列表
eventBus.emit(DEEPLINK_EVENTS.MCP_INSTALL, {
mcpConfig: JSON.stringify(resultServerConfig)
})
}
console.log('所有 MCP 服务器处理完成')
} catch (error) {
console.error('解析或处理 MCP 配置时出错:', error)
}
}
}