-
Notifications
You must be signed in to change notification settings - Fork 150
Pubsub pattern subs #1114
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
Merged
Merged
Pubsub pattern subs #1114
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
62bb2a4
refactor(pubsub): reorganize module structure and extract encoders
leshy 4567f6a
small fix to pass CI tests
leshy 750c462
fix: update shmpubsub import path after impl/ reorganization
leshy 213ccdb
lcm class cleanup
leshy d9295ae
encoder typing fixes
leshy 978ad35
feat: add Glob and regex pattern support to LCM Topic subscriptions
leshy b0e95f2
feat: add subscribe_all and Topic.from_channel_str for typed pattern …
leshy 496d7ea
refactor: extract resolve_msg_type to dimos.msgs.helpers
leshy 4e3fa63
lcm pattern sub test rename
leshy d077c67
pattern sub tests and docs
leshy 3a0c089
bridge type spec
leshy c8bdc33
fix: bridge.py type annotations and missing subscribe_topic param
leshy 1bcb6c7
fix: resolve mypy type errors across pubsub modules
leshy 46ce9ee
pattern sub test fix
leshy 3a4f03a
pattern tests fixed
leshy 7db7176
fix: add import-untyped to mypy ignore comments for cupy/contact_gras…
leshy 4751f9e
Merge branch 'fix/mypy-import-untyped-ignores' into feature/pubsub-pa…
leshy 07144d8
fix: add DecodingError and LCM_SELF_TEST filtering to pubsub encoders
leshy 9f1ffc7
fix: resolve ruff warnings for explicit re-exports and loop variable …
leshy 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
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
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| from dimos.msgs.helpers import resolve_msg_type | ||
| from dimos.msgs.protocol import DimosMsg | ||
|
|
||
| __all__ = ["DimosMsg"] | ||
| __all__ = ["DimosMsg", "resolve_msg_type"] |
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 |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Copyright 2025-2026 Dimensional Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from functools import lru_cache | ||
| import importlib | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from dimos.msgs import DimosMsg | ||
|
|
||
|
|
||
| @lru_cache(maxsize=256) | ||
| def resolve_msg_type(type_name: str) -> type[DimosMsg] | None: | ||
| """Resolve a message type name to its class. | ||
|
|
||
| Args: | ||
| type_name: Type name in format "module.ClassName" (e.g., "geometry_msgs.Vector3") | ||
|
|
||
| Returns: | ||
| The message class or None if not found. | ||
| """ | ||
| try: | ||
| module_name, class_name = type_name.rsplit(".", 1) | ||
| except ValueError: | ||
| return None | ||
|
|
||
| # Try different import paths | ||
| import_paths = [ | ||
| f"dimos.msgs.{module_name}", | ||
| f"dimos_lcm.{module_name}", | ||
| ] | ||
|
|
||
| for path in import_paths: | ||
| try: | ||
| module = importlib.import_module(path) | ||
| return getattr(module, class_name) # type: ignore[no-any-return] | ||
| except (ImportError, AttributeError): | ||
| continue | ||
|
|
||
| return None |
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| # Copyright 2025-2026 Dimensional Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Bridge utilities for connecting pubsub systems.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import TYPE_CHECKING, Generic, Protocol, TypeVar | ||
|
|
||
| from dimos.protocol.service.spec import Service | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Callable | ||
|
|
||
| from dimos.protocol.pubsub.spec import AllPubSub, PubSub | ||
|
|
||
|
|
||
| TopicT = TypeVar("TopicT") | ||
| MsgT = TypeVar("MsgT") | ||
| TopicFrom = TypeVar("TopicFrom") | ||
| TopicTo = TypeVar("TopicTo") | ||
| MsgFrom = TypeVar("MsgFrom") | ||
| MsgTo = TypeVar("MsgTo") | ||
|
|
||
|
|
||
| class Translator(Protocol[TopicFrom, TopicTo, MsgFrom, MsgTo]): # type: ignore[misc] | ||
| """Protocol for translating topics and messages between pubsub systems.""" | ||
|
|
||
| def topic(self, topic: TopicFrom) -> TopicTo: | ||
| """Translate a topic from source to destination format.""" | ||
| ... | ||
|
|
||
| def msg(self, msg: MsgFrom) -> MsgTo: | ||
| """Translate a message from source to destination format.""" | ||
| ... | ||
|
|
||
|
|
||
| def bridge( | ||
| pubsub1: AllPubSub[TopicFrom, MsgFrom], | ||
| pubsub2: PubSub[TopicTo, MsgTo], | ||
| translator: Translator[TopicFrom, TopicTo, MsgFrom, MsgTo], | ||
| # optionally we can override subscribe_all | ||
| # and only bridge a specific part of the pubsub tree | ||
| topic_from: TopicFrom | None = None, | ||
| ) -> Callable[[], None]: | ||
| def pass_msg(msg: MsgFrom, topic: TopicFrom) -> None: | ||
| pubsub2.publish(translator.topic(topic), translator.msg(msg)) | ||
|
|
||
| # Bridge only specific messages from pubsub1 to pubsub2 | ||
| if topic_from: | ||
| return pubsub1.subscribe(topic_from, pass_msg) | ||
|
|
||
| # Bridge all messages from pubsub1 to pubsub2 | ||
| return pubsub1.subscribe_all(pass_msg) | ||
|
|
||
|
|
||
| @dataclass | ||
| class BridgeConfig(Generic[TopicFrom, TopicTo, MsgFrom, MsgTo]): | ||
| """Configuration for a one-way bridge.""" | ||
|
|
||
| source: AllPubSub[TopicFrom, MsgFrom] | ||
| destination: PubSub[TopicTo, MsgTo] | ||
| translator: Translator[TopicFrom, TopicTo, MsgFrom, MsgTo] | ||
| subscribe_topic: TopicFrom | None = None | ||
|
|
||
|
|
||
| class Bridge(Service[BridgeConfig[TopicFrom, TopicTo, MsgFrom, MsgTo]]): | ||
| """Service that bridges messages from one pubsub to another.""" | ||
|
|
||
| _unsubscribe: Callable[[], None] | None = None | ||
|
|
||
| def start(self) -> None: | ||
| super().start() | ||
| self._unsubscribe = bridge( | ||
| self.config.source, | ||
| self.config.destination, | ||
| self.config.translator, | ||
leshy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.config.subscribe_topic, | ||
| ) | ||
|
|
||
| def stop(self) -> None: | ||
| if self._unsubscribe: | ||
| self._unsubscribe() | ||
| self._unsubscribe = None | ||
| super().stop() | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.