-
-
Notifications
You must be signed in to change notification settings - Fork 10k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ refactor: refactor the aiChat slice actions (#4542)
* ✅ test: add test for files prompts * ♻️ refactor: refactor the aiChat store
- Loading branch information
Showing
9 changed files
with
161 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { ChatFileItem, ChatImageItem } from '@/types/message'; | ||
|
||
import { filesPrompts } from './index'; | ||
|
||
describe('filesPrompts', () => { | ||
// 创建测试用的示例数据 | ||
const mockImage: ChatImageItem = { | ||
id: 'img-1', | ||
alt: 'test image', | ||
url: 'https://example.com/image.jpg', | ||
}; | ||
|
||
const mockFile: ChatFileItem = { | ||
id: 'file-1', | ||
name: 'test.pdf', | ||
fileType: 'application/pdf', | ||
size: 1024, | ||
url: 'https://example.com/test.pdf', | ||
}; | ||
|
||
it('should generate prompt with only images', () => { | ||
const result = filesPrompts({ | ||
imageList: [mockImage], | ||
fileList: undefined, | ||
}); | ||
|
||
expect(result).toEqual( | ||
`<files_info> | ||
<images> | ||
<images_docstring>here are user upload images you can refer to</images_docstring> | ||
<image name="test image" url="https://example.com/image.jpg"></image> | ||
</images> | ||
</files_info>`, | ||
); | ||
}); | ||
|
||
it('should generate prompt with only files', () => { | ||
const result = filesPrompts({ | ||
imageList: [], | ||
fileList: [mockFile], | ||
}); | ||
|
||
expect(result).toEqual( | ||
`<files_info> | ||
<files> | ||
<files_docstring>here are user upload files you can refer to</files_docstring> | ||
<file id="file-1" name="test.pdf" type="application/pdf" size="1024" url="https://example.com/test.pdf"></file> | ||
</files> | ||
</files_info>`, | ||
); | ||
}); | ||
|
||
it('should generate prompt with both images and files', () => { | ||
const result = filesPrompts({ | ||
imageList: [mockImage], | ||
fileList: [mockFile], | ||
}); | ||
|
||
expect(result).toEqual( | ||
`<files_info> | ||
<images> | ||
<images_docstring>here are user upload images you can refer to</images_docstring> | ||
<image name="test image" url="https://example.com/image.jpg"></image> | ||
</images> | ||
<files> | ||
<files_docstring>here are user upload files you can refer to</files_docstring> | ||
<file id="file-1" name="test.pdf" type="application/pdf" size="1024" url="https://example.com/test.pdf"></file> | ||
</files> | ||
</files_info>`, | ||
); | ||
}); | ||
|
||
it('should generate prompt with empty lists', () => { | ||
const result = filesPrompts({ | ||
imageList: [], | ||
fileList: [], | ||
}); | ||
|
||
expect(result).toEqual(''); | ||
}); | ||
|
||
it('should handle multiple images and files', () => { | ||
const images: ChatImageItem[] = [ | ||
mockImage, | ||
{ | ||
id: 'img-2', | ||
alt: 'second image', | ||
url: 'https://example.com/image2.jpg', | ||
}, | ||
]; | ||
|
||
const files: ChatFileItem[] = [ | ||
mockFile, | ||
{ | ||
id: 'file-2', | ||
name: 'document.docx', | ||
fileType: 'application/docx', | ||
size: 2048, | ||
url: 'https://example.com/document.docx', | ||
}, | ||
]; | ||
|
||
const result = filesPrompts({ | ||
imageList: images, | ||
fileList: files, | ||
}); | ||
|
||
expect(result).toContain('second image'); | ||
expect(result).toContain('document.docx'); | ||
expect(result).toMatch(/<image.*?>.*<image.*?>/s); // Check for multiple image tags | ||
expect(result).toMatch(/<file.*?>.*<file.*?>/s); // Check for multiple file tags | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { StateCreator } from 'zustand/vanilla'; | ||
|
||
import { ChatStore } from '@/store/chat/store'; | ||
|
||
import { AIGenerateAction, generateAIChat } from './generateAIChat'; | ||
import { ChatRAGAction, chatRag } from './rag'; | ||
|
||
export interface ChatAIChatAction extends ChatRAGAction, AIGenerateAction { | ||
/**/ | ||
} | ||
|
||
export const chatAiChat: StateCreator< | ||
ChatStore, | ||
[['zustand/devtools', never]], | ||
[], | ||
ChatAIChatAction | ||
> = (...params) => ({ | ||
...chatRag(...params), | ||
...generateAIChat(...params), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters