Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.env
.tox
uv.lock
*.db
*.pyc

dist
Expand Down
130 changes: 0 additions & 130 deletions lib/lsp-devtools/lsp_devtools/agent/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
from __future__ import annotations

import argparse
import asyncio
import logging
import subprocess
import sys

from lsp_devtools.cli.utils import get_log_level

from .agent import Agent
from .agent import MessageHeader
from .agent import MessageSource
Expand All @@ -21,125 +13,3 @@
"MessageHeader",
"MessageSource",
]


async def forward_stderr(server: asyncio.subprocess.Process):
"""Forward the server's stderr to the agent's stderr."""
if server.stderr is None:
return

# EOF is signalled with an empty bytestring
while (line := await server.stderr.readline()) != b"":
sys.stderr.buffer.write(line)


class AgentClientHandler(logging.Handler):
"""Forwards log messages through the client - server connection."""

def __init__(self, client: AgentClient, level: int = 0) -> None:
super().__init__(level)
self.client = client

def emit(self, record: logging.LogRecord) -> None:
msg = (self.format(record) + "\n").encode()

source = MessageSource.AGENT
length = len(msg)
data = b"".join([MessageHeader.pack(source, length), msg])

self.client.forward_message(data)


async def main(args, cmd: list[str]):
client = AgentClient()

log_level = get_log_level(args.verbose)
logger = logging.getLogger("lsp_devtools")
logger.setLevel(log_level)

handler = AgentClientHandler(client)
handler.setFormatter(logging.Formatter("[%(name)s]: %(message)s"))
handler.setLevel(log_level)

logger.addHandler(handler)

command, *arguments = cmd
server = await asyncio.create_subprocess_exec(
command,
*arguments,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)

agent = Agent(server, sys.stdin.buffer, sys.stdout.buffer, client.forward_message)

await asyncio.gather(
client.start_tcp(args.host, args.port),
agent.start(),
forward_stderr(server),
)


def run_agent(args, extra: list[str] | None):
if extra is None:
print("Missing server start command", file=sys.stderr)
return 1

try:
asyncio.run(main(args, extra))
except asyncio.CancelledError:
pass


def cli(commands: argparse._SubParsersAction):
cmd: argparse.ArgumentParser = commands.add_parser(
"agent",
help="instrument an LSP session",
formatter_class=argparse.RawDescriptionHelpFormatter,
description="""\
This command runs the given JSON-RPC server as a subprocess, wrapping it in a
an "AgentClient" which will capture all messages sent to/from the wrapped
server, forwarding them onto an "AgentServer" to be processed.

To wrap a server, supply its start command after all other agent options and
preceeded by a `--`, for example:

lsp-devtools agent -p 1234 -- python -m esbonio

Wrapping a JSON-RPC server with this command is required to enable the
majority of the lsp-devtools suite of tools.

┌─ RPC Client ─┐ ┌──── Agent Client ────┐ ┌─ RPC Server ─┐
│ │ │ ┌──────────────┐ │ │ │
│ stdout│─────│───│ │───│────│stdin │
│ │ │ │ Agent │ │ │ │
│ stdin│─────│───│ │───│────│stdout │
│ │ │ └──────────────┘ │ │ │
│ │ │ │ │ │
└──────────────┘ └──────────────────────┘ └──────────────┘
│ tcp
┌──────────────┐
│ │
│ Agent Server │
│ │
└──────────────┘

""",
)

cmd.add_argument(
"--host",
help="the host to connect to.",
default="localhost",
)
cmd.add_argument(
"-p",
"--port",
help="the port to connect to",
default=8765,
)

cmd.set_defaults(run=run_agent)
16 changes: 10 additions & 6 deletions lib/lsp-devtools/lsp_devtools/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@


BUILTIN_COMMANDS = [
"lsp_devtools.agent",
# "lsp_devtools.client",
"lsp_devtools.inspector",
"lsp_devtools.record",
"lsp_devtools.cli.agent",
"lsp_devtools.cli.client",
"lsp_devtools.cli.inspector",
"lsp_devtools.cli.record",
]


