-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
880 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
121 changes: 121 additions & 0 deletions
121
x-pack/plugins/security_solution/public/security_assistant/api.tsx
This file contains 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,121 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import axios from 'axios'; | ||
|
||
export const fetchOpenAlerts = async () => { | ||
try { | ||
// TODO: Fetch alerts via alerts API if need be | ||
return []; | ||
} catch (error) { | ||
console.error('Error fetching open alerts:', error); | ||
throw error; | ||
} | ||
}; | ||
|
||
export interface FetchVirusTotalAnalysisProps { | ||
analysisId: string; | ||
apiKey: string; | ||
baseUrl: string; | ||
} | ||
export const fetchVirusTotalAnalysis = async ({ | ||
analysisId, | ||
apiKey, | ||
baseUrl, | ||
}: FetchVirusTotalAnalysisProps) => { | ||
try { | ||
const response = await axios.get(`${baseUrl}/analyses/${analysisId}`, { | ||
headers: { | ||
'x-apikey': apiKey, | ||
}, | ||
}); | ||
return response.data; | ||
} catch (error) { | ||
console.error('Error while fetching analysis from VirusTotal:', error); | ||
return null; | ||
} | ||
}; | ||
|
||
export interface SendFileToVirusTotalProps { | ||
file: File; | ||
apiKey: string; | ||
baseUrl: string; | ||
} | ||
export const sendFileToVirusTotal = async ({ | ||
file, | ||
apiKey, | ||
baseUrl, | ||
}: SendFileToVirusTotalProps) => { | ||
const url = `${baseUrl}/files`; | ||
|
||
const formData = new FormData(); | ||
formData.append('file', file); // Append the file to the FormData object | ||
|
||
try { | ||
const response = await axios.post(url, formData, { | ||
headers: { | ||
'x-apikey': apiKey, | ||
'Content-Type': 'multipart/form-data', | ||
}, | ||
}); | ||
return response.data; | ||
} catch (error) { | ||
console.error('Error while uploading file to VirusTotal:', error); | ||
throw error; | ||
} | ||
}; | ||
|
||
export interface SendMessageProps { | ||
conversation: { role: string; content: string }[]; | ||
baseUrl: string; | ||
apiKey: string; | ||
} | ||
export const sendMessage = async ({ | ||
conversation, | ||
baseUrl, | ||
apiKey, | ||
}: SendMessageProps): Promise<string> => { | ||
const messages = conversation.map((msg) => ({ | ||
role: msg.role, | ||
content: msg.content, | ||
})); | ||
|
||
const requestBody = { | ||
messages: messages, | ||
n: 1, | ||
stop: null, | ||
temperature: 0.2, | ||
}; | ||
|
||
try { | ||
const response = await fetch(baseUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'api-key': apiKey, | ||
}, | ||
body: JSON.stringify(requestBody), | ||
}); | ||
|
||
const data = await response.json(); | ||
if (!response.ok) { | ||
console.error('Error in ChatGPT API response:', data); | ||
return 'An error occurred while processing your request.'; | ||
} | ||
|
||
if (data.choices && data.choices.length > 0 && data.choices[0].message.content) { | ||
const result = data.choices[0].message.content.trim(); | ||
return result; | ||
} else { | ||
console.error('Unexpected API response format:', data); | ||
return 'An error occurred while processing your request.'; | ||
} | ||
} catch (error) { | ||
console.error('Error while sending message to ChatGPT:', error); | ||
return 'An error occurred while processing your request.'; | ||
} | ||
}; |
Oops, something went wrong.