-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathadmin.llm-models.new.tsx
More file actions
514 lines (477 loc) · 18.4 KB
/
Copy pathadmin.llm-models.new.tsx
File metadata and controls
514 lines (477 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
import { Form, useActionData, useSearchParams } from "@remix-run/react";
import { redirect } from "@remix-run/server-runtime";
import { typedjson } from "remix-typedjson";
import { z } from "zod";
import { useState } from "react";
import { Button, LinkButton } from "~/components/primitives/Buttons";
import { Input } from "~/components/primitives/Input";
import { prisma } from "~/db.server";
import { dashboardAction, dashboardLoader } from "~/services/routeBuilders/dashboardBuilder";
import { generateFriendlyId } from "~/v3/friendlyIdentifiers";
import { llmPricingRegistry } from "~/v3/llmPricingRegistry.server";
export const loader = dashboardLoader(
{ authorization: { requireSuper: true } },
async () => {
return typedjson({});
}
);
const CreateSchema = z.object({
modelName: z.string().min(1),
matchPattern: z.string().min(1),
pricingTiersJson: z.string(),
provider: z.string().optional(),
description: z.string().optional(),
contextWindow: z.string().optional(),
maxOutputTokens: z.string().optional(),
capabilities: z.string().optional(),
isHidden: z.string().optional(),
pricingUnit: z.string().optional(),
});
export const action = dashboardAction(
{ authorization: { requireSuper: true } },
async ({ request }) => {
const formData = await request.formData();
const raw = Object.fromEntries(formData);
console.log("[admin] create model form data:", JSON.stringify(raw).slice(0, 500));
const parsed = CreateSchema.safeParse(raw);
if (!parsed.success) {
console.log("[admin] create model validation error:", JSON.stringify(parsed.error.issues));
return typedjson({ error: "Invalid form data", details: parsed.error.issues }, { status: 400 });
}
const { modelName, matchPattern, pricingTiersJson } = parsed.data;
// Validate regex — strip (?i) POSIX flag since our registry handles it
try {
const testPattern = matchPattern.startsWith("(?i)") ? matchPattern.slice(4) : matchPattern;
new RegExp(testPattern);
} catch {
return typedjson({ error: "Invalid regex in matchPattern" }, { status: 400 });
}
let pricingTiers: Array<{
name: string;
isDefault: boolean;
priority: number;
conditions: Array<{ usageDetailPattern: string; operator: string; value: number }>;
prices: Record<string, number>;
}>;
try {
pricingTiers = JSON.parse(pricingTiersJson) as typeof pricingTiers;
} catch {
return typedjson({ error: "Invalid pricing tiers JSON" }, { status: 400 });
}
const { provider, description, contextWindow, maxOutputTokens, capabilities, isHidden, pricingUnit } = parsed.data;
const model = await prisma.llmModel.create({
data: {
friendlyId: generateFriendlyId("llm_model"),
modelName,
matchPattern,
source: "admin",
provider: provider || null,
description: description || null,
contextWindow: contextWindow ? parseInt(contextWindow) || null : null,
maxOutputTokens: maxOutputTokens ? parseInt(maxOutputTokens) || null : null,
capabilities: capabilities ? capabilities.split(",").map((s) => s.trim()).filter(Boolean) : [],
isHidden: isHidden === "on",
pricingUnit: pricingUnit || null,
},
});
for (const tier of pricingTiers) {
await prisma.llmPricingTier.create({
data: {
modelId: model.id,
name: tier.name,
isDefault: tier.isDefault,
priority: tier.priority,
conditions: tier.conditions,
prices: {
create: Object.entries(tier.prices).map(([usageType, price]) => ({
modelId: model.id,
usageType,
price,
})),
},
},
});
}
await llmPricingRegistry?.reload();
return redirect(`/admin/llm-models/${model.friendlyId}`);
}
);
export default function AdminLlmModelNewRoute() {
const actionData = useActionData<{ error?: string; details?: unknown[] }>();
const [params] = useSearchParams();
const initialModelName = params.get("modelName") ?? "";
const [modelName, setModelName] = useState(initialModelName);
const [matchPattern, setMatchPattern] = useState("");
const [provider, setProvider] = useState("");
const [description, setDescription] = useState("");
const [contextWindow, setContextWindow] = useState("");
const [maxOutputTokens, setMaxOutputTokens] = useState("");
const [capabilities, setCapabilities] = useState("");
const [isHidden, setIsHidden] = useState(false);
const [pricingUnit, setPricingUnit] = useState("tokens");
const [testInput, setTestInput] = useState("");
const [tiers, setTiers] = useState<TierData[]>([
{ name: "Standard", isDefault: true, priority: 0, conditions: [], prices: { input: 0, output: 0 } },
]);
let testResult: boolean | null = null;
if (testInput && matchPattern) {
try {
const pattern = matchPattern.startsWith("(?i)")
? matchPattern.slice(4)
: matchPattern;
testResult = new RegExp(pattern, "i").test(testInput);
} catch {
testResult = null;
}
}
// Auto-generate match pattern from model name
function autoPattern() {
if (modelName) {
const escaped = modelName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
setMatchPattern(`(?i)^(${escaped})$`);
}
}
return (
<main className="flex h-full min-w-0 flex-1 flex-col overflow-y-auto px-4 pb-4">
<div className="max-w-3xl space-y-6">
<div className="flex items-center justify-between">
<h2 className="text-lg font-medium text-text-bright">New LLM Model</h2>
<LinkButton to="/admin/llm-models" variant="tertiary/small">
Back to list
</LinkButton>
</div>
<Form method="post">
<input type="hidden" name="pricingTiersJson" value={JSON.stringify(tiers)} />
<div className="space-y-4">
<div className="space-y-2">
<label className="text-xs font-medium text-text-dimmed">Model Name</label>
<Input
name="modelName"
value={modelName}
onChange={(e) => setModelName(e.target.value)}
variant="medium"
fullWidth
placeholder="e.g. gemini-3-flash"
/>
</div>
<div className="space-y-2">
<div className="flex items-center justify-between">
<label className="text-xs font-medium text-text-dimmed">Match Pattern (regex)</label>
<button
type="button"
onClick={autoPattern}
className="text-xs text-indigo-400 hover:text-indigo-300"
>
Auto-generate from name
</button>
</div>
<Input
name="matchPattern"
value={matchPattern}
onChange={(e) => setMatchPattern(e.target.value)}
variant="medium"
fullWidth
className="font-mono text-xs"
placeholder="(?i)^(google/)?(gemini-3-flash)$"
/>
</div>
<div className="space-y-2">
<label className="text-xs font-medium text-text-dimmed">Test pattern match</label>
<div className="flex items-center gap-2">
<Input
value={testInput}
onChange={(e) => setTestInput(e.target.value)}
placeholder="Type a model name to test..."
variant="medium"
fullWidth
/>
{testInput && (
<span
className={`text-xs font-medium ${
testResult ? "text-green-400" : "text-red-400"
}`}
>
{testResult ? "Match" : "No match"}
</span>
)}
</div>
</div>
{/* Catalog metadata */}
<div className="space-y-2 border-t border-grid-dimmed pt-4">
<label className="text-sm font-medium text-text-bright">Catalog Metadata</label>
<div className="grid grid-cols-2 gap-3">
<div className="space-y-1">
<label className="text-xs font-medium text-text-dimmed">Provider</label>
<Input
name="provider"
value={provider}
onChange={(e) => setProvider(e.target.value)}
variant="medium"
fullWidth
placeholder="openai, anthropic, google"
/>
</div>
<div className="space-y-1">
<label className="text-xs font-medium text-text-dimmed">Context Window</label>
<Input
name="contextWindow"
value={contextWindow}
onChange={(e) => setContextWindow(e.target.value)}
variant="medium"
fullWidth
placeholder="128000"
/>
</div>
</div>
<div className="space-y-1">
<label className="text-xs font-medium text-text-dimmed">Description</label>
<Input
name="description"
value={description}
onChange={(e) => setDescription(e.target.value)}
variant="medium"
fullWidth
placeholder="Brief model description"
/>
</div>
<div className="grid grid-cols-2 gap-3">
<div className="space-y-1">
<label className="text-xs font-medium text-text-dimmed">Max Output Tokens</label>
<Input
name="maxOutputTokens"
value={maxOutputTokens}
onChange={(e) => setMaxOutputTokens(e.target.value)}
variant="medium"
fullWidth
placeholder="16384"
/>
</div>
<div className="space-y-1">
<label className="text-xs font-medium text-text-dimmed">Features (comma-separated)</label>
<Input
name="capabilities"
value={capabilities}
onChange={(e) => setCapabilities(e.target.value)}
variant="medium"
fullWidth
placeholder="vision, tool_use, streaming, json_mode"
/>
</div>
</div>
<div className="space-y-1">
<label className="text-xs font-medium text-text-dimmed">Pricing Unit</label>
<select
name="pricingUnit"
value={pricingUnit}
onChange={(e) => setPricingUnit(e.target.value)}
className="w-full rounded border border-grid-dimmed bg-charcoal-750 px-2 py-1.5 text-sm text-text-bright"
>
<option value="">(unset)</option>
{PRICING_UNITS.map((u) => (
<option key={u} value={u}>
{u}
</option>
))}
</select>
</div>
<label className="flex items-center gap-2 text-xs text-text-dimmed">
<input
type="checkbox"
name="isHidden"
checked={isHidden}
onChange={(e) => setIsHidden(e.target.checked)}
/>
Hidden (exclude from model registry)
</label>
</div>
{/* Pricing tiers */}
<div className="space-y-3">
<div className="flex items-center justify-between">
<label className="text-sm font-medium text-text-bright">Pricing Tiers</label>
<Button
type="button"
variant="tertiary/small"
onClick={() =>
setTiers([
...tiers,
{
name: `Tier ${tiers.length + 1}`,
isDefault: tiers.length === 0,
priority: tiers.length,
conditions: [],
prices: {},
},
])
}
>
Add tier
</Button>
</div>
{tiers.map((tier, tierIdx) => (
<TierEditor
key={tierIdx}
tier={tier}
onChange={(updated) => {
const next = [...tiers];
next[tierIdx] = updated;
setTiers(next);
}}
onRemove={() => setTiers(tiers.filter((_, i) => i !== tierIdx))}
/>
))}
</div>
{actionData?.error && (
<div className="rounded-md bg-red-500/10 border border-red-500/30 p-3 text-sm text-red-400">
{actionData.error}
{actionData.details && (
<pre className="mt-1 text-xs text-red-300/70 overflow-auto">
{JSON.stringify(actionData.details, null, 2)}
</pre>
)}
</div>
)}
<div className="flex items-center gap-2 border-t border-grid-dimmed pt-4">
<Button type="submit" variant="primary/medium">
Create model
</Button>
<LinkButton to="/admin/llm-models" variant="tertiary/medium">
Cancel
</LinkButton>
</div>
</div>
</Form>
</div>
</main>
);
}
// ---------------------------------------------------------------------------
// Shared tier editor (duplicated from detail page — could be extracted later)
// ---------------------------------------------------------------------------
type TierData = {
name: string;
isDefault: boolean;
priority: number;
conditions: Array<{ usageDetailPattern: string; operator: string; value: number }>;
prices: Record<string, number>;
};
const PRICING_UNITS = ["tokens", "characters", "images", "minutes", "requests", "free", "not_findable"];
const COMMON_USAGE_TYPES = [
"input",
"output",
"input_cached_tokens",
"cache_creation_input_tokens",
"reasoning_tokens",
];
function TierEditor({
tier,
onChange,
onRemove,
}: {
tier: TierData;
onChange: (t: TierData) => void;
onRemove: () => void;
}) {
const [newUsageType, setNewUsageType] = useState("");
return (
<div className="rounded-md border border-grid-dimmed bg-charcoal-800 p-3 space-y-3">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<input
className="bg-charcoal-750 text-text-bright rounded px-2 py-1 text-sm border border-grid-dimmed"
value={tier.name}
onChange={(e) => onChange({ ...tier, name: e.target.value })}
placeholder="Tier name"
/>
<label className="flex items-center gap-1 text-xs text-text-dimmed">
<input
type="checkbox"
checked={tier.isDefault}
onChange={(e) => onChange({ ...tier, isDefault: e.target.checked })}
/>
Default
</label>
<label className="flex items-center gap-1 text-xs text-text-dimmed">
Priority:
<input
type="number"
className="w-12 bg-charcoal-750 text-text-bright rounded px-1 py-0.5 text-xs border border-grid-dimmed"
value={tier.priority}
onChange={(e) => onChange({ ...tier, priority: parseInt(e.target.value) || 0 })}
/>
</label>
</div>
<button
type="button"
onClick={onRemove}
className="text-xs text-red-400 hover:text-red-300"
>
Remove tier
</button>
</div>
<div className="space-y-1">
<span className="text-xs font-medium text-text-dimmed">Prices (per token)</span>
<div className="space-y-1">
{Object.entries(tier.prices).map(([usageType, price]) => (
<div key={usageType} className="flex items-center gap-2">
<span className="w-48 text-xs font-mono text-text-dimmed">{usageType}</span>
<input
type="text"
className="w-32 bg-charcoal-750 text-text-bright rounded px-2 py-0.5 text-xs font-mono border border-grid-dimmed"
value={price}
onChange={(e) => {
const val = parseFloat(e.target.value);
if (!isNaN(val)) {
onChange({ ...tier, prices: { ...tier.prices, [usageType]: val } });
}
}}
/>
<button
type="button"
onClick={() => {
const { [usageType]: _, ...rest } = tier.prices;
onChange({ ...tier, prices: rest });
}}
className="text-xs text-red-400 hover:text-red-300"
>
x
</button>
</div>
))}
</div>
<div className="flex items-center gap-2 pt-1">
<select
className="bg-charcoal-750 text-text-dimmed rounded px-2 py-0.5 text-xs border border-grid-dimmed"
value={newUsageType}
onChange={(e) => setNewUsageType(e.target.value)}
>
<option value="">Add price...</option>
{COMMON_USAGE_TYPES.filter((t) => !(t in tier.prices)).map((t) => (
<option key={t} value={t}>
{t}
</option>
))}
<option value="__custom">Custom...</option>
</select>
{newUsageType && (
<Button
type="button"
variant="tertiary/small"
onClick={() => {
const key =
newUsageType === "__custom"
? prompt("Usage type name:") ?? ""
: newUsageType;
if (key) {
onChange({ ...tier, prices: { ...tier.prices, [key]: 0 } });
setNewUsageType("");
}
}}
>
Add
</Button>
)}
</div>
</div>
</div>
);
}