@@ -22,6 +22,7 @@ type App struct {
2222 firstMessage * string
2323 events chan tea.Msg
2424 throttleDuration time.Duration
25+ cancel context.CancelFunc
2526}
2627
2728func New (title , agentFilename string , rt runtime.Runtime , agents * team.Team , sess * session.Session , firstMessage * string ) * App {
@@ -50,7 +51,8 @@ func (a *App) Title() string {
5051}
5152
5253// Run one agent loop
53- func (a * App ) Run (ctx context.Context , message string ) {
54+ func (a * App ) Run (ctx context.Context , cancel context.CancelFunc , message string ) {
55+ a .cancel = cancel
5456 go func () {
5557 // Special shell command
5658 if strings .HasPrefix (message , "!" ) {
@@ -62,6 +64,9 @@ func (a *App) Run(ctx context.Context, message string) {
6264 // User message
6365 a .session .AddMessage (session .UserMessage (a .agentFilename , message ))
6466 for event := range a .runtime .RunStream (ctx , a .session ) {
67+ if ctx .Err () != nil {
68+ return
69+ }
6570 a .events <- event
6671 }
6772 }()
@@ -89,6 +94,24 @@ func (a *App) Resume(confirmationType string) {
8994 }
9095}
9196
97+ func (a * App ) NewSession () {
98+ if a .cancel != nil {
99+ a .cancel ()
100+ a .cancel = nil
101+ }
102+ a .session = session .New ()
103+ }
104+
105+ func (a * App ) CompactSession () {
106+ if a .runtime != nil && a .session != nil {
107+ events := make (chan runtime.Event , 100 )
108+ a .runtime .Summarize (context .Background (), a .session , events )
109+ for event := range events {
110+ a .events <- event
111+ }
112+ }
113+ }
114+
92115// ResumeStartOAuth resumes the runtime with OAuth authorization confirmation
93116func (a * App ) ResumeStartOAuth (confirmation bool ) {
94117 if a .runtime != nil {
0 commit comments