forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel-form.ts
More file actions
141 lines (122 loc) · 4.22 KB
/
Copy pathmodel-form.ts
File metadata and controls
141 lines (122 loc) · 4.22 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
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { z } from 'zod'
import type { Model } from '../types'
import { parseModelTags as parseTagsFromUtils } from './model-utils'
// ============================================================================
// Model Form Schema
// ============================================================================
/**
* Model form validation schema
*/
export const modelFormSchema = z.object({
id: z.number().optional(),
model_name: z.string().min(1, 'Model name is required'),
description: z.string().default(''),
icon: z.string().default(''),
tags: z.array(z.string()).default([]),
vendor_id: z.number().optional(),
endpoints: z.string().default(''),
name_rule: z.number().min(0).max(3).default(0),
status: z.boolean().default(true),
sync_official: z.boolean().default(true),
enable_groups: z.array(z.string()).default([]),
quota_types: z.array(z.number()).default([]),
})
export type ModelFormValues = z.infer<typeof modelFormSchema>
// ============================================================================
// Vendor Form Schema
// ============================================================================
/**
* Vendor form validation schema
*/
export const vendorFormSchema = z.object({
id: z.number().optional(),
name: z.string().min(1, 'Vendor name is required'),
description: z.string().default(''),
icon: z.string().default(''),
status: z.number().default(1),
})
export type VendorFormValues = z.infer<typeof vendorFormSchema>
// ============================================================================
// Form Data Transformation
// ============================================================================
/**
* Transform model to form default values
*/
export function transformModelToFormDefaults(model: Model): ModelFormValues {
return {
id: model.id,
model_name: model.model_name,
description: model.description || '',
icon: model.icon || '',
tags: parseTagsFromUtils(model.tags),
vendor_id: model.vendor_id,
endpoints: model.endpoints || '',
name_rule: model.name_rule || 0,
status: model.status === 1,
sync_official: model.sync_official === 1,
enable_groups: model.enable_groups || [],
quota_types: model.quota_types || [],
}
}
/**
* Transform form data to model create/update payload
*/
export function transformFormDataToModelPayload(
formData: ModelFormValues
): Partial<Model> {
return {
id: formData.id,
model_name: formData.model_name,
description: formData.description || '',
icon: formData.icon || '',
tags: formatTagsArray(formData.tags),
vendor_id: formData.vendor_id,
endpoints: formData.endpoints || '',
name_rule: formData.name_rule,
status: formData.status ? 1 : 0,
sync_official: formData.sync_official ? 1 : 0,
enable_groups: formData.enable_groups,
quota_types: formData.quota_types,
}
}
// ============================================================================
// Parsing and Formatting Helpers
// ============================================================================
/**
* Format tags array to string
*/
export function formatTagsArray(tags: string[]): string {
return tags.filter(Boolean).join(',')
}
/**
* Validate JSON string
*/
export function validateJSON(value: string): boolean {
if (!value || value.trim() === '') return true
try {
JSON.parse(value)
return true
} catch {
return false
}
}
/**
* Validate endpoints JSON
*/
export function validateEndpoints(endpoints: string): boolean {
return validateJSON(endpoints)
}