@@ -12,7 +12,7 @@ import { convertToLegacyEvents } from "@/legacy/convert";
12
12
import { LegacyRuntimeProtocolEvent } from "@/legacy/types" ;
13
13
import { lastValueFrom , of } from "rxjs" ;
14
14
import { transformChunks } from "@/chunks" ;
15
- import { AgentStateMutation , RunAgentSubscriber } from "./subscriber" ;
15
+ import { AgentStateMutation , RunAgentSubscriber , runSubscribersWithMutation } from "./subscriber" ;
16
16
17
17
export abstract class AbstractAgent {
18
18
public agentId ?: string ;
@@ -50,22 +50,27 @@ export abstract class AbstractAgent {
50
50
51
51
protected abstract run ( input : RunAgentInput ) : Observable < BaseEvent > ;
52
52
53
- public async runAgent ( parameters ?: RunAgentParameters ) : Promise < void > {
53
+ public async runAgent (
54
+ parameters ?: RunAgentParameters ,
55
+ subscriber ?: RunAgentSubscriber ,
56
+ ) : Promise < void > {
54
57
this . agentId = this . agentId ?? uuidv4 ( ) ;
55
58
const input = this . prepareRunAgentInput ( parameters ) ;
59
+ const subscribers = [ ...this . subscribers , subscriber ?? { } ] ;
60
+
61
+ this . onInitialize ( input , subscribers ) ;
56
62
57
63
const pipeline = pipe (
58
64
( ) => this . run ( input ) ,
59
65
transformChunks ( this . debug ) ,
60
66
verifyEvents ( this . debug ) ,
61
- ( source$ ) => this . apply ( input , source$ ) ,
62
- ( source$ ) => this . processApplyEvents ( input , source$ ) ,
67
+ ( source$ ) => this . apply ( input , source$ , subscribers ) ,
68
+ ( source$ ) => this . processApplyEvents ( input , source$ , subscribers ) ,
63
69
catchError ( ( error ) => {
64
- this . onError ( error ) ;
65
- return throwError ( ( ) => error ) ;
70
+ return this . onError ( input , error , subscribers ) ;
66
71
} ) ,
67
72
finalize ( ( ) => {
68
- this . onFinalize ( ) ;
73
+ this . onFinalize ( input , subscribers ) ;
69
74
} ) ,
70
75
) ;
71
76
@@ -77,21 +82,37 @@ export abstract class AbstractAgent {
77
82
protected apply (
78
83
input : RunAgentInput ,
79
84
events$ : Observable < BaseEvent > ,
85
+ subscribers : RunAgentSubscriber [ ] ,
80
86
) : Observable < AgentStateMutation > {
81
- return defaultApplyEvents ( input , events$ ) ;
87
+ return defaultApplyEvents ( input , events$ , this , subscribers ) ;
82
88
}
83
89
84
90
protected processApplyEvents (
85
91
input : RunAgentInput ,
86
92
events$ : Observable < AgentStateMutation > ,
93
+ subscribers : RunAgentSubscriber [ ] ,
87
94
) : Observable < AgentStateMutation > {
88
95
return events$ . pipe (
89
96
tap ( ( event ) => {
90
97
if ( event . messages ) {
91
98
this . messages = event . messages ;
99
+ subscribers . forEach ( ( subscriber ) => {
100
+ subscriber . onMessagesChanged ?.( {
101
+ messages : this . messages ,
102
+ agent : this ,
103
+ input,
104
+ } ) ;
105
+ } ) ;
92
106
}
93
107
if ( event . state ) {
94
108
this . state = event . state ;
109
+ subscribers . forEach ( ( subscriber ) => {
110
+ subscriber . onStateChanged ?.( {
111
+ state : this . state ,
112
+ agent : this ,
113
+ input,
114
+ } ) ;
115
+ } ) ;
95
116
}
96
117
} ) ,
97
118
) ;
@@ -109,11 +130,117 @@ export abstract class AbstractAgent {
109
130
} ;
110
131
}
111
132
112
- protected onError ( error : Error ) {
113
- console . error ( "Agent execution failed:" , error ) ;
133
+ protected onInitialize ( input : RunAgentInput , subscribers : RunAgentSubscriber [ ] ) {
134
+ const onRunInitializedMutation = runSubscribersWithMutation (
135
+ subscribers ,
136
+ this . messages ,
137
+ this . state ,
138
+ ( subscriber , messages , state ) =>
139
+ subscriber . onRunInitialized ?.( { messages, state, agent : this , input } ) ,
140
+ ) ;
141
+ if (
142
+ onRunInitializedMutation . messages !== undefined ||
143
+ onRunInitializedMutation . state !== undefined
144
+ ) {
145
+ if ( onRunInitializedMutation . messages ) {
146
+ this . messages = onRunInitializedMutation . messages ;
147
+ input . messages = onRunInitializedMutation . messages ;
148
+ subscribers . forEach ( ( subscriber ) => {
149
+ subscriber . onMessagesChanged ?.( {
150
+ messages : this . messages ,
151
+ agent : this ,
152
+ input,
153
+ } ) ;
154
+ } ) ;
155
+ }
156
+ if ( onRunInitializedMutation . state ) {
157
+ this . state = onRunInitializedMutation . state ;
158
+ input . state = onRunInitializedMutation . state ;
159
+ subscribers . forEach ( ( subscriber ) => {
160
+ subscriber . onStateChanged ?.( {
161
+ state : this . state ,
162
+ agent : this ,
163
+ input,
164
+ } ) ;
165
+ } ) ;
166
+ }
167
+ }
114
168
}
115
169
116
- protected onFinalize ( ) { }
170
+ protected onError ( input : RunAgentInput , error : Error , subscribers : RunAgentSubscriber [ ] ) {
171
+ const onRunFailedMutation = runSubscribersWithMutation (
172
+ subscribers ,
173
+ this . messages ,
174
+ this . state ,
175
+ ( subscriber , messages , state ) =>
176
+ subscriber . onRunFailed ?.( { error, messages, state, agent : this , input } ) ,
177
+ ) ;
178
+
179
+ if ( onRunFailedMutation . messages !== undefined || onRunFailedMutation . state !== undefined ) {
180
+ if ( onRunFailedMutation . messages !== undefined ) {
181
+ this . messages = onRunFailedMutation . messages ;
182
+ subscribers . forEach ( ( subscriber ) => {
183
+ subscriber . onMessagesChanged ?.( {
184
+ messages : this . messages ,
185
+ agent : this ,
186
+ input,
187
+ } ) ;
188
+ } ) ;
189
+ }
190
+ if ( onRunFailedMutation . state !== undefined ) {
191
+ this . state = onRunFailedMutation . state ;
192
+ subscribers . forEach ( ( subscriber ) => {
193
+ subscriber . onStateChanged ?.( {
194
+ state : this . state ,
195
+ agent : this ,
196
+ input,
197
+ } ) ;
198
+ } ) ;
199
+ }
200
+ }
201
+ if ( onRunFailedMutation . stopPropagation !== true ) {
202
+ console . error ( "Agent execution failed:" , error ) ;
203
+ return throwError ( ( ) => error ) ;
204
+ } else {
205
+ return of ( null ) ;
206
+ }
207
+ }
208
+
209
+ protected onFinalize ( input : RunAgentInput , subscribers : RunAgentSubscriber [ ] ) {
210
+ const onRunFinalizedMutation = runSubscribersWithMutation (
211
+ subscribers ,
212
+ this . messages ,
213
+ this . state ,
214
+ ( subscriber , messages , state ) =>
215
+ subscriber . onRunFinalized ?.( { messages, state, agent : this , input } ) ,
216
+ ) ;
217
+
218
+ if (
219
+ onRunFinalizedMutation . messages !== undefined ||
220
+ onRunFinalizedMutation . state !== undefined
221
+ ) {
222
+ if ( onRunFinalizedMutation . messages !== undefined ) {
223
+ this . messages = onRunFinalizedMutation . messages ;
224
+ subscribers . forEach ( ( subscriber ) => {
225
+ subscriber . onMessagesChanged ?.( {
226
+ messages : this . messages ,
227
+ agent : this ,
228
+ input,
229
+ } ) ;
230
+ } ) ;
231
+ }
232
+ if ( onRunFinalizedMutation . state !== undefined ) {
233
+ this . state = onRunFinalizedMutation . state ;
234
+ subscribers . forEach ( ( subscriber ) => {
235
+ subscriber . onStateChanged ?.( {
236
+ state : this . state ,
237
+ agent : this ,
238
+ input,
239
+ } ) ;
240
+ } ) ;
241
+ }
242
+ }
243
+ }
117
244
118
245
public clone ( ) {
119
246
const cloned = Object . create ( Object . getPrototypeOf ( this ) ) ;
0 commit comments