initial commit: rocket.new export of broadcastbeat

This commit is contained in:
Ryan Salazar
2026-05-07 16:39:17 +00:00
commit 70aa1ad46e
302 changed files with 60710 additions and 0 deletions

52
src/lib/hooks/useChat.ts Normal file
View File

@@ -0,0 +1,52 @@
'use client';
import { useState, useCallback } from 'react';
import { getChatCompletion, getStreamingChatCompletion } from '@/lib/ai/chatCompletion';
export function useChat(provider: string, model: string, streaming: boolean = true) {
const [response, setResponse] = useState('');
const [fullResponse, setFullResponse] = useState<any>(null);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<Error | null>(null);
const sendMessage = useCallback(
async (messages: object[], parameters: object = {}) => {
setResponse('');
setFullResponse(streaming ? [] : null);
setIsLoading(true);
setError(null);
try {
if (streaming) {
await getStreamingChatCompletion(
provider,
model,
messages,
(chunk) => {
setFullResponse((prev: any[]) => [...prev, chunk]);
const content = chunk?.choices?.[0]?.delta?.content;
if (content) setResponse((prev) => prev + content);
},
() => setIsLoading(false),
(err) => {
setError(err);
setIsLoading(false);
},
parameters
);
} else {
const result = await getChatCompletion(provider, model, messages, parameters);
setFullResponse(result);
setResponse(result?.choices?.[0]?.message?.content || '');
setIsLoading(false);
}
} catch (err) {
setError(err instanceof Error ? err : new Error('Unknown error'));
setIsLoading(false);
}
},
[provider, model, streaming]
);
return { response, fullResponse, isLoading, error, sendMessage };
}