-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathget-message-content.ts
More file actions
36 lines (31 loc) · 973 Bytes
/
get-message-content.ts
File metadata and controls
36 lines (31 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import type { Message } from 'types/pipe';
export function getMessageContent(message: Message): string | null {
// Tool calls have no content
if (!message?.content) return null;
// If content is a string, return it
if (typeof message.content === 'string') {
return message.content;
}
/**
* If content is an array, find the text content part and return its text
*
* 1. Image messages have text and image content objects
* {"type": "text", "text": "What’s in this image?"},
* {
* "type": "image_url",
* "image_url": {
* "url": "",
* },
* },
*
* 2. Audio messages always have text and audio content objects
* content: [
* {type: 'text', text: 'What is in this recording?'},
* {type: 'input_audio', input_audio: {data: base64str, format: 'wav'}},
* ];
*/
if (Array.isArray(message.content)) {
return message.content.find(item => item.type === 'text')?.text || null;
}
return null;
}