@@ -17,22 +17,22 @@ extension Agent {
1717 _ budgetPhase: inout ContextBudgetPhase ? ,
1818 usage: TokenUsage ,
1919 messages: inout [ ChatMessage ] ,
20- continuation : AsyncThrowingStream < StreamEvent , Error > . Continuation ? = nil
20+ emit : StreamEmitter ? = nil
2121 ) {
2222 guard var phase = budgetPhase else { return }
2323 let result = phase. afterResponse ( usage: usage, messages: & messages)
2424 budgetPhase = phase
25- continuation ? . yield ( . make ( . budgetUpdated( budget: result. budget) ) )
25+ emit ? . yield ( . budgetUpdated( budget: result. budget) )
2626 if result. advisoryEmitted {
27- continuation ? . yield ( . make ( . budgetAdvisory( budget: result. budget) ) )
27+ emit ? . yield ( . budgetAdvisory( budget: result. budget) )
2828 }
2929 }
3030
3131 @discardableResult
3232 func executePruneCalls(
3333 _ calls: [ IndexedToolCall ] ,
3434 messages: inout [ ChatMessage ] ,
35- continuation : AsyncThrowingStream < StreamEvent , Error > . Continuation ? = nil
35+ emit : StreamEmitter ? = nil
3636 ) -> ( historyWasRewritten: Bool , results: [ IndexedToolResult ] ) {
3737 let pruneEnabled = configuration. contextBudget? . enablePruneTool == true
3838 var historyWasRewritten = false
@@ -54,11 +54,9 @@ extension Agent {
5454 }
5555 }
5656 results. append ( IndexedToolResult ( index: indexed. index, call: indexed. call, result: result) )
57- continuation? . yield ( . make( . toolCallCompleted(
58- id: indexed. call. id,
59- name: indexed. call. name,
60- result: result
61- ) ) )
57+ emit? . yield ( . toolCallCompleted(
58+ id: indexed. call. id, name: indexed. call. name, result: result
59+ ) )
6260 }
6361 return ( historyWasRewritten: historyWasRewritten, results: results)
6462 }
@@ -82,7 +80,7 @@ extension Agent {
8280 var allResults = try await executeIndexedCalls ( autoExecute, context: executionContext, approvalHandler: handler)
8381
8482 let ( approved, denied) = try await resolveApprovals (
85- needsApproval, handler: handler, allowlist: & allowlist, continuation : nil
83+ needsApproval, handler: handler, allowlist: & allowlist
8684 )
8785 try Task . checkCancellation ( )
8886
@@ -97,47 +95,50 @@ extension Agent {
9795
9896 func executeStreamingResults(
9997 _ calls: [ IndexedToolCall ] , context: C , messages: [ ChatMessage ] ,
98+ options: InvocationOptions ,
10099 continuation: AsyncThrowingStream < StreamEvent , Error > . Continuation ,
101- approvalHandler : ToolApprovalHandler ? = nil , allowlist: inout Set < String >
100+ allowlist: inout Set < String >
102101 ) async throws -> [ IndexedToolResult ] {
103102 guard !calls. isEmpty else { return [ ] }
104103 let executionContext = context. withParentHistory ( messages. resolvedPrefixForInheritance ( ) )
104+ let approvalHandler = options. approvalHandler
105+ let emit = StreamEmitter ( factory: options. eventFactory, continuation: continuation)
105106
106107 guard let handler = approvalHandler, configuration. approvalPolicy != . none else {
107108 return try await executeIndexedStreamingCalls (
108109 calls,
109110 context: executionContext,
110- continuation : continuation ,
111- approvalHandler : approvalHandler
111+ options : options ,
112+ continuation : continuation
112113 )
113114 }
114115
115116 let ( autoExecute, needsApproval) = partitionCallsRequiringApproval ( calls, allowlist: allowlist)
116117 var allResults = try await executeIndexedStreamingCalls (
117118 autoExecute,
118119 context: executionContext,
119- continuation : continuation ,
120- approvalHandler : handler
120+ options : options ,
121+ continuation : continuation
121122 )
122123
123124 let ( approved, denied) = try await resolveApprovals (
124- needsApproval, handler: handler, allowlist : & allowlist , continuation : continuation
125+ needsApproval, handler: handler, emit : emit , allowlist : & allowlist
125126 )
126127 try Task . checkCancellation ( )
127128
128129 for entry in denied {
129130 let truncatedEntry = truncatedIndexedToolResult ( entry)
130- continuation . yield ( . make ( . toolCallCompleted(
131+ emit . yield ( . toolCallCompleted(
131132 id: truncatedEntry. call. id, name: truncatedEntry. call. name, result: truncatedEntry. result
132- ) ) )
133+ ) )
133134 allResults. append ( truncatedEntry)
134135 }
135136
136137 try await allResults. append ( contentsOf: executeIndexedStreamingCalls (
137138 approved,
138139 context: executionContext,
139- continuation : continuation ,
140- approvalHandler : handler
140+ options : options ,
141+ continuation : continuation
141142 ) )
142143 return allResults. sorted { $0. index < $1. index }
143144 }
@@ -194,14 +195,14 @@ extension Agent {
194195 private func executeIndexedStreamingCalls(
195196 _ calls: [ IndexedToolCall ] ,
196197 context: C ,
197- continuation : AsyncThrowingStream < StreamEvent , Error > . Continuation ,
198- approvalHandler : ToolApprovalHandler ?
198+ options : InvocationOptions ,
199+ continuation : AsyncThrowingStream < StreamEvent , Error > . Continuation
199200 ) async throws -> [ IndexedToolResult ] {
200201 let results = try await executeToolsStreaming (
201202 calls. map ( \. call) ,
202203 context: context,
203- continuation : continuation ,
204- approvalHandler : approvalHandler
204+ options : options ,
205+ continuation : continuation
205206 )
206207 return zip ( calls, results) . map { indexed, entry in
207208 IndexedToolResult (
0 commit comments