Skip to content

Commit 2658014

Browse files
feat: add 'invoke browsers' command to list browsers for an invocation
Adds CLI support for the SDK's InvocationService.ListBrowsers() method which returns all active browser sessions created within a specific invocation. Usage: kernel invoke browsers <invocation_id> [--output json] Co-authored-by: mason <mason@onkernel.com>
1 parent 91545de commit 2658014

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

cmd/invoke.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ var invocationHistoryCmd = &cobra.Command{
3535
RunE: runInvocationHistory,
3636
}
3737

38+
var invocationBrowsersCmd = &cobra.Command{
39+
Use: "browsers <invocation_id>",
40+
Short: "List browser sessions for an invocation",
41+
Long: "List all active browser sessions created within a specific invocation.",
42+
Args: cobra.ExactArgs(1),
43+
RunE: runInvocationBrowsers,
44+
}
45+
3846
func init() {
3947
invokeCmd.Flags().StringP("version", "v", "latest", "Specify a version of the app to invoke (optional, defaults to 'latest')")
4048
invokeCmd.Flags().StringP("payload", "p", "", "JSON payload for the invocation (optional)")
@@ -49,6 +57,9 @@ func init() {
4957
invocationHistoryCmd.Flags().String("version", "", "Filter by invocation version")
5058
invocationHistoryCmd.Flags().StringP("output", "o", "", "Output format: json for raw API response")
5159
invokeCmd.AddCommand(invocationHistoryCmd)
60+
61+
invocationBrowsersCmd.Flags().StringP("output", "o", "", "Output format: json for raw API response")
62+
invokeCmd.AddCommand(invocationBrowsersCmd)
5263
}
5364

5465
func runInvoke(cmd *cobra.Command, args []string) error {
@@ -433,3 +444,56 @@ func runInvocationHistory(cmd *cobra.Command, args []string) error {
433444
}
434445
return nil
435446
}
447+
448+
func runInvocationBrowsers(cmd *cobra.Command, args []string) error {
449+
client := getKernelClient(cmd)
450+
invocationID := args[0]
451+
output, _ := cmd.Flags().GetString("output")
452+
453+
if output != "" && output != "json" {
454+
return fmt.Errorf("unsupported --output value: use 'json'")
455+
}
456+
457+
resp, err := client.Invocations.ListBrowsers(cmd.Context(), invocationID)
458+
if err != nil {
459+
pterm.Error.Printf("Failed to list browsers for invocation: %v\n", err)
460+
return nil
461+
}
462+
463+
if output == "json" {
464+
if len(resp.Browsers) == 0 {
465+
fmt.Println("[]")
466+
return nil
467+
}
468+
return util.PrintPrettyJSONSlice(resp.Browsers)
469+
}
470+
471+
if len(resp.Browsers) == 0 {
472+
pterm.Info.Printf("No active browsers found for invocation %s\n", invocationID)
473+
return nil
474+
}
475+
476+
table := pterm.TableData{{"Session ID", "Created At", "Headless", "Stealth", "Timeout", "CDP WS URL", "Live View URL"}}
477+
478+
for _, browser := range resp.Browsers {
479+
created := util.FormatLocal(browser.CreatedAt)
480+
liveView := browser.BrowserLiveViewURL
481+
if liveView == "" {
482+
liveView = "-"
483+
}
484+
485+
table = append(table, []string{
486+
browser.SessionID,
487+
created,
488+
fmt.Sprintf("%v", browser.Headless),
489+
fmt.Sprintf("%v", browser.Stealth),
490+
fmt.Sprintf("%d", browser.TimeoutSeconds),
491+
truncateURL(browser.CdpWsURL, 40),
492+
truncateURL(liveView, 40),
493+
})
494+
}
495+
496+
pterm.Info.Printf("Browsers for invocation %s:\n", invocationID)
497+
pterm.DefaultTable.WithHasHeader().WithData(table).Render()
498+
return nil
499+
}

0 commit comments

Comments
 (0)