diff --git a/ExampleExtension/ExampleExtension.vcxproj b/ExampleExtension/ExampleExtension.vcxproj index 56a1cba..cd962e4 100644 --- a/ExampleExtension/ExampleExtension.vcxproj +++ b/ExampleExtension/ExampleExtension.vcxproj @@ -24,31 +24,32 @@ Win32Proj ExampleExtension 10.0 + LocalMtExtension DynamicLibrary true - v141 + v143 Unicode DynamicLibrary false - v141 + v143 true Unicode DynamicLibrary true - v141 + v143 Unicode DynamicLibrary false - v141 + v143 true Unicode diff --git a/ExampleExtension/ExampleVisualExtension.vcxproj b/ExampleExtension/ExampleVisualExtension.vcxproj index 7960aa6..cde810a 100644 --- a/ExampleExtension/ExampleVisualExtension.vcxproj +++ b/ExampleExtension/ExampleVisualExtension.vcxproj @@ -29,26 +29,26 @@ DynamicLibrary true - v141 + v143 Unicode DynamicLibrary false - v141 + v143 true Unicode DynamicLibrary true - v141 + v143 Unicode DynamicLibrary false - v141 + v143 true Unicode diff --git a/ExampleExtension/Extension.cpp b/ExampleExtension/Extension.cpp index ad7204b..80c5e0b 100644 --- a/ExampleExtension/Extension.cpp +++ b/ExampleExtension/Extension.cpp @@ -28,25 +28,26 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved */ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) { - // Your code here... -#ifdef COPY_CLIPBOARD - // This example extension automatically copies sentences from the hook currently selected by the user into the clipboard. - if (sentenceInfo["current select"]) - { - HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (sentence.size() + 2) * sizeof(wchar_t)); - memcpy(GlobalLock(hMem), sentence.c_str(), (sentence.size() + 2) * sizeof(wchar_t)); - GlobalUnlock(hMem); - OpenClipboard(0); - EmptyClipboard(); - SetClipboardData(CF_UNICODETEXT, hMem); - CloseClipboard(); - } - return false; -#endif // COPY_CLIPBOARD - -#ifdef EXTRA_NEWLINES - // This example extension adds extra newlines to all sentences. - sentence += L"\r\n"; return true; -#endif // EXTRA_NEWLINES +// // Your code here... +//#ifdef COPY_CLIPBOARD +// // This example extension automatically copies sentences from the hook currently selected by the user into the clipboard. +// if (sentenceInfo["current select"]) +// { +// HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (sentence.size() + 2) * sizeof(wchar_t)); +// memcpy(GlobalLock(hMem), sentence.c_str(), (sentence.size() + 2) * sizeof(wchar_t)); +// GlobalUnlock(hMem); +// OpenClipboard(0); +// EmptyClipboard(); +// SetClipboardData(CF_UNICODETEXT, hMem); +// CloseClipboard(); +// } +// return false; +//#endif // COPY_CLIPBOARD +// +//#ifdef EXTRA_NEWLINES +// // This example extension adds extra newlines to all sentences. +// sentence += L"\r\n"; +// return true; +//#endif // EXTRA_NEWLINES } diff --git a/ExampleExtension/ExtensionImpl.cpp b/ExampleExtension/ExtensionImpl.cpp index c33a42d..6735eb3 100644 --- a/ExampleExtension/ExtensionImpl.cpp +++ b/ExampleExtension/ExtensionImpl.cpp @@ -1,4 +1,9 @@ #include "extension.h" +#include "httplib.h" +#include +#include +#include +#include bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo); @@ -13,21 +18,63 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo); This function may be run concurrently with itself: please make sure it's thread safe. It will not be run concurrently with DllMain. */ + +std::string WideStringToString(const std::wstring& text) +{ + std::vector buffer((text.size() + 1) * 4); + WideCharToMultiByte(CP_UTF8, 0, text.c_str(), -1, buffer.data(), buffer.size(), nullptr, nullptr); + return buffer.data(); +} + extern "C" __declspec(dllexport) wchar_t* OnNewSentence(wchar_t* sentence, const InfoForExtension* sentenceInfo) { - try - { - std::wstring sentenceCopy(sentence); - int oldSize = sentenceCopy.size(); - if (ProcessSentence(sentenceCopy, SentenceInfo{ sentenceInfo })) - { - if (sentenceCopy.size() > oldSize) sentence = (wchar_t*)HeapReAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, sentence, (sentenceCopy.size() + 1) * sizeof(wchar_t)); - wcscpy_s(sentence, sentenceCopy.size() + 1, sentenceCopy.c_str()); - } - } - catch (SKIP) - { - *sentence = L'\0'; - } - return sentence; + try + { + std::wstring sentenceCopy(sentence); + size_t oldSize = sentenceCopy.size(); + if (ProcessSentence(sentenceCopy, SentenceInfo{ sentenceInfo })) + { + Json::Value data; + data["model"] = "qwen2:1.5b"; + data["prompt"] = "Please translate the following content into Chinese! (Return to Chinese translation directly): " + WideStringToString(sentence); + data["stream"] = false; + + Json::StreamWriterBuilder writer; + std::string json_data = Json::writeString(writer, data); + + httplib::Client cli("http://localhost:11434"); + auto res = cli.Post("/api/generate", json_data, "application/json"); + + if (res && res->status == 200) + { + Json::CharReaderBuilder reader; + Json::Value json_response; + std::istringstream s(res->body); + std::string errs; + if (Json::parseFromStream(reader, s, &json_response, &errs)) + { + std::string translated_text = json_response["response"].asString(); + + std::wstring translated_sentence = std::wstring_convert>().from_bytes(translated_text); + + if (translated_sentence.size() > oldSize) + { + sentence = (wchar_t*)HeapReAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, sentence, (translated_sentence.size() + 1) * sizeof(wchar_t)); + } + wcscpy_s(sentence, translated_sentence.size() + 1, translated_sentence.c_str()); + } + } + else + { + *sentence = L'\0'; + } + } + } + catch (SKIP) + { + *sentence = L'\0'; + } + return sentence; } + +