"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.main = main; var _piAi = require("@mariozechner/pi-ai"); var _chalk = _interopRequireDefault(require("chalk")); var _readline = require("readline"); var _args = require("./cli/args.js"); var _configSelector = require("./cli/config-selector.js"); var _fileProcessor = require("./cli/file-processor.js"); var _listModels = require("./cli/list-models.js"); var _sessionPicker = require("./cli/session-picker.js"); var _config = require("./config.js"); var _authStorage = require("./core/auth-storage.js"); var _index = require("./core/export-html/index.js"); var _keybindings = require("./core/keybindings.js"); var _modelRegistry = require("./core/model-registry.js"); var _modelResolver = require("./core/model-resolver.js"); var _packageManager = require("./core/package-manager.js"); var _resourceLoader = require("./core/resource-loader.js"); var _sdk = require("./core/sdk.js"); var _sessionManager = require("./core/session-manager.js"); var _settingsManager = require("./core/settings-manager.js"); var _timings = require("./core/timings.js"); var _index2 = require("./core/tools/index.js"); var _migrations = require("./migrations.js"); var _index3 = require("./modes/index.js"); var _theme = require("./modes/interactive/theme/theme.js");function _interopRequireDefault(e) {return e && e.__esModule ? e : { default: e };} /** * Main entry point for the coding agent CLI. * * This file handles CLI argument parsing and translates them into * createAgentSession() options. The SDK does the heavy lifting. */ /** * Read all content from piped stdin. * Returns undefined if stdin is a TTY (interactive terminal). */async function readPipedStdin() {// If stdin is a TTY, we're running interactively - don't read stdin if (process.stdin.isTTY) {return undefined;} return new Promise((resolve) => { let data = ""; process.stdin.setEncoding("utf8"); process.stdin.on("data", (chunk) => { data += chunk; }); process.stdin.on("end", () => { resolve(data.trim() || undefined); }); process.stdin.resume(); }); } function reportSettingsErrors(settingsManager, context) { const errors = settingsManager.drainErrors(); for (const { scope, error } of errors) { console.error(_chalk.default.yellow(`Warning (${context}, ${scope} settings): ${error.message}`)); if (error.stack) { console.error(_chalk.default.dim(error.stack)); } } } function isTruthyEnvFlag(value) { if (!value) return false; return value === "1" || value.toLowerCase() === "true" || value.toLowerCase() === "yes"; } function getPackageCommandUsage(command) { switch (command) { case "install": return `${_config.APP_NAME} install [-l]`; case "remove": return `${_config.APP_NAME} remove [-l]`; case "update": return `${_config.APP_NAME} update [source]`; case "list": return `${_config.APP_NAME} list`; } } function printPackageCommandHelp(command) { switch (command) { case "install": console.log(`${_chalk.default.bold("Usage:")} ${getPackageCommandUsage("install")} Install a package and add it to settings. Options: -l, --local Install project-locally (.pi/settings.json) Examples: ${_config.APP_NAME} install npm:@foo/bar ${_config.APP_NAME} install git:github.com/user/repo ${_config.APP_NAME} install git:git@github.com:user/repo ${_config.APP_NAME} install https://github.com/user/repo ${_config.APP_NAME} install ssh://git@github.com/user/repo ${_config.APP_NAME} install ./local/path `); return; case "remove": console.log(`${_chalk.default.bold("Usage:")} ${getPackageCommandUsage("remove")} Remove a package and its source from settings. Options: -l, --local Remove from project settings (.pi/settings.json) Example: ${_config.APP_NAME} remove npm:@foo/bar `); return; case "update": console.log(`${_chalk.default.bold("Usage:")} ${getPackageCommandUsage("update")} Update installed packages. If is provided, only that package is updated. `); return; case "list": console.log(`${_chalk.default.bold("Usage:")} ${getPackageCommandUsage("list")} List installed packages from user and project settings. `); return; } } function parsePackageCommand(args) { const [command, ...rest] = args; if (command !== "install" && command !== "remove" && command !== "update" && command !== "list") { return undefined; } let local = false; let help = false; let invalidOption; let source; for (const arg of rest) { if (arg === "-h" || arg === "--help") { help = true; continue; } if (arg === "-l" || arg === "--local") { if (command === "install" || command === "remove") { local = true; } else { invalidOption = invalidOption ?? arg; } continue; } if (arg.startsWith("-")) { invalidOption = invalidOption ?? arg; continue; } if (!source) { source = arg; } } return { command, source, local, help, invalidOption }; } async function handlePackageCommand(args) { const options = parsePackageCommand(args); if (!options) { return false; } if (options.help) { printPackageCommandHelp(options.command); return true; } if (options.invalidOption) { console.error(_chalk.default.red(`Unknown option ${options.invalidOption} for "${options.command}".`)); console.error(_chalk.default.dim(`Use "${_config.APP_NAME} --help" or "${getPackageCommandUsage(options.command)}".`)); process.exitCode = 1; return true; } const source = options.source; if ((options.command === "install" || options.command === "remove") && !source) { console.error(_chalk.default.red(`Missing ${options.command} source.`)); console.error(_chalk.default.dim(`Usage: ${getPackageCommandUsage(options.command)}`)); process.exitCode = 1; return true; } const cwd = process.cwd(); const agentDir = (0, _config.getAgentDir)(); const settingsManager = _settingsManager.SettingsManager.create(cwd, agentDir); reportSettingsErrors(settingsManager, "package command"); const packageManager = new _packageManager.DefaultPackageManager({ cwd, agentDir, settingsManager }); packageManager.setProgressCallback((event) => { if (event.type === "start") { process.stdout.write(_chalk.default.dim(`${event.message}\n`)); } }); try { switch (options.command) { case "install": await packageManager.install(source, { local: options.local }); packageManager.addSourceToSettings(source, { local: options.local }); console.log(_chalk.default.green(`Installed ${source}`)); return true; case "remove":{ await packageManager.remove(source, { local: options.local }); const removed = packageManager.removeSourceFromSettings(source, { local: options.local }); if (!removed) { console.error(_chalk.default.red(`No matching package found for ${source}`)); process.exitCode = 1; return true; } console.log(_chalk.default.green(`Removed ${source}`)); return true; } case "list":{ const globalSettings = settingsManager.getGlobalSettings(); const projectSettings = settingsManager.getProjectSettings(); const globalPackages = globalSettings.packages ?? []; const projectPackages = projectSettings.packages ?? []; if (globalPackages.length === 0 && projectPackages.length === 0) { console.log(_chalk.default.dim("No packages installed.")); return true; } const formatPackage = (pkg, scope) => { const source = typeof pkg === "string" ? pkg : pkg.source; const filtered = typeof pkg === "object"; const display = filtered ? `${source} (filtered)` : source; console.log(` ${display}`); const path = packageManager.getInstalledPath(source, scope); if (path) { console.log(_chalk.default.dim(` ${path}`)); } }; if (globalPackages.length > 0) { console.log(_chalk.default.bold("User packages:")); for (const pkg of globalPackages) { formatPackage(pkg, "user"); } } if (projectPackages.length > 0) { if (globalPackages.length > 0) console.log(); console.log(_chalk.default.bold("Project packages:")); for (const pkg of projectPackages) { formatPackage(pkg, "project"); } } return true; } case "update": await packageManager.update(source); if (source) { console.log(_chalk.default.green(`Updated ${source}`)); } else { console.log(_chalk.default.green("Updated packages")); } return true; } } catch (error) { const message = error instanceof Error ? error.message : "Unknown package command error"; console.error(_chalk.default.red(`Error: ${message}`)); process.exitCode = 1; return true; } } async function prepareInitialMessage(parsed, autoResizeImages) { if (parsed.fileArgs.length === 0) { return {}; } const { text, images } = await (0, _fileProcessor.processFileArguments)(parsed.fileArgs, { autoResizeImages }); let initialMessage; if (parsed.messages.length > 0) { initialMessage = text + parsed.messages[0]; parsed.messages.shift(); } else { initialMessage = text; } return { initialMessage, initialImages: images.length > 0 ? images : undefined }; } /** * Resolve a session argument to a file path. * If it looks like a path, use as-is. Otherwise try to match as session ID prefix. */ async function resolveSessionPath(sessionArg, cwd, sessionDir) { // If it looks like a file path, use as-is if (sessionArg.includes("/") || sessionArg.includes("\\") || sessionArg.endsWith(".jsonl")) { return { type: "path", path: sessionArg }; } // Try to match as session ID in current project first const localSessions = await _sessionManager.SessionManager.list(cwd, sessionDir); const localMatches = localSessions.filter((s) => s.id.startsWith(sessionArg)); if (localMatches.length >= 1) { return { type: "local", path: localMatches[0].path }; } // Try global search across all projects const allSessions = await _sessionManager.SessionManager.listAll(); const globalMatches = allSessions.filter((s) => s.id.startsWith(sessionArg)); if (globalMatches.length >= 1) { const match = globalMatches[0]; return { type: "global", path: match.path, cwd: match.cwd }; } // Not found anywhere return { type: "not_found", arg: sessionArg }; } /** Prompt user for yes/no confirmation */ async function promptConfirm(message) { return new Promise((resolve) => { const rl = (0, _readline.createInterface)({ input: process.stdin, output: process.stdout }); rl.question(`${message} [y/N] `, (answer) => { rl.close(); resolve(answer.toLowerCase() === "y" || answer.toLowerCase() === "yes"); }); }); } /** Helper to call CLI-only session_directory handlers before the initial session manager is created */ async function callSessionDirectoryHook(extensions, cwd) { let customSessionDir; for (const ext of extensions.extensions) { const handlers = ext.handlers.get("session_directory"); if (!handlers || handlers.length === 0) continue; for (const handler of handlers) { try { const event = { type: "session_directory", cwd }; const result = await handler(event); if (result?.sessionDir) { customSessionDir = result.sessionDir; } } catch (err) { const message = err instanceof Error ? err.message : String(err); console.error(_chalk.default.red(`Extension "${ext.path}" session_directory handler failed: ${message}`)); } } } return customSessionDir; } async function createSessionManager(parsed, cwd, extensions) { if (parsed.noSession) { return _sessionManager.SessionManager.inMemory(); } // CLI flag takes precedence, otherwise ask extensions for custom session directory let effectiveSessionDir = parsed.sessionDir; if (!effectiveSessionDir) { effectiveSessionDir = await callSessionDirectoryHook(extensions, cwd); } if (parsed.session) { const resolved = await resolveSessionPath(parsed.session, cwd, effectiveSessionDir); switch (resolved.type) { case "path": case "local": return _sessionManager.SessionManager.open(resolved.path, effectiveSessionDir); case "global":{ // Session found in different project - ask user if they want to fork console.log(_chalk.default.yellow(`Session found in different project: ${resolved.cwd}`)); const shouldFork = await promptConfirm("Fork this session into current directory?"); if (!shouldFork) { console.log(_chalk.default.dim("Aborted.")); process.exit(0); } return _sessionManager.SessionManager.forkFrom(resolved.path, cwd, effectiveSessionDir); } case "not_found": console.error(_chalk.default.red(`No session found matching '${resolved.arg}'`)); process.exit(1); } } if (parsed.continue) { return _sessionManager.SessionManager.continueRecent(cwd, effectiveSessionDir); } // --resume is handled separately (needs picker UI) // If effective session dir is set, create new session there if (effectiveSessionDir) { return _sessionManager.SessionManager.create(cwd, effectiveSessionDir); } // Default case (new session) returns undefined, SDK will create one return undefined; } function buildSessionOptions(parsed, scopedModels, sessionManager, modelRegistry, settingsManager) { const options = {}; let cliThinkingFromModel = false; if (sessionManager) { options.sessionManager = sessionManager; } // Model from CLI // - supports --provider --model // - supports --model / if (parsed.model) { const resolved = (0, _modelResolver.resolveCliModel)({ cliProvider: parsed.provider, cliModel: parsed.model, modelRegistry }); if (resolved.warning) { console.warn(_chalk.default.yellow(`Warning: ${resolved.warning}`)); } if (resolved.error) { console.error(_chalk.default.red(resolved.error)); process.exit(1); } if (resolved.model) { options.model = resolved.model; // Allow "--model :" as a shorthand. // Explicit --thinking still takes precedence (applied later). if (!parsed.thinking && resolved.thinkingLevel) { options.thinkingLevel = resolved.thinkingLevel; cliThinkingFromModel = true; } } } if (!options.model && scopedModels.length > 0 && !parsed.continue && !parsed.resume) { // Check if saved default is in scoped models - use it if so, otherwise first scoped model const savedProvider = settingsManager.getDefaultProvider(); const savedModelId = settingsManager.getDefaultModel(); const savedModel = savedProvider && savedModelId ? modelRegistry.find(savedProvider, savedModelId) : undefined; const savedInScope = savedModel ? scopedModels.find((sm) => (0, _piAi.modelsAreEqual)(sm.model, savedModel)) : undefined; if (savedInScope) { options.model = savedInScope.model; // Use thinking level from scoped model config if explicitly set if (!parsed.thinking && savedInScope.thinkingLevel) { options.thinkingLevel = savedInScope.thinkingLevel; } } else { options.model = scopedModels[0].model; // Use thinking level from first scoped model if explicitly set if (!parsed.thinking && scopedModels[0].thinkingLevel) { options.thinkingLevel = scopedModels[0].thinkingLevel; } } } // Thinking level from CLI (takes precedence over scoped model thinking levels set above) if (parsed.thinking) { options.thinkingLevel = parsed.thinking; } // Scoped models for Ctrl+P cycling // Keep thinking level undefined when not explicitly set in the model pattern. // Undefined means "inherit current session thinking level" during cycling. if (scopedModels.length > 0) { options.scopedModels = scopedModels.map((sm) => ({ model: sm.model, thinkingLevel: sm.thinkingLevel })); } // API key from CLI - set in authStorage // (handled by caller before createAgentSession) // Tools if (parsed.noTools) { // --no-tools: start with no built-in tools // --tools can still add specific ones back if (parsed.tools && parsed.tools.length > 0) { options.tools = parsed.tools.map((name) => _index2.allTools[name]); } else { options.tools = []; } } else if (parsed.tools) { options.tools = parsed.tools.map((name) => _index2.allTools[name]); } return { options, cliThinkingFromModel }; } async function handleConfigCommand(args) { if (args[0] !== "config") { return false; } const cwd = process.cwd(); const agentDir = (0, _config.getAgentDir)(); const settingsManager = _settingsManager.SettingsManager.create(cwd, agentDir); reportSettingsErrors(settingsManager, "config command"); const packageManager = new _packageManager.DefaultPackageManager({ cwd, agentDir, settingsManager }); const resolvedPaths = await packageManager.resolve(); await (0, _configSelector.selectConfig)({ resolvedPaths, settingsManager, cwd, agentDir }); process.exit(0); } async function main(args) { const offlineMode = args.includes("--offline") || isTruthyEnvFlag(process.env.PI_OFFLINE); if (offlineMode) { process.env.PI_OFFLINE = "1"; process.env.PI_SKIP_VERSION_CHECK = "1"; } if (await handlePackageCommand(args)) { return; } if (await handleConfigCommand(args)) { return; } // Run migrations (pass cwd for project-local migrations) const { migratedAuthProviders: migratedProviders, deprecationWarnings } = (0, _migrations.runMigrations)(process.cwd()); // First pass: parse args to get --extension paths const firstPass = (0, _args.parseArgs)(args); // Early load extensions to discover their CLI flags const cwd = process.cwd(); const agentDir = (0, _config.getAgentDir)(); const settingsManager = _settingsManager.SettingsManager.create(cwd, agentDir); reportSettingsErrors(settingsManager, "startup"); const authStorage = _authStorage.AuthStorage.create(); const modelRegistry = new _modelRegistry.ModelRegistry(authStorage, (0, _config.getModelsPath)()); const resourceLoader = new _resourceLoader.DefaultResourceLoader({ cwd, agentDir, settingsManager, additionalExtensionPaths: firstPass.extensions, additionalSkillPaths: firstPass.skills, additionalPromptTemplatePaths: firstPass.promptTemplates, additionalThemePaths: firstPass.themes, noExtensions: firstPass.noExtensions, noSkills: firstPass.noSkills, noPromptTemplates: firstPass.noPromptTemplates, noThemes: firstPass.noThemes, systemPrompt: firstPass.systemPrompt, appendSystemPrompt: firstPass.appendSystemPrompt }); await resourceLoader.reload(); (0, _timings.time)("resourceLoader.reload"); const extensionsResult = resourceLoader.getExtensions(); for (const { path, error } of extensionsResult.errors) { console.error(_chalk.default.red(`Failed to load extension "${path}": ${error}`)); } // Apply pending provider registrations from extensions immediately // so they're available for model resolution before AgentSession is created for (const { name, config } of extensionsResult.runtime.pendingProviderRegistrations) { modelRegistry.registerProvider(name, config); } extensionsResult.runtime.pendingProviderRegistrations = []; const extensionFlags = new Map(); for (const ext of extensionsResult.extensions) { for (const [name, flag] of ext.flags) { extensionFlags.set(name, { type: flag.type }); } } // Second pass: parse args with extension flags const parsed = (0, _args.parseArgs)(args, extensionFlags); // Pass flag values to extensions via runtime for (const [name, value] of parsed.unknownFlags) { extensionsResult.runtime.flagValues.set(name, value); } if (parsed.version) { console.log(_config.VERSION); process.exit(0); } if (parsed.help) { (0, _args.printHelp)(); process.exit(0); } if (parsed.listModels !== undefined) { const searchPattern = typeof parsed.listModels === "string" ? parsed.listModels : undefined; await (0, _listModels.listModels)(modelRegistry, searchPattern); process.exit(0); } // Read piped stdin content (if any) - skip for RPC mode which uses stdin for JSON-RPC if (parsed.mode !== "rpc") { const stdinContent = await readPipedStdin(); if (stdinContent !== undefined) { // Force print mode since interactive mode requires a TTY for keyboard input parsed.print = true; // Prepend stdin content to messages parsed.messages.unshift(stdinContent); } } if (parsed.export) { let result; try { const outputPath = parsed.messages.length > 0 ? parsed.messages[0] : undefined; result = await (0, _index.exportFromFile)(parsed.export, outputPath); } catch (error) { const message = error instanceof Error ? error.message : "Failed to export session"; console.error(_chalk.default.red(`Error: ${message}`)); process.exit(1); } console.log(`Exported to: ${result}`); process.exit(0); } if (parsed.mode === "rpc" && parsed.fileArgs.length > 0) { console.error(_chalk.default.red("Error: @file arguments are not supported in RPC mode")); process.exit(1); } const { initialMessage, initialImages } = await prepareInitialMessage(parsed, settingsManager.getImageAutoResize()); const isInteractive = !parsed.print && parsed.mode === undefined; const mode = parsed.mode || "text"; (0, _theme.initTheme)(settingsManager.getTheme(), isInteractive); // Show deprecation warnings in interactive mode if (isInteractive && deprecationWarnings.length > 0) { await (0, _migrations.showDeprecationWarnings)(deprecationWarnings); } let scopedModels = []; const modelPatterns = parsed.models ?? settingsManager.getEnabledModels(); if (modelPatterns && modelPatterns.length > 0) { scopedModels = await (0, _modelResolver.resolveModelScope)(modelPatterns, modelRegistry); } // Create session manager based on CLI flags let sessionManager = await createSessionManager(parsed, cwd, extensionsResult); // Handle --resume: show session picker if (parsed.resume) { // Initialize keybindings so session picker respects user config _keybindings.KeybindingsManager.create(); // Compute effective session dir for resume (same logic as createSessionManager) const effectiveSessionDir = parsed.sessionDir || (await callSessionDirectoryHook(extensionsResult, cwd)); const selectedPath = await (0, _sessionPicker.selectSession)((onProgress) => _sessionManager.SessionManager.list(cwd, effectiveSessionDir, onProgress), _sessionManager.SessionManager.listAll); if (!selectedPath) { console.log(_chalk.default.dim("No session selected")); (0, _theme.stopThemeWatcher)(); process.exit(0); } sessionManager = _sessionManager.SessionManager.open(selectedPath, effectiveSessionDir); } const { options: sessionOptions, cliThinkingFromModel } = buildSessionOptions(parsed, scopedModels, sessionManager, modelRegistry, settingsManager); sessionOptions.authStorage = authStorage; sessionOptions.modelRegistry = modelRegistry; sessionOptions.resourceLoader = resourceLoader; // Handle CLI --api-key as runtime override (not persisted) if (parsed.apiKey) { if (!sessionOptions.model) { console.error(_chalk.default.red("--api-key requires a model to be specified via --model, --provider/--model, or --models")); process.exit(1); } authStorage.setRuntimeApiKey(sessionOptions.model.provider, parsed.apiKey); } const { session, modelFallbackMessage } = await (0, _sdk.createAgentSession)(sessionOptions); if (!isInteractive && !session.model) { console.error(_chalk.default.red("No models available.")); console.error(_chalk.default.yellow("\nSet an API key environment variable:")); console.error(" ANTHROPIC_API_KEY, OPENAI_API_KEY, GEMINI_API_KEY, etc."); console.error(_chalk.default.yellow(`\nOr create ${(0, _config.getModelsPath)()}`)); process.exit(1); } // Clamp thinking level to model capabilities for CLI-provided thinking levels. // This covers both --thinking and --model :. const cliThinkingOverride = parsed.thinking !== undefined || cliThinkingFromModel; if (session.model && cliThinkingOverride) { let effectiveThinking = session.thinkingLevel; if (!session.model.reasoning) { effectiveThinking = "off"; } else if (effectiveThinking === "xhigh" && !(0, _piAi.supportsXhigh)(session.model)) { effectiveThinking = "high"; } if (effectiveThinking !== session.thinkingLevel) { session.setThinkingLevel(effectiveThinking); } } if (mode === "rpc") { await (0, _index3.runRpcMode)(session); } else if (isInteractive) { if (scopedModels.length > 0 && (parsed.verbose || !settingsManager.getQuietStartup())) { const modelList = scopedModels. map((sm) => { const thinkingStr = sm.thinkingLevel ? `:${sm.thinkingLevel}` : ""; return `${sm.model.id}${thinkingStr}`; }). join(", "); console.log(_chalk.default.dim(`Model scope: ${modelList} ${_chalk.default.gray("(Ctrl+P to cycle)")}`)); } (0, _timings.printTimings)(); const mode = new _index3.InteractiveMode(session, { migratedProviders, modelFallbackMessage, initialMessage, initialImages, initialMessages: parsed.messages, verbose: parsed.verbose }); await mode.run(); } else { await (0, _index3.runPrintMode)(session, { mode, messages: parsed.messages, initialMessage, initialImages }); (0, _theme.stopThemeWatcher)(); if (process.stdout.writableLength > 0) { await new Promise((resolve) => process.stdout.once("drain", resolve)); } process.exit(0); } } /* v9-8cc4640a4151063a */