@@ -1699,6 +1699,120 @@ describe("openai-completions stop-reason tool-call guard", () => {
16991699 } ) ;
17001700 } ) ;
17011701
1702+ it ( "replaces the stored function name on a same-id fragmented continuation" , async ( ) => {
1703+ // A stable tool-call id that repeats across deltas signals a continuation of
1704+ // the same call, so the latest nonempty function-name snapshot must replace
1705+ // the stored name — mirroring the pinned OpenAI SDK and the managed
1706+ // transport. Without this the first fragment (e.g. "get_") freezes and the
1707+ // call is published under a stale, partial name.
1708+ mockChunksRef . chunks = [
1709+ makeToolCallChunk ( "call_name" , "get_" , '{"city":' ) ,
1710+ makeToolCallChunk ( "call_name" , "weather" , '"Paris"}' ) ,
1711+ makeFinishChunk ( "tool_calls" ) ,
1712+ ] ;
1713+
1714+ const stream = streamOpenAICompletions ( model , context , { apiKey : "sk-test" } ) ;
1715+ const result = await stream . result ( ) ;
1716+
1717+ expect ( result . content ) . toContainEqual ( {
1718+ type : "toolCall" ,
1719+ id : "call_name" ,
1720+ name : "weather" ,
1721+ arguments : { city : "Paris" } ,
1722+ } ) ;
1723+ } ) ;
1724+
1725+ it ( "replaces the stored name when an index-resolved continuation omits the id" , async ( ) => {
1726+ // A continuation delta that re-uses the established index but omits the id
1727+ // is still the same tool call — the block was already resolved by index
1728+ // above, so an absent id is a continuation, not a conflicting identity. The
1729+ // latest nonempty function-name snapshot must replace the stored name,
1730+ // matching the pinned OpenAI SDK accumulator. Only an explicitly conflicting
1731+ // id (next test) keeps the first name.
1732+ mockChunksRef . chunks = [
1733+ makeToolCallChunk ( "call_A" , "first" , '{"city":' ) ,
1734+ {
1735+ id : "chatcmpl-test" ,
1736+ choices : [
1737+ {
1738+ index : 0 ,
1739+ delta : {
1740+ tool_calls : [
1741+ { index : 0 , function : { name : "second" , arguments : '"Paris"}' } , type : "function" } ,
1742+ ] ,
1743+ } ,
1744+ finish_reason : "tool_calls" ,
1745+ } ,
1746+ ] ,
1747+ } ,
1748+ ] ;
1749+
1750+ const stream = streamOpenAICompletions ( model , context , { apiKey : "sk-test" } ) ;
1751+ const result = await stream . result ( ) ;
1752+
1753+ expect ( result . content ) . toContainEqual ( {
1754+ type : "toolCall" ,
1755+ id : "call_A" ,
1756+ name : "second" ,
1757+ arguments : { city : "Paris" } ,
1758+ } ) ;
1759+ } ) ;
1760+
1761+ it ( "keeps the first name when a continuation carries a conflicting id" , async ( ) => {
1762+ // A continuation whose id explicitly conflicts with the established block
1763+ // id is not the same call, so in direct mode the first tool identity wins
1764+ // and the stored name is preserved.
1765+ mockChunksRef . chunks = [
1766+ makeToolCallChunk ( "call_A" , "first" , '{"city":' ) ,
1767+ makeToolCallChunk ( "call_B" , "second" , '"Paris"}' ) ,
1768+ makeFinishChunk ( "tool_calls" ) ,
1769+ ] ;
1770+
1771+ const stream = streamOpenAICompletions ( model , context , { apiKey : "sk-test" } ) ;
1772+ const result = await stream . result ( ) ;
1773+
1774+ expect ( result . content ) . toContainEqual ( {
1775+ type : "toolCall" ,
1776+ id : "call_A" ,
1777+ name : "first" ,
1778+ arguments : { city : "Paris" } ,
1779+ } ) ;
1780+ } ) ;
1781+
1782+ it ( "does not erase the stored name when a continuation carries an empty name" , async ( ) => {
1783+ // An empty function-name snapshot must not replace the stored name — the
1784+ // guard checks truthiness, so "" is skipped. This mirrors the pinned SDK
1785+ // (which only replaces on nonempty fn.name) and satisfies the issue's
1786+ // "empty-name continuation" assertion requirement.
1787+ mockChunksRef . chunks = [
1788+ makeToolCallChunk ( "call_name" , "get_weather" , '{"city":' ) ,
1789+ {
1790+ id : "chatcmpl-test" ,
1791+ choices : [
1792+ {
1793+ index : 0 ,
1794+ delta : {
1795+ tool_calls : [
1796+ { index : 0 , function : { name : "" , arguments : '"Paris"}' } , type : "function" } ,
1797+ ] ,
1798+ } ,
1799+ finish_reason : "tool_calls" ,
1800+ } ,
1801+ ] ,
1802+ } ,
1803+ ] ;
1804+
1805+ const stream = streamOpenAICompletions ( model , context , { apiKey : "sk-test" } ) ;
1806+ const result = await stream . result ( ) ;
1807+
1808+ expect ( result . content ) . toContainEqual ( {
1809+ type : "toolCall" ,
1810+ id : "call_name" ,
1811+ name : "get_weather" ,
1812+ arguments : { city : "Paris" } ,
1813+ } ) ;
1814+ } ) ;
1815+
17021816 it ( "publishes post-tool text immediately and closes blocks in their original order" , async ( ) => {
17031817 mockChunksRef . chunks = [
17041818 makeToolCallChunk ( "call_1" , "lookup" , '{"value":1}' ) ,
0 commit comments