UNPKG

@hashbrownai/core

Version:

Runtime helpers for Hashbrown AI

471 lines 14.2 kB
/** * Skillet is an LLM-optimized streaming JSON Parser - perfectly suited for streaming hot and fresh JSON. * * Portions of this code are derived from Zod (MIT License) (https://github.com/colinhacks/zod). * See the LICENSE file in the project root for full license text. * * @license MIT * @author LiveLoveApp, LLC * @see https://github.com/liveloveapp/hashbrown * @see https://github.com/colinhacks/zod */ import { CleanInterfaceShape, Flatten, IsStringUnion, IsUnion, UnionToTuple } from '../utils/types'; /** * @internal */ export declare const internal = "~schema"; export type internal = typeof internal; export declare const PRIMITIVE_WRAPPER_FIELD_NAME = "__wrappedPrimitive"; type TypeInternals = { definition: { description: string; streaming: boolean; }; }; type TypeBox = { [internal]: TypeInternals; toJsonSchema: () => object; parseJsonSchema: (object: unknown, path: string[]) => any; toTypeScript: (pathSeen?: Set<HashbrownType>) => string; }; /** * @internal */ export interface HashbrownTypeCtor<T extends TypeBox, D = T[internal]['definition']> { new (def: D): T; init(inst: T, def: D): asserts inst is T; toJsonSchema(schema: any): any; parseJsonSchema(object: unknown, path: string[]): any; toTypeScript: (pathSeen?: Set<HashbrownType>) => string; } /** * @internal */ export declare const HashbrownTypeCtor: <T extends TypeBox, D extends TypeInternals["definition"] = T[internal]["definition"]>({ name, initializer, toJsonSchemaImpl, parseJsonSchemaImpl, toTypeScriptImpl, validateImpl, toStreamingImpl, }: { name: string; initializer: (instance: T, definition: D) => void; toJsonSchemaImpl: (schema: HashbrownTypeCtor<T, D>) => any; parseJsonSchemaImpl: (schema: HashbrownTypeCtor<T, D>, object: unknown, path: string[]) => any; toTypeScriptImpl: (schema: HashbrownTypeCtor<T, D>, pathSeen: Set<HashbrownType>) => string; validateImpl: (schema: HashbrownTypeCtor<T, D>, definition: D, object: unknown, path: string[]) => void; toStreamingImpl: (schema: HashbrownTypeCtor<T, D>, definition: D, object: unknown, path: string[]) => unknown; }) => HashbrownTypeCtor<T, D>; interface HashbrownTypeDefinition { type: 'string' | 'literal' | 'number' | 'boolean' | 'integer' | 'object' | 'array' | 'enum' | 'any-of' | 'null'; description: string; streaming: boolean; } /** * @public */ export interface HashbrownType<out Result = unknown> { [internal]: HashbrownTypeInternals<Result>; toJsonSchema: () => any; parseJsonSchema: (object: unknown, path?: string[]) => any; validate: (object: unknown, path?: string[]) => void; toTypeScript: (pathSeen?: Set<HashbrownType>) => string; toStreaming: (object: unknown, path?: string[]) => unknown; } /** * @internal */ export interface HashbrownTypeInternals<out Result = unknown> extends HashbrownType<Result> { definition: HashbrownTypeDefinition; result: Result; } /** * @public */ export declare const HashbrownType: HashbrownTypeCtor<HashbrownType>; /** * -------------------------------------- * -------------------------------------- * String Type * -------------------------------------- * -------------------------------------- */ interface StringTypeDefinition extends HashbrownTypeDefinition { type: 'string'; } /** * @internal */ export interface StringTypeInternals extends HashbrownTypeInternals<string> { definition: StringTypeDefinition; } /** * @public */ export interface StringType extends HashbrownType<string> { [internal]: StringTypeInternals; } /** * @public */ export declare const StringType: HashbrownTypeCtor<StringType>; /** * @public */ export declare function isStringType(type: HashbrownType): type is StringType; /** * @public */ export declare function string(description: string): StringType; /** * -------------------------------------- * -------------------------------------- * Literal Type * -------------------------------------- * -------------------------------------- */ interface LiteralTypeDefinition<T extends string | number | boolean = string | number | boolean> extends HashbrownTypeDefinition { type: 'literal'; value: T; } /** * @internal */ export interface LiteralTypeInternals<T extends string | number | boolean = string | number | boolean> extends HashbrownTypeInternals<T> { definition: LiteralTypeDefinition<T>; } /** * @public */ export interface LiteralType<T extends string | number | boolean = string | number | boolean> extends HashbrownType<T> { [internal]: LiteralTypeInternals<T>; } /** * @public */ export declare const LiteralType: HashbrownTypeCtor<LiteralType>; /** * @public */ export declare function isLiteralType(type: HashbrownType): type is LiteralType; /** * @public */ export declare function literal<T extends string>(value: T): LiteralType<T>; /** * -------------------------------------- * -------------------------------------- * Number Type * -------------------------------------- * -------------------------------------- */ interface NumberTypeDefinition extends HashbrownTypeDefinition { type: 'number'; } /** * @internal */ export interface NumberTypeInternals extends HashbrownTypeInternals<number> { definition: NumberTypeDefinition; } /** * @public */ export interface NumberType extends HashbrownType<number> { [internal]: NumberTypeInternals; } /** * @public */ export declare const NumberType: HashbrownTypeCtor<NumberType>; /** * @public */ export declare function isNumberType(type: HashbrownType): type is NumberType; /** * @public */ export declare function number(description: string): NumberType; /** * -------------------------------------- * -------------------------------------- * Boolean Type * -------------------------------------- * -------------------------------------- */ interface BooleanTypeDefinition extends HashbrownTypeDefinition { type: 'boolean'; } /** * @internal */ export interface BooleanTypeInternals extends HashbrownTypeInternals<boolean> { definition: BooleanTypeDefinition; } /** * @public */ export interface BooleanType extends HashbrownType<boolean> { [internal]: BooleanTypeInternals; } /** * @public */ export declare const BooleanType: HashbrownTypeCtor<BooleanType>; /** * @public */ export declare function isBooleanType(type: HashbrownType): type is BooleanType; /** * @public */ export declare function boolean(description: string): BooleanType; /** * -------------------------------------- * -------------------------------------- * Integer Type * -------------------------------------- * -------------------------------------- */ interface IntegerTypeDefinition extends HashbrownTypeDefinition { type: 'integer'; } /** * @internal */ export interface IntegerTypeInternals extends HashbrownTypeInternals<number> { definition: IntegerTypeDefinition; } /** * @public */ export interface IntegerType extends HashbrownType<number> { [internal]: IntegerTypeInternals; } /** * @public */ export declare const IntegerType: HashbrownTypeCtor<IntegerType>; /** * @public */ export declare function isIntegerType(type: HashbrownType): type is IntegerType; /** * @public */ export declare function integer(description: string): IntegerType; /** * -------------------------------------- * -------------------------------------- * Object Type * -------------------------------------- * -------------------------------------- */ type ObjectTypeResult<T extends Record<string, any>> = string extends keyof T ? object : {} extends T ? object : Flatten<{ -readonly [K in keyof T]: T[K][internal]['result']; }>; interface ObjectTypeDefinition<out Shape extends Record<string, any> = Record<string, any>> extends HashbrownTypeDefinition { type: 'object'; readonly shape: Shape; } /** * @internal */ export interface ObjectTypeInternals<Result extends Readonly<Record<string, any>>> extends HashbrownTypeInternals<ObjectTypeResult<Result>> { definition: ObjectTypeDefinition<Result>; } /** * @public */ export interface ObjectType<Result extends Readonly<Record<string, any>> = Readonly<Record<string, any>>> extends HashbrownType { [internal]: ObjectTypeInternals<Result>; } /** * @public */ export declare const ObjectType: HashbrownTypeCtor<ObjectType>; /** * @public */ export declare function isObjectType(type: HashbrownType): type is ObjectType; /** * @public */ export declare function object<Shape extends Record<string, any>>(description: string, shape: Shape): ObjectType<CleanInterfaceShape<Shape>>; /** * -------------------------------------- * -------------------------------------- * Array Type * -------------------------------------- * -------------------------------------- */ interface ArrayTypeDefinition<out Item extends HashbrownType = HashbrownType> extends HashbrownTypeDefinition { type: 'array'; element: Item; } /** * @internal */ export interface ArrayTypeInternals<Item extends HashbrownType = HashbrownType> extends HashbrownTypeInternals<Item[internal]['result'][]> { definition: ArrayTypeDefinition<Item>; } /** * @public */ export interface ArrayType<Item extends HashbrownType = HashbrownType> extends HashbrownType { [internal]: ArrayTypeInternals<Item>; } /** * @public */ export declare const ArrayType: HashbrownTypeCtor<ArrayType>; /** * @public */ export declare function isArrayType(type: HashbrownType): type is ArrayType; /** * @public */ export declare function array<Item extends HashbrownType>(description: string, item: Item): ArrayType<Item>; /** * -------------------------------------- * -------------------------------------- * Any-Of Type * -------------------------------------- * -------------------------------------- */ interface AnyOfTypeDefinition<Options extends readonly HashbrownType[] = readonly HashbrownType[]> extends HashbrownTypeDefinition { type: 'any-of'; options: Options; } /** * @internal */ export interface AnyOfTypeInternals<Options extends readonly HashbrownType[]> extends HashbrownTypeInternals<Options[number][internal]['result']> { definition: AnyOfTypeDefinition<Options>; } /** * @public */ export interface AnyOfType<Options extends readonly HashbrownType[] = readonly HashbrownType[]> extends HashbrownType<Options[number][internal]['result']> { [internal]: AnyOfTypeInternals<Options>; } /** * @public */ export declare const AnyOfType: HashbrownTypeCtor<AnyOfType>; /** * @public */ export declare function isAnyOfType(type: HashbrownType): type is AnyOfType; /** * @public */ export declare function anyOf<const Options extends readonly HashbrownType[]>(options: Options): SchemaForUnion<Options[number][internal]['result']>; /** * -------------------------------------- * -------------------------------------- * Enum Type * -------------------------------------- * -------------------------------------- */ /** * @internal */ interface EnumTypeDefinition<out Entries extends readonly any[]> extends HashbrownTypeDefinition { type: 'enum'; entries: Entries; } /** * @internal */ export interface EnumTypeInternals<Result extends readonly any[]> extends HashbrownTypeInternals<Result[number]> { definition: EnumTypeDefinition<Result>; } /** * @public */ export interface EnumType<Entries extends readonly string[] = readonly string[]> extends HashbrownType { /** * @internal */ [internal]: EnumTypeInternals<Entries>; } /** * @public */ export declare const EnumType: HashbrownTypeCtor<EnumType>; /** * @public */ export declare function isEnumType(type: HashbrownType): type is EnumType; /** * @public */ export declare function enumeration<const Entries extends readonly string[]>(description: string, entries: [...Entries]): EnumType<Entries>; /** * -------------------------------------- * -------------------------------------- * Null Type * -------------------------------------- * -------------------------------------- */ interface NullTypeDefinition extends HashbrownTypeDefinition { type: 'null'; } /** * @internal */ export interface NullTypeInternals extends HashbrownTypeInternals<null> { definition: NullTypeDefinition; } /** * @public */ export interface NullType extends HashbrownType<null> { [internal]: NullTypeInternals; } /** * @public */ export declare const NullType: HashbrownTypeCtor<NullType>; /** * @public */ export declare function isNullType(type: HashbrownType): type is NullType; /** * @public */ export declare function nullish(): NullType; /** * -------------------------------------- * -------------------------------------- * Streaming Helpers * -------------------------------------- * -------------------------------------- */ export declare function needsDiscriminatorWrapperInAnyOf(schema: HashbrownType): boolean; export declare function isStreaming(schema: HashbrownType): boolean; /** * @public */ export declare function isHashbrownType(type: any): type is HashbrownType; /** * -------------------------------------- * -------------------------------------- * Type Utilities * -------------------------------------- * -------------------------------------- */ /** * @public */ export type Infer<T extends HashbrownType> = T[internal]['result']; /** * @internal */ export type SchemaForUnion<T> = AnyOfType<UnionToTuple<T> extends infer U ? U extends any[] ? { [K in keyof U]: Schema<U[K]>; } : never : never>; /** * @public */ export type Schema<T> = IsStringUnion<T> extends true ? [T] extends [string] ? UnionToTuple<T> extends infer U ? U extends string[] ? EnumType<U> : never : never : never : IsUnion<T> extends true ? SchemaForUnion<T> : T extends Array<infer U> ? ArrayType<Schema<U>> : T extends string ? string extends T ? StringType : LiteralType<T> : T extends number ? NumberType : T extends boolean ? BooleanType : T extends null ? NullType : T extends object ? ObjectType<{ [K in keyof T]: Schema<T[K]>; }> : never; export {}; //# sourceMappingURL=base.d.ts.map