Skip to content

Commit db8e866

Browse files
committed
feat(kit): message support media types
1 parent d29a3cd commit db8e866

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

packages/kit/src/types.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,69 @@
33
*/
44
export type MessageRole = 'system' | 'user' | 'assistant'
55

6+
interface TextInput {
7+
type: 'input_text'
8+
text: string
9+
}
10+
11+
interface ImageInput {
12+
type: 'image_url'
13+
image_url: {
14+
/**
15+
* 图片base64数据或图片url
16+
* 需要注意,传入Base64,图像格式(即image/{format})需要与支持的图片列表中的Content Type保持一致。
17+
* PNG图像: data:image/png;base64,${base64Image}
18+
* JPEG图像: data:image/jpeg;base64,${base64Image}
19+
* WEBP图像: data:image/webp;base64,${base64Image}
20+
*/
21+
url: string // 图片base64数据或图片url
22+
}
23+
[extra: string]: unknown
24+
}
25+
26+
interface FileInput {
27+
type: 'input_file'
28+
filename: string
29+
file_data: string // 文件base64数据
30+
[extra: string]: unknown
31+
}
32+
33+
interface AudioInput {
34+
type: 'input_audio'
35+
input_audio: {
36+
/**
37+
* 音频base64数据或音频url
38+
* 例如:data:;base64,${base64Audio}
39+
*/
40+
data: string
41+
format: string // 音频格式,如 'mp3'
42+
}
43+
}
44+
45+
interface VideoInput {
46+
type: 'input_video'
47+
video_url: {
48+
/**
49+
* 视频base64数据或视频url
50+
* 例如:data:;base64,${base64Video}
51+
*/
52+
url: string
53+
}
54+
}
55+
56+
/**
57+
* 多模态消息内容
58+
*/
59+
type MediaContent = Array<TextInput | ImageInput | AudioInput | VideoInput | FileInput>
60+
61+
export type MessageContent = string | MediaContent
62+
663
/**
764
* 聊天消息接口
865
*/
966
export interface ChatMessage {
1067
role: MessageRole
11-
content: string
68+
content: MessageContent
1269
name?: string
1370
}
1471

packages/kit/src/vue/message/useMessage.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import { reactive, Reactive, ref, toRaw, type Ref } from 'vue'
7-
import type { ChatMessage } from '../../types'
7+
import type { ChatMessage, MessageContent } from '../../types'
88
import type { AIClient } from '../../client'
99

1010
export enum STATUS {
@@ -53,7 +53,7 @@ export interface UseMessageReturn {
5353
/** 是否使用流式响应 */
5454
useStream: Ref<boolean>
5555
/** 发送消息 */
56-
sendMessage: (content?: string, clearInput?: boolean) => Promise<void>
56+
sendMessage: (content?: MessageContent, clearInput?: boolean) => Promise<void>
5757
/** 清空消息 */
5858
clearMessages: () => void
5959
/** 添加消息 */
@@ -167,8 +167,9 @@ export function useMessage(options: UseMessageOptions): UseMessageReturn {
167167
}
168168

169169
// 发送消息
170-
const sendMessage = async (content: string = inputMessage.value, clearInput: boolean = true) => {
171-
if (!content?.trim() || GeneratingStatus.includes(messageState.status)) {
170+
const sendMessage = async (content: MessageContent = inputMessage.value, clearInput: boolean = true) => {
171+
const isEmptyContent = (typeof content === 'string' && !content.trim()) || content.length === 0
172+
if (isEmptyContent || GeneratingStatus.includes(messageState.status)) {
172173
return
173174
}
174175

0 commit comments

Comments
 (0)