dsh-agent-sdk-bridge 1.0.0 → 1.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/DEVELOPMENT.md +62 -3
- package/FAQ.md +58 -0
- package/README.md +53 -24
- package/client.js +2411 -1802
- package/cordis.patch.yml +4 -3
- package/lib/index.js +3151 -250
- package/lib/types/api.d.ts +5 -2
- package/lib/types/brand.d.ts +26 -3
- package/lib/types/codebuddy.d.ts +12 -1
- package/lib/types/config.d.ts +41 -1
- package/lib/types/index.d.ts +21 -0
- package/lib/types/minimax-adapter.d.ts +86 -0
- package/lib/types/minimax-cli.d.ts +69 -0
- package/lib/types/minimax.d.ts +493 -0
- package/package.json +7 -2
- package/src/api.ts +120 -32
- package/src/brand.ts +49 -5
- package/src/codebuddy.ts +55 -4
- package/src/config.ts +53 -1
- package/src/index.ts +50 -4
- package/src/minimax-adapter.ts +370 -0
- package/src/minimax-cli.ts +276 -0
- package/src/minimax.ts +2413 -0
package/src/index.ts
CHANGED
|
@@ -29,10 +29,12 @@ import type { ServerResponse, IncomingMessage } from 'node:http'
|
|
|
29
29
|
import type { AttachmentStore, ImageAttachmentRef } from '@deepseek-ai/dsh-attachment'
|
|
30
30
|
import { QoderAdapter, QODER_PROVIDER, QODER_INTL_PROVIDER } from './adapter.ts'
|
|
31
31
|
import { CodeBuddyAdapter, CODEBUDDY_PROVIDER } from './codebuddy-adapter.ts'
|
|
32
|
+
import { MinimaxAdapter, MINIMAX_PROVIDER } from './minimax-adapter.ts'
|
|
32
33
|
import { API_PREFIX, handleApi } from './api.ts'
|
|
33
34
|
import type { BrandId } from './brand.ts'
|
|
34
35
|
import { BRANDS } from './brand.ts'
|
|
35
36
|
import { stopSetup } from './setup.ts'
|
|
37
|
+
import { stopMinimaxSetup } from './minimax-cli.ts'
|
|
36
38
|
import type { LiveConfig, ParsedConfig } from './config.ts'
|
|
37
39
|
import { Config as ConfigSchema, liveConfig } from './config.ts'
|
|
38
40
|
|
|
@@ -77,6 +79,24 @@ declare module '@deepseek-ai/cordis' {
|
|
|
77
79
|
export { QoderAdapter, QODER_PROVIDER, QODER_INTL_PROVIDER, cliContextWindow, pickContextWindow } from './adapter.ts'
|
|
78
80
|
export type { ImageRequestReader, QoderAdapterOptions } from './adapter.ts'
|
|
79
81
|
export { CodeBuddyAdapter, CODEBUDDY_PROVIDER, resolveCodeBuddyModelId } from './codebuddy-adapter.ts'
|
|
82
|
+
export { MinimaxAdapter, MINIMAX_PROVIDER, resolveMinimaxModelId } from './minimax-adapter.ts'
|
|
83
|
+
export {
|
|
84
|
+
MinimaxAcp, MinimaxAuth, MinimaxCatalog, MinimaxSession, MinimaxSessionManager,
|
|
85
|
+
minimaxColdStream, minimaxDataDir, minimaxAuthStatePath, minimaxCliCommand,
|
|
86
|
+
isHostMcpTool, hostMcpToolName, minimaxModelsFromConfigOptions,
|
|
87
|
+
minimaxUsage, minimaxUsageFromParts, invalidateMinimaxUsage,
|
|
88
|
+
minimaxRegionBlocked, minimaxRegionBlockedCached, minimaxRegionBlockMessage,
|
|
89
|
+
minimaxHostBelongsToOtherRegion, invalidateMinimaxRegionAccess, MinimaxRegionBlockedError,
|
|
90
|
+
MINIMAX_DEFAULT_CONTEXT_WINDOW, MINIMAX_DEFAULT_MAX_TOKENS, MINIMAX_USAGE_CACHE_TTL_MS,
|
|
91
|
+
} from './minimax.ts'
|
|
92
|
+
export type {
|
|
93
|
+
MinimaxAccountInfo, MinimaxAuthStatus, MinimaxCreditInfo, MinimaxMcpToolResult,
|
|
94
|
+
MinimaxModel, MinimaxPlanInfo, MinimaxQuotaWindow, MinimaxUsageSnapshot,
|
|
95
|
+
} from './minimax.ts'
|
|
96
|
+
export {
|
|
97
|
+
MinimaxCliSetup, readMinimaxSetupStatus, resolveMinimaxCli, startMinimaxSetup, stopMinimaxSetup,
|
|
98
|
+
} from './minimax-cli.ts'
|
|
99
|
+
export type { MinimaxSetupMode, MinimaxSetupStatus, MinimaxCliSpec } from './minimax-cli.ts'
|
|
80
100
|
export type { CodeBuddyAdapterOptions } from './codebuddy-adapter.ts'
|
|
81
101
|
export {
|
|
82
102
|
CodeBuddyAuth, CodeBuddyAccountReader, CodeBuddyCatalog, CodeBuddySession, CodeBuddySessionManager,
|
|
@@ -135,8 +155,8 @@ const REQUEST_IMAGE_MAX_BYTES = 1_048_576
|
|
|
135
155
|
* used, so a change to these fields is the only thing that needs a republish.
|
|
136
156
|
*/
|
|
137
157
|
const STRUCTURAL_FIELDS: ReadonlySet<string> = new Set([
|
|
138
|
-
'cnEnabled', 'intlEnabled', 'codebuddyEnabled',
|
|
139
|
-
'displayName', 'intlDisplayName', 'codebuddyDisplayName',
|
|
158
|
+
'cnEnabled', 'intlEnabled', 'codebuddyEnabled', 'minimaxEnabled',
|
|
159
|
+
'displayName', 'intlDisplayName', 'codebuddyDisplayName', 'minimaxDisplayName',
|
|
140
160
|
])
|
|
141
161
|
|
|
142
162
|
/** The configurable-provider directory rows for the enabled editions. */
|
|
@@ -156,6 +176,9 @@ function directoryFor(live: LiveConfig): {
|
|
|
156
176
|
if (live.codebuddyEnabled()) {
|
|
157
177
|
rows.push({ provider: CODEBUDDY_PROVIDER, displayName: live.codebuddyDisplayName(), settingsNs: NS, settingsPath: [] })
|
|
158
178
|
}
|
|
179
|
+
if (live.minimaxEnabled()) {
|
|
180
|
+
rows.push({ provider: MINIMAX_PROVIDER, displayName: live.minimaxDisplayName(), settingsNs: NS, settingsPath: [] })
|
|
181
|
+
}
|
|
159
182
|
return rows
|
|
160
183
|
}
|
|
161
184
|
|
|
@@ -221,7 +244,7 @@ export function apply(ctx: Context, config: ParsedConfig): void {
|
|
|
221
244
|
* own catalog, warm sessions, account reading and user choices, so a request
|
|
222
245
|
* can never be resolved against another provider's catalog.
|
|
223
246
|
*/
|
|
224
|
-
const adapters: Record<BrandId, QoderAdapter | CodeBuddyAdapter> = {
|
|
247
|
+
const adapters: Record<BrandId, QoderAdapter | CodeBuddyAdapter | MinimaxAdapter> = {
|
|
225
248
|
cn: new QoderAdapter({
|
|
226
249
|
...shared,
|
|
227
250
|
brand: 'cn',
|
|
@@ -256,6 +279,24 @@ export function apply(ctx: Context, config: ParsedConfig): void {
|
|
|
256
279
|
: live.codebuddyGlobalModelContextWindows(),
|
|
257
280
|
modelAliases: live.codebuddyModelAliases,
|
|
258
281
|
}),
|
|
282
|
+
minimax: new MinimaxAdapter({
|
|
283
|
+
maxSessions: live.maxSessions,
|
|
284
|
+
modelCacheTtlMs: live.modelCacheTtlMs,
|
|
285
|
+
catalogFetchTimeoutMs: live.catalogFetchTimeoutMs,
|
|
286
|
+
toolResultTimeoutMs: live.toolResultTimeoutMs,
|
|
287
|
+
abortGraceMs: live.abortGraceMs,
|
|
288
|
+
defaultMaxTokens: live.defaultMaxTokens,
|
|
289
|
+
defaultReasoningEffort: live.defaultReasoningEffort,
|
|
290
|
+
displayName: live.minimaxDisplayName,
|
|
291
|
+
region: live.minimaxRegion,
|
|
292
|
+
disabledModels: region => region === 'cn'
|
|
293
|
+
? live.minimaxCnDisabledModels()
|
|
294
|
+
: live.minimaxGlobalDisabledModels(),
|
|
295
|
+
modelContextWindows: region => region === 'cn'
|
|
296
|
+
? live.minimaxCnModelContextWindows()
|
|
297
|
+
: live.minimaxGlobalModelContextWindows(),
|
|
298
|
+
modelAliases: live.minimaxModelAliases,
|
|
299
|
+
}),
|
|
259
300
|
}
|
|
260
301
|
|
|
261
302
|
ctx.inject(['attachments'], (scope) => {
|
|
@@ -273,6 +314,7 @@ export function apply(ctx: Context, config: ParsedConfig): void {
|
|
|
273
314
|
const enabled = (id: BrandId): boolean => {
|
|
274
315
|
if (id === 'cn') return live.cnEnabled()
|
|
275
316
|
if (id === 'global') return live.intlEnabled()
|
|
317
|
+
if (id === 'minimax') return live.minimaxEnabled()
|
|
276
318
|
return live.codebuddyEnabled()
|
|
277
319
|
}
|
|
278
320
|
|
|
@@ -329,11 +371,15 @@ export function apply(ctx: Context, config: ParsedConfig): void {
|
|
|
329
371
|
adapters.cn.close()
|
|
330
372
|
adapters.global.close()
|
|
331
373
|
;(adapters.codebuddy as CodeBuddyAdapter).close()
|
|
374
|
+
;(adapters.minimax as MinimaxAdapter).close()
|
|
332
375
|
}, 'llm-qoder-cn.sessions')
|
|
333
376
|
|
|
334
377
|
// A CLI helper still running when this plugin is reloaded or removed would
|
|
335
378
|
// otherwise keep writing into a page that can no longer read it.
|
|
336
|
-
ctx.effect(() => () =>
|
|
379
|
+
ctx.effect(() => () => {
|
|
380
|
+
stopSetup()
|
|
381
|
+
stopMinimaxSetup()
|
|
382
|
+
}, 'llm-qoder-cn.setup')
|
|
337
383
|
|
|
338
384
|
// The settings page's data source: the live catalog joined with the user's
|
|
339
385
|
// on/off and window choices. Served from its own route because the catalog
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `MinimaxAdapter`: route the harness LLM seam onto a MiniMax Code account.
|
|
3
|
+
*
|
|
4
|
+
* The ACP agent owns the conversation; this adapter decides what new text to
|
|
5
|
+
* send, which model/effort to select, and how the ACP tool calls map onto the
|
|
6
|
+
* harness's tool seam. One provider serves both login regions; the region is a
|
|
7
|
+
* live config choice and every ACP process is pinned to it.
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-agent-sdk-bridge/minimax-adapter
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { LlmAdapter } from '@deepseek-ai/dsh-llm'
|
|
13
|
+
import type {
|
|
14
|
+
GenerateOptions, LlmModelInfo, LlmProviderInfo, LlmResolvedModelInfo,
|
|
15
|
+
PreparedAdapterCall, RequestMessage, StreamChunk,
|
|
16
|
+
} from '@deepseek-ai/dsh-llm'
|
|
17
|
+
import type { MinimaxRegion } from './brand.ts'
|
|
18
|
+
import {
|
|
19
|
+
MinimaxAuth, MinimaxCatalog, MinimaxMcpServer, MinimaxSessionManager,
|
|
20
|
+
MINIMAX_CACHE_TTL_MS, MINIMAX_DEFAULT_CONTEXT_WINDOW, MINIMAX_DEFAULT_MAX_TOKENS,
|
|
21
|
+
MINIMAX_FETCH_TIMEOUT_MS, invalidateMinimaxRegionAccess, invalidateMinimaxUsage,
|
|
22
|
+
minimaxColdStream, minimaxRegionBlockMessage, minimaxRegionBlocked,
|
|
23
|
+
minimaxRegionBlockedCached, minimaxSignedIn, minimaxUsage,
|
|
24
|
+
} from './minimax.ts'
|
|
25
|
+
import type { MinimaxModel, MinimaxUsageSnapshot } from './minimax.ts'
|
|
26
|
+
import { resolveMinimaxCli } from './minimax-cli.ts'
|
|
27
|
+
import { reasoningInfo, planContinuation } from './adapter.ts'
|
|
28
|
+
import type { Live } from './live.ts'
|
|
29
|
+
import { readLive, readLiveOptional } from './live.ts'
|
|
30
|
+
import { feedToText, renderInitialFeed } from './render.ts'
|
|
31
|
+
|
|
32
|
+
/** The MiniMax Code provider route. */
|
|
33
|
+
export const MINIMAX_PROVIDER = 'minimax-acp'
|
|
34
|
+
|
|
35
|
+
/** Everything the adapter reads lazily from the plugin config. */
|
|
36
|
+
export interface MinimaxAdapterOptions {
|
|
37
|
+
maxSessions?: Live<number>
|
|
38
|
+
modelCacheTtlMs?: Live<number>
|
|
39
|
+
catalogFetchTimeoutMs?: Live<number>
|
|
40
|
+
toolResultTimeoutMs?: Live<number>
|
|
41
|
+
abortGraceMs?: Live<number>
|
|
42
|
+
defaultMaxTokens?: Live<number>
|
|
43
|
+
defaultReasoningEffort?: Live<string | undefined>
|
|
44
|
+
displayName?: Live<string>
|
|
45
|
+
region?: Live<MinimaxRegion>
|
|
46
|
+
/** Per-region deny-lists; the page edits each region's list separately. */
|
|
47
|
+
disabledModels?: (region: MinimaxRegion) => readonly string[]
|
|
48
|
+
/** Per-region context-window overrides, keyed by model id. */
|
|
49
|
+
modelContextWindows?: (region: MinimaxRegion) => Readonly<Record<string, number>>
|
|
50
|
+
modelAliases?: Live<Readonly<Record<string, string>>>
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const NO_MODELS: readonly string[] = []
|
|
54
|
+
const NO_ALIASES: Readonly<Record<string, string>> = {}
|
|
55
|
+
|
|
56
|
+
/** Map a harness-side id onto an ACP model value. */
|
|
57
|
+
export function resolveMinimaxModelId(
|
|
58
|
+
model: string,
|
|
59
|
+
aliases: Readonly<Record<string, string>> = {},
|
|
60
|
+
): string {
|
|
61
|
+
const configured = aliases[model] ?? aliases[model.toLowerCase()]
|
|
62
|
+
if (configured !== undefined && configured.length > 0) return configured
|
|
63
|
+
const stripped = model.startsWith('minimax-') ? model.slice('minimax-'.length) : model
|
|
64
|
+
return stripped
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function modelInfo(provider: string, model: MinimaxModel): LlmModelInfo {
|
|
68
|
+
return {
|
|
69
|
+
provider,
|
|
70
|
+
id: model.id,
|
|
71
|
+
name: model.name,
|
|
72
|
+
...model.description === undefined ? {} : { description: model.description },
|
|
73
|
+
inputModalities: model.vision ? ['text', 'image'] : ['text'],
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The MiniMax Code-backed adapter. */
|
|
78
|
+
export class MinimaxAdapter extends LlmAdapter {
|
|
79
|
+
private readonly options: MinimaxAdapterOptions
|
|
80
|
+
private readonly sessions: MinimaxSessionManager
|
|
81
|
+
private readonly catalog: MinimaxCatalog
|
|
82
|
+
private readonly auth = new MinimaxAuth()
|
|
83
|
+
private readonly mcpServer = new MinimaxMcpServer()
|
|
84
|
+
private readonly disabledCache = new Map<MinimaxRegion, { key: string, set: ReadonlySet<string> }>()
|
|
85
|
+
|
|
86
|
+
constructor(options: MinimaxAdapterOptions = {}) {
|
|
87
|
+
super()
|
|
88
|
+
this.options = options
|
|
89
|
+
this.sessions = new MinimaxSessionManager(
|
|
90
|
+
options.maxSessions ?? 8,
|
|
91
|
+
this.mcpServer,
|
|
92
|
+
{ toolResultTimeoutMs: options.toolResultTimeoutMs, abortGraceMs: options.abortGraceMs },
|
|
93
|
+
)
|
|
94
|
+
this.catalog = new MinimaxCatalog(
|
|
95
|
+
options.region ?? (() => 'cn'),
|
|
96
|
+
options.modelCacheTtlMs ?? MINIMAX_CACHE_TTL_MS,
|
|
97
|
+
options.catalogFetchTimeoutMs ?? MINIMAX_FETCH_TIMEOUT_MS,
|
|
98
|
+
)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private get region(): MinimaxRegion {
|
|
102
|
+
return readLive(this.options.region ?? (() => 'cn'), 'cn')
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private disabledFor(region: MinimaxRegion): ReadonlySet<string> {
|
|
106
|
+
const source = this.options.disabledModels?.(region) ?? NO_MODELS
|
|
107
|
+
const key = source.join('\u0000')
|
|
108
|
+
const cached = this.disabledCache.get(region)
|
|
109
|
+
if (cached?.key === key) return cached.set
|
|
110
|
+
const set = new Set(source)
|
|
111
|
+
this.disabledCache.set(region, { key, set })
|
|
112
|
+
return set
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private get aliases(): Readonly<Record<string, string>> {
|
|
116
|
+
return readLive(this.options.modelAliases, NO_ALIASES)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private get displayName(): string {
|
|
120
|
+
return readLive(this.options.displayName, 'MiniMax Code')
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
override providerInfo(provider: string): LlmProviderInfo {
|
|
124
|
+
return { id: provider, name: this.displayName }
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The live catalog, unfiltered (the page must show switched-off models). */
|
|
128
|
+
async describeCatalog(region: MinimaxRegion = this.region): Promise<readonly MinimaxModel[]> {
|
|
129
|
+
if (!this.isSignedIn(region)) return []
|
|
130
|
+
if (await minimaxRegionBlocked(region)) return []
|
|
131
|
+
return this.catalog.models(region)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Whether the region's endpoints are refusing this machine's network. */
|
|
135
|
+
regionBlocked(region: MinimaxRegion = this.region): boolean {
|
|
136
|
+
return minimaxRegionBlockedCached(region)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Whether the selected (or given) region has a stored login. */
|
|
140
|
+
isSignedIn(region: MinimaxRegion = this.region): boolean {
|
|
141
|
+
return this.auth.status(region).signedIn || minimaxSignedIn(region)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Why the last catalog read failed, for the page's guidance. */
|
|
145
|
+
catalogError(region: MinimaxRegion = this.region): string | undefined {
|
|
146
|
+
if (resolveMinimaxCli() === undefined) {
|
|
147
|
+
return '未安装 MiniMax Code CLI(mcode)。请先安装后再登录。'
|
|
148
|
+
}
|
|
149
|
+
if (minimaxRegionBlockedCached(region)) return minimaxRegionBlockMessage(region)
|
|
150
|
+
if (!this.isSignedIn(region)) return '请先登录 MiniMax Code,登录后才会读取并显示模型目录。'
|
|
151
|
+
return this.catalog.error(region)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Drop the cached catalog and the region verdict behind it.
|
|
156
|
+
*
|
|
157
|
+
* The page calls this with `fresh=1` on a region switch, which is exactly
|
|
158
|
+
* when the network may have changed; keeping a 30-second "blocked" verdict
|
|
159
|
+
* would hide the catalog the switch was meant to reveal.
|
|
160
|
+
*/
|
|
161
|
+
invalidateCatalog(region?: MinimaxRegion): void {
|
|
162
|
+
this.catalog.invalidate(region)
|
|
163
|
+
invalidateMinimaxRegionAccess(region)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Login status, installer state and (when signed in) the account's quota. */
|
|
167
|
+
async usageInfo(fresh = false, region?: MinimaxRegion): Promise<MinimaxUsageSnapshot> {
|
|
168
|
+
const target = region ?? this.region
|
|
169
|
+
const installed = resolveMinimaxCli() !== undefined
|
|
170
|
+
const status = this.auth.status(target)
|
|
171
|
+
const signedIn = status.signedIn || minimaxSignedIn(target)
|
|
172
|
+
if (!signedIn) {
|
|
173
|
+
return {
|
|
174
|
+
at: Date.now(),
|
|
175
|
+
region: target,
|
|
176
|
+
signedIn: false,
|
|
177
|
+
installed,
|
|
178
|
+
...installed ? {} : { error: '未安装 MiniMax Code CLI(mcode)。' },
|
|
179
|
+
...status.error === undefined ? {} : { error: status.error },
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
const snapshot = await minimaxUsage(target, { fresh })
|
|
183
|
+
return {
|
|
184
|
+
...snapshot,
|
|
185
|
+
signedIn: true,
|
|
186
|
+
installed,
|
|
187
|
+
...snapshot.error !== undefined || status.error === undefined ? {} : { error: status.error },
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Drop cached quota readings and the region verdict (login, logout, CLI). */
|
|
192
|
+
invalidateUsage(region?: MinimaxRegion): void {
|
|
193
|
+
invalidateMinimaxUsage(region)
|
|
194
|
+
invalidateMinimaxRegionAccess(region)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Login progress for one region; the page polls this. */
|
|
198
|
+
authStatus(region: MinimaxRegion): ReturnType<MinimaxAuth['status']> {
|
|
199
|
+
return this.auth.status(region)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
startLogin(region: MinimaxRegion): ReturnType<MinimaxAuth['start']> {
|
|
203
|
+
return this.auth.start(region, ok => {
|
|
204
|
+
if (!ok) return
|
|
205
|
+
this.sessions.closeAll()
|
|
206
|
+
this.invalidateCatalog(region)
|
|
207
|
+
this.invalidateUsage(region)
|
|
208
|
+
invalidateMinimaxRegionAccess(region)
|
|
209
|
+
})
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async logout(region: MinimaxRegion): ReturnType<MinimaxAuth['logout']> {
|
|
213
|
+
const result = await this.auth.logout(region)
|
|
214
|
+
this.sessions.closeAll()
|
|
215
|
+
this.invalidateCatalog(region)
|
|
216
|
+
this.invalidateUsage(region)
|
|
217
|
+
invalidateMinimaxRegionAccess(region)
|
|
218
|
+
return result
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
isModelDisabled(id: string, region: MinimaxRegion = this.region): boolean {
|
|
222
|
+
return this.disabledFor(region).has(id)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
contextWindowFor(id: string, region: MinimaxRegion = this.region): number | undefined {
|
|
226
|
+
return this.options.modelContextWindows?.(region)[id]
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
override async listModels(provider: string): Promise<readonly LlmModelInfo[]> {
|
|
230
|
+
const region = this.region
|
|
231
|
+
if (!this.isSignedIn(region)) return []
|
|
232
|
+
if (await minimaxRegionBlocked(region)) return []
|
|
233
|
+
const models = await this.catalog.models(region)
|
|
234
|
+
const disabled = this.disabledFor(region)
|
|
235
|
+
return models.filter(model => !disabled.has(model.id)).map(model => modelInfo(provider, model))
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
override async resolveModel(provider: string, model: string): Promise<LlmResolvedModelInfo> {
|
|
239
|
+
const region = this.region
|
|
240
|
+
const target = resolveMinimaxModelId(model, this.aliases)
|
|
241
|
+
const live = (await this.catalog.models(region)).find(entry => entry.id === target)
|
|
242
|
+
if (live !== undefined) {
|
|
243
|
+
const reasoning = reasoningInfo(live.reasoningEfforts, live.defaultEffort, live.reasoning)
|
|
244
|
+
return {
|
|
245
|
+
provider,
|
|
246
|
+
id: model,
|
|
247
|
+
name: live.name,
|
|
248
|
+
...live.description === undefined ? {} : { description: live.description },
|
|
249
|
+
inputModalities: live.vision ? ['text', 'image'] : ['text'],
|
|
250
|
+
context: { contextWindow: this.contextWindowFor(target, region) ?? live.defaultContextWindow },
|
|
251
|
+
defaultMaxTokens: readLive(this.options.defaultMaxTokens, MINIMAX_DEFAULT_MAX_TOKENS),
|
|
252
|
+
...reasoning === undefined ? {} : { reasoning },
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
const reasoning = reasoningInfo(
|
|
256
|
+
undefined,
|
|
257
|
+
readLiveOptional(this.options.defaultReasoningEffort),
|
|
258
|
+
true,
|
|
259
|
+
)
|
|
260
|
+
return {
|
|
261
|
+
provider,
|
|
262
|
+
id: model,
|
|
263
|
+
name: model,
|
|
264
|
+
inputModalities: ['text'],
|
|
265
|
+
context: { contextWindow: this.contextWindowFor(target, region) ?? MINIMAX_DEFAULT_CONTEXT_WINDOW },
|
|
266
|
+
defaultMaxTokens: readLive(this.options.defaultMaxTokens, MINIMAX_DEFAULT_MAX_TOKENS),
|
|
267
|
+
...reasoning === undefined ? {} : { reasoning },
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
override async prepareCall(provider: string, model: string, signal?: AbortSignal): Promise<PreparedAdapterCall> {
|
|
272
|
+
const info = await this.resolveModel(provider, model)
|
|
273
|
+
void signal
|
|
274
|
+
return { model: info, stream: options => this.dispatch(options) }
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
async *stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
|
|
278
|
+
yield* this.dispatch(options)
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
private async *dispatch(options: GenerateOptions): AsyncGenerator<StreamChunk> {
|
|
282
|
+
const region = this.region
|
|
283
|
+
if (!this.isSignedIn(region)) {
|
|
284
|
+
const label = region === 'cn' ? 'MiniMax 国内站' : 'MiniMax 国际站'
|
|
285
|
+
throw new Error(`${label}尚未登录,请先在设置 → MiniMax Code 中登录。`)
|
|
286
|
+
}
|
|
287
|
+
if (await minimaxRegionBlocked(region)) {
|
|
288
|
+
throw new Error(minimaxRegionBlockMessage(region))
|
|
289
|
+
}
|
|
290
|
+
const model = resolveMinimaxModelId(options.model, this.aliases)
|
|
291
|
+
if (options.sessionId === undefined || options.purpose !== undefined) {
|
|
292
|
+
const prompt = feedToText(renderInitialFeed(options.system, options.messages))
|
|
293
|
+
+ '\n(这是一次性旁路请求,直接输出下一条助手回复。)'
|
|
294
|
+
for (const chunk of await minimaxColdStream(region, prompt, model, options)) yield chunk
|
|
295
|
+
return
|
|
296
|
+
}
|
|
297
|
+
const sessionId = String(options.sessionId)
|
|
298
|
+
const effort = options.reasoningEffort ?? readLiveOptional(this.options.defaultReasoningEffort)
|
|
299
|
+
let session = this.sessions.forSession(sessionId, region, model)
|
|
300
|
+
if (session.fedMessages === undefined) {
|
|
301
|
+
yield* this.firstTurn(session, options, region, model, effort)
|
|
302
|
+
return
|
|
303
|
+
}
|
|
304
|
+
const tail = options.messages.slice(session.fedMessages.length)
|
|
305
|
+
session.deliverToolResults(tail)
|
|
306
|
+
const plan = planContinuation(session.fedMessages, options.messages)
|
|
307
|
+
if (plan.rebuild) {
|
|
308
|
+
this.sessions.dispose(sessionId)
|
|
309
|
+
session = this.sessions.forSession(sessionId, region, model)
|
|
310
|
+
yield* this.firstTurn(session, options, region, model, effort)
|
|
311
|
+
return
|
|
312
|
+
}
|
|
313
|
+
if (plan.feed !== null && session.busy) {
|
|
314
|
+
// The user took over while a tool round was still open. The old ACP turn
|
|
315
|
+
// cannot accept the new message, so rebuild from the durable history.
|
|
316
|
+
this.sessions.dispose(sessionId)
|
|
317
|
+
session = this.sessions.forSession(sessionId, region, model)
|
|
318
|
+
yield* this.firstTurn(session, options, region, model, effort)
|
|
319
|
+
return
|
|
320
|
+
}
|
|
321
|
+
const reset = session.configure(region, model, {
|
|
322
|
+
effort,
|
|
323
|
+
system: options.system,
|
|
324
|
+
cwd: this.cwdOf(options),
|
|
325
|
+
})
|
|
326
|
+
if (reset || (plan.feed === null && !session.busy)) {
|
|
327
|
+
// Either the prompt/cwd change discarded the warm ACP process, or the
|
|
328
|
+
// tool round it was waiting on ended without it. Only a complete replay
|
|
329
|
+
// can restore the conversation in both cases.
|
|
330
|
+
this.sessions.dispose(sessionId)
|
|
331
|
+
session = this.sessions.forSession(sessionId, region, model)
|
|
332
|
+
yield* this.firstTurn(session, options, region, model, effort)
|
|
333
|
+
return
|
|
334
|
+
}
|
|
335
|
+
session.ensureTools(options.tools ?? [])
|
|
336
|
+
session.fedMessages = options.messages
|
|
337
|
+
session.fedSystem = options.system
|
|
338
|
+
const feed = plan.feed === null ? null : feedToText(plan.feed)
|
|
339
|
+
yield* session.stream(options, feed)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
private async *firstTurn(
|
|
343
|
+
session: ReturnType<MinimaxSessionManager['forSession']>,
|
|
344
|
+
options: GenerateOptions,
|
|
345
|
+
region: MinimaxRegion,
|
|
346
|
+
model: string,
|
|
347
|
+
effort: string | undefined,
|
|
348
|
+
): AsyncGenerator<StreamChunk> {
|
|
349
|
+
session.configure(region, model, {
|
|
350
|
+
effort,
|
|
351
|
+
system: options.system,
|
|
352
|
+
cwd: this.cwdOf(options),
|
|
353
|
+
})
|
|
354
|
+
session.ensureTools(options.tools ?? [])
|
|
355
|
+
session.fedMessages = options.messages
|
|
356
|
+
session.fedSystem = options.system
|
|
357
|
+
const feed = feedToText(renderInitialFeed(undefined, options.messages))
|
|
358
|
+
yield* session.stream(options, feed)
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
private cwdOf(options: GenerateOptions): string | undefined {
|
|
362
|
+
return (options as GenerateOptions & { cwd?: string }).cwd
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
close(): void {
|
|
366
|
+
this.sessions.closeAll()
|
|
367
|
+
this.mcpServer.close()
|
|
368
|
+
this.auth.stop()
|
|
369
|
+
}
|
|
370
|
+
}
|