(input: string, setInput: (value: string) => void)
| 13 | }; |
| 14 | |
| 15 | const enhancePrompt = async (input: string, setInput: (value: string) => void) => { |
| 16 | setEnhancingPrompt(true); |
| 17 | setPromptEnhanced(false); |
| 18 | |
| 19 | const response = await fetch('/api/enhancer', { |
| 20 | method: 'POST', |
| 21 | body: JSON.stringify({ |
| 22 | message: input, |
| 23 | }), |
| 24 | }); |
| 25 | |
| 26 | const reader = response.body?.getReader(); |
| 27 | |
| 28 | const originalInput = input; |
| 29 | |
| 30 | if (reader) { |
| 31 | const decoder = new TextDecoder(); |
| 32 | |
| 33 | let _input = ''; |
| 34 | let _error; |
| 35 | |
| 36 | try { |
| 37 | setInput(''); |
| 38 | |
| 39 | while (true) { |
| 40 | const { value, done } = await reader.read(); |
| 41 | |
| 42 | if (done) { |
| 43 | break; |
| 44 | } |
| 45 | |
| 46 | _input += decoder.decode(value); |
| 47 | |
| 48 | logger.trace('Set input', _input); |
| 49 | |
| 50 | setInput(_input); |
| 51 | } |
| 52 | } catch (error) { |
| 53 | _error = error; |
| 54 | setInput(originalInput); |
| 55 | } finally { |
| 56 | if (_error) { |
| 57 | logger.error(_error); |
| 58 | } |
| 59 | |
| 60 | setEnhancingPrompt(false); |
| 61 | setPromptEnhanced(true); |
| 62 | |
| 63 | setTimeout(() => { |
| 64 | setInput(_input); |
| 65 | }); |
| 66 | } |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | return { enhancingPrompt, promptEnhanced, enhancePrompt, resetEnhancer }; |
| 71 | } |
no outgoing calls
no test coverage detected