"use client"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import { IconInfoCircle } from "@tabler/icons-react"; import { Button } from "@kandev/ui/button"; import { Input } from "@kandev/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@kandev/ui/select"; import { Switch } from "@kandev/ui/switch"; import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip"; import { SettingsFieldDescription, SettingsFieldLabel, SettingsErrorText, } from "@/components/settings/settings-typography"; import type { DynamicErrorClass, DynamicErrorPolicy } from "@/lib/types/agent-profile"; const MAX_RETRIES = 10; const MAX_INITIAL_INTERVAL_SECONDS = 3600; const MAX_WAIT_SECONDS = 7 * 24 * 60 * 60; export function isDynamicErrorPolicyValid(policy: DynamicErrorPolicy): boolean { if (policy.retry.enabled) { if (policy.retry.maxRetries < 1 || policy.retry.maxRetries > MAX_RETRIES) return false; if ( policy.retry.initialIntervalSeconds < 1 || policy.retry.initialIntervalSeconds > MAX_INITIAL_INTERVAL_SECONDS ) { return false; } } else if (policy.retry.maxRetries !== 0 || policy.retry.initialIntervalSeconds !== 0) { return false; } if (policy.waitForReset.enabled) { return ( policy.waitForReset.maxWaitSeconds >= 1 && policy.waitForReset.maxWaitSeconds <= MAX_WAIT_SECONDS ); } return policy.waitForReset.maxWaitSeconds === 0; } type PolicyTranslator = (key: string) => string; function retryValidationMessage( policy: DynamicErrorPolicy, t: PolicyTranslator, ): string | undefined { if (!policy.retry.enabled) return undefined; if (policy.retry.maxRetries < 1 || policy.retry.maxRetries > MAX_RETRIES) { return t("agents:dynamicPolicyMaxRetriesValidation"); } if ( policy.retry.initialIntervalSeconds < 1 || policy.retry.initialIntervalSeconds > MAX_INITIAL_INTERVAL_SECONDS ) { return t("agents:dynamicPolicyInitialIntervalValidation"); } return undefined; } function waitValidationMessage( policy: DynamicErrorPolicy, t: PolicyTranslator, ): string | undefined { if ( policy.waitForReset.enabled && (policy.waitForReset.maxWaitSeconds < 1 || policy.waitForReset.maxWaitSeconds > MAX_WAIT_SECONDS) ) { return t("agents:dynamicPolicyMaxWaitValidation"); } return undefined; } function DynamicPolicyOptionHelp({ option }: { option: "retry" | "wait" | "outcome" }) { const { t } = useTranslation(); const [open, setOpen] = useState(false); const titleKey = `agents:dynamicPolicy${option[0].toUpperCase()}${option.slice(1)}`; const helpKey = `${titleKey}Help`; const optionLabel = t(titleKey); return ( setOpen((current) => !current)} data-testid={`dynamic-policy-option-help-${option}`} > {t(helpKey)} ); } // eslint-disable-next-line max-lines-per-function -- keeps the two-class policy form cohesive. export function DynamicPolicyEditor({ errorClass, policy, onChange, }: { errorClass: DynamicErrorClass; policy: DynamicErrorPolicy; onChange: (patch: Partial) => void; }) { const { t } = useTranslation(); const isTransient = errorClass === "transient"; const title = t(isTransient ? "agents:dynamicTransientErrors" : "agents:dynamicHardErrors"); const description = t( isTransient ? "agents:dynamicTransientErrorsDescription" : "agents:dynamicHardErrorsDescription", ); const retryLabel = t("agents:dynamicPolicyRetry"); const waitLabel = t("agents:dynamicPolicyWaitForReset"); const outcomeLabel = t("agents:dynamicPolicyAfterExhausted"); const retryError = retryValidationMessage(policy, t); const waitError = waitValidationMessage(policy, t); const retryMaxInvalid = retryError === t("agents:dynamicPolicyMaxRetriesValidation"); const retryIntervalInvalid = retryError === t("agents:dynamicPolicyInitialIntervalValidation"); const updateRetry = (patch: Partial) => onChange({ retry: { ...policy.retry, ...patch } }); const updateWait = (patch: Partial) => onChange({ waitForReset: { ...policy.waitForReset, ...patch } }); const schedule = policy.retry.enabled ? t("agents:dynamicPolicySchedule", { first: policy.retry.initialIntervalSeconds, retries: policy.retry.maxRetries, }) : t("agents:dynamicPolicyNoRetry"); return ( {title} {description} {retryLabel} {schedule} updateRetry( enabled ? { enabled, maxRetries: policy.retry.maxRetries || 1, initialIntervalSeconds: policy.retry.initialIntervalSeconds || 5, } : { enabled: false, maxRetries: 0, initialIntervalSeconds: 0 }, ) } aria-label={retryLabel} /> {policy.retry.enabled && ( {t("agents:dynamicPolicyMaxRetries")} updateRetry({ maxRetries: Number(event.target.value) || 0 })} className="min-h-11" aria-label={t("agents:dynamicPolicyMaxRetries")} aria-invalid={retryMaxInvalid} /> {retryMaxInvalid && {retryError}} {t("agents:dynamicPolicyInitialInterval")} updateRetry({ initialIntervalSeconds: Number(event.target.value) || 0 }) } className="min-h-11" aria-label={t("agents:dynamicPolicyInitialInterval")} aria-invalid={retryIntervalInvalid} /> {retryIntervalInvalid && {retryError}} )} {waitLabel} {t("agents:dynamicPolicyWaitDescription")} updateWait( enabled ? { enabled, maxWaitSeconds: policy.waitForReset.maxWaitSeconds || 300 } : { enabled: false, maxWaitSeconds: 0 }, ) } aria-label={waitLabel} /> {policy.waitForReset.enabled && ( {t("agents:dynamicPolicyMaxWait")} updateWait({ maxWaitSeconds: Number(event.target.value) || 0 })} className="min-h-11" aria-label={t("agents:dynamicPolicyMaxWait")} aria-invalid={Boolean(waitError)} /> {waitError && {waitError}} )} {outcomeLabel} onChange({ onExhausted: value as "skip" | "stop" })} > {t("agents:dynamicPolicySkip")} {t("agents:dynamicPolicyStop")} ); }
{description}