basant307/AI_Governance_Project
048
1/**2 * @license3 * Copyright 2025 Google LLC4 * SPDX-License-Identifier: Apache-2.05 */6 7import type { Config } from '@qwen-code/qwen-code-core';8import { OutputFormat } from '@qwen-code/qwen-code-core';9import { validateAuthMethod } from './config/auth.js';10import { type LoadedSettings } from './config/settings.js';11import { JsonOutputAdapter } from './nonInteractive/io/JsonOutputAdapter.js';12import { StreamJsonOutputAdapter } from './nonInteractive/io/StreamJsonOutputAdapter.js';13import { runExitCleanup } from './utils/cleanup.js';14import { writeStderrLine } from './utils/stdioHelpers.js';15 16export async function validateNonInteractiveAuth(17 useExternalAuth: boolean | undefined,18 nonInteractiveConfig: Config,19 settings: LoadedSettings,20): Promise<Config> {21 try {22 // Get the actual authType from config which has already resolved CLI args, env vars, and settings23 const authType = nonInteractiveConfig24 .getModelsConfig()25 .getCurrentAuthType();26 if (!authType) {27 throw new Error(28 'No auth type is selected. Please configure an auth type (e.g. via settings or `--auth-type`) before running in non-interactive mode.',29 );30 }31 const resolvedAuthType: NonNullable<typeof authType> = authType;32 33 const enforcedType = settings.merged.security?.auth?.enforcedType;34 if (enforcedType && enforcedType !== resolvedAuthType) {35 const message = `The configured auth type is ${enforcedType}, but the current auth type is ${resolvedAuthType}. Please re-authenticate with the correct type.`;36 throw new Error(message);37 }38 39 if (!useExternalAuth) {40 const err = validateAuthMethod(resolvedAuthType, nonInteractiveConfig);41 if (err != null) {42 throw new Error(err);43 }44 }45 46 await nonInteractiveConfig.refreshAuth(resolvedAuthType);47 return nonInteractiveConfig;48 } catch (error) {49 const outputFormat = nonInteractiveConfig.getOutputFormat();50 51 // In JSON and STREAM_JSON modes, emit error result and exit52 if (53 outputFormat === OutputFormat.JSON ||54 outputFormat === OutputFormat.STREAM_JSON55 ) {56 let adapter;57 if (outputFormat === OutputFormat.JSON) {58 adapter = new JsonOutputAdapter(nonInteractiveConfig);59 } else {60 adapter = new StreamJsonOutputAdapter(61 nonInteractiveConfig,62 nonInteractiveConfig.getIncludePartialMessages(),63 );64 }65 const errorMessage =66 error instanceof Error ? error.message : String(error);67 adapter.emitResult({68 isError: true,69 errorMessage,70 durationMs: 0,71 apiDurationMs: 0,72 numTurns: 0,73 usage: undefined,74 });75 await runExitCleanup();76 process.exit(1);77 }78 79 // For other modes (text), use existing error handling80 writeStderrLine(error instanceof Error ? error.message : String(error));81 process.exit(1);82 }83}84 