-
Notifications
You must be signed in to change notification settings - Fork 555
Expand file tree
/
Copy pathchat.ts
More file actions
39 lines (31 loc) · 1.1 KB
/
chat.ts
File metadata and controls
39 lines (31 loc) · 1.1 KB
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
37
38
39
import { startsWith } from './startsWith';
import type { ChatMessageBase, ChatToolMessage } from '../../components';
export const getTextContent = (message: ChatMessageBase) => {
return message.parts
.map((part) => ('text' in part ? part.text : ''))
.join('');
};
export const hasTextContent = (message: ChatMessageBase) => {
return getTextContent(message).trim() !== '';
};
export const getToolParts = (message: ChatMessageBase) => {
return message.parts.filter((part) => startsWith(part.type, 'tool-'));
};
export const hasToolParts = (message: ChatMessageBase) => {
return getToolParts(message).length > 0;
};
export const isPartText = (
part: ChatMessageBase['parts'][number]
): part is Extract<ChatMessageBase['parts'][number], { type: 'text' }> => {
return part.type === 'text';
};
export const isPartTool = (
part: ChatMessageBase['parts'][number]
): part is ChatToolMessage => {
return startsWith(part.type, 'tool-');
};
export const toolHasOutput = (message: ChatMessageBase) => {
return message.parts.some(
(part) => isPartTool(part) && part.state === 'output-available'
);
};