mirror of
https://github.com/QwenLM/qwen-code.git
synced 2026-09-12 14:46:02 +00:00
* feat(core): add CodeModeOnly tool execution * fix(core): preserve CodeModeOnly execution semantics * fix(core): drop the tool_search reminder in CodeModeOnly CodeModeOnly hides `tool_search` and binds every deferred tool — schema included — into the `exec` description, so progressive discovery has nothing left to do. The session prelude still announced those tools as "reachable via `tool_search`", pointing the model at a tool that is never declared and re-billing the text on every cached prefix. Worse, tools already callable as `tools.web_fetch(...)` looked gated behind a lookup step that does not exist. The subagent and fork-resume callers already opted out, but the main-session prelude sites did not, so the gate now lives where the prelude is built rather than in each caller. * chore: refresh settings schema * chore: refresh companion notices * fix: register exec tool metadata * fix(cli): localize exec tool display name * fix(core): stop offering ToolSearch lookups in CodeModeOnly CodeModeOnly hides tool_search and binds every deferred schema into the exec description, so text that tells the model to look a tool up describes a surface it cannot reach. Return an empty deferred summary from the registry itself - this also covers the fork-resume reminder that bypassed the earlier call-site gate - and point the image zoom hint at tools.zoom_image. * feat(core): teach the system prompt CodeModeOnly's tool surface In CodeModeOnly the model can only call exec, so the tool guidance and the worked examples described a surface it does not have: bare tool names it cannot call, an instruction to issue several tool calls in one response, and examples in a direct tool-call syntax that does not exist there. Give "Using Your Tools" a code-mode branch that routes every tool through tools.<name>, states which tools are direct controls instead, and replaces multi-call parallelism with batching inside one exec program. Swap the four model-family example sets for one shared exec set, since the syntax differences they exist for do not apply. The mechanics of exec itself stay in its tool description; the prompt carries only policy. * test: include code mode host in standalone fixture * fix(core): return code mode media as multimodal output * feat(core): support image tool results in code mode * fix(core): carry deferred tool schemas in the exec description CodeModeOnly hides tool_search and never surfaces a nested call as a history functionCall, so a signature collapsed to Record<string, unknown> could never be filled in later: parameter names were reachable only by guessing, or by reading them off validation errors one at a time. Deferred tools keep their registry state; only the generated signature gains the schema. * fix(node-repl): restore the bin exec bit after rebuild npm chmods dist/index.js when it links the bin at install time, but the build deletes that exact file and tsc re-emits it as 0644. After any rebuild node_modules/.bin/node-repl-mcp therefore points at a non-executable target and spawning it fails with EACCES (exit 126) before the shebang is ever read. OR the exec bits into the emitted mode instead of setting 0o755, so a restrictive umask is preserved rather than widened. * fix(cli): pin patched Ink version * feat(cli): add collapsible tool call details * fix(dev): use CLI tsconfig outside repository * fix(cli): preserve expanded tool details after completion * fix(cli): fail headless runs on model API errors * fix(core): expose shell timeout bounds in code mode * feat(core): add code mode timeout helpers * feat(cli): expose code mode in settings * fix(mcp): preserve structured results for model and exec consumers * fix(core): preserve code mode output and truncate inline * feat(core): port scoped context tools to code mode * fix(build): synchronize pnpm lockfile and capability docs * fix(core): recover from rejected Responses encrypted reasoning
302 lines
11 KiB
JavaScript
302 lines
11 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2025 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { createRequire } from 'node:module';
|
|
import { writeFileSync, rmSync, readFileSync } from 'node:fs';
|
|
import { wasmLoader } from 'esbuild-plugin-wasm';
|
|
import { isStubbedSdkNodeExporterImport } from './scripts/sdk-node-exporter-stub.js';
|
|
|
|
let esbuild;
|
|
try {
|
|
esbuild = (await import('esbuild')).default;
|
|
} catch (_error) {
|
|
console.warn('esbuild not available, skipping bundle step');
|
|
process.exit(0);
|
|
}
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
const require = createRequire(import.meta.url);
|
|
const pkg = require(path.resolve(__dirname, 'package.json'));
|
|
|
|
// Clean dist directory (cross-platform)
|
|
rmSync(path.resolve(__dirname, 'dist'), { recursive: true, force: true });
|
|
|
|
/**
|
|
* Resolve `import X from '*.wasm?binary'` imports to an inline Uint8Array.
|
|
*
|
|
* The `?binary` suffix is a build-time hint: at bundle time (esbuild) the WASM
|
|
* bytes are embedded as base64 and exported as a default Uint8Array, so no
|
|
* external vendor files are needed at runtime. In source / transpiled mode
|
|
* the dynamic import throws and the caller falls back to reading from
|
|
* node_modules via `require.resolve`.
|
|
*/
|
|
const wasmBinaryPlugin = {
|
|
name: 'wasm-binary',
|
|
setup(build) {
|
|
build.onResolve({ filter: /\.wasm\?binary$/ }, (args) => {
|
|
const specifier = args.path.replace(/\?binary$/, '');
|
|
const localRequire = createRequire(
|
|
path.resolve(args.resolveDir || __dirname, '_dummy_.js'),
|
|
);
|
|
return {
|
|
path: localRequire.resolve(specifier),
|
|
namespace: 'wasm-binary',
|
|
};
|
|
});
|
|
build.onLoad({ filter: /.*/, namespace: 'wasm-binary' }, (args) => {
|
|
const contents = readFileSync(args.path);
|
|
return { contents, loader: 'binary' };
|
|
});
|
|
},
|
|
};
|
|
|
|
// `@opentelemetry/sdk-node` eagerly require()s every exporter package to
|
|
// support `OTEL_*_EXPORTER` env-based auto-configuration, which would drag
|
|
// both OTLP protocol chains (~2 MiB) back into the sdk-impl static closure
|
|
// and defeat the per-protocol dynamic imports in
|
|
// `packages/core/src/telemetry/sdk-{impl,exporters-grpc,exporters-http}.ts`
|
|
// (issue #7264). From sdk-node 0.221 the env auto-configuration helpers were
|
|
// additionally extracted into `@opentelemetry/configuration` and the two
|
|
// `otlp-*exporter-base` packages, which sdk-node requires eagerly; they get
|
|
// the same treatment. qwen-code always passes explicit `spanProcessors` /
|
|
// `logRecordProcessors` to NodeSDK, so those env code paths are unreachable
|
|
// for traces and logs. Stub the packages ONLY when imported by
|
|
// sdk-node itself — our own protocol modules keep resolving the real ones.
|
|
// The stubbed constructors throw so an unexpectedly reached env path (e.g.
|
|
// `OTEL_METRICS_EXPORTER=otlp`) fails loudly instead of exporting nowhere;
|
|
// NodeSDK.start() is wrapped in try/catch in `sdk.ts`. The resolve decision
|
|
// lives in scripts/sdk-node-exporter-stub.js so it stays unit-testable.
|
|
const sdkNodeExporterStubPlugin = {
|
|
name: 'sdk-node-exporter-stub',
|
|
setup(build) {
|
|
build.onResolve(
|
|
{
|
|
filter:
|
|
/^@opentelemetry\/(exporter-|configuration$|otlp-(grpc-)?exporter-base$)/,
|
|
},
|
|
(args) => {
|
|
if (!isStubbedSdkNodeExporterImport(args.path, args.importer)) {
|
|
return null;
|
|
}
|
|
return { path: args.path, namespace: 'sdk-node-exporter-stub' };
|
|
},
|
|
);
|
|
build.onLoad(
|
|
{ filter: /.*/, namespace: 'sdk-node-exporter-stub' },
|
|
(args) => ({
|
|
contents: `
|
|
const throwStubbed = (name) => {
|
|
throw new Error(
|
|
'qwen-code bundles @opentelemetry/sdk-node without ' +
|
|
${JSON.stringify(args.path)} + ' (env-based exporter ' +
|
|
'selection is unsupported; configure telemetry via ' +
|
|
'qwen-code settings instead). Attempted to construct: ' + name,
|
|
);
|
|
};
|
|
const handler = {
|
|
// Module interop and thenable probes must see a plain, non-callable
|
|
// namespace: a bundler or await import() reads then and __esModule
|
|
// (Symbol.* keys are already covered by the typeof check), and
|
|
// constructing those as "exporters" would throw a confusing error.
|
|
get: (_t, prop) => {
|
|
if (
|
|
typeof prop !== 'string' ||
|
|
prop === 'then' ||
|
|
prop === '__esModule'
|
|
) {
|
|
return undefined;
|
|
}
|
|
return function stubbedExporter() {
|
|
throwStubbed(prop);
|
|
};
|
|
},
|
|
};
|
|
module.exports = new Proxy({}, handler);
|
|
`,
|
|
loader: 'js',
|
|
}),
|
|
);
|
|
},
|
|
};
|
|
|
|
const syncFileEncodingTreeShakePlugin = {
|
|
name: 'sync-file-encoding-tree-shake',
|
|
setup(build) {
|
|
build.onResolve(
|
|
{ filter: /^\.\/services\/sync-file-encoding\.js$/ },
|
|
(args) => {
|
|
if (
|
|
!/[\\/]packages[\\/]core[\\/](?:src|dist[\\/]src)[\\/]index\.(?:ts|js)$/.test(
|
|
args.importer,
|
|
)
|
|
) {
|
|
return null;
|
|
}
|
|
const sourceExtension = args.importer.endsWith('.ts') ? '.ts' : '.js';
|
|
return {
|
|
path: path.resolve(
|
|
args.resolveDir,
|
|
args.path.replace(/\.js$/, sourceExtension),
|
|
),
|
|
sideEffects: false,
|
|
};
|
|
},
|
|
);
|
|
},
|
|
};
|
|
|
|
const external = [
|
|
'@lydell/node-pty',
|
|
'node-pty',
|
|
'@lydell/node-pty-darwin-arm64',
|
|
'@lydell/node-pty-darwin-x64',
|
|
'@lydell/node-pty-linux-x64',
|
|
'@lydell/node-pty-win32-arm64',
|
|
'@lydell/node-pty-win32-x64',
|
|
'@qwen-code/audio-capture',
|
|
'@teddyzhu/clipboard',
|
|
'@teddyzhu/clipboard-darwin-arm64',
|
|
'@teddyzhu/clipboard-darwin-x64',
|
|
'@teddyzhu/clipboard-linux-x64-gnu',
|
|
'@teddyzhu/clipboard-linux-arm64-gnu',
|
|
'@teddyzhu/clipboard-win32-x64-msvc',
|
|
'@teddyzhu/clipboard-win32-arm64-msvc',
|
|
'sharp',
|
|
];
|
|
|
|
// Name of the directory under `dist/` that esbuild emits shared chunks into.
|
|
// MUST stay in sync with `BUNDLE_CHUNK_DIR` in
|
|
// `packages/core/src/utils/bundlePaths.ts`, whose `resolveBundleDir` helper
|
|
// strips this exact segment when modules look up sibling assets at runtime.
|
|
// Renaming here without renaming there silently breaks bundled-binary lookup
|
|
// in skill-manager / ripgrepUtils / i18n / extensions/new.
|
|
const BUNDLE_CHUNK_DIR = 'chunks';
|
|
|
|
const mainBuild = esbuild.build({
|
|
entryPoints: { cli: 'packages/cli/src/cli.ts' },
|
|
bundle: true,
|
|
outdir: 'dist',
|
|
entryNames: '[name]',
|
|
chunkNames: `${BUNDLE_CHUNK_DIR}/[name]-[hash]`,
|
|
splitting: true,
|
|
platform: 'node',
|
|
format: 'esm',
|
|
target: 'node22',
|
|
external,
|
|
packages: 'bundle',
|
|
inject: [path.resolve(__dirname, 'scripts/esbuild-shims.js')],
|
|
banner: {
|
|
js: `// Force strict mode and setup for ESM
|
|
"use strict";`,
|
|
},
|
|
alias: {
|
|
'is-in-ci': path.resolve(__dirname, 'packages/cli/src/patches/is-in-ci.ts'),
|
|
'jsonc-parser': require.resolve('jsonc-parser/lib/esm/main.js'),
|
|
'@qwen-code/web-templates': path.resolve(
|
|
__dirname,
|
|
'packages/web-templates/src/index.ts',
|
|
),
|
|
// Resolve to userland punycode instead of deprecated node:punycode built-in
|
|
punycode: require.resolve('punycode/'),
|
|
},
|
|
define: {
|
|
'process.env.CLI_VERSION': JSON.stringify(pkg.version),
|
|
// react-reconciler ≥0.33 (ink 7) gates its dev build behind NODE_ENV
|
|
// and calls performance.measure() on every render, leaking
|
|
// PerformanceMeasure objects into the global measureEntryBuffer.
|
|
// Setting production here tree-shakes the entire dev build (~15k lines).
|
|
'process.env.NODE_ENV': JSON.stringify('production'),
|
|
// Make global available for compatibility
|
|
global: 'globalThis',
|
|
// Redirect free __dirname/__filename references to the shim so that
|
|
// vendored libraries that emit their own `var __dirname` locals don't
|
|
// collide with our injected bindings when code-splitting is enabled.
|
|
//
|
|
// CONTRIBUTOR WARNING: this rewrite applies to *all* source files, so
|
|
// any bare `__dirname` / `__filename` in our own code resolves to the
|
|
// shim chunk's on-disk location (i.e. `dist/chunks/`), NOT the source
|
|
// file's own directory. To get a per-file path, declare a local shadow
|
|
// at the top of the module:
|
|
//
|
|
// import { fileURLToPath } from 'node:url';
|
|
// const __filename = fileURLToPath(import.meta.url);
|
|
// const __dirname = path.dirname(__filename);
|
|
//
|
|
// esbuild leaves the local binding alone (it's a declared identifier,
|
|
// not a free reference). For sibling-asset lookups in modules that may
|
|
// be hoisted into a shared chunk, prefer
|
|
// `resolveBundleDir(import.meta.url)` from
|
|
// `packages/core/src/utils/bundlePaths.ts` — it both produces a
|
|
// per-file path and strips the chunk segment when the module ends up
|
|
// under `dist/chunks/`.
|
|
__dirname: '__qwen_dirname',
|
|
__filename: '__qwen_filename',
|
|
},
|
|
loader: { '.node': 'file' },
|
|
plugins: [
|
|
sdkNodeExporterStubPlugin,
|
|
syncFileEncodingTreeShakePlugin,
|
|
wasmBinaryPlugin,
|
|
wasmLoader({ mode: 'embedded' }),
|
|
],
|
|
metafile: true,
|
|
write: true,
|
|
keepNames: true,
|
|
});
|
|
|
|
// fzf index worker — runs in its own worker_threads worker that
|
|
// `fzfWorkerHandle.ts` spawns via `new Worker(new URL('./fzfWorker.js', ...))`.
|
|
// Must exist as a standalone file next to `dist/cli.js` so the URL resolves
|
|
// at runtime; we bundle it self-contained (no chunk splitting) so fzf is
|
|
// inlined and the worker doesn't need to walk back into node_modules from
|
|
// the published tarball. `prepare-package.js` whitelists `fzfWorker.js` in
|
|
// the dist `files` array.
|
|
const workerBuild = esbuild.build({
|
|
entryPoints: ['packages/core/src/utils/filesearch/fzfWorker.ts'],
|
|
bundle: true,
|
|
outfile: 'dist/fzfWorker.js',
|
|
platform: 'node',
|
|
format: 'esm',
|
|
target: 'node22',
|
|
external,
|
|
packages: 'bundle',
|
|
// fzf is CJS — needs the same require()-shim the main bundle uses for
|
|
// CJS interop in ESM output.
|
|
inject: [path.resolve(__dirname, 'scripts/esbuild-shims.js')],
|
|
banner: {
|
|
js: `"use strict";`,
|
|
},
|
|
write: true,
|
|
keepNames: true,
|
|
});
|
|
|
|
const codeModeHostBuild = esbuild.build({
|
|
entryPoints: ['packages/core/src/code-mode/host.ts'],
|
|
bundle: true,
|
|
outfile: 'dist/codeModeHost.js',
|
|
platform: 'node',
|
|
format: 'esm',
|
|
target: 'node22',
|
|
packages: 'bundle',
|
|
inject: [path.resolve(__dirname, 'scripts/esbuild-shims.js')],
|
|
banner: { js: `"use strict";` },
|
|
write: true,
|
|
keepNames: true,
|
|
});
|
|
|
|
Promise.all([mainBuild, workerBuild, codeModeHostBuild])
|
|
.then(([{ metafile }]) => {
|
|
if (process.env.DEV === 'true') {
|
|
writeFileSync('./dist/esbuild.json', JSON.stringify(metafile, null, 2));
|
|
}
|
|
})
|
|
.catch((error) => {
|
|
console.error('esbuild build failed:', error);
|
|
process.exitCode = 1;
|
|
});
|