From c29fe3b7dfa3585e46c67889d28db0be489580a2 Mon Sep 17 00:00:00 2001 From: nick-delirium Date: Mon, 28 Apr 2025 16:42:55 +0200 Subject: [PATCH] ui: move kai service out for ease of code splitting --- frontend/app/components/Kai/KaiChat.tsx | 1 - frontend/app/components/Kai/KaiService.ts | 92 +++++++++++++++++++ frontend/app/components/Kai/KaiStore.ts | 2 +- .../app/components/Kai/components/ChatMsg.tsx | 1 - frontend/app/services/AiService.ts | 89 ------------------ frontend/app/services/index.ts | 3 + 6 files changed, 96 insertions(+), 92 deletions(-) create mode 100644 frontend/app/components/Kai/KaiService.ts diff --git a/frontend/app/components/Kai/KaiChat.tsx b/frontend/app/components/Kai/KaiChat.tsx index fb7972198..6f9a42cdc 100644 --- a/frontend/app/components/Kai/KaiChat.tsx +++ b/frontend/app/components/Kai/KaiChat.tsx @@ -123,7 +123,6 @@ function ChatsModal({ onSelect }: { onSelect: (threadId: string, title: string) queryKey: ['kai', 'chats'], queryFn: () => aiService.getKaiChats(userId, projectId), staleTime: 1000 * 60, - cacheTime: 1000 * 60 * 5, }); const onDelete = async (id: string) => { diff --git a/frontend/app/components/Kai/KaiService.ts b/frontend/app/components/Kai/KaiService.ts new file mode 100644 index 000000000..b2fe4a6c9 --- /dev/null +++ b/frontend/app/components/Kai/KaiService.ts @@ -0,0 +1,92 @@ +import AiService from "@/services/AiService"; + +export default class KaiService extends AiService { + getKaiChats = async (userId: string, projectId: string): Promise<{ title: string, threadId: string }[]> => { + // const r = await this.client.get('/kai/PROJECT_ID/chats'); + const jwt = window.env.KAI_TESTING // this.client.getJwt() + const r = await fetch(`http://localhost:8700/kai/${projectId}/chats?user_id=${userId}`, { + headers: new Headers({ + Accept: 'application/json', + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${jwt}` + }), + }); + if (!r.ok) { + throw new Error('Failed to fetch chats'); + } + const data = await r.json(); + return data; + } + + deleteKaiChat = async (projectId: string, userId: string, threadId: string): Promise => { + const jwt = window.env.KAI_TESTING // this.client.getJwt() + const r = await fetch(`http://localhost:8700/kai/${projectId}/chats/${threadId}?user_id=${userId}`, { + method: 'DELETE', + headers: new Headers({ + Accept: 'application/json', + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${jwt}` + }), + }); + if (!r.ok) { + throw new Error('Failed to delete chat'); + } + return true; + } + + getKaiChat = async (projectId: string, userId: string, threadId: string): Promise<{ role: string, content: string, message_id: any }[]> => { + const jwt = window.env.KAI_TESTING // this.client.getJwt() + const r = await fetch(`http://localhost:8700/kai/${projectId}/chats/${threadId}?user_id=${userId}`, { + method: 'GET', + headers: new Headers({ + Accept: 'application/json', + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${jwt}` + }), + }); + if (!r.ok) { + throw new Error('Failed to fetch chat'); + } + const data = await r.json(); + return data; + } + + createKaiChat = async (projectId: string, userId: string): Promise => { + const jwt = window.env.KAI_TESTING // this.client.getJwt() + const r = await fetch(`http://localhost:8700/kai/${projectId}/chat/new?user_id=${userId}`, { + method: 'GET', + headers: new Headers({ + Accept: 'application/json', + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${jwt}` + }), + }) + if (!r.ok) { + throw new Error('Failed to create chat'); + } + const data = await r.json(); + return data; + } + + feedback = async (positive: boolean | null, messageId: string, projectId: string, userId: string) => { + const jwt = window.env.KAI_TESTING // this.client.getJwt() + const r = await fetch(`http://localhost:8700/kai/${projectId}/messages/feedback`, { + method: 'POST', + headers: new Headers({ + Accept: 'application/json', + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${jwt}` + }), + body: JSON.stringify({ + message_id: messageId, + value: positive, + user_id: userId, + }), + }); + if (!r.ok) { + throw new Error('Failed to send feedback'); + } + + return await r.json() + } +} diff --git a/frontend/app/components/Kai/KaiStore.ts b/frontend/app/components/Kai/KaiStore.ts index 9d6b92fa8..03612bb0c 100644 --- a/frontend/app/components/Kai/KaiStore.ts +++ b/frontend/app/components/Kai/KaiStore.ts @@ -1,6 +1,6 @@ import { makeAutoObservable, runInAction } from 'mobx'; import { BotChunk, ChatManager, Message } from './SocketManager'; -import { aiService } from 'App/services'; +import { kaiService as aiService } from 'App/services'; import { toast } from 'react-toastify'; class KaiStore { diff --git a/frontend/app/components/Kai/components/ChatMsg.tsx b/frontend/app/components/Kai/components/ChatMsg.tsx index 3511b40cd..d95c5d3bf 100644 --- a/frontend/app/components/Kai/components/ChatMsg.tsx +++ b/frontend/app/components/Kai/components/ChatMsg.tsx @@ -3,7 +3,6 @@ import { Icon } from 'UI'; import cn from 'classnames'; import Markdown from 'react-markdown'; import { Loader, ThumbsUp, ThumbsDown, ListRestart } from 'lucide-react'; -import { toast } from 'react-toastify'; import { kaiStore } from '../KaiStore'; export function ChatMsg({ diff --git a/frontend/app/services/AiService.ts b/frontend/app/services/AiService.ts index 4f1d806f5..8d16ed436 100644 --- a/frontend/app/services/AiService.ts +++ b/frontend/app/services/AiService.ts @@ -84,93 +84,4 @@ export default class AiService extends BaseService { const { data } = await r.json(); return data; } - - getKaiChats = async (userId: string, projectId: string): Promise<{ title: string, threadId: string }[]> => { - // const r = await this.client.get('/kai/PROJECT_ID/chats'); - const jwt = window.env.KAI_TESTING // this.client.getJwt() - const r = await fetch(`http://localhost:8700/kai/${projectId}/chats?user_id=${userId}`, { - headers: new Headers({ - Accept: 'application/json', - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${jwt}` - }), - }); - if (!r.ok) { - throw new Error('Failed to fetch chats'); - } - const data = await r.json(); - return data; - } - - deleteKaiChat = async (projectId: string, userId: string, threadId: string): Promise => { - const jwt = window.env.KAI_TESTING // this.client.getJwt() - const r = await fetch(`http://localhost:8700/kai/${projectId}/chats/${threadId}?user_id=${userId}`, { - method: 'DELETE', - headers: new Headers({ - Accept: 'application/json', - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${jwt}` - }), - }); - if (!r.ok) { - throw new Error('Failed to delete chat'); - } - return true; - } - - getKaiChat = async (projectId: string, userId: string, threadId: string): Promise<{ role: string, content: string, message_id: any }[]> => { - const jwt = window.env.KAI_TESTING // this.client.getJwt() - const r = await fetch(`http://localhost:8700/kai/${projectId}/chats/${threadId}?user_id=${userId}`, { - method: 'GET', - headers: new Headers({ - Accept: 'application/json', - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${jwt}` - }), - }); - if (!r.ok) { - throw new Error('Failed to fetch chat'); - } - const data = await r.json(); - return data; - } - - createKaiChat = async (projectId: string, userId: string): Promise => { - const jwt = window.env.KAI_TESTING // this.client.getJwt() - const r = await fetch(`http://localhost:8700/kai/${projectId}/chat/new?user_id=${userId}`, { - method: 'GET', - headers: new Headers({ - Accept: 'application/json', - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${jwt}` - }), - }) - if (!r.ok) { - throw new Error('Failed to create chat'); - } - const data = await r.json(); - return data; - } - - feedback = async (positive: boolean | null, messageId: string, projectId: string, userId: string) => { - const jwt = window.env.KAI_TESTING // this.client.getJwt() - const r = await fetch(`http://localhost:8700/kai/${projectId}/messages/feedback`, { - method: 'POST', - headers: new Headers({ - Accept: 'application/json', - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${jwt}` - }), - body: JSON.stringify({ - message_id: messageId, - value: positive, - user_id: userId, - }), - }); - if (!r.ok) { - throw new Error('Failed to send feedback'); - } - - return await r.json() - } } diff --git a/frontend/app/services/index.ts b/frontend/app/services/index.ts index c352f2c6d..10c2ded87 100644 --- a/frontend/app/services/index.ts +++ b/frontend/app/services/index.ts @@ -25,6 +25,7 @@ import IssueReportsService from './IssueReportsService'; import CustomFieldService from './CustomFieldService'; import IntegrationsService from './IntegrationsService'; import ProjectsService from './ProjectsService'; +import KaiService from '@/components/Kai/KaiService'; export const dashboardService = new DashboardService(); export const metricService = new MetricService(); @@ -52,6 +53,7 @@ export const customFieldService = new CustomFieldService(); export const integrationsService = new IntegrationsService(); export const searchService = new SearchService(); export const projectsService = new ProjectsService(); +export const kaiService = new KaiService(); export const services = [ projectsService, @@ -80,4 +82,5 @@ export const services = [ customFieldService, integrationsService, searchService, + kaiService, ];