dsh-codearts-dock 0.0.0-stage → 1.0.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/DISCLAIMER.md +47 -0
- package/LICENSE +21 -0
- package/README.md +172 -2
- package/SECURITY.md +47 -0
- package/THIRD_PARTY_NOTICES.md +64 -0
- package/client.js +3519 -0
- package/cordis.patch.yml +10 -0
- package/docs/DETAIL.md +25 -0
- package/docs/DETAIL.zh.md +25 -0
- package/docs/PUBLISHING.md +43 -0
- package/docs/preview.svg +78 -0
- package/icons/dsh-connect-codearts-128.png +0 -0
- package/icons/dsh-connect-codearts-256.png +0 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +88 -4
- package/scripts/build-market-index.mjs +24 -0
- package/scripts/check-legal.sh +33 -0
- package/scripts/check-release.mjs +29 -0
- package/scripts/test.mjs +11 -0
- package/src/account.js +217 -0
- package/src/accounts.js +481 -0
- package/src/adapter.js +742 -0
- package/src/backup.js +89 -0
- package/src/catalog.js +269 -0
- package/src/checkin.js +271 -0
- package/src/credentials.js +435 -0
- package/src/features.js +79 -0
- package/src/gateway.js +521 -0
- package/src/health.js +18 -0
- package/src/index.js +1108 -0
- package/src/models.js +147 -0
- package/src/paths.js +36 -0
- package/src/preferences.js +90 -0
- package/src/protocol/crypto.js +160 -0
- package/src/protocol/sign.js +176 -0
- package/src/protocol/sm4.js +264 -0
- package/src/protocol/sse.js +134 -0
- package/src/sts.js +118 -0
- package/src/usage.js +55 -0
- package/test/accounts.test.mjs +322 -0
- package/test/adapter.test.mjs +234 -0
- package/test/assembler.test.mjs +86 -0
- package/test/features.test.mjs +257 -0
- package/test/live-probe.mjs +55 -0
- package/test/protocol.test.mjs +167 -0
- package/test/registration.test.mjs +301 -0
- package/test/sm4.test.mjs +64 -0
- package/test/ui.test.mjs +370 -0
- package/test/visual.test.mjs +438 -0
package/src/adapter.js
ADDED
|
@@ -0,0 +1,742 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The 码道 LLM adapter.
|
|
3
|
+
*
|
|
4
|
+
* This is a native `LlmAdapter`, not a local HTTP bridge. The Host registers
|
|
5
|
+
* it against a provider route and calls `stream()` directly, which removes a
|
|
6
|
+
* whole process, a port, a restart, and a rewrite of the model list on disk
|
|
7
|
+
* from the design — the catalog is whatever the gateway said at the last
|
|
8
|
+
* refresh.
|
|
9
|
+
*
|
|
10
|
+
* The adapter converts between two vocabularies:
|
|
11
|
+
*
|
|
12
|
+
* Host messages (durable, role-tagged, block-structured)
|
|
13
|
+
* -> OpenAI chat-completions messages (what the gateway accepts)
|
|
14
|
+
*
|
|
15
|
+
* gateway SSE events (text / reasoning / tool-call deltas)
|
|
16
|
+
* -> Host `StreamChunk`s (block-start, delta, block-end, usage, finish)
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { GatewayError, MadaoClient } from './gateway.js'
|
|
20
|
+
import { parseIdentity } from './account.js'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @typedef {object} AdapterDeps
|
|
24
|
+
* @property {import('./credentials.js').CredentialProvider} credentials resolves a signed triple.
|
|
25
|
+
* @property {import('./catalog.js').Catalog} catalog the live model catalog.
|
|
26
|
+
* @property {() => string|undefined} [gateway] gateway root override.
|
|
27
|
+
* @property {() => any} [attachments] the Host attachment service, for image bytes.
|
|
28
|
+
* @property {(key: any) => MadaoClient} [createClient] client factory, injected by tests.
|
|
29
|
+
* @property {(line: string) => void} [log] diagnostic sink.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The Host's `LlmAdapter` base class, or a local stand-in with the same defaults.
|
|
34
|
+
*
|
|
35
|
+
* The Host provides `@deepseek-ai/dsh-llm` at runtime and resolves it for loaded
|
|
36
|
+
* plugins; a checkout run on its own (tests, a probe script) has no such package.
|
|
37
|
+
* Extending the base MATTERS — it carries `imageRequestPricing` and
|
|
38
|
+
* `imageRequestTarget`, which the Host calls unconditionally. A plain class that
|
|
39
|
+
* merely looks like an adapter registers and streams fine, then dies mid-session
|
|
40
|
+
* the first time some other code path asks for one of them:
|
|
41
|
+
*
|
|
42
|
+
* this.adapters.get(...)?.adapter.imageRequestPricing is not a function
|
|
43
|
+
*
|
|
44
|
+
* Hence: use the real base when it resolves, and fall back to an equivalent one
|
|
45
|
+
* so the plugin still runs outside the Host. The two are interchangeable because
|
|
46
|
+
* every method the fallback defines is the same default the Host's base defines.
|
|
47
|
+
*/
|
|
48
|
+
let LlmAdapter
|
|
49
|
+
try {
|
|
50
|
+
;({ LlmAdapter } = await import('@deepseek-ai/dsh-llm'))
|
|
51
|
+
} catch {
|
|
52
|
+
/**
|
|
53
|
+
* The Host base class's defaults, restated.
|
|
54
|
+
*
|
|
55
|
+
* A provider that prices no images and has no per-image target reports exactly
|
|
56
|
+
* that, and a provider with no retry opinion defers to the Host's default.
|
|
57
|
+
*/
|
|
58
|
+
LlmAdapter = class {
|
|
59
|
+
providerInfo(provider) {
|
|
60
|
+
return { id: provider, name: provider }
|
|
61
|
+
}
|
|
62
|
+
providerRetryPolicy() {
|
|
63
|
+
return undefined
|
|
64
|
+
}
|
|
65
|
+
imageRequestPricing() {
|
|
66
|
+
return undefined
|
|
67
|
+
}
|
|
68
|
+
imageRequestTarget() {
|
|
69
|
+
return { width: 0, height: 0, maxBytes: 0 }
|
|
70
|
+
}
|
|
71
|
+
listModels() {
|
|
72
|
+
return []
|
|
73
|
+
}
|
|
74
|
+
resolveModel(provider, model) {
|
|
75
|
+
return { provider, id: model, name: model }
|
|
76
|
+
}
|
|
77
|
+
prepareCall() {
|
|
78
|
+
throw new Error('prepareCall must be implemented by the adapter')
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Provider route this adapter owns. */
|
|
84
|
+
export const PROVIDER_ID = 'codearts'
|
|
85
|
+
|
|
86
|
+
/** The name the model selector shows for this route. */
|
|
87
|
+
export const PROVIDER_NAME = '码道 CodeArts'
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Reasoning level ids the Host understands, mapped to what the gateway reads.
|
|
91
|
+
*
|
|
92
|
+
* The gateway reads `reasoning_effort` and ignores anything else, so a level
|
|
93
|
+
* only works when it maps to a value in this table.
|
|
94
|
+
*/
|
|
95
|
+
const REASONING_LEVELS = {
|
|
96
|
+
off: 'none',
|
|
97
|
+
low: 'low',
|
|
98
|
+
medium: 'medium',
|
|
99
|
+
high: 'high',
|
|
100
|
+
max: 'max',
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Reasoning effort ids the Host offers, in display order. */
|
|
104
|
+
const HOST_EFFORT_ORDER = ['off', 'low', 'medium', 'high', 'max']
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Build the adapter.
|
|
108
|
+
*
|
|
109
|
+
* @param {AdapterDeps} deps adapter dependencies.
|
|
110
|
+
* @returns {any} an `LlmAdapter`-compatible instance.
|
|
111
|
+
*/
|
|
112
|
+
export function createCodeArtsAdapter(deps) {
|
|
113
|
+
return new CodeArtsAdapter(deps)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The adapter.
|
|
118
|
+
*
|
|
119
|
+
* EXTENDS `LlmAdapter`, and that is not decoration. The base carries methods the
|
|
120
|
+
* Host calls unconditionally — `imageRequestPricing` and `imageRequestTarget`
|
|
121
|
+
* among them — with sane defaults for a provider that prices no images. A plain
|
|
122
|
+
* class that merely *looks* like an adapter passes registration and streams
|
|
123
|
+
* fine, then dies mid-session the first time any other code path asks for one of
|
|
124
|
+
* those methods: `this.adapters.get(...)?.adapter.imageRequestPricing is not a
|
|
125
|
+
* function`.
|
|
126
|
+
*/
|
|
127
|
+
class CodeArtsAdapter extends LlmAdapter {
|
|
128
|
+
/**
|
|
129
|
+
* @param {AdapterDeps} deps adapter dependencies.
|
|
130
|
+
*/
|
|
131
|
+
constructor(deps) {
|
|
132
|
+
// `super()` first: this class extends the Host's base, and a derived
|
|
133
|
+
// constructor may not touch `this` before it runs.
|
|
134
|
+
super()
|
|
135
|
+
this.deps = deps
|
|
136
|
+
this.clients = new Map()
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Human-readable route metadata. */
|
|
140
|
+
providerInfo(provider) {
|
|
141
|
+
return { id: provider, name: PROVIDER_NAME }
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The provider-owned retry policy.
|
|
146
|
+
*
|
|
147
|
+
* A rejected request body is never worth retrying: the same bytes produce
|
|
148
|
+
* the same 400 whoever sends them. Transport failures and 5xx are.
|
|
149
|
+
*/
|
|
150
|
+
providerRetryPolicy() {
|
|
151
|
+
return {
|
|
152
|
+
mode: 'normal',
|
|
153
|
+
maxRetries: 2,
|
|
154
|
+
retryableCodes: ['REQUEST_FAILED', 'PROVIDER_ERROR', 'RATE_LIMITED'],
|
|
155
|
+
initialDelayMs: 1000,
|
|
156
|
+
maxDelayMs: 15_000,
|
|
157
|
+
jitterRatio: 0.2,
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The catalog this route advertises. */
|
|
162
|
+
async listModels(provider) {
|
|
163
|
+
if (provider !== PROVIDER_ID) return []
|
|
164
|
+
// The SELECTION, not the raw roster: a model the user unticked must leave
|
|
165
|
+
// the picker, and one they ticked must appear even if the upstream added it
|
|
166
|
+
// after the last refresh.
|
|
167
|
+
const catalog = this.deps.catalog
|
|
168
|
+
const models = typeof catalog.visible === 'function' ? catalog.visible() : catalog.models()
|
|
169
|
+
return models.map((model) => this.#modelInfo(provider, this.#effective(model)))
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Resolve one model's exact metadata. */
|
|
173
|
+
async resolveModel(provider, modelId) {
|
|
174
|
+
if (provider !== PROVIDER_ID) {
|
|
175
|
+
throw new GatewayError(`码道适配器不服务于 provider "${provider}"`)
|
|
176
|
+
}
|
|
177
|
+
const model = this.#effective(this.deps.catalog.find(modelId))
|
|
178
|
+
if (!model) {
|
|
179
|
+
// The Host accepts unlisted model ids for routing, but there is nothing
|
|
180
|
+
// to declare about one, so answer with the bare identity rather than
|
|
181
|
+
// inventing capabilities.
|
|
182
|
+
return { provider, id: modelId, name: modelId }
|
|
183
|
+
}
|
|
184
|
+
return {
|
|
185
|
+
...this.#modelInfo(provider, model),
|
|
186
|
+
context: model.context > 0 ? { contextWindow: model.context } : undefined,
|
|
187
|
+
defaultMaxTokens: model.maxOutput,
|
|
188
|
+
...(this.#reasoningInfo(model) ? { reasoning: this.#reasoningInfo(model) } : {}),
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Bind model metadata and dispatch to one adapter generation. */
|
|
193
|
+
async prepareCall(provider, modelId) {
|
|
194
|
+
const resolved = await this.resolveModel(provider, modelId)
|
|
195
|
+
return {
|
|
196
|
+
model: resolved,
|
|
197
|
+
stream: (options) => this.stream(options),
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Stream one call.
|
|
203
|
+
*
|
|
204
|
+
* Emits the block lifecycle explicitly: the Host's assembler is tolerant of
|
|
205
|
+
* delta-only streams, but starting and closing each block keeps the
|
|
206
|
+
* transcript correct when text and reasoning interleave.
|
|
207
|
+
*
|
|
208
|
+
* @param {any} options Host generation options.
|
|
209
|
+
* @returns {AsyncGenerator<any>} Host stream chunks.
|
|
210
|
+
*/
|
|
211
|
+
async *stream(options) {
|
|
212
|
+
const startedAt = Date.now()
|
|
213
|
+
const generation = this.deps.usage?.generation()
|
|
214
|
+
let status = 'aborted'
|
|
215
|
+
let inputTokens = null, outputTokens = null
|
|
216
|
+
try {
|
|
217
|
+
for await (const chunk of this.#stream(options)) {
|
|
218
|
+
if (chunk.type === 'usage') {
|
|
219
|
+
inputTokens = chunk.usage?.inputTokens ?? inputTokens
|
|
220
|
+
outputTokens = chunk.usage?.outputTokens ?? outputTokens
|
|
221
|
+
}
|
|
222
|
+
if (chunk.type === 'finish') status = chunk.reason?.kind === 'error' ? 'error' : chunk.reason?.kind === 'aborted' ? 'aborted' : 'success'
|
|
223
|
+
yield chunk
|
|
224
|
+
}
|
|
225
|
+
} catch (error) {
|
|
226
|
+
status = error?.name === 'AbortError' ? 'aborted' : 'error'
|
|
227
|
+
throw error
|
|
228
|
+
} finally {
|
|
229
|
+
try {
|
|
230
|
+
await this.deps.usage?.record({
|
|
231
|
+
at: startedAt, model: this.deps.catalog.resolveId?.(options.model) ?? options.model,
|
|
232
|
+
durationMs: Date.now() - startedAt, inputTokens, outputTokens, status,
|
|
233
|
+
}, generation)
|
|
234
|
+
} catch { /* A local history write must never fail a model request. */ }
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async *#stream(options) {
|
|
239
|
+
const model = this.#effective(this.deps.catalog.find(options.model))
|
|
240
|
+
const client = await this.#client()
|
|
241
|
+
|
|
242
|
+
/** @type {number} */ let nextIndex = 0
|
|
243
|
+
/** Open text block index, or undefined when none is open. */
|
|
244
|
+
let textIndex
|
|
245
|
+
/** Open reasoning block index. */
|
|
246
|
+
let reasoningIndex
|
|
247
|
+
/** Open tool-call blocks, keyed by the upstream call index. */
|
|
248
|
+
const toolBlocks = new Map()
|
|
249
|
+
/** Tool-call ids by upstream index, so later argument frames match. */
|
|
250
|
+
const toolIds = new Map()
|
|
251
|
+
// The Host assembler treats a `block-end` block as authoritative: it
|
|
252
|
+
// REPLACES whatever the deltas accumulated. So each close must echo the
|
|
253
|
+
// content this adapter streamed, or the block ends up empty.
|
|
254
|
+
let textContent = ''
|
|
255
|
+
let reasoningContent = ''
|
|
256
|
+
/** Accumulated arguments and names, keyed by upstream call index. */
|
|
257
|
+
const toolArgs = new Map()
|
|
258
|
+
const toolNames = new Map()
|
|
259
|
+
let sawToolCall = false
|
|
260
|
+
/** Finish reason seen on the wire, emitted once the stream settles. */
|
|
261
|
+
let pendingFinish
|
|
262
|
+
|
|
263
|
+
const closeAll = function* () {
|
|
264
|
+
if (textIndex !== undefined) {
|
|
265
|
+
yield { type: 'block-end', index: textIndex, block: { type: 'text', text: textContent } }
|
|
266
|
+
textIndex = undefined
|
|
267
|
+
}
|
|
268
|
+
if (reasoningIndex !== undefined) {
|
|
269
|
+
yield {
|
|
270
|
+
type: 'block-end',
|
|
271
|
+
index: reasoningIndex,
|
|
272
|
+
block: { type: 'reasoning', text: reasoningContent },
|
|
273
|
+
}
|
|
274
|
+
reasoningIndex = undefined
|
|
275
|
+
}
|
|
276
|
+
for (const [callIndex, blockIndex] of toolBlocks) {
|
|
277
|
+
yield {
|
|
278
|
+
type: 'block-end',
|
|
279
|
+
index: blockIndex,
|
|
280
|
+
block: {
|
|
281
|
+
type: 'tool-call',
|
|
282
|
+
id: toolIds.get(callIndex) ?? `tool-${callIndex}`,
|
|
283
|
+
name: toolNames.get(callIndex) ?? '',
|
|
284
|
+
arguments: toolArgs.get(callIndex) ?? '',
|
|
285
|
+
},
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
toolBlocks.clear()
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
try {
|
|
292
|
+
const messages = await toGatewayMessages(options, this.deps.attachments)
|
|
293
|
+
const request = {
|
|
294
|
+
// Never the display string: the gateway answers a `name · ratio` value
|
|
295
|
+
// with HTTP 200 and an empty body, which surfaces only as "returned a
|
|
296
|
+
// completed response with no content".
|
|
297
|
+
model:
|
|
298
|
+
typeof this.deps.catalog?.resolveId === 'function'
|
|
299
|
+
? this.deps.catalog.resolveId(options.model)
|
|
300
|
+
: options.model,
|
|
301
|
+
messages,
|
|
302
|
+
maxTokens: options.maxTokens ?? model?.maxOutput,
|
|
303
|
+
temperature: options.temperature,
|
|
304
|
+
...(options.stop ? { stop: options.stop } : {}),
|
|
305
|
+
...(options.tools && options.tools.length > 0
|
|
306
|
+
? { tools: options.tools.map(toGatewayTool) }
|
|
307
|
+
: {}),
|
|
308
|
+
...(options.reasoningEffort ? { reasoningEffort: String(options.reasoningEffort) } : {}),
|
|
309
|
+
signal: options.signal,
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
await client.heartbeat('busy')
|
|
313
|
+
|
|
314
|
+
for await (const event of client.streamChat(request)) {
|
|
315
|
+
if (options.signal?.aborted) {
|
|
316
|
+
yield* closeAll()
|
|
317
|
+
yield { type: 'finish', reason: { kind: 'aborted', failure: { message: '已取消', code: 'ABORTED' } } }
|
|
318
|
+
return
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (event.reasoning) {
|
|
322
|
+
if (reasoningIndex === undefined) {
|
|
323
|
+
reasoningIndex = nextIndex++
|
|
324
|
+
yield { type: 'block-start', index: reasoningIndex, blockType: 'reasoning' }
|
|
325
|
+
}
|
|
326
|
+
reasoningContent += event.reasoning
|
|
327
|
+
yield { type: 'reasoning-delta', index: reasoningIndex, text: event.reasoning }
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (event.text) {
|
|
331
|
+
if (textIndex === undefined) {
|
|
332
|
+
textIndex = nextIndex++
|
|
333
|
+
yield { type: 'block-start', index: textIndex, blockType: 'text' }
|
|
334
|
+
}
|
|
335
|
+
textContent += event.text
|
|
336
|
+
yield { type: 'text-delta', index: textIndex, text: event.text }
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (event.toolCalls) {
|
|
340
|
+
sawToolCall = true
|
|
341
|
+
for (const delta of event.toolCalls) {
|
|
342
|
+
const callIndex = typeof delta.index === 'number' ? delta.index : 0
|
|
343
|
+
if (delta.id) toolIds.set(callIndex, delta.id)
|
|
344
|
+
|
|
345
|
+
let blockIndex = toolBlocks.get(callIndex)
|
|
346
|
+
if (blockIndex === undefined) {
|
|
347
|
+
blockIndex = nextIndex++
|
|
348
|
+
toolBlocks.set(callIndex, blockIndex)
|
|
349
|
+
// Close a pending text block first: a tool call terminates the
|
|
350
|
+
// assistant's visible text for that turn.
|
|
351
|
+
if (textIndex !== undefined) {
|
|
352
|
+
yield {
|
|
353
|
+
type: 'block-end',
|
|
354
|
+
index: textIndex,
|
|
355
|
+
block: { type: 'text', text: textContent },
|
|
356
|
+
}
|
|
357
|
+
textIndex = undefined
|
|
358
|
+
}
|
|
359
|
+
yield { type: 'block-start', index: blockIndex, blockType: 'tool-call' }
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const id = toolIds.get(callIndex) ?? `tool-${callIndex}`
|
|
363
|
+
const name = delta.function?.name
|
|
364
|
+
const argumentsDelta = delta.function?.arguments ?? ''
|
|
365
|
+
if (name) toolNames.set(callIndex, name)
|
|
366
|
+
if (argumentsDelta) toolArgs.set(callIndex, (toolArgs.get(callIndex) ?? '') + argumentsDelta)
|
|
367
|
+
if (name || argumentsDelta) {
|
|
368
|
+
yield {
|
|
369
|
+
type: 'tool-call-delta',
|
|
370
|
+
index: blockIndex,
|
|
371
|
+
id,
|
|
372
|
+
...(name ? { name } : {}),
|
|
373
|
+
argumentsDelta,
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if (event.usage) {
|
|
380
|
+
yield {
|
|
381
|
+
type: 'usage',
|
|
382
|
+
usage: {
|
|
383
|
+
inputTokens: event.usage.inputTokens,
|
|
384
|
+
outputTokens: event.usage.outputTokens,
|
|
385
|
+
...(event.usage.totalTokens ? { totalTokens: event.usage.totalTokens } : {}),
|
|
386
|
+
},
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// The gateway's own order is text/reasoning, then `finish`, then the
|
|
391
|
+
// `usage` frame, then `[DONE]`. Returning on `finish` would therefore
|
|
392
|
+
// discard the token counts, so the reason is recorded and the finish
|
|
393
|
+
// chunk is emitted once the stream is actually over.
|
|
394
|
+
if (event.finish) {
|
|
395
|
+
pendingFinish = mapFinishReason(event.finish, sawToolCall)
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
if (event.done) break
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// A completed reply with reasoning but no visible text and no tool call
|
|
402
|
+
// is the failure mode this gateway produces when the output budget runs
|
|
403
|
+
// out inside the thinking phase: reasoning and content share
|
|
404
|
+
// `max_tokens`. The Host would otherwise report only "returned a
|
|
405
|
+
// completed response with no content", which names no cause and suggests
|
|
406
|
+
// no fix. Say what happened instead.
|
|
407
|
+
if (
|
|
408
|
+
textContent === '' &&
|
|
409
|
+
reasoningContent !== '' &&
|
|
410
|
+
!sawToolCall &&
|
|
411
|
+
(pendingFinish === undefined || pendingFinish.kind === 'stop')
|
|
412
|
+
) {
|
|
413
|
+
const budget = request.maxTokens ?? 0
|
|
414
|
+
const note =
|
|
415
|
+
`(模型把输出预算都用在思考上了:思考 ${reasoningContent.length} 字,正文 0 字,` +
|
|
416
|
+
`本次 max_tokens=${budget}。思考与正文共用这个预算——提高 max_tokens(几百以上)通常即可恢复。)`
|
|
417
|
+
if (textIndex === undefined) {
|
|
418
|
+
textIndex = nextIndex++
|
|
419
|
+
yield { type: 'block-start', index: textIndex, blockType: 'text' }
|
|
420
|
+
}
|
|
421
|
+
textContent = note
|
|
422
|
+
yield { type: 'text-delta', index: textIndex, text: note }
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// Settle: close open blocks, then finish with whatever reason arrived.
|
|
426
|
+
yield* closeAll()
|
|
427
|
+
yield {
|
|
428
|
+
type: 'finish',
|
|
429
|
+
reason: pendingFinish ?? (sawToolCall ? { kind: 'tool-calls' } : { kind: 'stop' }),
|
|
430
|
+
}
|
|
431
|
+
} catch (error) {
|
|
432
|
+
yield* closeAll()
|
|
433
|
+
|
|
434
|
+
const failure = describeFailure(error)
|
|
435
|
+
// A cancellation surfaces as an aborted finish; anything else as an error.
|
|
436
|
+
yield {
|
|
437
|
+
type: 'finish',
|
|
438
|
+
reason:
|
|
439
|
+
error?.name === 'AbortError'
|
|
440
|
+
? { kind: 'aborted', failure }
|
|
441
|
+
: { kind: 'error', failure },
|
|
442
|
+
}
|
|
443
|
+
} finally {
|
|
444
|
+
// Releasing the concurrency slot matters even on failure: the gateway
|
|
445
|
+
// allows only a few sessions, and a held slot blocks every new turn.
|
|
446
|
+
void client.heartbeat('idle').catch(() => {})
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/** Build (or reuse) the gateway client for the current credential. */
|
|
451
|
+
async #client() {
|
|
452
|
+
const resolved = await this.deps.credentials.resolve()
|
|
453
|
+
const key = resolved.key
|
|
454
|
+
const signature = `${key.ak}:${key.securityToken}`
|
|
455
|
+
const existing = this.clients.get(signature)
|
|
456
|
+
if (existing) return existing
|
|
457
|
+
|
|
458
|
+
// A renewed credential replaces the client: the old one signs with a
|
|
459
|
+
// dead token.
|
|
460
|
+
this.clients.clear()
|
|
461
|
+
const client = this.deps.createClient
|
|
462
|
+
? this.deps.createClient(key)
|
|
463
|
+
: new MadaoClient({ credentials: key, gateway: this.deps.gateway?.() })
|
|
464
|
+
this.clients.set(signature, client)
|
|
465
|
+
return client
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Apply the user's capability overrides to one model, when the catalog can.
|
|
470
|
+
*
|
|
471
|
+
* Optional on purpose: `effective` is a later addition, and a catalog that
|
|
472
|
+
* predates it (a test double, a downgraded host) must degrade to the plain
|
|
473
|
+
* model rather than break every request.
|
|
474
|
+
*/
|
|
475
|
+
#effective(model) {
|
|
476
|
+
const catalog = this.deps.catalog
|
|
477
|
+
return typeof catalog?.effective === 'function' ? catalog.effective(model) : model
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/** Convert one catalog entry into Host model metadata. */
|
|
481
|
+
#modelInfo(provider, model) {
|
|
482
|
+
return {
|
|
483
|
+
provider,
|
|
484
|
+
id: model.id,
|
|
485
|
+
name: model.ratio ? `${model.name} · ${model.ratio}` : model.name,
|
|
486
|
+
...(model.description ? { description: model.description } : {}),
|
|
487
|
+
...(model.supportsImages ? { inputModalities: ['text', 'image'] } : { inputModalities: ['text'] }),
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/** Build the Host reasoning descriptor for a model that declares levels. */
|
|
492
|
+
#reasoningInfo(model) {
|
|
493
|
+
if (!model.strengths || model.strengths.length === 0) return undefined
|
|
494
|
+
const levels = new Set(model.strengths)
|
|
495
|
+
// `allowOff` is the user's answer to "should this model offer a 'no thinking'
|
|
496
|
+
// level". Undefined keeps the upstream answer; false withholds it, because a
|
|
497
|
+
// model that refuses `off` fails the whole request for anyone who picks it.
|
|
498
|
+
const offerOff = model.allowOff !== false
|
|
499
|
+
return {
|
|
500
|
+
efforts: HOST_EFFORT_ORDER.filter(
|
|
501
|
+
(level) => (level === 'off' ? offerOff : levels.has(level)),
|
|
502
|
+
).map(
|
|
503
|
+
(level) => ({
|
|
504
|
+
id: level,
|
|
505
|
+
name: REASONING_LABELS[level] ?? level,
|
|
506
|
+
}),
|
|
507
|
+
),
|
|
508
|
+
defaultEffort: model.defaultStrength && levels.has(model.defaultStrength)
|
|
509
|
+
? model.defaultStrength
|
|
510
|
+
: [...levels][0],
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/** Display names for the reasoning levels. */
|
|
516
|
+
const REASONING_LABELS = {
|
|
517
|
+
off: '关闭思考',
|
|
518
|
+
low: '低',
|
|
519
|
+
medium: '中',
|
|
520
|
+
high: '高',
|
|
521
|
+
max: '最高',
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Convert Host messages into OpenAI chat messages.
|
|
526
|
+
*
|
|
527
|
+
* Three conversions are load-bearing:
|
|
528
|
+
*
|
|
529
|
+
* • `developer` becomes `system`. The gateway accepts only
|
|
530
|
+
* system/user/assistant/tool; a `developer` message is answered with an
|
|
531
|
+
* empty 200 response, which the Host can only report as an empty reply.
|
|
532
|
+
* • Tool results attach to the tool call they answer, as `tool` messages.
|
|
533
|
+
* • Images become `image_url` parts carrying a data URL — the gateway
|
|
534
|
+
* recognises that OpenAI shape and not the client's internal `file` one.
|
|
535
|
+
* The bytes come from the attachment service, because a durable image
|
|
536
|
+
* block holds a reference, not a payload.
|
|
537
|
+
*
|
|
538
|
+
* @param {any} options Host generation options.
|
|
539
|
+
* @param {any} [attachments] the Host attachment service, when mounted.
|
|
540
|
+
* @returns {Promise<Array<object>>} gateway messages.
|
|
541
|
+
*/
|
|
542
|
+
async function toGatewayMessages(options, attachments) {
|
|
543
|
+
/** @type {Array<object>} */
|
|
544
|
+
const messages = []
|
|
545
|
+
|
|
546
|
+
if (typeof options.system === 'string' && options.system !== '') {
|
|
547
|
+
messages.push({ role: 'system', content: options.system })
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
for (const message of options.messages ?? []) {
|
|
551
|
+
switch (message.role) {
|
|
552
|
+
case 'system':
|
|
553
|
+
case 'developer':
|
|
554
|
+
// The gateway silently ignores `developer`, so it must be downgraded
|
|
555
|
+
// rather than dropped: it carries the system prompt.
|
|
556
|
+
messages.push({ role: 'system', content: flattenText(message.content) })
|
|
557
|
+
break
|
|
558
|
+
|
|
559
|
+
case 'user': {
|
|
560
|
+
const content = await toGatewayContent(message.content, attachments, options.signal)
|
|
561
|
+
if (content !== '') messages.push({ role: 'user', content })
|
|
562
|
+
break
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
case 'assistant': {
|
|
566
|
+
const text = flattenText(message.content)
|
|
567
|
+
const toolCalls = (message.content ?? [])
|
|
568
|
+
.filter((block) => block?.type === 'tool-call')
|
|
569
|
+
.map((block) => ({
|
|
570
|
+
id: block.id,
|
|
571
|
+
type: 'function',
|
|
572
|
+
function: { name: block.name, arguments: block.arguments },
|
|
573
|
+
}))
|
|
574
|
+
if (text === '' && toolCalls.length === 0) break
|
|
575
|
+
messages.push({
|
|
576
|
+
role: 'assistant',
|
|
577
|
+
...(text !== '' ? { content: text } : { content: null }),
|
|
578
|
+
...(toolCalls.length > 0 ? { tool_calls: toolCalls } : {}),
|
|
579
|
+
})
|
|
580
|
+
break
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
case 'tool':
|
|
584
|
+
messages.push({
|
|
585
|
+
role: 'tool',
|
|
586
|
+
tool_call_id: message.toolCallId ?? message.source?.callId,
|
|
587
|
+
content: flattenText(message.content),
|
|
588
|
+
})
|
|
589
|
+
break
|
|
590
|
+
|
|
591
|
+
default:
|
|
592
|
+
break
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
return messages
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* Flatten Host content blocks into plain text.
|
|
601
|
+
* @param {Array<any>|string|undefined} content content blocks.
|
|
602
|
+
* @returns {string} concatenated text.
|
|
603
|
+
*/
|
|
604
|
+
function flattenText(content) {
|
|
605
|
+
if (typeof content === 'string') return content
|
|
606
|
+
if (!Array.isArray(content)) return ''
|
|
607
|
+
return content
|
|
608
|
+
.filter((block) => block?.type === 'text')
|
|
609
|
+
.map((block) => block.text ?? '')
|
|
610
|
+
.join('')
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* Convert Host content into the gateway's content form.
|
|
615
|
+
*
|
|
616
|
+
* All-text content collapses to a plain string, which is both cheaper on the
|
|
617
|
+
* wire and the only form some models accept.
|
|
618
|
+
*
|
|
619
|
+
* @param {Array<any>|string|undefined} content content blocks.
|
|
620
|
+
* @param {any} [attachments] the Host attachment service.
|
|
621
|
+
* @param {AbortSignal} [signal] cancellation.
|
|
622
|
+
* @returns {Promise<string|Array<object>>} gateway content.
|
|
623
|
+
*/
|
|
624
|
+
async function toGatewayContent(content, attachments, signal) {
|
|
625
|
+
if (typeof content === 'string') return content
|
|
626
|
+
if (!Array.isArray(content)) return ''
|
|
627
|
+
|
|
628
|
+
const parts = []
|
|
629
|
+
for (const block of content) {
|
|
630
|
+
if (block?.type === 'text' && typeof block.text === 'string') {
|
|
631
|
+
parts.push({ type: 'text', text: block.text })
|
|
632
|
+
} else if (block?.type === 'image' && block.attachment) {
|
|
633
|
+
// A durable image block carries a reference; the bytes come from the
|
|
634
|
+
// attachment service as a data URL the gateway understands.
|
|
635
|
+
const encoded = await encodeImage(block.attachment, attachments, signal)
|
|
636
|
+
if (encoded) parts.push({ type: 'image_url', image_url: { url: encoded } })
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
if (parts.length === 0) return ''
|
|
641
|
+
if (parts.every((part) => part.type === 'text')) {
|
|
642
|
+
return parts.map((part) => part.text).join('')
|
|
643
|
+
}
|
|
644
|
+
return parts
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/**
|
|
648
|
+
* Encode one durable image as a data URL.
|
|
649
|
+
*
|
|
650
|
+
* An image that cannot be read is reported to the model as a short text note
|
|
651
|
+
* rather than silently dropped: a missing picture must not look like the
|
|
652
|
+
* question was never sent with one.
|
|
653
|
+
*
|
|
654
|
+
* @param {any} ref the image attachment reference.
|
|
655
|
+
* @param {any} attachments the Host attachment service.
|
|
656
|
+
* @param {AbortSignal} [signal] cancellation.
|
|
657
|
+
* @returns {Promise<string|null>} the data URL, or null when unreadable.
|
|
658
|
+
*/
|
|
659
|
+
async function encodeImage(ref, attachments, signal) {
|
|
660
|
+
try {
|
|
661
|
+
if (typeof attachments?.readImageRequest === 'function') {
|
|
662
|
+
// Ask for the image at its stored size: the gateway does its own
|
|
663
|
+
// resizing, and a second downscale here would only lose detail.
|
|
664
|
+
const read = await attachments.readImageRequest(
|
|
665
|
+
ref,
|
|
666
|
+
{ width: ref.width, height: ref.height, maxBytes: ref.bytes },
|
|
667
|
+
signal,
|
|
668
|
+
)
|
|
669
|
+
return `data:${read.mediaType};base64,${Buffer.from(read.data).toString('base64')}`
|
|
670
|
+
}
|
|
671
|
+
if (typeof attachments?.readImage === 'function') {
|
|
672
|
+
const read = await attachments.readImage(ref, signal)
|
|
673
|
+
return `data:${ref.mediaType};base64,${Buffer.from(read.data).toString('base64')}`
|
|
674
|
+
}
|
|
675
|
+
} catch {
|
|
676
|
+
return null
|
|
677
|
+
}
|
|
678
|
+
return null
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* Convert one Host tool schema into the gateway's function-tool form.
|
|
683
|
+
* @param {any} tool Host tool schema.
|
|
684
|
+
* @returns {object} gateway tool schema.
|
|
685
|
+
*/
|
|
686
|
+
function toGatewayTool(tool) {
|
|
687
|
+
return {
|
|
688
|
+
type: 'function',
|
|
689
|
+
function: {
|
|
690
|
+
name: tool.name,
|
|
691
|
+
description: tool.description ?? '',
|
|
692
|
+
parameters: tool.parameters ?? { type: 'object', properties: {} },
|
|
693
|
+
},
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* Map an upstream finish reason onto the Host vocabulary.
|
|
699
|
+
* @param {string} finish upstream reason.
|
|
700
|
+
* @param {boolean} sawToolCall whether any tool call was streamed.
|
|
701
|
+
* @returns {any} Host finish reason.
|
|
702
|
+
*/
|
|
703
|
+
function mapFinishReason(finish, sawToolCall) {
|
|
704
|
+
switch (finish) {
|
|
705
|
+
case 'tool_calls':
|
|
706
|
+
case 'function_call':
|
|
707
|
+
return { kind: 'tool-calls' }
|
|
708
|
+
case 'length':
|
|
709
|
+
case 'max_tokens':
|
|
710
|
+
return { kind: 'max-tokens' }
|
|
711
|
+
case 'content_filter':
|
|
712
|
+
return { kind: 'stop' }
|
|
713
|
+
default:
|
|
714
|
+
// Some gateways report `stop` even for a tool-call turn.
|
|
715
|
+
return sawToolCall ? { kind: 'tool-calls' } : { kind: 'stop' }
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* Turn a thrown error into a Host failure.
|
|
721
|
+
*
|
|
722
|
+
* The gateway's queue codes are translated rather than relayed, because the
|
|
723
|
+
* upstream text names a business code and not what the user should do.
|
|
724
|
+
* @param {any} error the thrown error.
|
|
725
|
+
* @returns {{message: string, code: string, status?: number}} Host failure facts.
|
|
726
|
+
*/
|
|
727
|
+
function describeFailure(error) {
|
|
728
|
+
if (error instanceof GatewayError) {
|
|
729
|
+
return {
|
|
730
|
+
message: error.message,
|
|
731
|
+
code: error.code ?? (error.status ? 'PROVIDER_ERROR' : 'REQUEST_FAILED'),
|
|
732
|
+
...(error.status ? { status: error.status } : {}),
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
if (error?.name === 'AbortError') {
|
|
736
|
+
return { message: '已取消', code: 'ABORTED' }
|
|
737
|
+
}
|
|
738
|
+
return { message: error?.message ?? String(error), code: 'REQUEST_FAILED' }
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/** Re-export so the Host half can name the identity helper without a second import. */
|
|
742
|
+
export { parseIdentity }
|