@@ -1248,4 +1248,171 @@ Earlier work
12481248 } ,
12491249 ] )
12501250 } )
1251+ test ( "aborted streaming tool call lowers to object input with paired error result" , ( ) => {
1252+ const assistant = SessionMessage . Assistant . make ( {
1253+ id : id ( "assistant-interrupted-tool" ) ,
1254+ type : "assistant" ,
1255+ agent : build ,
1256+ model : { id : Model . ID . make ( "model" ) , providerID : Provider . ID . make ( "provider" ) } ,
1257+ content : [
1258+ SessionMessage . AssistantTool . make ( {
1259+ type : "tool" ,
1260+ id : "call_interrupted" ,
1261+ name : "shell" ,
1262+ executed : false ,
1263+ state : SessionMessage . ToolStateStreaming . make ( { status : "streaming" , input : "not-json" } ) ,
1264+ time : { created } ,
1265+ } ) ,
1266+ ] ,
1267+ time : { created } ,
1268+ finish : "error" ,
1269+ error : { type : "aborted" , message : "Step interrupted" } ,
1270+ } )
1271+
1272+ const messages = toLLMMessages ( [ assistant ] , model )
1273+ expect ( messages . map ( ( message ) => message . role ) ) . toEqual ( [ "assistant" , "tool" ] )
1274+ expect ( messages [ 0 ] ?. content ) . toEqual ( [
1275+ {
1276+ type : "tool-call" ,
1277+ id : "call_interrupted" ,
1278+ name : "shell" ,
1279+ input : { } ,
1280+ providerExecuted : false ,
1281+ providerMetadata : undefined ,
1282+ } ,
1283+ ] )
1284+ expect ( messages [ 1 ] ?. content ) . toEqual ( [
1285+ {
1286+ type : "tool-result" ,
1287+ id : "call_interrupted" ,
1288+ name : "shell" ,
1289+ result : {
1290+ type : "error" ,
1291+ value : { error : { type : "aborted" , message : "Tool execution interrupted" } , content : [ ] } ,
1292+ } ,
1293+ providerExecuted : false ,
1294+ cache : undefined ,
1295+ metadata : undefined ,
1296+ providerMetadata : undefined ,
1297+ } ,
1298+ ] )
1299+ } )
1300+
1301+ test ( "aborted running tool call keeps object input with paired error result" , ( ) => {
1302+ const assistant = SessionMessage . Assistant . make ( {
1303+ id : id ( "assistant-interrupted-running" ) ,
1304+ type : "assistant" ,
1305+ agent : build ,
1306+ model : { id : Model . ID . make ( "model" ) , providerID : Provider . ID . make ( "provider" ) } ,
1307+ content : [
1308+ SessionMessage . AssistantTool . make ( {
1309+ type : "tool" ,
1310+ id : "call_running" ,
1311+ name : "shell" ,
1312+ executed : false ,
1313+ state : SessionMessage . ToolStateRunning . make ( { status : "running" , input : { command : "pwd" } , metadata : { } } ) ,
1314+ time : { created } ,
1315+ } ) ,
1316+ ] ,
1317+ time : { created } ,
1318+ finish : "error" ,
1319+ error : { type : "aborted" , message : "Tool execution interrupted" } ,
1320+ } )
1321+
1322+ const messages = toLLMMessages ( [ assistant ] , model )
1323+ expect ( messages . map ( ( message ) => message . role ) ) . toEqual ( [ "assistant" , "tool" ] )
1324+ expect ( messages [ 0 ] ?. content ) . toEqual ( [
1325+ {
1326+ type : "tool-call" ,
1327+ id : "call_running" ,
1328+ name : "shell" ,
1329+ input : { command : "pwd" } ,
1330+ providerExecuted : false ,
1331+ providerMetadata : undefined ,
1332+ } ,
1333+ ] )
1334+ expect ( messages [ 1 ] ?. content ) . toEqual ( [
1335+ {
1336+ type : "tool-result" ,
1337+ id : "call_running" ,
1338+ name : "shell" ,
1339+ result : {
1340+ type : "error" ,
1341+ value : { error : { type : "aborted" , message : "Tool execution interrupted" } , content : [ ] } ,
1342+ } ,
1343+ providerExecuted : false ,
1344+ cache : undefined ,
1345+ metadata : undefined ,
1346+ providerMetadata : undefined ,
1347+ } ,
1348+ ] )
1349+ } )
1350+
1351+ test ( "non-abort error leaves non-terminal tool call without synthesized result" , ( ) => {
1352+ const assistant = SessionMessage . Assistant . make ( {
1353+ id : id ( "assistant-non-abort" ) ,
1354+ type : "assistant" ,
1355+ agent : build ,
1356+ model : { id : Model . ID . make ( "model" ) , providerID : Provider . ID . make ( "provider" ) } ,
1357+ content : [
1358+ SessionMessage . AssistantTool . make ( {
1359+ type : "tool" ,
1360+ id : "call_pending" ,
1361+ name : "shell" ,
1362+ executed : false ,
1363+ state : SessionMessage . ToolStateStreaming . make ( { status : "streaming" , input : "not-json" } ) ,
1364+ time : { created } ,
1365+ } ) ,
1366+ ] ,
1367+ time : { created } ,
1368+ finish : "error" ,
1369+ error : { type : "unknown" , message : "Something else failed" } ,
1370+ } )
1371+
1372+ const messages = toLLMMessages ( [ assistant ] , model )
1373+ expect ( messages . map ( ( message ) => message . role ) ) . toEqual ( [ "assistant" ] )
1374+ expect ( messages [ 0 ] ?. content ) . toEqual ( [
1375+ {
1376+ type : "tool-call" ,
1377+ id : "call_pending" ,
1378+ name : "shell" ,
1379+ input : { } ,
1380+ providerExecuted : false ,
1381+ providerMetadata : undefined ,
1382+ } ,
1383+ ] )
1384+ } )
1385+ test ( "aborted streaming tool input that is not JSON object lowers to {}" , ( ) => {
1386+ const assistant = SessionMessage . Assistant . make ( {
1387+ id : id ( "assistant-aborted-streaming-min" ) ,
1388+ type : "assistant" ,
1389+ agent : build ,
1390+ model : { id : Model . ID . make ( "model" ) , providerID : Provider . ID . make ( "provider" ) } ,
1391+ content : [
1392+ SessionMessage . AssistantTool . make ( {
1393+ type : "tool" ,
1394+ id : "call_aborted" ,
1395+ name : "shell" ,
1396+ executed : false ,
1397+ state : SessionMessage . ToolStateStreaming . make ( { status : "streaming" , input : "not-json" } ) ,
1398+ time : { created } ,
1399+ } ) ,
1400+ ] ,
1401+ time : { created } ,
1402+ finish : "error" ,
1403+ error : { type : "aborted" , message : "Tool execution interrupted" } ,
1404+ } )
1405+ const messages = toLLMMessages ( [ assistant ] , model )
1406+ expect ( messages [ 0 ] ?. content ) . toEqual ( [
1407+ {
1408+ type : "tool-call" ,
1409+ id : "call_aborted" ,
1410+ name : "shell" ,
1411+ input : { } ,
1412+ providerExecuted : false ,
1413+ providerMetadata : undefined ,
1414+ } ,
1415+ ] )
1416+ } )
1417+
12511418} )
0 commit comments