forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel-utils.ts
More file actions
197 lines (163 loc) · 5.31 KB
/
Copy pathmodel-utils.ts
File metadata and controls
197 lines (163 loc) · 5.31 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
/*
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 { type TFunction } from 'i18next'
import { formatTimestampToDate } from '@/lib/format'
import { getNameRuleConfig, getQuotaTypeConfig } from '../constants'
import type { NameRule, Model } from '../types'
// ============================================================================
// Time Formatting
// ============================================================================
/**
* Format timestamp to standard date string (YYYY-MM-DD HH:mm:ss)
*/
export function formatTimestamp(timestamp: number): string {
if (!timestamp || timestamp === 0) return '-'
return formatTimestampToDate(timestamp)
}
/**
* Format relative time
*/
export function formatRelativeTime(timestamp: number): string {
if (!timestamp || timestamp === 0) return 'Never'
const now = Date.now()
const time = timestamp * 1000
const diff = now - time
const seconds = Math.floor(diff / 1000)
const minutes = Math.floor(seconds / 60)
const hours = Math.floor(minutes / 60)
const days = Math.floor(hours / 24)
if (days > 0) return `${days} day${days > 1 ? 's' : ''} ago`
if (hours > 0) return `${hours} hour${hours > 1 ? 's' : ''} ago`
if (minutes > 0) return `${minutes} minute${minutes > 1 ? 's' : ''} ago`
return `${seconds} second${seconds !== 1 ? 's' : ''} ago`
}
// ============================================================================
// Tags Parsing
// ============================================================================
/**
* Parse tags string to array
*/
export function parseModelTags(tags: string | undefined): string[] {
if (!tags) return []
return tags
.split(',')
.map((tag) => tag.trim())
.filter(Boolean)
}
/**
* Format tags array to string
*/
export function formatTagsString(tags: string[]): string {
return tags.join(',')
}
// ============================================================================
// Endpoints Parsing
// ============================================================================
/**
* Parse endpoints JSON string
*/
export function parseEndpoints(
endpoints: string | undefined
): Record<string, unknown> | unknown[] | null {
if (!endpoints || endpoints.trim() === '') return null
try {
return JSON.parse(endpoints)
} catch {
return null
}
}
/**
* Format endpoints to display
*/
export function formatEndpointsDisplay(
endpoints: string | undefined
): string[] {
const parsed = parseEndpoints(endpoints)
if (!parsed) return []
if (typeof parsed === 'object' && !Array.isArray(parsed)) {
return Object.keys(parsed)
}
if (Array.isArray(parsed)) {
return parsed.map(String)
}
return []
}
// ============================================================================
// Name Rule Utils
// ============================================================================
/**
* Get name rule label
*/
export function getNameRuleLabelByRule(rule: NameRule, t: TFunction): string {
const config = getNameRuleConfig(t)
return config[rule]?.label || '-'
}
/**
* Get name rule config by rule
*/
export function getNameRuleConfigByRule(rule: NameRule, t: TFunction) {
const config = getNameRuleConfig(t)
return config[rule] || config[0]
}
// ============================================================================
// Quota Type Utils
// ============================================================================
/**
* Format quota types array
*/
export function formatQuotaTypes(
quotaTypes: number[] | undefined,
t: TFunction
): string {
if (!quotaTypes || quotaTypes.length === 0) return '-'
const config = getQuotaTypeConfig(t)
return quotaTypes.map((qt) => config[qt]?.label || String(qt)).join(', ')
}
// ============================================================================
// Model Validation
// ============================================================================
/**
* Validate model name
*/
export function validateModelName(name: string): boolean {
return name.trim().length > 0
}
/**
* Validate endpoints JSON
*/
export function validateEndpointsJSON(endpoints: string): boolean {
if (!endpoints || endpoints.trim() === '') return true
try {
JSON.parse(endpoints)
return true
} catch {
return false
}
}
// ============================================================================
// Model Status Utils
// ============================================================================
/**
* Check if model is enabled
*/
export function isModelEnabled(model: Model): boolean {
return model.status === 1
}
/**
* Check if model syncs with official
*/
export function isModelSyncOfficial(model: Model): boolean {
return model.sync_official === 1
}