@@ -315,36 +315,43 @@ const applyMissingInputError = (input: RunOutcomeInput) => {
315315} ;
316316
317317const runChatRequest = ( socket : ServerWebSocket < SocketData > , message : ClientChatRequest ) => {
318- const model = selectModel ( toModelSelection ( { data : message . data , tokens : socket . data . tokens } ) ) ;
319- const coreMessages = toCoreMessages ( message . messages ) ;
320- const text = readUserInputText ( coreMessages ) ;
321- const config = message . data ?. agentConfig ;
322- const context = buildAgentContext ( { config, context : message . data ?. context ?? null } ) ;
323- const threadId = message . data ?. threadId ?? message . chatId ;
324-
325- const writer = createWebSocketUiWriter ( socket , message . requestId ) ;
326- const eventStream = createAiSdkInteractionEventStream ( { writer } ) ;
327-
328318 const outcomeInput : RunOutcomeInput = { socket, requestId : message . requestId } ;
329- if ( ! text ) {
330- return applyMissingInputError ( outcomeInput ) ;
319+ try {
320+ const model = selectModel ( toModelSelection ( { data : message . data , tokens : socket . data . tokens } ) ) ;
321+ const coreMessages = toCoreMessages ( message . messages ) ;
322+ const text = readUserInputText ( coreMessages ) ;
323+ const config = message . data ?. agentConfig ;
324+ const context = buildAgentContext ( { config, context : message . data ?. context ?? null } ) ;
325+ const threadId = message . data ?. threadId ?? message . chatId ;
326+
327+ const writer = createWebSocketUiWriter ( socket , message . requestId ) ;
328+ const eventStream = createAiSdkInteractionEventStream ( { writer } ) ;
329+
330+ if ( ! text ) {
331+ return applyMissingInputError ( outcomeInput ) ;
332+ }
333+ const runtime = createAgentRuntime ( {
334+ model,
335+ config,
336+ subagents : message . data ?. subagents ,
337+ } ) ;
338+ const runResult = runtime . stream ( {
339+ text,
340+ context : context ?? undefined ,
341+ threadId,
342+ eventStream,
343+ interactionId : message . chatId ,
344+ correlationId : message . requestId ,
345+ } ) ;
346+ const handlerInput = createRunChatHandlerInput ( outcomeInput , runResult ) ;
347+
348+ return maybeTry (
349+ bindFirst ( applyRunError , outcomeInput ) ,
350+ bindFirst ( handleRunResult , handlerInput ) ,
351+ ) ;
352+ } catch ( error ) {
353+ return applyRunError ( outcomeInput , error ) ;
331354 }
332- const runtime = createAgentRuntime ( {
333- model,
334- config,
335- subagents : message . data ?. subagents ,
336- } ) ;
337- const runResult = runtime . stream ( {
338- text,
339- context : context ?? undefined ,
340- threadId,
341- eventStream,
342- interactionId : message . chatId ,
343- correlationId : message . requestId ,
344- } ) ;
345- const handlerInput = createRunChatHandlerInput ( outcomeInput , runResult ) ;
346-
347- return maybeTry ( bindFirst ( applyRunError , outcomeInput ) , bindFirst ( handleRunResult , handlerInput ) ) ;
348355} ;
349356
350357const handleAssistantTransport = ( req : Request ) : MaybePromise < Response > => {
@@ -362,30 +369,42 @@ const handleAssistantBodyOrError = (body: unknown) => {
362369 return handleAssistantBody ( body ) ;
363370} ;
364371
372+ const reportAssistantError = ( error : unknown ) => {
373+ console . error ( error ) ;
374+ return true ;
375+ } ;
376+
365377const handleAssistantBody = ( body : unknown ) : Response => {
366378 const parsed = parseAssistantTransportRequest ( body ) ;
367379 if ( ! parsed ) {
368380 return new Response ( "Invalid assistant transport payload" , { status : 400 } ) ;
369381 }
370-
371- const model = selectModel ( toModelSelection ( { data : parsed . data } ) ) ;
372- const coreMessages = toCoreMessagesFromAssistantCommands ( parsed . commands ) ;
373- const chatId = parsed . data ?. chatId ?? crypto . randomUUID ( ) ;
374-
375- const streamAdapter = createAssistantUiInteractionStream ( { includeReasoning : true } ) ;
376- const eventStream = streamAdapter . eventStream ;
377-
378- const recipeId = resolveInteractionRecipeId ( parsed . data ?. recipeId ) ;
379- const runResult = runInteractionRequest ( {
380- recipeId,
381- model,
382- messages : coreMessages ,
383- eventStream,
384- interactionId : chatId ,
385- correlationId : chatId ,
386- } ) ;
387- finalizeAssistantRun ( runResult , streamAdapter . controller ) ;
388- return AssistantStream . toResponse ( streamAdapter . stream , new DataStreamEncoder ( ) ) ;
382+ let streamAdapter : ReturnType < typeof createAssistantUiInteractionStream > | null = null ;
383+
384+ try {
385+ const model = selectModel ( toModelSelection ( { data : parsed . data } ) ) ;
386+ const coreMessages = toCoreMessagesFromAssistantCommands ( parsed . commands ) ;
387+ const chatId = parsed . data ?. chatId ?? crypto . randomUUID ( ) ;
388+
389+ streamAdapter = createAssistantUiInteractionStream ( { includeReasoning : true } ) ;
390+ const eventStream = streamAdapter . eventStream ;
391+
392+ const recipeId = resolveInteractionRecipeId ( parsed . data ?. recipeId ) ;
393+ const runResult = runInteractionRequest ( {
394+ recipeId,
395+ model,
396+ messages : coreMessages ,
397+ eventStream,
398+ interactionId : chatId ,
399+ correlationId : chatId ,
400+ } ) ;
401+ finalizeAssistantRun ( runResult , streamAdapter . controller ) ;
402+ return AssistantStream . toResponse ( streamAdapter . stream , new DataStreamEncoder ( ) ) ;
403+ } catch ( error ) {
404+ reportAssistantError ( error ) ;
405+ closeAssistantControllerIfPresent ( streamAdapter ? streamAdapter . controller : null ) ;
406+ return new Response ( "Internal server error" , { status : 500 } ) ;
407+ }
389408} ;
390409
391410const readRequestBody = ( req : Request ) : MaybePromise < unknown > =>
@@ -400,6 +419,13 @@ const closeAssistantController = (controller: { close: () => void }) => {
400419 return true ;
401420} ;
402421
422+ const closeAssistantControllerIfPresent = ( controller : { close : ( ) => void } | null ) => {
423+ if ( ! controller ) {
424+ return null ;
425+ }
426+ return closeAssistantController ( controller ) ;
427+ } ;
428+
403429const ignoreAssistantRunError = ( _error : unknown ) => null ;
404430
405431const finalizeAssistantRun = ( run : MaybePromise < unknown > , controller : { close : ( ) => void } ) =>
0 commit comments