Files
ipython-demo/src/langchain_demo/ipython_wrapper.py
T

45 lines
1.4 KiB
Python

from typing import Literal
from langchain.tools import tool
from ipython_demo import App
from ipython_demo.models import CallOptions, CallResponse, ShellInfo
app = App() # global, again
# should be similar to mcp/webapp.
@tool
def run_code(
code: str,
shell_name: str | Literal["last", "new"] = "last",
options: CallOptions | None = None,
) -> CallResponse:
"""Run code in a named shell and return a frontend-facing call response.
use shell_name="new" to create a new shell, or shell_name="last" to use the most recently selected shell. Otherwise, use an existing shell name to run code in that shell.
"""
return app.run_code(code, shell_name, options=options)
@tool
def list_shells() -> list[ShellInfo]:
"""Return frontend-safe metadata for all known shells."""
return app.list_shells()
@tool
def get_shell(shell_name: str | Literal["last"]) -> ShellInfo:
"""Return frontend-safe metadata for a single shell."""
return app.get_last_shell() if shell_name == "last" else app.get_shell(shell_name)
@tool
def create_shell() -> ShellInfo:
"""Create a fresh named shell and return its metadata."""
return app.create_shell()
@tool
def get_last_shell() -> ShellInfo:
"""
Return metadata for the most recently selected shell.
get_shell('last') is equivalent, but this is a more explicit name.
"""
return app.get_last_shell()