adding to CheckSendEnabled a check for if there is a curren task (#6812)
This commit is contained in:
1 parent
c04e2185f9
commit
3dc09d698d
3 files changed
+122
-93
No files matched your search
+14
-8
@@ -2,6 +2,7 @@ package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@@ -152,7 +153,7 @@ func newTaskNewCommand() *cobra.Command {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create task: %w", err)
|
||||
}
|
||||
|
||||
|
||||
if global.Config.Verbose {
|
||||
fmt.Printf("Task created successfully with ID: %s\n", taskID)
|
||||
}
|
||||
@@ -309,17 +310,22 @@ func NewTaskSendCommand() *cobra.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
sendDisabled, err := taskManager.CheckSendDisabled(ctx)
|
||||
|
||||
// Check if we can send a message
|
||||
err = taskManager.CheckSendEnabled(ctx)
|
||||
if err != nil {
|
||||
// Handle specific error cases
|
||||
if errors.Is(err, task.ErrNoActiveTask) {
|
||||
fmt.Println("Cannot send message: no active task")
|
||||
return nil
|
||||
}
|
||||
if errors.Is(err, task.ErrTaskBusy) {
|
||||
fmt.Println("Cannot send message: task is currently busy")
|
||||
return nil
|
||||
}
|
||||
// All other errors are unexpected
|
||||
return fmt.Errorf("failed to check if message can be sent: %w", err)
|
||||
}
|
||||
|
||||
if sendDisabled {
|
||||
fmt.Println("Cannot send message: task is currently busy")
|
||||
return nil
|
||||
}
|
||||
|
||||
if mode != "" {
|
||||
if err := taskManager.SetModeAndSendMessage(ctx, mode, message, images, files); err != nil {
|
||||
return fmt.Errorf("failed to set mode and send message: %w", err)
|
||||
|
||||
@@ -2,6 +2,7 @@ package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -108,83 +109,91 @@ func (ih *InputHandler) Start(ctx context.Context, errChan chan error) {
|
||||
}
|
||||
|
||||
// Check if we can send a regular message
|
||||
sendDisabled, err := ih.manager.CheckSendDisabled(ctx)
|
||||
err = ih.manager.CheckSendEnabled(ctx)
|
||||
if err != nil {
|
||||
// Handle specific error cases
|
||||
if errors.Is(err, ErrNoActiveTask) {
|
||||
// No active task - don't show input prompt
|
||||
ih.coordinator.SetInputAllowed(false)
|
||||
continue
|
||||
}
|
||||
if errors.Is(err, ErrTaskBusy) {
|
||||
// Task is busy - don't show input prompt
|
||||
ih.coordinator.SetInputAllowed(false)
|
||||
continue
|
||||
}
|
||||
// Unexpected error
|
||||
if global.Config.Verbose {
|
||||
fmt.Printf("\nDebug: CheckSendDisabled error: %v\n", err)
|
||||
fmt.Printf("\nDebug: CheckSendEnabled error: %v\n", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// If send is enabled (not disabled), show prompt
|
||||
if !sendDisabled {
|
||||
ih.coordinator.SetInputAllowed(true)
|
||||
// If we reach here, we can send a message
|
||||
ih.coordinator.SetInputAllowed(true)
|
||||
|
||||
// Lock output to prevent race with streaming display
|
||||
ih.coordinator.LockOutput()
|
||||
// Lock output to prevent race with streaming display
|
||||
ih.coordinator.LockOutput()
|
||||
|
||||
// Show prompt and get input
|
||||
message, shouldSend, err := ih.promptForInput(ctx)
|
||||
// Show prompt and get input
|
||||
message, shouldSend, err := ih.promptForInput(ctx)
|
||||
|
||||
// Unlock output after form dismissed
|
||||
ih.coordinator.UnlockOutput()
|
||||
// Unlock output after form dismissed
|
||||
ih.coordinator.UnlockOutput()
|
||||
|
||||
if err != nil {
|
||||
// Check if the error is due to interrupt (Ctrl+C) or context cancellation
|
||||
if err == huh.ErrUserAborted || ctx.Err() != nil {
|
||||
// User pressed Ctrl+C - cancel context to exit FollowConversation
|
||||
ih.cancelFunc()
|
||||
return
|
||||
if err != nil {
|
||||
// Check if the error is due to interrupt (Ctrl+C) or context cancellation
|
||||
if err == huh.ErrUserAborted || ctx.Err() != nil {
|
||||
// User pressed Ctrl+C - cancel context to exit FollowConversation
|
||||
ih.cancelFunc()
|
||||
return
|
||||
}
|
||||
if global.Config.Verbose {
|
||||
fmt.Printf("\nDebug: Input prompt error: %v\n", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
ih.coordinator.SetInputAllowed(false)
|
||||
|
||||
if shouldSend {
|
||||
// Check for mode switch commands first
|
||||
newMode, remainingMessage, isModeSwitch := ih.parseModeSwitch(message)
|
||||
if isModeSwitch {
|
||||
// Switch mode
|
||||
if err := ih.manager.SetMode(ctx, newMode, nil, nil, nil); err != nil {
|
||||
fmt.Printf("\nError switching to %s mode: %v\n", newMode, err)
|
||||
continue
|
||||
}
|
||||
if global.Config.Verbose {
|
||||
fmt.Printf("\nDebug: Input prompt error: %v\n", err)
|
||||
fmt.Printf("\nSwitched to %s mode\n", newMode)
|
||||
|
||||
// If there's remaining message, use it as the new message to send
|
||||
if remainingMessage != "" {
|
||||
message = remainingMessage
|
||||
} else {
|
||||
// No message to send, just mode switch
|
||||
time.Sleep(1 * time.Second)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Handle special commands
|
||||
if handled := ih.handleSpecialCommand(ctx, message); handled {
|
||||
continue
|
||||
}
|
||||
|
||||
ih.coordinator.SetInputAllowed(false)
|
||||
|
||||
if shouldSend {
|
||||
// Check for mode switch commands first
|
||||
newMode, remainingMessage, isModeSwitch := ih.parseModeSwitch(message)
|
||||
if isModeSwitch {
|
||||
// Switch mode
|
||||
if err := ih.manager.SetMode(ctx, newMode, nil, nil, nil); err != nil {
|
||||
fmt.Printf("\nError switching to %s mode: %v\n", newMode, err)
|
||||
continue
|
||||
}
|
||||
fmt.Printf("\nSwitched to %s mode\n", newMode)
|
||||
|
||||
// If there's remaining message, use it as the new message to send
|
||||
if remainingMessage != "" {
|
||||
message = remainingMessage
|
||||
} else {
|
||||
// No message to send, just mode switch
|
||||
time.Sleep(1 * time.Second)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Handle special commands
|
||||
if handled := ih.handleSpecialCommand(ctx, message); handled {
|
||||
continue
|
||||
}
|
||||
|
||||
// Send the message
|
||||
if err := ih.manager.SendMessage(ctx, message, nil, nil, ""); err != nil {
|
||||
fmt.Printf("\nError sending message: %v\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if global.Config.Verbose {
|
||||
fmt.Printf("\nDebug: Message sent successfully\n")
|
||||
}
|
||||
|
||||
// Give the system a moment to process before re-polling
|
||||
time.Sleep(1 * time.Second)
|
||||
// Send the message
|
||||
if err := ih.manager.SendMessage(ctx, message, nil, nil, ""); err != nil {
|
||||
fmt.Printf("\nError sending message: %v\n", err)
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
ih.coordinator.SetInputAllowed(false)
|
||||
|
||||
if global.Config.Verbose {
|
||||
fmt.Printf("\nDebug: Message sent successfully\n")
|
||||
}
|
||||
|
||||
// Give the system a moment to process before re-polling
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+38
-24
@@ -18,6 +18,12 @@ import (
|
||||
"github.com/cline/grpc-go/cline"
|
||||
)
|
||||
|
||||
// Sentinel errors for CheckSendEnabled
|
||||
var (
|
||||
ErrNoActiveTask = fmt.Errorf("no active task")
|
||||
ErrTaskBusy = fmt.Errorf("task is currently busy")
|
||||
)
|
||||
|
||||
// Manager handles task execution and message display
|
||||
type Manager struct {
|
||||
mu sync.RWMutex
|
||||
@@ -243,21 +249,32 @@ func (m *Manager) ValidateCheckpointExists(ctx context.Context, checkpointID int
|
||||
return fmt.Errorf("checkpoint ID %d not found in task history", checkpointID)
|
||||
}
|
||||
|
||||
// CheckSendDisabled determines if we can send a message to the current task
|
||||
// CheckSendEnabled checks if we can send a message to the current task
|
||||
// Returns nil if sending is allowed, or an error indicating why it's not allowed
|
||||
// We duplicate the logic from buttonConfig::getButtonConfig
|
||||
func (m *Manager) CheckSendDisabled(ctx context.Context) (bool, error) {
|
||||
func (m *Manager) CheckSendEnabled(ctx context.Context) error {
|
||||
state, err := m.client.State.GetLatestState(ctx, &cline.EmptyRequest{})
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to get latest state: %w", err)
|
||||
return fmt.Errorf("failed to get latest state: %w", err)
|
||||
}
|
||||
|
||||
var stateData types.ExtensionState
|
||||
if err := json.Unmarshal([]byte(state.StateJson), &stateData); err != nil {
|
||||
return fmt.Errorf("failed to parse state: %w", err)
|
||||
}
|
||||
|
||||
// Check if there is an active task
|
||||
if stateData.CurrentTaskItem == nil {
|
||||
return ErrNoActiveTask
|
||||
}
|
||||
|
||||
messages, err := m.extractMessagesFromState(state.StateJson)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to extract messages: %w", err)
|
||||
return fmt.Errorf("failed to extract messages: %w", err)
|
||||
}
|
||||
|
||||
if len(messages) == 0 {
|
||||
return false, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// Use final message to perform validation
|
||||
@@ -287,7 +304,7 @@ func (m *Manager) CheckSendDisabled(ctx context.Context) (bool, error) {
|
||||
if global.Config.Verbose {
|
||||
m.renderer.RenderDebug("Send disabled: task is streaming and non-error")
|
||||
}
|
||||
return true, nil
|
||||
return ErrTaskBusy
|
||||
}
|
||||
|
||||
// All ask messages allow sending
|
||||
@@ -295,7 +312,7 @@ func (m *Manager) CheckSendDisabled(ctx context.Context) (bool, error) {
|
||||
if global.Config.Verbose {
|
||||
m.renderer.RenderDebug("Send enabled: ask message")
|
||||
}
|
||||
return false, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// Technically unnecessary but implements getButtonConfig 1-1
|
||||
@@ -303,14 +320,14 @@ func (m *Manager) CheckSendDisabled(ctx context.Context) (bool, error) {
|
||||
if global.Config.Verbose {
|
||||
m.renderer.RenderDebug("Send disabled: API request is active")
|
||||
}
|
||||
return true, nil
|
||||
return ErrTaskBusy
|
||||
}
|
||||
|
||||
if global.Config.Verbose {
|
||||
m.renderer.RenderDebug("Send disabled: default fallback")
|
||||
}
|
||||
|
||||
return true, nil
|
||||
return ErrTaskBusy
|
||||
}
|
||||
|
||||
// CheckNeedsApproval determines if the current task is waiting for approval
|
||||
@@ -639,7 +656,7 @@ func (m *Manager) ShowConversation(ctx context.Context) error {
|
||||
m.mu.Lock()
|
||||
m.isStreamingMode = false
|
||||
m.mu.Unlock()
|
||||
|
||||
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
@@ -678,17 +695,17 @@ func (m *Manager) FollowConversation(ctx context.Context, instanceAddress string
|
||||
m.mu.Unlock()
|
||||
|
||||
if global.Config.OutputFormat != "plain" {
|
||||
markdown := fmt.Sprintf("*Using instance: %s*\n*Press Ctrl+C to exit*", instanceAddress)
|
||||
rendered := m.renderer.RenderMarkdown(markdown)
|
||||
fmt.Printf("%s", rendered)
|
||||
} else {
|
||||
markdown := fmt.Sprintf("*Using instance: %s*\n*Press Ctrl+C to exit*", instanceAddress)
|
||||
rendered := m.renderer.RenderMarkdown(markdown)
|
||||
fmt.Printf("%s", rendered)
|
||||
} else {
|
||||
fmt.Printf("Using instance: %s\n", instanceAddress)
|
||||
if interactive {
|
||||
fmt.Println("Following task conversation in interactive mode... (Press Ctrl+C to exit)")
|
||||
} else {
|
||||
fmt.Println("Following task conversation... (Press Ctrl+C to exit)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
@@ -764,7 +781,7 @@ func (m *Manager) FollowConversationUntilCompletion(ctx context.Context) error {
|
||||
m.mu.Lock()
|
||||
m.isStreamingMode = true
|
||||
m.mu.Unlock()
|
||||
|
||||
|
||||
fmt.Println("Following task conversation until completion... (Press Ctrl+C to exit)")
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
@@ -1033,7 +1050,7 @@ func (m *Manager) processStateUpdate(stateUpdate *cline.State, coordinator *Stre
|
||||
coordinator.MarkProcessedInCurrentTurn(msgKey)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
case msg.Type == types.MessageTypeAsk:
|
||||
msgKey := fmt.Sprintf("%d", msg.Timestamp)
|
||||
// Only render if not already handled by partial stream
|
||||
@@ -1132,10 +1149,10 @@ func (m *Manager) displayMessage(msg *types.ClineMessage, isLast, isPartial bool
|
||||
m.mu.RUnlock()
|
||||
|
||||
dc := &handlers.DisplayContext{
|
||||
State: m.state,
|
||||
Renderer: m.renderer,
|
||||
ToolRenderer: m.toolRenderer,
|
||||
SystemRenderer: m.systemRenderer,
|
||||
State: m.state,
|
||||
Renderer: m.renderer,
|
||||
ToolRenderer: m.toolRenderer,
|
||||
SystemRenderer: m.systemRenderer,
|
||||
IsLast: isLast,
|
||||
IsPartial: isPartial,
|
||||
MessageIndex: messageIndex,
|
||||
@@ -1182,7 +1199,6 @@ func (m *Manager) loadAndDisplayRecentHistory(ctx context.Context) (int, error)
|
||||
totalMessages := len(messages)
|
||||
startIndex := 0
|
||||
|
||||
|
||||
if totalMessages > maxHistoryMessages {
|
||||
startIndex = totalMessages - maxHistoryMessages
|
||||
if global.Config.OutputFormat != "plain" {
|
||||
@@ -1202,8 +1218,6 @@ func (m *Manager) loadAndDisplayRecentHistory(ctx context.Context) (int, error)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
for i := startIndex; i < len(messages); i++ {
|
||||
msg := messages[i]
|
||||
|
||||
|
||||
Reference in new issue
Block a user