@hex4c59/pi-ext-qoder 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/NOTICE +9 -0
- package/README.md +151 -2
- package/THIRD_PARTY.md +13 -0
- package/extensions/qoder.ts +960 -0
- package/lib/account.ts +129 -0
- package/lib/catalog.ts +228 -0
- package/lib/credit-report.ts +112 -0
- package/lib/model-report.ts +107 -0
- package/lib/qoder-core.mjs +864 -0
- package/lib/relay-registry.ts +34 -0
- package/lib/report-overlay.ts +175 -0
- package/lib/request-body.ts +48 -0
- package/lib/sse.ts +63 -0
- package/lib/types.ts +100 -0
- package/lib/usage-ui.ts +354 -0
- package/lib/web-search.ts +171 -0
- package/package.json +51 -4
- package/scripts/prepare-signing.mjs +51 -0
- package/vendor/LICENSE.qoder +202 -0
- package/vendor/PROVENANCE.md +42 -0
- package/vendor/auth_glue.mjs +14 -0
- package/vendor/auth_wasm.wasm +0 -0
|
@@ -0,0 +1,960 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Qoder 的 Pi provider:接入原生 /login 和自定义流式传输。
|
|
3
|
+
*
|
|
4
|
+
* 设备授权使用 PKCE 与 /api/v1/deviceToken/poll,凭据和签名材料由
|
|
5
|
+
* Pi 的 auth.json 保存;对话由 Qoder 自带 WASM 签名并走 agent-chat SSE。
|
|
6
|
+
* 扩展自动同步模型目录、转换工具调用与思考流,并展示账号和逐次请求的积分用量。
|
|
7
|
+
*
|
|
8
|
+
* 本模块读取 Pi 保存的 Qoder 凭据,不记录 Token 或请求签名。
|
|
9
|
+
*/
|
|
10
|
+
import { createHash } from 'node:crypto'
|
|
11
|
+
import type { ExtensionAPI } from '@earendil-works/pi-coding-agent'
|
|
12
|
+
import { readStoredCredential } from '@earendil-works/pi-coding-agent'
|
|
13
|
+
import {
|
|
14
|
+
type AssistantMessage,
|
|
15
|
+
type AssistantMessageEventStream,
|
|
16
|
+
type Model,
|
|
17
|
+
type OAuthCredentials,
|
|
18
|
+
type OAuthLoginCallbacks,
|
|
19
|
+
type SimpleStreamOptions,
|
|
20
|
+
type Tool,
|
|
21
|
+
type ToolCall,
|
|
22
|
+
type ToolCallArgumentsStream as ToolCallArgumentsStreamType,
|
|
23
|
+
type TranscriptContext,
|
|
24
|
+
ToolCallArgumentsStream,
|
|
25
|
+
calculateCost,
|
|
26
|
+
collapseSystemMessages,
|
|
27
|
+
createAssistantMessageEventStream,
|
|
28
|
+
getCurrentSystemPrompt,
|
|
29
|
+
getCurrentTools,
|
|
30
|
+
} from '@earendil-works/pi-ai'
|
|
31
|
+
import { Type } from 'typebox'
|
|
32
|
+
import {
|
|
33
|
+
CONFIG,
|
|
34
|
+
buildCredentialFromDeviceToken,
|
|
35
|
+
classifyRejection,
|
|
36
|
+
delay,
|
|
37
|
+
deviceLoginUrl,
|
|
38
|
+
effortForLevel,
|
|
39
|
+
fetchRegionEndpoints,
|
|
40
|
+
mapUsage,
|
|
41
|
+
refreshCredential,
|
|
42
|
+
signChatRequest,
|
|
43
|
+
unwrapFrame,
|
|
44
|
+
waitForDeviceToken,
|
|
45
|
+
} from '../lib/qoder-core.mjs'
|
|
46
|
+
import { formatSearchResult, searchWeb } from '../lib/web-search.ts'
|
|
47
|
+
import { buildAgentRequest } from '../lib/request-body.ts'
|
|
48
|
+
import { readSseData } from '../lib/sse.ts'
|
|
49
|
+
import { API_ID, PROVIDER_ID, createCatalogRefresh, definitionForRequest, snapshotModels } from '../lib/catalog.ts'
|
|
50
|
+
import { installUsageUi } from '../lib/usage-ui.ts'
|
|
51
|
+
import type { QoderCredential, QoderUsage } from '../lib/types.ts'
|
|
52
|
+
|
|
53
|
+
/** 按 Token 保存有界缓存,支持并发使用不同内存凭据的 SDK 会话。 */
|
|
54
|
+
const remembered = new Map<string, QoderCredential>()
|
|
55
|
+
const MAX_REMEMBERED_CREDENTIALS = 16
|
|
56
|
+
|
|
57
|
+
function remember(credential: QoderCredential): QoderCredential {
|
|
58
|
+
remembered.delete(credential.access)
|
|
59
|
+
remembered.set(credential.access, { ...credential })
|
|
60
|
+
while (remembered.size > MAX_REMEMBERED_CREDENTIALS) {
|
|
61
|
+
const oldest = remembered.keys().next().value
|
|
62
|
+
if (oldest !== undefined) remembered.delete(oldest)
|
|
63
|
+
}
|
|
64
|
+
return credential
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 读取 Pi 为此 provider 保存的凭据,不记录其内容。 */
|
|
68
|
+
function storedCredential(): QoderCredential | undefined {
|
|
69
|
+
const raw = readStoredCredential(PROVIDER_ID)
|
|
70
|
+
if (raw === null || typeof raw !== 'object' || raw.type !== 'oauth') return undefined
|
|
71
|
+
return raw as QoderCredential
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 解析用于签名的凭据。accessHint 是 Pi 为本次请求解析出的访问凭据。
|
|
76
|
+
* 读取存储可感知其他 Pi 进程的刷新或重新登录;进程缓存用于回退。
|
|
77
|
+
*/
|
|
78
|
+
function resolveCredential(accessHint?: string): QoderCredential {
|
|
79
|
+
const stored = storedCredential()
|
|
80
|
+
const credential = accessHint === undefined
|
|
81
|
+
? stored
|
|
82
|
+
: stored?.access === accessHint ? stored : remembered.get(accessHint)
|
|
83
|
+
if (credential === undefined) {
|
|
84
|
+
throw new Error(accessHint === undefined
|
|
85
|
+
? 'Qoder is not signed in. Run /login and choose "Qoder".'
|
|
86
|
+
: 'Qoder has no signing credential matching the resolved token. Run /login again.')
|
|
87
|
+
}
|
|
88
|
+
if (typeof credential.access !== 'string' || credential.access === '') {
|
|
89
|
+
throw new Error('Qoder credential has no access token. Run /login again.')
|
|
90
|
+
}
|
|
91
|
+
if (typeof credential.machineId !== 'string' || credential.machineId === ''
|
|
92
|
+
|| typeof credential.encryptUserInfo !== 'string' || credential.encryptUserInfo === ''
|
|
93
|
+
|| typeof credential.signingKey !== 'string' || credential.signingKey === '') {
|
|
94
|
+
throw new Error('Qoder credential is missing its signing fields (older login). Run /login again.')
|
|
95
|
+
}
|
|
96
|
+
return remember(credential)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** 同一账号、网关和模型的限流冷却;重试次数仍由 Pi 控制。 */
|
|
100
|
+
type QueueCooldowns = Map<string, number>
|
|
101
|
+
const standaloneCooldowns: QueueCooldowns = new Map()
|
|
102
|
+
const DEFAULT_QUEUE_RETRY_MS = 30_000
|
|
103
|
+
const MAX_QUEUE_COOLDOWNS = 128
|
|
104
|
+
|
|
105
|
+
/** 支持 HTTP Retry-After 的秒数与日期格式。 */
|
|
106
|
+
function retryAfterMs(headers?: Headers): number | undefined {
|
|
107
|
+
const value = headers?.get('retry-after')?.trim()
|
|
108
|
+
if (!value) return undefined
|
|
109
|
+
const seconds = Number(value)
|
|
110
|
+
const ms = Number.isFinite(seconds) ? Math.ceil(seconds * 1000) : Date.parse(value) - Date.now()
|
|
111
|
+
return Number.isSafeInteger(ms) && ms > 0 ? ms : undefined
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** 保存冷却截止时间,清理过期项并限制内存占用。 */
|
|
115
|
+
function rememberCooldown(cooldowns: QueueCooldowns, key: string, ms: number) {
|
|
116
|
+
const now = Date.now()
|
|
117
|
+
for (const [candidate, until] of cooldowns) {
|
|
118
|
+
if (until <= now) cooldowns.delete(candidate)
|
|
119
|
+
}
|
|
120
|
+
const until = Math.max(cooldowns.get(key) ?? 0, now + ms)
|
|
121
|
+
cooldowns.delete(key)
|
|
122
|
+
cooldowns.set(key, until)
|
|
123
|
+
while (cooldowns.size > MAX_QUEUE_COOLDOWNS) {
|
|
124
|
+
const oldest = cooldowns.keys().next().value
|
|
125
|
+
if (oldest !== undefined) cooldowns.delete(oldest)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Pi 的退避可能早于服务端冷却;发送新请求前补足剩余等待,并允许 Esc 取消。 */
|
|
130
|
+
async function waitForCooldown(cooldowns: QueueCooldowns, key: string, signal: AbortSignal, maxDelay = 60_000) {
|
|
131
|
+
for (;;) {
|
|
132
|
+
signal.throwIfAborted()
|
|
133
|
+
const remaining = (cooldowns.get(key) ?? 0) - Date.now()
|
|
134
|
+
if (remaining <= 0) return
|
|
135
|
+
if (maxDelay > 0 && remaining > maxDelay) {
|
|
136
|
+
throw new Error(`Qoder retry delay is ${Math.ceil(remaining / 1000)}s (max: ${Math.ceil(maxDelay / 1000)}s). Wait or choose another model with /model.`)
|
|
137
|
+
}
|
|
138
|
+
await delay(Math.min(remaining, 2_147_483_647), signal)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** 将已分类的 Qoder 拒绝响应转为 Pi 可显示的错误,并保留服务端冷却。 */
|
|
143
|
+
function rejectionError(
|
|
144
|
+
rejection: { code: string; message: string; resourceLimit?: boolean; retryAfterMs?: number; queueCount?: number },
|
|
145
|
+
cooldowns: QueueCooldowns,
|
|
146
|
+
key: string,
|
|
147
|
+
headers?: Headers,
|
|
148
|
+
): Error {
|
|
149
|
+
if (rejection.code === 'rate_limit') {
|
|
150
|
+
const ms = Math.max(rejection.retryAfterMs ?? 0, retryAfterMs(headers) ?? 0) || DEFAULT_QUEUE_RETRY_MS
|
|
151
|
+
rememberCooldown(cooldowns, key, ms)
|
|
152
|
+
const queued = rejection.queueCount === undefined ? '' : `; ${rejection.queueCount} queued`
|
|
153
|
+
// 普通 429 保留原错误,供 Pi 识别不可重试的套餐/额度耗尽,避免误当资源排队。
|
|
154
|
+
const detail = rejection.resourceLimit ? '' : ` ${rejection.message}`
|
|
155
|
+
return new Error(`Qoder queue/rate limit (429): retry after ${Math.ceil(ms / 1000)}s${queued}.${detail} Wait or choose another model with /model.`)
|
|
156
|
+
}
|
|
157
|
+
if (rejection.code === 'daily_limit') {
|
|
158
|
+
return new Error(`${rejection.message} — this gateway node has hit its own daily counter, which is unrelated to your credits balance. The extension already tried every inference node it knows; retry later or pin another gateway with QODER_ENDPOINT.`)
|
|
159
|
+
}
|
|
160
|
+
if (rejection.code === 'auth') {
|
|
161
|
+
return new Error(`Qoder rejected the credential: ${rejection.message} — run /login qoder to sign in again`)
|
|
162
|
+
}
|
|
163
|
+
return new Error(rejection.message)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** 从凭据中提取签名所需字段。 */
|
|
167
|
+
function signingFacts(credential: QoderCredential) {
|
|
168
|
+
return {
|
|
169
|
+
machineId: credential.machineId ?? '',
|
|
170
|
+
uid: credential.uid ?? '',
|
|
171
|
+
encryptUserInfo: credential.encryptUserInfo ?? '',
|
|
172
|
+
signingKey: credential.signingKey ?? '',
|
|
173
|
+
dataPolicy: credential.dataPolicy ?? '',
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// ---------------------------------------------------------------------------
|
|
178
|
+
// 登录、刷新与 Pi 访问凭据解析
|
|
179
|
+
// ---------------------------------------------------------------------------
|
|
180
|
+
|
|
181
|
+
/** /login 使用的设备授权流程。 */
|
|
182
|
+
async function login(callbacks: OAuthLoginCallbacks): Promise<QoderCredential> {
|
|
183
|
+
// 重用设备标识,让重复登录仍对应同一设备。
|
|
184
|
+
const existing = storedCredential()
|
|
185
|
+
const machineId = typeof existing?.machineId === 'string' && existing.machineId !== ''
|
|
186
|
+
? existing.machineId
|
|
187
|
+
: crypto.randomUUID()
|
|
188
|
+
const pending = deviceLoginUrl(machineId)
|
|
189
|
+
callbacks.onAuth({
|
|
190
|
+
url: pending.authUrl,
|
|
191
|
+
instructions: 'Open this URL, approve the Qoder account, then return to pi; login completes automatically.',
|
|
192
|
+
})
|
|
193
|
+
callbacks.onProgress?.('Waiting for Qoder approval…')
|
|
194
|
+
const payload = await waitForDeviceToken(pending, CONFIG.deviceFlowTimeoutMs, { signal: callbacks.signal })
|
|
195
|
+
if (payload === undefined) {
|
|
196
|
+
throw new Error('Qoder device login timed out; run /login again.')
|
|
197
|
+
}
|
|
198
|
+
callbacks.onProgress?.('Qoder approved; preparing signing fields…')
|
|
199
|
+
return remember(await buildCredentialFromDeviceToken(payload, {
|
|
200
|
+
machineId,
|
|
201
|
+
signal: callbacks.signal,
|
|
202
|
+
}))
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Pi 在凭据临近过期时调用的 Token 轮换。 */
|
|
206
|
+
async function refreshToken(credentials: OAuthCredentials, signal: AbortSignal): Promise<QoderCredential> {
|
|
207
|
+
return remember(await refreshCredential(credentials, { signal }))
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** 为 Pi 的兼容 OAuth 接口提供本次请求的访问凭据。 */
|
|
211
|
+
function getApiKey(credentials: OAuthCredentials): string {
|
|
212
|
+
// 同步本次请求的签名材料,避免跨进程刷新后仍使用旧材料。
|
|
213
|
+
remember(credentials)
|
|
214
|
+
return credentials.access
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// ---------------------------------------------------------------------------
|
|
218
|
+
// 对话记录转换
|
|
219
|
+
// ---------------------------------------------------------------------------
|
|
220
|
+
|
|
221
|
+
type WirePart =
|
|
222
|
+
| { type: 'text'; text: string }
|
|
223
|
+
| { type: 'image_url'; image_url: { url: string } }
|
|
224
|
+
|
|
225
|
+
interface WireMessage {
|
|
226
|
+
role: 'system' | 'user' | 'assistant' | 'tool'
|
|
227
|
+
content: string | WirePart[]
|
|
228
|
+
tool_calls?: {
|
|
229
|
+
id: string
|
|
230
|
+
type: 'function'
|
|
231
|
+
function: { name: string; arguments: string }
|
|
232
|
+
}[]
|
|
233
|
+
tool_call_id?: string
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function imagePart(mimeType: string, data: string): WirePart {
|
|
237
|
+
return { type: 'image_url', image_url: { url: `data:${mimeType};base64,${data}` } }
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* 将 Pi 对话转换为 Qoder agent-chat 使用的 OpenAI 消息结构。
|
|
242
|
+
* 思考内容不回放;工具调用与结果分别映射到 tool_calls 和 tool 消息。
|
|
243
|
+
*/
|
|
244
|
+
function toWireMessages(messages: TranscriptContext['messages'], systemPrompt: string): WireMessage[] {
|
|
245
|
+
const output: WireMessage[] = []
|
|
246
|
+
if (systemPrompt.trim() !== '') output.push({ role: 'system', content: systemPrompt })
|
|
247
|
+
|
|
248
|
+
// Qoder 要求每个 assistant tool_call 在任何其他角色之前得到 tool 结果。
|
|
249
|
+
// 图片先缓存到整个批次结束,避免 user 消息插在 tool 结果之间;中断的调用补显式占位结果。
|
|
250
|
+
let pending: { id: string; original: string }[] = []
|
|
251
|
+
const assigned = new Map<string, string[]>()
|
|
252
|
+
let images: WirePart[] = []
|
|
253
|
+
let sequence = 0
|
|
254
|
+
|
|
255
|
+
const flushToolBatch = () => {
|
|
256
|
+
for (const call of pending) {
|
|
257
|
+
output.push({ role: 'tool', tool_call_id: call.id, content: '(tool call interrupted before a result was recorded)' })
|
|
258
|
+
const queue = assigned.get(call.original)
|
|
259
|
+
const index = queue?.indexOf(call.id) ?? -1
|
|
260
|
+
if (queue && index >= 0) {
|
|
261
|
+
queue.splice(index, 1)
|
|
262
|
+
if (queue.length === 0) assigned.delete(call.original)
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
pending = []
|
|
266
|
+
if (images.length > 0) {
|
|
267
|
+
output.push({ role: 'user', content: [{ type: 'text', text: 'Attached image(s) from tool result:' }, ...images] })
|
|
268
|
+
images = []
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
for (const message of messages) {
|
|
273
|
+
if (message.role === 'system') continue
|
|
274
|
+
if (message.role === 'user') {
|
|
275
|
+
flushToolBatch()
|
|
276
|
+
if (typeof message.content === 'string') {
|
|
277
|
+
if (message.content !== '') output.push({ role: 'user', content: message.content })
|
|
278
|
+
continue
|
|
279
|
+
}
|
|
280
|
+
const parts: WirePart[] = []
|
|
281
|
+
for (const block of message.content) {
|
|
282
|
+
if (block.type === 'text' && block.text !== '') parts.push({ type: 'text', text: block.text })
|
|
283
|
+
else if (block.type === 'image') parts.push(imagePart(block.mimeType, block.data))
|
|
284
|
+
}
|
|
285
|
+
if (parts.length === 0) continue
|
|
286
|
+
const onlyText = parts.every((part) => part.type === 'text')
|
|
287
|
+
output.push({
|
|
288
|
+
role: 'user',
|
|
289
|
+
content: onlyText ? parts.map((part) => part.type === 'text' ? part.text : '').join('') : parts,
|
|
290
|
+
})
|
|
291
|
+
continue
|
|
292
|
+
}
|
|
293
|
+
if (message.role === 'assistant') {
|
|
294
|
+
flushToolBatch()
|
|
295
|
+
let text = ''
|
|
296
|
+
const calls: NonNullable<WireMessage['tool_calls']> = []
|
|
297
|
+
for (const block of message.content) {
|
|
298
|
+
if (block.type === 'text') text += block.text
|
|
299
|
+
else if (block.type === 'toolCall') {
|
|
300
|
+
const original = block.id || `call_${calls.length + 1}`
|
|
301
|
+
// 跨 provider 的助手调用 ID 可能含分隔符或来自其他网关;Qoder 只要求批内配对。
|
|
302
|
+
const id = `call_${++sequence}`
|
|
303
|
+
const queue = assigned.get(original) ?? []
|
|
304
|
+
queue.push(id)
|
|
305
|
+
assigned.set(original, queue)
|
|
306
|
+
pending.push({ id, original })
|
|
307
|
+
calls.push({
|
|
308
|
+
id,
|
|
309
|
+
type: 'function',
|
|
310
|
+
function: { name: block.name, arguments: JSON.stringify(block.arguments ?? {}) },
|
|
311
|
+
})
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (text === '' && calls.length === 0) continue
|
|
315
|
+
// Qoder 网关会丢弃 content:null 的助手消息,导致后续 tool 结果失去配对。
|
|
316
|
+
const assistant: WireMessage = { role: 'assistant', content: text }
|
|
317
|
+
if (calls.length > 0) assistant.tool_calls = calls
|
|
318
|
+
output.push(assistant)
|
|
319
|
+
continue
|
|
320
|
+
}
|
|
321
|
+
if (message.role === 'toolResult') {
|
|
322
|
+
const original = message.toolCallId
|
|
323
|
+
const queue = original ? assigned.get(original) : undefined
|
|
324
|
+
const id = queue?.shift()
|
|
325
|
+
// 没有对应助手调用的结果会让上游拒绝整段请求,直接丢弃。
|
|
326
|
+
if (!id) continue
|
|
327
|
+
if (original && queue?.length === 0) assigned.delete(original)
|
|
328
|
+
pending = pending.filter((call) => call.id !== id)
|
|
329
|
+
const textParts = message.content
|
|
330
|
+
.filter((block) => block.type === 'text')
|
|
331
|
+
.map((block) => block.text)
|
|
332
|
+
const resultImages = message.content.filter((block) => block.type === 'image')
|
|
333
|
+
output.push({
|
|
334
|
+
role: 'tool',
|
|
335
|
+
tool_call_id: id,
|
|
336
|
+
content: textParts.join('\n') || (message.isError ? '(tool failed without output)' : '(no tool output)'),
|
|
337
|
+
})
|
|
338
|
+
for (const image of resultImages) images.push(imagePart(image.mimeType, image.data))
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
flushToolBatch()
|
|
342
|
+
return output
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** 将 Pi 工具转换为 OpenAI 函数声明。 */
|
|
346
|
+
function toWireTools(tools: Tool[]) {
|
|
347
|
+
return tools.map((tool) => ({
|
|
348
|
+
type: 'function' as const,
|
|
349
|
+
function: {
|
|
350
|
+
name: tool.name,
|
|
351
|
+
description: tool.description ?? '',
|
|
352
|
+
parameters: tool.parameters ?? { type: 'object', properties: {} },
|
|
353
|
+
},
|
|
354
|
+
}))
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// ---------------------------------------------------------------------------
|
|
358
|
+
// 流式传输
|
|
359
|
+
// ---------------------------------------------------------------------------
|
|
360
|
+
|
|
361
|
+
/** 校验来自请求回调和上游 JSON 的对象边界。 */
|
|
362
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
363
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** 合并模型和调用方请求头,大小写不敏感;null 用于移除默认头。 */
|
|
367
|
+
function requestHeaders(model: Model<string>, options: SimpleStreamOptions | undefined, defaults: Record<string, string>) {
|
|
368
|
+
const headers = new Headers(defaults)
|
|
369
|
+
for (const group of [model.headers, options?.headers]) {
|
|
370
|
+
for (const [name, value] of Object.entries(group ?? {})) {
|
|
371
|
+
if (value === null) headers.delete(name)
|
|
372
|
+
else headers.set(name, value)
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
return Object.fromEntries(headers)
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** 请求级环境覆盖优先于模型地址;其他配置回退到进程初始化值。 */
|
|
379
|
+
function signingOptions(model: Model<string>, options?: SimpleStreamOptions) {
|
|
380
|
+
const configured = (name: string, fallback: string) => options?.env?.[name]?.trim() || fallback
|
|
381
|
+
return {
|
|
382
|
+
endpoint: configured('QODER_ENDPOINT', model.baseUrl || CONFIG.endpoint).replace(/\/+$/, ''),
|
|
383
|
+
apiPath: configured('QODER_API_PATH', CONFIG.apiPath),
|
|
384
|
+
cliVersion: configured('QODER_CLI_VERSION', CONFIG.cliVersion),
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function emptyAssistantMessage(model: Model<string>): AssistantMessage {
|
|
389
|
+
return {
|
|
390
|
+
role: 'assistant',
|
|
391
|
+
content: [],
|
|
392
|
+
api: model.api,
|
|
393
|
+
provider: model.provider,
|
|
394
|
+
model: model.id,
|
|
395
|
+
usage: {
|
|
396
|
+
input: 0,
|
|
397
|
+
output: 0,
|
|
398
|
+
cacheRead: 0,
|
|
399
|
+
cacheWrite: 0,
|
|
400
|
+
totalTokens: 0,
|
|
401
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
402
|
+
qoderRequestId: crypto.randomUUID(),
|
|
403
|
+
} as QoderUsage,
|
|
404
|
+
stopReason: 'pending',
|
|
405
|
+
timestamp: Date.now(),
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** 上游在首字节前静默断连;属于可重发的传输失败,而不是模型结论。 */
|
|
410
|
+
class StreamClosedError extends Error {
|
|
411
|
+
constructor() {
|
|
412
|
+
super('Qoder stream closed before completion')
|
|
413
|
+
this.name = 'StreamClosedError'
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** 首次连接的最大尝试次数:一次正常请求加四次重试。 */
|
|
418
|
+
const MAX_STREAM_ATTEMPTS = 5
|
|
419
|
+
/** 每次重发前递增等待,避免连续撞上同一段网络抖动。 */
|
|
420
|
+
const RETRY_BACKOFF_MS = [0, 400, 1_000, 2_000, 4_000]
|
|
421
|
+
|
|
422
|
+
/** 遇到 110 时最多轮换的网关数。定义在扩展内,避免 /reload 后读取到旧版 core。 */
|
|
423
|
+
const ENDPOINT_ROTATION_LIMIT = 2
|
|
424
|
+
|
|
425
|
+
/** 网关节点计数用尽(code 110)与凭据无关;本地识别,避免依赖 core 的版本。 */
|
|
426
|
+
function isDailyLimitPayload(payload: unknown): boolean {
|
|
427
|
+
const body = typeof payload === 'string' ? (() => { try { return JSON.parse(payload) } catch { return undefined } })() : payload
|
|
428
|
+
if (body === null || typeof body !== 'object') return false
|
|
429
|
+
const code = (body as Record<string, unknown>).code
|
|
430
|
+
return code !== undefined && code !== null && String(code) === '110'
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/** 网关节点日计数已满;换节点重试即可,与积分、凭据无关。 */
|
|
434
|
+
class GatewayFullError extends Error {
|
|
435
|
+
constructor(message: string) {
|
|
436
|
+
super(message)
|
|
437
|
+
this.name = 'GatewayFullError'
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** 按账号缓存区域推理节点,避免每个请求都访问中心节点。 */
|
|
442
|
+
const regionCache = new Map<string, { nodes: string[]; at: number }>()
|
|
443
|
+
|
|
444
|
+
/** 显式指定推理节点,优先于中心节点下发;用于固定节点或离线验证。 */
|
|
445
|
+
function pinnedNodes(options?: SimpleStreamOptions): string[] {
|
|
446
|
+
const raw = options?.env?.QODER_INFER_NODES?.trim() || ''
|
|
447
|
+
return raw
|
|
448
|
+
.split(',')
|
|
449
|
+
.map(node => node.trim().replace(/\/+$/, ''))
|
|
450
|
+
.filter(node => node !== '')
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* 同一集群内可互替的推理节点。
|
|
455
|
+
*
|
|
456
|
+
* 实测国际版 api1/api2/api3 服务同一批模型,其中 api3 出现过独立的日计数用尽,
|
|
457
|
+
* 因此节点之间可以重试;未知域名不猜测,只使用配置值。
|
|
458
|
+
*/
|
|
459
|
+
function siblingNodes(endpoint: string): string[] {
|
|
460
|
+
try {
|
|
461
|
+
const host = new URL(endpoint).host
|
|
462
|
+
if (!/^api\d+\.qoder\.sh$/.test(host)) return []
|
|
463
|
+
return ['https://api1.qoder.sh', 'https://api2.qoder.sh', 'https://api3.qoder.sh']
|
|
464
|
+
.filter(node => node !== endpoint)
|
|
465
|
+
} catch {
|
|
466
|
+
return []
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/** 该账号缓存的区域节点;只在发现网关满时刷新,避免每次请求都访问中心节点。 */
|
|
471
|
+
function cachedNodes(credential: QoderCredential): string[] {
|
|
472
|
+
const cached = regionCache.get(String(credential.uid || credential.access))
|
|
473
|
+
if (cached === undefined || Date.now() - cached.at >= CONFIG.regionTtlMs) return []
|
|
474
|
+
return cached.nodes
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/** 本次请求要依次尝试的网关:显式指定 > 区域缓存 > 配置网关及其同集群兄弟节点。 */
|
|
478
|
+
/** 节点连续失败的冷却时间;期间仍可作为最后手段尝试,但不再排在最前。 */
|
|
479
|
+
const NODE_COOLDOWN_MS = 10 * 60_000
|
|
480
|
+
/** 冷却表按节点地址记录,避免每次都先撞已知不可用的节点。 */
|
|
481
|
+
const nodeCooldowns = new Map<string, number>()
|
|
482
|
+
|
|
483
|
+
/** 标记节点计数用尽,放入冷却。 */
|
|
484
|
+
function markNodeFull(endpoint: string): void {
|
|
485
|
+
nodeCooldowns.set(endpoint, Date.now() + NODE_COOLDOWN_MS)
|
|
486
|
+
if (nodeCooldowns.size > 16) {
|
|
487
|
+
const oldest = nodeCooldowns.keys().next().value
|
|
488
|
+
if (oldest !== undefined && oldest !== endpoint) nodeCooldowns.delete(oldest)
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** 节点恢复可用后清除冷却。 */
|
|
493
|
+
function clearNodeFull(endpoint: string): void {
|
|
494
|
+
nodeCooldowns.delete(endpoint)
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/** 已知可用的节点排在前面,冷却中的排到队尾,保证仍会被尝试。 */
|
|
498
|
+
function orderByHealth(nodes: string[]): string[] {
|
|
499
|
+
const now = Date.now()
|
|
500
|
+
const healthy: string[] = []
|
|
501
|
+
const cooling: string[] = []
|
|
502
|
+
for (const node of nodes) {
|
|
503
|
+
if ((nodeCooldowns.get(node) ?? 0) > now) cooling.push(node)
|
|
504
|
+
else healthy.push(node)
|
|
505
|
+
}
|
|
506
|
+
return [...healthy, ...cooling]
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
function gatewayPlan(credential: QoderCredential, endpoint: string, options?: SimpleStreamOptions): string[] {
|
|
510
|
+
const pinned = pinnedNodes(options)
|
|
511
|
+
if (pinned.length > 0) return pinned
|
|
512
|
+
return orderByHealth([...new Set([...cachedNodes(credential), endpoint, ...siblingNodes(endpoint)])])
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/** 静态节点全部用尽后,向中心节点补一次候选;失败时返回空数组。 */
|
|
516
|
+
async function discoverGateways(credential: QoderCredential, known: string[]): Promise<string[]> {
|
|
517
|
+
// /reload 后 core 可能仍是旧模块;缺少区域端点能力时只使用静态节点。
|
|
518
|
+
if (typeof fetchRegionEndpoints !== 'function') return []
|
|
519
|
+
const key = String(credential.uid || credential.access)
|
|
520
|
+
const nodes = await fetchRegionEndpoints(credential)
|
|
521
|
+
regionCache.set(key, { nodes, at: Date.now() })
|
|
522
|
+
return nodes.filter(node => !known.includes(node))
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/** 将上游结束原因转换为 Pi 的结束原因。 */
|
|
526
|
+
function stopReasonOf(finishReason: string | null | undefined): 'stop' | 'length' | 'toolUse' | 'error' {
|
|
527
|
+
if (finishReason === 'tool_calls' || finishReason === 'function_call') return 'toolUse'
|
|
528
|
+
if (finishReason === 'length') return 'length'
|
|
529
|
+
if (finishReason === null || finishReason === undefined || finishReason === 'stop' || finishReason === 'end') return 'stop'
|
|
530
|
+
return 'error'
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* 将一次 Qoder 响应转换为 Pi 助手消息事件。
|
|
535
|
+
* 签名依赖本次请求体,因此由本模块执行传输。
|
|
536
|
+
*/
|
|
537
|
+
function streamQoder(
|
|
538
|
+
model: Model<string>,
|
|
539
|
+
context: TranscriptContext,
|
|
540
|
+
options?: SimpleStreamOptions,
|
|
541
|
+
onComplete?: (message: AssistantMessage) => void,
|
|
542
|
+
cooldowns: QueueCooldowns = standaloneCooldowns,
|
|
543
|
+
): AssistantMessageEventStream {
|
|
544
|
+
// 缺少凭据时抛出错误,由 Pi 的流式包装器转换为错误事件。
|
|
545
|
+
const credential = resolveCredential(options?.apiKey)
|
|
546
|
+
const definition = definitionForRequest(model)
|
|
547
|
+
if (definition === undefined) {
|
|
548
|
+
throw new Error(`Unknown Qoder model "${model.id}".`)
|
|
549
|
+
}
|
|
550
|
+
const resolved = { id: model.id, def: definition }
|
|
551
|
+
|
|
552
|
+
const transcript = collapseSystemMessages(context)
|
|
553
|
+
const systemPrompt = getCurrentSystemPrompt(transcript.messages)
|
|
554
|
+
const tools = getCurrentTools(transcript.messages)
|
|
555
|
+
const messages = toWireMessages(transcript.messages, systemPrompt)
|
|
556
|
+
if (messages.length === 0) {
|
|
557
|
+
throw new Error('Qoder request has no messages.')
|
|
558
|
+
}
|
|
559
|
+
const effort = effortForLevel(resolved.def, options?.reasoning)
|
|
560
|
+
const output = emptyAssistantMessage(model)
|
|
561
|
+
const built = buildAgentRequest({
|
|
562
|
+
requestId: (output.usage as QoderUsage).qoderRequestId,
|
|
563
|
+
sessionId: options?.sessionId,
|
|
564
|
+
modelId: resolved.id,
|
|
565
|
+
definition: resolved.def,
|
|
566
|
+
messages,
|
|
567
|
+
tools: toWireTools(tools),
|
|
568
|
+
toolChoice: options?.toolChoice,
|
|
569
|
+
effort,
|
|
570
|
+
maxTokens: options?.maxTokens ?? model.maxTokens,
|
|
571
|
+
temperature: options?.temperature,
|
|
572
|
+
window: model.contextWindow,
|
|
573
|
+
})
|
|
574
|
+
|
|
575
|
+
const stream = createAssistantMessageEventStream()
|
|
576
|
+
const controller = new AbortController()
|
|
577
|
+
let started = false
|
|
578
|
+
const timeoutMs = options?.timeoutMs ?? 600_000
|
|
579
|
+
const requestSignal = options?.signal === undefined
|
|
580
|
+
? controller.signal
|
|
581
|
+
: AbortSignal.any([options.signal, controller.signal])
|
|
582
|
+
const timeout = setTimeout(() => controller.abort(new Error('Qoder request timed out')), timeoutMs)
|
|
583
|
+
|
|
584
|
+
// 上游偶发在首字节前静默断连;仅在尚未向调用方推送任何事件时重发,避免重复输出。
|
|
585
|
+
const attempt = async (attemptEndpoint?: string): Promise<void> => {
|
|
586
|
+
let terminal = false
|
|
587
|
+
const blocks = output.content
|
|
588
|
+
let thinkingIndex = -1
|
|
589
|
+
let textIndex = -1
|
|
590
|
+
const callIndex = new Map<number, number>()
|
|
591
|
+
const callArgs = new Map<number, ToolCallArgumentsStreamType>()
|
|
592
|
+
let finishReason: string | null = null
|
|
593
|
+
let rawUsage: unknown
|
|
594
|
+
let sawContent = false
|
|
595
|
+
|
|
596
|
+
const ensureThinking = (signature: string) => {
|
|
597
|
+
if (thinkingIndex < 0) {
|
|
598
|
+
thinkingIndex = blocks.length
|
|
599
|
+
blocks.push({ type: 'thinking', thinking: '', thinkingSignature: signature })
|
|
600
|
+
stream.push({ type: 'thinking_start', contentIndex: thinkingIndex, partial: output })
|
|
601
|
+
}
|
|
602
|
+
return blocks[thinkingIndex] as { type: 'thinking'; thinking: string }
|
|
603
|
+
}
|
|
604
|
+
const ensureText = () => {
|
|
605
|
+
if (textIndex < 0) {
|
|
606
|
+
textIndex = blocks.length
|
|
607
|
+
blocks.push({ type: 'text', text: '' })
|
|
608
|
+
stream.push({ type: 'text_start', contentIndex: textIndex, partial: output })
|
|
609
|
+
}
|
|
610
|
+
return blocks[textIndex] as { type: 'text'; text: string }
|
|
611
|
+
}
|
|
612
|
+
const ensureToolCall = (call: Record<string, unknown>) => {
|
|
613
|
+
const upstream = typeof call.index === 'number' && Number.isInteger(call.index) ? call.index : 0
|
|
614
|
+
const fn = isRecord(call.function) ? call.function : {}
|
|
615
|
+
let index = callIndex.get(upstream)
|
|
616
|
+
if (index === undefined) {
|
|
617
|
+
index = blocks.length
|
|
618
|
+
const block: ToolCall = {
|
|
619
|
+
type: 'toolCall',
|
|
620
|
+
id: typeof call?.id === 'string' ? call.id : '',
|
|
621
|
+
name: typeof fn.name === 'string' ? fn.name : '',
|
|
622
|
+
arguments: {},
|
|
623
|
+
}
|
|
624
|
+
blocks.push(block)
|
|
625
|
+
callIndex.set(upstream, index)
|
|
626
|
+
callArgs.set(upstream, new ToolCallArgumentsStream())
|
|
627
|
+
stream.push({ type: 'toolcall_start', contentIndex: index, partial: output })
|
|
628
|
+
}
|
|
629
|
+
const block = blocks[index] as ToolCall
|
|
630
|
+
if (block.id === '' && typeof call?.id === 'string') block.id = call.id
|
|
631
|
+
if (block.name === '' && typeof fn.name === 'string') block.name = fn.name
|
|
632
|
+
if (typeof fn.arguments === 'string' && fn.arguments !== '') {
|
|
633
|
+
const args = callArgs.get(upstream)
|
|
634
|
+
if (args !== undefined) {
|
|
635
|
+
block.arguments = args.append(fn.arguments)
|
|
636
|
+
stream.push({
|
|
637
|
+
type: 'toolcall_delta',
|
|
638
|
+
contentIndex: index,
|
|
639
|
+
delta: fn.arguments,
|
|
640
|
+
partial: output,
|
|
641
|
+
})
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
return index
|
|
645
|
+
}
|
|
646
|
+
const emitChunk = (chunk: Record<string, unknown>) => {
|
|
647
|
+
if (isRecord(chunk.usage)) rawUsage = { ...(isRecord(rawUsage) ? rawUsage : {}), ...chunk.usage }
|
|
648
|
+
const choice: unknown = Array.isArray(chunk.choices) ? chunk.choices[0] : undefined
|
|
649
|
+
if (!isRecord(choice)) return
|
|
650
|
+
const delta = choice.delta
|
|
651
|
+
if (isRecord(delta)) {
|
|
652
|
+
const reasoning = typeof delta.reasoning_content === 'string'
|
|
653
|
+
? delta.reasoning_content
|
|
654
|
+
: typeof delta.reasoning === 'string' ? delta.reasoning : ''
|
|
655
|
+
if (reasoning !== '') {
|
|
656
|
+
sawContent = true
|
|
657
|
+
const block = ensureThinking('reasoning_content')
|
|
658
|
+
block.thinking += reasoning
|
|
659
|
+
stream.push({ type: 'thinking_delta', contentIndex: thinkingIndex, delta: reasoning, partial: output })
|
|
660
|
+
}
|
|
661
|
+
if (typeof delta.content === 'string' && delta.content !== '') {
|
|
662
|
+
sawContent = true
|
|
663
|
+
const block = ensureText()
|
|
664
|
+
block.text += delta.content
|
|
665
|
+
stream.push({ type: 'text_delta', contentIndex: textIndex, delta: delta.content, partial: output })
|
|
666
|
+
}
|
|
667
|
+
for (const call of Array.isArray(delta.tool_calls) ? delta.tool_calls : []) {
|
|
668
|
+
if (!isRecord(call)) throw new Error('Qoder returned a malformed tool-call delta')
|
|
669
|
+
sawContent = true
|
|
670
|
+
ensureToolCall(call)
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
if (typeof choice.finish_reason === 'string') finishReason = choice.finish_reason
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
const updateUsage = () => {
|
|
677
|
+
const usage = mapUsage(rawUsage)
|
|
678
|
+
if (usage !== undefined) {
|
|
679
|
+
calculateCost(model, usage)
|
|
680
|
+
output.usage = { ...usage, qoderRequestId: (output.usage as QoderUsage).qoderRequestId } as QoderUsage
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
const finish = () => {
|
|
685
|
+
for (const [upstream, index] of callIndex) {
|
|
686
|
+
const block = blocks[index] as ToolCall
|
|
687
|
+
const args = callArgs.get(upstream)
|
|
688
|
+
if (args !== undefined) {
|
|
689
|
+
const final = args.finish()
|
|
690
|
+
block.arguments = final.arguments
|
|
691
|
+
if (final.argumentsError !== undefined) block.argumentsError = final.argumentsError
|
|
692
|
+
}
|
|
693
|
+
stream.push({ type: 'toolcall_end', contentIndex: index, toolCall: block, partial: output })
|
|
694
|
+
}
|
|
695
|
+
if (thinkingIndex >= 0) {
|
|
696
|
+
const block = blocks[thinkingIndex] as { thinking: string }
|
|
697
|
+
stream.push({ type: 'thinking_end', contentIndex: thinkingIndex, content: block.thinking, partial: output })
|
|
698
|
+
}
|
|
699
|
+
if (textIndex >= 0) {
|
|
700
|
+
const block = blocks[textIndex] as { text: string }
|
|
701
|
+
stream.push({ type: 'text_end', contentIndex: textIndex, content: block.text, partial: output })
|
|
702
|
+
}
|
|
703
|
+
updateUsage()
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
try {
|
|
707
|
+
options?.signal?.throwIfAborted?.()
|
|
708
|
+
let payload: unknown = built.body
|
|
709
|
+
const replaced = await options?.onPayload?.(payload, model)
|
|
710
|
+
if (replaced !== undefined) payload = replaced
|
|
711
|
+
if (!isRecord(payload)) throw new Error('Qoder payload must be a JSON object')
|
|
712
|
+
const signing = signingOptions(model, options)
|
|
713
|
+
// 账号相同的 Token 轮换继续遵守冷却;只保留摘要,不在键中保存原始凭据。
|
|
714
|
+
const cooldownKey = createHash('sha256').update(JSON.stringify([
|
|
715
|
+
credential.uid || credential.access, signing.endpoint, signing.apiPath, payload.model,
|
|
716
|
+
])).digest('hex')
|
|
717
|
+
await waitForCooldown(cooldowns, cooldownKey, requestSignal, options?.maxRetryDelayMs)
|
|
718
|
+
|
|
719
|
+
const endpoint = attemptEndpoint ?? signing.endpoint
|
|
720
|
+
const signed = await signChatRequest(payload, signingFacts(credential), { ...signing, endpoint })
|
|
721
|
+
requestSignal.throwIfAborted()
|
|
722
|
+
const response = await (options?.fetch ?? globalThis.fetch)(signed.url, {
|
|
723
|
+
method: 'POST',
|
|
724
|
+
headers: requestHeaders(model, options, signed.headers),
|
|
725
|
+
body: signed.body,
|
|
726
|
+
signal: requestSignal,
|
|
727
|
+
})
|
|
728
|
+
await options?.onResponse?.({ status: response.status, headers: Object.fromEntries(response.headers) }, model)
|
|
729
|
+
if (!response.ok) {
|
|
730
|
+
const detail = await response.text().catch(() => '')
|
|
731
|
+
const rejection = classifyRejection(response.status, detail, `Qoder HTTP ${response.status}`)
|
|
732
|
+
if (rejection.code === 'daily_limit' || isDailyLimitPayload(detail)) throw new GatewayFullError(rejection.message)
|
|
733
|
+
throw rejectionError(rejection, cooldowns, cooldownKey, response.headers)
|
|
734
|
+
}
|
|
735
|
+
if (!started) {
|
|
736
|
+
stream.push({ type: 'start', partial: output })
|
|
737
|
+
started = true
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
if (response.body === null) throw new Error('Qoder response has no body')
|
|
741
|
+
let sawEnd = false
|
|
742
|
+
for await (const rawFrame of readSseData(response.body, requestSignal)) {
|
|
743
|
+
if (rawFrame.trim() === '') continue
|
|
744
|
+
if (rawFrame.trim() === '[DONE]') {
|
|
745
|
+
sawEnd = true
|
|
746
|
+
break
|
|
747
|
+
}
|
|
748
|
+
let providerEvent: unknown
|
|
749
|
+
try {
|
|
750
|
+
providerEvent = JSON.parse(rawFrame)
|
|
751
|
+
} catch {
|
|
752
|
+
throw new Error('Qoder returned malformed SSE JSON')
|
|
753
|
+
}
|
|
754
|
+
if (!isRecord(providerEvent)) throw new Error('Qoder returned a non-object SSE event')
|
|
755
|
+
await options?.onProviderStreamEvent?.(providerEvent, model)
|
|
756
|
+
requestSignal.throwIfAborted()
|
|
757
|
+
const frame = unwrapFrame(providerEvent)
|
|
758
|
+
if (frame === undefined) continue
|
|
759
|
+
if (frame.done === true) {
|
|
760
|
+
sawEnd = true
|
|
761
|
+
break
|
|
762
|
+
}
|
|
763
|
+
if (frame.status !== undefined && frame.status >= 400) {
|
|
764
|
+
const rejection = classifyRejection(frame.status, frame.raw ?? frame.chunk, `Qoder rejected the request (${frame.status})`)
|
|
765
|
+
// 110 由网关节点自己的计数下发;换节点重试,不当作终态。
|
|
766
|
+
if (rejection.code === 'daily_limit' || isDailyLimitPayload(frame.raw ?? frame.chunk)) throw new GatewayFullError(rejection.message)
|
|
767
|
+
throw rejectionError(rejection, cooldowns, cooldownKey, response.headers)
|
|
768
|
+
}
|
|
769
|
+
const chunk: unknown = frame.chunk
|
|
770
|
+
if (chunk === undefined) continue
|
|
771
|
+
if (!isRecord(chunk)) throw new Error('Qoder returned a malformed stream chunk')
|
|
772
|
+
if (!Array.isArray(chunk.choices) && chunk.code !== undefined) {
|
|
773
|
+
const rejection = classifyRejection(500, chunk, 'Qoder returned an error')
|
|
774
|
+
throw rejectionError(rejection, cooldowns, cooldownKey, response.headers)
|
|
775
|
+
}
|
|
776
|
+
emitChunk(chunk)
|
|
777
|
+
}
|
|
778
|
+
requestSignal.throwIfAborted()
|
|
779
|
+
if (!sawEnd && finishReason === null) {
|
|
780
|
+
throw new StreamClosedError()
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
if (!sawContent) throw new Error('Qoder returned an empty response')
|
|
784
|
+
const reason = stopReasonOf(finishReason)
|
|
785
|
+
if (reason === 'error') {
|
|
786
|
+
throw new Error(`Qoder finish reason: ${String(finishReason)}`)
|
|
787
|
+
}
|
|
788
|
+
finish()
|
|
789
|
+
output.stopReason = reason
|
|
790
|
+
if (reason === 'toolUse' && callIndex.size === 0) {
|
|
791
|
+
throw new Error('Qoder reported tool calls without any tool-call delta')
|
|
792
|
+
}
|
|
793
|
+
stream.push({ type: 'done', reason, message: output })
|
|
794
|
+
stream.end()
|
|
795
|
+
terminal = true
|
|
796
|
+
} catch (error) {
|
|
797
|
+
// 这里不中止共享信号:可重发的断流需要复用同一信号重开请求。
|
|
798
|
+
updateUsage()
|
|
799
|
+
if (options?.signal?.aborted) output.stopReason = 'aborted'
|
|
800
|
+
else output.stopReason = 'error'
|
|
801
|
+
output.errorMessage = error instanceof Error ? error.message : String(error)
|
|
802
|
+
throw error
|
|
803
|
+
} finally {
|
|
804
|
+
// 仅在请求真正终结时记账;可重发的断流不生成请求记录。
|
|
805
|
+
if (terminal) {
|
|
806
|
+
try { onComplete?.(output) } catch { /* 会话可能已关闭。 */ }
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
const run = async () => {
|
|
812
|
+
// 外层:网关节点;内层:同一节点的断流重发。
|
|
813
|
+
const gateways = gatewayPlan(credential, signingOptions(model, options).endpoint, options)
|
|
814
|
+
const tried: string[] = []
|
|
815
|
+
let gatewayIndex = 0
|
|
816
|
+
let lastGatewayFailure: Error | undefined
|
|
817
|
+
for (; gatewayIndex < gateways.length && gatewayIndex <= ENDPOINT_ROTATION_LIMIT + 2; gatewayIndex++) {
|
|
818
|
+
let pushed = false
|
|
819
|
+
let movedOn = false
|
|
820
|
+
for (let attemptNumber = 1; attemptNumber <= MAX_STREAM_ATTEMPTS; attemptNumber++) {
|
|
821
|
+
// 只有内容事件会进入调用方上下文;start 只是提示,仍可安全重发。
|
|
822
|
+
pushed = false
|
|
823
|
+
const originalPush = stream.push.bind(stream)
|
|
824
|
+
const tracked = ((event: Parameters<typeof stream.push>[0]) => {
|
|
825
|
+
if (event.type !== 'start') pushed = true
|
|
826
|
+
return originalPush(event)
|
|
827
|
+
}) as typeof stream.push
|
|
828
|
+
stream.push = tracked
|
|
829
|
+
try {
|
|
830
|
+
await attempt(gateways[gatewayIndex])
|
|
831
|
+
clearNodeFull(gateways[gatewayIndex])
|
|
832
|
+
stream.push = originalPush
|
|
833
|
+
clearTimeout(timeout)
|
|
834
|
+
return
|
|
835
|
+
} catch (error) {
|
|
836
|
+
stream.push = originalPush
|
|
837
|
+
// 网关节点日计数已满:未输出内容时换下一个节点,否则按终态上报。
|
|
838
|
+
if (error instanceof GatewayFullError) {
|
|
839
|
+
lastGatewayFailure = error
|
|
840
|
+
const failed = gateways[gatewayIndex]
|
|
841
|
+
markNodeFull(failed)
|
|
842
|
+
try { tried.push(new URL(failed).host) } catch { /* 忽略畸形地址。 */ }
|
|
843
|
+
if (!pushed) { movedOn = true }
|
|
844
|
+
break
|
|
845
|
+
}
|
|
846
|
+
if (error instanceof StreamClosedError && !pushed && attemptNumber < MAX_STREAM_ATTEMPTS) {
|
|
847
|
+
output.content.length = 0
|
|
848
|
+
output.stopReason = 'pending'
|
|
849
|
+
output.errorMessage = undefined
|
|
850
|
+
const wait = RETRY_BACKOFF_MS[Math.min(attemptNumber, RETRY_BACKOFF_MS.length - 1)]
|
|
851
|
+
if (wait > 0) await delay(wait, requestSignal).catch(() => {})
|
|
852
|
+
continue
|
|
853
|
+
}
|
|
854
|
+
clearTimeout(timeout)
|
|
855
|
+
controller.abort(error)
|
|
856
|
+
stream.push({
|
|
857
|
+
type: 'error',
|
|
858
|
+
reason: output.stopReason as 'error' | 'aborted',
|
|
859
|
+
error: output,
|
|
860
|
+
})
|
|
861
|
+
stream.end()
|
|
862
|
+
try { onComplete?.(output) } catch { /* 会话可能已关闭。 */ }
|
|
863
|
+
return
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
if (!movedOn) break
|
|
867
|
+
// 换节点重发:清掉上一节点留下的错误与流状态,调用方从未收到内容。
|
|
868
|
+
output.content.length = 0
|
|
869
|
+
output.stopReason = 'pending'
|
|
870
|
+
output.errorMessage = undefined
|
|
871
|
+
// 准备下一个节点:静态节点用尽时向中心节点补一次。
|
|
872
|
+
if (gatewayIndex === gateways.length - 1) {
|
|
873
|
+
const extra = await discoverGateways(credential, gateways)
|
|
874
|
+
if (extra.length === 0) break
|
|
875
|
+
gateways.push(...extra)
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
// 所有节点都报 110:按终态上报,并说明这是节点计数而不是积分或凭据问题。
|
|
879
|
+
const failure = rejectionError(
|
|
880
|
+
{
|
|
881
|
+
code: 'daily_limit',
|
|
882
|
+
message: lastGatewayFailure?.message
|
|
883
|
+
?? (tried.length > 0 ? `Qoder gateways are full: ${tried.join(', ')}` : 'Qoder gateway is full'),
|
|
884
|
+
},
|
|
885
|
+
cooldowns, 'gateway-exhausted',
|
|
886
|
+
)
|
|
887
|
+
clearTimeout(timeout)
|
|
888
|
+
controller.abort(failure)
|
|
889
|
+
output.stopReason = 'error'
|
|
890
|
+
output.errorMessage = failure.message
|
|
891
|
+
stream.push({ type: 'error', reason: 'error', error: output })
|
|
892
|
+
stream.end()
|
|
893
|
+
try { onComplete?.(output) } catch { /* 会话可能已关闭。 */ }
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
void run()
|
|
897
|
+
return stream
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
// ---------------------------------------------------------------------------
|
|
901
|
+
// 扩展入口
|
|
902
|
+
// ---------------------------------------------------------------------------
|
|
903
|
+
|
|
904
|
+
export default function qoderGlobalExtension(pi: ExtensionAPI) {
|
|
905
|
+
// 原生 ESM 依赖可跨 /reload 保留旧 CONFIG;在扩展重新注册时同步认证配置。
|
|
906
|
+
CONFIG.clientId = process.env.QODER_CLIENT_ID?.trim() || 'e883ade2-e6e3-4d6d-adf7-f92ceff5fdcb'
|
|
907
|
+
const usage = installUsageUi(pi, resolveCredential)
|
|
908
|
+
const cooldowns: QueueCooldowns = new Map()
|
|
909
|
+
pi.on('session_shutdown', () => { cooldowns.clear() })
|
|
910
|
+
|
|
911
|
+
/** 搜索走 Qoder 自己的网关,需要已登录凭据与 WASM 签名。 */
|
|
912
|
+
pi.registerTool({
|
|
913
|
+
name: 'qoder_web_search',
|
|
914
|
+
label: 'Qoder web search',
|
|
915
|
+
description: 'Search the web through the Qoder gateway. Use for current events, documentation or facts not in the codebase. Returns titles, links and snippets.',
|
|
916
|
+
parameters: Type.Object({
|
|
917
|
+
query: Type.String({ description: 'Search query' }),
|
|
918
|
+
allowed_domains: Type.Optional(Type.Array(Type.String(), { description: 'Restrict results to these domains' })),
|
|
919
|
+
blocked_domains: Type.Optional(Type.Array(Type.String(), { description: 'Exclude these domains' })),
|
|
920
|
+
}),
|
|
921
|
+
async execute(_toolCallId, params, signal, _onUpdate, ctx) {
|
|
922
|
+
let credential: QoderCredential
|
|
923
|
+
try {
|
|
924
|
+
const auth = await ctx.modelRegistry.getProviderAuth(PROVIDER_ID)
|
|
925
|
+
if (!auth?.auth.apiKey) throw new Error('not signed in')
|
|
926
|
+
credential = resolveCredential(auth.auth.apiKey)
|
|
927
|
+
} catch {
|
|
928
|
+
throw new Error('Qoder web search needs a Qoder login. Run /login and choose Qoder.')
|
|
929
|
+
}
|
|
930
|
+
const result = await searchWeb(params.query, credential, {
|
|
931
|
+
signal,
|
|
932
|
+
...(params.allowed_domains?.length ? { allowedDomains: params.allowed_domains } : {}),
|
|
933
|
+
...(params.blocked_domains?.length ? { blockedDomains: params.blocked_domains } : {}),
|
|
934
|
+
})
|
|
935
|
+
return {
|
|
936
|
+
content: [{ type: 'text', text: formatSearchResult(result) }],
|
|
937
|
+
details: { query: result.query, count: result.hits.length, requestId: result.requestId, hits: result.hits },
|
|
938
|
+
}
|
|
939
|
+
},
|
|
940
|
+
})
|
|
941
|
+
pi.registerProvider(PROVIDER_ID, {
|
|
942
|
+
name: 'Qoder',
|
|
943
|
+
baseUrl: CONFIG.endpoint,
|
|
944
|
+
api: API_ID,
|
|
945
|
+
models: snapshotModels(),
|
|
946
|
+
refreshModels: createCatalogRefresh(),
|
|
947
|
+
oauth: {
|
|
948
|
+
name: 'Qoder',
|
|
949
|
+
isSubscription: true,
|
|
950
|
+
login,
|
|
951
|
+
refreshToken,
|
|
952
|
+
getApiKey,
|
|
953
|
+
},
|
|
954
|
+
streamSimple: (model, context, options) => streamQoder(model, context, options, usage.record, cooldowns),
|
|
955
|
+
})
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
// 导出供离线检查及其他 provider 实现复用。
|
|
959
|
+
export { streamQoder, toWireMessages, toWireTools, resolveCredential, login, refreshToken, getApiKey }
|
|
960
|
+
export type { QoderCredential }
|