How to set callerid for attended transfer?

I have asterisk 13.13cert2, pjsip, a simple dialplan:

extensions = {
    ["from-local"] = {
        ["_."] = from_local
    },
    ["from-transfer"] = {
        ["_.*"] = transfer
    }
}

function from_local(context,extension)
    app.Set("TRANSFER_CONTEXT=from-transfer")
    app.Dial("PJSIP/" .. extension .. ",,tT")
end

function transfer(context, extension)
    app.Set('CALLERID(name)=Transfer')
    app.Set('CALLERID(num)=1111')
    app.Dial("PJSIP/" .. extension .. ",,tT")
end

And several endpoints:

[9716]
   type=endpoint
   transport=udp-transport
   aors=9716
   auth=9716
   context=from-local
   disallow=all
   allow=alaw
   direct_media=yes
   dtmf_mode=auto
   rewrite_contact=yes
   callerid=9716 <9716>

[9717]
   type=endpoint
   transport=udp-transport
   aors=9717
   auth=9717
   context=from-local
   disallow=all
   allow=alaw,g729,ulaw
   direct_media=yes
   dtmf_mode=auto
   rewrite_contact=yes
   callerid=9717 <9717>

[9718]
   type=endpoint
   transport=udp-transport
   aors=9718
   auth=9718
   context=from-local
   disallow=all
   allow=alaw
   direct_media=yes
   dtmf_mode=auto
   rewrite_contact=yes
   callerid=9718 <9718>

I’m calling from 9716 to 9717 and transferring to 9718. In the case of blind transfer, invite (header From:) contains the information that i set using the CALLERID function. But in the case of attended transfer, invite (header From:) contains the information of the person who made the transfer, and not what i set using the CALLERID function.
How to change callerid when use attended transfer?

I’m assuming that these are SIP transfers, not features transfers.

Asterisk cannot tell the difference between an independent call on the phone’s second line and a transfer,so there is no way for it to to associate the caller ID of the original caller, or even know that the call is being made as a transfer.

If you never make second line calls other than as part of a transfer, you might be able to use group counts to detect a second line outgoing call.

Also _. is bad practice (also matches h, e, i, t, s, etc.) and will produce a warning, and _.* is not valid dialplan pattern syntax.

This transfer by codes from features.conf:

[featuremap]
blindxfer => *2
atxfer => #

Dialplan is given only to illustrate my problem.