File size: 8,796 Bytes
105f2e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
557e100
105f2e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
557e100
 
 
 
 
 
 
105f2e1
 
 
 
557e100
 
105f2e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
557e100
105f2e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
557e100
105f2e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
557e100
 
105f2e1
 
 
 
 
 
 
 
 
 
 
 
 
 
587ef78
105f2e1
 
 
 
 
 
 
 
 
 
587ef78
105f2e1
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import json
import subprocess
import sys
import textwrap
from pathlib import Path

from agent_core.config import DEFAULT_CADQUERY_OUTPUT_PATH, WORKDIR
from agent_core.outputs import resolve_run_output, update_latest_link, workspace_relative, write_manifest
from agent_core.utils import json_response


CADQUERY_RUNNER = r"""
import contextlib
import io
import json
import sys
import traceback
from pathlib import Path

stdout_buffer = io.StringIO()
stderr_buffer = io.StringIO()


def tail(text, limit=4000):
    if not text:
        return ""
    return text[-limit:]


def fail(stage, exc=None, error_type=None, error=None):
    payload = {
        "ok": False,
        "stage": stage,
        "error_type": error_type or (type(exc).__name__ if exc else "Error"),
        "error": error or (str(exc) if exc else ""),
        "traceback_tail": tail(traceback.format_exc() if exc else ""),
        "stdout": tail(stdout_buffer.getvalue()),
        "stderr": tail(stderr_buffer.getvalue()),
    }
    print(json.dumps(payload, ensure_ascii=False))


try:
    request = json.loads(sys.stdin.read())
    code = request["code"]
    output_path = Path(request["output_path"])
    preview_path = Path(request["preview_path"])
except Exception as exc:
    fail("input", exc)
    sys.exit(0)

try:
    import cadquery as cq
except Exception as exc:
    fail("import", exc)
    sys.exit(0)

try:
    compiled = compile(code, "<cadquery_code>", "exec")
except Exception as exc:
    fail("syntax", exc)
    sys.exit(0)

namespace = {
    "__name__": "__cadquery_user_code__",
    "cq": cq,
}

try:
    with contextlib.redirect_stdout(stdout_buffer), contextlib.redirect_stderr(stderr_buffer):
        exec(compiled, namespace)
except Exception as exc:
    fail("execution", exc)
    sys.exit(0)

if "result" not in namespace:
    fail("result", error_type="MissingResult", error="CadQuery code must assign the final model to variable 'result'.")
    sys.exit(0)

result = namespace["result"]
if result is None:
    fail("result", error_type="InvalidResult", error="'result' is None.")
    sys.exit(0)

exportable_types = tuple(
    value
    for name in ("Workplane", "Shape", "Assembly", "Sketch")
    for value in [getattr(cq, name, None)]
    if isinstance(value, type)
)

is_exportable = isinstance(result, exportable_types)
if isinstance(result, (list, tuple)) and result:
    is_exportable = all(isinstance(item, exportable_types) for item in result)

if not is_exportable:
    fail(
        "result",
        error_type="InvalidResultType",
        error=f"'result' must be a CadQuery object or a non-empty list/tuple of CadQuery objects, got {type(result).__name__}.",
    )
    sys.exit(0)

try:
    output_path.parent.mkdir(parents=True, exist_ok=True)
    cq.exporters.export(result, str(output_path))
except Exception as exc:
    fail("export", exc)
    sys.exit(0)

preview_error = None
try:
    preview_path.parent.mkdir(parents=True, exist_ok=True)
    cq.exporters.export(result, str(preview_path))
except Exception as exc:
    preview_error = str(exc)

payload = {
    "ok": True,
    "stage": "done",
    "output_path": str(output_path),
    "preview_path": str(preview_path) if preview_path.exists() else None,
    "warning": f"Failed to export STL preview: {preview_error}" if preview_error else None,
    "stdout": tail(stdout_buffer.getvalue()),
    "stderr": tail(stderr_buffer.getvalue()),
}
print(json.dumps(payload, ensure_ascii=False))
"""


