Commit c06e0e6
authored
feat: agent-controlled reply-to via [[reply_to:message_id]] directive (#777)
* feat: agent-controlled reply-to via [[reply_to:message_id]] directive
Problem:
Agents currently cannot reply to a specific message in a thread.
All output is sent as plain messages, losing conversational context
in busy multi-bot threads.
Solution:
Two-layer change enabling agents to specify reply targets:
1. Input: SenderContext now includes message_id, so agents know
the ID of each incoming message.
2. Output: Agents can prefix their response with directives:
[[reply_to:1502606076451885136]]
Actual reply content...
OAB parses consecutive [[key:value]] lines as a header block,
strips them from content, and uses them for platform-specific
message delivery (Discord: message_reference).
Design decisions evaluated:
- Option A (chosen): Inline directives in output text. Minimal change,
no ACP protocol modification, forward-compatible (unknown keys ignored).
- Option B (deferred): ACP metadata field. Cleaner but requires protocol
change across all backends. Appropriate when more directives are needed
(ephemeral, components, attachments).
Directive format:
- Consecutive [[key:value]] lines at output start = header block
- First non-[[...]] line = content begins
- Unknown keys silently ignored (forward compatible)
- reply_to value must be numeric snowflake (validated)
- Extensible: future directives like [[ephemeral:true]] can be added
Implementation:
- adapter.rs: parse_output_directives() + OutputDirectives struct
- adapter.rs: ChatAdapter::send_message_with_reply() (default: fallback)
- discord.rs: CreateMessage::reference_message() for reply-to
- discord.rs: build_sender_context() includes msg.id
- slack.rs, cron.rs, gateway.rs: message_id field added to SenderContext
* fix: address review findings on reply-to directive
F1 (streaming path): reply_to only works in send-once mode. This is
acceptable because streaming is disabled in multi-bot threads (where
reply-to matters most). Added explanatory comment.
F2 (CRLF offset): Fixed parse_output_directives to handle both \n
and \r\n line endings correctly instead of assuming +1.
F3 (API error fallback): send_message_with_reply now catches Discord
API errors (unknown message, cross-channel reference) and falls back
to plain send_message instead of propagating the error.
* fix: reply_to directive works in streaming path too
When reply_to directive is present and streaming mode created a
placeholder, the placeholder is blanked (zero-width space) and the
real content is sent as a new reply message. This ensures reply_to
has consistent semantics regardless of streaming mode.
Behavior:
- Streaming + no reply_to: normal edit-in-place (unchanged)
- Streaming + reply_to: blank placeholder, send as reply
- Send-once + reply_to: send as reply (unchanged)
* docs: add output directives documentation
Covers:
- Directive format spec ([[key:value]] header block)
- reply_to directive usage and behavior
- SenderContext.message_id for getting message IDs
- Multi-agent use case example
- Comparison with OpenClaw/Hermes Agent
- Future directives roadmap
* docs: add agent-controlled reply-to to README features
* test: add unit tests for parse_output_directives
8 tests covering:
- Normal reply_to parsing
- No directives (plain content)
- Multiple directives (unknown keys ignored)
- Invalid reply_to (non-numeric) rejected
- Empty reply_to rejected
- CRLF line endings handled correctly
- Directive-only output (no content)
- Non-directive first line stops parsing
* fix: simplify streaming + reply_to path (remove redundant edits)
Per review: the three sequential edits were wasteful and caused
brief content duplication. Simplified to:
1. Single edit to zero-width space (hide placeholder)
2. Send real content as reply
No more flicker or ghost content.
* fix: add fallback logging + 2 more edge case tests
- Discord: tracing::warn on reply_to fallback (was silent)
- Test: duplicate reply_to (last wins)
- Test: CRLF with multiple directives
Total directive tests: 10
* fix: relax message_id validation for cross-platform compatibility
Was: numeric-only (Discord snowflake)
Now: alphanumeric + dots + hyphens + underscores, max 64 chars
This allows:
- Discord snowflakes: 1502606076451885136
- Slack ts: 1234567890.123456
- UUID-style: 550e8400-e29b-41d4-a716-446655440000
Rejects: whitespace, control chars, empty, >64 chars
Added test: parse_slack_ts_format_accepted
Updated test: rejection now checks whitespace (not hyphens)
* fix: guard against empty content after directive stripping
If agent output is directive-only (e.g. just [[reply_to:123]] with
no actual content), stripped_content would be empty. Discord rejects
empty messages, causing silent failures.
Fix: if content is empty/whitespace after stripping, fall back to
'_(no response)_' — same behavior as when agent returns no text.
* fix: delete placeholder instead of zero-width space on reply_to
Adds delete_message to ChatAdapter trait (default no-op) and
implements it for Discord. Streaming + reply_to path now deletes
the placeholder entirely instead of editing to zero-width space.
No more ghost empty bubbles in Discord threads.
* fix: delete_message default falls back to edit zero-width space
Per review: default no-op would leave placeholder visible if delete
fails or adapter doesn't support it. Default now edits to zero-width
space (existing behavior), Discord overrides with real delete.
* fix: clippy errors (unnecessary_unwrap + too_many_arguments)
- Replace is_some() + unwrap() with if let Some(ref reply_id)
- Allow clippy::too_many_arguments on build_sender_context (8 params)
* fix: log unknown directives at debug level
Helps agent developers diagnose typos like [[reply-to:...]] vs
[[reply_to:...]]. Forward compatible: unknown keys still ignored
at runtime, just logged for debugging.
* fix: remaining clippy unnecessary_unwrap in streaming path
* docs: note Slack reply_to is parsed but not yet implemented
* docs: remove Future Directives section (avoid premature commitment)
* fix: send-before-delete order + parse directives before markdown
F1: Send reply first, then delete placeholder. If send fails,
placeholder remains visible (no message loss for user).
F2: Parse directives before markdown::convert_tables. Directives
are meta-layer and should be stripped before content transforms.
* fix: parse directives from raw text_buf + check send before delete
F1: Directives now parsed from raw text_buf BEFORE compose_display,
ensuring tool call output cannot interfere with directive parsing.
F3: Send result is checked — placeholder only deleted if first chunk
sends successfully. On send failure, placeholder remains visible
(no message loss).
* fix: [[X]] without colon stops parsing (preserves agent content)
B2: Lines like [[Note]], [[Summary]], [[Thought]] (no colon) are
legitimate agent content, not directives. Parser now only advances
content_start when split_once(':') succeeds. Without colon → break.
Added test: parse_bracket_without_colon_preserved
* docs: fix value spec accuracy + document duplicate-key behavior
D1: Value spec now correctly describes cross-platform validation
(non-empty, ≤64 chars, no whitespace) instead of 'numeric only'.
D2: Added rule: 'If the same key appears multiple times, last wins.'
Also: clarified [[X]] without colon stops parsing.
* fix: add warn logging on send/delete failure + align docs with parser
- tracing::warn on reply send failure (ops can diagnose permission issues)
- tracing::warn on placeholder delete failure
- Docs: 'no whitespace' → 'ASCII alphanumeric plus ., -, _' (matches code)
---------
Co-authored-by: chaodu-agent <chaodu-agent@users.noreply.github.com>1 parent 7bf7f63 commit c06e0e6
7 files changed
Lines changed: 361 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| 45 | + | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
14 | 71 | | |
15 | 72 | | |
16 | 73 | | |
| |||
106 | 163 | | |
107 | 164 | | |
108 | 165 | | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
109 | 170 | | |
110 | 171 | | |
111 | 172 | | |
| |||
141 | 202 | | |
142 | 203 | | |
143 | 204 | | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
144 | 223 | | |
145 | 224 | | |
146 | 225 | | |
| |||
536 | 615 | | |
537 | 616 | | |
538 | 617 | | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
539 | 624 | | |
540 | 625 | | |
541 | 626 | | |
| |||
554 | 639 | | |
555 | 640 | | |
556 | 641 | | |
557 | | - | |
558 | | - | |
559 | | - | |
560 | | - | |
561 | | - | |
562 | | - | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
563 | 677 | | |
564 | 678 | | |
565 | 679 | | |
| 680 | + | |
| 681 | + | |
566 | 682 | | |
567 | | - | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
568 | 697 | | |
569 | 698 | | |
570 | 699 | | |
| |||
879 | 1008 | | |
880 | 1009 | | |
881 | 1010 | | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
399 | 399 | | |
400 | 400 | | |
401 | 401 | | |
| 402 | + | |
402 | 403 | | |
403 | 404 | | |
404 | 405 | | |
| |||
0 commit comments