diff --git a/pdl-live-react/src/helpers.ts b/pdl-live-react/src/helpers.ts index b669017bc..d87231147 100644 --- a/pdl-live-react/src/helpers.ts +++ b/pdl-live-react/src/helpers.ts @@ -4,10 +4,12 @@ import type { GraniteioModelBlock, PdlBlock, TextBlock, + ArgsBlock, + CodeBlock, } from "./pdl_ast" /** Re-export for convenience */ -export { type PdlBlock } from "./pdl_ast" +export type { PdlBlock } from "./pdl_ast" type MakeNonNullable = { [K in keyof T]-?: NonNullable @@ -282,3 +284,7 @@ export function extractStructuredModelResponse({ return { resultForDisplay, lang, meta } } + +export function isArgs(block: ArgsBlock | CodeBlock): block is ArgsBlock { + return Array.isArray((block as ArgsBlock).args) +} diff --git a/pdl-live-react/src/pdl_ast.d.ts b/pdl-live-react/src/pdl_ast.d.ts index 373bb8089..979ccec9b 100644 --- a/pdl-live-react/src/pdl_ast.d.ts +++ b/pdl-live-react/src/pdl_ast.d.ts @@ -18,6 +18,7 @@ export type Program = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -134,6 +135,11 @@ export type Description18 = string | null * */ export type Description19 = string | null +/** + * Documentation associated to the block. + * + */ +export type Description20 = string | null /** * Name of the variable used to store the result of the execution of the block. * @@ -161,8 +167,8 @@ export type Parser = | PdlParser | RegexParser | null -export type Description20 = string | null -export type Spec20 = { +export type Description21 = string | null +export type Spec21 = { [k: string]: unknown } | null export type Pdl = @@ -174,6 +180,7 @@ export type Pdl = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -190,8 +197,8 @@ export type Pdl = | ErrorBlock | EmptyBlock | null -export type Description21 = string | null -export type Spec21 = { +export type Description22 = string | null +export type Spec22 = { [k: string]: unknown } | null export type Regex = string @@ -209,6 +216,7 @@ export type Fallback = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -287,6 +295,7 @@ export type Fallback1 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -343,6 +352,7 @@ export type Program1 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -395,6 +405,7 @@ export type Fallback2 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -447,6 +458,7 @@ export type PdlTrace = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -499,6 +511,7 @@ export type Fallback3 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -551,6 +564,7 @@ export type PdlTrace1 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -603,6 +617,7 @@ export type Fallback4 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -692,6 +707,7 @@ export type Fallback5 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -742,6 +758,7 @@ export type Content = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -794,6 +811,7 @@ export type Fallback6 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -843,6 +861,7 @@ export type Object = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -869,6 +888,7 @@ export type Object = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -922,6 +942,7 @@ export type Fallback7 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -972,6 +993,7 @@ export type Array = ( | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -1025,6 +1047,7 @@ export type Fallback8 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -1075,6 +1098,7 @@ export type Lastof = ( | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -1128,6 +1152,7 @@ export type Fallback9 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -1179,6 +1204,7 @@ export type Text = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -1203,6 +1229,7 @@ export type Text = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -1257,6 +1284,7 @@ export type Fallback10 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -1320,6 +1348,7 @@ export type Repeat = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -1386,6 +1415,7 @@ export type PdlTrace2 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -1440,6 +1470,7 @@ export type Fallback11 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -1522,6 +1553,7 @@ export type Then = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -1582,6 +1614,7 @@ export type Fallback12 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -1638,6 +1671,7 @@ export type Then1 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -1667,6 +1701,7 @@ export type Else = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -1720,6 +1755,7 @@ export type Fallback13 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -1798,6 +1834,7 @@ export type Fallback14 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -1876,6 +1913,7 @@ export type Fallback15 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -1914,11 +1952,92 @@ export type Context15 = export type PdlId15 = string | null export type PdlIsLeaf15 = true export type Kind15 = "code" +export type Lang = "command" +/** + * The argument vector to spawn. + * + */ +export type Args = (string | LocalizedExpression)[] +/** + * Name of the variable used to store the result of the execution of the block. + * + */ +export type Def20 = string | null +/** + * Indicate if the block contributes to the result and background context. + * + */ +export type Contribute16 = ( + | ContributeTarget + | { + [k: string]: ContributeValue + } +)[] +/** + * Parser to use to construct a value out of a string result. + */ +export type Parser16 = + | ("json" | "jsonl" | "yaml") + | PdlParser + | RegexParser + | null +/** + * Block to execute in case of error. + * + */ +export type Fallback16 = + | boolean + | number + | string + | FunctionBlock + | CallBlock + | LitellmModelBlock + | GraniteioModelBlock + | CodeBlock + | ArgsBlock + | GetBlock + | DataBlock + | IfBlock + | MatchBlock + | RepeatBlock + | TextBlock + | LastOfBlock + | ArrayBlock + | ObjectBlock + | MessageBlock + | ReadBlock + | IncludeBlock + | ImportBlock + | ErrorBlock + | EmptyBlock + | null +/** + * Role associated to the block and sub-blocks. + * Typical roles are `system`, `user`, and `assistant`, + * but there may be other roles such as `available_tools`. + */ +export type Role16 = string | null +/** + * Current context + * + */ +export type Context16 = + | { + [k: string]: unknown + }[] + | null +/** + * Unique identifier for this block + * + */ +export type PdlId16 = string | null +export type PdlIsLeaf16 = true +export type Kind16 = "code" /** * Programming language of the code. * */ -export type Lang = "python" | "command" | "jinja" | "pdl" +export type Lang1 = "python" | "command" | "jinja" | "pdl" /** * Code to execute. * @@ -1932,6 +2051,7 @@ export type Code = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -1952,12 +2072,12 @@ export type Code = * Name of the variable used to store the result of the execution of the block. * */ -export type Def20 = string | null +export type Def21 = string | null /** * Indicate if the block contributes to the result and background context. * */ -export type Contribute16 = ( +export type Contribute17 = ( | ContributeTarget | { [k: string]: ContributeValue @@ -1966,7 +2086,7 @@ export type Contribute16 = ( /** * Parser to use to construct a value out of a string result. */ -export type Parser16 = +export type Parser17 = | ("json" | "jsonl" | "yaml") | PdlParser | RegexParser @@ -1975,7 +2095,7 @@ export type Parser16 = * Block to execute in case of error. * */ -export type Fallback16 = +export type Fallback17 = | boolean | number | string @@ -1984,6 +2104,7 @@ export type Fallback16 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -2005,12 +2126,12 @@ export type Fallback16 = * Typical roles are `system`, `user`, and `assistant`, * but there may be other roles such as `available_tools`. */ -export type Role16 = string | null +export type Role17 = string | null /** * Current context * */ -export type Context16 = +export type Context17 = | { [k: string]: unknown }[] @@ -2019,9 +2140,9 @@ export type Context16 = * Unique identifier for this block * */ -export type PdlId16 = string | null -export type PdlIsLeaf16 = true -export type Kind16 = "model" +export type PdlId17 = string | null +export type PdlIsLeaf17 = true +export type Kind17 = "model" /** * Messages to send to the model. * @@ -2035,6 +2156,7 @@ export type Input = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -2067,6 +2189,7 @@ export type PdlTrace3 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -2118,12 +2241,12 @@ export type Parameters = * Name of the variable used to store the result of the execution of the block. * */ -export type Def21 = string | null +export type Def22 = string | null /** * Indicate if the block contributes to the result and background context. * */ -export type Contribute17 = ( +export type Contribute18 = ( | ContributeTarget | { [k: string]: ContributeValue @@ -2132,7 +2255,7 @@ export type Contribute17 = ( /** * Parser to use to construct a value out of a string result. */ -export type Parser17 = +export type Parser18 = | ("json" | "jsonl" | "yaml") | PdlParser | RegexParser @@ -2141,7 +2264,7 @@ export type Parser17 = * Block to execute in case of error. * */ -export type Fallback17 = +export type Fallback18 = | boolean | number | string @@ -2150,6 +2273,7 @@ export type Fallback17 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -2171,12 +2295,12 @@ export type Fallback17 = * Typical roles are `system`, `user`, and `assistant`, * but there may be other roles such as `available_tools`. */ -export type Role17 = string | null +export type Role18 = string | null /** * Current context * */ -export type Context17 = +export type Context18 = | { [k: string]: unknown }[] @@ -2185,9 +2309,9 @@ export type Context17 = * Unique identifier for this block * */ -export type PdlId17 = string | null -export type PdlIsLeaf17 = true -export type Kind17 = "model" +export type PdlId18 = string | null +export type PdlIsLeaf18 = true +export type Kind18 = "model" /** * Name of the model following the LiteLLM convention. * @@ -2206,6 +2330,7 @@ export type Input1 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -2236,6 +2361,7 @@ export type PdlTrace4 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -2320,12 +2446,12 @@ export type MaxRetries = number | string | null * Name of the variable used to store the result of the execution of the block. * */ -export type Def22 = string | null +export type Def23 = string | null /** * Indicate if the block contributes to the result and background context. * */ -export type Contribute18 = ( +export type Contribute19 = ( | ContributeTarget | { [k: string]: ContributeValue @@ -2334,7 +2460,7 @@ export type Contribute18 = ( /** * Parser to use to construct a value out of a string result. */ -export type Parser18 = +export type Parser19 = | ("json" | "jsonl" | "yaml") | PdlParser | RegexParser @@ -2343,7 +2469,7 @@ export type Parser18 = * Block to execute in case of error. * */ -export type Fallback18 = +export type Fallback19 = | boolean | number | string @@ -2352,6 +2478,7 @@ export type Fallback18 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -2373,12 +2500,12 @@ export type Fallback18 = * Typical roles are `system`, `user`, and `assistant`, * but there may be other roles such as `available_tools`. */ -export type Role18 = string | null +export type Role19 = string | null /** * Current context * */ -export type Context18 = +export type Context19 = | { [k: string]: unknown }[] @@ -2387,9 +2514,9 @@ export type Context18 = * Unique identifier for this block * */ -export type PdlId18 = string | null -export type PdlIsLeaf18 = true -export type Kind18 = "call" +export type PdlId19 = string | null +export type PdlIsLeaf19 = true +export type Kind19 = "call" export type PdlTrace5 = | boolean | number @@ -2399,6 +2526,7 @@ export type PdlTrace5 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -2419,12 +2547,12 @@ export type PdlTrace5 = * Name of the variable used to store the result of the execution of the block. * */ -export type Def23 = string | null +export type Def24 = string | null /** * Indicate if the block contributes to the result and background context. * */ -export type Contribute19 = ( +export type Contribute20 = ( | ContributeTarget | { [k: string]: ContributeValue @@ -2433,7 +2561,7 @@ export type Contribute19 = ( /** * Parser to use to construct a value out of a string result. */ -export type Parser19 = +export type Parser20 = | ("json" | "jsonl" | "yaml") | PdlParser | RegexParser @@ -2442,7 +2570,7 @@ export type Parser19 = * Block to execute in case of error. * */ -export type Fallback19 = +export type Fallback20 = | boolean | number | string @@ -2451,6 +2579,7 @@ export type Fallback19 = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -2472,12 +2601,12 @@ export type Fallback19 = * Typical roles are `system`, `user`, and `assistant`, * but there may be other roles such as `available_tools`. */ -export type Role19 = string | null +export type Role20 = string | null /** * Current context * */ -export type Context19 = +export type Context20 = | { [k: string]: unknown }[] @@ -2486,9 +2615,9 @@ export type Context19 = * Unique identifier for this block * */ -export type PdlId19 = string | null -export type PdlIsLeaf19 = true -export type Kind19 = "function" +export type PdlId20 = string | null +export type PdlIsLeaf20 = true +export type Kind20 = "function" /** * Functions parameters with their types. * @@ -2509,6 +2638,7 @@ export type Return = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -2534,6 +2664,7 @@ export type PdlBlock = | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -2558,18 +2689,18 @@ export interface FunctionBlock { description?: Description spec?: Spec defs?: Defs - def?: Def23 - contribute?: Contribute19 - parser?: Parser19 - fallback?: Fallback19 - role?: Role19 - context?: Context19 - pdl__id?: PdlId19 + def?: Def24 + contribute?: Contribute20 + parser?: Parser20 + fallback?: Fallback20 + role?: Role20 + context?: Context20 + pdl__id?: PdlId20 pdl__result?: unknown pdl__location?: PdlLocationType | null pdl__timing?: PdlTiming | null - pdl__is_leaf?: PdlIsLeaf19 - kind?: Kind19 + pdl__is_leaf?: PdlIsLeaf20 + kind?: Kind20 function: Function return: Return } @@ -2594,6 +2725,7 @@ export interface Defs { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -2618,18 +2750,18 @@ export interface CallBlock { description?: Description1 spec?: Spec1 defs?: Defs1 - def?: Def22 - contribute?: Contribute18 - parser?: Parser18 - fallback?: Fallback18 - role?: Role18 - context?: Context18 - pdl__id?: PdlId18 + def?: Def23 + contribute?: Contribute19 + parser?: Parser19 + fallback?: Fallback19 + role?: Role19 + context?: Context19 + pdl__id?: PdlId19 pdl__result?: unknown pdl__location?: PdlLocationType | null pdl__timing?: PdlTiming | null - pdl__is_leaf?: PdlIsLeaf18 - kind?: Kind18 + pdl__is_leaf?: PdlIsLeaf19 + kind?: Kind19 call: unknown args?: unknown pdl__trace?: PdlTrace5 @@ -2655,6 +2787,7 @@ export interface Defs1 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -2686,18 +2819,18 @@ export interface LitellmModelBlock { description?: Description2 spec?: Spec2 defs?: Defs2 - def?: Def21 - contribute?: Contribute17 - parser?: Parser17 - fallback?: Fallback17 - role?: Role17 - context?: Context17 - pdl__id?: PdlId17 + def?: Def22 + contribute?: Contribute18 + parser?: Parser18 + fallback?: Fallback18 + role?: Role18 + context?: Context18 + pdl__id?: PdlId18 pdl__result?: unknown pdl__location?: PdlLocationType | null pdl__timing?: PdlTiming | null - pdl__is_leaf?: PdlIsLeaf17 - kind?: Kind17 + pdl__is_leaf?: PdlIsLeaf18 + kind?: Kind18 model: Model1 input?: Input1 modelResponse?: Modelresponse1 @@ -2731,6 +2864,7 @@ export interface Defs2 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -2755,18 +2889,18 @@ export interface GraniteioModelBlock { description?: Description3 spec?: Spec3 defs?: Defs3 - def?: Def20 - contribute?: Contribute16 - parser?: Parser16 - fallback?: Fallback16 - role?: Role16 - context?: Context16 - pdl__id?: PdlId16 + def?: Def21 + contribute?: Contribute17 + parser?: Parser17 + fallback?: Fallback17 + role?: Role17 + context?: Context17 + pdl__id?: PdlId17 pdl__result?: unknown pdl__location?: PdlLocationType | null pdl__timing?: PdlTiming | null - pdl__is_leaf?: PdlIsLeaf16 - kind?: Kind16 + pdl__is_leaf?: PdlIsLeaf17 + kind?: Kind17 model: unknown input?: Input modelResponse?: Modelresponse @@ -2802,6 +2936,7 @@ export interface Defs3 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -2836,6 +2971,75 @@ export interface CodeBlock { description?: Description4 spec?: Spec4 defs?: Defs4 + def?: Def20 + contribute?: Contribute16 + parser?: Parser16 + fallback?: Fallback16 + role?: Role16 + context?: Context16 + pdl__id?: PdlId16 + pdl__result?: unknown + pdl__location?: PdlLocationType | null + pdl__timing?: PdlTiming | null + pdl__is_leaf?: PdlIsLeaf16 + kind?: Kind16 + lang: Lang1 + code: Code +} +/** + * Type specification of the result of the block. + * + */ +export interface Spec4 { + [k: string]: unknown +} +/** + * Set of definitions executed before the execution of the block. + * + */ +export interface Defs4 { + [k: string]: + | boolean + | number + | string + | FunctionBlock + | CallBlock + | LitellmModelBlock + | GraniteioModelBlock + | CodeBlock + | ArgsBlock + | GetBlock + | DataBlock + | IfBlock + | MatchBlock + | RepeatBlock + | TextBlock + | LastOfBlock + | ArrayBlock + | ObjectBlock + | MessageBlock + | ReadBlock + | IncludeBlock + | ImportBlock + | ErrorBlock + | EmptyBlock + | null +} +/** + * Execute a command line, which will spawn a subprocess with the given argument vector. Note: if you need a shell script execution, you must wrap your command line in /bin/sh or somne shell of your choosing. + * + * Example: + * ```PDL + * args: + * - /bin/sh + * - "-c" + * - "if [[ $x = 1 ]]; then echo y; else echo n; fi" + * ``` + */ +export interface ArgsBlock { + description?: Description5 + spec?: Spec5 + defs?: Defs5 def?: Def19 contribute?: Contribute15 parser?: Parser15 @@ -2848,21 +3052,21 @@ export interface CodeBlock { pdl__timing?: PdlTiming | null pdl__is_leaf?: PdlIsLeaf15 kind?: Kind15 - lang: Lang - code: Code + lang?: Lang + args: Args } /** * Type specification of the result of the block. * */ -export interface Spec4 { +export interface Spec5 { [k: string]: unknown } /** * Set of definitions executed before the execution of the block. * */ -export interface Defs4 { +export interface Defs5 { [k: string]: | boolean | number @@ -2872,6 +3076,7 @@ export interface Defs4 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -2895,9 +3100,9 @@ export interface Defs4 { * The GetBlock is deprecated. Use DataBlock instead. */ export interface GetBlock { - description?: Description5 - spec?: Spec5 - defs?: Defs5 + description?: Description6 + spec?: Spec6 + defs?: Defs6 def?: Def18 contribute?: Contribute14 parser?: Parser14 @@ -2916,14 +3121,14 @@ export interface GetBlock { * Type specification of the result of the block. * */ -export interface Spec5 { +export interface Spec6 { [k: string]: unknown } /** * Set of definitions executed before the execution of the block. * */ -export interface Defs5 { +export interface Defs6 { [k: string]: | boolean | number @@ -2933,6 +3138,7 @@ export interface Defs5 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -2975,9 +3181,9 @@ export interface Defs5 { * ``` */ export interface DataBlock { - description?: Description6 - spec?: Spec6 - defs?: Defs6 + description?: Description7 + spec?: Spec7 + defs?: Defs7 def?: Def17 contribute?: Contribute13 parser?: Parser13 @@ -2997,14 +3203,14 @@ export interface DataBlock { * Type specification of the result of the block. * */ -export interface Spec6 { +export interface Spec7 { [k: string]: unknown } /** * Set of definitions executed before the execution of the block. * */ -export interface Defs6 { +export interface Defs7 { [k: string]: | boolean | number @@ -3014,6 +3220,7 @@ export interface Defs6 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -3044,9 +3251,9 @@ export interface Defs6 { * ``` */ export interface IfBlock { - description?: Description7 - spec?: Spec7 - defs?: Defs7 + description?: Description8 + spec?: Spec8 + defs?: Defs8 def?: Def16 contribute?: Contribute12 parser?: Parser12 @@ -3068,14 +3275,14 @@ export interface IfBlock { * Type specification of the result of the block. * */ -export interface Spec7 { +export interface Spec8 { [k: string]: unknown } /** * Set of definitions executed before the execution of the block. * */ -export interface Defs7 { +export interface Defs8 { [k: string]: | boolean | number @@ -3085,6 +3292,7 @@ export interface Defs7 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -3106,9 +3314,9 @@ export interface Defs7 { * Match control structure. */ export interface MatchBlock { - description?: Description8 - spec?: Spec8 - defs?: Defs8 + description?: Description9 + spec?: Spec9 + defs?: Defs9 def?: Def11 contribute?: Contribute11 parser?: Parser11 @@ -3128,14 +3336,14 @@ export interface MatchBlock { * Type specification of the result of the block. * */ -export interface Spec8 { +export interface Spec9 { [k: string]: unknown } /** * Set of definitions executed before the execution of the block. * */ -export interface Defs8 { +export interface Defs9 { [k: string]: | boolean | number @@ -3145,6 +3353,7 @@ export interface Defs8 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -3175,9 +3384,9 @@ export interface Defs8 { * ``` */ export interface RepeatBlock { - description?: Description9 - spec?: Spec9 - defs?: Defs9 + description?: Description10 + spec?: Spec10 + defs?: Defs10 def?: Def10 contribute?: Contribute10 parser?: Parser10 @@ -3202,14 +3411,14 @@ export interface RepeatBlock { * Type specification of the result of the block. * */ -export interface Spec9 { +export interface Spec10 { [k: string]: unknown } /** * Set of definitions executed before the execution of the block. * */ -export interface Defs9 { +export interface Defs10 { [k: string]: | boolean | number @@ -3219,6 +3428,7 @@ export interface Defs9 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -3240,9 +3450,9 @@ export interface Defs9 { * Create the concatenation of the stringify version of the result of each block of the list of blocks. */ export interface TextBlock { - description?: Description10 - spec?: Spec10 - defs?: Defs10 + description?: Description11 + spec?: Spec11 + defs?: Defs11 def?: Def9 contribute?: Contribute9 parser?: Parser9 @@ -3261,14 +3471,14 @@ export interface TextBlock { * Type specification of the result of the block. * */ -export interface Spec10 { +export interface Spec11 { [k: string]: unknown } /** * Set of definitions executed before the execution of the block. * */ -export interface Defs10 { +export interface Defs11 { [k: string]: | boolean | number @@ -3278,6 +3488,7 @@ export interface Defs10 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -3299,9 +3510,9 @@ export interface Defs10 { * Return the value of the last block if the list of blocks. */ export interface LastOfBlock { - description?: Description11 - spec?: Spec11 - defs?: Defs11 + description?: Description12 + spec?: Spec12 + defs?: Defs12 def?: Def8 contribute?: Contribute8 parser?: Parser8 @@ -3320,14 +3531,14 @@ export interface LastOfBlock { * Type specification of the result of the block. * */ -export interface Spec11 { +export interface Spec12 { [k: string]: unknown } /** * Set of definitions executed before the execution of the block. * */ -export interface Defs11 { +export interface Defs12 { [k: string]: | boolean | number @@ -3337,6 +3548,7 @@ export interface Defs11 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -3358,9 +3570,9 @@ export interface Defs11 { * Return the array of values computed by each block of the list of blocks. */ export interface ArrayBlock { - description?: Description12 - spec?: Spec12 - defs?: Defs12 + description?: Description13 + spec?: Spec13 + defs?: Defs13 def?: Def7 contribute?: Contribute7 parser?: Parser7 @@ -3379,14 +3591,14 @@ export interface ArrayBlock { * Type specification of the result of the block. * */ -export interface Spec12 { +export interface Spec13 { [k: string]: unknown } /** * Set of definitions executed before the execution of the block. * */ -export interface Defs12 { +export interface Defs13 { [k: string]: | boolean | number @@ -3396,6 +3608,7 @@ export interface Defs12 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -3417,9 +3630,9 @@ export interface Defs12 { * Return the object where the value of each field is defined by a block. If the body of the object is an array, the resulting object is the union of the objects computed by each element of the array. */ export interface ObjectBlock { - description?: Description13 - spec?: Spec13 - defs?: Defs13 + description?: Description14 + spec?: Spec14 + defs?: Defs14 def?: Def6 contribute?: Contribute6 parser?: Parser6 @@ -3438,14 +3651,14 @@ export interface ObjectBlock { * Type specification of the result of the block. * */ -export interface Spec13 { +export interface Spec14 { [k: string]: unknown } /** * Set of definitions executed before the execution of the block. * */ -export interface Defs13 { +export interface Defs14 { [k: string]: | boolean | number @@ -3455,6 +3668,7 @@ export interface Defs13 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -3476,9 +3690,9 @@ export interface Defs13 { * Create a message. */ export interface MessageBlock { - description?: Description14 - spec?: Spec14 - defs?: Defs14 + description?: Description15 + spec?: Spec15 + defs?: Defs15 def?: Def5 contribute?: Contribute5 parser?: Parser5 @@ -3497,14 +3711,14 @@ export interface MessageBlock { * Type specification of the result of the block. * */ -export interface Spec14 { +export interface Spec15 { [k: string]: unknown } /** * Set of definitions executed before the execution of the block. * */ -export interface Defs14 { +export interface Defs15 { [k: string]: | boolean | number @@ -3514,6 +3728,7 @@ export interface Defs14 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -3535,9 +3750,9 @@ export interface Defs14 { * Read from a file or standard input. */ export interface ReadBlock { - description?: Description15 - spec?: Spec15 - defs?: Defs15 + description?: Description16 + spec?: Spec16 + defs?: Defs16 def?: Def4 contribute?: Contribute4 parser?: Parser4 @@ -3558,14 +3773,14 @@ export interface ReadBlock { * Type specification of the result of the block. * */ -export interface Spec15 { +export interface Spec16 { [k: string]: unknown } /** * Set of definitions executed before the execution of the block. * */ -export interface Defs15 { +export interface Defs16 { [k: string]: | boolean | number @@ -3575,6 +3790,7 @@ export interface Defs15 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -3596,9 +3812,9 @@ export interface Defs15 { * Include a PDL file. */ export interface IncludeBlock { - description?: Description16 - spec?: Spec16 - defs?: Defs16 + description?: Description17 + spec?: Spec17 + defs?: Defs17 def?: Def3 contribute?: Contribute3 parser?: Parser3 @@ -3618,14 +3834,14 @@ export interface IncludeBlock { * Type specification of the result of the block. * */ -export interface Spec16 { +export interface Spec17 { [k: string]: unknown } /** * Set of definitions executed before the execution of the block. * */ -export interface Defs16 { +export interface Defs17 { [k: string]: | boolean | number @@ -3635,6 +3851,7 @@ export interface Defs16 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -3656,9 +3873,9 @@ export interface Defs16 { * Import a PDL file. */ export interface ImportBlock { - description?: Description17 - spec?: Spec17 - defs?: Defs17 + description?: Description18 + spec?: Spec18 + defs?: Defs18 def?: Def2 contribute?: Contribute2 parser?: Parser2 @@ -3678,14 +3895,14 @@ export interface ImportBlock { * Type specification of the result of the block. * */ -export interface Spec17 { +export interface Spec18 { [k: string]: unknown } /** * Set of definitions executed before the execution of the block. * */ -export interface Defs17 { +export interface Defs18 { [k: string]: | boolean | number @@ -3695,6 +3912,7 @@ export interface Defs17 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -3716,9 +3934,9 @@ export interface Defs17 { * Block representing an error generated at runtime. */ export interface ErrorBlock { - description?: Description18 - spec?: Spec18 - defs?: Defs18 + description?: Description19 + spec?: Spec19 + defs?: Defs19 def?: Def1 contribute?: Contribute1 parser?: Parser1 @@ -3738,14 +3956,14 @@ export interface ErrorBlock { * Type specification of the result of the block. * */ -export interface Spec18 { +export interface Spec19 { [k: string]: unknown } /** * Set of definitions executed before the execution of the block. * */ -export interface Defs18 { +export interface Defs19 { [k: string]: | boolean | number @@ -3755,6 +3973,7 @@ export interface Defs18 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -3776,9 +3995,9 @@ export interface Defs18 { * Block without an action. It can contain definitions. */ export interface EmptyBlock { - description?: Description19 - spec?: Spec19 - defs?: Defs19 + description?: Description20 + spec?: Spec20 + defs?: Defs20 def?: Def contribute?: Contribute parser?: Parser @@ -3796,14 +4015,14 @@ export interface EmptyBlock { * Type specification of the result of the block. * */ -export interface Spec19 { +export interface Spec20 { [k: string]: unknown } /** * Set of definitions executed before the execution of the block. * */ -export interface Defs19 { +export interface Defs20 { [k: string]: | boolean | number @@ -3813,6 +4032,7 @@ export interface Defs19 { | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock @@ -3852,16 +4072,16 @@ export interface Table { [k: string]: number } export interface PdlParser { - description?: Description20 - spec?: Spec20 + description?: Description21 + spec?: Spec21 pdl: Pdl } /** * A regular expression parser */ export interface RegexParser { - description?: Description21 - spec?: Spec21 + description?: Description22 + spec?: Spec22 regex: Regex mode?: Mode } diff --git a/pdl-live-react/src/pdl_ast_utils.ts b/pdl-live-react/src/pdl_ast_utils.ts index 5e68b1285..50120ffc0 100644 --- a/pdl-live-react/src/pdl_ast_utils.ts +++ b/pdl-live-react/src/pdl_ast_utils.ts @@ -1,7 +1,7 @@ import { match, P } from "ts-pattern" import { PdlBlock } from "./pdl_ast" -import { hasContextInformation } from "./helpers" +import { hasContextInformation, isArgs } from "./helpers" export function map_block_children( f: (block: PdlBlock) => PdlBlock, @@ -51,8 +51,11 @@ export function map_block_children( } }) .with({ kind: "code" }, (block) => { - const code = f(block.code) - return { ...block, code: code } + if (isArgs(block)) { + return block + } else { + return { ...block, code: f(block.code) } + } }) .with({ kind: "get" }, (block) => block) .with( @@ -155,7 +158,9 @@ export function iter_block_children( if (block.input) f(block.input) }) .with({ kind: "code" }, (block) => { - f(block.code) + if (!isArgs(block)) { + f(block.code) + } }) .with({ kind: "get" }, () => {}) .with({ kind: "data" }, () => {}) diff --git a/pdl-live-react/src/view/detail/kind/Code.tsx b/pdl-live-react/src/view/detail/kind/Code.tsx index 7de6d36f0..9cfd6be11 100644 --- a/pdl-live-react/src/view/detail/kind/Code.tsx +++ b/pdl-live-react/src/view/detail/kind/Code.tsx @@ -2,12 +2,17 @@ import Group from "../Group" import Code from "../../code/Code" import Result from "../../Result" +import { isArgs } from "../../../helpers" export default function CodeItems({ - block: { code, lang, pdl__result }, + block, }: { - block: import("../../../pdl_ast").CodeBlock + block: + | import("../../../pdl_ast").ArgsBlock + | import("../../../pdl_ast").CodeBlock }) { + const { lang, pdl__result } = block + const code = isArgs(block) ? block.args.join(" ") : block.code return ( <> {typeof code === "string" && ( diff --git a/src/pdl/pdl-schema.json b/src/pdl/pdl-schema.json index f98c8f555..ae2aad298 100644 --- a/src/pdl/pdl-schema.json +++ b/src/pdl/pdl-schema.json @@ -27,6 +27,375 @@ "title": "AnyPattern", "type": "object" }, + "ArgsBlock": { + "additionalProperties": false, + "description": "Execute a command line, which will spawn a subprocess with the given argument vector. Note: if you need a shell script execution, you must wrap your command line in /bin/sh or somne shell of your choosing.\n\nExample:\n```PDL\nargs:\n- /bin/sh\n- \"-c\"\n- \"if [[ $x = 1 ]]; then echo y; else echo n; fi\"\n```", + "properties": { + "description": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Documentation associated to the block.\n ", + "title": "Description" + }, + "spec": { + "default": null, + "description": "Type specification of the result of the block.\n ", + "title": "Spec" + }, + "defs": { + "additionalProperties": { + "anyOf": [ + { + "type": "boolean" + }, + { + "type": "integer" + }, + { + "type": "number" + }, + { + "type": "string" + }, + { + "$ref": "#/$defs/FunctionBlock" + }, + { + "$ref": "#/$defs/CallBlock" + }, + { + "$ref": "#/$defs/LitellmModelBlock" + }, + { + "$ref": "#/$defs/GraniteioModelBlock" + }, + { + "$ref": "#/$defs/CodeBlock" + }, + { + "$ref": "#/$defs/ArgsBlock" + }, + { + "$ref": "#/$defs/GetBlock" + }, + { + "$ref": "#/$defs/DataBlock" + }, + { + "$ref": "#/$defs/IfBlock" + }, + { + "$ref": "#/$defs/MatchBlock" + }, + { + "$ref": "#/$defs/RepeatBlock" + }, + { + "$ref": "#/$defs/TextBlock" + }, + { + "$ref": "#/$defs/LastOfBlock" + }, + { + "$ref": "#/$defs/ArrayBlock" + }, + { + "$ref": "#/$defs/ObjectBlock" + }, + { + "$ref": "#/$defs/MessageBlock" + }, + { + "$ref": "#/$defs/ReadBlock" + }, + { + "$ref": "#/$defs/IncludeBlock" + }, + { + "$ref": "#/$defs/ImportBlock" + }, + { + "$ref": "#/$defs/ErrorBlock" + }, + { + "$ref": "#/$defs/EmptyBlock" + }, + { + "type": "null" + } + ] + }, + "default": {}, + "description": "Set of definitions executed before the execution of the block.\n ", + "title": "Defs", + "type": "object" + }, + "def": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Name of the variable used to store the result of the execution of the block.\n ", + "title": "Def" + }, + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "anyOf": [ + { + "$ref": "#/$defs/ContributeTarget" + }, + { + "additionalProperties": { + "$ref": "#/$defs/ContributeValue" + }, + "type": "object" + } + ] + }, + "title": "Contribute", + "type": "array" + }, + "parser": { + "anyOf": [ + { + "enum": [ + "json", + "jsonl", + "yaml" + ], + "type": "string" + }, + { + "$ref": "#/$defs/PdlParser" + }, + { + "$ref": "#/$defs/RegexParser" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Parser to use to construct a value out of a string result.", + "title": "Parser" + }, + "fallback": { + "anyOf": [ + { + "type": "boolean" + }, + { + "type": "integer" + }, + { + "type": "number" + }, + { + "type": "string" + }, + { + "$ref": "#/$defs/FunctionBlock" + }, + { + "$ref": "#/$defs/CallBlock" + }, + { + "$ref": "#/$defs/LitellmModelBlock" + }, + { + "$ref": "#/$defs/GraniteioModelBlock" + }, + { + "$ref": "#/$defs/CodeBlock" + }, + { + "$ref": "#/$defs/ArgsBlock" + }, + { + "$ref": "#/$defs/GetBlock" + }, + { + "$ref": "#/$defs/DataBlock" + }, + { + "$ref": "#/$defs/IfBlock" + }, + { + "$ref": "#/$defs/MatchBlock" + }, + { + "$ref": "#/$defs/RepeatBlock" + }, + { + "$ref": "#/$defs/TextBlock" + }, + { + "$ref": "#/$defs/LastOfBlock" + }, + { + "$ref": "#/$defs/ArrayBlock" + }, + { + "$ref": "#/$defs/ObjectBlock" + }, + { + "$ref": "#/$defs/MessageBlock" + }, + { + "$ref": "#/$defs/ReadBlock" + }, + { + "$ref": "#/$defs/IncludeBlock" + }, + { + "$ref": "#/$defs/ImportBlock" + }, + { + "$ref": "#/$defs/ErrorBlock" + }, + { + "$ref": "#/$defs/EmptyBlock" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Block to execute in case of error.\n ", + "title": "Fallback" + }, + "role": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Role associated to the block and sub-blocks.\nTypical roles are `system`, `user`, and `assistant`,\nbut there may be other roles such as `available_tools`.", + "title": "Role" + }, + "context": { + "anyOf": [ + { + "items": { + "type": "object" + }, + "type": "array" + }, + { + "type": "null" + } + ], + "default": [], + "description": "Current context\n ", + "title": "Context" + }, + "pdl__id": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": "", + "description": "Unique identifier for this block\n ", + "title": "Pdl Id" + }, + "pdl__result": { + "anyOf": [ + {}, + { + "type": "null" + } + ], + "default": null, + "description": "Result of the execution of the block", + "title": "Pdl Result" + }, + "pdl__location": { + "anyOf": [ + { + "$ref": "#/$defs/PdlLocationType" + }, + { + "type": "null" + } + ], + "default": null + }, + "pdl__timing": { + "anyOf": [ + { + "$ref": "#/$defs/PdlTiming" + }, + { + "type": "null" + } + ], + "default": null + }, + "pdl__is_leaf": { + "const": true, + "default": true, + "title": "Pdl Is Leaf", + "type": "boolean" + }, + "kind": { + "const": "code", + "default": "code", + "title": "Kind", + "type": "string" + }, + "lang": { + "const": "command", + "default": "command", + "title": "Lang", + "type": "string" + }, + "args": { + "description": "The argument vector to spawn.\n ", + "items": { + "anyOf": [ + { + "type": "string" + }, + { + "$ref": "#/$defs/LocalizedExpression_TypeVar_" + } + ] + }, + "title": "Args", + "type": "array" + } + }, + "required": [ + "args" + ], + "title": "ArgsBlock", + "type": "object" + }, "ArrayBlock": { "additionalProperties": false, "description": "Return the array of values computed by each block of the list of blocks.", @@ -79,6 +448,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -222,6 +594,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -393,6 +768,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -562,6 +940,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -705,6 +1086,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -901,6 +1285,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -1012,6 +1399,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -1155,6 +1545,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -1335,6 +1728,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -1480,6 +1876,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -1623,6 +2022,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -1841,6 +2243,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -1984,6 +2389,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -2180,6 +2588,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -2323,6 +2734,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -2497,6 +2911,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -2609,6 +3026,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -2752,6 +3172,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -2933,6 +3356,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -3045,6 +3471,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -3188,6 +3617,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -3392,6 +3824,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -3535,6 +3970,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -3717,6 +4155,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -3824,6 +4265,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -3993,6 +4437,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -4136,6 +4583,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -4320,6 +4770,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -4401,6 +4854,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -4526,6 +4982,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -4669,6 +5128,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -4843,6 +5305,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -4954,6 +5419,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -5097,6 +5565,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -5271,6 +5742,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -5450,6 +5924,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -5593,6 +6070,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -5764,6 +6244,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -5876,6 +6359,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -6019,6 +6505,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -6200,6 +6689,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -6307,6 +6799,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -6869,6 +7364,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -7012,6 +7510,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -7265,6 +7766,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -7411,6 +7915,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -7554,6 +8061,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -7722,6 +8232,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -7834,6 +8347,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -7977,6 +8493,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -8149,6 +8668,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -8231,6 +8753,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -8436,6 +8961,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -8605,6 +9133,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -8748,6 +9279,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -8852,6 +9386,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -8995,6 +9532,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -9279,6 +9819,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -9422,6 +9965,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -9634,6 +10180,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -9775,6 +10324,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -9893,6 +10445,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -10036,6 +10591,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -10205,6 +10763,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, @@ -10280,6 +10841,9 @@ { "$ref": "#/$defs/CodeBlock" }, + { + "$ref": "#/$defs/ArgsBlock" + }, { "$ref": "#/$defs/GetBlock" }, diff --git a/src/pdl/pdl_ast.py b/src/pdl/pdl_ast.py index 0367f360c..a2c88926d 100644 --- a/src/pdl/pdl_ast.py +++ b/src/pdl/pdl_ast.py @@ -455,7 +455,11 @@ class GraniteioModelBlock(ModelBlock): """ -class CodeBlock(LeafBlock): +class BaseCodeBlock(LeafBlock): + kind: Literal[BlockKind.CODE] = BlockKind.CODE + + +class CodeBlock(BaseCodeBlock): """ Execute a piece of code. @@ -470,7 +474,6 @@ class CodeBlock(LeafBlock): ``` """ - kind: Literal[BlockKind.CODE] = BlockKind.CODE lang: Annotated[ Literal["python", "command", "jinja", "pdl"], BeforeValidator(_ensure_lower) ] @@ -481,6 +484,25 @@ class CodeBlock(LeafBlock): """ +class ArgsBlock(BaseCodeBlock): + """ + Execute a command line, which will spawn a subprocess with the given argument vector. Note: if you need a shell script execution, you must wrap your command line in /bin/sh or somne shell of your choosing. + + Example: + ```PDL + args: + - /bin/sh + - "-c" + - "if [[ $x = 1 ]]; then echo y; else echo n; fi" + ``` + """ + + lang: Annotated[Literal["command"], BeforeValidator(_ensure_lower)] = "command" + args: list[ExpressionType[str]] + """The argument vector to spawn. + """ + + class GetBlock(LeafBlock): """ Get the value of a variable. @@ -772,6 +794,7 @@ class EmptyBlock(LeafBlock): | LitellmModelBlock | GraniteioModelBlock | CodeBlock + | ArgsBlock | GetBlock | DataBlock | IfBlock diff --git a/src/pdl/pdl_dumper.py b/src/pdl/pdl_dumper.py index a5c2e369f..c93064ceb 100644 --- a/src/pdl/pdl_dumper.py +++ b/src/pdl/pdl_dumper.py @@ -7,6 +7,7 @@ from . import pdl_ast from .pdl_ast import ( AnyPattern, + ArgsBlock, ArrayBlock, ArrayPattern, Block, @@ -138,6 +139,8 @@ def block_to_dict( # noqa: C901 d["modelResponse"] = block.modelResponse if block.pdl__model_stats is not None: d["pdl__model_stats"] = model_stats_to_dict(block.pdl__model_stats) + case ArgsBlock(): + d["args"] = block.args case CodeBlock(): d["lang"] = block.lang d["code"] = block_to_dict(block.code, json_compatible) diff --git a/src/pdl/pdl_interpreter.py b/src/pdl/pdl_interpreter.py index ad8125f23..352154968 100644 --- a/src/pdl/pdl_interpreter.py +++ b/src/pdl/pdl_interpreter.py @@ -36,6 +36,7 @@ from .pdl_ast import ( # noqa: E402 AdvancedBlockType, AnyPattern, + ArgsBlock, ArrayBlock, ArrayPattern, Block, @@ -448,7 +449,7 @@ def process_block_body( result, background, scope, trace = process_call_model( state, scope, block, loc ) - case CodeBlock(): + case ArgsBlock() | CodeBlock(): result, background, scope, trace = process_call_code( state, scope, block, loc ) @@ -1444,17 +1445,27 @@ def generate_client_response_single( def process_call_code( - state: InterpreterState, scope: ScopeType, block: CodeBlock, loc: PdlLocationType -) -> tuple[PdlLazy[Any], LazyMessages, ScopeType, CodeBlock]: + state: InterpreterState, + scope: ScopeType, + block: ArgsBlock | CodeBlock, + loc: PdlLocationType, +) -> tuple[PdlLazy[Any], LazyMessages, ScopeType, ArgsBlock | CodeBlock]: background: LazyMessages - code_, _, _, block = process_block_of( - block, - "code", - state.with_yield_result(False).with_yield_background(False), - scope, - loc, - ) - code_s = code_.result() + code_a: None | list[str] = None + code_s = "" + match block: + case ArgsBlock(): + code_a = [process_expr(scope, arg_i, loc) for arg_i in block.args] + case CodeBlock(): + code_, _, _, block = process_block_of( + block, + "code", + state.with_yield_result(False).with_yield_background(False), + scope, + loc, + ) + code_s = code_.result() + match block.lang: case "python": try: @@ -1472,7 +1483,7 @@ def process_call_code( ) from exc case "command": try: - result = call_command(code_s) + result = call_command(code_s, code_a) background = PdlList( [ PdlDict( # type: ignore @@ -1546,8 +1557,11 @@ def call_python(code: str, scope: ScopeType) -> PdlLazy[Any]: return PdlConst(result) -def call_command(code: str) -> PdlLazy[str]: - args = shlex.split(code) +def call_command(code: str, code_a: list[str] | None) -> PdlLazy[str]: + if code_a is not None: + args = code_a + else: + args = shlex.split(code) p = subprocess.run( args, capture_output=True, text=True, check=False, shell=False ) # nosec B603 diff --git a/tests/test_code.py b/tests/test_code.py index c099640e7..513813587 100644 --- a/tests/test_code.py +++ b/tests/test_code.py @@ -61,6 +61,26 @@ def test_contribute_false(): ] } +command_data_args = { + "lastOf": [ + { + "def": "world1", + "lang": "command", + "code": "echo -n \\'World\\'", # test nested quotes + }, + { + "def": "world", + "args": [ + "echo", + "-n", + "${ world1 }", # and jinja expansion of nested quotes + ], + "contribute": [], + }, + "Hello ${ world }!", + ] +} + def test_command(): result = exec_dict(command_data, output="all") @@ -70,6 +90,15 @@ def test_command(): assert scope["world"] == "World" +def test_command_args(): + result = exec_dict(command_data_args, output="all") + document = result["result"] + scope = result["scope"] + assert document == "Hello 'World'!" + assert scope["world1"] == "'World'" + assert scope["world"] == "'World'" + + def test_jinja1(): prog_str = """ defs: