aboutsummaryrefslogtreecommitdiff
path: root/apps/web/app/lib
diff options
context:
space:
mode:
authorDhravya Shah <[email protected]>2025-02-14 12:43:55 -0800
committerDhravya Shah <[email protected]>2025-02-14 12:43:55 -0800
commit186efa4244846bf761c7cf4f9cc5d1087b8f95df (patch)
tree16cca1d69a22ccee586ea0eb64703817ac2330f9 /apps/web/app/lib
parentdocs: remove getting started title (diff)
downloadarchived-supermemory-186efa4244846bf761c7cf4f9cc5d1087b8f95df.tar.xz
archived-supermemory-186efa4244846bf761c7cf4f9cc5d1087b8f95df.zip
delete spaces
Diffstat (limited to 'apps/web/app/lib')
-rw-r--r--apps/web/app/lib/hooks/use-chat-stream.ts2
-rw-r--r--apps/web/app/lib/hooks/use-live-transcript.tsx132
-rw-r--r--apps/web/app/lib/hooks/use-spaces.tsx1
3 files changed, 134 insertions, 1 deletions
diff --git a/apps/web/app/lib/hooks/use-chat-stream.ts b/apps/web/app/lib/hooks/use-chat-stream.ts
index f47df3df..4cf25111 100644
--- a/apps/web/app/lib/hooks/use-chat-stream.ts
+++ b/apps/web/app/lib/hooks/use-chat-stream.ts
@@ -22,7 +22,7 @@ export const useChatStream = (initialMessages: CoreMessage[], initialThreadUuid?
},
keepLastMessageOnError: true,
onError: (e) => {
- alert(`Error in chat: ${e}`);
+ console.error(e);
},
body: {
threadId: threadUuid,
diff --git a/apps/web/app/lib/hooks/use-live-transcript.tsx b/apps/web/app/lib/hooks/use-live-transcript.tsx
new file mode 100644
index 00000000..05c3436a
--- /dev/null
+++ b/apps/web/app/lib/hooks/use-live-transcript.tsx
@@ -0,0 +1,132 @@
+
+import { useState, useEffect, useCallback } from "react";
+import { useQueue } from "@uidotdev/usehooks";
+import { LiveClient, LiveTranscriptionEvents, createClient } from "@deepgram/sdk";
+
+export function useLiveTranscript() {
+
+ const { add, remove, first, size, queue } = useQueue<any>([]);
+ const [apiKey, _] = useState<string | null>("");
+ const [connection, setConnection] = useState<LiveClient | null>(null);
+ const [isListening, setIsListening] = useState(false);
+ const [isLoading, setIsLoading] = useState(true);
+ const [isProcessing, setIsProcessing] = useState(false);
+ const [micOpen, setMicOpen] = useState(false);
+ const [microphone, setMicrophone] = useState<MediaRecorder | null>(null);
+ const [userMedia, setUserMedia] = useState<MediaStream | null>(null);
+ const [caption, setCaption] = useState<string>("");
+ const [status, setStatus] = useState<string>("Not Connected");
+ // Initialize Deepgram connection
+ const initializeConnection = useCallback(() => {
+ if (!apiKey) return null;
+
+ const deepgram = createClient(apiKey);
+ const connection = deepgram.listen.live({
+ model: "nova-3",
+ language: "en",
+ smart_format: true,
+ interim_results: true,
+ punctuate: true,
+ diarize: true,
+ utterances: true,
+ });
+
+ connection.on(LiveTranscriptionEvents.Open, () => {
+ setStatus("Connected");
+ setIsListening(true);
+ });
+
+ connection.on(LiveTranscriptionEvents.Close, () => {
+ setStatus("Not Connected");
+ setIsListening(false);
+ });
+
+ connection.on(LiveTranscriptionEvents.Error, (error) => {
+ console.error("Deepgram error:", error);
+ setStatus("Error occurred");
+ });
+
+ connection.on(LiveTranscriptionEvents.Transcript, (data) => {
+ const transcript = data.channel.alternatives[0].transcript;
+ if (data.is_final) {
+ if (transcript && transcript.trim() !== "") {
+ setCaption((prev) => prev + " " + transcript);
+ }
+ }
+ });
+
+ return connection;
+ }, [apiKey]);
+
+ const toggleMicrophone = useCallback(async () => {
+ if (microphone && userMedia) {
+ setUserMedia(null);
+ setMicrophone(null);
+ microphone.stop();
+ if (connection) {
+ connection.finish();
+ setConnection(null);
+ }
+ } else {
+ const userMedia = await navigator.mediaDevices.getUserMedia({
+ audio: true,
+ });
+
+ const microphone = new MediaRecorder(userMedia);
+ microphone.start(250);
+
+ microphone.onstart = () => {
+ setMicOpen(true);
+ // Create new connection when starting microphone
+ const newConnection = initializeConnection();
+ if (newConnection) {
+ setConnection(newConnection);
+ }
+ };
+
+ microphone.onstop = () => {
+ setMicOpen(false);
+ };
+
+ microphone.ondataavailable = (e) => {
+ add(e.data);
+ };
+
+ setUserMedia(userMedia);
+ setMicrophone(microphone);
+ }
+ }, [add, microphone, userMedia, connection, initializeConnection]);
+
+ useEffect(() => {
+ setIsLoading(false);
+ }, []);
+
+ useEffect(() => {
+ const processQueue = async () => {
+ if (size > 0 && !isProcessing && isListening && connection) {
+ setIsProcessing(true);
+ try {
+ const blob = first;
+ if (blob) {
+ connection.send(blob);
+ }
+ remove();
+ } catch (error) {
+ console.error("Error processing audio:", error);
+ }
+ setIsProcessing(false);
+ }
+ };
+
+ const interval = setInterval(processQueue, 100);
+ return () => clearInterval(interval);
+ }, [connection, queue, remove, first, size, isProcessing, isListening]);
+
+ return {
+ toggleMicrophone,
+ caption,
+ status,
+ isListening,
+ isLoading,
+ };
+} \ No newline at end of file
diff --git a/apps/web/app/lib/hooks/use-spaces.tsx b/apps/web/app/lib/hooks/use-spaces.tsx
index 827c7c79..14ffb56c 100644
--- a/apps/web/app/lib/hooks/use-spaces.tsx
+++ b/apps/web/app/lib/hooks/use-spaces.tsx
@@ -7,6 +7,7 @@ export type ExtraSpaceMetaData = {
canRead: boolean;
canEdit: boolean;
isOwner: boolean;
+ isPublic: boolean;
};
owner: {
id: string;