File size: 2,259 Bytes
0e23a69 | 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 | // ── NeuralEdge API Service ────────────────────────────────────────
// Talks to the STATEFUL /game/* endpoints (not the stateless /reset + /step).
// Base URL: VITE_API_BASE env var OR http://localhost:8000
const BASE = import.meta.env.VITE_API_BASE ?? 'http://localhost:8000'
async function _post(path, body) {
const res = await fetch(`${BASE}${path}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
})
if (!res.ok) {
const text = await res.text().catch(() => '')
throw new Error(`${path} → ${res.status}: ${text}`)
}
return res.json()
}
/**
* POST /game/reset — start a stateful episode (persists server-side).
* @param {number} seed
* @returns {Promise<{ observation, reward, done, info }>}
*/
export async function apiReset(seed = 42) {
return _post('/game/reset', { seed })
}
/**
* POST /game/step — advance the persistent episode by one round.
* @param {string} decision
* @param {string} coalitionPitch
* @returns {Promise<{ observation, reward, done, info }>}
*/
export async function apiStep(decision, coalitionPitch = '') {
return _post('/game/step', {
decision,
coalition_pitch: coalitionPitch,
})
}
/**
* GET /health — backend liveness check.
* @returns {Promise<boolean>}
*/
export async function apiHealth() {
try {
const res = await fetch(`${BASE}/health`)
const data = await res.json()
return data.status === 'healthy'
} catch {
return false
}
}
/**
* POST /qwen/decide — ask the backend Qwen/Ollama proxy for a decision.
* Falls back to greedy on the server side if Ollama is unavailable.
* @param {object} obs The current BoardSimObservation from state
* @returns {Promise<{ decision: string, coalition_pitch: string, source: string }>}
*/
export async function apiQwenDecide(obs) {
return _post('/qwen/decide', {
state: obs.state ?? {},
event: obs.event ?? '',
options: obs.options ?? [],
npc_statements: obs.npc_statements ?? [],
round: obs.round ?? 1,
})
}
|