Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Generate text and images via the reverse-engineered Gemini Web API with multi-turn conversation support.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/gemini-webapi/utils/parsing.ts
1import { logger } from './logger.js';23export function get_nested_value<T = unknown>(data: unknown, path: number[], def?: T): T {4let cur: unknown = data;5for (let i = 0; i < path.length; i++) {6const k = path[i]!;7if (!Array.isArray(cur)) {8logger.debug(`Safe navigation: path ${JSON.stringify(path)} ended at index ${i} (key '${k}'), returning default.`);9return def as T;10}11cur = cur[k];12if (cur === undefined) {13logger.debug(`Safe navigation: path ${JSON.stringify(path)} ended at index ${i} (key '${k}'), returning default.`);14return def as T;15}16}1718if (cur == null && def !== undefined) return def as T;19return cur as T;20}2122export function extract_json_from_response(text: string): unknown {23if (typeof text !== 'string') {24throw new TypeError(`Input text is expected to be a string, got ${typeof text} instead.`);25}2627let last: unknown = undefined;28for (const line of text.split(/\r?\n/)) {29const trimmed = line.trim();30if (!trimmed) continue;31try {32last = JSON.parse(trimmed) as unknown;33} catch {}34}3536if (last === undefined) {37throw new Error('Could not find a valid JSON object or array in the response.');38}3940return last;41}4243export const extractJsonFromResponse = extract_json_from_response;44export const getNestedValue = get_nested_value;45