mirror of
https://github.com/harivansh-afk/asap.it.git
synced 2026-04-19 02:01:33 +00:00
first commit
This commit is contained in:
commit
1cdbffff09
200 changed files with 30007 additions and 0 deletions
86
app/lib/hooks/usePromptEnhancer.ts
Normal file
86
app/lib/hooks/usePromptEnhancer.ts
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
import { useState } from 'react';
|
||||
import type { ProviderInfo } from '~/types/model';
|
||||
import { createScopedLogger } from '~/utils/logger';
|
||||
|
||||
const logger = createScopedLogger('usePromptEnhancement');
|
||||
|
||||
export function usePromptEnhancer() {
|
||||
const [enhancingPrompt, setEnhancingPrompt] = useState(false);
|
||||
const [promptEnhanced, setPromptEnhanced] = useState(false);
|
||||
|
||||
const resetEnhancer = () => {
|
||||
setEnhancingPrompt(false);
|
||||
setPromptEnhanced(false);
|
||||
};
|
||||
|
||||
const enhancePrompt = async (
|
||||
input: string,
|
||||
setInput: (value: string) => void,
|
||||
model: string,
|
||||
provider: ProviderInfo,
|
||||
apiKeys?: Record<string, string>,
|
||||
) => {
|
||||
setEnhancingPrompt(true);
|
||||
setPromptEnhanced(false);
|
||||
|
||||
const requestBody: any = {
|
||||
message: input,
|
||||
model,
|
||||
provider,
|
||||
};
|
||||
|
||||
if (apiKeys) {
|
||||
requestBody.apiKeys = apiKeys;
|
||||
}
|
||||
|
||||
const response = await fetch('/api/enhancer', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
const reader = response.body?.getReader();
|
||||
|
||||
const originalInput = input;
|
||||
|
||||
if (reader) {
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
let _input = '';
|
||||
let _error;
|
||||
|
||||
try {
|
||||
setInput('');
|
||||
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
|
||||
_input += decoder.decode(value);
|
||||
|
||||
logger.trace('Set input', _input);
|
||||
|
||||
setInput(_input);
|
||||
}
|
||||
} catch (error) {
|
||||
_error = error;
|
||||
setInput(originalInput);
|
||||
} finally {
|
||||
if (_error) {
|
||||
logger.error(_error);
|
||||
}
|
||||
|
||||
setEnhancingPrompt(false);
|
||||
setPromptEnhanced(true);
|
||||
|
||||
setTimeout(() => {
|
||||
setInput(_input);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return { enhancingPrompt, promptEnhanced, enhancePrompt, resetEnhancer };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue