From f155571dc82fa18a8d5d08450c9d72a4f5c0c25c Mon Sep 17 00:00:00 2001 From: AzumoAurora3032 Date: Sat, 25 Oct 2025 17:37:53 +0900 Subject: [PATCH] Enhance argument parsing and error handling Refactor argument parsing and error handling for profiles and tasks. Improve environment variable handling with error checks. --- main.js | 85 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 71 insertions(+), 14 deletions(-) diff --git a/main.js b/main.js index 4402cb964..0c254b829 100644 --- a/main.js +++ b/main.js @@ -4,6 +4,10 @@ import yargs from 'yargs'; import { hideBin } from 'yargs/helpers'; import { readFileSync } from 'fs'; +/** + * コマンドライン引数をyargsを使ってパースします。 + * @returns {object} パースされた引数オブジェクト。 + */ function parseArguments() { return yargs(hideBin(process.argv)) .option('profiles', { @@ -22,51 +26,104 @@ function parseArguments() { .alias('help', 'h') .parse(); } + +// --- 引数と環境変数の処理 --- + const args = parseArguments(); + +// コマンドライン引数の適用 if (args.profiles) { settings.profiles = args.profiles; } if (args.task_path) { - let tasks = JSON.parse(readFileSync(args.task_path, 'utf8')); + // タスクファイルを読み込み、指定されたタスクを抽出 + const tasks = JSON.parse(readFileSync(args.task_path, 'utf8')); if (args.task_id) { settings.task = tasks[args.task_id]; - settings.task.task_id = args.task_id; + // タスクIDをタスクオブジェクトに注入 + if (settings.task) { + settings.task.task_id = args.task_id; + } else { + throw new Error(`Task ID '${args.task_id}' not found in task file.`); + } } else { throw new Error('task_id is required when task_path is provided'); } } -// these environment variables override certain settings +// 環境変数を適用し、既存の設定を上書き if (process.env.MINECRAFT_PORT) { settings.port = process.env.MINECRAFT_PORT; } if (process.env.MINDSERVER_PORT) { settings.mindserver_port = process.env.MINDSERVER_PORT; } -if (process.env.PROFILES && JSON.parse(process.env.PROFILES).length > 0) { - settings.profiles = JSON.parse(process.env.PROFILES); +if (process.env.PROFILES) { + try { + const envProfiles = JSON.parse(process.env.PROFILES); + if (Array.isArray(envProfiles) && envProfiles.length > 0) { + settings.profiles = envProfiles; + } + } catch (e) { + console.error('Error parsing PROFILES environment variable:', e.message); + } } if (process.env.INSECURE_CODING) { settings.allow_insecure_coding = true; } if (process.env.BLOCKED_ACTIONS) { - settings.blocked_actions = JSON.parse(process.env.BLOCKED_ACTIONS); + try { + settings.blocked_actions = JSON.parse(process.env.BLOCKED_ACTIONS); + } catch (e) { + console.error('Error parsing BLOCKED_ACTIONS environment variable:', e.message); + } } if (process.env.MAX_MESSAGES) { - settings.max_messages = process.env.MAX_MESSAGES; + // 文字列として受け取った値を数値に変換 + settings.max_messages = Number(process.env.MAX_MESSAGES); } if (process.env.NUM_EXAMPLES) { - settings.num_examples = process.env.NUM_EXAMPLES; + // 文字列として受け取った値を数値に変換 + settings.num_examples = Number(process.env.NUM_EXAMPLES); } if (process.env.LOG_ALL) { - settings.log_all_prompts = process.env.LOG_ALL; + // 環境変数の値をブール値として解釈 + settings.log_all_prompts = process.env.LOG_ALL === 'true' || process.env.LOG_ALL === '1'; } +// --- Mindcraftの初期化とエージェントの作成 --- + +// Mindcraftのコアサービスを初期化 Mindcraft.init(false, settings.mindserver_port, settings.auto_open_ui); -for (let profile of settings.profiles) { - const profile_json = JSON.parse(readFileSync(profile, 'utf8')); - settings.profile = profile_json; - Mindcraft.createAgent(settings); -} \ No newline at end of file +// 全てのプロファイルパスを反復処理し、それぞれを読み込んで固有のエージェントインスタンスを作成します。 +for (const profilePath of settings.profiles) { + console.log(`Loading profile from: ${profilePath}`); + + try { + // プロファイル設定ファイルを読み込み + const profile_json = JSON.parse(readFileSync(profilePath, 'utf8')); + + // 現在のエージェントのための新しい、個別の設定オブジェクトを作成します。 + // これにより、グローバルな`settings`オブジェクト内の一つのプロファイルオブジェクトが、 + // 異なるエージェントの初期化フェーズで共有・変異することを防ぎます。 + const agentSettings = { + ...settings, // ベース設定をコピー + profile: profile_json // このエージェント固有のプロファイルを注入 + }; + + // 専用の設定でエージェントを作成 + Mindcraft.createAgent(agentSettings); + console.log(`Agent created for profile: ${profilePath}`); + + } catch (error) { + console.error(`Failed to load or create agent for profile path: ${profilePath}. Error: ${error.message}`); + // アプリケーションを停止せずに次のプロファイルへ続行 + } +} + +// プロファイルが読み込まれなかった場合の警告 +if (settings.profiles.length === 0) { + console.warn("No agent profiles were provided or loaded. No agents were created."); +}