@@ -13,7 +13,7 @@ import { TextBlock } from '../types/messages.js'
1313import type { AgentNodeOptions } from './nodes.js'
1414import { AgentNode } from './nodes.js'
1515import { MultiAgentState , MultiAgentResult , NodeResult , Status } from './state.js'
16- import type { MultiAgent } from './multiagent.js'
16+ import type { MultiAgent , MultiAgentOptions } from './multiagent.js'
1717import type { MultiAgentStreamEvent } from './events.js'
1818import {
1919 AfterMultiAgentInvocationEvent ,
@@ -158,8 +158,8 @@ export class Swarm implements MultiAgent {
158158 * @param input - The input to pass to the start agent
159159 * @returns Promise resolving to the final MultiAgentResult
160160 */
161- async invoke ( input : MultiAgentInput ) : Promise < MultiAgentResult > {
162- const gen = this . stream ( input )
161+ async invoke ( input : MultiAgentInput , options ?: MultiAgentOptions ) : Promise < MultiAgentResult > {
162+ const gen = this . stream ( input , options )
163163 let next = await gen . next ( )
164164 while ( ! next . done ) {
165165 next = await gen . next ( )
@@ -174,10 +174,13 @@ export class Swarm implements MultiAgent {
174174 * @param input - The input to pass to the start agent
175175 * @returns Async generator yielding streaming events and returning a MultiAgentResult
176176 */
177- async * stream ( input : MultiAgentInput ) : AsyncGenerator < MultiAgentStreamEvent , MultiAgentResult , undefined > {
177+ async * stream (
178+ input : MultiAgentInput ,
179+ options ?: MultiAgentOptions
180+ ) : AsyncGenerator < MultiAgentStreamEvent , MultiAgentResult , undefined > {
178181 await this . initialize ( )
179182
180- const gen = this . _stream ( input )
183+ const gen = this . _stream ( input , options ?. signal )
181184 let next = await gen . next ( )
182185 while ( ! next . done ) {
183186 if ( next . value instanceof HookableEvent ) {
@@ -189,7 +192,10 @@ export class Swarm implements MultiAgent {
189192 return next . value
190193 }
191194
192- private async * _stream ( input : MultiAgentInput ) : AsyncGenerator < MultiAgentStreamEvent , MultiAgentResult , undefined > {
195+ private async * _stream (
196+ input : MultiAgentInput ,
197+ signal ?: AbortSignal
198+ ) : AsyncGenerator < MultiAgentStreamEvent , MultiAgentResult , undefined > {
193199 const state = new MultiAgentState ( {
194200 nodeIds : [ ...this . nodes . keys ( ) ] ,
195201 } )
@@ -209,10 +215,11 @@ export class Swarm implements MultiAgent {
209215
210216 try {
211217 while ( state . steps < this . config . maxSteps ) {
218+ if ( signal ?. aborted ) break
212219 state . steps ++
213220
214221 // Execute current node
215- const nodeResult = yield * this . _streamNode ( node , input , state , handoff , multiAgentSpan )
222+ const nodeResult = yield * this . _streamNode ( node , input , state , handoff , multiAgentSpan , signal )
216223 handoff = nodeResult . structuredOutput as HandoffResult | undefined
217224 state . results . push ( nodeResult )
218225
@@ -231,6 +238,7 @@ export class Swarm implements MultiAgent {
231238 this . _checkSteps ( state , handoff )
232239
233240 result = new MultiAgentResult ( {
241+ ...( signal ?. aborted && { status : Status . CANCELLED } ) ,
234242 results : state . results ,
235243 content : this . _resolveContent ( state ) ,
236244 duration : Date . now ( ) - state . startTime ,
@@ -257,7 +265,8 @@ export class Swarm implements MultiAgent {
257265 input : MultiAgentInput ,
258266 state : MultiAgentState ,
259267 handoff : HandoffResult | undefined ,
260- multiAgentSpan : Span | null
268+ multiAgentSpan : Span | null ,
269+ signal ?: AbortSignal
261270 ) : AsyncGenerator < MultiAgentStreamEvent , NodeResult , undefined > {
262271 const nodeState = state . node ( node . id ) !
263272 const handoffSchema = this . _buildHandoffSchema ( node . id )
@@ -283,7 +292,7 @@ export class Swarm implements MultiAgent {
283292
284293 try {
285294 const gen = this . _tracer . withSpanContext ( nodeSpan , ( ) =>
286- node . stream ( nodeInput , state , { structuredOutputSchema : handoffSchema } )
295+ node . stream ( nodeInput , state , { structuredOutputSchema : handoffSchema , ... ( signal && { signal } ) } )
287296 )
288297 let next = await this . _tracer . withSpanContext ( nodeSpan , ( ) => gen . next ( ) )
289298 while ( ! next . done ) {
@@ -345,7 +354,8 @@ export class Swarm implements MultiAgent {
345354 }
346355
347356 private _resolveContent ( state : MultiAgentState ) : ContentBlock [ ] {
348- const last = state . results [ state . results . length - 1 ] !
357+ const last = state . results [ state . results . length - 1 ]
358+ if ( ! last ) return [ ]
349359 state . node ( last . nodeId ) ! . terminus = true
350360
351361 const handoff = last . structuredOutput as HandoffResult | undefined
0 commit comments