// Command ocprobe exercises the OpenChamber API from a terminal. It is the // M0 probe: no UI, just the same client the app uses. // // go run ./cmd/ocprobe -url http://127.0.0.1:3000 health // go run ./cmd/ocprobe -url http://127.0.0.1:3000 -password secret sessions // go run ./cmd/ocprobe -url http://127.0.0.1:3000 messages ses_123 // go run ./cmd/ocprobe -url http://127.0.0.1:3000 events -seconds 20 package main import ( "context" "encoding/json" "flag" "fmt" "os" "os/signal" "strings" "syscall" "time" "github.com/openchamber/openchamber-native/internal/ocapi" ) func main() { var ( base = flag.String("url", "http://127.0.0.1:3000", "OpenChamber server URL") password = flag.String("password", "", "UI password (empty for a passwordless server)") proxy = flag.String("proxy", "", "optional HTTP(S) or SOCKS5 proxy URL") insecure = flag.Bool("insecure", false, "skip TLS verification") fp = flag.String("fingerprint", "", "pin the server certificate (SHA-256 hex)") timeout = flag.Duration("timeout", 20*time.Second, "request timeout") limit = flag.Int("limit", 20, "page size for sessions/messages") seconds = flag.Int("seconds", 10, "how long to watch events") types = flag.String("types", "", "comma-separated event types for events") ) flag.Parse() if flag.NArg() == 0 { usage() } cmd := flag.Arg(0) client, err := ocapi.New(*base, ocapi.Options{ Proxy: *proxy, InsecureSkipVerify: *insecure, CertFingerprint: *fp, Timeout: *timeout, }) if err != nil { fail(err) } ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop() // Every command except health authenticates first. if cmd != "health" { if err := authenticate(ctx, client, *password); err != nil { fail(err) } } switch cmd { case "health": h, err := client.Health(ctx) if err != nil { fail(err) } printJSON(h) fmt.Fprintf(os.Stderr, "server %s, openchamber %s, opencode ready=%v\n", client.BaseURL(), h.OpenChamberVersion, h.IsOpenCodeReady) case "status": st, err := client.SessionStatus(ctx) if err != nil { fail(err) } printJSON(st) case "sessions": page, err := client.ListSessions(ctx, ocapi.ListSessionsOptions{Limit: *limit, Order: "desc"}) if err != nil { fail(err) } printJSON(page) fmt.Fprintf(os.Stderr, "%d sessions, next=%q\n", len(page.Data), page.Cursor.Next) case "messages": if flag.NArg() < 2 { fail(fmt.Errorf("messages needs a session id")) } page, err := client.ListMessages(ctx, flag.Arg(1), ocapi.ListMessagesOptions{Limit: *limit, Order: "desc"}) if err != nil { fail(err) } printJSON(page) fmt.Fprintf(os.Stderr, "%d messages, next=%q\n", len(page.Data), page.Cursor.Next) case "raw": if flag.NArg() < 2 { fail(fmt.Errorf("raw needs an absolute file path, and optionally a directory")) } dir := "" if flag.NArg() > 2 { dir = flag.Arg(2) } data, mime, err := client.RawFile(ctx, flag.Arg(1), dir) if err != nil { fail(err) } fmt.Fprintf(os.Stderr, "%d bytes, %s\n", len(data), mime) case "prompt": if flag.NArg() < 3 { fail(fmt.Errorf("prompt needs a session id and text")) } if err := client.Prompt(ctx, flag.Arg(1), ocapi.PromptOptions{Text: strings.Join(flag.Args()[2:], " ")}); err != nil { fail(err) } fmt.Fprintln(os.Stderr, "prompt accepted; watch `events` for the reply") case "interrupt": if flag.NArg() < 2 { fail(fmt.Errorf("interrupt needs a session id")) } ok, err := client.Interrupt(ctx, flag.Arg(1), "") if err != nil { fail(err) } fmt.Fprintf(os.Stderr, "interrupted=%v\n", ok) case "events": var list []string if strings.TrimSpace(*types) != "" { for _, t := range strings.Split(*types, ",") { if t = strings.TrimSpace(t); t != "" { list = append(list, t) } } } ectx, cancel := context.WithTimeout(ctx, time.Duration(*seconds)*time.Second) defer cancel() fmt.Fprintf(os.Stderr, "watching events for %ds…\n", *seconds) err := client.StreamEvents(ectx, list, func(ev ocapi.Event) error { fmt.Printf("%s %s %s\n", time.UnixMilli(ev.Created).Format("15:04:05"), ev.Type, string(ev.Data)) return nil }) if err != nil && ectx.Err() == nil { fail(err) } default: usage() } } func authenticate(ctx context.Context, c *ocapi.Client, password string) error { st, err := c.SessionStatus(ctx) if err == nil { if !st.Authenticated { return fmt.Errorf("server did not accept the request") } return nil } if !ocapi.IsUnauthorized(err) { return err } if strings.TrimSpace(password) == "" { return fmt.Errorf("this server requires a password; pass -password") } res, err := c.Login(ctx, ocapi.LoginRequest{ Password: password, DedupeKey: probeDedupeKey(), ClientLabel: "OpenChamber Probe", ClientKind: "desktop", DevicePlatform: "cli", }) if err != nil { if wait, ok := ocapi.IsRateLimited(err); ok { return fmt.Errorf("%w (retry after %s)", err, wait) } return err } if res.ClientToken == "" { return fmt.Errorf("login returned no client token") } return nil } func probeDedupeKey() string { host, err := os.Hostname() if err != nil || host == "" { return "ocprobe" } return "ocprobe-" + host } func printJSON(v any) { enc := json.NewEncoder(os.Stdout) enc.SetIndent("", " ") _ = enc.Encode(v) } func usage() { fmt.Fprint(os.Stderr, `usage: ocprobe [flags] commands: health server health (no auth) status probe /auth/session sessions list sessions (newest first) messages latest messages of a session raw [directory] fetch a file the server can read prompt send a message interrupt stop the running turn events watch the event stream `) flag.PrintDefaults() os.Exit(2) } func fail(err error) { fmt.Fprintln(os.Stderr, "ocprobe:", err) os.Exit(1) }