zhou zhou
2 天以前 0a1d91e42e6c5af96e1108e9ebcc37e99eb3b22c
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
import { $t } from '@/locales'
 
const AI_PARAM_REPORT_TITLE = () => $t('pages.system.aiParam.reportTitle')
 
const PROVIDER_OPTIONS = [{ label: 'OPENAI_COMPATIBLE', value: 'OPENAI_COMPATIBLE' }]
 
const STATUS_OPTIONS = [
  { label: () => $t('pages.system.aiParam.status.default'), value: 1 },
  { label: () => $t('pages.system.aiParam.status.candidate'), value: 0 }
]
 
const VALIDATE_STATUS_META = {
  VALID: { text: () => $t('pages.system.aiParam.validation.valid'), type: 'success' },
  INVALID: { text: () => $t('pages.system.aiParam.validation.invalid'), type: 'danger' },
  NOT_TESTED: { text: () => $t('pages.system.aiParam.validation.notTested'), type: 'info' }
}
 
function createAiParamSearchState() {
  return {
    condition: '',
    providerType: '',
    model: '',
    status: ''
  }
}
 
function createAiParamFormState() {
  return {
    id: null,
    name: '',
    providerType: 'OPENAI_COMPATIBLE',
    baseUrl: '',
    apiKey: '',
    model: '',
    temperature: 0.7,
    topP: 1,
    maxTokens: null,
    timeoutMs: 60000,
    streamingEnabled: true,
    status: 1,
    memo: '',
    validateStatus: 'NOT_TESTED',
    lastValidateMessage: '',
    lastValidateElapsedMs: null,
    'lastValidateTime$': '',
    updateBy: '',
    'updateTime$': ''
  }
}
 
function getAiParamPaginationKey() {
  return {
    current: 'current',
    size: 'pageSize'
  }
}
 
function getAiParamProviderOptions() {
  return PROVIDER_OPTIONS
}
 
function getAiParamStatusOptions() {
  return STATUS_OPTIONS.map((item) => ({ ...item, label: item.label() }))
}
 
function getAiParamStatusMeta(status) {
  return Number(status) === 1
    ? { text: $t('pages.system.aiParam.status.default'), type: 'success' }
    : { text: $t('pages.system.aiParam.status.candidate'), type: 'info' }
}
 
function getAiParamValidateStatusMeta(status) {
  const meta = VALIDATE_STATUS_META[String(status || '').trim()]
  if (meta) {
    return { text: meta.text(), type: meta.type }
  }
  return { text: status || $t('common.status.unknown'), type: 'info' }
}
 
function buildAiParamSearchParams(params = {}) {
  return {
    condition: normalizeText(params.condition),
    providerType: normalizeText(params.providerType),
    model: normalizeText(params.model),
    status: normalizeOptionalNumber(params.status)
  }
}
 
function buildAiParamPageQueryParams(params = {}) {
  return {
    current: Number(params.current) > 0 ? Number(params.current) : 1,
    pageSize: Number(params.pageSize) > 0 ? Number(params.pageSize) : 20,
    condition: normalizeText(params.condition),
    providerType: normalizeText(params.providerType),
    model: normalizeText(params.model),
    status: normalizeOptionalNumber(params.status)
  }
}
 
function buildAiParamDialogModel(record = {}) {
  const base = createAiParamFormState()
  return {
    ...base,
    ...record,
    id: normalizeOptionalNumber(record.id) ?? null,
    providerType: normalizeText(record.providerType) || base.providerType,
    status: normalizeOptionalNumber(record.status) ?? base.status,
    temperature: normalizeOptionalFloat(record.temperature) ?? base.temperature,
    topP: normalizeOptionalFloat(record.topP) ?? base.topP,
    maxTokens: normalizeOptionalNumber(record.maxTokens),
    timeoutMs: normalizeOptionalNumber(record.timeoutMs) ?? base.timeoutMs,
    streamingEnabled:
      record.streamingEnabled === undefined || record.streamingEnabled === null
        ? base.streamingEnabled
        : Boolean(record.streamingEnabled),
    validateStatus: normalizeText(record.validateStatus) || base.validateStatus,
    lastValidateMessage: normalizeText(record.lastValidateMessage),
    lastValidateElapsedMs: normalizeOptionalNumber(record.lastValidateElapsedMs),
    'lastValidateTime$': normalizeText(record['lastValidateTime$']),
    updateBy: record.updateBy ?? '',
    'updateTime$': normalizeText(record['updateTime$'])
  }
}
 
