-
-
Notifications
You must be signed in to change notification settings - Fork 426
Expand file tree
/
Copy pathChatPlugin.swift
More file actions
59 lines (53 loc) · 2 KB
/
ChatPlugin.swift
File metadata and controls
59 lines (53 loc) · 2 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
import Foundation
public struct ChatPluginRequest: Sendable {
public var text: String
public var arguments: [String]
public var history: [ChatMessage]
public init(text: String, arguments: [String], history: [ChatMessage]) {
self.text = text
self.arguments = arguments
self.history = history
}
}
public protocol ChatPlugin {
typealias Response = ChatAgentResponse
typealias Request = ChatPluginRequest
static var id: String { get }
static var command: String { get }
static var name: String { get }
static var description: String { get }
// In this method, the plugin is able to send more complicated response. It also enables it to
// perform special tasks like starting a new message or reporting progress.
func sendForComplicatedResponse(
_ request: Request
) async -> AsyncThrowingStream<Response, any Error>
// This method allows the plugin to respond a stream of text content only.
func sendForTextResponse(_ request: Request) async -> AsyncThrowingStream<String, any Error>
func formatContent(_ content: Response.Content) -> Response.Content
init()
}
public extension ChatPlugin {
func formatContent(_ content: Response.Content) -> Response.Content {
return content
}
func sendForComplicatedResponse(
_ request: Request
) async -> AsyncThrowingStream<Response, any Error> {
let textStream = await sendForTextResponse(request)
return AsyncThrowingStream<Response, any Error> { continuation in
let task = Task {
do {
for try await text in textStream {
continuation.yield(Response.content(.text(text)))
}
continuation.finish()
} catch {
continuation.finish(throwing: error)
}
}
continuation.onTermination = { _ in
task.cancel()
}
}
}
}