Skip to content

Commit

Permalink
Merge pull request #18 from SkywardAI/new-functions
Browse files Browse the repository at this point in the history
chat function implemented
  • Loading branch information
cbh778899 authored Jul 7, 2024
2 parents 36bc4ca + f452a72 commit f63787f
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 38 deletions.
2 changes: 1 addition & 1 deletion components/chat-page/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const {
selectConversation, startNewConversation,
componetDismount:conversationDismount, componentReMount: conversationRemount
} = useConversation(c=>{
if(c.id === 'not_selected' || c.id === last_selected_id) return;
if(c.id === 'not_selected' || c.id === null || c.id === last_selected_id) return;
last_selected_id = c.id;

const last_selected = document.querySelector('#chat-history .tickets-list .ticket.selected')
Expand Down
15 changes: 7 additions & 8 deletions components/info-page.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { VERSION } from '../settings.js'
import capitalizeFirstLetter from '../tools/capitalizeFirstLetter.js';
import request from '../tools/request.js';

export default function createInfoPage() {
const icon = document.getElementById('sidebar-icon-info');
Expand All @@ -13,22 +14,20 @@ export default function createInfoPage() {
}

async function updateVersions() {
await new Promise(s=>setTimeout(s, 5000));


const query_versions = await (await request('version')).json();

const versions = {
rebel: VERSION,
kirin: '0.1.8',
some: '1.2.3',
random: '3.4.5',
version: '4.5.6'
rebel: `v${VERSION}`,
...query_versions
};

const all_versions_elem = document.getElementById('all-versions');
all_versions_elem.innerHTML = '';

for(const key in versions) {
all_versions_elem.insertAdjacentHTML('beforeend',
`<div class='version-elem'>${capitalizeFirstLetter(key)}: <span>v${versions[key]}</span></div>`
`<div class='version-elem'>${capitalizeFirstLetter(key)}: <span>${versions[key]}</span></div>`
)
}
}
21 changes: 6 additions & 15 deletions global/useConversation.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import apiAddress from "../tools/apiAddress.js";
import request from "../tools/request.js";
import createHook from "./createHook.js";
import useHistory from "./useHistory.js";
import useSessionId from "./useSessionId.js";

const defaultConversationSetting = {
id: null,
Expand All @@ -16,16 +15,11 @@ let currentConversation = {
...defaultConversationSetting,
id: 'not_selected'
};
let currentSession;


const { onmount, remount, dismount, updateAll } = createHook();
const { addHistory:addUserHistoryTicket } = useHistory(null);

useSessionId(id=>{
currentSession = id;
})

export default function useConversation(updated) {
const mount_key = onmount(updated);

Expand All @@ -42,31 +36,28 @@ export default function useConversation(updated) {
async function sendMessage(message) {
addHistory([{ type: 'out', message }]);

const { sessionUuid, message:botResponse } = await (await fetch(apiAddress('chat'), {
const { sessionUuid, message:botResponse } =
await (await request('chat', {
method: 'POST',
signal: AbortSignal.timeout(5000),
headers: {
authenticated: currentSession
},
body: {
sessionUuid: currentConversation.id,
sessionUuid: currentConversation.id || "uuid",
message
}
})).json()

addHistory([{type: 'in', message: botResponse}])
if(currentConversation.id === null) {
addUserHistoryTicket({
id: sessionUuid, name: 'New Conversation',
createdAt: new Date().toUTCString()
})
currentConversation.id = sessionUuid;
}
addHistory([{type: 'in', message: botResponse}])
}

async function selectConversation(id, settings = null) {
const conversation_history = [];
await (await fetch(apiAddress(`chat/history/${id}`)))
await (await request(`chat/history/${id}`))
.json().forEach(({type, message})=>{
conversation_history.push({type, message});
})
Expand Down
10 changes: 4 additions & 6 deletions global/useHistory.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import apiAddress from "../tools/apiAddress.js";
import request from "../tools/request.js";
import createHook from "./createHook.js";
import useSessionId from "./useSessionId.js";

Expand All @@ -23,11 +23,9 @@ export default function useHistory(updated) {
const mount_key = onmount(updated)

async function requestUpdateHistory() {
const chat_history = await (await fetch(apiAddress('chat/'), {
headers: {
authenticated: currentSession
}
})).json();
if(!currentSession) return;

const chat_history = await (await request('chat')).json();

history.length = 0;
chat_history.forEach(({sessionUuid, name, createdAt}) => {
Expand Down
4 changes: 2 additions & 2 deletions global/useSessionId.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import apiAddress from "../tools/apiAddress.js";
import request from "../tools/request.js";
import createHook from "./createHook.js";

let currentSession = null, init = false;
Expand All @@ -9,7 +9,7 @@ export default function useSessionId(updated) {
const mount_key = onmount(updated)

async function genSession() {
const { token } = await (await fetch(apiAddress('auth/token'), { mode: 'no-cors' })).json();
const { token } = await (await request('auth/token')).json()
currentSession = token;
updateAll(currentSession);
}
Expand Down
2 changes: 1 addition & 1 deletion settings.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const VERSION = '0.0.1'
export const VERSION = '0.1.0'
export const LOCAL_API_ADDRESS = 'http://localhost:8000/api'
5 changes: 0 additions & 5 deletions tools/apiAddress.js

This file was deleted.

27 changes: 27 additions & 0 deletions tools/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { LOCAL_API_ADDRESS } from "../settings.js";
import useSessionId from "../global/useSessionId.js";

let session_id = '';
useSessionId(id=>{session_id = id});

export default function request(url, options={}) {
const headers = {
Accept: 'application/json'
}
if(options.method && options.method === 'POST') {
headers['Content-Type'] = 'application/json'
}
if(/^(chat|accounts).*$/.test(url) && session_id) {
headers['Authorization'] = `Bearer ${session_id}`
}

if(options.body && typeof options.body === 'object') {
options.body = JSON.stringify(options.body)
}

url = `${LOCAL_API_ADDRESS}/${url}`
return fetch(url, {
headers,
...options
})
}

0 comments on commit f63787f

Please sign in to comment.