@@ -178,31 +178,31 @@ func TestModelCallbacks_BeforeSkip(t *testing.T) {
178178 })
179179
180180 llmFlow := New (nil , nil , Options {ModelCallbacks : modelCallbacks })
181- invocation := & agent.Invocation {
182- InvocationID : "test-invocation" ,
183- AgentName : "test-agent" ,
184- Model : & mockModel {
181+ invocation := agent .NewInvocation (
182+ agent .WithInvocationModel (& mockModel {
185183 responses : []* model.Response {{ID : "should-not-be-called" }},
186- },
187- Session : & session.Session {
188- ID : "test-session" ,
189- },
190- }
184+ }),
185+ agent .WithInvocationSession (& session.Session {ID : "test-session" }),
186+ )
191187 eventChan , err := llmFlow .Run (ctx , invocation )
192188 require .NoError (t , err )
193189 var events []* event.Event
194190 for evt := range eventChan {
191+ if evt .RequiresCompletion {
192+ key := agent .AppendEventNoticeKeyPrefix + evt .ID
193+ invocation .NotifyCompletion (ctx , key )
194+ }
195195 events = append (events , evt )
196- // Receive the first event and cancel ctx to prevent deadlock.
197- cancel ()
198- break
196+ if len ( events ) >= 2 {
197+ break
198+ }
199199 }
200- require .Equal (t , 1 , len (events ))
201- require .Equal (t , "skip-response" , events [0 ].Response .ID )
200+ require .Equal (t , 2 , len (events ))
201+ require .Equal (t , "skip-response" , events [1 ].Response .ID )
202202}
203203
204204func TestModelCBs_BeforeCustom (t * testing.T ) {
205- ctx , cancel := context .WithTimeout (context .Background (), 500 * time .Millisecond )
205+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
206206 defer cancel ()
207207
208208 modelCallbacks := model .NewCallbacks ()
@@ -211,27 +211,27 @@ func TestModelCBs_BeforeCustom(t *testing.T) {
211211 })
212212
213213 llmFlow := New (nil , nil , Options {ModelCallbacks : modelCallbacks })
214- invocation := & agent.Invocation {
215- InvocationID : "test-invocation" ,
216- AgentName : "test-agent" ,
217- Model : & mockModel {
214+ invocation := agent .NewInvocation (
215+ agent .WithInvocationModel (& mockModel {
218216 responses : []* model.Response {{ID : "should-not-be-called" }},
219- },
220- Session : & session.Session {
221- ID : "test-session" ,
222- },
223- }
217+ }),
218+ agent .WithInvocationSession (& session.Session {ID : "test-session" }),
219+ )
224220 eventChan , err := llmFlow .Run (ctx , invocation )
225221 require .NoError (t , err )
226222 var events []* event.Event
227223 for evt := range eventChan {
224+ if evt .RequiresCompletion {
225+ key := agent .AppendEventNoticeKeyPrefix + evt .ID
226+ invocation .NotifyCompletion (ctx , key )
227+ }
228228 events = append (events , evt )
229- // Receive the first event and cancel ctx to prevent deadlock.
230- cancel ()
231- break
229+ if len ( events ) >= 2 {
230+ break
231+ }
232232 }
233- require .Equal (t , 1 , len (events ))
234- require .Equal (t , "custom-before" , events [0 ].Response .ID )
233+ require .Equal (t , 2 , len (events ))
234+ require .Equal (t , "custom-before" , events [1 ].Response .ID )
235235}
236236
237237func TestModelCallbacks_BeforeError (t * testing.T ) {
@@ -244,26 +244,32 @@ func TestModelCallbacks_BeforeError(t *testing.T) {
244244 })
245245
246246 llmFlow := New (nil , nil , Options {ModelCallbacks : modelCallbacks })
247- invocation := & agent.Invocation {
248- InvocationID : "test-invocation" ,
249- AgentName : "test-agent" ,
250- Model : & mockModel {
247+ invocation := agent .NewInvocation (
248+ agent .WithInvocationModel (& mockModel {
251249 responses : []* model.Response {{ID : "should-not-be-called" }},
252- },
253- }
250+ }),
251+ agent .WithInvocationSession (& session.Session {ID : "test-session" }),
252+ )
254253 eventChan , err := llmFlow .Run (ctx , invocation )
255254 require .NoError (t , err )
256255 var events []* event.Event
257256 for evt := range eventChan {
257+ if evt .RequiresCompletion {
258+ key := agent .AppendEventNoticeKeyPrefix + evt .ID
259+ invocation .NotifyCompletion (ctx , key )
260+ }
258261 events = append (events , evt )
262+ if len (events ) >= 2 {
263+ break
264+ }
259265 // Receive the first error event and cancel ctx to prevent deadlock.
260266 if evt .Error != nil && evt .Error .Message == "before error" {
261267 cancel ()
262268 break
263269 }
264270 }
265- require .Equal (t , 1 , len (events ))
266- require .Equal (t , "before error" , events [0 ].Error .Message )
271+ require .Equal (t , 2 , len (events ))
272+ require .Equal (t , "before error" , events [1 ].Error .Message )
267273}
268274
269275func TestModelCBs_AfterOverride (t * testing.T ) {
@@ -278,28 +284,28 @@ func TestModelCBs_AfterOverride(t *testing.T) {
278284 )
279285
280286 llmFlow := New (nil , nil , Options {ModelCallbacks : modelCallbacks })
281- invocation := & agent.Invocation {
282- InvocationID : "test-invocation" ,
283- AgentName : "test-agent" ,
284- Model : & mockModel {
287+ invocation := agent .NewInvocation (
288+ agent .WithInvocationModel (& mockModel {
285289 responses : []* model.Response {{ID : "original" }},
286- },
287- Session : & session.Session {
288- ID : "test-session" ,
289- },
290- }
290+ }),
291+ agent .WithInvocationSession (& session.Session {ID : "test-session" }),
292+ )
291293 eventChan , err := llmFlow .Run (ctx , invocation )
292294 require .NoError (t , err )
293295 var events []* event.Event
294296 for evt := range eventChan {
297+ if evt .RequiresCompletion {
298+ key := agent .AppendEventNoticeKeyPrefix + evt .ID
299+ invocation .NotifyCompletion (ctx , key )
300+ }
295301 events = append (events , evt )
296- // Receive the first event and cancel ctx to prevent deadlock.
297- cancel ()
298- break
302+ if len ( events ) >= 2 {
303+ break
304+ }
299305 }
300- require .Equal (t , 1 , len (events ))
306+ require .Equal (t , 2 , len (events ))
301307 t .Log (events [0 ])
302- require .Equal (t , "after-override" , events [0 ].Response .Object )
308+ require .Equal (t , "after-override" , events [1 ].Response .Object )
303309}
304310
305311func TestModelCallbacks_AfterError (t * testing.T ) {
@@ -314,29 +320,32 @@ func TestModelCallbacks_AfterError(t *testing.T) {
314320 )
315321
316322 llmFlow := New (nil , nil , Options {ModelCallbacks : modelCallbacks })
317- invocation := & agent.Invocation {
318- InvocationID : "test-invocation" ,
319- AgentName : "test-agent" ,
320- Model : & mockModel {
323+ invocation := agent .NewInvocation (
324+ agent .WithInvocationModel (& mockModel {
321325 responses : []* model.Response {{ID : "original" }},
322- },
323- Session : & session.Session {
324- ID : "test-session" ,
325- },
326- }
326+ }),
327+ agent .WithInvocationSession (& session.Session {ID : "test-session" }),
328+ )
327329 eventChan , err := llmFlow .Run (ctx , invocation )
328330 require .NoError (t , err )
329331 var events []* event.Event
330332 for evt := range eventChan {
333+ if evt .RequiresCompletion {
334+ key := agent .AppendEventNoticeKeyPrefix + evt .ID
335+ invocation .NotifyCompletion (ctx , key )
336+ }
331337 events = append (events , evt )
338+ if len (events ) >= 2 {
339+ break
340+ }
332341 // Receive the first error event and cancel ctx to prevent deadlock.
333342 if evt .Error != nil && evt .Error .Message == "after error" {
334343 cancel ()
335344 break
336345 }
337346 }
338- require .Equal (t , 1 , len (events ))
339- require .Equal (t , "after error" , events [0 ].Error .Message )
347+ require .Equal (t , 2 , len (events ))
348+ require .Equal (t , "after error" , events [1 ].Error .Message )
340349}
341350
342351// noResponseModel returns a closed channel without emitting any responses.
@@ -356,15 +365,21 @@ func TestRun_NoPanicWhenModelReturnsNoResponses(t *testing.T) {
356365 defer cancel ()
357366
358367 f := New (nil , nil , Options {})
359- inv := & agent.Invocation {InvocationID : "inv-nil" , AgentName : "agent-nil" , Model : & noResponseModel {}}
368+ inv := agent .NewInvocation (
369+ agent .WithInvocationModel (& noResponseModel {}),
370+ )
360371
361372 ch , err := f .Run (ctx , inv )
362373 require .NoError (t , err )
363374
364375 // Collect all events until channel closes. Expect none and, importantly, no panic.
365376 var count int
366- for range ch {
377+ for evt := range ch {
378+ if evt .RequiresCompletion {
379+ key := agent .AppendEventNoticeKeyPrefix + evt .ID
380+ inv .NotifyCompletion (ctx , key )
381+ }
367382 count ++
368383 }
369- require .Equal (t , 0 , count )
384+ require .Equal (t , 1 , count )
370385}
0 commit comments