@@ -80,7 +80,7 @@ const agentMachineSetup = setup({
8080 } ,
8181
8282 sttSpeechDurationMet : ( { context } ) => {
83- if ( ! context . speechStartedAt ) return false ;
83+ if ( context . speechStartedAt === null ) return false ;
8484 const minDuration = context . config . interruption ?. minDurationMs ?? 200 ;
8585 return Date . now ( ) - context . speechStartedAt >= minDuration ;
8686 } ,
@@ -390,11 +390,14 @@ const agentMachineSetup = setup({
390390 } ) ,
391391 } ) ,
392392 recordHumanTurnStart : assign ( {
393- metrics : ( { context } ) => ( {
394- ...context . metrics ,
395- humanTurnStartTime : Date . now ( ) ,
396- totalTurns : context . metrics . totalTurns + 1 ,
397- } ) ,
393+ metrics : ( { context } ) => {
394+ if ( context . metrics . humanTurnStartTime !== null ) return context . metrics ;
395+ return {
396+ ...context . metrics ,
397+ humanTurnStartTime : Date . now ( ) ,
398+ totalTurns : context . metrics . totalTurns + 1 ,
399+ } ;
400+ } ,
398401 } ) ,
399402
400403 recordHumanTurnEnd : assign ( {
@@ -467,6 +470,17 @@ const agentMachineSetup = setup({
467470 } ;
468471 } ,
469472 } ) ,
473+ recordResponseLengthFromComplete : assign ( {
474+ metrics : ( { context, event } ) => {
475+ if ( event . type !== "_llm:complete" ) return context . metrics ;
476+ const responseLength = event . fullText . length ;
477+ if ( responseLength <= context . metrics . currentResponseLength ) return context . metrics ;
478+ return {
479+ ...context . metrics ,
480+ currentResponseLength : responseLength ,
481+ } ;
482+ } ,
483+ } ) ,
470484
471485 recordFirstSentence : assign ( {
472486 metrics : ( { context } ) => {
@@ -808,6 +822,7 @@ export const agentMachine = agentMachineSetup.createMachine({
808822 actions : [
809823 { type : "setHumanTurnStarted" } ,
810824 { type : "emitHumanTurnStarted" } ,
825+ { type : "recordHumanTurnStart" } ,
811826 { type : "debugLogEvent" } ,
812827 { type : "emitHumanTurnTranscript" } ,
813828 { type : "updatePartialTranscriptFromEvent" } ,
@@ -832,6 +847,7 @@ export const agentMachine = agentMachineSetup.createMachine({
832847 actions : [
833848 { type : "setHumanTurnStarted" } ,
834849 { type : "emitHumanTurnStarted" } ,
850+ { type : "recordHumanTurnStart" } ,
835851 { type : "debugLogEvent" } ,
836852 { type : "emitHumanTurnTranscript" } ,
837853 { type : "forwardToTurnDetector" } ,
@@ -860,6 +876,7 @@ export const agentMachine = agentMachineSetup.createMachine({
860876 actions : [
861877 { type : "setHumanTurnStarted" } ,
862878 { type : "emitHumanTurnStarted" } ,
879+ { type : "recordHumanTurnStart" } ,
863880 { type : "debugLogEvent" } ,
864881 { type : "recordHumanTurnEnd" } ,
865882 { type : "emitHumanTurnTranscript" } ,
@@ -920,6 +937,7 @@ export const agentMachine = agentMachineSetup.createMachine({
920937 actions : [
921938 { type : "setHumanTurnStarted" } ,
922939 { type : "emitHumanTurnStarted" } ,
940+ { type : "recordHumanTurnStart" } ,
923941 { type : "debugLogEvent" } ,
924942 { type : "recordHumanTurnEnd" } ,
925943 { type : "emitHumanTurnTranscript" } ,
@@ -1042,14 +1060,19 @@ export const agentMachine = agentMachineSetup.createMachine({
10421060 guard : "aiTurnHadNoAudio" ,
10431061 actions : [
10441062 { type : "clearLLMRef" } ,
1063+ { type : "recordResponseLengthFromComplete" } ,
10451064 { type : "recordAITurnComplete" } ,
10461065 { type : "emitAITurnEnded" } ,
10471066 { type : "addAssistantMessageFromEvent" } ,
10481067 { type : "resetTurnMetrics" } ,
10491068 ] ,
10501069 } ,
10511070 {
1052- actions : [ { type : "clearLLMRef" } , { type : "addAssistantMessageFromEvent" } ] ,
1071+ actions : [
1072+ { type : "clearLLMRef" } ,
1073+ { type : "recordResponseLengthFromComplete" } ,
1074+ { type : "addAssistantMessageFromEvent" } ,
1075+ ] ,
10531076 } ,
10541077 ] ,
10551078 } ,
@@ -1177,7 +1200,11 @@ export const agentMachine = agentMachineSetup.createMachine({
11771200 } ,
11781201 } ,
11791202 stopped : {
1180- entry : [ { type : "abortSessionController" } , { type : "emitAgentStopped" } ] ,
1203+ entry : [
1204+ { type : "abortSessionController" } ,
1205+ { type : "abortTurnController" } ,
1206+ { type : "emitAgentStopped" } ,
1207+ ] ,
11811208 type : "final" ,
11821209 } ,
11831210 } ,
0 commit comments