Skip to content

feat: Discord listen mode with Universe multi-agent routing and follo…#21

Merged
Dhwanil25 merged 1 commit into
mainfrom
feat/multi-agent-improvements
Apr 5, 2026
Merged

feat: Discord listen mode with Universe multi-agent routing and follo…#21
Dhwanil25 merged 1 commit into
mainfrom
feat/multi-agent-improvements

Conversation

@Dhwanil25
Copy link
Copy Markdown
Owner

…w-ups

  • Add Discord bot integration (bot token + channel ID via manual HTTPS proxy)
  • Poll Discord every 30s for !run commands, route to Agent Universe
  • Send multi-agent results back to Discord when complete
  • Support follow-up messages to continue the same Universe session
  • Add ChannelSendButton component for sending output to any configured channel
  • Add channel dispatch lib supporting Discord, Telegram, Slack, and more
  • Add auto-notify to Scheduler when jobs complete
  • Wire Discord listen mode toggle and status indicator in ChannelsPage

What does this PR do?

Related issue

Closes #

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Refactor / cleanup
  • Performance improvement
  • Other — describe:

How to test

Screenshots

Before After

Notes for reviewers

Checklist

  • npm run lint passes
  • Tested locally end-to-end (Agent Universe launches and completes)
  • No API keys, tokens, or secrets in the diff
  • Referenced the related issue above

…w-ups

- Add Discord bot integration (bot token + channel ID via manual HTTPS proxy)
- Poll Discord every 30s for !run commands, route to Agent Universe
- Send multi-agent results back to Discord when complete
- Support follow-up messages to continue the same Universe session
- Add ChannelSendButton component for sending output to any configured channel
- Add channel dispatch lib supporting Discord, Telegram, Slack, and more
- Add auto-notify to Scheduler when jobs complete
- Wire Discord listen mode toggle and status indicator in ChannelsPage

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 5, 2026

Claude finished @Dhwanil25's task —— View job


🔍 Code Review: Discord Integration

This PR adds comprehensive Discord bot integration with multi-agent routing. Overall implementation is solid with good separation of concerns. Here's my detailed analysis:


Strengths

Architecture & Organization:

  • Clean separation between useDiscordListener (polling logic), channelDispatch (multi-platform messaging), and ChannelSendButton (UI component)
  • Smart follow-up session tracking allows continuous conversations
  • Good integration with existing Universe page for multi-agent execution

Security & Best Practices:

  • No hardcoded secrets — all tokens read from localStorage
  • Bot self-filtering — prevents responding to own messages via botIdRef.current
  • Safe API proxy — custom Discord proxy in vite.config.ts properly handles Authorization headers
  • Message chunking — handles Discord's 2000-char limit correctly

Code Quality:

  • Excellent TypeScript usage throughout
  • Consistent error handling patterns
  • Good use of React patterns (refs for stable references, proper cleanup)

🚨 Critical Issues

1. Memory Leak Risk (useDiscordListener.ts:79-221)

The Discord listener creates intervals but cleanup could fail:

const poll = async () => {
  if (cancelled) return  // ❌ Early return prevents localStorage update
  // ... polling logic
}

Fix: Move cancelled check after localStorage operations to prevent incomplete state.

2. Race Condition (useDiscordListener.ts:134)

if (pendingRef.current || followUpPending.current) break // busy — retry next poll

This could cause infinite retries if refs never clear due to errors.

Recommendation: Add timeout/retry limit to prevent endless pending states.

3. Unhandled Network Errors (channelDispatch.ts:43-181)

Multiple API calls lack proper error boundaries:

const res = await fetch(url) // ❌ Network timeouts not handled
const d = await res.json()   // ❌ Could throw on malformed JSON

Fix: Add try-catch around JSON parsing and network timeouts.


⚠️ Performance Concerns

1. Aggressive Polling (useDiscordListener.ts:9)

const POLL_MS = 30_000 // Every 30 seconds
  • Could hit Discord rate limits with many users
  • No exponential backoff on failures
  • Consider Discord's rate limits: 50 requests per second globally

2. Missing Request Debouncing (ChannelSendButton.tsx:20-27)

Button can be clicked rapidly before sending state updates.

Fix: Disable button immediately on click:

const send = async (channelId: string) => {
  if (sending) return // Add guard
  setSending(channelId)
  // ... rest of logic
}

3. Heavy Re-renders (useDiscordListener.ts:65-67)

Status object recreation on every state update:

setStatus(s => ({ ...s, active: true, error: null })) // ❌ New object every time

🔧 Code Quality Issues

1. TypeScript any Usage (channelDispatch.ts:52, 65)

const d = await res.json() as { ok: boolean; description?: string } // ❌ Runtime cast

Consider proper type guards or schema validation.

2. Inconsistent Error Handling

Some functions return { ok: boolean; msg: string }, others throw. Pick one pattern.

3. Magic Numbers (channelDispatch.ts:35)

function truncate(text: string, max = 3800): string // Why 3800?

Should be named constants with comments explaining limits.


🎯 Specific File Issues

vite.config.ts:18-51

  • Manual Discord proxy is clever solution for Authorization headers
  • ✅ Proper error handling and CORS setup
  • Consider adding rate limiting middleware

scripts/test-discord.mjs

  • Excellent debugging tool
  • ✅ Clear step-by-step validation
  • Could add permissions checking

ChannelSendButton.tsx:68-97

  • Dropdown positioning could break on scroll/resize
  • Consider using a proper dropdown library for better UX

📝 Action Items

High Priority:

  1. Fix memory leak in Discord listener cleanup
  2. Add network timeout handling to all fetch calls
  3. Implement exponential backoff for Discord API failures
  4. Add race condition protection for pending refs

Medium Priority:

  1. Add proper TypeScript interfaces for API responses
  2. Implement consistent error handling pattern
  3. Add request debouncing to prevent double-sends
  4. Consider rate limiting for Discord API calls

Low Priority:

  1. Replace magic numbers with named constants
  2. Add unit tests for core polling logic
  3. Improve dropdown UX in ChannelSendButton

Ready to Merge?

Not yet — please address the critical memory leak and race condition issues first. The security and architecture are solid, but the reliability concerns need fixing before production use.

The Discord integration concept is excellent and well-executed overall. With the critical fixes above, this will be a robust feature.

--- | Branch: feat/multi-agent-improvements

@Dhwanil25 Dhwanil25 merged commit b420efa into main Apr 5, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant