useAiConfig.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. export 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. const isSummaryEnabled = issueTypeConfig.issueSummary.enabled;
  41. const isAutofixEnabled = issueTypeConfig.autofix;
  42. const hasResources = issueTypeConfig.resources;
  43. const hasGenAIConsent = autofixSetupData?.genAIConsent.ok ?? organization.genAIConsent;
  44. const hasSummary = hasGenAIConsent && isSummaryEnabled && areAiFeaturesAllowed;
  45. const hasAutofix =
  46. isAutofixEnabled && areAiFeaturesAllowed && hasStacktrace && !isSampleError;
  47. const needsGenAIConsent =
  48. !hasGenAIConsent && (isSummaryEnabled || isAutofixEnabled) && areAiFeaturesAllowed;
  49. const needsAutofixSetup =
  50. isAutofixEnabled &&
  51. !isAutofixSetupLoading &&
  52. (!autofixSetupData?.genAIConsent.ok || !autofixSetupData?.integration.ok) &&
  53. areAiFeaturesAllowed;
  54. return {
  55. hasSummary,
  56. hasAutofix,
  57. needsGenAIConsent,
  58. needsAutofixSetup,
  59. hasResources,
  60. isAutofixSetupLoading,
  61. };
  62. };