Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ FetchContent_Declare(
FetchContent_MakeAvailable(Catch2)

add_executable(LLMAssistantTests
src/ModelParams.cpp
src/RequestTarget.cpp
src/SimpleJson.cpp
src/TextCodec.cpp
Expand Down
2 changes: 2 additions & 0 deletions LLMAssistant.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
<ClInclude Include="src\LLMService.h" />
<ClInclude Include="src\LLMResult.h" />
<ClInclude Include="src\menuCmdID.h" />
<ClInclude Include="src\ModelParams.h" />
<ClInclude Include="src\PluginDefinition.h" />
<ClInclude Include="src\PluginInterface.h" />
<ClInclude Include="src\RequestTarget.h" />
Expand All @@ -109,6 +110,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\LLMService.cpp" />
<ClCompile Include="src\ModelParams.cpp" />
<ClCompile Include="src\NppPlugin.cpp" />
<ClCompile Include="src\PluginDefinition.cpp" />
<ClCompile Include="src\RequestTarget.cpp" />
Expand Down
46 changes: 26 additions & 20 deletions src/LLMService.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "LLMService.h"
#include "ModelParams.h"
#include "SimpleJson.h"
#include "TextCodec.h"

#include <algorithm>
#include <cwctype>
#include <mutex>
#include <sstream>

Expand All @@ -12,34 +11,24 @@
namespace {
constexpr DWORD kRequestTimeoutMilliseconds = 60000;

// Version of the saved settings. Bump it, and update isRetiredModel, to switch
// saved retired models to the default again.
// 1: retired models (gpt-4o, gpt-4.1, gpt-5, ...) switched to gpt-6-luna.
constexpr DWORD kSettingsVersion = 1;

std::wstring lastErrorMessage(const wchar_t* operation)
{
const DWORD error = GetLastError();
return std::wstring(operation) + L" failed (Win32 error " + std::to_wstring(error) + L").";
}

// Reasoning models (GPT-5 family and the o-series) reject the classic
// "max_tokens" and any non-default "temperature". They require
// "max_completion_tokens" and no temperature override. The chat-tuned
// variant "gpt-5-chat*" behaves like gpt-4o and keeps the classic params.
bool isReasoningModel(const std::wstring& model)
{
std::wstring name = model;
std::transform(name.begin(), name.end(), name.begin(), towlower);
const auto startsWith = [&](const wchar_t* prefix) { return name.rfind(prefix, 0) == 0; };

if (startsWith(L"gpt-5")) {
return !startsWith(L"gpt-5-chat");
}
return startsWith(L"o1") || startsWith(L"o3") || startsWith(L"o4");
}
}

LLMService* LLMService::instance = nullptr;

const wchar_t* LLMService::defaultModel()
{
return L"gpt-4o";
// OpenAI's fast, low-cost model and its recommended replacement for gpt-4o.
return L"gpt-6-luna";
}

LLMService::LLMService()
Expand Down Expand Up @@ -127,7 +116,7 @@ LLMResult LLMService::callLLMWithApiKey(
builder.addString("model", modelUtf8);
if (isReasoningModel(model)) {
// Reasoning tokens are billed against this budget too, so keep it
// generous to avoid empty responses from the GPT-5 family.
// generous to avoid empty responses from reasoning models.
builder.addNumber("max_completion_tokens", 4000);
} else {
builder.addNumber("max_tokens", 1000);
Expand Down Expand Up @@ -304,7 +293,22 @@ void LLMService::loadSettings()
modelBuffer[modelSize / sizeof(wchar_t)] = L'\0';
setModel(modelBuffer);
}

DWORD settingsVersion = 0;
DWORD versionSize = sizeof(settingsVersion);
DWORD versionType = REG_DWORD;
if (RegQueryValueEx(key, L"SettingsVersion", nullptr, &versionType,
reinterpret_cast<LPBYTE>(&settingsVersion), &versionSize) != ERROR_SUCCESS
|| versionType != REG_DWORD) {
settingsVersion = 0;
}
RegCloseKey(key);

// Switch a retired model to the default once. saveSettings records the
// version, so a retired model the user picks again afterwards is kept.
if (settingsVersion < kSettingsVersion && isRetiredModel(getModel())) {
setModel(defaultModel());
}
}

void LLMService::saveSettings()
Expand All @@ -322,5 +326,7 @@ void LLMService::saveSettings()
const std::wstring currentModel = getModel();
const DWORD modelSize = static_cast<DWORD>((currentModel.length() + 1) * sizeof(wchar_t));
RegSetValueEx(key, L"Model", 0, REG_SZ, reinterpret_cast<const BYTE*>(currentModel.c_str()), modelSize);
RegSetValueEx(key, L"SettingsVersion", 0, REG_DWORD, reinterpret_cast<const BYTE*>(&kSettingsVersion),
sizeof(kSettingsVersion));
RegCloseKey(key);
}
44 changes: 44 additions & 0 deletions src/ModelParams.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "ModelParams.h"

#include <algorithm>
#include <cwctype>

namespace {
std::wstring toLower(const std::wstring& text)
{
std::wstring lower = text;
std::transform(lower.begin(), lower.end(), lower.begin(), towlower);
return lower;
}

bool startsWith(const std::wstring& text, const wchar_t* prefix)
{
return text.rfind(prefix, 0) == 0;
}
}

bool isReasoningModel(const std::wstring& model)
{
const std::wstring name = toLower(model);

// Only the legacy GPT-3.5/GPT-4 families (gpt-4o, gpt-4.1, ...) and the
// chat-tuned "*-chat-latest" variants keep the classic params. Any other
// GPT model is treated as reasoning so new releases (gpt-6-luna, ...)
// work without a plugin update.
if (startsWith(name, L"gpt-3") || startsWith(name, L"gpt-4") || name.find(L"-chat") != std::wstring::npos) {
return false;
}
return startsWith(name, L"gpt-") || startsWith(name, L"o1") || startsWith(name, L"o3")
|| startsWith(name, L"o4");
}

bool isRetiredModel(const std::wstring& model)
{
const std::wstring name = toLower(model);

// See https://developers.openai.com/api/docs/deprecations.
return startsWith(name, L"gpt-3") || startsWith(name, L"gpt-4")
|| name == L"gpt-5" || startsWith(name, L"gpt-5-")
|| name == L"gpt-5.1" || startsWith(name, L"gpt-5.1-")
|| name.find(L"-chat-latest") != std::wstring::npos;
}
16 changes: 16 additions & 0 deletions src/ModelParams.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef MODELPARAMS_H
#define MODELPARAMS_H

#include <string>

// Reasoning models (every GPT model from GPT-5 onward and the o-series) reject
// the classic "max_tokens" and any non-default "temperature". They require
// "max_completion_tokens" and no temperature override.
bool isReasoningModel(const std::wstring& model);

// Models OpenAI has deprecated or already shut down: the GPT-3.5/GPT-4
// families, the original GPT-5 generation, GPT-5.1 and the "*-chat-latest"
// variants. Saved settings using one are switched to the default model.
bool isRetiredModel(const std::wstring& model);

#endif
21 changes: 10 additions & 11 deletions src/SettingsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,18 @@ void SettingsDialog::onInitDialog()
std::wstring currentKey = LLMService::getInstance().getApiKey();
SetDlgItemText(hDlg, IDC_API_KEY, currentKey.c_str());

// Populate the model selector with newer OpenAI models (gpt-4o and above).
// The combo is editable, so a brand-new model name can also be typed in
// without needing a plugin rebuild.
// Populate the model selector with OpenAI's current (non-deprecated)
// models. The combo is editable, so a brand-new or older model name can
// also be typed in without needing a plugin rebuild.
HWND combo = GetDlgItem(hDlg, IDC_MODEL_COMBO);
static const wchar_t* const kModels[] = {
L"gpt-5",
L"gpt-5-mini",
L"gpt-5-nano",
L"gpt-5-chat-latest",
L"gpt-4.1",
L"gpt-4.1-mini",
L"gpt-4.1-nano",
L"gpt-4o"
L"gpt-6-luna",
L"gpt-6-astra",
L"gpt-6.1-sol",
L"gpt-6-sol",
L"gpt-5.6-sol",
L"gpt-5.6-terra",
L"gpt-5.6-luna"
};
for (const wchar_t* model : kModels) {
SendMessage(combo, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(model));
Expand Down
43 changes: 43 additions & 0 deletions tests/CoreTests.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "ModelParams.h"
#include "RequestTarget.h"
#include "SimpleJson.h"
#include "TextCodec.h"
Expand Down Expand Up @@ -42,6 +43,48 @@ TEST_CASE("OpenAI errors are recognized separately from content")
REQUIRE(parser.extractContent().empty());
}

TEST_CASE("GPT-5 and newer models use reasoning parameters")
{
REQUIRE(isReasoningModel(L"gpt-6-luna"));
REQUIRE(isReasoningModel(L"gpt-6-astra"));
REQUIRE(isReasoningModel(L"gpt-6.1-sol"));
REQUIRE(isReasoningModel(L"gpt-5.6-terra"));
REQUIRE(isReasoningModel(L"GPT-5-mini"));
REQUIRE(isReasoningModel(L"o4-mini"));
}

TEST_CASE("Legacy and chat-tuned models keep classic parameters")
{
REQUIRE_FALSE(isReasoningModel(L"gpt-4o"));
REQUIRE_FALSE(isReasoningModel(L"gpt-4.1-mini"));
REQUIRE_FALSE(isReasoningModel(L"gpt-3.5-turbo"));
REQUIRE_FALSE(isReasoningModel(L"gpt-5-chat-latest"));
REQUIRE_FALSE(isReasoningModel(L"chatgpt-4o-latest"));
}

TEST_CASE("Deprecated and shut-down models are retired")
{
REQUIRE(isRetiredModel(L"gpt-4o"));
REQUIRE(isRetiredModel(L"GPT-4o-mini"));
REQUIRE(isRetiredModel(L"gpt-4.1-nano"));
REQUIRE(isRetiredModel(L"gpt-3.5-turbo"));
REQUIRE(isRetiredModel(L"gpt-5"));
REQUIRE(isRetiredModel(L"gpt-5-mini"));
REQUIRE(isRetiredModel(L"gpt-5-chat-latest"));
REQUIRE(isRetiredModel(L"gpt-5.1"));
REQUIRE(isRetiredModel(L"gpt-5.2-chat-latest"));
}

TEST_CASE("Current models are not retired")
{
REQUIRE_FALSE(isRetiredModel(L"gpt-6-luna"));
REQUIRE_FALSE(isRetiredModel(L"gpt-6.1-sol"));
REQUIRE_FALSE(isRetiredModel(L"gpt-5.6-terra"));
REQUIRE_FALSE(isRetiredModel(L"gpt-5.5"));
REQUIRE_FALSE(isRetiredModel(L"gpt-5.2"));
REQUIRE_FALSE(isRetiredModel(L"o4-mini"));
}

TEST_CASE("Selection fallback captures the complete document range")
{
const TextRange range = resolveTextRange(12, 12, 100);
Expand Down
Loading