Skip to content

Commit 34b75d1

Browse files
authored
Fix chat endpoint (#23)
This pull request includes several improvements and new features for the chat functionality in the `ChatContainer` component and related services. The most important changes include adding support for handling `chatUuid` in the payload and response headers, and updating the API endpoints. Enhancements to `ChatContainer` component: * [`src/components/ChatContainer/ChatContainer.tsx`](diffhunk://#diff-c0bfccaf03a29a5059f6a47c6dd2cdd194fcb1e06fe8e0e188d700a86204f073R167-R171): Added support for including `chatUuid` in the payload if available and setting `chatUuid` from response headers if not already set. [[1]](diffhunk://#diff-c0bfccaf03a29a5059f6a47c6dd2cdd194fcb1e06fe8e0e188d700a86204f073R167-R171) [[2]](diffhunk://#diff-c0bfccaf03a29a5059f6a47c6dd2cdd194fcb1e06fe8e0e188d700a86204f073R217-R221) Updates to `APIClient` and `ChatService`: * [`src/services/APIClient.ts`](diffhunk://#diff-c541918b610d84d9d4591faecad3008e1a8a244d446b8b929f3e017ea97fd558L57-R61): Modified the `fetch` method to merge custom headers with default headers and enabled CORS mode. * [`src/services/ChatService.ts`](diffhunk://#diff-9dd13b51c673219aa9d6fb02c8bc1100134172df11c9c69008b0b299c0b3d53fL43-R43): Updated the `sendMessage` method to use the new API endpoint `/api/v1/chats`. * [`src/services/ChatService.ts`](diffhunk://#diff-9dd13b51c673219aa9d6fb02c8bc1100134172df11c9c69008b0b299c0b3d53fL55-R78): Enhanced the `sendStreamMessage` method to handle `chatUuid` from response headers and added debug logging for all response headers.
1 parent bd96ed4 commit 34b75d1

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/components/ChatContainer/ChatContainer.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ export const ChatContainer: React.FC<ChatContainerProps> = ({
164164
})
165165
};
166166

167+
if (chatUuid) {
168+
payload.chat_uuid = chatUuid;
169+
}
170+
171+
167172
if (config.streamResponse) {
168173
await handleStreamingResponse(chatService, payload);
169174
} else {
@@ -209,6 +214,11 @@ export const ChatContainer: React.FC<ChatContainerProps> = ({
209214
return newMessages;
210215
});
211216
}
217+
},
218+
(headerChatUuid) => {
219+
if (headerChatUuid && !chatUuid) {
220+
setChatUuid(headerChatUuid);
221+
}
212222
}
213223
);
214224
};

src/services/APIClient.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ export class APIClient {
5454
): Promise<Response> {
5555
const response = await fetch(`${this.baseUrl}${endpoint}`, {
5656
...options,
57-
headers: this.getHeaders(),
57+
headers: {
58+
...this.getHeaders(),
59+
...options.headers
60+
},
61+
mode: 'cors',
5862
});
5963

6064
if (!response.ok) {

src/services/ChatService.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class ChatService extends APIClient {
4040
}
4141

4242
async sendMessage(payload: ChatPayload): Promise<APIResponse<ChatResponse>> {
43-
return this.fetchWithError<ChatResponse>('/ask', {
43+
return this.fetchWithError<ChatResponse>('/api/v1/chats', {
4444
method: 'POST',
4545
body: JSON.stringify(payload),
4646
});
@@ -52,14 +52,30 @@ export class ChatService extends APIClient {
5252

5353
async sendStreamMessage(
5454
payload: ChatPayload,
55-
onChunk: (chunk: StreamChunk) => void
55+
onChunk: (chunk: StreamChunk) => void,
56+
onHeaderChatUuid?: (chatUuid: string) => void
5657
): Promise<void> {
5758
try {
5859
const response = await this.fetchStream('/api/v1/chats/stream', {
5960
method: 'POST',
6061
body: JSON.stringify(payload),
6162
});
6263

64+
//const chatUuid = response.headers.get('X-Chat-Uuid');
65+
// Debug: Log all available headers
66+
console.log('All response headers:');
67+
response.headers.forEach((value, name) => {
68+
console.log(`${name}: ${value}`);
69+
});
70+
71+
// Try both casing variants
72+
const chatUuid = response.headers.get('X-MKit-Chat-UUID');
73+
74+
if (chatUuid && onHeaderChatUuid) {
75+
onHeaderChatUuid(chatUuid);
76+
}
77+
78+
6379
const reader = response.body?.getReader();
6480
if (!reader) {
6581
throw new Error('Response body is not readable');

0 commit comments

Comments
 (0)