-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathConversationServiceProvider.swift
More file actions
154 lines (133 loc) · 4.97 KB
/
ConversationServiceProvider.swift
File metadata and controls
154 lines (133 loc) · 4.97 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import CopilotForXcodeKit
import Foundation
import CodableWrappers
public protocol ConversationServiceType {
func createConversation(_ request: ConversationRequest, workspace: WorkspaceInfo) async throws
func createTurn(with conversationId: String, request: ConversationRequest, workspace: WorkspaceInfo) async throws
func cancelProgress(_ workDoneToken: String, workspace: WorkspaceInfo) async throws
func rateConversation(turnId: String, rating: ConversationRating, workspace: WorkspaceInfo) async throws
func copyCode(request: CopyCodeRequest, workspace: WorkspaceInfo) async throws
func templates(workspace: WorkspaceInfo) async throws -> [ChatTemplate]?
func models(workspace: WorkspaceInfo) async throws -> [CopilotModel]?
}
public protocol ConversationServiceProvider {
func createConversation(_ request: ConversationRequest) async throws
func createTurn(with conversationId: String, request: ConversationRequest) async throws
func stopReceivingMessage(_ workDoneToken: String) async throws
func rateConversation(turnId: String, rating: ConversationRating) async throws
func copyCode(_ request: CopyCodeRequest) async throws
func templates() async throws -> [ChatTemplate]?
func models() async throws -> [CopilotModel]?
}
public struct FileReference: Hashable, Codable, Equatable {
public let url: URL
public let relativePath: String?
public let fileName: String?
public var isCurrentEditor: Bool = false
public init(url: URL, relativePath: String?, fileName: String?, isCurrentEditor: Bool = false) {
self.url = url
self.relativePath = relativePath
self.fileName = fileName
self.isCurrentEditor = isCurrentEditor
}
public init(url: URL, isCurrentEditor: Bool = false) {
self.url = url
self.relativePath = nil
self.fileName = nil
self.isCurrentEditor = isCurrentEditor
}
public func hash(into hasher: inout Hasher) {
hasher.combine(url)
hasher.combine(isCurrentEditor)
}
public static func == (lhs: FileReference, rhs: FileReference) -> Bool {
return lhs.url == rhs.url && lhs.isCurrentEditor == rhs.isCurrentEditor
}
}
extension FileReference {
public func getPathRelativeToHome() -> String {
let filePath = url.path
guard !filePath.isEmpty else { return "" }
let homeDirectory = FileManager.default.homeDirectoryForCurrentUser.path
if !homeDirectory.isEmpty {
return filePath.replacingOccurrences(of: homeDirectory, with: "~")
}
return filePath
}
}
public struct TurnSchema: Codable {
public var request: String
public var response: String?
public var agentSlug: String?
public var turnId: String?
public init(request: String, response: String? = nil, agentSlug: String? = nil, turnId: String? = nil) {
self.request = request
self.response = response
self.agentSlug = agentSlug
self.turnId = turnId
}
}
public struct ConversationRequest {
public var workDoneToken: String
public var content: String
public var workspaceFolder: String
public var skills: [String]
public var ignoredSkills: [String]?
public var references: [FileReference]?
public var model: String?
public var turns: [TurnSchema]
public init(
workDoneToken: String,
content: String,
workspaceFolder: String,
skills: [String],
ignoredSkills: [String]? = nil,
references: [FileReference]? = nil,
model: String? = nil,
turns: [TurnSchema] = []
) {
self.workDoneToken = workDoneToken
self.content = content
self.workspaceFolder = workspaceFolder
self.skills = skills
self.ignoredSkills = ignoredSkills
self.references = references
self.model = model
self.turns = turns
}
}
public struct CopyCodeRequest {
public var turnId: String
public var codeBlockIndex: Int
public var copyType: CopyKind
public var copiedCharacters: Int
public var totalCharacters: Int
public var copiedText: String
init(turnId: String, codeBlockIndex: Int, copyType: CopyKind, copiedCharacters: Int, totalCharacters: Int, copiedText: String) {
self.turnId = turnId
self.codeBlockIndex = codeBlockIndex
self.copyType = copyType
self.copiedCharacters = copiedCharacters
self.totalCharacters = totalCharacters
self.copiedText = copiedText
}
}
public enum ConversationRating: Int, Codable {
case unrated = 0
case helpful = 1
case unhelpful = -1
}
public enum CopyKind: Int, Codable {
case keyboard = 1
case toolbar = 2
}
public struct ConversationFollowUp: Codable, Equatable {
public var message: String
public var id: String
public var type: String
public init(message: String, id: String, type: String) {
self.message = message
self.id = id
self.type = type
}
}