Skip to content

Commit

Permalink
#345 updated GView to support Query Interface
Browse files Browse the repository at this point in the history
#346 added initial support for LLMs
  • Loading branch information
rzaharia committed Oct 5, 2024
1 parent 9806549 commit e85eee3
Show file tree
Hide file tree
Showing 11 changed files with 414 additions and 7 deletions.
6 changes: 6 additions & 0 deletions GViewCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ target_link_libraries(${PROJECT_NAME} PRIVATE unofficial::sqlite3::sqlite3)
find_package(re2 CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE re2::re2)

find_package(CURL REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE CURL::libcurl nlohmann_json::nlohmann_json)

find_package(nlohmann_json REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json)

if (MSVC)
add_compile_options(-W3)
elseif (APPLE)
Expand Down
26 changes: 26 additions & 0 deletions GViewCore/include/GView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,31 @@ namespace Utils
};
} // namespace Utils

namespace CommonInterfaces
{
namespace SmartAssistants
{
struct CORE_EXPORT SmartAssistantPromptInterface
{
virtual std::string AskSmartAssistant(std::string_view prompt, bool& isSuccess) = 0;
virtual ~SmartAssistantPromptInterface() = default;
};
struct CORE_EXPORT SmartAssistantRegisterInterface : SmartAssistantPromptInterface
{
virtual std::string_view GetSmartAssistantName() const = 0;
virtual std::string_view GetSmartAssistantDescription() const = 0;
virtual void ReceiveConfigToken(std::string_view config_data) = 0;
};
}// namespace SmartAssistants

struct CORE_EXPORT QueryInterface
{
virtual bool RegisterSmartAssistantInterface(Pointer<SmartAssistants::SmartAssistantRegisterInterface> smartAssistantInterface) = 0;
virtual SmartAssistants::SmartAssistantPromptInterface* GetSmartAssistantInterface() = 0;
virtual ~QueryInterface() = default;
};
} // namespace CommonInterfaces