function buildAiParamSavePayload(formData = {}) {
  return {
    ...(normalizeOptionalNumber(formData.id) !== null ? { id: normalizeOptionalNumber(formData.id) } : {}),
    name: normalizeText(formData.name),
    providerType: normalizeText(formData.providerType) || 'OPENAI_COMPATIBLE',
    baseUrl: normalizeText(formData.baseUrl),
    apiKey: normalizeText(formData.apiKey),
    model: normalizeText(formData.model),
    temperature: normalizeOptionalFloat(formData.temperature),
    topP: normalizeOptionalFloat(formData.topP),
    maxTokens: normalizeOptionalNumber(formData.maxTokens),
    timeoutMs: normalizeOptionalNumber(formData.timeoutMs),
    streamingEnabled: Boolean(formData.streamingEnabled),
    status: normalizeOptionalNumber(formData.status) ?? 1,
    memo: normalizeText(formData.memo)
  }
}
 
function normalizeAiParamRow(record = {}) {
  const statusMeta = getAiParamStatusMeta(record.status)
  const validateMeta = getAiParamValidateStatusMeta(record.validateStatus)
 
  return {
    ...record,
    providerType: normalizeText(record.providerType) || 'OPENAI_COMPATIBLE',
    model: normalizeText(record.model),
    baseUrl: normalizeText(record.baseUrl),
    memo: normalizeText(record.memo),
    statusBool: Number(record.status) === 1,
    statusText: statusMeta.text,
    statusType: statusMeta.type,
    validateStatusText: validateMeta.text,
    validateStatusType: validateMeta.type,
    streamingEnabled: Boolean(record.streamingEnabled),
    streamingText: record.streamingEnabled
      ? $t('pages.system.aiParam.streaming.enabled')
      : $t('pages.system.aiParam.streaming.disabled'),
    'createTime$': normalizeText(record['createTime$']),
    'updateTime$': normalizeText(record['updateTime$']),
    'lastValidateTime$': normalizeText(record['lastValidateTime$'])
  }
}
 
function buildAiParamPrintRows(records = []) {
  return records.map((item) => {
    const row = normalizeAiParamRow(item)
    return {
      name: row.name || '-',
      providerType: row.providerType || '-',
      model: row.model || '-',
      statusText: row.statusText,
      validateStatusText: row.validateStatusText,
      timeoutMs: row.timeoutMs ?? '--',
      updateTime: row['updateTime$'] || '--',
      memo: row.memo || '--'
    }
  })
}
 
function getAiParamReportColumns() {
  return [
    { label: $t('pages.system.aiParam.table.name'), prop: 'name' },
    { label: $t('pages.system.aiParam.table.providerType'), prop: 'providerType' },
    { label: $t('pages.system.aiParam.table.model'), prop: 'model' },
    { label: $t('pages.system.aiParam.table.status'), prop: 'statusText' },
    { label: $t('pages.system.aiParam.table.validateStatus'), prop: 'validateStatusText' },
    { label: $t('pages.system.aiParam.table.timeoutMs'), prop: 'timeoutMs' },
    { label: $t('table.updateTime'), prop: 'updateTime' },
    { label: $t('table.remark'), prop: 'memo' }
  ]
}
 
function normalizeText(value) {
  return value === undefined || value === null ? '' : String(value).trim()
}
 
function normalizeOptionalNumber(value) {
  if (value === undefined || value === null || value === '') {
    return null
  }
  const number = Number(value)
  return Number.isFinite(number) ? number : null
}
 
function normalizeOptionalFloat(value) {
  if (value === undefined || value === null || value === '') {
    return null
  }
  const number = Number(value)
  return Number.isFinite(number) ? number : null
}
 
export {
  AI_PARAM_REPORT_TITLE,
  buildAiParamDialogModel,
  buildAiParamPageQueryParams,
  buildAiParamPrintRows,
  buildAiParamSavePayload,
  buildAiParamSearchParams,
  createAiParamFormState,
  createAiParamSearchState,
  getAiParamPaginationKey,
  getAiParamProviderOptions,
  getAiParamReportColumns,
  getAiParamStatusMeta,
  getAiParamStatusOptions,
  getAiParamValidateStatusMeta,
  normalizeAiParamRow
}