Skip to content
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

[FEATURE] Handle OpenAI downtime and large requests #149

Open
guibranco opened this issue Dec 12, 2024 · 2 comments · May be fixed by #151
Open

[FEATURE] Handle OpenAI downtime and large requests #149

guibranco opened this issue Dec 12, 2024 · 2 comments · May be fixed by #151
Labels
♻️ code quality Code quality-related tasks or issues 📝 documentation Tasks related to writing or updating documentation enhancement New feature or request gitauto GitAuto label to trigger the app in a issue. good first issue Good for newcomers hacktoberfest Participation in the Hacktoberfest event help wanted Extra attention is needed 🕔 high effort A task that can be completed in a few days 👷🏼 infrastructure Infrastructure-related tasks or issues 🧑‍💻 tech-debit Technical debt that needs to be addressed 🧪 tests Tasks related to testing 🛠 WIP Work in progress

Comments

@guibranco
Copy link
Owner

guibranco commented Dec 12, 2024

This feature aims to enhance the resilience and user experience of dotnet-aicommitmessage by addressing two scenarios:

  1. OpenAI Downtime/Unresponsiveness:

    • Detect if the OpenAI API is down or unresponsive.
    • Abort the process gracefully, displaying a clear error message to the user.
  2. Large Requests (Exceeding OpenAI Limits):

    • If the Git diff output size exceeds acceptable limits:
      • Abort the commit process.
      • Provide an error message instructing the user to reduce the number of staged files or the size of the changes.

Acceptance Criteria

  • OpenAI Downtime Handling:

    • The tool should detect when the OpenAI API is unreachable or unresponsive (e.g., network issues, server downtime).
    • An appropriate error message should be returned, clearly informing the user of the issue.
  • Handling Large Requests:

    • Implement a mechanism to calculate and validate the size of the Git diff output.
    • If the size exceeds a predefined limit, return an error message and abort the commit process.
    • The error message should suggest staging fewer files or smaller changes.

Proposed Error Messages

  • OpenAI Downtime:

    ⚠️ OpenAI API is currently unavailable. Please try again later.

  • Large Request:

    🚫 The staged changes are too large to process. Please reduce the number of files or size of changes and try again.

Additional Notes

  • Ensure robust error handling to prevent crashes during these scenarios.
  • Add relevant unit tests to verify both downtime handling and request size validation.
  • Document these behaviors in the README.md or a dedicated error-handling section.

Sample example code

public string GenerateCommitMessage(GenerateCommitMessageOptions options)
{
    const int MaxDiffSizeBytes = 10240; // Limit Git diff size to 10 KB
    var model = EnvironmentLoader.LoadOpenAiModel();
    var url = EnvironmentLoader.LoadOpenAiApiUrl();
    var key = EnvironmentLoader.LoadOpenAiApiKey();

    var client = new ChatClient(
        model,
        new ApiKeyCredential(key),
        new OpenAIClientOptions { Endpoint = new Uri(url) }
    );

    var branch = string.IsNullOrEmpty(options.Branch)
        ? GitHelper.GetBranchName()
        : options.Branch;

    var diff = string.IsNullOrEmpty(options.Diff) ? GitHelper.GetGitDiff() : options.Diff;

    // Check if the Git diff is too large
    if (Encoding.UTF8.GetByteCount(diff) > MaxDiffSizeBytes)
    {
        throw new InvalidOperationException(
            "🚫 The staged changes are too large to process. Please reduce the number of files or size of changes and try again."
        );
    }

    var message = options.Message; // Use the provided message from the prepare-commit-msg hook

    if (string.IsNullOrEmpty(branch) && string.IsNullOrEmpty(diff))
    {
        throw new InvalidOperationException(
            "Unable to generate commit message: Both branch and diff are empty."
        );
    }

    var formattedMessage =
        "Branch: "
        + (string.IsNullOrEmpty(branch) ? "<unknown>" : branch)
        + "\n\n"
        + "Original message: "
        + message
        + "\n\n"
        + "Git Diff: "
        + (string.IsNullOrEmpty(diff) ? "<no changes>" : diff);

    // Call OpenAI API and handle downtime
    try
    {
        var chatCompletion = client.CompleteChat(
            new SystemChatMessage(Constants.SystemMessage),
            new UserChatMessage(formattedMessage)
        );

        var text = chatCompletion.Value.Content[0].Text;

        // Post-processing the generated message
        if (text.Length >= 7 && text[..7] == "type - ")
        {
            text = text[7..];
        }

        var provider = GetGitProvider();
        if (provider == GitProvider.GitHub)
        {
            var issueNumber = BranchNameUtility.ExtractIssueNumber(branch);
            if (!string.IsNullOrWhiteSpace(issueNumber))
            {
                text = $"#{issueNumber} {text}";
            }
        }
        else
        {
            var jiraTicketNumber = BranchNameUtility.ExtractJiraTicket(branch);
            if (!string.IsNullOrWhiteSpace(jiraTicketNumber))
            {
                text = $"[{jiraTicketNumber}] {text}";
            }
        }

        var gitVersionCommand = GitVersionUtility.ExtractGitVersionBumpCommand(message);
        if (!string.IsNullOrEmpty(gitVersionCommand))
        {
            text = $"{text} {gitVersionCommand}";
        }

        return text;
    }
    catch (Exception ex)
    {
        throw new InvalidOperationException(
            $"⚠️ OpenAI API is currently unavailable or an error occurred. Please try again later.\nError: {ex.Message}"
        );
    }
}
Copy link
Contributor

gitauto-ai bot commented Dec 12, 2024

Click the checkbox below to generate a PR!

  • Generate PR

@guibranco, You have 16 requests left in this cycle which refreshes on 2024-12-26 00:19:30+00:00.
If you have any questions or concerns, please contact us at [email protected].

@guibranco guibranco added 📝 documentation Tasks related to writing or updating documentation enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers 👷🏼 infrastructure Infrastructure-related tasks or issues ♻️ code quality Code quality-related tasks or issues 🧪 tests Tasks related to testing 🧑‍💻 tech-debit Technical debt that needs to be addressed hacktoberfest Participation in the Hacktoberfest event 🕔 high effort A task that can be completed in a few days labels Dec 12, 2024
@gitauto-ai gitauto-ai bot added the gitauto GitAuto label to trigger the app in a issue. label Dec 13, 2024
Copy link
Contributor

gitauto-ai bot commented Dec 13, 2024

@guibranco Pull request completed! Check it out here #151 🚀

Note: I automatically create a pull request for an unassigned and open issue in order from oldest to newest once a day at 00:00 UTC, as long as you have remaining automation usage. Should you have any questions or wish to change settings or limits, please feel free to contact [email protected] or invite us to Slack Connect.

@gstraccini gstraccini bot added the 🛠 WIP Work in progress label Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
♻️ code quality Code quality-related tasks or issues 📝 documentation Tasks related to writing or updating documentation enhancement New feature or request gitauto GitAuto label to trigger the app in a issue. good first issue Good for newcomers hacktoberfest Participation in the Hacktoberfest event help wanted Extra attention is needed 🕔 high effort A task that can be completed in a few days 👷🏼 infrastructure Infrastructure-related tasks or issues 🧑‍💻 tech-debit Technical debt that needs to be addressed 🧪 tests Tasks related to testing 🛠 WIP Work in progress
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant