fix: emit subagent user message and correct systemMessage properties

This commit is contained in:
mingholy.lmh
2025-11-06 15:26:44 +08:00
parent edb4b36408
commit 38ea6e1c74
6 changed files with 231 additions and 23 deletions

View File

@@ -11,7 +11,11 @@ import type {
TaskResultDisplay,
ToolCallResponseInfo,
} from '@qwen-code/qwen-code-core';
import { ToolErrorType } from '@qwen-code/qwen-code-core';
import {
ToolErrorType,
MCPServerStatus,
getMCPServerStatus,
} from '@qwen-code/qwen-code-core';
import type { Part } from '@google/genai';
import type {
CLIUserMessage,
@@ -55,6 +59,15 @@ vi.mock('../ui/utils/computeStats.js', () => ({
}),
}));
vi.mock('@qwen-code/qwen-code-core', async (importOriginal) => {
const actual =
await importOriginal<typeof import('@qwen-code/qwen-code-core')>();
return {
...actual,
getMCPServerStatus: vi.fn(),
};
});
describe('normalizePartList', () => {
it('should return empty array for null input', () => {
expect(normalizePartList(null)).toEqual([]);
@@ -477,6 +490,10 @@ describe('buildSystemMessage', () => {
let mockConfig: Config;
beforeEach(() => {
vi.clearAllMocks();
// Mock getMCPServerStatus to return CONNECTED by default
vi.mocked(getMCPServerStatus).mockReturnValue(MCPServerStatus.CONNECTED);
mockConfig = {
getToolRegistry: vi.fn().mockReturnValue({
getAllToolNames: vi.fn().mockReturnValue(['tool1', 'tool2']),

View File

@@ -13,7 +13,11 @@ import type {
ToolCallResponseInfo,
SessionMetrics,
} from '@qwen-code/qwen-code-core';
import { ToolErrorType } from '@qwen-code/qwen-code-core';
import {
OutputFormat,
ToolErrorType,
getMCPServerStatus,
} from '@qwen-code/qwen-code-core';
import type { Part, PartListUnion } from '@google/genai';
import type {
CLIUserMessage,
@@ -243,13 +247,25 @@ export async function buildSystemMessage(
const mcpServerList = mcpServers
? Object.keys(mcpServers).map((name) => ({
name,
status: 'connected',
status: getMCPServerStatus(name),
}))
: [];
// Load slash commands
const slashCommands = await loadSlashCommandNames(config);
// Load subagent names from config
let agentNames: string[] = [];
try {
const subagentManager = config.getSubagentManager();
const subagents = await subagentManager.listSubagents();
agentNames = subagents.map((subagent) => subagent.name);
} catch (error) {
if (config.getDebugMode()) {
console.error('[buildSystemMessage] Failed to load subagents:', error);
}
}
const systemMessage: CLISystemMessage = {
type: 'system',
subtype: 'init',
@@ -261,13 +277,8 @@ export async function buildSystemMessage(
model: config.getModel(),
permissionMode,
slash_commands: slashCommands,
apiKeySource: 'none',
qwen_code_version: config.getCliVersion() || 'unknown',
output_style: 'default',
agents: [],
skills: [],
// Note: capabilities are NOT included in system messages
// They are only in the initialize control response
agents: agentNames,
};
return systemMessage;
@@ -536,11 +547,29 @@ export function createTaskToolProgressHandler(
}
}
// Handle subagent initial message (prompt) in non-interactive mode with json/stream-json output
// Emit when this is the first update (previous is undefined) and task starts
if (
!previous &&
taskDisplay.taskPrompt &&
!config.isInteractive() &&
(config.getOutputFormat() === OutputFormat.JSON ||
config.getOutputFormat() === OutputFormat.STREAM_JSON)
) {
// Emit the user message with the correct parent_tool_use_id
adapter.emitUserMessage(
[{ text: taskDisplay.taskPrompt }],
taskToolCallId,
);
}
// Update previous state
previousTaskStates.set(callId, taskDisplay);
}
};
// No longer need to attach adapter to handler - task.ts uses TaskResultDisplay.message instead
return {
handler: outputUpdateHandler,
};