Skip to content

Commit ba7a741

Browse files
committed
Add RAGChatAgent
1 parent 4b1224f commit ba7a741

File tree

6 files changed

+201
-52
lines changed

6 files changed

+201
-52
lines changed

Core/Sources/ChatGPTChatTab/ChatPanel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ struct ChatPanel_Preview: PreviewProvider {
504504
subtitle: "Hi Hi Hi Hi",
505505
uri: "https://google.com",
506506
startLine: nil,
507-
kind: .class
507+
kind: .symbol(.class)
508508
),
509509
]
510510
),

Core/Sources/ChatGPTChatTab/Views/BotMessage.swift

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -139,28 +139,31 @@ struct ReferenceIcon: View {
139139
RoundedRectangle(cornerRadius: 4)
140140
.fill({
141141
switch kind {
142-
case .class:
143-
Color.purple
144-
case .struct:
145-
Color.purple
146-
case .enum:
147-
Color.purple
148-
case .actor:
149-
Color.purple
150-
case .protocol:
151-
Color.purple
152-
case .extension:
153-
Color.indigo
154-
case .case:
155-
Color.green
156-
case .property:
157-
Color.teal
158-
case .typealias:
159-
Color.orange
160-
case .function:
161-
Color.teal
162-
case .method:
163-
Color.blue
142+
case .symbol(let symbol):
143+
switch symbol {
144+
case .class:
145+
Color.purple
146+
case .struct:
147+
Color.purple
148+
case .enum:
149+
Color.purple
150+
case .actor:
151+
Color.purple
152+
case .protocol:
153+
Color.purple
154+
case .extension:
155+
Color.indigo
156+
case .case:
157+
Color.green
158+
case .property:
159+
Color.teal
160+
case .typealias:
161+
Color.orange
162+
case .function:
163+
Color.teal
164+
case .method:
165+
Color.blue
166+
}
164167
case .text:
165168
Color.gray
166169
case .webpage:
@@ -173,28 +176,31 @@ struct ReferenceIcon: View {
173176
.overlay(alignment: .center) {
174177
Group {
175178
switch kind {
176-
case .class:
177-
Text("C")
178-
case .struct:
179-
Text("S")
180-
case .enum:
181-
Text("E")
182-
case .actor:
183-
Text("A")
184-
case .protocol:
185-
Text("Pr")
186-
case .extension:
187-
Text("Ex")
188-
case .case:
189-
Text("K")
190-
case .property:
191-
Text("P")
192-
case .typealias:
193-
Text("T")
194-
case .function:
195-
Text("𝑓")
196-
case .method:
197-
Text("M")
179+
case .symbol(let symbol):
180+
switch symbol {
181+
case .class:
182+
Text("C")
183+
case .struct:
184+
Text("S")
185+
case .enum:
186+
Text("E")
187+
case .actor:
188+
Text("A")
189+
case .protocol:
190+
Text("Pr")
191+
case .extension:
192+
Text("Ex")
193+
case .case:
194+
Text("K")
195+
case .property:
196+
Text("P")
197+
case .typealias:
198+
Text("T")
199+
case .function:
200+
Text("𝑓")
201+
case .method:
202+
Text("M")
203+
}
198204
case .text:
199205
Text("Tx")
200206
case .webpage:
@@ -225,7 +231,7 @@ struct ReferenceIcon: View {
225231
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
226232
uri: "https://google.com",
227233
startLine: nil,
228-
kind: .class
234+
kind: .symbol(.class)
229235
), count: 20),
230236
chat: .init(initialState: .init(), reducer: { Chat(service: .init()) })
231237
)
@@ -240,35 +246,35 @@ struct ReferenceIcon: View {
240246
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
241247
uri: "https://google.com",
242248
startLine: nil,
243-
kind: .class
249+
kind: .symbol(.class)
244250
),
245251
.init(
246252
title: "BotMessage.swift:100-102",
247253
subtitle: "/Core/Sources/ChatGPTChatTab/Views",
248254
uri: "https://google.com",
249255
startLine: nil,
250-
kind: .struct
256+
kind: .symbol(.struct)
251257
),
252258
.init(
253259
title: "ReferenceList",
254260
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
255261
uri: "https://google.com",
256262
startLine: nil,
257-
kind: .function
263+
kind: .symbol(.function)
258264
),
259265
.init(
260266
title: "ReferenceList",
261267
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
262268
uri: "https://google.com",
263269
startLine: nil,
264-
kind: .case
270+
kind: .symbol(.case)
265271
),
266272
.init(
267273
title: "ReferenceList",
268274
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
269275
uri: "https://google.com",
270276
startLine: nil,
271-
kind: .extension
277+
kind: .symbol(.extension)
272278
),
273279
.init(
274280
title: "ReferenceList",

Tool/Package.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,16 @@ let package = Package(
346346

347347
.testTarget(name: "SuggestionProviderTests", dependencies: ["SuggestionProvider"]),
348348

349+
.target(
350+
name: "RAGChatAgent",
351+
dependencies: [
352+
"ChatBasic",
353+
"ChatContextCollector",
354+
"OpenAIService",
355+
"Preferences",
356+
]
357+
),
358+
349359
// MARK: - GitHub Copilot
350360

351361
.target(

Tool/Sources/ChatBasic/ChatMessage.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public struct ChatMessage: Equatable, Codable {
144144
public init(
145145
id: String = UUID().uuidString,
146146
senderId: String? = nil,
147-
repinesToo: String? = nil,
147+
responseTo: String? = nil,
148148
role: Role,
149149
content: String?,
150150
name: String? = nil,
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import AIModel
2+
import ChatBasic
3+
import Foundation
4+
import OpenAIService
5+
6+
public struct ChatAgentConfiguration: Codable {
7+
public var capabilityIds: Set<String>
8+
public var temperature: Double?
9+
public var modelId: String?
10+
public var model: ChatModel?
11+
public var stop: [String]?
12+
public var maxTokens: Int?
13+
public var minimumReplyTokens: Int?
14+
public var runFunctionsAutomatically: Bool?
15+
public var apiKey: String?
16+
}
17+
18+
public actor RAGChatAgent: ChatAgent {
19+
let configuration: ChatAgentConfiguration
20+
21+
init(configuration: ChatAgentConfiguration) {
22+
self.configuration = configuration
23+
}
24+
25+
public func send(_ request: Request) async -> AsyncThrowingStream<Response, any Error> {
26+
fatalError()
27+
// var continuation: AsyncThrowingStream<Response, any Error>.Continuation!
28+
// let stream = AsyncThrowingStream<Response, any Error> { cont in
29+
// continuation = cont
30+
// }
31+
//
32+
// await withTaskCancellationHandler {
33+
// <#code#>
34+
// } onCancel: {
35+
// continuation.finish(throwing: CancellationError())
36+
// }
37+
//
38+
// return .init { continuation in
39+
// Task {
40+
// let response = try await chatGPTService.send(content: request.text, summary: nil)
41+
// continuation.finish()
42+
// }
43+
// }
44+
}
45+
}
46+
47+
extension RAGChatAgent {
48+
var allCapabilities: [String: any RAGChatAgentCapability] {
49+
RAGChatAgentCapabilityContainer.capabilities
50+
}
51+
52+
func capability(for identifier: String) -> (any RAGChatAgentCapability)? {
53+
allCapabilities[identifier]
54+
}
55+
}
56+
57+
final class ChatFunctionProvider: ChatGPTFunctionProvider {
58+
var functions: [any ChatGPTFunction] = []
59+
60+
init() {}
61+
62+
func removeAll() {
63+
functions = []
64+
}
65+
66+
func append(functions others: [any ChatGPTFunction]) {
67+
functions.append(contentsOf: others)
68+
}
69+
70+
var functionCallStrategy: OpenAIService.FunctionCallStrategy? {
71+
nil
72+
}
73+
}
74+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import ChatBasic
2+
import Foundation
3+
4+
/// A singleton that stores all the possible capabilities of an ``RAGChatAgent``.
5+
public enum RAGChatAgentCapabilityContainer {
6+
static var capabilities: [String: any RAGChatAgentCapability] = [:]
7+
static func add(_ capability: any RAGChatAgentCapability) {
8+
capabilities[capability.id] = capability
9+
}
10+
11+
static func add(_ capabilities: [any RAGChatAgentCapability]) {
12+
capabilities.forEach { add($0) }
13+
}
14+
}
15+
16+
/// A protocol that defines the capability of an ``RAGChatAgent``.
17+
protocol RAGChatAgentCapability: Identifiable {
18+
typealias Request = ChatAgentRequest
19+
typealias Reference = ChatAgentContext.Reference
20+
21+
/// The name to be displayed to the user.
22+
var name: String { get }
23+
/// The identifier of the capability.
24+
var id: String { get }
25+
/// Fetch the context for a given request. It can return a portion of the context at a time.
26+
func fetchContext(for request: ChatAgentRequest) async -> AsyncStream<ChatAgentContext>
27+
}
28+
29+
public struct ChatAgentContext {
30+
public typealias Reference = ChatMessage.Reference
31+
32+
/// Extra system prompt to be included in the chat request.
33+
public var extraSystemPrompt: String?
34+
/// References to be included in the chat request.
35+
public var references: [Reference]
36+
/// Functions to be included in the chat request.
37+
public var functions: [any ChatGPTFunction]
38+
39+
public init(
40+
extraSystemPrompt: String? = nil,
41+
references: [ChatMessage.Reference] = [],
42+
functions: [any ChatGPTFunction] = []
43+
) {
44+
self.extraSystemPrompt = extraSystemPrompt
45+
self.references = references
46+
self.functions = functions
47+
}
48+
}
49+
50+
// MARK: - Default Implementation
51+
52+
extension RAGChatAgentCapability {
53+
func fetchContext(for request: ChatAgentRequest) async -> AsyncStream<ChatAgentContext> {
54+
return AsyncStream { continuation in
55+
continuation.finish()
56+
}
57+
}
58+
}
59+

0 commit comments

Comments
 (0)