11import { match , P } from "ts-pattern"
22
3- import { PdlBlock } from "./pdl_ast"
4- import { hasContextInformation , hasResult , isArgs } from "./helpers"
3+ import { Backend , PdlBlock , Processor } from "./pdl_ast"
4+ import { ExpressionT , isArgs } from "./helpers"
55
66export function map_block_children (
7- f : ( block : PdlBlock ) => PdlBlock ,
7+ f_block : ( block : PdlBlock ) => PdlBlock ,
8+ f_expr : ( expr : ExpressionT < unknown > ) => ExpressionT < unknown > ,
89 block : PdlBlock ,
910) : PdlBlock {
1011 if (
@@ -21,118 +22,171 @@ export function map_block_children(
2122 } else {
2223 const defs : { [ k : string ] : PdlBlock } = { }
2324 for ( const x in block . defs ) {
24- defs [ x ] = f ( block . defs [ x ] )
25+ defs [ x ] = f_block ( block . defs [ x ] )
2526 }
2627 new_block = { ...block , defs : defs }
2728 }
29+ if ( new_block ?. contribute !== undefined ) {
30+ const contribute = new_block . contribute ?. map ( ( contrib ) =>
31+ match ( contrib )
32+ . with ( { } , ( c ) =>
33+ Object . fromEntries (
34+ Object . entries ( c ) . map ( ( [ k , v ] ) => [
35+ k ,
36+ match ( v )
37+ . with ( { value : P . array ( P . _ ) } , ( v ) => ( {
38+ value : v . value . map ( f_expr ) ,
39+ } ) )
40+ . otherwise ( ( v ) => v ) ,
41+ ] ) ,
42+ ) ,
43+ )
44+ . otherwise ( ( contrib ) => contrib ) ,
45+ )
46+ new_block = { ...new_block , contribute }
47+ }
48+ // @ts -expect-error: TODO
2849 new_block = match ( new_block )
2950 // .with(P.string, s => s)
3051 . with ( { kind : "empty" } , ( block ) => block )
3152 . with ( { kind : "function" } , ( block ) => {
32- const returns = f ( block . return )
33- return { ...block , return : returns }
53+ const return_ = f_block ( block . return )
54+ return { ...block , return : return_ }
55+ } )
56+ . with ( { kind : "call" } , ( block ) => {
57+ const call = f_expr ( block . call )
58+ const args = f_expr ( block . args )
59+ return { ...block , call, args }
3460 } )
35- . with ( { kind : "call" } , ( block ) => block )
61+ . with (
62+ {
63+ kind : "model" ,
64+ platform : "granite-io" ,
65+ backend : P . nonNullable ,
66+ processor : P . _ ,
67+ } ,
68+ ( block ) => {
69+ const model = f_expr ( block . model )
70+ const input = block . input ? f_block ( block . input ) : undefined
71+ // @ts -expect-error: f_expr does not preserve the type of the expression
72+ const parameters : Parameters = block . parameters
73+ ? f_expr ( block . parameters )
74+ : undefined
75+ // @ts -expect-error: f_expr does not preserve the type of the expression
76+ const backend : Backend = f_expr ( block . backend )
77+ // @ts -expect-error: f_expr does not preserve the type of the expression
78+ const processor : Processor = block . processor
79+ ? f_expr ( block . processor )
80+ : undefined
81+ return {
82+ ...block ,
83+ model,
84+ input,
85+ parameters,
86+ backend,
87+ processor,
88+ }
89+ } ,
90+ )
3691 . with ( { kind : "model" } , ( block ) => {
37- if ( block . input ) {
38- const input = f ( block . input )
39- console . error ( "!!!!!" , input )
40- block = { ...block , input }
41- }
42- if (
43- hasResult ( block . model ) &&
44- typeof block . model . pdl__result === "string"
45- ) {
46- block = { ...block , model : block . model . pdl__result }
47- }
48- // Remove `defsite` from context:
92+ const model = f_expr ( block . model )
93+ const input = block . input ? f_block ( block . input ) : undefined
94+ const parameters = block . parameters ? f_expr ( block . parameters ) : undefined
4995 return {
5096 ...block ,
51- context : ! hasContextInformation ( block )
52- ? undefined
53- : JSON . parse (
54- JSON . stringify ( block . context , ( k , v ) =>
55- k === "defsite" ? undefined : v ,
56- ) ,
57- ) ,
97+ platform : "litellm" ,
98+ model,
99+ input,
100+ parameters,
58101 }
59102 } )
60103 . with ( { kind : "code" } , ( block ) => {
61104 if ( isArgs ( block ) ) {
62- return block
63- } else {
64- return { ...block , code : f ( block . code ) }
105+ const args = block . args . map ( ( arg ) => f_expr ( arg ) )
106+ return { ...block , args }
65107 }
108+ return { ...block , code : f_block ( block . code ) }
66109 } )
67110 . with ( { kind : "get" } , ( block ) => block )
68- . with (
69- { kind : "data" , data : P . union ( P . number , P . boolean , P . string ) } ,
70- ( { data } ) => data , // { kind: "data", data: "hello" } -> "hello"
71- )
72- . with ( { kind : "data" } , ( block ) => block )
111+ . with ( { kind : "data" } , ( block ) => {
112+ const data = f_expr ( block . data )
113+ return { ...block , data }
114+ } )
73115 . with ( { kind : "text" } , ( block ) => {
74116 let text
75117 if ( block . text instanceof Array ) {
76- text = block . text . map ( f )
118+ text = block . text . map ( f_block )
77119 } else {
78- text = f ( block . text )
120+ text = f_block ( block . text )
79121 }
80122 return { ...block , text : text }
81123 } )
82124 . with ( { kind : "lastOf" } , ( block ) => {
83- const lastOf = block . lastOf . map ( f )
125+ const lastOf = block . lastOf . map ( f_block )
84126 return { ...block , lastOf : lastOf }
85127 } )
86128 . with ( { kind : "array" } , ( block ) => {
87- const array = block . array . map ( f )
129+ const array = block . array . map ( f_block )
88130 return { ...block , array : array }
89131 } )
90132 . with ( { kind : "object" } , ( block ) => {
91133 let object
92134 if ( block . object instanceof Array ) {
93- object = block . object . map ( f )
135+ object = block . object . map ( f_block )
94136 } else {
95137 object = Object . fromEntries (
96- Object . entries ( block . object ) . map ( ( [ k , v ] ) => [ k , f ( v ) ] ) ,
138+ Object . entries ( block . object ) . map ( ( [ k , v ] ) => [ k , f_block ( v ) ] ) ,
97139 )
98140 }
99141 return { ...block , object : object }
100142 } )
101143 . with ( { kind : "message" } , ( block ) => {
102- const content = f ( block . content )
144+ const content = f_block ( block . content )
103145 return { ...block , content : content }
104146 } )
105147 . with ( { kind : "if" } , ( block ) => {
106- const then_ = f ( block . then )
107- const else_ = block . else ? f ( block . else ) : undefined
108- return { ...block , then : then_ , else : else_ }
148+ const if_ = f_expr ( block . if )
149+ const then_ = f_block ( block . then )
150+ const else_ = block . else ? f_block ( block . else ) : undefined
151+ return { ...block , if : if_ , then : then_ , else : else_ }
109152 } )
110153 . with ( { kind : "match" } , ( block ) => {
154+ const match = f_expr ( block . match )
111155 const with_ = block . with . map ( ( match_case ) => {
112- return { ...match_case , then : f ( match_case . then ) }
156+ const if_ = f_expr ( match_case . if )
157+ const then_ = f_block ( match_case . then )
158+ return { ...match_case , if : if_ , then : then_ }
113159 } )
114- return { ...block , with : with_ }
160+ return { ...block , match , with : with_ }
115161 } )
116162 . with ( { kind : "repeat" } , ( block ) => {
117- const repeat = f ( block . repeat )
118- return { ...block , repeat : repeat }
163+ const for_ = block ?. for ? f_expr ( block . for ) : undefined
164+ const until = block ?. until ? f_expr ( block . until ) : undefined
165+ const max_iterations = block ?. max_iterations
166+ ? f_expr ( block . max_iterations )
167+ : undefined
168+ const repeat = f_block ( block . repeat )
169+ return { ...block , for : for_ , repeat, until, max_iterations }
119170 } )
120171 . with ( { kind : "error" } , ( block ) => {
121- const doc = f ( block . program )
172+ const doc = f_block ( block . program )
122173 return { ...block , program : doc }
123174 } )
124- . with ( { kind : "read" } , ( block ) => block )
175+ . with ( { kind : "read" } , ( block ) => {
176+ const read = f_expr ( block . read )
177+ return { ...block , read }
178+ } )
125179 . with ( { kind : "include" } , ( block ) => block )
126180 . with ( { kind : "import" } , ( block ) => block )
127181 . with ( { kind : undefined } , ( block ) => block )
128182 . exhaustive ( )
129183 match ( new_block )
130184 . with ( { parser : { pdl : P . _ } } , ( block ) => {
131- block . parser . pdl = f ( block . parser . pdl )
185+ block . parser . pdl = f_block ( block . parser . pdl )
132186 } )
133187 . otherwise ( ( ) => { } )
134188 if ( block . fallback ) {
135- block . fallback = f ( block . fallback )
189+ block . fallback = f_block ( block . fallback )
136190 }
137191 return new_block
138192}
0 commit comments