feat(models): declare request APIs per preset
Bind API support and preference to the preset's provider and model. Carry the API declaration through factory selection, fallback chains, hot reload and WebUI persistence. Use Copilot endpoint metadata for automatic routing and keep reasoning replay and compaction defaults in provider capability profiles. Consolidate Responses follow-ups from #5906, #5935, #6020, #6051 and #5834; acknowledge equivalent contributions in #6105 and #5863. Co-authored-by: Parman Mohammadalizadeh <prmma23@gmail.com> Co-authored-by: Lanre Shittu <136805224+Shizoqua@users.noreply.github.com> Co-authored-by: kkkhoo <128304090+kkkhoo@users.noreply.github.com> Co-authored-by: pei711 <199601925+pei711@users.noreply.github.com> Co-authored-by: Steven Huang <134844631+remote-controlled-man@users.noreply.github.com> Co-authored-by: Louis Yue <179701714+selfucker@users.noreply.github.com> Co-authored-by: iuiuiu <92971770+iuiu-py@users.noreply.github.com>
This commit is contained in:
1 parent
3c866d63ac
commit
b5808e47de
23 files changed
+738
-63
No files matched your search
@@ -0,0 +1,75 @@
|
||||
import { ChevronDown } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { SettingsRow } from "@/components/settings/shared/SettingsControls";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuTrigger,
|
||||
} from "@/components/ui/dropdown-menu";
|
||||
import type { ModelAPIConfig } from "@/lib/types";
|
||||
|
||||
export function modelAPISelection(api: ModelAPIConfig | null | undefined): string {
|
||||
if (!api) return "auto";
|
||||
if (!api.supported_apis.includes("responses")) return "chat_completions";
|
||||
if (!api.supported_apis.includes("chat_completions")) return "responses";
|
||||
return (api.preferred_api ?? api.supported_apis[0]) === "responses"
|
||||
? "prefer_responses"
|
||||
: "prefer_chat";
|
||||
}
|
||||
|
||||
export function ModelAPIControl({
|
||||
value,
|
||||
onChange,
|
||||
}: {
|
||||
value: ModelAPIConfig | null;
|
||||
onChange: (api: ModelAPIConfig | null) => void;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const options: Array<{ value: string; label: string; api: ModelAPIConfig | null }> = [
|
||||
{ value: "auto", label: t("settings.models.apiAuto"), api: null },
|
||||
{
|
||||
value: "chat_completions", label: "Chat Completions",
|
||||
api: { supported_apis: ["chat_completions"], preferred_api: "chat_completions" },
|
||||
},
|
||||
{
|
||||
value: "responses", label: "Responses",
|
||||
api: { supported_apis: ["responses"], preferred_api: "responses" },
|
||||
},
|
||||
{
|
||||
value: "prefer_responses", label: t("settings.models.apiPreferResponses"),
|
||||
api: { supported_apis: ["responses", "chat_completions"], preferred_api: "responses" },
|
||||
},
|
||||
{
|
||||
value: "prefer_chat", label: t("settings.models.apiPreferChat"),
|
||||
api: { supported_apis: ["chat_completions", "responses"], preferred_api: "chat_completions" },
|
||||
},
|
||||
];
|
||||
const selected = options.find((option) => option.value === modelAPISelection(value));
|
||||
return (
|
||||
<SettingsRow title={t("settings.models.requestAPI")} description={t("settings.models.requestAPIHelp")}>
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
aria-label={t("settings.models.requestAPI")}
|
||||
className="h-9 w-full justify-between gap-3 rounded-full px-3 text-[13px]"
|
||||
>
|
||||
<span className="truncate">{selected?.label}</span>
|
||||
<ChevronDown className="h-3.5 w-3.5 shrink-0 text-muted-foreground" aria-hidden />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end" className="min-w-[220px]">
|
||||
{options.map((option) => (
|
||||
<DropdownMenuItem key={option.value} onSelect={() => onChange(option.api)}>
|
||||
{option.label}
|
||||
</DropdownMenuItem>
|
||||
))}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</SettingsRow>
|
||||
);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { ProviderIcon } from "@/components/settings/models/ProviderSettings";
|
||||
import { ModelAPIControl } from "@/components/settings/models/ModelAPIControl";
|
||||
import { ToggleButton } from "@/components/settings/ToggleButton";
|
||||
import { useAutoSave } from "@/components/settings/shared/useAutoSave";
|
||||
import { useEffect, useId, useRef, useState, type Dispatch, type SetStateAction } from "react";
|
||||
@@ -42,7 +43,7 @@ import {
|
||||
} from "@/components/ui/dialog";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { cn } from "@/lib/utils";
|
||||
import type { SettingsPayload } from "@/lib/types";
|
||||
import type { ModelAPIConfig, SettingsPayload } from "@/lib/types";
|
||||
|
||||
export interface AgentSettingsDraft {
|
||||
model: string;
|
||||
@@ -52,6 +53,7 @@ export interface AgentSettingsDraft {
|
||||
contextWindowTokens: number;
|
||||
temperature: number;
|
||||
reasoningEffort: string;
|
||||
api: ModelAPIConfig | null;
|
||||
timezone: string;
|
||||
toolHintMaxLength: number;
|
||||
}
|
||||
@@ -92,6 +94,7 @@ export const DEFAULT_AGENT_SETTINGS_DRAFT: AgentSettingsDraft = {
|
||||
contextWindowTokens: 200_000,
|
||||
temperature: 0.1,
|
||||
reasoningEffort: "",
|
||||
api: null,
|
||||
timezone: "UTC",
|
||||
toolHintMaxLength: 40,
|
||||
};
|
||||
@@ -115,6 +118,7 @@ export function agentDraftFromPayload(
|
||||
),
|
||||
temperature: activePreset?.temperature ?? payload.agent.temperature,
|
||||
reasoningEffort: activePreset?.reasoning_effort ?? "",
|
||||
api: activePreset ? activePreset.api ?? null : payload.agent.api ?? null,
|
||||
timezone: payload.agent.timezone,
|
||||
toolHintMaxLength: payload.agent.tool_hint_max_length,
|
||||
};
|
||||
@@ -337,6 +341,7 @@ export function ModelsSettings({
|
||||
contextWindowTokens: normalizeContextWindowTokens(preset.context_window_tokens),
|
||||
temperature: preset.temperature,
|
||||
reasoningEffort: preset.reasoning_effort ?? "",
|
||||
api: preset.api ?? null,
|
||||
}));
|
||||
setEditorRowKey(rowKey);
|
||||
setEditorOpen(true);
|
||||
@@ -446,6 +451,7 @@ export function ModelsSettings({
|
||||
...prev,
|
||||
provider,
|
||||
model: provider === prev.provider ? prev.model : "",
|
||||
api: provider === prev.provider ? prev.api : null,
|
||||
modelPreset: clearSuggestedName ? "" : prev.modelPreset,
|
||||
}));
|
||||
}}
|
||||
@@ -495,11 +501,20 @@ export function ModelsSettings({
|
||||
setForm((prev) => ({
|
||||
...prev,
|
||||
model,
|
||||
api: model === prev.model ? prev.api : null,
|
||||
modelPreset: canSuggestName ? suggestion : prev.modelPreset,
|
||||
}));
|
||||
}}
|
||||
/>
|
||||
</SettingsRow>
|
||||
{(selectedProvider ?? settings.providers.find(
|
||||
(provider) => provider.name === selectedPreset?.resolved_provider,
|
||||
))?.model_api_configurable === true ? (
|
||||
<ModelAPIControl
|
||||
value={form.api}
|
||||
onChange={(api) => setForm((prev) => ({ ...prev, api }))}
|
||||
/>
|
||||
) : null}
|
||||
<button
|
||||
type="button"
|
||||
aria-expanded={advancedOpen}
|
||||
|
||||
@@ -7,6 +7,7 @@ import type {
|
||||
PendingRestartSections,
|
||||
} from "@/components/settings/contracts";
|
||||
import { agentDraftFromPayload } from "@/components/settings/models/ModelsSettings";
|
||||
import { modelAPISelection } from "@/components/settings/models/ModelAPIControl";
|
||||
import {
|
||||
CUSTOM_PROVIDER_CREATION_KEY,
|
||||
providerFormFromRow,
|
||||
@@ -195,6 +196,8 @@ export function useModelSettingsActions({
|
||||
contextWindowTokens: form.contextWindowTokens,
|
||||
temperature: form.temperature,
|
||||
reasoningEffort: form.reasoningEffort || null,
|
||||
api: settings.providers.find((row) => row.name === provider)?.model_api_configurable === true
|
||||
? form.api : undefined,
|
||||
});
|
||||
const createdPreset = payload.created_model_preset;
|
||||
const nextOrder = createdPreset ? [...modelCallOrder, createdPreset] : null;
|
||||
@@ -257,6 +260,11 @@ export function useModelSettingsActions({
|
||||
form.temperature !== selectedPreset.temperature ? form.temperature : undefined,
|
||||
reasoningEffort:
|
||||
reasoningEffort !== selectedPreset.reasoning_effort ? reasoningEffort : undefined,
|
||||
api: settings.providers.find((row) => row.name === (
|
||||
form.provider === "auto" ? selectedPreset.resolved_provider : form.provider
|
||||
))?.model_api_configurable === true &&
|
||||
modelAPISelection(form.api) !== modelAPISelection(selectedPreset.api)
|
||||
? form.api : undefined,
|
||||
});
|
||||
applyPayload(payload);
|
||||
setForm(agentDraftFromPayload(payload, nextName));
|
||||
@@ -290,6 +298,7 @@ export function useModelSettingsActions({
|
||||
modelPreset: "",
|
||||
provider,
|
||||
model: "",
|
||||
api: null,
|
||||
maxTokens: primaryPreset?.max_tokens ?? settings.agent.max_tokens,
|
||||
contextWindowTokens: normalizeContextWindowTokens(
|
||||
primaryPreset?.context_window_tokens ?? settings.agent.context_window_tokens,
|
||||
|
||||
@@ -20,6 +20,7 @@ import type {
|
||||
SettingsSectionKey,
|
||||
} from "@/components/settings/contracts";
|
||||
import { agentDraftFromPayload } from "@/components/settings/models/ModelsSettings";
|
||||
import { modelAPISelection } from "@/components/settings/models/ModelAPIControl";
|
||||
import { useModelSettingsActions } from "@/components/settings/models/useModelSettingsActions";
|
||||
import {
|
||||
useProviderFormsSync,
|
||||
@@ -298,7 +299,8 @@ export function useSettingsController({
|
||||
form.maxTokens !== selectedPreset.max_tokens ||
|
||||
form.contextWindowTokens !== normalizeContextWindowTokens(selectedPreset.context_window_tokens) ||
|
||||
form.temperature !== selectedPreset.temperature ||
|
||||
form.reasoningEffort !== (selectedPreset.reasoning_effort ?? "")
|
||||
form.reasoningEffort !== (selectedPreset.reasoning_effort ?? "") ||
|
||||
modelAPISelection(form.api) !== modelAPISelection(selectedPreset.api)
|
||||
);
|
||||
}, [form, modelPresetEditingName, settings]);
|
||||
|
||||
|
||||
@@ -717,6 +717,11 @@
|
||||
"presetNameDuplicate": "A preset with this name already exists.",
|
||||
"presetNamePlaceholder": "e.g. Fast writing",
|
||||
"advancedOptions": "Advanced options",
|
||||
"requestAPI": "Request API",
|
||||
"requestAPIHelp": "Applies to this preset. Select Responses for gateways that require it; allow Chat fallback only when the model supports both APIs.",
|
||||
"apiAuto": "Automatic",
|
||||
"apiPreferResponses": "Responses with Chat fallback",
|
||||
"apiPreferChat": "Chat preferred (both APIs)",
|
||||
"contextSummary": "Context {{context}}",
|
||||
"outputSummary": "Max {{max}} tokens",
|
||||
"contextWindowHint": "{{tokens}} tokens",
|
||||
|
||||
@@ -717,6 +717,11 @@
|
||||
"presetNameDuplicate": "已存在同名预设。",
|
||||
"presetNamePlaceholder": "例如:快速写作",
|
||||
"advancedOptions": "高级选项",
|
||||
"requestAPI": "请求 API",
|
||||
"requestAPIHelp": "仅用于此预设。网关要求 Responses 时可在这里选择;只有模型支持两种 API 时才允许回退到 Chat。",
|
||||
"apiAuto": "自动",
|
||||
"apiPreferResponses": "Responses(可回退 Chat)",
|
||||
"apiPreferChat": "优先 Chat(支持两种 API)",
|
||||
"contextSummary": "上下文 {{context}}",
|
||||
"outputSummary": "输出上限 {{max}} Token",
|
||||
"contextWindowHint": "{{tokens}} Token",
|
||||
|
||||
@@ -1152,7 +1152,7 @@ export async function fetchSidebarState(
|
||||
function modelGenerationSettingsPayload(
|
||||
configuration: Pick<
|
||||
ModelConfigurationCreate,
|
||||
"maxTokens" | "contextWindowTokens" | "temperature" | "reasoningEffort"
|
||||
"maxTokens" | "contextWindowTokens" | "temperature" | "reasoningEffort" | "api"
|
||||
>,
|
||||
): Record<string, unknown> {
|
||||
const payload: Record<string, unknown> = {};
|
||||
@@ -1168,6 +1168,9 @@ function modelGenerationSettingsPayload(
|
||||
if (configuration.reasoningEffort !== undefined) {
|
||||
payload.reasoning_effort = configuration.reasoningEffort ?? "";
|
||||
}
|
||||
if (configuration.api !== undefined) {
|
||||
payload.api = configuration.api;
|
||||
}
|
||||
return payload;
|
||||
}
|
||||
|
||||
|
||||
@@ -595,6 +595,11 @@ export interface RuntimeCapabilities {
|
||||
can_export_diagnostics: boolean;
|
||||
}
|
||||
|
||||
export interface ModelAPIConfig {
|
||||
supported_apis: Array<"chat_completions" | "responses">;
|
||||
preferred_api?: "chat_completions" | "responses" | null;
|
||||
}
|
||||
|
||||
interface ProviderModelInfo {
|
||||
id: string;
|
||||
label?: string | null;
|
||||
@@ -603,6 +608,7 @@ interface ProviderModelInfo {
|
||||
context_window?: number | null;
|
||||
reasoning_efforts?: string[];
|
||||
supports_backend_search?: boolean;
|
||||
api?: ModelAPIConfig | null;
|
||||
}
|
||||
|
||||
export interface ProviderModelsPayload {
|
||||
@@ -672,6 +678,7 @@ export interface SettingsPayload {
|
||||
context_window_tokens: number;
|
||||
temperature: number;
|
||||
reasoning_effort: string | null;
|
||||
api?: ModelAPIConfig | null;
|
||||
timezone: string;
|
||||
tool_hint_max_length: number;
|
||||
};
|
||||
@@ -689,6 +696,7 @@ export interface SettingsPayload {
|
||||
temperature: number;
|
||||
reasoning_effort: string | null;
|
||||
reasoning_effort_values?: string[];
|
||||
api?: ModelAPIConfig | null;
|
||||
}>;
|
||||
model_call_order: string[];
|
||||
model_call_order_editable: boolean;
|
||||
@@ -708,6 +716,7 @@ export interface SettingsPayload {
|
||||
default_api_base?: string | null;
|
||||
model_selectable?: boolean;
|
||||
model_catalog?: ProviderModelsPayload["catalog_kind"];
|
||||
model_api_configurable?: boolean;
|
||||
api_type?: "auto" | "chat_completions" | "responses";
|
||||
oauth_account?: string | null;
|
||||
oauth_expires_at?: number | null;
|
||||
@@ -1307,6 +1316,7 @@ export interface ModelConfigurationCreate {
|
||||
contextWindowTokens?: number;
|
||||
temperature?: number;
|
||||
reasoningEffort?: string | null;
|
||||
api?: ModelAPIConfig | null;
|
||||
}
|
||||
|
||||
export interface ModelConfigurationUpdate {
|
||||
@@ -1318,6 +1328,7 @@ export interface ModelConfigurationUpdate {
|
||||
contextWindowTokens?: number;
|
||||
temperature?: number;
|
||||
reasoningEffort?: string | null;
|
||||
api?: ModelAPIConfig | null;
|
||||
}
|
||||
|
||||
export interface ProviderSettingsUpdate {
|
||||
|
||||
@@ -89,6 +89,50 @@ async function togglePresetEditor(name = "primary") {
|
||||
describe("Settings models", () => {
|
||||
installSettingsViewTestHooks();
|
||||
|
||||
it("saves and restores a preset API declaration, then returns to automatic", async () => {
|
||||
let payload = settingsPayload();
|
||||
payload.providers = [{ name: "openai", label: "OpenAI", configured: true, model_api_configurable: true }];
|
||||
payload.model_presets[0].provider = "openai";
|
||||
vi.stubGlobal("fetch", vi.fn(async () => jsonResponse(payload)));
|
||||
requestMutationMock.mockImplementation(async (action, args) => {
|
||||
expect(action).toBe("settings.model_configuration.update");
|
||||
payload = {
|
||||
...payload,
|
||||
model_presets: payload.model_presets.map((preset) => ({ ...preset, api: args.api })),
|
||||
};
|
||||
return payload;
|
||||
});
|
||||
renderSettingsView({ initialSection: "models" });
|
||||
await togglePresetEditor();
|
||||
await openPopover(screen.getByLabelText("Request API"));
|
||||
fireEvent.click(screen.getByRole("menuitem", { name: "Responses" }));
|
||||
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
||||
await waitFor(() => expect(requestMutationMock).toHaveBeenLastCalledWith(
|
||||
"settings.model_configuration.update",
|
||||
{ name: "primary", api: { supported_apis: ["responses"], preferred_api: "responses" } },
|
||||
20_000,
|
||||
));
|
||||
await waitFor(() => expect(screen.getByRole("button", { name: "Save" })).toBeDisabled());
|
||||
fireEvent.click(screen.getByRole("button", { name: "Close", exact: true }));
|
||||
await togglePresetEditor();
|
||||
expect(screen.getByLabelText("Request API")).toHaveTextContent("Responses");
|
||||
await openPopover(screen.getByLabelText("Request API"));
|
||||
fireEvent.click(screen.getByRole("menuitem", { name: "Automatic" }));
|
||||
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
||||
await waitFor(() => expect(requestMutationMock).toHaveBeenLastCalledWith(
|
||||
"settings.model_configuration.update", { name: "primary", api: null }, 20_000,
|
||||
));
|
||||
});
|
||||
|
||||
it("hides API controls when the host omits preset API support", async () => {
|
||||
const payload = settingsPayload();
|
||||
payload.providers = [{ name: "openai", label: "OpenAI", configured: true }];
|
||||
vi.stubGlobal("fetch", vi.fn(async () => jsonResponse(payload)));
|
||||
renderSettingsView({ initialSection: "models" });
|
||||
await togglePresetEditor();
|
||||
expect(screen.queryByRole("button", { name: "Request API" })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it.each(["manual", "poll", "direct"])("reauthenticates from the catalog and preserves the preset draft (%s)", async (mode) => {
|
||||
const payload = settingsPayload();
|
||||
payload.providers = [{
|
||||
|
||||
Reference in new issue
Block a user