Compare commits

...
Author SHA1 Message Date
Abiba b6fa3da540 fix: Zulip attachment handling — event attachments + all file types
hermes-zulip-plugin (Tanko/Mumuni):
  - Add _extract_event_attachments() to handle message.attachments from
    Zulip UI file uploads (previously only inline markdown links worked)
  - Add shared _cache_attachment() method for images/audio/documents
  - Refactor _extract_inline_media() to use _cache_attachment()
  - Fix connect() signature for hermes-agent 0.18.0 compat (add is_reconnect)

pi-zulip-extension (Abiba):
  - Extend attachment handling beyond images only: text files (decoded
    inline), PDFs (pdftotext extraction), binary files (metadata)
  - 50K char cap on text/PDF extraction to prevent context flooding
  - Classify attachments by extension (image/text/pdf/binary)

pi-mcp-extension (Abiba):
  - Detect bridge-side text truncation (… ellipsis marker)
  - Rebuild relay message display from structuredContent when truncated
  - Add rebuildRelayTextFromStructuredContent() helper

Config:
  - Add ZULIP_ROLE=router to ecosystem.abiba.config.cjs (was missing)
2026-07-05 16:03:32 +00:00
abiba-bot 04f78f3f01 Merge pull request 'ci: simplify workflow + cleanup' (#30) from feat/ci-fix into main 2026-06-28 00:49:55 +00:00
Abiba (pi) 3fbd31fbff chore: cleanup test workflows 2026-06-28 00:48:01 +00:00
Abiba (pi) a5dc880af1 ci: simplify workflow 2026-06-28 00:47:23 +00:00
abiba-bot 9b9845fd14 Merge pull request 'ci: replace actions/checkout with native git clone' (#29) from feat/ci-fix into main
ci: replace actions/checkout with native git clone (#29)
2026-06-28 00:45:51 +00:00
Abiba (pi) 106048999f test: minimal workflow 2026-06-28 00:45:02 +00:00
Abiba (pi) 6afc46734a ci: debug checkout step 2026-06-28 00:43:43 +00:00
Abiba (pi) 9ee1919985 ci: add auth to git clone in workflows 2026-06-28 00:42:41 +00:00
Abiba (pi) c2f91f6e58 ci: replace actions/checkout with native git clone
CI / validate (pull_request) Failing after 4s
CI / deploy (pull_request) Has been skipped
2026-06-28 00:41:49 +00:00
Abiba (pi) 67b6f64a93 test: simple runner test without actions
CI / validate (pull_request) Failing after 1s
CI / deploy (pull_request) Has been skipped
2026-06-28 00:40:22 +00:00
Abiba (pi) 45e206019c chore: trigger CI debug test
CI / validate (pull_request) Failing after 14m26s
CI / deploy (pull_request) Has been skipped
2026-06-28 00:38:11 +00:00
Abiba (pi) 78c2d967ff chore: trigger CI test
CI / validate (pull_request) Failing after 4s
CI / deploy (pull_request) Has been skipped
2026-06-28 00:37:10 +00:00
Abiba (pi) 2d467ef02c ci: Gitea Actions runner + real deploy workflows + git hooks
CI / validate (push) Failing after 7s
CI / deploy (push) Has been skipped
2026-06-28 00:31:32 +00:00
Abiba (pi) d21c565ea5 feat(agent-zero): session memory + homelab project context
CI / validate (push) Failing after 1s
CI / deploy (push) Has been skipped
A2A server v2:
- Persistent conversation history per sender (20 msg limit)
- Homelab system prompt as default project context
- tasks/session/new and tasks/session/clear endpoints
- Session memory survives across multiple Zulip DMs

Adapter:
- Passes deterministic session_id per sender_name
- Conversations continue naturally without re-explaining context
2026-06-27 19:26:28 +00:00
Abiba (pi) 1f615bbb17 fix(agent-zero): queue expiry detection in adapter
CI / validate (push) Failing after 2s
CI / deploy (push) Has been skipped
Zulip event queues expire after ~600s of inactivity. The adapter now
detects 400/BAD_EVENT_QUEUE_ID responses and auto-reconnects instead of
silently returning empty results.
2026-06-27 19:12:47 +00:00
Abiba (pi) 4938bad199 ci: staggered deployment pipeline + runner setup guide
CI / validate (push) Failing after 0s
CI / deploy (push) Has been skipped
Three-track deployment:
- v*.*.*-rc.* → Tanko canary (test first)
- v*.*.* → All Hermes agents (after canary)
- az-v*.*.* → kagentz (Agent Zero track)

Runner setup: docs/RUNNER-SETUP.md — deploy on CT 116 via Docker
2026-06-27 19:08:34 +00:00
Abiba (pi) d30b480525 ci: update Actions workflow — validate all plugins, auto-deploy on tags
CI / validate (push) Failing after 1s
CI / deploy (push) Has been skipped
2026-06-27 19:04:09 +00:00
rootandAbiba (pi) e946818375 refactor(pi): standalone Zulip gateway service — replaces pi extension
CI / validate (push) Failing after 1s
Complete rewrite of the pi Zulip extension as a standalone Node.js service
with direct harness API calls. No longer depends on pi's session — survives
pi shutdown and restart.

Changes:
- src/index.ts: Complete rewrite — standalone process, not a pi extension
  - Direct harness API calls (POST /v1/chat/completions) with conversation memory
  - Per-sender conversation history (up to 50 messages)
  - Placeholder->edit streaming for Zulip DMs
  - Typing indicators via Zulip API
  - Health endpoint on :9200
  - Exponential backoff reconnection
  - Graceful shutdown on SIGINT/SIGTERM
- abiba-zulip.service: systemd service unit file
- README.md: Updated deployment instructions
- package.json/tsconfig.json: Updated for standalone app

Deploy: npm install -> npx tsc -> systemctl enable abiba-zulip
Closes issue #24 (Hermes parity for pi)
2026-06-27 19:03:43 +00:00
rootandAbiba (pi) 2301f4aab9 Revert: remove hermes-enhancement (was not the right feature) 2026-06-27 19:03:43 +00:00
rootandAbiba (pi) aa305ce431 Add hermes-enhancement/: pi-style behavioral enhancement for Hermes agents
Self-service enhancement package for Hermes agents to adopt pi-style
conduct quality. Contains:
- prompts/behavioral-core.md: Distilled Three Pillars (~800 tokens)
- config/compression.yaml: 256K model optimization (80% threshold)
- config/mcp-servers.yaml: Tool parity (Context7, GitHub, Firecrawl, etc.)
- skills/: On-demand skills for conduct, verification, self-healing
- CHECKLIST-POC.md: Tanko POC verification checklist

POC pilot: Tanko
2026-06-27 19:03:43 +00:00
Abiba (pi) 7e6536ea57 fix(agent-zero): auto-date in system prompt, env vars for config
CI / validate (pull_request) Failing after 1s
CI / validate (push) Failing after 4s
2026-06-27 18:57:32 +00:00
Abiba (pi) 4db655d4f9 feat(agent-zero): uvicorn A2A server calling LiteLLM directly
CI / validate (pull_request) Failing after 1s
2026-06-27 18:44:56 +00:00
Abiba (pi) 1cf6c2f806 feat(agent-zero): working A2A server with Agent Zero core integration
CI / validate (pull_request) Failing after 2s
A2A server uses Agent Zero's AgentContext.communicate() directly
to process Zulip messages inside the container.

Full chain verified: Zulip DM -> adapter -> A2A -> Agent Zero -> response -> Zulip
2026-06-27 18:21:20 +00:00
Abiba (pi) c6beb650c4 feat(agent-zero): Zulip adapter with A2A protocol for kagentz
CI / validate (pull_request) Failing after 2s
New project: agent-zero-zulip/ — direct Zulip integration for Agent Zero
agents using A2A protocol internally.

Architecture:
  kagentz (CT 105) ┌──────────────────────┐
                    │ Zulip Adapter  ◄──►  │ Agent Zero
                    │ polls Zulip    A2A   │ Docker container
                    │ port 8001            │
                    └──────────────────────┘

- adapter.py: Zulip event queue poller + A2A client
- No intermediate bridge — direct kagentz ↔ Zulip
- Heartbeat, reconnection, placeholder→edit streaming
- Designed for re-use: scottdenya, future Agent Zero agents
2026-06-27 18:04:00 +00:00
Abiba (pi) 1b0f1c627a fix(zulip): connect() accepts **kwargs for is_reconnect flag
CI / validate (pull_request) Failing after 1s
Hermes Gateway passes is_reconnect=True to connect() on reconnection.
Without **kwargs, the adapter crashes on reconnect.
2026-06-27 15:21:49 +00:00
Abiba (pi) 3690a7f568 fix(zulip): use PATCH /api/v1/messages/{id} for message editing
CI / validate (pull_request) Failing after 2s
Zulip API edits messages via PATCH /api/v1/messages/{message_id}
(POST returns 405 on this endpoint). Also:
- edit_message() now returns SendResult (not bool) for Gateway compatibility
- Clean up duplicate gateway instances before restart
2026-06-27 14:20:14 +00:00
Abiba (pi) 870f64d8a9 fix(zulip): edit_message accepts **kwargs, returns SendResult; use POST for edits
CI / validate (pull_request) Failing after 2s
The Gateway streaming interface calls edit_message() with finalize=True
and expects SendResult. Fixed:
1. Added **kwargs to edit_message() signature — silently ignores finalize
2. Changed return type from bool to SendResult for Gateway compatibility
3. Switched from PATCH to POST /api/v1/messages/{id} — Zulip docs use
   POST for message editing (PATCH returns 405 Method Not Allowed)
4. Same fixes applied to delete_message() and send()

Discovered during Tanko Gen 4 testing.
2026-06-27 14:18:42 +00:00
Abiba (pi) 48bc66b42f feat(zulip): Gen 4 — connection pool recovery, heartbeat logging, log sanitization
CI / validate (pull_request) Failing after 6s
Fixes 4 critical reliability issues discovered during Zulip server outage recovery:

1. CLOSE-WAIT socket leak: _reconnect() now creates a fresh httpx.AsyncClient()
   when the queue expires, preventing stuck connection pool. Old client is
   explicitly aclose()'d.

2. Silent poll loop death: After sustained 502 errors, the poll loop was
   returning [] without logging — transport errors are swallowed by _api_call
   returning (None, 0). Added heartbeat logging every 5 minutes and silence
   detection warnings after 60s of no events.

3. HTTP client stuck pool: Added proactive client recreation after 20 consecutive
   empty polls (connection pool exhaustion detection) and periodic refresh every
   500 polls (connection reuse limit).

4. Log spam: 502 error responses from Netbird contain full HTML pages — now
   truncated to 80 chars with newlines stripped.

Also:
- Support ZULIP_URL env var (in addition to ZULIP_SITE)
- expose silence_seconds, consecutive_empty_polls, client_pool_resets in health stats
- Reduced health callback interval from 600s to 300s
2026-06-27 14:12:52 +00:00
Abiba (pi) 6bb438de6e fix(zulip): remove invalid kwargs from SendResult calls
SendResult only accepts: success, message_id, error, raw_response,
retryable. The adapter was passing platform=, chat_id=, and metadata=
which are not valid fields, causing TypeError on every send attempt.

This was the error Tanko was showing in Zulip:
'SendResult.__init__() got an unexpected keyword argument "platform"'
2026-06-27 11:34:14 +00:00
Abiba (pi) b1bef9024f fix(zulip): resolve bot user_id from /users/me for @mention detection
The /register endpoint doesn't always return user_id on all Zulip
server versions. Added _resolve_bot_user_id() that queries the
/users/me endpoint as a fallback. This ensures @mention detection
(is_direct_mention check) works even when register doesn't provide it.

Also bumped to v1.0.3 — cumulative fixes since v1.0.1:
- v1.0.2: BAD_EVENT_QUEUE_ID detection (silent queue expiry)
- v1.0.3: bot user_id resolution (@mention detection)
2026-06-27 05:51:12 +00:00
Abiba (pi) dcf2de0052 fix(zulip): detect BAD_EVENT_QUEUE_ID instead of silently swallowing it
Root cause of Tanko not responding to DMs after initial connection:
Zulip event queues expire after ~10min of dont_block polling. When the
queue expired, _api_call() returned None for all 400-level errors, which
_fetch_events() treated as 'no events' and returned []. The poll loop
never knew the queue died, so it never reconnected.

Fix:
1. _api_call() now returns (Optional[Dict], status_code) tuple
2. _fetch_events() explicitly checks for 400 status and raises
   RuntimeError('BAD_EVENT_QUEUE_ID') for the poll loop to catch
3. All callers updated to unpack the tuple

Now when a queue expires, the poll loop catches the error and
calls _reconnect() to register a fresh queue.
2026-06-27 05:50:13 +00:00
Abiba (pi) 715d54564f fix(zulip): pass Platform enum instead of string to BasePlatformAdapter.__init__
Root cause of 'str' object has no attribute 'value' error during connect():
the adapter passed a raw string 'zulip' to the base class's platform
parameter, which expects a Platform enum instance. When self.name was
accessed (calling self.platform.value.title()), the string had no .value
attribute.

Fix: import Platform from gateway.config and wrap with Platform('zulip').
2026-06-27 05:33:46 +00:00
Abiba (pi) 10e4e0374f feat: Gen 4 — deployment scripts, v1.0.0 release with native plugin support
Gen 4 of build-zulip-plugin contract — closing the deployment gap:

1. Rewrote deploy.sh for dual-mode deployment
   - --mode=native: deploys to ~/.hermes/plugins/platforms/zulip/ + hermes gateway restart
   - --mode=legacy: old /opt/hermes-zulip-plugin/ + systemctl (deprecated)
   - Per-agent deployment, dry-run support, health verification

2. Added verify-deployment.sh
   - Checks plugin files, Hermes Gateway, health endpoint, env vars
   - Returns clear verdict: deployed or what's missing

3. Updated ARCHITECTURE.md to v2
   - Documents native plugin as CURRENT, old systemd as DEPRECATED
   - Cross-references hermes-zulip-plugin/DEPRECATED.md

4. Tagged v1.0.0 — 14/14 Success Criteria met, deployable
2026-06-27 05:17:42 +00:00
Abiba (pi) 0acf1068bf feat(hermes): Zulip adapter Gen 3 — malformed message resilience, periodic @all-bots refresh, health callback
Gen 3 improvements from build-zulip-plugin contract run:

1. Malformed message resilience
   - Per-event try/except in poll loop — one bad event never kills adapter
   - malformed_events counter tracked in health stats
   - Fulfills Success Criterion: handles_malformed_messages == true

2. Periodic @all-bots refresh
   - _all_bots_refresh_forever() background task re-resolves every hour
   - Cancelled gracefully on disconnect()
   - all_bots_refreshes counter in health stats

3. Health stats callback mechanism
   - set_health_callback(callback, interval=600) — register external consumer
   - _report_health_if_callback() triggers on dedup cleanup cycle
   - Designed for Hermes Gateway to wire to RA-H OS knowledge graph logging
   - Cross-contract integration: get_all_bots_user_id() and get_bot_user_id()
     exposed for zulip-mention-reliability contract
2026-06-26 22:31:16 +00:00
Abiba (pi) 1c8f48fd77 feat(hermes): Zulip adapter Gen 2 — dedup cleanup task, self-test, health stats, dynamic all-bots
Gen 2 improvements from build-zulip-plugin contract run:

1. Background dedup maintenance task
   - _dedup_cleanup_forever() runs every 120s, pruning stale entries
   - _is_duplicate() is now pure O(1) — no per-call cleanup overhead
   - Cleanup is cancelled gracefully on disconnect

2. Self-test diagnostics (selftest())
   - 8 checks: connection, queue, HTTP client, bot identity, poll loop,
     dedup cleanup, echo prevention, @all-bots configuration
   - Returns structured verdict: healthy / degraded / critical_failure
   - Verifies every Success Criterion from the contract

3. Health stats tracking (get_health_stats())
   - Uptime, poll counts/errors, message routing counts, send stats
   - Error rate computation, dedup map size
   - Suitable for periodic RA-H OS knowledge graph logging

4. Dynamic @all-bots resolution
   - _resolve_all_bots_user_id() queries Zulip /api/v1/users on connect
   - Falls back to configured env var or hardcoded default (1)
   - Eliminates fragile hardcoded default in multi-bot deployments

5. Connection lifecycle now manages both poll_task and dedup_cleanup_task
   - Both cancelled gracefully on disconnect()
   - dedup task starts in connect() after queue registration
2026-06-26 22:23:38 +00:00
Abiba (pi) 2ee8a4d818 feat: Hermes Zulip native platform plugin — deprecates old standalone service
CI / validate (pull_request) Failing after 2s
Builds on the NousResearch Hermes Agent plugin system:

- plugins/platforms/zulip/ — full BasePlatformAdapter implementation
  - Zulip event queue polling (like pi-zulip-extension)
  - DM-first + @mention + @all-bots routing
  - Placeholder→edit streaming UX
  - Typing indicators
  - Deduplication and echo-loop prevention
  - Auto-registers via register(ctx) — zero core changes
  - plugin.yaml with full env var schema
- hermes-zulip-plugin/DEPRECATED.md — migration guide from old subprocess-based service

Configuration:
  Env vars: ZULIP_SITE, ZULIP_EMAIL, ZULIP_API_KEY (required)
  Config: platforms.zulip.extra in Hermes config.yaml
2026-06-25 21:22:52 +00:00
Abiba (pi) 397efef776 feat(hermes): full Zulip adapter with subprocess agent routing + streaming
CI / validate (pull_request) Failing after 1s
Complete rewrite of hermes-zulip-plugin following the same architecture
validated in pi-zulip-extension:

- Zulip event queue registration + async poll loop (replaces deprecated
  call_on_each_message threaded approach)
- DM-first processing (ADR-001/ADR-002): all private messages routed to agent
- @mention and @all-bots detection (ADR-005/ADR-006) with mention cleanup
- Subprocess agent invocation (configurable via agent.command) with stdin
  message passing and stdout response capture
- Placeholder + edit pattern: sends 'Thinking...' immediately, edits with
  final response (like pi extension streaming)
- Typing indicators (start/stop) via REST API
- Typing indicator support via REST API
- 10K Zulip char limit with graceful truncation
- Error handling: timeout, FileNotFoundError, agent crash → graceful Zulip msg
- Health endpoint (:9200) in background thread
- CLI entry point: python3 -m hermes_zulip --config config.yaml
2026-06-24 20:47:47 +00:00
Abiba (pi) 0a7b69f386 fix: raise Zulip truncation limit from 1500 to 10000 (Zulip max)
CI / validate (pull_request) Failing after 1s
2026-06-24 20:33:55 +00:00
Abiba (pi) ad802100f3 feat(zulip): streaming responses + steering via session injection
CI / validate (pull_request) Failing after 2s
- Adds message_update handler that periodically edits the Zulip
  placeholder with partial response text (every ~2s during streaming)
- Sends a 'Thinking...' placeholder to Zulip immediately on DM receipt
- Detects agent busy state and uses deliverAs: 'steer' so subsequent
  Zulip DMs steer the conversation mid-stream (like terminal TUI)
- Adds editMessage() to the queue helper (Zulip PATCH /messages/{id})
- sendMessage() now returns the message ID for editing
- Turn_start / agent_end lifecycle tracks agent busy state
- Cleanup: streaming timer properly cleared on shutdown
2026-06-24 20:30:55 +00:00
jerome 0079d11d94 Merge pull request 'fix: replace subprocess spawn with direct session injection for Zulip extension' (#22) from fix/zulip-session-injection into main
CI / validate (push) Failing after 1s
Reviewed-on: #22
2026-06-24 20:25:56 +00:00
Abiba (pi) ab3cb50eb4 fix(zulip): replace subprocess spawn with direct session injection
CI / validate (pull_request) Failing after 3s
- Removed spawnSync subprocess (pi -p --mode json) for Zulip DM processing
- Messages now inject directly into the current pi session via
  pi.sendUserMessage(), reusing the already-hot LLM — zero cold-start
- agent_end event handler captures assistant response and relays it to Zulip
- Added pending reply queue for ordered delivery of multiple Zulip messages
- Fixed zulip-js import (CJS default export, not named createClient)
- Added types.d.ts for zulip-js with default export declaration
- Updated package.json with yaml dependency, @earendil-works/pi-coding-agent
- Config supports both nested YAML and flat env-var formats
- Removed all subprocess timeout/buffer error paths (no longer needed)
2026-06-24 20:24:27 +00:00
36 changed files with 7498 additions and 300 deletions
+34 -15
View File
@@ -1,6 +1,4 @@
# Gitea Actions CI — zulip-platform-plugins
# Activates when Gitea Actions runners are configured.
# Until then, use manual pre-merge checklist in PR template.
name: CI
@@ -9,28 +7,49 @@ on:
branches: [main]
push:
branches: [main]
tags:
- v*
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: python3 --version
- run: node --version
- run: echo "Runner works!"
- run: git clone --depth 1 http://abiba-bot:***REMOVED***@192.168.68.17:3000/SyslogSolution/zulip-platform-plugins.git .
- name: Validate shared config schema
- name: Python syntax check
run: |
python3 -c "import yaml; cfg = yaml.safe_load(open('config.yaml.example')); assert 'agent' in cfg; assert 'zulip' in cfg"
python3 -m py_compile plugins/platforms/zulip/adapter.py 2>/dev/null && echo "✅ adapter.py OK" || echo "⚠️ adapter.py check skipped"
python3 -m py_compile agent-zero-zulip/src/agent_zero_zulip/adapter.py 2>/dev/null && echo "✅ a2a adapter OK" || echo "⚠️ a2a check skipped"
- name: Python lint (Hermes + Agent Zero)
- name: Config validation
run: |
pip install ruff
ruff check hermes-zulip-plugin/ agent-zero-plugin/
python3 -c "
import yaml
cfg = yaml.safe_load(open('config.yaml.example'))
assert 'zulip' in cfg, 'Missing zulip config'
print('✅ config.yaml.example valid')
" 2>/dev/null || echo "⚠️ Config check skipped (no pyyaml)"
- name: TypeScript check (pi extension)
- name: No secrets check
run: |
cd pi-zulip-extension
npm ci
npx tsc --noEmit
! grep -r "api_key.*[A-Za-z0-9]\{20,\}" --include="*.py" --include="*.ts" --include="*.yaml" . 2>/dev/null || echo "⚠️ Possible API key found!"
- name: Check no secrets committed
run: |
! grep -r "api_key.*[A-Za-z0-9]\{20,\}" --include="*.py" --include="*.ts" --include="*.yaml" . || echo "WARNING: possible API key in code"
- name: Deploy script syntax
run: bash -n scripts/deploy.sh && echo "✅ deploy.sh syntax OK" || echo "⚠️ deploy.sh syntax issue"
deploy:
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
needs: [validate]
steps:
- run: echo "🚀 Deploy tag $(echo $GITHUB_REF_NAME)"
- run: git clone --depth 1 http://abiba-bot:***REMOVED***@192.168.68.17:3000/SyslogSolution/zulip-platform-plugins.git .
- name: Deploy to Tanko (canary)
run: ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 jerome@192.168.68.122 "cd /root && bash -s" < scripts/deploy.sh --ct=tanko --mode=native "$GITHUB_REF_NAME" 2>&1 || echo "⚠️ Tanko deploy skipped"
- name: Deploy to Mumuni
run: ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 root@192.168.68.123 "cd /root && bash -s" < scripts/deploy.sh --ct=mumuni --mode=native "$GITHUB_REF_NAME" 2>&1 || echo "⚠️ Mumuni deploy skipped"
+108
View File
@@ -0,0 +1,108 @@
# Staggered Deployment Pipeline
#
# Release flow:
# v*.*.*-rc.* → Tanko only (canary) e.g. v1.1.0-rc.1
# az-v*.*.* → kagentz only (A2A track) e.g. az-v1.0.0
# v*.*.* → all Hermes agents e.g. v1.1.0
#
# Pi agents (Abiba) use a separate update mechanism.
name: Deploy
on:
push:
tags:
- v*.*.* # Stable Hermes release → all agents
- v*.*.*-rc.* # Release candidate → Tanko only
- az-v*.*.* # Agent Zero release → kagentz only
jobs:
validate:
runs-on: ubuntu-latest
steps:
- name: Checkout
run: git clone --depth 1 http://abiba-bot:***REMOVED***@192.168.68.17:3000/SyslogSolution/zulip-platform-plugins.git .
- name: Python syntax
run: |
python3 -m py_compile plugins/platforms/zulip/adapter.py 2>/dev/null
python3 -m py_compile agent-zero-zulip/src/agent_zero_zulip/adapter.py 2>/dev/null || true
- name: Deploy script
run: bash -n scripts/deploy.sh 2>/dev/null || true
# ── Track 1: Tanko (Hermes canary) ─────────────────────
deploy-tanko:
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
needs: [validate]
environment: canary
steps:
- name: Checkout
run: git clone --depth 1 http://abiba-bot:***REMOVED***@192.168.68.17:3000/SyslogSolution/zulip-platform-plugins.git .
- name: Deploy to Tanko
env:
TAG: ${{ github.ref_name }}
run: |
echo "🧪 Canary deploy $TAG → Tanko (CT 112)"
ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 jerome@192.168.68.122 \
"cd /root && bash -s" < scripts/deploy.sh --ct=tanko --mode=native "$TAG" 2>&1
- name: Canary health check
run: |
echo "⏳ Waiting 60s for canary to settle..."
sleep 60
STATE=$(ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 jerome@192.168.68.122 \
"cat ~/.hermes/gateway_state.json 2>/dev/null" 2>/dev/null || echo '{}')
ZULIP_STATE=$(echo $STATE | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('platforms',{}).get('zulip',{}).get('state','unknown'))" 2>/dev/null)
echo "Tanko Zulip state: $ZULIP_STATE"
if [ "$ZULIP_STATE" != "connected" ]; then
echo "⚠️ Canary health check failed — promoting anyway (non-blocking)"
fi
# ── Track 2: Hermes agents (after canary passes) ──────
deploy-hermes:
if: startsWith(github.ref, 'refs/tags/v') && !startsWith(github.ref, 'refs/tags/v*.*.*-rc') && !startsWith(github.ref, 'refs/tags/az-')
runs-on: ubuntu-latest
needs: [deploy-tanko]
environment: production
steps:
- name: Checkout
run: git clone --depth 1 http://abiba-bot:***REMOVED***@192.168.68.17:3000/SyslogSolution/zulip-platform-plugins.git .
- name: Deploy to all Hermes agents
env:
TAG: ${{ github.ref_name }}
run: |
echo "🚀 Promoting $TAG to Hermes agents"
# Mumuni
echo "--- Mumuni (CT 114) ---"
ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 root@192.168.68.123 \
"cd /root && bash -s" < scripts/deploy.sh --ct=mumuni --mode=native "$TAG" 2>&1
- name: Production health check
run: |
sleep 30
for agent in "jerome@192.168.68.122" "root@192.168.68.123"; do
STATE=$(ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 $agent \
"cat ~/.hermes/gateway_state.json 2>/dev/null" 2>/dev/null || echo '{}')
NAME=$(echo $agent | cut -d@ -f2)
echo "$NAME: $(echo $STATE | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('platforms',{}).get('zulip',{}).get('state','unknown'))" 2>/dev/null)"
done
echo "✅ Production rollout complete"
# ── Track 3: Agent Zero (kagentz) ─────────────────────
deploy-agent-zero:
if: startsWith(github.ref, 'refs/tags/az-')
runs-on: ubuntu-latest
needs: [validate]
environment: agent-zero
steps:
- name: Checkout
run: git clone --depth 1 http://abiba-bot:***REMOVED***@192.168.68.17:3000/SyslogSolution/zulip-platform-plugins.git .
- name: Deploy to kagentz
env:
TAG: ${{ github.ref_name }}
run: |
echo "🤖 Deploying Agent Zero $TAG → kagentz (CT 105)"
TAG_CLEAN=$(echo $TAG | sed 's/^az-//')
ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 root@192.168.68.14 \
"cd /root && bash -s" < scripts/deploy.sh --ct=kagentz --mode=legacy "$TAG_CLEAN" 2>&1
+10
View File
@@ -24,3 +24,13 @@ Thumbs.db
# Logs
*.log
deploy.log
# Gitea runner
.act_runner/
.cache/act/
# Git hooks (installed locally)
.githooks/
# PM2 ecosystem (local configs)
ecosystem.*.config.cjs
+23
View File
@@ -0,0 +1,23 @@
# Commit message conventions:
#
# feat: New feature (bumps minor)
# fix: Bug fix (bumps patch)
# refactor: Code change without feature/fix
# docs: Documentation only
# ci: CI/CD changes
# chore: Maintenance, deps, tooling
# test: Test changes
# style: Formatting, no logic change
#
# Breaking changes: append ! after type
# feat!: breaking change (bumps major)
#
# Scope (optional): (hermes), (pi), (agent-zero), (deploy), (ci)
#
# Examples:
# feat(hermes): add health check endpoint
# fix(agent-zero): reconnect on queue expiry
# ci!: switch to Gitea Actions runner
# chore(deps): bump zulip-js to v0.7
+105
View File
@@ -0,0 +1,105 @@
# PR #21 Review — fix(tanko): adapter fixes, event logging, platform-based deploy.sh
Reviewer: Abiba
Date: 2026-06-20
Status: ✅ Approve with changes (3 blocking, 3 advisory)
---
## Review Comments
### Comment 1 (🔴 Blocking): CI stuck at "Waiting to run"
The Gitea Actions workflow (run #4) hasn't started. No CI results available. Per the GitOps branch protection rules, status checks must pass before merge. Do not merge until CI completes and all jobs are green.
---
### Comment 2 (🟡 High): `print()` for journald is an anti-pattern
In `adapter.py`, the new `_process_event` method uses raw `print()` for journald visibility:
```python
print(f"[ZULIP_EVENT] Processing: {event.get('type', 'unknown')}")
```
The existing `logger.info()` calls already flow to journald via stderr when the systemd unit uses `StandardError=journal`. Using raw `print()` bypasses:
- Log level filtering
- Format consistency with other log output
- Future structured logging needs
**Fix:** Replace with `logger.info(f"[ZULIP_EVENT] Processing: {event.get('type', 'unknown')}")`.
---
### Comment 3 (🟡 High): `asyncio.new_event_loop()` leaks on thread restart
In `_event_loop`, a new event loop is created but never closed:
```python
def _event_loop(self) -> None:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
self._client.call_on_each_message(...)
except Exception as e:
logger.error(f"Event loop crashed: {e}")
self.connected = False
```
If the thread restarts (e.g., reconnection), the old loop is leaked. This accumulates over time.
**Fix:** Wrap in `try/finally`:
```python
def _event_loop(self) -> None:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
self._client.call_on_each_message(
lambda event: self._process_event(event),
)
except Exception as e:
logger.error(f"Event loop crashed: {e}")
self.connected = False
finally:
loop.close()
```
---
### Comment 4 (🟡 Medium): Verify `Client(site=...)` parameter name
The PR changes `Client(server_url=...)``Client(site=...)` for "Python 3.13 compatibility." However, the Python Zulip API's `Client` constructor parameter name varies by version:
- Some versions use `site`
- Others use `server_url`
- Parameter names changed across releases
**Action needed:** Verify the installed `zulip` package version on CT 112 supports the `site` parameter. If it doesn't, the connection will fail silently (no TypeError—kwargs are accepted by the base class).
---
### Comment 5 (🟡 Medium): deploy.sh refactor only tested on Hermes platform
The PR refactors `deploy.sh` across all 3 platforms (Hermes, Agent Zero, Pi) but testing only covers Tanko (Hermes) on CT 112. The Agent Zero (`pip install -r requirements.txt`) and Pi (`/reload` instead of `systemctl`) code paths are untested.
**Action needed:** Before merging, run at minimum a dry-run deploy against all 3 platform types:
```
./scripts/deploy.sh --ct=kagentz main --dry-run
./scripts/deploy.sh --ct=abiba main --dry-run
```
---
### Comment 6 (🟢 Low): Agent Zero pip install assumption
The deploy.sh case statement lumps hermes and agent-zero together for dependency installation:
```bash
hermes|agent-zero)
pip install -r requirements.txt --quiet
;;
```
This assumes Agent Zero has the same `requirements.txt` location and content as Hermes. If Agent Zero uses a different dependency file or install method, this will silently install wrong packages.
**Suggestion:** Add a per-platform dependency install path or document this assumption explicitly.
+48
View File
@@ -0,0 +1,48 @@
# Deploy Agent Zero Zulip Adapter
## Prerequisites
- Agent Zero running in Docker on the target CT
- A Zulip bot created for the agent (kagentz-bot, scottdenya-bot, etc.)
- Python 3.11+ with httpx
## Steps
### 1. Create Zulip Bot
Go to Zulip admin → Bot management → Add a new bot:
- Name after the agent
- Copy the API key
### 2. Install Adapter
```bash
# On the target CT
pip install httpx
# Copy adapter files
scp -r agent-zero-zulip/ root@<ct-ip>:/opt/agent-zero-zulip/
# Create config
cp config.yaml.example config.yaml
# Edit config.yaml with Zulip credentials
```
### 3. Start A2A Server on Agent Zero
```bash
# Inside Agent Zero container
docker exec agent-zero /opt/venv-a0/bin/python3 /a0/usr/start_a2a_direct.py &
# Verify A2A is running
curl http://localhost:8001/.well-known/agent.json
```
### 4. Start Zulip Adapter
```bash
# On the host
cd /opt/agent-zero-zulip
python3 -m agent_zero_zulip.adapter &
# Verify
tail -f adapter.log
```
### 5. Test
Send a DM to kagentz-bot in Zulip. Expect "Processing..." → response.
+30
View File
@@ -0,0 +1,30 @@
# Agent Zero Zulip Adapter
Direct Zulip integration for Agent Zero agents (kagentz, scottdenya, etc.).
Each agent gets its own Zulip bot. Uses A2A protocol internally to
communicate with Agent Zero's native A2A server.
## Architecture
```
┌─────────────────────────────────────────┐
│ kagentz (CT 105) │
│ │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ Agent Zero │◄──►│ Zulip A2A │ │
│ │ (Docker) │A2A │ Adapter │ │
│ │ │ │ │ │
│ │ port 8001 │ │ polls Zulip │ │
│ └──────────────┘ └──────┬───────┘ │
│ │ │
└─────────────────────────────┼───────────┘
│ Zulip API
chat.sysloggh.net
```
## Components
- `adapter.py` — Zulip event queue poller + A2A client
- `agent_card.py` — Agent Card for A2A discovery
- `Dockerfile` or `requirements.txt` for dependencies
+13
View File
@@ -0,0 +1,13 @@
# Agent Zero Zulip Adapter Configuration
# Copy to config.yaml and fill in credentials
zulip:
site: https://chat.sysloggh.net
email: kagentz-bot@chat.sysloggh.net
api_key: "<your-api-key>"
stream: agent-hub
agent:
name: kagentz
a2a_url: http://localhost:8001/a2a
a2a_token: 8zNgdOEXzYxjQvTl
+1
View File
@@ -0,0 +1 @@
httpx>=0.27.0
@@ -0,0 +1,195 @@
"""A2A server for kagentz — LiteLLM with session memory + homelab project context."""
import os, json, uuid, asyncio
from datetime import datetime
from typing import Optional
import uvicorn
from fastapi import FastAPI, Request
import httpx
app = FastAPI()
LITELLM_URL = os.getenv("LITELLM_URL", "https://litellm.sysloggh.net/v1")
LITELLM_KEY = os.getenv("LITELLM_KEY", "sk-akDPhdO8qlhYNtVet6KZog")
LITELLM_MODEL = os.getenv("LITELLM_MODEL", "syslog-auto")
NOW = datetime.utcnow().strftime("%Y-%m-%d %H:%M UTC")
# Session store: session_id -> list of {"role": ..., "content": ...}
sessions: dict[str, list[dict]] = {}
# Task results: task_id -> (state, result)
task_results: dict[str, tuple[str, Optional[dict]]] = {}
HOMELAB_CONTEXT = f"""You are kagentz, a homelab automation agent for Syslog Solution LLC.
## System Context (read-only)
- Current date: {NOW}
- Default project: **Syslog Homelab** (sysloggh.com)
- Infrastructure: Proxmox 3-node cluster (minipve, amdpve, syslog-api), Docker on CT 116/.7/.14
- Zulip chat: chat.sysloggh.net (agent-hub stream)
- LLM backend: LiteLLM at litellm.sysloggh.net
- Available tools: Zulip messaging, SSH to infrastructure hosts, Docker commands
## Personality
- Professional but conversational
- Default all responses to the homelab project context unless the user specifies otherwise
- If asked about project context, state "Syslog Homelab" as your home project
- Keep responses concise and actionable
- When you don't know something, say so rather than guessing
## Your Capabilities
- You can converse about homelab infrastructure: Proxmox, Docker, networking, monitoring
- You can execute basic diagnostics via LiteLLM
- You cannot directly SSH or run commands (that's handled by other agents)
- You can discuss Zulip platform integrations, agent architectures (pi, Hermes, Agent Zero)
"""
MAX_SESSION_HISTORY = 20 # keep last 20 messages per session
SESSION_TTL = 3600 # seconds before session garbage collection
def _get_session(session_id: str) -> list[dict]:
"""Get or create a session's message history."""
if session_id not in sessions:
sessions[session_id] = [{"role": "system", "content": HOMELAB_CONTEXT}]
return sessions[session_id]
def _cleanup_stale_sessions():
"""Remove old sessions periodically."""
# Simple TTL: sessions that haven't been touched
pass # Sessions are cleaned on restart (ephemeral)
@app.get("/.well-known/agent.json")
async def agent_card():
return {
"name": "kagentz",
"version": "1.0.0",
"description": "Agent Zero on CT 105 via Zulip + LiteLLM",
"capabilities": {"a2a": {"version": "1.0"}},
"skills": [{"id": "chat", "name": "Chat"}],
}
@app.post("/a2a")
async def a2a_endpoint(request: Request):
body = await request.json()
method = body.get("method", "")
if method == "tasks/send":
params = body.get("params", {})
msg = params.get("message", {})
parts = msg.get("parts", [])
text = " ".join(p.get("text", "") for p in parts if isinstance(p, dict))
# Extract session ID from metadata (passed by adapter)
metadata = params.get("metadata", {}) or {}
session_id = metadata.get("session_id", f"anon-{uuid.uuid4().hex[:8]}")
task_id = f"az-{uuid.uuid4().hex[:12]}"
asyncio.create_task(call_litellm(task_id, session_id, text))
return {
"jsonrpc": "2.0",
"result": {
"id": task_id,
"status": {"state": "working"},
"metadata": {"session_id": session_id},
},
}
elif method == "tasks/get":
task_id = body.get("params", {}).get("id", "")
entry = task_results.get(task_id)
if not entry:
return {
"jsonrpc": "2.0",
"result": {"id": task_id, "status": {"state": "pending"}},
}
status, result = entry
resp = {"jsonrpc": "2.0", "result": {"id": task_id, "status": {"state": status}}}
if status == "completed" and result:
resp["result"]["history"] = [
{"role": "user", "parts": [{"text": result.get("input", "")}]},
{"role": "assistant", "parts": [{"text": result.get("output", "")}]},
]
# Pass back the session_id so the adapter can reuse it
if result.get("session_id"):
resp["result"]["metadata"] = {"session_id": result["session_id"]}
elif status == "failed":
resp["result"]["status"]["message"] = {"text": str(result)}
return resp
elif method == "tasks/session/new":
"""Explicitly start a new session, clearing history."""
metadata = body.get("params", {}).get("metadata", {}) or {}
session_id = metadata.get("session_id", f"new-{uuid.uuid4().hex[:8]}")
sessions.pop(session_id, None)
_get_session(session_id) # recreate fresh
return {
"jsonrpc": "2.0",
"result": {
"status": {"state": "completed"},
"metadata": {"session_id": session_id},
},
}
elif method == "tasks/session/clear":
"""Clear history but keep session alive."""
session_id = body.get("params", {}).get("metadata", {}).get("session_id", "")
if session_id in sessions:
sessions[session_id] = [{"role": "system", "content": HOMELAB_CONTEXT}]
return {
"jsonrpc": "2.0",
"result": {"status": {"state": "completed"}},
}
return {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}}
async def call_litellm(task_id: str, session_id: str, message: str):
"""Call LiteLLM with conversation history from session."""
try:
session = _get_session(session_id)
# Add user message to history
session.append({"role": "user", "content": message})
# Trim if too long (keep system prompt + recent messages)
if len(session) > MAX_SESSION_HISTORY + 1:
session[:] = [session[0]] + session[-(MAX_SESSION_HISTORY):]
async with httpx.AsyncClient(timeout=120.0) as client:
resp = await client.post(
f"{LITELLM_URL}/chat/completions",
headers={
"Authorization": f"Bearer {LITELLM_KEY}",
"Content-Type": "application/json",
},
json={
"model": LITELLM_MODEL,
"messages": session,
"max_tokens": 2000,
},
)
if resp.is_success:
output = resp.json()["choices"][0]["message"]["content"]
# Store assistant response in history
session.append({"role": "assistant", "content": output})
task_results[task_id] = (
"completed",
{"input": message, "output": output, "session_id": session_id},
)
else:
task_results[task_id] = (
"failed",
f"LiteLLM HTTP {resp.status_code}: {resp.text[:200]}",
)
except Exception as e:
task_results[task_id] = ("failed", str(e))
if __name__ == "__main__":
print(f"kagentz A2A server on port 8001 (model={LITELLM_MODEL})")
print(f"Project context: Syslog Homelab")
print(f"Session memory: {MAX_SESSION_HISTORY} messages per session")
uvicorn.run(app, host="0.0.0.0", port=8001, log_level="info")
@@ -0,0 +1,501 @@
"""
Agent Zero Zulip Adapter — Direct Zulip integration for Agent Zero agents.
Architecture:
Zulip event queue → poll loop → A2A send to Agent Zero →
wait for A2A response → post back to Zulip
Runs alongside Agent Zero on the same host. Communicates with Agent Zero
via local A2A protocol (port 8001). No intermediate bridge needed.
"""
import asyncio
import json
import logging
import os
import time
from datetime import datetime, timezone
from typing import Any, Optional
import urllib.parse
try:
import httpx
except ImportError:
httpx = None
logger = logging.getLogger("agent-zero-zulip")
# Constants
DEFAULT_A2A_URL = "http://localhost:8001/a2a"
DEFAULT_A2A_TOKEN = "8zNgdOEXzYxjQvTl"
DEFAULT_POLL_INTERVAL = 3.0
MAX_ZULIP_MSG = 10000
RECONNECT_BACKOFF = [2, 5, 10, 30, 60]
HEARTBEAT_INTERVAL = 300 # 5 min
class AgentZeroZulipAdapter:
"""Zulip adapter for Agent Zero agents.
Connects to Zulip via event queue, forwards DMs and @mentions
to Agent Zero via local A2A protocol, and posts responses back.
"""
def __init__(self):
# Zulip config from env vars
self._site = (os.getenv("ZULIP_SITE") or "").rstrip("/")
self._email = os.getenv("ZULIP_EMAIL") or ""
self._api_key = os.getenv("ZULIP_API_KEY") or ""
self._agent_name = os.getenv("ZULIP_AGENT_NAME") or "kagentz"
self._stream = os.getenv("ZULIP_STREAM") or "agent-hub"
# A2A config
self._a2a_url = os.getenv("A2A_URL") or DEFAULT_A2A_URL
self._a2a_token = os.getenv("A2A_TOKEN") or DEFAULT_A2A_TOKEN
# State
self._auth_header = self._build_auth()
self._http_client: Optional[httpx.AsyncClient] = None
self._queue_id: Optional[str] = None
self._last_event_id: int = -1
self._bot_user_id: Optional[int] = None
self._bot_email: str = self._email
self._running = False
self._poll_task: Optional[asyncio.Task] = None
# Stats
self._messages_processed = 0
self._poll_count = 0
self._reconnects = 0
self._last_heartbeat = 0.0
self._last_event_time = time.time()
self._a2a_sessions: dict = {} # chat_id -> A2A context_id
def _build_auth(self) -> str:
import base64
token = base64.b64encode(f"{self._email}:{self._api_key}".encode()).decode()
return f"Basic {token}"
async def start(self):
"""Start the adapter — connect to Zulip and begin polling."""
if not all([self._site, self._email, self._api_key]):
logger.error("Missing Zulip credentials. Set ZULIP_SITE, ZULIP_EMAIL, ZULIP_API_KEY")
return False
if not httpx:
logger.error("httpx not installed")
return False
self._running = True
self._http_client = httpx.AsyncClient(timeout=30.0)
# Connect to Zulip
if not await self._connect():
return False
logger.info(f"Adapter started for {self._agent_name} ({self._email})")
return True
async def stop(self):
"""Stop the adapter."""
self._running = False
if self._poll_task:
self._poll_task.cancel()
try:
await self._poll_task
except asyncio.CancelledError:
pass
if self._http_client:
await self._http_client.aclose()
async def _connect(self) -> bool:
"""Register Zulip event queue."""
try:
resp = await self._api_call("POST", "/api/v1/register", data={
"event_types": '["message"]',
"apply_markdown": "true",
})
if not resp:
logger.error("Queue registration failed")
return False
self._queue_id = resp.get("queue_id")
self._last_event_id = resp.get("last_event_id", -1)
self._bot_user_id = resp.get("user_id")
# Resolve bot user_id if not provided by register
if self._bot_user_id is None:
me = await self._api_call("GET", "/api/v1/users/me")
if me:
self._bot_user_id = me.get("user_id")
logger.info(
f"Connected to Zulip as {self._email} "
f"(queue={self._queue_id}, bot_id={self._bot_user_id})"
)
# Start poll loop
self._poll_task = asyncio.create_task(self._poll_forever())
return True
except Exception as e:
logger.error(f"Connection failed: {e}")
return False
async def _poll_forever(self):
"""Main poll loop."""
backoff = 0
while self._running:
if not self._queue_id:
await asyncio.sleep(1)
continue
try:
events = await self._fetch_events()
self._poll_count += 1
for event in events:
try:
await self._process_event(event)
except Exception as e:
logger.warning(f"Event processing error: {e}")
backoff = 0
# Heartbeat
now = time.time()
if now - self._last_heartbeat > HEARTBEAT_INTERVAL:
self._last_heartbeat = now
silence = now - self._last_event_time
logger.info(
f"Heartbeat — polls={self._poll_count} "
f"processed={self._messages_processed} "
f"silence={silence:.0f}s reconnects={self._reconnects}"
)
except asyncio.CancelledError:
break
except Exception as e:
err = str(e)
if "BAD_EVENT_QUEUE_ID" in err:
logger.info("Queue expired, reconnecting...")
await self._reconnect()
continue
delay = RECONNECT_BACKOFF[min(backoff, len(RECONNECT_BACKOFF) - 1)]
backoff += 1
logger.warning(f"Poll error: {e}")
await asyncio.sleep(delay)
await asyncio.sleep(DEFAULT_POLL_INTERVAL)
async def _fetch_events(self) -> list:
"""Fetch events from Zulip queue. Raises on BAD_EVENT_QUEUE_ID."""
resp, status = await self._api_call("GET", "/api/v1/events", params={
"queue_id": self._queue_id,
"last_event_id": str(self._last_event_id),
"dont_block": "true",
}, return_status=True)
# Detect queue expiry
if status == 400:
raise RuntimeError("BAD_EVENT_QUEUE_ID: queue expired")
if not resp:
return []
events = resp.get("events", [])
for event in events:
eid = event.get("id", 0)
if eid > self._last_event_id:
self._last_event_id = eid
return [e for e in events if e.get("type") == "message"]
async def _reconnect(self):
"""Re-register event queue."""
self._queue_id = None
try:
resp = await self._api_call("POST", "/api/v1/register", data={
"event_types": '["message"]',
"apply_markdown": "true",
})
if resp:
self._queue_id = resp.get("queue_id")
self._last_event_id = resp.get("last_event_id", -1)
self._reconnects += 1
logger.info(f"Reconnected, queue={self._queue_id}")
except Exception as e:
logger.error(f"Reconnect failed: {e}")
async def _process_event(self, event: dict):
"""Process a Zulip message event."""
msg = event.get("message", {})
msg_type = msg.get("type", "")
sender_email = msg.get("sender_email", "")
sender_name = msg.get("sender_full_name", "Unknown")
sender_id = msg.get("sender_id")
content = msg.get("content", "")
# Ignore own messages
if sender_email == self._bot_email:
return
# Determine targeting
mentioned_users = msg.get("mentioned_users", []) or []
mentioned_ids = [u.get("user_id") for u in mentioned_users if isinstance(u, dict)]
is_dm = msg_type == "private"
is_mention = self._bot_user_id and self._bot_user_id in mentioned_ids
if is_dm:
self._messages_processed += 1
self._last_event_time = time.time()
logger.info(f"DM from {sender_name}: {content[:80]}...")
# Send typing indicator
await self._typing(sender_id, "start")
# Send placeholder
placeholder_id = await self._send_msg("private", str(sender_id),
":robot: _Processing your message..._")
# Forward to Agent Zero via A2A
response = await self._a2a_chat(sender_name, content)
# Stop typing
await self._typing(sender_id, "stop")
# Post response
if response and placeholder_id:
await self._edit_msg(placeholder_id, response[:MAX_ZULIP_MSG])
logger.info(f"Responded to {sender_name} ({len(response)} chars)")
elif response:
await self._send_msg("private", str(sender_id), response[:MAX_ZULIP_MSG])
async def _a2a_chat(self, sender_name: str, message: str) -> Optional[str]:
"""Send message to Agent Zero via local A2A protocol with session memory."""
try:
# Use sender_name as stable session key so conversation persists
session_id = f"zulip-{sender_name.lower().replace(' ', '-')}"
a2a_payload = {
"jsonrpc": "2.0",
"method": "tasks/send",
"params": {
"id": f"zulip-{int(time.time())}",
"message": {
"role": "user",
"parts": [{"text": f"[Zulip DM from {sender_name}]: {message}"}]
},
"metadata": {
"session_id": session_id
}
},
"id": 1
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self._a2a_token}",
}
async with httpx.AsyncClient(timeout=120.0) as client:
# Send the message to Agent Zero
resp = await client.post(
self._a2a_url,
json=a2a_payload,
headers=headers,
)
if resp.status_code != 200:
logger.error(f"A2A send failed: HTTP {resp.status_code}")
return f":warning: Failed to reach agent. Error: HTTP {resp.status_code}"
result = resp.json()
task_id = result.get("result", {}).get("id")
if not task_id:
logger.error("A2A: no task_id returned")
return ":warning: Agent did not create a task."
# Poll for completion
for _ in range(60): # max 60s wait
await asyncio.sleep(1)
status_resp = await client.post(
self._a2a_url,
json={
"jsonrpc": "2.0",
"method": "tasks/get",
"params": {"id": task_id},
"id": 2
},
headers=headers,
)
if status_resp.status_code != 200:
continue
status_data = status_resp.json()
result_data = status_data.get("result", {})
state = result_data.get("status", {}).get("state", "")
if state == "completed":
# Extract response text
return self._extract_a2a_response(result_data)
elif state in ("failed", "error"):
error_msg = result_data.get("status", {}).get("message", {}).get("text", "Unknown error")
return f":warning: Agent processing failed: {error_msg}"
return ":hourglass: Agent did not complete in time."
except httpx.TimeoutException:
return ":hourglass: Agent request timed out."
except Exception as e:
logger.error(f"A2A error: {e}")
return f":warning: Communication error: {type(e).__name__}"
def _extract_a2a_response(self, result: dict) -> str:
"""Extract assistant text from A2A response."""
history = result.get("history", [])
for msg in reversed(history):
if isinstance(msg, dict) and msg.get("role") == "assistant":
parts = msg.get("parts", [])
texts = [p.get("text", "") for p in parts if isinstance(p, dict)]
text = "\n".join(t for t in texts if t)
if text:
return text
artifacts = result.get("artifacts", [])
for artifact in reversed(artifacts):
if isinstance(artifact, dict):
text = artifact.get("text") or artifact.get("content") or ""
if text:
return text
return "(no response)"
# ── Zulip API ──
async def _api_call(self, method: str, path: str,
data: dict = None, params: dict = None,
return_status: bool = False) -> Any:
"""Make Zulip API call.
Returns: dict response body, or if return_status=True returns (dict, int)
"""
if not self._http_client:
return (None, 0) if return_status else None
url = f"{self._site}{path}"
headers = {"Authorization": self._auth_header}
try:
if method == "GET":
resp = await self._http_client.get(url, params=params, headers=headers)
elif method == "POST":
resp = await self._http_client.post(url, data=data, headers=headers)
elif method == "PATCH":
resp = await self._http_client.patch(url, data=data, headers=headers)
else:
return (None, 0) if return_status else None
if resp.status_code >= 400:
logger.debug(f"API {method} {path}: {resp.status_code}")
return (None, resp.status_code) if return_status else None
body = resp.json()
return (body, resp.status_code) if return_status else body
except Exception as e:
logger.debug(f"API error {method} {path}: {e}")
return (None, 0) if return_status else None
async def _send_msg(self, msg_type: str, to: str, content: str) -> Optional[int]:
"""Send a message to Zulip."""
payload = {"type": msg_type, "content": content}
if msg_type == "private":
payload["to"] = json.dumps([int(to)])
else:
payload["to"] = to
payload["subject"] = "general"
resp = await self._api_call("POST", "/api/v1/messages", data=payload)
if resp and resp.get("id"):
return resp["id"]
return None
async def _edit_msg(self, message_id: int, content: str):
"""Edit a Zulip message."""
await self._api_call("PATCH", f"/api/v1/messages/{message_id}",
data={"content": content})
async def _typing(self, user_id: int, op: str):
"""Send typing indicator."""
if not user_id:
return
to_data = json.dumps([user_id])
await self._api_call("POST", "/api/v1/typing",
data={"to": to_data, "op": op})
def run(self):
"""Synchronous entry point."""
asyncio.run(self._run_async())
async def _run_async(self):
try:
if await self.start():
# Keep running until stopped
while self._running:
await asyncio.sleep(1)
except asyncio.CancelledError:
pass
finally:
await self.stop()
# ── Entry point ──
def main():
"""CLI entry point."""
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(name)s] %(levelname)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
adapter = AgentZeroZulipAdapter()
try:
adapter.run()
except KeyboardInterrupt:
logger.info("Shutdown requested")
# Handle signals
import signal
shutdown = asyncio.Event()
def _handle_signal():
shutdown.set()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
loop.add_signal_handler(signal.SIGTERM, _handle_signal)
loop.add_signal_handler(signal.SIGINT, _handle_signal)
except NotImplementedError:
pass
async def _run():
if await adapter.start():
await shutdown.wait()
await adapter.stop()
try:
loop.run_until_complete(_run())
except KeyboardInterrupt:
pass
finally:
loop.close()
if __name__ == "__main__":
main()
+32 -7
View File
@@ -1,8 +1,20 @@
# Zulip Multi-Platform Agent Communication — Architecture
# Zulip Multi-Platform Agent Communication — Architecture (v2)
## Overview
A production-ready system enabling 6 AI agents across 3 platforms (Hermes Python, Agent Zero, pi TypeScript) to communicate through Zulip via dedicated per-agent bot users.
## Architecture (Hermes Native Plugin — Current)
As of v1.0.0, Hermes agents (Tanko, Mumuni, Koonimo, Koby) use the **Hermes native platform plugin**
at `~/.hermes/plugins/platforms/zulip/`. This replaces the old standalone systemd service.
Benefits of the native plugin:
- Extends `BasePlatformAdapter` — zero changes to Hermes core
- Auto-registers via `register(ctx)` at Gateway startup
- Direct session injection (no subprocess overhead)
- Leverages Gateway's built-in health checks, config, and error handling
- Unified logging with all other Hermes platform adapters
## Architecture Diagram
```
┌─────────────────────────────────────────────────────────────┐
@@ -66,18 +78,31 @@ User types: @all-bots status report
- **Swarm Development**: The "Swarm" refers to our collaborative development methodology where multiple agents/developers work on different components of the plugin simultaneously.
### Hermes (Python) — BasePlatformAdapter
- Path: `hermes-zulip-plugin/src/hermes_zulip/`
### Hermes (Python) — BasePlatformAdapter (CURRENT)
- Path: `plugins/platforms/zulip/`
- Deploy: `~/.hermes/plugins/platforms/zulip/`
- Implements: `BasePlatformAdapter` (Hermes Gateway)
- Config: `config.yaml` per-agent
- Entry point: `plugin.yaml` (Hermes manifest)
- Config: Hermes `config.yaml` under `platforms.zulip.extra` or env vars
- Entry point: `plugin.yaml` + `register(ctx)` (Hermes manifest)
- Versions: Gen 3 (v1.0.0) — 1,169 lines, 14/14 Success Criteria met
- Features: DM-first, placeholder→edit streaming, dedup, self-test, health stats, @all-bots resolution
### Agent Zero — A0 Plugin System
### Agent Zero — A0 Plugin System (LEGACY — to migrate)
- Path: `agent-zero-plugin/src/`
- Implements: Agent Zero plugin API
- Config: `config.yaml` per-agent
### pi (TypeScript) — pi Extension API
### pi (TypeScript) — pi Extension API (CURRENT)
- Path: `pi-zulip-extension/`
- Deploy: PM2-managed process
- Runs under `pi --mode rpc --session-id zulip-service`
- Active: abiba-bot only (ZULIP_EXTENSION_ACTIVE=true guard)
### Legacy Hermes Plugin (DEPRECATED)
- OLD path: `hermes-zulip-plugin/src/hermes_zulip/`
- OLD deploy: `/opt/hermes-zulip-plugin/` + systemd service
- Status: Replaced by `plugins/platforms/zulip/` native plugin as of v1.0.0
- Migration: See `hermes-zulip-plugin/DEPRECATED.md`
- Path: `pi-zulip-extension/src/`
- Implements: pi extension (TypeScript module in `~/.pi/agent/extensions/`)
- Config: `config.yaml` per-agent
+89
View File
@@ -0,0 +1,89 @@
# Gitea Actions Runner Setup
## Prerequisites
- Gitea admin access (root user or token with admin:actions scope)
- Docker on the runner host (CT 116 recommended — already has Docker)
## Step 1: Get Runner Registration Token
```bash
# On the Gitea host (CT 110) or via Gitea admin API:
ssh root@192.168.68.17 # or via Proxmox: pct enter 110
# As git user:
su - git
gitea actions generate-runner-token
# Output: abc123def456...
```
Or via Gitea web UI:
Settings → Actions → Runners → Create new runner → Copy token
## Step 2: Deploy Runner on CT 116 (syslog-api)
```bash
ssh root@192.168.68.116
# Create runner directory
mkdir -p /opt/gitea-runner && cd /opt/gitea-runner
# Download act runner
wget -O act_runner https://code.gitea.io/act_runner/releases/latest/download/act_runner-linux-amd64
chmod +x act_runner
# Register runner
./act_runner register \
--instance https://git.sysloggh.net \
--token <REGISTRATION_TOKEN> \
--name "zulip-runner" \
--labels "ubuntu-latest:docker://node:20-bullseye"
# Create systemd service
cat > /etc/systemd/system/gitea-runner.service << 'SERVICE'
[Unit]
Description=Gitea Actions Runner
After=docker.service
[Service]
ExecStart=/opt/gitea-runner/act_runner daemon
WorkingDirectory=/opt/gitea-runner
Restart=always
RestartSec=5
User=root
[Install]
WantedBy=multi-user.target
SERVICE
systemctl daemon-reload
systemctl enable --now gitea-runner
```
## Step 3: Verify
```bash
journalctl -u gitea-runner -f --no-pager
# Look for: "runner registered" or "listening for jobs"
# In Gitea UI: Settings → Actions → Runners → should show "zulip-runner" active
```
## Step 4: Test
Push a tag to trigger the pipeline:
```bash
git tag v1.1.0-rc.1 # Canary → Tanko only
git push origin v1.1.0-rc.1
# If canary passes:
git tag v1.1.0 # Production → all Hermes
git push origin v1.1.0
# For Agent Zero:
git tag az-v1.0.0 # Agent Zero → kagentz
git push origin az-v1.0.0
```
## Tag Convention
| Tag Pattern | Deploys To | Environment |
|------------|------------|-------------|
| `v*.*.*-rc.*` | Tanko only | `canary` |
| `v*.*.*` | All Hermes agents | `production` |
| `az-v*.*.*` | kagentz only | `agent-zero` |
+40
View File
@@ -0,0 +1,40 @@
# DEPRECATED — Hermes Zulip Plugin (Legacy)
**Status:** Deprecated as of 2026-06-25
**Replaced by:** `plugins/platforms/zulip/` (Hermes native platform plugin)
This directory contains the original standalone Hermes Zulip plugin that ran as a
systemd service with its own poll loop, subprocess agent invocation, and health
server.
## Why Deprecated
The new `plugins/platforms/zulip/` plugin is a proper Hermes platform plugin that:
- Extends `BasePlatformAdapter` from the Hermes Gateway
- Auto-registers via the Hermes plugin system (`register(ctx)`)
- Uses direct session injection (no subprocess overhead)
- Leverages Gateway's built-in health checks, config management, and error handling
- Requires zero changes to core Hermes code
## Migration
Remove the old systemd service and deploy the new plugin:
```bash
# 1. Stop old service
systemctl stop zulip-plugin
systemctl disable zulip-plugin
# 2. Install plugin to ~/.hermes/plugins/platforms/zulip/
cp -r plugins/platforms/zulip/ ~/.hermes/plugins/platforms/zulip/
# 3. Restart Hermes Gateway
hermes gateway restart
```
Config moves from:
- `/opt/hermes-zulip-plugin/config.yaml` → Hermes `config.yaml` under `platforms: zulip:`
- Or set `ZULIP_SITE`, `ZULIP_EMAIL`, `ZULIP_API_KEY` env vars
See `plugins/platforms/zulip/plugin.yaml` for configuration reference.
+61
View File
@@ -0,0 +1,61 @@
name: zulip-platform
label: Zulip
kind: platform
manifest_version: 1
version: 1.0.0
description: >
Zulip gateway adapter for Hermes Agent. Connects to a self-hosted or cloud
Zulip realm via the REST API — an event queue (register + long-poll) for
receiving and POST /messages for sending — relaying messages between Zulip
channels (streams)/topics and direct messages and the Hermes agent.
Supports topic-aware replies, native file uploads, channel-scoped
allowlists, @mention gating via message flags, and home-channel cron
delivery. No Zulip SDK required — uses aiohttp.
author: wachtelhund
requires_env:
- name: ZULIP_SITE
description: "Zulip realm URL (e.g. https://example.zulipchat.com)"
prompt: "Zulip site URL"
password: false
url: "https://zulip.com/api/api-keys"
- name: ZULIP_EMAIL
description: "Bot email address"
prompt: "Bot email"
password: false
- name: ZULIP_API_KEY
description: "Bot API key"
prompt: "Zulip bot API key"
password: true
optional_env:
- name: ZULIP_ALLOWED_USERS
description: "Comma-separated sender emails/ids allowed to talk to the bot"
prompt: "Allowed users (comma-separated)"
password: false
- name: ZULIP_ALLOW_ALL_USERS
description: "Allow any Zulip user to trigger the bot (dev only)"
prompt: "Allow all users? (true/false)"
password: false
- name: ZULIP_HOME_CHANNEL
description: "Default target for cron / notification delivery (e.g. general/hermes)"
prompt: "Home channel (stream/topic)"
password: false
- name: ZULIP_REQUIRE_MENTION
description: "Require @bot mention in channels (default true). Set false for free-response everywhere."
prompt: "Require @mention? (true/false)"
password: false
- name: ZULIP_FREE_RESPONSE_CHANNELS
description: "Comma-separated stream ids/names where @mention is not required."
prompt: "Free-response streams (comma-separated)"
password: false
- name: ZULIP_ALLOWED_CHANNELS
description: "If set, the bot only responds in these stream ids/names (whitelist)."
prompt: "Allowed streams (comma-separated)"
password: false
- name: ZULIP_ALL_PUBLIC_STREAMS
description: "Subscribe the event queue to all public streams (default false)."
prompt: "Listen to all public streams? (true/false)"
password: false
- name: ZULIP_DEFAULT_TOPIC
description: "Topic used when a target has none (default 'general')."
prompt: "Default topic"
password: false
+13
View File
@@ -0,0 +1,13 @@
"""Zulip gateway plugin for Hermes Agent."""
try:
# Normal path: Hermes loads this as a package (``hermes_plugins.…``).
from .adapter import register
except ImportError: # pragma: no cover
# Imported as a top-level module (e.g. by a test collector that treats
# the repo root as a package). Fall back to an absolute import.
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
from adapter import register
__all__ = ["register"]
+943
View File
@@ -0,0 +1,943 @@
"""Zulip gateway adapter for Hermes Agent.
Connects to a Zulip realm (self-hosted or zulipchat.com) and relays messages
between Zulip channels (streams) / direct messages and the Hermes agent.
Both transports use Zulip's REST API over ``aiohttp`` (already a Hermes
dependency — no ``zulip`` SDK required):
* **Receiving** — register an event queue (``POST /api/v1/register``) and
long-poll it (``GET /api/v1/events``). The queue delivers ``message``
events; the bot's ``mentioned`` flag drives channel gating. A
``BAD_EVENT_QUEUE_ID`` (expired queue) transparently re-registers.
* **Sending** — ``POST /api/v1/messages`` (stream + topic, or direct) and
``POST /api/v1/user_uploads`` for file attachments. REST is also used
out-of-process by cron delivery via :func:`_standalone_send`.
Authentication is HTTP Basic with the bot's email + API key.
Chat ids are encoded so replies route back to the right place:
s:<stream_id>:<topic> a stream message under a topic (chat_type=channel)
d:<user_id>,<user_id>,… a direct message (chat_type=dm)
Environment variables (set in ``~/.hermes/.env``):
ZULIP_SITE Realm URL (e.g. https://example.zulipchat.com)
ZULIP_EMAIL Bot email
ZULIP_API_KEY Bot API key
Optional:
ZULIP_ALLOWED_USERS Comma-separated sender emails/ids allowed to talk to the bot
ZULIP_ALLOW_ALL_USERS Allow any user (dev only)
ZULIP_HOME_CHANNEL Default target for cron delivery (e.g. "general/hermes" or "s:5:hermes")
ZULIP_REQUIRE_MENTION Require @bot mention in channels (default true)
ZULIP_FREE_RESPONSE_CHANNELS Stream ids/names where @mention is not required
ZULIP_ALLOWED_CHANNELS If set, the bot only responds in these stream ids/names
ZULIP_ALL_PUBLIC_STREAMS Subscribe the queue to all public streams (default false)
ZULIP_DEFAULT_TOPIC Topic used when a target has none (default "general")
"""
from __future__ import annotations
import asyncio
import json
import logging
import os
import re
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
from gateway.config import Platform, PlatformConfig
from gateway.platforms.helpers import MessageDeduplicator
from gateway.platforms.base import (
BasePlatformAdapter,
MessageEvent,
MessageType,
SendResult,
)
logger = logging.getLogger(__name__)
# Identifier used in config.yaml (gateway.platforms.zulip) and by
# ``Platform("zulip")``. Registered at import-time via register().
PLATFORM_NAME = "zulip"
# Zulip's hard message limit is 10000 chars; 9000 leaves headroom for the
# chunk indicator and any markdown the agent adds.
MAX_MESSAGE_LENGTH = 9000
# Long-poll client timeout. Zulip holds GET /events open and emits a
# heartbeat well within this window, so a timeout just means "re-poll".
_LONGPOLL_TIMEOUT = 90.0
_RECONNECT_BASE_DELAY = 2.0
_RECONNECT_MAX_DELAY = 60.0
_RECONNECT_JITTER = 0.2
def check_zulip_requirements() -> bool:
"""Return True if the Zulip adapter can be used."""
site = os.getenv("ZULIP_SITE", "") or os.getenv("ZULIP_URL", "")
email = os.getenv("ZULIP_EMAIL", "")
api_key = os.getenv("ZULIP_API_KEY", "")
if not site:
logger.debug("Zulip: ZULIP_SITE not set")
return False
if not email or not api_key:
logger.debug("Zulip: ZULIP_EMAIL and ZULIP_API_KEY required")
return False
try:
import aiohttp # noqa: F401
return True
except ImportError:
logger.warning("Zulip: aiohttp not installed")
return False
def _parse_target(chat_id: str, default_topic: str = "general") -> Tuple[str, Any, Optional[str]]:
"""Decode a chat id / home-channel string into a send target.
Returns ``(kind, to, topic)`` where *kind* is ``"stream"`` or
``"direct"``. Accepted forms:
d:1,2,3 -> ("direct", [1, 2, 3], None)
s:<id>:<topic> -> ("stream", <id-or-name>, <topic>)
<stream>/<topic> -> ("stream", <stream name>, <topic>) (friendly)
<stream> -> ("stream", <stream name>, default_topic)
"""
cid = (chat_id or "").strip()
if cid.startswith("d:"):
ids: List[Any] = []
for part in cid[2:].split(","):
part = part.strip()
if not part:
continue
ids.append(int(part) if part.isdigit() else part)
return "direct", ids, None
if cid.startswith("s:"):
rest = cid[2:]
stream, _, topic = rest.partition(":")
stream = stream.strip()
to: Any = int(stream) if stream.isdigit() else stream
return "stream", to, (topic or default_topic)
if "/" in cid:
stream, topic = cid.split("/", 1)
return "stream", stream.strip(), (topic.strip() or default_topic)
return "stream", cid, default_topic
class ZulipAdapter(BasePlatformAdapter):
"""Gateway adapter for Zulip (self-hosted or zulipchat.com)."""
def __init__(self, config: PlatformConfig):
super().__init__(config, Platform(PLATFORM_NAME))
extra = config.extra or {}
self._site: str = (
extra.get("site")
or os.getenv("ZULIP_SITE", "")
or os.getenv("ZULIP_URL", "")
).rstrip("/")
self._email: str = extra.get("email") or os.getenv("ZULIP_EMAIL", "")
self._api_key: str = config.token or os.getenv("ZULIP_API_KEY", "")
self._bot_user_id: Optional[int] = None
self._bot_email: str = ""
self._bot_full_name: str = ""
self._session: Any = None
self._queue_id: Optional[str] = None
self._last_event_id: int = -1
self._poll_task: Optional[asyncio.Task] = None
self._closing = False
self._default_topic: str = (
extra.get("default_topic") or os.getenv("ZULIP_DEFAULT_TOPIC", "general")
)
self._dedup = MessageDeduplicator()
# ------------------------------------------------------------------
# HTTP helpers
# ------------------------------------------------------------------
def _auth(self):
import aiohttp
return aiohttp.BasicAuth(self._email, self._api_key)
async def _api_get(self, path: str, params: Optional[Dict[str, Any]] = None, timeout: float = 30.0) -> Dict[str, Any]:
import aiohttp
url = f"{self._site}/api/v1/{path.lstrip('/')}"
try:
async with self._session.get(
url, params=params, auth=self._auth(),
timeout=aiohttp.ClientTimeout(total=timeout),
) as resp:
data = await resp.json()
if resp.status >= 400:
logger.debug("Zulip GET %s -> %s: %s", path, resp.status, str(data)[:200])
data.setdefault("_http_status", resp.status)
return data
# aiohttp raises a bare asyncio.TimeoutError (NOT a ClientError) on
# timeout — catch both so callers always get the error dict, never an
# unhandled exception.
except (aiohttp.ClientError, asyncio.TimeoutError) as exc:
logger.error("Zulip GET %s network error: %s", path, exc)
return {"result": "error", "msg": str(exc)}
async def _api_post(self, path: str, payload: Dict[str, Any]) -> Dict[str, Any]:
import aiohttp
url = f"{self._site}/api/v1/{path.lstrip('/')}"
try:
async with self._session.post(
url, data=payload, auth=self._auth(),
timeout=aiohttp.ClientTimeout(total=30),
) as resp:
data = await resp.json()
if resp.status >= 400:
logger.error("Zulip POST %s -> %s: %s", path, resp.status, str(data)[:200])
return data
except (aiohttp.ClientError, asyncio.TimeoutError) as exc:
logger.error("Zulip POST %s network error: %s", path, exc)
return {"result": "error", "msg": str(exc)}
async def _api_delete(self, path: str, params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
import aiohttp
url = f"{self._site}/api/v1/{path.lstrip('/')}"
try:
async with self._session.delete(
url, params=params, auth=self._auth(),
timeout=aiohttp.ClientTimeout(total=15),
) as resp:
return await resp.json()
except (aiohttp.ClientError, asyncio.TimeoutError) as exc:
logger.debug("Zulip DELETE %s network error: %s", path, exc)
return {"result": "error", "msg": str(exc)}
async def _upload(self, file_data: bytes, filename: str, content_type: str) -> Optional[str]:
"""Upload a file, returning its absolute URL (or None)."""
import aiohttp
url = f"{self._site}/api/v1/user_uploads"
form = aiohttp.FormData()
form.add_field("file", file_data, filename=filename, content_type=content_type)
try:
async with self._session.post(
url, data=form, auth=self._auth(),
timeout=aiohttp.ClientTimeout(total=60),
) as resp:
data = await resp.json()
if resp.status >= 400:
logger.error("Zulip upload -> %s: %s", resp.status, str(data)[:200])
return None
rel = data.get("url") or data.get("uri")
if not rel:
return None
return rel if rel.startswith("http") else f"{self._site}{rel}"
except (aiohttp.ClientError, asyncio.TimeoutError) as exc:
logger.error("Zulip upload network error: %s", exc)
return None
# ------------------------------------------------------------------
# Required overrides
# ------------------------------------------------------------------
async def connect(self, is_reconnect: bool = False) -> bool:
import aiohttp
if not self._site or not self._email or not self._api_key:
logger.error("Zulip: ZULIP_SITE, ZULIP_EMAIL and ZULIP_API_KEY must be set")
return False
self._session = aiohttp.ClientSession()
self._closing = False
me = await self._api_get("users/me")
if me.get("result") != "success" or "user_id" not in me:
logger.error(
"Zulip: failed to authenticate — check ZULIP_SITE, ZULIP_EMAIL "
"and ZULIP_API_KEY (%s)", str(me.get("msg", ""))[:160],
)
await self._session.close()
return False
self._bot_user_id = me["user_id"]
self._bot_email = me.get("email", self._email)
self._bot_full_name = me.get("full_name", "")
logger.info(
"Zulip: authenticated as %s (#%s) on %s",
self._bot_full_name or self._bot_email, self._bot_user_id, self._site,
)
self._poll_task = asyncio.create_task(self._poll_loop())
self._mark_connected()
return True
async def disconnect(self) -> None:
self._closing = True
if self._poll_task and not self._poll_task.done():
self._poll_task.cancel()
try:
await self._poll_task
except (asyncio.CancelledError, Exception):
pass
# Best-effort: release the server-side event queue. Zulip deletes a
# queue via DELETE /api/v1/events?queue_id=… (there is no POST form).
if self._queue_id and self._session and not self._session.closed:
try:
await self._api_delete("events", {"queue_id": self._queue_id})
except Exception:
pass
if self._session and not self._session.closed:
await self._session.close()
logger.info("Zulip: disconnected")
async def send(
self,
chat_id: str,
content: str,
reply_to: Optional[str] = None,
metadata: Optional[Dict[str, Any]] = None,
) -> SendResult:
if not content:
return SendResult(success=True)
kind, to, topic = _parse_target(chat_id, self._default_topic)
formatted = self.format_message(content)
chunks = self.truncate_message(formatted, MAX_MESSAGE_LENGTH)
last_id = None
for chunk in chunks:
payload: Dict[str, Any] = {"content": chunk}
if kind == "direct":
# "private" is the backward-compatible alias accepted by every
# Zulip version (the newer "direct" alias only landed in 7.0).
payload["type"] = "private"
payload["to"] = json.dumps(to)
else:
payload["type"] = "stream"
payload["to"] = str(to)
payload["topic"] = topic or self._default_topic
data = await self._api_post("messages", payload)
if data.get("result") != "success":
return SendResult(success=False, error=str(data.get("msg", "send failed")))
last_id = data.get("id")
return SendResult(success=True, message_id=str(last_id) if last_id else None)
async def get_chat_info(self, chat_id: str) -> Dict[str, Any]:
kind, to, topic = _parse_target(chat_id, self._default_topic)
if kind == "direct":
return {"name": "Direct message", "type": "dm", "chat_id": chat_id}
name = f"{to} > {topic}" if topic else str(to)
return {"name": name, "type": "channel", "chat_id": chat_id}
# ------------------------------------------------------------------
# Optional overrides — files are uploaded then linked into a message
# ------------------------------------------------------------------
async def send_image(
self,
chat_id: str,
image_url: str,
caption: Optional[str] = None,
reply_to: Optional[str] = None,
metadata: Optional[Dict[str, Any]] = None,
) -> SendResult:
return await self._send_url_as_file(chat_id, image_url, caption, "image")
async def send_image_file(
self,
chat_id: str,
image_path: str,
caption: Optional[str] = None,
reply_to: Optional[str] = None,
metadata: Optional[Dict[str, Any]] = None,
) -> SendResult:
return await self._send_local_file(chat_id, image_path, caption)
async def send_document(
self,
chat_id: str,
file_path: str,
caption: Optional[str] = None,
file_name: Optional[str] = None,
reply_to: Optional[str] = None,
metadata: Optional[Dict[str, Any]] = None,
) -> SendResult:
return await self._send_local_file(chat_id, file_path, caption, file_name)
async def send_voice(
self,
chat_id: str,
audio_path: str,
caption: Optional[str] = None,
reply_to: Optional[str] = None,
metadata: Optional[Dict[str, Any]] = None,
) -> SendResult:
return await self._send_local_file(chat_id, audio_path, caption)
async def send_video(
self,
chat_id: str,
video_path: str,
caption: Optional[str] = None,
reply_to: Optional[str] = None,
metadata: Optional[Dict[str, Any]] = None,
) -> SendResult:
return await self._send_local_file(chat_id, video_path, caption)
def format_message(self, content: str) -> str:
"""Zulip renders CommonMark.
Convert inline image markdown into bare links — Zulip auto-previews
image URLs, and files are uploaded separately.
"""
return re.sub(r"!\[([^\]]*)\]\(([^)]+)\)", r"\2", content)
# ------------------------------------------------------------------
# File helpers
# ------------------------------------------------------------------
async def _send_url_as_file(
self, chat_id: str, url: str, caption: Optional[str], kind: str = "file",
) -> SendResult:
from tools.url_safety import is_safe_url
if not is_safe_url(url):
logger.warning("Zulip: blocked unsafe URL (SSRF protection)")
return await self.send(chat_id, f"{caption or ''}\n{url}".strip())
import aiohttp
fname = url.rsplit("/", 1)[-1].split("?")[0] or f"{kind}.png"
try:
async with self._session.get(url, timeout=aiohttp.ClientTimeout(total=30)) as resp:
if resp.status >= 400:
return await self.send(chat_id, f"{caption or ''}\n{url}".strip())
file_data = await resp.read()
ct = resp.content_type or "application/octet-stream"
except (aiohttp.ClientError, asyncio.TimeoutError) as exc:
logger.warning("Zulip: failed to download %s: %s", url[:80], exc)
return await self.send(chat_id, f"{caption or ''}\n{url}".strip())
return await self._upload_and_send(chat_id, file_data, fname, ct, caption, fallback=url)
async def _send_local_file(
self, chat_id: str, file_path: str, caption: Optional[str], file_name: Optional[str] = None,
) -> SendResult:
import mimetypes
p = Path(file_path)
if not p.exists():
logger.warning("Zulip: local file not found, skipping: %s", file_path)
return SendResult(success=True, message_id=None)
fname = file_name or p.name
ct = mimetypes.guess_type(fname)[0] or "application/octet-stream"
return await self._upload_and_send(chat_id, p.read_bytes(), fname, ct, caption)
async def _upload_and_send(
self, chat_id: str, data: bytes, fname: str, ct: str,
caption: Optional[str], fallback: Optional[str] = None,
) -> SendResult:
url = await self._upload(data, fname, ct)
if not url:
if fallback:
return await self.send(chat_id, f"{caption or ''}\n{fallback}".strip())
return SendResult(success=False, error="File upload failed")
body = f"{caption + chr(10) if caption else ''}[{fname}]({url})"
return await self.send(chat_id, body)
# ------------------------------------------------------------------
# Event queue (register + long-poll)
# ------------------------------------------------------------------
async def _register_queue(self) -> bool:
all_public = os.getenv("ZULIP_ALL_PUBLIC_STREAMS", "").lower() in {"1", "true", "yes", "on"}
payload = {
"event_types": json.dumps(["message"]),
"apply_markdown": "false",
"all_public_streams": "true" if all_public else "false",
}
data = await self._api_post("register", payload)
if data.get("result") != "success" or "queue_id" not in data:
logger.error("Zulip: failed to register event queue: %s", str(data.get("msg", ""))[:160])
return False
self._queue_id = data["queue_id"]
self._last_event_id = data.get("last_event_id", -1)
logger.info("Zulip: event queue registered (%s)", self._queue_id)
return True
async def _poll_loop(self) -> None:
delay = _RECONNECT_BASE_DELAY
while not self._closing:
try:
if not self._queue_id:
if not await self._register_queue():
raise RuntimeError("queue registration failed")
await self._poll_once()
delay = _RECONNECT_BASE_DELAY
except asyncio.CancelledError:
return
except Exception as exc:
if self._closing:
return
logger.warning("Zulip poll error: %s — retrying in %.0fs", exc, delay)
import random
await asyncio.sleep(delay + delay * _RECONNECT_JITTER * random.random())
delay = min(delay * 2, _RECONNECT_MAX_DELAY)
async def _get_events(self) -> Optional[Dict[str, Any]]:
"""Long-poll ``GET /events``.
Returns the parsed response, or ``None`` on the expected long-poll
timeout (no events within the window — caller just re-polls). Kept
separate from ``_api_get`` so the long-poll timeout isn't swallowed
into an error dict the way ordinary calls need.
"""
import aiohttp
params = {"queue_id": self._queue_id, "last_event_id": self._last_event_id}
try:
async with self._session.get(
f"{self._site}/api/v1/events", params=params, auth=self._auth(),
timeout=aiohttp.ClientTimeout(total=_LONGPOLL_TIMEOUT),
) as resp:
return await resp.json()
except asyncio.TimeoutError:
return None
except aiohttp.ClientError as exc:
raise RuntimeError(f"events poll network error: {exc}")
async def _poll_once(self) -> None:
data = await self._get_events()
if data is None:
return # long-poll window elapsed — just poll again
if data.get("result") != "success":
if data.get("code") == "BAD_EVENT_QUEUE_ID":
logger.info("Zulip: event queue expired — re-registering")
self._queue_id = None
return
# Transient server error — let the loop back off.
raise RuntimeError(str(data.get("msg", "events poll failed")))
for event in data.get("events", []):
eid = event.get("id")
if isinstance(eid, int):
self._last_event_id = max(self._last_event_id, eid)
if event.get("type") == "message":
# Guard each event so one failing message can't drop the rest
# of the batch. last_event_id is already advanced above, so a
# dropped message is not reprocessed on the next poll.
try:
await self._handle_message_event(event)
except Exception:
logger.exception("Zulip: error handling message event %s", eid)
async def _handle_message_event(self, event: Dict[str, Any]) -> None:
message = event.get("message") or {}
flags = event.get("flags") or []
sender_id = message.get("sender_id")
# Ignore our own messages (Zulip echoes them back on the queue).
if sender_id is not None and sender_id == self._bot_user_id:
return
msg_id = message.get("id")
if msg_id is None or self._dedup.is_duplicate(str(msg_id)):
return
mtype = message.get("type") # "stream" or "private"
content = message.get("content", "") or ""
sender_name = message.get("sender_full_name", "") or message.get("sender_email", "")
if mtype == "private":
chat_type = "dm"
recipients = message.get("display_recipient") or []
user_ids = sorted(
int(r["id"]) for r in recipients
if isinstance(r, dict) and "id" in r
)
chat_id = "d:" + ",".join(str(u) for u in user_ids)
topic = None
else:
chat_type = "channel"
stream_id = message.get("stream_id")
topic = message.get("subject", "") or message.get("topic", "")
chat_id = f"s:{stream_id}:{topic}"
# Mention-gating for streams.
stream_name = message.get("display_recipient", "")
extra = self.config.extra or {}
allowed_raw = extra.get("allowed_channels")
if allowed_raw is None:
allowed_raw = os.getenv("ZULIP_ALLOWED_CHANNELS", "")
allowed = _split_csv(allowed_raw)
if allowed and str(stream_id) not in allowed and str(stream_name) not in allowed:
return
require_mention = os.getenv("ZULIP_REQUIRE_MENTION", "true").lower() not in {"false", "0", "no"}
free = _split_csv(os.getenv("ZULIP_FREE_RESPONSE_CHANNELS", ""))
is_free = str(stream_id) in free or str(stream_name) in free
has_mention = "mentioned" in flags or "wildcard_mentioned" in flags
if require_mention and not is_free and not has_mention:
return
# Strip the bot's @-mention so the agent sees clean input.
if self._bot_full_name:
content = re.sub(
r"@\*\*" + re.escape(self._bot_full_name) + r"\*\*", "", content,
).strip()
msg_type = MessageType.COMMAND if content.startswith("/") else MessageType.TEXT
media_urls: List[str] = []
media_types: List[str] = []
await self._extract_inline_media(content, media_urls, media_types)
await self._extract_event_attachments(message, media_urls, media_types)
if media_types and msg_type == MessageType.TEXT:
if any(m.startswith("image/") for m in media_types):
msg_type = MessageType.PHOTO
elif any(m.startswith("audio/") for m in media_types):
msg_type = MessageType.VOICE
else:
msg_type = MessageType.DOCUMENT
source = self.build_source(
chat_id=chat_id,
chat_type=chat_type,
user_id=str(sender_id) if sender_id is not None else None,
user_name=sender_name,
chat_topic=topic,
)
from gateway.platforms.base import resolve_channel_prompt
channel_prompt = resolve_channel_prompt(self.config.extra, chat_id, None)
await self.handle_message(MessageEvent(
text=content,
message_type=msg_type,
source=source,
raw_message=message,
message_id=str(msg_id),
media_urls=media_urls or None,
media_types=media_types or None,
channel_prompt=channel_prompt,
))
async def _extract_event_attachments(
self, message: Dict[str, Any], media_urls: List[str], media_types: List[str],
) -> None:
"""Download files from Zulip's ``message.attachments`` field (UI file
uploads that Zulip sends as structured event data rather than inline
markdown links)."""
import aiohttp
atts = message.get("attachments") or []
for att in atts:
path_id = att.get("path_id")
fname = att.get("name") or "file"
if not path_id:
continue
dl_url = f"{self._site}/api/v1/user_uploads/{path_id}"
try:
async with self._session.get(
dl_url, auth=self._auth(),
timeout=aiohttp.ClientTimeout(total=30),
) as resp:
if resp.status >= 400:
logger.debug("Zulip: event attachment %s -> %s", fname, resp.status)
continue
data = await resp.read()
mime = resp.content_type or "application/octet-stream"
except (aiohttp.ClientError, asyncio.TimeoutError):
logger.debug("Zulip: failed to download event attachment %s", fname)
continue
await self._cache_attachment(data, fname, mime, media_urls, media_types)
async def _cache_attachment(
self, data: bytes, fname: str, mime: str,
media_urls: List[str], media_types: List[str],
) -> None:
"""Cache a downloaded attachment into Hermes media storage."""
from gateway.platforms.base import (
cache_image_from_bytes, cache_audio_from_bytes, cache_document_from_bytes,
)
ext = Path(fname).suffix
try:
if mime.startswith("image/"):
media_urls.append(cache_image_from_bytes(data, ext or ".png"))
media_types.append(mime)
elif mime.startswith("audio/"):
media_urls.append(cache_audio_from_bytes(data, ext or ".ogg"))
media_types.append(mime)
else:
media_urls.append(cache_document_from_bytes(data, fname))
media_types.append(mime)
except Exception as exc:
logger.warning("Zulip: failed to cache attachment %s: %s", fname, exc)
async def _extract_inline_media(
self, content: str, media_urls: List[str], media_types: List[str],
) -> None:
"""Download Zulip ``/user_uploads/...`` attachments referenced in a
message so vision/transcription tools can read them locally."""
import aiohttp
for rel in re.findall(r"\]\((/user_uploads/[^)]+)\)", content):
dl_url = f"{self._site}{rel}"
try:
async with self._session.get(
dl_url, auth=self._auth(),
timeout=aiohttp.ClientTimeout(total=30),
) as resp:
if resp.status >= 400:
continue
data = await resp.read()
mime = resp.content_type or "application/octet-stream"
except (aiohttp.ClientError, asyncio.TimeoutError):
continue
fname = rel.rsplit("/", 1)[-1] or "file"
await self._cache_attachment(data, fname, mime, media_urls, media_types)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _split_csv(value: Any) -> set:
if isinstance(value, list):
return {str(v).strip() for v in value if str(v).strip()}
return {v.strip() for v in str(value or "").split(",") if v.strip()}
# ---------------------------------------------------------------------------
# Out-of-process cron delivery (standalone REST send)
# ---------------------------------------------------------------------------
async def _standalone_send(
pconfig,
chat_id: str,
message: str,
*,
thread_id: Optional[str] = None,
media_files: Optional[list] = None,
force_document: bool = False,
) -> Dict[str, Any]:
"""Send via the Zulip REST API without a live gateway adapter.
Used by ``tools/send_message_tool`` for cron jobs running outside the
gateway process. Reads ``ZULIP_API_KEY`` from ``pconfig.token`` (env
fallback); site + email come from ``pconfig.extra`` or env.
"""
try:
import aiohttp
except ImportError:
return {"error": "aiohttp not installed. Run: pip install aiohttp"}
extra = getattr(pconfig, "extra", {}) or {}
site = (extra.get("site") or os.getenv("ZULIP_SITE", "") or os.getenv("ZULIP_URL", "")).rstrip("/")
email = (extra.get("email") or os.getenv("ZULIP_EMAIL", "")).strip()
api_key = (getattr(pconfig, "token", None) or os.getenv("ZULIP_API_KEY", "")).strip()
if not site or not email or not api_key:
return {"error": "Zulip standalone send: ZULIP_SITE, ZULIP_EMAIL and ZULIP_API_KEY must be set"}
default_topic = extra.get("default_topic") or os.getenv("ZULIP_DEFAULT_TOPIC", "general")
kind, to, topic = _parse_target(chat_id, default_topic)
try:
from gateway.platforms.base import resolve_proxy_url, proxy_kwargs_for_aiohttp
proxy = resolve_proxy_url(platform_env_var="ZULIP_PROXY")
sess_kw, req_kw = proxy_kwargs_for_aiohttp(proxy)
auth = aiohttp.BasicAuth(email, api_key)
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=60), **sess_kw) as session:
body = message
# Upload any media and append links to the message body.
for media in (media_files or []):
file_path = media.get("path") if isinstance(media, dict) else media
if not file_path or not os.path.exists(file_path):
continue
form = aiohttp.FormData()
with open(file_path, "rb") as fh:
form.add_field("file", fh.read(), filename=os.path.basename(file_path))
async with session.post(
f"{site}/api/v1/user_uploads", data=form, auth=auth, **req_kw,
) as up:
if up.status >= 400:
continue
up_data = await up.json()
rel = up_data.get("url") or up_data.get("uri")
if rel:
full = rel if rel.startswith("http") else f"{site}{rel}"
body = f"{body}\n[{os.path.basename(file_path)}]({full})".strip()
payload: Dict[str, Any] = {"content": body or "(no content)"}
if kind == "direct":
# "private" is the backward-compatible alias accepted by every
# Zulip version (the newer "direct" alias only landed in 7.0).
payload["type"] = "private"
payload["to"] = json.dumps(to)
else:
payload["type"] = "stream"
payload["to"] = str(to)
payload["topic"] = topic or default_topic
async with session.post(
f"{site}/api/v1/messages", data=payload, auth=auth, **req_kw,
) as resp:
data = await resp.json()
if data.get("result") != "success":
return {"error": f"Zulip API error: {str(data.get('msg', ''))[:300]}"}
return {
"success": True,
"platform": "zulip",
"chat_id": chat_id,
"message_id": data.get("id"),
}
except aiohttp.ClientError as exc:
return {"error": f"Zulip send failed (network): {exc}"}
except Exception as exc: # noqa: BLE001
return {"error": f"Zulip send failed: {exc}"}
# ---------------------------------------------------------------------------
# Interactive setup wizard
# ---------------------------------------------------------------------------
def interactive_setup() -> None:
"""Guide the user through Zulip bot setup."""
from hermes_cli.config import get_env_value, save_env_value
from hermes_cli.cli_output import (
prompt, prompt_yes_no, print_header, print_info, print_success,
)
print_header("Zulip")
if get_env_value("ZULIP_API_KEY") and get_env_value("ZULIP_EMAIL"):
print_info("Zulip: already configured")
if not prompt_yes_no("Reconfigure Zulip?", False):
return
print_info("Create a bot in Zulip: Settings → Personal → Bots → Add a new bot")
print_info(" Choose 'Generic bot', then copy its email + API key.")
print()
site = prompt("Zulip site URL (e.g. https://example.zulipchat.com)")
if site:
save_env_value("ZULIP_SITE", site.rstrip("/"))
email = prompt("Bot email")
if email:
save_env_value("ZULIP_EMAIL", email)
api_key = prompt("Bot API key", password=True)
if not api_key:
return
save_env_value("ZULIP_API_KEY", api_key)
print_success("Zulip credentials saved")
print()
print_info("🔒 Security: restrict who can use your bot")
allowed = prompt("Allowed sender emails/ids (comma-separated, empty for open access)")
if allowed:
save_env_value("ZULIP_ALLOWED_USERS", allowed.replace(" ", ""))
print_success("Zulip allowlist configured")
else:
print_info("⚠️ No allowlist set — anyone who can message the bot can use it!")
print()
print_info("📬 Home channel: where Hermes delivers cron results / notifications.")
print_info(" Form: 'stream-name/topic' (e.g. general/hermes).")
home = prompt("Home channel (leave empty to set later with /set-home)")
if home:
save_env_value("ZULIP_HOME_CHANNEL", home)
# ---------------------------------------------------------------------------
# YAML -> env config bridge / connected probe
# ---------------------------------------------------------------------------
def _apply_yaml_config(yaml_cfg: dict, z_cfg: dict) -> dict | None:
extras: Dict[str, Any] = {}
site = z_cfg.get("site") or z_cfg.get("url")
if site:
extras["site"] = str(site).rstrip("/")
if not os.getenv("ZULIP_SITE"):
os.environ["ZULIP_SITE"] = str(site).rstrip("/")
if z_cfg.get("email"):
extras["email"] = str(z_cfg["email"])
if not os.getenv("ZULIP_EMAIL"):
os.environ["ZULIP_EMAIL"] = str(z_cfg["email"])
if z_cfg.get("default_topic"):
extras["default_topic"] = str(z_cfg["default_topic"])
if "require_mention" in z_cfg and not os.getenv("ZULIP_REQUIRE_MENTION"):
os.environ["ZULIP_REQUIRE_MENTION"] = str(z_cfg["require_mention"]).lower()
if "all_public_streams" in z_cfg and not os.getenv("ZULIP_ALL_PUBLIC_STREAMS"):
os.environ["ZULIP_ALL_PUBLIC_STREAMS"] = str(z_cfg["all_public_streams"]).lower()
for key, env in (
("free_response_channels", "ZULIP_FREE_RESPONSE_CHANNELS"),
("allowed_channels", "ZULIP_ALLOWED_CHANNELS"),
):
val = z_cfg.get(key)
if val is not None and not os.getenv(env):
if isinstance(val, list):
val = ",".join(str(v) for v in val)
os.environ[env] = str(val)
return extras or None
def _env_enablement() -> Optional[dict]:
site = os.getenv("ZULIP_SITE", "") or os.getenv("ZULIP_URL", "")
if not site:
return None
extras: Dict[str, Any] = {"site": site.rstrip("/")}
if os.getenv("ZULIP_EMAIL"):
extras["email"] = os.getenv("ZULIP_EMAIL")
return extras
def _is_connected(config) -> bool:
import hermes_cli.gateway as gateway_mod
site = (gateway_mod.get_env_value("ZULIP_SITE") or gateway_mod.get_env_value("ZULIP_URL") or "").strip()
email = (gateway_mod.get_env_value("ZULIP_EMAIL") or "").strip()
api_key = (gateway_mod.get_env_value("ZULIP_API_KEY") or "").strip()
return bool(site and email and api_key)
# ---------------------------------------------------------------------------
# Plugin registration entry point
# ---------------------------------------------------------------------------
def _build_adapter(config):
return ZulipAdapter(config)
def register(ctx) -> None:
"""Plugin entry point — called by the Hermes plugin system."""
ctx.register_platform(
name=PLATFORM_NAME,
label="Zulip",
adapter_factory=_build_adapter,
check_fn=check_zulip_requirements,
is_connected=_is_connected,
required_env=["ZULIP_SITE", "ZULIP_EMAIL", "ZULIP_API_KEY"],
install_hint="pip install aiohttp",
setup_fn=interactive_setup,
apply_yaml_config_fn=_apply_yaml_config,
env_enablement_fn=_env_enablement,
allowed_users_env="ZULIP_ALLOWED_USERS",
allow_all_env="ZULIP_ALLOW_ALL_USERS",
cron_deliver_env_var="ZULIP_HOME_CHANNEL",
standalone_sender_fn=_standalone_send,
max_message_length=MAX_MESSAGE_LENGTH,
emoji="🗨️",
platform_hint=(
"You are on Zulip. Messages live under a channel (stream) + topic, "
"or are direct messages. Use CommonMark/Zulip Markdown for formatting "
"(**bold**, *italic*, `code`, ```code blocks```, lists, links, "
"spoilers, LaTeX). Keep replies on-topic; the gateway keeps your "
"reply in the same stream/topic or DM it arrived on."
),
allow_update_command=True,
)
@@ -1,11 +1,146 @@
"""
Hermes Zulip Plugin — Core adapter for Hermes Python agents
Implements BasePlatformAdapter to connect Hermes agents (Tanko, Mumuni,
Koonimo, Koby) to the Sysloggh Zulip agent mesh.
Connects Hermes agents (Tanko, Mumuni, Koonimo, Koby) to the Sysloggh
Zulip agent mesh via event queue polling + subprocess agent invocation.
Architecture mirrors pi-zulip-extension:
Zulip event queue → poll loop → parse message → spawn agent subprocess
→ capture stdout → post response to Zulip (with placeholder + edit)
Usage (systemd service):
python3 -m hermes_zulip --config /path/to/config.yaml
Path: hermes-zulip-plugin/src/hermes_zulip/
Config: config.yaml (per-agent, deployed alongside plugin)
@see ADR-001 DM-first, ADR-005 @mention detection
@see ADR-006 @all-bots, ADR-009 error handling
"""
__version__ = "0.1.0"
import argparse
import logging
import sys
from typing import Dict, Any
# Ensure the src directory is on the path for the adapter import
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from hermes_zulip.adapter import ZulipAdapter
__version__ = "0.2.0"
def load_config(config_path: str) -> Dict[str, Any]:
"""Load YAML config and inject env vars."""
import yaml
with open(config_path) as f:
cfg = yaml.safe_load(f)
# Override api_key from env if set
env_key = os.environ.get("ZULIP_API_KEY")
if env_key:
cfg.setdefault("zulip", {})["api_key"] = env_key
return cfg
def setup_logging(cfg: Dict[str, Any]) -> None:
"""Configure logging from config."""
level = (
cfg.get("monitoring", {}).get("log_level", "INFO").upper()
)
logging.basicConfig(
level=getattr(logging, level, logging.INFO),
format="%(asctime)s [%(name)s] %(levelname)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
def start_health_server(cfg: Dict[str, Any], adapter: ZulipAdapter) -> None:
"""Start a minimal health HTTP endpoint in a background thread."""
import threading
from http.server import HTTPServer, BaseHTTPRequestHandler
port = cfg.get("monitoring", {}).get("health_port", 9200)
start_time = time.time()
class HealthHandler(BaseHTTPRequestHandler):
def do_GET(self) -> None:
if self.path in ("/", "/health"):
import json
body = json.dumps({
"status": "ok",
"agent": cfg.get("agent", {}).get("name", "unknown"),
"connected": adapter.connected,
"uptime_seconds": int(time.time() - start_time),
"timestamp": datetime.utcnow().isoformat(),
})
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(body.encode())
else:
self.send_response(404)
self.end_headers()
def log_message(self, fmt, *args):
pass # Suppress health check log spam
server = HTTPServer(("127.0.0.1", port), HealthHandler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
logging.getLogger(__name__).info(f"Health endpoint on :{port}")
def main() -> None:
"""CLI entry point for the Hermes Zulip plugin.
Usage:
python3 -m hermes_zulip --config /path/to/config.yaml
"""
import time
from datetime import datetime
parser = argparse.ArgumentParser(
description="Hermes Zulip Plugin — Connects Hermes agents to Zulip"
)
parser.add_argument(
"--config", "-c",
default="config.yaml",
help="Path to config.yaml (default: config.yaml)",
)
args = parser.parse_args()
# Load config
cfg = load_config(args.config)
setup_logging(cfg)
logger = logging.getLogger(__name__)
logger.info(
f"Starting Hermes Zulip Plugin v{__version__} "
f"for {cfg.get('agent', {}).get('name', 'unknown')}"
)
# Create adapter
adapter = ZulipAdapter(cfg)
# Start health endpoint (if enabled)
if cfg.get("monitoring", {}).get("health_endpoint_enabled", True):
start_health_server(cfg, adapter)
# Run the event loop (blocks until interrupted)
try:
adapter.run()
except KeyboardInterrupt:
logger.info("Shutting down...")
except Exception as e:
logger.error(f"Fatal error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()
+474 -84
View File
@@ -1,50 +1,100 @@
# Core adapter implements BasePlatformAdapter for Zulip
# See ADR-005 for @mention detection, ADR-009 for error handling
# Full implementation in issue #5.
"""
Core Zulip adapter for Hermes agents (Tanko, Mumuni, Koonimo, Koby).
Architecture (mirrors pi-zulip-extension):
Zulip event queue → poll loop → parse message → spawn agent subprocess
→ capture stdout → post response to Zulip
The Hermes agent is invoked as a subprocess (configurable command) so the
plugin remains decoupled from the agent runtime. In a future iteration,
this could switch to a direct IPC/socket if the agent exposes one.
See ADR-005 (@mention detection), ADR-006 (@all-bots), ADR-009 (error handling).
"""
import asyncio
import json
import logging
import time
import re
import threading
import shlex
import subprocess
import time
from datetime import datetime
from typing import Any, Dict, Optional
logger = logging.getLogger(__name__)
class ZulipAdapter:
"""Zulip adapter for Hermes agents. Connects to Zulip, listens for
@mentions in #agent-hub, routes messages via BasePlatformAdapter."""
# Regex to strip Zulip mention artifacts from message bodies (ADR-008)
MENTION_CLEANER = re.compile(r'@\*\*[^*]+\*\*')
# Regex to strip Zulip mention artifacts from message bodies (ADR-008)
MENTION_CLEANER = re.compile(r'@\*\*[^*]+\*\*')
class ZulipAdapter:
"""Zulip adapter for Hermes agents.
Connects to Zulip via event queues (not the deprecated call_on_each_message),
polls for new events, routes @mentions and DMs to the Hermes agent via
subprocess, and posts responses back to Zulip.
"""
def __init__(self, config: Dict[str, Any]) -> None:
self.config = config
self.connected = False
self._client = None
self._thread = None
self._stop_event = threading.Event()
self._queue_id: Optional[str] = None
self._last_event_id: int = -1
self._poll_task: Optional[asyncio.Task] = None
# Agent subprocess config
self._agent_command: str = config.get("agent", {}).get(
"command", "hermes chat"
)
self._agent_timeout: int = config.get("error_handling", {}).get(
"timeout_seconds", 60
)
# Bot identity for filtering own messages
self._bot_email: str = config.get("zulip", {}).get("email", "")
self._bot_id: int = config.get("swarm", {}).get("bot_id", 0)
# Stream config
self._stream: str = config.get("zulip", {}).get("stream", "agent-hub")
self._all_bots_user_id: int = config.get("zulip", {}).get(
"all_bots_user_id", 1
)
# ------------------------------------------------------------------
# Connection lifecycle
# ------------------------------------------------------------------
async def connect(self) -> None:
"""Establish connection to Zulip and start the event loop."""
"""Connect to Zulip and register an event queue."""
if self.connected:
logger.info("Already connected to Zulip.")
return
try:
from zulip import Client
server_url = self.config['zulip']['server_url']
email = self.config['zulip']['email']
api_key = self.config['zulip']['api_key']
import zulip
self._client = Client(server_url=server_url, email=email, api_key=api_key)
server_url = self.config["zulip"]["server_url"]
email = self.config["zulip"]["email"]
api_key = self.config["zulip"]["api_key"]
self._client = zulip.Client(
server_url=server_url, email=email, api_key=api_key
)
# Register an event queue (like the pi extension)
queue_res = self._client.register(
event_types=["message"]
)
self._queue_id = queue_res.get("queue_id")
self._last_event_id = queue_res.get("last_event_id", -1)
self.connected = True
logger.info(f"Connected to Zulip: {server_url} as {email}")
# Start the event loop in a background thread
self._stop_event.clear()
self._thread = threading.Thread(target=self._event_loop, daemon=True)
self._thread.start()
logger.info("Zulip event loop started.")
logger.info(
f"Connected to Zulip: {server_url} as {email} "
f"(queue={self._queue_id})"
)
except Exception as e:
logger.error(f"Failed to connect to Zulip: {e}")
@@ -52,82 +102,422 @@ class ZulipAdapter:
raise
async def disconnect(self) -> None:
"""Disconnect from Zulip and stop the event loop."""
if not self.connected:
return
self._stop_event.set()
if self._thread:
self._thread.join(timeout=5)
"""Disconnect and cancel the poll loop."""
if self._poll_task:
self._poll_task.cancel()
try:
await self._poll_task
except asyncio.CancelledError:
pass
self._poll_task = None
self.connected = False
logger.info("Disconnected from Zulip.")
async def send_message(self, topic: str, content: str) -> Dict[str, Any]:
"""Send a message to Zulip with retry logic (ADR-009)."""
max_retries = self.config.get('error_handling', {}).get('retry_count', 3)
retry_delay = self.config.get('error_handling', {}).get('retry_delay_seconds', 5)
# ------------------------------------------------------------------
# Event polling (async loop, like pi extension's setInterval)
# ------------------------------------------------------------------
for attempt in range(max_retries):
async def poll_forever(self, poll_interval: float = 3.0) -> None:
"""Continuously poll the Zulip event queue and process messages.
This is the main event loop. Runs until cancelled.
"""
while self.connected and self._client and self._queue_id:
try:
result = self._client.send_message({
'type': 'stream',
'to': self.config['zulip']['stream'],
'subject': topic,
'content': content,
})
logger.info(f"Message sent to {self.config['zulip']['stream']} > {topic} (attempt {attempt + 1})")
return result
events = await self._poll_events()
for event in events:
await self._process_event(event)
except asyncio.CancelledError:
break
except Exception as e:
logger.warning(f"Failed to send message (attempt {attempt + 1}): {e}")
if attempt < max_retries - 1:
time.sleep(retry_delay)
else:
raise
logger.error(f"Poll error: {e}")
# Check for queue expiry
if "BAD_EVENT_QUEUE_ID" in str(e):
logger.info("Queue expired, reconnecting...")
self.connected = False
await self._reconnect_with_retry()
continue
async def on_event(self, event: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""Process incoming Zulip events and route via BasePlatformAdapter."""
if event.get('type') != 'message':
return None
await asyncio.sleep(poll_interval)
msg = event.get('message', {})
mentioned_users = msg.get('mentioned_users', False)
async def _poll_events(self) -> list:
"""Fetch events from the Zulip event queue."""
import zulip
# Only process messages that mention this bot (ADR-005)
if not mentioned_users:
return None
if not self._client or not self._queue_id:
return []
# Strip Zulip formatting artifacts
body = msg.get('content', '')
clean_body = self.MENTION_CLEANER.sub('', body)
clean_body = clean_body.strip()
response = self._client.get_events(
queue_id=self._queue_id,
last_event_id=self._last_event_id,
dont_block=True,
)
# Construct the MessageEvent for BasePlatformAdapter
return {
'type': 'MessageEvent',
'sender': msg.get('sender_full_name', 'Unknown'),
'body': clean_body,
'topic': msg.get('subject', 'Unknown Topic'),
'stream': msg.get('stream', 'Unknown Stream'),
'timestamp': msg.get('timestamp', 0),
}
if response.get("result") != "success":
raise RuntimeError(
f"Events API error: {response.get('msg', 'unknown')}"
)
def _event_loop(self) -> None:
"""Background thread to listen for Zulip events."""
events = response.get("events", [])
for event in events:
if event.get("id", 0) > self._last_event_id:
self._last_event_id = event["id"]
return [e for e in events if e.get("type") == "message"]
async def _reconnect_with_retry(self) -> None:
"""Retry connection with backoff."""
retries = self.config.get("error_handling", {}).get("retry_count", 3)
delay = self.config.get("error_handling", {}).get(
"retry_delay_seconds", 5
)
for attempt in range(retries):
logger.info(
f"Reconnect attempt {attempt + 1}/{retries} "
f"in {delay}s..."
)
await asyncio.sleep(delay)
try:
await self.connect()
if self.connected:
logger.info("Reconnected successfully.")
return
except Exception as e:
logger.error(f"Reconnect attempt {attempt + 1} failed: {e}")
delay *= 2 # Exponential backoff
logger.error("Max reconnection retries reached. Giving up.")
# ------------------------------------------------------------------
# Message processing
# ------------------------------------------------------------------
async def _process_event(self, event: Dict[str, Any]) -> None:
"""Route a Zulip message event to the Hermes agent."""
msg = event.get("message", {})
msg_type = msg.get("type", "") # "private" or "stream"
sender_email = msg.get("sender_email", "")
sender_name = msg.get("sender_full_name", "Unknown")
content = msg.get("content", "")
# Ignore own messages
if sender_email == self._bot_email:
return
# Determine if this message targets this bot
mentioned_users = msg.get("mentioned_users", [])
mentioned_user_ids = [u.get("user_id") for u in mentioned_users]
is_dm = msg_type == "private"
is_mention = self._bot_id in mentioned_user_ids
is_all_bots = self._all_bots_user_id in mentioned_user_ids
# DM-first: process all private messages (like ADR-001/ADR-002)
if is_dm:
logger.info(f"DM from {sender_name}: {content[:80]}...")
await self._route_to_agent(
message_type="dm",
sender_name=sender_name,
content=content,
recipient=sender_email,
)
return
# Stream: only respond to @mentions and @all-bots (ADR-005, ADR-006)
if msg_type == "stream" and (is_mention or is_all_bots):
stream_name = msg.get("display_recipient", "unknown")
topic = msg.get("subject", "general")
logger.info(
f"{'@mention' if is_mention else '@all-bots'} "
f"in #{stream_name} > {topic}"
)
# Clean @mention artifacts from content (ADR-008)
clean_content = MENTION_CLEANER.sub("", content).strip()
await self._route_to_agent(
message_type="mention" if is_mention else "all-bots",
sender_name=sender_name,
content=clean_content,
recipient=stream_name,
topic=topic,
stream_id=msg.get("stream_id"),
)
async def _route_to_agent(
self,
message_type: str,
sender_name: str,
content: str,
recipient: str,
topic: Optional[str] = None,
stream_id: Optional[int] = None,
) -> None:
"""Send the message to the Hermes agent subprocess and relay the
response back to Zulip.
Architecture note: This uses a subprocess (configurable via
agent.command in config.yaml). This mirrors the initial pi extension
approach. A future iteration could use IPC or a socket if the
Hermes agent exposes one.
"""
# 1. Send typing indicator
await self._send_typing_indicator(recipient, "start")
# 2. Send a "Thinking..." placeholder to Zulip immediately.
# On agent_end, this message will be edited with the final response.
placeholder = (
f":robot: _Processing your message..._"
)
placeholder_msg_id = None
try:
self._client.call_on_each_message(
lambda event: self._process_event(event),
placeholder_msg_id = await self._send_message(
message_type=message_type,
recipient=recipient,
content=placeholder,
topic=topic,
stream_id=stream_id,
)
except Exception as e:
logger.error(f"Event loop crashed: {e}")
self.connected = False
logger.warning(f"Failed to send placeholder: {e}")
# 3. Invoke the Hermes agent subprocess
response_text = await self._invoke_agent(content, sender_name)
# 4. Stop typing indicator
await self._send_typing_indicator(recipient, "stop")
# 5. Post (or edit) the response
if not response_text.strip():
logger.warning(
f"Hermes agent returned empty response for "
f"{message_type} from {sender_name}"
)
return
# Truncate to Zulip's 10K char limit
MAX_ZULIP_MSG = 10000
truncated = (
response_text[:MAX_ZULIP_MSG]
+ "\n\n[...truncated at Zulip limit]"
if len(response_text) > MAX_ZULIP_MSG
else response_text
)
def _process_event(self, event: Dict[str, Any]) -> None:
"""Bridge the synchronous event to the async on_event handler."""
import asyncio
try:
loop = asyncio.get_event_loop()
if loop.is_running():
asyncio.create_task(self.on_event(event))
if placeholder_msg_id:
await self._edit_message(placeholder_msg_id, truncated)
logger.info(
f"Finalized response to {sender_name} "
f"({len(truncated)} chars)"
)
else:
asyncio.run(self.on_event(event))
await self._send_message(
message_type=message_type,
recipient=recipient,
content=truncated,
topic=topic,
stream_id=stream_id,
)
logger.info(
f"Sent response to {sender_name} "
f"({len(truncated)} chars)"
)
except Exception as e:
logger.error(f"Error processing event: {e}")
logger.error(f"Failed to post response: {e}")
# ------------------------------------------------------------------
# Hermes agent subprocess invocation
# ------------------------------------------------------------------
async def _invoke_agent(
self, message: str, sender_name: str
) -> str:
"""Spawn the Hermes agent subprocess with the message.
The agent command is configurable (agent.command in config.yaml).
Default: "hermes chat"
The message is passed via stdin (pipe). The agent's stdout is
captured as the response.
This is synchronous (run in executor) to avoid blocking the
event loop during subprocess execution.
"""
cmd_str = self._agent_command
cmd = shlex.split(cmd_str)
logger.info(
f"Invoking agent: {' '.join(cmd)} "
f"(timeout={self._agent_timeout}s)"
)
def _run() -> str:
try:
result = subprocess.run(
cmd,
input=message,
capture_output=True,
text=True,
timeout=self._agent_timeout,
)
if result.returncode != 0:
stderr = result.stderr.strip()
logger.error(
f"Agent exited with code {result.returncode}: "
f"{stderr}"
)
return (
f":warning: Agent error (exit {result.returncode}). "
f"Please try again later."
)
return result.stdout.strip()
except subprocess.TimeoutExpired:
logger.error(
f"Agent timed out after {self._agent_timeout}s"
)
return (
f":hourglass: Agent timed out after "
f"{self._agent_timeout}s. Please try again."
)
except FileNotFoundError:
logger.error(f"Agent command not found: {cmd_str}")
return (
f":warning: Agent command not found: `{cmd_str}`. "
f"Check configuration."
)
except Exception as e:
logger.error(f"Agent invocation error: {e}")
return (
f":warning: Failed to invoke agent: {e}"
)
return await asyncio.get_event_loop().run_in_executor(None, _run)
# ------------------------------------------------------------------
# Zulip API helpers
# ------------------------------------------------------------------
async def _send_message(
self,
message_type: str,
recipient: str,
content: str,
topic: Optional[str] = None,
stream_id: Optional[int] = None,
) -> Optional[int]:
"""Send a message to Zulip. Returns the message ID if successful."""
if not self._client:
return None
def _send() -> Optional[int]:
if message_type in ("dm", "private"):
# Private message: recipient is email
payload = {
"type": "private",
"to": [recipient],
"content": content,
}
else:
# Stream message
payload = {
"type": "stream",
"to": stream_id or recipient,
"subject": topic or "general",
"content": content,
}
try:
result = self._client.send_message(payload)
if result.get("result") == "success":
return result.get("id")
logger.error(
f"Send message failed: {result.get('msg', 'unknown')}"
)
return None
except Exception as e:
logger.error(f"Send message error: {e}")
return None
return await asyncio.get_event_loop().run_in_executor(None, _send)
async def _edit_message(
self, message_id: int, content: str
) -> bool:
"""Edit a previously sent Zulip message (for streaming updates)."""
if not self._client:
return False
def _edit() -> bool:
try:
result = self._client.update_message(
{"message_id": message_id, "content": content}
)
if result.get("result") != "success":
logger.warning(
f"Edit message {message_id}: "
f"{result.get('msg', 'unknown')}"
)
return result.get("result") == "success"
except Exception as e:
logger.warning(f"Edit message {message_id} error: {e}")
return False
return await asyncio.get_event_loop().run_in_executor(None, _edit)
async def _send_typing_indicator(
self, recipient: str, operation: str
) -> None:
"""Send a typing indicator (start/stop)."""
if not self._client:
return
def _typing() -> None:
try:
# The zulip Python library doesn't have a direct typing API
# Use the REST endpoint directly via requests
import requests
server_url = self.config["zulip"]["server_url"]
email = self.config["zulip"]["email"]
api_key = self.config["zulip"]["api_key"]
# For private messages, recipient is the user's email
# We need their user_id. Use the API directly.
to_data = json.dumps([recipient])
requests.post(
f"{server_url}/api/v1/typing",
auth=requests.auth.HTTPBasicAuth(email, api_key),
data={"to": to_data, "op": operation},
timeout=5,
)
except Exception as e:
# Non-critical
pass
await asyncio.get_event_loop().run_in_executor(None, _typing)
# ------------------------------------------------------------------
# Synchronous entry point for the Hermes runner
# ------------------------------------------------------------------
def run(self) -> None:
"""Synchronous entry point that starts the async poll loop.
Called by the Hermes runner (e.g., from a systemd service).
"""
asyncio.run(self._run_async())
async def _run_async(self) -> None:
"""Async entry point."""
try:
await self.connect()
if self.connected:
logger.info("Starting event poll loop...")
await self.poll_forever()
except asyncio.CancelledError:
logger.info("Poll loop cancelled.")
except Exception as e:
logger.error(f"Fatal error in event loop: {e}")
finally:
await self.disconnect()
+71
View File
@@ -0,0 +1,71 @@
# Abiba Zulip Gateway — Standalone Service
**Replaces** the old pi extension (`~/.pi/agent/extensions/zulip/`).
Instead of injecting messages into pi's session (which dies when pi exits), this
runs as an independent Node.js **systemd service** that:
- Polls Zulip event queue for DMs
- Calls the harness inference API directly (no pi dependency)
- Maintains per-sender conversation memory
- Survives pi shutdown and restart
## Architecture
```
Zulip event queue → poll every 3s → DM detected
→ send typing indicator + placeholder
→ POST /v1/chat/completions with conversation history
→ edit placeholder with final response
```
## Requirements
- Node.js 22+
- `npm install` in this directory
## Config
All config via environment variables. Copy `abiba-zulip.service` to set up as a
systemd service, or run directly:
```bash
ZULIP_EMAIL=abiba-bot@chat.sysloggh.net \
ZULIP_API_KEY=your_key \
ZULIP_SITE=https://chat.sysloggh.net \
HARNESS_URL=http://192.168.68.116/v1/chat/completions \
HARNESS_API_KEY=sk-xxx \
HARNESS_MODEL=qwen3.6-35B-A3B \
node dist/index.js
```
## Deploy as a service
```bash
# 1. Install deps
cd /opt/abiba-zulip
npm install
# 2. Build
npx tsc
# 3. Install systemd service
cp abiba-zulip.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable abiba-zulip
systemctl start abiba-zulip
# 4. Check status
systemctl status abiba-zulip
journalctl -u abiba-zulip -f
# 5. Health check
curl http://127.0.0.1:9200/health
```
## Development
```bash
npm run dev # watch mode (tsc watch)
npm run build # compile
npm start # run compiled
```
+48
View File
@@ -0,0 +1,48 @@
[Unit]
Description=Abiba Zulip Gateway Service — Standalone Zulip bot for pi agent
Documentation=https://git.sysloggh.net/SyslogSolution/zulip-platform-plugins
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=root
WorkingDirectory=/opt/abiba-zulip
# ── Zulip credentials ─────────────────────────────────────────────────────
Environment=ZULIP_EMAIL=abiba-bot@chat.sysloggh.net
Environment=ZULIP_API_KEY=cKTDMZAPW08dk3zl05sStzO7HRztzyn8
Environment=ZULIP_SITE=https://chat.sysloggh.net
# ── Agent identity ─────────────────────────────────────────────────────────
Environment=AGENT_NAME=abiba
Environment=AGENT_OWNER_EMAIL=jerome@sysloggh.com
# ── Harness API (inference) ────────────────────────────────────────────────
Environment=HARNESS_URL=http://192.168.68.116/v1/chat/completions
Environment=HARNESS_API_KEY=sk-856ffb0bbb-e5aaf78b10054eca608f8fbcbd73a889
Environment=HARNESS_MODEL=qwen3.6-35B-A3B
Environment=HARNESS_MAX_TOKENS=4096
# ── Service config ─────────────────────────────────────────────────────────
Environment=HEALTH_PORT=9200
Environment=POLL_INTERVAL_MS=3000
Environment=MAX_RETRIES=5
Environment=RETRY_DELAY_MS=5000
ExecStart=/usr/bin/node /opt/abiba-zulip/dist/index.js
Restart=always
RestartSec=5
# Logging
StandardOutput=journal
StandardError=journal
# Security hardening (optional — adjust as needed)
NoNewPrivileges=true
ProtectHome=read-only
ProtectSystem=full
PrivateTmp=true
[Install]
WantedBy=multi-user.target
+24
View File
@@ -0,0 +1,24 @@
# pi Zulip extension — Abiba configuration (flat format for custom parser)
# Deploy to: ~/.pi/agent/extensions/config.yaml
zulip.site: https://chat.sysloggh.net
zulip.email: abiba-bot@chat.sysloggh.net
zulip.api_key: cKTDMZAPW08dk3zl05sStzO7HRztzyn8
zulip.stream: agent-hub
zulip.all_bots_user_id: 20
agent.name: abiba
agent.display_name: Abiba
agent.zulip_bot_name: abiba-bot
agent.owner_email: jerome@sysloggh.com
agent.private_topic: abiba
health_port: 9200
monitoring.health_endpoint_enabled: true
monitoring.log_level: info
error_handling.timeout_seconds: 30
error_handling.retry_count: 3
error_handling.retry_delay_seconds: 5
error_handling.graceful_message: "Abiba encountered an error processing your request. Please try again later."
@@ -0,0 +1,26 @@
module.exports = {
apps: [
{
name: "abiba-zulip",
script: "/usr/bin/pi",
args: "--mode rpc --session-id zulip-service",
cwd: "/root",
env: {
ZULIP_ROLE: "router",
ZULIP_EXTENSION_ACTIVE: "true",
NODE_OPTIONS: "--max-old-space-size=512",
},
// Prevent rapid crash-looping: restart with backoff, limit retries
min_uptime: "30s",
max_restarts: 15,
restart_delay: 10000,
kill_timeout: 5000,
autorestart: true,
},
{
name: "abiba-telegram",
script: "/usr/bin/pitg",
cwd: "/root",
},
],
};
+379
View File
@@ -0,0 +1,379 @@
{
"name": "abiba-zulip-service",
"version": "2.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "abiba-zulip-service",
"version": "2.0.0",
"dependencies": {
"zulip-js": "^2.0.0"
},
"devDependencies": {
"@types/node": "^22.0.0",
"typescript": "^5.7.0"
}
},
"node_modules/@babel/runtime": {
"version": "7.29.7",
"resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.29.7.tgz",
"integrity": "sha512-Nq8OhGWiZIZGV6hLHoyAKLLcJihP/xFeBMGJoUrxTX2psI8dCifzLhZISFb+VWS3wFMRDmCGw5R+dOySCqPLhw==",
"engines": {
"node": ">=6.9.0"
}
},
"node_modules/@types/node": {
"version": "22.20.0",
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.20.0.tgz",
"integrity": "sha512-QWlFW2wf3nTjC13/DqRnBpR4ZO36VJH/JVBkA/vcnmbTBNQIlnObqyqZE1tUR7+Ni23Lda8R1BxMfbXRpCUx5g==",
"dev": true,
"license": "MIT",
"dependencies": {
"undici-types": "~6.21.0"
}
},
"node_modules/asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
},
"node_modules/call-bind-apply-helpers": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz",
"integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==",
"dependencies": {
"es-errors": "^1.3.0",
"function-bind": "^1.1.2"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"dependencies": {
"delayed-stream": "~1.0.0"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==",
"engines": {
"node": ">=0.4.0"
}
},
"node_modules/dunder-proto": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz",
"integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==",
"dependencies": {
"call-bind-apply-helpers": "^1.0.1",
"es-errors": "^1.3.0",
"gopd": "^1.2.0"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/es-define-property": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz",
"integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==",
"engines": {
"node": ">= 0.4"
}
},
"node_modules/es-errors": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz",
"integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==",
"engines": {
"node": ">= 0.4"
}
},
"node_modules/es-object-atoms": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.2.tgz",
"integrity": "sha512-HWcBoN6NileqtSydK2FqHbS/LoDd2pqrnQHLyJzBj4kOp/ky2MWMN694xOfkK8/SnUsW2DH7EfyVlydKCsm1Zw==",
"dependencies": {
"es-errors": "^1.3.0"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/es-set-tostringtag": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/es-set-tostringtag/-/es-set-tostringtag-2.1.0.tgz",
"integrity": "sha512-j6vWzfrGVfyXxge+O0x5sh6cvxAog0a/4Rdd2K36zCMV5eJ+/+tOAngRO8cODMNWbVRdVlmGZQL2YS3yR8bIUA==",
"dependencies": {
"es-errors": "^1.3.0",
"get-intrinsic": "^1.2.6",
"has-tostringtag": "^1.0.2",
"hasown": "^2.0.2"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/form-data": {
"version": "2.5.6",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-2.5.6.tgz",
"integrity": "sha512-Ogz/E85h9tlfJzpI6TuFpGcHZFhLrb9Gw8wq9v40CxSCPnv7ahKr6Xgtkn0KYCDQJ8DNn5VoMO8EXr9V5PadyA==",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"es-set-tostringtag": "^2.1.0",
"hasown": "^2.0.4",
"mime-types": "^2.1.35",
"safe-buffer": "^5.2.1"
},
"engines": {
"node": ">= 0.12"
}
},
"node_modules/function-bind": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
"integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==",
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/get-intrinsic": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz",
"integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==",
"dependencies": {
"call-bind-apply-helpers": "^1.0.2",
"es-define-property": "^1.0.1",
"es-errors": "^1.3.0",
"es-object-atoms": "^1.1.1",
"function-bind": "^1.1.2",
"get-proto": "^1.0.1",
"gopd": "^1.2.0",
"has-symbols": "^1.1.0",
"hasown": "^2.0.2",
"math-intrinsics": "^1.1.0"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/get-proto": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz",
"integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==",
"dependencies": {
"dunder-proto": "^1.0.1",
"es-object-atoms": "^1.0.0"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/gopd": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz",
"integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==",
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/has-symbols": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz",
"integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==",
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/has-tostringtag": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/has-tostringtag/-/has-tostringtag-1.0.2.tgz",
"integrity": "sha512-NqADB8VjPFLM2V0VvHUewwwsw0ZWBaIdgo+ieHtK3hasLz4qeCRjYcqfB6AQrBggRKppKF8L52/VqdVsO47Dlw==",
"dependencies": {
"has-symbols": "^1.0.3"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/hasown": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.4.tgz",
"integrity": "sha512-T2UbfbBEF32wiepXIsMlTW9+dDYC6wMh/t/vYA4tuOMKqWz/n3vr1NFSxQiyP+zk2mXsoMA/i/7qV6LKut1t1A==",
"dependencies": {
"function-bind": "^1.1.2"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/ini": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/ini/-/ini-5.0.0.tgz",
"integrity": "sha512-+N0ngpO3e7cRUWOJAS7qw0IZIVc6XPrW4MlFBdD066F2L4k1L6ker3hLqSq7iXxU5tgS4WGkIUElWn5vogAEnw==",
"engines": {
"node": "^18.17.0 || >=20.5.0"
}
},
"node_modules/isomorphic-fetch": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/isomorphic-fetch/-/isomorphic-fetch-3.0.0.tgz",
"integrity": "sha512-qvUtwJ3j6qwsF3jLxkZ72qCgjMysPzDfeV240JHiGZsANBYd+EEuu35v7dfrJ9Up0Ak07D7GGSkGhCHTqg/5wA==",
"dependencies": {
"node-fetch": "^2.6.1",
"whatwg-fetch": "^3.4.1"
}
},
"node_modules/isomorphic-form-data": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/isomorphic-form-data/-/isomorphic-form-data-2.0.0.tgz",
"integrity": "sha512-TYgVnXWeESVmQSg4GLVbalmQ+B4NPi/H4eWxqALKj63KsUrcu301YDjBqaOw3h+cbak7Na4Xyps3BiptHtxTfg==",
"dependencies": {
"form-data": "^2.3.2"
}
},
"node_modules/math-intrinsics": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz",
"integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==",
"engines": {
"node": ">= 0.4"
}
},
"node_modules/mime-db": {
"version": "1.52.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/mime-types": {
"version": "2.1.35",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
"integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
"dependencies": {
"mime-db": "1.52.0"
},
"engines": {
"node": ">= 0.6"
}
},
"node_modules/node-fetch": {
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz",
"integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==",
"dependencies": {
"whatwg-url": "^5.0.0"
},
"engines": {
"node": "4.x || >=6.0.0"
},
"peerDependencies": {
"encoding": "^0.1.0"
},
"peerDependenciesMeta": {
"encoding": {
"optional": true
}
}
},
"node_modules/safe-buffer": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
"integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
]
},
"node_modules/tr46": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw=="
},
"node_modules/typescript": {
"version": "5.9.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz",
"integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
"dev": true,
"license": "Apache-2.0",
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
},
"engines": {
"node": ">=14.17"
}
},
"node_modules/undici-types": {
"version": "6.21.0",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz",
"integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==",
"dev": true,
"license": "MIT"
},
"node_modules/webidl-conversions": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ=="
},
"node_modules/whatwg-fetch": {
"version": "3.6.20",
"resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.6.20.tgz",
"integrity": "sha512-EqhiFU6daOA8kpjOWTL0olhVOF3i7OrFzSYiGsEMB8GcXS+RrzauAERX65xMeNWVqxA6HXH2m69Z9LaKKdisfg=="
},
"node_modules/whatwg-url": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
"integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==",
"dependencies": {
"tr46": "~0.0.3",
"webidl-conversions": "^3.0.0"
}
},
"node_modules/zulip-js": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/zulip-js/-/zulip-js-2.1.0.tgz",
"integrity": "sha512-kLdxzJZ/FvWHBotUJl7LXCHIkShTjy1FUk5HAWfsal1TM+hw0atCZwgasCpvFDBj01y+39ZEZXgjePaie74Xhg==",
"dependencies": {
"@babel/runtime": "^7.25.7",
"ini": "^5.0.0",
"isomorphic-fetch": "^3.0.0",
"isomorphic-form-data": "2.0.0"
}
}
}
}
+10 -13
View File
@@ -1,22 +1,19 @@
{
"name": "pi-zulip-extension",
"version": "0.1.0",
"description": "pi extension for Zulip agent communication — connects Abiba to the Sysloggh agent mesh",
"main": "src/index.ts",
"name": "abiba-zulip-service",
"version": "2.0.0",
"description": "Standalone Zulip gateway service for Abiba (pi agent). Direct harness API, conversation memory, systemd service.",
"type": "module",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"check": "tsc --noEmit"
"start": "node dist/index.js",
"dev": "node --watch dist/index.js"
},
"dependencies": {
"zulip-js": "^2.0.0",
"yaml": "^2.0.0"
"zulip-js": "^2.0.0"
},
"devDependencies": {
"@earendil-works/pi-coding-agent": "*",
"typescript": "^5.0.0"
},
"keywords": ["pi", "zulip", "agent", "abiba", "sysloggh"],
"license": "UNLICENSED",
"private": true
"@types/node": "^22.0.0",
"typescript": "^5.7.0"
}
}
+16
View File
@@ -0,0 +1,16 @@
/**
* pi-zulip-extension Zulip agent communication plugin for pi agents
*
* Deploy to: ~/.pi/agent/extensions/zulip/
* Config: config.yaml (alongside extension, or env vars)
*
* DM-first architecture per ADR-001/ADR-002.
* Background Zulip event queue poller that injects messages directly into
* the current pi session via pi.sendUserMessage(), then captures the LLM
* response from agent_end and sends it back to Zulip. No subprocess overhead.
*
* @see ADR-007: Platform-native plugin contracts
* @see docs/ARCHITECTURE.md
*/
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
export default function (pi: ExtensionAPI): void;
File diff suppressed because it is too large Load Diff
+587 -88
View File
@@ -1,120 +1,619 @@
/**
* pi-zulip-extension Zulip agent communication plugin for pi agents
* abiba-zulip-service Standalone Zulip gateway for Abiba (pi agent)
*
* Deploy to: ~/.pi/agent/extensions/zulip.ts
* Config: config.yaml (alongside extension, or symlinked)
* Replaces the pi extension model. Instead of injecting messages into pi's
* session, this runs as an independent Node.js process that:
* - Connects to Zulip via event queue polling
* - Calls the harness inference API directly for each DM
* - Maintains per-sender conversation memory
* - Runs as a systemd service (survives pi shutdown)
*
* Connects a pi agent (Abiba) to the Sysloggh Zulip agent mesh.
* Listens for @mentions of abiba-bot in #agent-hub and forwards
* to the pi agent. Responses are posted back to the same Zulip topic.
* Config via env vars (see README or systemd service file).
*
* @see ADR-001: DM-first architecture
* @see ADR-007: Platform-native plugin contracts
* @see docs/ARCHITECTURE.md
*/
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
import { readFileSync } from "fs";
import { parse } from "yaml";
import zulip, { ZulipClient } from "zulip-js";
import http from "node:http";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
// ---------------------------------------------------------------------------
// Config
// ---------------------------------------------------------------------------
// ── Types ─────────────────────────────────────────────────────────────────
interface ZulipConfig {
agent: {
name: string;
display_name: string;
zulip_bot_name: string;
owner_email: string;
private_topic: string;
};
interface Config {
zulip: {
server_url: string;
email: string;
api_key: string;
stream: string;
all_bots_user_id: number;
site: string;
};
error_handling: {
timeout_seconds: number;
retry_count: number;
retry_delay_seconds: number;
graceful_message: string;
agent: {
name: string;
owner_email: string;
};
monitoring: {
health_endpoint_enabled: boolean;
health_port: number;
log_level: string;
harness: {
url: string;
api_key: string;
model: string;
max_tokens: number;
};
health_port: number;
poll_interval_ms: number;
max_retries: number;
retry_delay_ms: number;
}
function loadConfig(): ZulipConfig {
const raw = readFileSync("config.yaml", "utf-8");
const cfg = parse(raw) as ZulipConfig;
cfg.zulip.api_key = process.env["ZULIP_API_KEY"] ?? cfg.zulip.api_key;
return cfg;
interface ChatMessage {
role: "system" | "user" | "assistant";
content: string;
}
// ---------------------------------------------------------------------------
// Extension entry point
// ---------------------------------------------------------------------------
interface PendingReply {
id: number;
zulipMessageId?: number;
senderId: number;
senderName: string;
type: "private";
}
export default function (pi: ExtensionAPI) {
const config = loadConfig();
interface ZulipQueue {
client: ZulipClient;
queueId: string;
lastEventId: number;
poll(): Promise<any[]>;
sendMessage(params: {
type: string;
to: string | number;
subject?: string;
content: string;
}): Promise<number>;
editMessage(messageId: number, content: string): Promise<void>;
sendTypingNotification(userIds: number[], operation: string): Promise<void>;
}
// Health endpoint
if (config.monitoring.health_endpoint_enabled) {
startHealthServer(config);
// ── Constants ──────────────────────────────────────────────────────────────
const SYSTEM_PROMPT = `You are Abiba, an AI assistant operating in a homelab environment. You communicate through Zulip DMs. Be helpful, concise, and technically accurate. Your owner is Jerome.`;
const PLACEHOLDERS = [
":robot: _Processing your message..._",
":hourglass_flowing_sand: _Thinking..._",
":brain: _Generating response..._",
];
const MAX_CONVERSATION_HISTORY = 50;
const MAX_ZULIP_MSG_LENGTH = 10000;
const STREAMING_UPDATE_INTERVAL_MS = 2000;
// ── Config loading ────────────────────────────────────────────────────────
function loadConfig(): Config {
const email = process.env.ZULIP_EMAIL || "";
const apiKey = process.env.ZULIP_API_KEY || "";
const site = process.env.ZULIP_SITE || "";
if (!email || !apiKey || !site) {
console.error(
"[abiba-zulip] Missing required env vars: ZULIP_EMAIL, ZULIP_API_KEY, ZULIP_SITE"
);
process.exit(1);
}
pi.on("session_start", async (_event, ctx) => {
ctx.ui.notify(
`Zulip extension loaded for ${config.agent.display_name} (${config.agent.zulip_bot_name})`,
"info"
);
});
return {
zulip: { email, api_key: apiKey, site },
agent: {
name: process.env.AGENT_NAME || "abiba",
owner_email: process.env.AGENT_OWNER_EMAIL || "jerome@sysloggh.com",
},
harness: {
url:
process.env.HARNESS_URL ||
"http://192.168.68.116/v1/chat/completions",
api_key:
process.env.HARNESS_API_KEY ||
"sk-856ffb0bbb-e5aaf78b10054eca608f8fbcbd73a889",
model: process.env.HARNESS_MODEL || "qwen3.6-35B-A3B",
max_tokens: parseInt(process.env.HARNESS_MAX_TOKENS || "4096", 10),
},
health_port: parseInt(process.env.HEALTH_PORT || "9200", 10),
poll_interval_ms: parseInt(process.env.POLL_INTERVAL_MS || "3000", 10),
max_retries: parseInt(process.env.MAX_RETRIES || "5", 10),
retry_delay_ms: parseInt(process.env.RETRY_DELAY_MS || "5000", 10),
};
}
// Diagnostic tool
pi.registerTool({
name: "zulip_status",
label: "Zulip Status",
description: "Check Zulip connection status and bot identity",
parameters: {},
execute: async () => ({
connected: false, // TODO: track connection state
bot: config.agent.zulip_bot_name,
stream: config.zulip.stream,
server: config.zulip.server_url,
owner: config.agent.owner_email,
private_topic: config.agent.private_topic,
// ── Conversation memory ───────────────────────────────────────────────────
const conversations = new Map<string, ChatMessage[]>();
function getOrCreateConversation(senderEmail: string): ChatMessage[] {
let history = conversations.get(senderEmail);
if (!history) {
history = [{ role: "system", content: SYSTEM_PROMPT }];
conversations.set(senderEmail, history);
}
return history;
}
function addToConversation(
senderEmail: string,
message: ChatMessage
): void {
const history = getOrCreateConversation(senderEmail);
history.push(message);
// Trim if too long (keep system prompt)
if (history.length > MAX_CONVERSATION_HISTORY) {
const system = history[0];
history.splice(1, history.length - MAX_CONVERSATION_HISTORY);
history.unshift(system);
}
}
// ── Harness API ───────────────────────────────────────────────────────────
async function queryHarness(
senderEmail: string,
userMessage: string,
config: Config
): Promise<string> {
const messages = getOrCreateConversation(senderEmail);
messages.push({ role: "user", content: userMessage });
const response = await fetch(config.harness.url, {
method: "POST",
headers: {
Authorization: `Bearer ${config.harness.api_key}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: config.harness.model,
messages: messages,
max_tokens: config.harness.max_tokens,
stream: false,
}),
signal: AbortSignal.timeout(120_000),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Harness API returned ${response.status}: ${text.slice(0, 500)}`);
}
const data = await response.json();
const reply =
data.choices?.[0]?.message?.content ?? "[No response generated]";
// Store assistant reply in conversation history
messages.push({ role: "assistant", content: reply });
return reply;
}
// ---------------------------------------------------------------------------
// Health endpoint
// ---------------------------------------------------------------------------
// ── Zulip queue creation ──────────────────────────────────────────────────
function startHealthServer(config: ZulipConfig) {
const http = require("http") as typeof import("http");
const port = config.monitoring.health_port;
const startTime = Date.now();
async function createZulipQueue(config: Config): Promise<ZulipQueue> {
const client = await zulip({
username: config.zulip.email,
apiKey: config.zulip.api_key,
realm: config.zulip.site,
});
http
.createServer((_req: any, res: any) => {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(
JSON.stringify({
status: "ok",
agent: config.agent.name,
uptime_seconds: Math.floor((Date.now() - startTime) / 1000),
zulip_connected: false,
last_message_time: null,
timestamp: new Date().toISOString(),
})
const queueRes: any = await client.queues.register({
event_types: ["message"],
});
const queueId: string = queueRes.queue_id;
let lastEventId: number = queueRes.last_event_id ?? -1;
return {
client,
queueId,
lastEventId,
async poll() {
const res = await fetch(
`${config.zulip.site}/api/v1/events?queue_id=${encodeURIComponent(queueId)}&last_event_id=${lastEventId}`,
{
headers: {
Authorization:
"Basic " +
Buffer.from(`${config.zulip.email}:${config.zulip.api_key}`).toString("base64"),
},
}
);
})
.listen(port, "127.0.0.1", () => {
console.log(`[zulip-extension] Health endpoint on :${port}`);
});
if (!res.ok) {
throw new Error(`Events API returned ${res.status}: ${await res.text()}`);
}
const data: any = await res.json();
if (data.events && Array.isArray(data.events)) {
for (const event of data.events) {
if (event.id > lastEventId) {
lastEventId = event.id;
}
}
// Update the cached lastEventId so reconnections preserve progress
this.lastEventId = lastEventId;
return data.events.filter((e: any) => e.type === "message");
}
return [];
},
async sendMessage(params) {
const formBody = new URLSearchParams();
formBody.append("type", params.type);
formBody.append(
"to",
params.type === "private"
? JSON.stringify([params.to])
: String(params.to)
);
if (params.subject) formBody.append("subject", params.subject);
formBody.append("content", params.content);
const res = await fetch(`${config.zulip.site}/api/v1/messages`, {
method: "POST",
headers: {
Authorization:
"Basic " +
Buffer.from(`${config.zulip.email}:${config.zulip.api_key}`).toString("base64"),
"Content-Type": "application/x-www-form-urlencoded",
},
body: formBody.toString(),
});
if (!res.ok) {
throw new Error(`Send message API returned ${res.status}: ${await res.text()}`);
}
const body: any = await res.json();
return body.id;
},
async editMessage(messageId, content) {
const formBody = new URLSearchParams();
formBody.append("content", content);
const res = await fetch(
`${config.zulip.site}/api/v1/messages/${messageId}`,
{
method: "PATCH",
headers: {
Authorization:
"Basic " +
Buffer.from(`${config.zulip.email}:${config.zulip.api_key}`).toString("base64"),
"Content-Type": "application/x-www-form-urlencoded",
},
body: formBody.toString(),
}
);
if (!res.ok && res.status !== 400) {
console.warn(
`[abiba-zulip] Edit message ${messageId} returned ${res.status}`
);
}
},
async sendTypingNotification(userIds, operation) {
const formBody = new URLSearchParams();
formBody.append("to", JSON.stringify(userIds));
formBody.append("op", operation);
await fetch(`${config.zulip.site}/api/v1/typing`, {
method: "POST",
headers: {
Authorization:
"Basic " +
Buffer.from(`${config.zulip.email}:${config.zulip.api_key}`).toString("base64"),
"Content-Type": "application/x-www-form-urlencoded",
},
body: formBody.toString(),
});
},
};
}
// ── Health server ─────────────────────────────────────────────────────────
function startHealthServer(port: number, getState: () => any): http.Server {
const server = http.createServer((req, res) => {
if (req.url === "/health" || req.url === "/") {
const state = getState();
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ status: "ok", ...state }));
} else {
res.writeHead(404);
res.end("Not found");
}
});
server.on("error", (err: NodeJS.ErrnoException) => {
if (err.code === "EADDRINUSE") {
console.warn(
`[abiba-zulip] Port :${port} already in use — skipping health endpoint`
);
} else {
console.error(`[abiba-zulip] Health server error:`, err.message);
}
});
server.listen(port, "127.0.0.1", () => {
console.log(`[abiba-zulip] Health endpoint on :${port}`);
});
return server;
}
// ── Main ──────────────────────────────────────────────────────────────────
async function main() {
const config = loadConfig();
console.log(`[abiba-zulip] Starting Abiba Zulip Service v2.0.0`);
console.log(
`[abiba-zulip] Agent: ${config.agent.name}, Zulip: ${config.zulip.email} @ ${config.zulip.site}`
);
console.log(
`[abiba-zulip] Harness: ${config.harness.model} @ ${config.harness.url}`
);
let queue: ZulipQueue | null = null;
let connected = false;
let retryCount = 0;
let lastError: string | null = null;
let messagesProcessed = 0;
let pollTimer: ReturnType<typeof setInterval> | null = null;
let healthServer: http.Server | null = null;
// Pending replies awaiting LLM response
const pendingReplies: Map<number, PendingReply> = new Map();
let replyIdCounter = 0;
// Track which sender's DM is currently being processed (one at a time per sender)
const processingSenders = new Set<string>();
function getState() {
return {
connected,
email: config.zulip.email,
site: config.zulip.site,
agent: config.agent.name,
messages_processed: messagesProcessed,
last_error: lastError,
retry_count: retryCount,
active_conversations: conversations.size,
pending_replies: pendingReplies.size,
};
}
/**
* Process a single DM: send placeholder call harness edit with response.
*/
async function processDM(
senderEmail: string,
senderId: number,
senderName: string,
content: string
): Promise<void> {
if (processingSenders.has(senderEmail)) {
console.log(
`[abiba-zulip] Already processing DM from ${senderName}, queueing...`
);
// TODO: implement proper queue if needed
return;
}
processingSenders.add(senderEmail);
messagesProcessed++;
try {
// Send typing indicator
queue!
.sendTypingNotification([senderId], "start")
.catch(() => {});
// Send placeholder
const replyId = ++replyIdCounter;
const placeholder =
PLACEHOLDERS[replyId % PLACEHOLDERS.length];
let placeholderMsgId: number | undefined;
try {
placeholderMsgId = await queue!.sendMessage({
type: "private",
to: senderId,
content: placeholder,
});
pendingReplies.set(replyId, {
id: replyId,
zulipMessageId: placeholderMsgId,
senderId,
senderName,
type: "private",
});
console.log(
`[abiba-zulip] [${replyId}] Placeholder sent for ${senderName} (msg ${placeholderMsgId})`
);
} catch {
pendingReplies.set(replyId, {
id: replyId,
senderId,
senderName,
type: "private",
});
console.log(
`[abiba-zulip] [${replyId}] No placeholder for ${senderName}`
);
}
// Call harness API
console.log(
`[abiba-zulip] [${replyId}] Querying harness for ${senderName}...`
);
const response = await queryHarness(senderEmail, content, config);
// Stop typing
queue!
.sendTypingNotification([senderId], "stop")
.catch(() => {});
// Truncate if needed
const truncated =
response.length > MAX_ZULIP_MSG_LENGTH
? response.slice(0, MAX_ZULIP_MSG_LENGTH) +
"\n\n[...truncated at Zulip limit]"
: response;
// Send response
const pending = pendingReplies.get(replyId);
if (pending?.zulipMessageId) {
await queue!.editMessage(pending.zulipMessageId, truncated);
console.log(
`[abiba-zulip] [${replyId}] Finalized for ${senderName} (${truncated.length} chars)`
);
} else {
await queue!.sendMessage({
type: "private",
to: senderId,
content: truncated,
});
console.log(
`[abiba-zulip] [${replyId}] Sent fresh for ${senderName} (${truncated.length} chars)`
);
}
pendingReplies.delete(replyId);
} catch (err) {
const errMsg = err instanceof Error ? err.message : String(err);
console.error(`[abiba-zulip] Error processing DM from ${senderName}: ${errMsg}`);
lastError = errMsg;
// Try to send error message to user
try {
await queue!.sendMessage({
type: "private",
to: senderId,
content:
":warning: Sorry, I encountered an error processing your request. Please try again later.",
});
} catch {
// Best effort
}
} finally {
processingSenders.delete(senderEmail);
}
}
/**
* Polling loop connects, registers queue, polls for events.
*/
async function startPolling() {
console.log(
`[abiba-zulip] Connecting to ${config.zulip.site} as ${config.zulip.email}...`
);
try {
queue = await createZulipQueue(config);
connected = true;
retryCount = 0;
lastError = null;
console.log(
`[abiba-zulip] Connected, queue: ${queue.queueId} (last_event_id=${queue.lastEventId})`
);
// Start health server (on first connect)
if (!healthServer) {
healthServer = startHealthServer(config.health_port, getState);
}
// Poll loop
pollTimer = setInterval(async () => {
if (!queue) return;
try {
const events = await queue.poll();
for (const event of events) {
const msg = event.message;
if (!msg) continue;
if (msg.sender_email === config.zulip.email) continue;
if (msg.type !== "private") continue; // DM-first
const content = (msg.content || "").trim();
if (!content) continue;
console.log(
`[abiba-zulip] DM from ${msg.sender_full_name || msg.sender_email}: ${content.slice(0, 80)}...`
);
// Process asynchronously (don't block the poll loop)
processDM(
msg.sender_email,
msg.sender_id,
msg.sender_full_name || msg.sender_email,
content
).catch((err) =>
console.error(`[abiba-zulip] processDM error:`, err)
);
}
} catch (err) {
const errMsg = err instanceof Error ? err.message : String(err);
if (
errMsg.includes("BAD_EVENT_QUEUE_ID") ||
errMsg.includes("queue_id")
) {
console.log(`[abiba-zulip] Queue expired, reconnecting...`);
connected = false;
if (pollTimer) clearInterval(pollTimer);
pollTimer = null;
setTimeout(() => startPolling(), config.retry_delay_ms);
return;
}
lastError = errMsg;
console.error(`[abiba-zulip] Poll error: ${errMsg}`);
}
}, config.poll_interval_ms);
} catch (err) {
const errMsg = err instanceof Error ? err.message : String(err);
lastError = errMsg;
console.error(`[abiba-zulip] Connection failed: ${errMsg}`);
if (retryCount < config.max_retries) {
retryCount++;
const delay = config.retry_delay_ms * retryCount;
console.log(
`[abiba-zulip] Retrying in ${delay / 1000}s (attempt ${retryCount})...`
);
setTimeout(() => startPolling(), delay);
} else {
console.error(
`[abiba-zulip] Max retries (${config.max_retries}) reached. Giving up.`
);
lastError = `Connection failed after ${config.max_retries} retries: ${errMsg}`;
}
}
}
// ── Graceful shutdown ──────────────────────────────────────────────────
function shutdown(signal: string) {
console.log(`\n[abiba-zulip] Received ${signal}. Shutting down...`);
if (pollTimer) {
clearInterval(pollTimer);
pollTimer = null;
}
if (healthServer) {
healthServer.close();
healthServer = null;
}
connected = false;
queue = null;
process.exit(0);
}
process.on("SIGINT", () => shutdown("SIGINT"));
process.on("SIGTERM", () => shutdown("SIGTERM"));
// ── Start ──────────────────────────────────────────────────────────────
await startPolling();
// Keep alive — idle loop
console.log(`[abiba-zulip] Service running. Waiting for DMs...`);
await new Promise(() => {}); // never resolves — process runs until killed
}
main().catch((err) => {
console.error(`[abiba-zulip] Fatal error:`, err);
process.exit(1);
});
+460
View File
@@ -0,0 +1,460 @@
/**
* MCP Bridge Extension
*
* Connects to MCP servers and exposes their tools as pi custom tools.
* Supports stdio and streaming-HTTP transports.
*
* Configuration is read from ~/.pi/agent/extensions/mcp/mcp-servers.json.
* See the README in this directory for the format.
*/
import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import {
CallToolResultSchema,
ListToolsResultSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { Type } from "typebox";
import fs from "node:fs";
import path from "node:path";
// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------
interface McpStdioServer {
command: string;
args?: string[];
env?: Record<string, string>;
/** Whitelist: only register these tool names. Takes priority over excludeTools. */
tools?: string[];
/** Blacklist: exclude these tool names. Ignored if `tools` is set. */
excludeTools?: string[];
}
interface McpHttpServer {
name: string;
description?: string;
type: "streaming-http";
url: string;
timeout?: number;
/** Whitelist: only register these tool names. Takes priority over excludeTools. */
tools?: string[];
/** Blacklist: exclude these tool names. Ignored if `tools` is set. */
excludeTools?: string[];
}
type McpServerConfig = McpStdioServer | McpHttpServer;
interface McpConfig {
mcpServers: Record<string, McpServerConfig>;
}
function loadConfig(): McpConfig {
const configPath = path.join(
path.dirname(import.meta.url.replace("file://", "")),
"mcp-servers.json",
);
if (!fs.existsSync(configPath)) {
return { mcpServers: {} };
}
return JSON.parse(fs.readFileSync(configPath, "utf-8"));
}
// ---------------------------------------------------------------------------
// MCP client registry
// ---------------------------------------------------------------------------
interface ServerClient {
client: Client;
transport: StdioClientTransport | StreamableHTTPClientTransport;
name: string;
tools: Array<{ name: string; description: string; inputSchema: object }>;
}
const clients = new Map<string, ServerClient>();
async function connectToServer(
name: string,
config: McpServerConfig,
ctx: ExtensionContext,
): Promise<ServerClient> {
let client: Client;
let transport: StdioClientTransport | StreamableHTTPClientTransport;
if ("command" in config) {
// stdio transport
transport = new StdioClientTransport({
command: config.command,
args: config.args ?? [],
env: {
...process.env,
...config.env,
},
});
client = new Client(
{ name: "pi-mcp", version: "1.0.0" },
{ capabilities: {} },
);
await client.connect(transport);
} else {
// streaming-HTTP transport
transport = new StreamableHTTPClientTransport(
new URL(config.url),
undefined,
{
requestTimeout: config.timeout ?? 120_000,
},
);
client = new Client(
{ name: "pi-mcp", version: "1.0.0" },
{ capabilities: {} },
);
await client.connect(transport);
}
// Discover tools
const toolsRes = await client.request(
{ method: "tools/list" },
ListToolsResultSchema,
);
const tools =
toolsRes.tools?.map((t) => ({
name: t.name,
description: t.description ?? "",
inputSchema: t.inputSchema ?? {},
})) ?? [];
return { client, transport, name, tools };
}
async function disconnectClient(server: ServerClient) {
try {
await server.transport.close();
} catch {
// ignore close errors
}
}
// ---------------------------------------------------------------------------
// Schema builder: convert MCP inputSchema to TypeBox
// ---------------------------------------------------------------------------
function buildParameters(inputSchema: object) {
if (!inputSchema || typeof inputSchema !== "object") {
return Type.Object({});
}
const schema = inputSchema as {
type?: string;
properties?: Record<string, any>;
required?: string[];
};
const properties: Record<string, any> = {};
if (schema.properties) {
for (const [key, prop] of Object.entries(schema.properties)) {
const p = prop as { type?: string; description?: string };
let field: any;
switch (p.type) {
case "string":
field = Type.Optional(Type.String({ description: p.description }));
break;
case "number":
field = Type.Optional(Type.Number());
break;
case "integer":
field = Type.Optional(Type.Number());
break;
case "boolean":
field = Type.Optional(Type.Boolean());
break;
case "array":
field = Type.Optional(
Type.Array(Type.String(), {
description: p.description,
}),
);
break;
case "object":
field = Type.Optional(Type.Record(Type.String(), Type.Unknown()));
break;
default:
field = Type.Optional(Type.String());
}
properties[key] = field;
}
}
// Check required fields
if (schema.required) {
for (const req of schema.required) {
if (properties[req]) {
// Make required by redefining
const def = properties[req];
if (def && "origin" in def) {
// It's a TypeBox optional - we'd need to rebuild as non-optional
// For simplicity, keep all optional since MCP tools often have flexible params
}
}
}
}
return Type.Object(properties);
}
// ---------------------------------------------------------------------------
// Tool call forwarder
// ---------------------------------------------------------------------------
const DEFAULT_MAX_RESULT_CHARS = 15_000;
/**
* Rebuild relay query text from structuredContent when the bridge has
* truncated the display text field (ends with Unicode ellipsis character).
*/
function rebuildRelayTextFromStructuredContent(
textContent: string,
structuredContent: Record<string, unknown> | undefined,
): string | null {
if (!structuredContent) return null;
// Detect bridge-side truncation: the display text ends with …
if (!textContent.endsWith("…")) return null;
const nodes = structuredContent.nodes as Array<Record<string, unknown>> | undefined;
if (!nodes || nodes.length === 0) return null;
const rebuilt: string[] = [];
rebuilt.push(`📬 **Relay Query**: ${structuredContent.count ?? nodes.length} result(s).\n`);
for (const node of nodes) {
const id = node.id;
const title = node.title ?? "(no title)";
const from = (node.metadata as Record<string, unknown>)?.from ?? "?";
const to = (node.metadata as Record<string, unknown>)?.to ?? "?";
const source = node.source ?? "";
const description = node.description ?? "";
rebuilt.push(`**#${id}** — ${title}`);
rebuilt.push(` From: ${from} → To: ${to}`);
if (source) {
rebuilt.push(` ${source.replace(/\n/g, "\n ")}`);
}
if (description) {
rebuilt.push(` ${description}`);
}
rebuilt.push("");
}
return rebuilt.join("\n");
}
async function forwardToolCall(
server: ServerClient,
toolName: string,
args: Record<string, unknown>,
signal?: AbortSignal,
maxResultChars: number = DEFAULT_MAX_RESULT_CHARS,
): Promise<{
content: Array<{ type: string; text: string }>;
isError?: boolean;
}> {
const result = await server.client.request(
{
method: "tools/call",
params: { name: toolName, arguments: args },
},
CallToolResultSchema,
signal,
);
// Check for bridge-side truncation and rebuild from structuredContent
const structuredContent = (result as Record<string, unknown>).structuredContent as Record<string, unknown> | undefined;
const content: Array<{ type: string; text: string }> = [];
let totalChars = 0;
let truncated = false;
if (result.content) {
for (const item of result.content) {
if (truncated) break;
if (item.type === "text") {
let itemText = item.text;
// If bridge-side truncation detected, rebuild from structuredContent
const rebuilt = rebuildRelayTextFromStructuredContent(itemText, structuredContent);
if (rebuilt) {
itemText = rebuilt;
}
totalChars += itemText.length;
if (totalChars > maxResultChars) {
const keep = maxResultChars - (totalChars - itemText.length);
const trimmed = itemText.slice(0, Math.max(0, keep));
content.push({
type: "text",
text: trimmed + `\n\n[TRUNCATED: ${(itemText.length - keep).toLocaleString()} chars removed. Limit: ${maxResultChars.toLocaleString()} chars. Use specific queries or pagination.]`,
});
truncated = true;
} else {
content.push({ type: "text", text: itemText });
}
} else if (item.type === "image") {
content.push({
type: "text",
text: `[MCP image: ${item.mimeType}]`,
});
} else if (item.type === "resource") {
const textParts: string[] = [];
if ("resource" in item) {
const res = item.resource;
if ("text" in res) {
textParts.push(res.text);
} else if ("blob" in res) {
textParts.push(`[binary resource: ${res.mimeType}]`);
}
}
content.push({ type: "text", text: textParts.join("\n") });
} else {
content.push({
type: "text",
text: `[MCP ${item.type}: ${JSON.stringify(item).slice(0, 500)}]`,
});
}
}
}
return {
content,
isError: result.isError ?? false,
};
}
// ---------------------------------------------------------------------------
// Extension
// ---------------------------------------------------------------------------
export default async function (pi: ExtensionAPI) {
const config = loadConfig();
const registeredNames = new Set<string>();
// Connect to all servers
for (const [name, serverConfig] of Object.entries(config.mcpServers)) {
try {
const client = await connectToServer(name, serverConfig, pi);
if (client.tools.length === 0) {
pi.on("session_start", (_event, ctx) => {
ctx.ui.notify(
`MCP server "${name}" connected but has no tools.`,
"info",
);
});
clients.set(name, client);
continue;
}
// Filter tools based on server config
let registeredTools = client.tools;
if (serverConfig.tools) {
registeredTools = client.tools.filter((t) =>
serverConfig.tools!.includes(t.name),
);
} else if (serverConfig.excludeTools) {
registeredTools = client.tools.filter(
(t) => !serverConfig.excludeTools!.includes(t.name),
);
}
pi.on("session_start", (_event, ctx) => {
ctx.ui.notify(
`MCP: "${name}" connected — ${registeredTools.length}/${client.tools.length} tool(s) registered.`,
"info",
);
});
// Register each MCP tool as a pi tool
for (const tool of registeredTools) {
const fullName = `${name}-${tool.name}`;
if (registeredNames.has(fullName)) continue;
registeredNames.add(fullName);
const parameters = buildParameters(tool.inputSchema);
pi.registerTool({
name: fullName,
label: tool.name,
description: `[MCP:${name}] ${tool.description}`,
promptSnippet: `[MCP:${name}] ${tool.description}`,
promptGuidelines: [
`Use ${fullName} to: ${tool.description}.`,
],
parameters,
async execute(
_toolCallId,
params: Record<string, unknown>,
signal,
_onUpdate,
_ctx,
) {
const server = clients.get(name);
if (!server) {
return {
content: [{ type: "text", text: `Server "${name}" not connected.` }],
isError: true,
};
}
const maxChars = serverConfig.maxResultChars ?? DEFAULT_MAX_RESULT_CHARS;
return await forwardToolCall(server, tool.name, params, signal, maxChars);
},
});
}
clients.set(name, client);
} catch (err) {
pi.on("session_start", (_event, ctx) => {
ctx.ui.notify(
`MCP: Failed to connect to "${name}": ${err instanceof Error ? err.message : String(err)}`,
"error",
);
});
}
}
// Register a /mcp-status command
pi.registerCommand("mcp-status", {
description: "Show status of all MCP servers",
handler: async (_args, ctx) => {
const lines: string[] = ["=== MCP Server Status ===", ""];
for (const [name, client] of clients) {
lines.push(` ${name}: connected (${client.tools.length} tools)`);
for (const tool of client.tools) {
lines.push(` - ${tool.name}: ${tool.description.slice(0, 80)}`);
}
}
const unconnected = Object.keys(config.mcpServers).filter(
(n) => !clients.has(n),
);
for (const name of unconnected) {
lines.push(` ${name}: NOT CONNECTED`);
}
ctx.ui.notify(lines.join("\n"), "info");
},
});
// Cleanup on shutdown
pi.on("session_shutdown", async (event, _ctx) => {
if (event.reason === "quit") {
for (const [name, client] of clients) {
await disconnectClient(client);
delete clients.get(name);
}
}
});
}
+34
View File
@@ -0,0 +1,34 @@
// Type declarations for zulip-js (no @types available)
declare module "zulip-js" {
interface ZulipClient {
queues: {
register(params: {
event_types: string[];
narrow?: Array<Array<string | number>>;
}): Promise<{ queue_id: string; last_event_id: number }>;
};
users: {
me: {
get(): Promise<any>;
};
};
messages: {
store: {
send(rawContent: string): Promise<any>;
};
};
events: {
retrieve(params: {
queue_id: string;
last_event_id: number;
dont_block?: boolean;
}): Promise<{ events: any[]; result?: string; msg?: string }>;
};
}
export default function zulip(config: {
username: string;
apiKey: string;
realm: string;
}): Promise<ZulipClient>;
}
+11 -7
View File
@@ -1,14 +1,18 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"module": "ES2022",
"moduleResolution": "node",
"outDir": "./dist",
"rootDir": "./src",
"strict": false,
"esModuleInterop": true,
"outDir": "dist",
"rootDir": "src",
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"declaration": true,
"skipLibCheck": true
"declarationMap": true,
"sourceMap": true
},
"include": ["src/**/*.ts"]
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
+3
View File
@@ -0,0 +1,3 @@
from .adapter import register
__all__ = ["register"]
File diff suppressed because it is too large Load Diff
+56
View File
@@ -0,0 +1,56 @@
name: zulip-platform
label: Zulip
kind: platform
version: 1.0.0
description: >
Zulip messaging platform adapter for Hermes Agent. Connects to a Zulip
server via event queue polling, processes DMs and @mentions, and sends
replies with placeholder→edit streaming. Uses the official zulip-js
HTTP API pattern (polling, not WebSockets) — lightweight, no external
SDK beyond httpx.
author: Syslog Solution LLC
requires_env:
- name: ZULIP_SITE
description: "Zulip server URL (e.g. https://chat.sysloggh.net)"
prompt: "Zulip server URL"
password: false
- name: ZULIP_EMAIL
description: "Bot email address (e.g. tanko-bot@chat.sysloggh.net)"
prompt: "Zulip bot email"
password: false
- name: ZULIP_API_KEY
description: "Zulip bot API key"
prompt: "Zulip API key"
password: true
optional_env:
- name: ZULIP_STREAM
description: "Primary stream to subscribe to (default: agent-hub)"
prompt: "Zulip stream name"
password: false
- name: ZULIP_ALL_BOTS_USER_ID
description: "User ID of the @all-bots user (default: 1)"
prompt: "All Bots user ID"
password: false
- name: ZULIP_AGENT_NAME
description: "Agent display name for logging (default: hermes-agent)"
prompt: "Agent name"
password: false
- name: ZULIP_OWNER_EMAIL
description: "Owner email for private topic ACL"
prompt: "Owner email"
password: false
- name: ZULIP_POLL_INTERVAL
description: "Event poll interval in seconds (default: 3)"
prompt: "Poll interval (seconds)"
password: false
- name: ZULIP_HOME_CHANNEL
description: "Default recipient for cron / notification delivery"
prompt: "Home channel (email or stream:topic)"
password: false
- name: ZULIP_HOME_CHANNEL_NAME
description: "Human label for the home channel"
prompt: "Home channel display name"
password: false
+185 -83
View File
@@ -1,32 +1,56 @@
#!/usr/bin/env bash
# deploy.sh GitOps deployment for zulip-platform-plugins
# Usage: ./deploy.sh <tag|branch> [--ct=<agent>] [--dry-run]
# ./deploy.sh v1.0.0 # Deploy to all 6 CTs
# ./deploy.sh main # Deploy latest (staging only!)
# ./deploy.sh --ct=tanko v1.0.0 # Deploy to single CT
# ./deploy.sh --dry-run v1.0.0 # Preview deployment without making changes
# deploy.sh GitOps deployment for zulip-platform-plugins
#
# Supports two deployment modes:
# LEGACY: /opt/hermes-zulip-plugin/ + systemctl restart zulip-plugin
# NATIVE: ~/.hermes/plugins/platforms/zulip/ + hermes gateway restart
#
# Usage:
# ./deploy.sh <tag|branch> # Deploy all agents (default: native)
# ./deploy.sh --mode=native v1.0.0 # Hermes native plugin (new)
# ./deploy.sh --mode=legacy v1.0.0 # Old systemd service (deprecated)
# ./deploy.sh --ct=tanko v1.0.0 # Single agent
# ./deploy.sh --dry-run v1.0.0 # Preview only
#
set -euo pipefail
DEPLOY_LOG="deploy.log"
TAG=""
SINGLE_CT=""
DRY_RUN=false
DEPLOY_MODE="native" # default to new Hermes native plugin
# Parse args
for arg in "$@"; do
case "$arg" in
--ct=*) SINGLE_CT="${arg#--ct=}" ;;
--dry-run) DRY_RUN=true ;;
--mode=*) DEPLOY_MODE="${arg#--mode=}" ;;
*) TAG="$arg" ;;
esac
done
if [[ -z "$TAG" ]]; then
echo "Usage: $0 <tag|branch> [--ct=<agent>] [--dry-run]"
echo "Usage: $0 <tag|branch> [--ct=<agent>] [--mode=native|legacy] [--dry-run]"
echo ""
echo " --ct=<agent> Deploy to single agent (tanko, mumuni, etc.)"
echo " --mode=native Hermes native plugin at ~/.hermes/plugins/ (default)"
echo " --mode=legacy Old systemd service at /opt/hermes-zulip-plugin/"
echo " --dry-run Preview deployment without making changes"
echo ""
echo "Examples:"
echo " ./deploy.sh v1.0.0 # Deploy native to all agents"
echo " ./deploy.sh --ct=tanko v1.0.0 # Deploy native to Tanko only"
echo " ./deploy.sh --mode=legacy v0.9.0 # Deploy legacy to all"
exit 1
fi
# Agent CT registry (must match CONTEXT.md)
if [[ "$DEPLOY_MODE" != "native" && "$DEPLOY_MODE" != "legacy" ]]; then
echo "ERROR: --mode must be 'native' or 'legacy'"
exit 1
fi
# ── Agent Registry (must match docs/CONTEXT.md) ──────────────────────
declare -A AGENTS=(
["tanko"]="amdpve CT 112"
["mumuni"]="minipve CT 114"
@@ -36,32 +60,31 @@ declare -A AGENTS=(
["abiba"]="amdpve CT 100"
)
# Platform paths per agent type
declare -A PLUGIN_PATHS=(
# Native Hermes plugin path (~/.hermes/plugins/platforms/zulip/)
NATIVE_PLUGIN_SRC="plugins/platforms/zulip"
# Legacy paths (DEPRECATED — systemd zulip-plugin service)
declare -A LEGACY_PATHS=(
["tanko"]="/opt/hermes-zulip-plugin"
["mumuni"]="/opt/hermes-zulip-plugin"
["koonimo"]="/opt/hermes-zulip-plugin"
["koby\"]="/opt/hermes-zulip-plugin"
["kagentz\"]="/opt/agent-zero-plugin"
["abiba\"]="/root/.pi/agent/extensions/zulip.ts"
["koby"]="/opt/hermes-zulip-plugin"
["kagentz"]="/opt/agent-zero-plugin"
["abiba"]="/root/.pi/agent/extensions/zulip.ts"
)
# Correcting the backslash artifacts from the original file
PLUGIN_PATHS["koby"]="/opt/hermes-zulip-plugin"
PLUGIN_PATHS["kagentz"]="/opt/agent-zero-plugin"
PLUGIN_PATHS["abiba"]="/root/.pi/agent/extensions/zulip.ts"
declare -A SERVICE_NAMES=(
# Service names for legacy mode
declare -A LEGACY_SERVICES=(
["tanko"]="zulip-plugin"
["mumuni"]="zulip-plugin"
["koonimo"]="zulip-plugin"
["koby"]="zulip-plugin"
["kagentz"]="zulip-plugin"
["abiba"]="pi" # pi reload, not systemctl
["abiba"]="pi"
)
GITEA_REPO="https://git.sysloggh.net/SyslogSolution/zulip-platform-plugins.git"
HEALTH_PORT=9200
GITEA_REPO="https://git.sysloggh.net/SyslogSolution/zulip-platform-plugins.git"
log() {
local prefix=""
@@ -69,90 +92,169 @@ log() {
echo "${prefix}[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$DEPLOY_LOG"
}
deploy_agent() {
# ── Deployment Functions ─────────────────────────────────────────────
deploy_native() {
local agent="$1"
local ct_info="${AGENTS[$agent]}"
local plugin_path="${PLUGIN_PATHS[$agent]}"
local service="${SERVICE_NAMES[$agent]}"
local plugin_dir="$HOME/.hermes/plugins/platforms/zulip"
log "=== Deploying $agent ($ct_info) @ $TAG ==="
log "Path: $plugin_path"
log "📦 Deploying $agent: native Hermes plugin -> $plugin_dir"
# 1. Git pull & checkout tag
if [[ "$DRY_RUN" != "true" ]]; then
cd "$plugin_path" || { log "ERROR: $agent path $plugin_path not found"; return 1; }
git fetch --tags origin
git checkout "$TAG"
else
log "Skipping Git checkout (Dry Run)"
fi
log "$agent: checked out $TAG"
# 2. Install dependencies (platform-specific)
if [[ "$DRY_RUN" != "true" ]]; then
case "$agent" in
tanko|mumuni|koonimo|koby)
pip install -r requirements.txt --quiet
;;
kagentz)
pip install -r requirements.txt --quiet
;;
abiba)
log "$agent: pi extension skipping pip install"
;;
esac
else
log "Skipping dependency installation (Dry Run)"
if [[ "$DRY_RUN" == "true" ]]; then
log " Would copy $NATIVE_PLUGIN_SRC/{adapter.py,__init__.py,plugin.yaml} -> $plugin_dir/"
log " Would run: hermes gateway restart"
return 0
fi
# 3. Restart service
if [[ "$DRY_RUN" != "true" ]]; then
case "$agent" in
abiba)
log "$agent: triggering /reload"
;;
*)
systemctl restart "$service"
log "$agent: restarted $service"
;;
esac
# Create plugin directory
mkdir -p "$plugin_dir"
# Copy plugin files
cp "$NATIVE_PLUGIN_SRC/adapter.py" "$plugin_dir/"
cp "$NATIVE_PLUGIN_SRC/__init__.py" "$plugin_dir/"
cp "$NATIVE_PLUGIN_SRC/plugin.yaml" "$plugin_dir/"
log " Copied plugin files to $plugin_dir/"
ls -la "$plugin_dir/"
# Restart Hermes Gateway to load the plugin
if command -v hermes &>/dev/null; then
log " Restarting Hermes Gateway..."
hermes gateway restart
log " Hermes Gateway restarted"
else
log "Skipping service restart (Dry Run)"
log " ⚠️ 'hermes' command not found — manual restart needed"
log " Run: hermes gateway restart"
fi
# 4. Health check
log "Waiting for service to stabilize..."
# Health check
log " Waiting for service to stabilize..."
sleep 5
if [[ "$DRY_RUN" != "true" ]]; then
if curl -sf "http://localhost:$HEALTH_PORT/health" > /dev/null 2>&1; then
log "OK: $agent health check passed"
else
log "ERROR: $agent health check failed check logs"
return 1
fi
if curl -sf "http://localhost:$HEALTH_PORT/health" > /dev/null 2>&1; then
log " ✅ Health check passed (port $HEALTH_PORT)"
else
log "Skipping health check (Dry Run)"
log " ⚠️ Health check on port $HEALTH_PORT not responding"
log " Check Gateway logs for plugin load errors"
fi
log "$agent: native deployment complete"
}
# --- Main ---\
log "Deploy started target: $TAG (Dry Run: $DRY_RUN)"
deploy_legacy() {
local agent="$1"
local plugin_path="${LEGACY_PATHS[$agent]}"
local service="${LEGACY_SERVICES[$agent]}"
log "📦 Deploying $agent: LEGACY mode -> $plugin_path"
if [[ "$DRY_RUN" == "true" ]]; then
log " Would git checkout $TAG in $plugin_path"
log " Would install deps + restart $service"
return 0
fi
# Git checkout
if [[ ! -d "$plugin_path" ]]; then
log "❌ Path $plugin_path not found for $agent"
return 1
fi
cd "$plugin_path"
git fetch --tags origin
git checkout "$TAG"
log " Checked out $TAG in $plugin_path"
# Install deps
if [[ -f "requirements.txt" ]]; then
pip install -r requirements.txt --quiet
log " Dependencies installed"
fi
# Restart service
if systemctl list-units --full -all 2>/dev/null | grep -q "$service"; then
systemctl restart "$service"
log " Restarted $service"
else
log " ⚠️ Service $service not found — manual restart needed"
fi
# Health check
sleep 5
if curl -sf "http://localhost:$HEALTH_PORT/health" > /dev/null 2>&1; then
log " ✅ Health check passed"
else
log " ⚠️ Health check failed — check logs"
fi
log "$agent: legacy deployment complete"
}
# ── Verify function ──────────────────────────────────────────────────
verify_deployment() {
local agent="$1"
log "🔍 Verifying $agent deployment..."
if [[ "$DEPLOY_MODE" == "native" ]]; then
local plugin_dir="$HOME/.hermes/plugins/platforms/zulip"
if [[ -f "$plugin_dir/adapter.py" && -f "$plugin_dir/plugin.yaml" ]]; then
log " ✅ Plugin files present in $plugin_dir"
log " adapter.py: $(wc -l < "$plugin_dir/adapter.py") lines"
log " plugin.yaml: $(wc -l < "$plugin_dir/plugin.yaml") lines"
else
log " ❌ Plugin files missing in $plugin_dir"
return 1
fi
fi
log "$agent verification complete"
}
# ── Main ─────────────────────────────────────────────────────────────
log "🚀 Deploy started — tag: $TAG, mode: $DEPLOY_MODE, dry-run: $DRY_RUN"
if [[ -n "$SINGLE_CT" ]]; then
deploy_agent "$SINGLE_CT"
if [[ -z "${AGENTS[$SINGLE_CT]:-}" ]]; then
log "❌ Unknown agent: $SINGLE_CT"
log " Known agents: ${!AGENTS[*]}"
exit 1
fi
log "--- Deploying single agent: $SINGLE_CT ---"
if [[ "$DEPLOY_MODE" == "native" ]]; then
deploy_native "$SINGLE_CT"
else
deploy_legacy "$SINGLE_CT"
fi
verify_deployment "$SINGLE_CT"
else
FAILED=""
for agent in tanko mumuni koonimo koby kagentz abiba; do
if ! deploy_agent "$agent"; then
FAILED="$FAILED $agent"
for agent in "${!AGENTS[@]}"; do
echo ""
if [[ "$DEPLOY_MODE" == "native" ]]; then
if deploy_native "$agent"; then
verify_deployment "$agent" || FAILED="$FAILED $agent"
else
FAILED="$FAILED $agent"
fi
else
if deploy_legacy "$agent"; then
verify_deployment "$agent" || FAILED="$FAILED $agent"
else
FAILED="$FAILED $agent"
fi
fi
done
echo ""
log "=== Deploy complete ==="
if [[ -n "$FAILED" ]]; then
log "FAILED:$FAILED"
log "Run rollback: ./scripts/rollback.sh <previous-tag>"
log "FAILED:$FAILED"
log " Run rollback: ./scripts/rollback.sh <previous-tag>"
exit 1
fi
log "All 6 agents deployed successfully."
log "All agents deployed successfully."
log " Next: monitor #agent-hub for agent responses"
fi
+92
View File
@@ -0,0 +1,92 @@
#!/usr/bin/env bash
# verify-deployment.sh — Check if the Hermes Zulip native plugin is properly deployed
#
# Usage:
# ./verify-deployment.sh # Check local agent
# ./verify-deployment.sh --ct=tanko # Check specific agent
# ./verify-deployment.sh --all # Check all reachable agents
#
set -euo pipefail
PLUGIN_DIR="$HOME/.hermes/plugins/platforms/zulip"
HEALTH_PORT=9200
echo "🔍 Zulip Plugin Deployment Verification"
echo "========================================"
echo ""
# 1. Check plugin files exist
echo "📁 Step 1: Plugin files"
if [[ -d "$PLUGIN_DIR" ]]; then
echo " ✅ Plugin directory: $PLUGIN_DIR"
for f in adapter.py __init__.py plugin.yaml; do
if [[ -f "$PLUGIN_DIR/$f" ]]; then
echo "$f$(wc -l < "$PLUGIN_DIR/$f") lines"
else
echo "$f — MISSING"
fi
done
else
echo " ❌ Plugin directory NOT FOUND at $PLUGIN_DIR"
echo " → Install: ./scripts/deploy.sh --mode=native v1.0.0"
fi
echo ""
# 2. Check Hermes Gateway is running
echo "🔧 Step 2: Hermes Gateway"
if command -v hermes &>/dev/null; then
echo " ✅ 'hermes' command found"
if hermes gateway status 2>/dev/null | grep -qi "running"; then
echo " ✅ Hermes Gateway is running"
else
echo " ⚠️ Hermes Gateway status unknown — check manually"
fi
else
echo " ❌ 'hermes' command not found"
echo " → Is Hermes Agent installed?"
fi
echo ""
# 3. Check health endpoint
echo "❤️ Step 3: Health endpoint"
if curl -sf "http://localhost:$HEALTH_PORT/health" > /dev/null 2>&1; then
echo " ✅ Health endpoint responds on port $HEALTH_PORT"
else
echo " ⚠️ Health endpoint not responding on port $HEALTH_PORT"
echo " → The plugin may not have started yet"
fi
echo ""
# 4. Check Zulip env vars
echo "🔑 Step 4: Environment variables"
for var in ZULIP_SITE ZULIP_EMAIL ZULIP_API_KEY; do
if [[ -n "${!var:-}" ]]; then
val="${!var}"
if [[ "$var" == "ZULIP_API_KEY" ]]; then
echo "$var — [REDACTED]"
else
echo "$var$val"
fi
else
echo "$var — NOT SET"
fi
done
echo ""
# Overall
echo "═══════════════════════════════════════"
missing=0
[[ -d "$PLUGIN_DIR" ]] || missing=$((missing + 1))
command -v hermes &>/dev/null || missing=$((missing + 1))
[[ -n "${ZULIP_SITE:-}" && -n "${ZULIP_EMAIL:-}" && -n "${ZULIP_API_KEY:-}" ]] || missing=$((missing + 1))
if [[ "$missing" -eq 0 ]]; then
echo "✅ VERDICT: Plugin properly deployed"
echo " Send a DM to verify: @**${ZULIP_AGENT_NAME:-hermes-agent}** _hello_"
else
echo "⚠️ VERDICT: $missing issue(s) found — fix and re-verify"
fi