This commit is contained in:
Anish Sarkar 2026-07-05 10:03:36 +05:30 committed by GitHub
commit 7c57c435d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 43 additions and 238 deletions

View file

@ -21,7 +21,6 @@ from app.schemas import (
ConnectionCreate,
ConnectionRead,
ConnectionUpdate,
ModelCreate,
ModelPreviewRead,
ModelProviderRead,
ModelRead,
@ -618,49 +617,6 @@ async def discover_connection_models(
return [_model_read(model) for model in conn.models]
@router.post("/model-connections/{connection_id}/models", response_model=ModelRead)
async def add_manual_model(
connection_id: int,
data: ModelCreate,
session: AsyncSession = Depends(get_async_session),
auth: AuthContext = Depends(get_auth_context),
):
conn = await _load_connection(session, connection_id)
await _assert_connection_access(
session, auth, conn, Permission.LLM_CONFIGS_UPDATE.value
)
model_id = data.model_id.strip()
if not model_id:
raise HTTPException(status_code=400, detail="model_id is required")
if any(existing.model_id == model_id for existing in conn.models):
raise HTTPException(
status_code=400, detail="Model already exists on this connection"
)
capabilities = derive_capabilities(conn, model_id)
model = Model(
connection_id=conn.id,
model_id=model_id,
display_name=data.display_name or None,
source=ModelSource.MANUAL,
capabilities_override={},
enabled=True,
catalog={},
)
_apply_model_facts(model, capabilities)
session.add(model)
await session.commit()
await session.refresh(model)
conn = await _load_connection(session, connection_id)
await _default_unset_roles(session, conn, list(conn.models))
if conn.search_space_id is not None:
await _clear_invalid_roles(session, conn.search_space_id)
await session.commit()
await session.refresh(model)
return _model_read(model)
@router.patch(
"/model-connections/{connection_id}/models", response_model=list[ModelRead]
)

View file

@ -43,7 +43,6 @@ from .model_connections import (
ConnectionCreate,
ConnectionRead,
ConnectionUpdate,
ModelCreate,
ModelPreviewRead,
ModelProviderRead,
ModelRead,
@ -203,7 +202,6 @@ __all__ = [
"MembershipRead",
"MembershipReadWithUser",
"MembershipUpdate",
"ModelCreate",
"ModelPreviewRead",
"ModelProviderRead",
"ModelRead",

View file

@ -93,17 +93,6 @@ class ConnectionUpdate(BaseModel):
enabled: bool | None = None
class ModelCreate(BaseModel):
"""Manually register a model id on a connection.
For providers without a usable ``/models`` endpoint (Perplexity, MiniMax,
Azure deployments, etc.) or to pin a single model from a noisy provider.
"""
model_id: str = Field(..., max_length=255)
display_name: str | None = Field(None, max_length=255)
class ModelUpdate(BaseModel):
display_name: str | None = Field(None, max_length=255)
enabled: bool | None = None

View file

@ -202,11 +202,6 @@ def _discovery_error_message(conn: Connection, exc: httpx.HTTPError) -> str:
return _docker_hint(base_url, exc)
def _allowlist(conn: Connection) -> set[str]:
raw = (conn.extra or {}).get("model_ids") or []
return {str(item).strip() for item in raw if str(item).strip()}
def _litellm_info(model_string: str, model_id: str) -> dict[str, Any]:
with contextlib.suppress(Exception):
info = litellm.get_model_info(model=model_string)
@ -438,7 +433,6 @@ async def _discover_bedrock_models(conn: Connection) -> list[dict[str, Any]]:
async def discover_models(conn: Connection) -> list[dict[str, Any]]:
allowlist = _allowlist(conn)
spec = spec_for(conn.provider)
try:
@ -459,8 +453,6 @@ async def discover_models(conn: Connection) -> list[dict[str, Any]]:
except httpx.HTTPError as exc:
raise ModelDiscoveryError(_discovery_error_message(conn, exc)) from exc
if allowlist:
results = [item for item in results if item["model_id"] in allowlist]
return results

View file

@ -59,6 +59,10 @@ def to_litellm(
kwargs: dict[str, Any] = {}
if api_key:
kwargs["api_key"] = api_key
elif provider == "lm_studio":
# LiteLLM's OpenAI-compatible adapter expects an api_key value even
# when LM Studio accepts unauthenticated local requests.
kwargs["api_key"] = "not-needed"
prefix = spec.litellm_prefix or str(provider)
model_string = f"{prefix}/{model_id}" if prefix else model_id

View file

@ -64,6 +64,22 @@ def test_openai_compatible_resolver_uses_explicit_api_base() -> None:
assert ensure_v1("http://example.com/v1") == "http://example.com/v1"
def test_lm_studio_resolver_supplies_dummy_api_key_when_empty() -> None:
model, kwargs = to_litellm(
{
"provider": "lm_studio",
"base_url": "http://host.docker.internal:1234/v1",
"api_key": None,
"extra": {},
},
"tinyllama-1.1b-chat-v0.6",
)
assert model == "openai/tinyllama-1.1b-chat-v0.6"
assert kwargs["api_base"] == "http://host.docker.internal:1234/v1"
assert kwargs["api_key"] == "not-needed"
def test_ollama_resolver_uses_native_api_base() -> None:
model, kwargs = to_litellm(
{

View file

@ -4,7 +4,6 @@ import type {
ConnectionCreateRequest,
ConnectionRead,
ConnectionUpdateRequest,
ModelCreateRequest,
ModelPreviewRead,
ModelRead,
ModelRoles,
@ -95,13 +94,7 @@ export const verifyModelConnectionMutationAtom = atomWithMutation((get) => {
if (result.ok) {
toast.success("Connection verified");
} else {
// Non-fatal: many providers lack a /models endpoint yet still serve
// chat. Guide the user to add model IDs manually instead of alarming.
toast.warning(
result.message
? `${result.message} Chat may still work — add model IDs manually.`
: "Couldn't list models. Chat may still work — add model IDs manually."
);
toast.warning(result.message || "Couldn't verify this connection.");
}
invalidateModelConnections(searchSpaceId);
},
@ -148,20 +141,6 @@ export const testPreviewModelMutationAtom = atomWithMutation(() => {
};
});
export const addManualModelMutationAtom = atomWithMutation((get) => {
const searchSpaceId = Number(get(activeSearchSpaceIdAtom));
return {
mutationKey: ["models", "add-manual"],
mutationFn: ({ connectionId, data }: { connectionId: number; data: ModelCreateRequest }) =>
modelConnectionsApiService.addManualModel(connectionId, data),
onSuccess: () => {
toast.success("Model added");
invalidateModelConnections(searchSpaceId);
},
onError: (error: Error) => toast.error(error.message || "Failed to add model"),
};
});
export const updateModelMutationAtom = atomWithMutation((get) => {
const searchSpaceId = Number(get(activeSearchSpaceIdAtom));
return {

View file

@ -113,7 +113,7 @@ export function BedrockConnectForm({ onDraftChange }: ProviderConnectFormProps)
</p>
) : null}
<p className="text-xs text-muted-foreground">
Add Bedrock model IDs from the provider&apos;s settings after connecting.
After entering credentials, refresh models to discover the Bedrock catalog for this region.
</p>
</div>
);

View file

@ -2,7 +2,6 @@ import { useAtomValue } from "jotai";
import { Eye, EyeOff, Settings } from "lucide-react";
import { useMemo, useState } from "react";
import {
addManualModelMutationAtom,
bulkUpdateModelsMutationAtom,
discoverConnectionModelsMutationAtom,
testPreviewModelMutationAtom,
@ -50,26 +49,17 @@ export function ConnectionSettingsDialog({
const discoverModels = useAtomValue(discoverConnectionModelsMutationAtom);
const testPreviewModel = useAtomValue(testPreviewModelMutationAtom);
const updateConnection = useAtomValue(updateModelConnectionMutationAtom);
const addManualModel = useAtomValue(addManualModelMutationAtom);
const bulkUpdateModels = useAtomValue(bulkUpdateModelsMutationAtom);
const allowlist = Array.isArray(connection.extra?.model_ids)
? (connection.extra.model_ids as string[])
: [];
const [isOpen, setIsOpen] = useState(false);
const [baseUrlDraft, setBaseUrlDraft] = useState(connection.base_url ?? "");
const [apiKeyDraft, setApiKeyDraft] = useState("");
const [showApiKey, setShowApiKey] = useState(false);
const [allowlistText, setAllowlistText] = useState(allowlist.join(", "));
const [isSavingConnectionSettings, setIsSavingConnectionSettings] = useState(false);
const [draftEnabledModelIds, setDraftEnabledModelIds] = useState(() =>
enabledModelIds(connection.models)
);
const isLocal =
connection.provider === "ollama_chat" ||
connection.provider === "lm_studio" ||
!connection.base_url?.startsWith("https");
const hasConnectionChanges =
baseUrlDraft.trim() !== (connection.base_url ?? "") ||
apiKeyDraft.trim() !== (connection.api_key ?? "");
@ -93,7 +83,6 @@ export function ConnectionSettingsDialog({
setBaseUrlDraft(connection.base_url ?? "");
setApiKeyDraft(connection.api_key ?? "");
setShowApiKey(false);
setAllowlistText(allowlist.join(", "));
setIsSavingConnectionSettings(false);
setDraftEnabledModelIds(enabledModelIds(connection.models));
}
@ -168,17 +157,6 @@ export function ConnectionSettingsDialog({
}
}
function saveAllowlist() {
const ids = allowlistText
.split(",")
.map((value) => value.trim())
.filter(Boolean);
updateConnection.mutate({
id: connection.id,
data: { extra: { ...(connection.extra ?? {}), model_ids: ids } },
});
}
function handleToggleModel(model: SelectableModel, enabled: boolean) {
if (typeof model.id !== "number") return;
const modelId = model.id;
@ -276,41 +254,15 @@ export function ConnectionSettingsDialog({
</div>
</div>
{!isLocal ? (
<div className="space-y-2">
<Label className="text-xs">Model IDs filter (optional)</Label>
<div className="flex gap-2">
<Input
value={allowlistText}
onChange={(event) => setAllowlistText(event.target.value)}
placeholder="Comma-separated, e.g. anthropic/claude-sonnet-4-5, google/gemini-2.5-pro"
/>
<Button size="sm" onClick={saveAllowlist} disabled={updateConnection.isPending}>
Save filter
</Button>
</div>
<p className="text-xs text-muted-foreground">
Leave empty to discover all models. Recommended for providers with large catalogs.
</p>
</div>
) : null}
<Separator className="bg-muted-foreground/20" />
<ModelsSelectionPanel
models={draftModels}
isRefreshing={discoverModels.isPending}
isAddingManual={addManualModel.isPending}
isUpdatingModel={isSavingConnectionSettings}
isBulkUpdating={isSavingConnectionSettings || bulkUpdateModels.isPending}
refreshLabel={`Refresh ${providerLabel} models`}
onRefresh={() => discoverModels.mutate(connection.id)}
onAddManual={(modelId) =>
addManualModel.mutate({
connectionId: connection.id,
data: { model_id: modelId },
})
}
onToggleModel={handleToggleModel}
onBulkToggle={handleBulkToggle}
/>

View file

@ -201,22 +201,6 @@ export function ModelProviderConnectionsPanel({
);
}
function addConnectModel(modelId: string) {
setConnectModels((current) => {
if (current.some((model) => model.model_id === modelId)) return current;
return [
...current,
{
model_id: modelId,
display_name: modelId,
source: "MANUAL",
enabled: true,
metadata: {},
},
];
});
}
function toggleConnectModel(model: SelectableModel, enabled: boolean) {
setConnectModels((current) =>
current.map((item) => (item.model_id === model.model_id ? { ...item, enabled } : item))
@ -277,7 +261,6 @@ export function ModelProviderConnectionsPanel({
previewModels={connectModels}
isPreviewingModels={previewModels.isPending}
onPreviewModels={refreshConnectModels}
onAddPreviewModel={addConnectModel}
onTogglePreviewModel={toggleConnectModel}
onBulkTogglePreviewModels={bulkToggleConnectModels}
/>

View file

@ -1,10 +1,7 @@
import { RefreshCw } from "lucide-react";
import { useState } from "react";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import { Input } from "@/components/ui/input";
import { Spinner } from "@/components/ui/spinner";
import {
capability,
capabilityLabels,
@ -18,14 +15,12 @@ interface ModelsSelectionPanelProps {
models: SelectableModel[];
description?: string;
emptyMessage?: string;
manualInputPlaceholder?: string;
refreshLabel?: string;
isRefreshing?: boolean;
isAddingManual?: boolean;
isRefreshDisabled?: boolean;
isUpdatingModel?: boolean;
isBulkUpdating?: boolean;
onRefresh?: () => void;
onAddManual?: (modelId: string) => void;
onToggleModel?: (model: SelectableModel, enabled: boolean) => void;
onBulkToggle?: (models: SelectableModel[], enabled: boolean) => void;
}
@ -34,18 +29,15 @@ export function ModelsSelectionPanel({
models,
description = "Select models to make available for this provider.",
emptyMessage = "No models available.",
manualInputPlaceholder = "Add a model ID manually",
refreshLabel = "Refresh models",
isRefreshing = false,
isAddingManual = false,
isRefreshDisabled = false,
isUpdatingModel = false,
isBulkUpdating = false,
onRefresh,
onAddManual,
onToggleModel,
onBulkToggle,
}: ModelsSelectionPanelProps) {
const [manualModelId, setManualModelId] = useState("");
const [modelFilter, setModelFilter] = useState<ModelCapabilityFilter | null>(null);
const filteredModels = modelFilter
@ -54,13 +46,6 @@ export function ModelsSelectionPanel({
const allFilteredModelsEnabled =
filteredModels.length > 0 && filteredModels.every((model) => model.enabled);
function addModel() {
const modelId = manualModelId.trim();
if (!modelId || !onAddManual) return;
onAddManual(modelId);
setManualModelId("");
}
function toggleFilteredModels() {
const nextEnabled = !allFilteredModelsEnabled;
const changedModels = filteredModels.filter((model) => model.enabled !== nextEnabled);
@ -91,7 +76,7 @@ export function ModelsSelectionPanel({
size="icon"
type="button"
onClick={onRefresh}
disabled={isRefreshing}
disabled={isRefreshing || isRefreshDisabled}
aria-label={refreshLabel}
>
<RefreshCw className={`h-4 w-4 ${isRefreshing ? "animate-spin" : ""}`} />
@ -100,32 +85,6 @@ export function ModelsSelectionPanel({
</div>
</div>
{onAddManual ? (
<div className="flex gap-2">
<Input
value={manualModelId}
onChange={(event) => setManualModelId(event.target.value)}
onKeyDown={(event) => {
if (event.key === "Enter") {
event.preventDefault();
addModel();
}
}}
placeholder={manualInputPlaceholder}
/>
<Button
size="sm"
type="button"
onClick={addModel}
disabled={isAddingManual || !manualModelId.trim()}
className="relative min-w-[88px]"
>
<span className={isAddingManual ? "opacity-0" : ""}>Add model</span>
{isAddingManual ? <Spinner size="xs" className="absolute" /> : null}
</Button>
</div>
) : null}
{models.length > 0 ? (
<div className="flex flex-wrap items-center gap-2">
<span className="text-xs font-medium text-muted-foreground">Filter models</span>
@ -179,11 +138,6 @@ export function ModelsSelectionPanel({
<div className="min-w-0 flex-1">
<div className="flex items-center gap-2 text-sm font-medium">
<span className="truncate">{modelLabel(model)}</span>
{model.source === "MANUAL" ? (
<Badge variant="outline" className="text-[10px]">
manual
</Badge>
) : null}
</div>
<div className="text-xs text-muted-foreground">
{capabilityLabels(model) || "No discovered capabilities"}

View file

@ -33,7 +33,6 @@ interface ProviderConnectDialogProps {
previewModels?: SelectableModel[];
isPreviewingModels?: boolean;
onPreviewModels?: (draft: ConnectionDraft) => void;
onAddPreviewModel?: (modelId: string) => void;
onTogglePreviewModel?: (model: SelectableModel, enabled: boolean) => void;
onBulkTogglePreviewModels?: (models: SelectableModel[], enabled: boolean) => void;
}
@ -53,7 +52,6 @@ export function ProviderConnectDialog({
previewModels = [],
isPreviewingModels = false,
onPreviewModels,
onAddPreviewModel,
onTogglePreviewModel,
onBulkTogglePreviewModels,
}: ProviderConnectDialogProps) {
@ -94,7 +92,8 @@ export function ProviderConnectDialog({
return "Select the models to enable for this provider";
})();
const canRefreshModels = !isAzure && !isVertex && (!isBedrock || canSubmit);
const supportsModelRefresh = !isAzure && !isVertex;
const canRefreshModels = supportsModelRefresh && canSubmit;
const hasEnabledModel =
previewModels.some((model) => model.enabled) || Boolean(currentDraft.seedModelId);
const canConnect = canSubmit && hasEnabledModel;
@ -130,18 +129,22 @@ export function ProviderConnectDialog({
<DefaultConnectForm {...formProps} />
)}
<Separator className="bg-muted-foreground/20" />
{!isAzure ? (
<>
<Separator className="bg-muted-foreground/20" />
<ModelsSelectionPanel
models={previewModels}
description={modelDescription}
isRefreshing={isPreviewingModels}
refreshLabel={`Refresh ${meta.name} models`}
onRefresh={canRefreshModels ? () => onPreviewModels?.(currentDraft) : undefined}
onAddManual={onAddPreviewModel}
onToggleModel={onTogglePreviewModel}
onBulkToggle={onBulkTogglePreviewModels}
/>
<ModelsSelectionPanel
models={previewModels}
description={modelDescription}
isRefreshing={isPreviewingModels}
isRefreshDisabled={!canRefreshModels}
refreshLabel={`Refresh ${meta.name} models`}
onRefresh={supportsModelRefresh ? () => onPreviewModels?.(currentDraft) : undefined}
onToggleModel={onTogglePreviewModel}
onBulkToggle={onBulkTogglePreviewModels}
/>
</>
) : null}
</div>
<ConnectFormFooter
onCancel={() => onOpenChange(false)}

View file

@ -111,7 +111,7 @@ export function VertexConnectForm({ onDraftChange }: ProviderConnectFormProps) {
</div>
)}
<p className="text-xs text-muted-foreground">
Add Vertex AI model IDs from the provider&apos;s settings after connecting.
SurfSense will show supported Vertex AI models from the provider catalog.
</p>
</div>
);

View file

@ -74,11 +74,6 @@ export const connectionUpdateRequest = z.object({
enabled: z.boolean().optional(),
});
export const modelCreateRequest = z.object({
model_id: z.string().min(1),
display_name: z.string().nullable().optional(),
});
export const modelUpdateRequest = z.object({
display_name: z.string().nullable().optional(),
enabled: z.boolean().optional(),
@ -135,7 +130,6 @@ export type ConnectionRead = z.infer<typeof connectionRead>;
export type ConnectionCreateRequest = z.infer<typeof connectionCreateRequest>;
export type ModelTestPreviewRequest = z.infer<typeof modelTestPreviewRequest>;
export type ConnectionUpdateRequest = z.infer<typeof connectionUpdateRequest>;
export type ModelCreateRequest = z.infer<typeof modelCreateRequest>;
export type ModelUpdateRequest = z.infer<typeof modelUpdateRequest>;
export type ModelsBulkUpdateRequest = z.infer<typeof modelsBulkUpdateRequest>;
export type ModelRoles = z.infer<typeof modelRoles>;

View file

@ -8,7 +8,6 @@ import {
connectionUpdateRequest,
type GlobalLlmConfigStatus,
globalLlmConfigStatus,
type ModelCreateRequest,
type ModelPreviewRead,
type ModelProviderRead,
type ModelRead,
@ -16,7 +15,6 @@ import {
type ModelsBulkUpdateRequest,
type ModelTestPreviewRequest,
type ModelUpdateRequest,
modelCreateRequest,
modelListResponse,
modelPreviewListResponse,
modelProviderListResponse,
@ -112,19 +110,6 @@ class ModelConnectionsApiService {
});
};
addManualModel = async (
connectionId: number,
request: ModelCreateRequest
): Promise<ModelRead> => {
const parsed = modelCreateRequest.safeParse(request);
if (!parsed.success) {
throw new ValidationError(parsed.error.issues.map((issue) => issue.message).join(", "));
}
return baseApiService.post(`/api/v1/model-connections/${connectionId}/models`, modelRead, {
body: parsed.data,
});
};
updateModel = async (id: number, request: ModelUpdateRequest): Promise<ModelRead> => {
const parsed = modelUpdateRequest.safeParse(request);
if (!parsed.success) {