Spaces:
Sleeping

LS / live_zcode_test.py
Adarsh Kumar
Upload 20 files
f45a595 verified
Raw History Blame Contribute Delete
4.21 kB
#!/usr/bin/env python3
"""Live two-step coding-agent probe against a running gateway.
It verifies that an OpenAI ``auto`` tool loop selects ``read_file``, accepts the
returned tool result, then selects ``write_file`` with schema-valid arguments.
The script does not execute either call.
"""
from __future__ import annotations
import json
import sys
import urllib.error
import urllib.request
BASE = (sys.argv[1] if len(sys.argv) > 1 else "http://127.0.0.1:7860").rstrip("/")
URL = BASE + "/v1/chat/completions"
TOOLS = [
{
"type": "function",
"function": {
"name": "read_file",
"description": "Read a workspace file",
"parameters": {
"type": "object",
"properties": {"path": {"type": "string"}},
"required": ["path"],
"additionalProperties": False,
},
},
},
{
"type": "function",
"function": {
"name": "write_file",
"description": "Create or overwrite a workspace file",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string"},
"content": {"type": "string"},
},
"required": ["path", "content"],
"additionalProperties": False,
},
},
},
{
"type": "function",
"function": {
"name": "execute_command",
"description": "Execute a shell command",
"parameters": {
"type": "object",
"properties": {"command": {"type": "string"}},
"required": ["command"],
"additionalProperties": False,
},
},
},
]
def post(messages):
payload = {
"model": "solar-pro3",
"messages": messages,
"tools": TOOLS,
"tool_choice": "auto",
"temperature": 0.1,
"max_tokens": 512,
}
req = urllib.request.Request(
URL,
data=json.dumps(payload).encode(),
headers={"Content-Type": "application/json", "Authorization": "Bearer public"},
)
try:
with urllib.request.urlopen(req, timeout=360) as response:
return json.loads(response.read())
except urllib.error.HTTPError as exc:
raise AssertionError(f"HTTP {exc.code}: {exc.read().decode()}") from exc
def main():
system = {"role": "system", "content": "You are a coding agent. Inspect files before editing."}
user = {
"role": "user",
"content": "Inspect README.md using the appropriate tool before you explain the project.",
}
first = post([system, user])
first_choice = first["choices"][0]
assert first_choice["finish_reason"] == "tool_calls", first
first_call = first_choice["message"]["tool_calls"][0]
assert first_call["function"]["name"] == "read_file", first_call
assert json.loads(first_call["function"]["arguments"])["path"] == "README.md"
print("PASS auto selected read_file")
second = post(
[
system,
user,
first_choice["message"],
{
"role": "tool",
"tool_call_id": first_call["id"],
"content": "# Demo Project\nA Python demo.",
},
{
"role": "user",
"content": (
'Now create hello.py containing a hello() function that returns "world". '
"Use the appropriate tool."
),
},
]
)
second_choice = second["choices"][0]
assert second_choice["finish_reason"] == "tool_calls", second
second_call = second_choice["message"]["tool_calls"][0]
assert second_call["function"]["name"] == "write_file", second_call
arguments = json.loads(second_call["function"]["arguments"])
assert arguments["path"] == "hello.py"
assert "def hello" in arguments["content"] and "world" in arguments["content"]
print("PASS tool result accepted; auto selected write_file")
print("ZCODE CODING-AGENT PROBE PASSED")
if __name__ == "__main__":
main()