task

package
v1.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultManager = NewManager()

DefaultManager is the global default task manager

Functions

func GenerateID added in v1.2.0

func GenerateID() string

GenerateID creates a short random ID (exported for agent tasks)

Types

type AgentTask added in v1.2.0

type AgentTask struct {
	ID          string     // Unique task ID
	AgentName   string     // Name of the agent type (Explore, Plan, etc.)
	Description string     // Brief description of the task
	Status      TaskStatus // Current status
	StartTime   time.Time  // When the task started
	EndTime     time.Time  // When the task ended (if completed)
	TurnCount   int        // Number of conversation turns
	TokenUsage  int        // Total tokens consumed
	Error       string     // Error message (if failed)
	// contains filtered or unexported fields
}

AgentTask represents a background agent task It implements the BackgroundTask interface

func NewAgentTask added in v1.2.0

func NewAgentTask(id, agentName, description string, ctx context.Context, cancel context.CancelFunc) *AgentTask

NewAgentTask creates a new agent task

func (*AgentTask) AppendOutput added in v1.2.0

func (t *AgentTask) AppendOutput(data []byte)

AppendOutput appends data to the output buffer and notifies subscribers

func (*AgentTask) AppendProgress added in v1.2.0

func (t *AgentTask) AppendProgress(msg string)

AppendProgress appends a progress message and notifies subscribers

func (*AgentTask) Complete added in v1.2.0

func (t *AgentTask) Complete(err error)

Complete marks the task as completed and notifies subscribers

func (*AgentTask) GetCancel added in v1.2.0

func (t *AgentTask) GetCancel() context.CancelFunc

GetCancel returns the task's cancel function

func (*AgentTask) GetContext added in v1.2.0

func (t *AgentTask) GetContext() context.Context

GetContext returns the task's context

func (*AgentTask) GetDescription added in v1.2.0

func (t *AgentTask) GetDescription() string

GetDescription returns the task description

func (*AgentTask) GetID added in v1.2.0

func (t *AgentTask) GetID() string

GetID returns the unique task identifier

func (*AgentTask) GetOutput added in v1.2.0

func (t *AgentTask) GetOutput() string

GetOutput returns the current output

func (*AgentTask) GetStatus added in v1.2.0

func (t *AgentTask) GetStatus() TaskInfo

GetStatus returns the current task status info

func (*AgentTask) GetType added in v1.2.0

func (t *AgentTask) GetType() TaskType

GetType returns the task type

func (*AgentTask) IsRunning added in v1.2.0

func (t *AgentTask) IsRunning() bool

IsRunning returns true if the task is still running

func (*AgentTask) Kill added in v1.2.0

func (t *AgentTask) Kill() error

Kill forcefully terminates the task

func (*AgentTask) MarkKilled added in v1.2.0

func (t *AgentTask) MarkKilled()

MarkKilled marks the task as killed (internal use)

func (*AgentTask) Stop added in v1.2.0

func (t *AgentTask) Stop() error

Stop gracefully stops the task by canceling the context

func (*AgentTask) Subscribe added in v1.2.0

func (t *AgentTask) Subscribe() <-chan ProgressUpdate

Subscribe returns a channel that receives progress updates The channel is closed when the task completes

func (*AgentTask) UpdateProgress added in v1.2.0

func (t *AgentTask) UpdateProgress(turnCount, tokenUsage int)

UpdateProgress updates the turn count and token usage

func (*AgentTask) WaitForCompletion added in v1.2.0

func (t *AgentTask) WaitForCompletion(timeout time.Duration) bool

WaitForCompletion waits until the task completes or timeout Returns true if completed, false if timeout

type BackgroundTask added in v1.2.0

type BackgroundTask interface {
	// GetID returns the unique task identifier
	GetID() string

	// GetType returns the task type (bash or agent)
	GetType() TaskType

	// GetDescription returns a brief description of the task
	GetDescription() string

	// GetStatus returns the current task status info
	GetStatus() TaskInfo

	// IsRunning returns true if the task is still running
	IsRunning() bool

	// WaitForCompletion waits until the task completes or timeout
	// Returns true if completed, false if timeout
	WaitForCompletion(timeout time.Duration) bool

	// Stop gracefully stops the task (SIGTERM for bash, context cancel for agent)
	Stop() error

	// Kill forcefully terminates the task (SIGKILL for bash)
	Kill() error

	// AppendOutput appends data to the output buffer
	AppendOutput(data []byte)

	// GetOutput returns the current output
	GetOutput() string
}

BackgroundTask is the common interface for all background task types Both BashTask and AgentTask implement this interface

type BashTask added in v1.2.0

type BashTask struct {
	ID          string             // Unique task ID
	Command     string             // The command being executed
	Description string             // Brief description
	Status      TaskStatus         // Current status
	PID         int                // Process ID
	StartTime   time.Time          // When the task started
	EndTime     time.Time          // When the task ended (if completed)
	ExitCode    int                // Exit code (if completed)
	Error       string             // Error message (if failed)
	Cmd         *exec.Cmd          // The running command
	Ctx         context.Context    // Task context
	Cancel      context.CancelFunc // Cancel function
	// contains filtered or unexported fields
}

