Has anyone had any success in using vercel ai streaming on forms with multiple inputs? As far as I can tell, it seems designed for single input, but I was playing with a workaround using a custom handleFormSubmit handler that serves as a wrapper for concatenating and then submitting the input.
The below works, but users need to hit submit twice which is unideal. I've played around with try / finally blocks but it didn't work either. Would love if anyone has any suggestions!
export default function SloganGenerator() {
const [name, setName] = useState("");
const [industry, setIndustry] = useState("");
const { completion, input, handleInputChange, handleSubmit } = useCompletion();
const handleFormSubmit = (e: React.FormEvent<HTMLFormElement>) => {
// Combine the three inputs into one prompt
const concatenatedInput = `Create a slogan for the following business with the name: ${name}, industry: ${industry}`;
// Create a synthetic ChangeEvent object
const syntheticEvent = {
target: {
value: concatenatedInput,
},
} as ChangeEvent<HTMLInputElement>;
console.log("concatenated input was", concatenatedInput)
handleInputChange(syntheticEvent);
console.log("input was", input) // only updates when user hits submit twice
// Submit the form using the SDK's handleSubmit
handleSubmit(e);
console.log("input 2 was", input) // only updates when user hits submit twice
};
return (
<form onSubmit={handleFormSubmit}>
<input
value={name}
placeholder="Business Name..."
onChange={(e) => setName(e.target.value)}
/>
<input
value={industry}
placeholder="Industry..."
onChange={(e) => setIndustry(e.target.value)}
/>
<button type="submit">Submit</button>
<a>{completion}</a>
</form>
);