@@ -260,7 +260,7 @@ describe('anthropic lifecycle instrumentation', () => {
260260 }
261261 } )
262262
263- it ( 'finishes after a resolving before subscriber leaves' , async ( ) => {
263+ it ( 'finishes after reading a raw response when the before subscriber leaves' , async ( ) => {
264264 const apmChannel = tracingChannel ( 'apm:anthropic:request' )
265265 const apmHandlers = { start ( ) { } }
266266 let asyncEndCount = 0
@@ -282,18 +282,19 @@ describe('anthropic lifecycle instrumentation', () => {
282282 messages . _nextApiPromise = apiPromise
283283
284284 try {
285- assert . strictEqual (
286- await messages . create ( { messages : [ { role : 'user' , content : 'Hi' } ] } ) . asResponse ( ) ,
287- apiPromise . _rawResponse
288- )
285+ const response = await messages . create ( { messages : [ { role : 'user' , content : 'Hi' } ] } ) . asResponse ( )
286+
287+ assert . strictEqual ( response , apiPromise . _rawResponse )
288+ assert . strictEqual ( asyncEndCount , 0 )
289+ await response . json ( )
289290 assert . strictEqual ( asyncEndCount , 1 )
290291 } finally {
291292 apmChannel . unsubscribe ( apmHandlers )
292293 unsubscribe ( )
293294 }
294295 } )
295296
296- it ( 'finishes an unconsumed raw response and evaluates it when read' , async ( ) => {
297+ it ( 'finishes a raw response with its result when read' , async ( ) => {
297298 const apmChannel = tracingChannel ( 'apm:anthropic:request' )
298299 const apmHandlers = { start ( ) { } }
299300 let asyncEndCtx
@@ -312,18 +313,42 @@ describe('anthropic lifecycle instrumentation', () => {
312313 const response = await messages . create ( { messages : [ { role : 'user' , content : 'Hi' } ] } ) . asResponse ( )
313314
314315 assert . strictEqual ( calls . length , 1 )
315- assert . ok ( asyncEndCtx , 'asyncEnd was not published' )
316- assert . strictEqual ( asyncEndCtx . finished , true )
316+ assert . strictEqual ( asyncEndCtx , undefined )
317317 assert . strictEqual ( response . bodyUsed , false )
318318 assert . deepStrictEqual ( await response . json ( ) , body )
319319 assert . strictEqual ( calls . length , 2 )
320320 assert . deepStrictEqual ( calls [ 1 ] . body , body )
321+ assert . ok ( asyncEndCtx , 'asyncEnd was not published' )
322+ assert . strictEqual ( asyncEndCtx . finished , true )
323+ assert . deepStrictEqual ( asyncEndCtx . result , body )
321324 } finally {
322325 apmChannel . unsubscribe ( apmHandlers )
323326 unsubscribe ( )
324327 }
325328 } )
326329
330+ it ( 'finishes a raw response reader when only tracing is active' , async ( ) => {
331+ const apmChannel = tracingChannel ( 'apm:anthropic:request' )
332+ const apmHandlers = { start ( ) { } }
333+ let asyncEndCtx
334+ apmHandlers . asyncEnd = ctx => { asyncEndCtx = ctx }
335+ apmChannel . subscribe ( apmHandlers )
336+
337+ const body = { role : 'assistant' , content : [ { type : 'text' , text : 'Hi' } ] }
338+ const messages = new Messages ( )
339+ messages . _nextApiPromise = new FakeAPIPromise ( body )
340+
341+ try {
342+ const response = await messages . create ( { messages : [ { role : 'user' , content : 'Hi' } ] } ) . asResponse ( )
343+
344+ assert . strictEqual ( asyncEndCtx , undefined )
345+ assert . deepStrictEqual ( await response . json ( ) , body )
346+ assert . deepStrictEqual ( asyncEndCtx . result , body )
347+ } finally {
348+ apmChannel . unsubscribe ( apmHandlers )
349+ }
350+ } )
351+
327352 it ( 'evaluates repeated asResponse() calls once' , async ( ) => {
328353 const { calls, unsubscribe } = subscribeAutoResolve ( [
329354 messagesBeforeChannel ,
@@ -377,11 +402,12 @@ describe('anthropic lifecycle instrumentation', () => {
377402 )
378403 sinon . assert . notCalled ( response . clone )
379404 assert . strictEqual ( calls . length , 0 )
380- assert . strictEqual ( asyncEndCount , 1 )
405+ assert . strictEqual ( asyncEndCount , 0 )
381406 assert . deepStrictEqual ( await response . json ( ) , body )
382407 sinon . assert . calledOnce ( readJson )
383408 assert . strictEqual ( calls . length , 1 )
384409 assert . strictEqual ( calls [ 0 ] . body , body )
410+ assert . strictEqual ( asyncEndCount , 1 )
385411 } finally {
386412 apmChannel . unsubscribe ( apmHandlers )
387413 unsubscribe ( )
@@ -508,22 +534,33 @@ describe('anthropic lifecycle instrumentation', () => {
508534 }
509535 } )
510536
511- it ( 'keeps a raw-terminal span finished when parsing starts later ' , async ( ) => {
537+ it ( 'retains the parsed result when parsing starts after raw response access ' , async ( ) => {
512538 const apmChannel = tracingChannel ( 'apm:anthropic:request' )
513539 const apmHandlers = { start ( ) { } }
514540 let asyncEndCount = 0
515- apmHandlers . asyncEnd = ( ) => { asyncEndCount ++ }
541+ let asyncEndCtx
542+ apmHandlers . asyncEnd = ctx => {
543+ asyncEndCount ++
544+ asyncEndCtx = ctx
545+ }
516546 apmChannel . subscribe ( apmHandlers )
517547
518548 const { unsubscribe } = subscribeAutoResolve ( [ messagesAfterChannel ] )
549+ const body = {
550+ role : 'assistant' ,
551+ content : [ { type : 'text' , text : 'Hi' } ] ,
552+ usage : { input_tokens : 1 , output_tokens : 2 } ,
553+ }
519554 const messages = new Messages ( )
520- messages . _nextApiPromise = new FakeAPIPromise ( { role : 'assistant' , content : [ ] } )
555+ messages . _nextApiPromise = new FakeAPIPromise ( body )
521556 const apiPromise = messages . create ( { messages : [ { role : 'user' , content : 'Hi' } ] } )
522557
523558 try {
524559 await apiPromise . asResponse ( )
525- await apiPromise . parse ( )
560+ assert . strictEqual ( asyncEndCount , 0 )
561+ assert . strictEqual ( await apiPromise . parse ( ) , body )
526562 assert . strictEqual ( asyncEndCount , 1 )
563+ assert . strictEqual ( asyncEndCtx . result , body )
527564 } finally {
528565 apmChannel . unsubscribe ( apmHandlers )
529566 unsubscribe ( )
0 commit comments