@@ -1,10 +1,15 @@
1-import type { AuthEvent, AuthPrompt, AuthType, Models } from "@earendil-works/pi-ai";
1+import type { Api, AuthEvent, AuthPrompt, AuthType, Model, Models } from "@earendil-works/pi-ai";
2+import { saveSelection } from "../config.ts";
23 import { dim, red } from "./styles.ts";
34 import { commonPrefix } from "./complete.ts";
45
56 export interface CommandContext {
67 /** The collection the running command may reach, e.g. to start a login. */
78 models: Models;
9+ /** The model the session is currently running. */
10+ model: Model<Api>;
11+ /** Switches the running session to `model`. */
12+ select(model: Model<Api>): void;
813 /** Aborts when the running command is cancelled (Ctrl+C). */
914 signal: AbortSignal;
1015 /** Appends already-styled lines to scrollback. */
@@ -98,7 +103,54 @@ const login: Command = {
98103 },
99104 };
100105
101-const COMMANDS: Command[] = [help, login];
106+const provider: Command = {
107+ name: "provider",
108+ description: "choose the provider and model",
109+ async run(ctx): Promise<void> {
110+ const available = await ctx.models.getAvailable(undefined, { signal: ctx.signal });
111+ const ids = [...new Set(available.map((m) => m.provider))];
112+ if (ids.length === 0) throw new Error("no authenticated providers");
113+ const providerId = await ctx.prompt({
114+ type: "select",
115+ message: "Select a provider",
116+ options: ids.map((id) => ({ id, label: ctx.models.getProvider(id)?.name ?? id })),
117+ });
118+ const models = available.filter((m) => m.provider === providerId);
119+ if (models.length === 0) throw new Error(`no models for provider "${providerId}"`);
120+ const name = ctx.models.getProvider(providerId)?.name ?? providerId;
121+ const modelId = await ctx.prompt({
122+ type: "select",
123+ message: `Select a model for ${name}`,
124+ options: models.map((m) => ({ id: m.id, label: m.name ?? m.id })),
125+ });
126+ const chosen = models.find((m) => m.id === modelId);
127+ if (chosen === undefined) throw new Error(`unknown model: ${modelId}`);
128+ saveSelection(providerId, modelId);
129+ ctx.select(chosen);
130+ },
131+};
132+
133+const model: Command = {
134+ name: "model",
135+ description: "choose a model for the current provider",
136+ async run(ctx): Promise<void> {
137+ const providerId = ctx.model.provider;
138+ const models = await ctx.models.getAvailable(providerId, { signal: ctx.signal });
139+ if (models.length === 0) throw new Error(`no models for provider "${providerId}"`);
140+ const name = ctx.models.getProvider(providerId)?.name ?? providerId;
141+ const modelId = await ctx.prompt({
142+ type: "select",
143+ message: `Select a model for ${name}`,
144+ options: models.map((m) => ({ id: m.id, label: m.name ?? m.id })),
145+ });
146+ const chosen = models.find((m) => m.id === modelId);
147+ if (chosen === undefined) throw new Error(`unknown model: ${modelId}`);
148+ saveSelection(providerId, modelId);
149+ ctx.select(chosen);
150+ },
151+};
152+
153+const COMMANDS: Command[] = [help, login, provider, model];
102154
103155 /** `/name` for a known `name`, else null; unknown slash text stays a message. */
104156 export function findCommand(text: string): { command: Command; args: string } | null {