📦 Release qwen-code CLI as a Standalone Bundled Package (#866)

This commit is contained in:
tanzhenxin
2025-10-24 17:08:59 +08:00
committed by GitHub
parent 5cf609c367
commit be633a80cc
36 changed files with 802 additions and 1077 deletions

View File

@@ -14,7 +14,7 @@ import {
type Mock,
} from 'vitest';
import type { RipGrepToolParams } from './ripGrep.js';
import { canUseRipgrep, RipGrepTool, ensureRgPath } from './ripGrep.js';
import { RipGrepTool } from './ripGrep.js';
import path from 'node:path';
import fs from 'node:fs/promises';
import os, { EOL } from 'node:os';
@@ -22,24 +22,11 @@ import type { Config } from '../config/config.js';
import { createMockWorkspaceContext } from '../test-utils/mockWorkspaceContext.js';
import type { ChildProcess } from 'node:child_process';
import { spawn } from 'node:child_process';
import { downloadRipGrep } from '@joshua.litt/get-ripgrep';
import { fileExists } from '../utils/fileUtils.js';
import { ensureRipgrepPath } from '../utils/ripgrepUtils.js';
// Mock dependencies for canUseRipgrep
vi.mock('@joshua.litt/get-ripgrep', () => ({
downloadRipGrep: vi.fn(),
}));
vi.mock('../utils/fileUtils.js', async (importOriginal) => {
const actual = await importOriginal<typeof import('../utils/fileUtils.js')>();
return {
...actual,
fileExists: vi.fn(),
};
});
vi.mock('../config/storage.js', () => ({
Storage: {
getGlobalBinDir: vi.fn().mockReturnValue('/mock/bin/dir'),
},
// Mock ripgrepUtils
vi.mock('../utils/ripgrepUtils.js', () => ({
ensureRipgrepPath: vi.fn(),
}));
// Mock child_process for ripgrep calls
@@ -49,97 +36,6 @@ vi.mock('child_process', () => ({
const mockSpawn = vi.mocked(spawn);
describe('canUseRipgrep', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('should return true if ripgrep already exists', async () => {
(fileExists as Mock).mockResolvedValue(true);
const result = await canUseRipgrep();
expect(result).toBe(true);
expect(fileExists).toHaveBeenCalledWith(path.join('/mock/bin/dir', 'rg'));
expect(downloadRipGrep).not.toHaveBeenCalled();
});
it('should download ripgrep and return true if it does not exist initially', async () => {
(fileExists as Mock)
.mockResolvedValueOnce(false)
.mockResolvedValueOnce(true);
(downloadRipGrep as Mock).mockResolvedValue(undefined);
const result = await canUseRipgrep();
expect(result).toBe(true);
expect(fileExists).toHaveBeenCalledTimes(2);
expect(downloadRipGrep).toHaveBeenCalledWith('/mock/bin/dir');
});
it('should return false if download fails and file does not exist', async () => {
(fileExists as Mock).mockResolvedValue(false);
(downloadRipGrep as Mock).mockResolvedValue(undefined);
const result = await canUseRipgrep();
expect(result).toBe(false);
expect(fileExists).toHaveBeenCalledTimes(2);
expect(downloadRipGrep).toHaveBeenCalledWith('/mock/bin/dir');
});
it('should propagate errors from downloadRipGrep', async () => {
const error = new Error('Download failed');
(fileExists as Mock).mockResolvedValue(false);
(downloadRipGrep as Mock).mockRejectedValue(error);
await expect(canUseRipgrep()).rejects.toThrow(error);
expect(fileExists).toHaveBeenCalledTimes(1);
expect(downloadRipGrep).toHaveBeenCalledWith('/mock/bin/dir');
});
});
describe('ensureRgPath', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('should return rg path if ripgrep already exists', async () => {
(fileExists as Mock).mockResolvedValue(true);
const rgPath = await ensureRgPath();
expect(rgPath).toBe(path.join('/mock/bin/dir', 'rg'));
expect(fileExists).toHaveBeenCalledOnce();
expect(downloadRipGrep).not.toHaveBeenCalled();
});
it('should return rg path if ripgrep is downloaded successfully', async () => {
(fileExists as Mock)
.mockResolvedValueOnce(false)
.mockResolvedValueOnce(true);
(downloadRipGrep as Mock).mockResolvedValue(undefined);
const rgPath = await ensureRgPath();
expect(rgPath).toBe(path.join('/mock/bin/dir', 'rg'));
expect(downloadRipGrep).toHaveBeenCalledOnce();
expect(fileExists).toHaveBeenCalledTimes(2);
});
it('should throw an error if ripgrep cannot be used after download attempt', async () => {
(fileExists as Mock).mockResolvedValue(false);
(downloadRipGrep as Mock).mockResolvedValue(undefined);
await expect(ensureRgPath()).rejects.toThrow('Cannot use ripgrep.');
expect(downloadRipGrep).toHaveBeenCalledOnce();
expect(fileExists).toHaveBeenCalledTimes(2);
});
it('should propagate errors from downloadRipGrep', async () => {
const error = new Error('Download failed');
(fileExists as Mock).mockResolvedValue(false);
(downloadRipGrep as Mock).mockRejectedValue(error);
await expect(ensureRgPath()).rejects.toThrow(error);
expect(fileExists).toHaveBeenCalledTimes(1);
expect(downloadRipGrep).toHaveBeenCalledWith('/mock/bin/dir');
});
});
// Helper function to create mock spawn implementations
function createMockSpawn(
options: {
@@ -201,8 +97,7 @@ describe('RipGrepTool', () => {
beforeEach(async () => {
vi.clearAllMocks();
(downloadRipGrep as Mock).mockResolvedValue(undefined);
(fileExists as Mock).mockResolvedValue(true);
(ensureRipgrepPath as Mock).mockResolvedValue('/mock/path/to/rg');
mockSpawn.mockClear();
tempRootDir = await fs.mkdtemp(path.join(os.tmpdir(), 'grep-tool-root-'));
grepTool = new RipGrepTool(mockConfig);
@@ -551,16 +446,18 @@ describe('RipGrepTool', () => {
});
it('should throw an error if ripgrep is not available', async () => {
// Make ensureRgPath throw
(fileExists as Mock).mockResolvedValue(false);
(downloadRipGrep as Mock).mockResolvedValue(undefined);
// Make ensureRipgrepBinary throw
(ensureRipgrepPath as Mock).mockRejectedValue(
new Error('Ripgrep binary not found'),
);
const params: RipGrepToolParams = { pattern: 'world' };
const invocation = grepTool.build(params);
expect(await invocation.execute(abortSignal)).toStrictEqual({
llmContent: 'Error during grep search operation: Cannot use ripgrep.',
returnDisplay: 'Error: Cannot use ripgrep.',
llmContent:
'Error during grep search operation: Ripgrep binary not found',
returnDisplay: 'Error: Ripgrep binary not found',
});
});
});

View File

@@ -8,44 +8,16 @@ import fs from 'node:fs';
import path from 'node:path';
import { EOL } from 'node:os';
import { spawn } from 'node:child_process';
import { downloadRipGrep } from '@joshua.litt/get-ripgrep';
import type { ToolInvocation, ToolResult } from './tools.js';
import { BaseDeclarativeTool, BaseToolInvocation, Kind } from './tools.js';
import { SchemaValidator } from '../utils/schemaValidator.js';
import { makeRelative, shortenPath } from '../utils/paths.js';
import { getErrorMessage, isNodeError } from '../utils/errors.js';
import type { Config } from '../config/config.js';
import { fileExists } from '../utils/fileUtils.js';
import { Storage } from '../config/storage.js';
import { ensureRipgrepPath } from '../utils/ripgrepUtils.js';
const DEFAULT_TOTAL_MAX_MATCHES = 20000;
function getRgPath(): string {
return path.join(Storage.getGlobalBinDir(), 'rg');
}
/**
* Checks if `rg` exists, if not then attempt to download it.
*/
export async function canUseRipgrep(): Promise<boolean> {
if (await fileExists(getRgPath())) {
return true;
}
await downloadRipGrep(Storage.getGlobalBinDir());
return await fileExists(getRgPath());
}
/**
* Ensures `rg` is downloaded, or throws.
*/
export async function ensureRgPath(): Promise<string> {
if (await canUseRipgrep()) {
return getRgPath();
}
throw new Error('Cannot use ripgrep.');
}
/**
* Parameters for the GrepTool
*/
@@ -320,7 +292,7 @@ class GrepToolInvocation extends BaseToolInvocation<
rgArgs.push(absolutePath);
try {
const rgPath = await ensureRgPath();
const rgPath = await ensureRipgrepPath();
const output = await new Promise<string>((resolve, reject) => {
const child = spawn(rgPath, rgArgs, {
windowsHide: true,
@@ -342,11 +314,7 @@ class GrepToolInvocation extends BaseToolInvocation<
child.on('error', (err) => {
options.signal.removeEventListener('abort', cleanup);
reject(
new Error(
`Failed to start ripgrep: ${err.message}. Please ensure @lvce-editor/ripgrep is properly installed.`,
),
);
reject(new Error(`Failed to start ripgrep: ${err.message}.`));
});
child.on('close', (code) => {