99)
1010
1111// GetFinalAssistantMessage waits for and returns the final assistant message from a session turn.
12- func GetFinalAssistantMessage (ctx context.Context , session * copilot.Session ) (* copilot.SessionEvent , error ) {
12+ // If alreadyIdle is true, skip waiting for session.idle (useful for resumed sessions where the
13+ // idle event was ephemeral and not persisted in the event history).
14+ func GetFinalAssistantMessage (ctx context.Context , session * copilot.Session , alreadyIdle ... bool ) (* copilot.SessionEvent , error ) {
1315 result := make (chan * copilot.SessionEvent , 1 )
1416 errCh := make (chan error , 1 )
1517
@@ -34,8 +36,9 @@ func GetFinalAssistantMessage(ctx context.Context, session *copilot.Session) (*c
3436 defer unsubscribe ()
3537
3638 // Also check existing messages in case the response already arrived
39+ isAlreadyIdle := len (alreadyIdle ) > 0 && alreadyIdle [0 ]
3740 go func () {
38- existing , err := getExistingFinalResponse (ctx , session )
41+ existing , err := getExistingFinalResponse (ctx , session , isAlreadyIdle )
3942 if err != nil {
4043 errCh <- err
4144 return
@@ -90,7 +93,7 @@ func GetNextEventOfType(session *copilot.Session, eventType copilot.SessionEvent
9093 }
9194}
9295
93- func getExistingFinalResponse (ctx context.Context , session * copilot.Session ) (* copilot.SessionEvent , error ) {
96+ func getExistingFinalResponse (ctx context.Context , session * copilot.Session , alreadyIdle bool ) (* copilot.SessionEvent , error ) {
9497 messages , err := session .GetMessages (ctx )
9598 if err != nil {
9699 return nil , err
@@ -125,10 +128,14 @@ func getExistingFinalResponse(ctx context.Context, session *copilot.Session) (*c
125128
126129 // Find session.idle and get last assistant message before it
127130 sessionIdleIndex := - 1
128- for i , msg := range currentTurnMessages {
129- if msg .Type == "session.idle" {
130- sessionIdleIndex = i
131- break
131+ if alreadyIdle {
132+ sessionIdleIndex = len (currentTurnMessages )
133+ } else {
134+ for i , msg := range currentTurnMessages {
135+ if msg .Type == "session.idle" {
136+ sessionIdleIndex = i
137+ break
138+ }
132139 }
133140 }
134141
0 commit comments