Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store session to indexeddb so we can use it again #33

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/components/chat/Conversation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 (
<div className="conversation-main">
{
Expand Down
4 changes: 2 additions & 2 deletions src/components/chat/Ticket.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export default function Ticket({ title, uid, selectChat, is_selected }) {
export default function Ticket({ title, info, selectChat, is_selected }) {
return (
<div
className={`ticket clickable${is_selected ? " selected":""}`}
onClick={()=>selectChat(uid)}
onClick={()=>selectChat(info)}
>
{ title }
</div>
Expand Down
13 changes: 7 additions & 6 deletions src/components/chat/Tickets.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(()=>{
Expand All @@ -51,9 +52,9 @@ export default function Tickets({selectChat, current_chat}) {
return (
<Ticket
key={`ticket-${title}-${uid}`}
title={title} uid={uid}
title={title} info={elem}
selectChat={selectChat}
is_selected={current_chat && uid === current_chat}
is_selected={current_chat.uid && uid === current_chat.uid}
/>
)
}) }
Expand Down
4 changes: 2 additions & 2 deletions src/components/chat/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import Conversation from "./Conversation";

export default function Chat() {

const [chat, selectChat] = useState(null);
const [chat, selectChat] = useState({});

return (
<div className="chat">
<Tickets selectChat={selectChat} current_chat={chat} />
<Conversation uid={chat} />
<Conversation uid={chat.uid} client={chat.client} />
</div>
)
}
7 changes: 7 additions & 0 deletions src/utils/idb/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,12 @@ export const versions = [
{ name: 'json' }
]
}
},
{
'chat-history': {
columns: [
{ 'name': "client" }
]
}
}
]
15 changes: 13 additions & 2 deletions src/utils/workers/aws-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,32 @@ 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;
}

/**
* @type {BedrockRuntimeClient?}
*/
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;
Expand Down