Spaces:
Runtime error
Runtime error
File size: 2,513 Bytes
e2d35c0 7a273d3 c58d3eb e2d35c0 1f1fdda 7a273d3 ea2442a 221eeed ea2442a 7a273d3 e2d35c0 | 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 | import type { AdminModelsSnapshot, WorldSnapshot } from "./types";
const API_BASE = import.meta.env.VITE_WORLD_SIMULATOR_API ?? defaultApiBase();
const ADMIN_TOKEN_STORAGE_KEY = "world-simulator-admin-token";
function defaultApiBase(): string {
if (window.location.hostname === "127.0.0.1" && window.location.port === "5173") {
return "http://127.0.0.1:8000";
}
return window.location.origin;
}
export class ApiResponseError extends Error {
readonly status: number;
constructor(message: string, status: number) {
super(message);
this.name = "ApiResponseError";
this.status = status;
}
}
export async function fetchWorldSnapshot(
signal?: AbortSignal,
options: { warmup?: boolean } = {},
): Promise<WorldSnapshot> {
const url = new URL(`${API_BASE}/scene/state`, window.location.origin);
if (options.warmup) {
url.searchParams.set("warmup", "1");
}
const response = await fetch(url, { signal });
if (!response.ok) {
throw new ApiResponseError(`World snapshot failed: ${response.status}`, response.status);
}
return (await response.json()) as WorldSnapshot;
}
export async function tickWorld(): Promise<void> {
const response = await fetch(`${API_BASE}/tick`, { method: "POST" });
if (!response.ok) {
throw new ApiResponseError(`World tick failed: ${response.status}`, response.status);
}
}
export async function fetchAdminModels(signal?: AbortSignal): Promise<AdminModelsSnapshot> {
const response = await fetch(`${API_BASE}/admin/models`, {
headers: adminHeaders(),
signal,
});
if (!response.ok) {
throw new ApiResponseError(`Model profiles failed: ${response.status}`, response.status);
}
return (await response.json()) as AdminModelsSnapshot;
}
export async function setNpcModelProfile(npcId: string, profileId: string): Promise<void> {
const response = await fetch(`${API_BASE}/admin/npcs/${encodeURIComponent(npcId)}/model`, {
method: "POST",
headers: {
"Content-Type": "application/json",
...adminHeaders(),
},
body: JSON.stringify({ profile_id: profileId }),
});
if (!response.ok) {
throw new ApiResponseError(`NPC model switch failed: ${response.status}`, response.status);
}
}
function adminHeaders(): Record<string, string> {
return {
"X-Admin-Token": adminToken(),
};
}
function adminToken(): string {
return (
import.meta.env.VITE_WORLD_SIMULATOR_ADMIN_TOKEN ||
window.localStorage.getItem(ADMIN_TOKEN_STORAGE_KEY) ||
"dev-admin-token"
);
}
|