|
1 | 1 | import { html } from "@commontools/common-html"; |
2 | | -import { recipe, lift, NAME, UI } from "../builder/index.js"; |
| 2 | +import { |
| 3 | + recipe, |
| 4 | + lift, |
| 5 | + generateData, |
| 6 | + handler, |
| 7 | + NAME, |
| 8 | + UI, |
| 9 | + ifElse, |
| 10 | +} from "../builder/index.js"; |
| 11 | +import { run, getCellReferenceOrValue } from "../runner/index.js"; |
| 12 | +import { openSaga, addGems, ID } from "../data.js"; |
3 | 13 |
|
4 | 14 | export const prompt = recipe<{ title: string }>("prompt", ({ title }) => { |
5 | 15 | // this kinda makes sense but feels painful? better syntactic sugar? |
6 | | - const url = lift<{ title: string }, string>( |
| 16 | + |
| 17 | + const url = lift( |
7 | 18 | ({ title }) => |
8 | 19 | `https://ct-img.m4ke.workers.dev/?prompt=${encodeURIComponent(title)}`, |
9 | 20 | )({ title }); |
10 | 21 |
|
| 22 | + const query = lift(({ title }) => ({ |
| 23 | + prompt: `generate 10 image prompt variations for the current prompt: ${title}. Some should change just the style, some should change the content, and some should change both.`, |
| 24 | + result: [], |
| 25 | + schema: { |
| 26 | + type: "array", |
| 27 | + items: { |
| 28 | + type: "object", |
| 29 | + properties: { |
| 30 | + title: { |
| 31 | + type: "string", |
| 32 | + description: "Image prompt", |
| 33 | + }, |
| 34 | + }, |
| 35 | + required: ["title"], |
| 36 | + }, |
| 37 | + }, |
| 38 | + }))({ |
| 39 | + title, |
| 40 | + }); |
| 41 | + |
| 42 | + const { result: variations } = generateData<string[]>(query); |
| 43 | + |
| 44 | + // const inner = ({ title }) => html`<div>${title}</div>`; |
| 45 | + const inner = ({ title }) => html` <common-button onclick=${handler( |
| 46 | + { title }, |
| 47 | + (_, { title }) => { |
| 48 | + const newPrompt = run(prompt, { |
| 49 | + title: getCellReferenceOrValue(title), |
| 50 | + }); |
| 51 | +
|
| 52 | + addGems([newPrompt]); |
| 53 | +
|
| 54 | + openSaga(newPrompt.get()[ID]); |
| 55 | + }, |
| 56 | + )}}"> |
| 57 | + ${title} |
| 58 | + </common-button>`; |
| 59 | + |
11 | 60 | return { |
12 | 61 | [NAME]: title, |
13 | | - [UI]: html`<div> |
14 | | - Prompt: ${title}<br /><img src=${url} width="100%" /> |
15 | | - </div>`, |
| 62 | + [UI]: html`<common-vstack gap="sm"> |
| 63 | + ${title} |
| 64 | + <img src=${url} width="100%" /> |
| 65 | + ${ifElse(variations, variations.map(inner), html`<i></i>`)} |
| 66 | + </common-vstack>`, |
16 | 67 | }; |
17 | 68 | }); |
0 commit comments