-
Notifications
You must be signed in to change notification settings - Fork 40
Fix WMQ examples (75-77) #492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mp-orkes
wants to merge
3
commits into
main
Choose a base branch
from
fix/wmq-examples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+70
−42
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,10 +4,18 @@ | |
| - wait_for_message_tool with streaming: push messages in and see the agent react | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| - Using handle.stream() to observe WAITING → processing → WAITING cycles | ||
| - runtime.send_message() to push payloads into the Workflow Message Queue | ||
| - handle.stop() ending the loop deterministically | ||
|
|
||
| The agent starts, immediately waits for a message, processes whatever it | ||
| receives (by calling wait_for_message again), then waits again. The caller | ||
| drives the conversation by sending messages and reading streamed events. | ||
| The agent starts, immediately waits for a message, answers it with respond(), | ||
| then loops back to wait_for_message. The caller drives the conversation from a | ||
| background thread — sending a task every 8 seconds — while the main thread reads | ||
| streamed events. | ||
|
|
||
| The agent's instructions tell it to never stop, so the loop only ends when the | ||
| sender calls handle.stop() — after giving the last task time to be answered. | ||
| That sets the ``_stop_requested`` workflow variable | ||
| checked by the DoWhile condition and pushes a ``{"_signal": "stop"}`` message to | ||
| unblock the pending PULL_WORKFLOW_MESSAGES. stream() then yields DONE. | ||
|
|
||
| Requirements: | ||
| - Conductor server running at http://localhost:8080 | ||
|
|
@@ -66,15 +74,16 @@ def main() -> None: | |
| print(f"Agent started: {handle.execution_id}\n") | ||
|
|
||
| # Push messages from a background thread while we stream events on the main thread. | ||
| # Wait long enough between sends for the agent to finish processing each message. | ||
| # No sleep after the last send — handle.stream() on the main thread is already the | ||
| # barrier: it blocks until DONE, which only fires once the workflow reaches a | ||
| # terminal state (after stop() sets the flag and the current iteration completes). | ||
| # Wait long enough between sends for the agent to finish processing each message — | ||
| # including after the last one. Calling stop() immediately after the final send | ||
| # would set _stop_requested while the agent is still mid-turn on that task, and the | ||
| # DoWhile would exit before it ever answers. | ||
| def sender(): | ||
| for task in TASKS: | ||
| time.sleep(8) | ||
| print(f"\n [caller] sending -> {task!r}") | ||
| runtime.send_message(handle.execution_id, {"task": task}) | ||
| time.sleep(8) | ||
| handle.stop() | ||
|
|
||
| threading.Thread(target=sender, daemon=True).start() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,36 +58,48 @@ def echo_message(value: str, topic: str, offset: int) -> str: | |
| ) | ||
|
|
||
|
|
||
| with AgentRuntime() as runtime: | ||
| handle = runtime.start(agent, "Start consuming messages from Kafka.") | ||
| print(f"Agent started: {handle.execution_id}") | ||
|
|
||
| consumer = Consumer( | ||
| { | ||
| "bootstrap.servers": KAFKA_BOOTSTRAP, | ||
| "group.id": KAFKA_GROUP, | ||
| "auto.offset.reset": "latest", | ||
| } | ||
| ) | ||
| consumer.subscribe([KAFKA_TOPIC]) | ||
| try: | ||
| while True: | ||
| msg = consumer.poll(timeout=1.0) | ||
| if msg is None: | ||
| continue | ||
| if msg.error(): | ||
| if msg.error().code() == KafkaError._PARTITION_EOF: | ||
| def main() -> None: | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| with AgentRuntime() as runtime: | ||
| handle = runtime.start(agent, "Start consuming messages from Kafka.") | ||
| print(f"Agent started: {handle.execution_id}") | ||
|
|
||
| consumer = Consumer( | ||
| { | ||
| "bootstrap.servers": KAFKA_BOOTSTRAP, | ||
| "group.id": KAFKA_GROUP, | ||
| "auto.offset.reset": "latest", | ||
| } | ||
| ) | ||
| consumer.subscribe([KAFKA_TOPIC]) | ||
| print(f"Consuming '{KAFKA_TOPIC}' from {KAFKA_BOOTSTRAP} — Ctrl+C to stop.") | ||
| try: | ||
| while True: | ||
| msg = consumer.poll(timeout=1.0) | ||
| if msg is None: | ||
| continue | ||
| raise RuntimeError(f"Kafka error: {msg.error()}") | ||
| runtime.send_message( | ||
| handle.execution_id, | ||
| { | ||
| "topic": msg.topic(), | ||
| "partition": msg.partition(), | ||
| "offset": msg.offset(), | ||
| "key": msg.key().decode("utf-8") if msg.key() else None, | ||
| "value": msg.value().decode("utf-8") if msg.value() else "", | ||
| }, | ||
| ) | ||
| finally: | ||
| consumer.close() | ||
| if msg.error(): | ||
| if msg.error().code() == KafkaError._PARTITION_EOF: | ||
| continue | ||
| raise RuntimeError(f"Kafka error: {msg.error()}") | ||
| runtime.send_message( | ||
| handle.execution_id, | ||
| { | ||
| "topic": msg.topic(), | ||
| "partition": msg.partition(), | ||
| "offset": msg.offset(), | ||
| "key": msg.key().decode("utf-8") if msg.key() else None, | ||
| "value": msg.value().decode("utf-8") if msg.value() else "", | ||
| }, | ||
| ) | ||
| except KeyboardInterrupt: | ||
| print("\nStopping agent...") | ||
| handle.stop() | ||
| finally: | ||
| consumer.close() | ||
|
|
||
|
|
||
| # Guard the runtime block: spawned tool workers re-import this module, and | ||
| # without the guard they would re-run the orchestration (multiprocessing's | ||
| # "Safe importing of main module" error). | ||
| if __name__ == "__main__": | ||
| main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.