Skip to content

Commit

Permalink
Refactor parse_openai_response to return detailed commit message or…
Browse files Browse the repository at this point in the history
… error
  • Loading branch information
soramimi committed Jun 16, 2024
1 parent 193dbb6 commit 8d6b32c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
33 changes: 16 additions & 17 deletions src/CommitMessageGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,9 @@ static std::string example_gemini_response()
)---";
}

std::vector<std::string> CommitMessageGenerator::parse_openai_response(std::string const &in, GenerativeAI::Type ai_type)
GeneratedCommitMessage CommitMessageGenerator::parse_openai_response(std::string const &in, GenerativeAI::Type ai_type)
{
error_message_.clear();
std::vector<std::string> lines;
bool ok1 = false;
std::string text;
char const *begin = in.c_str();
Expand Down Expand Up @@ -244,6 +243,7 @@ std::vector<std::string> CommitMessageGenerator::parse_openai_response(std::stri
}
}
if (ok1) {
std::vector<std::string> lines;
misc::splitLines(text, &lines, false);
size_t i = lines.size();
while (i > 0) {
Expand Down Expand Up @@ -290,9 +290,18 @@ std::vector<std::string> CommitMessageGenerator::parse_openai_response(std::stri
lines.erase(lines.begin() + i);
}
}
return lines;
QStringList ret;
for (auto const &line : lines) {
ret.push_back(QString::fromStdString(line));
}
return ret;
} else {
GeneratedCommitMessage ret;
ret.error = true;
ret.error_status = QString::fromStdString(error_status_);
ret.error_message = QString::fromStdString(error_message_);
return ret;
}
return {};
}

std::string CommitMessageGenerator::generatePrompt(QString diff, int max)
Expand Down Expand Up @@ -354,7 +363,7 @@ std::string CommitMessageGenerator::generatePromptJSON(GenerativeAI::Model const
return json;
}

std::vector<std::string> CommitMessageGenerator::test()
GeneratedCommitMessage CommitMessageGenerator::test()
{
std::string s = R"---(
)---";
Expand All @@ -368,13 +377,7 @@ GeneratedCommitMessage CommitMessageGenerator::generate(GitPtr g)
constexpr bool save_log = true;

if (0) { // for debugging JSON parsing
auto list = test();
QStringList out;
for (int i = 0; i < max_message_count && i < list.size(); i++) {
out.push_back(QString::fromStdString(list[i]));
}
return out;

return test();
}

QString diff = g->diff_head();
Expand Down Expand Up @@ -440,11 +443,7 @@ GeneratedCommitMessage CommitMessageGenerator::generate(GitPtr g)
if (!error_status_.empty()) {
return GeneratedCommitMessage::Error(QString::fromStdString(error_status_), QString::fromStdString(error_message_));
}
QStringList out;
for (int i = 0; i < max_message_count && i < list.size(); i++) {
out.push_back(QString::fromStdString(list[i]));
}
return out;
return list;
}

return {};
Expand Down
4 changes: 2 additions & 2 deletions src/CommitMessageGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class CommitMessageGenerator {
private:
std::string error_status_;
std::string error_message_;
std::vector<std::string> parse_openai_response(const std::string &in, GenerativeAI::Type ai_type);
GeneratedCommitMessage parse_openai_response(const std::string &in, GenerativeAI::Type ai_type);
std::string generatePrompt(QString diff, int max);
std::string generatePromptJSON(const GenerativeAI::Model &model, QString diff, int max);
std::vector<std::string> test();
GeneratedCommitMessage test();
public:
CommitMessageGenerator() = default;
GeneratedCommitMessage generate(GitPtr g);
Expand Down

0 comments on commit 8d6b32c

Please sign in to comment.