Skip to content

Commit d86bcc7

Browse files
committed
store session to indexeddb so we can use it again
Signed-off-by: cbh778899 <[email protected]>
1 parent 894ba5c commit d86bcc7

File tree

6 files changed

+47
-14
lines changed

6 files changed

+47
-14
lines changed

src/components/chat/Conversation.jsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { FileImageFill, FileTextFill, Paperclip, Send, StopCircleFill } from 're
44
import useIDB from "../../utils/idb";
55
import { isModelLoaded, loadModel } from '../../utils/workers/worker'
66
import { getCompletionFunctions } from "../../utils/workers";
7+
import { setClient } from "../../utils/workers/aws-worker";
78

8-
export default function Conversation({ uid }) {
9+
export default function Conversation({ uid, client }) {
910

1011
const [conversation, setConversation] = useState([]);
1112
const [message, setMessage] = useState('');
@@ -92,7 +93,7 @@ export default function Conversation({ uid }) {
9293
content: new Uint8Array(await upload_file.arrayBuffer()),
9394
format: upload_file.name.split('.').pop().toLowerCase()
9495
}
95-
if(!is_img) file_obj.name = upload_file.name;
96+
if(!is_img) file_obj.name = upload_file.name.split('.').slice(0, -1).join('_');
9697
user_message[
9798
is_img ? 'image' : 'document'
9899
] = file_obj;
@@ -126,6 +127,19 @@ export default function Conversation({ uid }) {
126127
})
127128
}, [conversation, pending_message])
128129

130+
useEffect(()=>{
131+
if(!chat_functions.current) return;
132+
133+
if(chat_functions.current.platform === 'AWS') {
134+
(async function() {
135+
if(await setClient(client)) {
136+
await idb.updateOne('chat-history', {client}, [{uid}])
137+
}
138+
})()
139+
}
140+
// eslint-disable-next-line
141+
}, [chat_functions, client])
142+
129143
return (
130144
<div className="conversation-main">
131145
{

src/components/chat/Ticket.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
export default function Ticket({ title, uid, selectChat, is_selected }) {
1+
export default function Ticket({ title, info, selectChat, is_selected }) {
22
return (
33
<div
44
className={`ticket clickable${is_selected ? " selected":""}`}
5-
onClick={()=>selectChat(uid)}
5+
onClick={()=>selectChat(info)}
66
>
77
{ title }
88
</div>

src/components/chat/Tickets.jsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ export default function Tickets({selectChat, current_chat}) {
2121
title: 'New Conversation',
2222
createdAt: timestamp,
2323
updatedAt: timestamp,
24-
uid: genRandomID()
24+
uid: genRandomID(),
25+
client: null
2526
}
2627
)
2728
const new_conv_info = await idb.getByID('chat-history', conv_id);
2829
new_conv_info &&
2930
setTickets([
30-
...tickets,
31-
new_conv_info
31+
new_conv_info,
32+
...tickets
3233
])
33-
selectChat(new_conv_info.uid)
34+
selectChat(new_conv_info)
3435
}
3536

3637
useEffect(()=>{
@@ -51,9 +52,9 @@ export default function Tickets({selectChat, current_chat}) {
5152
return (
5253
<Ticket
5354
key={`ticket-${title}-${uid}`}
54-
title={title} uid={uid}
55+
title={title} info={elem}
5556
selectChat={selectChat}
56-
is_selected={current_chat && uid === current_chat}
57+
is_selected={current_chat.uid && uid === current_chat.uid}
5758
/>
5859
)
5960
}) }

src/components/chat/index.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import Conversation from "./Conversation";
44

55
export default function Chat() {
66

7-
const [chat, selectChat] = useState(null);
7+
const [chat, selectChat] = useState({});
88

99
return (
1010
<div className="chat">
1111
<Tickets selectChat={selectChat} current_chat={chat} />
12-
<Conversation uid={chat} />
12+
<Conversation uid={chat.uid} client={chat.client} />
1313
</div>
1414
)
1515
}

src/utils/idb/settings.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,12 @@ export const versions = [
3131
{ name: 'json' }
3232
]
3333
}
34+
},
35+
{
36+
'chat-history': {
37+
columns: [
38+
{ 'name': "client" }
39+
]
40+
}
3441
}
3542
]

src/utils/workers/aws-worker.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,32 @@ export async function getCredentials(json_credentials = null) {
1717
}
1818

1919
export async function storeCredentials(credentials, all_filled, enabled = false) {
20-
const update_result = await instance.updateByID('credentials', 'AWS', {json: JSON.stringify(credentials)})
20+
const update_result = await instance.updateByID('credentials', 'AWS', {json: credentials})
2121
if(all_filled && enabled) await initBedrockClient();
2222
return !!update_result
2323
}
2424

2525
export async function getJSONCredentials() {
2626
const record = await instance.getByID('credentials', 'AWS', ['json']);
27-
return (record && record.json ? JSON.parse(record.json) : null);
27+
if(!record) return null;
28+
return record.json || null;
2829
}
2930

3031
/**
3132
* @type {BedrockRuntimeClient?}
3233
*/
3334
let bedrock_client = null;
3435

36+
export async function setClient(client) {
37+
if(!client) {
38+
await initBedrockClient();
39+
return bedrock_client;
40+
} else {
41+
bedrock_client = client;
42+
return null;
43+
}
44+
}
45+
3546
export async function initBedrockClient() {
3647
const credentials = await getJSONCredentials();
3748
if(!credentials) return false;

0 commit comments

Comments
 (0)