-
Notifications
You must be signed in to change notification settings - Fork 0
Add AgentsView as an optional sessions integration #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "strings" | ||
| ) | ||
|
|
||
| const agentsViewBinary = "agentsview" | ||
|
|
||
| var agentsViewLookPath = exec.LookPath | ||
|
|
||
| const agentsViewInstallHint = `AgentsView not found on PATH. | ||
|
|
||
| dotagents sessions launches AgentsView as an optional session search, replay, | ||
| telemetry, and usage dashboard. AgentsView remains independently installed and | ||
| owns its own local index. | ||
|
|
||
| Install it from https://github.com/kenn-io/agentsview, then re-run: dotagents sessions` | ||
|
|
||
| type sessionsOptions struct { | ||
| NoOpen bool | ||
| SSHHost string | ||
| } | ||
|
|
||
| func parseSessionsArgs(args []string) (sessionsOptions, []string, error) { | ||
| var opts sessionsOptions | ||
| var passthrough []string | ||
| for i := 0; i < len(args); i++ { | ||
| arg := args[i] | ||
| switch { | ||
| case arg == "--no-open": | ||
| opts.NoOpen = true | ||
| case arg == "--open": | ||
| opts.NoOpen = false | ||
| case arg == "--ssh-host": | ||
| if i+1 >= len(args) { | ||
| return sessionsOptions{}, nil, errors.New("--ssh-host requires a value (e.g. --ssh-host user@host)") | ||
| } | ||
| opts.SSHHost = args[i+1] | ||
| i++ | ||
| case strings.HasPrefix(arg, "--ssh-host="): | ||
| opts.SSHHost = strings.TrimPrefix(arg, "--ssh-host=") | ||
| default: | ||
| passthrough = append(passthrough, arg) | ||
| } | ||
| } | ||
| return opts, passthrough, nil | ||
| } | ||
|
|
||
| func agentsViewServeArgs(opts sessionsOptions, passthrough []string, remote bool) []string { | ||
| args := []string{"serve"} | ||
| if (opts.NoOpen || remote) && !containsArg(passthrough, "--no-browser") { | ||
| args = append(args, "--no-browser") | ||
| } | ||
| return append(args, passthrough...) | ||
| } | ||
|
|
||
| func containsArg(args []string, target string) bool { | ||
| for _, arg := range args { | ||
| if arg == target { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func agentsViewPort(args []string) string { | ||
| for i := range args { | ||
| switch { | ||
| case args[i] == "--port" && i+1 < len(args): | ||
| return args[i+1] | ||
| case strings.HasPrefix(args[i], "--port="): | ||
| return strings.TrimPrefix(args[i], "--port=") | ||
| } | ||
| } | ||
| return "8080" | ||
| } | ||
|
|
||
| func announceSessionsRemote(port string, sshHost string) { | ||
| url := "http://127.0.0.1:" + port | ||
| fmt.Fprintln(os.Stdout, "Launching AgentsView on the remote host.") | ||
| fmt.Fprintf(os.Stdout, " %s\n", url) | ||
| if tunnel, ok := tunnelCommand(url, sshHost); ok { | ||
| fmt.Fprintf(os.Stdout, " from your machine: %s\n", tunnel) | ||
| fmt.Fprintln(os.Stdout, " then open the URL above locally.") | ||
| } else { | ||
| fmt.Fprintln(os.Stdout, " add --ssh-host user@host for a ready tunnel command.") | ||
| } | ||
| fmt.Fprintln(os.Stdout) | ||
| } | ||
|
|
||
| func runSessions(args []string) error { | ||
| opts, passthrough, err := parseSessionsArgs(args) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| path, err := agentsViewLookPath(agentsViewBinary) | ||
| if err != nil { | ||
| return errors.New(agentsViewInstallHint) | ||
| } | ||
|
|
||
| remote := os.Getenv("SSH_CONNECTION") != "" || opts.SSHHost != "" | ||
| if !isHelpRequest(passthrough) { | ||
| if remote { | ||
| announceSessionsRemote(agentsViewPort(passthrough), resolveSSHHost(opts.SSHHost, os.Getenv)) | ||
| } else { | ||
| fmt.Fprintln(os.Stdout, "Launching AgentsView for session search, replay, telemetry, and usage.") | ||
| } | ||
| } | ||
|
|
||
| cmd := exec.Command(path, agentsViewServeArgs(opts, passthrough, remote)...) // nosemgrep: go.lang.security.audit.dangerous-exec-command | ||
| cmd.Stdin = os.Stdin | ||
| cmd.Stdout = os.Stdout | ||
| cmd.Stderr = os.Stderr | ||
| return cmd.Run() | ||
| } | ||
|
|
||
| func isHelpRequest(args []string) bool { | ||
| return containsArg(args, "--help") || containsArg(args, "-h") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "reflect" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestParseSessionsArgs(t *testing.T) { | ||
| opts, passthrough, err := parseSessionsArgs([]string{"--no-open", "--ssh-host=m4", "--port", "9090", "--no-sync"}) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if !opts.NoOpen || opts.SSHHost != "m4" { | ||
| t.Fatalf("parseSessionsArgs options = %+v", opts) | ||
| } | ||
| want := []string{"--port", "9090", "--no-sync"} | ||
| if !reflect.DeepEqual(passthrough, want) { | ||
| t.Fatalf("parseSessionsArgs passthrough = %v, want %v", passthrough, want) | ||
| } | ||
| } | ||
|
|
||
| func TestParseSessionsArgsRequiresSSHHost(t *testing.T) { | ||
| if _, _, err := parseSessionsArgs([]string{"--ssh-host"}); err == nil { | ||
| t.Fatal("expected missing --ssh-host value to fail") | ||
| } | ||
| } | ||
|
|
||
| func TestAgentsViewServeArgs(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| opts sessionsOptions | ||
| passthrough []string | ||
| remote bool | ||
| want []string | ||
| }{ | ||
| {name: "local defaults", want: []string{"serve"}}, | ||
| {name: "no open", opts: sessionsOptions{NoOpen: true}, want: []string{"serve", "--no-browser"}}, | ||
| {name: "remote", remote: true, passthrough: []string{"--port", "9090"}, want: []string{"serve", "--no-browser", "--port", "9090"}}, | ||
| {name: "existing no browser", remote: true, passthrough: []string{"--no-browser"}, want: []string{"serve", "--no-browser"}}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := agentsViewServeArgs(tt.opts, tt.passthrough, tt.remote) | ||
| if !reflect.DeepEqual(got, tt.want) { | ||
| t.Fatalf("agentsViewServeArgs = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAgentsViewPort(t *testing.T) { | ||
| for _, tt := range []struct { | ||
| args []string | ||
| want string | ||
| }{ | ||
| {want: "8080"}, | ||
| {args: []string{"--port", "9090"}, want: "9090"}, | ||
| {args: []string{"--port=7070"}, want: "7070"}, | ||
| } { | ||
| if got := agentsViewPort(tt.args); got != tt.want { | ||
| t.Fatalf("agentsViewPort(%v) = %q, want %q", tt.args, got, tt.want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestRunSessionsMissingBinary(t *testing.T) { | ||
| orig := agentsViewLookPath | ||
| t.Cleanup(func() { agentsViewLookPath = orig }) | ||
| agentsViewLookPath = func(string) (string, error) { return "", errors.New("not found") } | ||
|
|
||
| err := runSessions(nil) | ||
| if err == nil { | ||
| t.Fatal("expected error when agentsview binary is missing") | ||
| } | ||
| if !strings.Contains(err.Error(), "AgentsView") || !strings.Contains(err.Error(), "github.com/kenn-io/agentsview") { | ||
| t.Fatalf("error should guide install, got: %v", err) | ||
| } | ||
| if !strings.Contains(err.Error(), "dotagents sessions") { | ||
| t.Fatalf("install hint should point at the sessions command, got: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestRunSessionsHelpSkipsRemoteBanner(t *testing.T) { | ||
| orig := agentsViewLookPath | ||
| t.Cleanup(func() { agentsViewLookPath = orig }) | ||
| agentsViewLookPath = func(string) (string, error) { return "/bin/echo", nil } | ||
|
|
||
| stdout, stderr, err := captureCLIOutput(t, func() error { | ||
| return runSessions([]string{"--help"}) | ||
| }) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if strings.Contains(stdout, "Launching AgentsView on the remote host.") { | ||
| t.Fatalf("help should not trigger remote banner:\n%s", stdout) | ||
| } | ||
| if strings.Contains(stdout, "Launching AgentsView for session") { | ||
| t.Fatalf("help should not print the launch banner:\n%s", stdout) | ||
| } | ||
| if stderr != "" { | ||
| t.Fatalf("help wrote stderr: %q", stderr) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a remote invocation forwards a specific non-loopback bind address, such as
--host 192.0.2.10, AgentsView listens on that address but this URL is always built with127.0.0.1;tunnelCommandconsequently forwards tolocalhost:PORT, where nothing is listening. Since the CLI help and documentation advertise--host ADDR, either derive the tunnel target from that flag or force AgentsView to bind to loopback in remote mode.AGENTS.md reference: AGENTS.md:L49-L51
Useful? React with 👍 / 👎.