Lewati ke konten utama

Scripts

Manage SeraphRuntime Lua script — engine yang sama dipake editor in-app. Setiap script dikunci (client_id, script_id) jadi banyak script bisa jalan barengan di banyak bot.

MethodPathBody / Query
POST/api/scripts/execute{client_id, script_id, source}
POST/api/scripts/stop{client_id, script_id}
GET/api/scripts/status?client_id&script_id
GET/api/scripts/console?client_id&script_id
POST/api/scripts/console/clear{client_id, script_id}

POST /api/scripts/execute

Compile + start source Lua di bot. Script jalan detached di scheduler background — call ini return pas runtime udah accept source-nya.

Body:

{
"client_id": "bot-1",
"script_id": "auto-mine",
"source": "while true do sleep(1000); print(\"tick\"); end"
}

Response (200):

{ "ok": true }

Errors:

StatusBody
404{"error":"unknown bot: bot-99"}
500{"error":"lua compile: <line>: unexpected symbol near 'end'"}

POST /api/scripts/stop

Cancel script yang lagi jalan. Runtime kirim sinyal cancellation lewat semua coroutine yang script-nya spawn; kalau script-nya register blok cleanup dia dapat kesempatan finish dulu sebelum VM di-drop.

Body:

{ "client_id": "bot-1", "script_id": "auto-mine" }

Response (200):

{ "ok": true }

Idempotent — stop script yang gak lagi jalan juga return 200.

GET /api/scripts/status

Query: ?client_id=bot-1&script_id=auto-mine

Response (200):

{
"running": true,
"last_error": null
}

last_error null waktu jalan dan kalau exit clean; ke-populate kalau VM mati dengan error runtime ("executor:42: attempt to index nil value (field 'world')").

GET /api/scripts/console

Buffer stdout yang di-capture script. Setiap print(...) dari sisi Lua append satu line; runtime trim buffer di ~5.000 entry biar script jangka panjang gak OOM-in host-nya.

Query: ?client_id=bot-1&script_id=auto-mine

Response (200):

{
"lines": [
{ "ts_ms": 1778205616123, "level": "info", "text": "tick" },
{ "ts_ms": 1778205617123, "level": "info", "text": "tick" },
{ "ts_ms": 1778205618400, "level": "error", "text": "executor:8: tile not found" }
]
}

level salah satu dari info, warn, error — di-set sesuai panggilan Lua-nya (print, warn, error throw).

POST /api/scripts/console/clear

Truncate buffer console.

Body:

{ "client_id": "bot-1", "script_id": "auto-mine" }

Response (200): { "ok": true }