@llamaindex/ui
Version: 
A comprehensive UI component library built with React, TypeScript, and Tailwind CSS for LlamaIndex applications
184 lines (181 loc) • 5.95 kB
JavaScript
import { __spreadValues, __spreadProps } from './chunk-JD6AELXS.mjs';
import { z } from 'zod/v4';
function zodToJsonSchema(zodSchema, options = {}) {
  const jsonSchema = z.toJSONSchema(zodSchema, {
    reused: "inline"
  });
  return modifyJsonSchema(jsonSchema, options);
}
function modifyJsonSchema(jsonSchema, {
  selectFields,
  firstFields = [],
  lastFields = [],
  hideFields = [],
  dereference = true
}) {
  let jsonObjectSchema = jsonSchema;
  if (dereference) {
    jsonObjectSchema = derefLocalRefs(jsonObjectSchema);
  }
  const updatedProperties = __spreadValues({}, jsonObjectSchema.properties);
  const exclude = new Set(hideFields);
  const shiftedFields = new Set((firstFields || []).concat(lastFields || []));
  const rawOrder = (firstFields || []).concat(
    (selectFields != null ? selectFields : Object.keys(updatedProperties)).filter(
      (x) => !shiftedFields.has(x)
    )
  ).concat(lastFields || []);
  const orderedAndFiltered = rawOrder.filter((field) => {
    if (exclude.has(field)) {
      return false;
    }
    exclude.add(field);
    return true;
  });
  const result = __spreadProps(__spreadValues({}, jsonObjectSchema), {
    properties: orderedAndFiltered.reduce(
      (acc, field) => {
        const key = String(field);
        if (updatedProperties[key] !== void 0) {
          acc[key] = updatedProperties[key];
        }
        return acc;
      },
      {}
    )
  });
  return result;
}
function isNullable(schema) {
  const { isNullable: isNullable2 } = extractFirstNotNullableSchema(schema);
  return isNullable2;
}
function isSimpleTypeSchema(schema) {
  return schema.type === "string" || schema.type === "number" || schema.type === "integer" || schema.type === "boolean" || schema.type === "null";
}
function getSchemaType(schema) {
  var _a, _b, _c, _d;
  if (typeof schema !== "object" || schema === null) {
    return "unknown";
  }
  if (schema.type === void 0 && "anyOf" in schema) {
    const unionSchema = schema;
    const nonNullSchema = (_a = unionSchema.anyOf) == null ? void 0 : _a.find(
      (subSchema) => subSchema.type !== "null"
    );
    if (nonNullSchema) {
      return getSchemaType(nonNullSchema);
    }
    return ((_b = unionSchema.anyOf) == null ? void 0 : _b[0]) ? getSchemaType(unionSchema.anyOf[0]) : "unknown";
  }
  if (schema.type === void 0 && "allOf" in schema) {
    const intersectSchema = schema;
    const nonNullSchema = (_c = intersectSchema.allOf) == null ? void 0 : _c.find(
      (subSchema) => subSchema.type !== "null"
    );
    if (nonNullSchema) {
      return getSchemaType(nonNullSchema);
    }
    return ((_d = intersectSchema.allOf) == null ? void 0 : _d[0]) ? getSchemaType(intersectSchema.allOf[0]) : "unknown";
  }
  if (!schema.type) {
    return "unknown";
  }
  return schema.type;
}
function isArraySchema(schema) {
  return schema.type === "array";
}
function extractFirstNotNullableSchema(schema) {
  var _a, _b, _c, _d, _e;
  if (schema.type === void 0 && schema.anyOf) {
    const anyNullable = (_b = (_a = schema.anyOf) == null ? void 0 : _a.some(
      (subSchema) => subSchema.type === "null"
    )) != null ? _b : false;
    if (anyNullable) {
      return {
        isNullable: true,
        schema: (_c = schema.anyOf) == null ? void 0 : _c.find(
          (subSchema) => subSchema.type !== "null"
        )
      };
    }
    return { isNullable: false, schema };
  }
  if (schema.type === void 0 && schema.allOf) {
    const allNullable = (_e = (_d = schema.allOf) == null ? void 0 : _d.some(
      (subSchema) => subSchema.type === "null"
    )) != null ? _e : false;
    if (allNullable) {
      return { isNullable: true, schema: void 0 };
    }
    return { isNullable: false, schema };
  }
  const isNullType = schema.type === "null";
  return { isNullable: isNullType, schema: isNullType ? void 0 : schema };
}
function isObjectSchema(schema) {
  return schema.type === "object";
}
function derefLocalRefs(schema) {
  const root = schema;
  const cache = /* @__PURE__ */ new Map();
  function cloneAndDeref(node, trail = []) {
    if (node && typeof node === "object") {
      const key = node;
      if (cache.has(key)) return cache.get(key);
      if (!Array.isArray(node)) {
        const obj = node;
        const refValue = obj["$ref"];
        if (typeof refValue === "string" && refValue.startsWith("#/")) {
          const target = resolvePointer(root, refValue);
          const rest = __spreadValues({}, obj);
          delete rest["$ref"];
          const expanded = cloneAndDeref(target, trail.concat([refValue]));
          return cloneAndDeref(
            __spreadValues(__spreadValues({}, expanded), rest),
            trail
          );
        }
      }
      const out = Array.isArray(node) ? [] : {};
      cache.set(key, out);
      for (const [k, v] of Object.entries(node)) {
        out[k] = cloneAndDeref(
          v,
          trail.concat([k])
        );
      }
      return out;
    }
    return node;
  }
  return cloneAndDeref(schema);
}
function resolvePointer(obj, pointer) {
  const parts = pointer.slice(2).split("/").map(unescapeToken);
  let cur = obj;
  for (const p of parts) {
    if (cur == null || typeof cur !== "object") {
      throw new Error(`Pointer ${pointer} not found at "${p}"`);
    }
    if (Array.isArray(cur)) {
      const index = Number(p);
      if (!Number.isInteger(index) || index < 0 || index >= cur.length) {
        throw new Error(`Pointer ${pointer} not found at "${p}"`);
      }
      cur = cur[index];
    } else {
      const objCur = cur;
      if (!(p in objCur)) {
        throw new Error(`Pointer ${pointer} not found at "${p}"`);
      }
      cur = objCur[p];
    }
  }
  return cur;
}
function unescapeToken(t) {
  return t.replace(/~1/g, "/").replace(/~0/g, "~");
}
export { derefLocalRefs, extractFirstNotNullableSchema, getSchemaType, isArraySchema, isNullable, isObjectSchema, isSimpleTypeSchema, modifyJsonSchema, zodToJsonSchema };