Repository navigation
feat(codex): add fast tier settings toggle #367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| "use client"; | ||
|
|
||
| import { useEffect, useState } from "react"; | ||
| import { Card } from "@/shared/components"; | ||
|
|
||
| export default function CodexServiceTierTab() { | ||
| const [enabled, setEnabled] = useState(false); | ||
| const [loading, setLoading] = useState(true); | ||
| const [saving, setSaving] = useState(false); | ||
| const [status, setStatus] = useState<"" | "saved" | "error">(""); | ||
|
|
||
| useEffect(() => { | ||
| fetch("/api/settings/codex-service-tier") | ||
| .then((res) => res.json()) | ||
| .then((data) => { | ||
| setEnabled(Boolean(data.enabled)); | ||
| setLoading(false); | ||
| }) | ||
| .catch(() => setLoading(false)); | ||
| }, []); | ||
|
|
||
| const save = async (nextEnabled: boolean) => { | ||
| setEnabled(nextEnabled); | ||
| setSaving(true); | ||
| setStatus(""); | ||
|
|
||
| try { | ||
| const res = await fetch("/api/settings/codex-service-tier", { | ||
| method: "PUT", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify({ enabled: nextEnabled }), | ||
| }); | ||
|
|
||
| if (res.ok) { | ||
| setStatus("saved"); | ||
| setTimeout(() => setStatus(""), 2000); | ||
| } else { | ||
| setStatus("error"); | ||
| setEnabled(!nextEnabled); | ||
| } | ||
| } catch { | ||
| setStatus("error"); | ||
| setEnabled(!nextEnabled); | ||
| } finally { | ||
| setSaving(false); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <Card> | ||
| <div className="flex items-center gap-3 mb-5"> | ||
| <div className="p-2 rounded-lg bg-sky-500/10 text-sky-500"> | ||
| <span className="material-symbols-outlined text-[20px]" aria-hidden="true"> | ||
| bolt | ||
| </span> | ||
| </div> | ||
| <div className="flex-1"> | ||
| <h3 className="text-lg font-semibold">Codex Fast Service Tier</h3> | ||
| <p className="text-sm text-text-muted"> | ||
| Inject `service_tier=fast` into Codex requests when the client leaves it unset. | ||
| </p> | ||
| </div> | ||
| {status === "saved" && ( | ||
| <span className="text-xs font-medium text-emerald-500 flex items-center gap-1"> | ||
| <span className="material-symbols-outlined text-[14px]">check_circle</span> | ||
| Saved | ||
| </span> | ||
| )} | ||
| {status === "error" && ( | ||
| <span className="text-xs font-medium text-rose-500 flex items-center gap-1"> | ||
| <span className="material-symbols-outlined text-[14px]">error</span> | ||
| Failed to save | ||
| </span> | ||
| )} | ||
| </div> | ||
|
|
||
| <div className="flex items-center justify-between p-4 rounded-lg bg-surface/30 border border-border/30"> | ||
| <div> | ||
| <p className="text-sm font-medium">Force fast tier for Codex</p> | ||
| <p className="text-xs text-text-muted mt-0.5"> | ||
| Off by default. Applies only to Codex requests and does not override an explicit tier. | ||
| </p> | ||
| </div> | ||
| <button | ||
| onClick={() => save(!enabled)} | ||
| disabled={loading || saving} | ||
| className={`relative inline-flex h-6 w-11 items-center rounded-full transition-colors ${ | ||
| enabled ? "bg-sky-500" : "bg-white/10" | ||
| }`} | ||
| > | ||
| <span | ||
| className={`inline-block h-4 w-4 rounded-full bg-white transition-transform ${ | ||
| enabled ? "translate-x-6" : "translate-x-1" | ||
| }`} | ||
| /> | ||
| </button> | ||
| </div> | ||
| </Card> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { NextResponse, type Request } from "next/server"; | ||
| import { getSettings, updateSettings } from "@/lib/localDb"; | ||
| import { setDefaultFastServiceTierEnabled } from "@omniroute/open-sse/executors/codex.ts"; | ||
| import { updateCodexServiceTierSchema } from "@/shared/validation/schemas"; | ||
| import { isValidationFailure, validateBody } from "@/shared/validation/helpers"; | ||
|
|
||
| export async function GET() { | ||
| try { | ||
| const settings = await getSettings(); | ||
| const persisted = | ||
| typeof settings.codexServiceTier === "string" | ||
| ? JSON.parse(settings.codexServiceTier) | ||
| : settings.codexServiceTier; | ||
|
|
||
| return NextResponse.json({ | ||
| enabled: typeof persisted?.enabled === "boolean" ? persisted.enabled : false, | ||
| }); | ||
| } catch (error) { | ||
| console.error("[API ERROR] /api/settings/codex-service-tier GET:", error); | ||
| return NextResponse.json({ error: "Failed to get config" }, { status: 500 }); | ||
| } | ||
| } | ||
|
|
||
| export async function PUT(request: Request) { | ||
| let rawBody; | ||
| try { | ||
| rawBody = await request.json(); | ||
| } catch { | ||
| return NextResponse.json( | ||
| { | ||
| error: { | ||
| message: "Invalid request", | ||
| details: [{ field: "body", message: "Invalid JSON body" }], | ||
| }, | ||
| }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| try { | ||
| const validation = validateBody(updateCodexServiceTierSchema, rawBody); | ||
| if (isValidationFailure(validation)) { | ||
| return NextResponse.json({ error: validation.error }, { status: 400 }); | ||
| } | ||
|
|
||
| const config = validation.data; | ||
| setDefaultFastServiceTierEnabled(config.enabled); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CRITICAL: Race condition - in-memory state updated before database The in-memory state ( Consider updating the database first, then updating in-memory state, or wrap both in a transaction with rollback capability. |
||
| await updateSettings({ codexServiceTier: config }); | ||
|
|
||
| return NextResponse.json(config); | ||
| } catch (error) { | ||
| console.error("[API ERROR] /api/settings/codex-service-tier PUT:", error); | ||
| return NextResponse.json({ error: "Failed to update config" }, { status: 500 }); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error handling in the
savefunction can be improved:statusis set to"error"on failure, but there is no corresponding UI element to display this error. This means the save operation fails silently for the user.To fix the inconsistent state, you can revert the
enabledstate on failure. For the silent failure, you should add a UI element to display whenstatus === 'error'. Here is a suggestion to fix the state inconsistency: