Fix AI workflow retry loops and improve fallback handling

This commit is contained in:
2026-02-28 22:01:07 -05:00
parent 3cc085e583
commit 953d7c0099
3 changed files with 158 additions and 2 deletions

View File

@@ -119,10 +119,12 @@ describe('ai config and runtime', () => {
system?: string;
prompt: string;
temperature: number;
maxRetries?: number;
}) => {
expect(input.system).toBe('Use concise style');
expect(input.prompt).toBe('Analyze this filing');
expect(input.temperature).toBe(0.4);
expect(input.maxRetries).toBe(0);
return { text: ' Generated insight ' };
});
@@ -216,4 +218,63 @@ describe('ai config and runtime', () => {
expect(result.model).toBe('qwen3:8b');
expect(result.text).toContain('AI SDK fallback mode is active');
});
it('falls back to local text when report workload fails with insufficient balance', async () => {
const warn = mock((_message: string) => {});
const result = await runAiAnalysis('Analyze this filing', 'Use concise style', {
env: {
ZHIPU_API_KEY: 'new-key'
},
warn,
createModel: () => ({}),
generate: async () => {
throw new Error('AI_RetryError: Failed after 3 attempts. Last error: Insufficient balance or no resource package. Please recharge.');
}
});
expect(result.provider).toBe('local-fallback');
expect(result.model).toBe('glm-4.7-flashx');
expect(result.text).toContain('AI SDK fallback mode is active');
expect(warn).toHaveBeenCalledTimes(1);
});
it('falls back to local text when report workload cause contains insufficient balance', async () => {
const warn = mock((_message: string) => {});
const result = await runAiAnalysis('Analyze this filing', 'Use concise style', {
env: {
ZHIPU_API_KEY: 'new-key'
},
warn,
createModel: () => ({}),
generate: async () => {
const retryError = new Error('AI_RetryError: Failed after 3 attempts.');
(retryError as Error & { cause?: unknown }).cause = new Error(
'Last error: Insufficient balance or no resource package. Please recharge.'
);
throw retryError;
}
});
expect(result.provider).toBe('local-fallback');
expect(result.model).toBe('glm-4.7-flashx');
expect(result.text).toContain('AI SDK fallback mode is active');
expect(warn).toHaveBeenCalledTimes(1);
});
it('keeps throwing unknown report workload errors', async () => {
await expect(
runAiAnalysis('Analyze this filing', 'Use concise style', {
env: {
ZHIPU_API_KEY: 'new-key'
},
warn: () => {},
createModel: () => ({}),
generate: async () => {
throw new Error('unexpected schema mismatch');
}
})
).rejects.toThrow('unexpected schema mismatch');
});
});