Commit Graph

154 Commits

Author SHA1 Message Date
zhi
92799176bf fix(install): fix uninstall order to satisfy config validation
- Remove from plugins.allow BEFORE deleting plugins.entries.dirigent
- OpenClaw validates that allow[] entries must exist in entries{}
- New order: allow → entry → paths → provider
2026-03-03 18:28:44 +00:00
zhi
cd0ce6a910 fix(install): use unsetPath for plugin entry removal
- Use openclaw config unset instead of get-modify-set
- Avoids triggering full plugins config validation
- Handles both added and replaced entries uniformly
2026-03-03 18:23:01 +00:00
zhi
a177150554 fix(install): uninstall always deletes plugin entry
- For uninstall, always delete plugins.entries.dirigent
- Don't try to restore old config (which may fail validation due to missing required fields)
- Provider restoration still works (replaced providers are restored)
2026-03-03 18:20:48 +00:00
zhi
b79cc1eb84 fix: use api.resolvePath for plugin directory
- Replace import.meta.url with api.resolvePath('.') for reliable path resolution
- Fixes no-reply API not starting due to incorrect pluginDir calculation
2026-03-03 18:16:47 +00:00
zhi
ed5ffd6c53 fix(install): clone function handle undefined
- clone(undefined) now returns undefined instead of throwing JSON.parse error
- This fixes install when plugins.entries.dirigent doesn't exist yet
2026-03-03 18:06:14 +00:00
zhi
be8fe835d0 fix(install): handle 'undefined' string from openclaw config get
- getJson now treats 'undefined' string as missing value
- Add try-catch to JSON.parse for robustness
2026-03-03 17:57:34 +00:00
zhi
91cd402627 fix(install): remove duplicate function definition
- Remove empty duplicate runOpenclaw function that caused syntax error
2026-03-03 17:02:08 +00:00
84b0363d59 Merge pull request 'dev/dirigent-tasks' (#11) from dev/dirigent-tasks into main
Reviewed-on: #11
2026-03-03 16:54:44 +00:00
zhi
83c03b6934 feat(install): idempotent install/uninstall
- Repeat install = reinstall: auto-detect existing install, uninstall first, then install
- Repeat uninstall = no-op: if no install record found, exit 0 with message
- Uses spawnSync to re-exec uninstall phase during reinstall
2026-03-03 16:39:40 +00:00
zhi
6423dbcdfa fix(install): handle plugins.allow in install/uninstall
- Install: add 'dirigent' to plugins.allow if not present, record in delta
- Uninstall: remove 'dirigent' from plugins.allow if it was added by us
- Delta tracking preserves other allowlist entries
2026-03-03 16:36:33 +00:00
zhi
d378e27be9 fix(install): delta-tracking uninstall + remove sh script
- Rewrite install/uninstall to use delta-tracking (added/replaced/removed)
- Install records only what the plugin changed:
  * added: plugin新增的配置(卸载时删除)
  * replaced: plugin覆盖的旧配置(卸载时恢复)
- Uninstall only affects plugin-managed keys, preserving user changes elsewhere
- Remove install-dirigent-openclaw.sh to avoid confusion
2026-03-03 16:34:50 +00:00
zhi
2ea4f59a1e build: support both registry publish and git clone install
- Add root package.json with files[] for registry publishing
- Add prepare script to sync dist/ from plugin/
- Add postinstall to auto-run install script
- Modify install script: auto-create dist/ if missing (git clone case)
- Plugin works in both scenarios:
  * npm install @hangman-lab/dirigent -> uses bundled dist/
  * git clone + node scripts/install-dirigent-openclaw.mjs -> auto-creates dist/
2026-03-03 16:24:05 +00:00
zhi
8e26744162 feat: lifecycle management - no-reply API + moderator bot follow gateway start/stop
- Added gateway_start hook: spawns no-reply API as child process, then starts moderator bot
- Added gateway_stop hook: kills no-reply API process, stops moderator bot
- No-reply API server.mjs is located relative to plugin dir via import.meta.url
- Moderator presence moved from register() to gateway_start for proper lifecycle
2026-03-03 16:01:34 +00:00
zhi
cb58e3d951 docs: add schedulingIdentifier to CONFIG.example.json 2026-03-03 14:23:52 +00:00
zhi
fd1bf449a4 refine: cleanup remaining whispergate refs, improve docs and TASKLIST formatting
- Fix enableWhispergatePolicyTool → enableDirigentPolicyTool in config schema and example
- Fix whisper-gateway → dirigentway in install script
- Add v0.2.0 changelog entry
- Improve README with scheduling identifier docs and English text
- Clean up plugin README with moderator handoff format docs
- Reformat TASKLIST with cleaner done markers
2026-03-03 10:13:39 +00:00
zhi
af33d747d9 feat: complete Dirigent rename + all TASKLIST items
- Task 1: Identity prompt now includes Discord userId
- Task 2: Added configurable schedulingIdentifier (default: ➡️)
- Task 3: Moderator handoff uses <@userId>+identifier instead of semantic messages
- Task 4: All prompts/comments/help text converted to English
- Task 5: Full project rename WhisperGate → Dirigent across all files

Breaking: config key changed from plugins.entries.whispergate to plugins.entries.dirigent
Breaking: channel policies file renamed to dirigent-channel-policies.json
Breaking: tool name changed from whispergate_tools to dirigent_tools
2026-03-03 10:10:27 +00:00
zhi
2afb982c04 docs: add tasklist for upcoming fixes 2026-03-03 09:59:19 +00:00
zhi
a18168749b docs: update README for turn-based features 2026-03-03 09:59:12 +00:00
329f6ed490 Merge pull request 'fix: advance turn in before_message_write to prevent race condition' (#10) from fix/turn-advance-race-condition into main
Reviewed-on: nav/WhisperGate#10
2026-03-03 03:47:35 +00:00
zhi
cf9be60145 fix: advance turn in before_message_write to prevent race condition
When a speaker finishes with an end symbol, the turn was only advanced
in the message_sent hook. But by that time, the message had already been
broadcast to other agents, whose before_model_resolve ran with the old
turn state, causing them to be blocked by the turn gate (forced no-reply).

Fix:
- Move turn advance for both NO_REPLY and end-symbol cases to
  before_message_write, which fires before the message is broadcast.
- Guard 1: Only the current speaker can advance the turn (accountId check).
- Guard 2: Only process assistant messages (role check). before_message_write
  fires for incoming user messages too, which contain end symbols from other
  agents and would cause cascading turn advances.
- Use sessionTurnHandled set to prevent double-advancing in message_sent.
2026-03-02 11:34:40 +00:00
zhi
6d17e6a911 fix: advance turn in before_message_write to prevent race condition
When a speaker finishes with an end symbol, the turn was only advanced
in the message_sent hook. But by that time, the message had already been
broadcast to other agents, whose before_model_resolve ran with the old
turn state, causing them to be blocked by the turn gate (forced no-reply).

Fix: Move turn advance for both NO_REPLY and end-symbol cases to
before_message_write, which fires before the message is broadcast.
Use sessionTurnHandled set to prevent double-advancing in message_sent.
2026-03-02 11:04:41 +00:00
aeecb534b7 Merge pull request 'fix: channelId extraction, sender identification, and per-channel turn order' (#9) from fix/turn-gate-and-handoff into main
Reviewed-on: orion/WhisperGate#9
2026-03-01 11:12:17 +00:00
zhi
d90083317b fix: channelId extraction, sender identification, and per-channel turn order
- Fix channelId extraction: ctx.channelId is platform name ('discord'), not
  the Discord channel snowflake. Now extracts from conversation_label field
  ('channel id:123456') and sessionKey fallback (':channel:123456').

- Fix extractDiscordChannelId: support 'discord:channel:xxx' format in
  addition to 'channel:xxx' for conversationId/event.to fields.

- Fix sender identification in message_received: event.from returns channel
  target, not sender ID. Now uses event.metadata.senderId for humanList
  matching so human messages correctly reset turn order.

- Fix per-channel turn order: was using all server-wide bot accounts from
  bindings, causing deadlock when turn landed on bots not in the channel.
  Now dynamically tracks which bot accounts are seen per channel via
  message_received and only includes those in turn order.

- Always save sessionChannelId/sessionAccountId mappings in before_model_resolve
  regardless of turn check result, so downstream hooks can use them.

- Add comprehensive debug logging to message_sent hook.
2026-03-01 11:08:41 +00:00
zhi
a4bc9990db fix: add sessionAccountId mapping and use in before_message_write
- Add sessionAccountId Map to track sessionKey -> accountId
- Save accountId in before_model_resolve when resolving accountId
- Use sessionChannelId/sessionAccountId fallback in before_message_write
2026-03-01 09:16:55 +00:00
zhi
435a7712b8 fix: add sessionChannelId mapping for message_sent
- Add sessionChannelId Map to track sessionKey -> channelId
- Save channelId in before_model_resolve when we have derived.channelId
- Fix message_sent to use sessionChannelId fallback when ctx.channelId is undefined
- Add debug logging to message_sent
2026-03-01 09:13:03 +00:00
zhi
9e754d32cf fix: make before_message_write synchronous
- Remove async/await from before_message_write hook
- Use fire-and-forget for sendModeratorMessage (void ... .catch())
- OpenClaw's before_message_write is synchronous, not async
2026-03-01 04:59:45 +00:00
zhi
692111eeda fix: implement turn gate and handoff improvements
1. Fix channelId priority: use ctx.channelId > conv.chat_id > conv.channel_id
2. Add sessionAllowed state: track if session was allowed (true) or forced no-reply (false)
3. Add sessionInjected Set: implement one-time prependContext injection
4. Add before_message_write hook: detect NO_REPLY and handle turn advancement
   - forced no-reply: don't advance turn
   - allowed + NO_REPLY: advance turn + handoff
   - dormant state: don't trigger handoff
5. message_sent: continues to handle real messages with end symbols
2026-02-28 21:44:53 +00:00
zhi
80439b0912 Revert "Merge pull request 'fix: use systemPrompt instead of prependContext for end marker instruction' (#5) from fix/moderator-and-system-prompt into feat/turn-based-speaking"
This reverts commit 6a81f75fd0, reversing
changes made to 86fdc63802.
2026-02-28 19:34:37 +00:00
zhi
7fdc7952b7 Merge pull request 'fix: add turn check debug logs + one-time prompt injection' (#6) from fix/turn-check-debug-and-onetime-inject into feat/turn-based-speaking 2026-02-28 19:27:59 +00:00
zhi
f03d32e15f fix: add turn check debug logs + one-time prompt injection
1. Turn check improvements:
   - Add debug logs for ctx.agentId, resolved accountId, turnOrder length
   - Fallback to ctx.accountId if resolveAccountId fails
   - Add resolveDiscordUserId debug logs for handoff troubleshooting

2. One-time prompt injection:
   - Add sessionInjected Set to track injected sessions
   - Use prependContext (not systemPrompt) but only inject once per session
   - Skip subsequent injections with debug log
2026-02-28 19:27:39 +00:00
zhi
6a81f75fd0 Merge pull request 'fix: use systemPrompt instead of prependContext for end marker instruction' (#5) from fix/moderator-and-system-prompt into feat/turn-based-speaking 2026-02-28 19:25:23 +00:00
zhi
81758b9a54 fix: use systemPrompt instead of prependContext for end marker instruction
- Change before_prompt_build hook to return systemPrompt instead of prependContext
- This ensures the end marker instruction is injected once per session as a system prompt, not repeatedly prepended to each user message
- Add moderatorBotToken to CONFIG.example.json for documentation
2026-02-28 19:24:50 +00:00
zhi
86fdc63802 fix: moderator presence reconnect loop
Root causes:
1. Multiple plugin subsystems each called startModeratorPresence,
   creating competing WebSocket connections to the same bot token.
   Discord only allows one connection per bot → 4008 rate limit →
   infinite reconnect loop (1000+ connects → token reset by Discord)

2. Invalid session (op 9) handler called scheduleReconnect, but the
   new connection would also get kicked → cascading reconnects

Fixes:
- Singleton guard: startModeratorPresence is a no-op if already started
- cleanup() nullifies old ws handlers before creating new connection
- Stale ws check: all callbacks verify they belong to current ws
- Exponential backoff with cap (max 60s) instead of fixed 2-5s delay
- heartbeat ACK tracking: detect zombie connections
- Non-recoverable codes (4004) properly stop all reconnection
2026-02-28 18:49:17 +00:00
zhi
385990ab90 fix: turn check runs independently of evaluateDecision
Turn order should be enforced for ALL messages, not just non-human ones.
Previously, human messages bypassed turn check because they go through
human_list_sender path with shouldUseNoReply=false. Now turn check
always runs when channel has turn state.
2026-02-28 12:41:46 +00:00
zhi
a6f2be44b7 feat: moderator bot presence via Discord Gateway
Use Node.js built-in WebSocket to maintain a minimal Discord Gateway
connection for the moderator bot, keeping it 'online' with a
'Watching Moderating' status. Handles heartbeat, reconnect, and resume.

Also fix package-plugin.mjs to include moderator-presence.ts in dist.
2026-02-28 12:33:58 +00:00
zhi
fb50b62509 fix: include turn-manager.ts in package-plugin.mjs
The packaging script didn't copy turn-manager.ts to dist, causing
'Cannot find module ./turn-manager.js' at gateway startup.
2026-02-28 12:27:58 +00:00
zhi
8c8757e9a7 fix: add moderatorBotToken to plugin configSchema
The plugin's openclaw.plugin.json has additionalProperties:false,
so any config field not in the schema causes gateway startup failure.
2026-02-28 12:11:04 +00:00
zhi
54ff78cffe feat: moderator bot for turn handoff messages
Add a dedicated moderator Discord bot that sends handoff messages when
the current speaker says NO_REPLY. This solves the wakeup problem.

Flow:
1. Agent A is current speaker, receives message
2. Agent A responds with NO_REPLY
3. Plugin detects NO_REPLY in message_sent hook, advances turn to Agent B
4. Plugin sends via moderator bot: '轮到(@AgentB)了,如果没有想说的请直接回复NO_REPLY'
5. This real Discord message triggers Agent B's session
6. Turn manager allows Agent B to respond

Implementation:
- moderatorBotToken config field for the moderator bot's Discord token
- userIdFromToken() extracts Discord user ID from bot token (base64)
- resolveDiscordUserId() maps accountId → Discord user ID via account tokens
- sendModeratorMessage() calls Discord REST API directly
- message_received ignores moderator bot messages (transparent to turn state)
- Moderator bot is NOT in the turn order
2026-02-28 12:10:52 +00:00
zhi
476308d0df refactor: auto-managed turn order + dormant state + identity injection
Turn system redesign:
- Turn order auto-populated from config bindings (all bot accounts)
- No manual turnOrder config needed
- Humans (humanList) excluded from turn order automatically
- Dormant state: when all agents NO_REPLY in a cycle, currentSpeaker=null
- Reactivation: any new message wakes the system
  - Human message → start from first in order
  - Bot not in order → start from first
  - Bot in order → next after sender
- Skip already-NO_REPLY'd agents when advancing

Identity injection:
- Group chat prompts now include agent identity
- Format: '你是 {name}(Discord 账号: {accountId})'

Other:
- Remove turnOrder from ChannelPolicy (no longer configurable)
- Add TURN-WAKEUP-PROBLEM.md documenting the NO_REPLY wake-up challenge
- Update message_received to call onNewMessage with proper human detection
- Update message_sent to call onSpeakerDone with NO_REPLY tracking
2026-02-28 12:10:52 +00:00
zhi
1d8881577d feat: turn-based speaking + slash commands + enhanced prompts
1. Rename tool: whispergateway_tools → whispergate_tools

2. Turn-based speaking mechanism:
   - New turn-manager.ts maintains per-channel turn state
   - ChannelPolicy新增turnOrder字段配置发言顺序
   - before_model_resolve hook检查当前agent是否为发言人
   - 非当前发言人直接切换到no-reply模型
   - message_sent hook检测结束符或NO_REPLY时推进turn
   - message_received检测到human消息时重置turn

3. 注入提示词增强:
   - buildEndMarkerInstruction增加isGroupChat参数
   - 群聊时追加规则:与自己无关时主动回复NO_REPLY

4. Slash command支持:
   - /whispergate status - 查看频道策略
   - /whispergate turn-status - 查看轮流状态
   - /whispergate turn-advance - 手动推进轮流
   - /whispergate turn-reset - 重置轮流顺序
2026-02-28 12:10:52 +00:00
52a613fdcc Merge pull request 'fix: add default values for optional config fields' (#3) from feat/whispergate-mvp into main
Reviewed-on: orion/WhisperGate#3
2026-02-27 15:31:32 +00:00
9047d3ad8f Merge pull request 'fix: bypass DM sessions without metadata and make tool globally visible' (#4) from fix/dm-bypass-and-tool-visibility into feat/whispergate-mvp
Reviewed-on: orion/WhisperGate#4
2026-02-27 15:30:11 +00:00
zhi
75f358001b fix(rules): handle multi-byte emoji in getLastChar via Array.from
getLastChar used t[t.length-1] which only gets the trailing surrogate
of emoji like 🔚 (U+1F51A, a surrogate pair in UTF-16). This meant
end symbol matching ALWAYS failed for emoji symbols, causing every
non-humanList message to hit rule_match_no_end_symbol -> no-reply.

Fix: use Array.from(t) to correctly split by Unicode code points.
2026-02-27 15:20:05 +00:00
zhi
3749de981f fix: use configured endSymbols in injected prompt and exempt gateway keywords
- buildEndMarkerInstruction() replaces hardcoded END_MARKER_INSTRUCTION,
  dynamically using the resolved policy's endSymbols
- Instruction now explicitly exempts gateway keywords (NO_REPLY, HEARTBEAT_OK)
  from requiring end symbols
- Export resolvePolicy from rules.ts for reuse in before_prompt_build hook
2026-02-27 15:16:06 +00:00
zhi
75d659787c fix(rules): strip trailing metadata blocks before checking end symbol
getLastChar was checking the last character of the full event.prompt,
which includes Conversation/Sender metadata blocks appended by OpenClaw
after the actual message. This meant end symbols like 🔚 at the end of
the message body were invisible — the last char was always backtick or
whitespace from the metadata JSON block.

Fix: strip trailing '(untrusted metadata)' blocks before extracting
the last character. This only affects non-humanList senders (humanList
senders bypass end symbol check via human_list_sender reason).
2026-02-27 14:37:59 +00:00
zhi
f74b3978e7 fix(installer): resolve plugin path relative to repo instead of hardcoded operator path
PLUGIN_PATH defaulted to /root/.openclaw/workspace-operator/... regardless
of which workspace the installer was run from. Now resolves relative to
the script location (../dist/whispergate).
2026-02-27 14:25:12 +00:00
zhi
f23d9049a7 fix: bypass DM sessions without metadata and make tool globally visible
1. DM bypass: when neither senderId nor channelId can be extracted from
   the prompt (DM sessions lack untrusted conversation info), skip the
   no-reply gate and allow the message through with end-marker injection.

2. Tool visibility: change whispergateway_tools registration from
   optional=true to optional=false so all agents can see the tool
   without needing explicit tools.allow entries.
2026-02-27 14:14:39 +00:00
a4836097e4 fix: add default values for optional config fields
- Add default values for enableDiscordControlTool, enableWhispergatePolicyTool,
  discordControlApiBaseUrl, enableDebugLogs, debugLogChannelIds
- Merge defaults in both baseConfig and getLivePluginConfig
- Fixes issue where whispergateway_tools tool was not exposed due to missing
  config fields in openclaw.json
2026-02-26 22:16:23 +00:00
4622173787 fix: restore model after no-reply executes (needsRestore flag) 2026-02-26 13:39:43 +00:00
211a94233f chore(debug): log parsed sender/channel fields in hook decision recompute 2026-02-26 11:47:23 +00:00