# investibook-cli

A command-line interface for the Investibook AI research platform. It brings full investigation capability to the terminal, enabling on-call engineers and AI agents (e.g., OpenClaw) to drive end-to-end investigations without a browser.

The CLI is a **thin client** — all state, persistence, and agent execution live in the Investibook backend. It speaks the same REST + SSE API as the web frontend, so actions performed via CLI appear in real time in the web UI.

## Build

```bash
cd cli
make build      # Build for current platform → ./investibook
make install    # Install to $GOPATH/bin
```

### Cross-platform Distribution

Build binaries for multiple platforms:

```bash
make dist              # Build for Linux x86_64, Linux ARM64, macOS ARM64 → dist/
make dist-compressed   # Build and create tar.gz archives → dist/
```

Supported platforms:
- **Linux x86_64** (`investibook-linux-amd64`)
- **Linux ARM64** (`investibook-linux-arm64`)
- **macOS ARM64** (`investibook-darwin-arm64`)

Requires Go 1.23+.

### Creating a Release

To create a new CLI release, push a tag with the `investibook/cli/v` prefix:

```bash
git tag investibook/cli/v1.0.0
git push origin investibook/cli/v1.0.0
```

This triggers the GitHub Actions workflow which:
1. Builds cross-platform binaries (Linux x86_64, Linux ARM64, macOS ARM64)
2. Creates a GitHub Release with auto-generated release notes
3. Uploads compressed tar.gz archives as release assets

Users can download the binaries from the [Releases page](https://github.com/tidbcloud/aiops/releases).

## Quick Start

```bash
# 1. Authenticate to production
investibook init --api-host https://investibook.tidbcloud.com --api-key ib_<your-key> --profile prod

# 1b. (Optional) Authenticate to dev environment
investibook init --api-host https://dev.investibook.internal --api-key ib_<dev-key> --profile dev

# 2. Switch between profiles
investibook profile list              # show all profiles
investibook profile use dev           # switch to dev

# 3. Create a workspace
investibook workspace create --name "Prod OOM Investigation"

# 4. Set active workspace
investibook workspace use <workspace-id>

# 5. Add a Jira issue source (triggers preparation pipeline)
investibook sources add jira-issues O11Y-195801

# 6. Chat with the AI agent
investibook chat "Diagnose the root cause and propose remediation steps"

# Submit without waiting; the backend keeps executing
investibook chat --detach "Diagnose the root cause and propose remediation steps"

# 7. View the generated report
investibook reports get latest
```

## Configuration

Config is stored at `~/.investibook/config.json` and supports multiple named profiles:

```json
{
  "active_profile": "prod",
  "profiles": {
    "prod": {
      "api": "https://investibook.tidbcloud.com",
      "token": "<bearer-token>",
      "api_key": "ib_<your-key>",
      "email": "alice@company.com"
    },
    "dev": {
      "api": "https://dev.investibook.internal",
      "token": "<bearer-token>",
      "api_key": "ib_<dev-key>",
      "email": "alice@company.com"
    }
  }
}
```

`token` is the short-lived session token exchanged from the API key. The CLI keeps
`api_key` in the local `0600` config file so it can refresh the session token when
the backend returns `401 Unauthorized`.

Environment variable overrides apply to the active profile at runtime (not persisted):

| Variable | Description |
|----------|-------------|
| `INVESTIBOOK_API` | API base URL |
| `INVESTIBOOK_TOKEN` | Bearer token |
| `INVESTIBOOK_WORKSPACE` | Default workspace ID |

## Commands

Chat commands submit a durable agent turn. Pressing Ctrl-C while streaming
explicitly stops that turn; terminal disconnects do not implicitly own or end
backend execution.

### `investibook init`

Initialize CLI configuration and authenticate.

```bash
investibook init --api-host <url> --api-key <key> [--profile <name>]
```

Generate an API key in the Investibook web UI: **Settings (⚙) → API Keys → Create key**.
The CLI stores the API key locally and automatically refreshes the short-lived
session token used by subsequent commands.

| Flag | Default | Description |
|------|---------|-------------|
| `--api-host` | `https://investibook.tidbcloud.com` | Investibook API base URL |
| `--api-key` | (required) | API key from Settings → API Keys |
| `--profile` | `default` | Profile name to create/update (e.g., `dev`, `prod`) |

### `investibook profile`

Manage authentication profiles.

```bash
investibook profile list              # alias: ls — show all profiles (* = active)
investibook profile use <name>        # switch active profile
```

### `investibook workspace` (alias: `ws`)

Manage workspaces.

```bash
investibook workspace create [--name <name>] [--description <desc>]
investibook workspace list                    # alias: ls
investibook workspace use <workspace-id>      # set default workspace
investibook workspace show [<workspace-id>]   # show details
investibook workspace delete <workspace-id>
```

### `investibook chat`

Chat with the AI agent. Streams the response via SSE with colored output (thinking in dim, tool calls in blue, errors in red).

```bash
investibook chat "<message>" [--workspace <id>]
investibook chat --interactive [--workspace <id>]    # REPL mode
```

| Flag | Description |
|------|-------------|
| `-i, --interactive` | Start interactive chat REPL (Ctrl+D or `/exit` to quit) |
| `-w, --workspace` | Override default workspace |

### `investibook sources`

Manage workspace sources (data inputs for investigation).

```bash
investibook sources list [--workspace <id>]           # alias: ls
investibook sources add jira-issues <issue-id> [--workspace <id>]
investibook sources remove <source-id> [--workspace <id>]
```

Adding a Jira issue source triggers the backend preparation pipeline — the CLI polls for completion and shows progress.

### `investibook reports`

View generated reports and artifacts from the Studio panel.

```bash
investibook reports list [--workspace <id>]                             # alias: ls
investibook reports get <report-id|latest> [--workspace <id>] [--format json|text]
```

| Flag | Default | Description |
|------|---------|-------------|
| `--format` | `text` | Output format: `text` (human-readable) or `json` (raw) |

### `investibook files`

Browse workspace files.

```bash
investibook files list [--workspace <id>]       # alias: ls — shows file tree
investibook files get <path> [--workspace <id>]  # display file content
```

## Global Flags

| Flag | Description |
|------|-------------|
| `-w, --workspace <id>` | Workspace ID (overrides default from config/env) |
| `-p, --profile <name>` | Profile name (overrides active profile) |

## Project Structure

```
cli/
├── main.go                  # Entry point
├── go.mod
├── Makefile
├── cmd/
│   ├── root.go              # Root command, global flags, auth helpers
│   ├── init.go              # `investibook init` — authentication setup
│   ├── chat.go              # `investibook chat` — SSE streaming chat
│   ├── workspace.go         # `investibook workspace` — CRUD operations
│   ├── sources.go           # `investibook sources` — source management
│   ├── files.go             # `investibook files` — file browsing
│   └── reports.go           # `investibook reports` — artifact viewing
└── internal/
    ├── api/
    │   └── client.go        # HTTP client for all Investibook API endpoints
    └── config/
        └── config.go        # Config loading/saving (~/.investibook/config.json)
```
