Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/cli/src/nonInteractive/control/ControlDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* which wraps these controllers with a stable programmatic API.
*
* Controllers:
* - SystemController: initialize, interrupt, set_model, supported_commands, get_context_usage
* - SystemController: initialize, interrupt, set_model, set_effort, supported_commands, get_context_usage, get_available_models, get_usage_info
* - PermissionController: can_use_tool, set_permission_mode
* - SdkMcpController: mcp_server_status (mcp_message handled via callback)
*
Expand Down Expand Up @@ -375,8 +375,11 @@ export class ControlDispatcher implements IPendingRequestRegistry {
case 'interrupt':
case 'continue_last_turn':
case 'set_model':
case 'set_effort':
case 'supported_commands':
case 'get_context_usage':
case 'get_available_models':
case 'get_usage_info':
return this.systemController;

case 'can_use_tool':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import type {
ControlRequestPayload,
CLIControlInitializeRequest,
CLIControlSetModelRequest,
CLIControlSetEffortRequest,
CLIControlGetUsageInfoRequest,
CLIMcpServerConfig,
CLIControlGetContextUsageRequest,
} from '../../types.js';
Expand All @@ -26,6 +28,8 @@ import {
createDebugLogger,
MCPServerConfig,
AuthProviderType,
normalizeReasoningEffort,
loadUsageDashboard,
type MCPOAuthConfig,
} from '@qwen-code/qwen-code-core';

Expand Down Expand Up @@ -70,6 +74,12 @@ export class SystemController extends BaseController {
signal,
);

case 'set_effort':
return this.handleSetEffort(
payload as CLIControlSetEffortRequest,
signal,
);

case 'supported_commands':
return this.handleSupportedCommands(signal);

Expand All @@ -79,6 +89,15 @@ export class SystemController extends BaseController {
signal,
);

case 'get_available_models':
return this.handleGetAvailableModels(signal);

case 'get_usage_info':
return this.handleGetUsageInfo(
payload as CLIControlGetUsageInfoRequest,
signal,
);

default:
throw new Error(`Unsupported request subtype in SystemController`);
}
Expand Down Expand Up @@ -253,6 +272,27 @@ export class SystemController extends BaseController {
}
}

if (payload.effort) {
const normalized = normalizeReasoningEffort(payload.effort);
if (normalized) {
try {
this.context.config.setReasoningEffort(normalized);
debugLogger.info(
`[SystemController] Set reasoning effort to: ${normalized}`,
);
} catch (error) {
debugLogger.error(
'[SystemController] Failed to set reasoning effort:',
error,
);
}
} else {
throw new Error(
`Invalid effort value: ${payload.effort}. Supported: low, medium, high, xhigh, max`,
);
}
}

// Build capabilities for response
const capabilities = this.buildControlCapabilities();

Expand Down Expand Up @@ -281,7 +321,12 @@ export class SystemController extends BaseController {
can_set_permission_mode:
typeof this.context.config.setApprovalMode === 'function',
can_set_model: typeof this.context.config.setModel === 'function',
can_set_effort:
typeof this.context.config.setReasoningEffort === 'function',
can_get_context_usage: true,
can_get_available_models:
typeof this.context.config.getAvailableModels === 'function',
can_get_usage_info: true,
// SDK MCP servers are supported - messages routed through control plane
can_handle_mcp_message: true,
};
Expand Down Expand Up @@ -455,6 +500,127 @@ export class SystemController extends BaseController {
}
}

/**
* Handle set_effort request
*
* Sets the reasoning effort tier at runtime.
*/
private async handleSetEffort(
payload: CLIControlSetEffortRequest,
signal: AbortSignal,
): Promise<Record<string, unknown>> {
if (signal.aborted) {
throw new Error('Request aborted');
}

const effort = payload.effort;
if (typeof effort !== 'string' || effort.trim() === '') {
throw new Error('Invalid effort specified for set_effort request');
}

const normalized = normalizeReasoningEffort(effort);
if (!normalized) {
throw new Error(
`Invalid effort value: ${effort}. Supported: low, medium, high, xhigh, max`,
);
}

try {
this.context.config.setReasoningEffort(normalized);

debugLogger.info(
`[SystemController] Reasoning effort set to: ${normalized}`,
);

return {
subtype: 'set_effort',
effort: normalized,
};
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : 'Failed to set effort';

debugLogger.error(
`[SystemController] Failed to set effort ${effort}:`,
error,
);

throw new Error(errorMessage);
}
}

