Skip to content

Commit 4b1224f

Browse files
committed
Add ChatAgent
1 parent 43cfbe4 commit 4b1224f

2 files changed

Lines changed: 62 additions & 16 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Foundation
2+
3+
public enum ChatAgentResponse {
4+
/// Post the status of the current message.
5+
case status(String)
6+
/// Send a token of the text message to the current message.
7+
case contentToken(String)
8+
/// Update the attachments of the current message.
9+
case attachments([URL])
10+
/// Update the references of the current message.
11+
case references([ChatMessage.Reference])
12+
/// End the message. The next contents will be sent as a new message.
13+
case finishMessage
14+
}
15+
16+
public struct ChatAgentRequest {
17+
public var text: String
18+
public var history: [ChatMessage]
19+
public var extraContext: String
20+
21+
public init(text: String, history: [ChatMessage], extraContext: String) {
22+
self.text = text
23+
self.history = history
24+
self.extraContext = extraContext
25+
}
26+
}
27+
28+
public protocol ChatAgent {
29+
typealias Response = ChatAgentResponse
30+
typealias Request = ChatAgentRequest
31+
func send(_ request: Request) async -> AsyncThrowingStream<Response, any Error>
32+
}
33+

Tool/Sources/ChatBasic/ChatMessage.swift

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,24 @@ public struct ChatMessage: Equatable, Codable {
4747
}
4848

4949
public struct Reference: Codable, Equatable {
50-
public enum Kind: String, Codable {
51-
case `class`
52-
case `struct`
53-
case `enum`
54-
case `actor`
55-
case `protocol`
56-
case `extension`
57-
case `case`
58-
case property
59-
case `typealias`
60-
case function
61-
case method
50+
public enum Kind: Codable, Equatable {
51+
public enum Symbol: String, Codable {
52+
case `class`
53+
case `struct`
54+
case `enum`
55+
case `actor`
56+
case `protocol`
57+
case `extension`
58+
case `case`
59+
case property
60+
case `typealias`
61+
case function
62+
case method
63+
}
64+
case symbol(Symbol)
6265
case text
6366
case webpage
64-
case other
67+
case other(String)
6568
}
6669

6770
public var title: String
@@ -78,8 +81,8 @@ public struct ChatMessage: Equatable, Codable {
7881
subTitle: String,
7982
content: String,
8083
uri: String,
81-
startLine: Int?,
82-
endLine: Int?,
84+
startLine: Int? = nil,
85+
endLine: Int? = nil,
8386
kind: Kind
8487
) {
8588
self.title = title
@@ -116,6 +119,12 @@ public struct ChatMessage: Equatable, Codable {
116119

117120
/// The id of the message.
118121
public var id: ID
122+
123+
/// The id of the sender of the message.
124+
public var senderId: String?
125+
126+
/// The id of the message that this message is a response to.
127+
public var responseTo: ID?
119128

120129
/// The number of tokens of this message.
121130
public var tokensCount: Int?
@@ -134,6 +143,8 @@ public struct ChatMessage: Equatable, Codable {
134143

135144
public init(
136145
id: String = UUID().uuidString,
146+
senderId: String? = nil,
147+
repinesToo: String? = nil,
137148
role: Role,
138149
content: String?,
139150
name: String? = nil,
@@ -143,6 +154,8 @@ public struct ChatMessage: Equatable, Codable {
143154
references: [Reference] = []
144155
) {
145156
self.role = role
157+
self.senderId = senderId
158+
self.responseTo = responseTo
146159
self.content = content
147160
self.name = name
148161
self.toolCalls = toolCalls
@@ -154,7 +167,7 @@ public struct ChatMessage: Equatable, Codable {
154167
}
155168

156169
public struct ReferenceKindFallback: FallbackValueProvider {
157-
public static var defaultValue: ChatMessage.Reference.Kind { .other }
170+
public static var defaultValue: ChatMessage.Reference.Kind { .other("Unknown") }
158171
}
159172

160173
public struct ChatMessageRoleFallback: FallbackValueProvider {

0 commit comments

Comments
 (0)