-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathChat.swift
More file actions
137 lines (112 loc) · 3.33 KB
/
Chat.swift
File metadata and controls
137 lines (112 loc) · 3.33 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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// SPDX-FileCopyrightText: Nextcloud GmbH
// SPDX-FileCopyrightText: 2026 Milen Pivchev
// SPDX-License-Identifier: GPL-3.0-or-later
import Foundation
// MARK: - ChatMessage
public struct AssistantChatMessage: Codable, Identifiable, Equatable {
public let id: Int
public let sessionId: Int
public let role: String
public let content: String
public let timestamp: Int
public var isFromHuman: Bool {
role == "human"
}
public init(id: Int, sessionId: Int, role: String, content: String, timestamp: Int) {
self.id = id
self.sessionId = sessionId
self.role = role
self.content = content
self.timestamp = timestamp
}
enum CodingKeys: String, CodingKey {
case id
case sessionId = "session_id"
case role
case content
case timestamp
}
}
// MARK: - ChatMessageRequest
public struct AssistantChatMessageRequest: Encodable {
public let sessionId: Int
public let role: String
public let content: String
public let timestamp: Int
public let firstHumanMessage: Bool
public init(sessionId: Int, role: String, content: String, timestamp: Int, firstHumanMessage: Bool) {
self.sessionId = sessionId
self.role = role
self.content = content
self.timestamp = timestamp
self.firstHumanMessage = firstHumanMessage
}
var bodyMap: [String: Any] {
return [
"sessionId": sessionId,
"role": role,
"content": content,
"timestamp": timestamp
]
}
enum CodingKeys: String, CodingKey {
case sessionId
case role
case content
case timestamp
case firstHumanMessage
}
}
// MARK: - Session
public struct AssistantConversation: Codable, Equatable, Hashable {
public let id: Int
public let userId: String?
private let title: String?
public let timestamp: Int
enum CodingKeys: String, CodingKey {
case id
case userId = "user_id"
case title
case timestamp
}
public var validTitle: String {
return title ?? createTitle()
func createTitle() -> String {
let date = Date(timeIntervalSince1970: TimeInterval(timestamp))
let formatter = DateFormatter()
formatter.locale = .current
formatter.timeZone = .current
formatter.dateFormat = "MMM d yyyy, HH:mm"
return formatter.string(from: date)
}
}
}
// MARK: - CreateConversation
public struct AssistantCreatedConversation: Codable, Equatable {
public let conversation: AssistantConversation
enum CodingKeys: String, CodingKey {
case conversation = "session"
}
}
// MARK: - Session
public struct AssistantSession: Codable, Equatable {
public let messageTaskId: Int?
public let titleTaskId: Int?
public let sessionTitle: String?
public let sessionAgencyPendingActions: String?
public let taskId: Int?
enum CodingKeys: String, CodingKey {
case messageTaskId
case titleTaskId
case sessionTitle
case sessionAgencyPendingActions
case taskId
}
}
// MARK: - SessionTask
public struct AssistantSessionTask: Codable, Equatable {
public let taskId: Int
enum CodingKeys: String, CodingKey {
case taskId
}
}