Skip to content

An open-source, faster Devin alternative

TypeScript

agent connect: add --bot-token flag; fix channel-name in success message

Igor Zalutskid1a2b2bf829e
diff --git a/cmd/oc/internal/commands/agent.go b/cmd/oc/internal/commands/agent.go
index c56d6b33..24ac32d0 100644
--- a/cmd/oc/internal/commands/agent.go
+++ b/cmd/oc/internal/commands/agent.go
@@ -10,6 +10,7 @@ package commands
import (
"fmt"
+ "os"
"strings"
"time"
@@ -17,6 +18,17 @@ import (
"github.com/spf13/cobra"
)
+// stdinIsTTY reports whether stdin is attached to a terminal. Used to gate
+// interactive prompts: if stdin isn't a TTY we refuse to prompt, since the
+// caller is a script or agent that can't respond.
+func stdinIsTTY() bool {
+ fi, err := os.Stdin.Stat()
+ if err != nil {
+ return false
+ }
+ return (fi.Mode() & os.ModeCharDevice) != 0
+}
+
// sessionsClient returns the sessions-api client from context, or errors if not configured.
func sessionsClient(cmd *cobra.Command) (*client.Client, error) {
c := client.SessionsFromContext(cmd.Context())
@@ -105,6 +117,8 @@ func init() {
agentCreateCmd.Flags().StringSlice("secret", nil, "Secrets (KEY=VALUE)")
agentCreateCmd.Flags().Bool("no-wait", false, "Don't wait for instance provisioning; exit after agent record is created")
+ agentConnectCmd.Flags().String("bot-token", "", "Telegram bot token (required for channel=telegram when stdin is not a TTY)")
+
agentInstallCmd.Flags().Bool("no-wait", false, "Don't wait for install orchestration to finish")
agentEventsCmd.Flags().Int("limit", 0, "Max events to return (1-200, default 50)")
diff --git a/cmd/oc/internal/commands/agent_connect.go b/cmd/oc/internal/commands/agent_connect.go
index 14130627..6e446e45 100644
--- a/cmd/oc/internal/commands/agent_connect.go
+++ b/cmd/oc/internal/commands/agent_connect.go
@@ -1,8 +1,6 @@
package commands
-// Channel lifecycle: connect, disconnect, list. The Telegram case prompts
-// interactively for a bot token since there's no --bot-token flag yet
-// (tracked as a launch-prep item).
+// Channel lifecycle: connect, disconnect, list.
import (
"bufio"
@@ -16,8 +14,10 @@ import (
var agentConnectCmd = &cobra.Command{
Use: "connect <id> <channel>",
Short: "Connect a channel to an agent",
- Long: "Connect a messaging channel (e.g. telegram) to a managed agent.",
- Args: cobra.ExactArgs(2),
+ Long: "Connect a messaging channel to a managed agent.\n\n" +
+ "Supported channels:\n" +
+ " telegram requires --bot-token (or interactive prompt if TTY)",
+ Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
sc, err := sessionsClient(cmd)
if err != nil {
@@ -30,16 +30,25 @@ var agentConnectCmd = &cobra.Command{
body := map[string]interface{}{}
if channel == "telegram" {
- fmt.Println("To connect Telegram:")
- fmt.Println(" 1. Open Telegram and message @BotFather")
- fmt.Println(" 2. Send /newbot, choose a name and username")
- fmt.Println(" 3. Copy the bot token")
- fmt.Println()
- fmt.Print("Paste bot token: ")
-
- reader := bufio.NewReader(os.Stdin)
- token, _ := reader.ReadString('\n')
- token = strings.TrimSpace(token)
+ token, _ := cmd.Flags().GetString("bot-token")
+ if token == "" {
+ if !stdinIsTTY() {
+ return fmt.Errorf("--bot-token is required when stdin is not a terminal")
+ }
+ fmt.Println("To connect Telegram:")
+ fmt.Println(" 1. Open Telegram and message @BotFather")
+ fmt.Println(" 2. Send /newbot, choose a name and username")
+ fmt.Println(" 3. Copy the bot token")
+ fmt.Println()
+ fmt.Print("Paste bot token: ")
+
+ reader := bufio.NewReader(os.Stdin)
+ line, err := reader.ReadString('\n')
+ if err != nil && line == "" {
+ return fmt.Errorf("reading bot token: %w", err)
+ }
+ token = strings.TrimSpace(line)
+ }
if token == "" {
return fmt.Errorf("bot token is required")
}
@@ -51,7 +60,7 @@ var agentConnectCmd = &cobra.Command{
return err
}
- fmt.Printf("Telegram connected to %s.\n", agentID)
+ fmt.Printf("%s connected to %s.\n", channel, agentID)
if channel == "telegram" {
fmt.Println("Message your bot on Telegram to start chatting.")
}