Spaces:
Sleeping

File size: 4,212 Bytes
f45a595
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/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()