Skip to content

Commit

Permalink
fix: auto resize the chatbox #48
Browse files Browse the repository at this point in the history
Signed-off-by: seven <[email protected]>
  • Loading branch information
Blankll committed Apr 27, 2024
1 parent 6810868 commit a188fc6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 83 deletions.
77 changes: 0 additions & 77 deletions src/common/httpClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CustomError } from './customError';
import { Buffer } from 'buffer';
import { lang } from '../lang';
import { Range } from './editor';

const { fetchApi } = window;

Expand Down Expand Up @@ -104,79 +103,3 @@ export const loadHttpClient = (con: {
ssl: con.sslCertVerification,
}),
});

export interface AiClient {
suggest: (fileContent: string, range: Range) => Promise<{ choices: Array<{ text: string }> }>;
}

const MODEL = 'gpt-3.5-turbo-0125';

const OPENAI_API_KEY = import.meta.env.VITE_OPEN_AI_API_KEY as string;

export const loadAiClient = async () => {
const headers = {
'Content-Type': 'application/json',
'OpenAI-Beta': 'assistants=v1',
Authorization: `Bearer ${OPENAI_API_KEY}`,
};
// Step 1: Create an Assistant
const { status, details } = await fetchApi.fetch('https://api.openai.com/v1/assistants', {
method: 'POST',
headers,
payload: JSON.stringify({
instructions: 'You are a personal math tutor. Write and run code to answer math questions.',
name: 'Math Tutor',
tools: [{ type: 'code_interpreter' }],
model: MODEL,
}),
});
if (status !== 200) {
throw new CustomError(status, details);
}
// Step 2: Create a Thread
const { data: thread, status: threadStatus } = await fetchApi.fetch(
`https://api.openai.com/v1/threads`,
{
method: 'POST',
headers,
payload: JSON.stringify({
messages: [
{
role: 'system',
content: 'You are a personal math tutor. Write and run code to answer math questions.',
},
],
}),
},
);

if (threadStatus !== 200) {
throw new CustomError(status, details);
}
const threadId = (thread as { id: string }).id;
return {
suggest: async (fileContent: string, range: Range) => {
// Step 3: Add a Message to the Thread
const { data } = await fetchApi.fetch(
`https://api.openai.com/v1/threads/${threadId}).id}/messages`,
{
method: 'POST',
headers,
payload: JSON.stringify({
messages: [
{
role: 'system',
content: fileContent,
current_line_number: range.startLineNumber,
current_column_number: range.startColumn,
end_line_number: range.endLineNumber,
end_column_number: range.endColumn,
},
],
}),
},
);
return data as { choices: Array<{ text: string }> };
},
} as AiClient;
};
9 changes: 3 additions & 6 deletions src/layout/components/chatbot-box.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ import MarkdownRender from '../../components/MarkdownRender.vue';
const chatStore = useChatStore();
const { sendMessage, fetchChats } = chatStore;
const { chats } = storeToRefs(chatStore);
const chatBot = ref({ active: false });
const chatBotNotification = ref({ enabled: false, level: '', message: '' });
Expand All @@ -97,13 +96,11 @@ const submitMsg = async () => {
};
const msgBoxHeight = ref(449);
const scrollBarStyle = computed(() => `max-height: ${msgBoxHeight.value}px`);
const scrollBarStyle = computed(() => `height: ${msgBoxHeight.value}px`);
const updateHeight = () => {
if (chatBot.value.active) {
const chatMsgContent = document.querySelector('.chat-box-container .message-list-box');
msgBoxHeight.value = chatMsgContent.clientHeight;
}
const chatMsgContent = document.querySelector('.chat-box-container .message-list-box');
msgBoxHeight.value = chatMsgContent.clientHeight;
};
onMounted(() => {
Expand Down

0 comments on commit a188fc6

Please sign in to comment.