Skip to content

Commit 902c252

Browse files
committed
iframe uses generateText
1 parent ac18bb6 commit 902c252

File tree

1 file changed

+26
-27
lines changed
  • typescript/packages/lookslike-high-level/src/recipes

1 file changed

+26
-27
lines changed

typescript/packages/lookslike-high-level/src/recipes/iframe.ts

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
UI,
55
NAME,
66
lift,
7+
ifElse,
78
generateText,
89
handler,
910
str,
@@ -85,16 +86,23 @@ const prepSuggestions = lift(({ src, prompt, schema }) => {
8586
return {};
8687
}
8788

88-
let user = `Given the current prompt: "${prompt}"`;
89-
user += `\n\nGiven the following schema:\n<view-model-schema>\n${JSON.stringify(schema, null, 2)}\n</view-model-schema>`;
90-
user += `\n\nAnd the current HTML:\n\`\`\`html\n${src}\n\`\`\``;
91-
user += `\n\nSuggest 3 prompts to enhancem, refine or branch off into a new UI. Keep it simple these add or change a single feature.
89+
let instructions = `Given the current prompt: "${prompt}"
9290
93-
Do not ever exceed a single sentence. Prefer terse, suggestions that take one step.
94-
`;
91+
Given the following schema:
92+
<view-model-schema>
93+
${JSON.stringify(schema, null, 2)}
94+
</view-model-schema>
95+
96+
And the current HTML:
97+
98+
${src}
99+
100+
Suggest 3 prompts to enhance, refine or branch off into a new UI. Keep it simple these add or change a single feature.
101+
102+
Do not ever exceed a single sentence. Prefer terse, suggestions that take one step.`;
95103

96104
return {
97-
messages: [user, '```json\n{"suggestions":['],
105+
messages: [instructions, '```json\n{"suggestions":['],
98106
system: `Suggest extensions to the UI either as modifications or forks off into new interfaces. Avoid bloat, focus on the user experience and creative potential. Respond in a json block.
99107
100108
\`\`\`json
@@ -215,31 +223,23 @@ const prepHTML = lift(({ prompt, schema, lastSrc }) => {
215223
}
216224
});
217225

218-
const grabHTML = lift<{ pending: boolean, partial: string }, string>(({ pending, partial }) => {
219-
if (pending || !partial) {
226+
const grabHTML = lift<{ result: string }, string | undefined>(({ result }) => {
227+
if (!result) {
220228
return '';
221229
}
222-
const html = partial.match(/```html\n([\s\S]+?)```/)?.[1];
230+
const html = result.match(/```html\n([\s\S]+?)```/)?.[1];
223231
if (!html) {
224-
console.error("No HTML found in text", partial);
232+
console.error("No HTML found in text", result);
225233
return '';
226234
}
227235
return html
228236
});
229237

230-
const genHTMLView = lift(({ pending, partial }) => {
231-
if (!partial) {
238+
const previewHTML = lift(({ pending, partial }) => {
239+
if (!partial || !pending) {
232240
return "";
233241
}
234-
if (pending) {
235-
return `<code>${partial.slice(-1000).replace(/</g, "&lt;").replace(/>/g, "&gt;").slice(-1000)}</code>`;
236-
}
237-
const html = partial.match(/```html\n([\s\S]+?)```/)?.[1];
238-
if (!html) {
239-
console.error("No HTML found in text", partial);
240-
return `<h2>error generating html...</h2><pre>${partial.replace(/>/g, "&gt;")}</pre>`;
241-
}
242-
return html
242+
return partial.slice(-200);
243243
});
244244

245245

@@ -270,10 +270,8 @@ export const iframe = recipe<{
270270

271271
const suggestions = grabSuggestions(generateText(prepSuggestions({ src, prompt, schema })));
272272

273-
// this html is a bit of a mess as changing src triggers suggestions and view (showing streaming)
274-
const { pending: pendingHTML, partial: partialHTML } = generateText(prepHTML({ prompt, schema, lastSrc }));
275-
src = grabHTML({ pending: pendingHTML, partial: partialHTML });
276-
const viewsrc = genHTMLView({ pending: pendingHTML, partial: partialHTML });
273+
// FIXME(ja): this html is a bit of a mess as changing src triggers suggestions and view (showing streaming)
274+
const { result, pending: pendingHTML, partial: partialHTML } = generateText(prepHTML({ prompt, schema, lastSrc }));
277275

278276
let firstSuggestion = getSuggestion({ suggestions, index: 0 });
279277
let secondSuggestion = getSuggestion({ suggestions, index: 1 });
@@ -287,7 +285,8 @@ export const iframe = recipe<{
287285
placeholder="title"
288286
oncommon-input=${updateValue({ value: title })}
289287
></common-input>
290-
<common-iframe src=${viewsrc} $context=${data}></common-iframe>
288+
<common-iframe src=${grabHTML({ result })} $context=${data}></common-iframe>
289+
<pre>${previewHTML({ partial: partialHTML, pending: pendingHTML })}
291290
<details>
292291
<summary>View Data</summary>
293292
<common-input

0 commit comments

Comments
 (0)