refactor(coding-agent): replace AgentSessionRuntimeHost with closure-based AgentSessionRuntime
- Replace AgentSessionRuntimeHost and bootstrap abstractions with AgentSessionRuntime - Runtime creation is now closure-based via CreateAgentSessionRuntimeFactory - Factory closes over process-global fixed inputs, recreates cwd-bound services per effective cwd - Session config (model, thinking, tools, scoped models) re-resolved per target cwd - CLI resource paths resolved once at startup as absolute paths - Swap lifecycle: teardown old, create next, apply next (hard fail on creation error) - Unified diagnostics model (info/warning/error) for args, services, session resolution, resources - No logging or process exits inside creation/parsing logic - Removed session_directory support - Removed session_switch and session_fork extension events (use session_start with reason) - Moved package/config CLI to package-manager-cli.ts - Fixed theme init for --resume session picker - Fixed flaky reftable footer test (content-based polling) - Fixed silent drop of unknown single-dash CLI flags - Added error diagnostics for missing explicit CLI resource paths - Updated SDK docs, examples, plans, exports, tests, changelog fixes #2753
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
import type { ThinkingLevel } from "@mariozechner/pi-agent-core";
|
||||
import chalk from "chalk";
|
||||
import { APP_NAME, CONFIG_DIR_NAME, ENV_AGENT_DIR } from "../config.js";
|
||||
import type { ExtensionFlag } from "../core/extensions/types.js";
|
||||
import { allTools, type ToolName } from "../core/tools/index.js";
|
||||
|
||||
export type Mode = "text" | "json" | "rpc";
|
||||
@@ -45,6 +46,7 @@ export interface Args {
|
||||
fileArgs: string[];
|
||||
/** Unknown flags (potentially extension flags) - map of flag name to value */
|
||||
unknownFlags: Map<string, boolean | string>;
|
||||
diagnostics: Array<{ type: "warning" | "error"; message: string }>;
|
||||
}
|
||||
|
||||
const VALID_THINKING_LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh"] as const;
|
||||
@@ -53,11 +55,12 @@ export function isValidThinkingLevel(level: string): level is ThinkingLevel {
|
||||
return VALID_THINKING_LEVELS.includes(level as ThinkingLevel);
|
||||
}
|
||||
|
||||
export function parseArgs(args: string[], extensionFlags?: Map<string, { type: "boolean" | "string" }>): Args {
|
||||
export function parseArgs(args: string[]): Args {
|
||||
const result: Args = {
|
||||
messages: [],
|
||||
fileArgs: [],
|
||||
unknownFlags: new Map(),
|
||||
diagnostics: [],
|
||||
};
|
||||
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
@@ -105,9 +108,10 @@ export function parseArgs(args: string[], extensionFlags?: Map<string, { type: "
|
||||
if (name in allTools) {
|
||||
validTools.push(name as ToolName);
|
||||
} else {
|
||||
console.error(
|
||||
chalk.yellow(`Warning: Unknown tool "${name}". Valid tools: ${Object.keys(allTools).join(", ")}`),
|
||||
);
|
||||
result.diagnostics.push({
|
||||
type: "warning",
|
||||
message: `Unknown tool "${name}". Valid tools: ${Object.keys(allTools).join(", ")}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
result.tools = validTools;
|
||||
@@ -116,11 +120,10 @@ export function parseArgs(args: string[], extensionFlags?: Map<string, { type: "
|
||||
if (isValidThinkingLevel(level)) {
|
||||
result.thinking = level;
|
||||
} else {
|
||||
console.error(
|
||||
chalk.yellow(
|
||||
`Warning: Invalid thinking level "${level}". Valid values: ${VALID_THINKING_LEVELS.join(", ")}`,
|
||||
),
|
||||
);
|
||||
result.diagnostics.push({
|
||||
type: "warning",
|
||||
message: `Invalid thinking level "${level}". Valid values: ${VALID_THINKING_LEVELS.join(", ")}`,
|
||||
});
|
||||
}
|
||||
} else if (arg === "--print" || arg === "-p") {
|
||||
result.print = true;
|
||||
@@ -159,18 +162,22 @@ export function parseArgs(args: string[], extensionFlags?: Map<string, { type: "
|
||||
result.offline = true;
|
||||
} else if (arg.startsWith("@")) {
|
||||
result.fileArgs.push(arg.slice(1)); // Remove @ prefix
|
||||
} else if (arg.startsWith("--") && extensionFlags) {
|
||||
// Check if it's an extension-registered flag
|
||||
const flagName = arg.slice(2);
|
||||
const extFlag = extensionFlags.get(flagName);
|
||||
if (extFlag) {
|
||||
if (extFlag.type === "boolean") {
|
||||
} else if (arg.startsWith("--")) {
|
||||
const eqIndex = arg.indexOf("=");
|
||||
if (eqIndex !== -1) {
|
||||
result.unknownFlags.set(arg.slice(2, eqIndex), arg.slice(eqIndex + 1));
|
||||
} else {
|
||||
const flagName = arg.slice(2);
|
||||
const next = args[i + 1];
|
||||
if (next !== undefined && !next.startsWith("-") && !next.startsWith("@")) {
|
||||
result.unknownFlags.set(flagName, next);
|
||||
i++;
|
||||
} else {
|
||||
result.unknownFlags.set(flagName, true);
|
||||
} else if (extFlag.type === "string" && i + 1 < args.length) {
|
||||
result.unknownFlags.set(flagName, args[++i]);
|
||||
}
|
||||
}
|
||||
// Unknown flags without extensionFlags are silently ignored (first pass)
|
||||
} else if (arg.startsWith("-") && !arg.startsWith("--")) {
|
||||
result.diagnostics.push({ type: "error", message: `Unknown option: ${arg}` });
|
||||
} else if (!arg.startsWith("-")) {
|
||||
result.messages.push(arg);
|
||||
}
|
||||
@@ -179,7 +186,17 @@ export function parseArgs(args: string[], extensionFlags?: Map<string, { type: "
|
||||
return result;
|
||||
}
|
||||
|
||||
export function printHelp(): void {
|
||||
export function printHelp(extensionFlags?: ExtensionFlag[]): void {
|
||||
const extensionFlagsText =
|
||||
extensionFlags && extensionFlags.length > 0
|
||||
? `\n${chalk.bold("Extension CLI Flags:")}\n${extensionFlags
|
||||
.map((flag) => {
|
||||
const value = flag.type === "string" ? " <value>" : "";
|
||||
const description = flag.description ?? `Registered by ${flag.extensionPath}`;
|
||||
return ` --${flag.name}${value}`.padEnd(30) + description;
|
||||
})
|
||||
.join("\n")}\n`
|
||||
: "";
|
||||
console.log(`${chalk.bold(APP_NAME)} - AI coding assistant with read, bash, edit, write tools
|
||||
|
||||
${chalk.bold("Usage:")}
|
||||
@@ -229,7 +246,7 @@ ${chalk.bold("Options:")}
|
||||
--help, -h Show this help
|
||||
--version, -v Show version number
|
||||
|
||||
Extensions can register additional flags (e.g., --plan from plan-mode extension).
|
||||
Extensions can register additional flags (e.g., --plan from plan-mode extension).${extensionFlagsText}
|
||||
|
||||
${chalk.bold("Examples:")}
|
||||
# Interactive mode
|
||||
|
||||
@@ -1,139 +1,36 @@
|
||||
import { copyFileSync, existsSync, mkdirSync } from "node:fs";
|
||||
import { basename, join, resolve } from "node:path";
|
||||
import type { ThinkingLevel } from "@mariozechner/pi-agent-core";
|
||||
import type { Model } from "@mariozechner/pi-ai";
|
||||
import { getAgentDir } from "../config.js";
|
||||
import { AuthStorage } from "./auth-storage.js";
|
||||
import type { SessionStartEvent, ToolDefinition } from "./extensions/index.js";
|
||||
import type { AgentSession } from "./agent-session.js";
|
||||
import type { AgentSessionRuntimeDiagnostic, AgentSessionServices } from "./agent-session-services.js";
|
||||
import type { SessionStartEvent } from "./extensions/index.js";
|
||||
import { emitSessionShutdownEvent } from "./extensions/runner.js";
|
||||
import { ModelRegistry } from "./model-registry.js";
|
||||
import { DefaultResourceLoader, type DefaultResourceLoaderOptions, type ResourceLoader } from "./resource-loader.js";
|
||||
import { type CreateAgentSessionResult, createAgentSession } from "./sdk.js";
|
||||
import type { CreateAgentSessionResult } from "./sdk.js";
|
||||
import { SessionManager } from "./session-manager.js";
|
||||
import { SettingsManager } from "./settings-manager.js";
|
||||
import type { Tool } from "./tools/index.js";
|
||||
|
||||
/**
|
||||
* Stable bootstrap inputs reused whenever the active runtime is replaced.
|
||||
* Result returned by runtime creation.
|
||||
*
|
||||
* Use this for process-level wiring that should survive `/new`, `/resume`,
|
||||
* `/fork`, and import flows. Session-local state belongs in the session file
|
||||
* or in settings, not here.
|
||||
* The caller gets the created session, its cwd-bound services, and all
|
||||
* diagnostics collected during setup.
|
||||
*/
|
||||
export interface AgentSessionRuntimeBootstrap {
|
||||
/** Agent directory used for auth, models, settings, sessions, and resource discovery. */
|
||||
agentDir?: string;
|
||||
/** Optional shared auth storage. If omitted, file-backed storage under agentDir is used. */
|
||||
authStorage?: AuthStorage;
|
||||
/** Initial model for the first created session runtime. */
|
||||
model?: Model<any>;
|
||||
/** Initial thinking level for the first created session runtime. */
|
||||
thinkingLevel?: ThinkingLevel;
|
||||
/** Optional scoped model list for model cycling and selection. */
|
||||
scopedModels?: Array<{ model: Model<any>; thinkingLevel?: ThinkingLevel }>;
|
||||
/** Built-in tool set override. */
|
||||
tools?: Tool[];
|
||||
/** Additional custom tools registered directly through the SDK. */
|
||||
customTools?: ToolDefinition[];
|
||||
/**
|
||||
* Resource loader input used for each created runtime.
|
||||
*
|
||||
* Pass either a factory that creates a fully custom ResourceLoader for the
|
||||
* target cwd, or DefaultResourceLoader options without cwd/agentDir/
|
||||
* settingsManager, which are supplied by the runtime.
|
||||
*/
|
||||
resourceLoader?:
|
||||
| ((cwd: string, agentDir: string) => Promise<ResourceLoader>)
|
||||
| Omit<DefaultResourceLoaderOptions, "cwd" | "agentDir" | "settingsManager">;
|
||||
export interface CreateAgentSessionRuntimeResult extends CreateAgentSessionResult {
|
||||
services: AgentSessionServices;
|
||||
diagnostics: AgentSessionRuntimeDiagnostic[];
|
||||
}
|
||||
|
||||
/** Options for creating one concrete runtime instance. */
|
||||
export interface CreateAgentSessionRuntimeOptions {
|
||||
/** Working directory for this runtime instance. */
|
||||
cwd: string;
|
||||
/** Optional preselected session manager. If omitted, normal session resolution applies. */
|
||||
sessionManager?: SessionManager;
|
||||
/** Optional preloaded resource loader to reuse instead of creating and reloading one. */
|
||||
resourceLoader?: ResourceLoader;
|
||||
/** Optional session_start metadata to emit when the runtime binds extensions. */
|
||||
sessionStartEvent?: SessionStartEvent;
|
||||
}
|
||||
|
||||
type AgentSessionRuntime = CreateAgentSessionResult & {
|
||||
/**
|
||||
* Creates a full runtime for a target cwd and session manager.
|
||||
*
|
||||
* The factory closes over process-global fixed inputs, recreates cwd-bound
|
||||
* services for the effective cwd, resolves session options against those
|
||||
* services, and finally creates the AgentSession.
|
||||
*/
|
||||
export type CreateAgentSessionRuntimeFactory = (options: {
|
||||
cwd: string;
|
||||
agentDir: string;
|
||||
authStorage: AuthStorage;
|
||||
modelRegistry: ModelRegistry;
|
||||
settingsManager: SettingsManager;
|
||||
resourceLoader: ResourceLoader;
|
||||
sessionManager: SessionManager;
|
||||
};
|
||||
|
||||
/**
|
||||
* Create one runtime instance containing an AgentSession plus the cwd-bound
|
||||
* services it depends on.
|
||||
*
|
||||
* Most SDK callers should keep the returned value wrapped in an
|
||||
* AgentSessionRuntimeHost instead of holding it directly. The host owns
|
||||
* replacing the runtime when switching sessions across files or working
|
||||
* directories.
|
||||
*/
|
||||
export async function createAgentSessionRuntime(
|
||||
bootstrap: AgentSessionRuntimeBootstrap,
|
||||
options: CreateAgentSessionRuntimeOptions,
|
||||
): Promise<AgentSessionRuntime> {
|
||||
const cwd = options.cwd;
|
||||
const agentDir = bootstrap.agentDir ?? getAgentDir();
|
||||
const authStorage = bootstrap.authStorage ?? AuthStorage.create(join(agentDir, "auth.json"));
|
||||
const settingsManager = SettingsManager.create(cwd, agentDir);
|
||||
const modelRegistry = ModelRegistry.create(authStorage, join(agentDir, "models.json"));
|
||||
const resourceLoader =
|
||||
options.resourceLoader ??
|
||||
(typeof bootstrap.resourceLoader === "function"
|
||||
? await bootstrap.resourceLoader(cwd, agentDir)
|
||||
: new DefaultResourceLoader({
|
||||
...(bootstrap.resourceLoader ?? {}),
|
||||
cwd,
|
||||
agentDir,
|
||||
settingsManager,
|
||||
}));
|
||||
if (!options.resourceLoader) {
|
||||
await resourceLoader.reload();
|
||||
}
|
||||
|
||||
const extensionsResult = resourceLoader.getExtensions();
|
||||
for (const { name, config } of extensionsResult.runtime.pendingProviderRegistrations) {
|
||||
modelRegistry.registerProvider(name, config);
|
||||
}
|
||||
extensionsResult.runtime.pendingProviderRegistrations = [];
|
||||
|
||||
const created = await createAgentSession({
|
||||
cwd,
|
||||
agentDir,
|
||||
authStorage,
|
||||
modelRegistry,
|
||||
settingsManager,
|
||||
resourceLoader,
|
||||
sessionManager: options.sessionManager,
|
||||
model: bootstrap.model,
|
||||
thinkingLevel: bootstrap.thinkingLevel,
|
||||
scopedModels: bootstrap.scopedModels,
|
||||
tools: bootstrap.tools,
|
||||
customTools: bootstrap.customTools,
|
||||
sessionStartEvent: options.sessionStartEvent,
|
||||
});
|
||||
|
||||
return {
|
||||
...created,
|
||||
cwd,
|
||||
agentDir,
|
||||
authStorage,
|
||||
modelRegistry,
|
||||
settingsManager,
|
||||
resourceLoader,
|
||||
sessionManager: created.session.sessionManager,
|
||||
};
|
||||
}
|
||||
sessionStartEvent?: SessionStartEvent;
|
||||
}) => Promise<CreateAgentSessionRuntimeResult>;
|
||||
|
||||
function extractUserMessageText(content: string | Array<{ type: string; text?: string }>): string {
|
||||
if (typeof content === "string") {
|
||||
@@ -147,28 +44,46 @@ function extractUserMessageText(content: string | Array<{ type: string; text?: s
|
||||
}
|
||||
|
||||
/**
|
||||
* Stable wrapper around a replaceable AgentSession runtime.
|
||||
* Owns the current AgentSession plus its cwd-bound services.
|
||||
*
|
||||
* Use this when your application needs `/new`, `/resume`, `/fork`, or import
|
||||
* behavior. After replacement, read `session` again and rebind any
|
||||
* session-local subscriptions or extension bindings.
|
||||
* Session replacement methods tear down the current runtime first, then create
|
||||
* and apply the next runtime. If creation fails, the error is propagated to the
|
||||
* caller. The caller is responsible for user-facing error handling.
|
||||
*/
|
||||
export class AgentSessionRuntimeHost {
|
||||
export class AgentSessionRuntime {
|
||||
constructor(
|
||||
private readonly bootstrap: AgentSessionRuntimeBootstrap,
|
||||
private runtime: AgentSessionRuntime,
|
||||
private _session: AgentSession,
|
||||
private _services: AgentSessionServices,
|
||||
private readonly createRuntime: CreateAgentSessionRuntimeFactory,
|
||||
private _diagnostics: AgentSessionRuntimeDiagnostic[] = [],
|
||||
private _modelFallbackMessage?: string,
|
||||
) {}
|
||||
|
||||
/** The currently active session instance. Re-read this after runtime replacement. */
|
||||
get session() {
|
||||
return this.runtime.session;
|
||||
get services(): AgentSessionServices {
|
||||
return this._services;
|
||||
}
|
||||
|
||||
get session(): AgentSession {
|
||||
return this._session;
|
||||
}
|
||||
|
||||
get cwd(): string {
|
||||
return this._services.cwd;
|
||||
}
|
||||
|
||||
get diagnostics(): readonly AgentSessionRuntimeDiagnostic[] {
|
||||
return this._diagnostics;
|
||||
}
|
||||
|
||||
get modelFallbackMessage(): string | undefined {
|
||||
return this._modelFallbackMessage;
|
||||
}
|
||||
|
||||
private async emitBeforeSwitch(
|
||||
reason: "new" | "resume",
|
||||
targetSessionFile?: string,
|
||||
): Promise<{ cancelled: boolean }> {
|
||||
const runner = this.runtime.session.extensionRunner;
|
||||
const runner = this.session.extensionRunner;
|
||||
if (!runner?.hasHandlers("session_before_switch")) {
|
||||
return { cancelled: false };
|
||||
}
|
||||
@@ -182,7 +97,7 @@ export class AgentSessionRuntimeHost {
|
||||
}
|
||||
|
||||
private async emitBeforeFork(entryId: string): Promise<{ cancelled: boolean }> {
|
||||
const runner = this.runtime.session.extensionRunner;
|
||||
const runner = this.session.extensionRunner;
|
||||
if (!runner?.hasHandlers("session_before_fork")) {
|
||||
return { cancelled: false };
|
||||
}
|
||||
@@ -194,48 +109,43 @@ export class AgentSessionRuntimeHost {
|
||||
return { cancelled: result?.cancel === true };
|
||||
}
|
||||
|
||||
private async replace(options: CreateAgentSessionRuntimeOptions): Promise<void> {
|
||||
const nextRuntime = await createAgentSessionRuntime(this.bootstrap, options);
|
||||
await emitSessionShutdownEvent(this.runtime.session.extensionRunner);
|
||||
this.runtime.session.dispose();
|
||||
if (process.cwd() !== nextRuntime.cwd) {
|
||||
process.chdir(nextRuntime.cwd);
|
||||
}
|
||||
this.runtime = nextRuntime;
|
||||
private async teardownCurrent(): Promise<void> {
|
||||
await emitSessionShutdownEvent(this.session.extensionRunner);
|
||||
this.session.dispose();
|
||||
}
|
||||
|
||||
private apply(result: CreateAgentSessionRuntimeResult): void {
|
||||
if (process.cwd() !== result.services.cwd) {
|
||||
process.chdir(result.services.cwd);
|
||||
}
|
||||
this._session = result.session;
|
||||
this._services = result.services;
|
||||
this._diagnostics = result.diagnostics;
|
||||
this._modelFallbackMessage = result.modelFallbackMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the active runtime with one opened from an existing session file.
|
||||
*
|
||||
* Emits `session_before_switch` before replacement and returns
|
||||
* `{ cancelled: true }` if an extension vetoes the switch.
|
||||
*/
|
||||
async switchSession(sessionPath: string): Promise<{ cancelled: boolean }> {
|
||||
const beforeResult = await this.emitBeforeSwitch("resume", sessionPath);
|
||||
if (beforeResult.cancelled) {
|
||||
return beforeResult;
|
||||
}
|
||||
|
||||
const previousSessionFile = this.runtime.session.sessionFile;
|
||||
const previousSessionFile = this.session.sessionFile;
|
||||
const sessionManager = SessionManager.open(sessionPath);
|
||||
await this.replace({
|
||||
cwd: sessionManager.getCwd(),
|
||||
sessionManager,
|
||||
sessionStartEvent: { type: "session_start", reason: "resume", previousSessionFile },
|
||||
});
|
||||
await this.teardownCurrent();
|
||||
this.apply(
|
||||
await this.createRuntime({
|
||||
cwd: sessionManager.getCwd(),
|
||||
agentDir: this.services.agentDir,
|
||||
sessionManager,
|
||||
sessionStartEvent: { type: "session_start", reason: "resume", previousSessionFile },
|
||||
}),
|
||||
);
|
||||
return { cancelled: false };
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the active runtime with a fresh session in the current cwd.
|
||||
*
|
||||
* `setup` runs after replacement against the new session manager, which lets
|
||||
* callers seed entries before normal use begins.
|
||||
*/
|
||||
async newSession(options?: {
|
||||
/** Optional parent session path recorded in the new session header. */
|
||||
parentSession?: string;
|
||||
/** Optional callback for seeding the new session manager after replacement. */
|
||||
setup?: (sessionManager: SessionManager) => Promise<void>;
|
||||
}): Promise<{ cancelled: boolean }> {
|
||||
const beforeResult = await this.emitBeforeSwitch("new");
|
||||
@@ -243,94 +153,106 @@ export class AgentSessionRuntimeHost {
|
||||
return beforeResult;
|
||||
}
|
||||
|
||||
const previousSessionFile = this.runtime.session.sessionFile;
|
||||
const sessionDir = this.runtime.sessionManager.getSessionDir();
|
||||
const sessionManager = SessionManager.create(this.runtime.cwd, sessionDir);
|
||||
const previousSessionFile = this.session.sessionFile;
|
||||
const sessionDir = this.session.sessionManager.getSessionDir();
|
||||
const sessionManager = SessionManager.create(this.cwd, sessionDir);
|
||||
if (options?.parentSession) {
|
||||
sessionManager.newSession({ parentSession: options.parentSession });
|
||||
}
|
||||
await this.replace({
|
||||
cwd: this.runtime.cwd,
|
||||
sessionManager,
|
||||
sessionStartEvent: { type: "session_start", reason: "new", previousSessionFile },
|
||||
});
|
||||
|
||||
await this.teardownCurrent();
|
||||
this.apply(
|
||||
await this.createRuntime({
|
||||
cwd: this.cwd,
|
||||
agentDir: this.services.agentDir,
|
||||
sessionManager,
|
||||
sessionStartEvent: { type: "session_start", reason: "new", previousSessionFile },
|
||||
}),
|
||||
);
|
||||
if (options?.setup) {
|
||||
await options.setup(this.runtime.sessionManager);
|
||||
this.runtime.session.agent.state.messages = this.runtime.sessionManager.buildSessionContext().messages;
|
||||
await options.setup(this.session.sessionManager);
|
||||
this.session.agent.state.messages = this.session.sessionManager.buildSessionContext().messages;
|
||||
}
|
||||
return { cancelled: false };
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the active runtime with a fork rooted at the given user-message
|
||||
* entry.
|
||||
*
|
||||
* Returns the selected user text so UIs can restore it into the editor after
|
||||
* the fork completes.
|
||||
*/
|
||||
async fork(entryId: string): Promise<{ cancelled: boolean; selectedText?: string }> {
|
||||
const beforeResult = await this.emitBeforeFork(entryId);
|
||||
if (beforeResult.cancelled) {
|
||||
return { cancelled: true };
|
||||
}
|
||||
|
||||
const selectedEntry = this.runtime.sessionManager.getEntry(entryId);
|
||||
const selectedEntry = this.session.sessionManager.getEntry(entryId);
|
||||
if (!selectedEntry || selectedEntry.type !== "message" || selectedEntry.message.role !== "user") {
|
||||
throw new Error("Invalid entry ID for forking");
|
||||
}
|
||||
|
||||
const previousSessionFile = this.runtime.session.sessionFile;
|
||||
const previousSessionFile = this.session.sessionFile;
|
||||
const selectedText = extractUserMessageText(selectedEntry.message.content);
|
||||
if (this.runtime.sessionManager.isPersisted()) {
|
||||
const currentSessionFile = this.runtime.session.sessionFile!;
|
||||
const sessionDir = this.runtime.sessionManager.getSessionDir();
|
||||
if (this.session.sessionManager.isPersisted()) {
|
||||
const currentSessionFile = this.session.sessionFile;
|
||||
if (!currentSessionFile) {
|
||||
throw new Error("Persisted session is missing a session file");
|
||||
}
|
||||
const sessionDir = this.session.sessionManager.getSessionDir();
|
||||
if (!selectedEntry.parentId) {
|
||||
const sessionManager = SessionManager.create(this.runtime.cwd, sessionDir);
|
||||
const sessionManager = SessionManager.create(this.cwd, sessionDir);
|
||||
sessionManager.newSession({ parentSession: currentSessionFile });
|
||||
await this.replace({
|
||||
cwd: this.runtime.cwd,
|
||||
sessionManager,
|
||||
sessionStartEvent: { type: "session_start", reason: "fork", previousSessionFile },
|
||||
});
|
||||
await this.teardownCurrent();
|
||||
this.apply(
|
||||
await this.createRuntime({
|
||||
cwd: this.cwd,
|
||||
agentDir: this.services.agentDir,
|
||||
sessionManager,
|
||||
sessionStartEvent: { type: "session_start", reason: "fork", previousSessionFile },
|
||||
}),
|
||||
);
|
||||
return { cancelled: false, selectedText };
|
||||
}
|
||||
|
||||
const sourceManager = SessionManager.open(currentSessionFile, sessionDir);
|
||||
const forkedSessionPath = sourceManager.createBranchedSession(selectedEntry.parentId)!;
|
||||
const forkedSessionPath = sourceManager.createBranchedSession(selectedEntry.parentId);
|
||||
if (!forkedSessionPath) {
|
||||
throw new Error("Failed to create forked session");
|
||||
}
|
||||
const sessionManager = SessionManager.open(forkedSessionPath, sessionDir);
|
||||
await this.replace({
|
||||
cwd: sessionManager.getCwd(),
|
||||
sessionManager,
|
||||
sessionStartEvent: { type: "session_start", reason: "fork", previousSessionFile },
|
||||
});
|
||||
await this.teardownCurrent();
|
||||
this.apply(
|
||||
await this.createRuntime({
|
||||
cwd: sessionManager.getCwd(),
|
||||
agentDir: this.services.agentDir,
|
||||
sessionManager,
|
||||
sessionStartEvent: { type: "session_start", reason: "fork", previousSessionFile },
|
||||
}),
|
||||
);
|
||||
return { cancelled: false, selectedText };
|
||||
}
|
||||
|
||||
const sessionManager = this.runtime.sessionManager;
|
||||
const sessionManager = this.session.sessionManager;
|
||||
if (!selectedEntry.parentId) {
|
||||
sessionManager.newSession({ parentSession: this.runtime.session.sessionFile });
|
||||
sessionManager.newSession({ parentSession: this.session.sessionFile });
|
||||
} else {
|
||||
sessionManager.createBranchedSession(selectedEntry.parentId);
|
||||
}
|
||||
await this.replace({
|
||||
cwd: this.runtime.cwd,
|
||||
sessionManager,
|
||||
sessionStartEvent: { type: "session_start", reason: "fork", previousSessionFile },
|
||||
});
|
||||
await this.teardownCurrent();
|
||||
this.apply(
|
||||
await this.createRuntime({
|
||||
cwd: this.cwd,
|
||||
agentDir: this.services.agentDir,
|
||||
sessionManager,
|
||||
sessionStartEvent: { type: "session_start", reason: "fork", previousSessionFile },
|
||||
}),
|
||||
);
|
||||
return { cancelled: false, selectedText };
|
||||
}
|
||||
|
||||
/**
|
||||
* Import a JSONL session file into the current session directory and replace
|
||||
* the active runtime with the imported session.
|
||||
*/
|
||||
async importFromJsonl(inputPath: string): Promise<{ cancelled: boolean }> {
|
||||
const resolvedPath = resolve(inputPath);
|
||||
if (!existsSync(resolvedPath)) {
|
||||
throw new Error(`File not found: ${resolvedPath}`);
|
||||
}
|
||||
|
||||
const sessionDir = this.runtime.sessionManager.getSessionDir();
|
||||
const sessionDir = this.session.sessionManager.getSessionDir();
|
||||
if (!existsSync(sessionDir)) {
|
||||
mkdirSync(sessionDir, { recursive: true });
|
||||
}
|
||||
@@ -341,23 +263,63 @@ export class AgentSessionRuntimeHost {
|
||||
return beforeResult;
|
||||
}
|
||||
|
||||
const previousSessionFile = this.runtime.session.sessionFile;
|
||||
const previousSessionFile = this.session.sessionFile;
|
||||
if (resolve(destinationPath) !== resolvedPath) {
|
||||
copyFileSync(resolvedPath, destinationPath);
|
||||
}
|
||||
|
||||
const sessionManager = SessionManager.open(destinationPath, sessionDir);
|
||||
await this.replace({
|
||||
cwd: sessionManager.getCwd(),
|
||||
sessionManager,
|
||||
sessionStartEvent: { type: "session_start", reason: "resume", previousSessionFile },
|
||||
});
|
||||
await this.teardownCurrent();
|
||||
this.apply(
|
||||
await this.createRuntime({
|
||||
cwd: sessionManager.getCwd(),
|
||||
agentDir: this.services.agentDir,
|
||||
sessionManager,
|
||||
sessionStartEvent: { type: "session_start", reason: "resume", previousSessionFile },
|
||||
}),
|
||||
);
|
||||
return { cancelled: false };
|
||||
}
|
||||
|
||||
/** Emit session shutdown for the active runtime and dispose it permanently. */
|
||||
async dispose(): Promise<void> {
|
||||
await emitSessionShutdownEvent(this.runtime.session.extensionRunner);
|
||||
this.runtime.session.dispose();
|
||||
await emitSessionShutdownEvent(this.session.extensionRunner);
|
||||
this.session.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the initial runtime from a runtime factory and initial session target.
|
||||
*
|
||||
* The same factory is stored on the returned AgentSessionRuntime and reused for
|
||||
* later /new, /resume, /fork, and import flows.
|
||||
*/
|
||||
export async function createAgentSessionRuntime(
|
||||
createRuntime: CreateAgentSessionRuntimeFactory,
|
||||
options: {
|
||||
cwd: string;
|
||||
agentDir: string;
|
||||
sessionManager: SessionManager;
|
||||
sessionStartEvent?: SessionStartEvent;
|
||||
},
|
||||
): Promise<AgentSessionRuntime> {
|
||||
const result = await createRuntime(options);
|
||||
if (process.cwd() !== result.services.cwd) {
|
||||
process.chdir(result.services.cwd);
|
||||
}
|
||||
return new AgentSessionRuntime(
|
||||
result.session,
|
||||
result.services,
|
||||
createRuntime,
|
||||
result.diagnostics,
|
||||
result.modelFallbackMessage,
|
||||
);
|
||||
}
|
||||
|
||||
export {
|
||||
type AgentSessionRuntimeDiagnostic,
|
||||
type AgentSessionServices,
|
||||
type CreateAgentSessionFromServicesOptions,
|
||||
type CreateAgentSessionServicesOptions,
|
||||
createAgentSessionFromServices,
|
||||
createAgentSessionServices,
|
||||
} from "./agent-session-services.js";
|
||||
|
||||
197
packages/coding-agent/src/core/agent-session-services.ts
Normal file
197
packages/coding-agent/src/core/agent-session-services.ts
Normal file
@@ -0,0 +1,197 @@
|
||||
import { join } from "node:path";
|
||||
import type { ThinkingLevel } from "@mariozechner/pi-agent-core";
|
||||
import type { Model } from "@mariozechner/pi-ai";
|
||||
import { getAgentDir } from "../config.js";
|
||||
import { AuthStorage } from "./auth-storage.js";
|
||||
import type { SessionStartEvent, ToolDefinition } from "./extensions/index.js";
|
||||
import { ModelRegistry } from "./model-registry.js";
|
||||
import { DefaultResourceLoader, type DefaultResourceLoaderOptions, type ResourceLoader } from "./resource-loader.js";
|
||||
import { type CreateAgentSessionResult, createAgentSession } from "./sdk.js";
|
||||
import type { SessionManager } from "./session-manager.js";
|
||||
import { SettingsManager } from "./settings-manager.js";
|
||||
import type { Tool } from "./tools/index.js";
|
||||
|
||||
/**
|
||||
* Non-fatal issues collected while creating services or sessions.
|
||||
*
|
||||
* Runtime creation returns diagnostics to the caller instead of printing or
|
||||
* exiting. The app layer decides whether warnings should be shown and whether
|
||||
* errors should abort startup.
|
||||
*/
|
||||
export interface AgentSessionRuntimeDiagnostic {
|
||||
type: "info" | "warning" | "error";
|
||||
message: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inputs for creating cwd-bound runtime services.
|
||||
*
|
||||
* These services are recreated whenever the effective session cwd changes.
|
||||
* CLI-provided resource paths should be resolved to absolute paths before they
|
||||
* reach this function, so later cwd switches do not reinterpret them.
|
||||
*/
|
||||
export interface CreateAgentSessionServicesOptions {
|
||||
cwd: string;
|
||||
agentDir?: string;
|
||||
authStorage?: AuthStorage;
|
||||
settingsManager?: SettingsManager;
|
||||
modelRegistry?: ModelRegistry;
|
||||
extensionFlagValues?: Map<string, boolean | string>;
|
||||
resourceLoaderOptions?: Omit<DefaultResourceLoaderOptions, "cwd" | "agentDir" | "settingsManager">;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inputs for creating an AgentSession from already-created services.
|
||||
*
|
||||
* Use this after services exist and any cwd-bound model/tool/session options
|
||||
* have been resolved against those services.
|
||||
*/
|
||||
export interface CreateAgentSessionFromServicesOptions {
|
||||
services: AgentSessionServices;
|
||||
sessionManager: SessionManager;
|
||||
sessionStartEvent?: SessionStartEvent;
|
||||
model?: Model<any>;
|
||||
thinkingLevel?: ThinkingLevel;
|
||||
scopedModels?: Array<{ model: Model<any>; thinkingLevel?: ThinkingLevel }>;
|
||||
tools?: Tool[];
|
||||
customTools?: ToolDefinition[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Coherent cwd-bound runtime services for one effective session cwd.
|
||||
*
|
||||
* This is infrastructure only. The AgentSession itself is created separately so
|
||||
* session options can be resolved against these services first.
|
||||
*/
|
||||
export interface AgentSessionServices {
|
||||
cwd: string;
|
||||
agentDir: string;
|
||||
authStorage: AuthStorage;
|
||||
settingsManager: SettingsManager;
|
||||
modelRegistry: ModelRegistry;
|
||||
resourceLoader: ResourceLoader;
|
||||
diagnostics: AgentSessionRuntimeDiagnostic[];
|
||||
}
|
||||
|
||||
function applyExtensionFlagValues(
|
||||
resourceLoader: ResourceLoader,
|
||||
extensionFlagValues: Map<string, boolean | string> | undefined,
|
||||
): AgentSessionRuntimeDiagnostic[] {
|
||||
if (!extensionFlagValues) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const diagnostics: AgentSessionRuntimeDiagnostic[] = [];
|
||||
const extensionsResult = resourceLoader.getExtensions();
|
||||
const registeredFlags = new Map<string, { type: "boolean" | "string" }>();
|
||||
for (const extension of extensionsResult.extensions) {
|
||||
for (const [name, flag] of extension.flags) {
|
||||
registeredFlags.set(name, { type: flag.type });
|
||||
}
|
||||
}
|
||||
|
||||
const unknownFlags: string[] = [];
|
||||
for (const [name, value] of extensionFlagValues) {
|
||||
const flag = registeredFlags.get(name);
|
||||
if (!flag) {
|
||||
unknownFlags.push(name);
|
||||
continue;
|
||||
}
|
||||
if (flag.type === "boolean") {
|
||||
extensionsResult.runtime.flagValues.set(name, true);
|
||||
continue;
|
||||
}
|
||||
if (typeof value === "string") {
|
||||
extensionsResult.runtime.flagValues.set(name, value);
|
||||
continue;
|
||||
}
|
||||
diagnostics.push({
|
||||
type: "error",
|
||||
message: `Extension flag "--${name}" requires a value`,
|
||||
});
|
||||
}
|
||||
|
||||
if (unknownFlags.length > 0) {
|
||||
diagnostics.push({
|
||||
type: "error",
|
||||
message: `Unknown option${unknownFlags.length === 1 ? "" : "s"}: ${unknownFlags.map((name) => `--${name}`).join(", ")}`,
|
||||
});
|
||||
}
|
||||
|
||||
return diagnostics;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create cwd-bound runtime services.
|
||||
*
|
||||
* Returns services plus diagnostics. It does not create an AgentSession.
|
||||
*/
|
||||
export async function createAgentSessionServices(
|
||||
options: CreateAgentSessionServicesOptions,
|
||||
): Promise<AgentSessionServices> {
|
||||
const cwd = options.cwd;
|
||||
const agentDir = options.agentDir ?? getAgentDir();
|
||||
const authStorage = options.authStorage ?? AuthStorage.create(join(agentDir, "auth.json"));
|
||||
const settingsManager = options.settingsManager ?? SettingsManager.create(cwd, agentDir);
|
||||
const modelRegistry = options.modelRegistry ?? ModelRegistry.create(authStorage, join(agentDir, "models.json"));
|
||||
const resourceLoader = new DefaultResourceLoader({
|
||||
...(options.resourceLoaderOptions ?? {}),
|
||||
cwd,
|
||||
agentDir,
|
||||
settingsManager,
|
||||
});
|
||||
await resourceLoader.reload();
|
||||
|
||||
const diagnostics: AgentSessionRuntimeDiagnostic[] = [];
|
||||
const extensionsResult = resourceLoader.getExtensions();
|
||||
for (const { name, config, extensionPath } of extensionsResult.runtime.pendingProviderRegistrations) {
|
||||
try {
|
||||
modelRegistry.registerProvider(name, config);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
diagnostics.push({
|
||||
type: "error",
|
||||
message: `Extension "${extensionPath}" error: ${message}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
extensionsResult.runtime.pendingProviderRegistrations = [];
|
||||
diagnostics.push(...applyExtensionFlagValues(resourceLoader, options.extensionFlagValues));
|
||||
|
||||
return {
|
||||
cwd,
|
||||
agentDir,
|
||||
authStorage,
|
||||
settingsManager,
|
||||
modelRegistry,
|
||||
resourceLoader,
|
||||
diagnostics,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an AgentSession from previously created services.
|
||||
*
|
||||
* This keeps session creation separate from service creation so callers can
|
||||
* resolve model, thinking, tools, and other session inputs against the target
|
||||
* cwd before constructing the session.
|
||||
*/
|
||||
export async function createAgentSessionFromServices(
|
||||
options: CreateAgentSessionFromServicesOptions,
|
||||
): Promise<CreateAgentSessionResult> {
|
||||
return createAgentSession({
|
||||
cwd: options.services.cwd,
|
||||
agentDir: options.services.agentDir,
|
||||
authStorage: options.services.authStorage,
|
||||
settingsManager: options.services.settingsManager,
|
||||
modelRegistry: options.services.modelRegistry,
|
||||
resourceLoader: options.services.resourceLoader,
|
||||
sessionManager: options.sessionManager,
|
||||
model: options.model,
|
||||
thinkingLevel: options.thinkingLevel,
|
||||
scopedModels: options.scopedModels,
|
||||
tools: options.tools,
|
||||
customTools: options.customTools,
|
||||
sessionStartEvent: options.sessionStartEvent,
|
||||
});
|
||||
}
|
||||
@@ -2350,7 +2350,7 @@ export class AgentSession {
|
||||
async reload(): Promise<void> {
|
||||
const previousFlagValues = this._extensionRunner?.getFlagValues();
|
||||
await this._extensionRunner?.emit({ type: "session_shutdown" });
|
||||
this.settingsManager.reload();
|
||||
await this.settingsManager.reload();
|
||||
resetApiProviders();
|
||||
await this._resourceLoader.reload();
|
||||
this._buildRuntime({
|
||||
|
||||
@@ -116,9 +116,6 @@ export type {
|
||||
SessionBeforeTreeEvent,
|
||||
SessionBeforeTreeResult,
|
||||
SessionCompactEvent,
|
||||
SessionDirectoryEvent,
|
||||
SessionDirectoryHandler,
|
||||
SessionDirectoryResult,
|
||||
SessionEvent,
|
||||
SessionShutdownEvent,
|
||||
// Events - Session
|
||||
|
||||
@@ -441,12 +441,6 @@ export interface ResourcesDiscoverResult {
|
||||
// Session Events
|
||||
// ============================================================================
|
||||
|
||||
/** Fired before session manager creation to allow custom session directory resolution */
|
||||
export interface SessionDirectoryEvent {
|
||||
type: "session_directory";
|
||||
cwd: string;
|
||||
}
|
||||
|
||||
/** Fired when a session is started, loaded, or reloaded */
|
||||
export interface SessionStartEvent {
|
||||
type: "session_start";
|
||||
@@ -522,7 +516,6 @@ export interface SessionTreeEvent {
|
||||
}
|
||||
|
||||
export type SessionEvent =
|
||||
| SessionDirectoryEvent
|
||||
| SessionStartEvent
|
||||
| SessionBeforeSwitchEvent
|
||||
| SessionBeforeForkEvent
|
||||
@@ -921,16 +914,6 @@ export interface BeforeAgentStartEventResult {
|
||||
systemPrompt?: string;
|
||||
}
|
||||
|
||||
export interface SessionDirectoryResult {
|
||||
/** Custom session directory path. If multiple extensions return this, the last one wins. */
|
||||
sessionDir?: string;
|
||||
}
|
||||
|
||||
/** Special startup-only handler. Unlike other events, this receives no ExtensionContext. */
|
||||
export type SessionDirectoryHandler = (
|
||||
event: SessionDirectoryEvent,
|
||||
) => Promise<SessionDirectoryResult | undefined> | SessionDirectoryResult | undefined;
|
||||
|
||||
export interface SessionBeforeSwitchResult {
|
||||
cancel?: boolean;
|
||||
}
|
||||
@@ -1006,7 +989,6 @@ export interface ExtensionAPI {
|
||||
// =========================================================================
|
||||
|
||||
on(event: "resources_discover", handler: ExtensionHandler<ResourcesDiscoverEvent, ResourcesDiscoverResult>): void;
|
||||
on(event: "session_directory", handler: SessionDirectoryHandler): void;
|
||||
on(event: "session_start", handler: ExtensionHandler<SessionStartEvent>): void;
|
||||
on(
|
||||
event: "session_before_switch",
|
||||
|
||||
@@ -12,11 +12,19 @@ export {
|
||||
type SessionStats,
|
||||
} from "./agent-session.js";
|
||||
export {
|
||||
type AgentSessionRuntimeBootstrap,
|
||||
AgentSessionRuntimeHost,
|
||||
type CreateAgentSessionRuntimeOptions,
|
||||
AgentSessionRuntime,
|
||||
type CreateAgentSessionRuntimeFactory,
|
||||
type CreateAgentSessionRuntimeResult,
|
||||
createAgentSessionRuntime,
|
||||
} from "./agent-session-runtime.js";
|
||||
export {
|
||||
type AgentSessionRuntimeDiagnostic,
|
||||
type AgentSessionServices,
|
||||
type CreateAgentSessionFromServicesOptions,
|
||||
type CreateAgentSessionServicesOptions,
|
||||
createAgentSessionFromServices,
|
||||
createAgentSessionServices,
|
||||
} from "./agent-session-services.js";
|
||||
export { type BashExecutorOptions, type BashResult, executeBash, executeBashWithOperations } from "./bash-executor.js";
|
||||
export type { CompactionResult } from "./compaction/index.js";
|
||||
export { createEventBus, type EventBus, type EventBusController } from "./event-bus.js";
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
TUI_KEYBINDINGS,
|
||||
KeybindingsManager as TuiKeybindingsManager,
|
||||
} from "@mariozechner/pi-tui";
|
||||
import { existsSync, readFileSync, writeFileSync } from "fs";
|
||||
import { existsSync, readFileSync } from "fs";
|
||||
import { join } from "path";
|
||||
import { getAgentDir } from "../config.js";
|
||||
|
||||
@@ -219,7 +219,7 @@ function toKeybindingsConfig(value: unknown): KeybindingsConfig {
|
||||
return config;
|
||||
}
|
||||
|
||||
function migrateKeybindingNames(rawConfig: Record<string, unknown>): {
|
||||
export function migrateKeybindingsConfig(rawConfig: Record<string, unknown>): {
|
||||
config: Record<string, unknown>;
|
||||
migrated: boolean;
|
||||
} {
|
||||
@@ -269,18 +269,6 @@ function loadRawConfig(path: string): Record<string, unknown> | undefined {
|
||||
}
|
||||
}
|
||||
|
||||
export function migrateKeybindingsConfigFile(agentDir: string = getAgentDir()): boolean {
|
||||
const configPath = join(agentDir, "keybindings.json");
|
||||
const rawConfig = loadRawConfig(configPath);
|
||||
if (!rawConfig) return false;
|
||||
|
||||
const { config, migrated } = migrateKeybindingNames(rawConfig);
|
||||
if (!migrated) return false;
|
||||
|
||||
writeFileSync(configPath, `${JSON.stringify(config, null, 2)}\n`, "utf-8");
|
||||
return true;
|
||||
}
|
||||
|
||||
export class KeybindingsManager extends TuiKeybindingsManager {
|
||||
private configPath: string | undefined;
|
||||
|
||||
@@ -307,7 +295,7 @@ export class KeybindingsManager extends TuiKeybindingsManager {
|
||||
private static loadFromFile(path: string): KeybindingsConfig {
|
||||
const rawConfig = loadRawConfig(path);
|
||||
if (!rawConfig) return {};
|
||||
return toKeybindingsConfig(migrateKeybindingNames(rawConfig).config);
|
||||
return toKeybindingsConfig(migrateKeybindingsConfig(rawConfig).config);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -57,11 +57,21 @@ export interface PackageUpdate {
|
||||
scope: Exclude<SourceScope, "temporary">;
|
||||
}
|
||||
|
||||
export interface ConfiguredPackage {
|
||||
source: string;
|
||||
scope: "user" | "project";
|
||||
filtered: boolean;
|
||||
installedPath?: string;
|
||||
}
|
||||
|
||||
export interface PackageManager {
|
||||
resolve(onMissing?: (source: string) => Promise<MissingSourceAction>): Promise<ResolvedPaths>;
|
||||
install(source: string, options?: { local?: boolean }): Promise<void>;
|
||||
installAndPersist(source: string, options?: { local?: boolean }): Promise<void>;
|
||||
remove(source: string, options?: { local?: boolean }): Promise<void>;
|
||||
removeAndPersist(source: string, options?: { local?: boolean }): Promise<boolean>;
|
||||
update(source?: string): Promise<void>;
|
||||
listConfiguredPackages(): ConfiguredPackage[];
|
||||
resolveExtensionSources(
|
||||
sources: string[],
|
||||
options?: { local?: boolean; temporary?: boolean },
|
||||
@@ -837,6 +847,34 @@ export class DefaultPackageManager implements PackageManager {
|
||||
return this.toResolvedPaths(accumulator);
|
||||
}
|
||||
|
||||
listConfiguredPackages(): ConfiguredPackage[] {
|
||||
const globalSettings = this.settingsManager.getGlobalSettings();
|
||||
const projectSettings = this.settingsManager.getProjectSettings();
|
||||
const configuredPackages: ConfiguredPackage[] = [];
|
||||
|
||||
for (const pkg of globalSettings.packages ?? []) {
|
||||
const source = typeof pkg === "string" ? pkg : pkg.source;
|
||||
configuredPackages.push({
|
||||
source,
|
||||
scope: "user",
|
||||
filtered: typeof pkg === "object",
|
||||
installedPath: this.getInstalledPath(source, "user"),
|
||||
});
|
||||
}
|
||||
|
||||
for (const pkg of projectSettings.packages ?? []) {
|
||||
const source = typeof pkg === "string" ? pkg : pkg.source;
|
||||
configuredPackages.push({
|
||||
source,
|
||||
scope: "project",
|
||||
filtered: typeof pkg === "object",
|
||||
installedPath: this.getInstalledPath(source, "project"),
|
||||
});
|
||||
}
|
||||
|
||||
return configuredPackages;
|
||||
}
|
||||
|
||||
async install(source: string, options?: { local?: boolean }): Promise<void> {
|
||||
const parsed = this.parseSource(source);
|
||||
const scope: SourceScope = options?.local ? "project" : "user";
|
||||
@@ -860,6 +898,11 @@ export class DefaultPackageManager implements PackageManager {
|
||||
});
|
||||
}
|
||||
|
||||
async installAndPersist(source: string, options?: { local?: boolean }): Promise<void> {
|
||||
await this.install(source, options);
|
||||
this.addSourceToSettings(source, options);
|
||||
}
|
||||
|
||||
async remove(source: string, options?: { local?: boolean }): Promise<void> {
|
||||
const parsed = this.parseSource(source);
|
||||
const scope: SourceScope = options?.local ? "project" : "user";
|
||||
@@ -879,6 +922,11 @@ export class DefaultPackageManager implements PackageManager {
|
||||
});
|
||||
}
|
||||
|
||||
async removeAndPersist(source: string, options?: { local?: boolean }): Promise<boolean> {
|
||||
await this.remove(source, options);
|
||||
return this.removeSourceFromSettings(source, options);
|
||||
}
|
||||
|
||||
async update(source?: string): Promise<void> {
|
||||
const globalSettings = this.settingsManager.getGlobalSettings();
|
||||
const projectSettings = this.settingsManager.getProjectSettings();
|
||||
|
||||
@@ -315,6 +315,7 @@ export class DefaultResourceLoader implements ResourceLoader {
|
||||
}
|
||||
|
||||
async reload(): Promise<void> {
|
||||
await this.settingsManager.reload();
|
||||
const resolvedPaths = await this.packageManager.resolve();
|
||||
const cliExtensionPaths = await this.packageManager.resolveExtensionSources(this.additionalExtensionPaths, {
|
||||
temporary: true,
|
||||
@@ -402,6 +403,11 @@ export class DefaultResourceLoader implements ResourceLoader {
|
||||
extensionsResult.errors.push({ path: conflict.path, error: conflict.message });
|
||||
}
|
||||
|
||||
for (const p of this.additionalExtensionPaths) {
|
||||
if (!existsSync(p)) {
|
||||
extensionsResult.errors.push({ path: p, error: `Extension path does not exist: ${p}` });
|
||||
}
|
||||
}
|
||||
this.extensionsResult = this.extensionsOverride ? this.extensionsOverride(extensionsResult) : extensionsResult;
|
||||
this.applyExtensionSourceInfo(this.extensionsResult.extensions, metadataByPath);
|
||||
|
||||
@@ -411,6 +417,11 @@ export class DefaultResourceLoader implements ResourceLoader {
|
||||
|
||||
this.lastSkillPaths = skillPaths;
|
||||
this.updateSkillsFromPaths(skillPaths, metadataByPath);
|
||||
for (const p of this.additionalSkillPaths) {
|
||||
if (!existsSync(p) && !this.skillDiagnostics.some((d) => d.path === p)) {
|
||||
this.skillDiagnostics.push({ type: "error", message: "Skill path does not exist", path: p });
|
||||
}
|
||||
}
|
||||
|
||||
const promptPaths = this.noPromptTemplates
|
||||
? this.mergePaths(cliEnabledPrompts, this.additionalPromptTemplatePaths)
|
||||
@@ -418,6 +429,11 @@ export class DefaultResourceLoader implements ResourceLoader {
|
||||
|
||||
this.lastPromptPaths = promptPaths;
|
||||
this.updatePromptsFromPaths(promptPaths, metadataByPath);
|
||||
for (const p of this.additionalPromptTemplatePaths) {
|
||||
if (!existsSync(p) && !this.promptDiagnostics.some((d) => d.path === p)) {
|
||||
this.promptDiagnostics.push({ type: "error", message: "Prompt template path does not exist", path: p });
|
||||
}
|
||||
}
|
||||
|
||||
const themePaths = this.noThemes
|
||||
? this.mergePaths(cliEnabledThemes, this.additionalThemePaths)
|
||||
@@ -425,6 +441,11 @@ export class DefaultResourceLoader implements ResourceLoader {
|
||||
|
||||
this.lastThemePaths = themePaths;
|
||||
this.updateThemesFromPaths(themePaths, metadataByPath);
|
||||
for (const p of this.additionalThemePaths) {
|
||||
if (!existsSync(p) && !this.themeDiagnostics.some((d) => d.path === p)) {
|
||||
this.themeDiagnostics.push({ type: "error", message: "Theme path does not exist", path: p });
|
||||
}
|
||||
}
|
||||
|
||||
const agentsFiles = { agentsFiles: loadProjectContextFiles({ cwd: this.cwd, agentDir: this.agentDir }) };
|
||||
const resolvedAgentsFiles = this.agentsFilesOverride ? this.agentsFilesOverride(agentsFiles) : agentsFiles;
|
||||
|
||||
@@ -86,12 +86,7 @@ export interface CreateAgentSessionResult {
|
||||
|
||||
// Re-exports
|
||||
|
||||
export {
|
||||
type AgentSessionRuntimeBootstrap,
|
||||
AgentSessionRuntimeHost,
|
||||
type CreateAgentSessionRuntimeOptions,
|
||||
createAgentSessionRuntime,
|
||||
} from "./agent-session-runtime.js";
|
||||
export * from "./agent-session-runtime.js";
|
||||
export type {
|
||||
ExtensionAPI,
|
||||
ExtensionCommandContext,
|
||||
|
||||
@@ -359,7 +359,8 @@ export class SettingsManager {
|
||||
return structuredClone(this.projectSettings);
|
||||
}
|
||||
|
||||
reload(): void {
|
||||
async reload(): Promise<void> {
|
||||
await this.writeQueue;
|
||||
const globalLoad = SettingsManager.tryLoadFromStorage(this.storage, "global");
|
||||
if (!globalLoad.error) {
|
||||
this.globalSettings = globalLoad.settings;
|
||||
|
||||
@@ -156,14 +156,20 @@ export type { ResourceCollision, ResourceDiagnostic, ResourceLoader } from "./co
|
||||
export { DefaultResourceLoader } from "./core/resource-loader.js";
|
||||
// SDK for programmatic usage
|
||||
export {
|
||||
type AgentSessionRuntimeBootstrap,
|
||||
AgentSessionRuntimeHost,
|
||||
AgentSessionRuntime,
|
||||
type AgentSessionRuntimeDiagnostic,
|
||||
type AgentSessionServices,
|
||||
type CreateAgentSessionFromServicesOptions,
|
||||
type CreateAgentSessionOptions,
|
||||
type CreateAgentSessionResult,
|
||||
type CreateAgentSessionRuntimeOptions,
|
||||
type CreateAgentSessionRuntimeFactory,
|
||||
type CreateAgentSessionRuntimeResult,
|
||||
type CreateAgentSessionServicesOptions,
|
||||
// Factory
|
||||
createAgentSession,
|
||||
createAgentSessionFromServices,
|
||||
createAgentSessionRuntime,
|
||||
createAgentSessionServices,
|
||||
createBashTool,
|
||||
// Tool factories (for custom cwd)
|
||||
createCodingTools,
|
||||
|
||||
247
packages/coding-agent/src/main-package-command.ts
Normal file
247
packages/coding-agent/src/main-package-command.ts
Normal file
@@ -0,0 +1,247 @@
|
||||
import chalk from "chalk";
|
||||
import { APP_NAME, getAgentDir } from "./config.js";
|
||||
import { DefaultPackageManager } from "./core/package-manager.js";
|
||||
import { SettingsManager } from "./core/settings-manager.js";
|
||||
|
||||
export type PackageCommand = "install" | "remove" | "update" | "list";
|
||||
|
||||
interface PackageCommandOptions {
|
||||
command: PackageCommand;
|
||||
source?: string;
|
||||
local: boolean;
|
||||
help: boolean;
|
||||
invalidOption?: string;
|
||||
}
|
||||
|
||||
function reportSettingsErrors(settingsManager: SettingsManager, context: string): void {
|
||||
const errors = settingsManager.drainErrors();
|
||||
for (const { scope, error } of errors) {
|
||||
console.error(chalk.yellow(`Warning (${context}, ${scope} settings): ${error.message}`));
|
||||
if (error.stack) {
|
||||
console.error(chalk.dim(error.stack));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getPackageCommandUsage(command: PackageCommand): string {
|
||||
switch (command) {
|
||||
case "install":
|
||||
return `${APP_NAME} install <source> [-l]`;
|
||||
case "remove":
|
||||
return `${APP_NAME} remove <source> [-l]`;
|
||||
case "update":
|
||||
return `${APP_NAME} update [source]`;
|
||||
case "list":
|
||||
return `${APP_NAME} list`;
|
||||
}
|
||||
}
|
||||
|
||||
function printPackageCommandHelp(command: PackageCommand): void {
|
||||
switch (command) {
|
||||
case "install":
|
||||
console.log(`${chalk.bold("Usage:")}
|
||||
${getPackageCommandUsage("install")}
|
||||
|
||||
Install a package and add it to settings.
|
||||
|
||||
Options:
|
||||
-l, --local Install project-locally (.pi/settings.json)
|
||||
|
||||
Examples:
|
||||
${APP_NAME} install npm:@foo/bar
|
||||
${APP_NAME} install git:github.com/user/repo
|
||||
${APP_NAME} install git:git@github.com:user/repo
|
||||
${APP_NAME} install https://github.com/user/repo
|
||||
${APP_NAME} install ssh://git@github.com/user/repo
|
||||
${APP_NAME} install ./local/path
|
||||
`);
|
||||
return;
|
||||
|
||||
case "remove":
|
||||
console.log(`${chalk.bold("Usage:")}
|
||||
${getPackageCommandUsage("remove")}
|
||||
|
||||
Remove a package and its source from settings.
|
||||
Alias: ${APP_NAME} uninstall <source> [-l]
|
||||
|
||||
Options:
|
||||
-l, --local Remove from project settings (.pi/settings.json)
|
||||
|
||||
Examples:
|
||||
${APP_NAME} remove npm:@foo/bar
|
||||
${APP_NAME} uninstall npm:@foo/bar
|
||||
`);
|
||||
return;
|
||||
|
||||
case "update":
|
||||
console.log(`${chalk.bold("Usage:")}
|
||||
${getPackageCommandUsage("update")}
|
||||
|
||||
Update installed packages.
|
||||
If <source> is provided, only that package is updated.
|
||||
`);
|
||||
return;
|
||||
|
||||
case "list":
|
||||
console.log(`${chalk.bold("Usage:")}
|
||||
${getPackageCommandUsage("list")}
|
||||
|
||||
List installed packages from user and project settings.
|
||||
`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
function parsePackageCommand(args: string[]): PackageCommandOptions | undefined {
|
||||
const [rawCommand, ...rest] = args;
|
||||
let command: PackageCommand | undefined;
|
||||
if (rawCommand === "uninstall") {
|
||||
command = "remove";
|
||||
} else if (rawCommand === "install" || rawCommand === "remove" || rawCommand === "update" || rawCommand === "list") {
|
||||
command = rawCommand;
|
||||
}
|
||||
if (!command) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let local = false;
|
||||
let help = false;
|
||||
let invalidOption: string | undefined;
|
||||
let source: string | undefined;
|
||||
|
||||
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 };
|
||||
}
|
||||
|
||||
export async function handlePackageCommand(args: string[]): Promise<boolean> {
|
||||
const options = parsePackageCommand(args);
|
||||
if (!options) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (options.help) {
|
||||
printPackageCommandHelp(options.command);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (options.invalidOption) {
|
||||
console.error(chalk.red(`Unknown option ${options.invalidOption} for "${options.command}".`));
|
||||
console.error(chalk.dim(`Use "${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.red(`Missing ${options.command} source.`));
|
||||
console.error(chalk.dim(`Usage: ${getPackageCommandUsage(options.command)}`));
|
||||
process.exitCode = 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
const cwd = process.cwd();
|
||||
const agentDir = getAgentDir();
|
||||
const settingsManager = SettingsManager.create(cwd, agentDir);
|
||||
reportSettingsErrors(settingsManager, "package command");
|
||||
const packageManager = new DefaultPackageManager({ cwd, agentDir, settingsManager });
|
||||
|
||||
packageManager.setProgressCallback((event) => {
|
||||
if (event.type === "start") {
|
||||
process.stdout.write(chalk.dim(`${event.message}\n`));
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
switch (options.command) {
|
||||
case "install":
|
||||
await packageManager.installAndPersist(source!, { local: options.local });
|
||||
console.log(chalk.green(`Installed ${source}`));
|
||||
return true;
|
||||
|
||||
case "remove": {
|
||||
const removed = await packageManager.removeAndPersist(source!, { local: options.local });
|
||||
if (!removed) {
|
||||
console.error(chalk.red(`No matching package found for ${source}`));
|
||||
process.exitCode = 1;
|
||||
return true;
|
||||
}
|
||||
console.log(chalk.green(`Removed ${source}`));
|
||||
return true;
|
||||
}
|
||||
|
||||
case "list": {
|
||||
const configuredPackages = packageManager.listConfiguredPackages();
|
||||
const userPackages = configuredPackages.filter((pkg) => pkg.scope === "user");
|
||||
const projectPackages = configuredPackages.filter((pkg) => pkg.scope === "project");
|
||||
|
||||
if (configuredPackages.length === 0) {
|
||||
console.log(chalk.dim("No packages installed."));
|
||||
return true;
|
||||
}
|
||||
|
||||
const formatPackage = (pkg: (typeof configuredPackages)[number]) => {
|
||||
const display = pkg.filtered ? `${pkg.source} (filtered)` : pkg.source;
|
||||
console.log(` ${display}`);
|
||||
if (pkg.installedPath) {
|
||||
console.log(chalk.dim(` ${pkg.installedPath}`));
|
||||
}
|
||||
};
|
||||
|
||||
if (userPackages.length > 0) {
|
||||
console.log(chalk.bold("User packages:"));
|
||||
for (const pkg of userPackages) {
|
||||
formatPackage(pkg);
|
||||
}
|
||||
}
|
||||
|
||||
if (projectPackages.length > 0) {
|
||||
if (userPackages.length > 0) console.log();
|
||||
console.log(chalk.bold("Project packages:"));
|
||||
for (const pkg of projectPackages) {
|
||||
formatPackage(pkg);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
case "update":
|
||||
await packageManager.update(source);
|
||||
if (source) {
|
||||
console.log(chalk.green(`Updated ${source}`));
|
||||
} else {
|
||||
console.log(chalk.green("Updated packages"));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
const message = error instanceof Error ? error.message : "Unknown package command error";
|
||||
console.error(chalk.red(`Error: ${message}`));
|
||||
process.exitCode = 1;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -9,27 +9,23 @@ import { resolve } from "node:path";
|
||||
import { type ImageContent, modelsAreEqual, supportsXhigh } from "@mariozechner/pi-ai";
|
||||
import chalk from "chalk";
|
||||
import { createInterface } from "readline";
|
||||
import { type Args, parseArgs, printHelp } from "./cli/args.js";
|
||||
import { selectConfig } from "./cli/config-selector.js";
|
||||
import { type Args, type Mode, parseArgs, printHelp } from "./cli/args.js";
|
||||
import { processFileArguments } from "./cli/file-processor.js";
|
||||
import { buildInitialMessage } from "./cli/initial-message.js";
|
||||
import { listModels } from "./cli/list-models.js";
|
||||
import { selectSession } from "./cli/session-picker.js";
|
||||
import { APP_NAME, getAgentDir, getModelsPath, VERSION } from "./config.js";
|
||||
import { getAgentDir, getModelsPath, VERSION } from "./config.js";
|
||||
import { type CreateAgentSessionRuntimeFactory, createAgentSessionRuntime } from "./core/agent-session-runtime.js";
|
||||
import {
|
||||
type AgentSessionRuntimeBootstrap,
|
||||
AgentSessionRuntimeHost,
|
||||
createAgentSessionRuntime,
|
||||
} from "./core/agent-session-runtime.js";
|
||||
type AgentSessionRuntimeDiagnostic,
|
||||
createAgentSessionFromServices,
|
||||
createAgentSessionServices,
|
||||
} from "./core/agent-session-services.js";
|
||||
import { AuthStorage } from "./core/auth-storage.js";
|
||||
import { exportFromFile } from "./core/export-html/index.js";
|
||||
import type { LoadExtensionsResult } from "./core/extensions/index.js";
|
||||
import { migrateKeybindingsConfigFile } from "./core/keybindings.js";
|
||||
import { ModelRegistry } from "./core/model-registry.js";
|
||||
import type { ModelRegistry } from "./core/model-registry.js";
|
||||
import { resolveCliModel, resolveModelScope, type ScopedModel } from "./core/model-resolver.js";
|
||||
import { restoreStdout, takeOverStdout } from "./core/output-guard.js";
|
||||
import { DefaultPackageManager } from "./core/package-manager.js";
|
||||
import { DefaultResourceLoader } from "./core/resource-loader.js";
|
||||
import type { CreateAgentSessionOptions } from "./core/sdk.js";
|
||||
import { SessionManager } from "./core/session-manager.js";
|
||||
import { SettingsManager } from "./core/settings-manager.js";
|
||||
@@ -38,6 +34,7 @@ import { allTools } from "./core/tools/index.js";
|
||||
import { runMigrations, showDeprecationWarnings } from "./migrations.js";
|
||||
import { InteractiveMode, runPrintMode, runRpcMode } from "./modes/index.js";
|
||||
import { initTheme, stopThemeWatcher } from "./modes/interactive/theme/theme.js";
|
||||
import { handleConfigCommand, handlePackageCommand } from "./package-manager-cli.js";
|
||||
|
||||
/**
|
||||
* Read all content from piped stdin.
|
||||
@@ -62,13 +59,21 @@ async function readPipedStdin(): Promise<string | undefined> {
|
||||
});
|
||||
}
|
||||
|
||||
function reportSettingsErrors(settingsManager: SettingsManager, context: string): void {
|
||||
const errors = settingsManager.drainErrors();
|
||||
for (const { scope, error } of errors) {
|
||||
console.error(chalk.yellow(`Warning (${context}, ${scope} settings): ${error.message}`));
|
||||
if (error.stack) {
|
||||
console.error(chalk.dim(error.stack));
|
||||
}
|
||||
function collectSettingsDiagnostics(
|
||||
settingsManager: SettingsManager,
|
||||
context: string,
|
||||
): AgentSessionRuntimeDiagnostic[] {
|
||||
return settingsManager.drainErrors().map(({ scope, error }) => ({
|
||||
type: "warning",
|
||||
message: `(${context}, ${scope} settings) ${error.message}`,
|
||||
}));
|
||||
}
|
||||
|
||||
function reportDiagnostics(diagnostics: readonly AgentSessionRuntimeDiagnostic[]): void {
|
||||
for (const diagnostic of diagnostics) {
|
||||
const color = diagnostic.type === "error" ? chalk.red : diagnostic.type === "warning" ? chalk.yellow : chalk.dim;
|
||||
const prefix = diagnostic.type === "error" ? "Error: " : diagnostic.type === "warning" ? "Warning: " : "";
|
||||
console.error(color(`${prefix}${diagnostic.message}`));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,243 +82,23 @@ function isTruthyEnvFlag(value: string | undefined): boolean {
|
||||
return value === "1" || value.toLowerCase() === "true" || value.toLowerCase() === "yes";
|
||||
}
|
||||
|
||||
type PackageCommand = "install" | "remove" | "update" | "list";
|
||||
type AppMode = "interactive" | "print" | "json" | "rpc";
|
||||
|
||||
interface PackageCommandOptions {
|
||||
command: PackageCommand;
|
||||
source?: string;
|
||||
local: boolean;
|
||||
help: boolean;
|
||||
invalidOption?: string;
|
||||
function resolveAppMode(parsed: Args, stdinIsTTY: boolean): AppMode {
|
||||
if (parsed.mode === "rpc") {
|
||||
return "rpc";
|
||||
}
|
||||
if (parsed.mode === "json") {
|
||||
return "json";
|
||||
}
|
||||
if (parsed.print || !stdinIsTTY) {
|
||||
return "print";
|
||||
}
|
||||
return "interactive";
|
||||
}
|
||||
|
||||
function getPackageCommandUsage(command: PackageCommand): string {
|
||||
switch (command) {
|
||||
case "install":
|
||||
return `${APP_NAME} install <source> [-l]`;
|
||||
case "remove":
|
||||
return `${APP_NAME} remove <source> [-l]`;
|
||||
case "update":
|
||||
return `${APP_NAME} update [source]`;
|
||||
case "list":
|
||||
return `${APP_NAME} list`;
|
||||
}
|
||||
}
|
||||
|
||||
function printPackageCommandHelp(command: PackageCommand): void {
|
||||
switch (command) {
|
||||
case "install":
|
||||
console.log(`${chalk.bold("Usage:")}
|
||||
${getPackageCommandUsage("install")}
|
||||
|
||||
Install a package and add it to settings.
|
||||
|
||||
Options:
|
||||
-l, --local Install project-locally (.pi/settings.json)
|
||||
|
||||
Examples:
|
||||
${APP_NAME} install npm:@foo/bar
|
||||
${APP_NAME} install git:github.com/user/repo
|
||||
${APP_NAME} install git:git@github.com:user/repo
|
||||
${APP_NAME} install https://github.com/user/repo
|
||||
${APP_NAME} install ssh://git@github.com/user/repo
|
||||
${APP_NAME} install ./local/path
|
||||
`);
|
||||
return;
|
||||
|
||||
case "remove":
|
||||
console.log(`${chalk.bold("Usage:")}
|
||||
${getPackageCommandUsage("remove")}
|
||||
|
||||
Remove a package and its source from settings.
|
||||
Alias: ${APP_NAME} uninstall <source> [-l]
|
||||
|
||||
Options:
|
||||
-l, --local Remove from project settings (.pi/settings.json)
|
||||
|
||||
Examples:
|
||||
${APP_NAME} remove npm:@foo/bar
|
||||
${APP_NAME} uninstall npm:@foo/bar
|
||||
`);
|
||||
return;
|
||||
|
||||
case "update":
|
||||
console.log(`${chalk.bold("Usage:")}
|
||||
${getPackageCommandUsage("update")}
|
||||
|
||||
Update installed packages.
|
||||
If <source> is provided, only that package is updated.
|
||||
`);
|
||||
return;
|
||||
|
||||
case "list":
|
||||
console.log(`${chalk.bold("Usage:")}
|
||||
${getPackageCommandUsage("list")}
|
||||
|
||||
List installed packages from user and project settings.
|
||||
`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
function parsePackageCommand(args: string[]): PackageCommandOptions | undefined {
|
||||
const [rawCommand, ...rest] = args;
|
||||
let command: PackageCommand | undefined;
|
||||
if (rawCommand === "uninstall") {
|
||||
command = "remove";
|
||||
} else if (rawCommand === "install" || rawCommand === "remove" || rawCommand === "update" || rawCommand === "list") {
|
||||
command = rawCommand;
|
||||
}
|
||||
if (!command) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let local = false;
|
||||
let help = false;
|
||||
let invalidOption: string | undefined;
|
||||
let source: string | undefined;
|
||||
|
||||
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: string[]): Promise<boolean> {
|
||||
const options = parsePackageCommand(args);
|
||||
if (!options) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (options.help) {
|
||||
printPackageCommandHelp(options.command);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (options.invalidOption) {
|
||||
console.error(chalk.red(`Unknown option ${options.invalidOption} for "${options.command}".`));
|
||||
console.error(chalk.dim(`Use "${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.red(`Missing ${options.command} source.`));
|
||||
console.error(chalk.dim(`Usage: ${getPackageCommandUsage(options.command)}`));
|
||||
process.exitCode = 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
const cwd = process.cwd();
|
||||
const agentDir = getAgentDir();
|
||||
const settingsManager = SettingsManager.create(cwd, agentDir);
|
||||
reportSettingsErrors(settingsManager, "package command");
|
||||
const packageManager = new DefaultPackageManager({ cwd, agentDir, settingsManager });
|
||||
|
||||
packageManager.setProgressCallback((event) => {
|
||||
if (event.type === "start") {
|
||||
process.stdout.write(chalk.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.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.red(`No matching package found for ${source}`));
|
||||
process.exitCode = 1;
|
||||
return true;
|
||||
}
|
||||
console.log(chalk.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.dim("No packages installed."));
|
||||
return true;
|
||||
}
|
||||
|
||||
const formatPackage = (pkg: (typeof globalPackages)[number], scope: "user" | "project") => {
|
||||
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.dim(` ${path}`));
|
||||
}
|
||||
};
|
||||
|
||||
if (globalPackages.length > 0) {
|
||||
console.log(chalk.bold("User packages:"));
|
||||
for (const pkg of globalPackages) {
|
||||
formatPackage(pkg, "user");
|
||||
}
|
||||
}
|
||||
|
||||
if (projectPackages.length > 0) {
|
||||
if (globalPackages.length > 0) console.log();
|
||||
console.log(chalk.bold("Project packages:"));
|
||||
for (const pkg of projectPackages) {
|
||||
formatPackage(pkg, "project");
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
case "update":
|
||||
await packageManager.update(source);
|
||||
if (source) {
|
||||
console.log(chalk.green(`Updated ${source}`));
|
||||
} else {
|
||||
console.log(chalk.green("Updated packages"));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
const message = error instanceof Error ? error.message : "Unknown package command error";
|
||||
console.error(chalk.red(`Error: ${message}`));
|
||||
process.exitCode = 1;
|
||||
return true;
|
||||
}
|
||||
function toPrintOutputMode(appMode: AppMode): Exclude<Mode, "rpc"> {
|
||||
return appMode === "json" ? "json" : "text";
|
||||
}
|
||||
|
||||
async function prepareInitialMessage(
|
||||
@@ -389,32 +174,6 @@ async function promptConfirm(message: string): Promise<boolean> {
|
||||
});
|
||||
}
|
||||
|
||||
/** Helper to call CLI-only session_directory handlers before the initial session manager is created */
|
||||
async function callSessionDirectoryHook(extensions: LoadExtensionsResult, cwd: string): Promise<string | undefined> {
|
||||
let customSessionDir: string | undefined;
|
||||
|
||||
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" as const, cwd };
|
||||
const result = (await handler(event)) as { sessionDir?: string } | undefined;
|
||||
|
||||
if (result?.sessionDir) {
|
||||
customSessionDir = result.sessionDir;
|
||||
}
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
console.error(chalk.red(`Extension "${ext.path}" session_directory handler failed: ${message}`));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return customSessionDir;
|
||||
}
|
||||
|
||||
function validateForkFlags(parsed: Args): void {
|
||||
if (!parsed.fork) return;
|
||||
|
||||
@@ -444,25 +203,21 @@ function forkSessionOrExit(sourcePath: string, cwd: string, sessionDir?: string)
|
||||
async function createSessionManager(
|
||||
parsed: Args,
|
||||
cwd: string,
|
||||
extensions: LoadExtensionsResult,
|
||||
sessionDir: string | undefined,
|
||||
settingsManager: SettingsManager,
|
||||
): Promise<SessionManager | undefined> {
|
||||
): Promise<SessionManager> {
|
||||
if (parsed.noSession) {
|
||||
return SessionManager.inMemory();
|
||||
}
|
||||
|
||||
// Priority: CLI flag > settings.json > extension hook
|
||||
const effectiveSessionDir =
|
||||
parsed.sessionDir ?? settingsManager.getSessionDir() ?? (await callSessionDirectoryHook(extensions, cwd));
|
||||
|
||||
if (parsed.fork) {
|
||||
const resolved = await resolveSessionPath(parsed.fork, cwd, effectiveSessionDir);
|
||||
const resolved = await resolveSessionPath(parsed.fork, cwd, sessionDir);
|
||||
|
||||
switch (resolved.type) {
|
||||
case "path":
|
||||
case "local":
|
||||
case "global":
|
||||
return forkSessionOrExit(resolved.path, cwd, effectiveSessionDir);
|
||||
return forkSessionOrExit(resolved.path, cwd, sessionDir);
|
||||
|
||||
case "not_found":
|
||||
console.error(chalk.red(`No session found matching '${resolved.arg}'`));
|
||||
@@ -471,22 +226,21 @@ async function createSessionManager(
|
||||
}
|
||||
|
||||
if (parsed.session) {
|
||||
const resolved = await resolveSessionPath(parsed.session, cwd, effectiveSessionDir);
|
||||
const resolved = await resolveSessionPath(parsed.session, cwd, sessionDir);
|
||||
|
||||
switch (resolved.type) {
|
||||
case "path":
|
||||
case "local":
|
||||
return SessionManager.open(resolved.path, effectiveSessionDir);
|
||||
return SessionManager.open(resolved.path, sessionDir);
|
||||
|
||||
case "global": {
|
||||
// Session found in different project - ask user if they want to fork
|
||||
console.log(chalk.yellow(`Session found in different project: ${resolved.cwd}`));
|
||||
const shouldFork = await promptConfirm("Fork this session into current directory?");
|
||||
if (!shouldFork) {
|
||||
console.log(chalk.dim("Aborted."));
|
||||
process.exit(0);
|
||||
}
|
||||
return forkSessionOrExit(resolved.path, cwd, effectiveSessionDir);
|
||||
return forkSessionOrExit(resolved.path, cwd, sessionDir);
|
||||
}
|
||||
|
||||
case "not_found":
|
||||
@@ -494,32 +248,46 @@ async function createSessionManager(
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (parsed.resume) {
|
||||
initTheme(settingsManager.getTheme(), true);
|
||||
try {
|
||||
const selectedPath = await selectSession(
|
||||
(onProgress) => SessionManager.list(cwd, sessionDir, onProgress),
|
||||
SessionManager.listAll,
|
||||
);
|
||||
if (!selectedPath) {
|
||||
console.log(chalk.dim("No session selected"));
|
||||
process.exit(0);
|
||||
}
|
||||
return SessionManager.open(selectedPath, sessionDir);
|
||||
} finally {
|
||||
stopThemeWatcher();
|
||||
}
|
||||
}
|
||||
|
||||
if (parsed.continue) {
|
||||
return SessionManager.continueRecent(cwd, effectiveSessionDir);
|
||||
return SessionManager.continueRecent(cwd, sessionDir);
|
||||
}
|
||||
// --resume is handled separately (needs picker UI)
|
||||
// If effective session dir is set, create new session there
|
||||
if (effectiveSessionDir) {
|
||||
return SessionManager.create(cwd, effectiveSessionDir);
|
||||
}
|
||||
// Default case (new session) returns undefined, SDK will create one
|
||||
return undefined;
|
||||
|
||||
return SessionManager.create(cwd, sessionDir);
|
||||
}
|
||||
|
||||
function buildSessionOptions(
|
||||
parsed: Args,
|
||||
scopedModels: ScopedModel[],
|
||||
sessionManager: SessionManager | undefined,
|
||||
hasExistingSession: boolean,
|
||||
modelRegistry: ModelRegistry,
|
||||
settingsManager: SettingsManager,
|
||||
): { options: CreateAgentSessionOptions; cliThinkingFromModel: boolean } {
|
||||
): {
|
||||
options: CreateAgentSessionOptions;
|
||||
cliThinkingFromModel: boolean;
|
||||
diagnostics: AgentSessionRuntimeDiagnostic[];
|
||||
} {
|
||||
const options: CreateAgentSessionOptions = {};
|
||||
const diagnostics: AgentSessionRuntimeDiagnostic[] = [];
|
||||
let cliThinkingFromModel = false;
|
||||
|
||||
if (sessionManager) {
|
||||
options.sessionManager = sessionManager;
|
||||
}
|
||||
|
||||
// Model from CLI
|
||||
// - supports --provider <name> --model <pattern>
|
||||
// - supports --model <provider>/<pattern>
|
||||
@@ -530,11 +298,10 @@ function buildSessionOptions(
|
||||
modelRegistry,
|
||||
});
|
||||
if (resolved.warning) {
|
||||
console.warn(chalk.yellow(`Warning: ${resolved.warning}`));
|
||||
diagnostics.push({ type: "warning", message: resolved.warning });
|
||||
}
|
||||
if (resolved.error) {
|
||||
console.error(chalk.red(resolved.error));
|
||||
process.exit(1);
|
||||
diagnostics.push({ type: "error", message: resolved.error });
|
||||
}
|
||||
if (resolved.model) {
|
||||
options.model = resolved.model;
|
||||
@@ -547,7 +314,7 @@ function buildSessionOptions(
|
||||
}
|
||||
}
|
||||
|
||||
if (!options.model && scopedModels.length > 0 && !parsed.continue && !parsed.resume) {
|
||||
if (!options.model && scopedModels.length > 0 && !hasExistingSession) {
|
||||
// Check if saved default is in scoped models - use it if so, otherwise first scoped model
|
||||
const savedProvider = settingsManager.getDefaultProvider();
|
||||
const savedModelId = settingsManager.getDefaultModel();
|
||||
@@ -600,66 +367,13 @@ function buildSessionOptions(
|
||||
options.tools = parsed.tools.map((name) => allTools[name]);
|
||||
}
|
||||
|
||||
return { options, cliThinkingFromModel };
|
||||
return { options, cliThinkingFromModel, diagnostics };
|
||||
}
|
||||
|
||||
function resolveCliPaths(cwd: string, paths: string[] | undefined): string[] | undefined {
|
||||
return paths?.map((value) => resolve(cwd, value));
|
||||
}
|
||||
|
||||
function buildRuntimeBootstrap(
|
||||
parsed: Args,
|
||||
cwd: string,
|
||||
agentDir: string,
|
||||
authStorage: AuthStorage,
|
||||
sessionOptions: CreateAgentSessionOptions,
|
||||
): AgentSessionRuntimeBootstrap {
|
||||
return {
|
||||
agentDir,
|
||||
authStorage,
|
||||
model: sessionOptions.model,
|
||||
thinkingLevel: sessionOptions.thinkingLevel,
|
||||
scopedModels: sessionOptions.scopedModels,
|
||||
tools: sessionOptions.tools,
|
||||
customTools: sessionOptions.customTools,
|
||||
resourceLoader: {
|
||||
additionalExtensionPaths: resolveCliPaths(cwd, parsed.extensions),
|
||||
additionalSkillPaths: resolveCliPaths(cwd, parsed.skills),
|
||||
additionalPromptTemplatePaths: resolveCliPaths(cwd, parsed.promptTemplates),
|
||||
additionalThemePaths: resolveCliPaths(cwd, parsed.themes),
|
||||
noExtensions: parsed.noExtensions,
|
||||
noSkills: parsed.noSkills,
|
||||
noPromptTemplates: parsed.noPromptTemplates,
|
||||
noThemes: parsed.noThemes,
|
||||
systemPrompt: parsed.systemPrompt,
|
||||
appendSystemPrompt: parsed.appendSystemPrompt,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function handleConfigCommand(args: string[]): Promise<boolean> {
|
||||
if (args[0] !== "config") {
|
||||
return false;
|
||||
}
|
||||
|
||||
const cwd = process.cwd();
|
||||
const agentDir = getAgentDir();
|
||||
const settingsManager = SettingsManager.create(cwd, agentDir);
|
||||
reportSettingsErrors(settingsManager, "config command");
|
||||
const packageManager = new DefaultPackageManager({ cwd, agentDir, settingsManager });
|
||||
|
||||
const resolvedPaths = await packageManager.resolve();
|
||||
|
||||
await selectConfig({
|
||||
resolvedPaths,
|
||||
settingsManager,
|
||||
cwd,
|
||||
agentDir,
|
||||
});
|
||||
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
export async function main(args: string[]) {
|
||||
resetTimings();
|
||||
const offlineMode = args.includes("--offline") || isTruthyEnvFlag(process.env.PI_OFFLINE);
|
||||
@@ -676,105 +390,28 @@ export async function main(args: string[]) {
|
||||
return;
|
||||
}
|
||||
|
||||
// First pass: parse args to get --extension paths
|
||||
const firstPass = parseArgs(args);
|
||||
time("parseArgs.firstPass");
|
||||
const shouldTakeOverStdout = firstPass.mode !== undefined || firstPass.print || !process.stdin.isTTY;
|
||||
const parsed = parseArgs(args);
|
||||
if (parsed.diagnostics.length > 0) {
|
||||
for (const d of parsed.diagnostics) {
|
||||
const color = d.type === "error" ? chalk.red : chalk.yellow;
|
||||
console.error(color(`${d.type === "error" ? "Error" : "Warning"}: ${d.message}`));
|
||||
}
|
||||
if (parsed.diagnostics.some((d) => d.type === "error")) {
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
time("parseArgs");
|
||||
let appMode = resolveAppMode(parsed, process.stdin.isTTY);
|
||||
const shouldTakeOverStdout = appMode !== "interactive";
|
||||
if (shouldTakeOverStdout) {
|
||||
takeOverStdout();
|
||||
}
|
||||
|
||||
// Run migrations (pass cwd for project-local migrations)
|
||||
const { migratedAuthProviders: migratedProviders, deprecationWarnings } = runMigrations(process.cwd());
|
||||
time("runMigrations");
|
||||
|
||||
// Early load extensions to discover their CLI flags
|
||||
const cwd = process.cwd();
|
||||
const agentDir = getAgentDir();
|
||||
const settingsManager = SettingsManager.create(cwd, agentDir);
|
||||
reportSettingsErrors(settingsManager, "startup");
|
||||
const authStorage = AuthStorage.create();
|
||||
const modelRegistry = ModelRegistry.create(authStorage, getModelsPath());
|
||||
|
||||
const resourceLoader = new 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,
|
||||
});
|
||||
time("createResourceLoader");
|
||||
await resourceLoader.reload();
|
||||
time("resourceLoader.reload");
|
||||
|
||||
const extensionsResult: LoadExtensionsResult = resourceLoader.getExtensions();
|
||||
for (const { path, error } of extensionsResult.errors) {
|
||||
console.error(chalk.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, extensionPath } of extensionsResult.runtime.pendingProviderRegistrations) {
|
||||
try {
|
||||
modelRegistry.registerProvider(name, config);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
console.error(chalk.red(`Extension "${extensionPath}" error: ${message}`));
|
||||
}
|
||||
}
|
||||
extensionsResult.runtime.pendingProviderRegistrations = [];
|
||||
|
||||
const extensionFlags = new Map<string, { type: "boolean" | "string" }>();
|
||||
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 = parseArgs(args, extensionFlags);
|
||||
time("parseArgs.secondPass");
|
||||
|
||||
// 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(VERSION);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (parsed.help) {
|
||||
printHelp();
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (parsed.listModels !== undefined) {
|
||||
const searchPattern = typeof parsed.listModels === "string" ? parsed.listModels : undefined;
|
||||
await listModels(modelRegistry, searchPattern);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Read piped stdin content (if any) - skip for RPC mode which uses stdin for JSON-RPC
|
||||
let stdinContent: string | undefined;
|
||||
if (parsed.mode !== "rpc") {
|
||||
stdinContent = await readPipedStdin();
|
||||
if (stdinContent !== undefined) {
|
||||
// Force print mode since interactive mode requires a TTY for keyboard input
|
||||
parsed.print = true;
|
||||
}
|
||||
}
|
||||
time("readPipedStdin");
|
||||
|
||||
if (parsed.export) {
|
||||
let result: string;
|
||||
try {
|
||||
@@ -789,9 +426,6 @@ export async function main(args: string[]) {
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
migrateKeybindingsConfigFile(agentDir);
|
||||
time("migrateKeybindingsConfigFile");
|
||||
|
||||
if (parsed.mode === "rpc" && parsed.fileArgs.length > 0) {
|
||||
console.error(chalk.red("Error: @file arguments are not supported in RPC mode"));
|
||||
process.exit(1);
|
||||
@@ -799,90 +433,179 @@ export async function main(args: string[]) {
|
||||
|
||||
validateForkFlags(parsed);
|
||||
|
||||
// Run migrations (pass cwd for project-local migrations)
|
||||
const { migratedAuthProviders: migratedProviders, deprecationWarnings } = runMigrations(process.cwd());
|
||||
time("runMigrations");
|
||||
|
||||
const cwd = process.cwd();
|
||||
const agentDir = getAgentDir();
|
||||
const startupSettingsManager = SettingsManager.create(cwd, agentDir);
|
||||
reportDiagnostics(collectSettingsDiagnostics(startupSettingsManager, "startup session lookup"));
|
||||
|
||||
// Decide the final runtime cwd before creating cwd-bound runtime services.
|
||||
// --session and --resume may select a session from another project, so project-local
|
||||
// settings, resources, provider registrations, and models must be resolved only after
|
||||
// the target session cwd is known. The startup-cwd settings manager is used only for
|
||||
// sessionDir lookup during session selection.
|
||||
const sessionManager = await createSessionManager(
|
||||
parsed,
|
||||
cwd,
|
||||
parsed.sessionDir ?? startupSettingsManager.getSessionDir(),
|
||||
startupSettingsManager,
|
||||
);
|
||||
time("createSessionManager");
|
||||
|
||||
const resolvedExtensionPaths = resolveCliPaths(cwd, parsed.extensions);
|
||||
const resolvedSkillPaths = resolveCliPaths(cwd, parsed.skills);
|
||||
const resolvedPromptTemplatePaths = resolveCliPaths(cwd, parsed.promptTemplates);
|
||||
const resolvedThemePaths = resolveCliPaths(cwd, parsed.themes);
|
||||
const authStorage = AuthStorage.create();
|
||||
const createRuntime: CreateAgentSessionRuntimeFactory = async ({
|
||||
cwd,
|
||||
agentDir,
|
||||
sessionManager,
|
||||
sessionStartEvent,
|
||||
}) => {
|
||||
const services = await createAgentSessionServices({
|
||||
cwd,
|
||||
agentDir,
|
||||
authStorage,
|
||||
extensionFlagValues: parsed.unknownFlags,
|
||||
resourceLoaderOptions: {
|
||||
additionalExtensionPaths: resolvedExtensionPaths,
|
||||
additionalSkillPaths: resolvedSkillPaths,
|
||||
additionalPromptTemplatePaths: resolvedPromptTemplatePaths,
|
||||
additionalThemePaths: resolvedThemePaths,
|
||||
noExtensions: parsed.noExtensions,
|
||||
noSkills: parsed.noSkills,
|
||||
noPromptTemplates: parsed.noPromptTemplates,
|
||||
noThemes: parsed.noThemes,
|
||||
systemPrompt: parsed.systemPrompt,
|
||||
appendSystemPrompt: parsed.appendSystemPrompt,
|
||||
},
|
||||
});
|
||||
const { settingsManager, modelRegistry, resourceLoader } = services;
|
||||
const diagnostics: AgentSessionRuntimeDiagnostic[] = [
|
||||
...services.diagnostics,
|
||||
...collectSettingsDiagnostics(settingsManager, "runtime creation"),
|
||||
...resourceLoader.getExtensions().errors.map(({ path, error }) => ({
|
||||
type: "error" as const,
|
||||
message: `Failed to load extension "${path}": ${error}`,
|
||||
})),
|
||||
];
|
||||
|
||||
const modelPatterns = parsed.models ?? settingsManager.getEnabledModels();
|
||||
const scopedModels =
|
||||
modelPatterns && modelPatterns.length > 0 ? await resolveModelScope(modelPatterns, modelRegistry) : [];
|
||||
const {
|
||||
options: sessionOptions,
|
||||
cliThinkingFromModel,
|
||||
diagnostics: sessionOptionDiagnostics,
|
||||
} = buildSessionOptions(
|
||||
parsed,
|
||||
scopedModels,
|
||||
sessionManager.buildSessionContext().messages.length > 0,
|
||||
modelRegistry,
|
||||
settingsManager,
|
||||
);
|
||||
diagnostics.push(...sessionOptionDiagnostics);
|
||||
|
||||
if (parsed.apiKey) {
|
||||
if (!sessionOptions.model) {
|
||||
diagnostics.push({
|
||||
type: "error",
|
||||
message: "--api-key requires a model to be specified via --model, --provider/--model, or --models",
|
||||
});
|
||||
} else {
|
||||
authStorage.setRuntimeApiKey(sessionOptions.model.provider, parsed.apiKey);
|
||||
}
|
||||
}
|
||||
|
||||
const created = await createAgentSessionFromServices({
|
||||
services,
|
||||
sessionManager,
|
||||
sessionStartEvent,
|
||||
model: sessionOptions.model,
|
||||
thinkingLevel: sessionOptions.thinkingLevel,
|
||||
scopedModels: sessionOptions.scopedModels,
|
||||
tools: sessionOptions.tools,
|
||||
customTools: sessionOptions.customTools,
|
||||
});
|
||||
const cliThinkingOverride = parsed.thinking !== undefined || cliThinkingFromModel;
|
||||
if (created.session.model && cliThinkingOverride) {
|
||||
let effectiveThinking = created.session.thinkingLevel;
|
||||
if (!created.session.model.reasoning) {
|
||||
effectiveThinking = "off";
|
||||
} else if (effectiveThinking === "xhigh" && !supportsXhigh(created.session.model)) {
|
||||
effectiveThinking = "high";
|
||||
}
|
||||
if (effectiveThinking !== created.session.thinkingLevel) {
|
||||
created.session.setThinkingLevel(effectiveThinking);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...created,
|
||||
services,
|
||||
diagnostics,
|
||||
};
|
||||
};
|
||||
time("createRuntime");
|
||||
const runtime = await createAgentSessionRuntime(createRuntime, {
|
||||
cwd: sessionManager.getCwd(),
|
||||
agentDir,
|
||||
sessionManager,
|
||||
});
|
||||
const { services, session, modelFallbackMessage } = runtime;
|
||||
const { settingsManager, modelRegistry, resourceLoader } = services;
|
||||
|
||||
if (parsed.help) {
|
||||
const extensionFlags = resourceLoader
|
||||
.getExtensions()
|
||||
.extensions.flatMap((extension) => Array.from(extension.flags.values()));
|
||||
printHelp(extensionFlags);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (parsed.listModels !== undefined) {
|
||||
const searchPattern = typeof parsed.listModels === "string" ? parsed.listModels : undefined;
|
||||
await listModels(modelRegistry, searchPattern);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Read piped stdin content (if any) - skip for RPC mode which uses stdin for JSON-RPC
|
||||
let stdinContent: string | undefined;
|
||||
if (appMode !== "rpc") {
|
||||
stdinContent = await readPipedStdin();
|
||||
if (stdinContent !== undefined) {
|
||||
appMode = "print";
|
||||
}
|
||||
}
|
||||
time("readPipedStdin");
|
||||
|
||||
const { initialMessage, initialImages } = await prepareInitialMessage(
|
||||
parsed,
|
||||
settingsManager.getImageAutoResize(),
|
||||
stdinContent,
|
||||
);
|
||||
time("prepareInitialMessage");
|
||||
const isInteractive = !parsed.print && parsed.mode === undefined;
|
||||
const startupBenchmark = isTruthyEnvFlag(process.env.PI_STARTUP_BENCHMARK);
|
||||
if (startupBenchmark && !isInteractive) {
|
||||
console.error(chalk.red("Error: PI_STARTUP_BENCHMARK only supports interactive mode"));
|
||||
process.exit(1);
|
||||
}
|
||||
const mode = parsed.mode || "text";
|
||||
initTheme(settingsManager.getTheme(), isInteractive);
|
||||
initTheme(settingsManager.getTheme(), appMode === "interactive");
|
||||
time("initTheme");
|
||||
|
||||
// Show deprecation warnings in interactive mode
|
||||
if (isInteractive && deprecationWarnings.length > 0) {
|
||||
if (appMode === "interactive" && deprecationWarnings.length > 0) {
|
||||
await showDeprecationWarnings(deprecationWarnings);
|
||||
}
|
||||
|
||||
let scopedModels: ScopedModel[] = [];
|
||||
const modelPatterns = parsed.models ?? settingsManager.getEnabledModels();
|
||||
if (modelPatterns && modelPatterns.length > 0) {
|
||||
scopedModels = await resolveModelScope(modelPatterns, modelRegistry);
|
||||
}
|
||||
const scopedModels = [...session.scopedModels];
|
||||
time("resolveModelScope");
|
||||
|
||||
// Create session manager based on CLI flags
|
||||
let sessionManager = await createSessionManager(parsed, cwd, extensionsResult, settingsManager);
|
||||
time("createSessionManager");
|
||||
|
||||
// Handle --resume: show session picker
|
||||
if (parsed.resume) {
|
||||
// Compute effective session dir for resume (same logic as createSessionManager)
|
||||
const effectiveSessionDir =
|
||||
parsed.sessionDir ??
|
||||
settingsManager.getSessionDir() ??
|
||||
(await callSessionDirectoryHook(extensionsResult, cwd));
|
||||
|
||||
const selectedPath = await selectSession(
|
||||
(onProgress) => SessionManager.list(cwd, effectiveSessionDir, onProgress),
|
||||
SessionManager.listAll,
|
||||
);
|
||||
if (!selectedPath) {
|
||||
console.log(chalk.dim("No session selected"));
|
||||
stopThemeWatcher();
|
||||
process.exit(0);
|
||||
}
|
||||
sessionManager = SessionManager.open(selectedPath, effectiveSessionDir);
|
||||
reportDiagnostics(runtime.diagnostics);
|
||||
if (runtime.diagnostics.some((diagnostic) => diagnostic.type === "error")) {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const { options: sessionOptions, cliThinkingFromModel } = buildSessionOptions(
|
||||
parsed,
|
||||
scopedModels,
|
||||
sessionManager,
|
||||
modelRegistry,
|
||||
settingsManager,
|
||||
);
|
||||
|
||||
if (parsed.apiKey) {
|
||||
if (!sessionOptions.model) {
|
||||
console.error(
|
||||
chalk.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 runtimeBootstrap = buildRuntimeBootstrap(parsed, cwd, agentDir, authStorage, sessionOptions);
|
||||
const runtime = await createAgentSessionRuntime(runtimeBootstrap, {
|
||||
cwd: sessionManager?.getCwd() ?? cwd,
|
||||
sessionManager,
|
||||
resourceLoader,
|
||||
});
|
||||
if (process.cwd() !== runtime.cwd) {
|
||||
process.chdir(runtime.cwd);
|
||||
}
|
||||
const runtimeHost = new AgentSessionRuntimeHost(runtimeBootstrap, runtime);
|
||||
const { session, modelFallbackMessage } = runtime;
|
||||
time("createAgentSession");
|
||||
|
||||
if (!isInteractive && !session.model) {
|
||||
if (appMode !== "interactive" && !session.model) {
|
||||
console.error(chalk.red("No models available."));
|
||||
console.error(chalk.yellow("\nSet an API key environment variable:"));
|
||||
console.error(" ANTHROPIC_API_KEY, OPENAI_API_KEY, GEMINI_API_KEY, etc.");
|
||||
@@ -890,25 +613,16 @@ export async function main(args: string[]) {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Clamp thinking level to model capabilities for CLI-provided thinking levels.
|
||||
// This covers both --thinking <level> and --model <pattern>:<thinking>.
|
||||
const cliThinkingOverride = parsed.thinking !== undefined || cliThinkingFromModel;
|
||||
if (session.model && cliThinkingOverride) {
|
||||
let effectiveThinking = session.thinkingLevel;
|
||||
if (!session.model.reasoning) {
|
||||
effectiveThinking = "off";
|
||||
} else if (effectiveThinking === "xhigh" && !supportsXhigh(session.model)) {
|
||||
effectiveThinking = "high";
|
||||
}
|
||||
if (effectiveThinking !== session.thinkingLevel) {
|
||||
session.setThinkingLevel(effectiveThinking);
|
||||
}
|
||||
const startupBenchmark = isTruthyEnvFlag(process.env.PI_STARTUP_BENCHMARK);
|
||||
if (startupBenchmark && appMode !== "interactive") {
|
||||
console.error(chalk.red("Error: PI_STARTUP_BENCHMARK only supports interactive mode"));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (mode === "rpc") {
|
||||
if (appMode === "rpc") {
|
||||
printTimings();
|
||||
await runRpcMode(runtimeHost);
|
||||
} else if (isInteractive) {
|
||||
await runRpcMode(runtime);
|
||||
} else if (appMode === "interactive") {
|
||||
if (scopedModels.length > 0 && (parsed.verbose || !settingsManager.getQuietStartup())) {
|
||||
const modelList = scopedModels
|
||||
.map((sm) => {
|
||||
@@ -919,7 +633,7 @@ export async function main(args: string[]) {
|
||||
console.log(chalk.dim(`Model scope: ${modelList} ${chalk.gray("(Ctrl+P to cycle)")}`));
|
||||
}
|
||||
|
||||
const interactiveMode = new InteractiveMode(runtimeHost, {
|
||||
const interactiveMode = new InteractiveMode(runtime, {
|
||||
migratedProviders,
|
||||
modelFallbackMessage,
|
||||
initialMessage,
|
||||
@@ -946,8 +660,8 @@ export async function main(args: string[]) {
|
||||
await interactiveMode.run();
|
||||
} else {
|
||||
printTimings();
|
||||
const exitCode = await runPrintMode(runtimeHost, {
|
||||
mode,
|
||||
const exitCode = await runPrintMode(runtime, {
|
||||
mode: toPrintOutputMode(appMode),
|
||||
messages: parsed.messages,
|
||||
initialMessage,
|
||||
initialImages,
|
||||
|
||||
@@ -6,6 +6,7 @@ import chalk from "chalk";
|
||||
import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "fs";
|
||||
import { dirname, join } from "path";
|
||||
import { CONFIG_DIR_NAME, getAgentDir, getBinDir } from "./config.js";
|
||||
import { migrateKeybindingsConfig } from "./core/keybindings.js";
|
||||
|
||||
const MIGRATION_GUIDE_URL =
|
||||
"https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/CHANGELOG.md#extensions-migration";
|
||||
@@ -152,6 +153,23 @@ function migrateCommandsToPrompts(baseDir: string, label: string): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
function migrateKeybindingsConfigFile(): void {
|
||||
const configPath = join(getAgentDir(), "keybindings.json");
|
||||
if (!existsSync(configPath)) return;
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(readFileSync(configPath, "utf-8")) as unknown;
|
||||
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
||||
return;
|
||||
}
|
||||
const { config, migrated } = migrateKeybindingsConfig(parsed as Record<string, unknown>);
|
||||
if (!migrated) return;
|
||||
writeFileSync(configPath, `${JSON.stringify(config, null, 2)}\n`, "utf-8");
|
||||
} catch {
|
||||
// Ignore malformed files during migration
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Move fd/rg binaries from tools/ to bin/ if they exist.
|
||||
*/
|
||||
@@ -290,6 +308,7 @@ export function runMigrations(cwd: string = process.cwd()): {
|
||||
const migratedAuthProviders = migrateAuthToAuthJson();
|
||||
migrateSessionsFromAgentRoot();
|
||||
migrateToolsToBin();
|
||||
migrateKeybindingsConfigFile();
|
||||
const deprecationWarnings = migrateExtensionSystem(cwd);
|
||||
return { migratedAuthProviders, deprecationWarnings };
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ import {
|
||||
VERSION,
|
||||
} from "../../config.js";
|
||||
import { type AgentSession, type AgentSessionEvent, parseSkillBlock } from "../../core/agent-session.js";
|
||||
import type { AgentSessionRuntimeHost } from "../../core/agent-session-runtime.js";
|
||||
import type { AgentSessionRuntime } from "../../core/agent-session-runtime.js";
|
||||
import type {
|
||||
ExtensionContext,
|
||||
ExtensionRunner,
|
||||
@@ -107,6 +107,7 @@ import {
|
||||
setRegisteredThemes,
|
||||
setTheme,
|
||||
setThemeInstance,
|
||||
stopThemeWatcher,
|
||||
Theme,
|
||||
type ThemeColor,
|
||||
theme,
|
||||
@@ -145,7 +146,7 @@ export interface InteractiveModeOptions {
|
||||
}
|
||||
|
||||
export class InteractiveMode {
|
||||
private runtimeHost: AgentSessionRuntimeHost;
|
||||
private runtimeHost: AgentSessionRuntime;
|
||||
private ui: TUI;
|
||||
private chatContainer: Container;
|
||||
private pendingMessagesContainer: Container;
|
||||
@@ -257,7 +258,7 @@ export class InteractiveMode {
|
||||
}
|
||||
|
||||
constructor(
|
||||
runtimeHost: AgentSessionRuntimeHost,
|
||||
runtimeHost: AgentSessionRuntime,
|
||||
private options: InteractiveModeOptions = {},
|
||||
) {
|
||||
this.runtimeHost = runtimeHost;
|
||||
@@ -1177,23 +1178,31 @@ export class InteractiveMode {
|
||||
this.loadingAnimation = undefined;
|
||||
}
|
||||
this.statusContainer.clear();
|
||||
const result = await this.runtimeHost.newSession(options);
|
||||
if (!result.cancelled) {
|
||||
await this.handleRuntimeSessionChange();
|
||||
this.renderCurrentSessionState();
|
||||
this.ui.requestRender();
|
||||
try {
|
||||
const result = await this.runtimeHost.newSession(options);
|
||||
if (!result.cancelled) {
|
||||
await this.handleRuntimeSessionChange();
|
||||
this.renderCurrentSessionState();
|
||||
this.ui.requestRender();
|
||||
}
|
||||
return result;
|
||||
} catch (error: unknown) {
|
||||
return this.handleFatalRuntimeError("Failed to create session", error);
|
||||
}
|
||||
return result;
|
||||
},
|
||||
fork: async (entryId) => {
|
||||
const result = await this.runtimeHost.fork(entryId);
|
||||
if (!result.cancelled) {
|
||||
await this.handleRuntimeSessionChange();
|
||||
this.renderCurrentSessionState();
|
||||
this.editor.setText(result.selectedText ?? "");
|
||||
this.showStatus("Forked to new session");
|
||||
try {
|
||||
const result = await this.runtimeHost.fork(entryId);
|
||||
if (!result.cancelled) {
|
||||
await this.handleRuntimeSessionChange();
|
||||
this.renderCurrentSessionState();
|
||||
this.editor.setText(result.selectedText ?? "");
|
||||
this.showStatus("Forked to new session");
|
||||
}
|
||||
return { cancelled: result.cancelled };
|
||||
} catch (error: unknown) {
|
||||
return this.handleFatalRuntimeError("Failed to fork session", error);
|
||||
}
|
||||
return { cancelled: result.cancelled };
|
||||
},
|
||||
navigateTree: async (targetId, options) => {
|
||||
const result = await this.session.navigateTree(targetId, {
|
||||
@@ -1275,6 +1284,14 @@ export class InteractiveMode {
|
||||
this.updateTerminalTitle();
|
||||
}
|
||||
|
||||
private async handleFatalRuntimeError(prefix: string, error: unknown): Promise<never> {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
this.showError(`${prefix}: ${message}`);
|
||||
stopThemeWatcher();
|
||||
this.stop();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
private renderCurrentSessionState(): void {
|
||||
this.chatContainer.clear();
|
||||
this.pendingMessagesContainer.clear();
|
||||
@@ -3817,13 +3834,17 @@ export class InteractiveMode {
|
||||
this.loadingAnimation = undefined;
|
||||
}
|
||||
this.statusContainer.clear();
|
||||
const result = await this.runtimeHost.switchSession(sessionPath);
|
||||
if (result.cancelled) {
|
||||
return;
|
||||
try {
|
||||
const result = await this.runtimeHost.switchSession(sessionPath);
|
||||
if (result.cancelled) {
|
||||
return;
|
||||
}
|
||||
await this.handleRuntimeSessionChange();
|
||||
this.renderCurrentSessionState();
|
||||
this.showStatus("Resumed session");
|
||||
} catch (error: unknown) {
|
||||
await this.handleFatalRuntimeError("Failed to resume session", error);
|
||||
}
|
||||
await this.handleRuntimeSessionChange();
|
||||
this.renderCurrentSessionState();
|
||||
this.showStatus("Resumed session");
|
||||
}
|
||||
|
||||
private async showOAuthSelector(mode: "login" | "logout"): Promise<void> {
|
||||
@@ -4088,7 +4109,7 @@ export class InteractiveMode {
|
||||
this.renderCurrentSessionState();
|
||||
this.showStatus(`Session imported from: ${inputPath}`);
|
||||
} catch (error: unknown) {
|
||||
this.showError(`Failed to import session: ${error instanceof Error ? error.message : "Unknown error"}`);
|
||||
await this.handleFatalRuntimeError("Failed to import session", error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4432,15 +4453,19 @@ export class InteractiveMode {
|
||||
this.loadingAnimation = undefined;
|
||||
}
|
||||
this.statusContainer.clear();
|
||||
const result = await this.runtimeHost.newSession();
|
||||
if (result.cancelled) {
|
||||
return;
|
||||
try {
|
||||
const result = await this.runtimeHost.newSession();
|
||||
if (result.cancelled) {
|
||||
return;
|
||||
}
|
||||
await this.handleRuntimeSessionChange();
|
||||
this.renderCurrentSessionState();
|
||||
this.chatContainer.addChild(new Spacer(1));
|
||||
this.chatContainer.addChild(new Text(`${theme.fg("accent", "✓ New session started")}`, 1, 1));
|
||||
this.ui.requestRender();
|
||||
} catch (error: unknown) {
|
||||
await this.handleFatalRuntimeError("Failed to create session", error);
|
||||
}
|
||||
await this.handleRuntimeSessionChange();
|
||||
this.renderCurrentSessionState();
|
||||
this.chatContainer.addChild(new Spacer(1));
|
||||
this.chatContainer.addChild(new Text(`${theme.fg("accent", "✓ New session started")}`, 1, 1));
|
||||
this.ui.requestRender();
|
||||
}
|
||||
|
||||
private handleDebugCommand(): void {
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
*/
|
||||
|
||||
import type { AssistantMessage, ImageContent } from "@mariozechner/pi-ai";
|
||||
import type { AgentSessionRuntimeHost } from "../core/agent-session-runtime.js";
|
||||
import type { AgentSessionRuntime } from "../core/agent-session-runtime.js";
|
||||
import { flushRawStdout, writeRawStdout } from "../core/output-guard.js";
|
||||
|
||||
/**
|
||||
@@ -28,7 +28,7 @@ export interface PrintModeOptions {
|
||||
* Run in print (single-shot) mode.
|
||||
* Sends prompts to the agent and outputs the result.
|
||||
*/
|
||||
export async function runPrintMode(runtimeHost: AgentSessionRuntimeHost, options: PrintModeOptions): Promise<number> {
|
||||
export async function runPrintMode(runtimeHost: AgentSessionRuntime, options: PrintModeOptions): Promise<number> {
|
||||
const { mode, messages = [], initialMessage, initialImages } = options;
|
||||
let exitCode = 0;
|
||||
let session = runtimeHost.session;
|
||||
@@ -124,6 +124,9 @@ export async function runPrintMode(runtimeHost: AgentSessionRuntimeHost, options
|
||||
}
|
||||
|
||||
return exitCode;
|
||||
} catch (error: unknown) {
|
||||
console.error(error instanceof Error ? error.message : String(error));
|
||||
return 1;
|
||||
} finally {
|
||||
unsubscribe?.();
|
||||
await runtimeHost.dispose();
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
*/
|
||||
|
||||
import * as crypto from "node:crypto";
|
||||
import type { AgentSessionRuntimeHost } from "../../core/agent-session-runtime.js";
|
||||
import type { AgentSessionRuntime } from "../../core/agent-session-runtime.js";
|
||||
import type {
|
||||
ExtensionUIContext,
|
||||
ExtensionUIDialogOptions,
|
||||
@@ -43,7 +43,7 @@ export type {
|
||||
* Run in RPC mode.
|
||||
* Listens for JSON commands on stdin, outputs events and responses on stdout.
|
||||
*/
|
||||
export async function runRpcMode(runtimeHost: AgentSessionRuntimeHost): Promise<never> {
|
||||
export async function runRpcMode(runtimeHost: AgentSessionRuntime): Promise<never> {
|
||||
takeOverStdout();
|
||||
let session = runtimeHost.session;
|
||||
let unsubscribe: (() => void) | undefined;
|
||||
@@ -628,29 +628,49 @@ export async function runRpcMode(runtimeHost: AgentSessionRuntimeHost): Promise<
|
||||
}
|
||||
|
||||
const handleInputLine = async (line: string) => {
|
||||
let parsed: unknown;
|
||||
try {
|
||||
const parsed = JSON.parse(line);
|
||||
parsed = JSON.parse(line);
|
||||
} catch (parseError: unknown) {
|
||||
output(
|
||||
error(
|
||||
undefined,
|
||||
"parse",
|
||||
`Failed to parse command: ${parseError instanceof Error ? parseError.message : String(parseError)}`,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle extension UI responses
|
||||
if (parsed.type === "extension_ui_response") {
|
||||
const response = parsed as RpcExtensionUIResponse;
|
||||
const pending = pendingExtensionRequests.get(response.id);
|
||||
if (pending) {
|
||||
pendingExtensionRequests.delete(response.id);
|
||||
pending.resolve(response);
|
||||
}
|
||||
return;
|
||||
// Handle extension UI responses
|
||||
if (
|
||||
typeof parsed === "object" &&
|
||||
parsed !== null &&
|
||||
"type" in parsed &&
|
||||
parsed.type === "extension_ui_response"
|
||||
) {
|
||||
const response = parsed as RpcExtensionUIResponse;
|
||||
const pending = pendingExtensionRequests.get(response.id);
|
||||
if (pending) {
|
||||
pendingExtensionRequests.delete(response.id);
|
||||
pending.resolve(response);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle regular commands
|
||||
const command = parsed as RpcCommand;
|
||||
const command = parsed as RpcCommand;
|
||||
try {
|
||||
const response = await handleCommand(command);
|
||||
output(response);
|
||||
|
||||
// Check for deferred shutdown request (idle between commands)
|
||||
await checkShutdownRequested();
|
||||
} catch (e: any) {
|
||||
output(error(undefined, "parse", `Failed to parse command: ${e.message}`));
|
||||
} catch (commandError: unknown) {
|
||||
output(
|
||||
error(
|
||||
command.id,
|
||||
command.type,
|
||||
commandError instanceof Error ? commandError.message : String(commandError),
|
||||
),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
270
packages/coding-agent/src/package-manager-cli.ts
Normal file
270
packages/coding-agent/src/package-manager-cli.ts
Normal file
@@ -0,0 +1,270 @@
|
||||
import chalk from "chalk";
|
||||
import { selectConfig } from "./cli/config-selector.js";
|
||||
import { APP_NAME, getAgentDir } from "./config.js";
|
||||
import { DefaultPackageManager } from "./core/package-manager.js";
|
||||
import { SettingsManager } from "./core/settings-manager.js";
|
||||
|
||||
export type PackageCommand = "install" | "remove" | "update" | "list";
|
||||
|
||||
interface PackageCommandOptions {
|
||||
command: PackageCommand;
|
||||
source?: string;
|
||||
local: boolean;
|
||||
help: boolean;
|
||||
invalidOption?: string;
|
||||
}
|
||||
|
||||
function reportSettingsErrors(settingsManager: SettingsManager, context: string): void {
|
||||
const errors = settingsManager.drainErrors();
|
||||
for (const { scope, error } of errors) {
|
||||
console.error(chalk.yellow(`Warning (${context}, ${scope} settings): ${error.message}`));
|
||||
if (error.stack) {
|
||||
console.error(chalk.dim(error.stack));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getPackageCommandUsage(command: PackageCommand): string {
|
||||
switch (command) {
|
||||
case "install":
|
||||
return `${APP_NAME} install <source> [-l]`;
|
||||
case "remove":
|
||||
return `${APP_NAME} remove <source> [-l]`;
|
||||
case "update":
|
||||
return `${APP_NAME} update [source]`;
|
||||
case "list":
|
||||
return `${APP_NAME} list`;
|
||||
}
|
||||
}
|
||||
|
||||
function printPackageCommandHelp(command: PackageCommand): void {
|
||||
switch (command) {
|
||||
case "install":
|
||||
console.log(`${chalk.bold("Usage:")}
|
||||
${getPackageCommandUsage("install")}
|
||||
|
||||
Install a package and add it to settings.
|
||||
|
||||
Options:
|
||||
-l, --local Install project-locally (.pi/settings.json)
|
||||
|
||||
Examples:
|
||||
${APP_NAME} install npm:@foo/bar
|
||||
${APP_NAME} install git:github.com/user/repo
|
||||
${APP_NAME} install git:git@github.com:user/repo
|
||||
${APP_NAME} install https://github.com/user/repo
|
||||
${APP_NAME} install ssh://git@github.com/user/repo
|
||||
${APP_NAME} install ./local/path
|
||||
`);
|
||||
return;
|
||||
|
||||
case "remove":
|
||||
console.log(`${chalk.bold("Usage:")}
|
||||
${getPackageCommandUsage("remove")}
|
||||
|
||||
Remove a package and its source from settings.
|
||||
Alias: ${APP_NAME} uninstall <source> [-l]
|
||||
|
||||
Options:
|
||||
-l, --local Remove from project settings (.pi/settings.json)
|
||||
|
||||
Examples:
|
||||
${APP_NAME} remove npm:@foo/bar
|
||||
${APP_NAME} uninstall npm:@foo/bar
|
||||
`);
|
||||
return;
|
||||
|
||||
case "update":
|
||||
console.log(`${chalk.bold("Usage:")}
|
||||
${getPackageCommandUsage("update")}
|
||||
|
||||
Update installed packages.
|
||||
If <source> is provided, only that package is updated.
|
||||
`);
|
||||
return;
|
||||
|
||||
case "list":
|
||||
console.log(`${chalk.bold("Usage:")}
|
||||
${getPackageCommandUsage("list")}
|
||||
|
||||
List installed packages from user and project settings.
|
||||
`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
function parsePackageCommand(args: string[]): PackageCommandOptions | undefined {
|
||||
const [rawCommand, ...rest] = args;
|
||||
let command: PackageCommand | undefined;
|
||||
if (rawCommand === "uninstall") {
|
||||
command = "remove";
|
||||
} else if (rawCommand === "install" || rawCommand === "remove" || rawCommand === "update" || rawCommand === "list") {
|
||||
command = rawCommand;
|
||||
}
|
||||
if (!command) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let local = false;
|
||||
let help = false;
|
||||
let invalidOption: string | undefined;
|
||||
let source: string | undefined;
|
||||
|
||||
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 };
|
||||
}
|
||||
|
||||
export async function handleConfigCommand(args: string[]): Promise<boolean> {
|
||||
if (args[0] !== "config") {
|
||||
return false;
|
||||
}
|
||||
|
||||
const cwd = process.cwd();
|
||||
const agentDir = getAgentDir();
|
||||
const settingsManager = SettingsManager.create(cwd, agentDir);
|
||||
reportSettingsErrors(settingsManager, "config command");
|
||||
const packageManager = new DefaultPackageManager({ cwd, agentDir, settingsManager });
|
||||
const resolvedPaths = await packageManager.resolve();
|
||||
|
||||
await selectConfig({
|
||||
resolvedPaths,
|
||||
settingsManager,
|
||||
cwd,
|
||||
agentDir,
|
||||
});
|
||||
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
export async function handlePackageCommand(args: string[]): Promise<boolean> {
|
||||
const options = parsePackageCommand(args);
|
||||
if (!options) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (options.help) {
|
||||
printPackageCommandHelp(options.command);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (options.invalidOption) {
|
||||
console.error(chalk.red(`Unknown option ${options.invalidOption} for "${options.command}".`));
|
||||
console.error(chalk.dim(`Use "${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.red(`Missing ${options.command} source.`));
|
||||
console.error(chalk.dim(`Usage: ${getPackageCommandUsage(options.command)}`));
|
||||
process.exitCode = 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
const cwd = process.cwd();
|
||||
const agentDir = getAgentDir();
|
||||
const settingsManager = SettingsManager.create(cwd, agentDir);
|
||||
reportSettingsErrors(settingsManager, "package command");
|
||||
const packageManager = new DefaultPackageManager({ cwd, agentDir, settingsManager });
|
||||
|
||||
packageManager.setProgressCallback((event) => {
|
||||
if (event.type === "start") {
|
||||
process.stdout.write(chalk.dim(`${event.message}\n`));
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
switch (options.command) {
|
||||
case "install":
|
||||
await packageManager.installAndPersist(source!, { local: options.local });
|
||||
console.log(chalk.green(`Installed ${source}`));
|
||||
return true;
|
||||
|
||||
case "remove": {
|
||||
const removed = await packageManager.removeAndPersist(source!, { local: options.local });
|
||||
if (!removed) {
|
||||
console.error(chalk.red(`No matching package found for ${source}`));
|
||||
process.exitCode = 1;
|
||||
return true;
|
||||
}
|
||||
console.log(chalk.green(`Removed ${source}`));
|
||||
return true;
|
||||
}
|
||||
|
||||
case "list": {
|
||||
const configuredPackages = packageManager.listConfiguredPackages();
|
||||
const userPackages = configuredPackages.filter((pkg) => pkg.scope === "user");
|
||||
const projectPackages = configuredPackages.filter((pkg) => pkg.scope === "project");
|
||||
|
||||
if (configuredPackages.length === 0) {
|
||||
console.log(chalk.dim("No packages installed."));
|
||||
return true;
|
||||
}
|
||||
|
||||
const formatPackage = (pkg: (typeof configuredPackages)[number]) => {
|
||||
const display = pkg.filtered ? `${pkg.source} (filtered)` : pkg.source;
|
||||
console.log(` ${display}`);
|
||||
if (pkg.installedPath) {
|
||||
console.log(chalk.dim(` ${pkg.installedPath}`));
|
||||
}
|
||||
};
|
||||
|
||||
if (userPackages.length > 0) {
|
||||
console.log(chalk.bold("User packages:"));
|
||||
for (const pkg of userPackages) {
|
||||
formatPackage(pkg);
|
||||
}
|
||||
}
|
||||
|
||||
if (projectPackages.length > 0) {
|
||||
if (userPackages.length > 0) console.log();
|
||||
console.log(chalk.bold("Project packages:"));
|
||||
for (const pkg of projectPackages) {
|
||||
formatPackage(pkg);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
case "update":
|
||||
await packageManager.update(source);
|
||||
if (source) {
|
||||
console.log(chalk.green(`Updated ${source}`));
|
||||
} else {
|
||||
console.log(chalk.green("Updated packages"));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
const message = error instanceof Error ? error.message : "Unknown package command error";
|
||||
console.error(chalk.red(`Error: ${message}`));
|
||||
process.exitCode = 1;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user