fix: refine protocol types

This commit is contained in:
mingholy.lmh
2025-10-31 12:57:54 +08:00
parent 1aa282c054
commit 8034fd5f82
5 changed files with 51 additions and 32 deletions

View File

@@ -131,7 +131,7 @@ function normalizeOutputFormat(
if (!format) { if (!format) {
return undefined; return undefined;
} }
if (format === 'stream-json') { if (format === OutputFormat.STREAM_JSON) {
return OutputFormat.STREAM_JSON; return OutputFormat.STREAM_JSON;
} }
if (format === 'json' || format === OutputFormat.JSON) { if (format === 'json' || format === OutputFormat.JSON) {
@@ -417,7 +417,7 @@ export async function parseArguments(settings: Settings): Promise<CliArgs> {
} }
if ( if (
argv['includePartialMessages'] && argv['includePartialMessages'] &&
argv['outputFormat'] !== 'stream-json' argv['outputFormat'] !== OutputFormat.STREAM_JSON
) { ) {
return '--include-partial-messages requires --output-format stream-json'; return '--include-partial-messages requires --output-format stream-json';
} }
@@ -661,7 +661,7 @@ export async function loadCliConfig(
// Interactive mode: explicit -i flag or (TTY + no args + no -p flag) // Interactive mode: explicit -i flag or (TTY + no args + no -p flag)
const hasQuery = !!argv.query; const hasQuery = !!argv.query;
const interactive = const interactive =
inputFormat === 'stream-json' inputFormat === InputFormat.STREAM_JSON
? false ? false
: !!argv.promptInteractive || : !!argv.promptInteractive ||
(process.stdin.isTTY && !hasQuery && !argv.prompt); (process.stdin.isTTY && !hasQuery && !argv.prompt);

View File

@@ -8,6 +8,7 @@ import type { Config } from '@qwen-code/qwen-code-core';
import { import {
AuthType, AuthType,
getOauthClient, getOauthClient,
InputFormat,
logUserPrompt, logUserPrompt,
} from '@qwen-code/qwen-code-core'; } from '@qwen-code/qwen-code-core';
import { render } from 'ink'; import { render } from 'ink';
@@ -418,16 +419,17 @@ export async function main() {
const inputFormat = const inputFormat =
typeof config.getInputFormat === 'function' typeof config.getInputFormat === 'function'
? config.getInputFormat() ? config.getInputFormat()
: 'text'; : InputFormat.TEXT;
if (inputFormat === 'stream-json') { const nonInteractiveConfig = await validateNonInteractiveAuth(
settings.merged.security?.auth?.selectedType,
settings.merged.security?.auth?.useExternal,
config,
settings,
);
if (inputFormat === InputFormat.STREAM_JSON) {
const trimmedInput = (input ?? '').trim(); const trimmedInput = (input ?? '').trim();
const nonInteractiveConfig = await validateNonInteractiveAuth(
settings.merged.security?.auth?.selectedType,
settings.merged.security?.auth?.useExternal,
config,
settings,
);
await runStreamJsonSession( await runStreamJsonSession(
nonInteractiveConfig, nonInteractiveConfig,
@@ -455,13 +457,6 @@ export async function main() {
prompt_length: input.length, prompt_length: input.length,
}); });
const nonInteractiveConfig = await validateNonInteractiveAuth(
settings.merged.security?.auth?.selectedType,
settings.merged.security?.auth?.useExternal,
config,
settings,
);
if (config.getDebugMode()) { if (config.getDebugMode()) {
console.log('Session ID: %s', sessionId); console.log('Session ID: %s', sessionId);
} }

View File

@@ -171,7 +171,8 @@ export async function runNonInteractive(
debugMode: config.getDebugMode(), debugMode: config.getDebugMode(),
}); });
const isStreamJsonOutput = config.getOutputFormat() === 'stream-json'; const isStreamJsonOutput =
config.getOutputFormat() === OutputFormat.STREAM_JSON;
const streamJsonContext = options.streamJson; const streamJsonContext = options.streamJson;
const streamJsonWriter = isStreamJsonOutput const streamJsonWriter = isStreamJsonOutput
? (streamJsonContext?.writer ?? ? (streamJsonContext?.writer ??

View File

@@ -18,7 +18,10 @@ import type {
ToolCallRequestInfo, ToolCallRequestInfo,
WaitingToolCall, WaitingToolCall,
} from '@qwen-code/qwen-code-core'; } from '@qwen-code/qwen-code-core';
import { ToolConfirmationOutcome } from '@qwen-code/qwen-code-core'; import {
InputFormat,
ToolConfirmationOutcome,
} from '@qwen-code/qwen-code-core';
import type { import type {
CLIControlPermissionRequest, CLIControlPermissionRequest,
CLIControlSetPermissionModeRequest, CLIControlSetPermissionModeRequest,
@@ -409,7 +412,7 @@ export class PermissionController extends BaseController {
): Promise<void> { ): Promise<void> {
try { try {
const inputFormat = this.context.config.getInputFormat?.(); const inputFormat = this.context.config.getInputFormat?.();
const isStreamJsonMode = inputFormat === 'stream-json'; const isStreamJsonMode = inputFormat === InputFormat.STREAM_JSON;
if (!isStreamJsonMode) { if (!isStreamJsonMode) {
// No SDK available - use local permission check // No SDK available - use local permission check

View File

@@ -1,5 +1,13 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Annotation for attaching metadata to content blocks
*/
export interface Annotation {
type: string;
value: string;
}
/** /**
* Usage information types * Usage information types
*/ */
@@ -37,7 +45,7 @@ export interface ModelUsage {
export interface CLIPermissionDenial { export interface CLIPermissionDenial {
tool_name: string; tool_name: string;
tool_use_id: string; tool_use_id: string;
tool_input: Record<string, any>; tool_input: unknown;
} }
/** /**
@@ -46,26 +54,30 @@ export interface CLIPermissionDenial {
export interface TextBlock { export interface TextBlock {
type: 'text'; type: 'text';
text: string; text: string;
annotations?: Annotation[];
} }
export interface ThinkingBlock { export interface ThinkingBlock {
type: 'thinking'; type: 'thinking';
thinking: string; thinking: string;
signature: string; signature?: string;
annotations?: Annotation[];
} }
export interface ToolUseBlock { export interface ToolUseBlock {
type: 'tool_use'; type: 'tool_use';
id: string; id: string;
name: string; name: string;
input: Record<string, any>; input: unknown;
annotations?: Annotation[];
} }
export interface ToolResultBlock { export interface ToolResultBlock {
type: 'tool_result'; type: 'tool_result';
tool_use_id: string; tool_use_id: string;
content: string | Array<Record<string, any>> | null; content?: string | ContentBlock[];
is_error?: boolean; is_error?: boolean;
annotations?: Annotation[];
} }
export type ContentBlock = export type ContentBlock =
@@ -79,7 +91,7 @@ export type ContentBlock =
*/ */
export interface APIUserMessage { export interface APIUserMessage {
role: 'user'; role: 'user';
content: string | ToolResultBlock[]; content: string | ContentBlock[];
} }
export interface APIAssistantMessage { export interface APIAssistantMessage {
@@ -101,6 +113,7 @@ export interface CLIUserMessage {
session_id: string; session_id: string;
message: APIUserMessage; message: APIUserMessage;
parent_tool_use_id: string | null; parent_tool_use_id: string | null;
options?: Record<string, unknown>;
} }
export interface CLIAssistantMessage { export interface CLIAssistantMessage {
@@ -151,6 +164,7 @@ export interface CLIResultMessageSuccess {
usage: ExtendedUsage; usage: ExtendedUsage;
modelUsage?: Record<string, ModelUsage>; modelUsage?: Record<string, ModelUsage>;
permission_denials: CLIPermissionDenial[]; permission_denials: CLIPermissionDenial[];
[key: string]: unknown;
} }
export interface CLIResultMessageError { export interface CLIResultMessageError {
@@ -166,6 +180,12 @@ export interface CLIResultMessageError {
usage: ExtendedUsage; usage: ExtendedUsage;
modelUsage?: Record<string, ModelUsage>; modelUsage?: Record<string, ModelUsage>;
permission_denials: CLIPermissionDenial[]; permission_denials: CLIPermissionDenial[];
error?: {
type?: string;
message: string;
[key: string]: unknown;
};
[key: string]: unknown;
} }
export type CLIResultMessage = CLIResultMessageSuccess | CLIResultMessageError; export type CLIResultMessage = CLIResultMessageSuccess | CLIResultMessageError;
@@ -232,7 +252,7 @@ export interface PermissionSuggestion {
type: 'allow' | 'deny' | 'modify'; type: 'allow' | 'deny' | 'modify';
label: string; label: string;
description?: string; description?: string;
modifiedInput?: Record<string, any>; modifiedInput?: unknown;
} }
/** /**
@@ -261,7 +281,7 @@ export interface CLIControlPermissionRequest {
subtype: 'can_use_tool'; subtype: 'can_use_tool';
tool_name: string; tool_name: string;
tool_use_id: string; tool_use_id: string;
input: Record<string, any>; input: unknown;
permission_suggestions: PermissionSuggestion[] | null; permission_suggestions: PermissionSuggestion[] | null;
blocked_path: string | null; blocked_path: string | null;
} }
@@ -280,7 +300,7 @@ export interface CLIControlSetPermissionModeRequest {
export interface CLIHookCallbackRequest { export interface CLIHookCallbackRequest {
subtype: 'hook_callback'; subtype: 'hook_callback';
callback_id: string; callback_id: string;
input: any; input: unknown;
tool_use_id: string | null; tool_use_id: string | null;
} }
@@ -331,19 +351,19 @@ export interface CLIControlRequest {
export interface PermissionApproval { export interface PermissionApproval {
allowed: boolean; allowed: boolean;
reason?: string; reason?: string;
modifiedInput?: Record<string, any>; modifiedInput?: unknown;
} }
export interface ControlResponse { export interface ControlResponse {
subtype: 'success'; subtype: 'success';
request_id: string; request_id: string;
response: Record<string, any> | null; response: unknown;
} }
export interface ControlErrorResponse { export interface ControlErrorResponse {
subtype: 'error'; subtype: 'error';
request_id: string; request_id: string;
error: string; error: string | { message: string; [key: string]: unknown };
} }
export interface CLIControlResponse { export interface CLIControlResponse {