@@ -689,6 +689,145 @@ describe(`createPiAgentAdapter`, () => {
689689 )
690690 } )
691691
692+ it ( `writes reasoning rows for streamed thinking events` , async ( ) => {
693+ const events : Array < ChangeEvent > = [ ]
694+ const usage = {
695+ input : 0 ,
696+ output : 0 ,
697+ cacheRead : 0 ,
698+ cacheWrite : 0 ,
699+ totalTokens : 0 ,
700+ cost : {
701+ input : 0 ,
702+ output : 0 ,
703+ cacheRead : 0 ,
704+ cacheWrite : 0 ,
705+ total : 0 ,
706+ } ,
707+ }
708+ const startMessage : AssistantMessage = {
709+ role : `assistant` ,
710+ content : [ ] ,
711+ api : `openai-codex-responses` ,
712+ provider : `openai-codex` ,
713+ model : `gpt-5.4` ,
714+ usage,
715+ stopReason : `stop` ,
716+ timestamp : Date . now ( ) ,
717+ }
718+ const partialMessage : AssistantMessage = {
719+ ...startMessage ,
720+ content : [ { type : `thinking` , thinking : `**Updating PR**\n\n` } ] ,
721+ }
722+ const finalMessage : AssistantMessage = {
723+ ...startMessage ,
724+ content : [
725+ {
726+ type : `thinking` ,
727+ thinking : `**Updating PR**\n\nI'm updating the PR description.` ,
728+ } ,
729+ ] ,
730+ }
731+
732+ const factory = createPiAgentAdapter ( {
733+ systemPrompt : `Test system prompt` ,
734+ provider : `openai-codex` ,
735+ model : `gpt-5.4` ,
736+ tools : [ ] ,
737+ streamFn : ( ) => {
738+ const stream = createAssistantMessageEventStream ( )
739+ queueMicrotask ( ( ) => {
740+ stream . push ( { type : `start` , partial : startMessage } )
741+ stream . push ( {
742+ type : `thinking_start` ,
743+ contentIndex : 0 ,
744+ partial : partialMessage ,
745+ } )
746+ stream . push ( {
747+ type : `thinking_delta` ,
748+ contentIndex : 0 ,
749+ delta : `**Updating PR**\n\n` ,
750+ partial : partialMessage ,
751+ } )
752+ stream . push ( {
753+ type : `thinking_delta` ,
754+ contentIndex : 0 ,
755+ delta : `I'm updating the PR description.` ,
756+ partial : finalMessage ,
757+ } )
758+ stream . push ( {
759+ type : `thinking_end` ,
760+ contentIndex : 0 ,
761+ content : `**Updating PR**\n\nI'm updating the PR description.` ,
762+ partial : finalMessage ,
763+ } )
764+ stream . end ( finalMessage )
765+ } )
766+ return stream
767+ } ,
768+ } )
769+
770+ const handle = factory ( {
771+ entityUrl : `test/entity-1` ,
772+ epoch : 1 ,
773+ messages : [ ] ,
774+ outboundIdSeed : { run : 0 , step : 0 , msg : 0 , tc : 0 , reasoning : 0 } ,
775+ writeEvent : ( event : ChangeEvent ) => {
776+ events . push ( event )
777+ } ,
778+ } )
779+
780+ await handle . run ( `hello` )
781+
782+ expect ( events ) . toContainEqual (
783+ expect . objectContaining ( {
784+ type : `reasoning` ,
785+ headers : expect . objectContaining ( { operation : `insert` } ) ,
786+ key : `reasoning-0` ,
787+ value : expect . objectContaining ( {
788+ status : `streaming` ,
789+ run_id : `run-0` ,
790+ } ) ,
791+ } )
792+ )
793+ expect ( events ) . toContainEqual (
794+ expect . objectContaining ( {
795+ type : `reasoning_delta` ,
796+ headers : expect . objectContaining ( { operation : `insert` } ) ,
797+ key : `reasoning-0:0` ,
798+ value : expect . objectContaining ( {
799+ reasoning_id : `reasoning-0` ,
800+ run_id : `run-0` ,
801+ delta : `**Updating PR**\n\n` ,
802+ } ) ,
803+ } )
804+ )
805+ expect ( events ) . toContainEqual (
806+ expect . objectContaining ( {
807+ type : `reasoning_delta` ,
808+ headers : expect . objectContaining ( { operation : `insert` } ) ,
809+ key : `reasoning-0:1` ,
810+ value : expect . objectContaining ( {
811+ reasoning_id : `reasoning-0` ,
812+ run_id : `run-0` ,
813+ delta : `I'm updating the PR description.` ,
814+ } ) ,
815+ } )
816+ )
817+ expect ( events ) . toContainEqual (
818+ expect . objectContaining ( {
819+ type : `reasoning` ,
820+ headers : expect . objectContaining ( { operation : `update` } ) ,
821+ key : `reasoning-0` ,
822+ value : expect . objectContaining ( {
823+ status : `completed` ,
824+ run_id : `run-0` ,
825+ summary_title : `Updating PR` ,
826+ } ) ,
827+ } )
828+ )
829+ } )
830+
692831 it ( `isRunning returns false initially` , ( ) => {
693832 const factory = createPiAgentAdapter ( {
694833 systemPrompt : `Test system prompt` ,
0 commit comments