fix(quick-pane): restore model catalog controls
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import type { Model } from "./workspace/types";
|
||||
|
||||
export type ModelIdentity = { provider: string; modelId: string };
|
||||
|
||||
export function modelIdentity(model: Model): ModelIdentity | undefined {
|
||||
if (!model.provider || !model.id) return undefined;
|
||||
return { provider: model.provider, modelId: model.id };
|
||||
}
|
||||
|
||||
export function modelIdentityValue(identity: ModelIdentity) {
|
||||
return JSON.stringify([identity.provider, identity.modelId]);
|
||||
}
|
||||
|
||||
export function modelIdentityFromValue(
|
||||
value: string,
|
||||
): ModelIdentity | undefined {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(value);
|
||||
if (
|
||||
!Array.isArray(parsed) ||
|
||||
parsed.length !== 2 ||
|
||||
!parsed.every((part) => typeof part === "string")
|
||||
)
|
||||
return undefined;
|
||||
return { provider: parsed[0], modelId: parsed[1] };
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function catalogModels(
|
||||
models: Model[],
|
||||
configured: ModelIdentity[],
|
||||
): Model[] {
|
||||
const available = models.filter((model) => model.provider && model.id);
|
||||
const keys = new Set(
|
||||
available
|
||||
.map(modelIdentity)
|
||||
.filter((identity): identity is ModelIdentity => !!identity)
|
||||
.map(modelIdentityValue),
|
||||
);
|
||||
for (const identity of configured) {
|
||||
if (!identity.provider || !identity.modelId) continue;
|
||||
const key = modelIdentityValue(identity);
|
||||
if (keys.has(key)) continue;
|
||||
available.unshift({ provider: identity.provider, id: identity.modelId });
|
||||
keys.add(key);
|
||||
}
|
||||
return available;
|
||||
}
|
||||
|
||||
export async function getModelCatalog() {
|
||||
const catalog = await invoke<{ models?: Model[] }>("get_model_catalog");
|
||||
return catalog?.models ?? [];
|
||||
}
|
||||
Reference in New Issue
Block a user