|
| 1 | +import { modelService } from "./modelService"; |
| 2 | +import { ModelType } from "@/types/modelConfig"; |
| 3 | + |
| 4 | +const TYPES_TO_SYNC: ModelType[] = [ |
| 5 | + ("llm" as unknown) as ModelType, |
| 6 | + ("embedding" as unknown) as ModelType, |
| 7 | + ("multi_embedding" as unknown) as ModelType, |
| 8 | + ("vlm" as unknown) as ModelType, |
| 9 | + ("tts" as unknown) as ModelType, |
| 10 | + ("stt" as unknown) as ModelType, |
| 11 | +]; |
| 12 | + |
| 13 | +/** |
| 14 | + * Sync models from ModelEngine and verify connectivity. |
| 15 | + * Returns an object with overall success and per-model verification results. |
| 16 | + */ |
| 17 | +export async function syncModelEngine(apiKey: string) { |
| 18 | + let syncFailed = false; |
| 19 | + try { |
| 20 | + for (const type of TYPES_TO_SYNC) { |
| 21 | + try { |
| 22 | + const providerModels = await modelService.addProviderModel({ |
| 23 | + provider: "modelengine", |
| 24 | + type: type as any, |
| 25 | + apiKey, |
| 26 | + }); |
| 27 | + if (providerModels && providerModels.length > 0) { |
| 28 | + await modelService.addBatchCustomModel({ |
| 29 | + api_key: apiKey, |
| 30 | + provider: "modelengine", |
| 31 | + type, |
| 32 | + models: providerModels, |
| 33 | + }); |
| 34 | + } |
| 35 | + } catch (err) { |
| 36 | + // mark that at least one provider fetch failed |
| 37 | + syncFailed = true; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // reload all models from backend |
| 42 | + const allModelsAfter = await modelService.getAllModels(); |
| 43 | + const modelEngineModels = allModelsAfter.filter((m) => m.source === "modelengine"); |
| 44 | + |
| 45 | + // update persisted api keys for modelengine models if needed |
| 46 | + if (modelEngineModels.length > 0 && apiKey) { |
| 47 | + const updates = modelEngineModels.map((m) => ({ |
| 48 | + model_id: String(m.id || m.name || m.displayName), |
| 49 | + apiKey: apiKey, |
| 50 | + })); |
| 51 | + try { |
| 52 | + await modelService.updateBatchModel(updates); |
| 53 | + } catch (err) { |
| 54 | + // non-fatal; continue to verification but flag sync failure |
| 55 | + syncFailed = true; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // verify each persistent model and collect results |
| 60 | + const verificationResults: Array<{ displayName: string; type: string; connected: boolean }> = []; |
| 61 | + for (const m of modelEngineModels) { |
| 62 | + if (!m.displayName) continue; |
| 63 | + try { |
| 64 | + const connected = await modelService.verifyCustomModel(m.displayName); |
| 65 | + verificationResults.push({ displayName: m.displayName, type: m.type, connected }); |
| 66 | + } catch (err) { |
| 67 | + verificationResults.push({ displayName: m.displayName, type: m.type, connected: false }); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + const anyUnavailable = verificationResults.some((r) => !r.connected); |
| 72 | + const success = !syncFailed && !anyUnavailable; |
| 73 | + return { success, verificationResults, error: syncFailed ? "provider_fetch_failed" : undefined }; |
| 74 | + } catch (err: any) { |
| 75 | + return { success: false, verificationResults: [], error: err?.message || String(err) }; |
| 76 | + } |
| 77 | +} |
0 commit comments