/**
* Handle get_available_models request
*
* Returns the list of models available for the current auth type.
*/
private async handleGetAvailableModels(
signal: AbortSignal,
): Promise<Record<string, unknown>> {
if (signal.aborted) {
throw new Error('Request aborted');
}

try {
const models = this.context.config
.getAvailableModels()
.map(({ id, label, capabilities, contextWindowSize }) => ({
id,
label,
capabilities,
contextWindowSize,
}));

return {
subtype: 'get_available_models',
models,
};
} catch (error) {
const errorMessage =
error instanceof Error
? error.message
: 'Failed to get available models';

debugLogger.error(
'[SystemController] Failed to get available models:',
error,
);

throw new Error(errorMessage);
}
}

/**
* Handle get_usage_info request
*
* Returns usage dashboard data for the specified time range.
*/
private async handleGetUsageInfo(
payload: CLIControlGetUsageInfoRequest,
signal: AbortSignal,
): Promise<Record<string, unknown>> {
if (signal.aborted) {
throw new Error('Request aborted');
}

try {
const range = payload.range;
const dashboard = await loadUsageDashboard(range ? { range } : undefined);

return {
subtype: 'get_usage_info',
...dashboard,
};
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : 'Failed to get usage info';

debugLogger.error('[SystemController] Failed to get usage info:', error);

throw new Error(errorMessage);
}
}

/**
* Handle supported_commands request
*
Expand Down
24 changes: 23 additions & 1 deletion packages/cli/src/nonInteractive/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,11 @@ export interface CLIControlInitializeRequest {
*/
mcpServers?: Record<string, CLIMcpServerConfig>;
agents?: SubagentConfig[];
/**
* Initial reasoning effort tier: 'low' | 'medium' | 'high' | 'xhigh' | 'max'.
* Applied at session start via config.setReasoningEffort().
*/
effort?: string;
}

export interface CLIControlSetPermissionModeRequest {
Expand Down Expand Up @@ -418,6 +423,20 @@ export interface CLIControlSetModelRequest {
model: string;
}

export interface CLIControlSetEffortRequest {
subtype: 'set_effort';
effort: string;
}

export interface CLIControlGetAvailableModelsRequest {
subtype: 'get_available_models';
}

export interface CLIControlGetUsageInfoRequest {
subtype: 'get_usage_info';
range?: 'today' | 'week' | 'month' | 'all';
}

export interface CLIControlMcpStatusRequest {
subtype: 'mcp_server_status';
}
Expand All @@ -440,9 +459,12 @@ export type ControlRequestPayload =
| CLIHookCallbackRequest
| CLIControlMcpMessageRequest
| CLIControlSetModelRequest
| CLIControlSetEffortRequest
| CLIControlMcpStatusRequest
| CLIControlSupportedCommandsRequest
| CLIControlGetContextUsageRequest;
| CLIControlGetContextUsageRequest
| CLIControlGetAvailableModelsRequest
| CLIControlGetUsageInfoRequest;

export interface CLIControlRequest {
type: 'control_request';
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk-python/src/qwen_code_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
AuthType,
CanUseTool,
CanUseToolContext,
Effort,
PermissionAllowResult,
PermissionDenyResult,
PermissionMode,
Expand Down Expand Up @@ -70,6 +71,7 @@ def query_sync(
"CanUseToolContext",
"ContentBlock",
"ControlRequestTimeoutError",
"Effort",
"PermissionAllowResult",
"PermissionDenyResult",
"PermissionMode",
Expand Down
24 changes: 24 additions & 0 deletions packages/sdk-python/src/qwen_code_sdk/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ class CLIControlInitializeRequest(TypedDict):
subtype: Literal["initialize"]
hooks: NotRequired[Any]
mcpServers: NotRequired[dict[str, dict[str, Any]]]
effort: NotRequired[str]


class CLIControlSetPermissionModeRequest(TypedDict):
Expand All @@ -238,6 +239,25 @@ class CLIControlSupportedCommandsRequest(TypedDict):
subtype: Literal["supported_commands"]


class CLIControlGetContextUsageRequest(TypedDict):
subtype: Literal["get_context_usage"]
show_details: NotRequired[bool]


class CLIControlSetEffortRequest(TypedDict):
subtype: Literal["set_effort"]
effort: str


class CLIControlGetAvailableModelsRequest(TypedDict):
subtype: Literal["get_available_models"]


class CLIControlGetUsageInfoRequest(TypedDict):
subtype: Literal["get_usage_info"]
range: NotRequired[Literal["today", "week", "month", "all"]]


ControlRequestPayload: TypeAlias = (
CLIControlInterruptRequest
| CLIControlPermissionRequest
Expand All @@ -246,6 +266,10 @@ class CLIControlSupportedCommandsRequest(TypedDict):
| CLIControlSetModelRequest
| CLIControlMcpStatusRequest
| CLIControlSupportedCommandsRequest
| CLIControlGetContextUsageRequest
| CLIControlSetEffortRequest
| CLIControlGetAvailableModelsRequest
| CLIControlGetUsageInfoRequest
| dict[str, Any]
)

Expand Down
Loading