Skip to content

Commit 15919cf

Browse files
committed
feat(FR-1694): Create a new agent page using the agent_nodes query
1 parent e9ef843 commit 15919cf

File tree

6 files changed

+1245
-3
lines changed

6 files changed

+1245
-3
lines changed

packages/backend.ai-ui/src/helper/index.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,39 @@ export const omitNullAndUndefinedFields = <T extends Record<string, any>>(
386386
) as Partial<T>;
387387
};
388388

389+
/**
390+
* Safely parses an unknown input into a plain object record.
391+
*
392+
* Accepts either:
393+
* - a non-null object (not an array) returned as-is
394+
* - a JSON string starting with '{' which will be parsed
395+
* Otherwise returns an empty object.
396+
*
397+
* This is a generalized form of the previous normalizeGpuAllocMap helper.
398+
*
399+
* @param raw - Unknown input value that may be an object or JSON string
400+
* @returns A record object or an empty object if parsing fails
401+
*/
402+
export function parseObjectMap<T = any>(raw: unknown): Record<string, T> {
403+
if (!raw) return {};
404+
if (typeof raw === 'object' && raw !== null && !Array.isArray(raw)) {
405+
return raw as Record<string, T>;
406+
}
407+
if (typeof raw === 'string') {
408+
const s = raw.trim();
409+
if (!s.startsWith('{')) return {};
410+
try {
411+
const parsed = JSON.parse(s);
412+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
413+
return parsed as Record<string, T>;
414+
}
415+
} catch {
416+
return {};
417+
}
418+
}
419+
return {};
420+
}
421+
389422
/**
390423
* Generates a random string of alphabetic characters.
391424
*

0 commit comments

Comments
 (0)