retort-js
Version:
Intuitive, production-ready prompt chaining in Javascript
85 lines (84 loc) • 2.94 kB
TypeScript
import { RetortExtendableFunction } from "./extendable-function";
export default class RetortTool<TParamSchema extends RetortShorthandSchema, TResult> extends RetortExtendableFunction {
name: string;
description?: string;
params?: RetortSchema;
function?: (params: SchemaToParams<TParamSchema>) => TResult;
constructor({ name, description, params }: {
name: string;
description?: string;
params?: TParamSchema;
function?: (params: TParamSchema) => TResult;
});
}
type RetortShorthandSchema = Record<string, ShorthandJsonSchemaProperty>;
type RetortSchema = Record<string, JsonSchemaProperty>;
type ShortHandStringProperty = "string" | StringProperty;
type ShortHandNumberProperty = "number" | "integer" | NumberProperty;
type ShortHandBooleanProperty = "boolean" | BooleanProperty;
interface ShorthandArrayProperty {
description?: string;
type: 'array';
items?: ShorthandJsonSchemaProperty;
}
/**
* Represents an object property in a JSON schema.
*/
interface ShorthandObjectProperty {
description?: string;
type: 'object';
properties?: Record<string, ShorthandJsonSchemaProperty>;
required?: string[];
}
type ShorthandJsonSchemaProperty = ShortHandStringProperty | ShortHandNumberProperty | ShortHandBooleanProperty | ShorthandArrayProperty | ShorthandObjectProperty;
/**
* Represents a string property in a JSON schema.
*/
interface StringProperty {
description?: string;
type: 'string';
format?: 'date-time' | 'email' | 'uri';
minLength?: number;
maxLength?: number;
}
/**
* Represents a number property in a JSON schema (includes integers).
*/
interface NumberProperty {
description?: string;
type: 'number' | 'integer';
minimum?: number;
maximum?: number;
}
/**
* Represents a boolean property in a JSON schema.
*/
interface BooleanProperty {
description?: string;
type: 'boolean';
}
/**
* Represents an array property in a JSON schema.
*/
interface ArrayProperty {
description?: string;
type: 'array';
items?: JsonSchemaProperty;
}
/**
* Represents an object property in a JSON schema.
*/
interface ObjectProperty {
description?: string;
type: 'object';
properties?: Record<string, JsonSchemaProperty>;
required?: string[];
}
/**
* Union type for any JSON schema property.
*/
type JsonSchemaProperty = StringProperty | NumberProperty | BooleanProperty | ArrayProperty | ObjectProperty;
type SchemaToParams<T> = T extends StringProperty ? string : T extends "string" ? string : T extends NumberProperty ? number : T extends "number" ? number : T extends BooleanProperty ? boolean : T extends "boolean" ? boolean : T extends ArrayProperty ? SchemaToParams<T["items"]>[] : T extends "array" ? any[] : T extends ObjectProperty ? {
[K in keyof T["properties"]]: SchemaToParams<T["properties"][K]>;
} : T extends "object" ? Record<string, any> : never;
export {};