def run_execute_cadquery(code: str, output_path: str = DEFAULT_CADQUERY_OUTPUT_PATH, prompt: str | None = None) -> str:
    try:
        if not code or not code.strip():
            return json_response({
                "ok": False,
                "stage": "input",
                "error_type": "EmptyCode",
                "error": "execute_cadquery requires non-empty CadQuery code.",
                "traceback_tail": "",
                "stdout": "",
                "stderr": "",
            })

        run_dir, output, run_id = resolve_run_output(output_path)
        preview = run_dir / "preview.stl"
        output_root = run_dir.parent.parent
    except Exception as exc:
        return json_response({
            "ok": False,
            "stage": "input",
            "error_type": type(exc).__name__,
            "error": str(exc),
            "traceback_tail": "",
            "stdout": "",
            "stderr": "",
        })

    request = {
        "code": code,
        "output_path": str(output),
        "preview_path": str(preview),
    }

    try:
        process = subprocess.run(
            [sys.executable, "-c", CADQUERY_RUNNER],
            input=json.dumps(request, ensure_ascii=False),
            cwd=WORKDIR,
            capture_output=True,
            text=True,
            timeout=120,
        )
    except subprocess.TimeoutExpired as exc:
        return json_response({
            "ok": False,
            "stage": "execution",
            "error_type": "TimeoutExpired",
            "error": "CadQuery execution timed out after 120 seconds.",
            "traceback_tail": "",
            "stdout": exc.stdout[-4000:] if exc.stdout else "",
            "stderr": exc.stderr[-4000:] if exc.stderr else "",
        })
    except Exception as exc:
        return json_response({
            "ok": False,
            "stage": "subprocess",
            "error_type": type(exc).__name__,
            "error": str(exc),
            "traceback_tail": "",
            "stdout": "",
            "stderr": "",
        })

    raw_output = process.stdout.strip()
    if not raw_output:
        return json_response({
            "ok": False,
            "stage": "subprocess",
            "error_type": "NoRunnerOutput",
            "error": "CadQuery runner returned no JSON output.",
            "traceback_tail": "",
            "stdout": "",
            "stderr": process.stderr[-4000:],
        })

    try:
        payload = json.loads(raw_output.splitlines()[-1])
    except Exception as exc:
        return json_response({
            "ok": False,
            "stage": "subprocess",
            "error_type": type(exc).__name__,
            "error": f"Failed to parse CadQuery runner output: {exc}",
            "traceback_tail": "",
            "stdout": raw_output[-4000:],
            "stderr": process.stderr[-4000:],
        })

    if process.returncode != 0 and payload.get("ok") is not False:
        payload = {
            "ok": False,
            "stage": "subprocess",
            "error_type": "RunnerFailed",
            "error": f"CadQuery runner exited with code {process.returncode}.",
            "traceback_tail": "",
            "stdout": raw_output[-4000:],
            "stderr": process.stderr[-4000:],
        }

    if payload.get("ok") and payload.get("output_path"):
        exported_path = Path(payload["output_path"])
        manifest_path = write_manifest(
            run_dir=run_dir,
            run_id=run_id,
            requested_output_path=output_path,
            output_path=exported_path,
            code=code,
            prompt=prompt,
            payload=payload,
        )
        latest_warning = update_latest_link(output_root, run_dir)

        payload["run_id"] = run_id
        payload["run_dir"] = workspace_relative(run_dir)
        payload["output_path"] = workspace_relative(exported_path)
        if payload.get("preview_path"):
            payload["preview_path"] = workspace_relative(Path(payload["preview_path"]))
        payload["manifest_path"] = workspace_relative(manifest_path)
        payload["latest_path"] = workspace_relative(output_root / "latest")
        if latest_warning:
            payload["warning"] = latest_warning

    return json_response(payload)


TOOL_SCHEMA = {
    "name": "execute_cadquery",
    "description": textwrap.dedent("""
        Execute CadQuery Python code and export the result as a STEP file.
        The code must assign the final CadQuery model to a variable named result.
        cadquery is pre-imported as cq.
        Do NOT supply output_path in normal usage. The tool automatically writes files to the configured artifact directory under a unique run directory. Only set output_path if the user explicitly requests a different location.
    """).strip(),
    "input_schema": {
        "type": "object",
        "properties": {
            "code": {
                "type": "string",
                "description": "CadQuery Python code. It must assign the final model to variable 'result'.",
            },
            "output_path": {
                "type": "string",
                "description": "Advanced: custom STEP file path or output directory. Omit this field to use the configured artifact directory (recommended).",
            },
        },
        "required": ["code"],
    },
}