@vandeepunk/pi-coding-agent 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/CHANGELOG.md +105 -0
  2. package/README.md +6 -6
  3. package/dist/cli/args.d.ts.map +1 -1
  4. package/dist/cli/args.js +1 -0
  5. package/dist/cli/args.js.map +1 -1
  6. package/dist/config.d.ts +1 -1
  7. package/dist/config.d.ts.map +1 -1
  8. package/dist/config.js +2 -2
  9. package/dist/config.js.map +1 -1
  10. package/dist/core/agent-session.d.ts.map +1 -1
  11. package/dist/core/agent-session.js +7 -0
  12. package/dist/core/agent-session.js.map +1 -1
  13. package/dist/core/auth-storage.d.ts.map +1 -1
  14. package/dist/core/auth-storage.js +16 -0
  15. package/dist/core/auth-storage.js.map +1 -1
  16. package/dist/core/export-html/template.css +3 -0
  17. package/dist/core/export-html/template.js +32 -15
  18. package/dist/core/extensions/loader.d.ts.map +1 -1
  19. package/dist/core/extensions/loader.js.map +1 -1
  20. package/dist/core/extensions/runner.d.ts +17 -2
  21. package/dist/core/extensions/runner.d.ts.map +1 -1
  22. package/dist/core/extensions/runner.js +53 -9
  23. package/dist/core/extensions/runner.js.map +1 -1
  24. package/dist/core/extensions/wrapper.d.ts.map +1 -1
  25. package/dist/core/extensions/wrapper.js +3 -3
  26. package/dist/core/extensions/wrapper.js.map +1 -1
  27. package/dist/core/model-registry.d.ts +3 -1
  28. package/dist/core/model-registry.d.ts.map +1 -1
  29. package/dist/core/model-registry.js +133 -37
  30. package/dist/core/model-registry.js.map +1 -1
  31. package/dist/core/model-resolver.d.ts.map +1 -1
  32. package/dist/core/model-resolver.js +5 -5
  33. package/dist/core/model-resolver.js.map +1 -1
  34. package/dist/core/package-manager.d.ts +21 -1
  35. package/dist/core/package-manager.d.ts.map +1 -1
  36. package/dist/core/package-manager.js +134 -33
  37. package/dist/core/package-manager.js.map +1 -1
  38. package/dist/core/prompt-templates.d.ts +3 -3
  39. package/dist/core/prompt-templates.d.ts.map +1 -1
  40. package/dist/core/prompt-templates.js +15 -15
  41. package/dist/core/prompt-templates.js.map +1 -1
  42. package/dist/core/resource-loader.d.ts.map +1 -1
  43. package/dist/core/resource-loader.js +6 -6
  44. package/dist/core/resource-loader.js.map +1 -1
  45. package/dist/core/settings-manager.d.ts +2 -2
  46. package/dist/core/settings-manager.d.ts.map +1 -1
  47. package/dist/core/settings-manager.js +4 -4
  48. package/dist/core/settings-manager.js.map +1 -1
  49. package/dist/core/skills.d.ts.map +1 -1
  50. package/dist/core/skills.js +57 -3
  51. package/dist/core/skills.js.map +1 -1
  52. package/dist/core/slash-commands.d.ts.map +1 -1
  53. package/dist/core/slash-commands.js +1 -0
  54. package/dist/core/slash-commands.js.map +1 -1
  55. package/dist/main.d.ts.map +1 -1
  56. package/dist/main.js +172 -177
  57. package/dist/main.js.map +1 -1
  58. package/dist/migrations.d.ts.map +1 -1
  59. package/dist/migrations.js +11 -11
  60. package/dist/migrations.js.map +1 -1
  61. package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
  62. package/dist/modes/interactive/components/assistant-message.js +9 -4
  63. package/dist/modes/interactive/components/assistant-message.js.map +1 -1
  64. package/dist/modes/interactive/components/config-selector.d.ts +1 -1
  65. package/dist/modes/interactive/components/config-selector.d.ts.map +1 -1
  66. package/dist/modes/interactive/components/config-selector.js +6 -6
  67. package/dist/modes/interactive/components/config-selector.js.map +1 -1
  68. package/dist/modes/interactive/components/model-selector.d.ts.map +1 -1
  69. package/dist/modes/interactive/components/model-selector.js +5 -0
  70. package/dist/modes/interactive/components/model-selector.js.map +1 -1
  71. package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -1
  72. package/dist/modes/interactive/components/scoped-models-selector.js +5 -0
  73. package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -1
  74. package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
  75. package/dist/modes/interactive/components/tool-execution.js +49 -34
  76. package/dist/modes/interactive/components/tool-execution.js.map +1 -1
  77. package/dist/modes/interactive/interactive-mode.d.ts +0 -1
  78. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  79. package/dist/modes/interactive/interactive-mode.js +117 -104
  80. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  81. package/dist/utils/git.d.ts +21 -1
  82. package/dist/utils/git.d.ts.map +1 -1
  83. package/dist/utils/git.js +150 -4
  84. package/dist/utils/git.js.map +1 -1
  85. package/docs/extensions.md +5 -0
  86. package/docs/models.md +40 -1
  87. package/docs/packages.md +23 -3
  88. package/docs/prompt-templates.md +6 -6
  89. package/docs/providers.md +13 -0
  90. package/docs/rpc.md +1 -1
  91. package/docs/sdk.md +5 -3
  92. package/docs/settings.md +2 -2
  93. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  94. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  95. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  96. package/examples/extensions/custom-provider-qwen-cli/package.json +1 -1
  97. package/examples/extensions/hello.ts +1 -1
  98. package/examples/extensions/subagent/README.md +4 -4
  99. package/examples/extensions/with-deps/package-lock.json +2 -2
  100. package/examples/extensions/with-deps/package.json +1 -1
  101. package/examples/sdk/08-prompt-templates.ts +2 -2
  102. package/package.json +7 -8
  103. /package/examples/extensions/subagent/{prompts → commands}/implement-and-review.md +0 -0
  104. /package/examples/extensions/subagent/{prompts → commands}/implement.md +0 -0
  105. /package/examples/extensions/subagent/{prompts → commands}/scout-and-plan.md +0 -0
