Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/chat/__tests__/handleChatWorkflowStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,30 @@ describe("handleChatWorkflowStream", () => {
expect(startArgs.modelId).toBe("anthropic/claude-haiku-4.5");
});

it("prefers validated.model over a persisted chat.model_id", async () => {
vi.mocked(validateChatWorkflow).mockResolvedValue({
messages: [],
chatId: CHAT_ID,
sessionId: SESSION_ID,
accountId: ACCOUNT_ID,
orgId: null,
authToken: "test-key",
model: "openai/gpt-5.4-mini",
});
vi.mocked(selectChats).mockResolvedValue([
{
id: CHAT_ID,
session_id: SESSION_ID,
active_stream_id: null,
model_id: "anthropic/claude-opus-4.6",
} as never,
]);
mockStartedRun();
await handleChatWorkflowStream(makeRequest());
const startArgs = vi.mocked(start).mock.calls[0]?.[1]?.[0] as { modelId: string };
expect(startArgs.modelId).toBe("openai/gpt-5.4-mini");
});

// Bundle A.4 — forward the Privy JWT from the validated body into
// AgentContext.recoupAccessToken so the sandbox env-builder can
// surface it as `RECOUP_ACCESS_TOKEN`.
Expand Down
9 changes: 9 additions & 0 deletions lib/chat/__tests__/validateChatWorkflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ describe("validateChatWorkflow", () => {
if (result instanceof NextResponse) return;
expect(result.recoupAccessToken).toBe("eyJ.test.jwt");
});

it("accepts and surfaces the gateway-format model selected in the UI", async () => {
const result = await validateChatWorkflow(
makeRequest({ ...validBody, model: "openai/gpt-5.4-mini" }),
);
expect(result).not.toBeInstanceOf(NextResponse);
if (result instanceof NextResponse) return;
expect(result.model).toBe("openai/gpt-5.4-mini");
});
});

describe("invalid body", () => {
Expand Down
3 changes: 2 additions & 1 deletion lib/chat/handleChatWorkflowStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export async function handleChatWorkflowStream(request: NextRequest): Promise<Re
await updateSession(validated.sessionId, buildActiveLifecycleUpdate(session.sandbox_state));
void persistLatestUserMessage(validated.chatId, validated.messages as never);

const modelId = chat.model_id ?? DEFAULT_MODEL_ID;
// UI selection wins over the chat's persisted model, then the default.
const modelId = validated.model ?? chat.model_id ?? DEFAULT_MODEL_ID;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Whitespace-only model values are treated as valid overrides, bypassing fallback to persisted/default model.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At lib/chat/handleChatWorkflowStream.ts, line 97:

<comment>Whitespace-only `model` values are treated as valid overrides, bypassing fallback to persisted/default model.</comment>

<file context>
@@ -90,7 +90,11 @@ export async function handleChatWorkflowStream(request: NextRequest): Promise<Re
+  // persisted model_id, then the default. Without this the workflow
+  // always billed DEFAULT_MODEL_ID since model_id is null until a
+  // chat is explicitly PATCHed.
+  const modelId = validated.model ?? chat.model_id ?? DEFAULT_MODEL_ID;
   const recoupOrgId = session.clone_url
     ? (extractOrgId(session.clone_url) ?? undefined)
</file context>
Suggested change
const modelId = validated.model ?? chat.model_id ?? DEFAULT_MODEL_ID;
const requestedModelId = validated.model?.trim();
const modelId = requestedModelId ? requestedModelId : chat.model_id ?? DEFAULT_MODEL_ID;

const recoupOrgId = session.clone_url
? (extractOrgId(session.clone_url) ?? undefined)
: undefined;
Expand Down
2 changes: 2 additions & 0 deletions lib/chat/validateChatWorkflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export const chatWorkflowBodySchema = z.object({
* field shape.
*/
recoupAccessToken: z.string().min(1).max(8192).optional(),
/** Gateway-format model id selected in the chat UI (e.g. `"openai/gpt-5.4-mini"`). */
model: z.string().min(1).max(256).optional(),
});

export type ChatWorkflowBody = z.infer<typeof chatWorkflowBodySchema>;
Expand Down
Loading