sdk: improve goView media import robustness

This commit is contained in:
clawdbot 2026-01-28 05:04:41 +08:00
parent 4e479870a9
commit 3dfb548c11
2 changed files with 53 additions and 2 deletions

View File

@ -312,6 +312,30 @@ function optionOf(cIn: GoViewComponentLike): unknown {
);
}
function looksLikeJsonString(input: string): boolean {
const s = input.trim();
if (!s) return false;
// Avoid parsing obvious URLs.
if (/^(https?:\/\/|\/\/|data:|rtsp:\/\/|rtmp:\/\/)/i.test(s)) return false;
return (
(s.startsWith('{') && s.endsWith('}')) ||
(s.startsWith('[') && s.endsWith(']'))
);
}
function normalizeOption(option: unknown): unknown {
// Some exports accidentally store option blobs as JSON strings.
if (typeof option === 'string' && looksLikeJsonString(option)) {
try {
return JSON.parse(option) as unknown;
} catch {
return option;
}
}
return option;
}
function toNumber(v: unknown, fallback: number): number {
if (typeof v === 'number' && Number.isFinite(v)) return v;
if (typeof v === 'string') {
@ -414,7 +438,7 @@ export function convertGoViewProjectToScreen(input: GoViewProjectLike | GoViewSt
for (const raw of componentList) {
const c = unwrapComponent(raw);
const option = optionOf(c);
const option = normalizeOption(optionOf(c));
// We try to infer the widget kind early so we can pick better default sizes
// when exports omit sizing information.

View File

@ -13,8 +13,35 @@ export function pickUrlLike(input: unknown, maxDepth = 3): string {
return pickUrlLikeInner(input, maxDepth);
}
function looksLikeJsonString(input: string): boolean {
const s = input.trim();
if (!s) return false;
// Avoid parsing obvious URLs.
if (/^(https?:\/\/|\/\/|data:|rtsp:\/\/|rtmp:\/\/)/i.test(s)) return false;
// Simple heuristic: JSON objects/arrays.
return (
(s.startsWith('{') && s.endsWith('}')) ||
(s.startsWith('[') && s.endsWith(']')) ||
// sometimes exports double-encode strings: "http://..."
(s.startsWith('"') && s.endsWith('"') && s.includes('://'))
);
}
function pickUrlLikeInner(input: unknown, depth: number): string {
if (typeof input === 'string') return input;
if (typeof input === 'string') {
// Some low-code exports stringify nested option blobs; try to recover URLs from them.
if (depth > 0 && looksLikeJsonString(input)) {
try {
const parsed = JSON.parse(input) as unknown;
const nested = pickUrlLikeInner(parsed, depth - 1);
if (nested) return nested;
} catch {
// ignore
}
}
return input;
}
if (!input) return '';
if (Array.isArray(input)) {