@@ -1 +1 @@
1
- {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AA4fH,wBAAsB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,iBA6NxC","sourcesContent":["/**\n * Main entry point for the coding agent CLI.\n *\n * This file handles CLI argument parsing and translates them into\n * createAgentSession() options. The SDK does the heavy lifting.\n */\n\nimport { homedir } from \"node:os\";\nimport { isAbsolute, join, relative, resolve } from \"node:path\";\nimport { type ImageContent, modelsAreEqual, supportsXhigh } from \"@mariozechner/pi-ai\";\nimport chalk from \"chalk\";\nimport { createInterface } from \"readline\";\nimport { type Args, parseArgs, printHelp } from \"./cli/args.js\";\nimport { selectConfig } from \"./cli/config-selector.js\";\nimport { processFileArguments } from \"./cli/file-processor.js\";\nimport { listModels } from \"./cli/list-models.js\";\nimport { selectSession } from \"./cli/session-picker.js\";\nimport { CONFIG_DIR_NAME, getAgentDir, getModelsPath, VERSION } from \"./config.js\";\nimport { AuthStorage } from \"./core/auth-storage.js\";\nimport { DEFAULT_THINKING_LEVEL } from \"./core/defaults.js\";\nimport { exportFromFile } from \"./core/export-html/index.js\";\nimport type { LoadExtensionsResult } from \"./core/extensions/index.js\";\nimport { KeybindingsManager } from \"./core/keybindings.js\";\nimport { ModelRegistry } from \"./core/model-registry.js\";\nimport { resolveModelScope, type ScopedModel } from \"./core/model-resolver.js\";\nimport { DefaultPackageManager } from \"./core/package-manager.js\";\nimport { DefaultResourceLoader } from \"./core/resource-loader.js\";\nimport { type CreateAgentSessionOptions, createAgentSession } from \"./core/sdk.js\";\nimport { SessionManager } from \"./core/session-manager.js\";\nimport { type PackageSource, SettingsManager } from \"./core/settings-manager.js\";\nimport { printTimings, time } from \"./core/timings.js\";\nimport { allTools } from \"./core/tools/index.js\";\nimport { runMigrations, showDeprecationWarnings } from \"./migrations.js\";\nimport { InteractiveMode, runPrintMode, runRpcMode } from \"./modes/index.js\";\nimport { initTheme, stopThemeWatcher } from \"./modes/interactive/theme/theme.js\";\n\n/**\n * Read all content from piped stdin.\n * Returns undefined if stdin is a TTY (interactive terminal).\n */\nasync function readPipedStdin(): Promise<string | undefined> {\n\t// If stdin is a TTY, we're running interactively - don't read stdin\n\tif (process.stdin.isTTY) {\n\t\treturn undefined;\n\t}\n\n\treturn new Promise((resolve) => {\n\t\tlet data = \"\";\n\t\tprocess.stdin.setEncoding(\"utf8\");\n\t\tprocess.stdin.on(\"data\", (chunk) => {\n\t\t\tdata += chunk;\n\t\t});\n\t\tprocess.stdin.on(\"end\", () => {\n\t\t\tresolve(data.trim() || undefined);\n\t\t});\n\t\tprocess.stdin.resume();\n\t});\n}\n\ntype PackageCommand = \"install\" | \"remove\" | \"update\" | \"list\";\n\ninterface PackageCommandOptions {\n\tcommand: PackageCommand;\n\tsource?: string;\n\tlocal: boolean;\n}\n\nfunction parsePackageCommand(args: string[]): PackageCommandOptions | undefined {\n\tconst [command, ...rest] = args;\n\tif (command !== \"install\" && command !== \"remove\" && command !== \"update\" && command !== \"list\") {\n\t\treturn undefined;\n\t}\n\n\tlet local = false;\n\tconst sources: string[] = [];\n\tfor (const arg of rest) {\n\t\tif (arg === \"-l\" || arg === \"--local\") {\n\t\t\tlocal = true;\n\t\t\tcontinue;\n\t\t}\n\t\tsources.push(arg);\n\t}\n\n\treturn { command, source: sources[0], local };\n}\n\nfunction expandTildePath(input: string): string {\n\tconst trimmed = input.trim();\n\tif (trimmed === \"~\") return homedir();\n\tif (trimmed.startsWith(\"~/\")) return resolve(homedir(), trimmed.slice(2));\n\tif (trimmed.startsWith(\"~\")) return resolve(homedir(), trimmed.slice(1));\n\treturn trimmed;\n}\n\nfunction resolveLocalSourceFromInput(source: string, cwd: string): string {\n\tconst expanded = expandTildePath(source);\n\treturn isAbsolute(expanded) ? resolve(expanded) : resolve(cwd, expanded);\n}\n\nfunction resolveLocalSourceFromSettings(source: string, baseDir: string): string {\n\tconst expanded = expandTildePath(source);\n\treturn isAbsolute(expanded) ? expanded : resolve(baseDir, expanded);\n}\n\nfunction normalizeLocalSourceForSettings(source: string, baseDir: string, cwd: string): string {\n\tconst resolved = resolveLocalSourceFromInput(source, cwd);\n\tconst rel = relative(baseDir, resolved);\n\treturn rel || \".\";\n}\n\nfunction normalizePackageSourceForSettings(source: string, baseDir: string, cwd: string): string {\n\tconst normalized = normalizeExtensionSource(source);\n\tif (normalized.type !== \"local\") {\n\t\treturn source;\n\t}\n\treturn normalizeLocalSourceForSettings(source, baseDir, cwd);\n}\n\nfunction normalizeExtensionSource(source: string): { type: \"npm\" | \"git\" | \"local\"; key: string } {\n\tif (source.startsWith(\"npm:\")) {\n\t\tconst spec = source.slice(\"npm:\".length).trim();\n\t\tconst match = spec.match(/^(@?[^@]+(?:\\/[^@]+)?)(?:@.+)?$/);\n\t\treturn { type: \"npm\", key: match?.[1] ?? spec };\n\t}\n\tif (source.startsWith(\"git:\")) {\n\t\tconst repo = source.slice(\"git:\".length).trim().split(\"@\")[0] ?? \"\";\n\t\treturn { type: \"git\", key: repo.replace(/^https?:\\/\\//, \"\").replace(/\\.git$/, \"\") };\n\t}\n\t// Raw git URLs\n\tif (source.startsWith(\"https://\") || source.startsWith(\"http://\")) {\n\t\tconst repo = source.split(\"@\")[0] ?? \"\";\n\t\treturn { type: \"git\", key: repo.replace(/^https?:\\/\\//, \"\").replace(/\\.git$/, \"\") };\n\t}\n\treturn { type: \"local\", key: source };\n}\n\nfunction normalizeSourceForInput(source: string, cwd: string): { type: \"npm\" | \"git\" | \"local\"; key: string } {\n\tconst normalized = normalizeExtensionSource(source);\n\tif (normalized.type !== \"local\") {\n\t\treturn normalized;\n\t}\n\treturn { type: \"local\", key: resolveLocalSourceFromInput(source, cwd) };\n}\n\nfunction normalizeSourceForSettings(source: string, baseDir: string): { type: \"npm\" | \"git\" | \"local\"; key: string } {\n\tconst normalized = normalizeExtensionSource(source);\n\tif (normalized.type !== \"local\") {\n\t\treturn normalized;\n\t}\n\treturn { type: \"local\", key: resolveLocalSourceFromSettings(source, baseDir) };\n}\n\nfunction sourcesMatch(a: string, b: string, baseDir: string, cwd: string): boolean {\n\tconst left = normalizeSourceForSettings(a, baseDir);\n\tconst right = normalizeSourceForInput(b, cwd);\n\treturn left.type === right.type && left.key === right.key;\n}\n\nfunction getPackageSourceString(pkg: PackageSource): string {\n\treturn typeof pkg === \"string\" ? pkg : pkg.source;\n}\n\nfunction packageSourcesMatch(a: PackageSource, b: string, baseDir: string, cwd: string): boolean {\n\tconst aSource = getPackageSourceString(a);\n\treturn sourcesMatch(aSource, b, baseDir, cwd);\n}\n\nfunction updatePackageSources(\n\tsettingsManager: SettingsManager,\n\tsource: string,\n\tlocal: boolean,\n\tcwd: string,\n\tagentDir: string,\n\taction: \"add\" | \"remove\",\n): boolean {\n\tconst currentSettings = local ? settingsManager.getProjectSettings() : settingsManager.getGlobalSettings();\n\tconst currentPackages = currentSettings.packages ?? [];\n\tconst baseDir = local ? join(cwd, CONFIG_DIR_NAME) : agentDir;\n\tconst normalizedSource = normalizePackageSourceForSettings(source, baseDir, cwd);\n\n\tlet nextPackages: PackageSource[];\n\tlet changed = false;\n\tif (action === \"add\") {\n\t\tconst exists = currentPackages.some((existing) => packageSourcesMatch(existing, source, baseDir, cwd));\n\t\tnextPackages = exists ? currentPackages : [...currentPackages, normalizedSource];\n\t\tchanged = !exists;\n\t} else {\n\t\tnextPackages = currentPackages.filter((existing) => !packageSourcesMatch(existing, source, baseDir, cwd));\n\t\tchanged = nextPackages.length !== currentPackages.length;\n\t}\n\n\tif (local) {\n\t\tsettingsManager.setProjectPackages(nextPackages);\n\t} else {\n\t\tsettingsManager.setPackages(nextPackages);\n\t}\n\n\treturn changed;\n}\n\nasync function handlePackageCommand(args: string[]): Promise<boolean> {\n\tconst options = parsePackageCommand(args);\n\tif (!options) {\n\t\treturn false;\n\t}\n\n\tconst cwd = process.cwd();\n\tconst agentDir = getAgentDir();\n\tconst settingsManager = SettingsManager.create(cwd, agentDir);\n\tconst packageManager = new DefaultPackageManager({ cwd, agentDir, settingsManager });\n\n\t// Set up progress callback for CLI feedback\n\tpackageManager.setProgressCallback((event) => {\n\t\tif (event.type === \"start\") {\n\t\t\tprocess.stdout.write(chalk.dim(`${event.message}\\n`));\n\t\t} else if (event.type === \"error\") {\n\t\t\tconsole.error(chalk.red(`Error: ${event.message}`));\n\t\t}\n\t});\n\n\tif (options.command === \"install\") {\n\t\tif (!options.source) {\n\t\t\tconsole.error(chalk.red(\"Missing install source.\"));\n\t\t\tprocess.exit(1);\n\t\t}\n\t\tawait packageManager.install(options.source, { local: options.local });\n\t\tupdatePackageSources(settingsManager, options.source, options.local, cwd, agentDir, \"add\");\n\t\tconsole.log(chalk.green(`Installed ${options.source}`));\n\t\treturn true;\n\t}\n\n\tif (options.command === \"remove\") {\n\t\tif (!options.source) {\n\t\t\tconsole.error(chalk.red(\"Missing remove source.\"));\n\t\t\tprocess.exit(1);\n\t\t}\n\t\tawait packageManager.remove(options.source, { local: options.local });\n\t\tconst removed = updatePackageSources(settingsManager, options.source, options.local, cwd, agentDir, \"remove\");\n\t\tif (!removed) {\n\t\t\tconsole.error(chalk.red(`No matching package found for ${options.source}`));\n\t\t\tprocess.exit(1);\n\t\t}\n\t\tconsole.log(chalk.green(`Removed ${options.source}`));\n\t\treturn true;\n\t}\n\n\tif (options.command === \"list\") {\n\t\tconst globalSettings = settingsManager.getGlobalSettings();\n\t\tconst projectSettings = settingsManager.getProjectSettings();\n\t\tconst globalPackages = globalSettings.packages ?? [];\n\t\tconst projectPackages = projectSettings.packages ?? [];\n\n\t\tif (globalPackages.length === 0 && projectPackages.length === 0) {\n\t\t\tconsole.log(chalk.dim(\"No packages installed.\"));\n\t\t\treturn true;\n\t\t}\n\n\t\tconst formatPackage = (pkg: (typeof globalPackages)[number], scope: \"user\" | \"project\") => {\n\t\t\tconst source = typeof pkg === \"string\" ? pkg : pkg.source;\n\t\t\tconst filtered = typeof pkg === \"object\";\n\t\t\tconst display = filtered ? `${source} (filtered)` : source;\n\t\t\tconsole.log(` ${display}`);\n\t\t\t// Show resolved path\n\t\t\tconst path = packageManager.getInstalledPath(source, scope);\n\t\t\tif (path) {\n\t\t\t\tconsole.log(chalk.dim(` ${path}`));\n\t\t\t}\n\t\t};\n\n\t\tif (globalPackages.length > 0) {\n\t\t\tconsole.log(chalk.bold(\"User packages:\"));\n\t\t\tfor (const pkg of globalPackages) {\n\t\t\t\tformatPackage(pkg, \"user\");\n\t\t\t}\n\t\t}\n\n\t\tif (projectPackages.length > 0) {\n\t\t\tif (globalPackages.length > 0) console.log();\n\t\t\tconsole.log(chalk.bold(\"Project packages:\"));\n\t\t\tfor (const pkg of projectPackages) {\n\t\t\t\tformatPackage(pkg, \"project\");\n\t\t\t}\n\t\t}\n\n\t\treturn true;\n\t}\n\n\tawait packageManager.update(options.source);\n\tif (options.source) {\n\t\tconsole.log(chalk.green(`Updated ${options.source}`));\n\t} else {\n\t\tconsole.log(chalk.green(\"Updated packages\"));\n\t}\n\treturn true;\n}\n\nasync function prepareInitialMessage(\n\tparsed: Args,\n\tautoResizeImages: boolean,\n): Promise<{\n\tinitialMessage?: string;\n\tinitialImages?: ImageContent[];\n}> {\n\tif (parsed.fileArgs.length === 0) {\n\t\treturn {};\n\t}\n\n\tconst { text, images } = await processFileArguments(parsed.fileArgs, { autoResizeImages });\n\n\tlet initialMessage: string;\n\tif (parsed.messages.length > 0) {\n\t\tinitialMessage = text + parsed.messages[0];\n\t\tparsed.messages.shift();\n\t} else {\n\t\tinitialMessage = text;\n\t}\n\n\treturn {\n\t\tinitialMessage,\n\t\tinitialImages: images.length > 0 ? images : undefined,\n\t};\n}\n\n/** Result from resolving a session argument */\ntype ResolvedSession =\n\t| { type: \"path\"; path: string } // Direct file path\n\t| { type: \"local\"; path: string } // Found in current project\n\t| { type: \"global\"; path: string; cwd: string } // Found in different project\n\t| { type: \"not_found\"; arg: string }; // Not found anywhere\n\n/**\n * Resolve a session argument to a file path.\n * If it looks like a path, use as-is. Otherwise try to match as session ID prefix.\n */\nasync function resolveSessionPath(sessionArg: string, cwd: string, sessionDir?: string): Promise<ResolvedSession> {\n\t// If it looks like a file path, use as-is\n\tif (sessionArg.includes(\"/\") || sessionArg.includes(\"\\\\\") || sessionArg.endsWith(\".jsonl\")) {\n\t\treturn { type: \"path\", path: sessionArg };\n\t}\n\n\t// Try to match as session ID in current project first\n\tconst localSessions = await SessionManager.list(cwd, sessionDir);\n\tconst localMatches = localSessions.filter((s) => s.id.startsWith(sessionArg));\n\n\tif (localMatches.length >= 1) {\n\t\treturn { type: \"local\", path: localMatches[0].path };\n\t}\n\n\t// Try global search across all projects\n\tconst allSessions = await SessionManager.listAll();\n\tconst globalMatches = allSessions.filter((s) => s.id.startsWith(sessionArg));\n\n\tif (globalMatches.length >= 1) {\n\t\tconst match = globalMatches[0];\n\t\treturn { type: \"global\", path: match.path, cwd: match.cwd };\n\t}\n\n\t// Not found anywhere\n\treturn { type: \"not_found\", arg: sessionArg };\n}\n\n/** Prompt user for yes/no confirmation */\nasync function promptConfirm(message: string): Promise<boolean> {\n\treturn new Promise((resolve) => {\n\t\tconst rl = createInterface({\n\t\t\tinput: process.stdin,\n\t\t\toutput: process.stdout,\n\t\t});\n\t\trl.question(`${message} [y/N] `, (answer) => {\n\t\t\trl.close();\n\t\t\tresolve(answer.toLowerCase() === \"y\" || answer.toLowerCase() === \"yes\");\n\t\t});\n\t});\n}\n\nasync function createSessionManager(parsed: Args, cwd: string): Promise<SessionManager | undefined> {\n\tif (parsed.noSession) {\n\t\treturn SessionManager.inMemory();\n\t}\n\tif (parsed.session) {\n\t\tconst resolved = await resolveSessionPath(parsed.session, cwd, parsed.sessionDir);\n\n\t\tswitch (resolved.type) {\n\t\t\tcase \"path\":\n\t\t\tcase \"local\":\n\t\t\t\treturn SessionManager.open(resolved.path, parsed.sessionDir);\n\n\t\t\tcase \"global\": {\n\t\t\t\t// Session found in different project - ask user if they want to fork\n\t\t\t\tconsole.log(chalk.yellow(`Session found in different project: ${resolved.cwd}`));\n\t\t\t\tconst shouldFork = await promptConfirm(\"Fork this session into current directory?\");\n\t\t\t\tif (!shouldFork) {\n\t\t\t\t\tconsole.log(chalk.dim(\"Aborted.\"));\n\t\t\t\t\tprocess.exit(0);\n\t\t\t\t}\n\t\t\t\treturn SessionManager.forkFrom(resolved.path, cwd, parsed.sessionDir);\n\t\t\t}\n\n\t\t\tcase \"not_found\":\n\t\t\t\tconsole.error(chalk.red(`No session found matching '${resolved.arg}'`));\n\t\t\t\tprocess.exit(1);\n\t\t}\n\t}\n\tif (parsed.continue) {\n\t\treturn SessionManager.continueRecent(cwd, parsed.sessionDir);\n\t}\n\t// --resume is handled separately (needs picker UI)\n\t// If --session-dir provided without --continue/--resume, create new session there\n\tif (parsed.sessionDir) {\n\t\treturn SessionManager.create(cwd, parsed.sessionDir);\n\t}\n\t// Default case (new session) returns undefined, SDK will create one\n\treturn undefined;\n}\n\nfunction buildSessionOptions(\n\tparsed: Args,\n\tscopedModels: ScopedModel[],\n\tsessionManager: SessionManager | undefined,\n\tmodelRegistry: ModelRegistry,\n\tsettingsManager: SettingsManager,\n): CreateAgentSessionOptions {\n\tconst options: CreateAgentSessionOptions = {};\n\n\tif (sessionManager) {\n\t\toptions.sessionManager = sessionManager;\n\t}\n\n\t// Model from CLI\n\tif (parsed.provider && parsed.model) {\n\t\tconst model = modelRegistry.find(parsed.provider, parsed.model);\n\t\tif (!model) {\n\t\t\tconsole.error(chalk.red(`Model ${parsed.provider}/${parsed.model} not found`));\n\t\t\tprocess.exit(1);\n\t\t}\n\t\toptions.model = model;\n\t} else if (scopedModels.length > 0 && !parsed.continue && !parsed.resume) {\n\t\t// Check if saved default is in scoped models - use it if so, otherwise first scoped model\n\t\tconst savedProvider = settingsManager.getDefaultProvider();\n\t\tconst savedModelId = settingsManager.getDefaultModel();\n\t\tconst savedModel = savedProvider && savedModelId ? modelRegistry.find(savedProvider, savedModelId) : undefined;\n\t\tconst savedInScope = savedModel ? scopedModels.find((sm) => modelsAreEqual(sm.model, savedModel)) : undefined;\n\n\t\tif (savedInScope) {\n\t\t\toptions.model = savedInScope.model;\n\t\t\t// Use thinking level from scoped model config if explicitly set\n\t\t\tif (!parsed.thinking && savedInScope.thinkingLevel) {\n\t\t\t\toptions.thinkingLevel = savedInScope.thinkingLevel;\n\t\t\t}\n\t\t} else {\n\t\t\toptions.model = scopedModels[0].model;\n\t\t\t// Use thinking level from first scoped model if explicitly set\n\t\t\tif (!parsed.thinking && scopedModels[0].thinkingLevel) {\n\t\t\t\toptions.thinkingLevel = scopedModels[0].thinkingLevel;\n\t\t\t}\n\t\t}\n\t}\n\n\t// Thinking level from CLI (takes precedence over scoped model thinking levels set above)\n\tif (parsed.thinking) {\n\t\toptions.thinkingLevel = parsed.thinking;\n\t}\n\n\t// Scoped models for Ctrl+P cycling - fill in default thinking level for models without explicit level\n\tif (scopedModels.length > 0) {\n\t\tconst defaultThinkingLevel = settingsManager.getDefaultThinkingLevel() ?? DEFAULT_THINKING_LEVEL;\n\t\toptions.scopedModels = scopedModels.map((sm) => ({\n\t\t\tmodel: sm.model,\n\t\t\tthinkingLevel: sm.thinkingLevel ?? defaultThinkingLevel,\n\t\t}));\n\t}\n\n\t// API key from CLI - set in authStorage\n\t// (handled by caller before createAgentSession)\n\n\t// Tools\n\tif (parsed.noTools) {\n\t\t// --no-tools: start with no built-in tools\n\t\t// --tools can still add specific ones back\n\t\tif (parsed.tools && parsed.tools.length > 0) {\n\t\t\toptions.tools = parsed.tools.map((name) => allTools[name]);\n\t\t} else {\n\t\t\toptions.tools = [];\n\t\t}\n\t} else if (parsed.tools) {\n\t\toptions.tools = parsed.tools.map((name) => allTools[name]);\n\t}\n\n\treturn options;\n}\n\nasync function handleConfigCommand(args: string[]): Promise<boolean> {\n\tif (args[0] !== \"config\") {\n\t\treturn false;\n\t}\n\n\tconst cwd = process.cwd();\n\tconst agentDir = getAgentDir();\n\tconst settingsManager = SettingsManager.create(cwd, agentDir);\n\tconst packageManager = new DefaultPackageManager({ cwd, agentDir, settingsManager });\n\n\tconst resolvedPaths = await packageManager.resolve();\n\n\tawait selectConfig({\n\t\tresolvedPaths,\n\t\tsettingsManager,\n\t\tcwd,\n\t\tagentDir,\n\t});\n\n\tprocess.exit(0);\n}\n\nexport async function main(args: string[]) {\n\tif (await handlePackageCommand(args)) {\n\t\treturn;\n\t}\n\n\tif (await handleConfigCommand(args)) {\n\t\treturn;\n\t}\n\n\t// Run migrations (pass cwd for project-local migrations)\n\tconst { migratedAuthProviders: migratedProviders, deprecationWarnings } = runMigrations(process.cwd());\n\n\t// First pass: parse args to get --extension paths\n\tconst firstPass = parseArgs(args);\n\n\t// Early load extensions to discover their CLI flags\n\tconst cwd = process.cwd();\n\tconst agentDir = getAgentDir();\n\tconst settingsManager = SettingsManager.create(cwd, agentDir);\n\tconst authStorage = new AuthStorage();\n\tconst modelRegistry = new ModelRegistry(authStorage, getModelsPath());\n\n\tconst resourceLoader = new DefaultResourceLoader({\n\t\tcwd,\n\t\tagentDir,\n\t\tsettingsManager,\n\t\tadditionalExtensionPaths: firstPass.extensions,\n\t\tadditionalSkillPaths: firstPass.skills,\n\t\tadditionalPromptTemplatePaths: firstPass.promptTemplates,\n\t\tadditionalThemePaths: firstPass.themes,\n\t\tnoExtensions: firstPass.noExtensions,\n\t\tnoSkills: firstPass.noSkills,\n\t\tnoPromptTemplates: firstPass.noPromptTemplates,\n\t\tnoThemes: firstPass.noThemes,\n\t\tsystemPrompt: firstPass.systemPrompt,\n\t\tappendSystemPrompt: firstPass.appendSystemPrompt,\n\t});\n\tawait resourceLoader.reload();\n\ttime(\"resourceLoader.reload\");\n\n\tconst extensionsResult: LoadExtensionsResult = resourceLoader.getExtensions();\n\tfor (const { path, error } of extensionsResult.errors) {\n\t\tconsole.error(chalk.red(`Failed to load extension \"${path}\": ${error}`));\n\t}\n\n\t// Apply pending provider registrations from extensions immediately\n\t// so they're available for model resolution before AgentSession is created\n\tfor (const { name, config } of extensionsResult.runtime.pendingProviderRegistrations) {\n\t\tmodelRegistry.registerProvider(name, config);\n\t}\n\textensionsResult.runtime.pendingProviderRegistrations = [];\n\n\tconst extensionFlags = new Map<string, { type: \"boolean\" | \"string\" }>();\n\tfor (const ext of extensionsResult.extensions) {\n\t\tfor (const [name, flag] of ext.flags) {\n\t\t\textensionFlags.set(name, { type: flag.type });\n\t\t}\n\t}\n\n\t// Second pass: parse args with extension flags\n\tconst parsed = parseArgs(args, extensionFlags);\n\n\t// Pass flag values to extensions via runtime\n\tfor (const [name, value] of parsed.unknownFlags) {\n\t\textensionsResult.runtime.flagValues.set(name, value);\n\t}\n\n\tif (parsed.version) {\n\t\tconsole.log(VERSION);\n\t\treturn;\n\t}\n\n\tif (parsed.help) {\n\t\tprintHelp();\n\t\treturn;\n\t}\n\n\tif (parsed.listModels !== undefined) {\n\t\tconst searchPattern = typeof parsed.listModels === \"string\" ? parsed.listModels : undefined;\n\t\tawait listModels(modelRegistry, searchPattern);\n\t\treturn;\n\t}\n\n\t// Read piped stdin content (if any) - skip for RPC mode which uses stdin for JSON-RPC\n\tif (parsed.mode !== \"rpc\") {\n\t\tconst stdinContent = await readPipedStdin();\n\t\tif (stdinContent !== undefined) {\n\t\t\t// Force print mode since interactive mode requires a TTY for keyboard input\n\t\t\tparsed.print = true;\n\t\t\t// Prepend stdin content to messages\n\t\t\tparsed.messages.unshift(stdinContent);\n\t\t}\n\t}\n\n\tif (parsed.export) {\n\t\ttry {\n\t\t\tconst outputPath = parsed.messages.length > 0 ? parsed.messages[0] : undefined;\n\t\t\tconst result = await exportFromFile(parsed.export, outputPath);\n\t\t\tconsole.log(`Exported to: ${result}`);\n\t\t\treturn;\n\t\t} catch (error: unknown) {\n\t\t\tconst message = error instanceof Error ? error.message : \"Failed to export session\";\n\t\t\tconsole.error(chalk.red(`Error: ${message}`));\n\t\t\tprocess.exit(1);\n\t\t}\n\t}\n\n\tif (parsed.mode === \"rpc\" && parsed.fileArgs.length > 0) {\n\t\tconsole.error(chalk.red(\"Error: @file arguments are not supported in RPC mode\"));\n\t\tprocess.exit(1);\n\t}\n\n\tconst { initialMessage, initialImages } = await prepareInitialMessage(parsed, settingsManager.getImageAutoResize());\n\tconst isInteractive = !parsed.print && parsed.mode === undefined;\n\tconst mode = parsed.mode || \"text\";\n\tinitTheme(settingsManager.getTheme(), isInteractive);\n\n\t// Show deprecation warnings in interactive mode\n\tif (isInteractive && deprecationWarnings.length > 0) {\n\t\tawait showDeprecationWarnings(deprecationWarnings);\n\t}\n\n\tlet scopedModels: ScopedModel[] = [];\n\tconst modelPatterns = parsed.models ?? settingsManager.getEnabledModels();\n\tif (modelPatterns && modelPatterns.length > 0) {\n\t\tscopedModels = await resolveModelScope(modelPatterns, modelRegistry);\n\t}\n\n\t// Create session manager based on CLI flags\n\tlet sessionManager = await createSessionManager(parsed, cwd);\n\n\t// Handle --resume: show session picker\n\tif (parsed.resume) {\n\t\t// Initialize keybindings so session picker respects user config\n\t\tKeybindingsManager.create();\n\n\t\tconst selectedPath = await selectSession(\n\t\t\t(onProgress) => SessionManager.list(cwd, parsed.sessionDir, onProgress),\n\t\t\tSessionManager.listAll,\n\t\t);\n\t\tif (!selectedPath) {\n\t\t\tconsole.log(chalk.dim(\"No session selected\"));\n\t\t\tstopThemeWatcher();\n\t\t\tprocess.exit(0);\n\t\t}\n\t\tsessionManager = SessionManager.open(selectedPath);\n\t}\n\n\tconst sessionOptions = buildSessionOptions(parsed, scopedModels, sessionManager, modelRegistry, settingsManager);\n\tsessionOptions.authStorage = authStorage;\n\tsessionOptions.modelRegistry = modelRegistry;\n\tsessionOptions.resourceLoader = resourceLoader;\n\n\t// Handle CLI --api-key as runtime override (not persisted)\n\tif (parsed.apiKey) {\n\t\tif (!sessionOptions.model) {\n\t\t\tconsole.error(chalk.red(\"--api-key requires a model to be specified via --provider/--model or -m/--models\"));\n\t\t\tprocess.exit(1);\n\t\t}\n\t\tauthStorage.setRuntimeApiKey(sessionOptions.model.provider, parsed.apiKey);\n\t}\n\n\tconst { session, modelFallbackMessage } = await createAgentSession(sessionOptions);\n\n\tif (!isInteractive && !session.model) {\n\t\tconsole.error(chalk.red(\"No models available.\"));\n\t\tconsole.error(chalk.yellow(\"\\nSet an API key environment variable:\"));\n\t\tconsole.error(\" ANTHROPIC_API_KEY, OPENAI_API_KEY, GEMINI_API_KEY, etc.\");\n\t\tconsole.error(chalk.yellow(`\\nOr create ${getModelsPath()}`));\n\t\tprocess.exit(1);\n\t}\n\n\t// Clamp thinking level to model capabilities (for CLI override case)\n\tif (session.model && parsed.thinking) {\n\t\tlet effectiveThinking = parsed.thinking;\n\t\tif (!session.model.reasoning) {\n\t\t\teffectiveThinking = \"off\";\n\t\t} else if (effectiveThinking === \"xhigh\" && !supportsXhigh(session.model)) {\n\t\t\teffectiveThinking = \"high\";\n\t\t}\n\t\tif (effectiveThinking !== session.thinkingLevel) {\n\t\t\tsession.setThinkingLevel(effectiveThinking);\n\t\t}\n\t}\n\n\tif (mode === \"rpc\") {\n\t\tawait runRpcMode(session);\n\t} else if (isInteractive) {\n\t\tif (scopedModels.length > 0 && (parsed.verbose || !settingsManager.getQuietStartup())) {\n\t\t\tconst modelList = scopedModels\n\t\t\t\t.map((sm) => {\n\t\t\t\t\tconst thinkingStr = sm.thinkingLevel ? `:${sm.thinkingLevel}` : \"\";\n\t\t\t\t\treturn `${sm.model.id}${thinkingStr}`;\n\t\t\t\t})\n\t\t\t\t.join(\", \");\n\t\t\tconsole.log(chalk.dim(`Model scope: ${modelList} ${chalk.gray(\"(Ctrl+P to cycle)\")}`));\n\t\t}\n\n\t\tprintTimings();\n\t\tconst mode = new InteractiveMode(session, {\n\t\t\tmigratedProviders,\n\t\t\tmodelFallbackMessage,\n\t\t\tinitialMessage,\n\t\t\tinitialImages,\n\t\t\tinitialMessages: parsed.messages,\n\t\t\tverbose: parsed.verbose,\n\t\t});\n\t\tawait mode.run();\n\t} else {\n\t\tawait runPrintMode(session, {\n\t\t\tmode,\n\t\t\tmessages: parsed.messages,\n\t\t\tinitialMessage,\n\t\t\tinitialImages,\n\t\t});\n\t\tstopThemeWatcher();\n\t\tif (process.stdout.writableLength > 0) {\n\t\t\tawait new Promise<void>((resolve) => process.stdout.once(\"drain\", resolve));\n\t\t}\n\t\tprocess.exit(0);\n\t}\n}\n"]}
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAkfH,wBAAsB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,iBA8NxC","sourcesContent":["/**\n * Main entry point for the coding agent CLI.\n *\n * This file handles CLI argument parsing and translates them into\n * createAgentSession() options. The SDK does the heavy lifting.\n */\n\nimport { type ImageContent, modelsAreEqual, supportsXhigh } from \"@mariozechner/pi-ai\";\nimport chalk from \"chalk\";\nimport { createInterface } from \"readline\";\nimport { type Args, parseArgs, printHelp } from \"./cli/args.js\";\nimport { selectConfig } from \"./cli/config-selector.js\";\nimport { processFileArguments } from \"./cli/file-processor.js\";\nimport { listModels } from \"./cli/list-models.js\";\nimport { selectSession } from \"./cli/session-picker.js\";\nimport { APP_NAME, getAgentDir, getModelsPath, VERSION } from \"./config.js\";\nimport { AuthStorage } from \"./core/auth-storage.js\";\nimport { DEFAULT_THINKING_LEVEL } from \"./core/defaults.js\";\nimport { exportFromFile } from \"./core/export-html/index.js\";\nimport type { LoadExtensionsResult } from \"./core/extensions/index.js\";\nimport { KeybindingsManager } from \"./core/keybindings.js\";\nimport { ModelRegistry } from \"./core/model-registry.js\";\nimport { resolveModelScope, type ScopedModel } from \"./core/model-resolver.js\";\nimport { DefaultPackageManager } from \"./core/package-manager.js\";\nimport { DefaultResourceLoader } from \"./core/resource-loader.js\";\nimport { type CreateAgentSessionOptions, createAgentSession } from \"./core/sdk.js\";\nimport { SessionManager } from \"./core/session-manager.js\";\nimport { SettingsManager } from \"./core/settings-manager.js\";\nimport { printTimings, time } from \"./core/timings.js\";\nimport { allTools } from \"./core/tools/index.js\";\nimport { runMigrations, showDeprecationWarnings } from \"./migrations.js\";\nimport { InteractiveMode, runPrintMode, runRpcMode } from \"./modes/index.js\";\nimport { initTheme, stopThemeWatcher } from \"./modes/interactive/theme/theme.js\";\n\n/**\n * Read all content from piped stdin.\n * Returns undefined if stdin is a TTY (interactive terminal).\n */\nasync function readPipedStdin(): Promise<string | undefined> {\n\t// If stdin is a TTY, we're running interactively - don't read stdin\n\tif (process.stdin.isTTY) {\n\t\treturn undefined;\n\t}\n\n\treturn new Promise((resolve) => {\n\t\tlet data = \"\";\n\t\tprocess.stdin.setEncoding(\"utf8\");\n\t\tprocess.stdin.on(\"data\", (chunk) => {\n\t\t\tdata += chunk;\n\t\t});\n\t\tprocess.stdin.on(\"end\", () => {\n\t\t\tresolve(data.trim() || undefined);\n\t\t});\n\t\tprocess.stdin.resume();\n\t});\n}\n\ntype PackageCommand = \"install\" | \"remove\" | \"update\" | \"list\";\n\ninterface PackageCommandOptions {\n\tcommand: PackageCommand;\n\tsource?: string;\n\tlocal: boolean;\n\thelp: boolean;\n\tinvalidOption?: string;\n}\n\nfunction getPackageCommandUsage(command: PackageCommand): string {\n\tswitch (command) {\n\t\tcase \"install\":\n\t\t\treturn `${APP_NAME} install <source> [-l]`;\n\t\tcase \"remove\":\n\t\t\treturn `${APP_NAME} remove <source> [-l]`;\n\t\tcase \"update\":\n\t\t\treturn `${APP_NAME} update [source]`;\n\t\tcase \"list\":\n\t\t\treturn `${APP_NAME} list`;\n\t}\n}\n\nfunction printPackageCommandHelp(command: PackageCommand): void {\n\tswitch (command) {\n\t\tcase \"install\":\n\t\t\tconsole.log(`${chalk.bold(\"Usage:\")}\n ${getPackageCommandUsage(\"install\")}\n\nInstall a package and add it to settings.\n\nOptions:\n -l, --local Install project-locally (.pi/settings.json)\n\nExamples:\n ${APP_NAME} install npm:@foo/bar\n ${APP_NAME} install git:github.com/user/repo\n ${APP_NAME} install https://github.com/user/repo\n ${APP_NAME} install git@github.com:user/repo\n ${APP_NAME} install ./local/path\n`);\n\t\t\treturn;\n\n\t\tcase \"remove\":\n\t\t\tconsole.log(`${chalk.bold(\"Usage:\")}\n ${getPackageCommandUsage(\"remove\")}\n\nRemove a package and its source from settings.\n\nOptions:\n -l, --local Remove from project settings (.pi/settings.json)\n\nExample:\n ${APP_NAME} remove npm:@foo/bar\n`);\n\t\t\treturn;\n\n\t\tcase \"update\":\n\t\t\tconsole.log(`${chalk.bold(\"Usage:\")}\n ${getPackageCommandUsage(\"update\")}\n\nUpdate installed packages.\nIf <source> is provided, only that package is updated.\n`);\n\t\t\treturn;\n\n\t\tcase \"list\":\n\t\t\tconsole.log(`${chalk.bold(\"Usage:\")}\n ${getPackageCommandUsage(\"list\")}\n\nList installed packages from user and project settings.\n`);\n\t\t\treturn;\n\t}\n}\n\nfunction parsePackageCommand(args: string[]): PackageCommandOptions | undefined {\n\tconst [command, ...rest] = args;\n\tif (command !== \"install\" && command !== \"remove\" && command !== \"update\" && command !== \"list\") {\n\t\treturn undefined;\n\t}\n\n\tlet local = false;\n\tlet help = false;\n\tlet invalidOption: string | undefined;\n\tlet source: string | undefined;\n\n\tfor (const arg of rest) {\n\t\tif (arg === \"-h\" || arg === \"--help\") {\n\t\t\thelp = true;\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (arg === \"-l\" || arg === \"--local\") {\n\t\t\tif (command === \"install\" || command === \"remove\") {\n\t\t\t\tlocal = true;\n\t\t\t} else {\n\t\t\t\tinvalidOption = invalidOption ?? arg;\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (arg.startsWith(\"-\")) {\n\t\t\tinvalidOption = invalidOption ?? arg;\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (!source) {\n\t\t\tsource = arg;\n\t\t}\n\t}\n\n\treturn { command, source, local, help, invalidOption };\n}\n\nasync function handlePackageCommand(args: string[]): Promise<boolean> {\n\tconst options = parsePackageCommand(args);\n\tif (!options) {\n\t\treturn false;\n\t}\n\n\tif (options.help) {\n\t\tprintPackageCommandHelp(options.command);\n\t\treturn true;\n\t}\n\n\tif (options.invalidOption) {\n\t\tconsole.error(chalk.red(`Unknown option ${options.invalidOption} for \"${options.command}\".`));\n\t\tconsole.error(chalk.dim(`Use \"${APP_NAME} --help\" or \"${getPackageCommandUsage(options.command)}\".`));\n\t\tprocess.exitCode = 1;\n\t\treturn true;\n\t}\n\n\tconst source = options.source;\n\tif ((options.command === \"install\" || options.command === \"remove\") && !source) {\n\t\tconsole.error(chalk.red(`Missing ${options.command} source.`));\n\t\tconsole.error(chalk.dim(`Usage: ${getPackageCommandUsage(options.command)}`));\n\t\tprocess.exitCode = 1;\n\t\treturn true;\n\t}\n\n\tconst cwd = process.cwd();\n\tconst agentDir = getAgentDir();\n\tconst settingsManager = SettingsManager.create(cwd, agentDir);\n\tconst packageManager = new DefaultPackageManager({ cwd, agentDir, settingsManager });\n\n\tpackageManager.setProgressCallback((event) => {\n\t\tif (event.type === \"start\") {\n\t\t\tprocess.stdout.write(chalk.dim(`${event.message}\\n`));\n\t\t}\n\t});\n\n\ttry {\n\t\tswitch (options.command) {\n\t\t\tcase \"install\":\n\t\t\t\tawait packageManager.install(source!, { local: options.local });\n\t\t\t\tpackageManager.addSourceToSettings(source!, { local: options.local });\n\t\t\t\tconsole.log(chalk.green(`Installed ${source}`));\n\t\t\t\treturn true;\n\n\t\t\tcase \"remove\": {\n\t\t\t\tawait packageManager.remove(source!, { local: options.local });\n\t\t\t\tconst removed = packageManager.removeSourceFromSettings(source!, { local: options.local });\n\t\t\t\tif (!removed) {\n\t\t\t\t\tconsole.error(chalk.red(`No matching package found for ${source}`));\n\t\t\t\t\tprocess.exitCode = 1;\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tconsole.log(chalk.green(`Removed ${source}`));\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\tcase \"list\": {\n\t\t\t\tconst globalSettings = settingsManager.getGlobalSettings();\n\t\t\t\tconst projectSettings = settingsManager.getProjectSettings();\n\t\t\t\tconst globalPackages = globalSettings.packages ?? [];\n\t\t\t\tconst projectPackages = projectSettings.packages ?? [];\n\n\t\t\t\tif (globalPackages.length === 0 && projectPackages.length === 0) {\n\t\t\t\t\tconsole.log(chalk.dim(\"No packages installed.\"));\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\n\t\t\t\tconst formatPackage = (pkg: (typeof globalPackages)[number], scope: \"user\" | \"project\") => {\n\t\t\t\t\tconst source = typeof pkg === \"string\" ? pkg : pkg.source;\n\t\t\t\t\tconst filtered = typeof pkg === \"object\";\n\t\t\t\t\tconst display = filtered ? `${source} (filtered)` : source;\n\t\t\t\t\tconsole.log(` ${display}`);\n\t\t\t\t\tconst path = packageManager.getInstalledPath(source, scope);\n\t\t\t\t\tif (path) {\n\t\t\t\t\t\tconsole.log(chalk.dim(` ${path}`));\n\t\t\t\t\t}\n\t\t\t\t};\n\n\t\t\t\tif (globalPackages.length > 0) {\n\t\t\t\t\tconsole.log(chalk.bold(\"User packages:\"));\n\t\t\t\t\tfor (const pkg of globalPackages) {\n\t\t\t\t\t\tformatPackage(pkg, \"user\");\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (projectPackages.length > 0) {\n\t\t\t\t\tif (globalPackages.length > 0) console.log();\n\t\t\t\t\tconsole.log(chalk.bold(\"Project packages:\"));\n\t\t\t\t\tfor (const pkg of projectPackages) {\n\t\t\t\t\t\tformatPackage(pkg, \"project\");\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\tcase \"update\":\n\t\t\t\tawait packageManager.update(source);\n\t\t\t\tif (source) {\n\t\t\t\t\tconsole.log(chalk.green(`Updated ${source}`));\n\t\t\t\t} else {\n\t\t\t\t\tconsole.log(chalk.green(\"Updated packages\"));\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t}\n\t} catch (error: unknown) {\n\t\tconst message = error instanceof Error ? error.message : \"Unknown package command error\";\n\t\tconsole.error(chalk.red(`Error: ${message}`));\n\t\tprocess.exitCode = 1;\n\t\treturn true;\n\t}\n}\n\nasync function prepareInitialMessage(\n\tparsed: Args,\n\tautoResizeImages: boolean,\n): Promise<{\n\tinitialMessage?: string;\n\tinitialImages?: ImageContent[];\n}> {\n\tif (parsed.fileArgs.length === 0) {\n\t\treturn {};\n\t}\n\n\tconst { text, images } = await processFileArguments(parsed.fileArgs, { autoResizeImages });\n\n\tlet initialMessage: string;\n\tif (parsed.messages.length > 0) {\n\t\tinitialMessage = text + parsed.messages[0];\n\t\tparsed.messages.shift();\n\t} else {\n\t\tinitialMessage = text;\n\t}\n\n\treturn {\n\t\tinitialMessage,\n\t\tinitialImages: images.length > 0 ? images : undefined,\n\t};\n}\n\n/** Result from resolving a session argument */\ntype ResolvedSession =\n\t| { type: \"path\"; path: string } // Direct file path\n\t| { type: \"local\"; path: string } // Found in current project\n\t| { type: \"global\"; path: string; cwd: string } // Found in different project\n\t| { type: \"not_found\"; arg: string }; // Not found anywhere\n\n/**\n * Resolve a session argument to a file path.\n * If it looks like a path, use as-is. Otherwise try to match as session ID prefix.\n */\nasync function resolveSessionPath(sessionArg: string, cwd: string, sessionDir?: string): Promise<ResolvedSession> {\n\t// If it looks like a file path, use as-is\n\tif (sessionArg.includes(\"/\") || sessionArg.includes(\"\\\\\") || sessionArg.endsWith(\".jsonl\")) {\n\t\treturn { type: \"path\", path: sessionArg };\n\t}\n\n\t// Try to match as session ID in current project first\n\tconst localSessions = await SessionManager.list(cwd, sessionDir);\n\tconst localMatches = localSessions.filter((s) => s.id.startsWith(sessionArg));\n\n\tif (localMatches.length >= 1) {\n\t\treturn { type: \"local\", path: localMatches[0].path };\n\t}\n\n\t// Try global search across all projects\n\tconst allSessions = await SessionManager.listAll();\n\tconst globalMatches = allSessions.filter((s) => s.id.startsWith(sessionArg));\n\n\tif (globalMatches.length >= 1) {\n\t\tconst match = globalMatches[0];\n\t\treturn { type: \"global\", path: match.path, cwd: match.cwd };\n\t}\n\n\t// Not found anywhere\n\treturn { type: \"not_found\", arg: sessionArg };\n}\n\n/** Prompt user for yes/no confirmation */\nasync function promptConfirm(message: string): Promise<boolean> {\n\treturn new Promise((resolve) => {\n\t\tconst rl = createInterface({\n\t\t\tinput: process.stdin,\n\t\t\toutput: process.stdout,\n\t\t});\n\t\trl.question(`${message} [y/N] `, (answer) => {\n\t\t\trl.close();\n\t\t\tresolve(answer.toLowerCase() === \"y\" || answer.toLowerCase() === \"yes\");\n\t\t});\n\t});\n}\n\nasync function createSessionManager(parsed: Args, cwd: string): Promise<SessionManager | undefined> {\n\tif (parsed.noSession) {\n\t\treturn SessionManager.inMemory();\n\t}\n\tif (parsed.session) {\n\t\tconst resolved = await resolveSessionPath(parsed.session, cwd, parsed.sessionDir);\n\n\t\tswitch (resolved.type) {\n\t\t\tcase \"path\":\n\t\t\tcase \"local\":\n\t\t\t\treturn SessionManager.open(resolved.path, parsed.sessionDir);\n\n\t\t\tcase \"global\": {\n\t\t\t\t// Session found in different project - ask user if they want to fork\n\t\t\t\tconsole.log(chalk.yellow(`Session found in different project: ${resolved.cwd}`));\n\t\t\t\tconst shouldFork = await promptConfirm(\"Fork this session into current directory?\");\n\t\t\t\tif (!shouldFork) {\n\t\t\t\t\tconsole.log(chalk.dim(\"Aborted.\"));\n\t\t\t\t\tprocess.exit(0);\n\t\t\t\t}\n\t\t\t\treturn SessionManager.forkFrom(resolved.path, cwd, parsed.sessionDir);\n\t\t\t}\n\n\t\t\tcase \"not_found\":\n\t\t\t\tconsole.error(chalk.red(`No session found matching '${resolved.arg}'`));\n\t\t\t\tprocess.exit(1);\n\t\t}\n\t}\n\tif (parsed.continue) {\n\t\treturn SessionManager.continueRecent(cwd, parsed.sessionDir);\n\t}\n\t// --resume is handled separately (needs picker UI)\n\t// If --session-dir provided without --continue/--resume, create new session there\n\tif (parsed.sessionDir) {\n\t\treturn SessionManager.create(cwd, parsed.sessionDir);\n\t}\n\t// Default case (new session) returns undefined, SDK will create one\n\treturn undefined;\n}\n\nfunction buildSessionOptions(\n\tparsed: Args,\n\tscopedModels: ScopedModel[],\n\tsessionManager: SessionManager | undefined,\n\tmodelRegistry: ModelRegistry,\n\tsettingsManager: SettingsManager,\n): CreateAgentSessionOptions {\n\tconst options: CreateAgentSessionOptions = {};\n\n\tif (sessionManager) {\n\t\toptions.sessionManager = sessionManager;\n\t}\n\n\t// Model from CLI\n\tif (parsed.provider && parsed.model) {\n\t\tconst model = modelRegistry.find(parsed.provider, parsed.model);\n\t\tif (!model) {\n\t\t\tconsole.error(chalk.red(`Model ${parsed.provider}/${parsed.model} not found`));\n\t\t\tprocess.exit(1);\n\t\t}\n\t\toptions.model = model;\n\t} else if (scopedModels.length > 0 && !parsed.continue && !parsed.resume) {\n\t\t// Check if saved default is in scoped models - use it if so, otherwise first scoped model\n\t\tconst savedProvider = settingsManager.getDefaultProvider();\n\t\tconst savedModelId = settingsManager.getDefaultModel();\n\t\tconst savedModel = savedProvider && savedModelId ? modelRegistry.find(savedProvider, savedModelId) : undefined;\n\t\tconst savedInScope = savedModel ? scopedModels.find((sm) => modelsAreEqual(sm.model, savedModel)) : undefined;\n\n\t\tif (savedInScope) {\n\t\t\toptions.model = savedInScope.model;\n\t\t\t// Use thinking level from scoped model config if explicitly set\n\t\t\tif (!parsed.thinking && savedInScope.thinkingLevel) {\n\t\t\t\toptions.thinkingLevel = savedInScope.thinkingLevel;\n\t\t\t}\n\t\t} else {\n\t\t\toptions.model = scopedModels[0].model;\n\t\t\t// Use thinking level from first scoped model if explicitly set\n\t\t\tif (!parsed.thinking && scopedModels[0].thinkingLevel) {\n\t\t\t\toptions.thinkingLevel = scopedModels[0].thinkingLevel;\n\t\t\t}\n\t\t}\n\t}\n\n\t// Thinking level from CLI (takes precedence over scoped model thinking levels set above)\n\tif (parsed.thinking) {\n\t\toptions.thinkingLevel = parsed.thinking;\n\t}\n\n\t// Scoped models for Ctrl+P cycling - fill in default thinking level for models without explicit level\n\tif (scopedModels.length > 0) {\n\t\tconst defaultThinkingLevel = settingsManager.getDefaultThinkingLevel() ?? DEFAULT_THINKING_LEVEL;\n\t\toptions.scopedModels = scopedModels.map((sm) => ({\n\t\t\tmodel: sm.model,\n\t\t\tthinkingLevel: sm.thinkingLevel ?? defaultThinkingLevel,\n\t\t}));\n\t}\n\n\t// API key from CLI - set in authStorage\n\t// (handled by caller before createAgentSession)\n\n\t// Tools\n\tif (parsed.noTools) {\n\t\t// --no-tools: start with no built-in tools\n\t\t// --tools can still add specific ones back\n\t\tif (parsed.tools && parsed.tools.length > 0) {\n\t\t\toptions.tools = parsed.tools.map((name) => allTools[name]);\n\t\t} else {\n\t\t\toptions.tools = [];\n\t\t}\n\t} else if (parsed.tools) {\n\t\toptions.tools = parsed.tools.map((name) => allTools[name]);\n\t}\n\n\treturn options;\n}\n\nasync function handleConfigCommand(args: string[]): Promise<boolean> {\n\tif (args[0] !== \"config\") {\n\t\treturn false;\n\t}\n\n\tconst cwd = process.cwd();\n\tconst agentDir = getAgentDir();\n\tconst settingsManager = SettingsManager.create(cwd, agentDir);\n\tconst packageManager = new DefaultPackageManager({ cwd, agentDir, settingsManager });\n\n\tconst resolvedPaths = await packageManager.resolve();\n\n\tawait selectConfig({\n\t\tresolvedPaths,\n\t\tsettingsManager,\n\t\tcwd,\n\t\tagentDir,\n\t});\n\n\tprocess.exit(0);\n}\n\nexport async function main(args: string[]) {\n\tif (await handlePackageCommand(args)) {\n\t\treturn;\n\t}\n\n\tif (await handleConfigCommand(args)) {\n\t\treturn;\n\t}\n\n\t// Run migrations (pass cwd for project-local migrations)\n\tconst { migratedAuthProviders: migratedProviders, deprecationWarnings } = runMigrations(process.cwd());\n\n\t// First pass: parse args to get --extension paths\n\tconst firstPass = parseArgs(args);\n\n\t// Early load extensions to discover their CLI flags\n\tconst cwd = process.cwd();\n\tconst agentDir = getAgentDir();\n\tconst settingsManager = SettingsManager.create(cwd, agentDir);\n\tconst authStorage = new AuthStorage();\n\tconst modelRegistry = new ModelRegistry(authStorage, getModelsPath());\n\n\tconst resourceLoader = new DefaultResourceLoader({\n\t\tcwd,\n\t\tagentDir,\n\t\tsettingsManager,\n\t\tadditionalExtensionPaths: firstPass.extensions,\n\t\tadditionalSkillPaths: firstPass.skills,\n\t\tadditionalPromptTemplatePaths: firstPass.promptTemplates,\n\t\tadditionalThemePaths: firstPass.themes,\n\t\tnoExtensions: firstPass.noExtensions,\n\t\tnoSkills: firstPass.noSkills,\n\t\tnoPromptTemplates: firstPass.noPromptTemplates,\n\t\tnoThemes: firstPass.noThemes,\n\t\tsystemPrompt: firstPass.systemPrompt,\n\t\tappendSystemPrompt: firstPass.appendSystemPrompt,\n\t});\n\tawait resourceLoader.reload();\n\ttime(\"resourceLoader.reload\");\n\n\tconst extensionsResult: LoadExtensionsResult = resourceLoader.getExtensions();\n\tfor (const { path, error } of extensionsResult.errors) {\n\t\tconsole.error(chalk.red(`Failed to load extension \"${path}\": ${error}`));\n\t}\n\n\t// Apply pending provider registrations from extensions immediately\n\t// so they're available for model resolution before AgentSession is created\n\tfor (const { name, config } of extensionsResult.runtime.pendingProviderRegistrations) {\n\t\tmodelRegistry.registerProvider(name, config);\n\t}\n\textensionsResult.runtime.pendingProviderRegistrations = [];\n\n\tconst extensionFlags = new Map<string, { type: \"boolean\" | \"string\" }>();\n\tfor (const ext of extensionsResult.extensions) {\n\t\tfor (const [name, flag] of ext.flags) {\n\t\t\textensionFlags.set(name, { type: flag.type });\n\t\t}\n\t}\n\n\t// Second pass: parse args with extension flags\n\tconst parsed = parseArgs(args, extensionFlags);\n\n\t// Pass flag values to extensions via runtime\n\tfor (const [name, value] of parsed.unknownFlags) {\n\t\textensionsResult.runtime.flagValues.set(name, value);\n\t}\n\n\tif (parsed.version) {\n\t\tconsole.log(VERSION);\n\t\tprocess.exit(0);\n\t}\n\n\tif (parsed.help) {\n\t\tprintHelp();\n\t\tprocess.exit(0);\n\t}\n\n\tif (parsed.listModels !== undefined) {\n\t\tconst searchPattern = typeof parsed.listModels === \"string\" ? parsed.listModels : undefined;\n\t\tawait listModels(modelRegistry, searchPattern);\n\t\tprocess.exit(0);\n\t}\n\n\t// Read piped stdin content (if any) - skip for RPC mode which uses stdin for JSON-RPC\n\tif (parsed.mode !== \"rpc\") {\n\t\tconst stdinContent = await readPipedStdin();\n\t\tif (stdinContent !== undefined) {\n\t\t\t// Force print mode since interactive mode requires a TTY for keyboard input\n\t\t\tparsed.print = true;\n\t\t\t// Prepend stdin content to messages\n\t\t\tparsed.messages.unshift(stdinContent);\n\t\t}\n\t}\n\n\tif (parsed.export) {\n\t\tlet result: string;\n\t\ttry {\n\t\t\tconst outputPath = parsed.messages.length > 0 ? parsed.messages[0] : undefined;\n\t\t\tresult = await exportFromFile(parsed.export, outputPath);\n\t\t} catch (error: unknown) {\n\t\t\tconst message = error instanceof Error ? error.message : \"Failed to export session\";\n\t\t\tconsole.error(chalk.red(`Error: ${message}`));\n\t\t\tprocess.exit(1);\n\t\t}\n\t\tconsole.log(`Exported to: ${result}`);\n\t\tprocess.exit(0);\n\t}\n\n\tif (parsed.mode === \"rpc\" && parsed.fileArgs.length > 0) {\n\t\tconsole.error(chalk.red(\"Error: @file arguments are not supported in RPC mode\"));\n\t\tprocess.exit(1);\n\t}\n\n\tconst { initialMessage, initialImages } = await prepareInitialMessage(parsed, settingsManager.getImageAutoResize());\n\tconst isInteractive = !parsed.print && parsed.mode === undefined;\n\tconst mode = parsed.mode || \"text\";\n\tinitTheme(settingsManager.getTheme(), isInteractive);\n\n\t// Show deprecation warnings in interactive mode\n\tif (isInteractive && deprecationWarnings.length > 0) {\n\t\tawait showDeprecationWarnings(deprecationWarnings);\n\t}\n\n\tlet scopedModels: ScopedModel[] = [];\n\tconst modelPatterns = parsed.models ?? settingsManager.getEnabledModels();\n\tif (modelPatterns && modelPatterns.length > 0) {\n\t\tscopedModels = await resolveModelScope(modelPatterns, modelRegistry);\n\t}\n\n\t// Create session manager based on CLI flags\n\tlet sessionManager = await createSessionManager(parsed, cwd);\n\n\t// Handle --resume: show session picker\n\tif (parsed.resume) {\n\t\t// Initialize keybindings so session picker respects user config\n\t\tKeybindingsManager.create();\n\n\t\tconst selectedPath = await selectSession(\n\t\t\t(onProgress) => SessionManager.list(cwd, parsed.sessionDir, onProgress),\n\t\t\tSessionManager.listAll,\n\t\t);\n\t\tif (!selectedPath) {\n\t\t\tconsole.log(chalk.dim(\"No session selected\"));\n\t\t\tstopThemeWatcher();\n\t\t\tprocess.exit(0);\n\t\t}\n\t\tsessionManager = SessionManager.open(selectedPath);\n\t}\n\n\tconst sessionOptions = buildSessionOptions(parsed, scopedModels, sessionManager, modelRegistry, settingsManager);\n\tsessionOptions.authStorage = authStorage;\n\tsessionOptions.modelRegistry = modelRegistry;\n\tsessionOptions.resourceLoader = resourceLoader;\n\n\t// Handle CLI --api-key as runtime override (not persisted)\n\tif (parsed.apiKey) {\n\t\tif (!sessionOptions.model) {\n\t\t\tconsole.error(chalk.red(\"--api-key requires a model to be specified via --provider/--model or -m/--models\"));\n\t\t\tprocess.exit(1);\n\t\t}\n\t\tauthStorage.setRuntimeApiKey(sessionOptions.model.provider, parsed.apiKey);\n\t}\n\n\tconst { session, modelFallbackMessage } = await createAgentSession(sessionOptions);\n\n\tif (!isInteractive && !session.model) {\n\t\tconsole.error(chalk.red(\"No models available.\"));\n\t\tconsole.error(chalk.yellow(\"\\nSet an API key environment variable:\"));\n\t\tconsole.error(\" ANTHROPIC_API_KEY, OPENAI_API_KEY, GEMINI_API_KEY, etc.\");\n\t\tconsole.error(chalk.yellow(`\\nOr create ${getModelsPath()}`));\n\t\tprocess.exit(1);\n\t}\n\n\t// Clamp thinking level to model capabilities (for CLI override case)\n\tif (session.model && parsed.thinking) {\n\t\tlet effectiveThinking = parsed.thinking;\n\t\tif (!session.model.reasoning) {\n\t\t\teffectiveThinking = \"off\";\n\t\t} else if (effectiveThinking === \"xhigh\" && !supportsXhigh(session.model)) {\n\t\t\teffectiveThinking = \"high\";\n\t\t}\n\t\tif (effectiveThinking !== session.thinkingLevel) {\n\t\t\tsession.setThinkingLevel(effectiveThinking);\n\t\t}\n\t}\n\n\tif (mode === \"rpc\") {\n\t\tawait runRpcMode(session);\n\t} else if (isInteractive) {\n\t\tif (scopedModels.length > 0 && (parsed.verbose || !settingsManager.getQuietStartup())) {\n\t\t\tconst modelList = scopedModels\n\t\t\t\t.map((sm) => {\n\t\t\t\t\tconst thinkingStr = sm.thinkingLevel ? `:${sm.thinkingLevel}` : \"\";\n\t\t\t\t\treturn `${sm.model.id}${thinkingStr}`;\n\t\t\t\t})\n\t\t\t\t.join(\", \");\n\t\t\tconsole.log(chalk.dim(`Model scope: ${modelList} ${chalk.gray(\"(Ctrl+P to cycle)\")}`));\n\t\t}\n\n\t\tprintTimings();\n\t\tconst mode = new InteractiveMode(session, {\n\t\t\tmigratedProviders,\n\t\t\tmodelFallbackMessage,\n\t\t\tinitialMessage,\n\t\t\tinitialImages,\n\t\t\tinitialMessages: parsed.messages,\n\t\t\tverbose: parsed.verbose,\n\t\t});\n\t\tawait mode.run();\n\t} else {\n\t\tawait runPrintMode(session, {\n\t\t\tmode,\n\t\t\tmessages: parsed.messages,\n\t\t\tinitialMessage,\n\t\t\tinitialImages,\n\t\t});\n\t\tstopThemeWatcher();\n\t\tif (process.stdout.writableLength > 0) {\n\t\t\tawait new Promise<void>((resolve) => process.stdout.once(\"drain\", resolve));\n\t\t}\n\t\tprocess.exit(0);\n\t}\n}\n"]}
package/dist/main.js CHANGED
@@ -4,8 +4,6 @@
4
4
  * This file handles CLI argument parsing and translates them into
