forked from yee94/opencode-qoder-provider
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
294 lines (259 loc) · 10.8 KB
/
index.ts
File metadata and controls
294 lines (259 loc) · 10.8 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
291
292
293
294
import type { Plugin, Hooks } from '@opencode-ai/plugin'
import { QODER_MODELS } from './src/models.js'
import { setMcpBridgeServers } from './src/mcp-bridge.js'
import { setAvailableAgentTypes } from './src/agent-bridge.js'
import { existsSync, readFileSync, readdirSync } from 'node:fs'
import { homedir } from 'node:os'
import { join } from 'node:path'
export const QoderProviderPlugin: Plugin = async () => {
// 发布包使用 dist/provider.js;源码直跑时回退到同目录 provider.ts。
const providerFileUrl = new URL(
existsSync(new URL('./provider.js', import.meta.url)) ? './provider.js' : './provider.ts',
import.meta.url,
).href
return {
async config(config) {
config.provider = config.provider ?? {}
const existing = config.provider.qoder ?? {}
// 将 QODER_MODELS 转换为 opencode provider models 格式(去掉 cost 字段,opencode 不识别)
const builtinModels: Record<string, object> = {}
for (const [key, m] of Object.entries(QODER_MODELS)) {
builtinModels[key] = {
name: m.name,
attachment: m.attachment,
reasoning: m.reasoning,
temperature: m.temperature,
tool_call: m.tool_call,
limit: m.limit,
// opencode 通过 modalities.input 判断是否支持图片输入(attachment 字段不参与此判断)
modalities: {
input: m.attachment ? ['text', 'image'] : ['text'],
output: ['text'],
},
}
}
// 用户在 opencode.json 里覆写的 models 优先级更高
const mergedModels = { ...builtinModels, ...(existing.models ?? {}) }
// 将 opencode config.mcp 服务器配置桥接到 Qoder SDK query() mcpServers
// normalizeMcpServerConfig 的格式转换已内联在 mcp-bridge 中,这里直接转换
const bridgedMcp = convertOpencodeMcp(config.mcp)
setMcpBridgeServers(bridgedMcp)
// 检测可用的 agent 类型:oh-my-opencode-slim 等插件会替换标准 agent 类型
// 此处从插件配置中提取实际可用的类型,供 normalizeToolInputObject 做映射
const detectedAgentTypes = detectAgentTypes(config)
if (detectedAgentTypes.length > 0) {
setAvailableAgentTypes(detectedAgentTypes)
}
const mergedProviderOptions = {
...(existing.options ?? {}),
...(Object.keys(bridgedMcp).length > 0
? {
mcpServers: {
...(((existing.options as Record<string, unknown> | undefined)?.mcpServers as Record<string, unknown> | undefined) ?? {}),
...bridgedMcp,
},
}
: {}),
}
config.provider.qoder = {
...existing,
// opencode 用 npm 字段来加载 SDK:file:// 开头则直接 import,否则 BunProc.install()
npm: existing.npm ?? providerFileUrl,
name: existing.name ?? 'Qoder',
options: mergedProviderOptions,
models: mergedModels,
}
},
auth: {
provider: 'qoder',
// loader:检查本地登录态,已登录则静默通过,无需用户输入任何东西
async loader(getAuth) {
// 优先检查 QoderWork 登录(~/.qoderwork),回退到 Qoder CLI(~/.qoder)
const authFiles = [
join(homedir(), '.qoderwork', '.auth', 'user'),
join(homedir(), '.qoder', '.auth', 'user'),
]
if (authFiles.some(existsSync)) {
// 已登录,返回空 options 即可,SDK 会自动读取认证文件
return {}
}
// 未登录,返回空对象让 opencode 触发 auth UI
return {}
},
methods: [
{
type: 'api',
// label 作为 opencode auth UI 里的提示文案
label: 'Open QoderWork and log in, or run `qoder login` in your terminal',
// 没有需要用户填写的字段
prompts: [],
async authorize() {
// 首先检查 qodercli 是否安装,未安装直接报错提示
const hasQoderCLI = checkQoderCLI()
if (!hasQoderCLI) {
return {
type: 'failed',
message: 'Qoder CLI not found. Please install Qoder CLI first. Reference: https://github.com/opencode-ai/opencode-qoder-auth#prerequisites',
}
}
const authFiles = [
join(homedir(), '.qoderwork', '.auth', 'user'),
join(homedir(), '.qoder', '.auth', 'user'),
]
if (authFiles.some(existsSync)) {
// 已经完成登录,标记为成功
return { type: 'success', key: 'qoder-cli-auth' }
}
// 未登录,提示用户登录
return { type: 'failed' }
},
},
],
},
} satisfies Hooks
}
export default QoderProviderPlugin
// ── opencode config.mcp → Qoder SDK mcpServers 格式转换 ──────────────────────
type QoderMcpServerConfig =
| { type?: 'stdio'; command: string; args?: string[]; env?: Record<string, string> }
| { type: 'http' | 'sse'; url: string; headers?: Record<string, string> }
function convertOpencodeMcp(
mcp: Record<string, unknown> | undefined,
): Record<string, QoderMcpServerConfig> {
if (!mcp || typeof mcp !== 'object') return {}
const result: Record<string, QoderMcpServerConfig> = {}
for (const [name, raw] of Object.entries(mcp)) {
const cfg = convertOneMcpEntry(raw)
if (cfg) result[name] = cfg
}
return result
}
function convertOneMcpEntry(raw: unknown): QoderMcpServerConfig | null {
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return null
const cfg = raw as Record<string, unknown>
if (cfg.enabled === false) return null
// stdio / local:command 是数组或字符串
if (Array.isArray(cfg.command) && cfg.command.every((v) => typeof v === 'string')) {
if (cfg.command.length === 0) return null
const [command, ...args] = cfg.command as string[]
const env = pickStringRecord(cfg.environment) ?? pickStringRecord(cfg.env)
return { type: 'stdio', command, ...(args.length > 0 ? { args } : {}), ...(env ? { env } : {}) }
}
if (typeof cfg.command === 'string') {
const args = pickStringArray(cfg.args)
const env = pickStringRecord(cfg.environment) ?? pickStringRecord(cfg.env)
return {
type: 'stdio',
command: cfg.command,
...(args && args.length > 0 ? { args } : {}),
...(env ? { env } : {}),
}
}
// remote / http
const url = typeof cfg.url === 'string' ? cfg.url : undefined
if (url) {
const headers = pickStringRecord(cfg.headers)
return {
type: cfg.type === 'sse' ? 'sse' : 'http',
url,
...(headers ? { headers } : {}),
}
}
return null
}
function pickStringArray(value: unknown): string[] | undefined {
return Array.isArray(value) && value.every((item) => typeof item === 'string')
? value
: undefined
}
function pickStringRecord(value: unknown): Record<string, string> | undefined {
if (!value || typeof value !== 'object' || Array.isArray(value)) return undefined
const entries = Object.entries(value as object).filter(
(e): e is [string, string] => typeof e[1] === 'string',
)
return entries.length > 0 ? Object.fromEntries(entries) : undefined
}
// ── agent type 检测 ──────────────────────────────────────────────────────────
/**
* 从 opencode config 和 oh-my-opencode-slim 配置中检测可用的 agent 类型。
*
* 检测优先级:
* 1. config.agent 中非 disabled 的自定义 agent
* 2. oh-my-opencode-slim 配置文件中活跃 preset 的 agent 类型
*/
function detectAgentTypes(config: Record<string, unknown>): string[] {
// 方案 1:从 config.agent 读取(oh-my-opencode-slim 可能在此注入)
const agentConfig = config.agent
if (agentConfig && typeof agentConfig === 'object' && !Array.isArray(agentConfig)) {
const types = Object.entries(agentConfig as Record<string, unknown>)
.filter(([, v]) => {
if (!v || typeof v !== 'object') return false
return (v as Record<string, unknown>).disable !== true
})
.map(([k]) => k)
if (types.length > 0) return types
}
// 方案 2:直接读取 oh-my-opencode-slim 配置文件
return detectOhMyOpencodeSlimTypes(config)
}
/**
* 从 oh-my-opencode-slim 配置文件中检测 agent 类型
*/
function detectOhMyOpencodeSlimTypes(config: Record<string, unknown>): string[] {
// 检查 plugin 列表中是否包含 oh-my-opencode-slim
const plugins = Array.isArray(config.plugin) ? config.plugin : []
const hasOMS = plugins.some((p) =>
typeof p === 'string' && (p.includes('oh-my-opencode-slim') || p.includes('oh-my-opencode')),
)
if (!hasOMS) return []
// 尝试读取配置文件
const configPaths = [
join(homedir(), '.config', 'opencode', 'oh-my-opencode-slim.jsonc'),
join(homedir(), '.config', 'opencode', 'oh-my-opencode-slim.json'),
join(homedir(), '.config', 'opencode', 'oh-my-opencode.jsonc'),
join(homedir(), '.config', 'opencode', 'oh-my-opencode.json'),
]
for (const cfgPath of configPaths) {
try {
if (!existsSync(cfgPath)) continue
const raw = readFileSync(cfgPath, 'utf8')
// 简单处理 JSONC:去掉行注释和块注释
const cleaned = raw.replace(/\/\/.*$/gm, '').replace(/\/\*[\s\S]*?\*\//g, '')
const parsed = JSON.parse(cleaned) as Record<string, unknown>
const preset = typeof parsed.preset === 'string' ? parsed.preset : undefined
const presets = parsed.presets as Record<string, unknown> | undefined
if (!preset || !presets) continue
const activePreset = presets[preset]
if (!activePreset || typeof activePreset !== 'object') continue
return Object.keys(activePreset as Record<string, unknown>)
} catch { /* ignore parse errors */ }
}
return []
}
function checkQoderCLI(): boolean {
// 1. PATH
const pathDirs = (process.env.PATH ?? '').split(process.platform === 'win32' ? ';' : ':')
for (const dir of pathDirs) {
const p = join(dir, process.platform === 'win32' ? 'qodercli.exe' : 'qodercli')
if (existsSync(p)) return true
}
// 2. Default paths
const home = homedir()
const paths = [
join(home, '.qoder', 'local', 'qodercli'),
join(home, '.qoder', 'local', 'qodercli.exe'),
]
if (paths.some(existsSync)) return true
// 3. Versioned bin dir: ~/.qoder/bin/qodercli/qodercli-<version>
const binDir = join(home, '.qoder', 'bin', 'qodercli')
if (existsSync(binDir)) {
try {
// Just check if there are any files starting with qodercli-
const files = readdirSync(binDir)
if (files.some((f: string) => f.startsWith('qodercli-'))) return true
} catch {
/* ignore */
}
}
return false
}