-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadMessageBuilder.ts
74 lines (65 loc) · 1.58 KB
/
threadMessageBuilder.ts
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import {
ChatCompletionRole,
ContentType,
MessageStatus,
ThreadContent,
ThreadMessage,
} from '@janhq/core'
import { FileInfo } from '@/containers/Providers/Jotai'
import { MessageRequestBuilder } from './messageRequestBuilder'
export class ThreadMessageBuilder {
messageRequest: MessageRequestBuilder
content: ThreadContent[] = []
constructor(messageRequest: MessageRequestBuilder) {
this.messageRequest = messageRequest
}
build(): ThreadMessage {
const timestamp = Date.now()
return {
id: this.messageRequest.msgId,
thread_id: this.messageRequest.thread.id,
role: ChatCompletionRole.User,
status: MessageStatus.Ready,
created: timestamp,
updated: timestamp,
object: 'thread.message',
content: this.content,
}
}
pushMessage(
prompt: string,
base64: string | undefined,
fileUpload: FileInfo[]
) {
if (base64 && fileUpload[0]?.type === 'image') {
this.content.push({
type: ContentType.Image,
text: {
value: prompt,
annotations: [base64],
},
})
}
if (base64 && fileUpload[0]?.type === 'pdf') {
this.content.push({
type: ContentType.Pdf,
text: {
value: prompt,
annotations: [base64],
name: fileUpload[0].file.name,
size: fileUpload[0].file.size,
},
})
}
if (prompt && !base64) {
this.content.push({
type: ContentType.Text,
text: {
value: prompt,
annotations: [],
},
})
}
return this
}
}