2121 HostHandlers ,
2222 NotebookHandlers ,
2323 dispatch ,
24+ dispatch_mutation ,
2425)
2526from marimo ._core .result import Ok
2627from marimo ._messaging .notification import ConsumerCapabilities
2728from marimo ._runtime .commands import HTTPRequest
2829from marimo ._server .api .auth import TOKEN_QUERY_PARAM
2930from marimo ._server .api .deps import AppState , AppStateBase
3031from marimo ._server .api .utils import format_url_host , open_url_in_browser
32+ from marimo ._server .extensions import api as extensions
3133from marimo ._server .host .auth import bearer_matches
3234from marimo ._server .tokens import AuthToken
3335from marimo ._types .ids import ConsumerId
4244
4345LOGGER = _loggers .marimo_logger ()
4446api = protocol .api
47+ _Operation = api .Operation | extensions .Operation
48+ _OPERATIONS = (* api .OPERATIONS , * extensions .OPERATIONS )
4549
4650# The operations a host's catalog serves, which the editor does not.
4751_CATALOG : tuple [api .Operation , ...] = (
@@ -213,9 +217,9 @@ def _open_quietly(browser: str, url: str) -> None:
213217
214218
215219def _endpoint (
216- operation : api . Operation ,
220+ operation : _Operation ,
217221) -> Callable [[Request ], Awaitable [Response ]]:
218- kind : type [api .Call ] = operation .Call
222+ kind : type [api .Call | extensions . Call ] = operation .Call
219223
220224 async def handle (request : Request ) -> Response :
221225 # Assemble the call from the request's parts, the shape the client
@@ -233,28 +237,36 @@ async def handle(request: Request) -> Response:
233237 return problem (
234238 "invalid-request" , "This operation takes no body"
235239 )
236- call : api .Call = msgspec .convert (parts , type = kind )
240+ call : api .Call | extensions .Call = msgspec .convert (
241+ parts , type = kind
242+ )
237243 except msgspec .DecodeError as e :
238244 return problem ("invalid-request" , str (e ))
239245 host = _context (request )
240- session = AppState (request ).get_current_session ()
241- notebook_id = request .path_params .get ("notebook_id" )
242- status , body = await dispatch (
243- host .mutations ,
244- host .api ,
245- call ,
246- ServerContext (
246+ if isinstance (call , extensions .Call ):
247+ status , body = await dispatch_mutation (
248+ host .mutations ,
249+ call ,
250+ notebook_id = call .params .notebook_id ,
251+ key = call .headers .idempotency_key ,
252+ handle = lambda : extensions .handle (call , host .bridge ),
253+ )
254+ else :
255+ session = AppState (request ).get_current_session ()
256+ notebook_id = request .path_params .get ("notebook_id" )
257+ context = ServerContext (
247258 request = _submitting_request (request ),
248- # Only the notebook's own editor can answer a prompt.
249259 allow_input = (
250260 session is not None
251261 and notebook_id is not None
252262 and host .bridge .session_for_notebook (notebook_id )
253263 is session
254264 ),
255265 app = request .app ,
256- ),
257- )
266+ )
267+ status , body = await dispatch (
268+ host .mutations , host .api , call , context
269+ )
258270 return _response (status , body )
259271
260272 return handle
@@ -279,7 +291,7 @@ def operation_routes() -> list[Route]:
279291 """Serve each operation at its path in the spec."""
280292 return [
281293 Route (operation .PATH , _endpoint (operation ), methods = [operation .METHOD ])
282- for operation in api . OPERATIONS
294+ for operation in _OPERATIONS
283295 ]
284296
285297
@@ -291,13 +303,13 @@ def editor_notebook_routes(prefix: str) -> list[Route]:
291303 _editor_endpoint (operation ),
292304 methods = [operation .METHOD ],
293305 )
294- for operation in api . OPERATIONS
306+ for operation in _OPERATIONS
295307 if operation not in _CATALOG
296308 ]
297309
298310
299311def _editor_endpoint (
300- operation : api . Operation ,
312+ operation : _Operation ,
301313) -> Callable [[Request ], Awaitable [Response ]]:
302314 serve = _endpoint (operation )
303315
@@ -318,7 +330,7 @@ async def handle(request: Request) -> Response:
318330 return handle
319331
320332
321- def _connection_may_call (request : Request , operation : api . Operation ) -> bool :
333+ def _connection_may_call (request : Request , operation : _Operation ) -> bool :
322334 """Whether the connection may issue this notebook operation.
323335
324336 Host commands are system-originated, so enforce connection capabilities
@@ -331,6 +343,10 @@ def _connection_may_call(request: Request, operation: api.Operation) -> bool:
331343 session = app_state .get_current_session ()
332344 if session is None :
333345 return False
346+ if operation in extensions .OPERATIONS and session is not _context (
347+ request
348+ ).bridge .session_for_notebook (request .path_params ["notebook_id" ]):
349+ return False
334350 consumer = session .room .get_consumer (ConsumerId (session_id ))
335351 capabilities = (
336352 session .room .get_capabilities (consumer )
0 commit comments