forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentExecutor.swift
More file actions
166 lines (149 loc) · 5.49 KB
/
AgentExecutor.swift
File metadata and controls
166 lines (149 loc) · 5.49 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
155
156
157
158
159
160
161
162
163
164
165
import Foundation
public actor AgentExecutor<InnerAgent: Agent>: Chain where InnerAgent.Input == String {
public typealias Input = String
public struct Output {
let finalOutput: String
let intermediateSteps: [AgentAction]
}
let agent: InnerAgent
let tools: [String: AgentTool]
let maxIteration: Int?
let maxExecutionTime: Double?
var earlyStopHandleType: AgentEarlyStopHandleType
var now: () -> Date = { Date() }
var isCancelled = false
public init(
agent: InnerAgent,
tools: [AgentTool],
maxIteration: Int? = 10,
maxExecutionTime: Double? = nil,
earlyStopHandleType: AgentEarlyStopHandleType = .force
) {
self.agent = agent
self.tools = tools.reduce(into: [:]) { $0[$1.name] = $1 }
self.maxIteration = maxIteration
self.maxExecutionTime = maxExecutionTime
self.earlyStopHandleType = earlyStopHandleType
}
public func callLogic(
_ input: Input,
callbackManagers: [CallbackManager]
) async throws -> Output {
try agent.validateTools(tools: Array(tools.values))
let startTime = now().timeIntervalSince1970
var iterations = 0
var intermediateSteps: [AgentAction] = []
func shouldContinue() -> Bool {
if isCancelled { return false }
if let maxIteration = maxIteration, iterations >= maxIteration {
return false
}
if let maxExecutionTime = maxExecutionTime,
now().timeIntervalSince1970 - startTime > maxExecutionTime
{
return false
}
return true
}
while shouldContinue() {
let nextStepOutput = try await takeNextStep(
input: input,
intermediateSteps: intermediateSteps,
callbackManagers: callbackManagers
)
switch nextStepOutput {
case let .finish(finish):
return end(
output: finish,
intermediateSteps: intermediateSteps,
callbackManagers: callbackManagers
)
case let .actions(actions):
intermediateSteps.append(contentsOf: actions)
if actions.count == 1,
let action = actions.first,
let toolFinish = getToolFinish(action: action)
{
return end(
output: toolFinish,
intermediateSteps: intermediateSteps,
callbackManagers: callbackManagers
)
}
}
iterations += 1
}
let output = try await agent.returnStoppedResponse(
input: input,
earlyStoppedHandleType: earlyStopHandleType,
intermediateSteps: intermediateSteps,
callbackManagers: callbackManagers
)
return end(
output: output,
intermediateSteps: intermediateSteps,
callbackManagers: callbackManagers
)
}
public nonisolated func parseOutput(_ output: Output) -> String {
output.finalOutput
}
public func cancel() {
isCancelled = true
earlyStopHandleType = .force
}
}
struct InvalidToolError: Error {}
extension AgentExecutor {
func end(
output: AgentFinish,
intermediateSteps: [AgentAction],
callbackManagers: [CallbackManager]
) -> Output {
for callbackManager in callbackManagers {
callbackManager.send(CallbackEvents.AgentDidFinish(info: output))
}
let finalOutput = output.returnValue
return .init(finalOutput: finalOutput, intermediateSteps: intermediateSteps)
}
func takeNextStep(
input: Input,
intermediateSteps: [AgentAction],
callbackManagers: [CallbackManager]
) async throws -> AgentNextStep {
let output = try await agent.plan(
input: input,
intermediateSteps: intermediateSteps,
callbackManagers: callbackManagers
)
switch output {
case .finish: return output
case let .actions(actions):
let completedActions = try await withThrowingTaskGroup(of: AgentAction.self) {
taskGroup in
for action in actions {
callbackManagers
.forEach { $0.send(CallbackEvents.AgentActionDidStart(info: action)) }
guard let tool = tools[action.toolName] else { throw InvalidToolError() }
taskGroup.addTask {
let observation = try await tool.run(input: action.toolInput)
return action.observationAvailable(observation)
}
}
var completedActions = [AgentAction]()
for try await action in taskGroup {
completedActions.append(action)
callbackManagers
.forEach { $0.send(CallbackEvents.AgentActionDidEnd(info: action)) }
}
return completedActions
}
return .actions(completedActions)
}
}
func getToolFinish(action: AgentAction) -> AgentFinish? {
guard let tool = tools[action.toolName] else { return nil }
guard tool.returnDirectly else { return nil }
return .init(returnValue: action.observation ?? "", log: "")
}
}