Files
kandev/apps/web/hooks/domains/settings/use-dynamic-models.ts
T
João Salavisa 270e4b39a9 fix(i18n): localize the two Agents hooks the lint count could not see (#2197)
Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-03 14:32:34 +02:00

111 lines
3.6 KiB
TypeScript

import { useState, useEffect, useCallback } from "react";
import { fetchDynamicModels } from "@/lib/api/domains/settings-api";
import type {
CommandEntry,
ModeEntry,
ModelEntry,
CapabilityStatus,
DynamicModelsResponse,
ModelConfig,
} from "@/lib/types/http";
// Set from an async catch rather than rendered as a literal, so this uses the
// module-level `t`, which resolves at call time. The message surfaces in the
// profile editor through `agents:failedToRefresh`.
import { t } from "@/lib/i18n";
type UseAgentCapabilitiesState = {
models: ModelEntry[];
modes: ModeEntry[];
commands: CommandEntry[];
currentModelId: string | undefined;
currentModeId: string | undefined;
status: CapabilityStatus | undefined;
isLoading: boolean;
error: string | null;
refresh: () => Promise<void>;
};
/**
* useAgentCapabilities fetches the full ACP probe cache for an agent
* (models, modes, current defaults) and keeps it in sync. Refresh triggers
* a live re-probe against the host utility and updates both models and
* modes atomically — so the profile page's refresh button covers the whole
* agent surface, not just models.
*/
export function useAgentCapabilities(
agentName: string | undefined,
initial: ModelConfig,
): UseAgentCapabilitiesState {
const supportsDynamicModels = initial.supports_dynamic_models;
const [models, setModels] = useState<ModelEntry[]>(initial.available_models);
const [modes, setModes] = useState<ModeEntry[]>(initial.available_modes ?? []);
const [commands, setCommands] = useState<CommandEntry[]>(initial.available_commands ?? []);
const [currentModelId, setCurrentModelId] = useState<string | undefined>(
initial.current_model_id,
);
const [currentModeId, setCurrentModeId] = useState<string | undefined>(initial.current_mode_id);
const [status, setStatus] = useState<CapabilityStatus | undefined>(initial.status);
const [manualRefreshAgentName, setManualRefreshAgentName] = useState<string>();
const hasManualRefresh = manualRefreshAgentName === agentName;
const [isLoading, setIsLoading] = useState(supportsDynamicModels && !!agentName);
const [error, setError] = useState<string | null>(null);
const fetchCaps = useCallback(
async (forceRefresh: boolean) => {
if (!agentName || !supportsDynamicModels) {
return;
}
setIsLoading(true);
setError(null);
try {
const response: DynamicModelsResponse = await fetchDynamicModels(agentName, {
refresh: forceRefresh,
});
setStatus(response.status);
setError(response.error ?? null);
if (forceRefresh) {
setManualRefreshAgentName(agentName);
}
if (response.status !== "failed") {
setModels(response.models ?? []);
setModes(response.modes ?? []);
setCommands(response.commands ?? []);
setCurrentModelId(response.current_model_id);
setCurrentModeId(response.current_mode_id);
}
} catch (err) {
setError(err instanceof Error ? err.message : t("agents:failedToFetchCapabilities"));
} finally {
setIsLoading(false);
}
},
[agentName, supportsDynamicModels],
);
useEffect(() => {
if (!hasManualRefresh) {
setStatus(initial.status);
}
}, [hasManualRefresh, initial.status]);
useEffect(() => {
if (supportsDynamicModels && agentName) {
void fetchCaps(false);
}
}, [agentName, supportsDynamicModels, fetchCaps]);
const refresh = useCallback(() => fetchCaps(true), [fetchCaps]);
return {
models,
modes,
commands,
currentModelId,
currentModeId,
status,
isLoading,
error,
refresh,
};
}