go/pkg/cli/log.go
Snider bfa5353205
Add streaming API to pkg/io and optimize agentic context gathering (#313)
* feat(io): add streaming API to Medium interface and optimize agentic context

- Added ReadStream and WriteStream to io.Medium interface.
- Implemented streaming methods in local and mock mediums.
- Updated pkg/agentic/context.go to use streaming I/O with LimitReader.
- Added 5000-byte truncation limit for all AI context file reads to reduce memory usage.
- Documented when to use streaming vs full-file APIs in io.Medium.

* feat(io): optimize streaming API and fix PR feedback

- Fixed resource leak in agentic context by using defer for closing file streams.
- Improved truncation logic in agentic context to handle multibyte characters correctly by checking byte length before string conversion.
- Added comprehensive documentation to ReadStream and WriteStream in local medium.
- Added unit tests for ReadStream and WriteStream in local medium.
- Applied formatting and fixed auto-merge CI configuration.

* feat(io): add streaming API and fix CI failures (syntax fix)

- Introduced ReadStream and WriteStream to io.Medium interface.
- Implemented streaming methods in local and mock mediums.
- Optimized agentic context with streaming reads and truncation logic.
- Fixed syntax error in local client tests by overwriting the file.
- Fixed auto-merge CI by adding checkout and repository context.
- Applied formatting fixes.
2026-02-05 11:00:49 +00:00

115 lines
2.9 KiB
Go

package cli
import (
"github.com/host-uk/core/pkg/framework"
"github.com/host-uk/core/pkg/log"
)
// LogLevel aliases for backwards compatibility.
type LogLevel = log.Level
// Log level constants aliased from the log package.
const (
// LogLevelQuiet suppresses all output.
LogLevelQuiet = log.LevelQuiet
// LogLevelError shows only error messages.
LogLevelError = log.LevelError
// LogLevelWarn shows warnings and errors.
LogLevelWarn = log.LevelWarn
// LogLevelInfo shows info, warnings, and errors.
LogLevelInfo = log.LevelInfo
// LogLevelDebug shows all messages including debug.
LogLevelDebug = log.LevelDebug
)
// LogService wraps log.Service with CLI styling.
type LogService struct {
*log.Service
}
// LogOptions configures the log service.
type LogOptions = log.Options
// NewLogService creates a log service factory with CLI styling.
func NewLogService(opts LogOptions) func(*framework.Core) (any, error) {
return func(c *framework.Core) (any, error) {
// Create the underlying service
factory := log.NewService(opts)
svc, err := factory(c)
if err != nil {
return nil, err
}
logSvc := svc.(*log.Service)
// Apply CLI styles
logSvc.StyleTimestamp = func(s string) string { return DimStyle.Render(s) }
logSvc.StyleDebug = func(s string) string { return DimStyle.Render(s) }
logSvc.StyleInfo = func(s string) string { return InfoStyle.Render(s) }
logSvc.StyleWarn = func(s string) string { return WarningStyle.Render(s) }
logSvc.StyleError = func(s string) string { return ErrorStyle.Render(s) }
logSvc.StyleSecurity = func(s string) string { return SecurityStyle.Render(s) }
return &LogService{Service: logSvc}, nil
}
}
// --- Package-level convenience ---
// Log returns the CLI's log service, or nil if not available.
func Log() *LogService {
if instance == nil {
return nil
}
svc, err := framework.ServiceFor[*LogService](instance.core, "log")
if err != nil {
return nil
}
return svc
}
// LogDebug logs a debug message if log service is available.
func LogDebug(msg string) {
if l := Log(); l != nil {
l.Debug(msg)
}
}
// LogInfo logs an info message if log service is available.
func LogInfo(msg string) {
if l := Log(); l != nil {
l.Info(msg)
}
}
// LogWarn logs a warning message if log service is available.
func LogWarn(msg string) {
if l := Log(); l != nil {
l.Warn(msg)
}
}
// LogError logs an error message if log service is available.
func LogError(msg string) {
if l := Log(); l != nil {
l.Error(msg)
}
}
// LogSecurity logs a security message if log service is available.
func LogSecurity(msg string, keyvals ...any) {
if l := Log(); l != nil {
// Ensure user context is included if not already present
hasUser := false
for i := 0; i < len(keyvals); i += 2 {
if keyvals[i] == "user" {
hasUser = true
break
}
}
if !hasUser {
keyvals = append(keyvals, "user", log.Username())
}
l.Security(msg, keyvals...)
}
}