-
Notifications
You must be signed in to change notification settings - Fork 56
feat: POST /conversations to companion backend #3691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
73f8fe0
feat: track currently opened resource in ResourceDetails
chriskari 332059b
feat: use columnLayoutState to track currentResource
chriskari f0392fb
Merge branch 'main' of https://github.com/kyma-project/busola into po…
chriskari 9c803ac
feat: update structure of api call
chriskari fb6c722
Merge branch 'main' of https://github.com/kyma-project/busola into po…
chriskari d5193c0
feat: proxy API request through our backend
chriskari 74fa194
Merge branch 'main' of https://github.com/kyma-project/busola into po…
chriskari 701547f
Merge branch 'main' of https://github.com/kyma-project/busola into po…
chriskari 47fc2e4
feat: automate token fetching in the backend
chriskari 4a614e8
feat: enhance columnLayouState to also track the start column resource
chriskari fb7da47
feat: adjust resourcetype of cluster overview
chriskari 6edb6f0
Merge branch 'main' of https://github.com/kyma-project/busola into po…
chriskari 4716f7a
feat: improve initial layout
chriskari 102719b
feat: adjust column state for cluster overview
chriskari 55fd928
Merge branch 'main' of https://github.com/kyma-project/busola into po…
chriskari 6bff107
feat: add loading indicator, and refresh when navigate to different r…
chriskari 0d1f772
feat: handle different auth method and fix errors regarding this
chriskari c98b2b8
feat: skip request if conversation has started
chriskari 837f50b
feat: add properly cased resourceType to navigation nodes
chriskari d7bdadc
feat: adjustments regarding previous commit
chriskari 0441366
Merge branch 'main' of https://github.com/kyma-project/busola into po…
chriskari 4c56eb6
feat: adjust introductory message based on existence of suggestions
chriskari e0cd17b
Merge branch 'main' of https://github.com/kyma-project/busola into po…
chriskari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { getKcpToken } from './getKcpToken'; | ||
|
|
||
| export class TokenManager { | ||
| constructor() { | ||
| this.currentToken = null; | ||
| this.tokenExpirationTime = null; | ||
| // Add buffer time (e.g., 5 minutes) before actual expiration to prevent edge cases | ||
| this.expirationBuffer = 5 * 60 * 1000; | ||
| } | ||
|
|
||
| async getToken() { | ||
| // Check if token exists and is not near expiration | ||
| if ( | ||
| this.currentToken && | ||
| this.tokenExpirationTime && | ||
| Date.now() < this.tokenExpirationTime - this.expirationBuffer | ||
| ) { | ||
| return this.currentToken; | ||
| } | ||
|
|
||
| // If token doesn't exist or is expired/near expiration, fetch new token | ||
| try { | ||
| const newToken = await getKcpToken(); | ||
| this.currentToken = newToken; | ||
| // Set expiration time based on JWT expiry | ||
| // You'll need to decode the JWT to get the actual expiration | ||
| this.tokenExpirationTime = this.getExpirationFromJWT(newToken); | ||
| return newToken; | ||
| } catch (error) { | ||
| console.error('Failed to refresh token:', error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| getExpirationFromJWT(token) { | ||
| try { | ||
| // Split the token and get the payload | ||
| const payload = JSON.parse( | ||
| Buffer.from(token.split('.')[1], 'base64').toString(), | ||
| ); | ||
| // exp is in seconds, convert to milliseconds | ||
| return payload.exp * 1000; | ||
| } catch (error) { | ||
| console.error('Error parsing JWT:', error); | ||
| // If we can't parse the expiration, set a default (e.g., 1 hour from now) | ||
| return Date.now() + 60 * 60 * 1000; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import express from 'express'; | ||
| import { TokenManager } from './TokenManager'; | ||
|
|
||
| const tokenManager = new TokenManager(); | ||
|
|
||
| const router = express.Router(); | ||
|
|
||
| router.use(express.json()); | ||
|
|
||
| async function handleAIChatRequest(req, res) { | ||
| const { namespace, resourceType, groupVersion, resourceName } = JSON.parse( | ||
| req.body.toString(), | ||
| ); | ||
| const clusterUrl = req.headers['x-cluster-url']; | ||
| const clusterToken = req.headers['x-k8s-authorization'].replace( | ||
|
||
| /^Bearer\s+/i, | ||
| '', | ||
| ); | ||
| const certificateAuthorityData = | ||
| req.headers['x-cluster-certificate-authority-data']; | ||
|
|
||
| try { | ||
| const url = 'https://companion.cp.dev.kyma.cloud.sap/api/conversations/'; | ||
| const payload = { | ||
| resource_kind: resourceType, | ||
| resource_api_version: groupVersion, | ||
| resource_name: resourceName, | ||
| namespace: namespace, | ||
| }; | ||
|
|
||
| const AUTH_TOKEN = await tokenManager.getToken(); | ||
|
|
||
| const response = await fetch(url, { | ||
| method: 'POST', | ||
| headers: { | ||
| Accept: 'application/json', | ||
| 'Content-Type': 'application/json', | ||
| Authorization: `Bearer ${AUTH_TOKEN}`, | ||
| 'X-Cluster-Certificate-Authority-Data': certificateAuthorityData, | ||
| 'X-Cluster-Url': clusterUrl, | ||
| 'X-K8s-Authorization': clusterToken, | ||
| }, | ||
| body: JSON.stringify(payload), | ||
| }); | ||
|
|
||
| const data = await response.json(); | ||
|
|
||
| res.json({ | ||
| promptSuggestions: data?.initial_questions, | ||
| conversationId: data?.conversation_id, | ||
| }); | ||
| } catch (error) { | ||
| console.error('Error in AI Chat proxy:', error); | ||
| res.status(500).json({ error: 'Failed to fetch AI chat data' }); | ||
| } | ||
| } | ||
|
|
||
| router.post('/suggestions', handleAIChatRequest); | ||
|
|
||
| export default router; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| export async function getKcpToken() { | ||
| const tokenUrl = 'https://kymatest.accounts400.ondemand.com/oauth2/token'; | ||
| const grantType = 'client_credentials'; | ||
| const clientId = process.env.COMPANION_KCP_AUTH_CLIENT_ID; | ||
| const clientSecret = process.env.COMPANION_KCP_AUTH_CLIENT_SECRET; | ||
|
|
||
| if (!clientId) { | ||
| throw new Error('COMPANION_KCP_AUTH_CLIENT_ID is not set'); | ||
| } | ||
| if (!clientSecret) { | ||
| throw new Error('COMPANION_KCP_AUTH_CLIENT_SECRET is not set'); | ||
| } | ||
|
|
||
| // Prepare request data | ||
| const requestBody = new URLSearchParams(); | ||
| requestBody.append('grant_type', grantType); | ||
|
|
||
| // Prepare authorization header | ||
| const authHeader = Buffer.from(`${clientId}:${clientSecret}`).toString( | ||
| 'base64', | ||
| ); | ||
|
|
||
| try { | ||
| const response = await fetch(tokenUrl, { | ||
| method: 'POST', | ||
| headers: { | ||
| Authorization: `Basic ${authHeader}`, | ||
| 'Content-Type': 'application/x-www-form-urlencoded', | ||
| }, | ||
| body: requestBody.toString(), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`HTTP error! status: ${response.status}`); | ||
| } | ||
|
|
||
| const data = await response.json(); | ||
| return data.access_token; | ||
| } catch (error) { | ||
| throw new Error(`Failed to fetch token: ${error.message}`); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use const like
PAYLOAD_INDEXinstead of magic number?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay