Skip to content

Commit e023a98

Browse files
committed
refac: submit suggestion prompt by default
1 parent 6dc0df2 commit e023a98

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
lines changed

src/lib/components/chat/Chat.svelte

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,12 @@
202202
203203
if (type === 'prompt') {
204204
// Handle prompt selection
205-
messageInput?.setText(data);
205+
messageInput?.setText(data, async () => {
206+
if (!($settings?.insertSuggestionPrompt ?? false)) {
207+
await tick();
208+
submitPrompt(prompt);
209+
}
210+
});
206211
}
207212
};
208213

src/lib/components/chat/MessageInput.svelte

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
export let codeInterpreterEnabled = false;
102102
103103
let showInputVariablesModal = false;
104+
let inputVariablesModalCallback = (variableValues) => {};
104105
let inputVariables = {};
105106
let inputVariableValues = {};
106107
@@ -122,11 +123,24 @@
122123
codeInterpreterEnabled
123124
});
124125
125-
const inputVariableHandler = async (text: string) => {
126+
const inputVariableHandler = async (text: string): Promise<string> => {
126127
inputVariables = extractInputVariables(text);
127-
if (Object.keys(inputVariables).length > 0) {
128-
showInputVariablesModal = true;
128+
129+
// No variables? return the original text immediately.
130+
if (Object.keys(inputVariables).length === 0) {
131+
return text;
129132
}
133+
134+
// Show modal and wait for the user's input.
135+
showInputVariablesModal = true;
136+
return await new Promise<string>((resolve) => {
137+
inputVariablesModalCallback = (variableValues) => {
138+
inputVariableValues = { ...inputVariableValues, ...variableValues };
139+
replaceVariables(inputVariableValues);
140+
showInputVariablesModal = false;
141+
resolve(text);
142+
};
143+
});
130144
};
131145
132146
const textVariableHandler = async (text: string) => {
@@ -244,7 +258,6 @@
244258
text = text.replaceAll('{{CURRENT_WEEKDAY}}', weekday);
245259
}
246260
247-
inputVariableHandler(text);
248261
return text;
249262
};
250263
@@ -280,7 +293,7 @@
280293
}
281294
};
282295
283-
export const setText = async (text?: string) => {
296+
export const setText = async (text?: string, cb?: (text: string) => void) => {
284297
const chatInput = document.getElementById('chat-input');
285298
286299
if (chatInput) {
@@ -296,6 +309,10 @@
296309
chatInput.focus();
297310
chatInput.dispatchEvent(new Event('input'));
298311
}
312+
313+
text = await inputVariableHandler(text);
314+
await tick();
315+
if (cb) await cb(text);
299316
}
300317
};
301318
@@ -758,11 +775,7 @@
758775
}
759776
];
760777
};
761-
reader.readAsDataURL(
762-
file['type'] === 'image/heic'
763-
? await convertHeicToJpeg(file)
764-
: file
765-
);
778+
reader.readAsDataURL(file['type'] === 'image/heic' ? await convertHeicToJpeg(file) : file);
766779
} else {
767780
uploadFileHandler(file);
768781
}
@@ -868,10 +881,7 @@
868881
<InputVariablesModal
869882
bind:show={showInputVariablesModal}
870883
variables={inputVariables}
871-
onSave={(variableValues) => {
872-
inputVariableValues = { ...inputVariableValues, ...variableValues };
873-
replaceVariables(inputVariableValues);
874-
}}
884+
onSave={inputVariablesModalCallback}
875885
/>
876886

877887
{#if loaded}

src/lib/components/chat/Settings/Interface.svelte

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
5050
let largeTextAsFile = false;
5151
52+
let insertSuggestionPrompt = false;
5253
let keepFollowUpPrompts = false;
5354
let insertFollowUpPrompt = false;
5455
@@ -200,6 +201,7 @@
200201
insertPromptAsRichText = $settings?.insertPromptAsRichText ?? false;
201202
promptAutocomplete = $settings?.promptAutocomplete ?? false;
202203
204+
insertSuggestionPrompt = $settings?.insertSuggestionPrompt ?? false;
203205
keepFollowUpPrompts = $settings?.keepFollowUpPrompts ?? false;
204206
insertFollowUpPrompt = $settings?.insertFollowUpPrompt ?? false;
205207
@@ -697,6 +699,25 @@
697699
</div>
698700
</div>
699701

702+
<div>
703+
<div class=" py-0.5 flex w-full justify-between">
704+
<div id="insert-suggestion-prompt-label" class=" self-center text-xs">
705+
{$i18n.t('Insert Suggestion Prompt to Input')}
706+
</div>
707+
708+
<div class="flex items-center gap-2 p-1">
709+
<Switch
710+
ariaLabelledbyId="insert-suggestion-prompt-label"
711+
tooltip={true}
712+
bind:state={insertSuggestionPrompt}
713+
on:change={() => {
714+
saveSettings({ insertSuggestionPrompt });
715+
}}
716+
/>
717+
</div>
718+
</div>
719+
</div>
720+
700721
<div>
701722
<div class=" py-0.5 flex w-full justify-between">
702723
<div id="keep-follow-up-prompts-label" class=" self-center text-xs">

0 commit comments

Comments
 (0)