From 156134d3d4ef671512fe0c43856c9decb5fe5478 Mon Sep 17 00:00:00 2001 From: "mingholy.lmh" Date: Tue, 16 Dec 2025 10:46:28 +0800 Subject: [PATCH] chore(sdk): bundle CLI into SDK package and inherit the dependencies --- packages/sdk-typescript/package.json | 3 +- packages/sdk-typescript/scripts/build.js | 32 ++++++++++++++++++++ packages/sdk-typescript/src/utils/cliPath.ts | 29 +++++++++++++++++- 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/packages/sdk-typescript/package.json b/packages/sdk-typescript/package.json index b071b8a3..6de4ff97 100644 --- a/packages/sdk-typescript/package.json +++ b/packages/sdk-typescript/package.json @@ -45,7 +45,8 @@ "node": ">=18.0.0" }, "dependencies": { - "@modelcontextprotocol/sdk": "^1.0.4" + "@modelcontextprotocol/sdk": "^1.0.4", + "tiktoken": "^1.0.21" }, "devDependencies": { "@types/node": "^20.14.0", diff --git a/packages/sdk-typescript/scripts/build.js b/packages/sdk-typescript/scripts/build.js index beda8b0e..ae3a21e8 100755 --- a/packages/sdk-typescript/scripts/build.js +++ b/packages/sdk-typescript/scripts/build.js @@ -91,3 +91,35 @@ if (existsSync(licenseSource)) { console.warn('Could not copy LICENSE:', error.message); } } + +console.log('Bundling CLI into SDK package...'); +const repoRoot = join(rootDir, '..', '..'); +const rootDistDir = join(repoRoot, 'dist'); + +if (!existsSync(rootDistDir) || !existsSync(join(rootDistDir, 'cli.js'))) { + console.log('Building CLI bundle...'); + try { + execSync('npm run bundle', { stdio: 'inherit', cwd: repoRoot }); + } catch (error) { + console.error('Failed to build CLI bundle:', error.message); + throw error; + } +} + +const cliDistDir = join(rootDir, 'dist', 'cli'); +mkdirSync(cliDistDir, { recursive: true }); + +console.log('Copying CLI bundle...'); +cpSync(join(rootDistDir, 'cli.js'), join(cliDistDir, 'cli.js')); + +const vendorSource = join(rootDistDir, 'vendor'); +if (existsSync(vendorSource)) { + cpSync(vendorSource, join(cliDistDir, 'vendor'), { recursive: true }); +} + +const localesSource = join(rootDistDir, 'locales'); +if (existsSync(localesSource)) { + cpSync(localesSource, join(cliDistDir, 'locales'), { recursive: true }); +} + +console.log('CLI bundle copied successfully to SDK package'); diff --git a/packages/sdk-typescript/src/utils/cliPath.ts b/packages/sdk-typescript/src/utils/cliPath.ts index 2d919413..99d604f1 100644 --- a/packages/sdk-typescript/src/utils/cliPath.ts +++ b/packages/sdk-typescript/src/utils/cliPath.ts @@ -20,6 +20,7 @@ import * as fs from 'node:fs'; import * as path from 'node:path'; import { execSync } from 'node:child_process'; +import { fileURLToPath } from 'node:url'; /** * Executable types supported by the SDK @@ -40,11 +41,37 @@ export type SpawnInfo = { originalInput: string; }; +function getBundledCliPath(): string | null { + try { + const currentFile = + typeof __filename !== 'undefined' + ? __filename + : fileURLToPath(import.meta.url); + + const currentDir = path.dirname(currentFile); + + const bundledCliPath = path.join(currentDir, 'cli', 'cli.js'); + + if (fs.existsSync(bundledCliPath)) { + return bundledCliPath; + } + + return null; + } catch { + return null; + } +} + export function findNativeCliPath(): string { + const bundledCli = getBundledCliPath(); + if (bundledCli) { + return bundledCli; + } + const homeDir = process.env['HOME'] || process.env['USERPROFILE'] || ''; const candidates: Array = [ - // 1. Environment variable (highest priority) + // 1. Environment variable process.env['QWEN_CODE_CLI_PATH'], // 2. Volta bin