zhou zhou
20 小时以前 4954d3978cf1967729a5a2d5b90f6baef18974da
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
package com.vincent.rsf.server.ai.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.ai.config.AiDefaults;
import com.vincent.rsf.server.ai.dto.AiChatModelOptionDto;
import com.vincent.rsf.server.ai.dto.AiParamValidateResultDto;
import com.vincent.rsf.server.ai.entity.AiParam;
import com.vincent.rsf.server.ai.mapper.AiParamMapper;
import com.vincent.rsf.server.ai.service.AiParamService;
import com.vincent.rsf.server.system.enums.StatusType;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
 
@Service("aiParamService")
@RequiredArgsConstructor
public class AiParamServiceImpl extends ServiceImpl<AiParamMapper, AiParam> implements AiParamService {
 
    private final AiParamValidationSupport aiParamValidationSupport;
    private final AiRedisSupport aiRedisSupport;
 
    @Override
    public AiParam getActiveParam(Long tenantId) {
        ensureTenantId(tenantId);
        AiParam aiParam = this.getOne(new LambdaQueryWrapper<AiParam>()
                .eq(AiParam::getTenantId, tenantId)
                .eq(AiParam::getStatus, StatusType.ENABLE.val)
                .eq(AiParam::getDeleted, 0)
                .last("limit 1"));
        if (aiParam == null) {
            throw new CoolException("未找到启用中的 AI 参数配置");
        }
        return aiParam;
    }
 
    @Override
    public AiParam getChatParam(Long tenantId, Long aiParamId) {
        ensureTenantId(tenantId);
        if (aiParamId == null) {
            return getActiveParam(tenantId);
        }
        AiParam aiParam = requireOwnedRecord(aiParamId, tenantId);
        if (!AiDefaults.PARAM_VALIDATE_VALID.equals(aiParam.getValidateStatus())) {
            throw new CoolException("所选 AI 模型未通过校验,暂不可用于对话");
        }
        return aiParam;
    }
 
