useAiConfig.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import {useAutofixSetup} from 'sentry/components/events/autofix/useAutofixSetup';
  2. import {EntryType, type Event} from 'sentry/types/event';
  3. import type {Group} from 'sentry/types/group';
  4. import type {Project} from 'sentry/types/project';
  5. import {getConfigForIssueType} from 'sentry/utils/issueTypeConfig';
  6. import {getRegionDataFromOrganization} from 'sentry/utils/regions';
  7. import useOrganization from 'sentry/utils/useOrganization';
  8. import {useIsSampleEvent} from 'sentry/views/issueDetails/utils';
  9. // Autofix requires the event to have stack trace frames in order to work correctly.
  10. function hasStacktraceWithFrames(event: Event) {
  11. for (const entry of event.entries) {
  12. if (entry.type === EntryType.EXCEPTION) {
  13. if (entry.data.values?.some(value => value.stacktrace?.frames?.length)) {
  14. return true;
  15. }
  16. }
  17. if (entry.type === EntryType.THREADS) {
  18. if (entry.data.values?.some(thread => thread.stacktrace?.frames?.length)) {
  19. return true;
  20. }
  21. }
  22. }
  23. return false;
  24. }
  25. export const useAiConfig = (
  26. group: Group,
  27. event: Event | undefined | null,
  28. project: Project
  29. ) => {
  30. const organization = useOrganization();
  31. const {data: autofixSetupData, isPending: isAutofixSetupLoading} = useAutofixSetup({
  32. groupId: group.id,
  33. });
  34. const isSampleError = useIsSampleEvent();
  35. const hasStacktrace = event && hasStacktraceWithFrames(event);
  36. const issueTypeConfig = getConfigForIssueType(group, project);
  37. const areAiFeaturesAllowed =
  38. !organization.hideAiFeatures &&
  39. getRegionDataFromOrganization(organization)?.name !== 'de' &&
  40. organization.features.includes('gen-ai-features');
  41. const isSummaryEnabled = issueTypeConfig.issueSummary.enabled;
  42. const isAutofixEnabled = issueTypeConfig.autofix;
  43. const hasResources = issueTypeConfig.resources;
  44. const hasGenAIConsent = autofixSetupData?.genAIConsent.ok ?? organization.genAIConsent;
  45. const hasSummary = hasGenAIConsent && isSummaryEnabled && areAiFeaturesAllowed;
  46. const hasAutofix =
  47. isAutofixEnabled && areAiFeaturesAllowed && hasStacktrace && !isSampleError;
  48. const needsGenAIConsent =
  49. !hasGenAIConsent && (isSummaryEnabled || isAutofixEnabled) && areAiFeaturesAllowed;
  50. const needsAutofixSetup =
  51. isAutofixEnabled &&
  52. !isAutofixSetupLoading &&
  53. (!autofixSetupData?.genAIConsent.ok || !autofixSetupData?.integration.ok) &&
  54. areAiFeaturesAllowed;
  55. return {
  56. hasSummary,
  57. hasAutofix,
  58. needsGenAIConsent,
  59. needsAutofixSetup,
  60. hasResources,
  61. isAutofixSetupLoading,
  62. };
  63. };