Skip to content

Commit effffd7

Browse files
authored
Really really import (#1091)
* seeder's "import json" command now creates a data holder recipe - the recipe has on logic, just holds the values - it is extended - you can optionally pass your own schema to it
1 parent 5ededd7 commit effffd7

File tree

3 files changed

+79
-12
lines changed

3 files changed

+79
-12
lines changed

seeder/cli.ts

+54-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
import { parseArgs } from "@std/cli/parse-args";
2-
import { castNewRecipe, CharmManager } from "@commontools/charm";
2+
import {
3+
castNewRecipe,
4+
CharmManager,
5+
compileAndRunRecipe,
6+
} from "@commontools/charm";
37
import { getEntityId, setBobbyServerUrl, storage } from "@commontools/runner";
48
import { createSession, Identity } from "@commontools/identity";
5-
import { formatJsonImportPrompt, LLMClient, setLLMUrl } from "@commontools/llm";
9+
import { LLMClient, setLLMUrl } from "@commontools/llm";
610
import { processWorkflow } from "@commontools/charm";
711
import { type CharmResult, CommandType, type Step } from "./interfaces.ts";
812
import { scenarios } from "./scenarios.ts";
913
import { toolshedUrl } from "./env.ts";
1014
import { llmVerifyCharm } from "./judge.ts";
15+
import {
16+
createJsonSchema,
17+
derive,
18+
NAME,
19+
recipe,
20+
UI,
21+
} from "@commontools/builder";
22+
import { h } from "@commontools/html";
1123
import { ensureReportDir, generateReport } from "./report.ts";
1224
import {
1325
addErrorListeners,
@@ -128,14 +140,48 @@ async function processCommand(
128140
throw new Error("Missing data for JSON import.");
129141
}
130142

131-
const jsonPrompt = formatJsonImportPrompt(prompt, step.data);
132-
const form = await processWorkflow(
133-
jsonPrompt,
143+
// FIXME(ja): we should move this to a common charm method so that
144+
// jumble and seeder can share
145+
const schema = step.dataSchema ?? createJsonSchema(step.data);
146+
const schemaString = JSON.stringify(schema, null, 2);
147+
const result = Object.keys(schema.properties ?? {}).map((key) =>
148+
` ${key}: data.${key},\n`
149+
).join("\n");
150+
151+
const dataRecipeSrc = `import { h } from "@commontools/html";
152+
import { recipe, UI, NAME, derive, type JSONSchema } from "@commontools/builder";
153+
154+
const schema = ${schemaString};
155+
156+
export default recipe(schema, schema, (data) => ({
157+
[NAME]: "Data Import",
158+
[UI]: <div><h2>schema</h2><pre>${
159+
schemaString.replaceAll("{", "&#123;")
160+
.replaceAll("}", "&#125;")
161+
.replaceAll("\n", "<br/>")
162+
}</pre></div>,
163+
${result}
164+
}));`;
165+
166+
const dataCharm = await compileAndRunRecipe(
134167
charmManager,
135-
{
136-
cache,
137-
},
168+
dataRecipeSrc,
169+
"data import",
170+
step.data,
138171
);
172+
173+
const form = await processWorkflow(prompt, charmManager, {
174+
existingCharm: dataCharm,
175+
cache,
176+
prefill: {
177+
classification: {
178+
workflowType: "imagine",
179+
confidence: 1.0,
180+
reasoning: "hard coded",
181+
},
182+
},
183+
});
184+
139185
const newCharm = form.generation?.charm;
140186
const id = getEntityId(newCharm);
141187
if (id) {

seeder/interfaces.ts

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export type Step = {
3131
type: CommandType;
3232
prompt: string;
3333
data?: any;
34+
dataSchema?: any;
3435
};
3536

3637
export type Scenario = {

seeder/scenarios.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { CommandType, type Scenario, type Step } from "./interfaces.ts";
33
export const scenarios: Scenario[] = [
44
{
55
name: "2048 Game Long",
6+
tags: ["smol"],
67
steps: [{
78
type: CommandType.New,
89
prompt: "a 2048 game",
910
}],
10-
tags: ["smol"],
1111
},
1212
{
1313
name: "2048 Game Short",
@@ -216,10 +216,31 @@ Hierarchical integrity is maintained as subitems with their Parent items.
216216
}],
217217
},
218218
{
219-
name: "Import JSON Sample",
219+
name: "Recipe Collection Manager",
220+
tags: ["import"],
220221
steps: [{
221222
type: CommandType.ImportJSON,
222-
prompt: "Recipe Collection",
223+
prompt:
224+
"Recipe collection manager, the lets the user dream up new recipes using the LLM",
225+
dataSchema: {
226+
type: "object",
227+
properties: {
228+
"recipes": {
229+
type: "array",
230+
items: {
231+
"type": "object",
232+
"properties": {
233+
"name": { "type": "string" },
234+
"ingredients": {
235+
"type": "array",
236+
"items": { "type": "string" },
237+
},
238+
"instructions": { "type": "string" },
239+
},
240+
},
241+
},
242+
},
243+
},
223244
data: {
224245
"recipes": [
225246
{
@@ -250,6 +271,5 @@ Hierarchical integrity is maintained as subitems with their Parent items.
250271
],
251272
},
252273
}],
253-
tags: ["import"],
254274
},
255275
];

0 commit comments

Comments
 (0)