    @Override
    public List<AiChatModelOptionDto> listChatModelOptions(Long tenantId) {
        ensureTenantId(tenantId);
        List<AiParam> params = this.list(new LambdaQueryWrapper<AiParam>()
                .eq(AiParam::getTenantId, tenantId)
                .eq(AiParam::getDeleted, 0)
                .eq(AiParam::getValidateStatus, AiDefaults.PARAM_VALIDATE_VALID)
                .orderByDesc(AiParam::getStatus)
                .orderByDesc(AiParam::getUpdateTime)
                .orderByDesc(AiParam::getCreateTime)
                .orderByDesc(AiParam::getId));
        if (params.isEmpty()) {
            return List.of(toChatModelOption(getActiveParam(tenantId)));
        }
        return params.stream()
                .map(this::toChatModelOption)
                .toList();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AiParam setDefaultParam(Long id, Long tenantId, Long userId) {
        ensureTenantId(tenantId);
        if (id == null) {
            throw new CoolException("AI 参数 ID 不能为空");
        }
        AiParam target = requireOwnedRecord(id, tenantId);
        if (!AiDefaults.PARAM_VALIDATE_VALID.equals(target.getValidateStatus())) {
            throw new CoolException("仅允许将校验通过的 AI 参数设置为默认");
        }
        Date now = new Date();
        this.lambdaUpdate()
                .eq(AiParam::getTenantId, tenantId)
                .eq(AiParam::getDeleted, 0)
                .set(AiParam::getStatus, StatusType.DISABLE.val)
                .set(AiParam::getUpdateBy, userId)
                .set(AiParam::getUpdateTime, now)
                .update();
        target.setStatus(StatusType.ENABLE.val);
        target.setUpdateBy(userId);
        target.setUpdateTime(now);
        if (!super.updateById(target)) {
            throw new CoolException("设置默认 AI 参数失败");
        }
        aiRedisSupport.evictTenantConfigCaches(tenantId);
        return target;
    }
 
    @Override
    public void validateBeforeSave(AiParam aiParam, Long tenantId) {
        ensureTenantId(tenantId);
        aiParam.setTenantId(tenantId);
        fillDefaults(aiParam);
        ensureBaseFields(aiParam);
        ensureSingleActive(tenantId, null, aiParam.getStatus());
        applyValidation(aiParam);
    }
 
    @Override
    public void validateBeforeUpdate(AiParam aiParam, Long tenantId) {
        ensureTenantId(tenantId);
        fillDefaults(aiParam);
        if (aiParam.getId() == null) {
            throw new CoolException("AI 参数 ID 不能为空");
        }
        AiParam current = requireOwnedRecord(aiParam.getId(), tenantId);
        aiParam.setTenantId(current.getTenantId());
        ensureBaseFields(aiParam);
        ensureDefaultStillExists(tenantId, current, aiParam.getStatus());
        ensureSingleActive(tenantId, aiParam.getId(), aiParam.getStatus());
        applyValidation(aiParam);
    }
 
    @Override
    public AiParamValidateResultDto validateDraft(AiParam aiParam, Long tenantId) {
        ensureTenantId(tenantId);
        fillDefaults(aiParam);
        ensureBaseFields(aiParam);
        return aiParamValidationSupport.validate(aiParam);
    }
 
    @Override
    public boolean save(AiParam entity) {
        boolean saved = super.save(entity);
        if (saved && entity != null && entity.getTenantId() != null) {
            aiRedisSupport.evictTenantConfigCaches(entity.getTenantId());
        }
        return saved;
    }
 
    @Override
    public boolean updateById(AiParam entity) {
        boolean updated = super.updateById(entity);
        if (updated && entity != null && entity.getTenantId() != null) {
            aiRedisSupport.evictTenantConfigCaches(entity.getTenantId());
        }
        return updated;
    }
 
    @Override
    public boolean removeByIds(java.util.Collection<?> list) {
        java.util.List<java.io.Serializable> ids = list == null ? java.util.List.of() : list.stream()
                .filter(java.util.Objects::nonNull)
                .map(item -> (java.io.Serializable) item)
                .toList();
        java.util.List<AiParam> records = this.listByIds(ids);
        ensureRemovingDefaultIsSafe(records);
        boolean removed = super.removeByIds(list);
        if (removed) {
            records.stream()
                    .map(AiParam::getTenantId)
                    .filter(java.util.Objects::nonNull)
                    .distinct()
                    .forEach(aiRedisSupport::evictTenantConfigCaches);
        }
        return removed;
    }
 
    private void ensureBaseFields(AiParam aiParam) {
        if (!StringUtils.hasText(aiParam.getName())) {
            throw new CoolException("AI 参数名称不能为空");
        }
        if (!StringUtils.hasText(aiParam.getProviderType())) {
            aiParam.setProviderType(AiDefaults.PROVIDER_OPENAI_COMPATIBLE);
        }
        if (!StringUtils.hasText(aiParam.getBaseUrl())) {
            throw new CoolException("AI Base URL 不能为空");
        }
        if (!StringUtils.hasText(aiParam.getApiKey())) {
            throw new CoolException("AI API Key 不能为空");
        }
        if (!StringUtils.hasText(aiParam.getModel())) {
            throw new CoolException("AI 模型不能为空");
        }
    }
 
    private void ensureSingleActive(Long tenantId, Long selfId, Integer status) {
        if (status == null || status != StatusType.ENABLE.val) {
            return;
        }
        LambdaQueryWrapper<AiParam> wrapper = new LambdaQueryWrapper<AiParam>()
                .eq(AiParam::getTenantId, tenantId)
                .eq(AiParam::getStatus, StatusType.ENABLE.val)
                .eq(AiParam::getDeleted, 0);
        if (selfId != null) {
            wrapper.ne(AiParam::getId, selfId);
        }
        if (this.count(wrapper) > 0) {
            throw new CoolException("同一租户仅允许一条启用中的 AI 参数配置");
        }
    }
 
    private void ensureDefaultStillExists(Long tenantId, AiParam current, Integer nextStatus) {
        if (current == null || current.getStatus() == null || current.getStatus() != StatusType.ENABLE.val) {
            return;
        }
        if (nextStatus != null && nextStatus == StatusType.ENABLE.val) {
            return;
        }
        long otherDefaultCount = this.count(new LambdaQueryWrapper<AiParam>()
                .eq(AiParam::getTenantId, tenantId)
                .eq(AiParam::getDeleted, 0)
                .eq(AiParam::getStatus, StatusType.ENABLE.val)
                .ne(AiParam::getId, current.getId()));
        if (otherDefaultCount == 0) {
            throw new CoolException("请先将其他 AI 参数设置为默认,再取消当前默认");
        }
    }
 
    private void ensureRemovingDefaultIsSafe(List<AiParam> records) {
        if (records == null || records.isEmpty()) {
            return;
        }
        records.stream()
                .filter(item -> item.getTenantId() != null && item.getStatus() != null && item.getStatus() == StatusType.ENABLE.val)
                .map(AiParam::getTenantId)
                .distinct()
                .forEach(this::ensureTenantHasRemainingDefaultAfterRemove);
    }
 
    private void ensureTenantHasRemainingDefaultAfterRemove(Long tenantId) {
        long defaultCount = this.count(new LambdaQueryWrapper<AiParam>()
                .eq(AiParam::getTenantId, tenantId)
                .eq(AiParam::getDeleted, 0)
                .eq(AiParam::getStatus, StatusType.ENABLE.val));
        if (defaultCount <= 1) {
            throw new CoolException("默认 AI 参数不能直接删除,请先将其他配置设为默认");
        }
    }
 
    private AiParam requireOwnedRecord(Long id, Long tenantId) {
        AiParam aiParam = this.getOne(new LambdaQueryWrapper<AiParam>()
                .eq(AiParam::getId, id)
                .eq(AiParam::getTenantId, tenantId)
                .eq(AiParam::getDeleted, 0)
                .last("limit 1"));
        if (aiParam == null) {
            throw new CoolException("AI 参数不存在或无权访问");
        }
        return aiParam;
    }
 
    private void ensureTenantId(Long tenantId) {
        if (tenantId == null) {
            throw new CoolException("当前租户不存在");
        }
    }
 
    private void fillDefaults(AiParam aiParam) {
        if (!StringUtils.hasText(aiParam.getProviderType())) {
            aiParam.setProviderType(AiDefaults.PROVIDER_OPENAI_COMPATIBLE);
        }
        if (aiParam.getTemperature() == null) {
            aiParam.setTemperature(AiDefaults.DEFAULT_TEMPERATURE);
        }
        if (aiParam.getTopP() == null) {
            aiParam.setTopP(AiDefaults.DEFAULT_TOP_P);
        }
        if (aiParam.getTimeoutMs() == null) {
            aiParam.setTimeoutMs(AiDefaults.DEFAULT_TIMEOUT_MS);
        }
        if (aiParam.getStreamingEnabled() == null) {
            aiParam.setStreamingEnabled(Boolean.TRUE);
        }
        if (!StringUtils.hasText(aiParam.getValidateStatus())) {
            aiParam.setValidateStatus(AiDefaults.PARAM_VALIDATE_NOT_TESTED);
        }
        if (aiParam.getStatus() == null) {
            aiParam.setStatus(StatusType.ENABLE.val);
        }
    }
 
    private void applyValidation(AiParam aiParam) {
        AiParamValidateResultDto validateResult = aiParamValidationSupport.validate(aiParam);
        aiParam.setValidateStatus(validateResult.getStatus());
        aiParam.setLastValidateMessage(validateResult.getMessage());
        aiParam.setLastValidateElapsedMs(validateResult.getElapsedMs());
        aiParam.setLastValidateTime(parseDate(validateResult.getValidatedAt()));
        if (!AiDefaults.PARAM_VALIDATE_VALID.equals(validateResult.getStatus())) {
            throw new CoolException(validateResult.getMessage());
        }
    }
 
    private Date parseDate(String dateTime) {
        if (!StringUtils.hasText(dateTime)) {
            return null;
        }
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateTime);
        } catch (Exception e) {
            throw new CoolException("解析校验时间失败: " + e.getMessage());
        }
    }
 
    private AiChatModelOptionDto toChatModelOption(AiParam aiParam) {
        return AiChatModelOptionDto.builder()
                .aiParamId(aiParam.getId())
                .name(aiParam.getName())
                .model(aiParam.getModel())
                .providerType(aiParam.getProviderType())
                .active(aiParam.getStatus() != null && aiParam.getStatus() == StatusType.ENABLE.val)
                .build();
    }
}