Expand Down Expand Up @@ -56,12 +56,16 @@ def main():
idx = sys.argv.index("--")
args, extra = sys.argv[1:idx], sys.argv[idx + 1 :]
except ValueError:
args, extra = sys.argv[1:], None
args, extra = sys.argv[1:], []

parsed_args = cli.parse_args(args)

if hasattr(parsed_args, "run"):
return parsed_args.run(parsed_args, extra)
try:
return parsed_args.run(parsed_args, extra)
except Exception as exc:
print(f"Error: {exc}")
return -1

cli.print_help()
return 0
136 changes: 136 additions & 0 deletions lib/lsp-devtools/lsp_devtools/cli/agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
from __future__ import annotations

import argparse
import asyncio
import logging
import subprocess
import sys

from lsp_devtools.agent import Agent
from lsp_devtools.agent import AgentClient
from lsp_devtools.agent import MessageHeader
from lsp_devtools.agent import MessageSource

from .utils import get_log_level


async def forward_stderr(server: asyncio.subprocess.Process):
"""Forward the server's stderr to the agent's stderr."""
if server.stderr is None:
return

# EOF is signalled with an empty bytestring
while (line := await server.stderr.readline()) != b"":
sys.stderr.buffer.write(line)


class AgentClientHandler(logging.Handler):
"""Forwards log messages through the client - server connection."""

def __init__(self, client: AgentClient, level: int = 0) -> None:
super().__init__(level)
self.client = client

def emit(self, record: logging.LogRecord) -> None:
msg = (self.format(record) + "\n").encode()

source = MessageSource.AGENT
length = len(msg)
data = b"".join([MessageHeader.pack(source, length), msg])

self.client.forward_message(data)


async def main(args, cmd: list[str]):
client = AgentClient()

log_level = get_log_level(args.verbose)
logger = logging.getLogger("lsp_devtools")
logger.setLevel(log_level)

handler = AgentClientHandler(client)
handler.setFormatter(logging.Formatter("[%(name)s]: %(message)s"))
handler.setLevel(log_level)

logger.addHandler(handler)

command, *arguments = cmd
server = await asyncio.create_subprocess_exec(
command,
*arguments,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)

agent = Agent(server, sys.stdin.buffer, sys.stdout.buffer, client.forward_message)

await asyncio.gather(
client.start_tcp(args.host, args.port),
agent.start(),
forward_stderr(server),
)


def run_agent(args, extra: list[str] | None):
if extra is None:
print("Missing server start command", file=sys.stderr)
return 1

try:
asyncio.run(main(args, extra))
except asyncio.CancelledError:
pass


def cli(commands: argparse._SubParsersAction):
cmd: argparse.ArgumentParser = commands.add_parser(
"agent",
help="instrument an LSP session",
formatter_class=argparse.RawDescriptionHelpFormatter,
description="""\
This command runs the given JSON-RPC server as a subprocess, wrapping it in a
an "AgentClient" which will capture all messages sent to/from the wrapped
server, forwarding them onto an "AgentServer" to be processed.

To wrap a server, supply its start command after all other agent options and
preceeded by a `--`, for example:

lsp-devtools agent -p 1234 -- python -m esbonio

Wrapping a JSON-RPC server with this command is required to enable the
majority of the lsp-devtools suite of tools.

┌─ RPC Client ─┐ ┌──── Agent Client ────┐ ┌─ RPC Server ─┐
│ │ │ ┌──────────────┐ │ │ │
│ stdout│─────│───│ │───│────│stdin │
│ │ │ │ Agent │ │ │ │
│ stdin│─────│───│ │───│────│stdout │
│ │ │ └──────────────┘ │ │ │
│ │ │ │ │ │
└──────────────┘ └──────────────────────┘ └──────────────┘
│ tcp
┌──────────────┐
│ │
│ Agent Server │
│ │
└──────────────┘

""",
)

cmd.add_argument(
"--host",
help="the host to connect to.",
default="localhost",
)
cmd.add_argument(
"-p",
"--port",
help="the port to connect to",
default=8765,
)

cmd.set_defaults(run=run_agent)
Loading