Stabilize report AI requests
This commit is contained in:
@@ -15,13 +15,13 @@ const actor: SafeUser = {
|
|||||||
updatedAt: new Date().toISOString(),
|
updatedAt: new Date().toISOString(),
|
||||||
};
|
};
|
||||||
|
|
||||||
const createService = (modelName = 'moonshot-v1') => {
|
const createService = (modelName = 'moonshot-v1', endpoint = 'https://provider.example/v1') => {
|
||||||
const settingsService = {
|
const settingsService = {
|
||||||
getSystemSettings: vi.fn().mockResolvedValue({
|
getSystemSettings: vi.fn().mockResolvedValue({
|
||||||
activeAiProvider: 'kimi',
|
activeAiProvider: 'kimi',
|
||||||
aiProviders: {
|
aiProviders: {
|
||||||
kimi: {
|
kimi: {
|
||||||
endpoint: 'https://provider.example/v1',
|
endpoint,
|
||||||
apiKey: 'test-key',
|
apiKey: 'test-key',
|
||||||
modelName,
|
modelName,
|
||||||
},
|
},
|
||||||
@@ -34,6 +34,7 @@ const createService = (modelName = 'moonshot-v1') => {
|
|||||||
|
|
||||||
describe('AiService', () => {
|
describe('AiService', () => {
|
||||||
const originalRetryDelays = process.env.AI_PROVIDER_RETRY_DELAYS_MS;
|
const originalRetryDelays = process.env.AI_PROVIDER_RETRY_DELAYS_MS;
|
||||||
|
const originalProviderTimeout = process.env.AI_PROVIDER_TIMEOUT_MS;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
process.env.AI_PROVIDER_RETRY_DELAYS_MS = '0,0';
|
process.env.AI_PROVIDER_RETRY_DELAYS_MS = '0,0';
|
||||||
@@ -46,6 +47,11 @@ describe('AiService', () => {
|
|||||||
} else {
|
} else {
|
||||||
process.env.AI_PROVIDER_RETRY_DELAYS_MS = originalRetryDelays;
|
process.env.AI_PROVIDER_RETRY_DELAYS_MS = originalRetryDelays;
|
||||||
}
|
}
|
||||||
|
if (originalProviderTimeout === undefined) {
|
||||||
|
delete process.env.AI_PROVIDER_TIMEOUT_MS;
|
||||||
|
} else {
|
||||||
|
process.env.AI_PROVIDER_TIMEOUT_MS = originalProviderTimeout;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('retries transient provider overloads and then returns a completion', async () => {
|
it('retries transient provider overloads and then returns a completion', async () => {
|
||||||
@@ -104,4 +110,69 @@ describe('AiService', () => {
|
|||||||
expect(requestBody).not.toHaveProperty('presence_penalty');
|
expect(requestBody).not.toHaveProperty('presence_penalty');
|
||||||
expect(requestBody).not.toHaveProperty('frequency_penalty');
|
expect(requestBody).not.toHaveProperty('frequency_penalty');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('uses the faster Moonshot text model for Kimi K2 text-only report prompts', async () => {
|
||||||
|
const fetchMock = vi.fn().mockResolvedValue(
|
||||||
|
new Response(JSON.stringify({ choices: [{ message: { content: '{"reply":"已完善"}' } }] }), { status: 200 }),
|
||||||
|
);
|
||||||
|
vi.stubGlobal('fetch', fetchMock);
|
||||||
|
|
||||||
|
await createService('kimi-k2.6', 'https://api.moonshot.cn/v1').chat(actor, {
|
||||||
|
messages: [{ role: 'user', content: '请继续完善手术步骤' }],
|
||||||
|
temperature: 0.3,
|
||||||
|
});
|
||||||
|
|
||||||
|
const requestBody = JSON.parse(String(fetchMock.mock.calls[0][1]?.body));
|
||||||
|
expect(requestBody).toMatchObject({
|
||||||
|
messages: [{ role: 'user', content: '请继续完善手术步骤' }],
|
||||||
|
model: 'moonshot-v1-32k',
|
||||||
|
temperature: 0.3,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps an image-capable Kimi model for image prompts and removes unsupported sampling options', async () => {
|
||||||
|
const fetchMock = vi.fn().mockResolvedValue(
|
||||||
|
new Response(JSON.stringify({ choices: [{ message: { content: '{"reply":"已分析图片"}' } }] }), { status: 200 }),
|
||||||
|
);
|
||||||
|
vi.stubGlobal('fetch', fetchMock);
|
||||||
|
|
||||||
|
await createService('moonshot-v1-32k', 'https://api.moonshot.cn/v1').chat(actor, {
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
role: 'user',
|
||||||
|
content: [
|
||||||
|
{ type: 'image_url', image_url: { url: 'data:image/png;base64,abc' } },
|
||||||
|
{ type: 'text', text: '请分析图片' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
temperature: 0.3,
|
||||||
|
});
|
||||||
|
|
||||||
|
const requestBody = JSON.parse(String(fetchMock.mock.calls[0][1]?.body));
|
||||||
|
expect(requestBody.model).toBe('kimi-k2.6');
|
||||||
|
expect(requestBody).not.toHaveProperty('temperature');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('turns slow provider responses into a structured timeout before the public gateway times out', async () => {
|
||||||
|
process.env.AI_PROVIDER_TIMEOUT_MS = '1';
|
||||||
|
const fetchMock = vi.fn((_url: string, init?: RequestInit) => new Promise((_resolve, reject) => {
|
||||||
|
init?.signal?.addEventListener('abort', () => {
|
||||||
|
const error = new Error('This operation was aborted');
|
||||||
|
error.name = 'AbortError';
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
vi.stubGlobal('fetch', fetchMock);
|
||||||
|
|
||||||
|
await expect(createService().chat(actor, {
|
||||||
|
messages: [{ role: 'user', content: '请完善报告内容' }],
|
||||||
|
})).rejects.toMatchObject({
|
||||||
|
response: expect.objectContaining({
|
||||||
|
code: 'AI_PROVIDER_TIMEOUT',
|
||||||
|
message: expect.stringContaining('AI 服务响应超时'),
|
||||||
|
}),
|
||||||
|
status: 504,
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -11,6 +11,9 @@ interface AiProvider {
|
|||||||
|
|
||||||
const RETRYABLE_PROVIDER_STATUSES = new Set([429, 500, 502, 503, 504]);
|
const RETRYABLE_PROVIDER_STATUSES = new Set([429, 500, 502, 503, 504]);
|
||||||
const DEFAULT_RETRY_DELAYS_MS = [600, 1200];
|
const DEFAULT_RETRY_DELAYS_MS = [600, 1200];
|
||||||
|
const DEFAULT_PROVIDER_TIMEOUT_MS = 45_000;
|
||||||
|
const DEFAULT_KIMI_TEXT_MODEL = 'moonshot-v1-32k';
|
||||||
|
const DEFAULT_KIMI_VISION_MODEL = 'kimi-k2.6';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AiService {
|
export class AiService {
|
||||||
@@ -52,9 +55,10 @@ export class AiService {
|
|||||||
|
|
||||||
const provider = await this.getActiveProvider(actor);
|
const provider = await this.getActiveProvider(actor);
|
||||||
const input = result.data;
|
const input = result.data;
|
||||||
|
const model = this.selectModel(provider, input);
|
||||||
const payload = this.normalizeProviderPayload({
|
const payload = this.normalizeProviderPayload({
|
||||||
...input,
|
...input,
|
||||||
model: provider.modelName || input.model,
|
model,
|
||||||
});
|
});
|
||||||
|
|
||||||
const response = await this.fetchProviderWithRetry(`${provider.endpoint}/chat/completions`, {
|
const response = await this.fetchProviderWithRetry(`${provider.endpoint}/chat/completions`, {
|
||||||
@@ -111,6 +115,45 @@ export class AiService {
|
|||||||
return normalized;
|
return normalized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private selectModel(provider: AiProvider, input: Record<string, unknown>) {
|
||||||
|
const configuredModel = provider.modelName || (typeof input.model === 'string' ? input.model : '');
|
||||||
|
if (!this.isMoonshotProvider(provider)) return configuredModel;
|
||||||
|
|
||||||
|
const hasImages = this.hasImageInput(input.messages);
|
||||||
|
if (hasImages && !this.supportsImageInput(configuredModel)) {
|
||||||
|
return process.env.AI_KIMI_VISION_MODEL || DEFAULT_KIMI_VISION_MODEL;
|
||||||
|
}
|
||||||
|
if (!hasImages && /^kimi-k2(?:[.-]|$)/i.test(configuredModel)) {
|
||||||
|
return process.env.AI_KIMI_TEXT_MODEL || DEFAULT_KIMI_TEXT_MODEL;
|
||||||
|
}
|
||||||
|
return configuredModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
private isMoonshotProvider(provider: AiProvider) {
|
||||||
|
return /moonshot\.cn/i.test(provider.endpoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
private supportsImageInput(model: string) {
|
||||||
|
return /vision|kimi-k2(?:[.-]|$)/i.test(model);
|
||||||
|
}
|
||||||
|
|
||||||
|
private hasImageInput(messages: unknown) {
|
||||||
|
if (!Array.isArray(messages)) return false;
|
||||||
|
return messages.some((message) => {
|
||||||
|
if (typeof message !== 'object' || message === null || !('content' in message)) return false;
|
||||||
|
return this.hasImageContent((message as { content?: unknown }).content);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private hasImageContent(content: unknown): boolean {
|
||||||
|
if (!Array.isArray(content)) return false;
|
||||||
|
return content.some((part) => (
|
||||||
|
typeof part === 'object' &&
|
||||||
|
part !== null &&
|
||||||
|
('image_url' in part || (part as { type?: unknown }).type === 'image_url')
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
private async parseProviderResponse(response: Response) {
|
private async parseProviderResponse(response: Response) {
|
||||||
const text = await response.text();
|
const text = await response.text();
|
||||||
if (!text) return null;
|
if (!text) return null;
|
||||||
@@ -122,10 +165,24 @@ export class AiService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async fetchProvider(url: string, init: RequestInit) {
|
private async fetchProvider(url: string, init: RequestInit) {
|
||||||
|
const timeoutMs = this.providerTimeoutMs();
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
||||||
try {
|
try {
|
||||||
return await fetch(url, init);
|
return await fetch(url, { ...init, signal: controller.signal });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error instanceof Error && error.name === 'AbortError') {
|
||||||
|
throw new HttpException(
|
||||||
|
{
|
||||||
|
code: 'AI_PROVIDER_TIMEOUT',
|
||||||
|
message: `AI 服务响应超时(${Math.round(timeoutMs / 1000)}秒),请稍后重试或缩短报告上下文。`,
|
||||||
|
},
|
||||||
|
504,
|
||||||
|
);
|
||||||
|
}
|
||||||
throw new BadRequestException(`AI 服务连接失败:${error instanceof Error ? error.message : String(error)}`);
|
throw new BadRequestException(`AI 服务连接失败:${error instanceof Error ? error.message : String(error)}`);
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timeout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,6 +227,7 @@ export class AiService {
|
|||||||
|
|
||||||
if (status === 429 && /overloaded/i.test(providerType)) return 'AI_PROVIDER_OVERLOADED';
|
if (status === 429 && /overloaded/i.test(providerType)) return 'AI_PROVIDER_OVERLOADED';
|
||||||
if (status === 429) return 'AI_PROVIDER_RATE_LIMITED';
|
if (status === 429) return 'AI_PROVIDER_RATE_LIMITED';
|
||||||
|
if (status === 504) return 'AI_PROVIDER_TIMEOUT';
|
||||||
if (status >= 500) return 'AI_PROVIDER_UNAVAILABLE';
|
if (status >= 500) return 'AI_PROVIDER_UNAVAILABLE';
|
||||||
return 'AI_PROVIDER_ERROR';
|
return 'AI_PROVIDER_ERROR';
|
||||||
}
|
}
|
||||||
@@ -189,6 +247,11 @@ export class AiService {
|
|||||||
.filter((value) => Number.isFinite(value) && value >= 0);
|
.filter((value) => Number.isFinite(value) && value >= 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private providerTimeoutMs() {
|
||||||
|
const value = Number(process.env.AI_PROVIDER_TIMEOUT_MS);
|
||||||
|
return Number.isFinite(value) && value > 0 ? value : DEFAULT_PROVIDER_TIMEOUT_MS;
|
||||||
|
}
|
||||||
|
|
||||||
private sleep(ms: number) {
|
private sleep(ms: number) {
|
||||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export const DEMO_SYSTEM_SETTINGS = {
|
|||||||
kimi: {
|
kimi: {
|
||||||
endpoint: 'https://api.moonshot.cn/v1',
|
endpoint: 'https://api.moonshot.cn/v1',
|
||||||
apiKey: DEMO_AI_API_KEY,
|
apiKey: DEMO_AI_API_KEY,
|
||||||
modelName: 'kimi-k2.6',
|
modelName: 'moonshot-v1-32k',
|
||||||
},
|
},
|
||||||
deepseek: { endpoint: 'https://api.deepseek.com/v1', apiKey: '', modelName: 'deepseek-chat' },
|
deepseek: { endpoint: 'https://api.deepseek.com/v1', apiKey: '', modelName: 'deepseek-chat' },
|
||||||
openai: { endpoint: 'https://api.openai.com/v1', apiKey: '', modelName: 'gpt-4o' },
|
openai: { endpoint: 'https://api.openai.com/v1', apiKey: '', modelName: 'gpt-4o' },
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import { PrismaService } from '../prisma/prisma.service.js';
|
|||||||
import { systemSettingsSchema, type SystemSettingsInput } from './settings.schemas.js';
|
import { systemSettingsSchema, type SystemSettingsInput } from './settings.schemas.js';
|
||||||
|
|
||||||
const DEFAULT_AI_PROVIDERS = {
|
const DEFAULT_AI_PROVIDERS = {
|
||||||
kimi: { endpoint: 'https://api.moonshot.cn/v1', apiKey: DEMO_SYSTEM_SETTINGS.aiProviders.kimi.apiKey, modelName: 'kimi-k2.6' },
|
kimi: { endpoint: 'https://api.moonshot.cn/v1', apiKey: DEMO_SYSTEM_SETTINGS.aiProviders.kimi.apiKey, modelName: 'moonshot-v1-32k' },
|
||||||
deepseek: { endpoint: 'https://api.deepseek.com/v1', apiKey: '', modelName: 'deepseek-chat' },
|
deepseek: { endpoint: 'https://api.deepseek.com/v1', apiKey: '', modelName: 'deepseek-chat' },
|
||||||
openai: { endpoint: 'https://api.openai.com/v1', apiKey: '', modelName: 'gpt-4o' },
|
openai: { endpoint: 'https://api.openai.com/v1', apiKey: '', modelName: 'gpt-4o' },
|
||||||
custom: { endpoint: '', apiKey: '', modelName: '' },
|
custom: { endpoint: '', apiKey: '', modelName: '' },
|
||||||
|
|||||||
@@ -53,6 +53,9 @@ const getAiErrorMessage = (error: unknown) => {
|
|||||||
if (error.status === 429 || error.code === 'AI_PROVIDER_OVERLOADED' || error.code === 'AI_PROVIDER_RATE_LIMITED') {
|
if (error.status === 429 || error.code === 'AI_PROVIDER_OVERLOADED' || error.code === 'AI_PROVIDER_RATE_LIMITED') {
|
||||||
return 'AI 服务当前繁忙或请求过多,请稍后重试。';
|
return 'AI 服务当前繁忙或请求过多,请稍后重试。';
|
||||||
}
|
}
|
||||||
|
if (error.status === 504 || error.code === 'AI_PROVIDER_TIMEOUT') {
|
||||||
|
return 'AI 服务响应超时,请稍后重试,或缩短报告上下文后再试。';
|
||||||
|
}
|
||||||
if (error.status >= 500 || error.code === 'AI_PROVIDER_UNAVAILABLE') {
|
if (error.status >= 500 || error.code === 'AI_PROVIDER_UNAVAILABLE') {
|
||||||
return 'AI 服务暂时不可用,请稍后重试或切换其他模型。';
|
return 'AI 服务暂时不可用,请稍后重试或切换其他模型。';
|
||||||
}
|
}
|
||||||
@@ -1331,7 +1334,7 @@ export default function ReportEditor() {
|
|||||||
try {
|
try {
|
||||||
const settings = storage.get<SystemSettings>('systemSettings', {} as SystemSettings);
|
const settings = storage.get<SystemSettings>('systemSettings', {} as SystemSettings);
|
||||||
const provider = settings.aiProviders?.[settings.activeAiProvider || 'kimi'];
|
const provider = settings.aiProviders?.[settings.activeAiProvider || 'kimi'];
|
||||||
const modelName = provider?.modelName || 'kimi-k2.6';
|
const modelName = provider?.modelName || 'moonshot-v1-32k';
|
||||||
let actualTargetId = aiTargetRegion;
|
let actualTargetId = aiTargetRegion;
|
||||||
if (aiModifyEnabled && actualTargetId === 'none') {
|
if (aiModifyEnabled && actualTargetId === 'none') {
|
||||||
const availableRegions = checkAiRegions();
|
const availableRegions = checkAiRegions();
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ export default function SystemSettings() {
|
|||||||
providers.kimi = {
|
providers.kimi = {
|
||||||
endpoint: (savedSettings as any).kimiApiEndpoint || providers.kimi.endpoint,
|
endpoint: (savedSettings as any).kimiApiEndpoint || providers.kimi.endpoint,
|
||||||
apiKey: (savedSettings as any).kimiApiKey || '',
|
apiKey: (savedSettings as any).kimiApiKey || '',
|
||||||
modelName: 'kimi-k2.6'
|
modelName: 'moonshot-v1-32k'
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
savedSettings.aiProviders = providers;
|
savedSettings.aiProviders = providers;
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ export interface SystemSettings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const DEFAULT_AI_PROVIDERS: Record<string, AiProviderConfig> = {
|
export const DEFAULT_AI_PROVIDERS: Record<string, AiProviderConfig> = {
|
||||||
kimi: { endpoint: 'https://api.moonshot.cn/v1', apiKey: '', modelName: 'kimi-k2.6' },
|
kimi: { endpoint: 'https://api.moonshot.cn/v1', apiKey: '', modelName: 'moonshot-v1-32k' },
|
||||||
deepseek: { endpoint: 'https://api.deepseek.com/v1', apiKey: '', modelName: 'deepseek-chat' },
|
deepseek: { endpoint: 'https://api.deepseek.com/v1', apiKey: '', modelName: 'deepseek-chat' },
|
||||||
openai: { endpoint: 'https://api.openai.com/v1', apiKey: '', modelName: 'gpt-4o' },
|
openai: { endpoint: 'https://api.openai.com/v1', apiKey: '', modelName: 'gpt-4o' },
|
||||||
custom: { endpoint: '', apiKey: '', modelName: '' }
|
custom: { endpoint: '', apiKey: '', modelName: '' }
|
||||||
|
|||||||
Reference in New Issue
Block a user