This commit is contained in:
Chenx221 2024-09-10 00:13:16 +08:00
parent d6791e949c
commit 57b5b04163
4 changed files with 92 additions and 43 deletions

View File

@ -24,31 +24,32 @@
<Keyword>Win32Proj</Keyword>
<RootNamespace>ExampleExtension</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ProjectName>LocalMtExtension</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>

View File

@ -29,26 +29,26 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>

View File

@ -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
}

View File

@ -1,4 +1,9 @@
#include "extension.h"
#include "httplib.h"
#include <json/json.h>
#include <locale>
#include <codecvt>
#include <string>
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo);
@ -13,16 +18,56 @@ 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<char> 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();
size_t 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());
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<std::codecvt_utf8<wchar_t>>().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)
@ -31,3 +76,5 @@ extern "C" __declspec(dllexport) wchar_t* OnNewSentence(wchar_t* sentence, const
}
return sentence;
}