ui: move kai service out for ease of code splitting

This commit is contained in:
nick-delirium 2025-04-28 16:42:55 +02:00
parent 314850939d
commit c29fe3b7df
No known key found for this signature in database
GPG key ID: 93ABD695DF5FDBA0
6 changed files with 96 additions and 92 deletions

View file

@ -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) => {

View file

@ -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<boolean> => {
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<number> => {
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()
}
}

View file

@ -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 {

View file

@ -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({

View file

@ -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<boolean> => {
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<number> => {
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()
}
}

View file

@ -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,
];