|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Example Python worker plugin using the nemo-relay-plugin SDK.""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +from nemo_relay_plugin import ConfigDiagnostic, DiagnosticLevel, Json, PluginContext, WorkerPlugin, serve_plugin |
| 9 | + |
| 10 | + |
| 11 | +class ExamplePythonWorker(WorkerPlugin): |
| 12 | + """Small worker plugin that tags tool request JSON and emits a host mark.""" |
| 13 | + |
| 14 | + plugin_id = "examples.python_grpc_worker" |
| 15 | + |
| 16 | + def validate(self, config: Json) -> list[ConfigDiagnostic]: |
| 17 | + if isinstance(config, dict) and config.get("reject") is True: |
| 18 | + return [ |
| 19 | + ConfigDiagnostic( |
| 20 | + level=DiagnosticLevel.ERROR, |
| 21 | + code="examples.python_grpc_worker.rejected", |
| 22 | + component=self.plugin_id, |
| 23 | + field="reject", |
| 24 | + message="Python gRPC worker rejection requested", |
| 25 | + ) |
| 26 | + ] |
| 27 | + return [] |
| 28 | + |
| 29 | + def register(self, ctx: PluginContext, config: Json) -> None: |
| 30 | + del config |
| 31 | + |
| 32 | + async def tag_tool_request(tool_name: str, args: Json) -> Json: |
| 33 | + await ctx.runtime.emit_mark( |
| 34 | + "examples.python_grpc_worker.tool_request", |
| 35 | + {"tool_name": tool_name, "source": "python-grpc-worker"}, |
| 36 | + ) |
| 37 | + return _tag_json(args) |
| 38 | + |
| 39 | + ctx.register_tool_request_intercept("tag_tool_request", tag_tool_request) |
| 40 | + |
| 41 | + |
| 42 | +def _tag_json(value: Json) -> Json: |
| 43 | + if isinstance(value, dict): |
| 44 | + return {**value, "python_grpc_worker": True} |
| 45 | + return {"value": value, "python_grpc_worker": True} |
| 46 | + |
| 47 | + |
| 48 | +async def main() -> None: |
| 49 | + """Entrypoint referenced by relay-plugin.toml.""" |
| 50 | + await serve_plugin(ExamplePythonWorker()) |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + import asyncio |
| 55 | + |
| 56 | + asyncio.run(main()) |
0 commit comments