namespace Hashes
{
class CORE_EXPORT Adler32
Expand Down Expand Up @@ -1487,6 +1512,7 @@ namespace View
virtual uint32 GetViewsCount() = 0;
virtual Reference<ViewControl> GetViewByIndex(uint32 index) = 0;
virtual bool SetViewByIndex(uint32 index) = 0;
virtual CommonInterfaces::QueryInterface* GetQueryInterface() = 0;

template <typename T>
inline bool CreateViewer(const std::optional<std::string_view> name = {})
Expand Down
1 change: 1 addition & 0 deletions GViewCore/src/App/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ target_sources(GViewCore PRIVATE
TutorialWindow.cpp
AboutWindow.cpp
KeyConfiguratorWindow.cpp
QueryInterface.cpp
)
4 changes: 4 additions & 0 deletions GViewCore/src/App/FileWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ FileWindow::FileWindow(std::unique_ptr<GView::Object> _obj, Reference<GView::App
// set the name
this->SetText(obj->GetName());
this->SetTag(obj->GetContentType()->GetTypeName(), "");

queryInterface.fileWindow = this;
}
Reference<GView::Object> FileWindow::GetObject()
{
Expand Down Expand Up @@ -298,6 +300,8 @@ void FileWindow::Start()
{
this->view->SetCurrentTabPageByIndex(0);
this->view->SetFocus();

queryInterface.Start();
}

bool FileWindow::UpdateKeys(KeyboardControlsInterface* impl)
Expand Down
3 changes: 2 additions & 1 deletion GViewCore/src/App/Instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ bool Instance::Init()
}

CHECK(BuildMainMenus(), false, "Fail to create bundle menus !");
this->defaultPlugin.Init();
this->defaultPlugin.InitDefaultPlugin();

// set up handlers
auto dsk = AppCUI::Application::GetDesktop();
Expand Down Expand Up @@ -389,6 +389,7 @@ bool Instance::Add(
// instantiate window
while (true) {
CHECKBK(plg->PopulateWindow(win.get()), "Failed to populate file window!");
CHECKBK(Type::InterfaceTabs::PopulateWindowSmartAssistantsTab(win.get()), "Failed to populate file window!");
win->Start(); // starts the window and set focus

auto res = AppCUI::Application::AddWindow(std::move(win), parentWindow);
Expand Down
177 changes: 177 additions & 0 deletions GViewCore/src/App/QueryInterface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#include <cassert>

#include "Internal.hpp"

using namespace GView::App;
using namespace GView::App::InstanceCommands;
using namespace GView::View;
using namespace AppCUI::Input;

#define SMART_ASSISTANTS_CONFIGURATION_NAME "SmartAssistants"
#define BUTTON_1_ID 1

GView::CommonInterfaces::QueryInterface* FileWindow::GetQueryInterface()
{
return &queryInterface;
}

using GView::CommonInterfaces::SmartAssistants::SmartAssistantRegisterInterface;

class SmartAssistantEntryTab : public AppCUI::Controls::TabPage
{
SmartAssistantRegisterInterface* smartAssistant;
Reference<AppCUI::Controls::Label> value;
Reference<AppCUI::Controls::Panel> chatHistory;
Reference<AppCUI::Controls::TextField> prompt;
Reference<AppCUI::Controls::Button> sendButton;
uint32 newY;

LocalString<128> gemini;

public:
SmartAssistantEntryTab(std::string_view caption, SmartAssistantRegisterInterface* smartAssistant) : TabPage(caption), smartAssistant(smartAssistant)
{
value = Factory::Label::Create(this, "Gemini", "x:1,y:1,w:60");
chatHistory = Factory::Panel::Create(this, "ChatHistory", "x:1,y:2,h:20,w:60");
newY = 0;

prompt = Factory::TextField::Create(this, "", "x:1,y:23,h:4,w:53");
sendButton = Factory::Button::Create(this, "Send", "x:54,y:25,h:4,w:6", BUTTON_1_ID);
}

bool OnEvent(Reference<Control>, Event evnt, int controlID) override
{
if (evnt == Event::ButtonClicked) {
if (controlID == BUTTON_1_ID) {
std::string text = prompt->GetText();
if (text.empty()) {
text = "Give me a prime number";
// return true;
}

LocalString<32> newLabelLocation;
newLabelLocation.SetFormat("x:1,y:%d,w:4,h:5", newY);
Factory::Label::Create(chatHistory, "You: ", newLabelLocation.GetText());
newLabelLocation.SetFormat("x:5,y:%d,w:53,h:5", newY);
Factory::Button::Create(chatHistory, text, newLabelLocation.GetText());
newY += 2;

bool success = false;
auto result = smartAssistant->AskSmartAssistant(text, success);

newLabelLocation.SetFormat("x:1,y:%d,w:11,h:1", newY);
const char* assistantLabel = success ? "Assistant: " : "Error:";
Factory::Label::Create(chatHistory, assistantLabel, newLabelLocation.GetText());

newLabelLocation.SetFormat("x:11,y:%d,w:47,h:1", newY);
Factory::Button::Create(chatHistory, result, newLabelLocation.GetText());
newY += 2;
prompt->SetText("");
return true;
}
return true;
}
return false;
}

void MarkNoConfig()
{
prompt->SetEnabled(false);

LocalString<128> textData;
textData.SetFormat(
"Error: No config token found!\nPlease add \"%s\" to \"%s\" section in GView.ini",
smartAssistant->GetSmartAssistantName().data(),
SMART_ASSISTANTS_CONFIGURATION_NAME);

prompt->SetText(textData);
sendButton->SetEnabled(false);
}
};

class SmartAssistantsTab : public AppCUI::Controls::TabPage
{
Reference<Tab> tabs;
uint32 tabsCount;

public:
SmartAssistantsTab() : TabPage("Smart &Assistants"),tabsCount(0)
{
tabs = Factory::Tab::Create(this, "l:1,t:1,r:1,b:1", TabFlags::TopTabs | TabFlags::TransparentBackground);
}

void AddSmartAssistant(SmartAssistantRegisterInterface* registerInterface)
{
tabs->AddControl(Pointer(new SmartAssistantEntryTab(registerInterface->GetSmartAssistantName(), registerInterface)));
++tabsCount;
}

void MarkLastAssistantNoConfig()
{
if (!tabs->SetCurrentTabPageByIndex(tabsCount - 1, false))
return;
tabs->GetCurrentTab().ToObjectRef<SmartAssistantEntryTab>()->MarkNoConfig();
tabs->SetCurrentTabPageByIndex(0, false);
}
};

namespace GView::App::QueryInterfaceImpl
{

bool GViewQueryInterface::RegisterSmartAssistantInterface(Pointer<SmartAssistantRegisterInterface> registerInterface)
{
const auto newAssistantName = registerInterface->GetSmartAssistantName();
if (newAssistantName.empty()) {
return false;
}
for (const auto& smartAssistant : smartAssistants) {
if (smartAssistant->GetSmartAssistantName().compare(newAssistantName) == 0) {
return false;
}
}
smartAssistants.push_back(std::move(registerInterface));
return true;
}
SmartAssistantPromptInterface* GViewQueryInterface::GetSmartAssistantInterface()
{
return nullptr;
}

void GViewQueryInterface::Start()
{
if (smartAssistants.empty()) {
return;
}

bool hasSmartAssistantConfigDat = false;
auto SmartAssistants = IniSection();
const auto settings = Application::GetAppSettings();
if (settings->HasSection(SMART_ASSISTANTS_CONFIGURATION_NAME)) {
SmartAssistants = settings->GetSection(SMART_ASSISTANTS_CONFIGURATION_NAME);
hasSmartAssistantConfigDat = true;
}

auto ptrSmartAssistantsTab = Pointer<TabPage>(new SmartAssistantsTab());
const auto convertedPtr = static_cast<SmartAssistantsTab*>(ptrSmartAssistantsTab.get());

for (auto& smartAssistant : smartAssistants) {
convertedPtr->AddSmartAssistant(smartAssistant.get());
bool hasConfig = false;
if (hasSmartAssistantConfigDat) {
auto currentData = SmartAssistants[smartAssistant->GetSmartAssistantName()];
if (currentData.HasValue()) {
auto dataValue = currentData.AsString();
if (dataValue.has_value()) {
hasConfig = true;
smartAssistant->ReceiveConfigToken(dataValue.value());
}
}
}
if (!hasConfig) {
convertedPtr->MarkLastAssistantNoConfig();
}
}
fileWindow->AddPanel(std::move(ptrSmartAssistantsTab), true);
}

} // namespace GView::App::QueryInterfaceImpl
1 change: 1 addition & 0 deletions GViewCore/src/Type/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
target_sources(GViewCore PRIVATE
DefaultTypePlugin.cpp
Plugin.cpp
SmartAssistantPlugin.cpp
Matcher.cpp
MagicMatcher.cpp
StartsWithMatcher.cpp
Expand Down
2 changes: 1 addition & 1 deletion GViewCore/src/Type/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Plugin::Plugin()
this->fnCreateInstance = nullptr;
this->fnPopulateWindow = nullptr;
}
void Plugin::Init()
void Plugin::InitDefaultPlugin()
{
// default initialization
this->fnValidate = DefaultTypePlugin::Validate;
Expand Down
Loading

0 comments on commit e85eee3

Please sign in to comment.