UNPKG

@hashbrownai/core

Version:

Runtime helpers for Hashbrown AI

284 lines 12.5 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'; 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; }; 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; } export declare const HashbrownTypeCtor: <T extends TypeBox, D extends TypeInternals["definition"] = T[internal]["definition"]>(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) => HashbrownTypeCtor<T, D>; interface HashbrownTypeDefinition { type: 'string' | 'literal' | 'number' | 'boolean' | 'integer' | 'object' | 'array' | 'enum' | 'any-of' | 'null'; description: string; streaming: boolean; } export interface HashbrownType<out Result = unknown> { [internal]: HashbrownTypeInternals<Result>; toJsonSchema: () => any; parseJsonSchema: (object: unknown, path?: string[]) => any; validateJsonSchema: (object: unknown) => void; toTypeScript: (pathSeen?: Set<HashbrownType>) => string; } interface HashbrownTypeInternals<out Result = unknown> extends HashbrownType<Result> { definition: HashbrownTypeDefinition; result: Result; } export declare const HashbrownType: HashbrownTypeCtor<HashbrownType>; /** * -------------------------------------- * -------------------------------------- * String Type * -------------------------------------- * -------------------------------------- */ interface StringTypeDefinition extends HashbrownTypeDefinition { type: 'string'; } interface StringTypeInternals extends HashbrownTypeInternals<string> { definition: StringTypeDefinition; } export interface StringType extends HashbrownType<string> { [internal]: StringTypeInternals; } export declare const StringType: HashbrownTypeCtor<StringType>; export declare function isStringType(type: HashbrownType): type is StringType; 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; } interface LiteralTypeInternals<T extends string | number | boolean = string | number | boolean> extends HashbrownTypeInternals<T> { definition: LiteralTypeDefinition<T>; } export interface LiteralType<T extends string | number | boolean = string | number | boolean> extends HashbrownType<T> { [internal]: LiteralTypeInternals<T>; } export declare const LiteralType: HashbrownTypeCtor<LiteralType>; export declare function isLiteralType(type: HashbrownType): type is LiteralType; export declare function literal<T extends string>(value: T): LiteralType<T>; /** * -------------------------------------- * -------------------------------------- * Number Type * -------------------------------------- * -------------------------------------- */ interface NumberTypeDefinition extends HashbrownTypeDefinition { type: 'number'; } interface NumberTypeInternals extends HashbrownTypeInternals<number> { definition: NumberTypeDefinition; } export interface NumberType extends HashbrownType<number> { [internal]: NumberTypeInternals; } export declare const NumberType: HashbrownTypeCtor<NumberType>; export declare function isNumberType(type: HashbrownType): type is NumberType; export declare function number(description: string): NumberType; /** * -------------------------------------- * -------------------------------------- * Boolean Type * -------------------------------------- * -------------------------------------- */ interface BooleanTypeDefinition extends HashbrownTypeDefinition { type: 'boolean'; } interface BooleanTypeInternals extends HashbrownTypeInternals<boolean> { definition: BooleanTypeDefinition; } export interface BooleanType extends HashbrownType<boolean> { [internal]: BooleanTypeInternals; } export declare const BooleanType: HashbrownTypeCtor<BooleanType>; export declare function isBooleanType(type: HashbrownType): type is BooleanType; export declare function boolean(description: string): BooleanType; /** * -------------------------------------- * -------------------------------------- * Integer Type * -------------------------------------- * -------------------------------------- */ interface IntegerTypeDefinition extends HashbrownTypeDefinition { type: 'integer'; } interface IntegerTypeInternals extends HashbrownTypeInternals<number> { definition: IntegerTypeDefinition; } export interface IntegerType extends HashbrownType<number> { [internal]: IntegerTypeInternals; } export declare const IntegerType: HashbrownTypeCtor<IntegerType>; export declare function isIntegerType(type: HashbrownType): type is IntegerType; 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; } interface ObjectTypeInternals<Result extends Readonly<Record<string, any>>> extends HashbrownTypeInternals<ObjectTypeResult<Result>> { definition: ObjectTypeDefinition<Result>; } export interface ObjectType<Result extends Readonly<Record<string, any>> = Readonly<Record<string, any>>> extends HashbrownType { [internal]: ObjectTypeInternals<Result>; } export declare const ObjectType: HashbrownTypeCtor<ObjectType>; export declare function isObjectType(type: HashbrownType): type is ObjectType; 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; } interface ArrayTypeInternals<Item extends HashbrownType = HashbrownType> extends HashbrownTypeInternals<Item[internal]['result'][]> { definition: ArrayTypeDefinition<Item>; } export interface ArrayType<Item extends HashbrownType = HashbrownType> extends HashbrownType { [internal]: ArrayTypeInternals<Item>; } export declare const ArrayType: HashbrownTypeCtor<ArrayType>; export declare function isArrayType(type: HashbrownType): type is ArrayType; 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; } interface AnyOfTypeInternals<Options extends readonly HashbrownType[]> extends HashbrownTypeInternals<Options[number][internal]['result']> { definition: AnyOfTypeDefinition<Options>; } export interface AnyOfType<Options extends readonly HashbrownType[] = readonly HashbrownType[]> extends HashbrownType<Options[number][internal]['result']> { [internal]: AnyOfTypeInternals<Options>; } export declare const AnyOfType: HashbrownTypeCtor<AnyOfType>; export declare function isAnyOfType(type: HashbrownType): type is AnyOfType; export declare function anyOf<const Options extends readonly HashbrownType[]>(options: Options): AnyOfType<Options>; /** * -------------------------------------- * -------------------------------------- * Enum Type * -------------------------------------- * -------------------------------------- */ /** * @internal */ interface EnumTypeDefinition<out Entries extends readonly any[]> extends HashbrownTypeDefinition { type: 'enum'; entries: Entries; } /** * @internal */ interface EnumTypeInternals<Result extends readonly any[]> extends HashbrownTypeInternals<Result[number]> { definition: EnumTypeDefinition<Result>; } export interface EnumType<Entries extends readonly string[] = readonly string[]> extends HashbrownType { /** * @internal */ [internal]: EnumTypeInternals<Entries>; } export declare const EnumType: HashbrownTypeCtor<EnumType>; export declare function isEnumType(type: HashbrownType): type is EnumType; export declare function enumeration<const Entries extends readonly string[]>(description: string, entries: [...Entries]): EnumType<Entries>; /** * -------------------------------------- * -------------------------------------- * Null Type * -------------------------------------- * -------------------------------------- */ interface NullTypeDefinition extends HashbrownTypeDefinition { type: 'null'; } interface NullTypeInternals extends HashbrownTypeInternals<null> { definition: NullTypeDefinition; } export interface NullType extends HashbrownType<null> { [internal]: NullTypeInternals; } export declare const NullType: HashbrownTypeCtor<NullType>; export declare function isNullType(type: HashbrownType): type is NullType; export declare function nullish(): NullType; /** * -------------------------------------- * -------------------------------------- * Streaming Helpers * -------------------------------------- * -------------------------------------- */ export declare function needsDiscriminatorWrapperInAnyOf(schema: HashbrownType): boolean; export declare function isStreaming(schema: HashbrownType): boolean; /** * -------------------------------------- * -------------------------------------- * Type Utilities * -------------------------------------- * -------------------------------------- */ export type Infer<T extends HashbrownType> = T[internal]['result']; type SchemaForUnion<T> = AnyOfType<UnionToTuple<T> extends infer U ? U extends any[] ? { [K in keyof U]: Schema<U[K]>; } : never : never>; 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