forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllPlugins.swift
More file actions
142 lines (119 loc) · 4.42 KB
/
AllPlugins.swift
File metadata and controls
142 lines (119 loc) · 4.42 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
import ChatBasic
import Foundation
import OpenAIService
import ShortcutChatPlugin
import TerminalChatPlugin
let allPlugins: [LegacyChatPlugin.Type] = [
LegacyChatPluginWrapper<TerminalChatPlugin>.self,
LegacyChatPluginWrapper<ShortcutChatPlugin>.self,
]
protocol LegacyChatPlugin: AnyObject {
static var command: String { get }
var name: String { get }
init(inside chatGPTService: any LegacyChatGPTServiceType, delegate: LegacyChatPluginDelegate)
func send(content: String, originalMessage: String) async
func cancel() async
func stopResponding() async
}
protocol LegacyChatPluginDelegate: AnyObject {
func pluginDidStart(_ plugin: LegacyChatPlugin)
func pluginDidEnd(_ plugin: LegacyChatPlugin)
func pluginDidStartResponding(_ plugin: LegacyChatPlugin)
func pluginDidEndResponding(_ plugin: LegacyChatPlugin)
func shouldStartAnotherPlugin(_ type: LegacyChatPlugin.Type, withContent: String)
}
final class LegacyChatPluginWrapper<Plugin: ChatPlugin>: LegacyChatPlugin {
static var command: String { Plugin.command }
var name: String { Plugin.name }
let chatGPTService: any LegacyChatGPTServiceType
weak var delegate: LegacyChatPluginDelegate?
var isCancelled = false
required init(
inside chatGPTService: any LegacyChatGPTServiceType,
delegate: any LegacyChatPluginDelegate
) {
self.chatGPTService = chatGPTService
self.delegate = delegate
}
func send(content: String, originalMessage: String) async {
delegate?.pluginDidStart(self)
delegate?.pluginDidStartResponding(self)
let id = "\(Self.command)-\(UUID().uuidString)"
var reply = ChatMessage(id: id, role: .assistant, content: "")
await chatGPTService.memory.mutateHistory { history in
history.append(.init(role: .user, content: originalMessage))
}
let plugin = Plugin()
let stream = await plugin.send(.init(
text: content,
arguments: [],
history: chatGPTService.memory.history
))
do {
var actions = [(id: String, name: String)]()
var actionResults = [String: String]()
var message = ""
for try await response in stream {
guard !isCancelled else { break }
if Task.isCancelled { break }
switch response {
case .status:
break
case let .content(content):
switch content {
case let .text(token):
message.append(token)
}
case .attachments:
break
case let .startAction(id, task):
actions.append((id: id, name: task))
case let .finishAction(id, result):
actionResults[id] = switch result {
case let .failure(error):
error
case let .success(result):
result
}
case .references:
break
case .startNewMessage:
break
}
await chatGPTService.memory.mutateHistory { history in
if history.last?.id == id {
history.removeLast()
}
let actionString = actions.map {
"> \($0.name): \(actionResults[$0.id] ?? "...")"
}.joined(separator: "\n>\n")
if message.isEmpty {
reply.content = actionString
} else {
reply.content = """
\(actionString)
\(message)
"""
}
history.append(reply)
}
}
} catch {
await chatGPTService.memory.mutateHistory { history in
if history.last?.id == id {
history.removeLast()
}
reply.content = error.localizedDescription
history.append(reply)
}
}
delegate?.pluginDidEndResponding(self)
delegate?.pluginDidEnd(self)
}
func cancel() async {
isCancelled = true
}
func stopResponding() async {
isCancelled = true
}
}