| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- export function createLLMMock(options: LLMMockOptions = {}) {
- const { shouldFail = false, delayMs = 0, response } = options;
- return {
- name: 'mock-llm' as const,
- vendor: 'mock' as const,
- mode: 'mock' as const,
- invoke: vi.fn().mockImplementation(async (input: string) => {
- if (delayMs > 0) {
- await new Promise((resolve) => setTimeout(resolve, delayMs));
- }
- if (shouldFail) {
- throw new Error('Mock LLM failure');
- }
- return {
- content: response || JSON.stringify({
- title: '测试书籍标题',
- outline: [
- { chapter: 1, title: '第一章', summary: '章节摘要' }
- ]
- })
- };
- }),
- stream: vi.fn().mockImplementation(async function* (input: string) {
- if (delayMs > 0) {
- await new Promise((resolve) => setTimeout(resolve, delayMs));
- }
- if (shouldFail) {
- throw new Error('Mock LLM failure');
- }
- yield { content: '模拟流式输出' };
- }),
- generate: vi.fn().mockImplementation(async (prompt: string) => {
- if (shouldFail) {
- throw new Error('Mock LLM failure');
- }
- return response || 'Mock response';
- }),
- };
- }
- export interface LLMMockOptions {
- shouldFail?: boolean;
- delayMs?: number;
- response?: string;
- }
- export const defaultLLMResponse = {
- title: 'AI发展史',
- outline: [
- { chapter: 1, title: '第一章:起源', summary: '讲述AI的起源' },
- { chapter: 2, title: '第二章:发展', summary: '讲述AI的发展' },
- ]
- };
|