mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-14 05:42:54 -07:00
feat(ui): pretty JSON rendering tool outputs (#9767)
Co-authored-by: Bryan Morgan <bryanmorgan@google.com>
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import stripAnsi from 'strip-ansi';
|
||||
|
||||
export function checkInput(input: string | null | undefined): boolean {
|
||||
if (input === null || input === undefined) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const trimmed = input.trim();
|
||||
if (!trimmed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!/^(?:\[|\{)/.test(trimmed)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (stripAnsi(trimmed) !== trimmed) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export function tryParseJSON(input: string): object | null {
|
||||
if (!checkInput(input)) return null;
|
||||
const trimmed = input.trim();
|
||||
try {
|
||||
const parsed = JSON.parse(trimmed);
|
||||
if (parsed === null || typeof parsed !== 'object') {
|
||||
return null;
|
||||
}
|
||||
if (Array.isArray(parsed) && parsed.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!Array.isArray(parsed) && Object.keys(parsed).length === 0) return null;
|
||||
|
||||
return parsed;
|
||||
} catch (_err) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user