@@ -44,22 +44,17 @@ export async function makeReal(editor: Editor) {
44
44
// It's okay if this is undefined—it will just mean that we'll use the
45
45
// one in the .env file instead.
46
46
const apiKeyFromDangerousApiKeyInput = (
47
- document . body . querySelector (
48
- '#openai_key_risky_but_cool'
49
- ) as HTMLInputElement
47
+ document . body . querySelector ( '#openai_key_risky_but_cool' ) as HTMLInputElement
50
48
) ?. value
51
49
52
50
// make a request to openai. `fetchFromOpenAi` is a next.js server action,
53
51
// so our api key is hidden.
54
- const openAiResponse = await fetchFromOpenAi (
55
- apiKeyFromDangerousApiKeyInput ,
56
- {
57
- model : 'gpt-4-vision-preview' ,
58
- max_tokens : 4096 ,
59
- temperature : 0 ,
60
- messages : prompt ,
61
- }
62
- )
52
+ const openAiResponse = await fetchFromOpenAi ( apiKeyFromDangerousApiKeyInput , {
53
+ model : 'gpt-4-vision-preview' ,
54
+ max_tokens : 4096 ,
55
+ temperature : 0 ,
56
+ messages : prompt ,
57
+ } )
63
58
64
59
// populate the response shape with the html we got back from openai.
65
60
populateResponseShape ( editor , responseShapeId , openAiResponse )
@@ -86,11 +81,11 @@ async function buildPromptForOpenAi(editor: Editor): Promise<GPT4VMessage[]> {
86
81
type : 'text' ,
87
82
text : 'Turn this into a single html file using tailwind.' ,
88
83
} ,
89
- {
90
- // send the text of all selected shapes, so that GPT can use it as a reference (if anything is hard to see)
91
- type : 'text' ,
92
- text : getSelectionAsText ( editor ) ,
93
- }
84
+ {
85
+ // send the text of all selected shapes, so that GPT can use it as a reference (if anything is hard to see)
86
+ type : 'text' ,
87
+ text : getSelectionAsText ( editor ) ,
88
+ } ,
94
89
]
95
90
96
91
// if the user has selected a previous response from gpt-4, include that too. hopefully gpt-4 will
@@ -166,8 +161,7 @@ function getContentOfPreviousResponse(editor: Editor) {
166
161
167
162
function getSelectionAsText ( editor : Editor ) {
168
163
const selectedShapeIds = editor . getSelectedShapeIds ( )
169
- const selectedShapeDescendantIds =
170
- editor . getShapeAndDescendantIds ( selectedShapeIds )
164
+ const selectedShapeDescendantIds = editor . getShapeAndDescendantIds ( selectedShapeIds )
171
165
172
166
const texts = Array . from ( selectedShapeDescendantIds )
173
167
. map ( ( id ) => {
@@ -182,8 +176,9 @@ function getSelectionAsText(editor: Editor) {
182
176
// @ts -expect-error
183
177
return shape . props . text
184
178
}
179
+ return null
185
180
} )
186
- . filter ( ( v ) => v !== null )
181
+ . filter ( ( v ) => v !== null && v !== '' )
187
182
188
183
return texts . join ( '\n' )
189
184
}
0 commit comments