mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
feat: add sprout-acp harness — bridges Sprout events to ACP agents (#10)
Co-authored-by: Tyler Longwell <tlongwell@block.xyz>
This commit is contained in:
co-authored by
Tyler Longwell
parent
0320196990
commit
4ecb5d3be2
@@ -0,0 +1,51 @@
|
||||
[package]
|
||||
name = "sprout-acp"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
rust-version.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
description = "ACP harness that bridges Sprout events to AI agents"
|
||||
|
||||
[[bin]]
|
||||
name = "sprout-acp"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
# Internal
|
||||
sprout-core = { workspace = true }
|
||||
|
||||
# Nostr
|
||||
nostr = { workspace = true }
|
||||
|
||||
# Async runtime
|
||||
tokio = { workspace = true }
|
||||
|
||||
# WebSocket
|
||||
tokio-tungstenite = { workspace = true }
|
||||
futures-util = { workspace = true }
|
||||
|
||||
# HTTP (channel discovery REST API)
|
||||
reqwest = { workspace = true }
|
||||
|
||||
# Serialization
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
|
||||
# IDs
|
||||
uuid = { workspace = true }
|
||||
chrono = { workspace = true }
|
||||
|
||||
# URL parsing
|
||||
url = { workspace = true }
|
||||
|
||||
# Logging
|
||||
tracing = { workspace = true }
|
||||
tracing-subscriber = { workspace = true }
|
||||
|
||||
# Error handling
|
||||
thiserror = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { workspace = true, features = ["test-util"] }
|
||||
@@ -0,0 +1,141 @@
|
||||
# sprout-acp
|
||||
|
||||
ACP harness that connects AI agents to Sprout. The harness listens for @mentions on the relay, prompts your agent, and the agent replies using Sprout MCP tools.
|
||||
|
||||
```
|
||||
Sprout Relay ──WS──→ sprout-acp ──stdio──→ Your Agent
|
||||
│
|
||||
Sprout MCP
|
||||
(send_message, etc.)
|
||||
```
|
||||
|
||||
Supports any agent that speaks [ACP](https://agentclientprotocol.com/) over stdio: **goose**, **codex** (via [codex-acp](https://github.com/zed-industries/codex-acp)), and **claude code** (via [claude-agent-acp](https://github.com/zed-industries/claude-agent-acp)).
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- A running Sprout relay (`just relay` or a hosted instance)
|
||||
- Docker services up (`docker compose up -d`) if running locally
|
||||
- A Nostr keypair for the agent (see [Generating Keys](#generating-keys))
|
||||
|
||||
Build:
|
||||
|
||||
```bash
|
||||
cargo build --release -p sprout-acp -p sprout-mcp-server
|
||||
export PATH="$PWD/target/release:$PATH"
|
||||
```
|
||||
|
||||
## Generating Keys
|
||||
|
||||
Each agent needs a Nostr keypair — this is the agent's identity in Sprout. Use `sprout-admin` to mint one:
|
||||
|
||||
```bash
|
||||
cargo run -p sprout-admin -- mint-token --name "my-agent" --scopes "messages:read,messages:write,channels:read"
|
||||
```
|
||||
|
||||
This prints an `nsec1...` private key and an API token. **Save both immediately — they're shown only once.**
|
||||
|
||||
> **Running multiple agents?** Mint a separate keypair for each. Every agent needs its own identity.
|
||||
|
||||
## Channels
|
||||
|
||||
The harness discovers channels by querying the relay with the agent's authenticated identity.
|
||||
|
||||
**Open channels** (the default for local dev) are accessible to any authenticated pubkey — no extra setup needed. Just start the harness and it will find and subscribe to all open channels.
|
||||
|
||||
**Private channels** require explicit membership. The relay doesn't yet have a REST/event API for managing channel members — this is a known gap. For now, use `create_channel` via the Sprout MCP tools to create new channels (the creator is automatically a member).
|
||||
|
||||
## Quick Start (goose)
|
||||
|
||||
```bash
|
||||
export SPROUT_PRIVATE_KEY="nsec1..." # your agent's key (see "Generating Keys")
|
||||
export SPROUT_RELAY_URL="ws://localhost:3000"
|
||||
export GOOSE_MODE=auto
|
||||
|
||||
sprout-acp
|
||||
```
|
||||
|
||||
That's it. The harness spawns `goose acp`, connects to the relay, discovers channels, and starts listening. When someone @mentions the agent, goose receives the message and can reply using the Sprout MCP tools that the harness configures automatically.
|
||||
|
||||
## Running with Codex
|
||||
|
||||
[codex-acp](https://github.com/zed-industries/codex-acp) wraps OpenAI Codex in an ACP interface.
|
||||
|
||||
```bash
|
||||
# Build the adapter (requires Rust 1.91+)
|
||||
cd /path/to/codex-acp && cargo build --release
|
||||
|
||||
# Run
|
||||
export OPENAI_API_KEY="sk-..." # required — use an OpenAI API key, not a ChatGPT subscription
|
||||
export SPROUT_ACP_AGENT_COMMAND="/path/to/codex-acp/target/release/codex-acp"
|
||||
export SPROUT_ACP_AGENT_ARGS='-c,permissions.approval_policy="never"'
|
||||
|
||||
sprout-acp
|
||||
```
|
||||
|
||||
> **API key note:** `codex-acp` always attempts a ChatGPT WebSocket login first, which logs a `426 Upgrade Required` error. This is expected and non-fatal — it falls back to `OPENAI_API_KEY` automatically. Set `OPENAI_API_KEY` to ensure it has a working fallback.
|
||||
|
||||
## Running with Claude Code
|
||||
|
||||
[claude-agent-acp](https://github.com/zed-industries/claude-agent-acp) wraps the Claude Agent SDK in an ACP interface.
|
||||
|
||||
```bash
|
||||
# Build the adapter
|
||||
cd /path/to/claude-agent-acp && npm install && npm run build
|
||||
|
||||
# Run
|
||||
export ANTHROPIC_API_KEY="sk-ant-..."
|
||||
export SPROUT_ACP_AGENT_COMMAND="node" # full path if using hermit: /path/to/sprout2/bin/node
|
||||
export SPROUT_ACP_AGENT_ARGS="/path/to/claude-agent-acp/dist/index.js"
|
||||
|
||||
sprout-acp
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
All configuration is via environment variables.
|
||||
|
||||
| Variable | Required | Default | Description |
|
||||
|----------|----------|---------|-------------|
|
||||
| `SPROUT_PRIVATE_KEY` | **yes** | — | Agent's Nostr private key (`nsec1...`). Used for relay auth and agent identity. |
|
||||
| `SPROUT_RELAY_URL` | no | `ws://localhost:3000` | Relay WebSocket URL. |
|
||||
| `SPROUT_ACP_AGENT_COMMAND` | no | `goose` | Agent binary to spawn. |
|
||||
| `SPROUT_ACP_AGENT_ARGS` | no | `acp` | Agent arguments (comma-separated). |
|
||||
| `SPROUT_ACP_MCP_COMMAND` | no | `sprout-mcp-server` | Path to the Sprout MCP server binary. |
|
||||
| `SPROUT_ACP_TURN_TIMEOUT` | no | `300` | Max seconds per agent turn before cancellation. |
|
||||
| `SPROUT_API_TOKEN` | no | — | API token (required if relay enforces token auth). |
|
||||
|
||||
**Note:** `SPROUT_ACP_AGENT_ARGS` splits on commas. For args with values, use: `-c,key="value"`.
|
||||
|
||||
**Legacy env vars:** `SPROUT_ACP_PRIVATE_KEY` and `SPROUT_ACP_API_TOKEN` are still accepted as fallbacks.
|
||||
|
||||
## How It Works
|
||||
|
||||
1. **Startup** — Spawns the agent subprocess, sends ACP `initialize`, connects to the relay with NIP-42 auth.
|
||||
2. **Channel discovery** — Queries the relay REST API for accessible channels, subscribes to each.
|
||||
3. **Event loop** — Listens for @mention events (kind 40001 with the agent's pubkey in a `#p` tag). Events queue per channel.
|
||||
4. **Prompting** — When events are pending and no prompt is in flight, drains all queued events for the oldest channel into a single batched prompt via ACP `session/prompt`.
|
||||
5. **Agent response** — The agent processes the prompt and uses Sprout MCP tools (`send_message`, `get_channel_history`, etc.) to interact with Sprout.
|
||||
6. **Recovery** — If the agent crashes, the harness respawns it. If the relay disconnects, the harness reconnects with a `since` filter to avoid missing events.
|
||||
|
||||
Only one prompt is in flight at a time (globally, not per-session). This matches the concurrency model of current ACP agents.
|
||||
|
||||
> **Note:** On startup, the harness replays all unprocessed @mentions since the last run. Expect a burst of activity if there are stale events in the channel.
|
||||
|
||||
## Using Any ACP Agent
|
||||
|
||||
The harness works with any agent that implements the [ACP spec](https://agentclientprotocol.com/) over stdio. The requirements are:
|
||||
|
||||
- Accept `initialize` and return a result
|
||||
- Accept `session/new` with `mcpServers` and return a `sessionId`
|
||||
- Accept `session/prompt` with a text message and stream `session/update` notifications
|
||||
- Return a `stopReason` (`end_turn`, `cancelled`, `max_tokens`, etc.)
|
||||
|
||||
Set `SPROUT_ACP_AGENT_COMMAND` and `SPROUT_ACP_AGENT_ARGS` to point at your agent binary.
|
||||
|
||||
## Testing
|
||||
|
||||
See the [root TESTING.md](../../TESTING.md) for the full integration testing guide — automated test suites, multi-agent E2E testing via the ACP harness, and troubleshooting.
|
||||
|
||||
## License
|
||||
|
||||
Apache-2.0
|
||||
@@ -0,0 +1,865 @@
|
||||
//! ACP client module — manages communication with an AI agent subprocess over stdio
|
||||
//! using JSON-RPC 2.0 (newline-delimited / NDJSON).
|
||||
//!
|
||||
//! # Lifecycle
|
||||
//! 1. [`AcpClient::spawn`] — launch agent binary as subprocess
|
||||
//! 2. [`AcpClient::initialize`] — protocol version negotiation
|
||||
//! 3. [`AcpClient::session_new`] — create session with MCP server config
|
||||
//! 4. [`AcpClient::session_prompt`] — send prompt, receive streaming updates, return stop reason
|
||||
//! 5. [`AcpClient::session_cancel`] / [`AcpClient::cancel_with_cleanup`] — cancel in-flight turn
|
||||
|
||||
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::process::{Child, ChildStdin, ChildStdout};
|
||||
|
||||
// ─── Public types ────────────────────────────────────────────────────────────
|
||||
|
||||
/// An MCP server configuration passed to `session/new`.
|
||||
///
|
||||
/// Corresponds to the `McpServerStdio` variant in the ACP schema.
|
||||
/// All four fields are **required** by the schema (`args` and `env` may be empty arrays).
|
||||
#[derive(Debug, Clone, serde::Serialize)]
|
||||
pub struct McpServer {
|
||||
pub name: String,
|
||||
pub command: String,
|
||||
pub args: Vec<String>,
|
||||
pub env: Vec<EnvVar>,
|
||||
}
|
||||
|
||||
/// A single environment variable for an MCP server.
|
||||
#[derive(Debug, Clone, serde::Serialize)]
|
||||
pub struct EnvVar {
|
||||
pub name: String,
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
/// Stop reason returned by `session/prompt` when the agent finishes a turn.
|
||||
///
|
||||
/// Maps to the `stopReason` field in the `SessionPromptResponse`.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum StopReason {
|
||||
/// Agent completed the turn normally (`"end_turn"`).
|
||||
EndTurn,
|
||||
/// Turn was cancelled via `session/cancel` (`"cancelled"`).
|
||||
Cancelled,
|
||||
/// Agent hit its token limit (`"max_tokens"`).
|
||||
MaxTokens,
|
||||
/// Agent hit its per-turn request limit (`"max_turn_requests"`).
|
||||
MaxTurnRequests,
|
||||
/// Agent refused the prompt (`"refusal"`).
|
||||
/// Note: refused turns are dropped from history by the agent.
|
||||
Refusal,
|
||||
}
|
||||
|
||||
impl StopReason {
|
||||
/// Parse a `stopReason` string from the ACP wire format.
|
||||
pub fn from_str(s: &str) -> Option<Self> {
|
||||
match s {
|
||||
"end_turn" => Some(Self::EndTurn),
|
||||
"cancelled" => Some(Self::Cancelled),
|
||||
"max_tokens" => Some(Self::MaxTokens),
|
||||
"max_turn_requests" => Some(Self::MaxTurnRequests),
|
||||
"refusal" => Some(Self::Refusal),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Errors that can occur in the ACP client.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum AcpError {
|
||||
#[error("IO error: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
|
||||
#[error("JSON error: {0}")]
|
||||
Json(#[from] serde_json::Error),
|
||||
|
||||
#[error("Agent process exited unexpectedly")]
|
||||
AgentExited,
|
||||
|
||||
#[error("Protocol error: {0}")]
|
||||
Protocol(String),
|
||||
}
|
||||
|
||||
// ─── AcpClient ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// ACP client that owns an agent subprocess and communicates over its stdio.
|
||||
///
|
||||
/// One `AcpClient` per agent process. Multiple sessions can be created on the
|
||||
/// same client via repeated calls to [`session_new`](AcpClient::session_new).
|
||||
pub struct AcpClient {
|
||||
/// The agent child process (kept alive to prevent zombie).
|
||||
child: Child,
|
||||
/// Write end of the agent's stdin pipe.
|
||||
stdin: ChildStdin,
|
||||
/// Buffered reader over the agent's stdout pipe (line-oriented).
|
||||
reader: BufReader<ChildStdout>,
|
||||
/// Monotonically increasing JSON-RPC request id counter.
|
||||
/// Harness-generated IDs are always numeric.
|
||||
next_id: u64,
|
||||
/// The id of a `session/request_permission` request that has been received
|
||||
/// but not yet responded to. Stored as `serde_json::Value` because JSON-RPC 2.0
|
||||
/// permits both numeric and string IDs from the agent.
|
||||
/// Used by [`cancel_with_cleanup`](AcpClient::cancel_with_cleanup) to send
|
||||
/// a `cancelled` outcome before the agent returns from `session/prompt`.
|
||||
pending_permission_id: Option<serde_json::Value>,
|
||||
/// Whether we have already sent a response to the pending permission request.
|
||||
/// Guards against double-response if a timeout fires after the allow_once
|
||||
/// response was written but before `pending_permission_id` was cleared.
|
||||
permission_responded: bool,
|
||||
}
|
||||
|
||||
impl AcpClient {
|
||||
// ── Lifecycle ─────────────────────────────────────────────────────────
|
||||
|
||||
/// Spawn the agent binary as a subprocess and connect to its stdio pipes.
|
||||
///
|
||||
/// After spawning, call [`initialize`](Self::initialize) before any other method.
|
||||
pub async fn spawn(command: &str, args: &[String]) -> Result<Self, AcpError> {
|
||||
use std::process::Stdio;
|
||||
|
||||
let mut cmd = tokio::process::Command::new(command);
|
||||
cmd.args(args)
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
// Inherit stderr so agent logs are visible in the harness terminal.
|
||||
.stderr(Stdio::inherit());
|
||||
|
||||
let mut child = cmd.spawn()?;
|
||||
|
||||
let stdin = child
|
||||
.stdin
|
||||
.take()
|
||||
.ok_or_else(|| AcpError::Protocol("failed to open agent stdin".into()))?;
|
||||
let stdout = child
|
||||
.stdout
|
||||
.take()
|
||||
.ok_or_else(|| AcpError::Protocol("failed to open agent stdout".into()))?;
|
||||
|
||||
Ok(Self {
|
||||
child,
|
||||
stdin,
|
||||
reader: BufReader::new(stdout),
|
||||
next_id: 0,
|
||||
pending_permission_id: None,
|
||||
permission_responded: false,
|
||||
})
|
||||
}
|
||||
|
||||
/// Send the `initialize` request and return the agent's response result value.
|
||||
///
|
||||
/// Must be called exactly once, before any other ACP method.
|
||||
/// The caller may inspect `agentCapabilities` in the returned value.
|
||||
pub async fn initialize(&mut self) -> Result<serde_json::Value, AcpError> {
|
||||
let params = serde_json::json!({
|
||||
"protocolVersion": 1,
|
||||
"clientCapabilities": {},
|
||||
"clientInfo": {
|
||||
"name": "sprout-acp",
|
||||
"version": env!("CARGO_PKG_VERSION")
|
||||
}
|
||||
});
|
||||
let result = self.send_request("initialize", params).await?;
|
||||
tracing::debug!(target: "acp::init", "initialize response: {result}");
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Send `session/new` and return the `sessionId` string.
|
||||
///
|
||||
/// `cwd` must be an absolute path. `mcp_servers` may be empty.
|
||||
pub async fn session_new(
|
||||
&mut self,
|
||||
cwd: &str,
|
||||
mcp_servers: Vec<McpServer>,
|
||||
) -> Result<String, AcpError> {
|
||||
let params = serde_json::json!({
|
||||
"cwd": cwd,
|
||||
"mcpServers": mcp_servers,
|
||||
});
|
||||
let result = self.send_request("session/new", params).await?;
|
||||
let session_id = result["sessionId"]
|
||||
.as_str()
|
||||
.ok_or_else(|| AcpError::Protocol("session/new response missing sessionId".into()))?
|
||||
.to_owned();
|
||||
tracing::info!(target: "acp::session", "session created: {session_id}");
|
||||
Ok(session_id)
|
||||
}
|
||||
|
||||
/// Send `session/prompt` and block until the agent returns a stop reason.
|
||||
///
|
||||
/// While waiting, incoming `session/update` notifications are logged and
|
||||
/// `session/request_permission` requests are auto-approved with `allow_once`.
|
||||
pub async fn session_prompt(
|
||||
&mut self,
|
||||
session_id: &str,
|
||||
prompt_text: &str,
|
||||
) -> Result<StopReason, AcpError> {
|
||||
let params = serde_json::json!({
|
||||
"sessionId": session_id,
|
||||
"prompt": [
|
||||
{ "type": "text", "text": prompt_text }
|
||||
]
|
||||
});
|
||||
let result = self.send_request("session/prompt", params).await?;
|
||||
self.parse_stop_reason(&result)
|
||||
}
|
||||
|
||||
/// Send a `session/cancel` **notification** (no `id` field, no response expected).
|
||||
///
|
||||
/// After calling this, the agent will eventually respond to the in-flight
|
||||
/// `session/prompt` with `stopReason: "cancelled"`. Use
|
||||
/// [`cancel_with_cleanup`](Self::cancel_with_cleanup) if you need to drain
|
||||
/// that response.
|
||||
///
|
||||
/// Note: async because writing to stdin requires async I/O.
|
||||
pub async fn session_cancel(&mut self, session_id: &str) -> Result<(), AcpError> {
|
||||
let params = serde_json::json!({
|
||||
"sessionId": session_id,
|
||||
});
|
||||
self.send_notification("session/cancel", params).await
|
||||
}
|
||||
|
||||
/// Cancel a turn cleanly, handling any pending permission request first.
|
||||
///
|
||||
/// Steps:
|
||||
/// 1. If there is a pending `session/request_permission` that hasn't been
|
||||
/// responded to yet, respond with `outcome: "cancelled"`.
|
||||
/// 2. Send `session/cancel` notification (no id).
|
||||
/// 3. Continue reading until the `session/prompt` response arrives with `stopReason: "cancelled"`.
|
||||
///
|
||||
/// Returns the final [`StopReason`] (almost always [`StopReason::Cancelled`]).
|
||||
pub async fn cancel_with_cleanup(&mut self, session_id: &str) -> Result<StopReason, AcpError> {
|
||||
// Step 1: respond to any pending permission request with "cancelled",
|
||||
// but only if we haven't already responded (guards against double-response race).
|
||||
if let Some(perm_id) = self.pending_permission_id.clone() {
|
||||
if !self.permission_responded {
|
||||
let response = permission_response_cancelled(&perm_id);
|
||||
self.write_ndjson(&response).await?;
|
||||
tracing::debug!(
|
||||
target: "acp::cancel",
|
||||
"responded cancelled to pending permission id={perm_id}"
|
||||
);
|
||||
}
|
||||
self.pending_permission_id = None;
|
||||
self.permission_responded = false;
|
||||
}
|
||||
|
||||
// Step 2: send session/cancel notification (no id)
|
||||
self.session_cancel(session_id).await?;
|
||||
tracing::info!(target: "acp::cancel", "sent session/cancel for {session_id}");
|
||||
|
||||
// Step 3: drain until we get the session/prompt response.
|
||||
// The prompt id is next_id - 1 (the most recently sent request was session/prompt).
|
||||
let prompt_id = self.next_id - 1;
|
||||
let result = self.read_until_response(prompt_id).await?;
|
||||
self.parse_stop_reason(&result)
|
||||
}
|
||||
|
||||
// ── Internal helpers ──────────────────────────────────────────────────
|
||||
|
||||
/// Serialize `value` as a single NDJSON line and flush to the agent's stdin.
|
||||
async fn write_ndjson(&mut self, value: &serde_json::Value) -> Result<(), AcpError> {
|
||||
let line = serde_json::to_string(value)?;
|
||||
self.stdin.write_all(line.as_bytes()).await?;
|
||||
self.stdin.write_all(b"\n").await?;
|
||||
self.stdin.flush().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Send a JSON-RPC request and wait for the matching response.
|
||||
///
|
||||
/// Assigns the next available id, writes the NDJSON line to stdin,
|
||||
/// then calls [`read_until_response`](Self::read_until_response).
|
||||
async fn send_request(
|
||||
&mut self,
|
||||
method: &str,
|
||||
params: serde_json::Value,
|
||||
) -> Result<serde_json::Value, AcpError> {
|
||||
let id = self.next_id;
|
||||
self.next_id += 1;
|
||||
|
||||
let msg = serde_json::json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": id,
|
||||
"method": method,
|
||||
"params": params,
|
||||
});
|
||||
|
||||
tracing::debug!(target: "acp::wire", "→ {}", &serde_json::to_string(&msg).unwrap_or_default());
|
||||
self.write_ndjson(&msg).await?;
|
||||
|
||||
self.read_until_response(id).await
|
||||
}
|
||||
|
||||
/// Send a JSON-RPC **notification** — no `id` field, no response expected.
|
||||
///
|
||||
/// Used for `session/cancel`. The absence of `id` is the JSON-RPC 2.0
|
||||
/// distinguisher between requests and notifications.
|
||||
async fn send_notification(
|
||||
&mut self,
|
||||
method: &str,
|
||||
params: serde_json::Value,
|
||||
) -> Result<(), AcpError> {
|
||||
// Notifications deliberately have NO "id" field.
|
||||
let msg = serde_json::json!({
|
||||
"jsonrpc": "2.0",
|
||||
"method": method,
|
||||
"params": params,
|
||||
});
|
||||
|
||||
tracing::debug!(target: "acp::wire", "→ (notification) {}", &serde_json::to_string(&msg).unwrap_or_default());
|
||||
self.write_ndjson(&msg).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Core message loop: read NDJSON lines until we get a response matching `expected_id`.
|
||||
///
|
||||
/// While waiting, handles:
|
||||
/// - `session/update` notifications → logged via tracing
|
||||
/// - `session/request_permission` requests → auto-approved with `allow_once`
|
||||
/// - Any other messages → debug-logged and ignored
|
||||
///
|
||||
/// Compares the incoming `id` field as a `serde_json::Value` against
|
||||
/// `json!(expected_id)` so that both numeric and string IDs work correctly.
|
||||
async fn read_until_response(
|
||||
&mut self,
|
||||
expected_id: u64,
|
||||
) -> Result<serde_json::Value, AcpError> {
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
let n = self.reader.read_line(&mut line).await?;
|
||||
if n == 0 {
|
||||
return Err(AcpError::AgentExited);
|
||||
}
|
||||
|
||||
let trimmed = line.trim();
|
||||
if trimmed.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
tracing::debug!(target: "acp::wire", "← {trimmed}");
|
||||
|
||||
let msg: serde_json::Value = match serde_json::from_str(trimmed) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
tracing::warn!(
|
||||
target: "acp::wire",
|
||||
"failed to parse line as JSON: {e} — skipping"
|
||||
);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
// Check if this is a response to our expected request (has matching id).
|
||||
// Compare as serde_json::Value so both numeric and string IDs work.
|
||||
if let Some(id) = msg.get("id") {
|
||||
if *id == serde_json::json!(expected_id) {
|
||||
if let Some(error) = msg.get("error") {
|
||||
return Err(AcpError::Protocol(error.to_string()));
|
||||
}
|
||||
return Ok(msg["result"].clone());
|
||||
}
|
||||
// Has an id but not ours — fall through to method dispatch
|
||||
// (e.g., session/request_permission has both id and method).
|
||||
}
|
||||
|
||||
// Dispatch by method name (notifications and agent-initiated requests).
|
||||
if let Some(method) = msg.get("method").and_then(|v| v.as_str()) {
|
||||
match method {
|
||||
"session/update" => {
|
||||
self.handle_session_update(&msg);
|
||||
}
|
||||
"session/request_permission" => {
|
||||
self.handle_permission_request(&msg).await?;
|
||||
}
|
||||
other => {
|
||||
tracing::debug!(target: "acp::wire", "ignoring unknown method: {other}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Log a `session/update` notification via tracing.
|
||||
///
|
||||
/// The discriminator field is `sessionUpdate` (not `type`) per the ACP schema.
|
||||
fn handle_session_update(&self, msg: &serde_json::Value) {
|
||||
let update = &msg["params"]["update"];
|
||||
let update_type = update
|
||||
.get("sessionUpdate")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("unknown");
|
||||
|
||||
match update_type {
|
||||
"agent_message_chunk" => {
|
||||
if let Some(text) = update["content"]["text"].as_str() {
|
||||
tracing::info!(target: "acp::stream", "{text}");
|
||||
}
|
||||
}
|
||||
"tool_call" => {
|
||||
let title = update
|
||||
.get("title")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("unknown");
|
||||
let kind = update
|
||||
.get("kind")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("unknown");
|
||||
tracing::info!(target: "acp::tool", "tool_call: {title} ({kind})");
|
||||
}
|
||||
"tool_call_update" => {
|
||||
let tool_id = update
|
||||
.get("toolCallId")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("?");
|
||||
let status = update.get("status").and_then(|v| v.as_str()).unwrap_or("?");
|
||||
tracing::info!(target: "acp::tool", "tool_call_update: {tool_id} → {status}");
|
||||
}
|
||||
"plan" => {
|
||||
tracing::info!(target: "acp::plan", "plan update received");
|
||||
}
|
||||
"agent_thought_chunk" => {
|
||||
if let Some(text) = update["content"]["text"].as_str() {
|
||||
tracing::debug!(target: "acp::thought", "{text}");
|
||||
}
|
||||
}
|
||||
other => {
|
||||
tracing::debug!(target: "acp::update", "session/update: {other}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Auto-approve a `session/request_permission` request from the agent.
|
||||
///
|
||||
/// Finds the option with `kind == "allow_once"` and responds with its `optionId`.
|
||||
/// If no `allow_once` option exists, falls back to `reject_once`.
|
||||
///
|
||||
/// **Critical:** Never hardcode `optionId` — always find it dynamically by `kind`.
|
||||
///
|
||||
/// The request `id` is stored as `serde_json::Value` to support both numeric
|
||||
/// and string IDs per JSON-RPC 2.0.
|
||||
async fn handle_permission_request(&mut self, msg: &serde_json::Value) -> Result<(), AcpError> {
|
||||
// Extract id as a Value — JSON-RPC 2.0 allows both numeric and string IDs.
|
||||
let id = msg
|
||||
.get("id")
|
||||
.cloned()
|
||||
.ok_or_else(|| AcpError::Protocol("permission request missing id".into()))?;
|
||||
|
||||
// Store pending permission id so cancel_with_cleanup can respond to it.
|
||||
self.pending_permission_id = Some(id.clone());
|
||||
// Mark as not yet responded — guards against double-response race.
|
||||
self.permission_responded = false;
|
||||
|
||||
let options = msg["params"]["options"]
|
||||
.as_array()
|
||||
.ok_or_else(|| AcpError::Protocol("permission request missing options".into()))?;
|
||||
|
||||
tracing::debug!(
|
||||
target: "acp::permission",
|
||||
"session/request_permission id={id}, {} options",
|
||||
options.len()
|
||||
);
|
||||
|
||||
// Find allow_once by kind — NEVER hardcode optionId.
|
||||
let allow_once = options
|
||||
.iter()
|
||||
.find(|opt| opt.get("kind").and_then(|k| k.as_str()) == Some("allow_once"));
|
||||
|
||||
let response = if let Some(opt) = allow_once {
|
||||
let option_id = opt["optionId"]
|
||||
.as_str()
|
||||
.ok_or_else(|| AcpError::Protocol("allow_once option missing optionId".into()))?;
|
||||
tracing::info!(
|
||||
target: "acp::permission",
|
||||
"auto-approving permission id={id} with allow_once optionId={option_id:?}"
|
||||
);
|
||||
permission_response_selected(&id, option_id)
|
||||
} else {
|
||||
// No allow_once — fall back to reject_once.
|
||||
tracing::warn!(
|
||||
target: "acp::permission",
|
||||
"no allow_once option found in permission request id={id}, falling back to reject_once"
|
||||
);
|
||||
let reject = options
|
||||
.iter()
|
||||
.find(|opt| opt.get("kind").and_then(|k| k.as_str()) == Some("reject_once"));
|
||||
|
||||
if let Some(opt) = reject {
|
||||
let option_id = opt["optionId"].as_str().unwrap_or("reject");
|
||||
permission_response_selected(&id, option_id)
|
||||
} else {
|
||||
return Err(AcpError::Protocol(
|
||||
"no suitable permission option found (neither allow_once nor reject_once)"
|
||||
.into(),
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
self.write_ndjson(&response).await?;
|
||||
// Mark as responded and clear pending now that we've successfully written the response.
|
||||
self.permission_responded = true;
|
||||
self.pending_permission_id = None;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Parse `stopReason` from a `session/prompt` result value.
|
||||
fn parse_stop_reason(&self, result: &serde_json::Value) -> Result<StopReason, AcpError> {
|
||||
let raw = result["stopReason"].as_str().ok_or_else(|| {
|
||||
AcpError::Protocol("session/prompt response missing stopReason".into())
|
||||
})?;
|
||||
StopReason::from_str(raw)
|
||||
.ok_or_else(|| AcpError::Protocol(format!("unknown stopReason: {raw:?}")))
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Permission response constructors ────────────────────────────────────────
|
||||
|
||||
/// Build a JSON-RPC permission response with `outcome: "selected"`.
|
||||
fn permission_response_selected(id: &serde_json::Value, option_id: &str) -> serde_json::Value {
|
||||
serde_json::json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": id,
|
||||
"result": { "outcome": { "outcome": "selected", "optionId": option_id } }
|
||||
})
|
||||
}
|
||||
|
||||
/// Build a JSON-RPC permission response with `outcome: "cancelled"`.
|
||||
fn permission_response_cancelled(id: &serde_json::Value) -> serde_json::Value {
|
||||
serde_json::json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": id,
|
||||
"result": { "outcome": { "outcome": "cancelled" } }
|
||||
})
|
||||
}
|
||||
|
||||
// ─── Drop: kill child process ─────────────────────────────────────────────────
|
||||
|
||||
impl Drop for AcpClient {
|
||||
fn drop(&mut self) {
|
||||
// Best-effort kill — ignore errors (process may have already exited).
|
||||
let _ = self.child.start_kill();
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Tests ───────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// ── StopReason parsing ────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn stop_reason_parses_all_known_values() {
|
||||
assert_eq!(StopReason::from_str("end_turn"), Some(StopReason::EndTurn));
|
||||
assert_eq!(
|
||||
StopReason::from_str("cancelled"),
|
||||
Some(StopReason::Cancelled)
|
||||
);
|
||||
assert_eq!(
|
||||
StopReason::from_str("max_tokens"),
|
||||
Some(StopReason::MaxTokens)
|
||||
);
|
||||
assert_eq!(
|
||||
StopReason::from_str("max_turn_requests"),
|
||||
Some(StopReason::MaxTurnRequests)
|
||||
);
|
||||
assert_eq!(StopReason::from_str("refusal"), Some(StopReason::Refusal));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stop_reason_returns_none_for_unknown() {
|
||||
assert_eq!(StopReason::from_str("unknown_value"), None);
|
||||
assert_eq!(StopReason::from_str(""), None);
|
||||
assert_eq!(StopReason::from_str("END_TURN"), None); // case-sensitive
|
||||
assert_eq!(StopReason::from_str("endturn"), None); // no camelCase
|
||||
}
|
||||
|
||||
// ── Permission option finding ─────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn find_allow_once_by_kind_not_by_option_id() {
|
||||
// optionId values are intentionally non-obvious to prove we don't hardcode them.
|
||||
let options: Vec<serde_json::Value> = serde_json::from_str(
|
||||
r#"[
|
||||
{"optionId": "opt-reject-42", "name": "Reject", "kind": "reject_once"},
|
||||
{"optionId": "opt-allow-99", "name": "Allow once", "kind": "allow_once"},
|
||||
{"optionId": "opt-always-7", "name": "Always allow", "kind": "allow_always"}
|
||||
]"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let allow_once = options
|
||||
.iter()
|
||||
.find(|opt| opt.get("kind").and_then(|k| k.as_str()) == Some("allow_once"));
|
||||
|
||||
assert!(allow_once.is_some(), "should find allow_once option");
|
||||
let opt = allow_once.unwrap();
|
||||
// Found by kind, not by hardcoded optionId
|
||||
assert_eq!(opt["kind"].as_str(), Some("allow_once"));
|
||||
assert_eq!(opt["optionId"].as_str(), Some("opt-allow-99"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_allow_once_returns_none_when_absent() {
|
||||
let options: Vec<serde_json::Value> = serde_json::from_str(
|
||||
r#"[
|
||||
{"optionId": "reject-1", "name": "Reject", "kind": "reject_once"},
|
||||
{"optionId": "reject-always", "name": "Always reject", "kind": "reject_always"}
|
||||
]"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let allow_once = options
|
||||
.iter()
|
||||
.find(|opt| opt.get("kind").and_then(|k| k.as_str()) == Some("allow_once"));
|
||||
|
||||
assert!(allow_once.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_reject_once_fallback_when_no_allow_once() {
|
||||
let options: Vec<serde_json::Value> = serde_json::from_str(
|
||||
r#"[{"optionId": "rej-x", "name": "Reject", "kind": "reject_once"}]"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let allow_once = options
|
||||
.iter()
|
||||
.find(|opt| opt.get("kind").and_then(|k| k.as_str()) == Some("allow_once"));
|
||||
assert!(allow_once.is_none());
|
||||
|
||||
let reject_once = options
|
||||
.iter()
|
||||
.find(|opt| opt.get("kind").and_then(|k| k.as_str()) == Some("reject_once"));
|
||||
assert!(reject_once.is_some());
|
||||
assert_eq!(reject_once.unwrap()["optionId"].as_str(), Some("rej-x"));
|
||||
}
|
||||
|
||||
// ── JSON-RPC message construction ─────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn request_has_id_field() {
|
||||
let id: u64 = 42;
|
||||
let msg = serde_json::json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": id,
|
||||
"method": "initialize",
|
||||
"params": {}
|
||||
});
|
||||
assert!(msg.get("id").is_some(), "request must have id field");
|
||||
assert_eq!(msg["id"].as_u64(), Some(42));
|
||||
assert_eq!(msg["jsonrpc"].as_str(), Some("2.0"));
|
||||
assert_eq!(msg["method"].as_str(), Some("initialize"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn notification_has_no_id_field() {
|
||||
// session/cancel is a notification — must NOT have an id field.
|
||||
let msg = serde_json::json!({
|
||||
"jsonrpc": "2.0",
|
||||
"method": "session/cancel",
|
||||
"params": {
|
||||
"sessionId": "sess_abc123"
|
||||
}
|
||||
});
|
||||
assert!(
|
||||
msg.get("id").is_none(),
|
||||
"notification must NOT have id field"
|
||||
);
|
||||
assert_eq!(msg["jsonrpc"].as_str(), Some("2.0"));
|
||||
assert_eq!(msg["method"].as_str(), Some("session/cancel"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn initialize_request_format() {
|
||||
let msg = serde_json::json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": 0u64,
|
||||
"method": "initialize",
|
||||
"params": {
|
||||
"protocolVersion": 1,
|
||||
"clientCapabilities": {},
|
||||
"clientInfo": {
|
||||
"name": "sprout-acp",
|
||||
"version": "0.1.0"
|
||||
}
|
||||
}
|
||||
});
|
||||
assert_eq!(msg["params"]["protocolVersion"].as_u64(), Some(1));
|
||||
assert_eq!(
|
||||
msg["params"]["clientInfo"]["name"].as_str(),
|
||||
Some("sprout-acp")
|
||||
);
|
||||
assert!(msg["params"]["clientCapabilities"].is_object());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn session_new_mcp_server_has_required_fields() {
|
||||
// Schema requires name, command, args, env — all present, args/env may be empty.
|
||||
let server = McpServer {
|
||||
name: "sprout-mcp".into(),
|
||||
command: "/usr/local/bin/sprout-mcp-server".into(),
|
||||
args: vec![],
|
||||
env: vec![
|
||||
EnvVar {
|
||||
name: "SPROUT_RELAY_URL".into(),
|
||||
value: "ws://localhost:3000".into(),
|
||||
},
|
||||
EnvVar {
|
||||
name: "SPROUT_PRIVATE_KEY".into(),
|
||||
value: "nsec1abc".into(),
|
||||
},
|
||||
],
|
||||
};
|
||||
let serialized = serde_json::to_value(&server).unwrap();
|
||||
assert_eq!(serialized["name"].as_str(), Some("sprout-mcp"));
|
||||
assert_eq!(
|
||||
serialized["command"].as_str(),
|
||||
Some("/usr/local/bin/sprout-mcp-server")
|
||||
);
|
||||
assert!(serialized["args"].is_array());
|
||||
assert_eq!(serialized["args"].as_array().unwrap().len(), 0);
|
||||
assert!(serialized["env"].is_array());
|
||||
assert_eq!(serialized["env"].as_array().unwrap().len(), 2);
|
||||
assert_eq!(
|
||||
serialized["env"][0]["name"].as_str(),
|
||||
Some("SPROUT_RELAY_URL")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn session_prompt_request_format() {
|
||||
let prompt_text = "[Sprout @mention]\nChannel: test\nFrom: npub1...\nMessage: hello";
|
||||
let msg = serde_json::json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": 2u64,
|
||||
"method": "session/prompt",
|
||||
"params": {
|
||||
"sessionId": "sess_abc123",
|
||||
"prompt": [
|
||||
{ "type": "text", "text": prompt_text }
|
||||
]
|
||||
}
|
||||
});
|
||||
assert_eq!(msg["method"].as_str(), Some("session/prompt"));
|
||||
let prompt = msg["params"]["prompt"].as_array().unwrap();
|
||||
assert_eq!(prompt.len(), 1);
|
||||
assert_eq!(prompt[0]["type"].as_str(), Some("text"));
|
||||
assert_eq!(prompt[0]["text"].as_str(), Some(prompt_text));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn permission_response_selected_format() {
|
||||
let id: u64 = 5;
|
||||
let option_id = "opt-allow-99";
|
||||
let response = serde_json::json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": id,
|
||||
"result": {
|
||||
"outcome": {
|
||||
"outcome": "selected",
|
||||
"optionId": option_id
|
||||
}
|
||||
}
|
||||
});
|
||||
assert_eq!(response["id"].as_u64(), Some(5));
|
||||
assert_eq!(
|
||||
response["result"]["outcome"]["outcome"].as_str(),
|
||||
Some("selected")
|
||||
);
|
||||
assert_eq!(
|
||||
response["result"]["outcome"]["optionId"].as_str(),
|
||||
Some("opt-allow-99")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn permission_response_cancelled_format() {
|
||||
let id: u64 = 5;
|
||||
let response = serde_json::json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": id,
|
||||
"result": {
|
||||
"outcome": {
|
||||
"outcome": "cancelled"
|
||||
}
|
||||
}
|
||||
});
|
||||
assert_eq!(
|
||||
response["result"]["outcome"]["outcome"].as_str(),
|
||||
Some("cancelled")
|
||||
);
|
||||
// cancelled outcome has no optionId
|
||||
assert!(response["result"]["outcome"].get("optionId").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn session_cancel_notification_has_session_id_in_params() {
|
||||
let session_id = "sess_xyz789";
|
||||
let msg = serde_json::json!({
|
||||
"jsonrpc": "2.0",
|
||||
"method": "session/cancel",
|
||||
"params": {
|
||||
"sessionId": session_id
|
||||
}
|
||||
});
|
||||
// Must have no id (notification)
|
||||
assert!(msg.get("id").is_none());
|
||||
// Must have sessionId in params
|
||||
assert_eq!(msg["params"]["sessionId"].as_str(), Some("sess_xyz789"));
|
||||
}
|
||||
|
||||
// ── String ID handling (Fix 1) ────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn permission_request_with_string_id() {
|
||||
// Verify that permission response uses the same ID type as the request.
|
||||
// JSON-RPC 2.0 permits string IDs from the agent.
|
||||
let string_id = serde_json::json!("perm-req-001");
|
||||
let response = serde_json::json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": string_id,
|
||||
"result": {
|
||||
"outcome": { "outcome": "selected", "optionId": "allow-once" }
|
||||
}
|
||||
});
|
||||
assert_eq!(response["id"], "perm-req-001");
|
||||
assert!(response["id"].is_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn id_comparison_works_for_numeric_and_string() {
|
||||
// Verify json!(expected_id) comparison logic used in read_until_response.
|
||||
let expected_id: u64 = 3;
|
||||
let numeric_response_id = serde_json::json!(3u64);
|
||||
let string_response_id = serde_json::json!("3");
|
||||
|
||||
// Numeric matches
|
||||
assert_eq!(numeric_response_id, serde_json::json!(expected_id));
|
||||
// String does NOT match numeric (correct — different types)
|
||||
assert_ne!(string_response_id, serde_json::json!(expected_id));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn permission_cancelled_response_preserves_id_type() {
|
||||
// String ID from agent should be echoed back as string in cancelled response.
|
||||
let string_id = serde_json::json!("req-abc");
|
||||
let cancelled = serde_json::json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": string_id.clone(),
|
||||
"result": { "outcome": { "outcome": "cancelled" } }
|
||||
});
|
||||
assert_eq!(cancelled["id"], string_id);
|
||||
assert!(cancelled["id"].is_string());
|
||||
|
||||
// Numeric ID from agent should be echoed back as numeric.
|
||||
let numeric_id = serde_json::json!(42u64);
|
||||
let cancelled_numeric = serde_json::json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": numeric_id.clone(),
|
||||
"result": { "outcome": { "outcome": "cancelled" } }
|
||||
});
|
||||
assert_eq!(cancelled_numeric["id"], numeric_id);
|
||||
assert!(cancelled_numeric["id"].is_number());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
use nostr::Keys;
|
||||
use thiserror::Error;
|
||||
|
||||
/// Errors that can occur when loading configuration from environment variables.
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ConfigError {
|
||||
#[error("required environment variable {0} is not set")]
|
||||
MissingVar(&'static str),
|
||||
|
||||
#[error("failed to parse nostr keys from {var}: {source}")]
|
||||
KeyParse {
|
||||
var: &'static str,
|
||||
#[source]
|
||||
source: nostr::key::Error,
|
||||
},
|
||||
}
|
||||
|
||||
/// Configuration for the sprout-acp harness.
|
||||
#[derive(Debug)]
|
||||
pub struct Config {
|
||||
/// Agent's nostr keypair — used for both relay auth and agent identity.
|
||||
///
|
||||
/// Parsed from `SPROUT_PRIVATE_KEY` (preferred) or the legacy
|
||||
/// `SPROUT_ACP_PRIVATE_KEY` + `SPROUT_AGENT_PRIVATE_KEY` pair.
|
||||
pub keys: Keys,
|
||||
/// API token, optional. Required if the relay enforces token auth.
|
||||
pub api_token: Option<String>,
|
||||
/// Relay WebSocket URL (`SPROUT_RELAY_URL`). Default: `ws://localhost:3000`.
|
||||
pub relay_url: String,
|
||||
|
||||
// --- Agent binary ---
|
||||
/// Agent command (`SPROUT_ACP_AGENT_COMMAND`). Default: `goose`.
|
||||
pub agent_command: String,
|
||||
/// Agent arguments (`SPROUT_ACP_AGENT_ARGS`, comma-separated). Default: `["acp"]`.
|
||||
pub agent_args: Vec<String>,
|
||||
|
||||
// --- MCP server ---
|
||||
/// MCP server binary path (`SPROUT_ACP_MCP_COMMAND`). Default: `sprout-mcp-server`.
|
||||
pub mcp_command: String,
|
||||
|
||||
// --- Tuning ---
|
||||
/// Maximum turn duration in seconds (`SPROUT_ACP_TURN_TIMEOUT`). Default: 300.
|
||||
pub turn_timeout_secs: u64,
|
||||
}
|
||||
|
||||
/// Parse a nostr `Keys` from the named environment variable.
|
||||
fn parse_keys_var(var: &'static str) -> Result<Keys, ConfigError> {
|
||||
let nsec = std::env::var(var).map_err(|_| ConfigError::MissingVar(var))?;
|
||||
Keys::parse(&nsec).map_err(|e| ConfigError::KeyParse { var, source: e })
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/// Load configuration from environment variables.
|
||||
///
|
||||
/// Key resolution order:
|
||||
/// 1. `SPROUT_PRIVATE_KEY` — single key for everything (preferred)
|
||||
/// 2. `SPROUT_ACP_PRIVATE_KEY` — legacy harness key (fallback)
|
||||
pub fn from_env() -> Result<Self, ConfigError> {
|
||||
let keys = parse_keys_var("SPROUT_PRIVATE_KEY")
|
||||
.or_else(|_| parse_keys_var("SPROUT_ACP_PRIVATE_KEY"))?;
|
||||
|
||||
let api_token = std::env::var("SPROUT_API_TOKEN")
|
||||
.or_else(|_| std::env::var("SPROUT_ACP_API_TOKEN"))
|
||||
.ok();
|
||||
|
||||
let relay_url =
|
||||
std::env::var("SPROUT_RELAY_URL").unwrap_or_else(|_| "ws://localhost:3000".to_string());
|
||||
|
||||
let agent_command =
|
||||
std::env::var("SPROUT_ACP_AGENT_COMMAND").unwrap_or_else(|_| "goose".to_string());
|
||||
|
||||
let agent_args = std::env::var("SPROUT_ACP_AGENT_ARGS")
|
||||
.map(|s| s.split(',').map(|a| a.trim().to_string()).collect())
|
||||
.unwrap_or_else(|_| vec!["acp".to_string()]);
|
||||
|
||||
let mcp_command = std::env::var("SPROUT_ACP_MCP_COMMAND")
|
||||
.unwrap_or_else(|_| "sprout-mcp-server".to_string());
|
||||
|
||||
let turn_timeout_secs = std::env::var("SPROUT_ACP_TURN_TIMEOUT")
|
||||
.ok()
|
||||
.and_then(|s| s.parse::<u64>().ok())
|
||||
.unwrap_or(300);
|
||||
|
||||
Ok(Config {
|
||||
keys,
|
||||
api_token,
|
||||
relay_url,
|
||||
agent_command,
|
||||
agent_args,
|
||||
mcp_command,
|
||||
turn_timeout_secs,
|
||||
})
|
||||
}
|
||||
|
||||
/// Return a human-readable summary (no secrets).
|
||||
pub fn summary(&self) -> String {
|
||||
format!(
|
||||
"relay={} pubkey={} agent_cmd={} {} mcp_cmd={} turn_timeout={}s",
|
||||
self.relay_url,
|
||||
self.keys.public_key().to_hex(),
|
||||
self.agent_command,
|
||||
self.agent_args.join(" "),
|
||||
self.mcp_command,
|
||||
self.turn_timeout_secs,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,299 @@
|
||||
mod acp;
|
||||
mod config;
|
||||
mod queue;
|
||||
mod relay;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::Result;
|
||||
use nostr::ToBech32;
|
||||
use tokio::time::timeout;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use uuid::Uuid;
|
||||
|
||||
use acp::{AcpClient, AcpError, EnvVar, McpServer, StopReason};
|
||||
use config::Config;
|
||||
use queue::{EventQueue, QueuedEvent};
|
||||
use relay::HarnessRelay;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(
|
||||
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("sprout_acp=info")),
|
||||
)
|
||||
.compact()
|
||||
.init();
|
||||
|
||||
let config = Config::from_env().map_err(|e| anyhow::anyhow!("configuration error: {e}"))?;
|
||||
tracing::info!("sprout-acp starting: {}", config.summary());
|
||||
|
||||
// ── Step 1: Spawn ACP agent subprocess and initialize ─────────────────────
|
||||
let mut acp = spawn_and_init(&config).await?;
|
||||
|
||||
// ── Step 2: Connect to Sprout relay ──────────────────────────────────────
|
||||
let pubkey_hex = config.keys.public_key().to_hex();
|
||||
let mut relay = HarnessRelay::connect(
|
||||
&config.relay_url,
|
||||
&config.keys,
|
||||
config.api_token.as_deref(),
|
||||
&pubkey_hex,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("relay connect error: {e}"))?;
|
||||
|
||||
tracing::info!("connected to relay at {}", config.relay_url);
|
||||
|
||||
// ── Step 3: Discover channels and subscribe ───────────────────────────────
|
||||
let channels = relay
|
||||
.discover_channels()
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("channel discovery error: {e}"))?;
|
||||
|
||||
tracing::info!("discovered {} channel(s)", channels.len());
|
||||
|
||||
for channel_id in &channels {
|
||||
if let Err(e) = relay.subscribe_channel(*channel_id).await {
|
||||
tracing::warn!("failed to subscribe to channel {channel_id}: {e}");
|
||||
} else {
|
||||
tracing::info!("subscribed to channel {channel_id}");
|
||||
}
|
||||
}
|
||||
|
||||
// ── Step 4: Main orchestration loop ──────────────────────────────────────
|
||||
let mut sessions: HashMap<Uuid, String> = HashMap::new();
|
||||
let mut queue = EventQueue::new();
|
||||
let mcp_servers = build_mcp_servers(&config);
|
||||
let turn_timeout = Duration::from_secs(config.turn_timeout_secs);
|
||||
|
||||
loop {
|
||||
// Wait for the next relay event or shutdown signal.
|
||||
let sprout_event = tokio::select! {
|
||||
event = relay.next_event() => event,
|
||||
_ = tokio::signal::ctrl_c() => {
|
||||
tracing::info!("shutting down (SIGINT/SIGTERM)");
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
match sprout_event {
|
||||
Some(sprout_event) => {
|
||||
// Push event into the queue.
|
||||
queue.push(QueuedEvent {
|
||||
channel_id: sprout_event.channel_id,
|
||||
event: sprout_event.event,
|
||||
received_at: std::time::Instant::now(),
|
||||
});
|
||||
|
||||
// Try to flush and process batches.
|
||||
loop {
|
||||
let batch = match queue.flush_next() {
|
||||
Some(b) => b,
|
||||
None => break,
|
||||
};
|
||||
|
||||
let channel_id = batch.channel_id;
|
||||
// Format prompt before potentially requeuing (borrows batch by ref).
|
||||
let prompt_text = queue::format_prompt(&batch);
|
||||
|
||||
// Get or create session for this channel.
|
||||
let session_id = match get_or_create_session(
|
||||
&mut sessions,
|
||||
channel_id,
|
||||
&mut acp,
|
||||
&mcp_servers,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(id) => id,
|
||||
Err(e) => {
|
||||
tracing::error!(
|
||||
"failed to create session for channel {channel_id}: {e}"
|
||||
);
|
||||
queue.requeue(batch);
|
||||
queue.mark_complete();
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
tracing::info!(
|
||||
"prompting agent for channel {channel_id} (session {session_id}, {} event(s))",
|
||||
batch.events.len()
|
||||
);
|
||||
|
||||
// Send prompt with turn timeout.
|
||||
let prompt_result =
|
||||
timeout(turn_timeout, acp.session_prompt(&session_id, &prompt_text)).await;
|
||||
|
||||
match prompt_result {
|
||||
Ok(Ok(stop_reason)) => {
|
||||
log_stop_reason(channel_id, &stop_reason);
|
||||
queue.mark_complete();
|
||||
}
|
||||
Ok(Err(AcpError::AgentExited)) => {
|
||||
tracing::error!("agent process exited — respawning");
|
||||
sessions.clear();
|
||||
queue.requeue(batch);
|
||||
queue.mark_complete();
|
||||
acp = respawn_agent(&config).await?;
|
||||
break;
|
||||
}
|
||||
Ok(Err(e)) => {
|
||||
tracing::error!("session_prompt error for channel {channel_id}: {e}");
|
||||
queue.requeue(batch);
|
||||
queue.mark_complete();
|
||||
sessions.remove(&channel_id);
|
||||
break;
|
||||
}
|
||||
Err(_elapsed) => {
|
||||
tracing::warn!(
|
||||
"turn timeout ({}s) for channel {channel_id} — cancelling",
|
||||
config.turn_timeout_secs
|
||||
);
|
||||
match acp.cancel_with_cleanup(&session_id).await {
|
||||
Ok(stop_reason) => {
|
||||
log_stop_reason(channel_id, &stop_reason);
|
||||
}
|
||||
Err(AcpError::AgentExited) => {
|
||||
tracing::error!("agent exited during cancel — respawning");
|
||||
sessions.clear();
|
||||
acp = respawn_agent(&config).await?;
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("cancel_with_cleanup error: {e}");
|
||||
}
|
||||
}
|
||||
queue.mark_complete();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
// Relay connection lost — reconnect.
|
||||
tracing::warn!("relay connection lost — reconnecting");
|
||||
loop {
|
||||
match relay.reconnect().await {
|
||||
Ok(()) => {
|
||||
tracing::info!("relay reconnected");
|
||||
break;
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("relay reconnect failed: {e} — retrying in 5s");
|
||||
tokio::time::sleep(Duration::from_secs(5)).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tracing::info!("sprout-acp stopped");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ── Helper: respawn agent after exit ─────────────────────────────────────────
|
||||
|
||||
async fn respawn_agent(config: &Config) -> Result<AcpClient> {
|
||||
match spawn_and_init(config).await {
|
||||
Ok(new_acp) => {
|
||||
tracing::info!("agent respawned successfully");
|
||||
Ok(new_acp)
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("failed to respawn agent: {e}");
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── Helper: spawn agent and initialize ───────────────────────────────────────
|
||||
|
||||
async fn spawn_and_init(config: &Config) -> Result<AcpClient> {
|
||||
let mut acp = AcpClient::spawn(&config.agent_command, &config.agent_args)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("failed to spawn agent: {e}"))?;
|
||||
|
||||
let init_result = acp
|
||||
.initialize()
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("agent initialize failed: {e}"))?;
|
||||
|
||||
tracing::info!("agent initialized: {init_result}");
|
||||
Ok(acp)
|
||||
}
|
||||
|
||||
// ── Helper: get or create session for a channel ───────────────────────────────
|
||||
|
||||
async fn get_or_create_session(
|
||||
sessions: &mut HashMap<Uuid, String>,
|
||||
channel_id: Uuid,
|
||||
acp: &mut AcpClient,
|
||||
mcp_servers: &[McpServer],
|
||||
) -> Result<String, AcpError> {
|
||||
if let Some(session_id) = sessions.get(&channel_id) {
|
||||
return Ok(session_id.clone());
|
||||
}
|
||||
|
||||
let cwd = std::env::current_dir()
|
||||
.unwrap_or_else(|_| std::path::PathBuf::from("/"))
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
|
||||
let session_id = acp.session_new(&cwd, mcp_servers.to_vec()).await?;
|
||||
tracing::info!("created session {session_id} for channel {channel_id}");
|
||||
sessions.insert(channel_id, session_id.clone());
|
||||
Ok(session_id)
|
||||
}
|
||||
|
||||
// ── Helper: build MCP server config from Config ───────────────────────────────
|
||||
|
||||
fn build_mcp_servers(config: &Config) -> Vec<McpServer> {
|
||||
vec![McpServer {
|
||||
name: "sprout-mcp".to_string(),
|
||||
command: config.mcp_command.clone(),
|
||||
args: vec![],
|
||||
env: {
|
||||
let mut env = vec![
|
||||
EnvVar {
|
||||
name: "SPROUT_RELAY_URL".into(),
|
||||
value: config.relay_url.clone(),
|
||||
},
|
||||
EnvVar {
|
||||
name: "SPROUT_PRIVATE_KEY".into(),
|
||||
value: config.keys.secret_key().to_bech32().unwrap_or_default(),
|
||||
},
|
||||
];
|
||||
if let Some(ref token) = config.api_token {
|
||||
env.push(EnvVar {
|
||||
name: "SPROUT_API_TOKEN".into(),
|
||||
value: token.clone(),
|
||||
});
|
||||
}
|
||||
env
|
||||
},
|
||||
}]
|
||||
}
|
||||
|
||||
// ── Helper: log stop reason at appropriate level ──────────────────────────────
|
||||
|
||||
fn log_stop_reason(channel_id: Uuid, stop_reason: &StopReason) {
|
||||
match stop_reason {
|
||||
StopReason::EndTurn => {
|
||||
tracing::info!("turn complete for channel {channel_id}: end_turn");
|
||||
}
|
||||
StopReason::Cancelled => {
|
||||
tracing::warn!("turn cancelled for channel {channel_id}");
|
||||
}
|
||||
StopReason::MaxTokens => {
|
||||
tracing::warn!("turn hit max_tokens for channel {channel_id}");
|
||||
}
|
||||
StopReason::MaxTurnRequests => {
|
||||
tracing::warn!("turn hit max_turn_requests for channel {channel_id}");
|
||||
}
|
||||
StopReason::Refusal => {
|
||||
tracing::warn!("turn refused for channel {channel_id}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,502 @@
|
||||
//! Event queue state machine for sprout-acp.
|
||||
//!
|
||||
//! Manages per-channel event queues with a global one-in-flight constraint.
|
||||
//! When the harness is ready to prompt the agent, it flushes the channel with
|
||||
//! the oldest pending event, draining ALL events for that channel into a single
|
||||
//! batch. Only one `session/prompt` is in flight at a time across all channels.
|
||||
|
||||
use nostr::{Event, ToBech32};
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::time::Instant;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// An event waiting in the queue.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct QueuedEvent {
|
||||
pub channel_id: Uuid,
|
||||
pub event: Event,
|
||||
pub received_at: Instant,
|
||||
}
|
||||
|
||||
/// A batch of events to prompt the agent with.
|
||||
#[derive(Debug)]
|
||||
pub struct FlushBatch {
|
||||
pub channel_id: Uuid,
|
||||
pub events: Vec<Event>,
|
||||
}
|
||||
|
||||
/// Per-channel event queue with global one-in-flight enforcement.
|
||||
///
|
||||
/// # State Machine
|
||||
///
|
||||
/// ```text
|
||||
/// State:
|
||||
/// queues: Map<channel_id, VecDeque<QueuedEvent>>
|
||||
/// prompt_in_flight: bool
|
||||
///
|
||||
/// Transitions:
|
||||
/// push(event):
|
||||
/// queues[event.channel_id].push_back(event)
|
||||
///
|
||||
/// flush_next() → Option<FlushBatch>:
|
||||
/// if prompt_in_flight: return None
|
||||
/// if all queues empty: return None
|
||||
/// channel = pick channel with oldest head event (min received_at)
|
||||
/// events = drain queues[channel]
|
||||
/// remove queues[channel] if now empty
|
||||
/// prompt_in_flight = true
|
||||
/// return Some(FlushBatch { channel, events })
|
||||
///
|
||||
/// mark_complete():
|
||||
/// prompt_in_flight = false
|
||||
/// ```
|
||||
pub struct EventQueue {
|
||||
queues: HashMap<Uuid, VecDeque<QueuedEvent>>,
|
||||
prompt_in_flight: bool,
|
||||
}
|
||||
|
||||
impl EventQueue {
|
||||
/// Create a new empty event queue.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
queues: HashMap::new(),
|
||||
prompt_in_flight: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Push an event into the queue for its channel.
|
||||
pub fn push(&mut self, event: QueuedEvent) {
|
||||
self.queues
|
||||
.entry(event.channel_id)
|
||||
.or_default()
|
||||
.push_back(event);
|
||||
}
|
||||
|
||||
/// Try to flush the next batch.
|
||||
///
|
||||
/// Returns `None` if a prompt is already in flight or if all queues are
|
||||
/// empty. Otherwise picks the channel with the oldest pending event (FIFO
|
||||
/// fairness across channels), drains ALL events for that channel into a
|
||||
/// single batch, sets `prompt_in_flight = true`, and returns the batch.
|
||||
pub fn flush_next(&mut self) -> Option<FlushBatch> {
|
||||
if self.prompt_in_flight {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Find the channel whose head event has the oldest received_at.
|
||||
let channel_id = self
|
||||
.queues
|
||||
.iter()
|
||||
.filter(|(_, q)| !q.is_empty())
|
||||
.min_by_key(|(_, q)| q.front().unwrap().received_at)
|
||||
.map(|(id, _)| *id)?;
|
||||
|
||||
// Drain ALL events for that channel.
|
||||
let queue = self.queues.remove(&channel_id)?;
|
||||
let events: Vec<Event> = queue.into_iter().map(|qe| qe.event).collect();
|
||||
|
||||
self.prompt_in_flight = true;
|
||||
|
||||
Some(FlushBatch { channel_id, events })
|
||||
}
|
||||
|
||||
/// Mark the current prompt as complete. Clears `prompt_in_flight`.
|
||||
pub fn mark_complete(&mut self) {
|
||||
self.prompt_in_flight = false;
|
||||
}
|
||||
|
||||
/// Re-queue a batch of events that failed to process.
|
||||
///
|
||||
/// Events are pushed back to the **front** of the channel's queue so they
|
||||
/// are processed first on the next flush cycle. This prevents event loss
|
||||
/// when session creation or `session/prompt` fails transiently.
|
||||
///
|
||||
/// Note: `received_at` is reset to `Instant::now()` for re-queued events.
|
||||
/// This means a re-queued channel competes fairly with other channels rather
|
||||
/// than always winning due to stale timestamps.
|
||||
pub fn requeue(&mut self, batch: FlushBatch) {
|
||||
let queue = self.queues.entry(batch.channel_id).or_default();
|
||||
// Push to front in reverse order so original order is preserved.
|
||||
for event in batch.events.into_iter().rev() {
|
||||
queue.push_front(QueuedEvent {
|
||||
channel_id: batch.channel_id,
|
||||
event,
|
||||
received_at: Instant::now(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a prompt is currently in flight.
|
||||
#[allow(dead_code)]
|
||||
pub fn is_in_flight(&self) -> bool {
|
||||
self.prompt_in_flight
|
||||
}
|
||||
|
||||
/// Total number of pending events across all channels.
|
||||
#[allow(dead_code)]
|
||||
pub fn pending_count(&self) -> usize {
|
||||
self.queues.values().map(|q| q.len()).sum()
|
||||
}
|
||||
|
||||
/// Number of channels with pending events.
|
||||
#[allow(dead_code)]
|
||||
pub fn pending_channels(&self) -> usize {
|
||||
self.queues.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for EventQueue {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// Format the Channel/From/Time/Message lines for a single event.
|
||||
fn format_event_lines(channel_id: Uuid, event: &Event) -> String {
|
||||
format!(
|
||||
"Channel: {}\nFrom: {}\nTime: {}\nMessage: {}",
|
||||
channel_id,
|
||||
event
|
||||
.pubkey
|
||||
.to_bech32()
|
||||
.unwrap_or_else(|_| event.pubkey.to_hex()),
|
||||
chrono::DateTime::from_timestamp(event.created_at.as_u64() as i64, 0)
|
||||
.map(|dt| dt.to_rfc3339())
|
||||
.unwrap_or_else(|| event.created_at.as_u64().to_string()),
|
||||
event.content,
|
||||
)
|
||||
}
|
||||
|
||||
/// Format a batch of events into a prompt string for the agent.
|
||||
pub fn format_prompt(batch: &FlushBatch) -> String {
|
||||
if batch.events.len() == 1 {
|
||||
format!(
|
||||
"[Sprout @mention]\n{}",
|
||||
format_event_lines(batch.channel_id, &batch.events[0])
|
||||
)
|
||||
} else {
|
||||
let mut prompt = format!("[Sprout @mention — {} events]\n", batch.events.len());
|
||||
for (i, event) in batch.events.iter().enumerate() {
|
||||
prompt.push_str(&format!(
|
||||
"\n--- Event {} ---\n{}\n",
|
||||
i + 1,
|
||||
format_event_lines(batch.channel_id, event)
|
||||
));
|
||||
}
|
||||
prompt
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Unit Tests ──────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use nostr::{EventBuilder, Keys, Kind};
|
||||
use std::time::Duration;
|
||||
|
||||
/// Build a test event with the given content.
|
||||
fn make_event(content: &str) -> Event {
|
||||
let keys = Keys::generate();
|
||||
EventBuilder::new(Kind::Custom(40001), content, [])
|
||||
.sign_with_keys(&keys)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Build a QueuedEvent for the given channel.
|
||||
fn make_queued(channel_id: Uuid, content: &str) -> QueuedEvent {
|
||||
QueuedEvent {
|
||||
channel_id,
|
||||
event: make_event(content),
|
||||
received_at: Instant::now(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Build a QueuedEvent with a specific `received_at` offset from now.
|
||||
fn make_queued_at(channel_id: Uuid, content: &str, age: Duration) -> QueuedEvent {
|
||||
QueuedEvent {
|
||||
channel_id,
|
||||
event: make_event(content),
|
||||
received_at: Instant::now() - age,
|
||||
}
|
||||
}
|
||||
|
||||
// ── Test 1: push + flush_next basic ──────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_push_flush_basic() {
|
||||
let mut q = EventQueue::new();
|
||||
let ch = Uuid::new_v4();
|
||||
|
||||
q.push(make_queued(ch, "hello"));
|
||||
|
||||
let batch = q.flush_next().expect("should return a batch");
|
||||
assert_eq!(batch.channel_id, ch);
|
||||
assert_eq!(batch.events.len(), 1);
|
||||
assert_eq!(batch.events[0].content, "hello");
|
||||
|
||||
// Queue should be empty now.
|
||||
assert_eq!(q.pending_count(), 0);
|
||||
assert_eq!(q.pending_channels(), 0);
|
||||
}
|
||||
|
||||
// ── Test 2: in_flight blocks flush ───────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_in_flight_blocks_flush() {
|
||||
let mut q = EventQueue::new();
|
||||
let ch = Uuid::new_v4();
|
||||
|
||||
q.push(make_queued(ch, "first"));
|
||||
let _batch = q.flush_next().expect("first flush should succeed");
|
||||
assert!(q.is_in_flight());
|
||||
|
||||
// Push another event while in-flight.
|
||||
q.push(make_queued(ch, "second"));
|
||||
|
||||
// flush_next must return None while in-flight.
|
||||
assert!(q.flush_next().is_none());
|
||||
}
|
||||
|
||||
// ── Test 3: mark_complete enables flush ──────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_mark_complete_enables_flush() {
|
||||
let mut q = EventQueue::new();
|
||||
let ch = Uuid::new_v4();
|
||||
|
||||
q.push(make_queued(ch, "first"));
|
||||
let _batch = q.flush_next().expect("first flush should succeed");
|
||||
|
||||
// Push while in-flight; flush blocked.
|
||||
q.push(make_queued(ch, "second"));
|
||||
assert!(q.flush_next().is_none());
|
||||
|
||||
// Complete the in-flight prompt.
|
||||
q.mark_complete();
|
||||
assert!(!q.is_in_flight());
|
||||
|
||||
// Now flush should succeed.
|
||||
let batch = q.flush_next().expect("should flush after mark_complete");
|
||||
assert_eq!(batch.channel_id, ch);
|
||||
assert_eq!(batch.events.len(), 1);
|
||||
assert_eq!(batch.events[0].content, "second");
|
||||
}
|
||||
|
||||
// ── Test 4: batch drain ───────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_batch_drain_all_events() {
|
||||
let mut q = EventQueue::new();
|
||||
let ch = Uuid::new_v4();
|
||||
|
||||
q.push(make_queued(ch, "msg1"));
|
||||
q.push(make_queued(ch, "msg2"));
|
||||
q.push(make_queued(ch, "msg3"));
|
||||
|
||||
assert_eq!(q.pending_count(), 3);
|
||||
|
||||
let batch = q.flush_next().expect("should return batch");
|
||||
assert_eq!(batch.channel_id, ch);
|
||||
assert_eq!(batch.events.len(), 3);
|
||||
assert_eq!(batch.events[0].content, "msg1");
|
||||
assert_eq!(batch.events[1].content, "msg2");
|
||||
assert_eq!(batch.events[2].content, "msg3");
|
||||
|
||||
// All drained.
|
||||
assert_eq!(q.pending_count(), 0);
|
||||
assert_eq!(q.pending_channels(), 0);
|
||||
}
|
||||
|
||||
// ── Test 5: FIFO fairness ─────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_fifo_fairness_picks_oldest_channel() {
|
||||
let mut q = EventQueue::new();
|
||||
let ch_a = Uuid::new_v4();
|
||||
let ch_b = Uuid::new_v4();
|
||||
|
||||
// Channel A has an older event (2 seconds ago), B has a newer one (1 second ago).
|
||||
q.push(make_queued_at(ch_a, "from A", Duration::from_secs(2)));
|
||||
q.push(make_queued_at(ch_b, "from B", Duration::from_secs(1)));
|
||||
|
||||
let batch = q.flush_next().expect("should return batch");
|
||||
// A is older, so it should be picked first.
|
||||
assert_eq!(batch.channel_id, ch_a);
|
||||
assert_eq!(batch.events[0].content, "from A");
|
||||
}
|
||||
|
||||
// ── Test 6: multi-channel interleave ─────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_multi_channel_interleave() {
|
||||
let mut q = EventQueue::new();
|
||||
let ch_a = Uuid::new_v4();
|
||||
let ch_b = Uuid::new_v4();
|
||||
|
||||
// A is older.
|
||||
q.push(make_queued_at(ch_a, "A-event", Duration::from_secs(2)));
|
||||
q.push(make_queued_at(ch_b, "B-event", Duration::from_secs(1)));
|
||||
|
||||
// First flush picks A.
|
||||
let batch_a = q.flush_next().expect("first flush");
|
||||
assert_eq!(batch_a.channel_id, ch_a);
|
||||
assert!(q.is_in_flight());
|
||||
|
||||
// B still pending.
|
||||
assert_eq!(q.pending_count(), 1);
|
||||
assert_eq!(q.pending_channels(), 1);
|
||||
|
||||
q.mark_complete();
|
||||
|
||||
// Second flush picks B.
|
||||
let batch_b = q.flush_next().expect("second flush");
|
||||
assert_eq!(batch_b.channel_id, ch_b);
|
||||
assert_eq!(batch_b.events[0].content, "B-event");
|
||||
|
||||
assert_eq!(q.pending_count(), 0);
|
||||
}
|
||||
|
||||
// ── Test 7: empty queue returns None ─────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_empty_queue_returns_none() {
|
||||
let mut q = EventQueue::new();
|
||||
assert!(q.flush_next().is_none());
|
||||
}
|
||||
|
||||
// ── Test 8: pending_count ─────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_pending_count() {
|
||||
let mut q = EventQueue::new();
|
||||
let ch_a = Uuid::new_v4();
|
||||
let ch_b = Uuid::new_v4();
|
||||
|
||||
assert_eq!(q.pending_count(), 0);
|
||||
assert_eq!(q.pending_channels(), 0);
|
||||
|
||||
q.push(make_queued(ch_a, "a1"));
|
||||
q.push(make_queued(ch_a, "a2"));
|
||||
q.push(make_queued(ch_b, "b1"));
|
||||
|
||||
assert_eq!(q.pending_count(), 3);
|
||||
assert_eq!(q.pending_channels(), 2);
|
||||
|
||||
// Flush A (2 events drained).
|
||||
let _ = q.flush_next();
|
||||
assert_eq!(q.pending_count(), 1);
|
||||
assert_eq!(q.pending_channels(), 1);
|
||||
|
||||
q.mark_complete();
|
||||
|
||||
// Flush B (1 event drained).
|
||||
let _ = q.flush_next();
|
||||
assert_eq!(q.pending_count(), 0);
|
||||
assert_eq!(q.pending_channels(), 0);
|
||||
}
|
||||
|
||||
// ── Test 9: format_prompt single event ───────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_format_prompt_single() {
|
||||
let ch = Uuid::new_v4();
|
||||
let event = make_event("Hello @agent");
|
||||
let npub = event
|
||||
.pubkey
|
||||
.to_bech32()
|
||||
.unwrap_or_else(|_| event.pubkey.to_hex());
|
||||
|
||||
let batch = FlushBatch {
|
||||
channel_id: ch,
|
||||
events: vec![event],
|
||||
};
|
||||
|
||||
let prompt = format_prompt(&batch);
|
||||
|
||||
assert!(prompt.starts_with("[Sprout @mention]\n"));
|
||||
assert!(prompt.contains(&format!("Channel: {}", ch)));
|
||||
assert!(prompt.contains(&format!("From: {}", npub)));
|
||||
assert!(prompt.contains("Message: Hello @agent"));
|
||||
// Should NOT contain "--- Event 1 ---" (that's the multi-event format).
|
||||
assert!(!prompt.contains("--- Event 1 ---"));
|
||||
}
|
||||
|
||||
// ── Test 9b: requeue preserves events ────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_requeue_preserves_events() {
|
||||
let mut queue = EventQueue::new();
|
||||
let ch = Uuid::new_v4();
|
||||
queue.push(make_queued(ch, "msg1"));
|
||||
queue.push(make_queued(ch, "msg2"));
|
||||
|
||||
let batch = queue.flush_next().unwrap();
|
||||
assert_eq!(batch.events.len(), 2);
|
||||
assert!(queue.is_in_flight());
|
||||
|
||||
// Simulate failure — requeue the batch.
|
||||
queue.requeue(batch);
|
||||
queue.mark_complete();
|
||||
|
||||
// Should be able to flush again and get the same events in order.
|
||||
let batch2 = queue.flush_next().unwrap();
|
||||
assert_eq!(batch2.events.len(), 2);
|
||||
assert_eq!(batch2.events[0].content, "msg1");
|
||||
assert_eq!(batch2.events[1].content, "msg2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_requeue_interleaves_with_other_channels() {
|
||||
let mut queue = EventQueue::new();
|
||||
let ch_a = Uuid::new_v4();
|
||||
let ch_b = Uuid::new_v4();
|
||||
|
||||
// ch_a has an older event.
|
||||
queue.push(make_queued_at(ch_a, "A-old", Duration::from_secs(5)));
|
||||
queue.push(make_queued_at(ch_b, "B-new", Duration::from_secs(1)));
|
||||
|
||||
// Flush ch_a first (older).
|
||||
let batch_a = queue.flush_next().unwrap();
|
||||
assert_eq!(batch_a.channel_id, ch_a);
|
||||
|
||||
// Requeue ch_a (simulating failure) and complete.
|
||||
queue.requeue(batch_a);
|
||||
queue.mark_complete();
|
||||
|
||||
// After requeue, ch_a's received_at is reset to now, so ch_b (older) goes first.
|
||||
let next_batch = queue.flush_next().unwrap();
|
||||
assert_eq!(next_batch.channel_id, ch_b);
|
||||
}
|
||||
|
||||
// ── Test 10: format_prompt batch ─────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_format_prompt_batch() {
|
||||
let ch = Uuid::new_v4();
|
||||
let e1 = make_event("first message");
|
||||
let e2 = make_event("second message");
|
||||
let e3 = make_event("third message");
|
||||
|
||||
let batch = FlushBatch {
|
||||
channel_id: ch,
|
||||
events: vec![e1, e2, e3],
|
||||
};
|
||||
|
||||
let prompt = format_prompt(&batch);
|
||||
|
||||
assert!(prompt.starts_with("[Sprout @mention — 3 events]\n"));
|
||||
assert!(prompt.contains("--- Event 1 ---"));
|
||||
assert!(prompt.contains("--- Event 2 ---"));
|
||||
assert!(prompt.contains("--- Event 3 ---"));
|
||||
assert!(prompt.contains("Message: first message"));
|
||||
assert!(prompt.contains("Message: second message"));
|
||||
assert!(prompt.contains("Message: third message"));
|
||||
// All events reference the same channel.
|
||||
assert_eq!(
|
||||
prompt.matches(&format!("Channel: {}", ch)).count(),
|
||||
3,
|
||||
"each event block should include the channel id"
|
||||
);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -51,7 +51,7 @@
|
||||
//!
|
||||
//! ### Channels
|
||||
//! - **`list_channels`** — List channels accessible to this agent, optionally filtered by
|
||||
//! visibility (`public` / `private`).
|
||||
//! visibility (`open` / `private`).
|
||||
//! - **`create_channel`** — Create a new channel with a given name, type, and visibility.
|
||||
//!
|
||||
//! ### Canvas
|
||||
|
||||
@@ -543,6 +543,19 @@ impl RelayClient {
|
||||
Ok(resp.text().await?)
|
||||
}
|
||||
|
||||
/// Get the canvas content for a channel via REST.
|
||||
pub async fn get_canvas(&self, channel_id: &str) -> anyhow::Result<String> {
|
||||
self.get(&format!("/api/channels/{}/canvas", channel_id))
|
||||
.await
|
||||
}
|
||||
|
||||
/// Set the canvas content for a channel via REST.
|
||||
pub async fn set_canvas(&self, channel_id: &str, content: &str) -> anyhow::Result<String> {
|
||||
let body = serde_json::json!({ "content": content });
|
||||
self.put(&format!("/api/channels/{}/canvas", channel_id), &body)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Authenticated GET to a full URL (for feed tools that build the URL themselves).
|
||||
pub async fn get_api(&self, url: &str) -> anyhow::Result<String> {
|
||||
let resp = self.apply_auth(self.http.get(url)).send().await?;
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
use sprout_core::kind::KIND_CANVAS;
|
||||
|
||||
use rmcp::{
|
||||
handler::server::{router::tool::ToolRouter, wrapper::Parameters},
|
||||
model::{ServerCapabilities, ServerInfo},
|
||||
@@ -79,7 +77,7 @@ pub struct GetChannelHistoryParams {
|
||||
/// Parameters for the `list_channels` tool.
|
||||
#[derive(Debug, Serialize, Deserialize, schemars::JsonSchema)]
|
||||
pub struct ListChannelsParams {
|
||||
/// Optional visibility filter: `"public"` or `"private"`.
|
||||
/// Optional visibility filter: `"open"` or `"private"`.
|
||||
#[serde(default)]
|
||||
pub visibility: Option<String>,
|
||||
}
|
||||
@@ -89,9 +87,9 @@ pub struct ListChannelsParams {
|
||||
pub struct CreateChannelParams {
|
||||
/// Display name for the new channel.
|
||||
pub name: String,
|
||||
/// Channel type identifier (e.g. `"text"`, `"voice"`).
|
||||
/// Channel type: `"stream"` (real-time chat) or `"forum"` (threaded discussions).
|
||||
pub channel_type: String,
|
||||
/// Visibility of the channel: `"public"` or `"private"`.
|
||||
/// Channel visibility: `"open"` (anyone can join) or `"private"` (invite-only).
|
||||
pub visibility: String,
|
||||
/// Optional human-readable description of the channel's purpose.
|
||||
#[serde(default)]
|
||||
@@ -128,7 +126,19 @@ pub struct ListWorkflowsParams {
|
||||
pub struct CreateWorkflowParams {
|
||||
/// UUID of the channel to own this workflow.
|
||||
pub channel_id: String,
|
||||
/// Full workflow definition in YAML format.
|
||||
/// Full workflow definition in YAML format. Required fields: name (string), trigger (object with
|
||||
/// 'on' field: 'message_posted', 'reaction_added', or 'webhook'), steps (array).
|
||||
/// Each step needs: id (alphanumeric/underscore), action (e.g. 'send_message'), and action-specific
|
||||
/// fields as direct properties (NOT nested under 'params'). Example:
|
||||
/// ```yaml
|
||||
/// name: My Workflow
|
||||
/// trigger:
|
||||
/// on: message_posted
|
||||
/// steps:
|
||||
/// - id: notify
|
||||
/// action: send_message
|
||||
/// text: Hello from workflow!
|
||||
/// ```
|
||||
pub yaml_definition: String,
|
||||
}
|
||||
|
||||
@@ -522,7 +532,10 @@ impl SproutMcpServer {
|
||||
}
|
||||
|
||||
/// Create a new Sprout channel.
|
||||
#[tool(name = "create_channel", description = "Create a new Sprout channel")]
|
||||
#[tool(
|
||||
name = "create_channel",
|
||||
description = "Create a new Sprout channel. channel_type must be 'stream' or 'forum'. visibility must be 'open' or 'private'."
|
||||
)]
|
||||
pub async fn create_channel(&self, Parameters(p): Parameters<CreateChannelParams>) -> String {
|
||||
let body = serde_json::json!({
|
||||
"name": p.name,
|
||||
@@ -545,28 +558,19 @@ impl SproutMcpServer {
|
||||
if let Err(e) = validate_uuid(&p.channel_id) {
|
||||
return format!("Error: {e}");
|
||||
}
|
||||
|
||||
let filter = nostr::Filter::new()
|
||||
.kind(nostr::Kind::Custom(KIND_CANVAS as u16))
|
||||
.custom_tag(
|
||||
nostr::SingleLetterTag::lowercase(nostr::Alphabet::H),
|
||||
[p.channel_id.as_str()],
|
||||
)
|
||||
.limit(50);
|
||||
|
||||
let sub_id = format!("canvas-{}", uuid::Uuid::new_v4());
|
||||
let events = match self.client.subscribe(&sub_id, vec![filter]).await {
|
||||
Ok(e) => e,
|
||||
Err(e) => return format!("Error: {e}"),
|
||||
};
|
||||
let _ = self.client.close_subscription(&sub_id).await;
|
||||
|
||||
let canvas_event = events.iter().max_by_key(|event| event.created_at.as_u64());
|
||||
|
||||
if let Some(event) = canvas_event {
|
||||
event.content.clone()
|
||||
} else {
|
||||
"No canvas set for this channel.".to_string()
|
||||
match self.client.get_canvas(&p.channel_id).await {
|
||||
Ok(body) => {
|
||||
// Parse REST JSON and return just the content string.
|
||||
if let Ok(v) = serde_json::from_str::<serde_json::Value>(&body) {
|
||||
match v.get("content").and_then(|c| c.as_str()) {
|
||||
Some(content) => content.to_string(),
|
||||
None => "No canvas set for this channel.".to_string(),
|
||||
}
|
||||
} else {
|
||||
body
|
||||
}
|
||||
}
|
||||
Err(e) => format!("Error: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -579,29 +583,20 @@ impl SproutMcpServer {
|
||||
if let Err(e) = validate_uuid(&p.channel_id) {
|
||||
return format!("Error: {e}");
|
||||
}
|
||||
|
||||
let keys = self.client.keys().clone();
|
||||
|
||||
let channel_tag = match nostr::Tag::parse(&["h", &p.channel_id]) {
|
||||
Ok(t) => t,
|
||||
Err(e) => return format!("Error building tag: {e}"),
|
||||
};
|
||||
|
||||
let event = match nostr::EventBuilder::new(
|
||||
nostr::Kind::Custom(KIND_CANVAS as u16),
|
||||
&p.content,
|
||||
[channel_tag],
|
||||
)
|
||||
.sign_with_keys(&keys)
|
||||
{
|
||||
Ok(e) => e,
|
||||
Err(e) => return format!("Error signing event: {e}"),
|
||||
};
|
||||
|
||||
match self.client.send_event(event).await {
|
||||
Ok(ok) if ok.accepted => "Canvas updated.".to_string(),
|
||||
Ok(ok) => format!("Canvas update rejected: {}", ok.message),
|
||||
Err(e) => format!("Relay error: {e}"),
|
||||
match self.client.set_canvas(&p.channel_id, &p.content).await {
|
||||
Ok(body) => {
|
||||
// Parse REST JSON; return a clean confirmation string.
|
||||
if let Ok(v) = serde_json::from_str::<serde_json::Value>(&body) {
|
||||
if v.get("ok").and_then(|o| o.as_bool()) == Some(true) {
|
||||
return "Canvas updated.".to_string();
|
||||
}
|
||||
if let Some(err) = v.get("error").and_then(|e| e.as_str()) {
|
||||
return format!("Error: {err}");
|
||||
}
|
||||
}
|
||||
body
|
||||
}
|
||||
Err(e) => format!("Error: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -629,7 +624,7 @@ impl SproutMcpServer {
|
||||
/// Create a new workflow in a channel from a YAML definition.
|
||||
#[tool(
|
||||
name = "create_workflow",
|
||||
description = "Create a new workflow in a channel from a YAML definition"
|
||||
description = "Create a new workflow from a YAML definition. Steps need 'id' (not 'name'), and action fields are direct properties (not nested under 'params'). Triggers: message_posted, reaction_added, webhook."
|
||||
)]
|
||||
pub async fn create_workflow(&self, Parameters(p): Parameters<CreateWorkflowParams>) -> String {
|
||||
if uuid::Uuid::parse_str(&p.channel_id).is_err() {
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
//! Canvas REST API.
|
||||
//!
|
||||
//! Endpoints:
|
||||
//! GET /api/channels/:channel_id/canvas — fetch the most recent canvas for a channel
|
||||
//! PUT /api/channels/:channel_id/canvas — set/update the canvas for a channel
|
||||
//!
|
||||
//! Canvas events are Nostr events of kind KIND_CANVAS (40100) with an "h" tag
|
||||
//! scoped to the channel UUID. The relay signs these events on behalf of the
|
||||
//! authenticated user (same pattern as `messages.rs`).
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::{HeaderMap, StatusCode},
|
||||
response::Json,
|
||||
};
|
||||
use nostr::util::hex as nostr_hex;
|
||||
use nostr::{EventBuilder, Kind, Tag};
|
||||
use serde::Deserialize;
|
||||
use sprout_core::kind::KIND_CANVAS;
|
||||
use sprout_db::event::EventQuery;
|
||||
|
||||
use crate::handlers::event::dispatch_persistent_event;
|
||||
use crate::state::AppState;
|
||||
|
||||
use super::{api_error, check_channel_access, extract_auth_pubkey, internal_error};
|
||||
|
||||
// ── GET /api/channels/:channel_id/canvas ─────────────────────────────────────
|
||||
|
||||
/// Fetch the most recent canvas for a channel.
|
||||
///
|
||||
/// Returns the canvas content, author pubkey (hex), and updated_at timestamp
|
||||
/// (Unix seconds) if a canvas exists, or `{"content": null}` if none has been set.
|
||||
pub async fn get_canvas(
|
||||
State(state): State<Arc<AppState>>,
|
||||
headers: HeaderMap,
|
||||
Path(channel_id_str): Path<String>,
|
||||
) -> Result<Json<serde_json::Value>, (StatusCode, Json<serde_json::Value>)> {
|
||||
let (_pubkey, pubkey_bytes) = extract_auth_pubkey(&headers, &state).await?;
|
||||
|
||||
let channel_id = uuid::Uuid::parse_str(&channel_id_str)
|
||||
.map_err(|_| api_error(StatusCode::BAD_REQUEST, "invalid channel UUID"))?;
|
||||
|
||||
check_channel_access(&state, channel_id, &pubkey_bytes).await?;
|
||||
|
||||
// Query the events table for the most recent KIND_CANVAS event scoped to
|
||||
// this channel. `channel_id` is stored as a column on the events row, so
|
||||
// we can filter directly without scanning tags.
|
||||
let q = EventQuery {
|
||||
channel_id: Some(channel_id),
|
||||
kinds: Some(vec![KIND_CANVAS as i32]),
|
||||
pubkey: None,
|
||||
since: None,
|
||||
until: None,
|
||||
limit: Some(1),
|
||||
offset: None,
|
||||
};
|
||||
|
||||
let events = state
|
||||
.db
|
||||
.query_events(&q)
|
||||
.await
|
||||
.map_err(|e| internal_error(&format!("db error: {e}")))?;
|
||||
|
||||
match events.into_iter().next() {
|
||||
Some(stored) => {
|
||||
// Canvas events are relay-signed; the real author is in the first `p` tag.
|
||||
let author_hex = stored
|
||||
.event
|
||||
.tags
|
||||
.find(nostr::TagKind::SingleLetter(
|
||||
nostr::SingleLetterTag::lowercase(nostr::Alphabet::P),
|
||||
))
|
||||
.and_then(|t| t.content().map(|s| s.to_string()))
|
||||
.unwrap_or_else(|| nostr_hex::encode(stored.event.pubkey.serialize()));
|
||||
let updated_at = stored.event.created_at.as_u64() as i64;
|
||||
Ok(Json(serde_json::json!({
|
||||
"content": stored.event.content,
|
||||
"updated_at": updated_at,
|
||||
"author": author_hex,
|
||||
})))
|
||||
}
|
||||
None => Ok(Json(serde_json::json!({ "content": null }))),
|
||||
}
|
||||
}
|
||||
|
||||
// ── PUT /api/channels/:channel_id/canvas ─────────────────────────────────────
|
||||
|
||||
/// Request body for setting the canvas.
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct SetCanvasBody {
|
||||
/// New canvas content (Markdown or plain text).
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
/// Set or update the canvas for a channel.
|
||||
///
|
||||
/// Creates a Nostr event of kind KIND_CANVAS signed by the relay keypair,
|
||||
/// attributed to the authenticated user via a `p` tag. Fans out to WebSocket
|
||||
/// subscribers so live clients receive the update immediately.
|
||||
pub async fn set_canvas(
|
||||
State(state): State<Arc<AppState>>,
|
||||
headers: HeaderMap,
|
||||
Path(channel_id_str): Path<String>,
|
||||
Json(body): Json<SetCanvasBody>,
|
||||
) -> Result<Json<serde_json::Value>, (StatusCode, Json<serde_json::Value>)> {
|
||||
let (_pubkey, pubkey_bytes) = extract_auth_pubkey(&headers, &state).await?;
|
||||
|
||||
let channel_id = uuid::Uuid::parse_str(&channel_id_str)
|
||||
.map_err(|_| api_error(StatusCode::BAD_REQUEST, "invalid channel UUID"))?;
|
||||
|
||||
check_channel_access(&state, channel_id, &pubkey_bytes).await?;
|
||||
|
||||
// Reject writes to archived channels (consistent with messages, metadata, etc.).
|
||||
let channel = state
|
||||
.db
|
||||
.get_channel(channel_id)
|
||||
.await
|
||||
.map_err(|e| internal_error(&format!("db error: {e}")))?;
|
||||
if channel.archived_at.is_some() {
|
||||
return Err(api_error(StatusCode::FORBIDDEN, "channel is archived"));
|
||||
}
|
||||
|
||||
// Attribution: real author carried in a `p` tag; event is relay-signed.
|
||||
let user_pubkey_hex = nostr_hex::encode(&pubkey_bytes);
|
||||
|
||||
let tags: Vec<Tag> = vec![
|
||||
// Real sender attribution (mirrors messages.rs pattern).
|
||||
Tag::parse(&["p", &user_pubkey_hex])
|
||||
.map_err(|e| internal_error(&format!("tag build error: {e}")))?,
|
||||
// NIP-29 channel scope tag.
|
||||
Tag::parse(&["h", &channel_id.to_string()])
|
||||
.map_err(|e| internal_error(&format!("tag build error: {e}")))?,
|
||||
];
|
||||
|
||||
let kind = Kind::from(KIND_CANVAS as u16);
|
||||
|
||||
let event = EventBuilder::new(kind, &body.content, tags)
|
||||
.sign_with_keys(&state.relay_keypair)
|
||||
.map_err(|e| internal_error(&format!("event signing error: {e}")))?;
|
||||
|
||||
let event_id_hex = event.id.to_hex();
|
||||
|
||||
let (stored_event, was_inserted) = state
|
||||
.db
|
||||
.insert_event(&event, Some(channel_id))
|
||||
.await
|
||||
.map_err(|e| internal_error(&format!("db error: {e}")))?;
|
||||
|
||||
if was_inserted {
|
||||
let _ =
|
||||
dispatch_persistent_event(&state, &stored_event, KIND_CANVAS, &user_pubkey_hex).await;
|
||||
}
|
||||
|
||||
Ok(Json(serde_json::json!({
|
||||
"ok": true,
|
||||
"event_id": event_id_hex,
|
||||
})))
|
||||
}
|
||||
@@ -14,6 +14,8 @@
|
||||
pub mod agents;
|
||||
/// Workflow approval grant/deny endpoints.
|
||||
pub mod approvals;
|
||||
/// Canvas (shared document) endpoints.
|
||||
pub mod canvas;
|
||||
/// Channel CRUD and membership endpoints.
|
||||
pub mod channels;
|
||||
/// Channel metadata endpoints (get, update, topic, purpose, archive).
|
||||
@@ -44,6 +46,7 @@ pub mod workflows;
|
||||
// Re-export all public handlers so router.rs can use `api::*_handler` unchanged.
|
||||
pub use agents::agents_handler;
|
||||
pub use approvals::{deny_approval, grant_approval};
|
||||
pub use canvas::{get_canvas, set_canvas};
|
||||
pub use channels::{channels_handler, create_channel};
|
||||
pub use channels_metadata::{
|
||||
archive_channel_handler, delete_channel_handler, get_channel_handler, set_purpose_handler,
|
||||
|
||||
@@ -83,6 +83,11 @@ pub fn build_router(state: Arc<AppState>) -> Router {
|
||||
"/api/channels/{channel_id}/unarchive",
|
||||
post(api::unarchive_channel_handler),
|
||||
)
|
||||
// Canvas routes
|
||||
.route(
|
||||
"/api/channels/{channel_id}/canvas",
|
||||
get(api::get_canvas).put(api::set_canvas),
|
||||
)
|
||||
// Message + thread routes
|
||||
.route(
|
||||
"/api/channels/{channel_id}/messages",
|
||||
|
||||
@@ -8,6 +8,7 @@ repository.workspace = true
|
||||
description = "Integration test client and E2E test suite for Sprout"
|
||||
|
||||
[dependencies]
|
||||
anyhow = { workspace = true }
|
||||
sprout-core = { workspace = true }
|
||||
sprout-mcp = { workspace = true }
|
||||
nostr = { workspace = true }
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
//! Send an @mention event to a Sprout channel targeting a specific pubkey.
|
||||
//! Usage: mention <channel_uuid> <target_pubkey_hex> <message>
|
||||
|
||||
use nostr::{EventBuilder, Keys, Kind, Tag};
|
||||
use sprout_test_client::SproutTestClient;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() < 4 {
|
||||
eprintln!("Usage: mention <channel_uuid> <target_pubkey_hex> <message>");
|
||||
std::process::exit(1);
|
||||
}
|
||||
let channel_id = &args[1];
|
||||
let target_pubkey = &args[2];
|
||||
let message = args[3..].join(" ");
|
||||
|
||||
let url = std::env::var("SPROUT_RELAY_URL").unwrap_or_else(|_| "ws://localhost:3000".into());
|
||||
let keys = Keys::generate();
|
||||
println!("Sender pubkey: {}", keys.public_key().to_hex());
|
||||
|
||||
let mut client = SproutTestClient::connect(&url, &keys).await?;
|
||||
|
||||
let h_tag = Tag::parse(&["h", channel_id])?;
|
||||
let p_tag = Tag::parse(&["p", target_pubkey])?;
|
||||
let event =
|
||||
EventBuilder::new(Kind::Custom(40001), message, [h_tag, p_tag]).sign_with_keys(&keys)?;
|
||||
|
||||
let ok = client.send_event(event).await?;
|
||||
if ok.accepted {
|
||||
println!("✅ @mention sent: {}", ok.event_id);
|
||||
} else {
|
||||
eprintln!("❌ Rejected: {}", ok.message);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -15,25 +15,18 @@
|
||||
//!
|
||||
//! # Auth
|
||||
//!
|
||||
//! The MCP server generates an ephemeral keypair on startup (no `SPROUT_PRIVATE_KEY`
|
||||
//! needed). In dev mode (`require_auth_token=false`) the relay accepts any
|
||||
//! authenticated NIP-42 client.
|
||||
//!
|
||||
//! # Channel setup
|
||||
//!
|
||||
//! Tests use the pre-seeded open channels that are stable across relay restarts.
|
||||
//! Each test generates a known keypair, creates a fresh channel via the REST API
|
||||
//! (so the keypair is the channel owner and member), then passes the private key
|
||||
//! as `SPROUT_PRIVATE_KEY` to the MCP server subprocess. This ensures the MCP
|
||||
//! server uses a stable identity that has access to the channels under test.
|
||||
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio};
|
||||
use std::time::Duration;
|
||||
|
||||
use nostr::{Keys, ToBech32};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
// ── Seeded channel IDs (UUID5-derived, stable across relay restarts) ──────────
|
||||
|
||||
const CHANNEL_GENERAL: &str = "9a1657ac-f7aa-5db0-b632-d8bbeb6dfb50";
|
||||
const CHANNEL_ENGINEERING: &str = "1c7e1c02-87bb-5e88-b2da-5a7a9432d0c9";
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────────
|
||||
|
||||
/// WebSocket relay URL (e.g. `ws://localhost:3001`).
|
||||
@@ -41,11 +34,63 @@ fn relay_ws_url() -> String {
|
||||
std::env::var("RELAY_URL").unwrap_or_else(|_| "ws://localhost:3001".to_string())
|
||||
}
|
||||
|
||||
/// HTTP relay URL derived from the WebSocket URL.
|
||||
fn relay_http_url() -> String {
|
||||
relay_ws_url()
|
||||
.replace("wss://", "https://")
|
||||
.replace("ws://", "http://")
|
||||
.trim_end_matches('/')
|
||||
.to_string()
|
||||
}
|
||||
|
||||
/// Generate a fresh Nostr keypair for a test run.
|
||||
fn generate_test_keys() -> Keys {
|
||||
Keys::generate()
|
||||
}
|
||||
|
||||
/// Encode the secret key as an `nsec1…` bech32 string.
|
||||
fn nsec_from_keys(keys: &Keys) -> String {
|
||||
keys.secret_key().to_bech32().expect("bech32 encode nsec")
|
||||
}
|
||||
|
||||
/// Create a fresh channel via the REST API using the given keypair as the owner.
|
||||
///
|
||||
/// Returns the new channel's UUID string. The creating pubkey is automatically
|
||||
/// added as a member, so the MCP server (using the same keypair) will have
|
||||
/// access to it.
|
||||
async fn create_channel_for_test(keys: &Keys, name: &str) -> String {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("{}/api/channels", relay_http_url());
|
||||
let resp = client
|
||||
.post(&url)
|
||||
.header("X-Pubkey", keys.public_key().to_hex())
|
||||
.json(&serde_json::json!({
|
||||
"name": name,
|
||||
"channel_type": "stream",
|
||||
"visibility": "open",
|
||||
}))
|
||||
.send()
|
||||
.await
|
||||
.expect("create channel request failed");
|
||||
assert_eq!(
|
||||
resp.status(),
|
||||
201,
|
||||
"channel creation failed: {}",
|
||||
resp.text().await.unwrap_or_default()
|
||||
);
|
||||
let body: serde_json::Value = resp.json().await.expect("parse channel response");
|
||||
body["id"]
|
||||
.as_str()
|
||||
.expect("channel id in response")
|
||||
.to_string()
|
||||
}
|
||||
|
||||
/// Spawn the MCP server as a subprocess with stdin/stdout piped.
|
||||
///
|
||||
/// The server connects to the relay and performs NIP-42 auth on startup.
|
||||
/// We give it a few seconds to complete the handshake before sending requests.
|
||||
fn spawn_mcp_server() -> Child {
|
||||
/// The server connects to the relay and performs NIP-42 auth on startup using
|
||||
/// the provided keypair (passed via `SPROUT_PRIVATE_KEY`).
|
||||
fn spawn_mcp_server(keys: &Keys) -> Child {
|
||||
let nsec = nsec_from_keys(keys);
|
||||
Command::new("cargo")
|
||||
.args([
|
||||
"run",
|
||||
@@ -56,6 +101,7 @@ fn spawn_mcp_server() -> Child {
|
||||
"--",
|
||||
])
|
||||
.env("SPROUT_RELAY_URL", relay_ws_url())
|
||||
.env("SPROUT_PRIVATE_KEY", &nsec)
|
||||
// Suppress verbose startup logs so they don't pollute stderr output.
|
||||
.env("RUST_LOG", "error")
|
||||
.stdin(Stdio::piped())
|
||||
@@ -74,9 +120,9 @@ struct McpSession {
|
||||
}
|
||||
|
||||
impl McpSession {
|
||||
/// Spawn the MCP server and wait for it to connect to the relay.
|
||||
async fn start() -> Self {
|
||||
let mut child = spawn_mcp_server();
|
||||
/// Spawn the MCP server with the given keypair and wait for it to connect.
|
||||
async fn start(keys: &Keys) -> Self {
|
||||
let mut child = spawn_mcp_server(keys);
|
||||
let stdin = child.stdin.take().expect("stdin not piped");
|
||||
let stdout = child.stdout.take().expect("stdout not piped");
|
||||
let reader = BufReader::new(stdout);
|
||||
@@ -200,11 +246,12 @@ impl McpSession {
|
||||
// ── Tests ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Spawn the MCP server, complete the initialize handshake, and verify that
|
||||
/// all 16 expected tools are listed by `tools/list`.
|
||||
/// all 36 expected tools are listed by `tools/list`.
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn test_mcp_initialize_and_list_tools() {
|
||||
let mut session = McpSession::start().await;
|
||||
let keys = generate_test_keys();
|
||||
let mut session = McpSession::start(&keys).await;
|
||||
|
||||
// ── initialize ──────────────────────────────────────────────────────────
|
||||
let init_resp = session.initialize();
|
||||
@@ -248,8 +295,8 @@ async fn test_mcp_initialize_and_list_tools() {
|
||||
|
||||
assert_eq!(
|
||||
tools.len(),
|
||||
16,
|
||||
"expected exactly 16 tools, got {}. Tools: {:?}",
|
||||
36,
|
||||
"expected exactly 36 tools, got {}. Tools: {:?}",
|
||||
tools.len(),
|
||||
tools
|
||||
.iter()
|
||||
@@ -277,6 +324,26 @@ async fn test_mcp_initialize_and_list_tools() {
|
||||
"get_feed",
|
||||
"get_feed_mentions",
|
||||
"get_feed_actions",
|
||||
"add_channel_member",
|
||||
"remove_channel_member",
|
||||
"list_channel_members",
|
||||
"join_channel",
|
||||
"leave_channel",
|
||||
"get_channel",
|
||||
"update_channel",
|
||||
"set_channel_topic",
|
||||
"set_channel_purpose",
|
||||
"archive_channel",
|
||||
"unarchive_channel",
|
||||
"send_reply",
|
||||
"get_thread",
|
||||
"open_dm",
|
||||
"add_dm_member",
|
||||
"list_dms",
|
||||
"add_reaction",
|
||||
"remove_reaction",
|
||||
"get_reactions",
|
||||
"set_profile",
|
||||
];
|
||||
|
||||
for expected in &expected_tools {
|
||||
@@ -302,11 +369,19 @@ async fn test_mcp_initialize_and_list_tools() {
|
||||
session.stop();
|
||||
}
|
||||
|
||||
/// Call `list_channels` via MCP and verify the response contains the seeded channels.
|
||||
/// Call `list_channels` via MCP and verify the response contains the channel
|
||||
/// we created for this test run.
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn test_mcp_list_channels() {
|
||||
let mut session = McpSession::start().await;
|
||||
let keys = generate_test_keys();
|
||||
let channel_id = create_channel_for_test(
|
||||
&keys,
|
||||
&format!("mcp-e2e-list-{}", uuid::Uuid::new_v4().simple()),
|
||||
)
|
||||
.await;
|
||||
|
||||
let mut session = McpSession::start(&keys).await;
|
||||
session.initialize();
|
||||
|
||||
let resp = session.call_tool("list_channels", json!({}));
|
||||
@@ -335,12 +410,12 @@ async fn test_mcp_list_channels() {
|
||||
"list_channels returned an empty channel list"
|
||||
);
|
||||
|
||||
// Verify the seeded general channel is present.
|
||||
// Verify the channel we just created is present.
|
||||
let ids: Vec<&str> = channels.iter().filter_map(|ch| ch["id"].as_str()).collect();
|
||||
|
||||
assert!(
|
||||
ids.contains(&CHANNEL_GENERAL),
|
||||
"expected seeded 'general' channel (id={CHANNEL_GENERAL}) in list, got: {ids:?}"
|
||||
ids.contains(&channel_id.as_str()),
|
||||
"expected created channel (id={channel_id}) in list, got: {ids:?}"
|
||||
);
|
||||
|
||||
// Each channel must have the required fields.
|
||||
@@ -361,7 +436,14 @@ async fn test_mcp_list_channels() {
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn test_mcp_send_and_read_message() {
|
||||
let mut session = McpSession::start().await;
|
||||
let keys = generate_test_keys();
|
||||
let channel_id = create_channel_for_test(
|
||||
&keys,
|
||||
&format!("mcp-e2e-msg-{}", uuid::Uuid::new_v4().simple()),
|
||||
)
|
||||
.await;
|
||||
|
||||
let mut session = McpSession::start(&keys).await;
|
||||
session.initialize();
|
||||
|
||||
// Generate a unique message content so we can identify it in history.
|
||||
@@ -372,7 +454,7 @@ async fn test_mcp_send_and_read_message() {
|
||||
let send_resp = session.call_tool(
|
||||
"send_message",
|
||||
json!({
|
||||
"channel_id": CHANNEL_GENERAL,
|
||||
"channel_id": channel_id,
|
||||
"content": content,
|
||||
}),
|
||||
);
|
||||
@@ -384,8 +466,8 @@ async fn test_mcp_send_and_read_message() {
|
||||
|
||||
let send_text = McpSession::tool_text(&send_resp);
|
||||
assert!(
|
||||
send_text.contains("Message sent"),
|
||||
"expected 'Message sent' in send_message response, got: {send_text}"
|
||||
send_text.contains("event_id"),
|
||||
"expected 'event_id' in send_message response, got: {send_text}"
|
||||
);
|
||||
assert!(
|
||||
!send_text.starts_with("Error"),
|
||||
@@ -399,7 +481,7 @@ async fn test_mcp_send_and_read_message() {
|
||||
let history_resp = session.call_tool(
|
||||
"get_channel_history",
|
||||
json!({
|
||||
"channel_id": CHANNEL_GENERAL,
|
||||
"channel_id": channel_id,
|
||||
"limit": 20,
|
||||
}),
|
||||
);
|
||||
@@ -415,9 +497,13 @@ async fn test_mcp_send_and_read_message() {
|
||||
"get_channel_history returned an error: {history_text}"
|
||||
);
|
||||
|
||||
let events: Vec<Value> = serde_json::from_str(&history_text).unwrap_or_else(|e| {
|
||||
panic!("get_channel_history response is not valid JSON array: {e}\n{history_text}")
|
||||
let history_json: Value = serde_json::from_str(&history_text).unwrap_or_else(|e| {
|
||||
panic!("get_channel_history response is not valid JSON: {e}\n{history_text}")
|
||||
});
|
||||
let events = history_json
|
||||
.get("messages")
|
||||
.and_then(|m| m.as_array())
|
||||
.unwrap_or_else(|| panic!("expected 'messages' array in response: {history_text}"));
|
||||
|
||||
let found = events
|
||||
.iter()
|
||||
@@ -438,7 +524,14 @@ async fn test_mcp_send_and_read_message() {
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn test_mcp_search() {
|
||||
let mut session = McpSession::start().await;
|
||||
let keys = generate_test_keys();
|
||||
let channel_id = create_channel_for_test(
|
||||
&keys,
|
||||
&format!("mcp-e2e-search-{}", uuid::Uuid::new_v4().simple()),
|
||||
)
|
||||
.await;
|
||||
|
||||
let mut session = McpSession::start(&keys).await;
|
||||
session.initialize();
|
||||
|
||||
// Generate a unique token that will appear in the search index.
|
||||
@@ -449,7 +542,7 @@ async fn test_mcp_search() {
|
||||
let send_resp = session.call_tool(
|
||||
"send_message",
|
||||
json!({
|
||||
"channel_id": CHANNEL_GENERAL,
|
||||
"channel_id": channel_id,
|
||||
"content": content,
|
||||
}),
|
||||
);
|
||||
@@ -461,8 +554,8 @@ async fn test_mcp_search() {
|
||||
|
||||
let send_text = McpSession::tool_text(&send_resp);
|
||||
assert!(
|
||||
send_text.contains("Message sent"),
|
||||
"expected 'Message sent', got: {send_text}"
|
||||
send_text.contains("event_id"),
|
||||
"expected 'event_id' in send_message response, got: {send_text}"
|
||||
);
|
||||
|
||||
// Wait for the search index to catch up.
|
||||
@@ -485,7 +578,7 @@ async fn test_mcp_search() {
|
||||
let history_resp = session.call_tool(
|
||||
"get_channel_history",
|
||||
json!({
|
||||
"channel_id": CHANNEL_GENERAL,
|
||||
"channel_id": channel_id,
|
||||
"limit": 50,
|
||||
}),
|
||||
);
|
||||
@@ -501,9 +594,13 @@ async fn test_mcp_search() {
|
||||
"get_channel_history returned an error: {history_text}"
|
||||
);
|
||||
|
||||
let events: Vec<Value> = serde_json::from_str(&history_text).unwrap_or_else(|e| {
|
||||
let history_json: Value = serde_json::from_str(&history_text).unwrap_or_else(|e| {
|
||||
panic!("get_channel_history response is not valid JSON: {e}\n{history_text}")
|
||||
});
|
||||
let events = history_json
|
||||
.get("messages")
|
||||
.and_then(|m| m.as_array())
|
||||
.unwrap_or_else(|| panic!("expected 'messages' array in response: {history_text}"));
|
||||
|
||||
let found = events
|
||||
.iter()
|
||||
@@ -524,7 +621,14 @@ async fn test_mcp_search() {
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn test_mcp_create_and_trigger_workflow() {
|
||||
let mut session = McpSession::start().await;
|
||||
let keys = generate_test_keys();
|
||||
let channel_id = create_channel_for_test(
|
||||
&keys,
|
||||
&format!("mcp-e2e-wf-{}", uuid::Uuid::new_v4().simple()),
|
||||
)
|
||||
.await;
|
||||
|
||||
let mut session = McpSession::start(&keys).await;
|
||||
session.initialize();
|
||||
|
||||
// A minimal webhook-triggered workflow (no external side effects).
|
||||
@@ -543,7 +647,7 @@ async fn test_mcp_create_and_trigger_workflow() {
|
||||
let create_resp = session.call_tool(
|
||||
"create_workflow",
|
||||
json!({
|
||||
"channel_id": CHANNEL_ENGINEERING,
|
||||
"channel_id": channel_id,
|
||||
"yaml_definition": yaml_definition,
|
||||
}),
|
||||
);
|
||||
@@ -555,9 +659,9 @@ async fn test_mcp_create_and_trigger_workflow() {
|
||||
|
||||
let create_text = McpSession::tool_text(&create_resp);
|
||||
if create_text.starts_with("Error") {
|
||||
// The MCP server uses an ephemeral keypair that may not exist in the
|
||||
// users table (FK constraint on workflows.owner_pubkey). This is a
|
||||
// test-environment limitation, not a bug. Skip gracefully.
|
||||
// The MCP server uses a keypair that may not exist in the users table
|
||||
// (FK constraint on workflows.owner_pubkey). This is a test-environment
|
||||
// limitation, not a bug. Skip gracefully.
|
||||
eprintln!("Skipping workflow test — MCP keypair not in users table: {create_text}");
|
||||
session.stop();
|
||||
return;
|
||||
@@ -583,7 +687,7 @@ async fn test_mcp_create_and_trigger_workflow() {
|
||||
let list_resp = session.call_tool(
|
||||
"list_workflows",
|
||||
json!({
|
||||
"channel_id": CHANNEL_ENGINEERING,
|
||||
"channel_id": channel_id,
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -701,7 +805,8 @@ async fn test_mcp_create_and_trigger_workflow() {
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn test_mcp_feed_tools() {
|
||||
let mut session = McpSession::start().await;
|
||||
let keys = generate_test_keys();
|
||||
let mut session = McpSession::start(&keys).await;
|
||||
session.initialize();
|
||||
|
||||
// ── get_feed ────────────────────────────────────────────────────────────
|
||||
@@ -776,7 +881,14 @@ async fn test_mcp_feed_tools() {
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn test_mcp_canvas_set_and_get() {
|
||||
let mut session = McpSession::start().await;
|
||||
let keys = generate_test_keys();
|
||||
let channel_id = create_channel_for_test(
|
||||
&keys,
|
||||
&format!("mcp-e2e-canvas-{}", uuid::Uuid::new_v4().simple()),
|
||||
)
|
||||
.await;
|
||||
|
||||
let mut session = McpSession::start(&keys).await;
|
||||
session.initialize();
|
||||
|
||||
let unique_content = format!("MCP E2E canvas test: {}", uuid::Uuid::new_v4().simple());
|
||||
@@ -785,7 +897,7 @@ async fn test_mcp_canvas_set_and_get() {
|
||||
let set_resp = session.call_tool(
|
||||
"set_canvas",
|
||||
json!({
|
||||
"channel_id": CHANNEL_GENERAL,
|
||||
"channel_id": channel_id,
|
||||
"content": unique_content,
|
||||
}),
|
||||
);
|
||||
@@ -796,9 +908,9 @@ async fn test_mcp_canvas_set_and_get() {
|
||||
);
|
||||
|
||||
let set_text = McpSession::tool_text(&set_resp);
|
||||
assert!(
|
||||
set_text.contains("Canvas updated"),
|
||||
"expected 'Canvas updated' in set_canvas response, got: {set_text}"
|
||||
assert_eq!(
|
||||
set_text, "Canvas updated.",
|
||||
"expected 'Canvas updated.' from set_canvas, got: {set_text}"
|
||||
);
|
||||
|
||||
// Small delay for the event to propagate.
|
||||
@@ -808,7 +920,7 @@ async fn test_mcp_canvas_set_and_get() {
|
||||
let get_resp = session.call_tool(
|
||||
"get_canvas",
|
||||
json!({
|
||||
"channel_id": CHANNEL_GENERAL,
|
||||
"channel_id": channel_id,
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -818,9 +930,9 @@ async fn test_mcp_canvas_set_and_get() {
|
||||
);
|
||||
|
||||
let get_text = McpSession::tool_text(&get_resp);
|
||||
assert!(
|
||||
get_text.contains(&unique_content),
|
||||
"expected canvas content '{unique_content}' in get_canvas response, got: {get_text}"
|
||||
assert_eq!(
|
||||
get_text, unique_content,
|
||||
"expected exact canvas content '{unique_content}' from get_canvas, got: {get_text}"
|
||||
);
|
||||
|
||||
session.stop();
|
||||
|
||||
@@ -31,8 +31,33 @@ fn sub_id(name: &str) -> String {
|
||||
format!("e2e-{name}-{}", uuid::Uuid::new_v4())
|
||||
}
|
||||
|
||||
fn channel_id(name: &str) -> String {
|
||||
format!("test-channel-{name}-{}", uuid::Uuid::new_v4())
|
||||
fn relay_http_url() -> String {
|
||||
relay_url()
|
||||
.replace("wss://", "https://")
|
||||
.replace("ws://", "http://")
|
||||
.trim_end_matches('/')
|
||||
.to_string()
|
||||
}
|
||||
|
||||
/// Create a real channel in the DB via REST so the relay accepts events for it.
|
||||
async fn create_test_channel(keys: &Keys) -> String {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("{}/api/channels", relay_http_url());
|
||||
let pubkey_hex = keys.public_key().to_hex();
|
||||
let resp = client
|
||||
.post(&url)
|
||||
.header("X-Pubkey", &pubkey_hex)
|
||||
.json(&serde_json::json!({
|
||||
"name": format!("relay-e2e-{}", uuid::Uuid::new_v4()),
|
||||
"channel_type": "stream",
|
||||
"visibility": "open",
|
||||
}))
|
||||
.send()
|
||||
.await
|
||||
.expect("create channel request");
|
||||
assert_eq!(resp.status(), 201, "channel creation failed");
|
||||
let body: serde_json::Value = resp.json().await.expect("parse channel response");
|
||||
body["id"].as_str().expect("channel id").to_string()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -52,11 +77,11 @@ async fn test_connect_and_authenticate() {
|
||||
#[ignore]
|
||||
async fn test_send_event_and_receive_via_subscription() {
|
||||
let url = relay_url();
|
||||
let channel = channel_id("send-recv");
|
||||
let kind: u16 = 40001;
|
||||
|
||||
let keys_a = Keys::generate();
|
||||
let keys_b = Keys::generate();
|
||||
let channel = create_test_channel(&keys_a).await;
|
||||
|
||||
let mut client_a = SproutTestClient::connect(&url, &keys_a)
|
||||
.await
|
||||
@@ -111,11 +136,11 @@ async fn test_send_event_and_receive_via_subscription() {
|
||||
#[ignore]
|
||||
async fn test_subscription_filters_by_kind() {
|
||||
let url = relay_url();
|
||||
let channel = channel_id("filter-kind");
|
||||
let target_kind: u16 = 40001;
|
||||
let other_kind: u16 = 40002;
|
||||
|
||||
let keys = Keys::generate();
|
||||
let channel = create_test_channel(&keys).await;
|
||||
|
||||
let mut client = SproutTestClient::connect(&url, &keys)
|
||||
.await
|
||||
@@ -183,10 +208,10 @@ async fn test_subscription_filters_by_kind() {
|
||||
#[ignore]
|
||||
async fn test_close_subscription_stops_delivery() {
|
||||
let url = relay_url();
|
||||
let channel = channel_id("close-sub");
|
||||
let kind: u16 = 40001;
|
||||
|
||||
let keys = Keys::generate();
|
||||
let channel = create_test_channel(&keys).await;
|
||||
let mut client = SproutTestClient::connect(&url, &keys)
|
||||
.await
|
||||
.expect("connect");
|
||||
@@ -275,10 +300,10 @@ async fn test_unauthenticated_rejected() {
|
||||
#[ignore]
|
||||
async fn test_multiple_concurrent_clients() {
|
||||
let url = relay_url();
|
||||
let channel = channel_id("multi-client");
|
||||
let kind: u16 = 40001;
|
||||
|
||||
let keys: Vec<Keys> = (0..3).map(|_| Keys::generate()).collect();
|
||||
let channel = create_test_channel(&keys[0]).await;
|
||||
|
||||
let mut clients: Vec<SproutTestClient> =
|
||||
futures_util::future::try_join_all(keys.iter().map(|k| SproutTestClient::connect(&url, k)))
|
||||
@@ -332,10 +357,10 @@ async fn test_multiple_concurrent_clients() {
|
||||
#[ignore]
|
||||
async fn test_stored_events_returned_before_eose() {
|
||||
let url = relay_url();
|
||||
let channel = channel_id("stored-events");
|
||||
let kind: u16 = 40001;
|
||||
|
||||
let keys = Keys::generate();
|
||||
let channel = create_test_channel(&keys).await;
|
||||
let mut client = SproutTestClient::connect(&url, &keys)
|
||||
.await
|
||||
.expect("connect");
|
||||
@@ -376,10 +401,10 @@ async fn test_stored_events_returned_before_eose() {
|
||||
#[ignore]
|
||||
async fn test_ephemeral_event_not_stored() {
|
||||
let url = relay_url();
|
||||
let channel = channel_id("ephemeral");
|
||||
let ephemeral_kind: u16 = 20001;
|
||||
|
||||
let keys = Keys::generate();
|
||||
let channel = create_test_channel(&keys).await;
|
||||
let mut client = SproutTestClient::connect(&url, &keys)
|
||||
.await
|
||||
.expect("connect");
|
||||
@@ -566,10 +591,10 @@ async fn test_nip11_relay_info() {
|
||||
#[ignore]
|
||||
async fn test_pubkey_mismatch_rejected() {
|
||||
let url = relay_url();
|
||||
let channel = channel_id("pubkey-mismatch");
|
||||
|
||||
let keys_a = Keys::generate();
|
||||
let keys_b = Keys::generate();
|
||||
let channel = create_test_channel(&keys_a).await;
|
||||
|
||||
let mut client = SproutTestClient::connect(&url, &keys_a)
|
||||
.await
|
||||
@@ -592,10 +617,10 @@ async fn test_pubkey_mismatch_rejected() {
|
||||
#[ignore]
|
||||
async fn test_eose_sent_for_empty_subscription() {
|
||||
let url = relay_url();
|
||||
let channel = channel_id("empty-eose");
|
||||
let kind: u16 = 40001;
|
||||
|
||||
let keys = Keys::generate();
|
||||
let channel = create_test_channel(&keys).await;
|
||||
let mut client = SproutTestClient::connect(&url, &keys)
|
||||
.await
|
||||
.expect("connect");
|
||||
|
||||
@@ -20,10 +20,10 @@
|
||||
//!
|
||||
//! # Channel setup
|
||||
//!
|
||||
//! The relay exposes REST endpoints to list and create channels. Tests use the
|
||||
//! pre-seeded open channels (`general`, `agents`, `engineering`, etc.) for read
|
||||
//! operations and create temporary channels for write coverage when needed.
|
||||
//! Some tests also send messages via WebSocket to set up search / feed data.
|
||||
//! Each test creates its own channels dynamically via `POST /api/channels`.
|
||||
//! No pre-seeded data is required — tests are fully self-contained and work
|
||||
//! against a fresh database. Some tests also send messages via WebSocket to
|
||||
//! set up search / feed data.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -79,13 +79,6 @@ async fn authed_post_json(
|
||||
.unwrap_or_else(|e| panic!("HTTP POST {url} failed: {e}"))
|
||||
}
|
||||
|
||||
/// Known open channel IDs seeded in the dev database.
|
||||
///
|
||||
/// These are UUID5-derived from the channel name and are stable across relay
|
||||
/// restarts as long as the seed data uses the same namespace + name inputs.
|
||||
const CHANNEL_GENERAL: &str = "9a1657ac-f7aa-5db0-b632-d8bbeb6dfb50";
|
||||
const CHANNEL_ENGINEERING: &str = "1c7e1c02-87bb-5e88-b2da-5a7a9432d0c9";
|
||||
|
||||
// ── Channel tests ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// GET /api/channels returns a non-empty list with the expected fields.
|
||||
@@ -97,6 +90,26 @@ async fn test_list_channels_returns_expected_fields() {
|
||||
let pubkey_hex = keys.public_key().to_hex();
|
||||
|
||||
let url = format!("{}/api/channels", relay_http_url());
|
||||
|
||||
// Ensure at least one channel exists (fresh DB may be empty).
|
||||
let seed_resp = authed_post_json(
|
||||
&client,
|
||||
&url,
|
||||
&pubkey_hex,
|
||||
serde_json::json!({
|
||||
"name": format!("list-test-{}", uuid::Uuid::new_v4()),
|
||||
"channel_type": "stream",
|
||||
"visibility": "open",
|
||||
"description": "Seed channel for list test"
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
seed_resp.status(),
|
||||
201,
|
||||
"bootstrap channel creation must succeed"
|
||||
);
|
||||
|
||||
let resp = authed_get(&client, &url, &pubkey_hex).await;
|
||||
|
||||
assert_eq!(resp.status(), 200, "expected 200 OK from /api/channels");
|
||||
@@ -190,6 +203,30 @@ async fn test_channel_visibility_open_channels_visible_to_all() {
|
||||
|
||||
let url = format!("{}/api/channels", relay_http_url());
|
||||
|
||||
// Create an open channel as keys_a so there is at least one open channel to verify.
|
||||
let open_channel_name = format!("e2e-open-{}", uuid::Uuid::new_v4().simple());
|
||||
let create_resp = authed_post_json(
|
||||
&client,
|
||||
&url,
|
||||
&keys_a.public_key().to_hex(),
|
||||
serde_json::json!({
|
||||
"name": open_channel_name,
|
||||
"channel_type": "stream",
|
||||
"visibility": "open"
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
create_resp.status(),
|
||||
201,
|
||||
"failed to create open channel for visibility test"
|
||||
);
|
||||
let created_channel: serde_json::Value = create_resp.json().await.expect("JSON");
|
||||
let open_channel_id = created_channel["id"]
|
||||
.as_str()
|
||||
.expect("created channel must have id")
|
||||
.to_string();
|
||||
|
||||
let resp_a = authed_get(&client, &url, &keys_a.public_key().to_hex()).await;
|
||||
let resp_b = authed_get(&client, &url, &keys_b.public_key().to_hex()).await;
|
||||
|
||||
@@ -199,7 +236,7 @@ async fn test_channel_visibility_open_channels_visible_to_all() {
|
||||
let channels_a: Vec<serde_json::Value> = resp_a.json().await.expect("JSON");
|
||||
let channels_b: Vec<serde_json::Value> = resp_b.json().await.expect("JSON");
|
||||
|
||||
// Both users should see the same set of open channels.
|
||||
// Both users should see the open channel we just created.
|
||||
let ids_a: std::collections::HashSet<String> = channels_a
|
||||
.iter()
|
||||
.filter_map(|c| c["id"].as_str().map(|s| s.to_string()))
|
||||
@@ -209,19 +246,13 @@ async fn test_channel_visibility_open_channels_visible_to_all() {
|
||||
.filter_map(|c| c["id"].as_str().map(|s| s.to_string()))
|
||||
.collect();
|
||||
|
||||
assert_eq!(
|
||||
ids_a, ids_b,
|
||||
"two fresh users should see the same set of open channels"
|
||||
);
|
||||
|
||||
// The well-known seeded channels must be present.
|
||||
assert!(
|
||||
ids_a.contains(CHANNEL_GENERAL),
|
||||
"expected seeded 'general' channel (id={CHANNEL_GENERAL})"
|
||||
ids_a.contains(&open_channel_id),
|
||||
"keys_a should see the open channel we created (id={open_channel_id})"
|
||||
);
|
||||
assert!(
|
||||
ids_a.contains(CHANNEL_ENGINEERING),
|
||||
"expected seeded 'engineering' channel (id={CHANNEL_ENGINEERING})"
|
||||
ids_b.contains(&open_channel_id),
|
||||
"keys_b (unrelated user) should also see the open channel (id={open_channel_id})"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -235,14 +266,36 @@ async fn test_rest_send_message_reaches_websocket_channel_subscriptions() {
|
||||
let poster_keys = Keys::generate();
|
||||
let ws_url = relay_ws_url();
|
||||
|
||||
// Create a fresh open channel for this test.
|
||||
let channels_url = format!("{}/api/channels", relay_http_url());
|
||||
let channel_name = format!("e2e-rest-live-{}", uuid::Uuid::new_v4().simple());
|
||||
let create_resp = authed_post_json(
|
||||
&client,
|
||||
&channels_url,
|
||||
&poster_keys.public_key().to_hex(),
|
||||
serde_json::json!({
|
||||
"name": channel_name,
|
||||
"channel_type": "stream",
|
||||
"visibility": "open"
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(create_resp.status(), 201, "failed to create test channel");
|
||||
let created: serde_json::Value = create_resp.json().await.expect("JSON");
|
||||
let channel_id = created["id"]
|
||||
.as_str()
|
||||
.expect("channel must have id")
|
||||
.to_string();
|
||||
|
||||
let mut subscriber = SproutTestClient::connect(&ws_url, &subscriber_keys)
|
||||
.await
|
||||
.expect("WebSocket connect failed");
|
||||
|
||||
let sid = format!("rest-live-{}", uuid::Uuid::new_v4().simple());
|
||||
let filter = Filter::new()
|
||||
.kind(Kind::Custom(40001))
|
||||
.custom_tag(SingleLetterTag::lowercase(Alphabet::H), [CHANNEL_GENERAL]);
|
||||
let filter = Filter::new().kind(Kind::Custom(40001)).custom_tag(
|
||||
SingleLetterTag::lowercase(Alphabet::H),
|
||||
[channel_id.as_str()],
|
||||
);
|
||||
|
||||
subscriber
|
||||
.subscribe(&sid, vec![filter])
|
||||
@@ -254,11 +307,7 @@ async fn test_rest_send_message_reaches_websocket_channel_subscriptions() {
|
||||
.expect("EOSE failed");
|
||||
|
||||
let content = format!("E2E REST live message: {}", uuid::Uuid::new_v4().simple());
|
||||
let url = format!(
|
||||
"{}/api/channels/{}/messages",
|
||||
relay_http_url(),
|
||||
CHANNEL_GENERAL
|
||||
);
|
||||
let url = format!("{}/api/channels/{}/messages", relay_http_url(), channel_id);
|
||||
let resp = authed_post_json(
|
||||
&client,
|
||||
&url,
|
||||
@@ -286,7 +335,7 @@ async fn test_rest_send_message_reaches_websocket_channel_subscriptions() {
|
||||
|
||||
assert!(
|
||||
tags.iter()
|
||||
.any(|tag| tag.len() >= 2 && tag[0] == "h" && tag[1] == CHANNEL_GENERAL),
|
||||
.any(|tag| tag.len() >= 2 && tag[0] == "h" && tag[1] == channel_id),
|
||||
"REST-created message is missing the channel h tag: {tags:?}"
|
||||
);
|
||||
assert!(
|
||||
@@ -363,6 +412,27 @@ async fn test_search_returns_indexed_event() {
|
||||
let pubkey_hex = keys.public_key().to_hex();
|
||||
let ws_url = relay_ws_url();
|
||||
|
||||
// Create a channel for this test so the event is accepted by the relay.
|
||||
let channels_url = format!("{}/api/channels", relay_http_url());
|
||||
let channel_name = format!("e2e-search-{}", uuid::Uuid::new_v4().simple());
|
||||
let create_resp = authed_post_json(
|
||||
&client,
|
||||
&channels_url,
|
||||
&pubkey_hex,
|
||||
serde_json::json!({
|
||||
"name": channel_name,
|
||||
"channel_type": "stream",
|
||||
"visibility": "open"
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(create_resp.status(), 201, "failed to create test channel");
|
||||
let created: serde_json::Value = create_resp.json().await.expect("JSON");
|
||||
let channel_id = created["id"]
|
||||
.as_str()
|
||||
.expect("channel must have id")
|
||||
.to_string();
|
||||
|
||||
let unique_token = format!("e2e-search-{}", uuid::Uuid::new_v4().simple());
|
||||
let content = format!("E2E REST search test marker: {unique_token}");
|
||||
|
||||
@@ -370,8 +440,8 @@ async fn test_search_returns_indexed_event() {
|
||||
.await
|
||||
.expect("WebSocket connect failed");
|
||||
|
||||
let e_tag = Tag::parse(&["e", CHANNEL_GENERAL]).expect("tag parse failed");
|
||||
let event = nostr::EventBuilder::new(Kind::Custom(40001), &content, [e_tag])
|
||||
let h_tag = Tag::parse(&["h", &channel_id]).expect("tag parse failed");
|
||||
let event = nostr::EventBuilder::new(Kind::Custom(40001), &content, [h_tag])
|
||||
.sign_with_keys(&keys)
|
||||
.expect("event sign failed");
|
||||
|
||||
@@ -673,7 +743,28 @@ async fn test_feed_returns_activity() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Send a message to an open channel so there is activity to return.
|
||||
// Create a channel for this test so the event is accepted by the relay.
|
||||
let channels_url = format!("{}/api/channels", relay_http_url());
|
||||
let channel_name = format!("e2e-feed-{}", uuid::Uuid::new_v4().simple());
|
||||
let create_resp = authed_post_json(
|
||||
&client,
|
||||
&channels_url,
|
||||
&pubkey_hex,
|
||||
serde_json::json!({
|
||||
"name": channel_name,
|
||||
"channel_type": "stream",
|
||||
"visibility": "open"
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(create_resp.status(), 201, "failed to create test channel");
|
||||
let created_channel: serde_json::Value = create_resp.json().await.expect("JSON");
|
||||
let channel_id = created_channel["id"]
|
||||
.as_str()
|
||||
.expect("channel must have id")
|
||||
.to_string();
|
||||
|
||||
// Send a message to the open channel so there is activity to return.
|
||||
let unique_token = format!("e2e-feed-{}", uuid::Uuid::new_v4().simple());
|
||||
let content = format!("E2E feed test: {unique_token}");
|
||||
|
||||
@@ -681,8 +772,8 @@ async fn test_feed_returns_activity() {
|
||||
.await
|
||||
.expect("WebSocket connect failed");
|
||||
|
||||
let e_tag = Tag::parse(&["e", CHANNEL_GENERAL]).expect("tag parse failed");
|
||||
let event = nostr::EventBuilder::new(Kind::Custom(40001), &content, [e_tag])
|
||||
let h_tag = Tag::parse(&["h", &channel_id]).expect("tag parse failed");
|
||||
let event = nostr::EventBuilder::new(Kind::Custom(40001), &content, [h_tag])
|
||||
.sign_with_keys(&keys)
|
||||
.expect("event sign failed");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user