Skip to content

Commit 0d9062f

Browse files
committed
Add a --debug option to the plugin runner
1 parent f4af8ec commit 0d9062f

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

src/lmstudio/async_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ async def _notify_client_termination(self) -> int:
367367
for rx_queue in self._mux.all_queues():
368368
await rx_queue.put(None)
369369
num_clients += 1
370-
self._logger.info(
370+
self._logger.debug(
371371
f"Notified {num_clients} clients of websocket termination",
372372
num_clients=num_clients,
373373
)

src/lmstudio/plugin/_dev_runner.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ async def register_dev_plugin(self) -> AsyncGenerator[tuple[str, str], None]:
107107
message: DevPluginRegistrationEndDict = {"type": "end"}
108108
await channel.send_message(message)
109109

110-
async def run_plugin(self, *, allow_local_imports: bool = True) -> int:
110+
async def run_plugin(
111+
self, *, allow_local_imports: bool = True, debug: bool = False
112+
) -> int:
111113
if not allow_local_imports:
112114
raise ValueError("Local imports are always permitted for dev plugins")
113115
async with self.register_dev_plugin() as (client_id, client_key):
@@ -117,35 +119,40 @@ async def run_plugin(self, *, allow_local_imports: bool = True) -> int:
117119
self._plugin_path,
118120
client_id,
119121
client_key,
122+
debug,
120123
)
121124
)
122125
return result.returncode
123126

124127

125-
# TODO: support the same subprocess monitoring features as `lms dev`
128+
# TODO: support the same source code change monitoring features as `lms dev`
126129
def _run_plugin_in_child_process(
127-
plugin_path: Path, client_id: str, client_key: str
130+
plugin_path: Path, client_id: str, client_key: str, debug: bool = False
128131
) -> subprocess.CompletedProcess[str]:
129132
env = os.environ.copy()
130133
env[ENV_CLIENT_ID] = client_id
131134
env[ENV_CLIENT_KEY] = client_key
132135
package_name = __spec__.parent
133136
assert package_name is not None
137+
debug_option = ("--debug",) if debug else ()
134138
command: list[str] = [
135139
sys.executable,
136140
"-m",
137141
package_name,
142+
*debug_option,
138143
os.fspath(plugin_path),
139144
]
140145
return subprocess.run(command, text=True, env=env)
141146

142147

143-
async def run_plugin_async(plugin_dir: str | os.PathLike[str]) -> int:
148+
async def run_plugin_async(
149+
plugin_dir: str | os.PathLike[str], *, debug: bool = False
150+
) -> int:
144151
"""Asynchronously execute a plugin in development mode."""
145152
async with DevPluginClient(plugin_dir) as dev_client:
146-
return await dev_client.run_plugin()
153+
return await dev_client.run_plugin(debug=debug)
147154

148155

149-
def run_plugin(plugin_dir: str | os.PathLike[str]) -> int:
156+
def run_plugin(plugin_dir: str | os.PathLike[str], *, debug: bool = False) -> int:
150157
"""Execute a plugin in development mode."""
151-
return asyncio.run(run_plugin_async(plugin_dir))
158+
return asyncio.run(run_plugin_async(plugin_dir, debug=debug))

src/lmstudio/plugin/cli.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Command line interface implementation."""
22

33
import argparse
4+
import logging
45
import os.path
56
import sys
67
import warnings
@@ -24,6 +25,7 @@ def _parse_args(
2425
"plugin_path", metavar="PLUGIN_PATH", help="Directory name of plugin to run"
2526
)
2627
parser.add_argument("--dev", action="store_true", help="Run in development mode")
28+
parser.add_argument("--debug", action="store_true", help="Enable debug logging")
2729
return parser, parser.parse_args(argv)
2830

2931

@@ -45,10 +47,8 @@ def main(argv: Sequence[str] | None = None) -> int:
4547
warnings.filterwarnings(
4648
"ignore", ".*the async API is not yet stable", FutureWarning
4749
)
48-
# TODO: Accept options to configure the verbosity
49-
import logging
50-
51-
logging.basicConfig(level=logging.DEBUG)
50+
log_level = logging.DEBUG if args.debug else logging.INFO
51+
logging.basicConfig(level=log_level)
5252
if not args.dev:
5353
try:
5454
runner.run_plugin(plugin_path, allow_local_imports=True)
@@ -57,7 +57,7 @@ def main(argv: Sequence[str] | None = None) -> int:
5757
else:
5858
# Retrieve args from API host, spawn plugin in subprocess
5959
try:
60-
_dev_runner.run_plugin(plugin_path)
60+
_dev_runner.run_plugin(plugin_path, debug=args.debug)
6161
except KeyboardInterrupt:
6262
pass # Subprocess handles reporting the plugin termination
6363
return 0

src/lmstudio/sync_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def _notify_client_termination(self) -> int:
321321
for rx_queue in self._mux.all_queues():
322322
rx_queue.put(None)
323323
num_clients += 1
324-
self._logger.info(
324+
self._logger.debug(
325325
f"Notified {num_clients} clients of websocket termination",
326326
num_clients=num_clients,
327327
)

0 commit comments

Comments
 (0)