BashTask represents a background bash command task It implements the BackgroundTask interface

func NewBashTask added in v1.2.0

func NewBashTask(id, command, description string, cmd *exec.Cmd, ctx context.Context, cancel context.CancelFunc) *BashTask

NewBashTask creates a new bash task

func (*BashTask) AppendOutput added in v1.2.0

func (t *BashTask) AppendOutput(data []byte)

AppendOutput appends data to the output buffer

func (*BashTask) Complete added in v1.2.0

func (t *BashTask) Complete(exitCode int, err error)

Complete marks the task as completed

func (*BashTask) GetDescription added in v1.2.0

func (t *BashTask) GetDescription() string

GetDescription returns the task description

func (*BashTask) GetID added in v1.2.0

func (t *BashTask) GetID() string

GetID returns the unique task identifier

func (*BashTask) GetOutput added in v1.2.0

func (t *BashTask) GetOutput() string

GetOutput returns the current output

func (*BashTask) GetStatus added in v1.2.0

func (t *BashTask) GetStatus() TaskInfo

GetStatus returns the current task status info

func (*BashTask) GetType added in v1.2.0

func (t *BashTask) GetType() TaskType

GetType returns the task type

func (*BashTask) IsRunning added in v1.2.0

func (t *BashTask) IsRunning() bool

IsRunning returns true if the task is still running

func (*BashTask) Kill added in v1.2.0

func (t *BashTask) Kill() error

Kill forcefully terminates the task (SIGKILL)

func (*BashTask) MarkKilled added in v1.2.0

func (t *BashTask) MarkKilled()

MarkKilled marks the task as killed (internal use)

func (*BashTask) Stop added in v1.2.0

func (t *BashTask) Stop() error

Stop gracefully stops the task (SIGTERM)

func (*BashTask) WaitForCompletion added in v1.2.0

func (t *BashTask) WaitForCompletion(timeout time.Duration) bool

WaitForCompletion waits until the task completes or timeout Returns true if completed, false if timeout

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager manages background tasks

func NewManager

func NewManager() *Manager

NewManager creates a new task manager

func (*Manager) Cleanup

func (m *Manager) Cleanup(maxAge time.Duration)

Cleanup removes all completed tasks older than maxAge

func (*Manager) Create

func (m *Manager) Create(cmd *exec.Cmd, command, description string, ctx context.Context, cancel context.CancelFunc) *BashTask

Create is an alias for CreateBashTask for backward compatibility Deprecated: Use CreateBashTask instead

func (*Manager) CreateBashTask added in v1.2.0

func (m *Manager) CreateBashTask(cmd *exec.Cmd, command, description string, ctx context.Context, cancel context.CancelFunc) *BashTask

CreateBashTask creates and registers a new bash task

func (*Manager) Get

func (m *Manager) Get(id string) (BackgroundTask, bool)

Get retrieves a task by ID

func (*Manager) GetBashTask added in v1.2.0

func (m *Manager) GetBashTask(id string) (*BashTask, bool)

GetBashTask retrieves a bash task by ID (for backward compatibility)

func (*Manager) Kill

func (m *Manager) Kill(id string) error

Kill terminates a task by ID

func (*Manager) List

func (m *Manager) List() []BackgroundTask

List returns all tasks

func (*Manager) ListRunning

func (m *Manager) ListRunning() []BackgroundTask

ListRunning returns all running tasks

func (*Manager) RegisterTask added in v1.2.0

func (m *Manager) RegisterTask(task BackgroundTask)

RegisterTask registers an existing task (used for agent tasks)

func (*Manager) Remove

func (m *Manager) Remove(id string)

Remove removes a completed task from the manager

type ProgressUpdate added in v1.2.0

type ProgressUpdate struct {
	Message string // Progress message (e.g., "Reading: file.go")
	Done    bool   // True if task is complete
}

ProgressUpdate represents a progress update from the agent

type TaskInfo

type TaskInfo struct {
	// Common fields
	ID          string
	Type        TaskType
	Description string
	Status      TaskStatus
	StartTime   time.Time
	EndTime     time.Time
	Output      string
	Error       string

	// Bash-specific fields
	Command  string
	PID      int
	ExitCode int

	// Agent-specific fields
	AgentName  string
	TurnCount  int
	TokenUsage int
}

TaskInfo is a snapshot of task information It contains both common fields and type-specific fields

type TaskStatus

type TaskStatus string

TaskStatus represents the status of a background task

const (
	StatusRunning   TaskStatus = "running"
	StatusCompleted TaskStatus = "completed"
	StatusFailed    TaskStatus = "failed"
	StatusKilled    TaskStatus = "killed"
)

type TaskType added in v1.2.0

type TaskType string

TaskType represents the type of background task

const (
	TaskTypeBash  TaskType = "bash"
	TaskTypeAgent TaskType = "agent"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL