feat(web): per-device provider settings (#4479)
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
1 parent
48aa875c0e
commit
95305c36fa
16 files changed
+1961
-688
No files matched your search
@@ -1,6 +1,28 @@
|
||||
import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
|
||||
|
||||
import { cn } from "~/lib/utils";
|
||||
import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
|
||||
|
||||
/** Canonical connection-phase → dot color mapping shared by every status dot. */
|
||||
export function connectionPhaseDotClassName(phase: EnvironmentConnectionPhase): string {
|
||||
switch (phase) {
|
||||
case "connected":
|
||||
return "bg-success";
|
||||
case "connecting":
|
||||
case "reconnecting":
|
||||
return "bg-warning";
|
||||
case "error":
|
||||
return "bg-destructive";
|
||||
default:
|
||||
return "bg-muted-foreground/40";
|
||||
}
|
||||
}
|
||||
|
||||
/** Ping halo for transitional phases; null renders no ping. */
|
||||
export function connectionPhasePingClassName(phase: EnvironmentConnectionPhase): string | null {
|
||||
return phase === "connecting" || phase === "reconnecting" ? "bg-warning/60 duration-2000" : null;
|
||||
}
|
||||
|
||||
type ConnectionStatusDotProps = {
|
||||
tooltipText?: string | null;
|
||||
dotClassName: string;
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import { EnvironmentId } from "@t3tools/contracts";
|
||||
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
|
||||
|
||||
import { reactHookHarness as hooks } from "../../test/reactHookHarness";
|
||||
|
||||
const settingsHooks = vi.hoisted(() => ({
|
||||
read: vi.fn(() => ({ providerInstances: {} })),
|
||||
update: vi.fn(() => vi.fn()),
|
||||
}));
|
||||
|
||||
vi.mock("react", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("react")>();
|
||||
const { reactHookHarness } = await import("../../test/reactHookHarness");
|
||||
return {
|
||||
...actual,
|
||||
useMemo: reactHookHarness.useMemo,
|
||||
useState: reactHookHarness.useState,
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("react/compiler-runtime", async () => {
|
||||
const { reactHookHarness } = await import("../../test/reactHookHarness");
|
||||
return { c: reactHookHarness.useMemoCache };
|
||||
});
|
||||
|
||||
vi.mock("../../hooks/useSettings", () => ({
|
||||
useEnvironmentSettings: settingsHooks.read,
|
||||
useUpdateEnvironmentSettings: settingsHooks.update,
|
||||
}));
|
||||
|
||||
import { AddProviderInstanceDialog } from "./AddProviderInstanceDialog";
|
||||
|
||||
const remoteEnvironmentId = EnvironmentId.make("remote-device");
|
||||
|
||||
describe("AddProviderInstanceDialog environment routing", () => {
|
||||
beforeEach(() => {
|
||||
hooks.reset();
|
||||
settingsHooks.read.mockClear();
|
||||
settingsHooks.update.mockClear();
|
||||
});
|
||||
|
||||
it("reads and writes settings through the supplied environment", () => {
|
||||
hooks.beginRender();
|
||||
AddProviderInstanceDialog({
|
||||
open: true,
|
||||
environmentId: remoteEnvironmentId,
|
||||
environmentLabel: "Remote device",
|
||||
onOpenChange: vi.fn(),
|
||||
});
|
||||
|
||||
expect(settingsHooks.read).toHaveBeenCalledWith(remoteEnvironmentId);
|
||||
expect(settingsHooks.update).toHaveBeenCalledWith(remoteEnvironmentId);
|
||||
});
|
||||
});
|
||||
@@ -6,10 +6,11 @@ import { useMemo, useState } from "react";
|
||||
import {
|
||||
ProviderInstanceId,
|
||||
ProviderDriverKind,
|
||||
type EnvironmentId,
|
||||
type ProviderInstanceConfig,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
import { usePrimarySettings, useUpdatePrimarySettings } from "../../hooks/useSettings";
|
||||
import { useEnvironmentSettings, useUpdateEnvironmentSettings } from "../../hooks/useSettings";
|
||||
import { cn } from "../../lib/utils";
|
||||
import { normalizeProviderAccentColor } from "../../providerInstances";
|
||||
import { Button } from "../ui/button";
|
||||
@@ -115,13 +116,20 @@ function validateInstanceId(id: string, existing: ReadonlySet<string>): string |
|
||||
}
|
||||
|
||||
interface AddProviderInstanceDialogProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
readonly open: boolean;
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly environmentLabel: string;
|
||||
readonly onOpenChange: (open: boolean) => void;
|
||||
}
|
||||
|
||||
export function AddProviderInstanceDialog({ open, onOpenChange }: AddProviderInstanceDialogProps) {
|
||||
const settings = usePrimarySettings();
|
||||
const updateSettings = useUpdatePrimarySettings();
|
||||
export function AddProviderInstanceDialog({
|
||||
open,
|
||||
environmentId,
|
||||
environmentLabel,
|
||||
onOpenChange,
|
||||
}: AddProviderInstanceDialogProps) {
|
||||
const settings = useEnvironmentSettings(environmentId);
|
||||
const updateSettings = useUpdateEnvironmentSettings(environmentId);
|
||||
|
||||
const [wizardStep, setWizardStep] = useState(0);
|
||||
const [driver, setDriver] = useState<ProviderDriverKind>(DEFAULT_DRIVER_KIND);
|
||||
@@ -227,8 +235,8 @@ export function AddProviderInstanceDialog({ open, onOpenChange }: AddProviderIns
|
||||
<DialogHeader>
|
||||
<DialogTitle>Add provider instance</DialogTitle>
|
||||
<DialogDescription>
|
||||
Configure an additional provider instance — for example, a second Codex install
|
||||
pointed at a different workspace.
|
||||
Configure an additional provider instance on {environmentLabel} — for example, a
|
||||
second Codex install pointed at a different workspace.
|
||||
</DialogDescription>
|
||||
<AddProviderInstanceWizardSteps
|
||||
currentStep={wizardStep}
|
||||
|
||||
@@ -0,0 +1,256 @@
|
||||
import type { ReactElement } from "react";
|
||||
import {
|
||||
DEFAULT_UNIFIED_SETTINGS,
|
||||
EnvironmentId,
|
||||
ProviderDriverKind,
|
||||
ProviderInstanceId,
|
||||
type ServerProvider,
|
||||
type UnifiedSettings,
|
||||
} from "@t3tools/contracts";
|
||||
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
|
||||
|
||||
import { visitElements } from "../../test/reactElementTree";
|
||||
import { reactHookHarness as hooks } from "../../test/reactHookHarness";
|
||||
|
||||
const atoms = vi.hoisted(() => ({
|
||||
providers: null as ReadonlyArray<ServerProvider> | null,
|
||||
providersAtom: Symbol("providers"),
|
||||
refreshProviders: Symbol("refreshProviders"),
|
||||
updateProvider: Symbol("updateProvider"),
|
||||
}));
|
||||
|
||||
const commands = vi.hoisted(() => ({
|
||||
refresh: vi.fn(),
|
||||
updateProvider: vi.fn(),
|
||||
}));
|
||||
|
||||
const settingsState = vi.hoisted(() => ({
|
||||
value: null as UnifiedSettings | null,
|
||||
readEnvironmentIds: [] as EnvironmentId[],
|
||||
updateEnvironmentIds: [] as EnvironmentId[],
|
||||
updateSettings: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("react", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("react")>();
|
||||
const { reactHookHarness } = await import("../../test/reactHookHarness");
|
||||
return {
|
||||
...actual,
|
||||
useCallback: reactHookHarness.useCallback,
|
||||
useMemo: reactHookHarness.useMemo,
|
||||
useRef: reactHookHarness.useRef,
|
||||
useState: reactHookHarness.useState,
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("react/compiler-runtime", async () => {
|
||||
const { reactHookHarness } = await import("../../test/reactHookHarness");
|
||||
return { c: reactHookHarness.useMemoCache };
|
||||
});
|
||||
|
||||
vi.mock("@effect/atom-react", () => ({
|
||||
useAtomValue: () => atoms.providers,
|
||||
}));
|
||||
|
||||
vi.mock("../../state/server", () => ({
|
||||
EMPTY_SERVER_PROVIDERS: [],
|
||||
serverEnvironment: {
|
||||
providersValueAtom: () => atoms.providersAtom,
|
||||
refreshProviders: atoms.refreshProviders,
|
||||
updateProvider: atoms.updateProvider,
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("../../state/use-atom-command", () => ({
|
||||
useAtomCommand: (atom: symbol) =>
|
||||
atom === atoms.refreshProviders ? commands.refresh : commands.updateProvider,
|
||||
}));
|
||||
|
||||
vi.mock("../../hooks/useSettings", () => ({
|
||||
useEnvironmentSettings: (environmentId: EnvironmentId) => {
|
||||
settingsState.readEnvironmentIds.push(environmentId);
|
||||
return settingsState.value;
|
||||
},
|
||||
useUpdateEnvironmentSettings: (environmentId: EnvironmentId) => {
|
||||
settingsState.updateEnvironmentIds.push(environmentId);
|
||||
return settingsState.updateSettings;
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("../../environments/primary", () => ({
|
||||
usePrimarySessionState: () => ({ data: null, error: null, isPending: false, refresh: vi.fn() }),
|
||||
}));
|
||||
|
||||
vi.mock("../../state/session", () => ({
|
||||
useEnvironmentSessionState: () => ({ data: null, hasError: false, isPending: true }),
|
||||
}));
|
||||
|
||||
import { EnvironmentProviderSettings } from "./ProviderSettingsPanel";
|
||||
|
||||
const environmentId = EnvironmentId.make("remote-device");
|
||||
const codexId = ProviderInstanceId.make("codex");
|
||||
const customId = ProviderInstanceId.make("codex_work");
|
||||
|
||||
function provider(): ServerProvider {
|
||||
return {
|
||||
instanceId: codexId,
|
||||
driver: ProviderDriverKind.make("codex"),
|
||||
enabled: true,
|
||||
installed: true,
|
||||
version: "1.0.0",
|
||||
status: "ready",
|
||||
auth: { status: "authenticated" },
|
||||
checkedAt: "2026-07-24T12:00:00.000Z",
|
||||
models: [],
|
||||
slashCommands: [],
|
||||
skills: [],
|
||||
versionAdvisory: {
|
||||
status: "behind_latest",
|
||||
currentVersion: "1.0.0",
|
||||
latestVersion: "1.1.0",
|
||||
updateCommand: "pnpm add -g @openai/codex@latest",
|
||||
canUpdate: true,
|
||||
checkedAt: "2026-07-24T12:00:00.000Z",
|
||||
message: "Update available.",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function renderPanel(options?: {
|
||||
readonly readOnly?: boolean;
|
||||
}): ReactElement<Record<string, unknown>> {
|
||||
hooks.beginRender();
|
||||
return EnvironmentProviderSettings({
|
||||
environmentId,
|
||||
environmentLabel: "Remote device",
|
||||
...(options?.readOnly === undefined ? {} : { readOnly: options.readOnly }),
|
||||
}) as ReactElement<Record<string, unknown>>;
|
||||
}
|
||||
|
||||
async function flushPromises(): Promise<void> {
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
}
|
||||
|
||||
describe("EnvironmentProviderSettings routing", () => {
|
||||
beforeEach(() => {
|
||||
hooks.reset();
|
||||
atoms.providers = null;
|
||||
settingsState.value = DEFAULT_UNIFIED_SETTINGS;
|
||||
settingsState.readEnvironmentIds = [];
|
||||
settingsState.updateEnvironmentIds = [];
|
||||
settingsState.updateSettings.mockReset();
|
||||
commands.refresh.mockReset().mockResolvedValue({ _tag: "Success" });
|
||||
commands.updateProvider.mockReset().mockResolvedValue({ _tag: "Success" });
|
||||
});
|
||||
|
||||
it("coalesces a nullable provider snapshot before rendering array-backed UI", () => {
|
||||
expect(() => renderPanel()).not.toThrow();
|
||||
expect(settingsState.readEnvironmentIds).toEqual([environmentId]);
|
||||
expect(settingsState.updateEnvironmentIds).toEqual([environmentId]);
|
||||
});
|
||||
|
||||
it("routes refresh and provider update commands to the selected environment", async () => {
|
||||
atoms.providers = [provider()];
|
||||
const panel = renderPanel();
|
||||
const refreshButton = visitElements(
|
||||
panel,
|
||||
(element) => element.props["aria-label"] === "Refresh provider status",
|
||||
);
|
||||
expect(refreshButton).not.toBeNull();
|
||||
(refreshButton?.props.onClick as (() => void) | undefined)?.();
|
||||
await flushPromises();
|
||||
|
||||
expect(commands.refresh).toHaveBeenCalledWith({ environmentId, input: {} });
|
||||
|
||||
const providerCard = visitElements(
|
||||
panel,
|
||||
(element) =>
|
||||
element.props.instanceId === codexId && typeof element.props.onRunUpdate === "function",
|
||||
);
|
||||
expect(providerCard).not.toBeNull();
|
||||
(providerCard?.props.onRunUpdate as (() => void) | undefined)?.();
|
||||
await flushPromises();
|
||||
|
||||
expect(commands.updateProvider).toHaveBeenCalledWith({
|
||||
environmentId,
|
||||
input: { provider: ProviderDriverKind.make("codex"), instanceId: codexId },
|
||||
});
|
||||
});
|
||||
|
||||
it("renders the provider layout inert with a limited-permissions notice when read only", () => {
|
||||
atoms.providers = [provider()];
|
||||
const panel = renderPanel({ readOnly: true });
|
||||
|
||||
const inertWrapper = visitElements(panel, (element) => element.props.inert === true);
|
||||
expect(inertWrapper).not.toBeNull();
|
||||
const providerCard = visitElements(panel, (element) => element.props.instanceId === codexId);
|
||||
expect(providerCard).not.toBeNull();
|
||||
|
||||
const notice = visitElements(panel, (element) => element.props.title === "Limited permissions");
|
||||
expect(notice).not.toBeNull();
|
||||
|
||||
expect(
|
||||
visitElements(panel, (element) => element.props["aria-label"] === "Add provider instance"),
|
||||
).toBeNull();
|
||||
expect(
|
||||
visitElements(panel, (element) => element.props["aria-label"] === "Refresh provider status"),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("keeps the editable layout interactive when not read only", () => {
|
||||
atoms.providers = [provider()];
|
||||
const panel = renderPanel();
|
||||
expect(visitElements(panel, (element) => element.props.inert === true)).toBeNull();
|
||||
expect(
|
||||
visitElements(panel, (element) => element.props.title === "Limited permissions"),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("deletes and resets provider configuration without erasing shared preferences", () => {
|
||||
settingsState.value = {
|
||||
...DEFAULT_UNIFIED_SETTINGS,
|
||||
providerInstances: {
|
||||
[codexId]: {
|
||||
driver: ProviderDriverKind.make("codex"),
|
||||
enabled: false,
|
||||
},
|
||||
[customId]: {
|
||||
driver: ProviderDriverKind.make("codex"),
|
||||
enabled: true,
|
||||
},
|
||||
},
|
||||
providerModelPreferences: {
|
||||
[customId]: { hiddenModels: ["hidden"], modelOrder: ["model"] },
|
||||
},
|
||||
favorites: [{ provider: customId, model: "favorite" }],
|
||||
};
|
||||
const panel = renderPanel();
|
||||
const customCard = visitElements(panel, (element) => element.props.instanceId === customId);
|
||||
expect(customCard).not.toBeNull();
|
||||
(customCard?.props.onDelete as (() => void) | undefined)?.();
|
||||
|
||||
expect(settingsState.updateSettings).toHaveBeenLastCalledWith({
|
||||
providerInstances: {
|
||||
[codexId]: settingsState.value.providerInstances?.[codexId],
|
||||
},
|
||||
});
|
||||
|
||||
settingsState.updateSettings.mockClear();
|
||||
const defaultCard = visitElements(panel, (element) => element.props.instanceId === codexId);
|
||||
const resetAction = defaultCard?.props.headerAction;
|
||||
const resetButton = visitElements(
|
||||
resetAction,
|
||||
(element) => typeof element.props.onClick === "function",
|
||||
);
|
||||
expect(resetButton).not.toBeNull();
|
||||
(resetButton?.props.onClick as (() => void) | undefined)?.();
|
||||
|
||||
const resetPatch = settingsState.updateSettings.mock.lastCall?.[0] as
|
||||
| Record<string, unknown>
|
||||
| undefined;
|
||||
expect(Object.keys(resetPatch ?? {}).sort()).toEqual(["providerInstances", "providers"]);
|
||||
expect(resetPatch).not.toHaveProperty("favorites");
|
||||
expect(resetPatch).not.toHaveProperty("providerModelPreferences");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,260 @@
|
||||
import { AuthOrchestrationOperateScope, EnvironmentId } from "@t3tools/contracts";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import {
|
||||
buildProviderEnvironmentOptions,
|
||||
classifyProviderEnvironmentAccess,
|
||||
resolvePrimaryOperateAccess,
|
||||
resolveRemoteOperateAccess,
|
||||
resolveSelectedProviderEnvironmentId,
|
||||
} from "./ProviderSettingsPanel.logic";
|
||||
|
||||
const primaryId = EnvironmentId.make("primary");
|
||||
const relayId = EnvironmentId.make("relay");
|
||||
const sshId = EnvironmentId.make("ssh");
|
||||
|
||||
const environments = [
|
||||
{ environmentId: sshId, label: "Zulu SSH" },
|
||||
{ environmentId: relayId, label: "Alpha Relay" },
|
||||
{ environmentId: primaryId, label: "This device" },
|
||||
] as const;
|
||||
|
||||
describe("provider environment selection", () => {
|
||||
it("sorts the primary environment first and the rest by label", () => {
|
||||
expect(
|
||||
buildProviderEnvironmentOptions(environments, primaryId).map(
|
||||
(environment) => environment.environmentId,
|
||||
),
|
||||
).toEqual([primaryId, relayId, sshId]);
|
||||
});
|
||||
|
||||
it("keeps a valid selection, then falls back to primary or the first environment", () => {
|
||||
const options = buildProviderEnvironmentOptions(environments, primaryId);
|
||||
|
||||
expect(resolveSelectedProviderEnvironmentId(options, sshId, primaryId)).toBe(sshId);
|
||||
expect(
|
||||
resolveSelectedProviderEnvironmentId(
|
||||
options.filter((environment) => environment.environmentId !== sshId),
|
||||
sshId,
|
||||
primaryId,
|
||||
),
|
||||
).toBe(primaryId);
|
||||
expect(resolveSelectedProviderEnvironmentId(options.slice(1), primaryId, primaryId)).toBe(
|
||||
relayId,
|
||||
);
|
||||
expect(resolveSelectedProviderEnvironmentId([], null, primaryId)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("provider environment access", () => {
|
||||
it("allows connected environments with config and operate access", () => {
|
||||
expect(
|
||||
classifyProviderEnvironmentAccess({
|
||||
connectionPhase: "connected",
|
||||
hasServerConfig: true,
|
||||
operateAccess: "granted",
|
||||
}),
|
||||
).toEqual({ kind: "editable" });
|
||||
});
|
||||
|
||||
it("waits for config before exposing controls", () => {
|
||||
expect(
|
||||
classifyProviderEnvironmentAccess({
|
||||
connectionPhase: "connected",
|
||||
hasServerConfig: false,
|
||||
operateAccess: "granted",
|
||||
}),
|
||||
).toEqual({ kind: "loading", reason: "config" });
|
||||
});
|
||||
|
||||
it("waits for unresolved operate access instead of assuming it is editable", () => {
|
||||
expect(
|
||||
classifyProviderEnvironmentAccess({
|
||||
connectionPhase: "connected",
|
||||
hasServerConfig: true,
|
||||
operateAccess: "pending",
|
||||
}),
|
||||
).toEqual({ kind: "loading", reason: "permissions" });
|
||||
});
|
||||
|
||||
it("represents known missing operate access as read only", () => {
|
||||
expect(
|
||||
classifyProviderEnvironmentAccess({
|
||||
connectionPhase: "connected",
|
||||
hasServerConfig: true,
|
||||
operateAccess: "denied",
|
||||
}),
|
||||
).toEqual({ kind: "read-only" });
|
||||
});
|
||||
|
||||
it.each(["available", "offline", "connecting", "reconnecting"] as const)(
|
||||
"keeps %s environments unavailable",
|
||||
(connectionPhase) => {
|
||||
expect(
|
||||
classifyProviderEnvironmentAccess({
|
||||
connectionPhase,
|
||||
hasServerConfig: true,
|
||||
operateAccess: "granted",
|
||||
}),
|
||||
).toEqual({ kind: "unavailable" });
|
||||
},
|
||||
);
|
||||
|
||||
it("separates connection errors from other unavailable states", () => {
|
||||
expect(
|
||||
classifyProviderEnvironmentAccess({
|
||||
connectionPhase: "error",
|
||||
hasServerConfig: true,
|
||||
operateAccess: "granted",
|
||||
}),
|
||||
).toEqual({ kind: "error" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("primary operate access", () => {
|
||||
const authenticated = {
|
||||
authenticated: true as const,
|
||||
scopes: [AuthOrchestrationOperateScope],
|
||||
};
|
||||
|
||||
it("keeps cached session data authoritative while SWR revalidates", () => {
|
||||
expect(
|
||||
resolvePrimaryOperateAccess({
|
||||
isPrimary: true,
|
||||
hasDesktopBridge: false,
|
||||
session: authenticated,
|
||||
isPending: true,
|
||||
hasError: false,
|
||||
}),
|
||||
).toBe("granted");
|
||||
});
|
||||
|
||||
it("reports pending only before any session has resolved", () => {
|
||||
expect(
|
||||
resolvePrimaryOperateAccess({
|
||||
isPrimary: true,
|
||||
hasDesktopBridge: false,
|
||||
session: null,
|
||||
isPending: true,
|
||||
hasError: false,
|
||||
}),
|
||||
).toBe("pending");
|
||||
});
|
||||
|
||||
it("treats a failed session fetch as a transport problem, not a denial", () => {
|
||||
expect(
|
||||
resolvePrimaryOperateAccess({
|
||||
isPrimary: true,
|
||||
hasDesktopBridge: false,
|
||||
session: null,
|
||||
isPending: false,
|
||||
hasError: true,
|
||||
}),
|
||||
).toBe("granted");
|
||||
});
|
||||
|
||||
it("denies unauthenticated sessions and sessions without the operate scope", () => {
|
||||
expect(
|
||||
resolvePrimaryOperateAccess({
|
||||
isPrimary: true,
|
||||
hasDesktopBridge: false,
|
||||
session: { authenticated: false },
|
||||
isPending: false,
|
||||
hasError: false,
|
||||
}),
|
||||
).toBe("denied");
|
||||
expect(
|
||||
resolvePrimaryOperateAccess({
|
||||
isPrimary: true,
|
||||
hasDesktopBridge: false,
|
||||
session: { authenticated: true, scopes: ["orchestration:read"] },
|
||||
isPending: false,
|
||||
hasError: false,
|
||||
}),
|
||||
).toBe("denied");
|
||||
expect(
|
||||
resolvePrimaryOperateAccess({
|
||||
isPrimary: true,
|
||||
hasDesktopBridge: false,
|
||||
session: null,
|
||||
isPending: false,
|
||||
hasError: false,
|
||||
}),
|
||||
).toBe("denied");
|
||||
});
|
||||
|
||||
it("grants desktop bridge and remote environments without blocking on the primary session", () => {
|
||||
expect(
|
||||
resolvePrimaryOperateAccess({
|
||||
isPrimary: true,
|
||||
hasDesktopBridge: true,
|
||||
session: null,
|
||||
isPending: true,
|
||||
hasError: false,
|
||||
}),
|
||||
).toBe("granted");
|
||||
expect(
|
||||
resolvePrimaryOperateAccess({
|
||||
isPrimary: false,
|
||||
hasDesktopBridge: false,
|
||||
session: null,
|
||||
isPending: true,
|
||||
hasError: false,
|
||||
}),
|
||||
).toBe("granted");
|
||||
});
|
||||
});
|
||||
|
||||
describe("remote operate access", () => {
|
||||
it("derives access from the environment session's granted scopes", () => {
|
||||
expect(
|
||||
resolveRemoteOperateAccess({
|
||||
session: { authenticated: true, scopes: [AuthOrchestrationOperateScope] },
|
||||
isPending: false,
|
||||
hasError: false,
|
||||
}),
|
||||
).toBe("granted");
|
||||
expect(
|
||||
resolveRemoteOperateAccess({
|
||||
session: { authenticated: true, scopes: ["orchestration:read"] },
|
||||
isPending: false,
|
||||
hasError: false,
|
||||
}),
|
||||
).toBe("denied");
|
||||
expect(
|
||||
resolveRemoteOperateAccess({
|
||||
session: { authenticated: false },
|
||||
isPending: false,
|
||||
hasError: false,
|
||||
}),
|
||||
).toBe("denied");
|
||||
});
|
||||
|
||||
it("reports pending before the first session resolve, then keeps cached data", () => {
|
||||
expect(resolveRemoteOperateAccess({ session: null, isPending: true, hasError: false })).toBe(
|
||||
"pending",
|
||||
);
|
||||
expect(
|
||||
resolveRemoteOperateAccess({
|
||||
session: { authenticated: true, scopes: [AuthOrchestrationOperateScope] },
|
||||
isPending: true,
|
||||
hasError: false,
|
||||
}),
|
||||
).toBe("granted");
|
||||
});
|
||||
|
||||
it("stays optimistic when the session fetch fails or an older server omits scopes", () => {
|
||||
// Transport failures and pre-scope-reporting servers are not permission
|
||||
// decisions; the environment RPC layer still rejects unauthorized writes.
|
||||
expect(resolveRemoteOperateAccess({ session: null, isPending: false, hasError: true })).toBe(
|
||||
"granted",
|
||||
);
|
||||
expect(
|
||||
resolveRemoteOperateAccess({
|
||||
session: { authenticated: true },
|
||||
isPending: false,
|
||||
hasError: false,
|
||||
}),
|
||||
).toBe("granted");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,160 @@
|
||||
import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
|
||||
import {
|
||||
AuthOrchestrationOperateScope,
|
||||
type AuthSessionState,
|
||||
type EnvironmentId,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
export interface ProviderEnvironmentOptionLike {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly label: string;
|
||||
}
|
||||
|
||||
export function buildProviderEnvironmentOptions<T extends ProviderEnvironmentOptionLike>(
|
||||
environments: ReadonlyArray<T>,
|
||||
primaryEnvironmentId: EnvironmentId | null,
|
||||
): ReadonlyArray<T> {
|
||||
return environments.toSorted((left, right) => {
|
||||
const leftIsPrimary = left.environmentId === primaryEnvironmentId;
|
||||
const rightIsPrimary = right.environmentId === primaryEnvironmentId;
|
||||
if (leftIsPrimary !== rightIsPrimary) {
|
||||
return leftIsPrimary ? -1 : 1;
|
||||
}
|
||||
return (
|
||||
left.label.localeCompare(right.label) ||
|
||||
String(left.environmentId).localeCompare(String(right.environmentId))
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export function resolveSelectedProviderEnvironmentId(
|
||||
environments: ReadonlyArray<ProviderEnvironmentOptionLike>,
|
||||
selectedEnvironmentId: EnvironmentId | null,
|
||||
primaryEnvironmentId: EnvironmentId | null,
|
||||
): EnvironmentId | null {
|
||||
if (
|
||||
selectedEnvironmentId !== null &&
|
||||
environments.some((environment) => environment.environmentId === selectedEnvironmentId)
|
||||
) {
|
||||
return selectedEnvironmentId;
|
||||
}
|
||||
if (
|
||||
primaryEnvironmentId !== null &&
|
||||
environments.some((environment) => environment.environmentId === primaryEnvironmentId)
|
||||
) {
|
||||
return primaryEnvironmentId;
|
||||
}
|
||||
return environments[0]?.environmentId ?? null;
|
||||
}
|
||||
|
||||
export type ProviderEnvironmentAccess =
|
||||
| { readonly kind: "editable" }
|
||||
/** `reason` distinguishes waiting on the device from waiting on permissions. */
|
||||
| { readonly kind: "loading"; readonly reason: "config" | "permissions" }
|
||||
| { readonly kind: "read-only" }
|
||||
| { readonly kind: "unavailable" }
|
||||
| { readonly kind: "error" };
|
||||
|
||||
/**
|
||||
* Whether the session may change provider configuration on an environment.
|
||||
* `pending` means the answer is still unknown, which must not be presented as
|
||||
* editable: rendering controls we already know might be rejected only turns a
|
||||
* permission problem into a failed write.
|
||||
*/
|
||||
export type ProviderOperateAccess = "granted" | "denied" | "pending";
|
||||
|
||||
/**
|
||||
* Resolve operate access from an environment's `/api/auth/session` answer.
|
||||
*
|
||||
* Cached session data wins over an in-flight revalidation. The session atoms
|
||||
* are SWR-backed, so they report `isPending` on every background refresh;
|
||||
* treating that as unknown would flip a working panel back to loading and
|
||||
* discard in-progress edits.
|
||||
*
|
||||
* `missingScopesAccess` decides the case where the session resolved but did
|
||||
* not report scopes: the primary serves the web app itself so its server
|
||||
* always reports them (absence means denial), while a remote device may run an
|
||||
* older server version that predates scope reporting, where denial would lock
|
||||
* out a legitimate session. The environment RPC layer stays authoritative
|
||||
* either way.
|
||||
*/
|
||||
function resolveSessionOperateAccess(input: {
|
||||
readonly session: Pick<AuthSessionState, "authenticated" | "scopes"> | null;
|
||||
readonly isPending: boolean;
|
||||
readonly hasError: boolean;
|
||||
readonly missingScopesAccess: "granted" | "denied";
|
||||
}): ProviderOperateAccess {
|
||||
if (input.session === null) {
|
||||
if (input.isPending) {
|
||||
return "pending";
|
||||
}
|
||||
// A failed session fetch is a transport problem, not a permission
|
||||
// decision — locking the panel read-only would misreport it. Stay
|
||||
// optimistic; the environment RPC layer still rejects unauthorized writes.
|
||||
return input.hasError ? "granted" : "denied";
|
||||
}
|
||||
if (!input.session.authenticated) {
|
||||
return "denied";
|
||||
}
|
||||
if (input.session.scopes === undefined) {
|
||||
return input.missingScopesAccess;
|
||||
}
|
||||
return input.session.scopes.includes(AuthOrchestrationOperateScope) ? "granted" : "denied";
|
||||
}
|
||||
|
||||
/** Operate access for the primary environment's own browser session. */
|
||||
export function resolvePrimaryOperateAccess(input: {
|
||||
readonly isPrimary: boolean;
|
||||
readonly hasDesktopBridge: boolean;
|
||||
readonly session: Pick<AuthSessionState, "authenticated" | "scopes"> | null;
|
||||
readonly isPending: boolean;
|
||||
readonly hasError: boolean;
|
||||
}): ProviderOperateAccess {
|
||||
if (!input.isPrimary || input.hasDesktopBridge) {
|
||||
return "granted";
|
||||
}
|
||||
return resolveSessionOperateAccess({
|
||||
session: input.session,
|
||||
isPending: input.isPending,
|
||||
hasError: input.hasError,
|
||||
missingScopesAccess: "denied",
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Operate access for a non-primary environment, derived from the scopes its
|
||||
* `/api/auth/session` endpoint reports for this client's credential.
|
||||
*/
|
||||
export function resolveRemoteOperateAccess(input: {
|
||||
readonly session: Pick<AuthSessionState, "authenticated" | "scopes"> | null;
|
||||
readonly isPending: boolean;
|
||||
readonly hasError: boolean;
|
||||
}): ProviderOperateAccess {
|
||||
return resolveSessionOperateAccess({
|
||||
...input,
|
||||
missingScopesAccess: "granted",
|
||||
});
|
||||
}
|
||||
|
||||
export function classifyProviderEnvironmentAccess(input: {
|
||||
readonly connectionPhase: EnvironmentConnectionPhase;
|
||||
readonly hasServerConfig: boolean;
|
||||
readonly operateAccess: ProviderOperateAccess;
|
||||
}): ProviderEnvironmentAccess {
|
||||
if (input.connectionPhase === "error") {
|
||||
return { kind: "error" };
|
||||
}
|
||||
if (input.connectionPhase !== "connected") {
|
||||
return { kind: "unavailable" };
|
||||
}
|
||||
if (!input.hasServerConfig) {
|
||||
return { kind: "loading", reason: "config" };
|
||||
}
|
||||
if (input.operateAccess === "pending") {
|
||||
return { kind: "loading", reason: "permissions" };
|
||||
}
|
||||
if (input.operateAccess === "denied") {
|
||||
return { kind: "read-only" };
|
||||
}
|
||||
return { kind: "editable" };
|
||||
}
|
||||
@@ -0,0 +1,902 @@
|
||||
import { useAtomValue } from "@effect/atom-react";
|
||||
import { connectionStatusText } from "@t3tools/client-runtime/connection";
|
||||
import { safeErrorLogAttributes } from "@t3tools/client-runtime/errors";
|
||||
import {
|
||||
isAtomCommandInterrupted,
|
||||
squashAtomCommandFailure,
|
||||
} from "@t3tools/client-runtime/state/runtime";
|
||||
import {
|
||||
defaultInstanceIdForDriver,
|
||||
type EnvironmentId,
|
||||
PROVIDER_DISPLAY_NAMES,
|
||||
ProviderDriverKind,
|
||||
type ProviderInstanceConfig,
|
||||
type ProviderInstanceId,
|
||||
} from "@t3tools/contracts";
|
||||
import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings";
|
||||
import {
|
||||
getBackgroundActivityPresetSettings,
|
||||
resolveServerBackgroundActivitySettings,
|
||||
} from "@t3tools/shared/backgroundActivitySettings";
|
||||
import * as Arr from "effect/Array";
|
||||
import * as Duration from "effect/Duration";
|
||||
import * as Equal from "effect/Equal";
|
||||
import * as Result from "effect/Result";
|
||||
import {
|
||||
CloudIcon,
|
||||
LaptopIcon,
|
||||
LoaderIcon,
|
||||
MonitorIcon,
|
||||
PlusIcon,
|
||||
RefreshCwIcon,
|
||||
TerminalIcon,
|
||||
} from "lucide-react";
|
||||
import { useCallback, useMemo, useRef, useState } from "react";
|
||||
|
||||
import { isDesktopLocalConnectionTarget } from "../../connection/desktopLocal";
|
||||
import { isElectron } from "../../env";
|
||||
import { usePrimarySessionState } from "../../environments/primary";
|
||||
import { useEnvironmentSettings, useUpdateEnvironmentSettings } from "../../hooks/useSettings";
|
||||
import { cn } from "../../lib/utils";
|
||||
import { resolveAppModelSelectionState } from "../../modelSelection";
|
||||
import {
|
||||
useEnvironments,
|
||||
usePrimaryEnvironmentId,
|
||||
type EnvironmentPresentation,
|
||||
} from "../../state/environments";
|
||||
import { EMPTY_SERVER_PROVIDERS, serverEnvironment } from "../../state/server";
|
||||
import { useEnvironmentSessionState } from "../../state/session";
|
||||
import { useAtomCommand } from "../../state/use-atom-command";
|
||||
import { getRelativeTimeState } from "../../timestampFormat";
|
||||
import {
|
||||
ConnectionStatusDot,
|
||||
connectionPhaseDotClassName,
|
||||
connectionPhasePingClassName,
|
||||
} from "../ConnectionStatusDot";
|
||||
import {
|
||||
canOneClickUpdateProviderCandidate,
|
||||
collectProviderUpdateCandidates,
|
||||
hasOneClickUpdateProviderCandidate,
|
||||
isProviderUpdateActive,
|
||||
type ProviderUpdateCandidate,
|
||||
} from "../ProviderUpdateLaunchNotification.logic";
|
||||
import { Button } from "../ui/button";
|
||||
import {
|
||||
NumberField,
|
||||
NumberFieldDecrement,
|
||||
NumberFieldGroup,
|
||||
NumberFieldIncrement,
|
||||
NumberFieldInput,
|
||||
} from "../ui/number-field";
|
||||
import { stackedThreadToast, toastManager } from "../ui/toast";
|
||||
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
|
||||
import { AddProviderInstanceDialog } from "./AddProviderInstanceDialog";
|
||||
import { ProviderInstanceCard } from "./ProviderInstanceCard";
|
||||
import { DRIVER_OPTIONS, getDriverOption } from "./providerDriverMeta";
|
||||
import { searchableSetting } from "./settingsSearch";
|
||||
import {
|
||||
backgroundActivityOverrideSettings,
|
||||
buildProviderInstanceUpdatePatch,
|
||||
durationToSeconds,
|
||||
normalizeIntervalSeconds,
|
||||
PROVIDER_HEALTH_INTERVAL_STEP_SECONDS,
|
||||
} from "./SettingsPanels.logic";
|
||||
import {
|
||||
PolicyTooltip,
|
||||
SettingResetButton,
|
||||
SettingsPageContainer,
|
||||
SettingsRow,
|
||||
SettingsSection,
|
||||
useRelativeTimeTick,
|
||||
} from "./settingsLayout";
|
||||
import {
|
||||
buildProviderEnvironmentOptions,
|
||||
classifyProviderEnvironmentAccess,
|
||||
type ProviderEnvironmentAccess,
|
||||
type ProviderOperateAccess,
|
||||
resolvePrimaryOperateAccess,
|
||||
resolveRemoteOperateAccess,
|
||||
resolveSelectedProviderEnvironmentId,
|
||||
} from "./ProviderSettingsPanel.logic";
|
||||
|
||||
function withoutProviderInstanceKey<V>(
|
||||
record: Readonly<Record<ProviderInstanceId, V>> | undefined,
|
||||
key: ProviderInstanceId,
|
||||
): Record<ProviderInstanceId, V> {
|
||||
const next = { ...record } as Record<ProviderInstanceId, V>;
|
||||
delete next[key];
|
||||
return next;
|
||||
}
|
||||
|
||||
function withoutProviderInstanceFavorites(
|
||||
favorites: ReadonlyArray<{ readonly provider: ProviderInstanceId; readonly model: string }>,
|
||||
instanceId: ProviderInstanceId,
|
||||
) {
|
||||
return favorites.filter((favorite) => favorite.provider !== instanceId);
|
||||
}
|
||||
|
||||
const PROVIDER_SETTINGS = DRIVER_OPTIONS.map((definition) => ({
|
||||
provider: definition.value,
|
||||
}));
|
||||
|
||||
function ProviderLastChecked({ lastCheckedAt }: { lastCheckedAt: string | null }) {
|
||||
useRelativeTimeTick();
|
||||
const lastCheckedRelative = getRelativeTimeState(lastCheckedAt);
|
||||
|
||||
if (lastCheckedRelative.status === "missing") {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (lastCheckedRelative.status === "invalid") {
|
||||
return <span className="text-[11px] text-muted-foreground/50">Checked unavailable</span>;
|
||||
}
|
||||
|
||||
return (
|
||||
<span className="text-[11px] text-muted-foreground/60">
|
||||
{lastCheckedRelative.suffix ? (
|
||||
<>
|
||||
Checked <span className="font-mono tabular-nums">{lastCheckedRelative.value}</span>{" "}
|
||||
{lastCheckedRelative.suffix}
|
||||
</>
|
||||
) : (
|
||||
<>Checked {lastCheckedRelative.value}</>
|
||||
)}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
function providerEnvironmentIcon(environment: EnvironmentPresentation) {
|
||||
if (environment.entry.target._tag === "PrimaryConnectionTarget") return MonitorIcon;
|
||||
if (environment.entry.target._tag === "RelayConnectionTarget") return CloudIcon;
|
||||
if (environment.entry.target._tag === "SshConnectionTarget") return TerminalIcon;
|
||||
if (isDesktopLocalConnectionTarget(environment.entry.target)) return LaptopIcon;
|
||||
return CloudIcon;
|
||||
}
|
||||
|
||||
function providerEnvironmentDetail(environment: EnvironmentPresentation): string {
|
||||
if (environment.entry.target._tag === "PrimaryConnectionTarget") return "Primary device";
|
||||
if (environment.relayManaged) return "T3 Connect";
|
||||
if (environment.entry.target._tag === "SshConnectionTarget") return "SSH";
|
||||
if (isDesktopLocalConnectionTarget(environment.entry.target)) return "Local device";
|
||||
return environment.displayUrl ?? "Remote device";
|
||||
}
|
||||
|
||||
function EnvironmentUnavailableRow({
|
||||
environment,
|
||||
access,
|
||||
}: {
|
||||
readonly environment: EnvironmentPresentation;
|
||||
readonly access: Exclude<ProviderEnvironmentAccess, { kind: "editable" | "read-only" }>;
|
||||
}) {
|
||||
const isLoading = access.kind === "loading";
|
||||
const title = isLoading
|
||||
? "Loading provider settings"
|
||||
: access.kind === "error"
|
||||
? "Could not connect to this device"
|
||||
: "Provider settings are unavailable";
|
||||
const description = isLoading
|
||||
? access.reason === "permissions"
|
||||
? "Checking what this session is allowed to change."
|
||||
: `Waiting for ${environment.label}'s configuration.`
|
||||
: connectionStatusText(environment.connection);
|
||||
// No spinner: this state can persist indefinitely for a wedged device, and a
|
||||
// continuously repainting animation would run the whole time.
|
||||
return (
|
||||
<SettingsSection title="Providers">
|
||||
<SettingsRow title={title} description={description} />
|
||||
</SettingsSection>
|
||||
);
|
||||
}
|
||||
|
||||
export function ProviderSettingsPanel() {
|
||||
const { environments, isReady } = useEnvironments();
|
||||
const primaryEnvironmentId = usePrimaryEnvironmentId();
|
||||
const options = useMemo(
|
||||
() => buildProviderEnvironmentOptions(environments, primaryEnvironmentId),
|
||||
[environments, primaryEnvironmentId],
|
||||
);
|
||||
// Raw user intent; the effective selection is re-derived every render so a
|
||||
// device that drops out of the catalog falls back without erasing the pick —
|
||||
// if it reappears (e.g. after a reconnect) the selection is restored.
|
||||
const [selectedEnvironmentId, setSelectedEnvironmentId] = useState<EnvironmentId | null>(
|
||||
primaryEnvironmentId,
|
||||
);
|
||||
const effectiveEnvironmentId = resolveSelectedProviderEnvironmentId(
|
||||
options,
|
||||
selectedEnvironmentId,
|
||||
primaryEnvironmentId,
|
||||
);
|
||||
const selectedEnvironment =
|
||||
options.find((environment) => environment.environmentId === effectiveEnvironmentId) ?? null;
|
||||
const onlyPrimaryDevice =
|
||||
options.length === 1 && options[0]?.entry.target._tag === "PrimaryConnectionTarget";
|
||||
|
||||
return (
|
||||
<SettingsPageContainer>
|
||||
{!onlyPrimaryDevice ? (
|
||||
<SettingsSection title="Devices">
|
||||
{options.length === 0 ? (
|
||||
// The catalog hydrates asynchronously, so an empty list before it is
|
||||
// ready means "not loaded yet", not "nothing is connected".
|
||||
<SettingsRow
|
||||
title={isReady ? "No connected devices" : "Loading devices"}
|
||||
description={
|
||||
isReady
|
||||
? "Connect an execution environment before configuring providers."
|
||||
: "Reading connected execution environments."
|
||||
}
|
||||
/>
|
||||
) : (
|
||||
<div className="grid gap-1 sm:grid-cols-2">
|
||||
{options.map((environment) => {
|
||||
const Icon = providerEnvironmentIcon(environment);
|
||||
const selected = environment.environmentId === effectiveEnvironmentId;
|
||||
const statusText = connectionStatusText(environment.connection);
|
||||
return (
|
||||
<button
|
||||
key={environment.environmentId}
|
||||
type="button"
|
||||
aria-pressed={selected}
|
||||
className={cn(
|
||||
"flex min-w-0 items-center gap-3 rounded-xl px-3 py-2.5 text-left transition-colors sm:px-4",
|
||||
selected
|
||||
? "bg-primary/8 ring-1 ring-primary/25 dark:bg-primary/12"
|
||||
: "hover:bg-muted/40",
|
||||
)}
|
||||
onClick={() => setSelectedEnvironmentId(environment.environmentId)}
|
||||
>
|
||||
<span className="flex size-8 shrink-0 items-center justify-center rounded-lg border border-border/60 bg-background text-muted-foreground">
|
||||
<Icon className="size-4" aria-hidden />
|
||||
</span>
|
||||
<span className="min-w-0 flex-1">
|
||||
<span className="flex items-center gap-1.5">
|
||||
<ConnectionStatusDot
|
||||
tooltipText={statusText}
|
||||
dotClassName={connectionPhaseDotClassName(environment.connection.phase)}
|
||||
pingClassName={connectionPhasePingClassName(environment.connection.phase)}
|
||||
/>
|
||||
<span className="truncate text-sm font-medium text-foreground">
|
||||
{environment.label}
|
||||
</span>
|
||||
</span>
|
||||
<span className="block truncate pl-[18px] text-xs text-muted-foreground">
|
||||
{providerEnvironmentDetail(environment)} · {statusText}
|
||||
</span>
|
||||
</span>
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</SettingsSection>
|
||||
) : null}
|
||||
|
||||
{selectedEnvironment ? (
|
||||
<SelectedEnvironmentProviderSettings
|
||||
key={selectedEnvironment.environmentId}
|
||||
environment={selectedEnvironment}
|
||||
/>
|
||||
) : null}
|
||||
</SettingsPageContainer>
|
||||
);
|
||||
}
|
||||
|
||||
function SelectedEnvironmentProviderSettings({
|
||||
environment,
|
||||
}: {
|
||||
readonly environment: EnvironmentPresentation;
|
||||
}) {
|
||||
const isPrimary = environment.entry.target._tag === "PrimaryConnectionTarget";
|
||||
if (isPrimary) {
|
||||
// The desktop app owns its primary server outright; a browser session
|
||||
// checks the scopes its cookie session was granted.
|
||||
if (isElectron) {
|
||||
return <AccessGatedProviderSettings environment={environment} operateAccess="granted" />;
|
||||
}
|
||||
return <PrimarySessionGatedProviderSettings environment={environment} />;
|
||||
}
|
||||
return <RemoteSessionGatedProviderSettings environment={environment} />;
|
||||
}
|
||||
|
||||
function PrimarySessionGatedProviderSettings({
|
||||
environment,
|
||||
}: {
|
||||
readonly environment: EnvironmentPresentation;
|
||||
}) {
|
||||
const primarySessionState = usePrimarySessionState();
|
||||
const operateAccess = resolvePrimaryOperateAccess({
|
||||
isPrimary: true,
|
||||
hasDesktopBridge: false,
|
||||
session: primarySessionState.data,
|
||||
isPending: primarySessionState.isPending,
|
||||
hasError: primarySessionState.error !== null,
|
||||
});
|
||||
return <AccessGatedProviderSettings environment={environment} operateAccess={operateAccess} />;
|
||||
}
|
||||
|
||||
function RemoteSessionGatedProviderSettings({
|
||||
environment,
|
||||
}: {
|
||||
readonly environment: EnvironmentPresentation;
|
||||
}) {
|
||||
const sessionState = useEnvironmentSessionState(environment.environmentId);
|
||||
const operateAccess = resolveRemoteOperateAccess({
|
||||
session: sessionState.data,
|
||||
isPending: sessionState.isPending,
|
||||
hasError: sessionState.hasError,
|
||||
});
|
||||
return <AccessGatedProviderSettings environment={environment} operateAccess={operateAccess} />;
|
||||
}
|
||||
|
||||
function AccessGatedProviderSettings({
|
||||
environment,
|
||||
operateAccess,
|
||||
}: {
|
||||
readonly environment: EnvironmentPresentation;
|
||||
readonly operateAccess: ProviderOperateAccess;
|
||||
}) {
|
||||
const access = classifyProviderEnvironmentAccess({
|
||||
connectionPhase: environment.connection.phase,
|
||||
hasServerConfig: environment.serverConfig !== null,
|
||||
operateAccess,
|
||||
});
|
||||
if (access.kind !== "editable" && access.kind !== "read-only") {
|
||||
return <EnvironmentUnavailableRow environment={environment} access={access} />;
|
||||
}
|
||||
return (
|
||||
<EnvironmentProviderSettings
|
||||
environmentId={environment.environmentId}
|
||||
environmentLabel={environment.label}
|
||||
readOnly={access.kind === "read-only"}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export function EnvironmentProviderSettings({
|
||||
environmentId,
|
||||
environmentLabel,
|
||||
readOnly = false,
|
||||
}: {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly environmentLabel: string;
|
||||
/**
|
||||
* Render the full provider layout, greyed out and inert, when this session's
|
||||
* credential lacks `orchestration:operate` on the environment. Showing the
|
||||
* real configuration keeps the view honest; disabling interaction keeps
|
||||
* every one of its writes from being offered and then rejected.
|
||||
*/
|
||||
readonly readOnly?: boolean;
|
||||
}) {
|
||||
const settings = useEnvironmentSettings(environmentId);
|
||||
const updateSettings = useUpdateEnvironmentSettings(environmentId);
|
||||
const serverProviders =
|
||||
useAtomValue(serverEnvironment.providersValueAtom(environmentId)) ?? EMPTY_SERVER_PROVIDERS;
|
||||
const refreshServerProviders = useAtomCommand(serverEnvironment.refreshProviders, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const updateProvider = useAtomCommand(serverEnvironment.updateProvider, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const [isRefreshingProviders, setIsRefreshingProviders] = useState(false);
|
||||
const [isAddInstanceDialogOpen, setIsAddInstanceDialogOpen] = useState(false);
|
||||
const [updatingProviderDrivers, setUpdatingProviderDrivers] = useState<
|
||||
ReadonlySet<ProviderDriverKind>
|
||||
>(() => new Set());
|
||||
const [openInstanceDetails, setOpenInstanceDetails] = useState<Record<string, boolean>>({});
|
||||
const refreshingRef = useRef(false);
|
||||
const updatingDriversRef = useRef<Set<ProviderDriverKind>>(new Set());
|
||||
|
||||
const providerUpdateCandidates = useMemo(
|
||||
() => collectProviderUpdateCandidates(serverProviders),
|
||||
[serverProviders],
|
||||
);
|
||||
const providerUpdateCandidateByInstanceId = useMemo(
|
||||
() => new Map(providerUpdateCandidates.map((candidate) => [candidate.instanceId, candidate])),
|
||||
[providerUpdateCandidates],
|
||||
);
|
||||
const visibleProviderSettings = PROVIDER_SETTINGS.filter(
|
||||
(providerSettings) =>
|
||||
providerSettings.provider !== "cursor" ||
|
||||
serverProviders.some(
|
||||
(provider) =>
|
||||
provider.instanceId === defaultInstanceIdForDriver(ProviderDriverKind.make("cursor")),
|
||||
),
|
||||
);
|
||||
const textGenerationModelSelection = resolveAppModelSelectionState(settings, serverProviders);
|
||||
const textGenInstanceId = textGenerationModelSelection.instanceId;
|
||||
const resolvedBackgroundActivity = resolveServerBackgroundActivitySettings(settings);
|
||||
const providerHealthPreset = getBackgroundActivityPresetSettings(
|
||||
resolvedBackgroundActivity.profile,
|
||||
).providerHealthRefreshInterval;
|
||||
const providerHealthRefreshIntervalSeconds = durationToSeconds(
|
||||
resolvedBackgroundActivity.providerHealthRefreshInterval,
|
||||
);
|
||||
const defaultProviderHealthRefreshIntervalSeconds = durationToSeconds(providerHealthPreset);
|
||||
const lastCheckedAt =
|
||||
serverProviders.length > 0
|
||||
? serverProviders.reduce(
|
||||
(latest, provider) => (provider.checkedAt > latest ? provider.checkedAt : latest),
|
||||
serverProviders[0]!.checkedAt,
|
||||
)
|
||||
: null;
|
||||
|
||||
const refreshProviders = useCallback(() => {
|
||||
if (refreshingRef.current) return;
|
||||
refreshingRef.current = true;
|
||||
setIsRefreshingProviders(true);
|
||||
void (async () => {
|
||||
const result = await refreshServerProviders({
|
||||
environmentId,
|
||||
input: {},
|
||||
});
|
||||
refreshingRef.current = false;
|
||||
setIsRefreshingProviders(false);
|
||||
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
|
||||
console.warn("Failed to refresh providers", {
|
||||
operation: "refresh-providers",
|
||||
environmentId,
|
||||
...safeErrorLogAttributes(squashAtomCommandFailure(result)),
|
||||
});
|
||||
}
|
||||
})();
|
||||
}, [environmentId, refreshServerProviders]);
|
||||
|
||||
const runProviderUpdate = useCallback(
|
||||
async (candidate: ProviderUpdateCandidate) => {
|
||||
// Ref-based re-entry guard, mirroring refreshProviders: a state updater
|
||||
// may run after this function returns, so it cannot gate the dispatch.
|
||||
if (updatingDriversRef.current.has(candidate.driver)) {
|
||||
return;
|
||||
}
|
||||
updatingDriversRef.current.add(candidate.driver);
|
||||
setUpdatingProviderDrivers((previous) => new Set(previous).add(candidate.driver));
|
||||
|
||||
const result = await updateProvider({
|
||||
environmentId,
|
||||
input: {
|
||||
provider: candidate.driver,
|
||||
instanceId: candidate.instanceId,
|
||||
},
|
||||
});
|
||||
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
|
||||
const error = squashAtomCommandFailure(result);
|
||||
toastManager.add(
|
||||
stackedThreadToast({
|
||||
type: "error",
|
||||
title: `Could not update ${PROVIDER_DISPLAY_NAMES[candidate.driver] ?? candidate.driver}`,
|
||||
description:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: "The provider update command could not be started.",
|
||||
}),
|
||||
);
|
||||
}
|
||||
updatingDriversRef.current.delete(candidate.driver);
|
||||
setUpdatingProviderDrivers((previous) => {
|
||||
if (!previous.has(candidate.driver)) {
|
||||
return previous;
|
||||
}
|
||||
const next = new Set(previous);
|
||||
next.delete(candidate.driver);
|
||||
return next;
|
||||
});
|
||||
},
|
||||
[environmentId, updateProvider],
|
||||
);
|
||||
|
||||
interface InstanceRow {
|
||||
readonly instanceId: ProviderInstanceId;
|
||||
readonly instance: ProviderInstanceConfig;
|
||||
readonly driver: ProviderDriverKind;
|
||||
readonly isDefault: boolean;
|
||||
readonly isDirty?: boolean;
|
||||
}
|
||||
|
||||
const instancesByDriver = new Map<
|
||||
ProviderDriverKind,
|
||||
Array<[ProviderInstanceId, ProviderInstanceConfig]>
|
||||
>();
|
||||
for (const [rawId, instance] of Object.entries(settings.providerInstances ?? {})) {
|
||||
const driver = instance.driver;
|
||||
const list = instancesByDriver.get(driver) ?? [];
|
||||
list.push([rawId as ProviderInstanceId, instance]);
|
||||
instancesByDriver.set(driver, list);
|
||||
}
|
||||
|
||||
const defaultSlotIdsBySource = new Set<string>(
|
||||
visibleProviderSettings.map((providerSettings) =>
|
||||
String(defaultInstanceIdForDriver(providerSettings.provider)),
|
||||
),
|
||||
);
|
||||
|
||||
const rows: InstanceRow[] = [];
|
||||
const visibleDriverKinds = new Set<ProviderDriverKind>(
|
||||
visibleProviderSettings.map((providerSettings) => providerSettings.provider),
|
||||
);
|
||||
|
||||
for (const providerSettings of visibleProviderSettings) {
|
||||
type LegacyProviderSettings = (typeof settings.providers)[keyof typeof settings.providers];
|
||||
const legacyProviders = settings.providers as Record<string, LegacyProviderSettings>;
|
||||
const defaultLegacyProviders = DEFAULT_UNIFIED_SETTINGS.providers as Record<
|
||||
string,
|
||||
LegacyProviderSettings
|
||||
>;
|
||||
const driver = providerSettings.provider;
|
||||
const defaultInstanceId = defaultInstanceIdForDriver(driver);
|
||||
const explicitInstance = settings.providerInstances?.[defaultInstanceId];
|
||||
// A remote device may run a server version whose settings predate this
|
||||
// driver, so the legacy mirror can be absent. Without either an explicit
|
||||
// instance or a legacy blob there is nothing to render for the slot.
|
||||
const legacyConfig = legacyProviders[providerSettings.provider];
|
||||
const defaultLegacyConfig = defaultLegacyProviders[providerSettings.provider];
|
||||
const effectiveInstance: ProviderInstanceConfig | undefined =
|
||||
explicitInstance ??
|
||||
(legacyConfig !== undefined
|
||||
? ({
|
||||
driver,
|
||||
enabled: legacyConfig.enabled,
|
||||
config: legacyConfig,
|
||||
} satisfies ProviderInstanceConfig)
|
||||
: undefined);
|
||||
// Only the default slot depends on the legacy blob; custom instances for
|
||||
// the driver must still render even when the slot has nothing to show.
|
||||
if (effectiveInstance !== undefined) {
|
||||
const isDirty =
|
||||
explicitInstance !== undefined || !Equal.equals(legacyConfig, defaultLegacyConfig);
|
||||
rows.push({
|
||||
instanceId: defaultInstanceId,
|
||||
instance: effectiveInstance,
|
||||
driver,
|
||||
isDefault: true,
|
||||
isDirty,
|
||||
});
|
||||
}
|
||||
for (const [id, instance] of instancesByDriver.get(providerSettings.provider) ?? []) {
|
||||
if (id === defaultInstanceId) continue;
|
||||
rows.push({ instanceId: id, instance, driver: instance.driver, isDefault: false });
|
||||
}
|
||||
}
|
||||
for (const [driver, list] of instancesByDriver) {
|
||||
if (visibleDriverKinds.has(driver)) continue;
|
||||
for (const [id, instance] of list) {
|
||||
rows.push({
|
||||
instanceId: id,
|
||||
instance,
|
||||
driver: instance.driver,
|
||||
isDefault: defaultSlotIdsBySource.has(String(id)),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const updateProviderInstance = (
|
||||
row: InstanceRow,
|
||||
next: ProviderInstanceConfig,
|
||||
options?: {
|
||||
readonly textGenerationModelSelection?: Parameters<
|
||||
typeof buildProviderInstanceUpdatePatch
|
||||
>[0]["textGenerationModelSelection"];
|
||||
},
|
||||
) => {
|
||||
updateSettings(
|
||||
buildProviderInstanceUpdatePatch({
|
||||
settings,
|
||||
instanceId: row.instanceId,
|
||||
instance: next,
|
||||
driver: row.driver,
|
||||
isDefault: row.isDefault,
|
||||
textGenerationModelSelection: options?.textGenerationModelSelection,
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
const deleteProviderInstance = (id: ProviderInstanceId) => {
|
||||
updateSettings({
|
||||
providerInstances: withoutProviderInstanceKey(settings.providerInstances, id),
|
||||
});
|
||||
};
|
||||
|
||||
const updateProviderModelPreferences = (
|
||||
instanceId: ProviderInstanceId,
|
||||
next: {
|
||||
readonly hiddenModels: ReadonlyArray<string>;
|
||||
readonly modelOrder: ReadonlyArray<string>;
|
||||
},
|
||||
) => {
|
||||
const hiddenModels = [...new Set(next.hiddenModels.filter((slug) => slug.trim().length > 0))];
|
||||
const modelOrder = [...new Set(next.modelOrder.filter((slug) => slug.trim().length > 0))];
|
||||
const rest = withoutProviderInstanceKey(settings.providerModelPreferences, instanceId);
|
||||
updateSettings({
|
||||
providerModelPreferences:
|
||||
hiddenModels.length === 0 && modelOrder.length === 0
|
||||
? rest
|
||||
: {
|
||||
...rest,
|
||||
[instanceId]: {
|
||||
hiddenModels,
|
||||
modelOrder,
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const updateProviderFavoriteModels = (
|
||||
instanceId: ProviderInstanceId,
|
||||
nextFavoriteModels: ReadonlyArray<string>,
|
||||
) => {
|
||||
const favoriteModels = [
|
||||
...new Set(
|
||||
Arr.filterMap(nextFavoriteModels, (slug) => {
|
||||
const trimmedSlug = slug.trim();
|
||||
return trimmedSlug.length > 0 ? Result.succeed(trimmedSlug) : Result.failVoid;
|
||||
}),
|
||||
),
|
||||
];
|
||||
updateSettings({
|
||||
favorites: [
|
||||
...withoutProviderInstanceFavorites(settings.favorites ?? [], instanceId),
|
||||
...favoriteModels.map((model) => ({ provider: instanceId, model })),
|
||||
],
|
||||
});
|
||||
};
|
||||
|
||||
const resetDefaultInstance = (driverKind: ProviderDriverKind) => {
|
||||
type LegacyProviderSettings = (typeof settings.providers)[keyof typeof settings.providers];
|
||||
const defaultLegacyProviders = DEFAULT_UNIFIED_SETTINGS.providers as Record<
|
||||
string,
|
||||
LegacyProviderSettings | undefined
|
||||
>;
|
||||
const defaultInstanceId = defaultInstanceIdForDriver(driverKind);
|
||||
const defaultLegacyProvider = defaultLegacyProviders[driverKind];
|
||||
if (defaultLegacyProvider === undefined) return;
|
||||
updateSettings({
|
||||
providers: {
|
||||
...settings.providers,
|
||||
[driverKind]: defaultLegacyProvider,
|
||||
} as typeof settings.providers,
|
||||
providerInstances: withoutProviderInstanceKey(settings.providerInstances, defaultInstanceId),
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<SettingsSection
|
||||
{...searchableSetting("providers")}
|
||||
headerAction={
|
||||
<div className="flex items-center gap-1.5">
|
||||
<ProviderLastChecked lastCheckedAt={lastCheckedAt} />
|
||||
{!readOnly ? (
|
||||
<>
|
||||
<Tooltip>
|
||||
<TooltipTrigger
|
||||
render={
|
||||
<Button
|
||||
size="icon-xs"
|
||||
variant="ghost"
|
||||
className="size-5 rounded-sm p-0 text-muted-foreground hover:text-foreground"
|
||||
onClick={() => setIsAddInstanceDialogOpen(true)}
|
||||
aria-label="Add provider instance"
|
||||
>
|
||||
<PlusIcon className="size-3" />
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
<TooltipPopup side="top">Add provider instance</TooltipPopup>
|
||||
</Tooltip>
|
||||
<Tooltip>
|
||||
<TooltipTrigger
|
||||
render={
|
||||
<Button
|
||||
size="icon-xs"
|
||||
variant="ghost"
|
||||
className="size-5 rounded-sm p-0 text-muted-foreground hover:text-foreground"
|
||||
disabled={isRefreshingProviders}
|
||||
onClick={() => void refreshProviders()}
|
||||
aria-label="Refresh provider status"
|
||||
>
|
||||
{isRefreshingProviders ? (
|
||||
<LoaderIcon className="size-3 animate-spin" />
|
||||
) : (
|
||||
<RefreshCwIcon className="size-3" />
|
||||
)}
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
<TooltipPopup side="top">Refresh provider status</TooltipPopup>
|
||||
</Tooltip>
|
||||
</>
|
||||
) : null}
|
||||
</div>
|
||||
}
|
||||
>
|
||||
{readOnly ? (
|
||||
<SettingsRow
|
||||
title="Limited permissions"
|
||||
description={`This session can view ${environmentLabel}'s providers, but its credential does not allow changing their configuration.`}
|
||||
/>
|
||||
) : null}
|
||||
<div
|
||||
// `inert` blocks focus and interaction in one attribute, so the
|
||||
// read-only view stays byte-for-byte the editable layout without
|
||||
// threading a disabled flag through every control.
|
||||
inert={readOnly}
|
||||
aria-disabled={readOnly || undefined}
|
||||
className={readOnly ? "space-y-1 opacity-50 select-none" : "space-y-1"}
|
||||
>
|
||||
<SettingsRow
|
||||
title={
|
||||
<span className="inline-flex items-center gap-1.5">
|
||||
Health check interval
|
||||
<PolicyTooltip>
|
||||
This interval is configured here, then the shared Background activity policy
|
||||
decides whether provider probes may run when the timer fires. Custom intervals
|
||||
appear as Advanced in General settings.
|
||||
</PolicyTooltip>
|
||||
</span>
|
||||
}
|
||||
description="Refresh provider availability, versions, auth state, and model metadata in the background. Set this to 0 seconds to rely on manual refreshes."
|
||||
resetAction={
|
||||
providerHealthRefreshIntervalSeconds !==
|
||||
defaultProviderHealthRefreshIntervalSeconds ? (
|
||||
<SettingResetButton
|
||||
label="provider health check interval"
|
||||
onClick={() =>
|
||||
updateSettings(
|
||||
backgroundActivityOverrideSettings(
|
||||
settings.backgroundActivity,
|
||||
resolvedBackgroundActivity,
|
||||
{
|
||||
providerHealthRefreshInterval: undefined,
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
||||
/>
|
||||
) : null
|
||||
}
|
||||
control={
|
||||
<div className="flex shrink-0 items-center gap-2">
|
||||
<NumberField
|
||||
value={providerHealthRefreshIntervalSeconds}
|
||||
min={0}
|
||||
step={PROVIDER_HEALTH_INTERVAL_STEP_SECONDS}
|
||||
size="sm"
|
||||
className="w-32"
|
||||
onValueChange={(value) =>
|
||||
updateSettings(
|
||||
backgroundActivityOverrideSettings(
|
||||
settings.backgroundActivity,
|
||||
resolvedBackgroundActivity,
|
||||
{
|
||||
providerHealthRefreshInterval: Duration.seconds(
|
||||
normalizeIntervalSeconds(value),
|
||||
),
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
||||
>
|
||||
<NumberFieldGroup>
|
||||
<NumberFieldDecrement aria-label="Decrease provider health check interval" />
|
||||
<NumberFieldInput aria-label="Provider health check interval in seconds" />
|
||||
<NumberFieldIncrement aria-label="Increase provider health check interval" />
|
||||
</NumberFieldGroup>
|
||||
</NumberField>
|
||||
<span className="text-xs text-muted-foreground">seconds</span>
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
|
||||
{rows.map((row) => {
|
||||
const driverOption = getDriverOption(row.driver);
|
||||
const liveProvider = serverProviders.find(
|
||||
(candidate) => candidate.instanceId === row.instanceId,
|
||||
);
|
||||
const updateCandidate = liveProvider
|
||||
? providerUpdateCandidateByInstanceId.get(liveProvider.instanceId)
|
||||
: undefined;
|
||||
const isDriverUpdateRunning =
|
||||
updateCandidate !== undefined &&
|
||||
(updatingProviderDrivers.has(updateCandidate.driver) ||
|
||||
serverProviders.some(
|
||||
(provider) =>
|
||||
provider.driver === updateCandidate.driver && isProviderUpdateActive(provider),
|
||||
));
|
||||
const showInlineUpdateButton =
|
||||
updateCandidate !== undefined &&
|
||||
hasOneClickUpdateProviderCandidate(updateCandidate, serverProviders);
|
||||
const canRunInlineUpdate =
|
||||
updateCandidate !== undefined &&
|
||||
canOneClickUpdateProviderCandidate(updateCandidate, serverProviders) &&
|
||||
!updatingProviderDrivers.has(updateCandidate.driver);
|
||||
const modelPreferences = settings.providerModelPreferences?.[row.instanceId] ?? {
|
||||
hiddenModels: [],
|
||||
modelOrder: [],
|
||||
};
|
||||
const favoriteModels = Arr.filterMap(settings.favorites ?? [], (favorite) =>
|
||||
favorite.provider === row.instanceId
|
||||
? Result.succeed(favorite.model)
|
||||
: Result.failVoid,
|
||||
);
|
||||
const resetLabel = driverOption?.label ?? String(row.driver);
|
||||
const headerAction =
|
||||
row.isDefault && row.isDirty ? (
|
||||
<SettingResetButton
|
||||
label={`${resetLabel} provider settings`}
|
||||
onClick={() => resetDefaultInstance(row.driver)}
|
||||
/>
|
||||
) : null;
|
||||
return (
|
||||
<ProviderInstanceCard
|
||||
key={row.instanceId}
|
||||
instanceId={row.instanceId}
|
||||
instance={row.instance}
|
||||
driverOption={driverOption}
|
||||
liveProvider={liveProvider}
|
||||
isExpanded={openInstanceDetails[row.instanceId] ?? false}
|
||||
onExpandedChange={(open) =>
|
||||
setOpenInstanceDetails((existing) => ({
|
||||
...existing,
|
||||
[row.instanceId]: open,
|
||||
}))
|
||||
}
|
||||
onUpdate={(next) => {
|
||||
const wasEnabled = row.instance.enabled ?? true;
|
||||
const isDisabling = next.enabled === false && wasEnabled;
|
||||
const shouldClearTextGen = isDisabling && textGenInstanceId === row.instanceId;
|
||||
if (shouldClearTextGen) {
|
||||
updateProviderInstance(row, next, {
|
||||
textGenerationModelSelection:
|
||||
DEFAULT_UNIFIED_SETTINGS.textGenerationModelSelection,
|
||||
});
|
||||
} else {
|
||||
updateProviderInstance(row, next);
|
||||
}
|
||||
}}
|
||||
onDelete={row.isDefault ? undefined : () => deleteProviderInstance(row.instanceId)}
|
||||
headerAction={headerAction}
|
||||
hiddenModels={modelPreferences.hiddenModels}
|
||||
favoriteModels={favoriteModels}
|
||||
modelOrder={modelPreferences.modelOrder}
|
||||
onHiddenModelsChange={(hiddenModels) =>
|
||||
updateProviderModelPreferences(row.instanceId, {
|
||||
...modelPreferences,
|
||||
hiddenModels,
|
||||
})
|
||||
}
|
||||
onFavoriteModelsChange={(favoriteModels) =>
|
||||
updateProviderFavoriteModels(row.instanceId, favoriteModels)
|
||||
}
|
||||
onModelOrderChange={(modelOrder) =>
|
||||
updateProviderModelPreferences(row.instanceId, {
|
||||
...modelPreferences,
|
||||
modelOrder,
|
||||
})
|
||||
}
|
||||
onRunUpdate={
|
||||
showInlineUpdateButton && updateCandidate
|
||||
? () => {
|
||||
if (!canRunInlineUpdate) {
|
||||
return;
|
||||
}
|
||||
void runProviderUpdate(updateCandidate);
|
||||
}
|
||||
: undefined
|
||||
}
|
||||
isUpdating={showInlineUpdateButton ? isDriverUpdateRunning : undefined}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</SettingsSection>
|
||||
|
||||
{isAddInstanceDialogOpen ? (
|
||||
<AddProviderInstanceDialog
|
||||
open
|
||||
environmentId={environmentId}
|
||||
environmentLabel={environmentLabel}
|
||||
onOpenChange={setIsAddInstanceDialogOpen}
|
||||
/>
|
||||
) : null}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -10,10 +10,12 @@ import type {
|
||||
} from "@t3tools/contracts";
|
||||
import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings";
|
||||
import {
|
||||
getBackgroundActivityBaseProfile,
|
||||
normalizeBackgroundActivitySettings,
|
||||
normalizeServerBackgroundActivitySettings,
|
||||
resolveServerBackgroundActivitySettings,
|
||||
} from "@t3tools/shared/backgroundActivitySettings";
|
||||
import * as Duration from "effect/Duration";
|
||||
import * as Equal from "effect/Equal";
|
||||
|
||||
export function isProjectGroupingEnabled(mode: SidebarProjectGroupingMode): boolean {
|
||||
@@ -190,3 +192,58 @@ export function buildProviderInstanceUpdatePatch(input: {
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
|
||||
// ── Background-activity interval helpers ─────────────────────────────
|
||||
// Shared by the General panel's interval rows and the Providers panel's
|
||||
// health-check row.
|
||||
|
||||
export const PROVIDER_HEALTH_INTERVAL_STEP_SECONDS = 30;
|
||||
|
||||
type BackgroundActivityOverridePatch = Partial<{
|
||||
[K in keyof BackgroundActivitySettings["overrides"]]:
|
||||
| BackgroundActivitySettings["overrides"][K]
|
||||
| undefined;
|
||||
}>;
|
||||
|
||||
export function durationToSeconds(duration: Duration.Duration): number {
|
||||
return Math.round(Duration.toMillis(duration) / 1_000);
|
||||
}
|
||||
|
||||
export function normalizeIntervalSeconds(value: number | null, minimum = 0): number {
|
||||
if (value === null || !Number.isFinite(value)) {
|
||||
return minimum;
|
||||
}
|
||||
return Math.max(minimum, Math.round(value));
|
||||
}
|
||||
|
||||
export function backgroundActivityOverrideSettings(
|
||||
current: BackgroundActivitySettings,
|
||||
resolved: ReturnType<typeof resolveServerBackgroundActivitySettings>,
|
||||
overrides: BackgroundActivityOverridePatch,
|
||||
) {
|
||||
const nextOverrides: BackgroundActivityOverridePatch = {
|
||||
automaticGitFetchInterval: resolved.automaticGitFetchInterval,
|
||||
providerHealthRefreshInterval: resolved.providerHealthRefreshInterval,
|
||||
hostPowerMonitorActiveInterval: resolved.hostPowerMonitorActiveInterval,
|
||||
hostPowerMonitorIdleInterval: resolved.hostPowerMonitorIdleInterval,
|
||||
idleClientTtl: resolved.idleClientTtl,
|
||||
pauseWhenHostLocked: resolved.pauseWhenHostLocked,
|
||||
pauseWhenHostLowPower: resolved.pauseWhenHostLowPower,
|
||||
pauseWhenClientLowPower: resolved.pauseWhenClientLowPower,
|
||||
pauseWhenOnBattery: resolved.pauseWhenOnBattery,
|
||||
...overrides,
|
||||
};
|
||||
for (const [key, value] of Object.entries(nextOverrides)) {
|
||||
if (value === undefined) {
|
||||
delete nextOverrides[key as keyof typeof nextOverrides];
|
||||
}
|
||||
}
|
||||
return {
|
||||
backgroundActivity: {
|
||||
schemaVersion: 1 as const,
|
||||
profile: "custom" as const,
|
||||
baseProfile: getBackgroundActivityBaseProfile(current),
|
||||
overrides: nextOverrides as BackgroundActivitySettings["overrides"],
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,30 +1,16 @@
|
||||
import {
|
||||
ArchiveIcon,
|
||||
ArchiveX,
|
||||
InfoIcon,
|
||||
LoaderIcon,
|
||||
PlusIcon,
|
||||
RefreshCwIcon,
|
||||
SettingsIcon,
|
||||
} from "lucide-react";
|
||||
import { ArchiveIcon, ArchiveX, LoaderIcon, SettingsIcon } from "lucide-react";
|
||||
import { Link } from "@tanstack/react-router";
|
||||
import type { CSSProperties, ReactNode } from "react";
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useAtomValue } from "@effect/atom-react";
|
||||
import {
|
||||
defaultInstanceIdForDriver,
|
||||
type BackgroundActivityProfile,
|
||||
type BackgroundActivitySettings,
|
||||
type DesktopUpdateChannel,
|
||||
PROVIDER_DISPLAY_NAMES,
|
||||
ProviderDriverKind,
|
||||
type ProviderInstanceConfig,
|
||||
type ProviderInstanceId,
|
||||
type ScopedThreadRef,
|
||||
type SidebarProjectGroupingMode,
|
||||
} from "@t3tools/contracts";
|
||||
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
|
||||
import { safeErrorLogAttributes } from "@t3tools/client-runtime/errors";
|
||||
import {
|
||||
isAtomCommandInterrupted,
|
||||
settlePromise,
|
||||
@@ -45,16 +31,10 @@ import {
|
||||
MIN_PROMPT_FONT_SIZE,
|
||||
MIN_TERMINAL_FONT_SIZE,
|
||||
} from "@t3tools/contracts/settings";
|
||||
import {
|
||||
getBackgroundActivityBaseProfile,
|
||||
getBackgroundActivityPresetSettings,
|
||||
resolveServerBackgroundActivitySettings,
|
||||
} from "@t3tools/shared/backgroundActivitySettings";
|
||||
import { resolveServerBackgroundActivitySettings } from "@t3tools/shared/backgroundActivitySettings";
|
||||
import { createModelSelection } from "@t3tools/shared/model";
|
||||
import * as Arr from "effect/Array";
|
||||
import * as Duration from "effect/Duration";
|
||||
import * as Equal from "effect/Equal";
|
||||
import * as Result from "effect/Result";
|
||||
import * as Schema from "effect/Schema";
|
||||
import { APP_VERSION, HOSTED_APP_CHANNEL, HOSTED_APP_CHANNEL_LABEL } from "../../branding";
|
||||
import {
|
||||
@@ -88,15 +68,10 @@ import {
|
||||
} from "../../providerInstances";
|
||||
import { ensureLocalApi, readLocalApi } from "../../localApi";
|
||||
import { isMacPlatform } from "../../lib/utils";
|
||||
import {
|
||||
primaryServerObservabilityAtom,
|
||||
primaryServerProvidersAtom,
|
||||
serverEnvironment,
|
||||
} from "../../state/server";
|
||||
import { usePrimaryEnvironment } from "../../state/environments";
|
||||
import { primaryServerObservabilityAtom, primaryServerProvidersAtom } from "../../state/server";
|
||||
import { useProjects } from "../../state/entities";
|
||||
import { useArchivedThreadSnapshots } from "../../lib/archivedThreadsState";
|
||||
import { formatRelativeTimeLabel, getRelativeTimeState } from "../../timestampFormat";
|
||||
import { formatRelativeTimeLabel } from "../../timestampFormat";
|
||||
import { Button } from "../ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
@@ -131,20 +106,13 @@ import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../
|
||||
import { Switch } from "../ui/switch";
|
||||
import { stackedThreadToast, toastManager } from "../ui/toast";
|
||||
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
|
||||
import { AddProviderInstanceDialog } from "./AddProviderInstanceDialog";
|
||||
import {
|
||||
canOneClickUpdateProviderCandidate,
|
||||
collectProviderUpdateCandidates,
|
||||
hasOneClickUpdateProviderCandidate,
|
||||
isProviderUpdateActive,
|
||||
type ProviderUpdateCandidate,
|
||||
} from "../ProviderUpdateLaunchNotification.logic";
|
||||
import { ProviderInstanceCard } from "./ProviderInstanceCard";
|
||||
import { DRIVER_OPTIONS, getDriverOption } from "./providerDriverMeta";
|
||||
import {
|
||||
backgroundActivityOverrideSettings,
|
||||
backgroundActivitySharedPolicySettings,
|
||||
buildProviderInstanceUpdatePatch,
|
||||
durationToSeconds,
|
||||
formatDiagnosticsDescription,
|
||||
normalizeIntervalSeconds,
|
||||
PROVIDER_HEALTH_INTERVAL_STEP_SECONDS,
|
||||
hasChangedBackgroundActivitySettings,
|
||||
isProjectGroupingEnabled,
|
||||
projectGroupingModeFromToggle,
|
||||
@@ -153,16 +121,15 @@ import {
|
||||
resolveBackgroundActivityProfileOption,
|
||||
} from "./SettingsPanels.logic";
|
||||
import {
|
||||
PolicyTooltip,
|
||||
SettingResetButton,
|
||||
SettingsPageContainer,
|
||||
SettingsRow,
|
||||
SettingsSection,
|
||||
useRelativeTimeTick,
|
||||
useSettingsSearchTargetId,
|
||||
} from "./settingsLayout";
|
||||
import { searchableSetting } from "./settingsSearch";
|
||||
import { ProjectFavicon } from "../ProjectFavicon";
|
||||
import { useAtomCommand } from "../../state/use-atom-command";
|
||||
|
||||
const THEME_OPTIONS = [
|
||||
{
|
||||
@@ -198,11 +165,6 @@ const BACKGROUND_ACTIVITY_PROFILE_LABELS: Record<BackgroundActivityProfile, stri
|
||||
};
|
||||
|
||||
type BackgroundActivityProfileOption = BackgroundActivityProfile | "advanced";
|
||||
type BackgroundActivityOverridePatch = Partial<{
|
||||
[K in keyof BackgroundActivitySettings["overrides"]]:
|
||||
| BackgroundActivitySettings["overrides"][K]
|
||||
| undefined;
|
||||
}>;
|
||||
|
||||
const BACKGROUND_ACTIVITY_PROFILE_OPTION_LABELS: Record<BackgroundActivityProfileOption, string> = {
|
||||
...BACKGROUND_ACTIVITY_PROFILE_LABELS,
|
||||
@@ -219,7 +181,6 @@ const BACKGROUND_ACTIVITY_PROFILE_DESCRIPTIONS: Record<BackgroundActivityProfile
|
||||
const ADVANCED_BACKGROUND_ACTIVITY_DESCRIPTION =
|
||||
"Uses custom background intervals with the selected shared power policy.";
|
||||
|
||||
const PROVIDER_HEALTH_INTERVAL_STEP_SECONDS = 30;
|
||||
const DEFAULT_DRIVER_KIND = ProviderDriverKind.make("codex");
|
||||
const BACKGROUND_ACTIVITY_BOOLEAN_OVERRIDES: ReadonlyArray<{
|
||||
readonly key:
|
||||
@@ -235,17 +196,6 @@ const BACKGROUND_ACTIVITY_BOOLEAN_OVERRIDES: ReadonlyArray<{
|
||||
{ key: "pauseWhenOnBattery", label: "Pause on battery" },
|
||||
];
|
||||
|
||||
function durationToSeconds(duration: Duration.Duration): number {
|
||||
return Math.round(Duration.toMillis(duration) / 1_000);
|
||||
}
|
||||
|
||||
function normalizeIntervalSeconds(value: number | null, minimum = 0): number {
|
||||
if (value === null || !Number.isFinite(value)) {
|
||||
return minimum;
|
||||
}
|
||||
return Math.max(minimum, Math.round(value));
|
||||
}
|
||||
|
||||
function resetBackgroundActivitySettings() {
|
||||
return {
|
||||
backgroundActivity: DEFAULT_UNIFIED_SETTINGS.backgroundActivity,
|
||||
@@ -262,105 +212,6 @@ function backgroundActivityProfileSettings(profile: BackgroundActivityProfile) {
|
||||
};
|
||||
}
|
||||
|
||||
function backgroundActivityOverrideSettings(
|
||||
current: BackgroundActivitySettings,
|
||||
resolved: ReturnType<typeof resolveServerBackgroundActivitySettings>,
|
||||
overrides: BackgroundActivityOverridePatch,
|
||||
) {
|
||||
const nextOverrides: BackgroundActivityOverridePatch = {
|
||||
automaticGitFetchInterval: resolved.automaticGitFetchInterval,
|
||||
providerHealthRefreshInterval: resolved.providerHealthRefreshInterval,
|
||||
hostPowerMonitorActiveInterval: resolved.hostPowerMonitorActiveInterval,
|
||||
hostPowerMonitorIdleInterval: resolved.hostPowerMonitorIdleInterval,
|
||||
idleClientTtl: resolved.idleClientTtl,
|
||||
pauseWhenHostLocked: resolved.pauseWhenHostLocked,
|
||||
pauseWhenHostLowPower: resolved.pauseWhenHostLowPower,
|
||||
pauseWhenClientLowPower: resolved.pauseWhenClientLowPower,
|
||||
pauseWhenOnBattery: resolved.pauseWhenOnBattery,
|
||||
...overrides,
|
||||
};
|
||||
for (const [key, value] of Object.entries(nextOverrides)) {
|
||||
if (value === undefined) {
|
||||
delete nextOverrides[key as keyof typeof nextOverrides];
|
||||
}
|
||||
}
|
||||
return {
|
||||
backgroundActivity: {
|
||||
schemaVersion: 1 as const,
|
||||
profile: "custom" as const,
|
||||
baseProfile: getBackgroundActivityBaseProfile(current),
|
||||
overrides: nextOverrides as BackgroundActivitySettings["overrides"],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function PolicyTooltip({ children }: { readonly children: string }) {
|
||||
return (
|
||||
<Tooltip>
|
||||
<TooltipTrigger
|
||||
render={
|
||||
<button
|
||||
type="button"
|
||||
className="inline-flex size-5 items-center justify-center rounded-sm text-muted-foreground hover:text-foreground"
|
||||
aria-label="Background policy details"
|
||||
>
|
||||
<InfoIcon className="size-3.5" />
|
||||
</button>
|
||||
}
|
||||
/>
|
||||
<TooltipPopup side="top" className="max-w-72">
|
||||
{children}
|
||||
</TooltipPopup>
|
||||
</Tooltip>
|
||||
);
|
||||
}
|
||||
|
||||
function withoutProviderInstanceKey<V>(
|
||||
record: Readonly<Record<ProviderInstanceId, V>> | undefined,
|
||||
key: ProviderInstanceId,
|
||||
): Record<ProviderInstanceId, V> {
|
||||
const next = { ...record } as Record<ProviderInstanceId, V>;
|
||||
delete next[key];
|
||||
return next;
|
||||
}
|
||||
|
||||
function withoutProviderInstanceFavorites(
|
||||
favorites: ReadonlyArray<{ readonly provider: ProviderInstanceId; readonly model: string }>,
|
||||
instanceId: ProviderInstanceId,
|
||||
) {
|
||||
return favorites.filter((favorite) => favorite.provider !== instanceId);
|
||||
}
|
||||
|
||||
const PROVIDER_SETTINGS = DRIVER_OPTIONS.map((definition) => ({
|
||||
provider: definition.value,
|
||||
}));
|
||||
|
||||
function ProviderLastChecked({ lastCheckedAt }: { lastCheckedAt: string | null }) {
|
||||
useRelativeTimeTick();
|
||||
const lastCheckedRelative = getRelativeTimeState(lastCheckedAt);
|
||||
|
||||
if (lastCheckedRelative.status === "missing") {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (lastCheckedRelative.status === "invalid") {
|
||||
return <span className="text-[11px] text-muted-foreground/50">Checked unavailable</span>;
|
||||
}
|
||||
|
||||
return (
|
||||
<span className="text-[11px] text-muted-foreground/60">
|
||||
{lastCheckedRelative.suffix ? (
|
||||
<>
|
||||
Checked <span className="font-mono tabular-nums">{lastCheckedRelative.value}</span>{" "}
|
||||
{lastCheckedRelative.suffix}
|
||||
</>
|
||||
) : (
|
||||
<>Checked {lastCheckedRelative.value}</>
|
||||
)}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
function AboutVersionTitle() {
|
||||
return (
|
||||
<span className="inline-flex items-center gap-2">
|
||||
@@ -2146,522 +1997,6 @@ export function GeneralSettingsPanel() {
|
||||
);
|
||||
}
|
||||
|
||||
export function ProviderSettingsPanel() {
|
||||
const settings = usePrimarySettings();
|
||||
const updateSettings = useUpdatePrimarySettings();
|
||||
const serverProviders = useAtomValue(primaryServerProvidersAtom);
|
||||
const primaryEnvironment = usePrimaryEnvironment();
|
||||
const refreshServerProviders = useAtomCommand(serverEnvironment.refreshProviders, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const updateProvider = useAtomCommand(serverEnvironment.updateProvider, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const [isRefreshingProviders, setIsRefreshingProviders] = useState(false);
|
||||
const [isAddInstanceDialogOpen, setIsAddInstanceDialogOpen] = useState(false);
|
||||
const [updatingProviderDrivers, setUpdatingProviderDrivers] = useState<
|
||||
ReadonlySet<ProviderDriverKind>
|
||||
>(() => new Set());
|
||||
const [openInstanceDetails, setOpenInstanceDetails] = useState<Record<string, boolean>>({});
|
||||
const refreshingRef = useRef(false);
|
||||
|
||||
const providerUpdateCandidates = useMemo(
|
||||
() => collectProviderUpdateCandidates(serverProviders),
|
||||
[serverProviders],
|
||||
);
|
||||
const providerUpdateCandidateByInstanceId = useMemo(
|
||||
() => new Map(providerUpdateCandidates.map((candidate) => [candidate.instanceId, candidate])),
|
||||
[providerUpdateCandidates],
|
||||
);
|
||||
const visibleProviderSettings = PROVIDER_SETTINGS.filter(
|
||||
(providerSettings) =>
|
||||
providerSettings.provider !== "cursor" ||
|
||||
serverProviders.some(
|
||||
(provider) =>
|
||||
provider.instanceId === defaultInstanceIdForDriver(ProviderDriverKind.make("cursor")),
|
||||
),
|
||||
);
|
||||
const textGenerationModelSelection = resolveAppModelSelectionState(settings, serverProviders);
|
||||
const textGenInstanceId = textGenerationModelSelection.instanceId;
|
||||
const resolvedBackgroundActivity = resolveServerBackgroundActivitySettings(settings);
|
||||
const providerHealthPreset = getBackgroundActivityPresetSettings(
|
||||
resolvedBackgroundActivity.profile,
|
||||
).providerHealthRefreshInterval;
|
||||
const providerHealthRefreshIntervalSeconds = durationToSeconds(
|
||||
resolvedBackgroundActivity.providerHealthRefreshInterval,
|
||||
);
|
||||
const defaultProviderHealthRefreshIntervalSeconds = durationToSeconds(providerHealthPreset);
|
||||
const lastCheckedAt =
|
||||
serverProviders.length > 0
|
||||
? serverProviders.reduce(
|
||||
(latest, provider) => (provider.checkedAt > latest ? provider.checkedAt : latest),
|
||||
serverProviders[0]!.checkedAt,
|
||||
)
|
||||
: null;
|
||||
|
||||
const refreshProviders = useCallback(() => {
|
||||
if (refreshingRef.current) return;
|
||||
refreshingRef.current = true;
|
||||
setIsRefreshingProviders(true);
|
||||
if (!primaryEnvironment) {
|
||||
refreshingRef.current = false;
|
||||
setIsRefreshingProviders(false);
|
||||
return;
|
||||
}
|
||||
void (async () => {
|
||||
const result = await refreshServerProviders({
|
||||
environmentId: primaryEnvironment.environmentId,
|
||||
input: {},
|
||||
});
|
||||
refreshingRef.current = false;
|
||||
setIsRefreshingProviders(false);
|
||||
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
|
||||
console.warn("Failed to refresh providers", {
|
||||
operation: "refresh-providers",
|
||||
environmentId: primaryEnvironment.environmentId,
|
||||
...safeErrorLogAttributes(squashAtomCommandFailure(result)),
|
||||
});
|
||||
}
|
||||
})();
|
||||
}, [primaryEnvironment, refreshServerProviders]);
|
||||
|
||||
const runProviderUpdate = useCallback(
|
||||
async (candidate: ProviderUpdateCandidate) => {
|
||||
if (!primaryEnvironment) return;
|
||||
let started = false;
|
||||
setUpdatingProviderDrivers((previous) => {
|
||||
if (previous.has(candidate.driver)) {
|
||||
return previous;
|
||||
}
|
||||
started = true;
|
||||
const next = new Set(previous);
|
||||
next.add(candidate.driver);
|
||||
return next;
|
||||
});
|
||||
if (!started) {
|
||||
return;
|
||||
}
|
||||
|
||||
const result = await updateProvider({
|
||||
environmentId: primaryEnvironment.environmentId,
|
||||
input: {
|
||||
provider: candidate.driver,
|
||||
instanceId: candidate.instanceId,
|
||||
},
|
||||
});
|
||||
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
|
||||
const error = squashAtomCommandFailure(result);
|
||||
toastManager.add(
|
||||
stackedThreadToast({
|
||||
type: "error",
|
||||
title: `Could not update ${PROVIDER_DISPLAY_NAMES[candidate.driver] ?? candidate.driver}`,
|
||||
description:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: "The provider update command could not be started.",
|
||||
}),
|
||||
);
|
||||
}
|
||||
setUpdatingProviderDrivers((previous) => {
|
||||
if (!previous.has(candidate.driver)) {
|
||||
return previous;
|
||||
}
|
||||
const next = new Set(previous);
|
||||
next.delete(candidate.driver);
|
||||
return next;
|
||||
});
|
||||
},
|
||||
[primaryEnvironment, updateProvider],
|
||||
);
|
||||
|
||||
interface InstanceRow {
|
||||
readonly instanceId: ProviderInstanceId;
|
||||
readonly instance: ProviderInstanceConfig;
|
||||
readonly driver: ProviderDriverKind;
|
||||
readonly isDefault: boolean;
|
||||
readonly isDirty?: boolean;
|
||||
}
|
||||
|
||||
const instancesByDriver = new Map<
|
||||
ProviderDriverKind,
|
||||
Array<[ProviderInstanceId, ProviderInstanceConfig]>
|
||||
>();
|
||||
for (const [rawId, instance] of Object.entries(settings.providerInstances ?? {})) {
|
||||
const driver = instance.driver;
|
||||
const list = instancesByDriver.get(driver) ?? [];
|
||||
list.push([rawId as ProviderInstanceId, instance]);
|
||||
instancesByDriver.set(driver, list);
|
||||
}
|
||||
|
||||
const defaultSlotIdsBySource = new Set<string>(
|
||||
visibleProviderSettings.map((providerSettings) =>
|
||||
String(defaultInstanceIdForDriver(providerSettings.provider)),
|
||||
),
|
||||
);
|
||||
|
||||
const rows: InstanceRow[] = [];
|
||||
const visibleDriverKinds = new Set<ProviderDriverKind>(
|
||||
visibleProviderSettings.map((providerSettings) => providerSettings.provider),
|
||||
);
|
||||
|
||||
for (const providerSettings of visibleProviderSettings) {
|
||||
type LegacyProviderSettings = (typeof settings.providers)[keyof typeof settings.providers];
|
||||
const legacyProviders = settings.providers as Record<string, LegacyProviderSettings>;
|
||||
const defaultLegacyProviders = DEFAULT_UNIFIED_SETTINGS.providers as Record<
|
||||
string,
|
||||
LegacyProviderSettings
|
||||
>;
|
||||
const driver = providerSettings.provider;
|
||||
const defaultInstanceId = defaultInstanceIdForDriver(driver);
|
||||
const explicitInstance = settings.providerInstances?.[defaultInstanceId];
|
||||
const legacyConfig = legacyProviders[providerSettings.provider]!;
|
||||
const defaultLegacyConfig = defaultLegacyProviders[providerSettings.provider]!;
|
||||
const effectiveInstance: ProviderInstanceConfig =
|
||||
explicitInstance ??
|
||||
({
|
||||
driver,
|
||||
enabled: legacyConfig.enabled,
|
||||
config: legacyConfig,
|
||||
} satisfies ProviderInstanceConfig);
|
||||
const isDirty =
|
||||
explicitInstance !== undefined || !Equal.equals(legacyConfig, defaultLegacyConfig);
|
||||
rows.push({
|
||||
instanceId: defaultInstanceId,
|
||||
instance: effectiveInstance,
|
||||
driver,
|
||||
isDefault: true,
|
||||
isDirty,
|
||||
});
|
||||
for (const [id, instance] of instancesByDriver.get(providerSettings.provider) ?? []) {
|
||||
if (id === defaultInstanceId) continue;
|
||||
rows.push({ instanceId: id, instance, driver: instance.driver, isDefault: false });
|
||||
}
|
||||
}
|
||||
for (const [driver, list] of instancesByDriver) {
|
||||
if (visibleDriverKinds.has(driver)) continue;
|
||||
for (const [id, instance] of list) {
|
||||
rows.push({
|
||||
instanceId: id,
|
||||
instance,
|
||||
driver: instance.driver,
|
||||
isDefault: defaultSlotIdsBySource.has(String(id)),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const updateProviderInstance = (
|
||||
row: InstanceRow,
|
||||
next: ProviderInstanceConfig,
|
||||
options?: {
|
||||
readonly textGenerationModelSelection?: Parameters<
|
||||
typeof buildProviderInstanceUpdatePatch
|
||||
>[0]["textGenerationModelSelection"];
|
||||
},
|
||||
) => {
|
||||
updateSettings(
|
||||
buildProviderInstanceUpdatePatch({
|
||||
settings,
|
||||
instanceId: row.instanceId,
|
||||
instance: next,
|
||||
driver: row.driver,
|
||||
isDefault: row.isDefault,
|
||||
textGenerationModelSelection: options?.textGenerationModelSelection,
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
const deleteProviderInstance = (id: ProviderInstanceId) => {
|
||||
updateSettings({
|
||||
providerInstances: withoutProviderInstanceKey(settings.providerInstances, id),
|
||||
providerModelPreferences: withoutProviderInstanceKey(settings.providerModelPreferences, id),
|
||||
favorites: withoutProviderInstanceFavorites(settings.favorites ?? [], id),
|
||||
});
|
||||
};
|
||||
|
||||
const updateProviderModelPreferences = (
|
||||
instanceId: ProviderInstanceId,
|
||||
next: {
|
||||
readonly hiddenModels: ReadonlyArray<string>;
|
||||
readonly modelOrder: ReadonlyArray<string>;
|
||||
},
|
||||
) => {
|
||||
const hiddenModels = [...new Set(next.hiddenModels.filter((slug) => slug.trim().length > 0))];
|
||||
const modelOrder = [...new Set(next.modelOrder.filter((slug) => slug.trim().length > 0))];
|
||||
const rest = withoutProviderInstanceKey(settings.providerModelPreferences, instanceId);
|
||||
updateSettings({
|
||||
providerModelPreferences:
|
||||
hiddenModels.length === 0 && modelOrder.length === 0
|
||||
? rest
|
||||
: {
|
||||
...rest,
|
||||
[instanceId]: {
|
||||
hiddenModels,
|
||||
modelOrder,
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const updateProviderFavoriteModels = (
|
||||
instanceId: ProviderInstanceId,
|
||||
nextFavoriteModels: ReadonlyArray<string>,
|
||||
) => {
|
||||
const favoriteModels = [
|
||||
...new Set(
|
||||
Arr.filterMap(nextFavoriteModels, (slug) => {
|
||||
const trimmedSlug = slug.trim();
|
||||
return trimmedSlug.length > 0 ? Result.succeed(trimmedSlug) : Result.failVoid;
|
||||
}),
|
||||
),
|
||||
];
|
||||
updateSettings({
|
||||
favorites: [
|
||||
...withoutProviderInstanceFavorites(settings.favorites ?? [], instanceId),
|
||||
...favoriteModels.map((model) => ({ provider: instanceId, model })),
|
||||
],
|
||||
});
|
||||
};
|
||||
|
||||
const resetDefaultInstance = (driverKind: ProviderDriverKind) => {
|
||||
type LegacyProviderSettings = (typeof settings.providers)[keyof typeof settings.providers];
|
||||
const defaultLegacyProviders = DEFAULT_UNIFIED_SETTINGS.providers as Record<
|
||||
string,
|
||||
LegacyProviderSettings | undefined
|
||||
>;
|
||||
const defaultInstanceId = defaultInstanceIdForDriver(driverKind);
|
||||
const defaultLegacyProvider = defaultLegacyProviders[driverKind];
|
||||
if (defaultLegacyProvider === undefined) return;
|
||||
updateSettings({
|
||||
providers: {
|
||||
...settings.providers,
|
||||
[driverKind]: defaultLegacyProvider,
|
||||
} as typeof settings.providers,
|
||||
providerInstances: withoutProviderInstanceKey(settings.providerInstances, defaultInstanceId),
|
||||
providerModelPreferences: withoutProviderInstanceKey(
|
||||
settings.providerModelPreferences,
|
||||
defaultInstanceId,
|
||||
),
|
||||
favorites: withoutProviderInstanceFavorites(settings.favorites ?? [], defaultInstanceId),
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<SettingsPageContainer>
|
||||
<SettingsSection
|
||||
{...searchableSetting("providers")}
|
||||
headerAction={
|
||||
<div className="flex items-center gap-1.5">
|
||||
<ProviderLastChecked lastCheckedAt={lastCheckedAt} />
|
||||
<Tooltip>
|
||||
<TooltipTrigger
|
||||
render={
|
||||
<Button
|
||||
size="icon-xs"
|
||||
variant="ghost"
|
||||
className="size-5 rounded-sm p-0 text-muted-foreground hover:text-foreground"
|
||||
onClick={() => setIsAddInstanceDialogOpen(true)}
|
||||
aria-label="Add provider instance"
|
||||
>
|
||||
<PlusIcon className="size-3" />
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
<TooltipPopup side="top">Add provider instance</TooltipPopup>
|
||||
</Tooltip>
|
||||
<Tooltip>
|
||||
<TooltipTrigger
|
||||
render={
|
||||
<Button
|
||||
size="icon-xs"
|
||||
variant="ghost"
|
||||
className="size-5 rounded-sm p-0 text-muted-foreground hover:text-foreground"
|
||||
disabled={isRefreshingProviders}
|
||||
onClick={() => void refreshProviders()}
|
||||
aria-label="Refresh provider status"
|
||||
>
|
||||
{isRefreshingProviders ? (
|
||||
<LoaderIcon className="size-3 animate-spin" />
|
||||
) : (
|
||||
<RefreshCwIcon className="size-3" />
|
||||
)}
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
<TooltipPopup side="top">Refresh provider status</TooltipPopup>
|
||||
</Tooltip>
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<SettingsRow
|
||||
title={
|
||||
<span className="inline-flex items-center gap-1.5">
|
||||
Health check interval
|
||||
<PolicyTooltip>
|
||||
This interval is configured here, then the shared Background activity policy decides
|
||||
whether provider probes may run when the timer fires. Custom intervals appear as
|
||||
Advanced in General settings.
|
||||
</PolicyTooltip>
|
||||
</span>
|
||||
}
|
||||
description="Refresh provider availability, versions, auth state, and model metadata in the background. Set this to 0 seconds to rely on manual refreshes."
|
||||
resetAction={
|
||||
providerHealthRefreshIntervalSeconds !== defaultProviderHealthRefreshIntervalSeconds ? (
|
||||
<SettingResetButton
|
||||
label="provider health check interval"
|
||||
onClick={() =>
|
||||
updateSettings(
|
||||
backgroundActivityOverrideSettings(
|
||||
settings.backgroundActivity,
|
||||
resolvedBackgroundActivity,
|
||||
{
|
||||
providerHealthRefreshInterval: undefined,
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
||||
/>
|
||||
) : null
|
||||
}
|
||||
control={
|
||||
<div className="flex shrink-0 items-center gap-2">
|
||||
<NumberField
|
||||
value={providerHealthRefreshIntervalSeconds}
|
||||
min={0}
|
||||
step={PROVIDER_HEALTH_INTERVAL_STEP_SECONDS}
|
||||
size="sm"
|
||||
className="w-32"
|
||||
onValueChange={(value) =>
|
||||
updateSettings(
|
||||
backgroundActivityOverrideSettings(
|
||||
settings.backgroundActivity,
|
||||
resolvedBackgroundActivity,
|
||||
{
|
||||
providerHealthRefreshInterval: Duration.seconds(
|
||||
normalizeIntervalSeconds(value),
|
||||
),
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
||||
>
|
||||
<NumberFieldGroup>
|
||||
<NumberFieldDecrement aria-label="Decrease provider health check interval" />
|
||||
<NumberFieldInput aria-label="Provider health check interval in seconds" />
|
||||
<NumberFieldIncrement aria-label="Increase provider health check interval" />
|
||||
</NumberFieldGroup>
|
||||
</NumberField>
|
||||
<span className="text-xs text-muted-foreground">seconds</span>
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
|
||||
{rows.map((row) => {
|
||||
const driverOption = getDriverOption(row.driver);
|
||||
const liveProvider = serverProviders.find(
|
||||
(candidate) => candidate.instanceId === row.instanceId,
|
||||
);
|
||||
const updateCandidate = liveProvider
|
||||
? providerUpdateCandidateByInstanceId.get(liveProvider.instanceId)
|
||||
: undefined;
|
||||
const isDriverUpdateRunning =
|
||||
updateCandidate !== undefined &&
|
||||
(updatingProviderDrivers.has(updateCandidate.driver) ||
|
||||
serverProviders.some(
|
||||
(provider) =>
|
||||
provider.driver === updateCandidate.driver && isProviderUpdateActive(provider),
|
||||
));
|
||||
const showInlineUpdateButton =
|
||||
updateCandidate !== undefined &&
|
||||
hasOneClickUpdateProviderCandidate(updateCandidate, serverProviders);
|
||||
const canRunInlineUpdate =
|
||||
updateCandidate !== undefined &&
|
||||
canOneClickUpdateProviderCandidate(updateCandidate, serverProviders) &&
|
||||
!updatingProviderDrivers.has(updateCandidate.driver);
|
||||
const modelPreferences = settings.providerModelPreferences?.[row.instanceId] ?? {
|
||||
hiddenModels: [],
|
||||
modelOrder: [],
|
||||
};
|
||||
const favoriteModels = Arr.filterMap(settings.favorites ?? [], (favorite) =>
|
||||
favorite.provider === row.instanceId ? Result.succeed(favorite.model) : Result.failVoid,
|
||||
);
|
||||
const resetLabel = driverOption?.label ?? String(row.driver);
|
||||
const headerAction =
|
||||
row.isDefault && row.isDirty ? (
|
||||
<SettingResetButton
|
||||
label={`${resetLabel} provider settings`}
|
||||
onClick={() => resetDefaultInstance(row.driver)}
|
||||
/>
|
||||
) : null;
|
||||
return (
|
||||
<ProviderInstanceCard
|
||||
key={row.instanceId}
|
||||
instanceId={row.instanceId}
|
||||
instance={row.instance}
|
||||
driverOption={driverOption}
|
||||
liveProvider={liveProvider}
|
||||
isExpanded={openInstanceDetails[row.instanceId] ?? false}
|
||||
onExpandedChange={(open) =>
|
||||
setOpenInstanceDetails((existing) => ({
|
||||
...existing,
|
||||
[row.instanceId]: open,
|
||||
}))
|
||||
}
|
||||
onUpdate={(next) => {
|
||||
const wasEnabled = row.instance.enabled ?? true;
|
||||
const isDisabling = next.enabled === false && wasEnabled;
|
||||
const shouldClearTextGen = isDisabling && textGenInstanceId === row.instanceId;
|
||||
if (shouldClearTextGen) {
|
||||
updateProviderInstance(row, next, {
|
||||
textGenerationModelSelection:
|
||||
DEFAULT_UNIFIED_SETTINGS.textGenerationModelSelection,
|
||||
});
|
||||
} else {
|
||||
updateProviderInstance(row, next);
|
||||
}
|
||||
}}
|
||||
onDelete={row.isDefault ? undefined : () => deleteProviderInstance(row.instanceId)}
|
||||
headerAction={headerAction}
|
||||
hiddenModels={modelPreferences.hiddenModels}
|
||||
favoriteModels={favoriteModels}
|
||||
modelOrder={modelPreferences.modelOrder}
|
||||
onHiddenModelsChange={(hiddenModels) =>
|
||||
updateProviderModelPreferences(row.instanceId, {
|
||||
...modelPreferences,
|
||||
hiddenModels,
|
||||
})
|
||||
}
|
||||
onFavoriteModelsChange={(favoriteModels) =>
|
||||
updateProviderFavoriteModels(row.instanceId, favoriteModels)
|
||||
}
|
||||
onModelOrderChange={(modelOrder) =>
|
||||
updateProviderModelPreferences(row.instanceId, {
|
||||
...modelPreferences,
|
||||
modelOrder,
|
||||
})
|
||||
}
|
||||
onRunUpdate={
|
||||
showInlineUpdateButton && updateCandidate
|
||||
? () => {
|
||||
if (!canRunInlineUpdate) {
|
||||
return;
|
||||
}
|
||||
void runProviderUpdate(updateCandidate);
|
||||
}
|
||||
: undefined
|
||||
}
|
||||
isUpdating={showInlineUpdateButton ? isDriverUpdateRunning : undefined}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</SettingsSection>
|
||||
|
||||
{isAddInstanceDialogOpen ? (
|
||||
<AddProviderInstanceDialog open onOpenChange={setIsAddInstanceDialogOpen} />
|
||||
) : null}
|
||||
</SettingsPageContainer>
|
||||
);
|
||||
}
|
||||
|
||||
export function ArchivedThreadsPanel() {
|
||||
const projects = useProjects();
|
||||
const { unarchiveThread, confirmAndDeleteThread } = useThreadActions();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Undo2Icon } from "lucide-react";
|
||||
import { InfoIcon, Undo2Icon } from "lucide-react";
|
||||
import { useLocation, useNavigate } from "@tanstack/react-router";
|
||||
import {
|
||||
createContext,
|
||||
@@ -83,6 +83,28 @@ function useSettingsSearchTarget<T extends HTMLElement>(id: string | undefined)
|
||||
return targetRef;
|
||||
}
|
||||
|
||||
/** Info affordance explaining how a setting interacts with the shared background policy. */
|
||||
export function PolicyTooltip({ children }: { readonly children: string }) {
|
||||
return (
|
||||
<Tooltip>
|
||||
<TooltipTrigger
|
||||
render={
|
||||
<button
|
||||
type="button"
|
||||
className="inline-flex size-5 items-center justify-center rounded-sm text-muted-foreground hover:text-foreground"
|
||||
aria-label="Background policy details"
|
||||
>
|
||||
<InfoIcon className="size-3.5" />
|
||||
</button>
|
||||
}
|
||||
/>
|
||||
<TooltipPopup side="top" className="max-w-72">
|
||||
{children}
|
||||
</TooltipPopup>
|
||||
</Tooltip>
|
||||
);
|
||||
}
|
||||
|
||||
/** Re-render every `intervalMs`; return a stable timestamp snapshot for render-time relative labels. */
|
||||
export function useRelativeTimeTick(intervalMs = 1_000) {
|
||||
const [nowMs, setNowMs] = useState(() => Date.now());
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { createFileRoute } from "@tanstack/react-router";
|
||||
|
||||
import { ProviderSettingsPanel } from "../components/settings/SettingsPanels";
|
||||
import { ProviderSettingsPanel } from "../components/settings/ProviderSettingsPanel";
|
||||
|
||||
function SettingsProvidersRoute() {
|
||||
return <ProviderSettingsPanel />;
|
||||
|
||||
@@ -33,7 +33,7 @@ interface PrimaryServerState {
|
||||
}
|
||||
|
||||
const EMPTY_AVAILABLE_EDITORS: ReadonlyArray<EditorId> = [];
|
||||
const EMPTY_SERVER_PROVIDERS: ReadonlyArray<ServerProvider> = [];
|
||||
export const EMPTY_SERVER_PROVIDERS: ReadonlyArray<ServerProvider> = [];
|
||||
const EMPTY_PRIMARY_SERVER_STATE: PrimaryServerState = {
|
||||
config: null,
|
||||
latestEvent: null,
|
||||
|
||||
@@ -2,7 +2,7 @@ import { useAtomValue } from "@effect/atom-react";
|
||||
import { createEnvironmentSessionAtoms } from "@t3tools/client-runtime/state/session";
|
||||
import type { EnvironmentId } from "@t3tools/contracts";
|
||||
import * as Option from "effect/Option";
|
||||
import { Atom } from "effect/unstable/reactivity";
|
||||
import { AsyncResult, Atom } from "effect/unstable/reactivity";
|
||||
|
||||
import { connectionAtomRuntime } from "../connection/runtime";
|
||||
import { appAtomRegistry } from "../rpc/atomRegistry";
|
||||
@@ -26,3 +26,17 @@ export function readPreparedConnection(environmentId: EnvironmentId) {
|
||||
appAtomRegistry.get(environmentSession.preparedConnectionValueAtom(environmentId)),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* This client's authenticated session on one environment, as reported by that
|
||||
* environment's `/api/auth/session` endpoint. `data` stays populated across
|
||||
* SWR revalidations; `isPending` is only meaningful before the first resolve.
|
||||
*/
|
||||
export function useEnvironmentSessionState(environmentId: EnvironmentId) {
|
||||
const result = useAtomValue(environmentSession.sessionStateAtom(environmentId));
|
||||
return {
|
||||
data: Option.getOrNull(AsyncResult.value(result)),
|
||||
hasError: result._tag === "Failure",
|
||||
isPending: result.waiting,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { isValidElement, type ReactElement } from "react";
|
||||
|
||||
/**
|
||||
* Depth-first search over a React element tree produced by calling a component
|
||||
* as a plain function (see `reactHookHarness`). Descends through props so
|
||||
* render-prop and slot-style children are reachable. Returns the first element
|
||||
* the visitor accepts, or null.
|
||||
*/
|
||||
export function visitElements(
|
||||
node: unknown,
|
||||
visitor: (element: ReactElement<Record<string, unknown>>) => boolean,
|
||||
): ReactElement<Record<string, unknown>> | null {
|
||||
if (Array.isArray(node)) {
|
||||
for (const child of node) {
|
||||
const found = visitElements(child, visitor);
|
||||
if (found) return found;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (!isValidElement<Record<string, unknown>>(node)) return null;
|
||||
if (visitor(node)) return node;
|
||||
for (const value of Object.values(node.props)) {
|
||||
const found = visitElements(value, visitor);
|
||||
if (found) return found;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
import type { Dispatch, SetStateAction } from "react";
|
||||
|
||||
/**
|
||||
* Minimal React hook shim for tests that call components as plain functions
|
||||
* instead of mounting a renderer. Slots are keyed by call order, mirroring
|
||||
* React's own rules-of-hooks contract, and `useMemoCache` emulates the React
|
||||
* Compiler runtime so compiled components can execute unmodified.
|
||||
*
|
||||
* This module must stay free of runtime `react` imports: it is loaded from
|
||||
* inside `vi.mock("react", ...)` factories, and a value import would recurse
|
||||
* into the in-progress mock. Wire it up in each test file (mock calls cannot
|
||||
* live here because vitest hoists them per test module):
|
||||
*
|
||||
* ```ts
|
||||
* import { reactHookHarness } from "~/test/reactHookHarness";
|
||||
*
|
||||
* vi.mock("react", async (importOriginal) => {
|
||||
* const actual = await importOriginal<typeof import("react")>();
|
||||
* const { reactHookHarness } = await import("~/test/reactHookHarness");
|
||||
* return {
|
||||
* ...actual,
|
||||
* useCallback: reactHookHarness.useCallback,
|
||||
* useMemo: reactHookHarness.useMemo,
|
||||
* useRef: reactHookHarness.useRef,
|
||||
* useState: reactHookHarness.useState,
|
||||
* };
|
||||
* });
|
||||
* vi.mock("react/compiler-runtime", async () => {
|
||||
* const { reactHookHarness } = await import("~/test/reactHookHarness");
|
||||
* return { c: reactHookHarness.useMemoCache };
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* Call `beginRender()` before each component invocation and `reset()` in
|
||||
* `beforeEach` to drop persisted state between tests.
|
||||
*/
|
||||
export function createReactHookHarness() {
|
||||
let cursor = 0;
|
||||
let slots: unknown[] = [];
|
||||
const nextIndex = () => cursor++;
|
||||
|
||||
return {
|
||||
beginRender() {
|
||||
cursor = 0;
|
||||
},
|
||||
reset() {
|
||||
cursor = 0;
|
||||
slots = [];
|
||||
},
|
||||
useCallback<T>(callback: T): T {
|
||||
nextIndex();
|
||||
return callback;
|
||||
},
|
||||
useMemo<T>(factory: () => T): T {
|
||||
nextIndex();
|
||||
return factory();
|
||||
},
|
||||
useMemoCache(size: number): unknown[] {
|
||||
const index = nextIndex();
|
||||
if (!slots[index]) {
|
||||
slots[index] = Array.from({ length: size }, () => Symbol.for("react.memo_cache_sentinel"));
|
||||
}
|
||||
return slots[index] as unknown[];
|
||||
},
|
||||
useRef<T>(initialValue: T): { current: T } {
|
||||
const index = nextIndex();
|
||||
if (!slots[index]) {
|
||||
slots[index] = { current: initialValue };
|
||||
}
|
||||
return slots[index] as { current: T };
|
||||
},
|
||||
useState<T>(initialValue: T | (() => T)): [T, Dispatch<SetStateAction<T>>] {
|
||||
const index = nextIndex();
|
||||
if (index >= slots.length) {
|
||||
slots[index] =
|
||||
typeof initialValue === "function" ? (initialValue as () => T)() : initialValue;
|
||||
}
|
||||
const setValue: Dispatch<SetStateAction<T>> = (nextValue) => {
|
||||
const previous = slots[index] as T;
|
||||
slots[index] =
|
||||
typeof nextValue === "function" ? (nextValue as (value: T) => T)(previous) : nextValue;
|
||||
};
|
||||
return [slots[index] as T, setValue];
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** Shared instance so `vi.mock` factories and test bodies see the same slots. */
|
||||
export const reactHookHarness = createReactHookHarness();
|
||||
@@ -1,14 +1,19 @@
|
||||
import type { EnvironmentId, ServerConfig } from "@t3tools/contracts";
|
||||
import type { AuthSessionState, EnvironmentId, ServerConfig } from "@t3tools/contracts";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Stream from "effect/Stream";
|
||||
import * as SubscriptionRef from "effect/SubscriptionRef";
|
||||
import type { HttpClient } from "effect/unstable/http";
|
||||
import { AsyncResult, Atom } from "effect/unstable/reactivity";
|
||||
|
||||
import { EnvironmentRegistry } from "../connection/registry.ts";
|
||||
import type { PreparedConnection } from "../connection/model.ts";
|
||||
import { EnvironmentSupervisor } from "../connection/supervisor.ts";
|
||||
import { environmentEndpointUrl } from "../environment/endpoint.ts";
|
||||
import { ManagedRelayDpopSigner } from "../relay/managedRelay.ts";
|
||||
import { safeErrorLogAttributes } from "../errors/safeLog.ts";
|
||||
import { executeEnvironmentHttpRequest, makeEnvironmentHttpApiClient } from "../rpc/http.ts";
|
||||
import { buildEnvironmentAuthHeaders, withEnvironmentCredentials } from "./environmentHttpAuth.ts";
|
||||
import { followStreamInEnvironment } from "./runtime.ts";
|
||||
|
||||
export function initialConfigOption<E>(
|
||||
@@ -25,8 +30,39 @@ export function initialConfigOption<E>(
|
||||
);
|
||||
}
|
||||
|
||||
// Bounded like the snapshot fetches: a wedged environment must not pin the
|
||||
// permissions check (and with it the settings UI) in a loading state for long.
|
||||
const DEFAULT_SESSION_STATE_TIMEOUT_MS = 6_000;
|
||||
|
||||
/**
|
||||
* Read the granted scopes of this client's session on one environment via its
|
||||
* `/api/auth/session` endpoint, authenticated with whatever credential the
|
||||
* connection was prepared with (cookie, bearer, or DPoP).
|
||||
*/
|
||||
export const fetchEnvironmentSessionState = Effect.fn(
|
||||
"clientRuntime.state.fetchEnvironmentSessionState",
|
||||
)(function* (input: {
|
||||
readonly prepared: PreparedConnection;
|
||||
readonly signer: Option.Option<ManagedRelayDpopSigner["Service"]>;
|
||||
readonly timeoutMs?: number;
|
||||
}) {
|
||||
const requestUrl = environmentEndpointUrl(input.prepared.httpBaseUrl, "/api/auth/session");
|
||||
const client = yield* makeEnvironmentHttpApiClient(input.prepared.httpBaseUrl);
|
||||
const headers = yield* buildEnvironmentAuthHeaders(
|
||||
input.prepared.httpAuthorization,
|
||||
"GET",
|
||||
requestUrl,
|
||||
input.signer,
|
||||
);
|
||||
return yield* executeEnvironmentHttpRequest(
|
||||
requestUrl,
|
||||
input.timeoutMs ?? DEFAULT_SESSION_STATE_TIMEOUT_MS,
|
||||
withEnvironmentCredentials(input.prepared.httpAuthorization, client.auth.session({ headers })),
|
||||
);
|
||||
});
|
||||
|
||||
export function createEnvironmentSessionAtoms<R, E>(
|
||||
runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
|
||||
runtime: Atom.AtomRuntime<EnvironmentRegistry | HttpClient.HttpClient | R, E>,
|
||||
) {
|
||||
const initialConfigAtom = Atom.family((environmentId: EnvironmentId) =>
|
||||
runtime.atom(
|
||||
@@ -86,10 +122,41 @@ export function createEnvironmentSessionAtoms<R, E>(
|
||||
).pipe(Atom.withLabel(`environment-prepared-connection:${environmentId}`)),
|
||||
);
|
||||
|
||||
// Keyed on the prepared connection's identity: a reconnect (new credential,
|
||||
// new base URL) swaps the prepared value, which re-runs the fetch, so scope
|
||||
// changes from re-pairing are picked up without an explicit refresh.
|
||||
const sessionStateAtom = Atom.family((environmentId: EnvironmentId) =>
|
||||
runtime
|
||||
.atom((get) => {
|
||||
const prepared = Option.getOrNull(get(preparedConnectionValueAtom(environmentId)));
|
||||
if (prepared === null) {
|
||||
return Effect.never;
|
||||
}
|
||||
return Effect.gen(function* () {
|
||||
const signer = yield* Effect.serviceOption(ManagedRelayDpopSigner);
|
||||
return yield* fetchEnvironmentSessionState({ prepared, signer });
|
||||
});
|
||||
})
|
||||
.pipe(
|
||||
Atom.swr({ staleTime: 30_000, revalidateOnMount: true }),
|
||||
Atom.setIdleTTL(5 * 60_000),
|
||||
Atom.withLabel(`environment-session-state:${environmentId}`),
|
||||
),
|
||||
);
|
||||
|
||||
const sessionStateValueAtom = Atom.family((environmentId: EnvironmentId) =>
|
||||
Atom.make(
|
||||
(get): AuthSessionState | null =>
|
||||
Option.getOrNull(AsyncResult.value(get(sessionStateAtom(environmentId)))) ?? null,
|
||||
).pipe(Atom.withLabel(`environment-session-state-value:${environmentId}`)),
|
||||
);
|
||||
|
||||
return {
|
||||
initialConfigAtom,
|
||||
initialConfigValueAtom,
|
||||
preparedConnectionAtom,
|
||||
preparedConnectionValueAtom,
|
||||
sessionStateAtom,
|
||||
sessionStateValueAtom,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user