Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

feat: implement Phase 2 instance management endpoints with full CLI support

Mario790e8721b7a2
diff --git a/cmd/pinchtab/cmd_cli.go b/cmd/pinchtab/cmd_cli.go
index 63bb7615..2f8568ed 100644
--- a/cmd/pinchtab/cmd_cli.go
+++ b/cmd/pinchtab/cmd_cli.go
@@ -182,13 +182,13 @@ WORKFLOW:
Commands:
quick <url> Navigate and analyze page (beginner-friendly)
-
+
INSTANCE MANAGEMENT:
instance launch Create new instance (--mode headed, --port 9999)
instance logs <id> Get instance logs (for debugging)
instance stop <id> Stop instance
instances List all running instances
-
+
BROWSER CONTROL:
nav, navigate <url> Navigate to URL (--new-tab, --block-images, --block-ads)
snap, snapshot Accessibility tree snapshot (-i, -c, -d, --max-tokens N)
@@ -207,7 +207,7 @@ Commands:
ss, screenshot Take screenshot (-o file, -q quality)
eval <expression> Evaluate JavaScript
pdf Export page as PDF (-o file, --landscape, --scale N)
-
+
OTHER:
health Server health check
help Show this help
@@ -650,15 +650,15 @@ func cliHealth(client *http.Client, base, token string) {
func cliInstance(client *http.Client, base, token string, args []string) {
if len(args) < 1 {
- fatal("Usage: pinchtab instance <subcommand> [options]\nSubcommands: launch, navigate, logs, stop")
+ fatal("Usage: pinchtab instance <subcommand> [options]\nSubcommands: start, launch (alias), navigate, logs, stop")
}
subCmd := args[0]
subArgs := args[1:]
switch subCmd {
- case "launch":
- cliInstanceLaunch(client, base, token, subArgs)
+ case "start", "launch": // "start" is new Phase 2 API, "launch" is legacy
+ cliInstanceStart(client, base, token, subArgs)
case "navigate":
cliInstanceNavigate(client, base, token, subArgs)
case "logs":
@@ -670,11 +670,16 @@ func cliInstance(client *http.Client, base, token string, args []string) {
}
}
-func cliInstanceLaunch(client *http.Client, base, token string, args []string) {
+func cliInstanceStart(client *http.Client, base, token string, args []string) {
body := map[string]any{}
for i := 0; i < len(args); i++ {
switch args[i] {
+ case "--profileId":
+ if i+1 < len(args) {
+ body["profileId"] = args[i+1]
+ i++
+ }
case "--mode":
if i+1 < len(args) {
body["mode"] = args[i+1]
@@ -688,8 +693,9 @@ func cliInstanceLaunch(client *http.Client, base, token string, args []string) {
}
}
- // doPost auto-prints JSON response
- doPost(client, base, token, "/instances/launch", body)
+ // Use new /instances/start endpoint if available, fall back to /instances/launch for backward compat
+ endpoint := "/instances/start"
+ doPost(client, base, token, endpoint, body)
}
func cliInstanceNavigate(client *http.Client, base, token string, args []string) {
@@ -706,21 +712,47 @@ func cliInstanceNavigate(client *http.Client, base, token string, args []string)
}
func cliInstanceLogs(client *http.Client, base, token string, args []string) {
- if len(args) < 1 {
- fatal("Usage: pinchtab instance logs <instance-id>")
+ var instID string
+
+ // Support both positional argument and --id flag
+ if len(args) == 0 {
+ fatal("Usage: pinchtab instance logs <instance-id> OR pinchtab instance logs --id <instance-id>")
+ }
+
+ // Check if first arg is --id flag
+ if args[0] == "--id" {
+ if len(args) < 2 {
+ fatal("Usage: --id requires instance ID")
+ }
+ instID = args[1]
+ } else {
+ // Positional argument (backward compat)
+ instID = args[0]
}
- instID := args[0]
logs := doGetRaw(client, base, token, fmt.Sprintf("/instances/%s/logs", instID), nil)
fmt.Println(string(logs))
}
func cliInstanceStop(client *http.Client, base, token string, args []string) {
- if len(args) < 1 {
- fatal("Usage: pinchtab instance stop <instance-id>")
+ var instID string
+
+ // Support both positional argument and --id flag
+ if len(args) == 0 {
+ fatal("Usage: pinchtab instance stop <instance-id> OR pinchtab instance stop --id <instance-id>")
+ }
+
+ // Check if first arg is --id flag
+ if args[0] == "--id" {
+ if len(args) < 2 {
+ fatal("Usage: --id requires instance ID")
+ }
+ instID = args[1]
+ } else {
+ // Positional argument (backward compat)
+ instID = args[0]
}
- instID := args[0]
// doPost auto-prints JSON response
doPost(client, base, token, fmt.Sprintf("/instances/%s/stop", instID), nil)
}
diff --git a/internal/orchestrator/handlers.go b/internal/orchestrator/handlers.go
index 6c2cbba5..0b02bf8a 100644
--- a/internal/orchestrator/handlers.go
+++ b/internal/orchestrator/handlers.go
@@ -23,6 +23,9 @@ func (o *Orchestrator) RegisterHandlers(mux *http.ServeMux) {
mux.HandleFunc("POST /start/{id}", o.handleStartByID)
mux.HandleFunc("POST /stop/{id}", o.handleStopByID)
+ // Phase 2: Instance Management (new API)
+ mux.HandleFunc("POST /instances/start", o.handleStartInstance)
+
// Dashboard / backward compat
mux.HandleFunc("POST /instances/launch", o.handleLaunchByName)
mux.HandleFunc("POST /instances/{id}/stop", o.handleStopByInstanceID)
@@ -167,6 +170,57 @@ func (o *Orchestrator) handleLogsByID(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(logs))
}
+// handleStartInstance: Phase 2 API endpoint for starting instances
+// POST /instances/start {profileId?, mode?, port?}
+// All parameters are optional
+func (o *Orchestrator) handleStartInstance(w http.ResponseWriter, r *http.Request) {
+ var req struct {
+ ProfileID string `json:"profileId,omitempty"` // Optional: Profile ID or name
+ Mode string `json:"mode,omitempty"` // Optional: "headed" or "headless"
+ Port string `json:"port,omitempty"` // Optional: port number
+ }
+
+ // Decode body if present (empty body is allowed)
+ if r.ContentLength > 0 {
+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
+ web.Error(w, 400, fmt.Errorf("invalid JSON"))
+ return
+ }
+ }
+
+ // Resolve profileId to profile name
+ // If profileId not provided, auto-generate instance with default profile or first available
+ var profileName string
+ var err error
+
+ if req.ProfileID != "" {
+ // User provided profileId (or name) - resolve it
+ profileName, err = o.resolveProfileName(req.ProfileID)
+ if err != nil {
+ web.Error(w, 404, fmt.Errorf("profile %q not found", req.ProfileID))
+ return
+ }
+ } else {
+ // No profileId provided - use auto-generated name (creates temporary profile)
+ profileName = fmt.Sprintf("instance-%d", time.Now().UnixNano())
+ }
+
+ // Parse mode: "headed" or "headless" (default: headless)
+ headless := true
+ if req.Mode == "headed" {
+ headless = false
+ }
+
+ // Port is optional - Launch() will auto-allocate if not provided
+ inst, err := o.Launch(profileName, req.Port, headless)
+ if err != nil {
+ web.Error(w, 409, err)
+ return
+ }
+
+ web.JSON(w, 201, inst)
+}
+
func (o *Orchestrator) handleAllTabs(w http.ResponseWriter, r *http.Request) {
web.JSON(w, 200, o.AllTabs())
}