mirror of
https://github.com/temporal-community/temporal-ai-agent.git
synced 2026-03-16 22:48:09 +01:00
refactor using Cursor
This commit is contained in:
75
frontend/src/services/api.js
Normal file
75
frontend/src/services/api.js
Normal file
@@ -0,0 +1,75 @@
|
||||
const API_BASE_URL = 'http://127.0.0.1:8000';
|
||||
|
||||
class ApiError extends Error {
|
||||
constructor(message, status) {
|
||||
super(message);
|
||||
this.status = status;
|
||||
this.name = 'ApiError';
|
||||
}
|
||||
}
|
||||
|
||||
async function handleResponse(response) {
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
throw new ApiError(
|
||||
errorData.message || 'An error occurred',
|
||||
response.status
|
||||
);
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export const apiService = {
|
||||
async getConversationHistory() {
|
||||
try {
|
||||
const res = await fetch(`${API_BASE_URL}/get-conversation-history`);
|
||||
return handleResponse(res);
|
||||
} catch (error) {
|
||||
throw new ApiError(
|
||||
'Failed to fetch conversation history',
|
||||
error.status || 500
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
async sendMessage(message) {
|
||||
if (!message?.trim()) {
|
||||
throw new ApiError('Message cannot be empty', 400);
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(
|
||||
`${API_BASE_URL}/send-prompt?prompt=${encodeURIComponent(message)}`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
}
|
||||
);
|
||||
return handleResponse(res);
|
||||
} catch (error) {
|
||||
throw new ApiError(
|
||||
'Failed to send message',
|
||||
error.status || 500
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
async confirm() {
|
||||
try {
|
||||
const res = await fetch(`${API_BASE_URL}/confirm`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
return handleResponse(res);
|
||||
} catch (error) {
|
||||
throw new ApiError(
|
||||
'Failed to confirm action',
|
||||
error.status || 500
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user