Skip to content

Commit 2f04877

Browse files
committed
Add followups endpoint, and handle followup suggestions
1 parent f9cfb2b commit 2f04877

File tree

4 files changed

+73
-8
lines changed

4 files changed

+73
-8
lines changed

backend/companion/companionRouter.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,66 @@ async function handleChatMessage(req, res) {
162162
}
163163
}
164164

165+
async function handleFollowUpSuggestions(req, res) {
166+
console.log('handleFollowUpSuggestions', req, res);
167+
168+
const clusterUrl = req.headers['x-cluster-url'];
169+
const certificateAuthorityData =
170+
req.headers['x-cluster-certificate-authority-data'];
171+
const clusterToken = req.headers['x-k8s-authorization']?.replace(
172+
/^Bearer\s+/i,
173+
'',
174+
);
175+
const clientCertificateData = req.headers['x-client-certificate-data'];
176+
const clientKeyData = req.headers['x-client-key-data'];
177+
const sessionId = req.headers['session-id'];
178+
const conversationId = sessionId;
179+
180+
try {
181+
const baseUrl =
182+
'https://companion.cp.dev.kyma.cloud.sap/api/conversations/';
183+
const targetUrl = new URL(
184+
`${encodeURIComponent(conversationId)}/questions`,
185+
baseUrl,
186+
);
187+
188+
const AUTH_TOKEN = await tokenManager.getToken();
189+
190+
const headers = {
191+
Accept: 'application/json',
192+
'Content-Type': 'application/json',
193+
Authorization: `Bearer ${AUTH_TOKEN}`,
194+
'X-Cluster-Certificate-Authority-Data': certificateAuthorityData,
195+
'X-Cluster-Url': clusterUrl,
196+
'Session-Id': sessionId,
197+
};
198+
199+
if (clusterToken) {
200+
headers['X-K8s-Authorization'] = clusterToken;
201+
} else if (clientCertificateData && clientKeyData) {
202+
headers['X-Client-Certificate-Data'] = clientCertificateData;
203+
headers['X-Client-Key-Data'] = clientKeyData;
204+
} else {
205+
throw new Error('Missing authentication credentials');
206+
}
207+
208+
const response = await fetch(targetUrl, {
209+
method: 'GET',
210+
headers,
211+
});
212+
213+
const data = await response.json();
214+
res.json({
215+
promptSuggestions: data?.questions,
216+
conversationId: data?.conversation_id,
217+
});
218+
} catch (error) {
219+
res.status(500).json({ error: 'Failed to fetch AI chat data' });
220+
}
221+
}
222+
165223
router.post('/suggestions', handlePromptSuggestions);
166224
router.post('/messages', handleChatMessage);
225+
router.post('/followup', handleFollowUpSuggestions);
167226

168227
export default router;
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { getClusterConfig } from 'state/utils/getBackendInfo';
2+
import { PromptSuggestionsResponse } from './getPromptSuggestions';
23

34
interface GetFollowUpQuestionsParams {
45
sessionID?: string;
56
handleFollowUpQuestions: (results: any) => void;
7+
handleError: (error?: Error) => void;
68
clusterUrl: string;
79
token: string;
810
certificateAuthorityData: string;
@@ -11,30 +13,33 @@ interface GetFollowUpQuestionsParams {
1113
export default async function getFollowUpQuestions({
1214
sessionID = '',
1315
handleFollowUpQuestions,
16+
handleError,
1417
clusterUrl,
1518
token,
1619
certificateAuthorityData,
1720
}: GetFollowUpQuestionsParams): Promise<void> {
1821
try {
1922
const { backendAddress } = getClusterConfig();
20-
const url = `${backendAddress}/api/v1/namespaces/ai-core/services/http:ai-backend-clusterip:5000/proxy/api/v1/llm/followup`;
21-
const payload = JSON.parse(`{"session_id":"${sessionID}"}`);
23+
const url = `${backendAddress}/ai-chat/followup`;
2224
const k8sAuthorization = `Bearer ${token}`;
2325

24-
let { results } = await fetch(url, {
26+
const response = await fetch(url, {
2527
headers: {
2628
accept: 'application/json',
2729
'content-type': 'application/json',
2830
'X-Cluster-Certificate-Authority-Data': certificateAuthorityData,
2931
'X-Cluster-Url': clusterUrl,
3032
'X-K8s-Authorization': k8sAuthorization,
31-
'X-User': sessionID,
33+
'Session-Id': sessionID,
3234
},
33-
body: JSON.stringify(payload),
3435
method: 'POST',
35-
}).then(result => result.json());
36-
handleFollowUpQuestions(results);
36+
});
37+
38+
const promptSuggestions = (await response.json()).promptSuggestions;
39+
40+
handleFollowUpQuestions(promptSuggestions);
3741
} catch (error) {
42+
handleError();
3843
console.error('Error fetching data:', error);
3944
}
4045
}

src/components/KymaCompanion/api/getPromptSuggestions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface GetPromptSuggestionsParams {
88
resourceName?: string;
99
}
1010

11-
interface PromptSuggestionsResponse {
11+
export interface PromptSuggestionsResponse {
1212
promptSuggestions: string[];
1313
conversationId: string;
1414
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export default function Chat() {
7979
getFollowUpQuestions({
8080
sessionID,
8181
handleFollowUpQuestions,
82+
handleError,
8283
clusterUrl: cluster.currentContext.cluster.cluster.server,
8384
token: authData.token,
8485
certificateAuthorityData:

0 commit comments

Comments
 (0)