forked from longtao.wu/droboticflow
143 lines
4.6 KiB
JavaScript
143 lines
4.6 KiB
JavaScript
const SOURCE_DATA_CONTENT_PATH_PREFIX = "/api/source-data/collections/";
|
|
const PACKAGE_CONTENT_PATH_PREFIX = "/api/packages/";
|
|
const OBJECT_STORAGE_CONTENT_PATH = "/api/object-storage/content";
|
|
const RUN_ARTIFACT_CONTENT_PATH_PREFIX = "/api/runs/";
|
|
const HUMAN_EVIDENCE_CONTENT_PATH_PREFIX = "/api/plugins/human-loop/items/";
|
|
|
|
function normalizeURL(input) {
|
|
if (!input || typeof input !== "string") {
|
|
return null;
|
|
}
|
|
try {
|
|
return new URL(input, "http://droboticflow.invalid");
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function isSourceDataContentURL(input) {
|
|
const normalized = normalizeURL(input);
|
|
if (!normalized) {
|
|
return false;
|
|
}
|
|
return normalized.pathname.startsWith(SOURCE_DATA_CONTENT_PATH_PREFIX) && normalized.pathname.endsWith("/content");
|
|
}
|
|
|
|
export function isPackageContentURL(input) {
|
|
const normalized = normalizeURL(input);
|
|
if (!normalized) {
|
|
return false;
|
|
}
|
|
return normalized.pathname.startsWith(PACKAGE_CONTENT_PATH_PREFIX) && normalized.pathname.endsWith("/content");
|
|
}
|
|
|
|
export function isObjectStorageContentURL(input) {
|
|
const normalized = normalizeURL(input);
|
|
if (!normalized) {
|
|
return false;
|
|
}
|
|
return normalized.pathname === OBJECT_STORAGE_CONTENT_PATH;
|
|
}
|
|
|
|
export function isRunArtifactContentURL(input) {
|
|
const normalized = normalizeURL(input);
|
|
if (!normalized) {
|
|
return false;
|
|
}
|
|
return (
|
|
normalized.pathname.startsWith(RUN_ARTIFACT_CONTENT_PATH_PREFIX) &&
|
|
normalized.pathname.includes("/artifacts/") &&
|
|
normalized.pathname.endsWith("/content")
|
|
);
|
|
}
|
|
|
|
export function isHumanEvidenceContentURL(input) {
|
|
const normalized = normalizeURL(input);
|
|
if (!normalized) return false;
|
|
return (
|
|
normalized.pathname.startsWith(HUMAN_EVIDENCE_CONTENT_PATH_PREFIX) &&
|
|
normalized.pathname.includes("/evidence/") &&
|
|
normalized.pathname.endsWith("/content")
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Detect a `bos://bucket/key` URL and return true.
|
|
*/
|
|
export function isBosURL(input) {
|
|
return typeof input === "string" && /^bos:\/\//i.test(input.trim());
|
|
}
|
|
|
|
/**
|
|
* Convert a `bos://bucket/key` URL to the object-storage proxy endpoint
|
|
* `/api/object-storage/content?bucket=BUCKET&key=KEY`.
|
|
*/
|
|
export function bosToProxyURL(input) {
|
|
if (!isBosURL(input)) return null;
|
|
const withoutScheme = input.trim().replace(/^bos:\/\//i, "");
|
|
const slashIndex = withoutScheme.indexOf("/");
|
|
if (slashIndex < 0) return null;
|
|
const bucket = withoutScheme.slice(0, slashIndex);
|
|
const key = withoutScheme.slice(slashIndex + 1);
|
|
if (!bucket || !key) return null;
|
|
return `/api/object-storage/content?bucket=${encodeURIComponent(bucket)}&key=${encodeURIComponent(key)}`;
|
|
}
|
|
|
|
/**
|
|
* Append a Media Fragment (`#t=start,end`) to a playable URL so the browser
|
|
* plays only the [start, end] seconds window of the referenced source video.
|
|
* Values must be finite non-negative numbers; otherwise the URL is unchanged.
|
|
*/
|
|
export function applyMediaFragmentRange(input, startSecond, endSecond) {
|
|
const start = Number(startSecond);
|
|
const end = Number(endSecond);
|
|
if (!Number.isFinite(start) || !Number.isFinite(end)) return input;
|
|
if (start < 0 || end <= start) return input;
|
|
if (typeof input !== "string" || !input) return input;
|
|
return `${input}#t=${start.toFixed(3)},${end.toFixed(3)}`;
|
|
}
|
|
|
|
/**
|
|
* Build a browser-playable video URL for an execution artifact, honoring a
|
|
* virtual-cut time range carried on `item.data` when present.
|
|
*/
|
|
export function buildArtifactPlayableVideoURL(item, previewURL) {
|
|
const playableURL = buildBrowserPlayableVideoURL(previewURL);
|
|
const data = item?.data && typeof item.data === "object" ? item.data : null;
|
|
if (data?.virtualCut === true) {
|
|
return applyMediaFragmentRange(playableURL, data.startSecond, data.endSecond);
|
|
}
|
|
return playableURL;
|
|
}
|
|
|
|
export function buildBrowserPlayableVideoURL(input) {
|
|
// Handle bos:// URLs by converting to the object-storage proxy
|
|
if (isBosURL(input)) {
|
|
const proxyURL = bosToProxyURL(input);
|
|
if (proxyURL) return `${proxyURL}&transcode=browser`;
|
|
return input;
|
|
}
|
|
|
|
const normalized = normalizeURL(input);
|
|
if (
|
|
!normalized ||
|
|
(!isSourceDataContentURL(input) &&
|
|
!isPackageContentURL(input) &&
|
|
!isObjectStorageContentURL(input) &&
|
|
!isRunArtifactContentURL(input) &&
|
|
!isHumanEvidenceContentURL(input))
|
|
) {
|
|
return typeof input === "string" ? input : "";
|
|
}
|
|
|
|
if (normalized.searchParams.get("transcode") !== "browser") {
|
|
normalized.searchParams.set("transcode", "browser");
|
|
}
|
|
|
|
if (/^https?:\/\//i.test(input)) {
|
|
return normalized.toString();
|
|
}
|
|
const query = normalized.searchParams.toString();
|
|
return `${normalized.pathname}${query ? `?${query}` : ""}${normalized.hash}`;
|
|
}
|