Spaces:
Sleeping
Sleeping
File size: 1,030 Bytes
73268e9 | 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 | """Wrapper async untuk SafeCodeExecutor — dipakai oleh api/code_exec.py."""
from __future__ import annotations
import asyncio
from typing import Any
from services.code_executor import safe_executor
async def jalankan_kode_terbatas(kode_sumber: str, panjang_maks: int) -> dict[str, Any]:
"""Jalankan kode di sandbox dan kembalikan hasil dengan field nama Indonesia.
Memanggil SafeCodeExecutor.execute() di thread pool agar tidak blok event loop.
Returns:
Dict dengan kunci: sukses, stdout, stderr, error_kind.
"""
if len(kode_sumber) > panjang_maks:
return {
"sukses": False,
"stdout": "",
"stderr": f"Kode melebihi batas {panjang_maks} karakter.",
"error_kind": "ValidationError",
}
hasil = await asyncio.to_thread(safe_executor.execute, kode_sumber)
return {
"sukses": hasil["success"],
"stdout": hasil["output"],
"stderr": hasil["error"] or "",
"error_kind": hasil["error_type"],
}
|