VEHwang's picture
Upload CoCogni4D-Bench (questions, media, scripts, render records, code) (part 4)
9d57416 verified
Raw History Blame Contribute Delete
3.48 kB
import type { SegmentView } from './contract.ts';
import type {SegmentFacts} from './facts-view.ts';
import {errorText} from './text.ts';
import {postNP} from './generation-logic.ts';
import type {BatchSettings, ExpandResult, GenSchema} from './np-batch.ts';
export type SegmentSummary = {
id: string; root: string; script_id: string; world_id: string;
duration_tier: string; duration_s: number; actor_count: number;
actor_counts: Record<string, number>;
intent_counts: {completed: number; discarded: number; truncated: number};
idle_remainder_fraction: number | null; selfcheck_conclusion: string;
generator_version: string; integrity: 'not_checked'; metadata_error: string | null;
};
const earlyRequests = new Map<string, Promise<unknown>>();
export function prefetchJSON(url: string) {
if (!earlyRequests.has(url)) {
const request = getJSON(url);
earlyRequests.set(url, request);
void request.catch(() => {if (earlyRequests.get(url) === request) earlyRequests.delete(url);});
}
}
export async function getJSON<T>(url: string, signal?: AbortSignal): Promise<T> {
const early = earlyRequests.get(url);
if (early) {earlyRequests.delete(url); return early as Promise<T>;}
try {
const response = await fetch(url, {signal, credentials: 'same-origin'});
const data = await response.json();
if (!response.ok) throw new Error(`${data.error?.code || response.status}:${errorText(data.error?.message || '请求失败')}`);
return data as T;
} catch (error) {
if (error instanceof Error && error.name === 'AbortError') throw error;
throw new Error(errorText(error));
}
}
export const getSegments = (signal?: AbortSignal) => getJSON<{segments: SegmentSummary[]}>('/api/v1/segments', signal);
export const getFacts = (id: string, view: string | undefined, signal?: AbortSignal) =>
getJSON<SegmentFacts>('/api/v1/segments/' + encodeURIComponent(id) + '/facts?hz=2' + (view ? '&view=' + encodeURIComponent(view) : ''), signal);
export const getSegment = (id: string, signal?: AbortSignal) => getJSON<SegmentView>('/api/v1/segments/' + encodeURIComponent(id), signal);
export type CatalogScene = {
id: string; capability_ready: boolean; rooms: number; by_label: Record<string, number>;
area_m2: number; outdoor: boolean; regions: {label: string; area_m2: number}[]; preview: string;
};
export type CatalogAsset = {
id: string; category: string; kind_zh: string; source: {path: string} | null;
bbox_m: [number, number, number] | null; full_height_m: number | null;
description: string | null; description_missing: boolean; preview: string; unused?: string;
};
export type CatalogData = {schema: string; scenes: CatalogScene[]; assets: CatalogAsset[];
unknown_labels: Record<string, number>; errors: {id: string; reason: string}[]};
// One catalog read per page: the config page and its two catalog panels each asked for it, a serial round on a slow link.
let catalogRequest: Promise<CatalogData> | null = null;
export function getCatalog(): Promise<CatalogData> {
if (!catalogRequest) {const request = catalogRequest = getJSON<CatalogData>('/api/v1/np/catalog'); void request.catch(() => {if (catalogRequest === request) catalogRequest = null;});}
return catalogRequest;
}
export const getGenSchema = (signal?: AbortSignal) => getJSON<GenSchema>('/api/v1/np/gen-schema', signal);
export const postExpand = (batch: BatchSettings, signal?: AbortSignal) => postNP<ExpandResult>('/api/v1/np/expand', {batch}, signal);