From d86bcc70dde623ec1f5569f5c214906cb4b8c63c Mon Sep 17 00:00:00 2001 From: cbh778899 Date: Mon, 16 Sep 2024 14:55:35 +1000 Subject: [PATCH] store session to indexeddb so we can use it again Signed-off-by: cbh778899 --- src/components/chat/Conversation.jsx | 18 ++++++++++++++++-- src/components/chat/Ticket.jsx | 4 ++-- src/components/chat/Tickets.jsx | 13 +++++++------ src/components/chat/index.jsx | 4 ++-- src/utils/idb/settings.js | 7 +++++++ src/utils/workers/aws-worker.js | 15 +++++++++++++-- 6 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/components/chat/Conversation.jsx b/src/components/chat/Conversation.jsx index bcb30a6..792103f 100644 --- a/src/components/chat/Conversation.jsx +++ b/src/components/chat/Conversation.jsx @@ -4,8 +4,9 @@ import { FileImageFill, FileTextFill, Paperclip, Send, StopCircleFill } from 're import useIDB from "../../utils/idb"; import { isModelLoaded, loadModel } from '../../utils/workers/worker' import { getCompletionFunctions } from "../../utils/workers"; +import { setClient } from "../../utils/workers/aws-worker"; -export default function Conversation({ uid }) { +export default function Conversation({ uid, client }) { const [conversation, setConversation] = useState([]); const [message, setMessage] = useState(''); @@ -92,7 +93,7 @@ export default function Conversation({ uid }) { content: new Uint8Array(await upload_file.arrayBuffer()), format: upload_file.name.split('.').pop().toLowerCase() } - if(!is_img) file_obj.name = upload_file.name; + if(!is_img) file_obj.name = upload_file.name.split('.').slice(0, -1).join('_'); user_message[ is_img ? 'image' : 'document' ] = file_obj; @@ -126,6 +127,19 @@ export default function Conversation({ uid }) { }) }, [conversation, pending_message]) + useEffect(()=>{ + if(!chat_functions.current) return; + + if(chat_functions.current.platform === 'AWS') { + (async function() { + if(await setClient(client)) { + await idb.updateOne('chat-history', {client}, [{uid}]) + } + })() + } + // eslint-disable-next-line + }, [chat_functions, client]) + return (
{ diff --git a/src/components/chat/Ticket.jsx b/src/components/chat/Ticket.jsx index d2e47fe..dd8afe8 100644 --- a/src/components/chat/Ticket.jsx +++ b/src/components/chat/Ticket.jsx @@ -1,8 +1,8 @@ -export default function Ticket({ title, uid, selectChat, is_selected }) { +export default function Ticket({ title, info, selectChat, is_selected }) { return (
selectChat(uid)} + onClick={()=>selectChat(info)} > { title }
diff --git a/src/components/chat/Tickets.jsx b/src/components/chat/Tickets.jsx index 9ecfdc5..3265434 100644 --- a/src/components/chat/Tickets.jsx +++ b/src/components/chat/Tickets.jsx @@ -21,16 +21,17 @@ export default function Tickets({selectChat, current_chat}) { title: 'New Conversation', createdAt: timestamp, updatedAt: timestamp, - uid: genRandomID() + uid: genRandomID(), + client: null } ) const new_conv_info = await idb.getByID('chat-history', conv_id); new_conv_info && setTickets([ - ...tickets, - new_conv_info + new_conv_info, + ...tickets ]) - selectChat(new_conv_info.uid) + selectChat(new_conv_info) } useEffect(()=>{ @@ -51,9 +52,9 @@ export default function Tickets({selectChat, current_chat}) { return ( ) }) } diff --git a/src/components/chat/index.jsx b/src/components/chat/index.jsx index e461494..b2d2691 100644 --- a/src/components/chat/index.jsx +++ b/src/components/chat/index.jsx @@ -4,12 +4,12 @@ import Conversation from "./Conversation"; export default function Chat() { - const [chat, selectChat] = useState(null); + const [chat, selectChat] = useState({}); return (
- +
) } \ No newline at end of file diff --git a/src/utils/idb/settings.js b/src/utils/idb/settings.js index 47c3a2b..17bf3de 100644 --- a/src/utils/idb/settings.js +++ b/src/utils/idb/settings.js @@ -31,5 +31,12 @@ export const versions = [ { name: 'json' } ] } + }, + { + 'chat-history': { + columns: [ + { 'name': "client" } + ] + } } ] \ No newline at end of file diff --git a/src/utils/workers/aws-worker.js b/src/utils/workers/aws-worker.js index 76657fd..1e9c148 100644 --- a/src/utils/workers/aws-worker.js +++ b/src/utils/workers/aws-worker.js @@ -17,14 +17,15 @@ export async function getCredentials(json_credentials = null) { } export async function storeCredentials(credentials, all_filled, enabled = false) { - const update_result = await instance.updateByID('credentials', 'AWS', {json: JSON.stringify(credentials)}) + const update_result = await instance.updateByID('credentials', 'AWS', {json: credentials}) if(all_filled && enabled) await initBedrockClient(); return !!update_result } export async function getJSONCredentials() { const record = await instance.getByID('credentials', 'AWS', ['json']); - return (record && record.json ? JSON.parse(record.json) : null); + if(!record) return null; + return record.json || null; } /** @@ -32,6 +33,16 @@ export async function getJSONCredentials() { */ let bedrock_client = null; +export async function setClient(client) { + if(!client) { + await initBedrockClient(); + return bedrock_client; + } else { + bedrock_client = client; + return null; + } +} + export async function initBedrockClient() { const credentials = await getJSONCredentials(); if(!credentials) return false;