xonecas.io
6119
My

mysis / files

mysis / internal / cli / autoplay.go

3.3 KB
1package cli
2
3import (
4	"context"
5	"fmt"
6	"os"
7	"strings"
8	"time"
9
10	"github.com/rs/zerolog/log"
11	"github.com/xonecas/mysis/internal/constants"
12	"github.com/xonecas/mysis/internal/features"
13	"github.com/xonecas/mysis/internal/provider"
14	"github.com/xonecas/mysis/internal/styles"
15)
16
17// initAutoplayService initializes the autoplay service with CLI-specific callbacks.
18// This should be called once when creating the App.
19func (app *App) initAutoplayService() {
20	app.autoplayService = features.NewAutoplayService(features.AutoplayCallbacks{
21		OnStarted: func(message string, interval time.Duration) {
22			fmt.Println(styles.Secondary.Render(fmt.Sprintf("Autoplay started: \"%s\"", message)))
23			fmt.Println(styles.Muted.Render(fmt.Sprintf("Interval: %ds (%d avg tool calls × %ds/tick)",
24				int(interval.Seconds()),
25				constants.AvgToolCallsPerTurn,
26				int(constants.GameTickDuration.Seconds()))))
27			fmt.Println(styles.Muted.Render("Type '/autoplay stop' to stop"))
28			fmt.Println()
29		},
30		OnStopped: func() {
31			fmt.Println(styles.Muted.Render("Autoplay stopped"))
32		},
33		OnTurn: func(ctx context.Context, message string) error {
34			fmt.Println(styles.Muted.Render("─── Autoplay Turn ───"))
35			fmt.Println(styles.Brand.Render("> ") + message)
36			log.Debug().Msg("About to process turn")
37
38			// Send autoplay message
39			userMsg := provider.Message{
40				Role:    "user",
41				Content: message,
42			}
43
44			app.mu.Lock()
45			app.history = append(app.history, userMsg)
46			app.mu.Unlock()
47
48			if err := app.sessionMgr.SaveMessage(app.sessionID, userMsg); err != nil {
49				log.Warn().Err(err).Msg("Failed to save autoplay message")
50			}
51
52			// Process turn
53			if err := app.processTurn(ctx); err != nil {
54				fmt.Fprintln(os.Stderr, styles.Error.Render("Error: "+err.Error()))
55				// Don't stop autoplay on errors - just log and continue
56				log.Warn().Err(err).Msg("Autoplay turn failed, continuing...")
57			}
58
59			fmt.Println() // Blank line after response
60			return nil
61		},
62		OnError: func(err error) {
63			log.Error().Err(err).Msg("Autoplay error")
64		},
65	})
66}
67
68// startAutoplayFromFlag starts autoplay from CLI flag.
69func (app *App) startAutoplayFromFlag(ctx context.Context, message string) error {
70	return app.autoplayService.Start(ctx, message)
71}
72
73// handleAutoplayCommand handles /autoplay commands
74func (app *App) handleAutoplayCommand(ctx context.Context, input string) error {
75	parts := strings.Fields(input)
76
77	if len(parts) == 1 {
78		// Just "/autoplay" - show status
79		status := app.autoplayService.Status()
80		if status.Enabled {
81			fmt.Println(styles.Secondary.Render(fmt.Sprintf("Autoplay active: \"%s\"", status.Message)))
82		} else {
83			fmt.Println(styles.Muted.Render("Autoplay not active"))
84			fmt.Println(styles.Muted.Render("Usage: /autoplay <message>"))
85		}
86		return nil
87	}
88
89	// Check for "stop" subcommand
90	if parts[1] == "stop" {
91		if err := app.autoplayService.Stop(); err != nil {
92			fmt.Println(styles.Muted.Render(err.Error()))
93		} else {
94			fmt.Println(styles.Success.Render("Autoplay stopped"))
95		}
96		return nil
97	}
98
99	// Join all parts after /autoplay as the message
100	message := strings.Join(parts[1:], " ")
101
102	if message == "" {
103		return fmt.Errorf("missing message for autoplay")
104	}
105
106	// Start autoplay
107	if err := app.autoplayService.Start(ctx, message); err != nil {
108		return fmt.Errorf("%s - use '/autoplay stop' first", err.Error())
109	}
110
111	return nil
112}