Spaces:
Sleeping
Sleeping
File size: 6,435 Bytes
32e1e21 6930eea 3bc4a98 98231b9 7ffe280 98231b9 3d6af21 98231b9 7ffe280 98231b9 3d6af21 98231b9 3d6af21 98231b9 32e1e21 bb4b10d 32e1e21 6930eea 7ffe280 3bc4a98 7ffe280 3bc4a98 | 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 | export type DemoMode = "baseline" | "oracle";
export type StationNode = {
station_id: string;
name: string;
slug: string;
lat: number;
lng: number;
total_slots: number;
};
export type DemoNewResponse = {
session_id: string;
obs: any;
station_nodes: StationNode[];
scenario?: string;
seed?: number;
sim_version?: string;
scenario_schedule?: any[];
};
export type DemoStepResponse = {
obs: any;
event: any;
scenario?: string;
scenario_events_at_tick?: any[];
tick?: number;
sim_version?: string;
anti_cheat_flags?: string[];
anti_cheat_details?: Record<string, string>;
role_kpis?: Record<string, Record<string, number>>;
role_reward_breakdown?: Record<string, Record<string, number>>;
mode?: "baseline" | "oracle";
oracle_lora_repo?: string;
oracle_llm_active?: boolean;
oracle_timed_out?: boolean;
oracle_skipped_env?: boolean;
action?: any;
forced_action?: boolean;
};
export type DemoSpawnVehicleResponse = {
request_id?: string;
session_id: string;
spawned_ev?: any;
assignment?: any;
event?: any;
ms?: number;
};
export type MANewResponse = {
session_id: string;
obs: any;
station_nodes: StationNode[];
scenario?: string;
seed?: number;
sim_version?: string;
messages?: any[];
grid_directive?: any;
};
export type MAStepResponse = {
session_id: string;
obs: any;
tick?: number;
scenario?: string;
grid_directive?: any;
fleet_action?: any;
resolved_action?: any;
violations?: string[];
messages?: any[];
role_rewards?: any;
};
async function sleep(ms: number) {
await new Promise((r) => setTimeout(r, ms));
}
export async function demoNew(seed: number, scenario: string = "baseline", fleet_mode: string = "mixed"): Promise<DemoNewResponse> {
const maxAttempts = 3;
let lastErr: unknown = null;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
const ctl = new AbortController();
const timeoutMs = 90_000;
const t = window.setTimeout(() => ctl.abort(), timeoutMs);
try {
const r = await fetch("/demo/new", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ seed, scenario, fleet_mode }),
signal: ctl.signal,
});
if (!r.ok) {
let detail = "";
try {
const j = await r.json();
detail = j?.detail ? ` — ${String(j.detail)}` : ` — ${JSON.stringify(j).slice(0, 500)}`;
} catch {
try {
const txt = await r.text();
detail = txt ? ` — ${txt.slice(0, 500)}` : "";
} catch {
detail = "";
}
}
throw new Error(`demoNew failed: ${r.status}${detail}`);
}
return (await r.json()) as DemoNewResponse;
} catch (e: any) {
lastErr = e;
const isAbort = e?.name === "AbortError";
if (attempt >= maxAttempts) {
if (isAbort) {
throw new Error(
"demoNew timed out (90s). The Space may be cold-starting. Wait ~30s and refresh, or try again."
);
}
throw e;
}
// Brief backoff for HF Spaces cold-start / transient network.
await sleep(isAbort ? 1_250 : 650);
} finally {
window.clearTimeout(t);
}
}
throw lastErr instanceof Error ? lastErr : new Error("demoNew failed.");
}
export async function demoStep(args: {
session_id: string;
mode: DemoMode;
oracle_lora_repo: string;
forced_action?: any;
}): Promise<DemoStepResponse> {
const ctl = new AbortController();
const t = window.setTimeout(() => ctl.abort(), 240_000);
try {
const r = await fetch("/demo/step", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(args),
signal: ctl.signal,
});
if (!r.ok) {
let detail = "";
try {
const j = await r.json();
detail = j?.detail ? ` — ${String(j.detail)}` : ` — ${JSON.stringify(j).slice(0, 500)}`;
} catch {
try {
const txt = await r.text();
detail = txt ? ` — ${txt.slice(0, 500)}` : "";
} catch {
detail = "";
}
}
throw new Error(`demoStep failed: ${r.status}${detail}`);
}
return (await r.json()) as DemoStepResponse;
} catch (e: any) {
if (e?.name === "AbortError") {
throw new Error("demoStep timed out after 4m — server may be loading Qwen+LoRA on CPU; try ORACLE_SKIP_LLM=1 on Space or fix LoRA repo id.");
}
throw e;
} finally {
window.clearTimeout(t);
}
}
export async function demoSpawnVehicle(args: {
session_id: string;
min_station_dist_m?: number;
battery_threshold_pct?: number;
}): Promise<DemoSpawnVehicleResponse> {
const r = await fetch("/demo/spawn_vehicle", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(args),
});
if (!r.ok) {
let detail = "";
try {
const j = await r.json();
detail = j?.detail ? ` — ${String(j.detail)}` : ` — ${JSON.stringify(j).slice(0, 500)}`;
} catch {
try {
const txt = await r.text();
detail = txt ? ` — ${txt.slice(0, 500)}` : "";
} catch {
detail = "";
}
}
throw new Error(`demoSpawnVehicle failed: ${r.status}${detail}`);
}
return (await r.json()) as DemoSpawnVehicleResponse;
}
export async function maNew(seed: number, scenario: string = "baseline", fleet_mode: string = "mixed"): Promise<MANewResponse> {
const r = await fetch("/ma/new", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ seed, scenario, fleet_mode }),
});
if (!r.ok) throw new Error(`maNew failed: ${r.status}`);
return (await r.json()) as MANewResponse;
}
export async function maAutoStep(args: {
session_id: string;
fleet_policy: "baseline" | "oracle";
oracle_lora_repo?: string;
}): Promise<MAStepResponse> {
const r = await fetch("/ma/auto_step", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(args),
});
if (!r.ok) throw new Error(`maAutoStep failed: ${r.status}`);
return (await r.json()) as MAStepResponse;
}
|