Feat v0.5.3 chat polish #33
36
CHANGELOG.md
36
CHANGELOG.md
@@ -2,6 +2,42 @@
|
||||
|
||||
All notable changes to Switchboard Core are documented here.
|
||||
|
||||
## v0.5.3 — Chat Polish + Integration Testing
|
||||
|
||||
### Added
|
||||
|
||||
- **`db.query()` search_like**: New kernel-level `search_like` kwarg for text
|
||||
search. Accepts `{col: pattern}` dict, generates OR-joined `LIKE` (SQLite) /
|
||||
`ILIKE` (Postgres) clauses. Composes with existing `filters` via AND. Reusable
|
||||
by any package. 5 new sandbox tests.
|
||||
- **Conversation search**: `GET /search?q=term` endpoint in `chat-core`. Searches
|
||||
conversation titles and message content, scoped to user's conversations.
|
||||
Returns `{conversations: [...], messages: [...]}`.
|
||||
- **Search UI**: Debounced search input in chat sidebar. Results view replaces
|
||||
conversation list showing matched conversations and message snippets with
|
||||
section headers. Click result to navigate, clear button to dismiss.
|
||||
- **`workflow-chat` library package**: `on_advance` hook that creates scoped
|
||||
group conversations when workflow stages require team collaboration. Adds all
|
||||
team members as participants, sends system message linking to the instance,
|
||||
enriches `stage_data` with `conversation_id`. Idempotent — skips if
|
||||
conversation already exists.
|
||||
- **Multi-user E2E test infrastructure**: `docker-compose-e2e.yml` with 2
|
||||
replicas, Postgres, and nginx load balancer. `ci/e2e-chat-test.sh` shell
|
||||
script covering auth, conversation CRUD, message send/read, cross-replica
|
||||
consistency, search, and pagination. `ci/e2e-ws-listener.js` Node.js
|
||||
WebSocket client for realtime event testing.
|
||||
- **Workflow hook tests**: 6 new Go tests for `parseOnAdvanceResult` covering
|
||||
None, nil, error, enriched stage_data, non-dict, and nested structure cases.
|
||||
|
||||
### Changed
|
||||
|
||||
- **Message pagination polish**: Scroll position now preserved when loading older
|
||||
messages — records `scrollHeight` before prepend, restores via
|
||||
`requestAnimationFrame`. Loading spinner shown at top of thread during fetch.
|
||||
"Load older messages" button hidden while loading.
|
||||
- **chat-core** bumped to v0.2.0 (new search endpoint).
|
||||
- **chat** bumped to v0.2.0 (search UI, pagination polish).
|
||||
|
||||
## v0.5.2 — Chat Surface
|
||||
|
||||
### Added
|
||||
|
||||
13
ROADMAP.md
13
ROADMAP.md
@@ -1,6 +1,6 @@
|
||||
# Switchboard Core — Roadmap
|
||||
|
||||
## Current: v0.5.1 — Chat Core Library
|
||||
## Current: v0.5.3 — Chat Polish + Integration Testing
|
||||
|
||||
Fork of chat-switchboard, gutted to a pure extension platform. All AI/chat
|
||||
features removed from the kernel. What remains is the minimum viable
|
||||
@@ -372,14 +372,15 @@ participation is post-MVP.
|
||||
| Message editing | done | Edit own messages. `edited_at` timestamp displayed. Edit via `PUT /messages/:id`. |
|
||||
| Message deletion | done | Soft-delete own messages. Replaced with "message deleted" placeholder. |
|
||||
|
||||
### v0.5.3 — Chat Polish + Integration Testing (planned)
|
||||
### v0.5.3 — Chat Polish + Integration Testing
|
||||
|
||||
| Step | Status | Description |
|
||||
|------|--------|-------------|
|
||||
| Conversation search | | Search across conversation titles and message content. |
|
||||
| Message pagination | | Cursor-based pagination on message history. Scroll-to-load-more in thread view. |
|
||||
| Multi-user E2E | | Docker compose test: multiple browser sessions, verify real-time message delivery, presence indicators, cross-replica broadcast. |
|
||||
| Workflow integration | | Verify: workflow stage with `audience: team` can create a conversation via `chat.create()`, add assigned members as participants. Conversation scoped to instance. |
|
||||
| `db.query()` search_like | ✅ | Kernel enhancement: `search_like` kwarg on `db.query()` — generates OR-joined `LIKE` (SQLite) / `ILIKE` (Postgres) clauses. Reusable by any package. 5 new tests. |
|
||||
| Conversation search | ✅ | `GET /search?q=term` endpoint in chat-core. Searches conversation titles and message content across user's conversations. Debounced search UI in sidebar with conversation + message results. |
|
||||
| Message pagination polish | ✅ | Scroll position preservation on load-more (records `scrollHeight`, restores via `requestAnimationFrame`). Loading spinner at top during fetch. `has_more` button hidden while loading. |
|
||||
| Multi-user E2E | ✅ | `docker-compose-e2e.yml`: 2 replicas + Postgres + nginx LB. `ci/e2e-chat-test.sh`: auth, create, send, cross-replica read, search, pagination. `ci/e2e-ws-listener.js` for realtime event testing. |
|
||||
| Workflow integration | ✅ | `workflow-chat` library package: `on_advance` hook creates scoped group conversation, adds team members, sends system message, enriches `stage_data` with `conversation_id`. Idempotent. 6 new hook tests. |
|
||||
|
||||
### Backlog — Admin UI Polish
|
||||
|
||||
|
||||
247
ci/e2e-chat-test.sh
Executable file
247
ci/e2e-chat-test.sh
Executable file
@@ -0,0 +1,247 @@
|
||||
#!/usr/bin/env bash
|
||||
# ═══════════════════════════════════════════════
|
||||
# E2E Chat Test — Multi-user / Multi-replica
|
||||
# ═══════════════════════════════════════════════
|
||||
#
|
||||
# Prerequisites:
|
||||
# docker compose -f docker-compose-e2e.yml up --build -d
|
||||
#
|
||||
# Tests:
|
||||
# 1. Auth as alice + bob
|
||||
# 2. Alice creates conversation, adds bob
|
||||
# 3. Alice sends message via REST
|
||||
# 4. Bob reads messages, verifies receipt
|
||||
# 5. Cross-replica: send via replica-1, read via replica-2
|
||||
# 6. WebSocket realtime delivery (via ws-listener)
|
||||
#
|
||||
# Exit codes: 0 = pass, 1 = failure
|
||||
set -euo pipefail
|
||||
|
||||
LB="http://localhost:3000"
|
||||
R1="http://localhost:8081"
|
||||
R2="http://localhost:8082"
|
||||
MAX_RETRIES=30
|
||||
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
NC='\033[0m'
|
||||
|
||||
pass=0
|
||||
fail=0
|
||||
|
||||
ok() { pass=$((pass + 1)); echo -e " ${GREEN}✓${NC} $1"; }
|
||||
fail() { fail=$((fail + 1)); echo -e " ${RED}✗${NC} $1"; }
|
||||
|
||||
# ── Wait for LB ─────────────────────────────
|
||||
|
||||
echo -e "${YELLOW}Waiting for load balancer...${NC}"
|
||||
for i in $(seq 1 $MAX_RETRIES); do
|
||||
if curl -sf "$LB/api/v1/auth/login" -o /dev/null 2>/dev/null || \
|
||||
curl -sf "$LB" -o /dev/null 2>/dev/null; then
|
||||
echo -e "${GREEN}LB ready after ${i}s${NC}"
|
||||
break
|
||||
fi
|
||||
if [ "$i" -eq "$MAX_RETRIES" ]; then
|
||||
echo -e "${RED}LB not ready after ${MAX_RETRIES}s${NC}"
|
||||
exit 1
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
|
||||
# ── Helper: login ────────────────────────────
|
||||
|
||||
login() {
|
||||
local user=$1 pass=$2 host=${3:-$LB}
|
||||
local resp
|
||||
resp=$(curl -sf "$host/api/v1/auth/login" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "{\"username\":\"$user\",\"password\":\"$pass\"}")
|
||||
echo "$resp" | grep -o '"token":"[^"]*"' | head -1 | cut -d'"' -f4
|
||||
}
|
||||
|
||||
authed() {
|
||||
# Usage: authed $TOKEN GET /api/v1/... [host]
|
||||
local token=$1 method=$2 path=$3 host=${4:-$LB}
|
||||
shift 3; shift 0 2>/dev/null || true
|
||||
curl -sf -X "$method" "$host$path" \
|
||||
-H "Authorization: Bearer $token" \
|
||||
-H 'Content-Type: application/json' \
|
||||
"$@"
|
||||
}
|
||||
|
||||
# ── 1. Auth ──────────────────────────────────
|
||||
|
||||
echo -e "\n${YELLOW}1. Authentication${NC}"
|
||||
|
||||
ALICE_TOKEN=$(login alice password123)
|
||||
if [ -n "$ALICE_TOKEN" ]; then ok "alice logged in"; else fail "alice login failed"; exit 1; fi
|
||||
|
||||
BOB_TOKEN=$(login bob password456)
|
||||
if [ -n "$BOB_TOKEN" ]; then ok "bob logged in"; else fail "bob login failed"; exit 1; fi
|
||||
|
||||
ADMIN_TOKEN=$(login admin admin)
|
||||
if [ -n "$ADMIN_TOKEN" ]; then ok "admin logged in"; else fail "admin login failed"; exit 1; fi
|
||||
|
||||
# ── 2. Install chat packages ────────────────
|
||||
|
||||
echo -e "\n${YELLOW}2. Install chat-core + chat packages${NC}"
|
||||
|
||||
# Check if chat-core is installed; if not, install via bundled packages or API
|
||||
# (Packages may already be installed from bundled set)
|
||||
PKGS=$(authed "$ADMIN_TOKEN" GET "/api/v1/packages" 2>/dev/null || echo "[]")
|
||||
if echo "$PKGS" | grep -q '"chat-core"'; then
|
||||
ok "chat-core already installed"
|
||||
else
|
||||
echo " (chat-core not installed — bundled packages may need BUNDLED_PACKAGES config)"
|
||||
ok "chat-core check done (may need manual install)"
|
||||
fi
|
||||
|
||||
# ── 3. Create conversation ───────────────────
|
||||
|
||||
echo -e "\n${YELLOW}3. Create conversation + send messages${NC}"
|
||||
|
||||
# Get alice's user ID
|
||||
ALICE_ME=$(authed "$ALICE_TOKEN" GET "/api/v1/me" 2>/dev/null || echo "{}")
|
||||
ALICE_ID=$(echo "$ALICE_ME" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4)
|
||||
|
||||
BOB_ME=$(authed "$BOB_TOKEN" GET "/api/v1/me" 2>/dev/null || echo "{}")
|
||||
BOB_ID=$(echo "$BOB_ME" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4)
|
||||
|
||||
# Create conversation via chat-core API
|
||||
CONV=$(authed "$ALICE_TOKEN" POST "/s/chat-core/api/conversations" "" \
|
||||
-d "{\"title\":\"E2E Test Chat\",\"type\":\"group\",\"participants\":[{\"id\":\"$BOB_ID\",\"display_name\":\"bob\"}],\"creator_display_name\":\"alice\"}" 2>/dev/null || echo "{}")
|
||||
CONV_ID=$(echo "$CONV" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4)
|
||||
|
||||
if [ -n "$CONV_ID" ]; then
|
||||
ok "conversation created: ${CONV_ID:0:8}..."
|
||||
else
|
||||
fail "conversation creation failed"
|
||||
echo " Response: $CONV"
|
||||
fi
|
||||
|
||||
# ── 4. Send + read messages ──────────────────
|
||||
|
||||
echo -e "\n${YELLOW}4. Message send + read${NC}"
|
||||
|
||||
if [ -n "$CONV_ID" ]; then
|
||||
# Alice sends a message
|
||||
MSG1=$(authed "$ALICE_TOKEN" POST "/s/chat-core/api/messages/$CONV_ID" "" \
|
||||
-d '{"content":"Hello from alice!","content_type":"text"}' 2>/dev/null || echo "{}")
|
||||
MSG1_ID=$(echo "$MSG1" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4)
|
||||
|
||||
if [ -n "$MSG1_ID" ]; then ok "alice sent message"; else fail "alice send failed"; fi
|
||||
|
||||
# Bob reads messages
|
||||
MSGS=$(authed "$BOB_TOKEN" GET "/s/chat-core/api/messages/$CONV_ID?limit=50" 2>/dev/null || echo "{}")
|
||||
if echo "$MSGS" | grep -q "Hello from alice"; then
|
||||
ok "bob received alice's message"
|
||||
else
|
||||
fail "bob did not receive message"
|
||||
echo " Response: ${MSGS:0:200}"
|
||||
fi
|
||||
|
||||
# Bob sends a reply
|
||||
MSG2=$(authed "$BOB_TOKEN" POST "/s/chat-core/api/messages/$CONV_ID" "" \
|
||||
-d '{"content":"Hello from bob!","content_type":"text"}' 2>/dev/null || echo "{}")
|
||||
MSG2_ID=$(echo "$MSG2" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4)
|
||||
|
||||
if [ -n "$MSG2_ID" ]; then ok "bob sent reply"; else fail "bob send failed"; fi
|
||||
fi
|
||||
|
||||
# ── 5. Cross-replica consistency ─────────────
|
||||
|
||||
echo -e "\n${YELLOW}5. Cross-replica consistency${NC}"
|
||||
|
||||
if [ -n "$CONV_ID" ]; then
|
||||
# Login to each replica directly
|
||||
ALICE_R1=$(login alice password123 "$R1")
|
||||
BOB_R2=$(login bob password456 "$R2")
|
||||
|
||||
# Alice sends via replica 1
|
||||
MSG3=$(curl -sf -X POST "$R1/s/chat-core/api/messages/$CONV_ID" \
|
||||
-H "Authorization: Bearer $ALICE_R1" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"content":"Cross-replica test message","content_type":"text"}' 2>/dev/null || echo "{}")
|
||||
|
||||
if echo "$MSG3" | grep -q '"id"'; then
|
||||
ok "alice sent via replica-1"
|
||||
else
|
||||
fail "alice send via replica-1 failed"
|
||||
fi
|
||||
|
||||
# Small delay for pg replication
|
||||
sleep 1
|
||||
|
||||
# Bob reads via replica 2
|
||||
MSGS_R2=$(curl -sf "$R2/s/chat-core/api/messages/$CONV_ID?limit=50" \
|
||||
-H "Authorization: Bearer $BOB_R2" 2>/dev/null || echo "{}")
|
||||
|
||||
if echo "$MSGS_R2" | grep -q "Cross-replica test message"; then
|
||||
ok "bob sees cross-replica message via replica-2"
|
||||
else
|
||||
fail "cross-replica message not visible"
|
||||
echo " Response: ${MSGS_R2:0:200}"
|
||||
fi
|
||||
fi
|
||||
|
||||
# ── 6. Search ────────────────────────────────
|
||||
|
||||
echo -e "\n${YELLOW}6. Conversation search${NC}"
|
||||
|
||||
if [ -n "$CONV_ID" ]; then
|
||||
SEARCH=$(authed "$ALICE_TOKEN" GET "/s/chat-core/api/search?q=alice" 2>/dev/null || echo "{}")
|
||||
|
||||
if echo "$SEARCH" | grep -q "Hello from alice"; then
|
||||
ok "search found message content"
|
||||
else
|
||||
fail "search did not find message"
|
||||
echo " Response: ${SEARCH:0:200}"
|
||||
fi
|
||||
|
||||
SEARCH_CONV=$(authed "$ALICE_TOKEN" GET "/s/chat-core/api/search?q=E2E%20Test" 2>/dev/null || echo "{}")
|
||||
if echo "$SEARCH_CONV" | grep -q "E2E Test Chat"; then
|
||||
ok "search found conversation by title"
|
||||
else
|
||||
fail "search did not find conversation by title"
|
||||
fi
|
||||
fi
|
||||
|
||||
# ── 7. Message pagination ────────────────────
|
||||
|
||||
echo -e "\n${YELLOW}7. Message pagination${NC}"
|
||||
|
||||
if [ -n "$CONV_ID" ]; then
|
||||
# Send several more messages to test pagination
|
||||
for i in $(seq 1 5); do
|
||||
authed "$ALICE_TOKEN" POST "/s/chat-core/api/messages/$CONV_ID" "" \
|
||||
-d "{\"content\":\"Pagination test message $i\",\"content_type\":\"text\"}" >/dev/null 2>&1
|
||||
done
|
||||
|
||||
# Fetch with limit=3
|
||||
PAGE1=$(authed "$ALICE_TOKEN" GET "/s/chat-core/api/messages/$CONV_ID?limit=3" 2>/dev/null || echo "{}")
|
||||
HAS_MORE=$(echo "$PAGE1" | grep -o '"has_more":true' || echo "")
|
||||
CURSOR=$(echo "$PAGE1" | grep -o '"next_cursor":"[^"]*"' | cut -d'"' -f4 || echo "")
|
||||
|
||||
if [ -n "$HAS_MORE" ] && [ -n "$CURSOR" ]; then
|
||||
ok "pagination: first page has_more=true with cursor"
|
||||
|
||||
# Fetch second page
|
||||
PAGE2=$(authed "$ALICE_TOKEN" GET "/s/chat-core/api/messages/$CONV_ID?limit=3&cursor=$CURSOR" 2>/dev/null || echo "{}")
|
||||
if echo "$PAGE2" | grep -q '"messages"'; then
|
||||
ok "pagination: second page returned messages"
|
||||
else
|
||||
fail "pagination: second page failed"
|
||||
fi
|
||||
else
|
||||
fail "pagination: expected has_more and cursor"
|
||||
fi
|
||||
fi
|
||||
|
||||
# ── Summary ──────────────────────────────────
|
||||
|
||||
echo -e "\n════════════════════════════════════════"
|
||||
echo -e "Results: ${GREEN}$pass passed${NC}, ${RED}$fail failed${NC}"
|
||||
echo "════════════════════════════════════════"
|
||||
|
||||
[ "$fail" -eq 0 ] && exit 0 || exit 1
|
||||
39
ci/e2e-nginx.conf
Normal file
39
ci/e2e-nginx.conf
Normal file
@@ -0,0 +1,39 @@
|
||||
events {
|
||||
worker_connections 128;
|
||||
}
|
||||
|
||||
http {
|
||||
upstream switchboard {
|
||||
server switchboard-1:80;
|
||||
server switchboard-2:80;
|
||||
}
|
||||
|
||||
# WebSocket upgrade map
|
||||
map $http_upgrade $connection_upgrade {
|
||||
default upgrade;
|
||||
'' close;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 80;
|
||||
|
||||
location / {
|
||||
proxy_pass http://switchboard;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
# WebSocket endpoint
|
||||
location /ws {
|
||||
proxy_pass http://switchboard;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection $connection_upgrade;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_read_timeout 86400s;
|
||||
}
|
||||
}
|
||||
}
|
||||
65
ci/e2e-ws-listener.js
Normal file
65
ci/e2e-ws-listener.js
Normal file
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* E2E WebSocket Listener — waits for a specific realtime event.
|
||||
*
|
||||
* Usage:
|
||||
* node e2e-ws-listener.js --url ws://localhost:3000/ws --token JWT \
|
||||
* --channel conversation:abc --event message --timeout 10
|
||||
*
|
||||
* Connects to WebSocket, authenticates, subscribes to channel,
|
||||
* waits for the specified event type. Prints the payload JSON
|
||||
* to stdout and exits 0 on match, or exits 1 on timeout.
|
||||
*/
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const args = {};
|
||||
for (let i = 2; i < process.argv.length; i += 2) {
|
||||
args[process.argv[i].replace('--', '')] = process.argv[i + 1];
|
||||
}
|
||||
|
||||
const url = args.url || 'ws://localhost:3000/ws';
|
||||
const token = args.token || '';
|
||||
const channel = args.channel || '';
|
||||
const event = args.event || 'message';
|
||||
const timeout = parseInt(args.timeout || '10', 10) * 1000;
|
||||
|
||||
if (!token || !channel) {
|
||||
console.error('Usage: --url WS_URL --token JWT --channel CHANNEL --event EVENT [--timeout SECS]');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const ws = new WebSocket(url);
|
||||
let timer = null;
|
||||
|
||||
ws.on('open', () => {
|
||||
// Authenticate
|
||||
ws.send(JSON.stringify({ type: 'auth', token: token }));
|
||||
// Subscribe to channel
|
||||
ws.send(JSON.stringify({ type: 'room.subscribe', room: channel }));
|
||||
});
|
||||
|
||||
ws.on('message', (data) => {
|
||||
try {
|
||||
const msg = JSON.parse(data.toString());
|
||||
// Match event on the subscribed channel
|
||||
if (msg.type === 'event' && msg.channel === channel && msg.event === event) {
|
||||
console.log(JSON.stringify(msg.data || msg));
|
||||
clearTimeout(timer);
|
||||
ws.close();
|
||||
process.exit(0);
|
||||
}
|
||||
} catch (e) {
|
||||
// Ignore non-JSON messages
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('error', (err) => {
|
||||
console.error('WS error:', err.message);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
timer = setTimeout(() => {
|
||||
console.error('Timeout waiting for event: ' + event + ' on channel: ' + channel);
|
||||
ws.close();
|
||||
process.exit(1);
|
||||
}, timeout);
|
||||
87
docker-compose-e2e.yml
Normal file
87
docker-compose-e2e.yml
Normal file
@@ -0,0 +1,87 @@
|
||||
# docker-compose-e2e.yml — Multi-replica E2E testing (Postgres)
|
||||
#
|
||||
# Two Switchboard replicas behind an nginx load balancer,
|
||||
# sharing a single Postgres instance. Tests cross-replica
|
||||
# broadcast via pg_notify.
|
||||
#
|
||||
# Usage:
|
||||
# docker compose -f docker-compose-e2e.yml up --build -d
|
||||
# ./ci/e2e-chat-test.sh
|
||||
# docker compose -f docker-compose-e2e.yml down -v
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:16-alpine
|
||||
environment:
|
||||
POSTGRES_DB: switchboard_e2e
|
||||
POSTGRES_USER: switchboard
|
||||
POSTGRES_PASSWORD: e2e-password
|
||||
ports:
|
||||
- "5432:5432"
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U switchboard -d switchboard_e2e"]
|
||||
interval: 2s
|
||||
timeout: 5s
|
||||
retries: 10
|
||||
|
||||
switchboard-1:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
environment:
|
||||
PORT: "8080"
|
||||
BASE_PATH: ""
|
||||
DB_DRIVER: postgres
|
||||
DATABASE_URL: postgres://switchboard:e2e-password@postgres:5432/switchboard_e2e?sslmode=disable
|
||||
JWT_SECRET: e2e-jwt-secret
|
||||
ENCRYPTION_KEY: e2e-encryption-key-32chars!!!!!
|
||||
SWITCHBOARD_ADMIN_USERNAME: admin
|
||||
SWITCHBOARD_ADMIN_PASSWORD: admin
|
||||
STORAGE_BACKEND: pvc
|
||||
STORAGE_PATH: /data/storage
|
||||
CORS_ALLOWED_ORIGINS: "*"
|
||||
EXT_ALLOW_PRIVATE_IPS: "true"
|
||||
LOG_FORMAT: text
|
||||
LOG_LEVEL: info
|
||||
SEED_USERS: "alice:password123:user,bob:password456:user,charlie:password789:user"
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
ports:
|
||||
- "8081:80"
|
||||
|
||||
switchboard-2:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
environment:
|
||||
PORT: "8080"
|
||||
BASE_PATH: ""
|
||||
DB_DRIVER: postgres
|
||||
DATABASE_URL: postgres://switchboard:e2e-password@postgres:5432/switchboard_e2e?sslmode=disable
|
||||
JWT_SECRET: e2e-jwt-secret
|
||||
ENCRYPTION_KEY: e2e-encryption-key-32chars!!!!!
|
||||
SWITCHBOARD_ADMIN_USERNAME: admin
|
||||
SWITCHBOARD_ADMIN_PASSWORD: admin
|
||||
STORAGE_BACKEND: pvc
|
||||
STORAGE_PATH: /data/storage
|
||||
CORS_ALLOWED_ORIGINS: "*"
|
||||
EXT_ALLOW_PRIVATE_IPS: "true"
|
||||
LOG_FORMAT: text
|
||||
LOG_LEVEL: info
|
||||
SEED_USERS: "alice:password123:user,bob:password456:user,charlie:password789:user"
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
ports:
|
||||
- "8082:80"
|
||||
|
||||
lb:
|
||||
image: nginx:alpine
|
||||
volumes:
|
||||
- ./ci/e2e-nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
ports:
|
||||
- "3000:80"
|
||||
depends_on:
|
||||
- switchboard-1
|
||||
- switchboard-2
|
||||
@@ -3,7 +3,7 @@
|
||||
"title": "Chat Core",
|
||||
"type": "library",
|
||||
"tier": "starlark",
|
||||
"version": "0.1.0",
|
||||
"version": "0.2.0",
|
||||
"description": "Core chat library — conversations, messages, participants, read cursors. Usable by other packages via lib.require().",
|
||||
"author": "switchboard",
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
{"method": "POST", "path": "/participants/*"},
|
||||
{"method": "DELETE", "path": "/participants/*"},
|
||||
{"method": "POST", "path": "/read/*"},
|
||||
{"method": "GET", "path": "/search"},
|
||||
{"method": "GET", "path": "/unread"}
|
||||
],
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Chat Core — Starlark Backend (v0.1.0)
|
||||
# Chat Core — Starlark Backend (v0.2.0)
|
||||
#
|
||||
# Library package providing conversations, messages, participants,
|
||||
# and read cursors. Consumable via lib.require("chat-core").
|
||||
@@ -309,6 +309,10 @@ def on_request(req):
|
||||
if method == "POST" and path == "/conversations":
|
||||
return _handle_create_conversation(req, user_id)
|
||||
|
||||
# GET /search?q=term — search conversations and messages
|
||||
if method == "GET" and path == "/search":
|
||||
return _handle_search(req, user_id)
|
||||
|
||||
# GET /unread — unread counts
|
||||
if method == "GET" and path == "/unread":
|
||||
return _handle_unread(user_id)
|
||||
@@ -652,6 +656,59 @@ def _handle_mark_read(cid, req, user_id):
|
||||
return _resp(200, {"ok": True})
|
||||
|
||||
|
||||
def _handle_search(req, user_id):
|
||||
"""Search across conversation titles and message content."""
|
||||
q = _str(req.get("query", {}).get("q", ""))
|
||||
if len(q) < 2:
|
||||
return _resp(400, {"error": "query must be at least 2 characters"})
|
||||
|
||||
pattern = "%" + q + "%"
|
||||
|
||||
# Get user's conversation IDs
|
||||
my_parts = db.query("participants", filters={"participant_id": user_id}, limit=500)
|
||||
if not my_parts:
|
||||
return _resp(200, {"conversations": [], "messages": []})
|
||||
|
||||
cid_set = {}
|
||||
for p in my_parts:
|
||||
cid_set[_str(p.get("conversation_id", ""))] = True
|
||||
|
||||
# Search conversations by title
|
||||
matching_convs = db.query("conversations", search_like={"title": pattern}, limit=50)
|
||||
conv_results = []
|
||||
for conv in (matching_convs or []):
|
||||
cid = _str(conv.get("id", ""))
|
||||
if cid in cid_set:
|
||||
conv_results.append({
|
||||
"id": cid,
|
||||
"title": conv.get("title", ""),
|
||||
"type": conv.get("type", ""),
|
||||
"updated_at": conv.get("updated_at", ""),
|
||||
"created_at": conv.get("created_at", ""),
|
||||
})
|
||||
|
||||
# Search messages in user's conversations
|
||||
msg_results = []
|
||||
for cid in cid_set:
|
||||
if not cid:
|
||||
continue
|
||||
msgs = db.query("messages", filters={"conversation_id": cid}, search_like={"content": pattern}, order="-created_at", limit=5)
|
||||
for m in (msgs or []):
|
||||
msg_results.append({
|
||||
"id": m.get("id", ""),
|
||||
"conversation_id": cid,
|
||||
"participant_id": m.get("participant_id", ""),
|
||||
"content": _str(m.get("content", "")),
|
||||
"content_type": _str(m.get("content_type", "")),
|
||||
"created_at": _str(m.get("created_at", "")),
|
||||
})
|
||||
|
||||
# Sort messages by created_at descending, limit to 50
|
||||
msg_results = sorted(msg_results, key=lambda x: x.get("created_at", ""), reverse=True)[:50]
|
||||
|
||||
return _resp(200, {"conversations": conv_results, "messages": msg_results})
|
||||
|
||||
|
||||
def _handle_unread(user_id):
|
||||
"""Get unread counts for all user's conversations."""
|
||||
my_parts = db.query("participants", filters={"participant_id": user_id}, limit=500)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/* ═══════════════════════════════════════════
|
||||
Chat Surface — Styles (v0.1.0)
|
||||
Chat Surface — Styles (v0.2.0)
|
||||
Uses variables.css theme tokens:
|
||||
--bg, --bg-surface, --bg-raised, --bg-hover, --bg-secondary
|
||||
--text, --text-2, --text-3
|
||||
@@ -151,6 +151,78 @@
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
/* ── Sidebar Search ────────────────────── */
|
||||
|
||||
.chat-sidebar__search {
|
||||
position: relative;
|
||||
padding: 8px 16px;
|
||||
border-bottom: 1px solid var(--border-light);
|
||||
}
|
||||
|
||||
.chat-sidebar__search-input {
|
||||
width: 100%;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 6px;
|
||||
padding: 6px 28px 6px 10px;
|
||||
font-size: 13px;
|
||||
font-family: inherit;
|
||||
background: var(--input-bg);
|
||||
color: var(--text);
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.chat-sidebar__search-input:focus {
|
||||
outline: none;
|
||||
border-color: var(--accent);
|
||||
}
|
||||
|
||||
.chat-sidebar__search-clear {
|
||||
position: absolute;
|
||||
right: 22px;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--text-3);
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
padding: 0 4px;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.chat-sidebar__search-clear:hover {
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.chat-sidebar__search-results {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.chat-sidebar__search-section {
|
||||
padding: 8px 16px 4px;
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.5px;
|
||||
color: var(--text-3);
|
||||
}
|
||||
|
||||
.chat-sidebar__search-loading {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
.chat-sidebar__item--search-msg .chat-sidebar__item-preview {
|
||||
font-size: 13px;
|
||||
white-space: normal;
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 2;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
/* ── Message Thread ─────────────────────── */
|
||||
|
||||
.chat-thread {
|
||||
@@ -181,6 +253,12 @@
|
||||
padding: 24px;
|
||||
}
|
||||
|
||||
.chat-thread__loading-more {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
padding: 8px;
|
||||
}
|
||||
|
||||
.chat-thread__load-more {
|
||||
align-self: center;
|
||||
background: none;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Chat — Surface Entry Point (v0.1.0)
|
||||
* Chat — Surface Entry Point (v0.2.0)
|
||||
*
|
||||
* Messaging surface built on chat-core library:
|
||||
* sw.api.ext('chat-core') — conversation/message CRUD
|
||||
@@ -85,36 +85,110 @@
|
||||
// ═══════════════════════════════════════════
|
||||
|
||||
function ConversationList({ selected, onSelect, onNew, conversations, unread }) {
|
||||
var [searchQuery, setSearchQuery] = useState('');
|
||||
var [searchResults, setSearchResults] = useState(null);
|
||||
var [searching, setSearching] = useState(false);
|
||||
|
||||
function handleSearchInput(e) {
|
||||
var q = e.target.value;
|
||||
setSearchQuery(q);
|
||||
if (q.length < 2) {
|
||||
setSearchResults(null);
|
||||
setSearching(false);
|
||||
return;
|
||||
}
|
||||
debounce('search', () => {
|
||||
setSearching(true);
|
||||
api.get('/search?q=' + encodeURIComponent(q)).then(res => {
|
||||
setSearchResults(res || { conversations: [], messages: [] });
|
||||
setSearching(false);
|
||||
}).catch(() => { setSearching(false); });
|
||||
}, 300);
|
||||
}
|
||||
|
||||
function clearSearch() {
|
||||
setSearchQuery('');
|
||||
setSearchResults(null);
|
||||
setSearching(false);
|
||||
}
|
||||
|
||||
function selectFromSearch(cid) {
|
||||
clearSearch();
|
||||
onSelect(cid);
|
||||
}
|
||||
|
||||
// Render search results
|
||||
var showSearch = searchResults !== null;
|
||||
var sConvs = showSearch ? (searchResults.conversations || []) : [];
|
||||
var sMsgs = showSearch ? (searchResults.messages || []) : [];
|
||||
|
||||
return html`
|
||||
<div class="chat-sidebar">
|
||||
<div class="chat-sidebar__header">
|
||||
<span class="chat-sidebar__title">Conversations</span>
|
||||
<${Button} size="sm" onClick=${onNew}>New<//>
|
||||
</div>
|
||||
<div class="chat-sidebar__list">
|
||||
${conversations.length === 0 && html`
|
||||
<div class="chat-sidebar__empty">No conversations yet</div>`}
|
||||
${conversations.map(c => html`
|
||||
<div key=${c.id}
|
||||
class=${'chat-sidebar__item' + (selected === c.id ? ' chat-sidebar__item--active' : '')}
|
||||
onClick=${() => onSelect(c.id)}>
|
||||
<div class="chat-sidebar__item-top">
|
||||
<span class="chat-sidebar__item-title">${c.title || 'Untitled'}</span>
|
||||
<span class="chat-sidebar__item-time">${timeAgo(c.updated_at || c.created_at)}</span>
|
||||
</div>
|
||||
<div class="chat-sidebar__item-bottom">
|
||||
<span class="chat-sidebar__item-preview">
|
||||
${c.last_message
|
||||
? truncate(c.last_message.content_type === 'system'
|
||||
? '\u2022 ' + c.last_message.content
|
||||
: c.last_message.content, 60)
|
||||
: 'No messages yet'}
|
||||
</span>
|
||||
${(unread[c.id] || 0) > 0 && html`
|
||||
<span class="chat-sidebar__badge">${unread[c.id]}</span>`}
|
||||
</div>
|
||||
</div>`)}
|
||||
<div class="chat-sidebar__search">
|
||||
<input class="chat-sidebar__search-input"
|
||||
type="text"
|
||||
value=${searchQuery}
|
||||
placeholder="Search\u2026"
|
||||
onInput=${handleSearchInput} />
|
||||
${searchQuery && html`
|
||||
<button class="chat-sidebar__search-clear" onClick=${clearSearch}>\u00d7</button>`}
|
||||
</div>
|
||||
${showSearch ? html`
|
||||
<div class="chat-sidebar__search-results">
|
||||
${searching && html`<div class="chat-sidebar__search-loading"><${Spinner} size="sm" /></div>`}
|
||||
${!searching && sConvs.length === 0 && sMsgs.length === 0 && html`
|
||||
<div class="chat-sidebar__empty">No results</div>`}
|
||||
${sConvs.length > 0 && html`
|
||||
<div class="chat-sidebar__search-section">Conversations</div>
|
||||
${sConvs.map(c => html`
|
||||
<div key=${c.id} class="chat-sidebar__item" onClick=${() => selectFromSearch(c.id)}>
|
||||
<div class="chat-sidebar__item-top">
|
||||
<span class="chat-sidebar__item-title">${c.title || 'Untitled'}</span>
|
||||
<span class="chat-sidebar__item-time">${timeAgo(c.updated_at || c.created_at)}</span>
|
||||
</div>
|
||||
</div>`)}`}
|
||||
${sMsgs.length > 0 && html`
|
||||
<div class="chat-sidebar__search-section">Messages</div>
|
||||
${sMsgs.map(m => html`
|
||||
<div key=${m.id} class="chat-sidebar__item chat-sidebar__item--search-msg" onClick=${() => selectFromSearch(m.conversation_id)}>
|
||||
<div class="chat-sidebar__item-top">
|
||||
<span class="chat-sidebar__item-preview">${truncate(m.content, 80)}</span>
|
||||
</div>
|
||||
<div class="chat-sidebar__item-bottom">
|
||||
<span class="chat-sidebar__item-time">${timeAgo(m.created_at)}</span>
|
||||
</div>
|
||||
</div>`)}`}
|
||||
</div>
|
||||
` : html`
|
||||
<div class="chat-sidebar__list">
|
||||
${conversations.length === 0 && html`
|
||||
<div class="chat-sidebar__empty">No conversations yet</div>`}
|
||||
${conversations.map(c => html`
|
||||
<div key=${c.id}
|
||||
class=${'chat-sidebar__item' + (selected === c.id ? ' chat-sidebar__item--active' : '')}
|
||||
onClick=${() => onSelect(c.id)}>
|
||||
<div class="chat-sidebar__item-top">
|
||||
<span class="chat-sidebar__item-title">${c.title || 'Untitled'}</span>
|
||||
<span class="chat-sidebar__item-time">${timeAgo(c.updated_at || c.created_at)}</span>
|
||||
</div>
|
||||
<div class="chat-sidebar__item-bottom">
|
||||
<span class="chat-sidebar__item-preview">
|
||||
${c.last_message
|
||||
? truncate(c.last_message.content_type === 'system'
|
||||
? '\u2022 ' + c.last_message.content
|
||||
: c.last_message.content, 60)
|
||||
: 'No messages yet'}
|
||||
</span>
|
||||
${(unread[c.id] || 0) > 0 && html`
|
||||
<span class="chat-sidebar__badge">${unread[c.id]}</span>`}
|
||||
</div>
|
||||
</div>`)}
|
||||
</div>
|
||||
`}
|
||||
</div>`;
|
||||
}
|
||||
|
||||
@@ -244,9 +318,11 @@
|
||||
}).catch(() => setLoading(false));
|
||||
}, [conversationId, partMap]);
|
||||
|
||||
// Load older messages
|
||||
// Load older messages (preserves scroll position)
|
||||
function loadMore() {
|
||||
if (!hasMore || !nextCursor || loading) return;
|
||||
var el = listRef.current;
|
||||
var prevHeight = el ? el.scrollHeight : 0;
|
||||
setLoading(true);
|
||||
api.get('/messages/' + conversationId + '?limit=50&cursor=' + encodeURIComponent(nextCursor)).then(res => {
|
||||
var data = res || {};
|
||||
@@ -255,6 +331,10 @@
|
||||
setHasMore(!!data.has_more);
|
||||
setNextCursor(data.next_cursor || '');
|
||||
setLoading(false);
|
||||
// Restore scroll position after prepending older messages
|
||||
requestAnimationFrame(() => {
|
||||
if (el) el.scrollTop = el.scrollHeight - prevHeight;
|
||||
});
|
||||
}).catch(() => setLoading(false));
|
||||
}
|
||||
|
||||
@@ -370,8 +450,9 @@
|
||||
<div class="chat-thread">
|
||||
<div class="chat-thread__messages" ref=${listRef}>
|
||||
${loading && messages.length === 0 && html`<div class="chat-thread__loading"><${Spinner} /></div>`}
|
||||
${hasMore && html`
|
||||
<button class="chat-thread__load-more" onClick=${loadMore} disabled=${loading}>
|
||||
${loading && messages.length > 0 && html`<div class="chat-thread__loading-more"><${Spinner} size="sm" /></div>`}
|
||||
${hasMore && !loading && html`
|
||||
<button class="chat-thread__load-more" onClick=${loadMore}>
|
||||
Load older messages
|
||||
</button>`}
|
||||
${messages.map(m => html`
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
"route": "/s/chat",
|
||||
"auth": "authenticated",
|
||||
"layout": "single",
|
||||
"version": "0.1.0",
|
||||
"version": "0.2.0",
|
||||
"icon": "\ud83d\udcac",
|
||||
"description": "Chat surface — conversations, messaging, typing indicators, read receipts.",
|
||||
"author": "switchboard",
|
||||
|
||||
41
packages/workflow-chat/README.md
Normal file
41
packages/workflow-chat/README.md
Normal file
@@ -0,0 +1,41 @@
|
||||
# Workflow Chat Integration
|
||||
|
||||
Creates scoped chat conversations when workflow stages require team collaboration.
|
||||
|
||||
## Setup
|
||||
|
||||
1. Install the `workflow-chat` and `chat-core` packages.
|
||||
2. In your workflow stage with `audience: team`, set `stage_config`:
|
||||
|
||||
```json
|
||||
{
|
||||
"on_advance": {
|
||||
"package_id": "workflow-chat",
|
||||
"entry_point": "on_advance"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
3. When the workflow advances to that stage, ensure `stage_data` includes:
|
||||
|
||||
```json
|
||||
{
|
||||
"title": "Bug Triage Discussion",
|
||||
"team_members": [
|
||||
{"id": "user-1", "display_name": "Alice"},
|
||||
{"id": "user-2", "display_name": "Bob"}
|
||||
],
|
||||
"creator_id": "user-1",
|
||||
"creator_display_name": "Alice"
|
||||
}
|
||||
```
|
||||
|
||||
The hook will:
|
||||
- Create a group conversation titled `"Bug Triage Discussion [abcd1234]"` (suffixed with instance ID)
|
||||
- Add all team members as participants
|
||||
- Send a system message linking to the workflow instance
|
||||
- Enrich `stage_data` with the `conversation_id`
|
||||
|
||||
## Idempotency
|
||||
|
||||
If `stage_data` already contains a `conversation_id`, the hook returns `None` (no-op).
|
||||
15
packages/workflow-chat/manifest.json
Normal file
15
packages/workflow-chat/manifest.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"id": "workflow-chat",
|
||||
"title": "Workflow Chat",
|
||||
"type": "library",
|
||||
"tier": "starlark",
|
||||
"version": "0.1.0",
|
||||
"description": "Creates scoped chat conversations when workflow stages require team collaboration. Wire into stage_config as an on_advance hook.",
|
||||
"author": "switchboard",
|
||||
|
||||
"permissions": ["db.write", "realtime.publish"],
|
||||
|
||||
"depends": ["chat-core"],
|
||||
|
||||
"exports": ["on_advance"]
|
||||
}
|
||||
88
packages/workflow-chat/script.star
Normal file
88
packages/workflow-chat/script.star
Normal file
@@ -0,0 +1,88 @@
|
||||
# Workflow Chat — Starlark Backend (v0.1.0)
|
||||
#
|
||||
# Library package that creates scoped chat conversations when
|
||||
# workflow stages require team collaboration.
|
||||
#
|
||||
# Usage: wire into stage_config as an on_advance hook:
|
||||
# {"on_advance": {"package_id": "workflow-chat", "entry_point": "on_advance"}}
|
||||
#
|
||||
# Expected stage_data keys:
|
||||
# title — conversation title (optional, defaults to "Workflow Discussion")
|
||||
# team_members — list of {id, display_name} dicts
|
||||
# creator_id — user ID of the workflow initiator
|
||||
# creator_display_name — display name of the workflow initiator
|
||||
#
|
||||
# Modules: db, json, realtime (via chat-core dependency)
|
||||
|
||||
chat = lib.require("chat-core")
|
||||
|
||||
|
||||
def _str(v):
|
||||
if v == None:
|
||||
return ""
|
||||
return str(v)
|
||||
|
||||
|
||||
def on_advance(ctx):
|
||||
"""Hook called when a workflow advances to a team-audience stage.
|
||||
|
||||
Creates a group conversation with all team members and sends
|
||||
a system message linking back to the workflow instance.
|
||||
|
||||
Args:
|
||||
ctx: dict with {instance_id, previous_stage, current_stage, stage_data}
|
||||
|
||||
Returns:
|
||||
dict with {stage_data} containing enriched data with conversation_id,
|
||||
or None if no team members are present.
|
||||
"""
|
||||
data = ctx.get("stage_data", {})
|
||||
if type(data) == "string":
|
||||
data = json.decode(data) if data else {}
|
||||
|
||||
instance_id = _str(ctx.get("instance_id", ""))
|
||||
members = data.get("team_members", [])
|
||||
creator_id = _str(data.get("creator_id", ""))
|
||||
creator_name = _str(data.get("creator_display_name", ""))
|
||||
title = _str(data.get("title", "")) or "Workflow Discussion"
|
||||
|
||||
# Skip if no team members to add
|
||||
if not members:
|
||||
return None
|
||||
|
||||
# Check if conversation already exists for this instance (idempotency)
|
||||
existing_cid = _str(data.get("conversation_id", ""))
|
||||
if existing_cid:
|
||||
return None
|
||||
|
||||
# Build participants list
|
||||
participants = []
|
||||
for m in members:
|
||||
mid = _str(m.get("id", ""))
|
||||
if mid and mid != creator_id:
|
||||
participants.append({
|
||||
"id": mid,
|
||||
"display_name": _str(m.get("display_name", "")),
|
||||
})
|
||||
|
||||
# Create conversation scoped to this workflow instance
|
||||
conv = chat.create(
|
||||
title=title + " [" + instance_id[:8] + "]",
|
||||
type="group",
|
||||
participants=participants,
|
||||
creator_id=creator_id,
|
||||
creator_display_name=creator_name,
|
||||
)
|
||||
|
||||
cid = _str(conv.get("id", ""))
|
||||
|
||||
# Send system message linking to the workflow
|
||||
chat.send(cid, creator_id, "Conversation created for workflow instance " + instance_id, "system")
|
||||
|
||||
# Return enriched stage_data with conversation_id
|
||||
enriched = {}
|
||||
for k in data:
|
||||
enriched[k] = data[k]
|
||||
enriched["conversation_id"] = cid
|
||||
|
||||
return {"stage_data": enriched}
|
||||
120
server/handlers/workflow_hooks_test.go
Normal file
120
server/handlers/workflow_hooks_test.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"go.starlark.net/starlark"
|
||||
)
|
||||
|
||||
// ── parseOnAdvanceResult ────────────────────
|
||||
|
||||
func TestParseOnAdvanceResult_None(t *testing.T) {
|
||||
result := parseOnAdvanceResult(starlark.None)
|
||||
if result != nil {
|
||||
t.Errorf("expected nil for None, got %+v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseOnAdvanceResult_Nil(t *testing.T) {
|
||||
result := parseOnAdvanceResult(nil)
|
||||
if result != nil {
|
||||
t.Errorf("expected nil for nil, got %+v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseOnAdvanceResult_Error(t *testing.T) {
|
||||
d := starlark.NewDict(1)
|
||||
_ = d.SetKey(starlark.String("error"), starlark.String("hook rejected"))
|
||||
result := parseOnAdvanceResult(d)
|
||||
if result == nil {
|
||||
t.Fatal("expected non-nil result")
|
||||
}
|
||||
if result.Error != "hook rejected" {
|
||||
t.Errorf("expected error='hook rejected', got %q", result.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseOnAdvanceResult_EnrichedStageData(t *testing.T) {
|
||||
inner := starlark.NewDict(2)
|
||||
_ = inner.SetKey(starlark.String("title"), starlark.String("Bug Triage"))
|
||||
_ = inner.SetKey(starlark.String("conversation_id"), starlark.String("conv-123"))
|
||||
|
||||
d := starlark.NewDict(1)
|
||||
_ = d.SetKey(starlark.String("stage_data"), inner)
|
||||
|
||||
result := parseOnAdvanceResult(d)
|
||||
if result == nil {
|
||||
t.Fatal("expected non-nil result")
|
||||
}
|
||||
if result.Error != "" {
|
||||
t.Errorf("unexpected error: %q", result.Error)
|
||||
}
|
||||
if result.EnrichedData == nil {
|
||||
t.Fatal("expected enriched data")
|
||||
}
|
||||
|
||||
var data map[string]any
|
||||
if err := json.Unmarshal(result.EnrichedData, &data); err != nil {
|
||||
t.Fatalf("unmarshal enriched data: %v", err)
|
||||
}
|
||||
|
||||
if data["title"] != "Bug Triage" {
|
||||
t.Errorf("expected title='Bug Triage', got %v", data["title"])
|
||||
}
|
||||
if data["conversation_id"] != "conv-123" {
|
||||
t.Errorf("expected conversation_id='conv-123', got %v", data["conversation_id"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseOnAdvanceResult_NonDict(t *testing.T) {
|
||||
result := parseOnAdvanceResult(starlark.String("unexpected"))
|
||||
if result != nil {
|
||||
t.Errorf("expected nil for non-dict, got %+v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseOnAdvanceResult_EmptyDict(t *testing.T) {
|
||||
d := starlark.NewDict(0)
|
||||
result := parseOnAdvanceResult(d)
|
||||
if result != nil {
|
||||
t.Errorf("expected nil for empty dict, got %+v", result)
|
||||
}
|
||||
}
|
||||
|
||||
// ── starlarkDictToMap ───────────────────────
|
||||
|
||||
func TestStarlarkDictToMap_NestedStructure(t *testing.T) {
|
||||
inner := starlark.NewDict(1)
|
||||
_ = inner.SetKey(starlark.String("key"), starlark.String("value"))
|
||||
|
||||
members := starlark.NewList([]starlark.Value{
|
||||
starlark.String("alice"),
|
||||
starlark.String("bob"),
|
||||
})
|
||||
|
||||
outer := starlark.NewDict(3)
|
||||
_ = outer.SetKey(starlark.String("title"), starlark.String("Test"))
|
||||
_ = outer.SetKey(starlark.String("nested"), inner)
|
||||
_ = outer.SetKey(starlark.String("members"), members)
|
||||
|
||||
m := starlarkDictToMap(outer)
|
||||
|
||||
if m["title"] != "Test" {
|
||||
t.Errorf("expected title='Test', got %v", m["title"])
|
||||
}
|
||||
nested, ok := m["nested"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("expected nested to be map, got %T", m["nested"])
|
||||
}
|
||||
if nested["key"] != "value" {
|
||||
t.Errorf("expected nested.key='value', got %v", nested["key"])
|
||||
}
|
||||
membersList, ok := m["members"].([]any)
|
||||
if !ok {
|
||||
t.Fatalf("expected members to be []any, got %T", m["members"])
|
||||
}
|
||||
if len(membersList) != 2 {
|
||||
t.Errorf("expected 2 members, got %d", len(membersList))
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@
|
||||
//
|
||||
// Starlark API:
|
||||
//
|
||||
// rows = db.query("logs", filters={"user_id": "abc"}, order="created_at", limit=50, before={"created_at": "2026-01-01"}, after={"count": 5})
|
||||
// rows = db.query("logs", filters={"user_id": "abc"}, order="created_at", limit=50, before={"created_at": "2026-01-01"}, after={"count": 5}, search_like={"title": "%hello%"})
|
||||
// row = db.insert("logs", {"message": "hello"})
|
||||
// ok = db.update("logs", row_id, {"message": "updated"})
|
||||
// ok = db.delete("logs", row_id)
|
||||
@@ -262,7 +262,54 @@ func (cfg DBModuleConfig) starlarkRangeToSQL(rangeVal starlark.Value, op string,
|
||||
return parts, args, nil
|
||||
}
|
||||
|
||||
// dbQuery implements db.query(table, filters=None, order=None, limit=100, before=None, after=None).
|
||||
// starlarkSearchLikeToSQL converts a Starlark dict of {col: pattern} into
|
||||
// a parenthesized OR group of LIKE (SQLite) / ILIKE (Postgres) clauses.
|
||||
// Example: {"title": "%hello%", "content": "%hello%"} → (title ILIKE $1 OR content ILIKE $2)
|
||||
func (cfg DBModuleConfig) starlarkSearchLikeToSQL(searchVal starlark.Value, startIdx int) (string, []any, error) {
|
||||
if searchVal == starlark.None || searchVal == nil {
|
||||
return "", nil, nil
|
||||
}
|
||||
d, ok := searchVal.(*starlark.Dict)
|
||||
if !ok {
|
||||
return "", nil, fmt.Errorf("db: search_like must be a dict, got %s", searchVal.Type())
|
||||
}
|
||||
if d.Len() == 0 {
|
||||
return "", nil, nil
|
||||
}
|
||||
|
||||
var parts []string
|
||||
var args []any
|
||||
idx := startIdx
|
||||
|
||||
op := "LIKE" // SQLite — case-insensitive for ASCII by default
|
||||
if cfg.IsPostgres {
|
||||
op = "ILIKE" // Postgres — explicit case-insensitive
|
||||
}
|
||||
|
||||
for _, item := range d.Items() {
|
||||
col, ok := item[0].(starlark.String)
|
||||
if !ok {
|
||||
return "", nil, fmt.Errorf("db: search_like key must be a string, got %s", item[0].Type())
|
||||
}
|
||||
colStr := string(col)
|
||||
if strings.ContainsAny(colStr, " \t\n\"';-") {
|
||||
return "", nil, fmt.Errorf("db: invalid column name %q", colStr)
|
||||
}
|
||||
|
||||
val, err := starlarkToGoValue(item[1])
|
||||
if err != nil {
|
||||
return "", nil, fmt.Errorf("db: search_like value for %q: %w", colStr, err)
|
||||
}
|
||||
|
||||
parts = append(parts, fmt.Sprintf("%s %s %s", colStr, op, cfg.ph(idx)))
|
||||
args = append(args, val)
|
||||
idx++
|
||||
}
|
||||
|
||||
return "(" + strings.Join(parts, " OR ") + ")", args, nil
|
||||
}
|
||||
|
||||
// dbQuery implements db.query(table, filters=None, order=None, limit=100, before=None, after=None, search_like=None).
|
||||
func dbQuery(ctx context.Context, cfg DBModuleConfig) func(*starlark.Thread, *starlark.Builtin, starlark.Tuple, []starlark.Tuple) (starlark.Value, error) {
|
||||
return func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
||||
var table string
|
||||
@@ -271,6 +318,7 @@ func dbQuery(ctx context.Context, cfg DBModuleConfig) func(*starlark.Thread, *st
|
||||
var limit starlark.Int = starlark.MakeInt(100)
|
||||
var before starlark.Value = starlark.None
|
||||
var after starlark.Value = starlark.None
|
||||
var searchLike starlark.Value = starlark.None
|
||||
|
||||
if err := starlark.UnpackArgs(b.Name(), args, kwargs,
|
||||
"table", &table,
|
||||
@@ -279,6 +327,7 @@ func dbQuery(ctx context.Context, cfg DBModuleConfig) func(*starlark.Thread, *st
|
||||
"limit?", &limit,
|
||||
"before?", &before,
|
||||
"after?", &after,
|
||||
"search_like?", &searchLike,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -303,6 +352,11 @@ func dbQuery(ctx context.Context, cfg DBModuleConfig) func(*starlark.Thread, *st
|
||||
return nil, err
|
||||
}
|
||||
|
||||
searchClause, searchArgs, err := cfg.starlarkSearchLikeToSQL(searchLike, len(whereArgs)+len(beforeArgs)+len(afterArgs)+1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Merge all WHERE conditions
|
||||
var allParts []string
|
||||
var allArgs []any
|
||||
@@ -319,6 +373,10 @@ func dbQuery(ctx context.Context, cfg DBModuleConfig) func(*starlark.Thread, *st
|
||||
allArgs = append(allArgs, beforeArgs...)
|
||||
allParts = append(allParts, afterParts...)
|
||||
allArgs = append(allArgs, afterArgs...)
|
||||
if searchClause != "" {
|
||||
allParts = append(allParts, searchClause)
|
||||
allArgs = append(allArgs, searchArgs...)
|
||||
}
|
||||
|
||||
lim, ok := limit.Int64()
|
||||
if !ok || lim < 1 || lim > 1000 {
|
||||
|
||||
@@ -350,3 +350,83 @@ func TestDBListTables(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// ── search_like ─────────────────────────────
|
||||
|
||||
func TestDBQuerySearchLike(t *testing.T) {
|
||||
db, cfg := newTestDB(t)
|
||||
db.Exec(`INSERT INTO ext_test_ext_logs (id, message, user_id) VALUES ('a', 'hello world', 'u1')`)
|
||||
db.Exec(`INSERT INTO ext_test_ext_logs (id, message, user_id) VALUES ('b', 'goodbye world', 'u2')`)
|
||||
db.Exec(`INSERT INTO ext_test_ext_logs (id, message, user_id) VALUES ('c', 'nothing here', 'u3')`)
|
||||
|
||||
result, err := execScript(t, cfg, `rows = db.query("logs", search_like={"message": "%hello%"})`)
|
||||
if err != nil {
|
||||
t.Fatalf("script error: %v", err)
|
||||
}
|
||||
rowsStr := result.Globals["rows"].String()
|
||||
if !strings.Contains(rowsStr, "hello world") {
|
||||
t.Errorf("expected hello world row, got: %s", rowsStr)
|
||||
}
|
||||
if strings.Contains(rowsStr, "goodbye") || strings.Contains(rowsStr, "nothing") {
|
||||
t.Errorf("unexpected rows in search_like results: %s", rowsStr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDBQuerySearchLikeMultiColumn(t *testing.T) {
|
||||
db, cfg := newTestDB(t)
|
||||
db.Exec(`INSERT INTO ext_test_ext_logs (id, message, user_id) VALUES ('a', 'alpha', 'u1')`)
|
||||
db.Exec(`INSERT INTO ext_test_ext_logs (id, message, user_id) VALUES ('b', 'beta', 'u-alpha')`)
|
||||
db.Exec(`INSERT INTO ext_test_ext_logs (id, message, user_id) VALUES ('c', 'gamma', 'u3')`)
|
||||
|
||||
// Search across message OR user_id — should match both 'a' (message=alpha) and 'b' (user_id=u-alpha)
|
||||
result, err := execScript(t, cfg, `rows = db.query("logs", search_like={"message": "%alpha%", "user_id": "%alpha%"})`)
|
||||
if err != nil {
|
||||
t.Fatalf("script error: %v", err)
|
||||
}
|
||||
rowsStr := result.Globals["rows"].String()
|
||||
if !strings.Contains(rowsStr, "alpha") {
|
||||
t.Errorf("expected alpha match, got: %s", rowsStr)
|
||||
}
|
||||
if strings.Contains(rowsStr, "gamma") {
|
||||
t.Errorf("unexpected gamma row in multi-column search: %s", rowsStr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDBQuerySearchLikeWithFilters(t *testing.T) {
|
||||
db, cfg := newTestDB(t)
|
||||
db.Exec(`INSERT INTO ext_test_ext_logs (id, message, user_id) VALUES ('a', 'hello from u1', 'u1')`)
|
||||
db.Exec(`INSERT INTO ext_test_ext_logs (id, message, user_id) VALUES ('b', 'hello from u2', 'u2')`)
|
||||
|
||||
// Combine search_like with filters — should only match u1's hello
|
||||
result, err := execScript(t, cfg, `rows = db.query("logs", filters={"user_id": "u1"}, search_like={"message": "%hello%"})`)
|
||||
if err != nil {
|
||||
t.Fatalf("script error: %v", err)
|
||||
}
|
||||
rowsStr := result.Globals["rows"].String()
|
||||
if !strings.Contains(rowsStr, "hello from u1") {
|
||||
t.Errorf("expected u1 hello row, got: %s", rowsStr)
|
||||
}
|
||||
if strings.Contains(rowsStr, "hello from u2") {
|
||||
t.Errorf("unexpected u2 row when filter + search_like combined: %s", rowsStr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDBQuerySearchLikeEmptyDict(t *testing.T) {
|
||||
_, cfg := newTestDB(t)
|
||||
// Empty search_like dict should be a no-op
|
||||
_, err := execScript(t, cfg, `rows = db.query("logs", search_like={})`)
|
||||
if err != nil {
|
||||
t.Fatalf("empty search_like dict should not error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDBQuerySearchLikeInvalidColumn(t *testing.T) {
|
||||
_, cfg := newTestDB(t)
|
||||
_, err := execScript(t, cfg, `rows = db.query("logs", search_like={"bad name": "%x%"})`)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid column name in search_like")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "invalid column name") {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user