@@ -7,6 +7,7 @@ import { Charm, CharmManager } from "./charm.ts";
77import { buildFullRecipe , getIframeRecipe } from "./iframe/recipe.ts" ;
88import { buildPrompt , RESPONSE_PREFILL } from "./iframe/prompt.ts" ;
99import { injectUserCode } from "./iframe/static.ts" ;
10+ import { isCell } from "../../runner/src/cell.ts" ;
1011
1112const llm = new LLMClient ( LLMClient . DEFAULT_URL ) ;
1213
@@ -32,31 +33,25 @@ const genSrc = async ({
3233 response = RESPONSE_PREFILL + response ;
3334 }
3435
35- const source = injectUserCode ( response . split ( RESPONSE_PREFILL ) [ 1 ] . split ( "\n```" ) [ 0 ] ) ;
36+ const source = injectUserCode (
37+ response . split ( RESPONSE_PREFILL ) [ 1 ] . split ( "\n```" ) [ 0 ] ,
38+ ) ;
3639 return source ;
3740} ;
3841
3942export async function iterate (
4043 charmManager : CharmManager ,
41- charm : Cell < Charm > | null ,
42- value : string ,
44+ charm : Cell < Charm > ,
45+ spec : string ,
4346 shiftKey : boolean ,
4447 model ?: string ,
45- ) : Promise < EntityId | undefined > {
46- if ( ! charm ) {
47- console . error ( "FIXME, no charm, what should we do?" ) ;
48- return ;
49- }
50-
48+ ) : Promise < Cell < Charm > > {
5149 const { iframe } = getIframeRecipe ( charm ) ;
5250 if ( ! iframe ) {
53- console . error (
54- "Cannot iterate on a non-iframe. Must extend instead." ,
55- ) ;
56- return ;
51+ throw new Error ( "Cannot iterate on a non-iframe. Must extend instead." ) ;
5752 }
5853
59- const newSpec = shiftKey ? iframe . spec + "\n" + value : value ;
54+ const newSpec = shiftKey ? iframe . spec + "\n" + spec : spec ;
6055
6156 const newIFrameSrc = await genSrc ( {
6257 src : iframe . src ,
@@ -66,21 +61,7 @@ export async function iterate(
6661 model : model ,
6762 } ) ;
6863
69- return saveNewRecipeVersion ( charmManager , charm , newIFrameSrc , newSpec ) ;
70- }
71-
72- export async function extend (
73- charmManager : CharmManager ,
74- charm : Cell < Charm > | null ,
75- value : string ,
76- model ?: string ,
77- ) : Promise < EntityId | undefined > {
78- if ( ! charm ) {
79- console . error ( "FIXME, no charm, what should we do?" ) ;
80- return ;
81- }
82-
83- return await castRecipeOnCell ( charmManager , charm , value ) ;
64+ return generateNewRecipeVersion ( charmManager , charm , newIFrameSrc , newSpec ) ;
8465}
8566
8667export function extractTitle ( src : string , defaultTitle : string ) : string {
@@ -89,7 +70,7 @@ export function extractTitle(src: string, defaultTitle: string): string {
8970 return htmlTitleMatch || jsTitleMatch || defaultTitle ;
9071}
9172
92- export const saveNewRecipeVersion = async (
73+ export const generateNewRecipeVersion = (
9374 charmManager : CharmManager ,
9475 charm : Cell < Charm > ,
9576 newIFrameSrc : string ,
@@ -98,19 +79,18 @@ export const saveNewRecipeVersion = async (
9879 const { recipeId, iframe } = getIframeRecipe ( charm ) ;
9980
10081 if ( ! recipeId || ! iframe ) {
101- console . error ( "FIXME, no recipeId or iframe, what should we do?" ) ;
102- return ;
82+ throw new Error ( "FIXME, no recipeId or iframe, what should we do?" ) ;
10383 }
10484
105- const name = extractTitle ( newIFrameSrc , ' <unknown>' ) ;
85+ const name = extractTitle ( newIFrameSrc , " <unknown>" ) ;
10686 const newRecipeSrc = buildFullRecipe ( {
10787 ...iframe ,
10888 src : newIFrameSrc ,
10989 spec : newSpec ,
11090 name,
11191 } ) ;
11292
113- return await compileAndRunRecipe (
93+ return compileAndRunRecipe (
11494 charmManager ,
11595 newRecipeSrc ,
11696 newSpec ,
@@ -119,38 +99,17 @@ export const saveNewRecipeVersion = async (
11999 ) ;
120100} ;
121101
122- export async function castRecipeOnCell (
123- charmManager : CharmManager ,
124- cell : Cell < any > ,
125- newSpec : string ,
126- ) : Promise < EntityId | undefined > {
127- const schema = { ...cell . schema , description : newSpec } ;
128- console . log ( "schema" , schema ) ;
129-
130- const newIFrameSrc = await genSrc ( { newSpec, schema } ) ;
131- const name = extractTitle ( newIFrameSrc , '<unknown>' ) ;
132- const newRecipeSrc = buildFullRecipe ( {
133- src : newIFrameSrc ,
134- spec : newSpec ,
135- argumentSchema : schema ,
136- resultSchema : { } ,
137- name,
138- } ) ;
139-
140- return await compileAndRunRecipe ( charmManager , newRecipeSrc , newSpec , cell ) ;
141- }
142-
143102export async function castNewRecipe (
144103 charmManager : CharmManager ,
145104 data : any ,
146105 newSpec : string ,
147- ) : Promise < EntityId | undefined > {
148- const schema = createJsonSchema ( { } , data ) ;
106+ ) : Promise < Cell < Charm > > {
107+ const schema = isCell ( data ) ? { ... data . schema } : createJsonSchema ( { } , data ) ;
149108 schema . description = newSpec ;
150109 console . log ( "schema" , schema ) ;
151110
152111 const newIFrameSrc = await genSrc ( { newSpec, schema } ) ;
153- const name = extractTitle ( newIFrameSrc , ' <unknown>' ) ;
112+ const name = extractTitle ( newIFrameSrc , " <unknown>" ) ;
154113 const newRecipeSrc = buildFullRecipe ( {
155114 src : newIFrameSrc ,
156115 spec : newSpec ,
@@ -159,7 +118,7 @@ export async function castNewRecipe(
159118 name,
160119 } ) ;
161120
162- return await compileAndRunRecipe ( charmManager , newRecipeSrc , newSpec , data ) ;
121+ return compileAndRunRecipe ( charmManager , newRecipeSrc , newSpec , data ) ;
163122}
164123
165124export async function compileRecipe (
@@ -169,13 +128,11 @@ export async function compileRecipe(
169128) {
170129 const { exports, errors } = await tsToExports ( recipeSrc ) ;
171130 if ( errors ) {
172- console . error ( "Compilation errors in recipe:" , errors ) ;
173- return ;
131+ throw new Error ( "Compilation errors in recipe" ) ;
174132 }
175133 const recipe = exports . default ;
176134 if ( ! recipe ) {
177- console . error ( "No default recipe found in the compiled exports." ) ;
178- return ;
135+ throw new Error ( "No default recipe found in the compiled exports." ) ;
179136 }
180137 const parentsIds = parents ?. map ( ( id ) => id . toString ( ) ) ;
181138 registerNewRecipe ( recipe , recipeSrc , spec , parentsIds ) ;
@@ -188,15 +145,11 @@ export async function compileAndRunRecipe(
188145 spec : string ,
189146 runOptions : any ,
190147 parents ?: string [ ] ,
191- ) : Promise < EntityId | undefined > {
148+ ) : Promise < Cell < Charm > > {
192149 const recipe = await compileRecipe ( recipeSrc , spec , parents ) ;
193150 if ( ! recipe ) {
194- return ;
151+ throw new Error ( "Failed to compile recipe" ) ;
195152 }
196153
197- const newCharm = await charmManager . runPersistent ( recipe , runOptions ) ;
198- await charmManager . add ( [ newCharm ] ) ;
199- await charmManager . syncRecipe ( newCharm ) ;
200-
201- return newCharm . entityId ;
154+ return charmManager . runPersistent ( recipe , runOptions ) ;
202155}
0 commit comments