Skip to content

Commit a31415b

Browse files
committed
Rename tool 'input' field to be language-specific and explain it in doc
Improvement after PR #23
1 parent 1ae4bad commit a31415b

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

open-webui/tools/run_code.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,21 @@ async def run_bash_command(
126126
127127
:param bash_command: Bash command or script to run.
128128
129-
:return: A JSON object with the following fields: `status`, `output`. In most cases, when `status` is "OK", the user is interested in the content of the `output` field. Otherwise, report the `status` field first.
129+
:return: A JSON object with the following fields: `bash_command`, `status`, `output`. In most cases, when `status` is "OK", the user is interested in the content of the `output` field. Otherwise, report the `status` field first.
130130
"""
131-
return await self._run_code(
131+
result = await self._run_code(
132132
language=Sandbox.LANGUAGE_BASH,
133133
code=bash_command,
134134
event_emitter=__event_emitter__,
135135
)
136+
return json.dumps(
137+
{
138+
"bash_command": bash_command,
139+
"status": result["status"],
140+
"output": result["output"],
141+
},
142+
ensure_ascii=False,
143+
)
136144

137145
async def run_python_code(
138146
self,
@@ -144,13 +152,21 @@ async def run_python_code(
144152
145153
:param python_code: Python code to run.
146154
147-
:return: A JSON object with the following fields: `status`, `output`. In most cases, when `status` is "OK", the user is interested in the content of the `output` field. Otherwise, report the `status` field first.
155+
:return: A JSON object with the following fields: `python_code`, `status`, `output`. In most cases, when `status` is "OK", the user is interested in the content of the `output` field. Otherwise, report the `status` field first.
148156
"""
149-
return await self._run_code(
157+
result = await self._run_code(
150158
language=Sandbox.LANGUAGE_PYTHON,
151159
code=python_code,
152160
event_emitter=__event_emitter__,
153161
)
162+
return json.dumps(
163+
{
164+
"python_code": python_code,
165+
"status": result["status"],
166+
"output": result["output"],
167+
},
168+
ensure_ascii=False,
169+
)
154170

155171
class _EventEmitter:
156172
"""
@@ -251,7 +267,7 @@ async def _run_code(
251267
:param code: The code to run.
252268
:param event_emitter: Event emitter to send status updates to.
253269
254-
:return: A JSON object with the following fields: `status`, `output`. In most cases, when `status` is "OK", the user is interested in the content of the `output` field. Otherwise, report the `status` field first.
270+
:return: A dictionary with the following fields: `status`, `output`.
255271
"""
256272
valves = self.valves
257273
debug = valves.DEBUG
@@ -280,7 +296,7 @@ async def _fail(error_message, status="SANDBOX_ERROR"):
280296
)
281297
else:
282298
await emitter.fail(error_message)
283-
return json.dumps({"status": status, "output": error_message})
299+
return {"status": status, "output": error_message}
284300

285301
try:
286302
max_ram_bytes = None
@@ -376,16 +392,10 @@ def _log(filename: str, log_line: str):
376392
done=True,
377393
description=f"[DEBUG MODE] status={status}; output={output}; valves=[{valves}]; debug={per_file_logs}",
378394
)
379-
return json.dumps(
380-
{
381-
# TODO: Explain the 'input' in the tool docstring.
382-
# TODO: Rename 'input' to the argument name in the tool.
383-
"input": code,
384-
"status": status,
385-
"output": output,
386-
},
387-
ensure_ascii=False,
388-
)
395+
return {
396+
"status": status,
397+
"output": output,
398+
}
389399
except Sandbox.PlatformNotSupportedException as e:
390400
return await _fail(f"Sandbox cannot run on this machine: {e}")
391401
except Sandbox.SandboxRuntimeException as e:

0 commit comments

Comments
 (0)