diff options
| author | adamdottv <[email protected]> | 2025-05-08 12:03:59 -0500 |
|---|---|---|
| committer | adamdottv <[email protected]> | 2025-05-08 12:03:59 -0500 |
| commit | f41b7bbd0a0cc731fd7c471b7ee8b26f14a21755 (patch) | |
| tree | bd34e77a07516735a220c210d4930fbe2132a63b /internal/status | |
| parent | e35ea2d448d1a3c9cf0a6fba1318e522fc61d1eb (diff) | |
| download | opencode-f41b7bbd0a0cc731fd7c471b7ee8b26f14a21755.tar.gz opencode-f41b7bbd0a0cc731fd7c471b7ee8b26f14a21755.zip | |
chore: refactoring status updates
Diffstat (limited to 'internal/status')
| -rw-r--r-- | internal/status/manager.go | 64 | ||||
| -rw-r--r-- | internal/status/status.go | 80 |
2 files changed, 144 insertions, 0 deletions
diff --git a/internal/status/manager.go b/internal/status/manager.go new file mode 100644 index 000000000..307316386 --- /dev/null +++ b/internal/status/manager.go @@ -0,0 +1,64 @@ +package status + +import ( + "log/slog" + "sync" +) + +// Manager handles status message management +type Manager struct { + service Service + mu sync.RWMutex +} + +// Global instance of the status manager +var globalManager *Manager + +// InitManager initializes the global status manager with the provided service +func InitManager(service Service) { + globalManager = &Manager{ + service: service, + } + + // Subscribe to status events for any global handling if needed + // go func() { + // ctx := context.Background() + // _ = service.Subscribe(ctx) + // }() + + slog.Debug("Status manager initialized") +} + +// GetService returns the status service from the global manager +func GetService() Service { + if globalManager == nil { + slog.Warn("Status manager not initialized, initializing with default service") + InitManager(NewService()) + } + + globalManager.mu.RLock() + defer globalManager.mu.RUnlock() + + return globalManager.service +} + +// Info publishes an info level status message using the global manager +func Info(message string) { + GetService().Info(message) +} + +// Warn publishes a warning level status message using the global manager +func Warn(message string) { + GetService().Warn(message) +} + +// Error publishes an error level status message using the global manager +func Error(message string) { + GetService().Error(message) +} + +// Debug publishes a debug level status message using the global manager +func Debug(message string) { + GetService().Debug(message) +} + diff --git a/internal/status/status.go b/internal/status/status.go new file mode 100644 index 000000000..5a6064c5b --- /dev/null +++ b/internal/status/status.go @@ -0,0 +1,80 @@ +package status + +import ( + "time" + + "github.com/opencode-ai/opencode/internal/pubsub" +) + +// Level represents the severity level of a status message +type Level string + +const ( + // LevelInfo represents an informational status message + LevelInfo Level = "info" + // LevelWarn represents a warning status message + LevelWarn Level = "warn" + // LevelError represents an error status message + LevelError Level = "error" + // LevelDebug represents a debug status message + LevelDebug Level = "debug" +) + +// StatusMessage represents a status update to be displayed in the UI +type StatusMessage struct { + Level Level `json:"level"` + Message string `json:"message"` + Timestamp time.Time `json:"timestamp"` +} + +// Service defines the interface for the status service +type Service interface { + pubsub.Suscriber[StatusMessage] + Info(message string) + Warn(message string) + Error(message string) + Debug(message string) +} + +type service struct { + *pubsub.Broker[StatusMessage] +} + +// Info publishes an info level status message +func (s *service) Info(message string) { + s.publish(LevelInfo, message) +} + +// Warn publishes a warning level status message +func (s *service) Warn(message string) { + s.publish(LevelWarn, message) +} + +// Error publishes an error level status message +func (s *service) Error(message string) { + s.publish(LevelError, message) +} + +// Debug publishes a debug level status message +func (s *service) Debug(message string) { + s.publish(LevelDebug, message) +} + +// publish creates and publishes a status message with the given level and message +func (s *service) publish(level Level, message string) { + statusMsg := StatusMessage{ + Level: level, + Message: message, + Timestamp: time.Now(), + } + s.Publish(pubsub.CreatedEvent, statusMsg) +} + +// NewService creates a new status service +func NewService() Service { + broker := pubsub.NewBroker[StatusMessage]() + return &service{ + Broker: broker, + } +} + |
