rune-framework 1.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
rune/__init__.py ADDED
@@ -0,0 +1,7 @@
1
+ """Rune SDK -- Official Python Caster SDK for the Rune framework."""
2
+
3
+ __version__ = "1.1.1"
4
+
5
+ from .caster import Caster
6
+ from .types import RuneConfig, RuneContext, FileAttachment
7
+ from .stream import StreamSender
@@ -0,0 +1 @@
1
+ """Generated protobuf/gRPC code. Do not edit manually."""
File without changes
File without changes
File without changes
@@ -0,0 +1,74 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Generated by the protocol buffer compiler. DO NOT EDIT!
3
+ # NO CHECKED-IN PROTOBUF GENCODE
4
+ # source: rune/wire/v1/rune.proto
5
+ # Protobuf Python Version: 6.31.1
6
+ """Generated protocol buffer code."""
7
+ from google.protobuf import descriptor as _descriptor
8
+ from google.protobuf import descriptor_pool as _descriptor_pool
9
+ from google.protobuf import runtime_version as _runtime_version
10
+ from google.protobuf import symbol_database as _symbol_database
11
+ from google.protobuf.internal import builder as _builder
12
+ _runtime_version.ValidateProtobufRuntimeVersion(
13
+ _runtime_version.Domain.PUBLIC,
14
+ 6,
15
+ 31,
16
+ 1,
17
+ '',
18
+ 'rune/wire/v1/rune.proto'
19
+ )
20
+ # @@protoc_insertion_point(imports)
21
+
22
+ _sym_db = _symbol_database.Default()
23
+
24
+
25
+
26
+
27
+ DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x17rune/wire/v1/rune.proto\x12\x0crune.wire.v1\"\xc5\x03\n\x0eSessionMessage\x12,\n\x06\x61ttach\x18\n \x01(\x0b\x32\x1a.rune.wire.v1.CasterAttachH\x00\x12,\n\x06\x64\x65tach\x18\x0b \x01(\x0b\x32\x1a.rune.wire.v1.CasterDetachH\x00\x12-\n\x06result\x18\x0c \x01(\x0b\x32\x1b.rune.wire.v1.ExecuteResultH\x00\x12\x31\n\x0cstream_event\x18\r \x01(\x0b\x32\x19.rune.wire.v1.StreamEventH\x00\x12-\n\nstream_end\x18\x0e \x01(\x0b\x32\x17.rune.wire.v1.StreamEndH\x00\x12-\n\nattach_ack\x18\x14 \x01(\x0b\x32\x17.rune.wire.v1.AttachAckH\x00\x12/\n\x07\x65xecute\x18\x15 \x01(\x0b\x32\x1c.rune.wire.v1.ExecuteRequestH\x00\x12-\n\x06\x63\x61ncel\x18\x16 \x01(\x0b\x32\x1b.rune.wire.v1.CancelRequestH\x00\x12,\n\theartbeat\x18\x1e \x01(\x0b\x32\x17.rune.wire.v1.HeartbeatH\x00\x42\t\n\x07payload\"\xdb\x01\n\x0c\x43\x61sterAttach\x12\x11\n\tcaster_id\x18\x01 \x01(\t\x12,\n\x05runes\x18\x02 \x03(\x0b\x32\x1d.rune.wire.v1.RuneDeclaration\x12\x36\n\x06labels\x18\x03 \x03(\x0b\x32&.rune.wire.v1.CasterAttach.LabelsEntry\x12\x16\n\x0emax_concurrent\x18\x04 \x01(\r\x12\x0b\n\x03key\x18\x05 \x01(\t\x1a-\n\x0bLabelsEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\"I\n\tAttachAck\x12\x10\n\x08\x61\x63\x63\x65pted\x18\x01 \x01(\x08\x12\x0e\n\x06reason\x18\x02 \x01(\t\x12\x1a\n\x12supported_features\x18\x03 \x03(\t\"\x1e\n\x0c\x43\x61sterDetach\x12\x0e\n\x06reason\x18\x01 \x01(\t\"\xc5\x01\n\x0fRuneDeclaration\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x0f\n\x07version\x18\x02 \x01(\t\x12\x13\n\x0b\x64\x65scription\x18\x03 \x01(\t\x12\x14\n\x0cinput_schema\x18\x04 \x01(\t\x12\x15\n\routput_schema\x18\x05 \x01(\t\x12\x17\n\x0fsupports_stream\x18\x06 \x01(\x08\x12&\n\x04gate\x18\x07 \x01(\x0b\x32\x18.rune.wire.v1.GateConfig\x12\x10\n\x08priority\x18\x08 \x01(\x05\"*\n\nGateConfig\x12\x0c\n\x04path\x18\x01 \x01(\t\x12\x0e\n\x06method\x18\x02 \x01(\t\"C\n\x0e\x46ileAttachment\x12\x10\n\x08\x66ilename\x18\x01 \x01(\t\x12\x0c\n\x04\x64\x61ta\x18\x02 \x01(\x0c\x12\x11\n\tmime_type\x18\x03 \x01(\t\"\xf9\x01\n\x0e\x45xecuteRequest\x12\x12\n\nrequest_id\x18\x01 \x01(\t\x12\x11\n\trune_name\x18\x02 \x01(\t\x12\r\n\x05input\x18\x03 \x01(\x0c\x12:\n\x07\x63ontext\x18\x04 \x03(\x0b\x32).rune.wire.v1.ExecuteRequest.ContextEntry\x12\x12\n\ntimeout_ms\x18\x05 \x01(\r\x12\x31\n\x0b\x61ttachments\x18\x06 \x03(\x0b\x32\x1c.rune.wire.v1.FileAttachment\x1a.\n\x0c\x43ontextEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\"3\n\rCancelRequest\x12\x12\n\nrequest_id\x18\x01 \x01(\t\x12\x0e\n\x06reason\x18\x02 \x01(\t\"\xb6\x01\n\rExecuteResult\x12\x12\n\nrequest_id\x18\x01 \x01(\t\x12$\n\x06status\x18\x02 \x01(\x0e\x32\x14.rune.wire.v1.Status\x12\x0e\n\x06output\x18\x03 \x01(\x0c\x12(\n\x05\x65rror\x18\x04 \x01(\x0b\x32\x19.rune.wire.v1.ErrorDetail\x12\x31\n\x0b\x61ttachments\x18\x05 \x03(\x0b\x32\x1c.rune.wire.v1.FileAttachment\"C\n\x0bStreamEvent\x12\x12\n\nrequest_id\x18\x01 \x01(\t\x12\x0c\n\x04\x64\x61ta\x18\x02 \x01(\x0c\x12\x12\n\nevent_type\x18\x03 \x01(\t\"o\n\tStreamEnd\x12\x12\n\nrequest_id\x18\x01 \x01(\t\x12$\n\x06status\x18\x02 \x01(\x0e\x32\x14.rune.wire.v1.Status\x12(\n\x05\x65rror\x18\x03 \x01(\x0b\x32\x19.rune.wire.v1.ErrorDetail\"!\n\tHeartbeat\x12\x14\n\x0ctimestamp_ms\x18\x01 \x01(\x04\"=\n\x0b\x45rrorDetail\x12\x0c\n\x04\x63ode\x18\x01 \x01(\t\x12\x0f\n\x07message\x18\x02 \x01(\t\x12\x0f\n\x07\x64\x65tails\x18\x03 \x01(\x0c*_\n\x06Status\x12\x16\n\x12STATUS_UNSPECIFIED\x10\x00\x12\x14\n\x10STATUS_COMPLETED\x10\x01\x12\x11\n\rSTATUS_FAILED\x10\x02\x12\x14\n\x10STATUS_CANCELLED\x10\x03\x32X\n\x0bRuneService\x12I\n\x07Session\x12\x1c.rune.wire.v1.SessionMessage\x1a\x1c.rune.wire.v1.SessionMessage(\x01\x30\x01\x62\x06proto3')
28
+
29
+ _globals = globals()
30
+ _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)
31
+ _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'rune.wire.v1.rune_pb2', _globals)
32
+ if not _descriptor._USE_C_DESCRIPTORS:
33
+ DESCRIPTOR._loaded_options = None
34
+ _globals['_CASTERATTACH_LABELSENTRY']._loaded_options = None
35
+ _globals['_CASTERATTACH_LABELSENTRY']._serialized_options = b'8\001'
36
+ _globals['_EXECUTEREQUEST_CONTEXTENTRY']._loaded_options = None
37
+ _globals['_EXECUTEREQUEST_CONTEXTENTRY']._serialized_options = b'8\001'
38
+ _globals['_STATUS']._serialized_start=1909
39
+ _globals['_STATUS']._serialized_end=2004
40
+ _globals['_SESSIONMESSAGE']._serialized_start=42
41
+ _globals['_SESSIONMESSAGE']._serialized_end=495
42
+ _globals['_CASTERATTACH']._serialized_start=498
43
+ _globals['_CASTERATTACH']._serialized_end=717
44
+ _globals['_CASTERATTACH_LABELSENTRY']._serialized_start=672
45
+ _globals['_CASTERATTACH_LABELSENTRY']._serialized_end=717
46
+ _globals['_ATTACHACK']._serialized_start=719
47
+ _globals['_ATTACHACK']._serialized_end=792
48
+ _globals['_CASTERDETACH']._serialized_start=794
49
+ _globals['_CASTERDETACH']._serialized_end=824
50
+ _globals['_RUNEDECLARATION']._serialized_start=827
51
+ _globals['_RUNEDECLARATION']._serialized_end=1024
52
+ _globals['_GATECONFIG']._serialized_start=1026
53
+ _globals['_GATECONFIG']._serialized_end=1068
54
+ _globals['_FILEATTACHMENT']._serialized_start=1070
55
+ _globals['_FILEATTACHMENT']._serialized_end=1137
56
+ _globals['_EXECUTEREQUEST']._serialized_start=1140
57
+ _globals['_EXECUTEREQUEST']._serialized_end=1389
58
+ _globals['_EXECUTEREQUEST_CONTEXTENTRY']._serialized_start=1343
59
+ _globals['_EXECUTEREQUEST_CONTEXTENTRY']._serialized_end=1389
60
+ _globals['_CANCELREQUEST']._serialized_start=1391
61
+ _globals['_CANCELREQUEST']._serialized_end=1442
62
+ _globals['_EXECUTERESULT']._serialized_start=1445
63
+ _globals['_EXECUTERESULT']._serialized_end=1627
64
+ _globals['_STREAMEVENT']._serialized_start=1629
65
+ _globals['_STREAMEVENT']._serialized_end=1696
66
+ _globals['_STREAMEND']._serialized_start=1698
67
+ _globals['_STREAMEND']._serialized_end=1809
68
+ _globals['_HEARTBEAT']._serialized_start=1811
69
+ _globals['_HEARTBEAT']._serialized_end=1844
70
+ _globals['_ERRORDETAIL']._serialized_start=1846
71
+ _globals['_ERRORDETAIL']._serialized_end=1907
72
+ _globals['_RUNESERVICE']._serialized_start=2006
73
+ _globals['_RUNESERVICE']._serialized_end=2094
74
+ # @@protoc_insertion_point(module_scope)
@@ -0,0 +1,100 @@
1
+ # Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
2
+ """Client and server classes corresponding to protobuf-defined services."""
3
+ import grpc
4
+ import warnings
5
+
6
+ from rune._proto.rune.wire.v1 import rune_pb2 as rune_dot_wire_dot_v1_dot_rune__pb2
7
+
8
+ GRPC_GENERATED_VERSION = '1.78.0'
9
+ GRPC_VERSION = grpc.__version__
10
+ _version_not_supported = False
11
+
12
+ try:
13
+ from grpc._utilities import first_version_is_lower
14
+ _version_not_supported = first_version_is_lower(GRPC_VERSION, GRPC_GENERATED_VERSION)
15
+ except ImportError:
16
+ _version_not_supported = True
17
+
18
+ if _version_not_supported:
19
+ raise RuntimeError(
20
+ f'The grpc package installed is at version {GRPC_VERSION},'
21
+ + ' but the generated code in rune/wire/v1/rune_pb2_grpc.py depends on'
22
+ + f' grpcio>={GRPC_GENERATED_VERSION}.'
23
+ + f' Please upgrade your grpc module to grpcio>={GRPC_GENERATED_VERSION}'
24
+ + f' or downgrade your generated code using grpcio-tools<={GRPC_VERSION}.'
25
+ )
26
+
27
+
28
+ class RuneServiceStub(object):
29
+ """Caster 与 Runtime 的双向流
30
+ """
31
+
32
+ def __init__(self, channel):
33
+ """Constructor.
34
+
35
+ Args:
36
+ channel: A grpc.Channel.
37
+ """
38
+ self.Session = channel.stream_stream(
39
+ '/rune.wire.v1.RuneService/Session',
40
+ request_serializer=rune_dot_wire_dot_v1_dot_rune__pb2.SessionMessage.SerializeToString,
41
+ response_deserializer=rune_dot_wire_dot_v1_dot_rune__pb2.SessionMessage.FromString,
42
+ _registered_method=True)
43
+
44
+
45
+ class RuneServiceServicer(object):
46
+ """Caster 与 Runtime 的双向流
47
+ """
48
+
49
+ def Session(self, request_iterator, context):
50
+ """Missing associated documentation comment in .proto file."""
51
+ context.set_code(grpc.StatusCode.UNIMPLEMENTED)
52
+ context.set_details('Method not implemented!')
53
+ raise NotImplementedError('Method not implemented!')
54
+
55
+
56
+ def add_RuneServiceServicer_to_server(servicer, server):
57
+ rpc_method_handlers = {
58
+ 'Session': grpc.stream_stream_rpc_method_handler(
59
+ servicer.Session,
60
+ request_deserializer=rune_dot_wire_dot_v1_dot_rune__pb2.SessionMessage.FromString,
61
+ response_serializer=rune_dot_wire_dot_v1_dot_rune__pb2.SessionMessage.SerializeToString,
62
+ ),
63
+ }
64
+ generic_handler = grpc.method_handlers_generic_handler(
65
+ 'rune.wire.v1.RuneService', rpc_method_handlers)
66
+ server.add_generic_rpc_handlers((generic_handler,))
67
+ server.add_registered_method_handlers('rune.wire.v1.RuneService', rpc_method_handlers)
68
+
69
+
70
+ # This class is part of an EXPERIMENTAL API.
71
+ class RuneService(object):
72
+ """Caster 与 Runtime 的双向流
73
+ """
74
+
75
+ @staticmethod
76
+ def Session(request_iterator,
77
+ target,
78
+ options=(),
79
+ channel_credentials=None,
80
+ call_credentials=None,
81
+ insecure=False,
82
+ compression=None,
83
+ wait_for_ready=None,
84
+ timeout=None,
85
+ metadata=None):
86
+ return grpc.experimental.stream_stream(
87
+ request_iterator,
88
+ target,
89
+ '/rune.wire.v1.RuneService/Session',
90
+ rune_dot_wire_dot_v1_dot_rune__pb2.SessionMessage.SerializeToString,
91
+ rune_dot_wire_dot_v1_dot_rune__pb2.SessionMessage.FromString,
92
+ options,
93
+ channel_credentials,
94
+ insecure,
95
+ call_credentials,
96
+ compression,
97
+ wait_for_ready,
98
+ timeout,
99
+ metadata,
100
+ _registered_method=True)
rune/caster.py ADDED
@@ -0,0 +1,454 @@
1
+ """Caster -- connects to Rune runtime and executes registered handlers."""
2
+ from __future__ import annotations
3
+
4
+ import asyncio
5
+ import inspect
6
+ import json
7
+ import logging
8
+ import threading
9
+ import time
10
+ from typing import Callable, Awaitable
11
+
12
+ import grpc
13
+
14
+ from .types import RuneConfig, RuneContext, FileAttachment
15
+ from .stream import StreamSender
16
+ from .handler import RegisteredRune, OnceHandler
17
+
18
+ # Ensure _proto dir is on sys.path before importing generated code.
19
+ from . import _proto # noqa: F401
20
+ from ._proto.rune.wire.v1 import rune_pb2, rune_pb2_grpc
21
+
22
+ logger = logging.getLogger("rune")
23
+
24
+
25
+ class Caster:
26
+ """Connects to a Rune runtime server and executes registered handlers."""
27
+
28
+ def __init__(
29
+ self,
30
+ addr: str = "localhost:50070",
31
+ caster_id: str = "python-caster",
32
+ max_concurrent: int = 10,
33
+ reconnect_base_delay: float = 1.0,
34
+ reconnect_max_delay: float = 30.0,
35
+ heartbeat_interval: float = 10.0,
36
+ labels: dict[str, str] | None = None,
37
+ api_key: str | None = None,
38
+ ) -> None:
39
+ self._addr = addr
40
+ self._caster_id = caster_id
41
+ self._max_concurrent = max_concurrent
42
+ self._runes: dict[str, RegisteredRune] = {}
43
+ self._cancelled: set[str] = set()
44
+ self._reconnect_base_delay = reconnect_base_delay
45
+ self._reconnect_max_delay = reconnect_max_delay
46
+ self._heartbeat_interval = heartbeat_interval
47
+ self._labels = labels or {}
48
+ self._api_key = api_key
49
+ self._shutdown = threading.Event()
50
+
51
+ # ------------------------------------------------------------------
52
+ # Decorator API
53
+ # ------------------------------------------------------------------
54
+
55
+ def rune(
56
+ self,
57
+ name: str,
58
+ *,
59
+ version: str = "0.0.0",
60
+ description: str = "",
61
+ gate: str | None = None,
62
+ gate_method: str = "POST",
63
+ input_schema: dict | None = None,
64
+ output_schema: dict | None = None,
65
+ priority: int = 0,
66
+ ) -> Callable:
67
+ """Decorator to register a unary rune handler."""
68
+
69
+ def decorator(fn: OnceHandler) -> OnceHandler:
70
+ config = RuneConfig(
71
+ name=name,
72
+ version=version,
73
+ description=description,
74
+ supports_stream=False,
75
+ gate=gate,
76
+ gate_method=gate_method,
77
+ input_schema=input_schema,
78
+ output_schema=output_schema,
79
+ priority=priority,
80
+ )
81
+ accepts_files = _handler_accepts_files(fn)
82
+ self._runes[name] = RegisteredRune(
83
+ config=config, handler=fn, is_stream=False, accepts_files=accepts_files,
84
+ )
85
+ return fn
86
+
87
+ return decorator
88
+
89
+ def stream_rune(
90
+ self,
91
+ name: str,
92
+ *,
93
+ version: str = "0.0.0",
94
+ description: str = "",
95
+ gate: str | None = None,
96
+ gate_method: str = "POST",
97
+ input_schema: dict | None = None,
98
+ output_schema: dict | None = None,
99
+ priority: int = 0,
100
+ ) -> Callable:
101
+ """Decorator to register a streaming rune handler."""
102
+
103
+ def decorator(fn: Callable) -> Callable:
104
+ config = RuneConfig(
105
+ name=name,
106
+ version=version,
107
+ description=description,
108
+ supports_stream=True,
109
+ gate=gate,
110
+ gate_method=gate_method,
111
+ input_schema=input_schema,
112
+ output_schema=output_schema,
113
+ priority=priority,
114
+ )
115
+ accepts_files = _handler_accepts_files(fn)
116
+ self._runes[name] = RegisteredRune(
117
+ config=config, handler=fn, is_stream=True, accepts_files=accepts_files,
118
+ )
119
+ return fn
120
+
121
+ return decorator
122
+
123
+ # ------------------------------------------------------------------
124
+ # Run
125
+ # ------------------------------------------------------------------
126
+
127
+ def run(self) -> None:
128
+ """Start the caster (blocking). Runs asyncio event loop with auto-reconnect."""
129
+ asyncio.run(self._run_with_reconnect())
130
+
131
+ def stop(self) -> None:
132
+ """Signal the caster to stop its run loop.
133
+
134
+ Safe to call from any thread. The ``run()`` method will return
135
+ shortly after this is called.
136
+ """
137
+ self._shutdown.set()
138
+
139
+ async def _run_with_reconnect(self) -> None:
140
+ """Reconnect loop with exponential backoff."""
141
+ delay = self._reconnect_base_delay
142
+ while not self._shutdown.is_set():
143
+ try:
144
+ await self._session()
145
+ # Session ended normally (detach) -- don't reconnect
146
+ logger.info("session ended normally")
147
+ break
148
+ except grpc.aio.AioRpcError as e:
149
+ if self._shutdown.is_set():
150
+ break
151
+ logger.warning("gRPC error: %s, reconnecting in %.1fs", e.code(), delay)
152
+ except Exception as e:
153
+ if self._shutdown.is_set():
154
+ break
155
+ logger.warning("session error: %s, reconnecting in %.1fs", e, delay)
156
+
157
+ # Wait for delay or shutdown, whichever comes first
158
+ if self._shutdown.wait(timeout=delay):
159
+ break # shutdown requested
160
+ delay = min(delay * 2, self._reconnect_max_delay)
161
+ logger.info("reconnecting to %s ...", self._addr)
162
+
163
+ # ------------------------------------------------------------------
164
+ # Attach message builder
165
+ # ------------------------------------------------------------------
166
+
167
+ def _build_attach_message(self) -> rune_pb2.SessionMessage:
168
+ """Build the CasterAttach session message."""
169
+ declarations = []
170
+ for registered in self._runes.values():
171
+ decl = rune_pb2.RuneDeclaration(
172
+ name=registered.config.name,
173
+ version=registered.config.version,
174
+ description=registered.config.description,
175
+ supports_stream=registered.config.supports_stream,
176
+ input_schema=json.dumps(registered.config.input_schema) if registered.config.input_schema else "",
177
+ output_schema=json.dumps(registered.config.output_schema) if registered.config.output_schema else "",
178
+ priority=registered.config.priority,
179
+ )
180
+ if registered.config.gate:
181
+ decl.gate.CopyFrom(
182
+ rune_pb2.GateConfig(
183
+ path=registered.config.gate,
184
+ method=registered.config.gate_method,
185
+ )
186
+ )
187
+ declarations.append(decl)
188
+
189
+ return rune_pb2.SessionMessage(
190
+ attach=rune_pb2.CasterAttach(
191
+ caster_id=self._caster_id,
192
+ runes=declarations,
193
+ labels=self._labels,
194
+ max_concurrent=self._max_concurrent,
195
+ key=self._api_key or "",
196
+ )
197
+ )
198
+
199
+ # ------------------------------------------------------------------
200
+ # Session
201
+ # ------------------------------------------------------------------
202
+
203
+ async def _session(self) -> None:
204
+ """Run one gRPC session."""
205
+ async with grpc.aio.insecure_channel(self._addr) as channel:
206
+ stub = rune_pb2_grpc.RuneServiceStub(channel)
207
+
208
+ # Bidirectional stream
209
+ outbound_queue: asyncio.Queue[rune_pb2.SessionMessage | None] = asyncio.Queue()
210
+
211
+ async def outbound_iter():
212
+ while True:
213
+ msg = await outbound_queue.get()
214
+ if msg is None:
215
+ break
216
+ yield msg
217
+
218
+ call = stub.Session(outbound_iter())
219
+
220
+ # Build and send attach message
221
+ attach_msg = self._build_attach_message()
222
+ await outbound_queue.put(attach_msg)
223
+
224
+ # Start heartbeat task
225
+ hb_task = asyncio.create_task(self._heartbeat_loop(outbound_queue))
226
+
227
+ # Monitor shutdown event to cancel the gRPC call
228
+ async def _watch_shutdown():
229
+ loop = asyncio.get_running_loop()
230
+ await loop.run_in_executor(None, self._shutdown.wait)
231
+ call.cancel()
232
+
233
+ shutdown_task = asyncio.create_task(_watch_shutdown())
234
+
235
+ try:
236
+ async for msg in call:
237
+ if self._shutdown.is_set():
238
+ break
239
+
240
+ payload = msg.WhichOneof("payload")
241
+
242
+ if payload == "attach_ack":
243
+ if msg.attach_ack.accepted:
244
+ logger.info(
245
+ "attached to %s, caster_id=%s",
246
+ self._addr,
247
+ self._caster_id,
248
+ )
249
+ else:
250
+ logger.error("attach rejected: %s", msg.attach_ack.reason)
251
+ break
252
+
253
+ elif payload == "execute":
254
+ req = msg.execute
255
+ asyncio.create_task(self._handle_execute(req, outbound_queue))
256
+
257
+ elif payload == "cancel":
258
+ self._cancelled.add(msg.cancel.request_id)
259
+ logger.info("cancel requested: %s", msg.cancel.request_id)
260
+
261
+ elif payload == "heartbeat":
262
+ pass # server heartbeat received
263
+
264
+ finally:
265
+ shutdown_task.cancel()
266
+ hb_task.cancel()
267
+ await outbound_queue.put(None) # stop outbound iter
268
+
269
+ # ------------------------------------------------------------------
270
+ # Heartbeat
271
+ # ------------------------------------------------------------------
272
+
273
+ async def _heartbeat_loop(self, queue: asyncio.Queue) -> None:
274
+ """Send periodic heartbeat."""
275
+ try:
276
+ while True:
277
+ await asyncio.sleep(self._heartbeat_interval)
278
+ msg = rune_pb2.SessionMessage(
279
+ heartbeat=rune_pb2.Heartbeat(
280
+ timestamp_ms=int(time.time() * 1000),
281
+ )
282
+ )
283
+ await queue.put(msg)
284
+ except asyncio.CancelledError:
285
+ pass
286
+
287
+ # ------------------------------------------------------------------
288
+ # Execute dispatch
289
+ # ------------------------------------------------------------------
290
+
291
+ async def _handle_execute(
292
+ self,
293
+ req: rune_pb2.ExecuteRequest,
294
+ outbound_queue: asyncio.Queue,
295
+ ) -> None:
296
+ """Execute a rune handler (once or stream)."""
297
+ registered = self._runes.get(req.rune_name)
298
+ if registered is None:
299
+ await outbound_queue.put(
300
+ rune_pb2.SessionMessage(
301
+ result=rune_pb2.ExecuteResult(
302
+ request_id=req.request_id,
303
+ status=rune_pb2.STATUS_FAILED,
304
+ error=rune_pb2.ErrorDetail(
305
+ code="NOT_FOUND",
306
+ message=f"rune '{req.rune_name}' not found",
307
+ ),
308
+ )
309
+ )
310
+ )
311
+ return
312
+
313
+ ctx = RuneContext(
314
+ rune_name=req.rune_name,
315
+ request_id=req.request_id,
316
+ context=dict(req.context),
317
+ )
318
+
319
+ try:
320
+ if registered.is_stream:
321
+ await self._execute_stream(registered, ctx, req, outbound_queue)
322
+ else:
323
+ await self._execute_once(registered, ctx, req, outbound_queue)
324
+ except Exception as e:
325
+ logger.error("handler error for %s: %s", req.rune_name, e)
326
+ await outbound_queue.put(
327
+ rune_pb2.SessionMessage(
328
+ result=rune_pb2.ExecuteResult(
329
+ request_id=req.request_id,
330
+ status=rune_pb2.STATUS_FAILED,
331
+ error=rune_pb2.ErrorDetail(
332
+ code="EXECUTION_FAILED",
333
+ message=str(e),
334
+ ),
335
+ )
336
+ )
337
+ )
338
+ finally:
339
+ # NF-8: Always clean up the cancelled set to prevent unbounded growth
340
+ self._cancelled.discard(req.request_id)
341
+
342
+ async def _execute_once(
343
+ self,
344
+ registered: RegisteredRune,
345
+ ctx: RuneContext,
346
+ req: rune_pb2.ExecuteRequest,
347
+ outbound_queue: asyncio.Queue,
348
+ ) -> None:
349
+ """Execute unary handler."""
350
+ if registered.accepts_files:
351
+ attachments = _extract_attachments(req)
352
+ result = registered.handler(ctx, bytes(req.input), attachments)
353
+ else:
354
+ result = registered.handler(ctx, bytes(req.input))
355
+ output = await _ensure_awaitable(result)
356
+
357
+ # Check if cancelled during execution
358
+ if req.request_id in self._cancelled:
359
+ self._cancelled.discard(req.request_id)
360
+ logger.info("request %s cancelled during execution, discarding", req.request_id)
361
+ return
362
+
363
+ await outbound_queue.put(
364
+ rune_pb2.SessionMessage(
365
+ result=rune_pb2.ExecuteResult(
366
+ request_id=req.request_id,
367
+ status=rune_pb2.STATUS_COMPLETED,
368
+ output=output,
369
+ )
370
+ )
371
+ )
372
+
373
+ async def _execute_stream(
374
+ self,
375
+ registered: RegisteredRune,
376
+ ctx: RuneContext,
377
+ req: rune_pb2.ExecuteRequest,
378
+ outbound_queue: asyncio.Queue,
379
+ ) -> None:
380
+ """Execute streaming handler."""
381
+
382
+ async def send_event(data: bytes) -> None:
383
+ if req.request_id in self._cancelled:
384
+ raise RuntimeError("cancelled")
385
+ await outbound_queue.put(
386
+ rune_pb2.SessionMessage(
387
+ stream_event=rune_pb2.StreamEvent(
388
+ request_id=req.request_id,
389
+ data=data,
390
+ )
391
+ )
392
+ )
393
+
394
+ sender = StreamSender(send_event)
395
+ if registered.accepts_files:
396
+ attachments = _extract_attachments(req)
397
+ result = registered.handler(ctx, bytes(req.input), attachments, sender)
398
+ else:
399
+ result = registered.handler(ctx, bytes(req.input), sender)
400
+ await _ensure_awaitable(result)
401
+
402
+ # Send StreamEnd
403
+ await outbound_queue.put(
404
+ rune_pb2.SessionMessage(
405
+ stream_end=rune_pb2.StreamEnd(
406
+ request_id=req.request_id,
407
+ status=rune_pb2.STATUS_COMPLETED,
408
+ )
409
+ )
410
+ )
411
+
412
+
413
+ # ------------------------------------------------------------------
414
+ # Helpers (module-level)
415
+ # ------------------------------------------------------------------
416
+
417
+
418
+ async def _ensure_awaitable(result):
419
+ """Await a coroutine or return a sync result directly.
420
+
421
+ If the handler is a regular (non-async) function its return value
422
+ is *not* a coroutine and attempting ``await`` on it raises
423
+ ``TypeError``. This wrapper detects that case and returns the
424
+ value as-is, so that sync handlers work transparently.
425
+ """
426
+ if inspect.isawaitable(result):
427
+ return await result
428
+ return result
429
+
430
+
431
+ def _handler_accepts_files(fn: Callable) -> bool:
432
+ """Check if handler function has a 'files' parameter (3rd arg for once, 3rd for stream)."""
433
+ sig = inspect.signature(fn)
434
+ params = list(sig.parameters.keys())
435
+ # Once handler: (ctx, input) or (ctx, input, files)
436
+ # Stream handler: (ctx, input, stream) or (ctx, input, files, stream)
437
+ # If there are >= 3 params, check the 3rd param name
438
+ if len(params) >= 3:
439
+ third = params[2]
440
+ if third == "files":
441
+ return True
442
+ return False
443
+
444
+
445
+ def _extract_attachments(req: rune_pb2.ExecuteRequest) -> list[FileAttachment]:
446
+ """Convert proto attachments to FileAttachment dataclass list."""
447
+ return [
448
+ FileAttachment(
449
+ filename=att.filename,
450
+ data=bytes(att.data),
451
+ mime_type=att.mime_type,
452
+ )
453
+ for att in req.attachments
454
+ ]
rune/handler.py ADDED
@@ -0,0 +1,27 @@
1
+ """Handler registry with decorator support."""
2
+ from __future__ import annotations
3
+
4
+ from dataclasses import dataclass
5
+ from typing import Callable, Awaitable
6
+
7
+ from .types import RuneConfig, RuneContext, FileAttachment
8
+ from .stream import StreamSender
9
+
10
+
11
+ # Handler function signatures
12
+ OnceHandler = Callable[[RuneContext, bytes], Awaitable[bytes]]
13
+ StreamHandler = Callable # async def(ctx, input, stream) -> None
14
+
15
+ # Extended handler signatures with file attachments
16
+ OnceHandlerWithFiles = Callable[[RuneContext, bytes, list[FileAttachment]], Awaitable[bytes]]
17
+ StreamHandlerWithFiles = Callable[[RuneContext, bytes, list[FileAttachment], StreamSender], Awaitable[None]]
18
+
19
+
20
+ @dataclass
21
+ class RegisteredRune:
22
+ """A registered rune with its config and handler."""
23
+
24
+ config: RuneConfig
25
+ handler: OnceHandler | StreamHandler | OnceHandlerWithFiles | StreamHandlerWithFiles
26
+ is_stream: bool
27
+ accepts_files: bool = False
rune/stream.py ADDED
@@ -0,0 +1,33 @@
1
+ """StreamSender for streaming Rune handlers."""
2
+ from __future__ import annotations
3
+
4
+ import json
5
+
6
+
7
+ class StreamSender:
8
+ """Sends stream events to the runtime."""
9
+
10
+ def __init__(self, send_fn):
11
+ self._send = send_fn
12
+ self._ended = False
13
+
14
+ async def emit(self, data: bytes | str | dict | list) -> None:
15
+ """Send a stream event.
16
+
17
+ Accepts bytes, str (auto-encoded to UTF-8), or dict/list (auto-serialized to JSON bytes).
18
+ """
19
+ if self._ended:
20
+ raise RuntimeError("stream already ended")
21
+ if isinstance(data, bytes):
22
+ raw = data
23
+ elif isinstance(data, str):
24
+ raw = data.encode("utf-8")
25
+ elif isinstance(data, (dict, list)):
26
+ raw = json.dumps(data).encode("utf-8")
27
+ else:
28
+ raw = str(data).encode("utf-8")
29
+ await self._send(raw)
30
+
31
+ async def end(self) -> None:
32
+ """Signal end of stream."""
33
+ self._ended = True
rune/types.py ADDED
@@ -0,0 +1,35 @@
1
+ """Core types for the Rune Python SDK."""
2
+ from __future__ import annotations
3
+
4
+ from dataclasses import dataclass, field
5
+ from typing import Any
6
+
7
+
8
+ @dataclass
9
+ class RuneConfig:
10
+ """Configuration for a Rune registration."""
11
+ name: str
12
+ version: str = "0.0.0"
13
+ description: str = ""
14
+ supports_stream: bool = False
15
+ gate: str | None = None # gate path, e.g. "/translate"
16
+ gate_method: str = "POST"
17
+ input_schema: dict | None = None # JSON Schema for input validation
18
+ output_schema: dict | None = None # JSON Schema for output validation
19
+ priority: int = 0 # Caster priority (higher wins)
20
+
21
+
22
+ @dataclass
23
+ class FileAttachment:
24
+ """File attachment for ExecuteRequest / ExecuteResult."""
25
+ filename: str
26
+ data: bytes
27
+ mime_type: str
28
+
29
+
30
+ @dataclass
31
+ class RuneContext:
32
+ """Execution context passed to handler."""
33
+ rune_name: str
34
+ request_id: str
35
+ context: dict[str, str] = field(default_factory=dict)
@@ -0,0 +1,172 @@
1
+ Metadata-Version: 2.4
2
+ Name: rune-framework
3
+ Version: 1.1.1
4
+ Summary: Official Python SDK for Rune — protocol-first multi-language function execution framework
5
+ Author: Chasey
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/chasey-dev/rune
8
+ Project-URL: Repository, https://github.com/chasey-dev/rune
9
+ Project-URL: Documentation, https://github.com/chasey-dev/rune/tree/main/sdks/python
10
+ Keywords: rune,grpc,distributed,function,execution,framework
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Requires-Python: >=3.10
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+ Requires-Dist: grpcio>=1.60.0
22
+ Requires-Dist: protobuf>=4.25.0
23
+ Provides-Extra: dev
24
+ Requires-Dist: grpcio-tools>=1.60.0; extra == "dev"
25
+ Requires-Dist: pytest>=7.0; extra == "dev"
26
+ Requires-Dist: pytest-asyncio>=0.23; extra == "dev"
27
+ Requires-Dist: httpx>=0.27; extra == "dev"
28
+ Dynamic: license-file
29
+
30
+ # Rune Python SDK
31
+
32
+ Official Python Caster SDK for the Rune framework.
33
+
34
+ ## 安装
35
+
36
+ ```bash
37
+ pip install rune-framework
38
+ ```
39
+
40
+ 要求 Python >= 3.10。
41
+
42
+ ## 快速开始
43
+
44
+ ```python
45
+ from rune import Caster
46
+
47
+ caster = Caster("localhost:50070")
48
+
49
+ @caster.rune("echo", gate="/echo")
50
+ async def echo(ctx, input):
51
+ return input
52
+
53
+ caster.run()
54
+ ```
55
+
56
+ 启动后 Runtime 自动暴露:
57
+ ```
58
+ POST /echo -- sync
59
+ POST /echo?stream=true -- SSE streaming
60
+ POST /echo?async=true -- async task
61
+ ```
62
+
63
+ ## Caster 配置
64
+
65
+ ```python
66
+ caster = Caster(
67
+ addr="localhost:50070", # Runtime gRPC 地址
68
+ caster_id="my-python-caster", # Caster ID(默认 "python-caster")
69
+ max_concurrent=10, # 最大并发处理数
70
+ labels={"env": "prod", "gpu": "true"}, # Caster 标签(用于标签路由)
71
+ api_key="rk_xxx", # API Key(认证开启时需要)
72
+ reconnect_base_delay=1.0, # 重连初始延迟(秒)
73
+ reconnect_max_delay=30.0, # 重连最大延迟(秒)
74
+ heartbeat_interval=10.0, # 心跳间隔(秒)
75
+ )
76
+ ```
77
+
78
+ ## 注册 Rune
79
+
80
+ ### Unary Handler
81
+
82
+ ```python
83
+ @caster.rune(
84
+ "translate",
85
+ gate="/translate", # HTTP 路由路径
86
+ gate_method="POST", # HTTP 方法(默认 POST)
87
+ version="1.0.0", # 语义版本
88
+ description="Translate text", # 人类可读描述
89
+ priority=10, # 优先级(越高越优先)
90
+ input_schema={ # JSON Schema 输入校验
91
+ "type": "object",
92
+ "properties": {
93
+ "text": {"type": "string"},
94
+ "lang": {"type": "string"}
95
+ },
96
+ "required": ["text", "lang"]
97
+ },
98
+ output_schema={ # JSON Schema 输出校验
99
+ "type": "object",
100
+ "properties": {
101
+ "translated": {"type": "string"}
102
+ }
103
+ },
104
+ )
105
+ async def translate(ctx, input):
106
+ data = json.loads(input)
107
+ result = do_translate(data["text"], data["lang"])
108
+ return json.dumps({"translated": result})
109
+ ```
110
+
111
+ ### Streaming Handler
112
+
113
+ ```python
114
+ @caster.stream_rune("generate", gate="/generate")
115
+ async def generate(ctx, input, sender):
116
+ prompt = json.loads(input)["prompt"]
117
+ for token in model.stream(prompt):
118
+ await sender.emit(token) # 发送一个 chunk
119
+ # StreamEnd 自动发送
120
+ ```
121
+
122
+ `StreamSender.emit()` 接受 `bytes`、`str`、`dict`、`list` 类型,自动序列化。
123
+
124
+ ### File Handler
125
+
126
+ Handler 的第三个参数命名为 `files` 时,自动接收文件附件:
127
+
128
+ ```python
129
+ @caster.rune("process-doc", gate="/process")
130
+ async def process_doc(ctx, input, files):
131
+ # files: list[FileAttachment]
132
+ for f in files:
133
+ print(f.filename, f.mime_type, len(f.data))
134
+ return json.dumps({"processed": len(files)})
135
+ ```
136
+
137
+ `FileAttachment` 字段:
138
+ - `filename: str` -- 文件名
139
+ - `data: bytes` -- 文件内容
140
+ - `mime_type: str` -- MIME 类型
141
+
142
+ ## Context
143
+
144
+ 每次调用都会传入 `RuneContext`:
145
+
146
+ ```python
147
+ @caster.rune("example", gate="/example")
148
+ async def example(ctx, input):
149
+ print(ctx.rune_name) # "example"
150
+ print(ctx.request_id) # "r-18e8f3a1b-0"
151
+ print(ctx.context) # {} (额外的 key-value 上下文)
152
+ return input
153
+ ```
154
+
155
+ ## 运行与停止
156
+
157
+ ```python
158
+ # 阻塞运行(自动重连)
159
+ caster.run()
160
+
161
+ # 从另一个线程停止
162
+ caster.stop()
163
+ ```
164
+
165
+ ## 开发
166
+
167
+ ```bash
168
+ cd sdks/python
169
+ pip install -e ".[dev]"
170
+ pytest tests/test_unit.py # 单元测试
171
+ pytest tests/test_e2e.py -m e2e # E2E 测试(需要运行中的 Runtime)
172
+ ```
@@ -0,0 +1,16 @@
1
+ rune/__init__.py,sha256=kP4VkoLmsWKtnCSErHwCraBlWmkOqG3o7hC3fC75Nuc,212
2
+ rune/caster.py,sha256=qRk1LnqF948Qqouxy0gfZq9i3YOdhafHY1hXqC0JFi8,16261
3
+ rune/handler.py,sha256=JK2jKVbzKEyqV1cwImXpn3aaNRXni5Cy-Z6l2CkHfMA,923
4
+ rune/stream.py,sha256=C3X3CiNYqDdZsBMJzNTK0zAFkH4SU6c71WsOhaM7XpI,960
5
+ rune/types.py,sha256=voKBeXciW_Jd5ptVKMNsK-AI78fpO20voNXvaiY-bsE,959
6
+ rune/_proto/__init__.py,sha256=zHdQtjuI2H8TM5wJPaWJY8iP3tMHpGrEEicuWA-rORk,58
7
+ rune/_proto/rune/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
+ rune/_proto/rune/wire/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
+ rune/_proto/rune/wire/v1/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
+ rune/_proto/rune/wire/v1/rune_pb2.py,sha256=pMwAyoq5_cQFFFXDLSYG6L5JV1o_VvaTPAhTX5jFqVA,6915
11
+ rune/_proto/rune/wire/v1/rune_pb2_grpc.py,sha256=CwxOh_bf7NEG58V7U0sUjKsThQTwXuwbNRUn4RFeZC4,3571
12
+ rune_framework-1.1.1.dist-info/licenses/LICENSE,sha256=6btH8q0Bvi2Y3u3i6Yx_nzOYSDjWOM0Lhl0OIbn77m0,1063
13
+ rune_framework-1.1.1.dist-info/METADATA,sha256=xBoA49Pefn2AzfRRyiCKRyF2xDC0ahfOE6xPqdxOWEw,4739
14
+ rune_framework-1.1.1.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
15
+ rune_framework-1.1.1.dist-info/top_level.txt,sha256=WiPBkToefNAERnF4884CB1cJyYYE5lnnGO6OU_iWONA,5
16
+ rune_framework-1.1.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Chasey
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ rune