feat: unified model configuration — BFF auth, delegation JWT, thread model selection
- Multi-user auth (better-sqlite3 + scrypt) with admin/user roles and ES256 delegation JWT signing for EvoScientist Config API calls - BFF config client sends Authorization: Bearer <service token> + X-Evo-Actor delegation JWT (design doc 7.3) - Model registry admin API routes (GET/PUT registry, credential replace, provider test) proxying the backend with revision CAS - ThreadModelSelection replaces model_override: PATCH CAS with expected_selection_revision, per-thread serialization, 409 THREAD_MODEL_SELECTION_CONFLICT - Run creation freezes a runtime snapshot per turn; command resumes reuse the turn's frozen snapshot from run metadata (design doc 7.2/8.2) - Chat UI: ModelRef picker fed by GET /api/models selectable list; removed /model <name> command, localStorage preference, and set-default star - Deleted lib/modelCommand.ts, lib/modelPreference.ts, lib/defaultModel.ts Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -51,13 +51,14 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
try {
|
||||
if (!verifyCredentials(username, password)) {
|
||||
const user = verifyCredentials(username, password);
|
||||
if (!user) {
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid username or password." },
|
||||
{ status: 401, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const { token, payload } = createSession(username);
|
||||
const { token, payload } = createSession(user.username, user.role);
|
||||
await recordLogin(payload);
|
||||
const response = NextResponse.json(
|
||||
{
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import { AUTH_COOKIE_NAME, isAuthenticationEnabled } from "@/lib/auth";
|
||||
import { decodeSessionPayload } from "@/lib/server/auth";
|
||||
import { decodeSessionPayload, roleForUsername } from "@/lib/server/auth";
|
||||
import { ensureBootstrapAdmin } from "@/lib/server/userStore";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
@@ -24,9 +25,17 @@ export function GET(request: NextRequest) {
|
||||
{ status: 401, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
ensureBootstrapAdmin();
|
||||
const role = roleForUsername(session.username);
|
||||
if (!role) {
|
||||
return NextResponse.json(
|
||||
{ error: "Authentication required." },
|
||||
{ status: 401, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ username: session.username },
|
||||
{ username: session.username, role },
|
||||
{ headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import { isAuthenticationEnabled } from "@/lib/auth";
|
||||
import { AUTH_COOKIE_NAME, isAuthenticationEnabled } from "@/lib/auth";
|
||||
import {
|
||||
AuthConfigurationError,
|
||||
authCookieOptions,
|
||||
createSession,
|
||||
getAuthConfiguration,
|
||||
decodeSessionPayload,
|
||||
recordLogin,
|
||||
roleForUsername,
|
||||
updateAuthPassword,
|
||||
verifyCredentials,
|
||||
} from "@/lib/server/auth";
|
||||
@@ -23,7 +24,7 @@ function hasControlCharacter(value: string): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
function passwordError(password: string): string | null {
|
||||
export function passwordValidationError(password: string): string | null {
|
||||
if (password.length < 8) return "New password must contain at least 8 characters.";
|
||||
if (password.length > 256) return "New password is too long.";
|
||||
if (hasControlCharacter(password)) {
|
||||
@@ -46,6 +47,15 @@ export async function POST(request: NextRequest) {
|
||||
{ status: 403, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const session = decodeSessionPayload(
|
||||
request.cookies.get(AUTH_COOKIE_NAME)?.value ?? ""
|
||||
);
|
||||
if (!session || session.expiresAt <= Math.floor(Date.now() / 1000)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Authentication required." },
|
||||
{ status: 401, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const body = (await request.json().catch(() => null)) as {
|
||||
currentPassword?: unknown;
|
||||
newPassword?: unknown;
|
||||
@@ -57,7 +67,7 @@ export async function POST(request: NextRequest) {
|
||||
typeof body?.newPassword === "string" ? body.newPassword : "";
|
||||
const confirmPassword =
|
||||
typeof body?.confirmPassword === "string" ? body.confirmPassword : "";
|
||||
const validationError = passwordError(newPassword);
|
||||
const validationError = passwordValidationError(newPassword);
|
||||
if (!currentPassword || !newPassword || !confirmPassword || validationError) {
|
||||
return NextResponse.json(
|
||||
{ error: validationError ?? "Complete all password fields." },
|
||||
@@ -72,8 +82,7 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
try {
|
||||
const config = getAuthConfiguration();
|
||||
if (!verifyCredentials(config.username, currentPassword)) {
|
||||
if (!verifyCredentials(session.username, currentPassword)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Current password is incorrect." },
|
||||
{ status: 401, headers: NO_STORE }
|
||||
@@ -85,8 +94,9 @@ export async function POST(request: NextRequest) {
|
||||
{ status: 400, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
await updateAuthPassword(newPassword);
|
||||
const { token, payload } = createSession(config.username);
|
||||
await updateAuthPassword(session.username, newPassword);
|
||||
const role = roleForUsername(session.username) ?? session.role;
|
||||
const { token, payload } = createSession(session.username, role);
|
||||
await recordLogin(payload);
|
||||
const response = NextResponse.json({ ok: true }, { headers: NO_STORE });
|
||||
response.cookies.set({ ...authCookieOptions(), value: token });
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import { requireActor, requireAdmin } from "@/lib/server/actor";
|
||||
import { isCrossOrigin } from "@/lib/server/workspace";
|
||||
import { NO_STORE, routeErrorResponse } from "@/lib/server/routeErrors";
|
||||
import {
|
||||
deleteUser,
|
||||
getUser,
|
||||
listUsers,
|
||||
setUserPassword,
|
||||
setUserRole,
|
||||
UserStoreError,
|
||||
} from "@/lib/server/userStore";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
type RouteContext = { params: Promise<{ username: string }> };
|
||||
|
||||
function lastAdminGuard(username: string, nextRole?: string): string | null {
|
||||
const admins = listUsers().filter((user) => user.role === "admin");
|
||||
const targetIsLastAdmin =
|
||||
admins.length === 1 && admins[0].username === username;
|
||||
if (targetIsLastAdmin && nextRole !== "admin") {
|
||||
return "The last admin account cannot be demoted or deleted.";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function PATCH(request: NextRequest, context: RouteContext) {
|
||||
try {
|
||||
if (isCrossOrigin(request)) {
|
||||
return NextResponse.json(
|
||||
{ code: "FORBIDDEN", message: "Cross-origin access is not allowed." },
|
||||
{ status: 403, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const actor = requireActor(request);
|
||||
requireAdmin(actor);
|
||||
const { username } = await context.params;
|
||||
if (!getUser(username)) {
|
||||
return NextResponse.json(
|
||||
{ code: "INVALID_REQUEST", message: `User '${username}' does not exist.` },
|
||||
{ status: 404, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const body = (await request.json().catch(() => null)) as {
|
||||
role?: unknown;
|
||||
password?: unknown;
|
||||
} | null;
|
||||
const nextRole =
|
||||
body?.role === "admin" ? "admin" : body?.role === "user" ? "user" : null;
|
||||
const nextPassword =
|
||||
typeof body?.password === "string" && body.password ? body.password : null;
|
||||
if (!nextRole && !nextPassword) {
|
||||
return NextResponse.json(
|
||||
{ code: "INVALID_REQUEST", message: "Provide role and/or password." },
|
||||
{ status: 400, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
if (nextRole) {
|
||||
const guard = lastAdminGuard(username, nextRole);
|
||||
if (guard) {
|
||||
return NextResponse.json(
|
||||
{ code: "INVALID_REQUEST", message: guard },
|
||||
{ status: 400, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
setUserRole(username, nextRole);
|
||||
}
|
||||
if (nextPassword) {
|
||||
if (nextPassword.length < 8 || nextPassword.length > 256) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
code: "INVALID_REQUEST",
|
||||
message: "Password must contain 8-256 characters.",
|
||||
},
|
||||
{ status: 400, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
setUserPassword(username, nextPassword);
|
||||
}
|
||||
return NextResponse.json({ user: getUser(username) }, { headers: NO_STORE });
|
||||
} catch (error) {
|
||||
if (error instanceof UserStoreError) {
|
||||
return NextResponse.json(
|
||||
{ code: "INVALID_REQUEST", message: error.message },
|
||||
{ status: 400, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
return routeErrorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(request: NextRequest, context: RouteContext) {
|
||||
try {
|
||||
if (isCrossOrigin(request)) {
|
||||
return NextResponse.json(
|
||||
{ code: "FORBIDDEN", message: "Cross-origin access is not allowed." },
|
||||
{ status: 403, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const actor = requireActor(request);
|
||||
requireAdmin(actor);
|
||||
const { username } = await context.params;
|
||||
if (username === actor.sub) {
|
||||
return NextResponse.json(
|
||||
{ code: "INVALID_REQUEST", message: "You cannot delete your own account." },
|
||||
{ status: 400, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const guard = lastAdminGuard(username);
|
||||
if (guard) {
|
||||
return NextResponse.json(
|
||||
{ code: "INVALID_REQUEST", message: guard },
|
||||
{ status: 400, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
deleteUser(username);
|
||||
return NextResponse.json({ ok: true }, { headers: NO_STORE });
|
||||
} catch (error) {
|
||||
if (error instanceof UserStoreError) {
|
||||
return NextResponse.json(
|
||||
{ code: "INVALID_REQUEST", message: error.message },
|
||||
{ status: 404, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
return routeErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import { requireActor, requireAdmin } from "@/lib/server/actor";
|
||||
import { isCrossOrigin } from "@/lib/server/workspace";
|
||||
import { NO_STORE, routeErrorResponse } from "@/lib/server/routeErrors";
|
||||
import {
|
||||
createUser,
|
||||
ensureBootstrapAdmin,
|
||||
listUsers,
|
||||
UserStoreError,
|
||||
validateUsername,
|
||||
} from "@/lib/server/userStore";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
/** Admin user management (design doc 7.2: the first admin comes from the
|
||||
* deployment environment; later users are managed by admins). */
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
if (isCrossOrigin(request)) {
|
||||
return NextResponse.json(
|
||||
{ code: "FORBIDDEN", message: "Cross-origin access is not allowed." },
|
||||
{ status: 403, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const actor = requireActor(request);
|
||||
requireAdmin(actor);
|
||||
ensureBootstrapAdmin();
|
||||
return NextResponse.json({ users: listUsers() }, { headers: NO_STORE });
|
||||
} catch (error) {
|
||||
return routeErrorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
if (isCrossOrigin(request)) {
|
||||
return NextResponse.json(
|
||||
{ code: "FORBIDDEN", message: "Cross-origin access is not allowed." },
|
||||
{ status: 403, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const actor = requireActor(request);
|
||||
requireAdmin(actor);
|
||||
ensureBootstrapAdmin();
|
||||
const body = (await request.json().catch(() => null)) as {
|
||||
username?: unknown;
|
||||
password?: unknown;
|
||||
role?: unknown;
|
||||
} | null;
|
||||
const username = typeof body?.username === "string" ? body.username.trim() : "";
|
||||
const password = typeof body?.password === "string" ? body.password : "";
|
||||
const role = body?.role === "admin" ? "admin" : "user";
|
||||
const invalid = validateUsername(username);
|
||||
if (invalid) {
|
||||
return NextResponse.json(
|
||||
{ code: "INVALID_REQUEST", message: invalid },
|
||||
{ status: 400, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
if (password.length < 8 || password.length > 256) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
code: "INVALID_REQUEST",
|
||||
message: "Password must contain 8-256 characters.",
|
||||
},
|
||||
{ status: 400, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const user = createUser(username, password, role);
|
||||
return NextResponse.json({ user }, { status: 201, headers: NO_STORE });
|
||||
} catch (error) {
|
||||
if (error instanceof UserStoreError) {
|
||||
return NextResponse.json(
|
||||
{ code: "INVALID_REQUEST", message: error.message },
|
||||
{ status: 400, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
return routeErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,12 @@ import {
|
||||
sanitizeConversationThread,
|
||||
} from "@/lib/server/conversationResponse";
|
||||
import { drainConversationScope } from "@/lib/server/scopeDrain";
|
||||
import {
|
||||
parseThreadModelSelection,
|
||||
readThreadModelSelection,
|
||||
ThreadModelSelectionError,
|
||||
withThreadSelectionLock,
|
||||
} from "@/lib/server/threadModelSelection";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
@@ -60,7 +66,12 @@ export async function PATCH(request: NextRequest, context: RouteContext) {
|
||||
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
||||
return NextResponse.json({ error: "Invalid conversation patch." }, { status: 400 });
|
||||
}
|
||||
const allowed = new Set(["title", "pinned", "model_override"]);
|
||||
const allowed = new Set([
|
||||
"title",
|
||||
"pinned",
|
||||
"model_selection",
|
||||
"expected_selection_revision",
|
||||
]);
|
||||
const entries = Object.entries(body as Record<string, unknown>);
|
||||
if (entries.length === 0 || entries.some(([key]) => !allowed.has(key) || forbiddenMetadataKey(key))) {
|
||||
return NextResponse.json({ error: "Unsupported conversation field." }, { status: 400 });
|
||||
@@ -71,16 +82,71 @@ export async function PATCH(request: NextRequest, context: RouteContext) {
|
||||
if ("pinned" in body && typeof body.pinned !== "boolean") {
|
||||
return NextResponse.json({ error: "pinned must be a boolean." }, { status: 400 });
|
||||
}
|
||||
const { deployment, thread, scope } = await verifiedConversation(threadId);
|
||||
const wantsSelection = "model_selection" in body;
|
||||
if (wantsSelection) {
|
||||
const expected = body.expected_selection_revision;
|
||||
if (typeof expected !== "number" || !Number.isInteger(expected) || expected < 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
code: "INVALID_REQUEST",
|
||||
error: "expected_selection_revision must be a non-negative integer.",
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
try {
|
||||
parseThreadModelSelection(body.model_selection);
|
||||
} catch (error) {
|
||||
if (error instanceof ThreadModelSelectionError) {
|
||||
return NextResponse.json(
|
||||
{ code: error.code, error: error.message },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
} else if ("expected_selection_revision" in body) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
code: "INVALID_REQUEST",
|
||||
error: "expected_selection_revision requires model_selection.",
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
const { deployment, scope } = await verifiedConversation(threadId);
|
||||
if (scope?.state === "deleting" || scope?.state === "deleted") {
|
||||
return NextResponse.json({ error: "Conversation is not available." }, { status: 409 });
|
||||
}
|
||||
const metadata = {
|
||||
...((thread.metadata as Record<string, unknown> | undefined) ?? {}),
|
||||
...body,
|
||||
};
|
||||
const updated = await deployment.threadClient.threads.update(threadId, { metadata });
|
||||
return NextResponse.json({ thread: sanitizeConversationThread(updated) });
|
||||
const updated = await withThreadSelectionLock(threadId, async () => {
|
||||
const current = await deployment.threadClient.threads.get(threadId);
|
||||
const currentMetadata =
|
||||
(current.metadata as Record<string, unknown> | undefined) ?? {};
|
||||
const metadata: Record<string, unknown> = { ...currentMetadata };
|
||||
if ("title" in body) metadata.title = body.title;
|
||||
if ("pinned" in body) metadata.pinned = body.pinned;
|
||||
if (wantsSelection) {
|
||||
const stored = readThreadModelSelection(currentMetadata);
|
||||
if (stored.revision !== body.expected_selection_revision) {
|
||||
return { ok: false as const, current: stored };
|
||||
}
|
||||
metadata.model_selection = parseThreadModelSelection(body.model_selection);
|
||||
metadata.model_selection_revision = stored.revision + 1;
|
||||
}
|
||||
const saved = await deployment.threadClient.threads.update(threadId, { metadata });
|
||||
return { ok: true as const, thread: saved };
|
||||
});
|
||||
if (!updated.ok) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
code: "THREAD_MODEL_SELECTION_CONFLICT",
|
||||
error: "Thread model selection changed; reload and retry.",
|
||||
current: updated.current,
|
||||
},
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
return NextResponse.json({ thread: sanitizeConversationThread(updated.thread) });
|
||||
} catch (error) {
|
||||
return errorResponse(error);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ const mocks = vi.hoisted(() => ({
|
||||
getActiveDeployment: vi.fn(),
|
||||
assertCutoverIsIdle: vi.fn(),
|
||||
assertThreadMatchesScope: vi.fn(),
|
||||
requireActor: vi.fn(() => ({ sub: "tester", role: "admin" as const })),
|
||||
configApiFetch: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("server-only", () => ({}));
|
||||
@@ -17,10 +19,46 @@ vi.mock("@/lib/server/conversationResponse", () => ({
|
||||
assertThreadMatchesScope: mocks.assertThreadMatchesScope,
|
||||
sanitizeConversationData: <T>(value: T) => value,
|
||||
}));
|
||||
vi.mock("@/lib/server/actor", () => ({
|
||||
requireActor: mocks.requireActor,
|
||||
}));
|
||||
vi.mock("@/lib/server/evoscientistConfigClient", () => ({
|
||||
configApiFetch: mocks.configApiFetch,
|
||||
ConfigApiError: class ConfigApiError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
readonly status: number,
|
||||
readonly code: string | null = null,
|
||||
readonly details: unknown[] = []
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
},
|
||||
}));
|
||||
vi.mock("@/lib/server/delegation", () => ({
|
||||
delegationDeploymentId: () => "webui-test",
|
||||
}));
|
||||
|
||||
const routes = await import("./route");
|
||||
const context = { params: Promise.resolve({ threadId: "thread-a" }) };
|
||||
|
||||
let snapshotCounter = 0;
|
||||
|
||||
function mockSnapshotApi() {
|
||||
mocks.configApiFetch.mockImplementation(
|
||||
async (_actor: unknown, path: string, options?: { method?: string }) => {
|
||||
if (path === "/api/runtime-snapshots" && options?.method === "POST") {
|
||||
snapshotCounter += 1;
|
||||
return { status: 201, body: { snapshot_id: `snap-${snapshotCounter}` } };
|
||||
}
|
||||
if (path.endsWith("/bind")) {
|
||||
return { status: 200, body: {} };
|
||||
}
|
||||
return { status: 200, body: {} };
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function legacyDeployment() {
|
||||
return {
|
||||
assistantId: "EvoScientist",
|
||||
@@ -39,7 +77,7 @@ function legacyDeployment() {
|
||||
};
|
||||
}
|
||||
|
||||
function scopedDeployment() {
|
||||
function scopedDeployment(priorRuns: unknown[] = []) {
|
||||
const scope = {
|
||||
deployment_id: "deployment-a",
|
||||
scope_id: "00000000-0000-4000-8000-000000000010",
|
||||
@@ -74,7 +112,7 @@ function scopedDeployment() {
|
||||
.mockResolvedValueOnce({ run_id: "run-initial", status: "pending" })
|
||||
.mockResolvedValueOnce({ run_id: "run-resume", status: "pending" }),
|
||||
get: vi.fn(),
|
||||
list: vi.fn().mockResolvedValue([]),
|
||||
list: vi.fn().mockResolvedValue(priorRuns),
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -83,6 +121,8 @@ function scopedDeployment() {
|
||||
describe("conversation run route", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
snapshotCounter = 0;
|
||||
mockSnapshotApi();
|
||||
});
|
||||
|
||||
it("keeps legacy BFF runs operational without scope metadata", async () => {
|
||||
@@ -119,7 +159,18 @@ describe("conversation run route", () => {
|
||||
metadata: expect.not.objectContaining({
|
||||
workspace_scope_id: expect.anything(),
|
||||
}),
|
||||
config: { configurable: { review_mode: "manual" } },
|
||||
config: {
|
||||
configurable: { review_mode: "manual", runtime_snapshot_id: "snap-1" },
|
||||
},
|
||||
})
|
||||
);
|
||||
// The fresh snapshot is bound to the created run.
|
||||
expect(mocks.configApiFetch).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
"/api/runtime-snapshots/snap-1/bind",
|
||||
expect.objectContaining({
|
||||
method: "POST",
|
||||
body: { langgraph_run_id: "run-a" },
|
||||
})
|
||||
);
|
||||
});
|
||||
@@ -178,11 +229,18 @@ describe("conversation run route", () => {
|
||||
});
|
||||
|
||||
it("creates a distinct run request for an approval resume in the same turn", async () => {
|
||||
const deployment = scopedDeployment();
|
||||
mocks.getActiveDeployment.mockResolvedValue(deployment);
|
||||
const turnId = "00000000-0000-4000-8000-000000000001";
|
||||
const initialRequestId = "00000000-0000-4000-8000-000000000002";
|
||||
const resumeRequestId = "00000000-0000-4000-8000-000000000003";
|
||||
// The resume reuses the turn's frozen snapshot from the initial run's
|
||||
// metadata instead of freezing a fresh one (design doc 7.2/8.2).
|
||||
const deployment = scopedDeployment([
|
||||
{
|
||||
run_id: "run-initial",
|
||||
metadata: { turn_id: turnId, runtime_snapshot_id: "snap-1" },
|
||||
},
|
||||
]);
|
||||
mocks.getActiveDeployment.mockResolvedValue(deployment);
|
||||
const initial = new Request(
|
||||
"http://localhost/api/conversations/thread-a/runs",
|
||||
{
|
||||
@@ -252,5 +310,36 @@ describe("conversation run route", () => {
|
||||
resumeRequestId,
|
||||
"run-resume"
|
||||
);
|
||||
// Initial run froze snap-1; the resume reused it (no second snapshot).
|
||||
expect(deployment.threadClient.runs.create).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
"thread-a",
|
||||
"EvoScientist",
|
||||
expect.objectContaining({
|
||||
config: {
|
||||
configurable: expect.objectContaining({
|
||||
runtime_snapshot_id: "snap-1",
|
||||
}),
|
||||
},
|
||||
})
|
||||
);
|
||||
expect(deployment.threadClient.runs.create).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
"thread-a",
|
||||
"EvoScientist",
|
||||
expect.objectContaining({
|
||||
config: {
|
||||
configurable: expect.objectContaining({
|
||||
runtime_snapshot_id: "snap-1",
|
||||
}),
|
||||
},
|
||||
})
|
||||
);
|
||||
const snapshotCreations = mocks.configApiFetch.mock.calls.filter(
|
||||
([, path, options]) =>
|
||||
path === "/api/runtime-snapshots" &&
|
||||
(options as { method?: string } | undefined)?.method === "POST"
|
||||
);
|
||||
expect(snapshotCreations).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -10,7 +10,15 @@ import {
|
||||
assertThreadMatchesScope,
|
||||
sanitizeConversationData,
|
||||
} from "@/lib/server/conversationResponse";
|
||||
import { getEvoScientistAdminToken } from "@/lib/server/evoscientistAdminProxy";
|
||||
import { requireActor } from "@/lib/server/actor";
|
||||
import {
|
||||
ConfigApiError,
|
||||
configApiFetch,
|
||||
} from "@/lib/server/evoscientistConfigClient";
|
||||
import { delegationDeploymentId } from "@/lib/server/delegation";
|
||||
import { routeErrorResponse } from "@/lib/server/routeErrors";
|
||||
import type { ThreadModelSelection } from "@/lib/modelRegistry";
|
||||
import { readThreadModelSelection } from "@/lib/server/threadModelSelection";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
@@ -34,83 +42,30 @@ function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
type RuntimeSnapshotPublic = {
|
||||
interface SnapshotPublicResponse {
|
||||
snapshot_id: string;
|
||||
provider: string;
|
||||
model: string;
|
||||
profile_revision: string | null;
|
||||
created_at: number;
|
||||
expires_at: number;
|
||||
runtime: Record<string, unknown>;
|
||||
};
|
||||
|
||||
function readRuntimeSnapshot(value: unknown): RuntimeSnapshotPublic | null {
|
||||
if (!isPlainObject(value)) return null;
|
||||
const requiredStrings = ["snapshot_id", "provider", "model"] as const;
|
||||
if (requiredStrings.some((key) => typeof value[key] !== "string"))
|
||||
return null;
|
||||
if (!isPlainObject(value.runtime)) return null;
|
||||
return value as unknown as RuntimeSnapshotPublic;
|
||||
}
|
||||
|
||||
async function priorTurnRuntimeSnapshot(
|
||||
/**
|
||||
* Retries, approval commands, and continuations of the same turn reuse the
|
||||
* original run's frozen snapshot (design doc 7.2/8.2): the snapshot id is
|
||||
* read back from the prior run's metadata.
|
||||
*/
|
||||
async function priorTurnSnapshotId(
|
||||
deployment: Awaited<ReturnType<typeof getActiveDeployment>>,
|
||||
threadId: string,
|
||||
turnId: string
|
||||
): Promise<RuntimeSnapshotPublic | null> {
|
||||
): Promise<string | null> {
|
||||
const runs = await deployment.threadClient.runs.list(threadId, { limit: 50 });
|
||||
for (const run of runs) {
|
||||
const metadata = isPlainObject(run.metadata) ? run.metadata : null;
|
||||
if (metadata?.turn_id !== turnId) continue;
|
||||
const snapshot = readRuntimeSnapshot(metadata.runtime_snapshot);
|
||||
if (snapshot) return snapshot;
|
||||
const snapshotId = metadata.runtime_snapshot_id;
|
||||
if (typeof snapshotId === "string" && snapshotId) return snapshotId;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function createRuntimeSnapshot(
|
||||
deployment: Awaited<ReturnType<typeof getActiveDeployment>>,
|
||||
snapshotId: string,
|
||||
modelOverride: Record<string, unknown> | null
|
||||
): Promise<RuntimeSnapshotPublic | null> {
|
||||
const model = modelOverride?.model;
|
||||
const provider = modelOverride?.model_provider;
|
||||
if (typeof model !== "string" || typeof provider !== "string") return null;
|
||||
|
||||
const token = getEvoScientistAdminToken();
|
||||
if (!token) {
|
||||
throw new Error("Runtime configuration snapshots are unavailable.");
|
||||
}
|
||||
const response = await fetch(
|
||||
new URL("/api/runtime-snapshots", deployment.langgraphApiUrl),
|
||||
{
|
||||
method: "POST",
|
||||
cache: "no-store",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-EvoScientist-Admin-Token": token,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
snapshot_id: snapshotId,
|
||||
model,
|
||||
provider,
|
||||
}),
|
||||
}
|
||||
);
|
||||
const payload = (await response.json().catch(() => null)) as {
|
||||
error?: unknown;
|
||||
snapshot?: unknown;
|
||||
} | null;
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
typeof payload?.error === "string"
|
||||
? payload.error
|
||||
: "Could not create a runtime configuration snapshot."
|
||||
);
|
||||
}
|
||||
return readRuntimeSnapshot(payload?.snapshot);
|
||||
}
|
||||
|
||||
async function scopeForThread(threadId: string) {
|
||||
const deployment = await getActiveDeployment();
|
||||
const thread = await deployment.threadClient.threads.get(threadId);
|
||||
@@ -118,7 +73,7 @@ async function scopeForThread(threadId: string) {
|
||||
if (deployment.isolationMode !== "legacy") {
|
||||
throw new Error("Workspace scope service is unavailable.");
|
||||
}
|
||||
return { deployment, scope: null };
|
||||
return { deployment, thread, scope: null };
|
||||
}
|
||||
const scope = await deployment.scopeRegistry.getByThread(threadId);
|
||||
assertThreadMatchesScope(
|
||||
@@ -129,7 +84,7 @@ async function scopeForThread(threadId: string) {
|
||||
if (scope.state === "deleting" || scope.state === "deleted") {
|
||||
throw new Error("Conversation is not available for new runs.");
|
||||
}
|
||||
return { deployment, scope };
|
||||
return { deployment, thread, scope };
|
||||
}
|
||||
|
||||
export async function GET(_request: NextRequest, context: RouteContext) {
|
||||
@@ -152,11 +107,12 @@ export async function GET(_request: NextRequest, context: RouteContext) {
|
||||
|
||||
export async function POST(request: NextRequest, context: RouteContext) {
|
||||
try {
|
||||
const actor = requireActor(request);
|
||||
const { threadId } = await context.params;
|
||||
const body = await request.json().catch(() => null);
|
||||
if (!isPlainObject(body)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid run request." },
|
||||
{ code: "INVALID_REQUEST", message: "Invalid run request." },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
@@ -167,11 +123,10 @@ export async function POST(request: NextRequest, context: RouteContext) {
|
||||
"input",
|
||||
"command",
|
||||
"review_mode",
|
||||
"model_override",
|
||||
]);
|
||||
if (Object.keys(body).some((key) => !allowed.has(key))) {
|
||||
return NextResponse.json(
|
||||
{ error: "Unsupported run field." },
|
||||
{ code: "INVALID_REQUEST", message: "Unsupported run field." },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
@@ -187,13 +142,13 @@ export async function POST(request: NextRequest, context: RouteContext) {
|
||||
: null;
|
||||
if (interruptKey && interruptKey.length > 256) {
|
||||
return NextResponse.json(
|
||||
{ error: "interrupt_key is too long." },
|
||||
{ code: "INVALID_REQUEST", message: "interrupt_key is too long." },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
if (!isPlainObject(body.input) && !isPlainObject(body.command)) {
|
||||
return NextResponse.json(
|
||||
{ error: "input or command is required." },
|
||||
{ code: "INVALID_REQUEST", message: "input or command is required." },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
@@ -220,9 +175,6 @@ export async function POST(request: NextRequest, context: RouteContext) {
|
||||
input: isPlainObject(body.input) ? body.input : null,
|
||||
command: isPlainObject(body.command) ? body.command : null,
|
||||
review_mode: body.review_mode ?? null,
|
||||
model_override: isPlainObject(body.model_override)
|
||||
? body.model_override
|
||||
: null,
|
||||
});
|
||||
const reservation = scope
|
||||
? await deployment.scopeRegistry!.reserveRun(scope.scope_id, {
|
||||
@@ -245,25 +197,41 @@ export async function POST(request: NextRequest, context: RouteContext) {
|
||||
runRequestId,
|
||||
});
|
||||
}
|
||||
const requestedOverride = isPlainObject(body.model_override)
|
||||
? body.model_override
|
||||
|
||||
// --- run snapshot (design doc 8.2) --------------------------------------
|
||||
// A command resume reuses the turn's original snapshot; a new turn
|
||||
// freezes the thread's current model selection into a fresh snapshot.
|
||||
let snapshotId = body.command
|
||||
? await priorTurnSnapshotId(deployment, threadId, turnId)
|
||||
: null;
|
||||
const priorSnapshot = body.command
|
||||
? await priorTurnRuntimeSnapshot(deployment, threadId, turnId)
|
||||
: null;
|
||||
const runtimeSnapshot =
|
||||
priorSnapshot ??
|
||||
(await createRuntimeSnapshot(
|
||||
deployment,
|
||||
runRequestId,
|
||||
requestedOverride
|
||||
));
|
||||
const effectiveOverride = runtimeSnapshot
|
||||
? {
|
||||
model: runtimeSnapshot.model,
|
||||
model_provider: runtimeSnapshot.provider,
|
||||
}
|
||||
: requestedOverride;
|
||||
let freshSnapshot = false;
|
||||
if (!snapshotId) {
|
||||
const selection = readThreadModelSelection(
|
||||
resolved.thread.metadata as Record<string, unknown> | undefined
|
||||
);
|
||||
const selectionRef: ThreadModelSelection = selection.selection;
|
||||
const { body: snapshot } =
|
||||
await configApiFetch<SnapshotPublicResponse>(
|
||||
actor,
|
||||
"/api/runtime-snapshots",
|
||||
{
|
||||
method: "POST",
|
||||
threadId,
|
||||
body: {
|
||||
run_request_id: runRequestId,
|
||||
thread_id: threadId,
|
||||
deployment_id: delegationDeploymentId(),
|
||||
model_selection_revision: selection.revision,
|
||||
primary:
|
||||
selectionRef === "inherit" ? null : selectionRef.primary,
|
||||
auxiliary:
|
||||
selectionRef === "inherit" ? null : selectionRef.auxiliary,
|
||||
},
|
||||
}
|
||||
);
|
||||
snapshotId = snapshot.snapshot_id;
|
||||
freshSnapshot = true;
|
||||
}
|
||||
|
||||
const metadata = {
|
||||
turn_id: turnId,
|
||||
@@ -271,7 +239,7 @@ export async function POST(request: NextRequest, context: RouteContext) {
|
||||
...(interruptKey ? { interrupt_key: interruptKey } : {}),
|
||||
request_hash: hash,
|
||||
review_mode: body.review_mode,
|
||||
...(runtimeSnapshot ? { runtime_snapshot: runtimeSnapshot } : {}),
|
||||
runtime_snapshot_id: snapshotId,
|
||||
...(scope && reservation
|
||||
? {
|
||||
workspace_scope_id: scope.scope_id,
|
||||
@@ -292,23 +260,46 @@ export async function POST(request: NextRequest, context: RouteContext) {
|
||||
}
|
||||
: {}),
|
||||
...(body.review_mode ? { review_mode: body.review_mode } : {}),
|
||||
...(effectiveOverride ?? {}),
|
||||
...(runtimeSnapshot
|
||||
? { runtime_snapshot_id: runtimeSnapshot.snapshot_id }
|
||||
: {}),
|
||||
runtime_snapshot_id: snapshotId,
|
||||
},
|
||||
};
|
||||
const run = await deployment.threadClient.runs.create(
|
||||
threadId,
|
||||
deployment.assistantId,
|
||||
{
|
||||
input: isPlainObject(body.input) ? body.input : undefined,
|
||||
command: isPlainObject(body.command) ? body.command : undefined,
|
||||
metadata,
|
||||
config,
|
||||
multitaskStrategy: "enqueue",
|
||||
let run;
|
||||
try {
|
||||
run = await deployment.threadClient.runs.create(
|
||||
threadId,
|
||||
deployment.assistantId,
|
||||
{
|
||||
input: isPlainObject(body.input) ? body.input : undefined,
|
||||
command: isPlainObject(body.command) ? body.command : undefined,
|
||||
metadata,
|
||||
config,
|
||||
multitaskStrategy: "enqueue",
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
if (freshSnapshot) {
|
||||
await configApiFetch(
|
||||
actor,
|
||||
`/api/runtime-snapshots/${encodeURIComponent(snapshotId)}`,
|
||||
{ method: "DELETE", threadId }
|
||||
).catch(() => undefined);
|
||||
}
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
if (freshSnapshot) {
|
||||
try {
|
||||
await configApiFetch(
|
||||
actor,
|
||||
`/api/runtime-snapshots/${encodeURIComponent(snapshotId)}/bind`,
|
||||
{ method: "POST", threadId, body: { langgraph_run_id: run.run_id } }
|
||||
);
|
||||
} catch (error) {
|
||||
await deployment.threadClient.runs
|
||||
.cancel(threadId, run.run_id)
|
||||
.catch(() => undefined);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
if (scope && reservation) {
|
||||
await deployment.scopeRegistry!.bindRun(
|
||||
scope.scope_id,
|
||||
@@ -336,6 +327,9 @@ export async function POST(request: NextRequest, context: RouteContext) {
|
||||
{ status: error.status }
|
||||
);
|
||||
}
|
||||
if (error instanceof ConfigApiError) {
|
||||
return routeErrorResponse(error);
|
||||
}
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: error instanceof Error ? error.message : "Failed to create run.",
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import { requireActor, requireAdmin } from "@/lib/server/actor";
|
||||
import { configApiFetch } from "@/lib/server/evoscientistConfigClient";
|
||||
import { isCrossOrigin } from "@/lib/server/workspace";
|
||||
import { NO_STORE, routeErrorResponse } from "@/lib/server/routeErrors";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
type RouteContext = { params: Promise<{ credentialId: string }> };
|
||||
|
||||
export async function PUT(request: NextRequest, context: RouteContext) {
|
||||
try {
|
||||
if (isCrossOrigin(request)) {
|
||||
return NextResponse.json(
|
||||
{ code: "FORBIDDEN", message: "Cross-origin access is not allowed." },
|
||||
{ status: 403, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const actor = requireActor(request);
|
||||
requireAdmin(actor);
|
||||
const { credentialId } = await context.params;
|
||||
if (!/^[a-z0-9][a-z0-9._-]{0,63}$/.test(credentialId)) {
|
||||
return NextResponse.json(
|
||||
{ code: "INVALID_REQUEST", message: "Invalid credential id." },
|
||||
{ status: 400, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const payload = (await request.json().catch(() => null)) as {
|
||||
operation?: unknown;
|
||||
secret_value?: unknown;
|
||||
} | null;
|
||||
if (
|
||||
payload?.operation !== "replace" ||
|
||||
typeof payload.secret_value !== "string" ||
|
||||
!payload.secret_value
|
||||
) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
code: "INVALID_REQUEST",
|
||||
message: "operation 'replace' and a non-empty secret_value are required.",
|
||||
},
|
||||
{ status: 400, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const { body } = await configApiFetch(
|
||||
actor,
|
||||
`/api/model-registry/credentials/${encodeURIComponent(credentialId)}`,
|
||||
{
|
||||
method: "PUT",
|
||||
body: { operation: "replace", secret_value: payload.secret_value },
|
||||
}
|
||||
);
|
||||
return NextResponse.json(body, { headers: NO_STORE });
|
||||
} catch (error) {
|
||||
return routeErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import { requireActor, requireAdmin } from "@/lib/server/actor";
|
||||
import { configApiFetch } from "@/lib/server/evoscientistConfigClient";
|
||||
import { isCrossOrigin } from "@/lib/server/workspace";
|
||||
import { NO_STORE, routeErrorResponse } from "@/lib/server/routeErrors";
|
||||
import type {
|
||||
GetModelRegistryResponse,
|
||||
PutModelRegistryRequest,
|
||||
} from "@/lib/modelRegistry";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
if (isCrossOrigin(request)) {
|
||||
return NextResponse.json(
|
||||
{ code: "FORBIDDEN", message: "Cross-origin access is not allowed." },
|
||||
{ status: 403, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const actor = requireActor(request);
|
||||
requireAdmin(actor);
|
||||
const { body } = await configApiFetch<GetModelRegistryResponse>(
|
||||
actor,
|
||||
"/api/model-registry"
|
||||
);
|
||||
return NextResponse.json(body, { headers: NO_STORE });
|
||||
} catch (error) {
|
||||
return routeErrorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: NextRequest) {
|
||||
try {
|
||||
if (isCrossOrigin(request)) {
|
||||
return NextResponse.json(
|
||||
{ code: "FORBIDDEN", message: "Cross-origin access is not allowed." },
|
||||
{ status: 403, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const actor = requireActor(request);
|
||||
requireAdmin(actor);
|
||||
const payload = (await request.json().catch(() => null)) as
|
||||
| (PutModelRegistryRequest & { credential_writes?: unknown })
|
||||
| null;
|
||||
if (
|
||||
!payload ||
|
||||
typeof payload.expected_revision !== "number" ||
|
||||
typeof payload.registry !== "object" ||
|
||||
payload.registry === null
|
||||
) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
code: "INVALID_REQUEST",
|
||||
message: "expected_revision and registry are required.",
|
||||
},
|
||||
{ status: 400, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const body: PutModelRegistryRequest = {
|
||||
expected_revision: payload.expected_revision,
|
||||
registry: payload.registry,
|
||||
...(Array.isArray(payload.credential_writes)
|
||||
? {
|
||||
credential_writes:
|
||||
payload.credential_writes as PutModelRegistryRequest["credential_writes"],
|
||||
}
|
||||
: {}),
|
||||
};
|
||||
const { body: saved } = await configApiFetch<GetModelRegistryResponse>(
|
||||
actor,
|
||||
"/api/model-registry",
|
||||
{ method: "PUT", body }
|
||||
);
|
||||
return NextResponse.json(saved, { headers: NO_STORE });
|
||||
} catch (error) {
|
||||
return routeErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import { requireActor, requireAdmin } from "@/lib/server/actor";
|
||||
import { configApiFetch } from "@/lib/server/evoscientistConfigClient";
|
||||
import { isCrossOrigin } from "@/lib/server/workspace";
|
||||
import { NO_STORE, routeErrorResponse } from "@/lib/server/routeErrors";
|
||||
import type {
|
||||
ProviderTestRequest,
|
||||
ProviderTestResponse,
|
||||
} from "@/lib/modelRegistry";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
if (isCrossOrigin(request)) {
|
||||
return NextResponse.json(
|
||||
{ code: "FORBIDDEN", message: "Cross-origin access is not allowed." },
|
||||
{ status: 403, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const actor = requireActor(request);
|
||||
requireAdmin(actor);
|
||||
const payload = (await request.json().catch(() => null)) as
|
||||
| ProviderTestRequest
|
||||
| null;
|
||||
if (
|
||||
!payload ||
|
||||
typeof payload.expected_registry_revision !== "number" ||
|
||||
typeof payload.model_ref?.provider_id !== "string" ||
|
||||
typeof payload.model_ref?.model_key !== "string"
|
||||
) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
code: "INVALID_REQUEST",
|
||||
message: "expected_registry_revision and model_ref are required.",
|
||||
},
|
||||
{ status: 400, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const { body } = await configApiFetch<ProviderTestResponse>(
|
||||
actor,
|
||||
"/api/model-registry/test",
|
||||
{ method: "POST", body: payload }
|
||||
);
|
||||
return NextResponse.json(body, { headers: NO_STORE });
|
||||
} catch (error) {
|
||||
return routeErrorResponse(error);
|
||||
}
|
||||
}
|
||||
+20
-28
@@ -1,39 +1,31 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import { requireActor } from "@/lib/server/actor";
|
||||
import { configApiFetch } from "@/lib/server/evoscientistConfigClient";
|
||||
import { isCrossOrigin } from "@/lib/server/workspace";
|
||||
import { getActiveDeployment } from "@/lib/server/activeDeployment";
|
||||
import { NO_STORE, routeErrorResponse } from "@/lib/server/routeErrors";
|
||||
import type { GetSelectableModelsResponse } from "@/lib/modelRegistry";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
function fail(error: unknown, status = 400) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: "Model registry request failed.",
|
||||
},
|
||||
{ status }
|
||||
);
|
||||
}
|
||||
|
||||
/** Selectable enabled models for the current user (design doc 9.1). Any
|
||||
* authenticated user may call this; the backend enforces the model:select
|
||||
* scope from the delegation JWT. */
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
if (isCrossOrigin(request)) {
|
||||
return fail("Cross-origin model registry access is not allowed.", 403);
|
||||
return NextResponse.json(
|
||||
{ code: "FORBIDDEN", message: "Cross-origin access is not allowed." },
|
||||
{ status: 403, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const deployment = await getActiveDeployment();
|
||||
const upstream = await fetch(new URL("/api/models", deployment.langgraphApiUrl), {
|
||||
cache: "no-store",
|
||||
});
|
||||
const body = await upstream.text();
|
||||
return new NextResponse(body, {
|
||||
status: upstream.status,
|
||||
headers: {
|
||||
"content-type":
|
||||
upstream.headers.get("content-type") ?? "application/json",
|
||||
},
|
||||
});
|
||||
const actor = requireActor(request);
|
||||
const { body } = await configApiFetch<GetSelectableModelsResponse>(
|
||||
actor,
|
||||
"/api/models"
|
||||
);
|
||||
return NextResponse.json(body, { headers: NO_STORE });
|
||||
} catch (error) {
|
||||
return fail(error);
|
||||
return routeErrorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,8 +29,6 @@ import {
|
||||
GripVertical,
|
||||
Ellipsis,
|
||||
ListX,
|
||||
LoaderCircle,
|
||||
Star,
|
||||
Gauge,
|
||||
Hand,
|
||||
Zap,
|
||||
@@ -103,12 +101,11 @@ import { WorkspaceFileDialog } from "@/app/components/WorkspaceFileDialog";
|
||||
import { MemoryFileDialog } from "@/app/components/MemoryFileDialog";
|
||||
import { FILE_LINK_EVENT, type FileLinkEventDetail } from "@/lib/fileLink";
|
||||
import { notifyWorkspaceChanged } from "@/lib/workspaceEvents";
|
||||
import { parseModelCommand, type ModelOverride } from "@/lib/modelCommand";
|
||||
import type { ModelRef } from "@/lib/modelRegistry";
|
||||
import {
|
||||
invalidateAvailableModels,
|
||||
modelRefLabel,
|
||||
useAvailableModels,
|
||||
} from "@/app/hooks/useAvailableModels";
|
||||
import { setDefaultModel } from "@/lib/defaultModel";
|
||||
import { formatTokenCount, useUsage } from "@/app/hooks/useUsage";
|
||||
import { UsageDetailsDialog } from "@/app/components/UsageDetailsDialog";
|
||||
|
||||
@@ -442,16 +439,13 @@ export const ChatInterface = React.memo<ChatInterfaceProps>(
|
||||
const [modelPickerOpen, setModelPickerOpen] = useState(false);
|
||||
const [usageDialogOpen, setUsageDialogOpen] = useState(false);
|
||||
const [modelSearch, setModelSearch] = useState("");
|
||||
const [settingDefaultKey, setSettingDefaultKey] = useState<string | null>(
|
||||
null
|
||||
);
|
||||
// Reset the search box every time the picker opens — stale filter state
|
||||
// surviving across opens would surprise the user.
|
||||
useEffect(() => {
|
||||
if (modelPickerOpen) setModelSearch("");
|
||||
}, [modelPickerOpen]);
|
||||
const {
|
||||
registry: modelRegistry,
|
||||
models: selectableModels,
|
||||
loading: modelRegistryLoading,
|
||||
error: modelRegistryError,
|
||||
} = useAvailableModels();
|
||||
@@ -460,33 +454,26 @@ export const ChatInterface = React.memo<ChatInterfaceProps>(
|
||||
useEffect(() => {
|
||||
if (modelRegistryUnavailable) {
|
||||
console.warn(
|
||||
"[model picker] configured model registry did not load",
|
||||
"[model picker] selectable model list did not load",
|
||||
modelRegistryError
|
||||
);
|
||||
}
|
||||
}, [modelRegistryError, modelRegistryUnavailable]);
|
||||
const pickerModels = useMemo(
|
||||
() =>
|
||||
modelRegistry.entries.map((entry) => ({
|
||||
model: entry.name,
|
||||
model_provider: entry.provider,
|
||||
})),
|
||||
[modelRegistry.entries]
|
||||
);
|
||||
// Case-insensitive substring filter on name + provider. Fuzzy match was
|
||||
// discussed but punted — substring catches the common "I know roughly
|
||||
// what I want" case and keeps the picker behavior predictable.
|
||||
const filteredPickerModels = useMemo(() => {
|
||||
const q = modelSearch.trim().toLowerCase();
|
||||
if (!q) return pickerModels;
|
||||
return pickerModels.filter((m) => {
|
||||
const provider = m.model_provider ?? "";
|
||||
if (!q) return selectableModels;
|
||||
return selectableModels.filter((m) => {
|
||||
return (
|
||||
m.model.toLowerCase().includes(q) ||
|
||||
provider.toLowerCase().includes(q)
|
||||
m.name.toLowerCase().includes(q) ||
|
||||
m.provider_name.toLowerCase().includes(q) ||
|
||||
m.model_ref.model_key.toLowerCase().includes(q) ||
|
||||
m.model_ref.provider_id.toLowerCase().includes(q)
|
||||
);
|
||||
});
|
||||
}, [pickerModels, modelSearch]);
|
||||
}, [selectableModels, modelSearch]);
|
||||
const autoApprovedRef = useRef<string | null>(null);
|
||||
const autoApproveResumePendingRef = useRef(false);
|
||||
const autoAnsweredAskUserRef = useRef<string | null>(null);
|
||||
@@ -516,8 +503,8 @@ export const ChatInterface = React.memo<ChatInterfaceProps>(
|
||||
subAgentActivity,
|
||||
asyncTasks,
|
||||
summarizationEvent,
|
||||
modelOverride,
|
||||
setModelOverride,
|
||||
modelSelection,
|
||||
setModelSelection,
|
||||
} = useChatContext();
|
||||
const usage = useUsage(threadId);
|
||||
const usageUnavailable =
|
||||
@@ -531,29 +518,6 @@ export const ChatInterface = React.memo<ChatInterfaceProps>(
|
||||
? "Tokens unavailable"
|
||||
: `${formatTokenCount(usage.summary.total_tokens)} tokens`;
|
||||
|
||||
const handleSetDefaultModel = useCallback(
|
||||
async (model: string, provider: string) => {
|
||||
const key = `${model}|${provider}`;
|
||||
setSettingDefaultKey(key);
|
||||
try {
|
||||
await setDefaultModel(model, provider);
|
||||
await setModelOverride({ model, model_provider: provider });
|
||||
invalidateAvailableModels();
|
||||
toast.success(`Default model set to ${model} (${provider}).`);
|
||||
setModelPickerOpen(false);
|
||||
} catch (err) {
|
||||
toast.error(
|
||||
err instanceof Error
|
||||
? `Couldn't set default model: ${err.message}`
|
||||
: "Couldn't set default model — try again."
|
||||
);
|
||||
} finally {
|
||||
setSettingDefaultKey(null);
|
||||
}
|
||||
},
|
||||
[setModelOverride]
|
||||
);
|
||||
|
||||
// Count of background async sub-agents (writing / data-analysis) still
|
||||
// running — drives the composer's "agents running" pulse. We poll each task's
|
||||
// REAL run status (via useAsyncAgents) rather than trusting the conversation
|
||||
@@ -741,40 +705,24 @@ export const ChatInterface = React.memo<ChatInterfaceProps>(
|
||||
}, []);
|
||||
|
||||
// What model will the next turn use? The pill is forward-looking — it
|
||||
// updates the moment the user changes (or clears) the override, instead
|
||||
// of lingering on what just ran. Priority:
|
||||
// 1. Per-thread model override (`/model` command, picker) — the next
|
||||
// run will use this verbatim.
|
||||
// 2. The model registry endpoint's reported deployment default —
|
||||
// what the next run will use when no override is set.
|
||||
// 3. The assistant's configured default from `assistant.config.configurable`.
|
||||
// 4. Last AI message's `response_metadata.model_name` — final fallback
|
||||
// when none of the above are available (older deployment without
|
||||
// `/api/models`).
|
||||
// The endpoint's `default` can disagree with what the runtime actually
|
||||
// boots with; that's a backend honesty problem and tracked separately.
|
||||
// updates the moment the user changes the thread selection. Priority:
|
||||
// 1. Explicit per-thread selection (ModelRef picker) — the next run
|
||||
// freezes exactly this into its runtime snapshot.
|
||||
// 2. `inherit` — the registry default resolved server-side at snapshot
|
||||
// creation; the pill shows the last observed model as a hint.
|
||||
// Token/context usage is intentionally NOT shown — the backend doesn't
|
||||
// persist usage_metadata, so it isn't reliably available here.
|
||||
const currentModel = useMemo(() => {
|
||||
if (modelOverride) {
|
||||
return formatModel(modelOverride.model, modelOverride.model_provider);
|
||||
}
|
||||
if (modelRegistry.defaultEntry) {
|
||||
return formatModel(
|
||||
modelRegistry.defaultEntry.name,
|
||||
modelRegistry.defaultEntry.provider ?? undefined
|
||||
if (modelSelection !== "inherit") {
|
||||
const match = selectableModels.find(
|
||||
(m) =>
|
||||
m.model_ref.provider_id === modelSelection.primary.provider_id &&
|
||||
m.model_ref.model_key === modelSelection.primary.model_key
|
||||
);
|
||||
}
|
||||
const cfg = assistant?.config as
|
||||
| { configurable?: Record<string, unknown> }
|
||||
| undefined;
|
||||
const configurable = cfg?.configurable;
|
||||
if (configurable) {
|
||||
const info = formatModel(
|
||||
configurable.model ?? configurable.model_name,
|
||||
configurable.model_provider
|
||||
);
|
||||
if (info) return info;
|
||||
return {
|
||||
name: match?.name ?? modelSelection.primary.model_key,
|
||||
provider: match?.provider_name || modelSelection.primary.provider_id,
|
||||
};
|
||||
}
|
||||
for (let i = messages.length - 1; i >= 0; i--) {
|
||||
const m = messages[i];
|
||||
@@ -787,12 +735,7 @@ export const ChatInterface = React.memo<ChatInterfaceProps>(
|
||||
if (info) return info;
|
||||
}
|
||||
return null;
|
||||
}, [
|
||||
messages,
|
||||
modelOverride,
|
||||
assistant?.config,
|
||||
modelRegistry.defaultEntry,
|
||||
]);
|
||||
}, [messages, modelSelection, selectableModels]);
|
||||
|
||||
// Bind captured sub-agent activity (keyed by subgraph namespace) to each task
|
||||
// tool call → its live steps. B': match a finished sub-agent to a task by its
|
||||
@@ -1067,49 +1010,6 @@ export const ChatInterface = React.memo<ChatInterfaceProps>(
|
||||
);
|
||||
}, []);
|
||||
|
||||
// Apply a parsed `/model` command. Three forms:
|
||||
// - show → open the model picker dialog
|
||||
// - reset → clear the per-thread override (revert to assistant default)
|
||||
// - set → persist the new override and confirm via toast. Names that
|
||||
// aren't in our curated list are still accepted — the backend
|
||||
// accepts anything `init_chat_model` knows, and we'd rather
|
||||
// not block a power user on our curation lagging the registry.
|
||||
const applyModelCommand = useCallback(
|
||||
async (cmd: ReturnType<typeof parseModelCommand>) => {
|
||||
if (!cmd) return;
|
||||
if (cmd.kind === "show") {
|
||||
setModelPickerOpen(true);
|
||||
return;
|
||||
}
|
||||
// Pre-thread picks are staged in useChat and applied to the first
|
||||
// run; no threadId guard needed.
|
||||
try {
|
||||
if (cmd.kind === "reset") {
|
||||
await setModelOverride(null);
|
||||
toast.success("Model override cleared.");
|
||||
} else {
|
||||
const next: ModelOverride = {
|
||||
model: cmd.model,
|
||||
...(cmd.provider ? { model_provider: cmd.provider } : {}),
|
||||
};
|
||||
await setModelOverride(next);
|
||||
toast.success(
|
||||
`Model set to ${cmd.model}${
|
||||
cmd.provider ? ` (${cmd.provider})` : ""
|
||||
}.`
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
toast.error(
|
||||
err instanceof Error
|
||||
? `Couldn't update model: ${err.message}`
|
||||
: "Couldn't update model — try again."
|
||||
);
|
||||
}
|
||||
},
|
||||
[setModelOverride]
|
||||
);
|
||||
|
||||
const handleSubmit = useCallback(
|
||||
(e?: FormEvent) => {
|
||||
if (e) {
|
||||
@@ -1117,13 +1017,11 @@ export const ChatInterface = React.memo<ChatInterfaceProps>(
|
||||
}
|
||||
const messageText = input.trim();
|
||||
if (!messageText) return;
|
||||
// Intercept `/model` before the agent sees it. The choice is stored on
|
||||
// the thread and remembered as the starting model for future new chats.
|
||||
// The textarea clears regardless of outcome so the command echo does
|
||||
// not linger.
|
||||
const modelCmd = parseModelCommand(messageText);
|
||||
if (modelCmd) {
|
||||
void applyModelCommand(modelCmd);
|
||||
// Bare `/model` opens the ModelRef picker; the free-string
|
||||
// `/model <name>` switching command was removed with the legacy
|
||||
// model_override mechanism (design doc 7.2).
|
||||
if (messageText === "/model") {
|
||||
setModelPickerOpen(true);
|
||||
setInput("");
|
||||
return;
|
||||
}
|
||||
@@ -1157,7 +1055,6 @@ export const ChatInterface = React.memo<ChatInterfaceProps>(
|
||||
},
|
||||
[
|
||||
input,
|
||||
applyModelCommand,
|
||||
assistant,
|
||||
hasPendingInterrupt,
|
||||
reviewMode,
|
||||
@@ -1651,23 +1548,26 @@ export const ChatInterface = React.memo<ChatInterfaceProps>(
|
||||
<DialogHeader>
|
||||
<DialogTitle>Choose model</DialogTitle>
|
||||
<DialogDescription>
|
||||
Saved for this chat and new chats in this browser.
|
||||
Saved for this chat; the next message freezes it into the
|
||||
run’s snapshot.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="mt-2 space-y-2">
|
||||
<div className="text-xs text-muted-foreground">
|
||||
Current:{" "}
|
||||
<span className="font-mono">
|
||||
{currentModel
|
||||
{modelSelection !== "inherit"
|
||||
? modelRefLabel(modelSelection.primary)
|
||||
: currentModel
|
||||
? `${currentModel.name}${
|
||||
currentModel.provider
|
||||
? ` (${currentModel.provider})`
|
||||
: ""
|
||||
}`
|
||||
: "deployment default"}
|
||||
} · default`
|
||||
: "registry default"}
|
||||
</span>
|
||||
{modelRegistryLoading && (
|
||||
<span className="ml-2 italic">loading registry…</span>
|
||||
<span className="ml-2 italic">loading models…</span>
|
||||
)}
|
||||
</div>
|
||||
{modelRegistryUnavailable && (
|
||||
@@ -1679,9 +1579,8 @@ export const ChatInterface = React.memo<ChatInterfaceProps>(
|
||||
{"\u26A0"}
|
||||
</span>
|
||||
<span>
|
||||
The deployment’s configured
|
||||
<span className="mx-1 font-mono">/api/models</span>
|
||||
registry didn’t load
|
||||
The deployment’s selectable model list didn’t
|
||||
load
|
||||
{modelRegistryError ? ` (${modelRegistryError})` : ""}.
|
||||
</span>
|
||||
</div>
|
||||
@@ -1699,19 +1598,16 @@ export const ChatInterface = React.memo<ChatInterfaceProps>(
|
||||
<li className="px-3 py-2 text-xs text-muted-foreground">
|
||||
{modelSearch
|
||||
? `No models match "${modelSearch}".`
|
||||
: "No models are configured."}
|
||||
: "No models are enabled."}
|
||||
</li>
|
||||
)}
|
||||
{filteredPickerModels.map((m) => {
|
||||
const isActive =
|
||||
modelOverride?.model === m.model &&
|
||||
modelOverride?.model_provider === m.model_provider;
|
||||
const isDefault =
|
||||
modelRegistry.defaultEntry?.name === m.model &&
|
||||
(modelRegistry.defaultEntry?.provider ?? null) ===
|
||||
(m.model_provider ?? null);
|
||||
const modelKey = `${m.model}|${m.model_provider ?? ""}`;
|
||||
const isSettingDefault = settingDefaultKey === modelKey;
|
||||
modelSelection !== "inherit" &&
|
||||
modelSelection.primary.provider_id ===
|
||||
m.model_ref.provider_id &&
|
||||
modelSelection.primary.model_key === m.model_ref.model_key;
|
||||
const modelKey = modelRefLabel(m.model_ref);
|
||||
return (
|
||||
<li
|
||||
key={modelKey}
|
||||
@@ -1723,18 +1619,16 @@ export const ChatInterface = React.memo<ChatInterfaceProps>(
|
||||
<button
|
||||
type="button"
|
||||
onClick={async () => {
|
||||
const ref: ModelRef = {
|
||||
provider_id: m.model_ref.provider_id,
|
||||
model_key: m.model_ref.model_key,
|
||||
};
|
||||
try {
|
||||
await setModelOverride({
|
||||
model: m.model,
|
||||
...(m.model_provider
|
||||
? { model_provider: m.model_provider }
|
||||
: {}),
|
||||
await setModelSelection({
|
||||
primary: ref,
|
||||
auxiliary: null,
|
||||
});
|
||||
toast.success(
|
||||
`Model set to ${m.model}${
|
||||
m.model_provider ? ` (${m.model_provider})` : ""
|
||||
}.`
|
||||
);
|
||||
toast.success(`Model set to ${modelKey}.`);
|
||||
setModelPickerOpen(false);
|
||||
} catch (err) {
|
||||
toast.error(
|
||||
@@ -1745,58 +1639,15 @@ export const ChatInterface = React.memo<ChatInterfaceProps>(
|
||||
}
|
||||
}}
|
||||
className={cn(
|
||||
"flex min-w-0 flex-1 items-center justify-between gap-3 rounded-l px-3 py-2 text-left text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring"
|
||||
"flex min-w-0 flex-1 items-center justify-between gap-3 rounded px-3 py-2 text-left text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring"
|
||||
)}
|
||||
>
|
||||
<span className="min-w-0 truncate font-mono">
|
||||
{m.model}
|
||||
{isDefault && (
|
||||
<span className="ml-2 text-xs font-normal text-muted-foreground">
|
||||
· default
|
||||
</span>
|
||||
)}
|
||||
{m.name}
|
||||
</span>
|
||||
<span className="shrink-0 text-xs text-muted-foreground">
|
||||
{m.provider_name || m.model_ref.provider_id}
|
||||
</span>
|
||||
{m.model_provider && (
|
||||
<span className="shrink-0 text-xs text-muted-foreground">
|
||||
{m.model_provider}
|
||||
</span>
|
||||
)}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
aria-label={
|
||||
isDefault
|
||||
? `${m.model} is the default model`
|
||||
: `Set ${m.model} as default`
|
||||
}
|
||||
title={
|
||||
isDefault ? "Default model" : "Set as default model"
|
||||
}
|
||||
disabled={
|
||||
!m.model_provider ||
|
||||
isDefault ||
|
||||
settingDefaultKey !== null
|
||||
}
|
||||
onClick={() => {
|
||||
if (m.model_provider) {
|
||||
void handleSetDefaultModel(
|
||||
m.model,
|
||||
m.model_provider
|
||||
);
|
||||
}
|
||||
}}
|
||||
className="flex w-10 shrink-0 items-center justify-center rounded-r text-muted-foreground hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring disabled:cursor-default disabled:opacity-70"
|
||||
>
|
||||
{isSettingDefault ? (
|
||||
<LoaderCircle className="size-4 animate-spin" />
|
||||
) : (
|
||||
<Star
|
||||
className={cn(
|
||||
"size-4",
|
||||
isDefault && "fill-current text-[var(--brand)]"
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
</button>
|
||||
</li>
|
||||
);
|
||||
@@ -1808,18 +1659,18 @@ export const ChatInterface = React.memo<ChatInterfaceProps>(
|
||||
variant="outline"
|
||||
onClick={async () => {
|
||||
try {
|
||||
await setModelOverride(null);
|
||||
toast.success("Model override cleared.");
|
||||
await setModelSelection("inherit");
|
||||
toast.success("Model reset to the registry default.");
|
||||
setModelPickerOpen(false);
|
||||
} catch (err) {
|
||||
toast.error(
|
||||
err instanceof Error
|
||||
? `Couldn't clear override: ${err.message}`
|
||||
: "Couldn't clear override — try again."
|
||||
? `Couldn't reset model: ${err.message}`
|
||||
: "Couldn't reset model — try again."
|
||||
);
|
||||
}
|
||||
}}
|
||||
disabled={!modelOverride}
|
||||
disabled={modelSelection === "inherit"}
|
||||
>
|
||||
Reset to default
|
||||
</Button>
|
||||
|
||||
@@ -1,116 +1,95 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import type { ModelRef, SelectableModel } from "@/lib/modelRegistry";
|
||||
|
||||
export interface ModelRegistryEntry {
|
||||
/** Short name as the user types in `/model <name>`. */
|
||||
name: string;
|
||||
/** Provider-specific model id (often equal to `name`). Informational only;
|
||||
* the WebUI doesn't pass it to the run config. */
|
||||
model_id: string;
|
||||
/** Provider key the backend uses to route the call. */
|
||||
provider: string;
|
||||
}
|
||||
const EMPTY: SelectableModel[] = [];
|
||||
|
||||
export interface ModelRegistry {
|
||||
entries: ReadonlyArray<ModelRegistryEntry>;
|
||||
/** Effective default reported by the deployment. This is the persisted pair
|
||||
* when available, otherwise the first enabled registry model. May be null
|
||||
* when no model is configured or on older deployments. */
|
||||
defaultEntry: { name: string; provider: string | null } | null;
|
||||
}
|
||||
|
||||
interface RegistryResponse {
|
||||
entries?: unknown;
|
||||
default?: unknown;
|
||||
}
|
||||
|
||||
const EMPTY: ModelRegistry = { entries: [], defaultEntry: null };
|
||||
|
||||
let cachedRegistry: Promise<ModelRegistry> | null = null;
|
||||
let cachedModels: Promise<SelectableModel[]> | null = null;
|
||||
const MODELS_CHANGED_EVENT = "evoscientist-models-changed";
|
||||
|
||||
export function invalidateAvailableModels(): void {
|
||||
cachedRegistry = null;
|
||||
cachedModels = null;
|
||||
if (typeof window !== "undefined") {
|
||||
window.dispatchEvent(new Event(MODELS_CHANGED_EVENT));
|
||||
}
|
||||
}
|
||||
|
||||
function fetchRegistry(): Promise<ModelRegistry> {
|
||||
if (cachedRegistry) return cachedRegistry;
|
||||
function isModelRef(value: unknown): value is ModelRef {
|
||||
if (!value || typeof value !== "object") return false;
|
||||
const ref = value as { provider_id?: unknown; model_key?: unknown };
|
||||
return (
|
||||
typeof ref.provider_id === "string" &&
|
||||
ref.provider_id.length > 0 &&
|
||||
typeof ref.model_key === "string" &&
|
||||
ref.model_key.length > 0
|
||||
);
|
||||
}
|
||||
|
||||
const fetchJson = async (url: string): Promise<RegistryResponse> => {
|
||||
const r = await fetch(url);
|
||||
if (!r.ok) throw new Error(`HTTP ${r.status}`);
|
||||
return (await r.json()) as RegistryResponse;
|
||||
function parseSelectableModel(raw: unknown): SelectableModel | null {
|
||||
if (!raw || typeof raw !== "object") return null;
|
||||
const entry = raw as {
|
||||
model_ref?: unknown;
|
||||
name?: unknown;
|
||||
provider_name?: unknown;
|
||||
effective_capabilities?: unknown;
|
||||
};
|
||||
if (!isModelRef(entry.model_ref) || typeof entry.name !== "string") {
|
||||
return null;
|
||||
}
|
||||
const caps = entry.effective_capabilities as
|
||||
| { tools?: unknown; vision?: unknown; structured_output?: unknown }
|
||||
| undefined;
|
||||
return {
|
||||
model_ref: {
|
||||
provider_id: entry.model_ref.provider_id,
|
||||
model_key: entry.model_ref.model_key,
|
||||
},
|
||||
name: entry.name,
|
||||
provider_name:
|
||||
typeof entry.provider_name === "string" ? entry.provider_name : "",
|
||||
effective_capabilities: {
|
||||
tools: caps?.tools === true,
|
||||
vision: caps?.vision === true,
|
||||
structured_output: caps?.structured_output === true,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const pending = fetchJson("/api/models")
|
||||
.then(async (r) => {
|
||||
const entries: ModelRegistryEntry[] = [];
|
||||
if (Array.isArray(r.entries)) {
|
||||
for (const raw of r.entries) {
|
||||
if (!raw || typeof raw !== "object") continue;
|
||||
const e = raw as {
|
||||
name?: unknown;
|
||||
model_id?: unknown;
|
||||
provider?: unknown;
|
||||
};
|
||||
if (
|
||||
typeof e.name === "string" &&
|
||||
typeof e.provider === "string" &&
|
||||
e.name &&
|
||||
e.provider
|
||||
) {
|
||||
entries.push({
|
||||
name: e.name,
|
||||
model_id: typeof e.model_id === "string" ? e.model_id : e.name,
|
||||
provider: e.provider,
|
||||
});
|
||||
}
|
||||
}
|
||||
function fetchSelectableModels(): Promise<SelectableModel[]> {
|
||||
if (cachedModels) return cachedModels;
|
||||
|
||||
const pending = fetch("/api/models")
|
||||
.then(async (response) => {
|
||||
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
||||
const body = (await response.json()) as { models?: unknown };
|
||||
if (!Array.isArray(body.models)) return EMPTY;
|
||||
const models: SelectableModel[] = [];
|
||||
for (const raw of body.models) {
|
||||
const parsed = parseSelectableModel(raw);
|
||||
if (parsed) models.push(parsed);
|
||||
}
|
||||
let defaultEntry: ModelRegistry["defaultEntry"] = null;
|
||||
if (r.default && typeof r.default === "object") {
|
||||
const d = r.default as { name?: unknown; provider?: unknown };
|
||||
if (typeof d.name === "string" && d.name) {
|
||||
defaultEntry = {
|
||||
name: d.name,
|
||||
provider:
|
||||
typeof d.provider === "string" && d.provider ? d.provider : null,
|
||||
};
|
||||
}
|
||||
}
|
||||
return { entries, defaultEntry } as ModelRegistry;
|
||||
return models;
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
cachedRegistry = null;
|
||||
throw err;
|
||||
.catch((error: unknown) => {
|
||||
cachedModels = null;
|
||||
throw error;
|
||||
});
|
||||
|
||||
cachedRegistry = pending;
|
||||
cachedModels = pending;
|
||||
return pending;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the backend's authoritative model registry through the WebUI's
|
||||
* same-origin `/api/models` proxy first, falling back to
|
||||
* the same-origin model registry endpoint. Results
|
||||
* are cached at module level — the registry is static between deployment
|
||||
* restarts, so remounting ChatInterface never triggers a redundant network
|
||||
* request.
|
||||
*
|
||||
* Failures are non-fatal but do not expose a hard-coded fallback list: the
|
||||
* picker must only display models returned by the deployment configuration.
|
||||
* Failed fetches are evicted from the cache so the next mount retries.
|
||||
*/
|
||||
/** Enabled-and-verified models the current user may select (design doc 9.1).
|
||||
* Results are cached at module level; `invalidateAvailableModels` forces a
|
||||
* refetch after registry changes. Failed fetches are evicted so the next
|
||||
* mount retries. There is no hard-coded fallback list. */
|
||||
export function useAvailableModels(): {
|
||||
registry: ModelRegistry;
|
||||
models: SelectableModel[];
|
||||
loading: boolean;
|
||||
error: string | null;
|
||||
} {
|
||||
const [registry, setRegistry] = useState<ModelRegistry>(EMPTY);
|
||||
const [models, setModels] = useState<SelectableModel[]>(EMPTY);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [refreshRevision, setRefreshRevision] = useState(0);
|
||||
@@ -125,16 +104,16 @@ export function useAvailableModels(): {
|
||||
let cancelled = false;
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
fetchRegistry()
|
||||
fetchSelectableModels()
|
||||
.then((result) => {
|
||||
if (cancelled) return;
|
||||
setRegistry(result);
|
||||
setModels(result);
|
||||
setError(null);
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
if (cancelled) return;
|
||||
setError(err instanceof Error ? err.message : "Failed to load models.");
|
||||
setRegistry(EMPTY);
|
||||
setModels(EMPTY);
|
||||
})
|
||||
.finally(() => {
|
||||
if (!cancelled) setLoading(false);
|
||||
@@ -145,5 +124,9 @@ export function useAvailableModels(): {
|
||||
};
|
||||
}, [refreshRevision]);
|
||||
|
||||
return { registry, loading, error };
|
||||
return { models, loading, error };
|
||||
}
|
||||
|
||||
export function modelRefLabel(ref: ModelRef): string {
|
||||
return `${ref.provider_id}/${ref.model_key}`;
|
||||
}
|
||||
|
||||
+125
-78
@@ -8,15 +8,8 @@ import { useQueryState } from "nuqs";
|
||||
import { parseSummarizationEvent } from "@/lib/summarization";
|
||||
import { findActiveTurnId } from "@/lib/usageTurn";
|
||||
import { toast } from "sonner";
|
||||
import {
|
||||
MODEL_OVERRIDE_METADATA_KEY,
|
||||
type ModelOverride,
|
||||
} from "@/lib/modelCommand";
|
||||
import {
|
||||
getNewChatModelPreference,
|
||||
setNewChatModelPreference,
|
||||
} from "@/lib/modelPreference";
|
||||
import { setThreadModelOverride } from "@/app/hooks/useThreads";
|
||||
import type { ModelRef, ThreadModelSelection } from "@/lib/modelRegistry";
|
||||
import { setThreadModelSelection } from "@/app/hooks/useThreads";
|
||||
import {
|
||||
clearRunCursor,
|
||||
getRunStreamCheckpoint,
|
||||
@@ -149,6 +142,54 @@ function hasActionableInterrupt(i: unknown): boolean {
|
||||
);
|
||||
}
|
||||
|
||||
function isModelRefShape(value: unknown): value is ModelRef {
|
||||
if (!value || typeof value !== "object") return false;
|
||||
const ref = value as { provider_id?: unknown; model_key?: unknown };
|
||||
return (
|
||||
typeof ref.provider_id === "string" && typeof ref.model_key === "string"
|
||||
);
|
||||
}
|
||||
|
||||
/** Client-side mirror of the thread metadata `model_selection` reader
|
||||
* (design doc 7.2). Malformed payloads degrade to `inherit` for display; the
|
||||
* BFF remains authoritative when the run snapshot is created. */
|
||||
function readSelectionFromMetadata(metadata: Record<string, unknown> | undefined): {
|
||||
selection: ThreadModelSelection;
|
||||
revision: number;
|
||||
} {
|
||||
const source = metadata ?? {};
|
||||
const rawRevision = source.model_selection_revision;
|
||||
const revision =
|
||||
typeof rawRevision === "number" &&
|
||||
Number.isInteger(rawRevision) &&
|
||||
rawRevision >= 0
|
||||
? rawRevision
|
||||
: 0;
|
||||
const raw = source.model_selection;
|
||||
if (raw === undefined || raw === "inherit") {
|
||||
return { selection: "inherit", revision };
|
||||
}
|
||||
if (raw && typeof raw === "object") {
|
||||
const candidate = raw as { primary?: unknown; auxiliary?: unknown };
|
||||
if (
|
||||
isModelRefShape(candidate.primary) &&
|
||||
(candidate.auxiliary === null || candidate.auxiliary === undefined ||
|
||||
isModelRefShape(candidate.auxiliary))
|
||||
) {
|
||||
return {
|
||||
selection: {
|
||||
primary: candidate.primary,
|
||||
auxiliary: isModelRefShape(candidate.auxiliary)
|
||||
? candidate.auxiliary
|
||||
: null,
|
||||
},
|
||||
revision,
|
||||
};
|
||||
}
|
||||
}
|
||||
return { selection: "inherit", revision };
|
||||
}
|
||||
|
||||
function latestTaskInterrupt(
|
||||
tasks: Array<{ interrupts?: unknown[] }> | undefined
|
||||
): unknown {
|
||||
@@ -295,83 +336,69 @@ export function useChat({
|
||||
fetchedThreadIdRef.current = fetchedThreadId;
|
||||
const recoveryRunRef = useRef(0);
|
||||
|
||||
// Per-thread model override. When set, gets folded into
|
||||
// `configurable.model` on every `stream.submit` — the backend's
|
||||
// `configurable_model` middleware
|
||||
// (EvoScientist/middleware/configurable_model.py) is what actually swaps
|
||||
// the chat model per request.
|
||||
// Per-thread model selection (design doc 7.2). The selection lives on the
|
||||
// thread metadata (`model_selection` + `model_selection_revision`) and is
|
||||
// written exclusively through the PATCH CAS endpoint; each new run freezes
|
||||
// it into a runtime snapshot server-side.
|
||||
//
|
||||
// The persistence dance gets a wrinkle for fresh chats: the thread row
|
||||
// doesn't exist server-side until the first `stream.submit` creates it,
|
||||
// so we can't write `model_override` into thread metadata yet. We stash
|
||||
// any pre-thread pick in `pendingOverrideRef`, fold it into the first
|
||||
// run's config via `buildRunConfig`, and write it through to metadata
|
||||
// when `threadId` actually shows up. Without this, the user's first
|
||||
// message goes to the deployment default even after they picked a model
|
||||
// from the empty composer.
|
||||
// localStorage is unavailable during SSR. Load the new-chat preference in
|
||||
// the effect below so server and first client render stay identical.
|
||||
const [modelOverride, setModelOverrideState] = useState<ModelOverride | null>(
|
||||
null
|
||||
);
|
||||
const pendingOverrideRef = useRef<ModelOverride | null>(null);
|
||||
// Fresh-chat wrinkle: the thread row doesn't exist server-side until the
|
||||
// first message creates it, so a pre-thread pick is stashed in
|
||||
// `pendingSelectionRef` and written through with
|
||||
// `expected_selection_revision=0` once the thread id appears.
|
||||
const [modelSelection, setModelSelectionState] =
|
||||
useState<ThreadModelSelection>("inherit");
|
||||
const [selectionRevision, setSelectionRevision] = useState(0);
|
||||
const pendingSelectionRef = useRef<ThreadModelSelection | null>(null);
|
||||
const createdThreadIdRef = useRef<string | null>(null);
|
||||
const ensureThreadPromiseRef = useRef<Promise<string> | null>(null);
|
||||
useEffect(() => {
|
||||
if (!threadId) {
|
||||
const remembered = getNewChatModelPreference();
|
||||
pendingOverrideRef.current = remembered;
|
||||
createdThreadIdRef.current = null;
|
||||
setModelOverrideState(remembered);
|
||||
setModelSelectionState(pendingSelectionRef.current ?? "inherit");
|
||||
setSelectionRevision(0);
|
||||
return;
|
||||
}
|
||||
// Thread just came into existence (or we switched onto an existing one).
|
||||
// If we have a pending pre-thread override, write it through to metadata
|
||||
// and keep the local state as-is. Otherwise fetch the thread's persisted
|
||||
// override and seed local state from it.
|
||||
if (createdThreadIdRef.current === threadId && pendingOverrideRef.current) {
|
||||
const pending = pendingOverrideRef.current;
|
||||
pendingOverrideRef.current = null;
|
||||
// If we have a pending pre-thread pick, write it through to metadata with
|
||||
// the initial revision; otherwise fetch the thread's persisted selection
|
||||
// and seed local state from it.
|
||||
if (
|
||||
createdThreadIdRef.current === threadId &&
|
||||
pendingSelectionRef.current
|
||||
) {
|
||||
const pending = pendingSelectionRef.current;
|
||||
pendingSelectionRef.current = null;
|
||||
createdThreadIdRef.current = null;
|
||||
void (async () => {
|
||||
try {
|
||||
await setThreadModelOverride(threadId, pending);
|
||||
await setThreadModelSelection(threadId, pending, 0);
|
||||
setModelSelectionState(pending);
|
||||
setSelectionRevision(1);
|
||||
} catch {
|
||||
// The local state still reflects the pick; the next `setModelOverride`
|
||||
// call (or thread reopen) gets another chance to persist it.
|
||||
// The local state still reflects the pick; the next
|
||||
// `setModelSelection` call (or thread reopen) resyncs from metadata.
|
||||
}
|
||||
})();
|
||||
return;
|
||||
}
|
||||
// Opening an existing thread from the New Chat screen must not write the
|
||||
// remembered new-chat preference over that thread's own metadata.
|
||||
pendingOverrideRef.current = null;
|
||||
// Opening an existing thread from the New Chat screen must not write a
|
||||
// staged pick over that thread's own selection.
|
||||
pendingSelectionRef.current = null;
|
||||
createdThreadIdRef.current = null;
|
||||
let cancelled = false;
|
||||
void (async () => {
|
||||
try {
|
||||
const { thread: t } = await getConversation(threadId);
|
||||
if (cancelled) return;
|
||||
const raw = (t.metadata ?? {})[MODEL_OVERRIDE_METADATA_KEY];
|
||||
if (
|
||||
raw &&
|
||||
typeof raw === "object" &&
|
||||
typeof (raw as { model?: unknown }).model === "string"
|
||||
) {
|
||||
const r = raw as { model: string; model_provider?: unknown };
|
||||
setModelOverrideState({
|
||||
model: r.model,
|
||||
model_provider:
|
||||
typeof r.model_provider === "string"
|
||||
? r.model_provider
|
||||
: undefined,
|
||||
});
|
||||
} else {
|
||||
setModelOverrideState(null);
|
||||
}
|
||||
const stored = readSelectionFromMetadata(
|
||||
(t.metadata ?? {}) as Record<string, unknown>
|
||||
);
|
||||
setModelSelectionState(stored.selection);
|
||||
setSelectionRevision(stored.revision);
|
||||
} catch (error) {
|
||||
if (cancelled) return;
|
||||
setModelOverrideState(null);
|
||||
setModelSelectionState("inherit");
|
||||
setSelectionRevision(0);
|
||||
if (error instanceof ConversationApiError && error.status === 404) {
|
||||
activeRunRef.current = null;
|
||||
setRunConnection(null);
|
||||
@@ -387,23 +414,44 @@ export function useChat({
|
||||
};
|
||||
}, [setThreadId, threadId]);
|
||||
|
||||
// Persist + apply locally. When the thread row exists, writes metadata
|
||||
// first so a reload keeps the choice. Pre-thread (new chat with no
|
||||
// threadId yet), stashes the override in a ref so the next send picks it
|
||||
// up via `buildRunConfig` and the thread-id effect can persist it as soon
|
||||
// as the row is created server-side.
|
||||
const setModelOverride = useCallback(
|
||||
async (next: ModelOverride | null) => {
|
||||
setModelOverrideState(next);
|
||||
setNewChatModelPreference(next);
|
||||
// Persist + apply locally. Pre-thread (new chat with no threadId yet),
|
||||
// stashes the pick in a ref so the thread-id effect can persist it as soon
|
||||
// as the row is created server-side. Existing threads go through the CAS
|
||||
// endpoint; a revision conflict refetches the current selection so the UI
|
||||
// never diverges from the stored one.
|
||||
const setModelSelection = useCallback(
|
||||
async (next: ThreadModelSelection) => {
|
||||
if (!threadId) {
|
||||
pendingOverrideRef.current = next;
|
||||
pendingSelectionRef.current = next === "inherit" ? null : next;
|
||||
setModelSelectionState(next);
|
||||
setSelectionRevision(0);
|
||||
return;
|
||||
}
|
||||
pendingOverrideRef.current = null;
|
||||
await setThreadModelOverride(threadId, next);
|
||||
pendingSelectionRef.current = null;
|
||||
try {
|
||||
await setThreadModelSelection(threadId, next, selectionRevision);
|
||||
setModelSelectionState(next);
|
||||
setSelectionRevision((revision) => revision + 1);
|
||||
} catch (error) {
|
||||
if (
|
||||
error instanceof ConversationApiError &&
|
||||
error.code === "THREAD_MODEL_SELECTION_CONFLICT"
|
||||
) {
|
||||
try {
|
||||
const { thread: t } = await getConversation(threadId);
|
||||
const stored = readSelectionFromMetadata(
|
||||
(t.metadata ?? {}) as Record<string, unknown>
|
||||
);
|
||||
setModelSelectionState(stored.selection);
|
||||
setSelectionRevision(stored.revision);
|
||||
} catch {
|
||||
// The next thread load resyncs.
|
||||
}
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[threadId]
|
||||
[threadId, selectionRevision]
|
||||
);
|
||||
|
||||
const liveMessages =
|
||||
@@ -947,7 +995,6 @@ export function useChat({
|
||||
input,
|
||||
command,
|
||||
review_mode: reviewMode,
|
||||
model_override: modelOverride,
|
||||
});
|
||||
const tracked: TrackedRun = {
|
||||
threadId: usableThreadId,
|
||||
@@ -1009,7 +1056,7 @@ export function useChat({
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
},
|
||||
[ensureThread, modelOverride, showError]
|
||||
[ensureThread, showError]
|
||||
);
|
||||
|
||||
const sendMessage = useCallback(
|
||||
@@ -1117,7 +1164,7 @@ export function useChat({
|
||||
stopStream,
|
||||
resumeInterrupt,
|
||||
subAgentActivity: EMPTY_SUB_AGENT_ACTIVITY,
|
||||
modelOverride,
|
||||
setModelOverride,
|
||||
modelSelection,
|
||||
setModelSelection,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import useSWRInfinite from "swr/infinite";
|
||||
import { patchConversation } from "@/lib/conversationApi";
|
||||
import type { ThreadModelSelection } from "@/lib/modelRegistry";
|
||||
|
||||
export interface ThreadItem {
|
||||
id: string;
|
||||
@@ -148,11 +149,18 @@ export async function pinThread(id: string, pinned: boolean): Promise<void> {
|
||||
await patchConversation(id, { pinned });
|
||||
}
|
||||
|
||||
export async function setThreadModelOverride(
|
||||
/** CAS write of the thread's model selection (design doc 7.2). Throws
|
||||
* `ConversationApiError` with code THREAD_MODEL_SELECTION_CONFLICT on a
|
||||
* revision mismatch. */
|
||||
export async function setThreadModelSelection(
|
||||
id: string,
|
||||
override: { model: string; model_provider?: string } | null
|
||||
selection: ThreadModelSelection,
|
||||
expectedRevision: number
|
||||
): Promise<void> {
|
||||
await patchConversation(id, { model_override: override });
|
||||
await patchConversation(id, {
|
||||
model_selection: selection,
|
||||
expected_selection_revision: expectedRevision,
|
||||
});
|
||||
}
|
||||
|
||||
function slugifyForFilename(input: string): string {
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
interface DefaultModelResponse {
|
||||
default?: {
|
||||
name?: unknown;
|
||||
provider?: unknown;
|
||||
};
|
||||
error?: unknown;
|
||||
}
|
||||
|
||||
export async function setDefaultModel(
|
||||
model: string,
|
||||
provider: string
|
||||
): Promise<void> {
|
||||
const response = await fetch(
|
||||
"/api/default-model",
|
||||
{
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ model, provider }),
|
||||
}
|
||||
);
|
||||
const payload = (await response
|
||||
.json()
|
||||
.catch(() => ({}))) as DefaultModelResponse;
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
typeof payload.error === "string"
|
||||
? payload.error
|
||||
: `Default model request failed with HTTP ${response.status}.`
|
||||
);
|
||||
}
|
||||
if (
|
||||
payload.default?.name !== model ||
|
||||
payload.default?.provider !== provider
|
||||
) {
|
||||
throw new Error("The backend did not confirm the requested default model.");
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
// `/model` command for the chat composer.
|
||||
//
|
||||
// The actual model switching is handled server-side by the backend's
|
||||
// `EvoScientist/middleware/configurable_model.py` middleware, which reads
|
||||
// `model` / `model_provider` from each run's `RunnableConfig.configurable`.
|
||||
// We just need to inject those fields into `stream.submit({ config: ... })`
|
||||
// and persist the choice on the thread. A separate browser preference seeds
|
||||
// future new chats without changing the deployment-wide default.
|
||||
//
|
||||
// Listing available models: the backend's authoritative registry lives in
|
||||
// `EvoScientist/llm/models.py` and is exposed at `GET /api/models` (mounted
|
||||
// via langgraph.json). `useAvailableModels` fetches that endpoint at runtime;
|
||||
// The picker intentionally has no hard-coded fallback: only entries returned
|
||||
// by the deployment configuration are shown. The command parser still passes
|
||||
// an explicitly typed model through for backward compatibility.
|
||||
|
||||
/** Thread metadata key carrying the per-thread model override. The LangGraph
|
||||
* thread record remains authoritative for existing conversations, so their
|
||||
* model survives reloads, thread switches, and cross-device opens. */
|
||||
export const MODEL_OVERRIDE_METADATA_KEY = "model_override";
|
||||
|
||||
export interface ModelOverride {
|
||||
/** Short name as used by the backend's `MODELS` registry, e.g.
|
||||
* "claude-sonnet-4-6" or "deepseek-v4-flash". */
|
||||
model: string;
|
||||
/** Provider routing hint. Optional — when omitted the backend uses the
|
||||
* default provider for that short name. */
|
||||
model_provider?: string;
|
||||
}
|
||||
|
||||
export interface CommonModelEntry extends ModelOverride {
|
||||
/** Human-readable label for the picker. Falls back to `model` when absent. */
|
||||
label?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Seed list for the `/model` picker. Not exhaustive — the backend's
|
||||
* `EvoScientist/llm/models.py` has the full registry. This list is the
|
||||
* "I want to switch fast, don't make me look it up" subset; users can still
|
||||
* type any name the backend knows via `/model <name> [provider]`.
|
||||
*/
|
||||
export const COMMON_MODELS: ReadonlyArray<CommonModelEntry> = [
|
||||
{ model: "claude-sonnet-4-6", model_provider: "anthropic" },
|
||||
{ model: "claude-haiku-4-5", model_provider: "anthropic" },
|
||||
{ model: "claude-opus-4-8", model_provider: "anthropic" },
|
||||
{ model: "gpt-5.5-pro", model_provider: "openai" },
|
||||
{ model: "gpt-5.5", model_provider: "openai" },
|
||||
{ model: "gpt-5-mini", model_provider: "openai" },
|
||||
{ model: "gemini-2.5-pro", model_provider: "google-genai" },
|
||||
{ model: "gemini-3.5-flash", model_provider: "google-genai" },
|
||||
{ model: "deepseek-v3.2", model_provider: "deepseek" },
|
||||
];
|
||||
|
||||
export type ModelCommand =
|
||||
| { kind: "show" }
|
||||
| { kind: "reset" }
|
||||
| { kind: "set"; model: string; provider?: string };
|
||||
|
||||
/**
|
||||
* Parse a chat-composer input as a `/model` command.
|
||||
*
|
||||
* Recognised forms:
|
||||
* - `/model` → kind: "show" (open the picker)
|
||||
* - `/model reset` → kind: "reset" (clear thread override)
|
||||
* - `/model <name>` → kind: "set", model=<name>
|
||||
* - `/model <name> <prov>`→ kind: "set", model=<name>, provider=<prov>
|
||||
*
|
||||
* Returns `null` for anything else, including a string that starts with
|
||||
* `/model` but has more than 2 args — we'd rather let the agent see a
|
||||
* misformatted command than guess. Trailing whitespace is tolerated.
|
||||
*/
|
||||
export function parseModelCommand(input: string): ModelCommand | null {
|
||||
const trimmed = input.trim();
|
||||
if (trimmed !== "/model" && !trimmed.startsWith("/model ")) return null;
|
||||
const rest = trimmed.slice("/model".length).trim();
|
||||
if (!rest) return { kind: "show" };
|
||||
const parts = rest.split(/\s+/);
|
||||
if (parts[0].toLowerCase() === "reset" && parts.length === 1) {
|
||||
return { kind: "reset" };
|
||||
}
|
||||
if (parts.length > 2) return null;
|
||||
return { kind: "set", model: parts[0], provider: parts[1] };
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
MODEL_PREFERENCE_STORAGE_KEY,
|
||||
getNewChatModelPreference,
|
||||
parseModelPreference,
|
||||
setNewChatModelPreference,
|
||||
type ModelPreferenceStorage,
|
||||
} from "./modelPreference";
|
||||
|
||||
function memoryStorage(initial: Record<string, string> = {}) {
|
||||
const values = new Map(Object.entries(initial));
|
||||
const storage: ModelPreferenceStorage = {
|
||||
getItem: (key) => values.get(key) ?? null,
|
||||
setItem: (key, value) => void values.set(key, value),
|
||||
removeItem: (key) => void values.delete(key),
|
||||
};
|
||||
return { storage, values };
|
||||
}
|
||||
|
||||
describe("new-chat model preference", () => {
|
||||
it("normalizes a valid model and provider", () => {
|
||||
expect(
|
||||
parseModelPreference({ model: " gpt-5 ", model_provider: " openai " })
|
||||
).toEqual({ model: "gpt-5", model_provider: "openai" });
|
||||
expect(parseModelPreference({ model: "claude-sonnet" })).toEqual({
|
||||
model: "claude-sonnet",
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects malformed preferences", () => {
|
||||
expect(parseModelPreference(null)).toBeNull();
|
||||
expect(parseModelPreference({ model: "" })).toBeNull();
|
||||
expect(
|
||||
parseModelPreference({ model: "gpt-5", model_provider: 42 })
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("persists the last selection and clears it on reset", () => {
|
||||
const { storage, values } = memoryStorage();
|
||||
setNewChatModelPreference(
|
||||
{ model: "gpt-5", model_provider: "openai" },
|
||||
storage
|
||||
);
|
||||
expect(getNewChatModelPreference(storage)).toEqual({
|
||||
model: "gpt-5",
|
||||
model_provider: "openai",
|
||||
});
|
||||
|
||||
setNewChatModelPreference(null, storage);
|
||||
expect(values.has(MODEL_PREFERENCE_STORAGE_KEY)).toBe(false);
|
||||
expect(getNewChatModelPreference(storage)).toBeNull();
|
||||
});
|
||||
|
||||
it("fails closed when stored JSON is corrupt", () => {
|
||||
const { storage } = memoryStorage({
|
||||
[MODEL_PREFERENCE_STORAGE_KEY]: "{not-json",
|
||||
});
|
||||
expect(getNewChatModelPreference(storage)).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -1,67 +0,0 @@
|
||||
import type { ModelOverride } from "@/lib/modelCommand";
|
||||
|
||||
export type ModelPreferenceStorage = Pick<
|
||||
Storage,
|
||||
"getItem" | "setItem" | "removeItem"
|
||||
>;
|
||||
|
||||
export const MODEL_PREFERENCE_STORAGE_KEY =
|
||||
"evoscientist-new-chat-model-preference";
|
||||
|
||||
function browserStorage(): ModelPreferenceStorage | null {
|
||||
if (typeof window === "undefined") return null;
|
||||
try {
|
||||
return window.localStorage;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function parseModelPreference(value: unknown): ModelOverride | null {
|
||||
if (!value || typeof value !== "object") return null;
|
||||
const raw = value as { model?: unknown; model_provider?: unknown };
|
||||
if (typeof raw.model !== "string" || !raw.model.trim()) return null;
|
||||
if (
|
||||
raw.model_provider !== undefined &&
|
||||
(typeof raw.model_provider !== "string" || !raw.model_provider.trim())
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
model: raw.model.trim(),
|
||||
...(typeof raw.model_provider === "string"
|
||||
? { model_provider: raw.model_provider.trim() }
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
|
||||
export function getNewChatModelPreference(
|
||||
storage: ModelPreferenceStorage | null = browserStorage()
|
||||
): ModelOverride | null {
|
||||
if (!storage) return null;
|
||||
try {
|
||||
const raw = storage.getItem(MODEL_PREFERENCE_STORAGE_KEY);
|
||||
if (!raw) return null;
|
||||
return parseModelPreference(JSON.parse(raw));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function setNewChatModelPreference(
|
||||
preference: ModelOverride | null,
|
||||
storage: ModelPreferenceStorage | null = browserStorage()
|
||||
): void {
|
||||
if (!storage) return;
|
||||
try {
|
||||
if (!preference) {
|
||||
storage.removeItem(MODEL_PREFERENCE_STORAGE_KEY);
|
||||
return;
|
||||
}
|
||||
const parsed = parseModelPreference(preference);
|
||||
if (!parsed) return;
|
||||
storage.setItem(MODEL_PREFERENCE_STORAGE_KEY, JSON.stringify(parsed));
|
||||
} catch {
|
||||
// Browser storage is a convenience. Chat must still work when it is blocked.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
/**
|
||||
* WebUI mirror of the EvoScientist model-registry contract. These types are
|
||||
* derived from the backend's exported OpenAPI document
|
||||
* (`EvoScientist/model_registry/openapi.json`, design doc sections 4.3, 6.2,
|
||||
* 9.x) — the backend remains the only authority; do not add frontend-only
|
||||
* fields here.
|
||||
*/
|
||||
|
||||
export interface ModelRef {
|
||||
provider_id: string;
|
||||
model_key: string;
|
||||
}
|
||||
|
||||
export type AuthMode = "none" | "api_key" | "bearer" | "adapter_managed";
|
||||
|
||||
export interface AuthConfig {
|
||||
mode: AuthMode;
|
||||
credential_id: string | null;
|
||||
}
|
||||
|
||||
export interface ProviderRuntimeConfig {
|
||||
timeout_seconds: number;
|
||||
max_retries: number;
|
||||
default_temperature: number | null;
|
||||
default_top_p: number | null;
|
||||
default_reasoning_effort: ReasoningEffort;
|
||||
}
|
||||
|
||||
export type ReasoningEffort = "auto" | "low" | "medium" | "high";
|
||||
|
||||
export interface Capabilities {
|
||||
tools: boolean;
|
||||
vision: boolean;
|
||||
structured_output: boolean;
|
||||
}
|
||||
|
||||
export interface ModelRuntimeConfig {
|
||||
limit_mode: "combined" | "input_only";
|
||||
context_window_tokens: number | null;
|
||||
max_input_tokens: number | null;
|
||||
max_output_tokens: number;
|
||||
min_effective_input_tokens: number;
|
||||
fixed_system_reserve_tokens: number;
|
||||
fixed_tools_reserve_tokens: number;
|
||||
fixed_attachments_reserve_tokens: number;
|
||||
limits_status: "confirmed" | "needs_confirmation";
|
||||
limits_source: "provider" | "tested_contract" | "user";
|
||||
temperature: number | null;
|
||||
top_p: number | null;
|
||||
reasoning_effort: ReasoningEffort;
|
||||
declared_capabilities: Capabilities;
|
||||
}
|
||||
|
||||
export interface ModelConfig {
|
||||
key: string;
|
||||
name: string;
|
||||
upstream_model_id: string;
|
||||
enabled: boolean;
|
||||
runtime: ModelRuntimeConfig;
|
||||
}
|
||||
|
||||
export interface ProviderConfig {
|
||||
id: string;
|
||||
name: string;
|
||||
adapter: string;
|
||||
base_url: string;
|
||||
auth: AuthConfig;
|
||||
enabled: boolean;
|
||||
runtime: ProviderRuntimeConfig;
|
||||
models: ModelConfig[];
|
||||
}
|
||||
|
||||
export interface RegistryDefaults {
|
||||
primary: ModelRef | null;
|
||||
auxiliary: ModelRef | null;
|
||||
}
|
||||
|
||||
export interface RegistryV4 {
|
||||
version: 4;
|
||||
revision: number;
|
||||
state: "bootstrap" | "active";
|
||||
defaults: RegistryDefaults;
|
||||
providers: ProviderConfig[];
|
||||
}
|
||||
|
||||
export interface AuthSpec {
|
||||
credential_required: boolean;
|
||||
credential_kind: "api_key" | "bearer_token" | "adapter_managed" | "none";
|
||||
target: string;
|
||||
target_name: string | null;
|
||||
}
|
||||
|
||||
export interface ParameterRule {
|
||||
supported: boolean;
|
||||
value_type: "integer" | "number" | "enum";
|
||||
nullable: "inherit" | "omit" | "forbidden";
|
||||
minimum?: number | null;
|
||||
maximum?: number | null;
|
||||
enum_values?: string[] | null;
|
||||
target: string;
|
||||
target_name: string;
|
||||
conflicts_with: string[];
|
||||
normalizer: string;
|
||||
}
|
||||
|
||||
export interface AdapterParameterSpec {
|
||||
adapter_id: string;
|
||||
spec_revision: number;
|
||||
model_selector: string;
|
||||
auth_specs: Partial<Record<AuthMode, AuthSpec>>;
|
||||
parameters: Record<string, ParameterRule>;
|
||||
protocol_capabilities: Capabilities;
|
||||
}
|
||||
|
||||
export interface CredentialStatus {
|
||||
credential_id: string;
|
||||
configured: boolean;
|
||||
hint: string | null;
|
||||
updated_at: string | null;
|
||||
}
|
||||
|
||||
export type ModelAvailabilityState =
|
||||
| "unavailable"
|
||||
| "configured"
|
||||
| "verification_failed"
|
||||
| "verification_stale"
|
||||
| "verified"
|
||||
| "enabled";
|
||||
|
||||
export interface ModelAvailability {
|
||||
model_ref: ModelRef;
|
||||
state: ModelAvailabilityState;
|
||||
selectable: boolean;
|
||||
reason_code: string | null;
|
||||
verification: {
|
||||
status: "none" | "passed" | "failed" | "stale";
|
||||
verified_at?: string | null;
|
||||
adapter_spec_revision?: number | null;
|
||||
};
|
||||
effective_capabilities: Capabilities;
|
||||
}
|
||||
|
||||
export interface EndpointPolicyPublic {
|
||||
public_https_allowed: true;
|
||||
development_endpoints: Array<{ id: string; url: string; label: string }>;
|
||||
}
|
||||
|
||||
export interface GetModelRegistryResponse {
|
||||
revision: number;
|
||||
registry: RegistryV4;
|
||||
adapter_specs: AdapterParameterSpec[];
|
||||
credential_status: CredentialStatus[];
|
||||
model_status: ModelAvailability[];
|
||||
endpoint_policy: EndpointPolicyPublic;
|
||||
}
|
||||
|
||||
export interface CredentialWrite {
|
||||
credential_id: string;
|
||||
operation: "replace";
|
||||
secret_value: string;
|
||||
}
|
||||
|
||||
export interface PutModelRegistryRequest {
|
||||
expected_revision: number;
|
||||
registry: RegistryV4;
|
||||
credential_writes?: CredentialWrite[];
|
||||
}
|
||||
|
||||
export interface ProviderTestRequest {
|
||||
expected_registry_revision: number;
|
||||
model_ref: ModelRef;
|
||||
}
|
||||
|
||||
export interface ProviderTestResponse {
|
||||
ok: boolean;
|
||||
registry_revision: number;
|
||||
model_ref: ModelRef;
|
||||
adapter_spec_revision: number;
|
||||
effective_request_options: Record<string, unknown>;
|
||||
model_status: ModelAvailability;
|
||||
latency_ms: number;
|
||||
}
|
||||
|
||||
export interface SelectableModel {
|
||||
model_ref: ModelRef;
|
||||
name: string;
|
||||
provider_name: string;
|
||||
effective_capabilities: Capabilities;
|
||||
}
|
||||
|
||||
export interface GetSelectableModelsResponse {
|
||||
models: SelectableModel[];
|
||||
}
|
||||
|
||||
/** ThreadModelSelection (design doc 7.2). */
|
||||
export type ThreadModelSelection =
|
||||
| "inherit"
|
||||
| { primary: ModelRef; auxiliary: ModelRef | null };
|
||||
|
||||
export interface RegistryApiError {
|
||||
code?: string;
|
||||
message?: string;
|
||||
details?: Array<{ path?: string; code?: string }>;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import "server-only";
|
||||
|
||||
import type { NextRequest } from "next/server";
|
||||
import { AUTH_COOKIE_NAME, isAuthenticationEnabled } from "@/lib/auth";
|
||||
import { decodeSessionPayload, roleForUsername } from "./auth";
|
||||
import { ensureBootstrapAdmin, type UserRole } from "./userStore";
|
||||
|
||||
export interface Actor {
|
||||
sub: string;
|
||||
role: UserRole;
|
||||
}
|
||||
|
||||
export class ActorError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
readonly status: 401 | 403 = 401
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the end-user actor for a BFF request. When WebUI authentication is
|
||||
* disabled (single-user local deployments), every request acts as the
|
||||
* built-in local admin — the loopback-only posture is enforced by the
|
||||
* callers that need it.
|
||||
*/
|
||||
export function requireActor(request: NextRequest): Actor {
|
||||
if (!isAuthenticationEnabled()) {
|
||||
return { sub: "local-admin", role: "admin" };
|
||||
}
|
||||
const session = decodeSessionPayload(
|
||||
request.cookies.get(AUTH_COOKIE_NAME)?.value ?? ""
|
||||
);
|
||||
if (!session || session.expiresAt <= Math.floor(Date.now() / 1000)) {
|
||||
throw new ActorError("Authentication required.", 401);
|
||||
}
|
||||
ensureBootstrapAdmin();
|
||||
const role = roleForUsername(session.username);
|
||||
if (!role) {
|
||||
throw new ActorError("Authentication required.", 401);
|
||||
}
|
||||
return { sub: session.username, role };
|
||||
}
|
||||
|
||||
export function requireAdmin(actor: Actor): void {
|
||||
if (actor.role !== "admin") {
|
||||
throw new ActorError("Model configuration requires an admin account.", 403);
|
||||
}
|
||||
}
|
||||
+68
-84
@@ -1,27 +1,27 @@
|
||||
import {
|
||||
createHmac,
|
||||
randomBytes,
|
||||
randomUUID,
|
||||
timingSafeEqual,
|
||||
} from "crypto";
|
||||
import { createHmac, timingSafeEqual } from "crypto";
|
||||
import { promises as fs } from "fs";
|
||||
import { homedir } from "os";
|
||||
import { join, resolve } from "path";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { join } from "path";
|
||||
import {
|
||||
AUTH_COOKIE_NAME,
|
||||
authSecret,
|
||||
isAuthenticationEnabled,
|
||||
sessionTtlSeconds,
|
||||
} from "@/lib/auth";
|
||||
|
||||
interface AuthConfiguration {
|
||||
username: string;
|
||||
password: string;
|
||||
secret: string;
|
||||
}
|
||||
import { webuiDataDir } from "./dataDir";
|
||||
import {
|
||||
ensureBootstrapAdmin,
|
||||
getUser,
|
||||
setUserPassword,
|
||||
verifyUserPassword,
|
||||
UserStoreError,
|
||||
type UserRole,
|
||||
type WebuiUser,
|
||||
} from "./userStore";
|
||||
|
||||
interface SessionPayload {
|
||||
username: string;
|
||||
role: UserRole;
|
||||
loggedInAt: string;
|
||||
expiresAt: number;
|
||||
}
|
||||
@@ -34,24 +34,41 @@ interface LoginAudit {
|
||||
|
||||
export class AuthConfigurationError extends Error {}
|
||||
|
||||
export function getAuthConfiguration(): AuthConfiguration {
|
||||
function requireSecret(): string {
|
||||
if (!isAuthenticationEnabled()) {
|
||||
throw new AuthConfigurationError("WebUI authentication is disabled.");
|
||||
}
|
||||
const username = process.env.WEBUI_AUTH_USERNAME?.trim();
|
||||
const password = process.env.WEBUI_AUTH_PASSWORD;
|
||||
const secret = authSecret();
|
||||
if (!username || !password || !secret) {
|
||||
throw new AuthConfigurationError(
|
||||
"Set WEBUI_AUTH_USERNAME, WEBUI_AUTH_PASSWORD, and WEBUI_AUTH_SECRET."
|
||||
);
|
||||
if (!secret) {
|
||||
throw new AuthConfigurationError("Set WEBUI_AUTH_SECRET.");
|
||||
}
|
||||
if (secret.length < 32) {
|
||||
throw new AuthConfigurationError(
|
||||
"WEBUI_AUTH_SECRET must contain at least 32 characters."
|
||||
);
|
||||
}
|
||||
return { username, password, secret };
|
||||
return secret;
|
||||
}
|
||||
|
||||
export function verifyCredentials(
|
||||
username: string,
|
||||
password: string
|
||||
): WebuiUser | null {
|
||||
requireSecret();
|
||||
try {
|
||||
ensureBootstrapAdmin();
|
||||
} catch (error) {
|
||||
throw new AuthConfigurationError(
|
||||
error instanceof Error ? error.message : "WebUI user store is not initialized."
|
||||
);
|
||||
}
|
||||
return verifyUserPassword(username, password);
|
||||
}
|
||||
|
||||
/** Resolve the current role for a session username (authoritative source is
|
||||
* the user store, so role changes apply without re-login). */
|
||||
export function roleForUsername(username: string): UserRole | null {
|
||||
return getUser(username)?.role ?? null;
|
||||
}
|
||||
|
||||
function constantTimeEqual(left: string, right: string): boolean {
|
||||
@@ -61,59 +78,25 @@ function constantTimeEqual(left: string, right: string): boolean {
|
||||
return timingSafeEqual(leftBuffer, rightBuffer);
|
||||
}
|
||||
|
||||
export function verifyCredentials(username: string, password: string): boolean {
|
||||
const config = getAuthConfiguration();
|
||||
return (
|
||||
constantTimeEqual(username, config.username) &&
|
||||
constantTimeEqual(password, config.password)
|
||||
);
|
||||
}
|
||||
|
||||
function envFilePath(): string {
|
||||
return join(process.cwd(), ".env");
|
||||
}
|
||||
|
||||
function replaceEnvValue(source: string, key: string, value: string): string {
|
||||
const expression = new RegExp(`^${key}=.*$`, "m");
|
||||
if (!expression.test(source)) {
|
||||
throw new AuthConfigurationError(`${key} is missing from .env.`);
|
||||
}
|
||||
return source.replace(expression, `${key}=${JSON.stringify(value)}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist the new password and rotate the signing key. Rotating the key makes
|
||||
* every old session invalid; the caller immediately issues a replacement cookie
|
||||
* to the account that successfully supplied the current password.
|
||||
* Persist the new password in the user store and rotate the signing key when
|
||||
* the local .env is writable. Rotating the key invalidates every other
|
||||
* session; the caller immediately issues a replacement cookie to the account
|
||||
* that successfully supplied the current password.
|
||||
*/
|
||||
export async function updateAuthPassword(password: string): Promise<void> {
|
||||
getAuthConfiguration();
|
||||
const target = envFilePath();
|
||||
let stat;
|
||||
export async function updateAuthPassword(
|
||||
username: string,
|
||||
password: string
|
||||
): Promise<void> {
|
||||
requireSecret();
|
||||
try {
|
||||
stat = await fs.lstat(target);
|
||||
} catch {
|
||||
throw new AuthConfigurationError("The local .env file is not available.");
|
||||
setUserPassword(username, password);
|
||||
} catch (error) {
|
||||
if (error instanceof UserStoreError) {
|
||||
throw new AuthConfigurationError(error.message);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
if (!stat.isFile() || stat.isSymbolicLink()) {
|
||||
throw new AuthConfigurationError("The local .env file is not writable.");
|
||||
}
|
||||
|
||||
const source = await fs.readFile(target, "utf8");
|
||||
const nextSecret = randomBytes(32).toString("hex");
|
||||
const next = replaceEnvValue(
|
||||
replaceEnvValue(source, "WEBUI_AUTH_PASSWORD", password),
|
||||
"WEBUI_AUTH_SECRET",
|
||||
nextSecret
|
||||
);
|
||||
const temp = `${target}.${randomUUID()}.tmp`;
|
||||
await fs.writeFile(temp, next, { encoding: "utf8", mode: 0o600 });
|
||||
await fs.rename(temp, target);
|
||||
|
||||
// Next reloads .env on restart; update this process too so the new password
|
||||
// and replacement session work immediately without waiting for a restart.
|
||||
process.env.WEBUI_AUTH_PASSWORD = password;
|
||||
process.env.WEBUI_AUTH_SECRET = nextSecret;
|
||||
}
|
||||
|
||||
function encode(value: string): string {
|
||||
@@ -132,14 +115,18 @@ function sign(payload: string, secret: string): string {
|
||||
return createHmac("sha256", secret).update(payload).digest("base64url");
|
||||
}
|
||||
|
||||
export function createSession(username: string): {
|
||||
export function createSession(
|
||||
username: string,
|
||||
role: UserRole
|
||||
): {
|
||||
token: string;
|
||||
payload: SessionPayload;
|
||||
} {
|
||||
const { secret } = getAuthConfiguration();
|
||||
const secret = requireSecret();
|
||||
const now = new Date();
|
||||
const payload: SessionPayload = {
|
||||
username,
|
||||
role,
|
||||
loggedInAt: now.toISOString(),
|
||||
expiresAt: Math.floor(now.getTime() / 1000) + sessionTtlSeconds(),
|
||||
};
|
||||
@@ -147,21 +134,13 @@ export function createSession(username: string): {
|
||||
return { token: `${encoded}.${sign(encoded, secret)}`, payload };
|
||||
}
|
||||
|
||||
function authDataDir(): string {
|
||||
const configured = process.env.EVOSCIENTIST_DATA_DIR?.trim();
|
||||
if (!configured) return join(homedir(), ".evoscientist");
|
||||
return configured.startsWith("~")
|
||||
? join(homedir(), configured.slice(1))
|
||||
: resolve(configured);
|
||||
}
|
||||
|
||||
function loginAuditPath(): string {
|
||||
return join(authDataDir(), "webui-auth.json");
|
||||
return join(webuiDataDir(), "webui-auth.json");
|
||||
}
|
||||
|
||||
export async function recordLogin(payload: SessionPayload): Promise<void> {
|
||||
const target = loginAuditPath();
|
||||
await fs.mkdir(authDataDir(), { recursive: true, mode: 0o700 });
|
||||
await fs.mkdir(webuiDataDir(), { recursive: true, mode: 0o700 });
|
||||
|
||||
let previous: LoginAudit | null = null;
|
||||
try {
|
||||
@@ -222,7 +201,12 @@ export function decodeSessionPayload(token: string): SessionPayload | null {
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return parsed as SessionPayload;
|
||||
return {
|
||||
username: parsed.username,
|
||||
role: parsed.role === "user" ? "user" : "admin",
|
||||
loggedInAt: parsed.loggedInAt,
|
||||
expiresAt: parsed.expiresAt,
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import "server-only";
|
||||
|
||||
import { homedir } from "node:os";
|
||||
import { join, resolve } from "node:path";
|
||||
|
||||
/** Root directory for WebUI-owned local state (users DB, delegation keys). */
|
||||
export function webuiDataDir(): string {
|
||||
const configured = process.env.EVOSCIENTIST_DATA_DIR?.trim();
|
||||
if (!configured) return join(homedir(), ".evoscientist");
|
||||
return configured.startsWith("~")
|
||||
? join(homedir(), configured.slice(1))
|
||||
: resolve(configured);
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
import "server-only";
|
||||
|
||||
import {
|
||||
createSign,
|
||||
generateKeyPairSync,
|
||||
randomUUID,
|
||||
} from "node:crypto";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { webuiDataDir } from "./dataDir";
|
||||
import type { UserRole } from "./userStore";
|
||||
|
||||
/**
|
||||
* Delegation JWT signing (design doc 7.3). The WebUI signs a short-lived
|
||||
* ES256 JWT per BFF → EvoScientist request; EvoScientist verifies it against
|
||||
* the registered public key for this deployment and rejects replays by jti.
|
||||
*/
|
||||
|
||||
const DELEGATION_ISSUER = "WebUI";
|
||||
const DELEGATION_AUDIENCE = "EvoScientist";
|
||||
const DELEGATION_TTL_SECONDS = 50; // spec ceiling is 60s; leave clock margin
|
||||
|
||||
export const SCOPE_MODEL_CONFIG_READ = "model_config:read";
|
||||
export const SCOPE_MODEL_CONFIG_WRITE = "model_config:write";
|
||||
export const SCOPE_MODEL_CONFIG_TEST = "model_config:test";
|
||||
export const SCOPE_MODEL_SELECT = "model:select";
|
||||
export const SCOPE_RUN_CREATE = "run:create";
|
||||
|
||||
const ADMIN_SCOPES = [
|
||||
SCOPE_MODEL_CONFIG_READ,
|
||||
SCOPE_MODEL_CONFIG_WRITE,
|
||||
SCOPE_MODEL_CONFIG_TEST,
|
||||
SCOPE_MODEL_SELECT,
|
||||
SCOPE_RUN_CREATE,
|
||||
] as const;
|
||||
|
||||
const USER_SCOPES = [SCOPE_MODEL_SELECT, SCOPE_RUN_CREATE] as const;
|
||||
|
||||
/** Fixed role → scopes mapping (design doc 7.2). */
|
||||
export function scopesForRole(role: UserRole): string[] {
|
||||
return [...(role === "admin" ? ADMIN_SCOPES : USER_SCOPES)];
|
||||
}
|
||||
|
||||
export function delegationDeploymentId(): string {
|
||||
return process.env.WEBUI_DEPLOYMENT_ID?.trim() || "webui-local";
|
||||
}
|
||||
|
||||
function keyDir(): string {
|
||||
return webuiDataDir();
|
||||
}
|
||||
|
||||
function privateKeyPath(): string {
|
||||
return path.join(keyDir(), "webui-delegation-es256.pem");
|
||||
}
|
||||
|
||||
function publicKeyPath(): string {
|
||||
return path.join(keyDir(), "webui-delegation-es256.pub.pem");
|
||||
}
|
||||
|
||||
let cachedPrivateKey: string | null = null;
|
||||
|
||||
function ensureKeyPair(): string {
|
||||
if (cachedPrivateKey) return cachedPrivateKey;
|
||||
const privatePath = privateKeyPath();
|
||||
try {
|
||||
cachedPrivateKey = fs.readFileSync(privatePath, "utf8");
|
||||
return cachedPrivateKey;
|
||||
} catch {
|
||||
// First run — generate and persist below.
|
||||
}
|
||||
const { privateKey, publicKey } = generateKeyPairSync("ec", {
|
||||
namedCurve: "P-256",
|
||||
});
|
||||
const privatePem = privateKey.export({ format: "pem", type: "pkcs8" }).toString();
|
||||
const publicPem = publicKey.export({ format: "pem", type: "spki" }).toString();
|
||||
fs.mkdirSync(keyDir(), { recursive: true, mode: 0o700 });
|
||||
const tempPath = `${privatePath}.${randomUUID()}.tmp`;
|
||||
fs.writeFileSync(tempPath, privatePem, { encoding: "utf8", mode: 0o600 });
|
||||
fs.renameSync(tempPath, privatePath);
|
||||
fs.writeFileSync(publicKeyPath(), publicPem, { encoding: "utf8", mode: 0o644 });
|
||||
cachedPrivateKey = privatePem;
|
||||
return privatePem;
|
||||
}
|
||||
|
||||
/** Public key PEM to register in EvoScientist config.yaml
|
||||
* (`webui_delegation_public_keys` under this deployment's id). */
|
||||
export function delegationPublicKeyPem(): string {
|
||||
ensureKeyPair();
|
||||
return fs.readFileSync(publicKeyPath(), "utf8");
|
||||
}
|
||||
|
||||
function base64Url(value: string | Buffer): string {
|
||||
return Buffer.from(value).toString("base64url");
|
||||
}
|
||||
|
||||
export interface DelegationClaims {
|
||||
sub: string;
|
||||
scopes: string[];
|
||||
threadId?: string | null;
|
||||
}
|
||||
|
||||
export function signDelegationJwt(claims: DelegationClaims): string {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const header = base64Url(JSON.stringify({ alg: "ES256", typ: "JWT" }));
|
||||
const payload: Record<string, unknown> = {
|
||||
iss: DELEGATION_ISSUER,
|
||||
aud: DELEGATION_AUDIENCE,
|
||||
sub: claims.sub,
|
||||
scopes: claims.scopes,
|
||||
deployment_id: delegationDeploymentId(),
|
||||
iat: now,
|
||||
exp: now + DELEGATION_TTL_SECONDS,
|
||||
jti: randomUUID(),
|
||||
};
|
||||
if (claims.threadId) payload.thread_id = claims.threadId;
|
||||
const body = base64Url(JSON.stringify(payload));
|
||||
const signer = createSign("sha256");
|
||||
signer.update(`${header}.${body}`);
|
||||
const signature = signer.sign({
|
||||
key: ensureKeyPair(),
|
||||
dsaEncoding: "ieee-p1363",
|
||||
});
|
||||
return `${header}.${body}.${signature.toString("base64url")}`;
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
import "server-only";
|
||||
|
||||
import { getActiveDeployment } from "./activeDeployment";
|
||||
import { scopesForRole, signDelegationJwt } from "./delegation";
|
||||
import type { Actor } from "./actor";
|
||||
|
||||
/**
|
||||
* BFF → EvoScientist Config/Runtime API client (design doc 7.3). Every call
|
||||
* carries the fixed authentication format:
|
||||
*
|
||||
* Authorization: Bearer <BFF service token>
|
||||
* X-Evo-Actor: <short-lived signed delegation JWT>
|
||||
*/
|
||||
|
||||
export interface ConfigApiErrorDetail {
|
||||
path?: string;
|
||||
code?: string;
|
||||
}
|
||||
|
||||
export class ConfigApiError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
readonly status: number,
|
||||
readonly code: string | null = null,
|
||||
readonly details: ConfigApiErrorDetail[] = []
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
export function bffServiceToken(): string {
|
||||
const token = process.env.EVOSCIENTIST_BFF_SERVICE_TOKEN?.trim();
|
||||
if (!token) {
|
||||
throw new ConfigApiError(
|
||||
"EVOSCIENTIST_BFF_SERVICE_TOKEN is not configured on this WebUI server.",
|
||||
503,
|
||||
"PLATFORM_CONFIG_MISSING"
|
||||
);
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
interface FetchOptions {
|
||||
method?: string;
|
||||
body?: unknown;
|
||||
threadId?: string | null;
|
||||
}
|
||||
|
||||
export async function configApiFetch<T>(
|
||||
actor: Actor,
|
||||
path: string,
|
||||
options: FetchOptions = {}
|
||||
): Promise<{ status: number; body: T }> {
|
||||
const deployment = await getActiveDeployment();
|
||||
const delegation = signDelegationJwt({
|
||||
sub: actor.sub,
|
||||
scopes: scopesForRole(actor.role),
|
||||
threadId: options.threadId ?? null,
|
||||
});
|
||||
const headers: Record<string, string> = {
|
||||
Authorization: `Bearer ${bffServiceToken()}`,
|
||||
"X-Evo-Actor": delegation,
|
||||
};
|
||||
let body: string | undefined;
|
||||
if (options.body !== undefined) {
|
||||
headers["Content-Type"] = "application/json";
|
||||
body = JSON.stringify(options.body);
|
||||
}
|
||||
const upstream = await fetch(
|
||||
new URL(path, deployment.langgraphApiUrl),
|
||||
{
|
||||
method: options.method ?? "GET",
|
||||
headers,
|
||||
body,
|
||||
cache: "no-store",
|
||||
}
|
||||
);
|
||||
const text = await upstream.text();
|
||||
let parsed: unknown = null;
|
||||
try {
|
||||
parsed = text ? JSON.parse(text) : null;
|
||||
} catch {
|
||||
// Non-JSON upstream body; surface status only.
|
||||
}
|
||||
if (!upstream.ok) {
|
||||
const errorBody = parsed as {
|
||||
code?: unknown;
|
||||
message?: unknown;
|
||||
details?: unknown;
|
||||
} | null;
|
||||
throw new ConfigApiError(
|
||||
typeof errorBody?.message === "string"
|
||||
? errorBody.message
|
||||
: `EvoScientist request failed (${upstream.status}).`,
|
||||
upstream.status,
|
||||
typeof errorBody?.code === "string" ? errorBody.code : null,
|
||||
Array.isArray(errorBody?.details)
|
||||
? (errorBody.details as ConfigApiErrorDetail[])
|
||||
: []
|
||||
);
|
||||
}
|
||||
return { status: upstream.status, body: parsed as T };
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import "server-only";
|
||||
|
||||
import { NextResponse } from "next/server";
|
||||
import { ActorError } from "./actor";
|
||||
import { ConfigApiError } from "./evoscientistConfigClient";
|
||||
|
||||
export const NO_STORE = { "Cache-Control": "no-store" } as const;
|
||||
|
||||
/** Map server-side failures to the BFF's outward error shape. Backend error
|
||||
* codes are forwarded unchanged so the UI can key off them (design doc 9.5). */
|
||||
export function routeErrorResponse(error: unknown): NextResponse {
|
||||
if (error instanceof ConfigApiError) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
code: error.code ?? undefined,
|
||||
message: error.message,
|
||||
details: error.details.length ? error.details : undefined,
|
||||
},
|
||||
{ status: error.status, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
if (error instanceof ActorError) {
|
||||
return NextResponse.json(
|
||||
{ code: error.status === 401 ? "UNAUTHENTICATED" : "FORBIDDEN", message: error.message },
|
||||
{ status: error.status, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
return NextResponse.json(
|
||||
{
|
||||
code: "INVALID_REQUEST",
|
||||
message: error instanceof Error ? error.message : "Request failed.",
|
||||
},
|
||||
{ status: 400, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import type { ModelRef, ThreadModelSelection } from "@/lib/modelRegistry";
|
||||
|
||||
const ID_PATTERN = /^[a-z0-9][a-z0-9._-]{0,63}$/;
|
||||
|
||||
export class ThreadModelSelectionError extends Error {
|
||||
readonly code: string;
|
||||
constructor(code: string, message: string) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function isModelRef(value: unknown): value is ModelRef {
|
||||
return (
|
||||
isPlainObject(value) &&
|
||||
typeof value.provider_id === "string" &&
|
||||
ID_PATTERN.test(value.provider_id) &&
|
||||
typeof value.model_key === "string" &&
|
||||
ID_PATTERN.test(value.model_key)
|
||||
);
|
||||
}
|
||||
|
||||
/** Validates a browser-supplied `model_selection` payload (design doc 7.2). */
|
||||
export function parseThreadModelSelection(value: unknown): ThreadModelSelection {
|
||||
if (value === "inherit") return "inherit";
|
||||
if (
|
||||
isPlainObject(value) &&
|
||||
isModelRef(value.primary) &&
|
||||
(value.auxiliary === null || isModelRef(value.auxiliary))
|
||||
) {
|
||||
return {
|
||||
primary: { provider_id: value.primary.provider_id, model_key: value.primary.model_key },
|
||||
auxiliary:
|
||||
value.auxiliary === null
|
||||
? null
|
||||
: {
|
||||
provider_id: value.auxiliary.provider_id,
|
||||
model_key: value.auxiliary.model_key,
|
||||
},
|
||||
};
|
||||
}
|
||||
throw new ThreadModelSelectionError(
|
||||
"INVALID_REQUEST",
|
||||
"model_selection must be \"inherit\" or { primary: ModelRef, auxiliary: ModelRef | null }."
|
||||
);
|
||||
}
|
||||
|
||||
export interface StoredThreadModelSelection {
|
||||
selection: ThreadModelSelection;
|
||||
revision: number;
|
||||
}
|
||||
|
||||
/** Reads the selection frozen on thread metadata; a thread that never wrote
|
||||
* `model_selection` is `inherit` at revision 0 (design doc 7.2). */
|
||||
export function readThreadModelSelection(
|
||||
metadata: Record<string, unknown> | undefined
|
||||
): StoredThreadModelSelection {
|
||||
const source = metadata ?? {};
|
||||
const rawRevision = source.model_selection_revision;
|
||||
const revision =
|
||||
typeof rawRevision === "number" && Number.isInteger(rawRevision) && rawRevision >= 0
|
||||
? rawRevision
|
||||
: 0;
|
||||
if (!("model_selection" in source) || source.model_selection === undefined) {
|
||||
return { selection: "inherit", revision };
|
||||
}
|
||||
return { selection: parseThreadModelSelection(source.model_selection), revision };
|
||||
}
|
||||
|
||||
/** Serializes the read-check-write CAS sequence per thread inside this
|
||||
* single-instance BFF (design doc 7.2). */
|
||||
const selectionLocks = new Map<string, Promise<void>>();
|
||||
|
||||
export async function withThreadSelectionLock<T>(
|
||||
threadId: string,
|
||||
fn: () => Promise<T>
|
||||
): Promise<T> {
|
||||
const prior = selectionLocks.get(threadId) ?? Promise.resolve();
|
||||
let release!: () => void;
|
||||
const current = new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
const chained = prior.then(() => current);
|
||||
selectionLocks.set(threadId, chained);
|
||||
await prior;
|
||||
try {
|
||||
return await fn();
|
||||
} finally {
|
||||
release();
|
||||
if (selectionLocks.get(threadId) === chained) {
|
||||
selectionLocks.delete(threadId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
import "server-only";
|
||||
|
||||
import { randomBytes, scryptSync, timingSafeEqual } from "node:crypto";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import Database from "better-sqlite3";
|
||||
import { webuiDataDir } from "./dataDir";
|
||||
|
||||
export type UserRole = "admin" | "user";
|
||||
|
||||
export interface WebuiUser {
|
||||
username: string;
|
||||
role: UserRole;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
interface UserRow {
|
||||
username: string;
|
||||
password_hash: string;
|
||||
role: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
const SCRYPT_N = 16384;
|
||||
const SCRYPT_R = 8;
|
||||
const SCRYPT_P = 1;
|
||||
const SCRYPT_KEYLEN = 64;
|
||||
const MAX_USERNAME_LENGTH = 64;
|
||||
const USERNAME_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
||||
|
||||
export class UserStoreError extends Error {}
|
||||
|
||||
const globalUsers = globalThis as { __evoscientistWebuiUsers?: Database.Database };
|
||||
|
||||
function databasePath(): string {
|
||||
return path.join(webuiDataDir(), "webui-users.sqlite3");
|
||||
}
|
||||
|
||||
function openDatabase(): Database.Database {
|
||||
if (globalUsers.__evoscientistWebuiUsers?.open) {
|
||||
return globalUsers.__evoscientistWebuiUsers;
|
||||
}
|
||||
const target = databasePath();
|
||||
fs.mkdirSync(path.dirname(target), { recursive: true, mode: 0o700 });
|
||||
const db = new Database(target);
|
||||
db.pragma("journal_mode = WAL");
|
||||
db.pragma("synchronous = FULL");
|
||||
db.pragma("busy_timeout = 5000");
|
||||
db.pragma("foreign_keys = ON");
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
username TEXT PRIMARY KEY,
|
||||
password_hash TEXT NOT NULL,
|
||||
role TEXT NOT NULL CHECK (role IN ('admin', 'user')),
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
)
|
||||
`);
|
||||
try {
|
||||
fs.chmodSync(target, 0o600);
|
||||
} catch {
|
||||
// Best effort on filesystems without POSIX permission support.
|
||||
}
|
||||
globalUsers.__evoscientistWebuiUsers = db;
|
||||
return db;
|
||||
}
|
||||
|
||||
/** Test hook: close the shared handle so each suite can use its own dir. */
|
||||
export function closeUserStoreForTests(): void {
|
||||
if (globalUsers.__evoscientistWebuiUsers?.open) {
|
||||
globalUsers.__evoscientistWebuiUsers.close();
|
||||
}
|
||||
globalUsers.__evoscientistWebuiUsers = undefined;
|
||||
}
|
||||
|
||||
function hashPassword(password: string): string {
|
||||
const salt = randomBytes(16);
|
||||
const derived = scryptSync(password, salt, SCRYPT_KEYLEN, {
|
||||
N: SCRYPT_N,
|
||||
r: SCRYPT_R,
|
||||
p: SCRYPT_P,
|
||||
});
|
||||
return [
|
||||
"scrypt",
|
||||
String(SCRYPT_N),
|
||||
String(SCRYPT_R),
|
||||
String(SCRYPT_P),
|
||||
salt.toString("base64"),
|
||||
derived.toString("base64"),
|
||||
].join("$");
|
||||
}
|
||||
|
||||
function verifyPassword(password: string, stored: string): boolean {
|
||||
const parts = stored.split("$");
|
||||
if (parts.length !== 6 || parts[0] !== "scrypt") return false;
|
||||
const [, n, r, p, saltB64, hashB64] = parts;
|
||||
const salt = Buffer.from(saltB64, "base64");
|
||||
const expected = Buffer.from(hashB64, "base64");
|
||||
let derived: Buffer;
|
||||
try {
|
||||
derived = scryptSync(password, salt, expected.length, {
|
||||
N: Number(n),
|
||||
r: Number(r),
|
||||
p: Number(p),
|
||||
});
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
return timingSafeEqual(derived, expected);
|
||||
}
|
||||
|
||||
function toUser(row: UserRow): WebuiUser {
|
||||
return {
|
||||
username: row.username,
|
||||
role: row.role === "admin" ? "admin" : "user",
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at,
|
||||
};
|
||||
}
|
||||
|
||||
export function validateUsername(username: string): string | null {
|
||||
if (!username || username.length > MAX_USERNAME_LENGTH) {
|
||||
return `Username must be 1-${MAX_USERNAME_LENGTH} characters.`;
|
||||
}
|
||||
if (!USERNAME_PATTERN.test(username)) {
|
||||
return "Username may only contain letters, digits, '.', '_' and '-', and must start with a letter or digit.";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function countUsers(): number {
|
||||
const row = openDatabase()
|
||||
.prepare("SELECT COUNT(*) AS count FROM users")
|
||||
.get() as { count: number };
|
||||
return row.count;
|
||||
}
|
||||
|
||||
export function listUsers(): WebuiUser[] {
|
||||
const rows = openDatabase()
|
||||
.prepare("SELECT * FROM users ORDER BY username")
|
||||
.all() as UserRow[];
|
||||
return rows.map(toUser);
|
||||
}
|
||||
|
||||
export function getUser(username: string): WebuiUser | null {
|
||||
const row = openDatabase()
|
||||
.prepare("SELECT * FROM users WHERE username = ?")
|
||||
.get(username) as UserRow | undefined;
|
||||
return row ? toUser(row) : null;
|
||||
}
|
||||
|
||||
export function createUser(
|
||||
username: string,
|
||||
password: string,
|
||||
role: UserRole
|
||||
): WebuiUser {
|
||||
const invalid = validateUsername(username);
|
||||
if (invalid) throw new UserStoreError(invalid);
|
||||
if (getUser(username)) {
|
||||
throw new UserStoreError(`User '${username}' already exists.`);
|
||||
}
|
||||
const now = new Date().toISOString();
|
||||
openDatabase()
|
||||
.prepare(
|
||||
"INSERT INTO users (username, password_hash, role, created_at, updated_at) VALUES (?, ?, ?, ?, ?)"
|
||||
)
|
||||
.run(username, hashPassword(password), role, now, now);
|
||||
return { username, role, createdAt: now, updatedAt: now };
|
||||
}
|
||||
|
||||
export function setUserPassword(username: string, password: string): void {
|
||||
const result = openDatabase()
|
||||
.prepare("UPDATE users SET password_hash = ?, updated_at = ? WHERE username = ?")
|
||||
.run(hashPassword(password), new Date().toISOString(), username);
|
||||
if (result.changes === 0) {
|
||||
throw new UserStoreError(`User '${username}' does not exist.`);
|
||||
}
|
||||
}
|
||||
|
||||
export function setUserRole(username: string, role: UserRole): void {
|
||||
const result = openDatabase()
|
||||
.prepare("UPDATE users SET role = ?, updated_at = ? WHERE username = ?")
|
||||
.run(role, new Date().toISOString(), username);
|
||||
if (result.changes === 0) {
|
||||
throw new UserStoreError(`User '${username}' does not exist.`);
|
||||
}
|
||||
}
|
||||
|
||||
export function deleteUser(username: string): void {
|
||||
const result = openDatabase()
|
||||
.prepare("DELETE FROM users WHERE username = ?")
|
||||
.run(username);
|
||||
if (result.changes === 0) {
|
||||
throw new UserStoreError(`User '${username}' does not exist.`);
|
||||
}
|
||||
}
|
||||
|
||||
export function verifyUserPassword(
|
||||
username: string,
|
||||
password: string
|
||||
): WebuiUser | null {
|
||||
const row = openDatabase()
|
||||
.prepare("SELECT * FROM users WHERE username = ?")
|
||||
.get(username) as UserRow | undefined;
|
||||
if (!row || !verifyPassword(password, row.password_hash)) return null;
|
||||
return toUser(row);
|
||||
}
|
||||
|
||||
/**
|
||||
* Seed the first admin from the deployment environment when the store is
|
||||
* empty (design doc 7.2: the first admin comes from env or an init command;
|
||||
* later users are managed by admins through the API).
|
||||
*/
|
||||
export function ensureBootstrapAdmin(): void {
|
||||
if (countUsers() > 0) return;
|
||||
const username = process.env.WEBUI_AUTH_USERNAME?.trim();
|
||||
const password = process.env.WEBUI_AUTH_PASSWORD;
|
||||
if (!username || !password) {
|
||||
throw new UserStoreError(
|
||||
"No WebUI users exist yet. Set WEBUI_AUTH_USERNAME and WEBUI_AUTH_PASSWORD to create the first admin."
|
||||
);
|
||||
}
|
||||
createUser(username, password, "admin");
|
||||
}
|
||||
+8
-9
@@ -11,6 +11,10 @@ interface SessionPayload {
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
// The middleware only verifies the HMAC signature and expiry; the username
|
||||
// and role are re-checked against the user store by the API routes, so
|
||||
// bootstrap/env credentials are not required here.
|
||||
|
||||
function base64UrlBytes(value: string): Uint8Array | null {
|
||||
try {
|
||||
const padded = value
|
||||
@@ -32,8 +36,7 @@ function arrayBuffer(bytes: Uint8Array): ArrayBuffer {
|
||||
|
||||
async function isValidSession(token: string | undefined): Promise<boolean> {
|
||||
const secret = authSecret();
|
||||
const expectedUsername = process.env.WEBUI_AUTH_USERNAME?.trim();
|
||||
if (!token || !secret || !expectedUsername) return false;
|
||||
if (!token || !secret) return false;
|
||||
const [encoded, signature] = token.split(".");
|
||||
if (!encoded || !signature) return false;
|
||||
const signatureBytes = base64UrlBytes(signature);
|
||||
@@ -59,7 +62,8 @@ async function isValidSession(token: string | undefined): Promise<boolean> {
|
||||
new TextDecoder().decode(raw)
|
||||
) as Partial<SessionPayload>;
|
||||
return (
|
||||
payload.username === expectedUsername &&
|
||||
typeof payload.username === "string" &&
|
||||
payload.username.length > 0 &&
|
||||
typeof payload.expiresAt === "number" &&
|
||||
payload.expiresAt > Math.floor(Date.now() / 1000)
|
||||
);
|
||||
@@ -98,12 +102,7 @@ export async function proxy(request: NextRequest) {
|
||||
if (!isAuthenticationEnabled() || isPublicPath(request.nextUrl.pathname)) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
if (
|
||||
!authSecret() ||
|
||||
authSecret()!.length < 32 ||
|
||||
!process.env.WEBUI_AUTH_USERNAME?.trim() ||
|
||||
!process.env.WEBUI_AUTH_PASSWORD
|
||||
) {
|
||||
if (!authSecret() || authSecret()!.length < 32) {
|
||||
return configurationError(request);
|
||||
}
|
||||
const token = request.cookies.get(AUTH_COOKIE_NAME)?.value;
|
||||
|
||||
Reference in New Issue
Block a user