@@ -211,6 +211,42 @@ export function enablePipelineStreaming(
211211 context . generation || 'none' ,
212212 ] . join ( ':' ) ;
213213
214+ const normalizeToolExecutionEvent = ( event : any ) => {
215+ const type = String ( event ?. type || '' ) ;
216+ if ( type === 'tool-use' ) {
217+ return {
218+ toolName : event ?. data ?. tool || event ?. metadata ?. toolName || 'tool' ,
219+ input : event ?. data ?. input ?? null ,
220+ output : event ?. data ?. output ?? null ,
221+ error : event ?. data ?. error ?? null ,
222+ } ;
223+ }
224+
225+ const isStoredToolResult =
226+ type === 'status' &&
227+ event ?. namespace === 'tools' &&
228+ event ?. data &&
229+ typeof event . data === 'object' &&
230+ event . data . tool ;
231+
232+ if ( ! isStoredToolResult ) {
233+ return null ;
234+ }
235+
236+ return {
237+ toolName : event . data . tool ,
238+ input : event . data . input ?? null ,
239+ output : event . data . output ?? null ,
240+ error : event . data . error ?? null ,
241+ } ;
242+ } ;
243+
244+ const normalizeToolError = ( error : unknown ) => {
245+ if ( ! error ) return null ;
246+ if ( typeof error === 'object' ) return error ;
247+ return { message : String ( error ) } ;
248+ } ;
249+
214250 const markDeliverableRun = async ( status : 'completed' | 'failed' , payload ?: any ) => {
215251 if ( ! ( await ensureDeliverableRun ( ) ) ) return ;
216252 const now = new Date ( ) . toISOString ( ) ;
@@ -261,6 +297,23 @@ export function enablePipelineStreaming(
261297
262298 await persistDeliverableEvent ( event , phaseName , agentName ) ;
263299
300+ const structuredTool = normalizeToolExecutionEvent ( event ) ;
301+ if ( structuredTool && await ensureDeliverableRun ( ) ) {
302+ const phaseId = phaseState . currentPhaseId || ( phaseName ? phaseIdByName . get ( phaseName ) : null ) ;
303+ const key = agentStepKey ( phaseId ?? null , phaseName , agentName , stepType ) ;
304+ const agentStepId = agentStepMap . get ( key ) || null ;
305+ const row : import ( '../types/db' ) . DPToolExecInsert = {
306+ agent_step_id : agentStepId ,
307+ substep_id : null ,
308+ tool_name : structuredTool . toolName as any ,
309+ tool_input : structuredTool . input as any ,
310+ tool_output : structuredTool . output as any ,
311+ tool_error : normalizeToolError ( structuredTool . error ) as any ,
312+ created_at : now as any
313+ } as any ;
314+ await insertRow ( 'deliverable_pipeline_tool_executions' , row ) ;
315+ }
316+
264317 if ( type === 'phase-start' ) {
265318 if ( ! ( phaseName && await ensureDeliverableRun ( ) ) ) return ;
266319 const row : import ( '../types/db' ) . DPPhaseDelegationInsert = {
@@ -350,25 +403,6 @@ export function enablePipelineStreaming(
350403 created_at : now as any
351404 } as any ;
352405 await insertRow ( 'deliverable_pipeline_generations' , row ) ;
353- } else if ( type === 'tool-use' ) {
354- if ( ! ( await ensureDeliverableRun ( ) ) ) return ;
355- const phaseId = phaseState . currentPhaseId || ( phaseName ? phaseIdByName . get ( phaseName ) : null ) ;
356- const key = agentStepKey ( phaseId ?? null , phaseName , agentName , stepType ) ;
357- const agentStepId = agentStepMap . get ( key ) || null ;
358- const toolName = event ?. data ?. tool || event ?. metadata ?. toolName || 'tool' ;
359- const toolInput = event ?. data ?. input || null ;
360- const toolOutput = event ?. data ?. output || null ;
361- const toolError = event ?. data ?. error ? { message : event . data . error } : null ;
362- const row : import ( '../types/db' ) . DPToolExecInsert = {
363- agent_step_id : agentStepId ,
364- substep_id : null ,
365- tool_name : toolName as any ,
366- tool_input : toolInput as any ,
367- tool_output : toolOutput as any ,
368- tool_error : toolError as any ,
369- created_at : now as any
370- } as any ;
371- await insertRow ( 'deliverable_pipeline_tool_executions' , row ) ;
372406 } else if ( type === 'status' ) {
373407 if ( event ?. namespace === 'llm' && event ?. key === 'input' ) {
374408 const messages = event ?. data ?. messages ;
0 commit comments