@@ -293,7 +293,11 @@ describe("openai completions DSML", () => {
293293 expect ( output . content ) . toEqual ( [ ] ) ;
294294 } ) ;
295295
296- it ( "emits recovered DeepSeek content-filter terminals as errors" , async ( ) => {
296+ it . each ( [
297+ { finishReason : "stop" , allowed : true } ,
298+ { finishReason : "length" , allowed : false } ,
299+ { finishReason : "content_filter" , allowed : false } ,
300+ ] ) ( "gates doubled DSML over HTTP on $finishReason" , async ( { finishReason, allowed } ) => {
297301 const server = createServer ( ( req , res ) => {
298302 req . resume ( ) ;
299303 req . on ( "end" , ( ) => {
@@ -302,17 +306,12 @@ describe("openai completions DSML", () => {
302306 "cache-control" : "no-cache" ,
303307 connection : "keep-alive" ,
304308 } ) ;
305- res . write (
306- `data: ${ JSON . stringify (
307- makeCompletionsChunk (
308- {
309- content :
310- '<|DSML|tool_calls><|DSML|invoke name="read">{"path":"/tmp/partial.md"}</|DSML|invoke></|DSML|tool_calls>' ,
311- } ,
312- "content_filter" ,
313- ) ,
314- ) } \n\n`,
315- ) ;
309+ const content =
310+ '<||DSML||tool_calls><||DSML||invoke name="exec"><||DSML||parameter name="code" string="true">return "ready";</||DSML||parameter></||DSML||invoke></||DSML||tool_calls>' ;
311+ for ( const char of content ) {
312+ res . write ( `data: ${ JSON . stringify ( makeCompletionsChunk ( { content : char } ) ) } \n\n` ) ;
313+ }
314+ res . write ( `data: ${ JSON . stringify ( makeCompletionsChunk ( { } , finishReason ) ) } \n\n` ) ;
316315 res . end ( "data: [DONE]\n\n" ) ;
317316 } ) ;
318317 } ) ;
@@ -329,42 +328,45 @@ describe("openai completions DSML", () => {
329328 ...createDeepSeekCompletionsModel ( ) ,
330329 baseUrl : `http://127.0.0.1:${ address . port } /v1` ,
331330 } ) ;
332- const stream = createOpenAICompletionsTransportStreamFn ( ) (
331+ const stream = await createOpenAICompletionsTransportStreamFn ( ) (
333332 model ,
334333 {
335334 systemPrompt : "system" ,
336335 messages : [ { role : "user" , content : "Read the file" , timestamp : Date . now ( ) } ] ,
337336 tools : [ ] ,
338- } as never ,
339- { apiKey : "test-key" } as never ,
337+ } ,
338+ { apiKey : "test-key" } ,
340339 ) ;
341340
342- const terminalEvents : Array < {
343- type : string ;
344- reason ?: string ;
345- error ?: Record < string , unknown > ;
346- } > = [ ] ;
347- for await ( const event of stream as AsyncIterable < {
348- type : string ;
349- reason ?: string ;
350- error ?: Record < string , unknown > ;
351- } > ) {
352- if ( event . type === "done" || event . type === "error" ) {
353- terminalEvents . push ( event ) ;
354- }
341+ const events = [ ] ;
342+ for await ( const event of stream ) {
343+ events . push ( event ) ;
355344 }
356-
357- expect ( terminalEvents ) . toEqual ( [
358- expect . objectContaining ( {
345+ const terminal = events . at ( - 1 ) ;
346+ if ( finishReason === "content_filter" ) {
347+ expect ( terminal ) . toMatchObject ( {
359348 type : "error" ,
360349 reason : "error" ,
361- error : expect . objectContaining ( {
350+ error : {
362351 stopReason : "error" ,
363352 errorMessage : "Provider finish_reason: content_filter" ,
364353 content : [ ] ,
365- } ) ,
366- } ) ,
367- ] ) ;
354+ } ,
355+ } ) ;
356+ } else {
357+ expect ( terminal ) . toMatchObject ( {
358+ type : "done" ,
359+ reason : allowed ? "toolUse" : "length" ,
360+ message : {
361+ stopReason : allowed ? "toolUse" : "length" ,
362+ content : allowed
363+ ? [ { type : "toolCall" , name : "exec" , arguments : { code : 'return "ready";' } } ]
364+ : [ ] ,
365+ } ,
366+ } ) ;
367+ }
368+ expect ( events . filter ( ( event ) => event . type === "toolcall_end" ) ) . toHaveLength ( allowed ? 1 : 0 ) ;
369+ expect ( events . filter ( ( event ) => event . type === "text_delta" ) ) . toEqual ( [ ] ) ;
368370 } finally {
369371 await new Promise < void > ( ( resolve , reject ) => {
370372 server . close ( ( error ) => ( error ? reject ( error ) : resolve ( ) ) ) ;
@@ -431,26 +433,51 @@ describe("openai completions DSML", () => {
431433 ] ) ;
432434 } ) ;
433435
434- it ( "does not recover malformed DeepSeek DSML tool calls" , async ( ) => {
436+ it . each ( [
437+ {
438+ name : "empty arguments" ,
439+ body : '<|DSML|invoke name="read"></|DSML|invoke>' ,
440+ } ,
441+ {
442+ name : "asymmetric invoke marker" ,
443+ body : '<|DSML|invoke name="read">{"path":"/tmp/unexecuted"}</|DSML|invoke>' ,
444+ } ,
445+ {
446+ name : "foreign invoke close" ,
447+ body : '<|DSML|invoke name="read">{"path":"/tmp/unexecuted"}</|DSML|invoke>' ,
448+ } ,
449+ {
450+ name : "asymmetric parameter marker" ,
451+ body : '<|DSML|invoke name="read"><|DSML|parameter name="path">/tmp/unexecuted</|DSML|parameter></|DSML|invoke>' ,
452+ } ,
453+ {
454+ name : "foreign parameter close" ,
455+ body : '<|DSML|invoke name="read"><|DSML|parameter name="path">/tmp/unexecuted</|DSML|parameter></|DSML|invoke>' ,
456+ } ,
457+ {
458+ name : "doubled invoke with single close" ,
459+ body : '<||DSML||invoke name="read">{"path":"/tmp/unexecuted"}</|DSML|invoke>' ,
460+ } ,
461+ {
462+ name : "mixed doubled marker" ,
463+ body : '<||DSML||invoke name="read">{"path":"/tmp/unexecuted"}</||DSML||invoke>' ,
464+ } ,
465+ ] ) ( "does not authorize DSML with $name" , async ( { body } ) => {
435466 const model = createDeepSeekCompletionsModel ( ) ;
436467 const output = createAssistantOutput ( model ) ;
437-
468+ const events : CapturedStreamEvent [ ] = [ ] ;
469+ const content = `<|DSML|tool_calls>${ body } </|DSML|tool_calls>` ;
438470 await processCompletionsStream (
439471 streamChunks ( [
440- makeCompletionsChunk (
441- {
442- content :
443- '<|DSML|tool_calls>\n<|DSML|invoke name="session_status">\n</|DSML|invoke>\n</|DSML|tool_calls>' ,
444- } ,
445- "stop" ,
446- ) ,
472+ ...Array . from ( content , ( char ) => makeCompletionsChunk ( { content : char } ) ) ,
473+ makeCompletionsChunk ( { } , "stop" ) ,
447474 ] ) ,
448475 output ,
449476 model ,
450- { push ( ) { } } ,
477+ { push : ( event ) => events . push ( event as CapturedStreamEvent ) } ,
451478 ) ;
452-
453479 expect ( output . stopReason ) . toBe ( "stop" ) ;
454480 expect ( output . content ) . toEqual ( [ ] ) ;
481+ expect ( events . filter ( ( event ) => event . type ?. startsWith ( "toolcall_" ) ) ) . toEqual ( [ ] ) ;
455482 } ) ;
456483} ) ;
0 commit comments