diff --git a/pdl-live-react/src/pdl_ast.d.ts b/pdl-live-react/src/pdl_ast.d.ts index 7a47607b7..7da0f4cc7 100644 --- a/pdl-live-react/src/pdl_ast.d.ts +++ b/pdl-live-react/src/pdl_ast.d.ts @@ -40,20 +40,6 @@ export type Program = * */ export type Description = string | null -export type PdlTypeType = - | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") - | EnumPdlType - | StrPdlType - | FloatPdlType - | IntPdlType - | ListPdlType - | PdlTypeType[] - | OptionalPdlType - | JsonSchemaTypePdlType - | ObjPdlType - | { - [k: string]: PdlTypeType - } export type Enum = unknown[] export type Minlength = number | null export type Maxlength = number | null @@ -68,12 +54,27 @@ export type Exclusiveminimum1 = number | null export type Maximum1 = number | null export type Exclusivemaximum1 = number | null export type List = PdlTypeType | ListPdlTypeConstraints -export type Minitems = number | null -export type Maxitems = number | null +export type PdlTypeType = + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null export type Type = string | string[] export type Obj = { [k: string]: PdlTypeType } | null +export type Minitems = number | null +export type Maxitems = number | null /** * Documentation associated to the block. * @@ -2838,7 +2839,7 @@ export type Function = { [k: string]: PdlTypeType } | null /** - * Body of the function + * Body of the function. * */ export type Return = @@ -2867,6 +2868,11 @@ export type Return = | ErrorBlock | EmptyBlock | null +/** + * Function signature computed from the function definition. + * + */ +export type Signature = string | null export type PdlBlock = | boolean | number @@ -2903,7 +2909,21 @@ export interface FunctionBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs def?: Def24 contribute?: Contribute20 @@ -2921,6 +2941,7 @@ export interface FunctionBlock { kind?: Kind20 function: Function return: Return + signature?: Signature } /** * Enumerated type. @@ -2979,14 +3000,6 @@ export interface IntPdlTypeConstraints { export interface ListPdlType { list: List } -/** - * Constraints on list type. - */ -export interface ListPdlTypeConstraints { - minItems?: Minitems - maxItems?: Maxitems - [k: string]: unknown -} /** * Optional type. */ @@ -3001,11 +3014,19 @@ export interface JsonSchemaTypePdlType { [k: string]: unknown } /** - * Optional type. + * Object type. */ export interface ObjPdlType { obj: Obj } +/** + * Constraints on list type. + */ +export interface ListPdlTypeConstraints { + minItems?: Minitems + maxItems?: Maxitems + [k: string]: unknown +} /** * Set of definitions executed before the execution of the block. * @@ -3047,7 +3068,21 @@ export interface CallBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs1 def?: Def23 contribute?: Contribute19 @@ -3115,7 +3150,21 @@ export interface LitellmModelBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs2 def?: Def22 contribute?: Contribute18 @@ -3184,7 +3233,21 @@ export interface GraniteioModelBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs3 def?: Def21 contribute?: Contribute17 @@ -3264,7 +3327,21 @@ export interface CodeBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs4 def?: Def20 contribute?: Contribute16 @@ -3332,7 +3409,21 @@ export interface ArgsBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs5 def?: Def19 contribute?: Contribute15 @@ -3394,7 +3485,21 @@ export interface GetBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs6 def?: Def18 contribute?: Contribute14 @@ -3474,7 +3579,21 @@ export interface DataBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs7 def?: Def17 contribute?: Contribute13 @@ -3544,7 +3663,21 @@ export interface IfBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs8 def?: Def16 contribute?: Contribute12 @@ -3624,7 +3757,21 @@ export interface MatchBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs9 def?: Def11 contribute?: Contribute11 @@ -3694,7 +3841,21 @@ export interface RepeatBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs10 def?: Def10 contribute?: Contribute10 @@ -3760,7 +3921,21 @@ export interface TextBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs11 def?: Def9 contribute?: Contribute9 @@ -3820,7 +3995,21 @@ export interface LastOfBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs12 def?: Def8 contribute?: Contribute8 @@ -3880,7 +4069,21 @@ export interface ArrayBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs13 def?: Def7 contribute?: Contribute7 @@ -3940,7 +4143,21 @@ export interface ObjectBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs14 def?: Def6 contribute?: Contribute6 @@ -4000,7 +4217,21 @@ export interface MessageBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs15 def?: Def5 contribute?: Contribute5 @@ -4073,7 +4304,21 @@ export interface ReadBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs16 def?: Def4 contribute?: Contribute4 @@ -4134,7 +4379,21 @@ export interface IncludeBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs17 def?: Def3 contribute?: Contribute3 @@ -4195,7 +4454,21 @@ export interface ImportBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs18 def?: Def2 contribute?: Contribute2 @@ -4255,7 +4528,21 @@ export interface ErrorBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs19 def?: Def1 contribute?: Contribute1 @@ -4315,7 +4602,21 @@ export interface EmptyBlock { * Type specification of the result of the block. * */ - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null defs?: Defs20 def?: Def contribute?: Contribute @@ -4388,7 +4689,21 @@ export interface Table { } export interface PdlParser { description?: Description21 - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null pdl: Pdl } /** @@ -4396,7 +4711,21 @@ export interface PdlParser { */ export interface RegexParser { description?: Description22 - spec?: PdlTypeType | null + spec?: + | ("null" | "bool" | "str" | "float" | "int" | "list" | "obj") + | EnumPdlType + | StrPdlType + | FloatPdlType + | IntPdlType + | ListPdlType + | PdlTypeType[] + | OptionalPdlType + | JsonSchemaTypePdlType + | ObjPdlType + | { + [k: string]: PdlTypeType + } + | null regex: Regex mode?: Mode } diff --git a/src/pdl/pdl-schema.json b/src/pdl/pdl-schema.json index 300a0a6e2..89ed2cdf8 100644 --- a/src/pdl/pdl-schema.json +++ b/src/pdl/pdl-schema.json @@ -45,14 +45,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -451,14 +444,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -984,14 +970,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -1480,14 +1459,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -1995,14 +1967,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -2399,14 +2364,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -2797,14 +2755,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -3361,14 +3312,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -3817,8 +3761,23 @@ "type": "null" } ], - "description": "Body of the function\n ", + "description": "Body of the function.\n ", "title": "Return" + }, + "signature": { + "anyOf": [ + { + "contentMediaType": "application/json", + "contentSchema": {}, + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Function signature computed from the function definition.\n ", + "title": "Signature" } }, "required": [ @@ -3846,14 +3805,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -4236,14 +4188,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -4820,14 +4765,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -5406,14 +5344,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -5880,14 +5811,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -6536,14 +6460,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -7066,14 +6983,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -8055,14 +7965,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -8647,14 +8550,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -9132,7 +9028,7 @@ }, "ObjPdlType": { "additionalProperties": false, - "description": "Optional type.", + "description": "Object type.", "properties": { "obj": { "anyOf": [ @@ -9173,14 +9069,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -9977,14 +9866,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null }, "pdl": { @@ -10182,6 +10064,9 @@ "$ref": "#/$defs/PdlTypeType" }, "type": "object" + }, + { + "type": "null" } ] }, @@ -10318,14 +10203,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -10736,14 +10614,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null }, "regex": { @@ -10787,14 +10658,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, @@ -11519,14 +11383,7 @@ "title": "Description" }, "spec": { - "anyOf": [ - { - "$ref": "#/$defs/PdlTypeType" - }, - { - "type": "null" - } - ], + "$ref": "#/$defs/PdlTypeType", "default": null, "description": "Type specification of the result of the block.\n " }, diff --git a/src/pdl/pdl_ast.py b/src/pdl/pdl_ast.py index 9abcb24df..9b76d37a1 100644 --- a/src/pdl/pdl_ast.py +++ b/src/pdl/pdl_ast.py @@ -19,6 +19,7 @@ BeforeValidator, ConfigDict, Field, + Json, RootModel, TypeAdapter, ) @@ -236,7 +237,7 @@ class JsonSchemaTypePdlType(PdlType): class ObjPdlType(PdlType): - """Optional type.""" + """Object type.""" obj: Optional[dict[str, "PdlTypeType"]] @@ -244,7 +245,8 @@ class ObjPdlType(PdlType): PdlTypeType = TypeAliasType( "PdlTypeType", Annotated[ - "Union[BasePdlType," # pyright: ignore + "Union[None," # pyright: ignore + " BasePdlType," " EnumPdlType," " StrPdlType," " FloatPdlType," @@ -269,7 +271,7 @@ class Parser(BaseModel): description: Optional[str] = None """Documentation associated to the parser. """ - spec: Optional[PdlTypeType] = None + spec: PdlTypeType = None """Expected type of the parsed value. """ @@ -348,7 +350,7 @@ class Block(BaseModel): description: Optional[str] = None """Documentation associated to the block. """ - spec: Optional[PdlTypeType] = None + spec: PdlTypeType = None """Type specification of the result of the block. """ defs: dict[str, "BlockType"] = {} @@ -416,8 +418,12 @@ class FunctionBlock(LeafBlock): """Functions parameters with their types. """ returns: "BlockType" = Field(..., alias="return") - """Body of the function + """Body of the function. + """ + signature: Optional[Json] = None + """Function signature computed from the function definition. """ + # Field for internal use pdl__scope: SkipJsonSchema[Optional[ScopeType]] = Field(default=None, repr=False) diff --git a/src/pdl/pdl_dumper.py b/src/pdl/pdl_dumper.py index 912ae06e9..a8162fb0d 100644 --- a/src/pdl/pdl_dumper.py +++ b/src/pdl/pdl_dumper.py @@ -317,8 +317,10 @@ def expr_to_dict(expr: ExpressionType, json_compatible: bool): def type_to_dict(t: PdlTypeType): - d: str | list | dict + d: None | str | list | dict match t: + case None: + d = None case "null" | "bool" | "str" | "float" | "int" | "list" | "obj": d = t case EnumPdlType(): diff --git a/src/pdl/pdl_interpreter.py b/src/pdl/pdl_interpreter.py index 650229ad3..d15a703f6 100644 --- a/src/pdl/pdl_interpreter.py +++ b/src/pdl/pdl_interpreter.py @@ -100,6 +100,7 @@ from .pdl_parser import PDLParseError, parse_file, parse_str # noqa: E402 from .pdl_python_repl import PythonREPL # noqa: E402 from .pdl_scheduler import yield_background, yield_result # noqa: E402 +from .pdl_schema_utils import get_json_schema # noqa: E402 from .pdl_schema_validator import type_check_args, type_check_spec # noqa: E402 from .pdl_utils import ( # noqa: E402 GeneratorWrapper, @@ -894,6 +895,16 @@ def process_block_body( if block.def_ is not None: scope = scope | {block.def_: closure} closure.pdl__scope = scope + signature: dict[str, Any] = {"type": "function"} + if block.def_ is not None: + signature["name"] = block.def_ + if block.description is not None: + signature["description"] = block.description + if block.function is not None: + signature["parameters"] = get_json_schema(block.function, False) or {} + else: + signature["parameters"] = {} + closure.signature = signature result = PdlConst(closure) background = PdlList([]) trace = closure.model_copy(update={}) @@ -976,6 +987,8 @@ def process_defs( state = state.with_iter(idx) state = state.with_yield_result(False) state = state.with_yield_background(False) + if isinstance(block, FunctionBlock) and block.def_ is None: + block = block.model_copy(update={"def_": x}) result, _, _, block_trace = process_block(state, scope, block, newloc) scope = scope | PdlDict({x: result}) defs_trace[x] = block_trace diff --git a/src/pdl/pdl_llms.py b/src/pdl/pdl_llms.py index 5c0db1a0c..f9d569654 100644 --- a/src/pdl/pdl_llms.py +++ b/src/pdl/pdl_llms.py @@ -183,7 +183,7 @@ def generate_text_stream( def set_structured_decoding_parameters( - spec: Optional[PdlTypeType], + spec: PdlTypeType, parameters: Optional[dict[str, Any]], ) -> dict[str, Any]: if parameters is None: diff --git a/src/pdl/pdl_schema_utils.py b/src/pdl/pdl_schema_utils.py index 78071c5ef..f5adb6f22 100644 --- a/src/pdl/pdl_schema_utils.py +++ b/src/pdl/pdl_schema_utils.py @@ -44,7 +44,7 @@ def convert_to_json_type(a_type): def pdltype_to_jsonschema( - pdl_type: Optional[PdlTypeType], additional_properties: bool + pdl_type: PdlTypeType, additional_properties: bool ) -> dict[str, Any]: schema: dict[str, Any] match pdl_type: diff --git a/src/pdl/pdl_schema_validator.py b/src/pdl/pdl_schema_validator.py index 160a1e8c4..c84ca5374 100644 --- a/src/pdl/pdl_schema_validator.py +++ b/src/pdl/pdl_schema_validator.py @@ -8,7 +8,9 @@ def type_check_args( - args: Optional[dict[str, Any]], params: Optional[dict[str, Any]], loc + args: Optional[dict[str, Any]], + params: Optional[dict[str, PdlTypeType]], + loc, ) -> list[str]: if (args == {} or args is None) and (params is None or params == {}): return [] @@ -35,7 +37,7 @@ def type_check_args( return type_check(args_copy, schema, loc) -def type_check_spec(result: Any, spec: Optional[PdlTypeType], loc) -> list[str]: +def type_check_spec(result: Any, spec: PdlTypeType, loc) -> list[str]: schema = pdltype_to_jsonschema(spec, False) if schema is None: return ["Error obtaining a valid schema from spec"] diff --git a/tests/test_function.py b/tests/test_function.py index 1fb2090b6..d78094421 100644 --- a/tests/test_function.py +++ b/tests/test_function.py @@ -20,6 +20,17 @@ def test_function_call(): assert text == "Hello world!" +def test_hello_signature(): + result = exec_dict(hello_def, output="all") + closure = result["scope"]["hello"] + assert closure.signature == { + "name": hello_def["def"], + "description": hello_def["description"], + "type": "function", + "parameters": {}, + } + + hello_params = { "description": "Call hello", "text": [ @@ -39,6 +50,22 @@ def test_function_params(): assert text == "Hello World!" +def test_hello_params_signature(): + result = exec_dict(hello_params, output="all") + closure = result["scope"]["hello"] + assert closure.signature == { + "name": hello_params["text"][0]["def"], + "description": hello_params["text"][0]["description"], + "type": "function", + "parameters": { + "type": "object", + "properties": {"name": {"type": "string"}}, + "required": ["name"], + "additionalProperties": False, + }, + } + + hello_stutter = { "description": "Repeat the context", "text": [