5
5
  * createAgentSession() options. The SDK does the heavy lifting.
6
6
  */
7
- import { homedir } from "node:os";
8
- import { isAbsolute, join, relative, resolve } from "node:path";
9
7
  import { modelsAreEqual, supportsXhigh } from "@mariozechner/pi-ai";
10
8
  import chalk from "chalk";
11
9
  import { createInterface } from "readline";
@@ -14,7 +12,7 @@ import { selectConfig } from "./cli/config-selector.js";
14
12
  import { processFileArguments } from "./cli/file-processor.js";
15
13
  import { listModels } from "./cli/list-models.js";
16
14
  import { selectSession } from "./cli/session-picker.js";
17
- import { CONFIG_DIR_NAME, getAgentDir, getModelsPath, VERSION } from "./config.js";
15
+ import { APP_NAME, getAgentDir, getModelsPath, VERSION } from "./config.js";
18
16
  import { AuthStorage } from "./core/auth-storage.js";
19
17
  import { DEFAULT_THINKING_LEVEL } from "./core/defaults.js";
20
18
  import { exportFromFile } from "./core/export-html/index.js";
@@ -52,205 +50,201 @@ async function readPipedStdin() {
52
50
  process.stdin.resume();
53
51
  });
54
52
  }
53
+ function getPackageCommandUsage(command) {
54
+ switch (command) {
55
+ case "install":
56
+ return `${APP_NAME} install <source> [-l]`;
57
+ case "remove":
58
+ return `${APP_NAME} remove <source> [-l]`;
59
+ case "update":
60
+ return `${APP_NAME} update [source]`;
61
+ case "list":
62
+ return `${APP_NAME} list`;
63
+ }
64
+ }
65
+ function printPackageCommandHelp(command) {
66
+ switch (command) {
67
+ case "install":
68
+ console.log(`${chalk.bold("Usage:")}
69
+ ${getPackageCommandUsage("install")}
70
+
71
+ Install a package and add it to settings.
72
+
73
+ Options:
74
+ -l, --local Install project-locally (.pi/settings.json)
75
+
76
+ Examples:
77
+ ${APP_NAME} install npm:@foo/bar
78
+ ${APP_NAME} install git:github.com/user/repo
79
+ ${APP_NAME} install https://github.com/user/repo
80
+ ${APP_NAME} install git@github.com:user/repo
81
+ ${APP_NAME} install ./local/path
82
+ `);
83
+ return;
84
+ case "remove":
85
+ console.log(`${chalk.bold("Usage:")}
86
+ ${getPackageCommandUsage("remove")}
87
+
88
+ Remove a package and its source from settings.
89
+
90
+ Options:
91
+ -l, --local Remove from project settings (.pi/settings.json)
92
+
93
+ Example:
94
+ ${APP_NAME} remove npm:@foo/bar
95
+ `);
96
+ return;
97
+ case "update":
98
+ console.log(`${chalk.bold("Usage:")}
99
+ ${getPackageCommandUsage("update")}
100
+
101
+ Update installed packages.
102
+ If <source> is provided, only that package is updated.
103
+ `);
104
+ return;
105
+ case "list":
106
+ console.log(`${chalk.bold("Usage:")}
107
+ ${getPackageCommandUsage("list")}
108
+
109
+ List installed packages from user and project settings.
110
+ `);
111
+ return;
112
+ }
113
+ }
55
114
  function parsePackageCommand(args) {
56
115
  const [command, ...rest] = args;
57
116
  if (command !== "install" && command !== "remove" && command !== "update" && command !== "list") {
58
117
  return undefined;
59
118
  }
60
119
  let local = false;
61
- const sources = [];
120
+ let help = false;
121
+ let invalidOption;
122
+ let source;
62
123
  for (const arg of rest) {
124
+ if (arg === "-h" || arg === "--help") {
125
+ help = true;
126
+ continue;
127
+ }
63
128
  if (arg === "-l" || arg === "--local") {
64
- local = true;
129
+ if (command === "install" || command === "remove") {
130
+ local = true;
131
+ }
132
+ else {
133
+ invalidOption = invalidOption ?? arg;
134
+ }
65
135
  continue;
66
136
  }
67
- sources.push(arg);
68
- }
69
- return { command, source: sources[0], local };
70
- }
71
- function expandTildePath(input) {
72
- const trimmed = input.trim();
73
- if (trimmed === "~")
74
- return homedir();
75
- if (trimmed.startsWith("~/"))
76
- return resolve(homedir(), trimmed.slice(2));
77
- if (trimmed.startsWith("~"))
78
- return resolve(homedir(), trimmed.slice(1));
79
- return trimmed;
80
- }
81
- function resolveLocalSourceFromInput(source, cwd) {
82
- const expanded = expandTildePath(source);
83
- return isAbsolute(expanded) ? resolve(expanded) : resolve(cwd, expanded);
84
- }
85
- function resolveLocalSourceFromSettings(source, baseDir) {
86
- const expanded = expandTildePath(source);
87
- return isAbsolute(expanded) ? expanded : resolve(baseDir, expanded);
88
- }
89
- function normalizeLocalSourceForSettings(source, baseDir, cwd) {
90
- const resolved = resolveLocalSourceFromInput(source, cwd);
91
- const rel = relative(baseDir, resolved);
92
- return rel || ".";
93
- }
94
- function normalizePackageSourceForSettings(source, baseDir, cwd) {
95
- const normalized = normalizeExtensionSource(source);
96
- if (normalized.type !== "local") {
97
- return source;
98
- }
99
- return normalizeLocalSourceForSettings(source, baseDir, cwd);
100
- }
101
- function normalizeExtensionSource(source) {
102
- if (source.startsWith("npm:")) {
103
- const spec = source.slice("npm:".length).trim();
104
- const match = spec.match(/^(@?[^@]+(?:\/[^@]+)?)(?:@.+)?$/);
105
- return { type: "npm", key: match?.[1] ?? spec };
106
- }
107
- if (source.startsWith("git:")) {
108
- const repo = source.slice("git:".length).trim().split("@")[0] ?? "";
109
- return { type: "git", key: repo.replace(/^https?:\/\//, "").replace(/\.git$/, "") };
110
- }
111
- // Raw git URLs
112
- if (source.startsWith("https://") || source.startsWith("http://")) {
113
- const repo = source.split("@")[0] ?? "";
114
- return { type: "git", key: repo.replace(/^https?:\/\//, "").replace(/\.git$/, "") };
115
- }
116
- return { type: "local", key: source };
117
- }
118
- function normalizeSourceForInput(source, cwd) {
119
- const normalized = normalizeExtensionSource(source);
120
- if (normalized.type !== "local") {
121
- return normalized;
122
- }
123
- return { type: "local", key: resolveLocalSourceFromInput(source, cwd) };
124
- }
125
- function normalizeSourceForSettings(source, baseDir) {
126
- const normalized = normalizeExtensionSource(source);
127
- if (normalized.type !== "local") {
128
- return normalized;
129
- }
130
- return { type: "local", key: resolveLocalSourceFromSettings(source, baseDir) };
131
- }
132
- function sourcesMatch(a, b, baseDir, cwd) {
133
- const left = normalizeSourceForSettings(a, baseDir);
134
- const right = normalizeSourceForInput(b, cwd);
135
- return left.type === right.type && left.key === right.key;
136
- }
137
- function getPackageSourceString(pkg) {
138
- return typeof pkg === "string" ? pkg : pkg.source;
139
- }
140
- function packageSourcesMatch(a, b, baseDir, cwd) {
141
- const aSource = getPackageSourceString(a);
142
- return sourcesMatch(aSource, b, baseDir, cwd);
143
- }
144
- function updatePackageSources(settingsManager, source, local, cwd, agentDir, action) {
145
- const currentSettings = local ? settingsManager.getProjectSettings() : settingsManager.getGlobalSettings();
146
- const currentPackages = currentSettings.packages ?? [];
147
- const baseDir = local ? join(cwd, CONFIG_DIR_NAME) : agentDir;
148
- const normalizedSource = normalizePackageSourceForSettings(source, baseDir, cwd);
149
- let nextPackages;
150
- let changed = false;
151
- if (action === "add") {
152
- const exists = currentPackages.some((existing) => packageSourcesMatch(existing, source, baseDir, cwd));
153
- nextPackages = exists ? currentPackages : [...currentPackages, normalizedSource];
154
- changed = !exists;
155
- }
156
- else {
157
- nextPackages = currentPackages.filter((existing) => !packageSourcesMatch(existing, source, baseDir, cwd));
158
- changed = nextPackages.length !== currentPackages.length;
159
- }
160
- if (local) {
161
- settingsManager.setProjectPackages(nextPackages);
162
- }
163
- else {
164
- settingsManager.setPackages(nextPackages);
137
+ if (arg.startsWith("-")) {
138
+ invalidOption = invalidOption ?? arg;
139
+ continue;
140
+ }
141
+ if (!source) {
142
+ source = arg;
143
+ }
165
144
  }
166
- return changed;
145
+ return { command, source, local, help, invalidOption };
167
146
  }
168
147
  async function handlePackageCommand(args) {
169
148
  const options = parsePackageCommand(args);
170
149
  if (!options) {
171
150
  return false;
172
151
  }
152
+ if (options.help) {
153
+ printPackageCommandHelp(options.command);
154
+ return true;
155
+ }
156
+ if (options.invalidOption) {
157
+ console.error(chalk.red(`Unknown option ${options.invalidOption} for "${options.command}".`));
158
+ console.error(chalk.dim(`Use "${APP_NAME} --help" or "${getPackageCommandUsage(options.command)}".`));
159
+ process.exitCode = 1;
160
+ return true;
161
+ }
162
+ const source = options.source;
163
+ if ((options.command === "install" || options.command === "remove") && !source) {
164
+ console.error(chalk.red(`Missing ${options.command} source.`));
165
+ console.error(chalk.dim(`Usage: ${getPackageCommandUsage(options.command)}`));
166
+ process.exitCode = 1;
167
+ return true;
168
+ }
173
169
  const cwd = process.cwd();
174
170
  const agentDir = getAgentDir();
175
171
  const settingsManager = SettingsManager.create(cwd, agentDir);
176
172
  const packageManager = new DefaultPackageManager({ cwd, agentDir, settingsManager });
177
- // Set up progress callback for CLI feedback
178
173
  packageManager.setProgressCallback((event) => {
179
174
  if (event.type === "start") {
180
175
  process.stdout.write(chalk.dim(`${event.message}\n`));
181
176
  }
182
- else if (event.type === "error") {
183
- console.error(chalk.red(`Error: ${event.message}`));
184
- }
185
177
  });
186
- if (options.command === "install") {
187
- if (!options.source) {
188
- console.error(chalk.red("Missing install source."));
189
- process.exit(1);
190
- }
191
- await packageManager.install(options.source, { local: options.local });
192
- updatePackageSources(settingsManager, options.source, options.local, cwd, agentDir, "add");
193
- console.log(chalk.green(`Installed ${options.source}`));
194
- return true;
195
- }
196
- if (options.command === "remove") {
197
- if (!options.source) {
198
- console.error(chalk.red("Missing remove source."));
199
- process.exit(1);
200
- }
201
- await packageManager.remove(options.source, { local: options.local });
202
- const removed = updatePackageSources(settingsManager, options.source, options.local, cwd, agentDir, "remove");
203
- if (!removed) {
204
- console.error(chalk.red(`No matching package found for ${options.source}`));
205
- process.exit(1);
206
- }
207
- console.log(chalk.green(`Removed ${options.source}`));
208
- return true;
209
- }
210
- if (options.command === "list") {
211
- const globalSettings = settingsManager.getGlobalSettings();
212
- const projectSettings = settingsManager.getProjectSettings();
213
- const globalPackages = globalSettings.packages ?? [];
214
- const projectPackages = projectSettings.packages ?? [];
215
- if (globalPackages.length === 0 && projectPackages.length === 0) {
216
- console.log(chalk.dim("No packages installed."));
217
- return true;
218
- }
219
- const formatPackage = (pkg, scope) => {
220
- const source = typeof pkg === "string" ? pkg : pkg.source;
221
- const filtered = typeof pkg === "object";
222
- const display = filtered ? `${source} (filtered)` : source;
223
- console.log(` ${display}`);
224
- // Show resolved path
225
- const path = packageManager.getInstalledPath(source, scope);
226
- if (path) {
227
- console.log(chalk.dim(` ${path}`));
228
- }
229
- };
230
- if (globalPackages.length > 0) {
231
- console.log(chalk.bold("User packages:"));
232
- for (const pkg of globalPackages) {
233
- formatPackage(pkg, "user");
178
+ try {
179
+ switch (options.command) {
180
+ case "install":
181
+ await packageManager.install(source, { local: options.local });
182
+ packageManager.addSourceToSettings(source, { local: options.local });
183
+ console.log(chalk.green(`Installed ${source}`));
184
+ return true;
185
+ case "remove": {
186
+ await packageManager.remove(source, { local: options.local });
187
+ const removed = packageManager.removeSourceFromSettings(source, { local: options.local });
188
+ if (!removed) {
189
+ console.error(chalk.red(`No matching package found for ${source}`));
190
+ process.exitCode = 1;
191
+ return true;
192
+ }
193
+ console.log(chalk.green(`Removed ${source}`));
194
+ return true;
234
195
  }
235
- }
236
- if (projectPackages.length > 0) {
237
- if (globalPackages.length > 0)
238
- console.log();
239
- console.log(chalk.bold("Project packages:"));
240
- for (const pkg of projectPackages) {
241
- formatPackage(pkg, "project");
196
+ case "list": {
197
+ const globalSettings = settingsManager.getGlobalSettings();
198
+ const projectSettings = settingsManager.getProjectSettings();
199
+ const globalPackages = globalSettings.packages ?? [];
200
+ const projectPackages = projectSettings.packages ?? [];
201
+ if (globalPackages.length === 0 && projectPackages.length === 0) {
202
+ console.log(chalk.dim("No packages installed."));
203
+ return true;
204
+ }
205
+ const formatPackage = (pkg, scope) => {
206
+ const source = typeof pkg === "string" ? pkg : pkg.source;
207
+ const filtered = typeof pkg === "object";
208
+ const display = filtered ? `${source} (filtered)` : source;
209
+ console.log(` ${display}`);
210
+ const path = packageManager.getInstalledPath(source, scope);
211
+ if (path) {
212
+ console.log(chalk.dim(` ${path}`));
213
+ }
214
+ };
215
+ if (globalPackages.length > 0) {
216
+ console.log(chalk.bold("User packages:"));
217
+ for (const pkg of globalPackages) {
218
+ formatPackage(pkg, "user");
219
+ }
220
+ }
221
+ if (projectPackages.length > 0) {
222
+ if (globalPackages.length > 0)
223
+ console.log();
224
+ console.log(chalk.bold("Project packages:"));
225
+ for (const pkg of projectPackages) {
226
+ formatPackage(pkg, "project");
227
+ }
228
+ }
229
+ return true;
242
230
  }
231
+ case "update":
232
+ await packageManager.update(source);
233
+ if (source) {
234
+ console.log(chalk.green(`Updated ${source}`));
235
+ }
236
+ else {
237
+ console.log(chalk.green("Updated packages"));
238
+ }
239
+ return true;
243
240
  }
244
- return true;
245
241
  }
246
- await packageManager.update(options.source);
247
- if (options.source) {
248
- console.log(chalk.green(`Updated ${options.source}`));
249
- }
250
- else {
251
- console.log(chalk.green("Updated packages"));
242
+ catch (error) {
243
+ const message = error instanceof Error ? error.message : "Unknown package command error";
244
+ console.error(chalk.red(`Error: ${message}`));
245
+ process.exitCode = 1;
246
+ return true;
252
247
  }
253
- return true;
254
248
  }
255
249
  async function prepareInitialMessage(parsed, autoResizeImages) {
256
250
  if (parsed.fileArgs.length === 0) {
@@ -484,16 +478,16 @@ export async function main(args) {
484
478
  }
485
479
  if (parsed.version) {
486
480
  console.log(VERSION);
487
- return;
481
+ process.exit(0);
488
482
  }
489
483
  if (parsed.help) {
490
484
  printHelp();
491
- return;
485
+ process.exit(0);
492
486
  }
493
487
  if (parsed.listModels !== undefined) {
494
488
  const searchPattern = typeof parsed.listModels === "string" ? parsed.listModels : undefined;
495
489
  await listModels(modelRegistry, searchPattern);
496
- return;
490
+ process.exit(0);
497
491
  }
498
492
  // Read piped stdin content (if any) - skip for RPC mode which uses stdin for JSON-RPC
499
493
  if (parsed.mode !== "rpc") {
@@ -506,17 +500,18 @@ export async function main(args) {
506
500
  }
507
501
  }
508
502
  if (parsed.export) {
503
+ let result;
509
504
  try {
510
505
  const outputPath = parsed.messages.length > 0 ? parsed.messages[0] : undefined;
511
- const result = await exportFromFile(parsed.export, outputPath);
512
- console.log(`Exported to: ${result}`);
513
- return;
506
+ result = await exportFromFile(parsed.export, outputPath);
514
507
  }
515
508
  catch (error) {
516
509
  const message = error instanceof Error ? error.message : "Failed to export session";
517
510
  console.error(chalk.red(`Error: ${message}`));
518
511
  process.exit(1);
519
512
  }
513
+ console.log(`Exported to: ${result}`);
514
+ process.exit(0);
520
515
  }
521
516
  if (parsed.mode === "rpc" && parsed.fileArgs.length > 0) {
522
517
  console.error(chalk.red("Error: @file arguments are not supported in RPC mode"));