@@ -27,6 +27,7 @@ export type AgentEvent =
2727 | { type: "toolOutput"; chunk: string }
2828 | { type: "toolResult"; name: string; text: string; isError: boolean; diffs?: FileDiff[] }
2929 | { type: "message"; message: AssistantMessage }
30+ | { type: "noModel" }
3031 | { type: "error"; message: string }
3132 | { type: "cancelled" }
3233 | { type: "complete" };
@@ -46,7 +47,8 @@ export const NO_INTERACTION: Interaction = {
4647 /** What one turn needs, shared by both projections: the TUI and `--print`. */
4748 export interface AgentOptions {
4849 models: Models;
49- model: Model<Api>;
50+ /** The selected model, or null until one is configured or chosen in the TUI. */
51+ model: Model<Api> | null;
5052 systemPrompt: string;
5153 tools: Tool[];
5254 /** The configured tool names, kept so a model switch can rebuild `tools`. */
@@ -65,6 +67,12 @@ interface AgentRun extends AgentOptions {
6567 export async function runAgentTurn(run: AgentRun): Promise<void> {
6668 const { messages, session, signal, interaction, onEvent } = run;
6769
70+ const model = run.model;
71+ if (model === null) {
72+ onEvent({ type: "noModel" });
73+ return;
74+ }
75+
6876 const cancelled = (): void => onEvent({ type: "cancelled" });
6977 const failed = (message: string): void => onEvent({ type: "error", message });
7078
@@ -105,9 +113,9 @@ export async function runAgentTurn(run: AgentRun): Promise<void> {
105113
106114 onEvent({ type: "phase", phase: "preparing" });
107115 session.appendRequest({
108- provider: run.model.provider,
109- model: run.model.id,
110- api: run.model.api,
116+ provider: model.provider,
117+ model: model.id,
118+ api: model.api,
111119 thinkingEffort: run.thinkingEffort,
112120 systemPrompt: run.systemPrompt,
113121 tools: run.tools,
@@ -117,7 +125,7 @@ export async function runAgentTurn(run: AgentRun): Promise<void> {
117125 const context = { systemPrompt: run.systemPrompt, tools: run.tools, messages };
118126 let stream;
119127 try {
120- stream = run.models.streamSimple(run.model, context, {
128+ stream = run.models.streamSimple(model, context, {
121129 reasoning: run.thinkingEffort === "off" ? undefined : run.thinkingEffort,
122130 signal,
123131 sessionId: session.id ?? undefined,
@@ -186,7 +194,7 @@ export async function runAgentTurn(run: AgentRun): Promise<void> {
186194 try {
187195 result = await executeTool(call.name, validateToolCall(run.tools, call), {
188196 signal,
189- supportsImages: acceptsImages(run.model),
197+ supportsImages: acceptsImages(model),
190198 onOutput: (chunk) => onEvent({ type: "toolOutput", chunk }),
191199 });
192200 } catch (error) {