2424#ifndef Q_MOC_RUN
2525#include < aiprocess/app_settings.h>
2626#include < aiprocess/log.h>
27+ #include < aiprocess/obfuscate_text.h>
2728#include < ai_processing.h>
2829#include < fmt/core.h>
2930#include < simple_enum/simple_enum.hpp>
30- #include < aiprocess/log.h>
3131#include < string>
3232#include < future>
3333#include < thread>
@@ -84,7 +84,7 @@ kdevcxx_with_ai::kdevcxx_with_ai(QObject * parent, QVariantList const &) : KDeve
8484void kdevcxx_with_ai::on_first_time ()
8585 {
8686 auto aisettings = aiprocess::load_ai_settings ();
87- aiprocess::warn (" api_key {}" , aisettings.api_key );
87+ aiprocess::warn (" api_key {}" , aiprocess::obfuscate_text ( aisettings.api_key ) );
8888 aiprocess::warn (" cxx_rules {}" , aisettings.cxx_rules );
8989 if (aisettings.api_key .empty ())
9090 {
@@ -109,8 +109,6 @@ void kdevcxx_with_ai::createActionsForMainWindow(Sublime::MainWindow *, QString
109109
110110 actions.addAction (" process_with_ai" , myAction);
111111 connect (myAction, &QAction::triggered, this , &kdevcxx_with_ai::on_process_with_ai);
112-
113- xmlFile = " :/ui/rcfile.ui.rc" ;
114112 }
115113
116114using namespace std ::string_view_literals;
@@ -121,84 +119,82 @@ void kdevcxx_with_ai::on_process_with_ai()
121119 auto aisettings{aiprocess::load_ai_settings ()};
122120 if (aisettings.api_key .empty ())
123121 {
124- info_dialog dialog (
122+ info_dialog dialog{
125123 " KDevCxx_With_Ai key setup still not done .." ,
126124 " Please edit file ~/.config/kdevcxx_with_ai/kdevcxx_with_ai_ai_settings.json\n and enter Your API key before "
127125 " calling any functions and adjust Your rules for AI.\n You can change them at any time without restarting "
128126 " KDevelop"
129- ) ;
127+ } ;
130128 dialog.exec ();
129+ return ;
131130 }
132131 }
132+
133133 using namespace std ::chrono_literals;
134- // qDebug() << "\nMy action was triggered!\n";
135- KTextEditor::View * view = KTextEditor::Editor::instance ()->application ()->activeMainWindow ()->activeView ();
134+ auto * view = KTextEditor::Editor::instance ()->application ()->activeMainWindow ()->activeView ();
135+
136+ if (!view || !view->selection ()) [[unlikely]]
137+ return ;
136138
137- if (nullptr != view && view->selection ())
139+ auto * document = view->document ();
140+ if (!document) [[unlikely]]
138141 {
139- KTextEditor::Document * document = view->document ();
140- if (nullptr == document)
141- {
142- qDebug () << " Invalid view->document" ;
143- return ;
144- }
145- // document_read_only_t read_only_guard{*document};
146- info (" Processing OpenAI request ..." );
147- QProgressDialog progressDialog (" Processing OpenAI Request..." , " Cancel" , 0 , 100 );
148- progressDialog.setWindowModality (Qt::WindowModal);
149- progressDialog.show ();
142+ warn (" Invalid view->document" );
143+ return ;
144+ }
150145
151- QString selected_text = view->selectionText ();
146+ info (" Processing OpenAI request ..." );
147+ QProgressDialog progressDialog{" Processing OpenAI Request..." , " Cancel" , 0 , 100 };
148+ progressDialog.setWindowModality (Qt::WindowModal);
149+ progressDialog.show ();
152150
153- auto fn_call_openai
154- = [](std::string text) -> expected<aiprocess::model_response_text_t , aiprocess::process_with_ai_error>
155- { return aiprocess::process_with_ai (std::move (text)); };
151+ QString selected_text = view->selectionText ();
156152
157- auto async_result = std::async (std::launch::async, fn_call_openai, selected_text.toStdString ());
158- debug (" async to OpenAI executed ..." );
153+ auto async_result = std::async (
154+ std::launch::async,
155+ [](QString text) -> expected<aiprocess::model_response_text_t , aiprocess::process_with_ai_error>
156+ { return aiprocess::process_with_ai (text.toStdString ()); },
157+ selected_text
158+ );
159159
160- std::this_thread::yield ();
160+ debug (" async to OpenAI executed ..." );
161+ std::this_thread::yield ();
161162
162- while (async_result.wait_for (50ms) != std::future_status::ready)
163- QCoreApplication::processEvents (QEventLoop::AllEvents, 100 );
164- auto result = async_result.get ();
165- debug (" async to OpenAI done ..." );
166- // auto result = process_with_ai(selected_text.toStdString());
163+ while (async_result.wait_for (50ms) != std::future_status::ready)
164+ QCoreApplication::processEvents (QEventLoop::AllEvents, 100 );
167165
168- if (result)
169- {
170- std::string cur_work_dir{};
171- if (auto path{get_view_file_path (*view)}; path.has_value ())
172- cur_work_dir = std::move (path.value ());
173- auto const & response{*result};
174- auto new_text = process_ai_response (response, std::move (cur_work_dir));
175- debug (" Proocessed response Command Text: {}\n Code Text: {}\n " , response.command , response.recived_text );
176-
177- // make it read write to apply chnages
178- // read_only_guard.clear_state();
179- if (!new_text.empty ())
180- {
181- document->replaceText (view->selectionRange (), QString::fromStdString (new_text));
182- debug (" document->replaceText called ..." );
183- }
184- }
185- else
186- {
187- aiprocess::li::error (" Got error from async {}\n " , result.error ());
188- QWidget * parentWidget = view; // The parent widget, can be 'nullptr' if there's no parent
189- QString title = " Error during processing AI request" ; // Title of the error dialog
190- KMessageBox::error (
191- parentWidget,
192- QString::fromStdString (stralgo::stl::merge (
193- " Error processing AI request " sv,
194- simple_enum::enum_name (result.error ()),
195- " \n check detailed log at ~/.config/kdevcxx_with_ai/" sv,
196- settings.log_path
197- )),
198- title
199- );
200- }
166+ auto result = async_result.get ();
167+ debug (" async to OpenAI done ..." );
168+
169+ if (!result)
170+ {
171+ aiprocess::li::error (" Got error from async {}\n " , result.error ());
172+ KMessageBox::error (
173+ view,
174+ QString::fromStdString (stralgo::stl::merge (
175+ " Error processing AI request " sv,
176+ simple_enum::enum_name (result.error ()),
177+ " \n check detailed log at ~/.config/kdevcxx_with_ai/" sv,
178+ settings.log_path
179+ )),
180+ " Error during processing AI request"
181+ );
182+ return ;
201183 }
184+
185+ std::string cur_work_dir{};
186+ if (auto path = get_view_file_path (*view); path.has_value ())
187+ cur_work_dir = std::move (path.value ());
188+
189+ auto const & response = *result;
190+ auto new_text = aiprocess::process_openai_json_response (response, std::move (cur_work_dir));
191+ debug (" Proocessed response Command Text: {}\n Code Text: {}\n " , response.command , response.recived_text );
192+
193+ if (new_text.empty ()) [[unlikely]]
194+ return ;
195+
196+ document->replaceText (view->selectionRange (), QString::fromStdString (new_text));
197+ debug (" document->replaceText called ..." );
202198 }
203199
204200kdevcxx_with_ai::~kdevcxx_with_ai () {}
0 commit comments