Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import { type BackgroundActivityProfile } from "@akeru/contracts";
import { resolveServerBackgroundActivitySettings } from "@akeru/shared/backgroundActivitySettings";
import * as Duration from "effect/Duration";
import { usePrimarySettings, useUpdatePrimarySettings } from "../../hooks/useSettings";
import { Button } from "../ui/button";
import {
Dialog,
DialogDescription,
DialogFooter,
DialogHeader,
DialogPanel,
DialogPopup,
DialogTitle,
} from "../ui/dialog";
import {
NumberField,
NumberFieldDecrement,
NumberFieldGroup,
NumberFieldIncrement,
NumberFieldInput,
} from "../ui/number-field";
import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
import { Switch } from "../ui/switch";
import {
backgroundActivityOverrideSettings,
backgroundActivitySharedPolicySettings,
durationToSeconds,
normalizeIntervalSeconds,
PROVIDER_HEALTH_INTERVAL_STEP_SECONDS,
} from "./SettingsPanels.logic";
import { useI18n } from "../../i18n";
import type { MessageKey } from "@akeru/client-runtime/i18n";
import { resetBackgroundActivitySettings } from "./useSettingsRestore";
export const BACKGROUND_ACTIVITY_PROFILE_LABELS: Record<BackgroundActivityProfile, MessageKey> = {
balanced: "Balanced",
performance: "Performance",
"battery-saver": "Battery saver",
};
type BackgroundActivityProfileOption = BackgroundActivityProfile | "advanced";
export const BACKGROUND_ACTIVITY_PROFILE_OPTION_LABELS: Record<
BackgroundActivityProfileOption,
MessageKey
> = {
...BACKGROUND_ACTIVITY_PROFILE_LABELS,
advanced: "Advanced",
};
export const BACKGROUND_ACTIVITY_PROFILE_DESCRIPTIONS: Record<
BackgroundActivityProfile,
MessageKey
> = {
balanced:
"Pauses background probes when clients are idle, the host is locked, or low power mode is active.",
performance: "Allows scoped background probes while any subscribed client remains connected.",
"battery-saver": "Also pauses background probes when the host or client is on battery.",
};
const BACKGROUND_ACTIVITY_BOOLEAN_OVERRIDES: ReadonlyArray<{
readonly key:
| "pauseWhenHostLocked"
| "pauseWhenHostLowPower"
| "pauseWhenClientLowPower"
| "pauseWhenOnBattery";
readonly label: MessageKey;
}> = [
{ key: "pauseWhenHostLocked", label: "Pause when host is locked" },
{ key: "pauseWhenHostLowPower", label: "Pause on host low power" },
{ key: "pauseWhenClientLowPower", label: "Pause on client low power" },
{ key: "pauseWhenOnBattery", label: "Pause on battery" },
];
export function backgroundActivityProfileSettings(profile: BackgroundActivityProfile) {
return {
backgroundActivity: {
schemaVersion: 1 as const,
profile,
overrides: {},
},
};
}
export function BackgroundActivityAdvancedDialog({
open,
onOpenChange,
}: {
readonly open: boolean;
readonly onOpenChange: (open: boolean) => void;
}) {
const { t } = useI18n();
const settings = usePrimarySettings();
const updateSettings = useUpdatePrimarySettings();
const resolvedBackgroundActivity = resolveServerBackgroundActivitySettings(settings);
const activeProfile = resolvedBackgroundActivity.profile;
const providerHealthRefreshIntervalSeconds = durationToSeconds(
resolvedBackgroundActivity.providerHealthRefreshInterval,
);
const hostPowerMonitorActiveIntervalSeconds = durationToSeconds(
resolvedBackgroundActivity.hostPowerMonitorActiveInterval,
);
const hostPowerMonitorIdleIntervalSeconds = durationToSeconds(
resolvedBackgroundActivity.hostPowerMonitorIdleInterval,
);
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogPopup className="max-w-xl">
<DialogHeader>
<DialogTitle>{t("Background Activity")}</DialogTitle>
<DialogDescription>
{t("Tune the shared power policy and the background intervals that feed it.")}
</DialogDescription>
</DialogHeader>
<DialogPanel className="space-y-0 px-6 pb-5">
<div className="overflow-hidden rounded-xl border bg-card text-card-foreground">
<div className="flex flex-col gap-3 border-b px-4 py-3 sm:flex-row sm:items-center sm:justify-between">
<div className="min-w-0 space-y-1">
<div className="text-sm font-medium">{t("Shared policy")}</div>
<p className="text-xs leading-relaxed text-muted-foreground">
{t("Controls whether background work may run after a subscribed interval fires.")}
</p>
</div>
<Select
value={activeProfile}
onValueChange={(value) => {
if (
value === "balanced" ||
value === "performance" ||
value === "battery-saver"
) {
updateSettings({
backgroundActivity: backgroundActivitySharedPolicySettings(settings, value),
});
}
}}
>
<SelectTrigger
className="w-full sm:w-40"
aria-label={t("Shared background policy")}
>
<SelectValue>{t(BACKGROUND_ACTIVITY_PROFILE_LABELS[activeProfile])}</SelectValue>
</SelectTrigger>
<SelectPopup align="end" alignItemWithTrigger={false}>
<SelectItem hideIndicator value="balanced">
{t(BACKGROUND_ACTIVITY_PROFILE_LABELS.balanced)}
</SelectItem>
<SelectItem hideIndicator value="performance">
{t(BACKGROUND_ACTIVITY_PROFILE_LABELS.performance)}
</SelectItem>
<SelectItem hideIndicator value="battery-saver">
{t(BACKGROUND_ACTIVITY_PROFILE_LABELS["battery-saver"])}
</SelectItem>
</SelectPopup>
</Select>
</div>
<div className="flex flex-col gap-3 px-4 py-3 sm:flex-row sm:items-center sm:justify-between">
<div className="min-w-0 space-y-1">
<div className="text-sm font-medium">{t("Provider health interval")}</div>
<p className="text-xs leading-relaxed text-muted-foreground">
{t("Refresh provider availability, versions, auth state, and model metadata.")}
</p>
</div>
<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={t("Decrease provider health interval")} />
<NumberFieldInput aria-label={t("Provider health interval in seconds")} />
<NumberFieldIncrement aria-label={t("Increase provider health interval")} />
</NumberFieldGroup>
</NumberField>
<span className="text-xs text-muted-foreground">{t("seconds")}</span>
</div>
</div>
<div className="flex flex-col gap-3 border-t px-4 py-3 sm:flex-row sm:items-center sm:justify-between">
<div className="min-w-0 space-y-1">
<div className="text-sm font-medium">{t("Host power monitor")}</div>
<p className="text-xs leading-relaxed text-muted-foreground">
{t("Poll host power state while clients are active.")}
</p>
</div>
<div className="flex shrink-0 items-center gap-2">
<NumberField
value={hostPowerMonitorActiveIntervalSeconds}
min={5}
step={5}
size="sm"
className="w-32"
onValueChange={(value) =>
updateSettings(
backgroundActivityOverrideSettings(
settings.backgroundActivity,
resolvedBackgroundActivity,
{
hostPowerMonitorActiveInterval: Duration.seconds(
normalizeIntervalSeconds(value, 5),
),
},
),
)
}
>
<NumberFieldGroup>
<NumberFieldDecrement aria-label={t("Decrease active host power interval")} />
<NumberFieldInput aria-label={t("Active host power interval in seconds")} />
<NumberFieldIncrement aria-label={t("Increase active host power interval")} />
</NumberFieldGroup>
</NumberField>
<span className="text-xs text-muted-foreground">{t("seconds")}</span>
</div>
</div>
<div className="flex flex-col gap-3 border-t px-4 py-3 sm:flex-row sm:items-center sm:justify-between">
<div className="min-w-0 space-y-1">
<div className="text-sm font-medium">{t("Idle host monitor")}</div>
<p className="text-xs leading-relaxed text-muted-foreground">
{t("Poll host power state when no foreground client is active.")}
</p>
</div>
<div className="flex shrink-0 items-center gap-2">
<NumberField
value={hostPowerMonitorIdleIntervalSeconds}
min={5}
step={30}
size="sm"
className="w-32"
onValueChange={(value) =>
updateSettings(
backgroundActivityOverrideSettings(
settings.backgroundActivity,
resolvedBackgroundActivity,
{
hostPowerMonitorIdleInterval: Duration.seconds(
normalizeIntervalSeconds(value, 5),
),
},
),
)
}
>
<NumberFieldGroup>
<NumberFieldDecrement aria-label={t("Decrease idle host power interval")} />
<NumberFieldInput aria-label={t("Idle host power interval in seconds")} />
<NumberFieldIncrement aria-label={t("Increase idle host power interval")} />
</NumberFieldGroup>
</NumberField>
<span className="text-xs text-muted-foreground">{t("seconds")}</span>
</div>
</div>
<div className="grid gap-0 border-t sm:grid-cols-2">
{BACKGROUND_ACTIVITY_BOOLEAN_OVERRIDES.map(({ key, label }) => (
<label
key={key}
className="flex items-center justify-between gap-3 border-b px-4 py-3 last:border-b-0 sm:border-r sm:even:border-r-0"
>
<span className="text-sm font-medium">{t(label)}</span>
<Switch
checked={resolvedBackgroundActivity[key]}
onCheckedChange={(checked) =>
updateSettings(
backgroundActivityOverrideSettings(
settings.backgroundActivity,
resolvedBackgroundActivity,
{
[key]: Boolean(checked),
},
),
)
}
aria-label={t(label)}
/>
</label>
))}
</div>
</div>
</DialogPanel>
<DialogFooter>
<Button
variant="outline"
onClick={() => updateSettings(resetBackgroundActivitySettings())}
>
{t("Reset all")}
</Button>
<Button onClick={() => onOpenChange(false)}>{t("Done")}</Button>
</DialogFooter>
</DialogPopup>
</Dialog>
);
}