Migrate AI runtime to SDK and hardcode Zhipu coding endpoint
This commit is contained in:
192
lib/server/ai.test.ts
Normal file
192
lib/server/ai.test.ts
Normal file
@@ -0,0 +1,192 @@
|
||||
import { beforeEach, describe, expect, it, mock } from 'bun:test';
|
||||
import {
|
||||
__resetAiWarningsForTests,
|
||||
getAiConfig,
|
||||
runAiAnalysis
|
||||
} from './ai';
|
||||
|
||||
type EnvSource = Record<string, string | undefined>;
|
||||
const CODING_API_BASE_URL = 'https://api.z.ai/api/coding/paas/v4';
|
||||
|
||||
describe('ai config and runtime', () => {
|
||||
beforeEach(() => {
|
||||
__resetAiWarningsForTests();
|
||||
});
|
||||
|
||||
it('uses coding endpoint defaults when optional env values are missing', () => {
|
||||
const config = getAiConfig({
|
||||
env: {
|
||||
ZHIPU_API_KEY: 'key'
|
||||
},
|
||||
warn: () => {}
|
||||
});
|
||||
|
||||
expect(config.apiKey).toBe('key');
|
||||
expect(config.baseUrl).toBe(CODING_API_BASE_URL);
|
||||
expect(config.model).toBe('glm-4.7-flashx');
|
||||
expect(config.temperature).toBe(0.2);
|
||||
});
|
||||
|
||||
it('ignores ZHIPU_BASE_URL and keeps the hardcoded coding endpoint', () => {
|
||||
const config = getAiConfig({
|
||||
env: {
|
||||
ZHIPU_API_KEY: 'key',
|
||||
ZHIPU_BASE_URL: 'https://api.z.ai/api/paas/v4'
|
||||
},
|
||||
warn: () => {}
|
||||
});
|
||||
|
||||
expect(config.baseUrl).toBe(CODING_API_BASE_URL);
|
||||
});
|
||||
|
||||
it('clamps temperature into [0, 2]', () => {
|
||||
const negative = getAiConfig({
|
||||
env: {
|
||||
ZHIPU_API_KEY: 'key',
|
||||
AI_TEMPERATURE: '-2'
|
||||
},
|
||||
warn: () => {}
|
||||
});
|
||||
expect(negative.temperature).toBe(0);
|
||||
|
||||
const high = getAiConfig({
|
||||
env: {
|
||||
ZHIPU_API_KEY: 'key',
|
||||
AI_TEMPERATURE: '9'
|
||||
},
|
||||
warn: () => {}
|
||||
});
|
||||
expect(high.temperature).toBe(2);
|
||||
|
||||
const invalid = getAiConfig({
|
||||
env: {
|
||||
ZHIPU_API_KEY: 'key',
|
||||
AI_TEMPERATURE: 'not-a-number'
|
||||
},
|
||||
warn: () => {}
|
||||
});
|
||||
expect(invalid.temperature).toBe(0.2);
|
||||
});
|
||||
|
||||
it('returns fallback output when ZHIPU_API_KEY is missing', async () => {
|
||||
const generate = mock(async () => ({ text: 'should-not-be-used' }));
|
||||
|
||||
const result = await runAiAnalysis(
|
||||
'Prompt line one\nPrompt line two',
|
||||
'System prompt',
|
||||
{
|
||||
env: {},
|
||||
warn: () => {},
|
||||
generate
|
||||
}
|
||||
);
|
||||
|
||||
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(generate).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('warns once when deprecated OPENCLAW_* env vars are present', () => {
|
||||
const warn = mock((_message: string) => {});
|
||||
|
||||
const env: EnvSource = {
|
||||
OPENCLAW_API_KEY: 'legacy-key',
|
||||
OPENCLAW_BASE_URL: 'http://legacy.local',
|
||||
ZHIPU_API_KEY: 'new-key'
|
||||
};
|
||||
|
||||
getAiConfig({ env, warn });
|
||||
getAiConfig({ env, warn });
|
||||
|
||||
expect(warn).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('warns once when ZHIPU_BASE_URL is set because coding endpoint is hardcoded', () => {
|
||||
const warn = mock((_message: string) => {});
|
||||
|
||||
const env: EnvSource = {
|
||||
ZHIPU_API_KEY: 'new-key',
|
||||
ZHIPU_BASE_URL: 'https://api.z.ai/api/paas/v4'
|
||||
};
|
||||
|
||||
getAiConfig({ env, warn });
|
||||
getAiConfig({ env, warn });
|
||||
|
||||
expect(warn).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('does not consume OPENCLAW_* values for live generation', async () => {
|
||||
const generate = mock(async () => ({ text: 'should-not-be-used' }));
|
||||
const warn = mock((_message: string) => {});
|
||||
|
||||
const result = await runAiAnalysis('Legacy-only env prompt', undefined, {
|
||||
env: {
|
||||
OPENCLAW_API_KEY: 'legacy-key',
|
||||
OPENCLAW_MODEL: 'legacy-model'
|
||||
},
|
||||
warn,
|
||||
generate
|
||||
});
|
||||
|
||||
expect(result.provider).toBe('local-fallback');
|
||||
expect(result.model).toBe('glm-4.7-flashx');
|
||||
expect(generate).not.toHaveBeenCalled();
|
||||
expect(warn).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('uses configured ZHIPU values and injected generator when API key exists', async () => {
|
||||
const createModel = mock((config: {
|
||||
apiKey?: string;
|
||||
model: string;
|
||||
baseUrl: string;
|
||||
temperature: number;
|
||||
}) => {
|
||||
expect(config.apiKey).toBe('new-key');
|
||||
expect(config.baseUrl).toBe(CODING_API_BASE_URL);
|
||||
expect(config.model).toBe('glm-4-plus');
|
||||
expect(config.temperature).toBe(0.4);
|
||||
return { modelId: config.model };
|
||||
});
|
||||
const generate = mock(async (input: {
|
||||
model: unknown;
|
||||
system?: string;
|
||||
prompt: string;
|
||||
temperature: number;
|
||||
}) => {
|
||||
expect(input.system).toBe('Use concise style');
|
||||
expect(input.prompt).toBe('Analyze this filing');
|
||||
expect(input.temperature).toBe(0.4);
|
||||
return { text: ' Generated insight ' };
|
||||
});
|
||||
|
||||
const result = await runAiAnalysis('Analyze this filing', 'Use concise style', {
|
||||
env: {
|
||||
ZHIPU_API_KEY: 'new-key',
|
||||
ZHIPU_MODEL: 'glm-4-plus',
|
||||
ZHIPU_BASE_URL: 'https://api.z.ai/api/paas/v4',
|
||||
AI_TEMPERATURE: '0.4'
|
||||
},
|
||||
warn: () => {},
|
||||
createModel,
|
||||
generate
|
||||
});
|
||||
|
||||
expect(createModel).toHaveBeenCalledTimes(1);
|
||||
expect(generate).toHaveBeenCalledTimes(1);
|
||||
expect(result.provider).toBe('zhipu');
|
||||
expect(result.model).toBe('glm-4-plus');
|
||||
expect(result.text).toBe('Generated insight');
|
||||
});
|
||||
|
||||
it('throws when AI generation returns an empty response', async () => {
|
||||
await expect(
|
||||
runAiAnalysis('Analyze this filing', undefined, {
|
||||
env: { ZHIPU_API_KEY: 'new-key' },
|
||||
warn: () => {},
|
||||
createModel: () => ({}),
|
||||
generate: async () => ({ text: ' ' })
|
||||
})
|
||||
).rejects.toThrow('AI SDK returned an empty response');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user