Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions installer/slackbot/AGENT_COMMAND_DOCS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Slack Bot Agent Command Documentation

## Overview

The `slack_bot.py` script runs a Socket Mode client that listens for commands in a specific Slack channel. The core integration point for external agent interaction is the `!agent` command, managed by the `handle_agent` function.

## The `!agent` Command Flow

When a user types `!agent <instructions>` (e.g., `!agent analyze the logs`), the bot performs the following steps:

1. **Input Parsing:**
- It strips the `!agent` prefix to isolate the `instructions` string.
- If no instructions are provided, it returns a warning message.

2. **Local Persistence:**
- The instructions are written to a local file defined by `AGENT_PAYLOAD_PATH` (default: `agent_payload.txt`).

3. **HTTP POST Request:**
- The bot sends a POST request to the `SANDBOX_URL` (default: `http://sandbox:8080`).
- It sets a timeout of 30 seconds.

4. **Response Handling:**
- If the request is successful, the bot captures the response.
- It attempts to parse the response as JSON; if that fails, it falls back to raw text.
- The first 2000 characters of the result are sent back to the Slack channel.

## HTTP Request Structure

### Request Payload

The bot sends the user's instructions as a JSON object with a single key: `prompt`.

**Example Request:**

```json
{
"prompt": "Please analyze the latest server logs and summarize errors."
}
```

### Response Expectation

While the bot handles raw text responses, a JSON response is preferred for structured data handling.

**Example Response:**

```json
{
"status": "success",
"job_id": "12345",
"message": "Analysis started. ETA 2 minutes."
}
```
43 changes: 19 additions & 24 deletions installer/slackbot/slack_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@
WEBHOOK_URL = os.environ.get("SLACK_WEBHOOK_URL")
DEFAULT_CHANNEL = os.environ.get("SLACK_DEFAULT_CHANNEL", "C08NTG6CXL5")
AGENT_PAYLOAD_PATH = Path(os.environ.get("AGENT_PAYLOAD_PATH", "agent_payload.txt"))
AGENT_TRIGGER_COMMAND = os.environ.get("AGENT_TRIGGER_COMMAND")
DEFAULT_AGENT_COMMAND = [
"python3",
"-c",
"print('Agent trigger placeholder executed')",
]
AGENT_COMMAND = shlex.split(AGENT_TRIGGER_COMMAND) if AGENT_TRIGGER_COMMAND else DEFAULT_AGENT_COMMAND
SANDBOX_URL = os.environ.get("SANDBOX_URL", "http://sandbox:8080")


# --- Public helper functions ---
Expand Down Expand Up @@ -110,32 +104,33 @@ def handle_agent(user, command_full):
text=f"❌ <@{user}> Unable to write agent payload. Error: {exc}",
)
return
#TODO: Add AI + Terrarium integration here

# Send payload to Sandbox via HTTP POST
try:
result = subprocess.run(
AGENT_COMMAND,
capture_output=True,
text=True,
check=True,
)
output = (result.stdout or "Command executed with no output").strip()
payload = {"prompt": instructions}
print(f"Sending agent instructions to {SANDBOX_URL}...")
response = requests.post(SANDBOX_URL, json=payload, timeout=30)
response.raise_for_status()

# Try to parse response as JSON, otherwise use text
try:
resp_data = response.json()
output_preview = str(resp_data)[:2000]
except ValueError:
output_preview = response.text[:2000]

send_slack_message(
DEFAULT_CHANNEL,
text=(
f"✅ <@{user}> Agent instructions saved to `{AGENT_PAYLOAD_PATH}`."
" Placeholder trigger output:\n```${output[:2000]}```"
f"✅ <@{user}> Agent instructions sent to Sandbox.\n"
f"Response:\n```\n{output_preview}\n```"
),
)
except subprocess.CalledProcessError as exc:
error_text = (exc.stderr or str(exc)).strip()
print("Agent trigger command failed:", error_text)
except Exception as exc:
print(f"Error communicating with sandbox: {exc}")
send_slack_message(
DEFAULT_CHANNEL,
text=(
f"❌ <@{user}> Agent trigger command failed with exit code {exc.returncode}."
f" Details:\n```${error_text[:2000]}```"
),
text=f"❌ <@{user}> Failed to trigger Sandbox pipeline. Error: {exc}",
)


Expand Down
Loading