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
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The MiniMax Code CLI (`mcode`): locating it and running its installer.
|
|
3
|
+
*
|
|
4
|
+
* MiniMax ships `@minimax-ai/code` as a global npm package whose `mcode`
|
|
5
|
+
* launcher is a Node shim. Its native SQLite dependency is installed by a
|
|
6
|
+
* postinstall script, and npm 12 blocks install scripts unless they are named
|
|
7
|
+
* with `--allow-scripts`; a plain `npm i -g` leaves a CLI that cannot start.
|
|
8
|
+
* The installer below therefore passes the flags the package documents.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-agent-sdk-bridge/minimax-cli
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { spawn, type ChildProcess } from 'node:child_process'
|
|
14
|
+
import { existsSync, readFileSync, realpathSync } from 'node:fs'
|
|
15
|
+
import { homedir } from 'node:os'
|
|
16
|
+
import { dirname, join, delimiter } from 'node:path'
|
|
17
|
+
import { TextDecoder } from 'node:util'
|
|
18
|
+
|
|
19
|
+
/** How to invoke the CLI: an executable plus its leading arguments. */
|
|
20
|
+
export interface MinimaxCliSpec {
|
|
21
|
+
command: string
|
|
22
|
+
args: string[]
|
|
23
|
+
/** The JavaScript entry, when the launcher resolves to one. */
|
|
24
|
+
cliPath?: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The npm package and the version verified to work with native SQLite. */
|
|
28
|
+
const PACKAGE = '@minimax-ai/code'
|
|
29
|
+
const INSTALL_VERSION = '0.6.12'
|
|
30
|
+
const INSTALL_FLAGS = [
|
|
31
|
+
'--ignore-scripts=false',
|
|
32
|
+
'--include=optional',
|
|
33
|
+
'--allow-scripts=@minimax-ai/code,better-sqlite3',
|
|
34
|
+
'--registry', 'https://registry.npmjs.org/',
|
|
35
|
+
'--foreground-scripts',
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
function fromJsEntry(path: string): MinimaxCliSpec {
|
|
39
|
+
return { command: process.execPath, args: [path], cliPath: path }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function fromShim(shim: string): MinimaxCliSpec | undefined {
|
|
43
|
+
if (shim.endsWith('.cmd') || shim.endsWith('.bat')) {
|
|
44
|
+
// npm's Windows shim forwards to the package's cli.js. Reading it avoids a
|
|
45
|
+
// cmd.exe layer, which keeps ACP's stdio byte-clean.
|
|
46
|
+
try {
|
|
47
|
+
const text = readFileSync(shim, 'utf8')
|
|
48
|
+
const match = /["']([^"']*node_modules[\\/]@minimax-ai[\\/]code[\\/]cli\.js)["']/.exec(text)
|
|
49
|
+
if (match !== null && existsSync(match[1])) return fromJsEntry(match[1])
|
|
50
|
+
} catch {
|
|
51
|
+
// Fall through to the sibling-package guess below.
|
|
52
|
+
}
|
|
53
|
+
const sibling = join(dirname(shim), 'node_modules', '@minimax-ai', 'code', 'cli.js')
|
|
54
|
+
if (existsSync(sibling)) return fromJsEntry(sibling)
|
|
55
|
+
return {
|
|
56
|
+
command: process.env.ComSpec ?? 'cmd.exe',
|
|
57
|
+
args: ['/d', '/c', shim],
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (shim.endsWith('.js') || shim.endsWith('.mjs')) return fromJsEntry(shim)
|
|
61
|
+
try {
|
|
62
|
+
const real = realpathSync(shim)
|
|
63
|
+
if (real.endsWith('.js') || real.endsWith('.mjs')) return fromJsEntry(real)
|
|
64
|
+
} catch {
|
|
65
|
+
// A path that cannot be resolved is still usable as a command.
|
|
66
|
+
}
|
|
67
|
+
return { command: shim, args: [] }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Locate `mcode`.
|
|
72
|
+
*
|
|
73
|
+
* `MCODE_CLI_PATH` wins (a cli.js path or an executable); otherwise the PATH
|
|
74
|
+
* shims and the global npm prefixes are checked. The explicit cli.js lookup
|
|
75
|
+
* matters on Windows, where spawning a `.cmd` for ACP would put cmd.exe between
|
|
76
|
+
* the protocol streams.
|
|
77
|
+
*/
|
|
78
|
+
export function resolveMinimaxCli(): MinimaxCliSpec | undefined {
|
|
79
|
+
const override = process.env.MCODE_CLI_PATH?.trim()
|
|
80
|
+
if (override !== undefined && override.length > 0 && existsSync(override)) {
|
|
81
|
+
return fromShim(override)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const names = process.platform === 'win32'
|
|
85
|
+
? ['mcode.cmd', 'mcode.exe', 'mcode']
|
|
86
|
+
: ['mcode']
|
|
87
|
+
for (const directory of (process.env.PATH ?? '').split(delimiter)) {
|
|
88
|
+
if (directory.trim() === '') continue
|
|
89
|
+
for (const name of names) {
|
|
90
|
+
const candidate = join(directory, name)
|
|
91
|
+
if (existsSync(candidate)) return fromShim(candidate)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const home = homedir()
|
|
96
|
+
const candidates = process.platform === 'win32'
|
|
97
|
+
? [
|
|
98
|
+
join(process.env.APPDATA ?? join(home, 'AppData', 'Roaming'), 'npm', 'node_modules', PACKAGE, 'cli.js'),
|
|
99
|
+
join(process.env.LOCALAPPDATA ?? join(home, 'AppData', 'Local'), 'npm', 'node_modules', PACKAGE, 'cli.js'),
|
|
100
|
+
join(process.env.ProgramFiles ?? 'C:\\Program Files', 'nodejs', 'node_modules', PACKAGE, 'cli.js'),
|
|
101
|
+
]
|
|
102
|
+
: [
|
|
103
|
+
join(home, '.npm-global', 'lib', 'node_modules', PACKAGE, 'cli.js'),
|
|
104
|
+
join('/usr', 'local', 'lib', 'node_modules', PACKAGE, 'cli.js'),
|
|
105
|
+
join('/usr', 'lib', 'node_modules', PACKAGE, 'cli.js'),
|
|
106
|
+
join(home, '.local', 'lib', 'node_modules', PACKAGE, 'cli.js'),
|
|
107
|
+
]
|
|
108
|
+
for (const candidate of candidates) {
|
|
109
|
+
if (existsSync(candidate)) return fromJsEntry(candidate)
|
|
110
|
+
}
|
|
111
|
+
return undefined
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The spawn argument list for one CLI invocation. */
|
|
115
|
+
export function minimaxCliArgs(spec: MinimaxCliSpec, args: readonly string[]): string[] {
|
|
116
|
+
return [...spec.args, ...args]
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** What the settings page may ask the installer to do. */
|
|
120
|
+
export type MinimaxSetupMode = 'install' | 'uninstall'
|
|
121
|
+
|
|
122
|
+
/** Whether a value names an installer action. */
|
|
123
|
+
export function isMinimaxSetupMode(value: unknown): value is MinimaxSetupMode {
|
|
124
|
+
return value === 'install' || value === 'uninstall'
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** One installer run's state, kept for the process lifetime. */
|
|
128
|
+
interface SetupRun {
|
|
129
|
+
child: ChildProcess
|
|
130
|
+
mode: MinimaxSetupMode
|
|
131
|
+
startedAt: number
|
|
132
|
+
finishedAt?: number
|
|
133
|
+
code?: number | null
|
|
134
|
+
lines: string[]
|
|
135
|
+
pending: string
|
|
136
|
+
decoder: TextDecoder
|
|
137
|
+
truncated: boolean
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Cap on kept output. */
|
|
141
|
+
const MAX_LINES = 400
|
|
142
|
+
|
|
143
|
+
let current: SetupRun | undefined
|
|
144
|
+
|
|
145
|
+
/** Snapshot the page polls. */
|
|
146
|
+
export interface MinimaxSetupStatus {
|
|
147
|
+
running: boolean
|
|
148
|
+
mode?: MinimaxSetupMode
|
|
149
|
+
startedAt?: number
|
|
150
|
+
finishedAt?: number
|
|
151
|
+
code?: number | null
|
|
152
|
+
lines: string[]
|
|
153
|
+
truncated: boolean
|
|
154
|
+
installed: boolean
|
|
155
|
+
cliPath?: string
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function absorb(run: SetupRun, chunk: Uint8Array, done: boolean): void {
|
|
159
|
+
run.pending += run.decoder.decode(chunk, { stream: !done })
|
|
160
|
+
const parts = run.pending.split(/\r\n|\r|\n/)
|
|
161
|
+
run.pending = done ? '' : (parts.pop() ?? '')
|
|
162
|
+
for (const part of parts) {
|
|
163
|
+
if (part.trim() === '') continue
|
|
164
|
+
run.lines.push(part)
|
|
165
|
+
}
|
|
166
|
+
if (done && run.pending.trim() !== '') run.lines.push(run.pending.trim())
|
|
167
|
+
while (run.lines.length > MAX_LINES) {
|
|
168
|
+
run.lines.shift()
|
|
169
|
+
run.truncated = true
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Start the npm installer or uninstaller.
|
|
175
|
+
*
|
|
176
|
+
* The command is fixed: the mode is an allow-list and no user input reaches
|
|
177
|
+
* argv. npm itself is run through cmd.exe on Windows because npm is a `.cmd`
|
|
178
|
+
* shim and Node refuses to spawn it directly on recent versions.
|
|
179
|
+
*/
|
|
180
|
+
export function startMinimaxSetup(
|
|
181
|
+
mode: MinimaxSetupMode,
|
|
182
|
+
onFinish?: (code: number | null) => void,
|
|
183
|
+
): { started: true } | { started: false, reason: 'busy' | 'unsupported', error: string } {
|
|
184
|
+
if (current !== undefined && current.finishedAt === undefined) {
|
|
185
|
+
return { started: false, reason: 'busy', error: '已经有一次 MiniMax CLI 安装在跑了,请等它结束。' }
|
|
186
|
+
}
|
|
187
|
+
if (process.platform !== 'win32' && mode === 'install') {
|
|
188
|
+
return {
|
|
189
|
+
started: false,
|
|
190
|
+
reason: 'unsupported',
|
|
191
|
+
error: '一键安装 MiniMax CLI 目前只支持 Windows;请在终端手动执行 npm install -g @minimax-ai/code。',
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const npmArgs = mode === 'install'
|
|
196
|
+
? ['install', '--global', `${PACKAGE}@${INSTALL_VERSION}`, ...INSTALL_FLAGS]
|
|
197
|
+
: ['uninstall', '--global', PACKAGE]
|
|
198
|
+
const command = process.platform === 'win32' ? (process.env.ComSpec ?? 'cmd.exe') : 'npm'
|
|
199
|
+
const args = process.platform === 'win32'
|
|
200
|
+
? ['/d', '/c', 'npm', ...npmArgs]
|
|
201
|
+
: npmArgs
|
|
202
|
+
|
|
203
|
+
const run: SetupRun = {
|
|
204
|
+
child: undefined as unknown as ChildProcess,
|
|
205
|
+
mode,
|
|
206
|
+
startedAt: Date.now(),
|
|
207
|
+
lines: [],
|
|
208
|
+
pending: '',
|
|
209
|
+
decoder: new TextDecoder('utf-8'),
|
|
210
|
+
truncated: false,
|
|
211
|
+
}
|
|
212
|
+
const child = spawn(command, args, {
|
|
213
|
+
cwd: homedir(),
|
|
214
|
+
windowsHide: true,
|
|
215
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
216
|
+
})
|
|
217
|
+
run.child = child
|
|
218
|
+
current = run
|
|
219
|
+
|
|
220
|
+
const finish = (code: number | null): void => {
|
|
221
|
+
if (run.finishedAt !== undefined) return
|
|
222
|
+
run.finishedAt = Date.now()
|
|
223
|
+
run.code = code
|
|
224
|
+
try {
|
|
225
|
+
onFinish?.(code)
|
|
226
|
+
} catch (error) {
|
|
227
|
+
run.lines.push(`收尾回调失败:${error instanceof Error ? error.message : String(error)}`)
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
child.stdout?.on('data', (chunk: Buffer) => absorb(run, chunk, false))
|
|
231
|
+
child.stderr?.on('data', (chunk: Buffer) => absorb(run, chunk, false))
|
|
232
|
+
child.on('error', error => {
|
|
233
|
+
run.lines.push(`无法启动 npm:${error instanceof Error ? error.message : String(error)}`)
|
|
234
|
+
finish(null)
|
|
235
|
+
})
|
|
236
|
+
child.on('close', code => {
|
|
237
|
+
absorb(run, new Uint8Array(), true)
|
|
238
|
+
finish(code)
|
|
239
|
+
})
|
|
240
|
+
return { started: true }
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** The latest installer run, with any not-yet-newlined tail included. */
|
|
244
|
+
export function readMinimaxSetupStatus(): MinimaxSetupStatus {
|
|
245
|
+
const spec = resolveMinimaxCli()
|
|
246
|
+
const installed = spec !== undefined
|
|
247
|
+
if (current === undefined) {
|
|
248
|
+
return { running: false, lines: [], truncated: false, installed, ...spec?.cliPath === undefined ? {} : { cliPath: spec.cliPath } }
|
|
249
|
+
}
|
|
250
|
+
const run = current
|
|
251
|
+
const lines = run.pending.trim() === ''
|
|
252
|
+
? run.lines
|
|
253
|
+
: [...run.lines, run.pending.trim()]
|
|
254
|
+
return {
|
|
255
|
+
running: run.finishedAt === undefined,
|
|
256
|
+
mode: run.mode,
|
|
257
|
+
startedAt: run.startedAt,
|
|
258
|
+
...run.finishedAt === undefined ? {} : { finishedAt: run.finishedAt, code: run.code ?? null },
|
|
259
|
+
lines,
|
|
260
|
+
truncated: run.truncated,
|
|
261
|
+
installed,
|
|
262
|
+
...spec?.cliPath === undefined ? {} : { cliPath: spec.cliPath },
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** Abandon a running installer. */
|
|
267
|
+
export function stopMinimaxSetup(): void {
|
|
268
|
+
if (current !== undefined && current.finishedAt === undefined) current.child.kill()
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** A small object facade for callers that prefer a service shape. */
|
|
272
|
+
export class MinimaxCliSetup {
|
|
273
|
+
readonly start = startMinimaxSetup
|
|
274
|
+
readonly status = readMinimaxSetupStatus
|
|
275
|
+
readonly stop = stopMinimaxSetup
|
|
276
|
+
}
|