Skip to content

Commit 0d1f772

Browse files
committed
feat: handle different auth method and fix errors regarding this
1 parent 6bff107 commit 0d1f772

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

backend/companion/companionRouter.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ async function handleAIChatRequest(req, res) {
1212
req.body.toString(),
1313
);
1414
const clusterUrl = req.headers['x-cluster-url'];
15-
const clusterToken = req.headers['x-k8s-authorization'].replace(
15+
const certificateAuthorityData =
16+
req.headers['x-cluster-certificate-authority-data'];
17+
const clusterToken = req.headers['x-k8s-authorization']?.replace(
1618
/^Bearer\s+/i,
1719
'',
1820
);
19-
const certificateAuthorityData =
20-
req.headers['x-cluster-certificate-authority-data'];
21+
const clientCertificateData = req.headers['x-client-certificate-data'];
22+
const clientKeyData = req.headers['x-client-key-data'];
2123

2224
try {
2325
const url = 'https://companion.cp.dev.kyma.cloud.sap/api/conversations/';
@@ -30,16 +32,26 @@ async function handleAIChatRequest(req, res) {
3032

3133
const AUTH_TOKEN = await tokenManager.getToken();
3234

35+
const headers = {
36+
Accept: 'application/json',
37+
'Content-Type': 'application/json',
38+
Authorization: `Bearer ${AUTH_TOKEN}`,
39+
'X-Cluster-Certificate-Authority-Data': certificateAuthorityData,
40+
'X-Cluster-Url': clusterUrl,
41+
};
42+
43+
if (clusterToken) {
44+
headers['X-K8s-Authorization'] = clusterToken;
45+
} else if (clientCertificateData && clientKeyData) {
46+
headers['X-Client-Certificate-Data'] = clientCertificateData;
47+
headers['X-Client-Key-Data'] = clientKeyData;
48+
} else {
49+
throw new Error('Missing authentication credentials');
50+
}
51+
3352
const response = await fetch(url, {
3453
method: 'POST',
35-
headers: {
36-
Accept: 'application/json',
37-
'Content-Type': 'application/json',
38-
Authorization: `Bearer ${AUTH_TOKEN}`,
39-
'X-Cluster-Certificate-Authority-Data': certificateAuthorityData,
40-
'X-Cluster-Url': clusterUrl,
41-
'X-K8s-Authorization': clusterToken,
42-
},
54+
headers,
4355
body: JSON.stringify(payload),
4456
});
4557

src/components/KymaCompanion/components/Chat/Chat.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export default function Chat({
7777
};
7878

7979
const setFollowUpLoading = () => {
80+
setErrorOccured(false);
8081
setChatHistory(prevMessages => {
8182
const [latestMessage] = prevMessages.slice(-1);
8283
return prevMessages.slice(0, -1).concat({
@@ -141,7 +142,7 @@ export default function Chat({
141142
if (chatHistory.length === 1) {
142143
if (initialSuggestionsLoading) {
143144
setFollowUpLoading();
144-
} else if (initialSuggestions.length) {
145+
} else if (initialSuggestions) {
145146
handleFollowUpQuestions(initialSuggestions);
146147
}
147148
}

src/components/KymaCompanion/hooks/usePromptSuggestions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export function usePromptSuggestions() {
4242

4343
async function fetchSuggestions() {
4444
setLoading(true);
45+
setSuggestions([]);
4546
try {
4647
const result = await getPromptSuggestions({
4748
post,

0 commit comments

Comments
 (0)