|
2 | 2 |
|
3 | 3 | import { revalidatePath } from "next/cache"; |
4 | 4 | import { canManageCourseImports } from "@/lib/auth/viewer"; |
5 | | -import { configuredOpenRouterModels } from "@/lib/course-import/openrouter"; |
6 | 5 | import { createClient } from "@/lib/supabase/server"; |
7 | 6 | import { IMPORT_MODEL_SETTING_KEY } from "@/lib/admin/settings"; |
| 7 | +import { fetchCatalogueModel } from "@/lib/admin/model-catalogue"; |
| 8 | +import { assertOpenRouterModel } from "@/lib/course-import/openrouter"; |
8 | 9 |
|
9 | 10 | export type ImportModelActionResult = { |
10 | 11 | ok: boolean; |
11 | 12 | model: string; |
12 | 13 | message: string; |
13 | 14 | }; |
14 | 15 |
|
15 | | -/** |
16 | | - * Set the model every subsequent import run requests. Rejecting models the |
17 | | - * deployment does not allow keeps this in step with the queue's own check |
18 | | - * rather than deferring the failure to a run that has already started. |
19 | | - */ |
| 16 | +function refreshImportPages() { |
| 17 | + revalidatePath("/admin", "layout"); |
| 18 | +} |
| 19 | + |
20 | 20 | export async function setImportModel( |
21 | 21 | model: string, |
22 | 22 | ): Promise<ImportModelActionResult> { |
23 | | - const requested = model.trim().toLowerCase(); |
24 | | - if (!configuredOpenRouterModels().includes(requested)) { |
| 23 | + if (!(await canManageCourseImports())) |
| 24 | + return { |
| 25 | + ok: false, |
| 26 | + model, |
| 27 | + message: "Import management permission is required.", |
| 28 | + }; |
| 29 | + try { |
| 30 | + const requested = assertOpenRouterModel(model); |
| 31 | + const supabase = await createClient(); |
| 32 | + const { error } = await supabase |
| 33 | + .from("app_settings") |
| 34 | + .upsert( |
| 35 | + { key: IMPORT_MODEL_SETTING_KEY, value: requested }, |
| 36 | + { onConflict: "key" }, |
| 37 | + ); |
| 38 | + if (error) |
| 39 | + return { |
| 40 | + ok: false, |
| 41 | + model, |
| 42 | + message: |
| 43 | + "The default model could not be saved. Choose an enabled model.", |
| 44 | + }; |
| 45 | + refreshImportPages(); |
| 46 | + return { |
| 47 | + ok: true, |
| 48 | + model: requested, |
| 49 | + message: "The default import model was updated.", |
| 50 | + }; |
| 51 | + } catch { |
25 | 52 | return { |
26 | 53 | ok: false, |
27 | 54 | model, |
28 | | - message: "Choose a model this deployment is configured to call.", |
| 55 | + message: "The default model could not be saved.", |
29 | 56 | }; |
30 | 57 | } |
| 58 | +} |
31 | 59 |
|
32 | | - if (!(await canManageCourseImports())) { |
| 60 | +export async function saveImportModel( |
| 61 | + model: string, |
| 62 | + refreshOnly = false, |
| 63 | +): Promise<ImportModelActionResult> { |
| 64 | + if (!(await canManageCourseImports())) |
33 | 65 | return { |
34 | 66 | ok: false, |
35 | 67 | model, |
36 | 68 | message: "Import management permission is required.", |
37 | 69 | }; |
| 70 | + try { |
| 71 | + const entry = await fetchCatalogueModel(model); |
| 72 | + const supabase = await createClient(); |
| 73 | + const { enabled, visible, ...pricing } = entry; |
| 74 | + const { error } = refreshOnly |
| 75 | + ? await supabase |
| 76 | + .from("import_models") |
| 77 | + .update(pricing) |
| 78 | + .eq("id", entry.id) |
| 79 | + .eq("enabled", true) |
| 80 | + .select("id") |
| 81 | + .single() |
| 82 | + : await supabase |
| 83 | + .from("import_models") |
| 84 | + .upsert({ ...pricing, enabled, visible }); |
| 85 | + if (error) |
| 86 | + return { ok: false, model, message: "The model could not be saved." }; |
| 87 | + refreshImportPages(); |
| 88 | + return { |
| 89 | + ok: true, |
| 90 | + model: entry.id, |
| 91 | + message: "The model and its pricing were saved.", |
| 92 | + }; |
| 93 | + } catch { |
| 94 | + return { |
| 95 | + ok: false, |
| 96 | + model, |
| 97 | + message: |
| 98 | + "The model could not be loaded from OpenRouter. Check its identifier and try again.", |
| 99 | + }; |
38 | 100 | } |
| 101 | +} |
39 | 102 |
|
| 103 | +export async function removeImportModel( |
| 104 | + model: string, |
| 105 | +): Promise<ImportModelActionResult> { |
| 106 | + if (!(await canManageCourseImports())) |
| 107 | + return { |
| 108 | + ok: false, |
| 109 | + model, |
| 110 | + message: "Import management permission is required.", |
| 111 | + }; |
40 | 112 | try { |
41 | 113 | const supabase = await createClient(); |
42 | | - const { error } = await supabase |
43 | | - .from("app_settings") |
44 | | - .upsert( |
45 | | - { key: IMPORT_MODEL_SETTING_KEY, value: requested }, |
46 | | - { onConflict: "key" }, |
47 | | - ); |
48 | | - if (error) { |
49 | | - return { ok: false, model, message: "The model could not be saved." }; |
50 | | - } |
| 114 | + const { data, error } = await supabase |
| 115 | + .from("import_models") |
| 116 | + .update({ enabled: false }) |
| 117 | + .eq("id", assertOpenRouterModel(model)) |
| 118 | + .select("id") |
| 119 | + .single(); |
| 120 | + if (error || !data) |
| 121 | + return { |
| 122 | + ok: false, |
| 123 | + model, |
| 124 | + message: |
| 125 | + "The model could not be removed. Choose another default first.", |
| 126 | + }; |
| 127 | + refreshImportPages(); |
| 128 | + return { |
| 129 | + ok: true, |
| 130 | + model, |
| 131 | + message: "The model was removed from future imports.", |
| 132 | + }; |
51 | 133 | } catch { |
52 | | - return { ok: false, model, message: "The model could not be saved." }; |
| 134 | + return { ok: false, model, message: "The model could not be removed." }; |
53 | 135 | } |
| 136 | +} |
54 | 137 |
|
55 | | - revalidatePath("/admin"); |
56 | | - revalidatePath("/admin/dashboard"); |
57 | | - return { |
58 | | - ok: true, |
59 | | - model: requested, |
60 | | - message: `Imports now use ${requested}.`, |
61 | | - }; |
| 138 | +export async function setImportModelVisibility( |
| 139 | + model: string, |
| 140 | + visible: boolean, |
| 141 | +): Promise<ImportModelActionResult> { |
| 142 | + if (!(await canManageCourseImports())) |
| 143 | + return { |
| 144 | + ok: false, |
| 145 | + model, |
| 146 | + message: "Import management permission is required.", |
| 147 | + }; |
| 148 | + try { |
| 149 | + if (typeof visible !== "boolean") |
| 150 | + throw new Error("The visibility is invalid."); |
| 151 | + const supabase = await createClient(); |
| 152 | + const { data, error } = await supabase |
| 153 | + .from("import_models") |
| 154 | + .update({ visible }) |
| 155 | + .eq("id", assertOpenRouterModel(model)) |
| 156 | + .eq("enabled", true) |
| 157 | + .select("id") |
| 158 | + .single(); |
| 159 | + if (error || !data) |
| 160 | + return { |
| 161 | + ok: false, |
| 162 | + model, |
| 163 | + message: |
| 164 | + "The model visibility could not be changed. Choose another default before hiding it.", |
| 165 | + }; |
| 166 | + refreshImportPages(); |
| 167 | + return { |
| 168 | + ok: true, |
| 169 | + model, |
| 170 | + message: visible |
| 171 | + ? "The model is available for selection." |
| 172 | + : "The model is hidden from selection.", |
| 173 | + }; |
| 174 | + } catch { |
| 175 | + return { |
| 176 | + ok: false, |
| 177 | + model, |
| 178 | + message: "The model visibility could not be changed.", |
| 179 | + }; |
| 180 | + } |
62 | 181 | } |
0 commit comments