@@ -1742,6 +1742,149 @@ describe('Chat', () => {
1742
1742
` ) ;
1743
1743
} ) ;
1744
1744
1745
+ it ( 'should send message when a tool error result is submitted' , async ( ) => {
1746
+ server . urls [ 'http://localhost:3000/api/chat' ] . response = [
1747
+ {
1748
+ type : 'stream-chunks' ,
1749
+ chunks : [
1750
+ formatChunk ( { type : 'start' } ) ,
1751
+ formatChunk ( { type : 'start-step' } ) ,
1752
+ formatChunk ( {
1753
+ type : 'tool-input-available' ,
1754
+ toolCallId : 'tool-call-0' ,
1755
+ toolName : 'test-tool' ,
1756
+ input : { testArg : 'test-value' } ,
1757
+ } ) ,
1758
+ formatChunk ( { type : 'finish-step' } ) ,
1759
+ formatChunk ( { type : 'finish' } ) ,
1760
+ ] ,
1761
+ } ,
1762
+ {
1763
+ type : 'stream-chunks' ,
1764
+ chunks : [
1765
+ formatChunk ( { type : 'start' } ) ,
1766
+ formatChunk ( { type : 'start-step' } ) ,
1767
+ formatChunk ( { type : 'finish-step' } ) ,
1768
+ formatChunk ( { type : 'finish' } ) ,
1769
+ ] ,
1770
+ } ,
1771
+ ] ;
1772
+
1773
+ let callCount = 0 ;
1774
+ const onFinishPromise = createResolvablePromise < void > ( ) ;
1775
+
1776
+ const chat = new TestChat ( {
1777
+ id : '123' ,
1778
+ generateId : mockId ( ) ,
1779
+ transport : new DefaultChatTransport ( {
1780
+ api : 'http://localhost:3000/api/chat' ,
1781
+ } ) ,
1782
+ sendAutomaticallyWhen : lastAssistantMessageIsCompleteWithToolCalls ,
1783
+ onFinish : ( ) => {
1784
+ callCount ++ ;
1785
+ if ( callCount === 2 ) {
1786
+ onFinishPromise . resolve ( ) ;
1787
+ }
1788
+ } ,
1789
+ } ) ;
1790
+
1791
+ await chat . sendMessage ( {
1792
+ text : 'Hello, world!' ,
1793
+ } ) ;
1794
+
1795
+ // user submits the tool result
1796
+ await chat . addToolResult ( {
1797
+ state : 'output-error' ,
1798
+ tool : 'test-tool' ,
1799
+ toolCallId : 'tool-call-0' ,
1800
+ errorText : 'test-error' ,
1801
+ } ) ;
1802
+
1803
+ // UI should show the tool result
1804
+ expect ( chat . messages ) . toMatchInlineSnapshot ( `
1805
+ [
1806
+ {
1807
+ "id": "id-0",
1808
+ "metadata": undefined,
1809
+ "parts": [
1810
+ {
1811
+ "text": "Hello, world!",
1812
+ "type": "text",
1813
+ },
1814
+ ],
1815
+ "role": "user",
1816
+ },
1817
+ {
1818
+ "id": "id-1",
1819
+ "metadata": undefined,
1820
+ "parts": [
1821
+ {
1822
+ "type": "step-start",
1823
+ },
1824
+ {
1825
+ "errorText": "test-error",
1826
+ "input": {
1827
+ "testArg": "test-value",
1828
+ },
1829
+ "output": undefined,
1830
+ "preliminary": undefined,
1831
+ "providerExecuted": undefined,
1832
+ "rawInput": undefined,
1833
+ "state": "output-error",
1834
+ "toolCallId": "tool-call-0",
1835
+ "type": "tool-test-tool",
1836
+ },
1837
+ ],
1838
+ "role": "assistant",
1839
+ },
1840
+ ]
1841
+ ` ) ;
1842
+
1843
+ await onFinishPromise . promise ;
1844
+
1845
+ // 2nd call should happen after the stream is finished
1846
+ expect ( server . calls . length ) . toBe ( 2 ) ;
1847
+
1848
+ // check details of the 2nd call
1849
+ expect ( await server . calls [ 1 ] . requestBodyJson ) . toMatchInlineSnapshot ( `
1850
+ {
1851
+ "id": "123",
1852
+ "messageId": "id-1",
1853
+ "messages": [
1854
+ {
1855
+ "id": "id-0",
1856
+ "parts": [
1857
+ {
1858
+ "text": "Hello, world!",
1859
+ "type": "text",
1860
+ },
1861
+ ],
1862
+ "role": "user",
1863
+ },
1864
+ {
1865
+ "id": "id-1",
1866
+ "parts": [
1867
+ {
1868
+ "type": "step-start",
1869
+ },
1870
+ {
1871
+ "errorText": "test-error",
1872
+ "input": {
1873
+ "testArg": "test-value",
1874
+ },
1875
+ "state": "output-error",
1876
+ "toolCallId": "tool-call-0",
1877
+ "type": "tool-test-tool",
1878
+ },
1879
+ ],
1880
+ "role": "assistant",
1881
+ },
1882
+ ],
1883
+ "trigger": "submit-message",
1884
+ }
1885
+ ` ) ;
1886
+ } ) ;
1887
+
1745
1888
it ( 'should send message when a dynamic tool result is submitted' , async ( ) => {
1746
1889
server . urls [ 'http://localhost:3000/api/chat' ] . response = [
1747
1890
{
@@ -1795,6 +1938,7 @@ describe('Chat', () => {
1795
1938
1796
1939
// user submits the tool result
1797
1940
await chat . addToolResult ( {
1941
+ state : 'output-available' ,
1798
1942
tool : 'test-tool' ,
1799
1943
toolCallId : 'tool-call-0' ,
1800
1944
output : 'test-result' ,
0 commit comments