-
Notifications
You must be signed in to change notification settings - Fork 1.1k
sidebar code-mode: add run_scratchpad utility functions #9637
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
Light2Dark
wants to merge
3
commits into
main
Choose a base branch
from
sham/code-mode-refactor-sidebar
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Copyright 2026 Marimo. All rights reserved. | ||
| """Meta keys for wiring screenshot credentials through ``HTTPRequest.meta``. | ||
| Code-mode tools running in the kernel need to call back into the marimo | ||
| server (e.g. ``ctx.screenshot()`` driving Playwright against the | ||
| kiosk page). The server stamps a trusted ``server_url`` and | ||
| ``auth_token`` onto each control request's ``meta`` dict; the runtime | ||
| side reads them when building the screenshot session. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| SCREENSHOT_SERVER_URL_KEY = "screenshot_server_url" | ||
| SCREENSHOT_AUTH_TOKEN_KEY = "screenshot_auth_token" |
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,55 @@ | ||
| # Copyright 2026 Marimo. All rights reserved. | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from marimo._ai._tools.types import CodeExecutionResult | ||
| from marimo._server.api.deps import AppState | ||
| from marimo._server.api.utils import get_code_mode_credentials | ||
| from marimo._server.scratchpad import run_scratchpad_code | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pydantic_ai import FunctionToolset | ||
| from starlette.requests import Request | ||
|
|
||
| from marimo._session.session import Session | ||
|
|
||
|
|
||
| def build_execute_code_toolset( | ||
| session: Session, | ||
| request: Request, | ||
| ) -> FunctionToolset[None]: | ||
| """Build a ``FunctionToolset`` exposing one tool: ``execute_code``. | ||
| The tool is bound to the caller's *session* and *request*; the model | ||
| never sees or passes a session id. Screenshot credentials are derived | ||
| per tool call from the request so ``ctx.screenshot()`` can call back | ||
| into this server (see ``marimo/_code_mode/_context.py``). | ||
| """ | ||
|
|
||
| from pydantic_ai import FunctionToolset | ||
|
|
||
| toolset: FunctionToolset[None] = FunctionToolset() | ||
|
|
||
| async def execute_code(code: str) -> CodeExecutionResult: | ||
| """Run Python inside the running notebook's kernel scratchpad. | ||
| Use this for all notebook mutations via ``marimo._code_mode``. | ||
| """ | ||
| server_url, auth_token = get_code_mode_credentials( | ||
| AppState(request), request | ||
| ) | ||
| return await run_scratchpad_code( | ||
| session, | ||
| request, | ||
| code=code, | ||
| server_url=server_url, | ||
| auth_token=auth_token, | ||
| ) | ||
|
|
||
| toolset.add_function( | ||
| execute_code, | ||
| name="execute_code", | ||
| description=execute_code.__doc__, | ||
| ) | ||
| return toolset |
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
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.