This repository was archived by the owner on Mar 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchatAPI.js
More file actions
147 lines (136 loc) · 5.04 KB
/
chatAPI.js
File metadata and controls
147 lines (136 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Configuration API 호출 함수들을 관리하는 파일
import { devLog } from '@/app/utils/logger';
import { API_BASE_URL } from '@/app/config.js';
/**
* Creates a new chat session
* @param {Object} params - The chat creation parameters
* @param {string} params.interaction_id - Unique interaction identifier
* @param {string} [params.input_data] - Optional initial message
* @returns {Promise<Object>} A promise that resolves with the chat creation response
*/
export const createNewChat = async ({ interaction_id, input_data = null }) => {
try {
const response = await fetch(`${API_BASE_URL}/api/chat/new`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
workflow_name: "default_mode",
workflow_id: "default_mode",
interaction_id,
input_data
}),
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(`HTTP ${response.status}: ${errorData.detail || 'Unknown error'}`);
}
return await response.json();
} catch (error) {
console.error('Error creating new chat:', error);
throw error;
}
};
/**
* Continues an existing chat session
* @param {Object} params - The chat execution parameters
* @param {string} params.user_input - The user's message
* @param {string} params.interaction_id - The interaction identifier
* @param {string} [params.workflow_id] - Optional workflow ID (defaults to "default_mode")
* @param {string} [params.workflow_name] - Optional workflow name (defaults to "default_mode")
* @returns {Promise<Object>} A promise that resolves with the chat response
*/
export const executeChatMessage = async ({
user_input,
interaction_id,
workflow_id = "default_mode",
workflow_name = "default_mode"
}) => {
try {
const response = await fetch(`${API_BASE_URL}/api/chat/execution`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
user_input,
interaction_id,
workflow_id,
workflow_name
}),
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(`HTTP ${response.status}: ${errorData.detail || 'Unknown error'}`);
}
return await response.json();
} catch (error) {
console.error('Error executing chat message:', error);
throw error;
}
};
/**
* High-level function to handle a complete chat flow
* @param {Object} params - The chat parameters
* @param {string} params.message - The user's message
* @param {string} [params.interaction_id] - Existing interaction ID or will generate new one
* @param {boolean} [params.isNewChat] - Whether this is a new chat session
* @returns {Promise<Object>} A promise that resolves with the chat response
*/
export const sendMessage = async ({
message,
interaction_id = null,
isNewChat = false
}) => {
try {
// Generate interaction ID if not provided
const chatInteractionId = interaction_id || `chat_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
if (isNewChat) {
// Create new chat session with initial message
const result = await createNewChat({
interaction_id: chatInteractionId,
input_data: message
});
return {
text: result.chat_response || 'Chat session created successfully',
interaction_id: result.interaction_id,
session_info: result.execution_meta,
timestamp: result.timestamp,
status: result.status
};
} else {
// Continue existing chat session
const result = await executeChatMessage({
user_input: message,
interaction_id: chatInteractionId
});
return {
text: result.ai_response,
interaction_id: result.interaction_id,
session_id: result.session_id,
session_info: result.execution_meta,
timestamp: result.timestamp,
status: result.status
};
}
} catch (error) {
console.error('Error in sendMessage:', error);
throw error;
}
};
/**
* Legacy function for backward compatibility - simulates sending a message
* @deprecated Use sendMessage with proper parameters instead
* @param {string} message - The message to send
* @returns {Promise<{text: string}>} A promise that resolves with a response object
*/
export const sendMessageLegacy = (message) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve({
text: `This is a response to your message: "${message}"`,
});
}, 1000); // Simulate 1 second delay
});
};