@firefliesai/schema-forge
Version:
Transform TypeScript classes into JSON Schema definitions with automatic support for OpenAI, Anthropic, and Google Gemini function calling (tool) formats
180 lines (179 loc) • 5.76 kB
TypeScript
/**
* Types for schema-forge
*/
import { SchemaType as SchemaVertexType } from './vertex-api.type';
/** @google/generative-ai */
export declare enum SchemaType {
OBJECT = "object"
}
/** @google/genai */
export declare enum Type {
OBJECT = "OBJECT"
}
export declare const JSON_SCHEMA_METADATA_KEY: unique symbol;
export declare const REQUIRED_PROPS_METADATA_KEY: unique symbol;
export type Constructor<T> = new (...args: any[]) => T;
export interface SchemaItemType {
type?: 'string' | 'number' | 'boolean' | Constructor<any>;
enum?: (string | number)[];
}
export interface BaseSchemaProperty {
description?: string;
enum?: string[] | number[] | object;
items?: SchemaItemType | JSONSchemaDefinition;
properties?: {
[key: string]: JSONSchemaProperty;
};
required?: string[];
}
export interface PropertyOptions extends BaseSchemaProperty {
isOptional?: boolean;
type?: 'string' | 'number' | 'boolean' | 'array' | 'object';
}
export interface JSONSchemaProperty extends BaseSchemaProperty {
type: string;
properties?: {
[key: string]: JSONSchemaProperty;
};
required?: string[];
}
export interface JSONSchemaDefinition extends Record<string, unknown> {
type: 'object';
properties: {
[key: string]: JSONSchemaProperty;
};
required?: string[];
}
export interface OpenAIToolFunction {
type: 'function';
function: {
name: string;
description?: string;
parameters: any;
strict?: boolean;
};
}
/** for chat completion api */
export interface OpenAIResponseFormatJsonSchema {
type: 'json_schema';
json_schema: {
name: string;
description?: string;
schema: any;
strict: boolean;
};
}
/** for new response API, equivalent to OpenAIResponseFormatJsonSchema */
export interface OpenAIResponseApiTextSchema {
type: 'json_schema';
name?: string;
description?: string;
schema: any;
strict?: boolean | null;
}
/** strict becomes required in new response api */
export interface OpenAIResponseApiToolFunction {
type: 'function';
name: string;
description?: string;
parameters: any;
strict: boolean;
}
export interface GeminiToolFunction {
name: string;
description?: string;
parameters: {
type: Type.OBJECT;
description?: string;
properties: Record<string, any>;
required?: string[];
};
}
export interface GeminiOldToolFunction {
name: string;
description?: string;
parameters: {
type: SchemaType.OBJECT;
description?: string;
properties: Record<string, any>;
required?: string[];
};
}
export interface GeminiVertexToolFunction {
name: string;
description?: string;
parameters: {
type: SchemaVertexType.OBJECT;
description?: string;
properties: Record<string, any>;
required?: string[];
};
}
/** Google Gemini (@google/genai):
* json schema like, except object becoming capital 'OBJECT */
export interface GeminiResponseSchema {
description?: string;
type: Type.OBJECT;
properties: Record<string, any>;
required?: string[];
}
export interface GeminiOldResponseSchema {
description?: string;
type: SchemaType.OBJECT;
properties: Record<string, any>;
required?: string[];
}
export interface GeminiVertexResponseSchema {
description?: string;
type: SchemaVertexType.OBJECT;
properties: Record<string, any>;
required?: string[];
}
export interface AnthropicToolFunction {
name: string;
description?: string;
input_schema: {
type: 'object';
properties: Record<string, any>;
required?: string[];
};
}
/**
* Structured output format options for different LLM providers
* @deprecated Use forStructuredOutput instead
*/
export type StructuredOutputFormat = 'none' | 'openai' | 'gemini';
export interface JsonSchemaOptions<T> {
/**
* Property overrides to apply to the schema
*/
propertyOverrides?: Partial<{
[P in PropertyPath<T>]: Partial<PropertyOptions>;
}>;
/**
* Whether to prepare the schema for OpenAI specific structured output
*/
forStructuredOutput?: boolean;
/**
* Specify which structured output format to use
* - 'none': No structured output formatting (default)
* - 'openai': Apply OpenAI-specific structured output formatting
* - 'gemini': Apply Gemini-specific structured output formatting
* @deprecated Use forStructuredOutput instead
*/
structuredOutputFormat?: StructuredOutputFormat;
}
export type OpenAIToolOptions<T> = JsonSchemaOptions<T>;
export type OpenAIResponseFormatOptions<T> = JsonSchemaOptions<T>;
export type GeminiToolOptions<T> = Omit<JsonSchemaOptions<T>, 'forStructuredOutput'> & {
propertyOverrides?: JsonSchemaOptions<T>['propertyOverrides'];
};
export type AnthropicToolOptions<T> = JsonSchemaOptions<T>;
export type GeminiResponseSchemaOptions<T> = Omit<JsonSchemaOptions<T>, 'forStructuredOutput'> & {
propertyOverrides?: JsonSchemaOptions<T>['propertyOverrides'];
};
export type Primitive = string | number | boolean;
export type ArrayElement<T> = T extends (infer U)[] ? U : never;
export type PathImpl<T, Key extends keyof T> = Key extends string ? T[Key] extends Primitive ? `${Key}` : T[Key] extends Array<any> ? `${Key}` | `${Key}.${PathImpl<ArrayElement<T[Key]>, keyof ArrayElement<T[Key]>>}` : T[Key] extends object ? `${Key}` | `${Key}.${PathImpl<T[Key], keyof T[Key]>}` : never : never;
export type PropertyPath<T> = PathImpl<T, keyof T>;
export type InferSchema<T> = T extends new (...args: any[]) => any ? ReturnType<typeof import('./utils').inferType<T>> : never;