fix(sdk): improve FORK_MODE to support bun and fallback to spawn for native type

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
mingholy.lmh 2026-02-05 17:39:12 +08:00
parent 2d75d82ec8
commit ed3ffe2aa2
2 changed files with 144 additions and 48 deletions

View File

@ -54,7 +54,6 @@ export class ProcessTransport implements Transport {
// Check if we should use fork for Electron integration
const useFork = env.FORK_MODE === '1';
console.log('useFork', useFork);
if (useFork) {
// Detect Electron environment
@ -73,30 +72,67 @@ export class ProcessTransport implements Transport {
let forkModulePath: string;
let forkArgs: string[];
let forkExecPath: string | undefined;
if (isElectron && isUsingExecPathForJs) {
// In Electron with JS file: use the JS file as module path, rest as args
forkModulePath = spawnInfo.args[0] ?? '';
forkArgs = [...spawnInfo.args.slice(1), ...cliArgs];
} else if (
(spawnInfo.type === 'node' || spawnInfo.type === 'bun') &&
spawnInfo.args.length > 0
) {
// For node/bun type: command is the runtime, args[0] is the JS module
forkModulePath = spawnInfo.args[0] ?? '';
forkArgs = [...spawnInfo.args.slice(1), ...cliArgs];
forkExecPath = spawnInfo.command;
} else {
// Normal case: use command as module path
forkModulePath = spawnInfo.command;
forkArgs = [...spawnInfo.args, ...cliArgs];
// Native or other types: cannot use fork, fall back to spawn
logger.debug(
`FORK_MODE enabled but CLI type '${spawnInfo.type}' does not support fork. Falling back to spawn.`,
);
forkModulePath = '';
forkArgs = [];
}
logger.debug(
`Forking CLI (${spawnInfo.type}): ${forkModulePath} ${forkArgs.join(' ')}`,
);
// Only use fork if we have a valid module path
if (forkModulePath) {
logger.debug(
`Forking CLI (${spawnInfo.type}): ${forkModulePath} ${forkArgs.join(' ')}`,
);
this.childProcess = fork(forkModulePath, forkArgs, {
cwd,
env,
stdio:
stderrMode === 'pipe'
? ['pipe', 'pipe', 'pipe', 'ipc']
: ['pipe', 'pipe', 'ignore', 'ipc'],
signal: this.abortController.signal,
});
const forkOptions: Parameters<typeof fork>[2] = {
cwd,
env,
stdio:
stderrMode === 'pipe'
? ['pipe', 'pipe', 'pipe', 'ipc']
: ['pipe', 'pipe', 'ignore', 'ipc'],
signal: this.abortController.signal,
};
if (forkExecPath) {
forkOptions.execPath = forkExecPath;
}
this.childProcess = fork(forkModulePath, forkArgs, forkOptions);
} else {
// Fallback to spawn for native/unsupported types
logger.debug(
`Spawning CLI (${spawnInfo.type}): ${spawnInfo.command} ${[...spawnInfo.args, ...cliArgs].join(' ')}`,
);
this.childProcess = spawn(
spawnInfo.command,
[...spawnInfo.args, ...cliArgs],
{
cwd,
env,
stdio: ['pipe', 'pipe', stderrMode],
signal: this.abortController.signal,
},
);
}
} else {
logger.debug(
`Spawning CLI (${spawnInfo.type}): ${spawnInfo.command} ${[...spawnInfo.args, ...cliArgs].join(' ')}`,

View File

@ -1393,10 +1393,10 @@ describe('ProcessTransport', () => {
it('should use fork when FORK_MODE=1', () => {
process.env.FORK_MODE = '1';
mockPrepareSpawnInfo.mockReturnValue({
command: 'qwen',
args: [],
type: 'native',
originalInput: 'qwen',
command: 'node',
args: ['/path/to/cli.js'],
type: 'node',
originalInput: 'node /path/to/cli.js',
});
mockFork.mockReturnValue(mockChildProcess);
@ -1446,21 +1446,24 @@ describe('ProcessTransport', () => {
new ProcessTransport(options);
// In non-Electron environment, command is used as modulePath
// In non-Electron environment, JS file is used as modulePath
// and execPath is set to the runtime (node)
expect(mockFork).toHaveBeenCalledWith(
'node', // modulePath is the command in non-Electron environment
expect.arrayContaining(['/path/to/cli.js']),
expect.any(Object),
'/path/to/cli.js', // modulePath is the JS file
expect.arrayContaining([]),
expect.objectContaining({
execPath: 'node',
}),
);
});
it('should configure stdio with ipc channel for fork', () => {
process.env.FORK_MODE = '1';
mockPrepareSpawnInfo.mockReturnValue({
command: 'qwen',
args: [],
type: 'native',
originalInput: 'qwen',
command: 'node',
args: ['/path/to/cli.js'],
type: 'node',
originalInput: 'node /path/to/cli.js',
});
mockFork.mockReturnValue(mockChildProcess);
@ -1482,10 +1485,10 @@ describe('ProcessTransport', () => {
it('should configure stdio with pipe for stderr when debug mode is on', () => {
process.env.FORK_MODE = '1';
mockPrepareSpawnInfo.mockReturnValue({
command: 'qwen',
args: [],
type: 'native',
originalInput: 'qwen',
command: 'node',
args: ['/path/to/cli.js'],
type: 'node',
originalInput: 'node /path/to/cli.js',
});
mockFork.mockReturnValue(mockChildProcess);
@ -1549,21 +1552,24 @@ describe('ProcessTransport', () => {
new ProcessTransport(options);
// In normal Node.js, command is used as modulePath, not cli.js
// In normal Node.js, JS file is used as modulePath
// and execPath is set to the runtime (node)
expect(mockFork).toHaveBeenCalledWith(
'node',
expect.arrayContaining(['/path/to/cli.js', '--some-arg']),
expect.any(Object),
'/path/to/cli.js',
expect.arrayContaining(['--some-arg']),
expect.objectContaining({
execPath: 'node',
}),
);
});
it('should pass env to fork', () => {
process.env.FORK_MODE = '1';
mockPrepareSpawnInfo.mockReturnValue({
command: 'qwen',
args: [],
type: 'native',
originalInput: 'qwen',
command: 'node',
args: ['/path/to/cli.js'],
type: 'node',
originalInput: 'node /path/to/cli.js',
});
mockFork.mockReturnValue(mockChildProcess);
@ -1589,10 +1595,10 @@ describe('ProcessTransport', () => {
it('should pass cwd to fork', () => {
process.env.FORK_MODE = '1';
mockPrepareSpawnInfo.mockReturnValue({
command: 'qwen',
args: [],
type: 'native',
originalInput: 'qwen',
command: 'node',
args: ['/path/to/cli.js'],
type: 'node',
originalInput: 'node /path/to/cli.js',
});
mockFork.mockReturnValue(mockChildProcess);
@ -1615,10 +1621,10 @@ describe('ProcessTransport', () => {
it('should pass abort signal to fork', () => {
process.env.FORK_MODE = '1';
mockPrepareSpawnInfo.mockReturnValue({
command: 'qwen',
args: [],
type: 'native',
originalInput: 'qwen',
command: 'node',
args: ['/path/to/cli.js'],
type: 'node',
originalInput: 'node /path/to/cli.js',
});
mockFork.mockReturnValue(mockChildProcess);
@ -1638,5 +1644,59 @@ describe('ProcessTransport', () => {
}),
);
});
it('should fallback to spawn for native type when FORK_MODE=1', () => {
process.env.FORK_MODE = '1';
mockPrepareSpawnInfo.mockReturnValue({
command: 'qwen',
args: [],
type: 'native',
originalInput: 'qwen',
});
mockSpawn.mockReturnValue(mockChildProcess);
const options: TransportOptions = {
pathToQwenExecutable: 'qwen',
};
new ProcessTransport(options);
// Native type should fallback to spawn, not fork
expect(mockFork).not.toHaveBeenCalled();
expect(mockSpawn).toHaveBeenCalledWith(
'qwen',
expect.any(Array),
expect.objectContaining({
stdio: ['pipe', 'pipe', 'ignore'],
}),
);
});
it('should use fork for bun type with correct execPath when FORK_MODE=1', () => {
process.env.FORK_MODE = '1';
mockPrepareSpawnInfo.mockReturnValue({
command: 'bun',
args: ['/path/to/cli.js'],
type: 'bun',
originalInput: 'bun /path/to/cli.js',
});
mockFork.mockReturnValue(mockChildProcess);
const options: TransportOptions = {
pathToQwenExecutable: 'qwen',
};
new ProcessTransport(options);
// Bun type should use fork with execPath set to bun
expect(mockSpawn).not.toHaveBeenCalled();
expect(mockFork).toHaveBeenCalledWith(
'/path/to/cli.js',
expect.any(Array),
expect.objectContaining({
execPath: 'bun',
}),
);
});
});
});