wxMaxima
Loading...
Searching...
No Matches
AiProvider.h
Go to the documentation of this file.
1// -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2//
3// Copyright (C) 2026 Gunter Königsmann <wxMaxima@physikbuch.de>
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15//
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19//
20// SPDX-License-Identifier: GPL-2.0+
21
22#ifndef AIPROVIDER_H
23#define AIPROVIDER_H
24
25#include "precomp.h"
26#include <wx/wx.h>
27#include <functional>
28#include <memory>
29#include <vector>
30
48 wxString role;
49 wxString content;
50};
51
58enum class AiProviderKind { None = 0, Anthropic = 1, OpenAI = 2, Google = 3, Qwen = 4, Custom = 5 };
59
71enum class AiProviderShape { Anthropic, OpenAiCompatible, Google };
72
76
86 wxString id;
88 wxString name;
89 AiProviderShape shape = AiProviderShape::OpenAiCompatible;
93 wxString baseUrl;
94 wxString model;
95};
96
106 wxString name;
107 wxString baseUrl;
108 wxString model;
109};
110
115std::vector<AiLocalServerPreset> AiKnownLocalServerPresets();
116
120wxString NewAiCustomProviderId();
121
126std::vector<AiCustomProviderConfig> ParseAiCustomProviders(const wxString &json);
127
130wxString SerializeAiCustomProviders(const std::vector<AiCustomProviderConfig> &providers);
131
134
139
148
158wxString AiProviderBaseUrl(AiProviderKind kind);
159
170
180public:
181 AiProvider(wxString baseUrl, wxString apiKey, wxString model)
182 : m_baseUrl(std::move(baseUrl)), m_apiKey(std::move(apiKey)),
183 m_model(std::move(model)) {}
184 virtual ~AiProvider() = default;
185
186 virtual AiProviderKind Kind() const = 0;
191 wxString Name() const {
192 return m_displayName.IsEmpty() ? AiProviderKindName(Kind()) : m_displayName;
193 }
194 void SetDisplayName(const wxString &name) { m_displayName = name; }
195
203 static bool SecretStoreAvailable();
204
209 static void SaveApiKey(const wxString &service, const wxString &apiKey);
213 static wxString LoadApiKey(const wxString &service);
214 static void DeleteApiKey(const wxString &service);
219 static wxString CustomProviderSecretService(const wxString &id);
225 static wxString BuiltinProviderSecretService(AiProviderKind kind);
226
229 virtual wxString RequestUrl() const { return m_baseUrl; }
230
233 virtual std::vector<std::pair<wxString, wxString>> AuthHeaders() const = 0;
234
240 virtual wxString BuildRequestBody(const wxString &context,
241 const std::vector<AiChatMessage> &history) const = 0;
242
246 virtual wxString ParseReply(const wxString &responseBody) const = 0;
247
264 static void SendChat(std::shared_ptr<const AiProvider> self, wxEvtHandler *owner,
265 const wxString &context,
266 const std::vector<AiChatMessage> &history,
267 std::function<void(bool ok, const wxString &replyOrError)> callback);
268
273 static bool NetworkingAvailable();
274
275protected:
276 wxString m_baseUrl;
277 wxString m_apiKey;
278 wxString m_model;
279 wxString m_displayName;
280};
281
285class AiProviderError : public std::runtime_error {
286public:
287 explicit AiProviderError(const std::string &msg) : std::runtime_error(msg) {}
288};
289
294std::shared_ptr<AiProvider> MakeAiProvider(AiProviderKind kind, const wxString &apiKey,
295 const wxString &model);
296
303std::shared_ptr<AiProvider> MakeAiProviderForShape(AiProviderShape shape,
304 const wxString &displayName,
305 const wxString &baseUrl,
306 const wxString &apiKey,
307 const wxString &model);
308
309#endif // AIPROVIDER_H
wxString AiProviderApiKeyUrl(AiProviderKind kind)
Where to go to create/find an API key for this provider – shown as a link next to that provider's key...
Definition: AiProvider.cpp:230
std::vector< AiCustomProviderConfig > ParseAiCustomProviders(const wxString &json)
Parses Configuration::AiCustomProvidersJson() back into a list; returns an empty list for an empty or...
Definition: AiProvider.cpp:337
AiProviderShape
Which request/response wire format a provider uses. The four built-in AiProviderKinds each hardcode o...
Definition: AiProvider.h:71
std::vector< AiLocalServerPreset > AiKnownLocalServerPresets()
A short, hand-picked list of common local LLM servers – not exhaustive, just the ones popular enough ...
Definition: AiProvider.cpp:309
wxString AiProviderBaseUrl(AiProviderKind kind)
A built-in provider's fixed request URL (Google's own, without the "<model>:generateContent" suffix i...
Definition: AiProvider.cpp:251
wxString AiProviderShapeName(AiProviderShape shape)
Human-readable name for the shape picker in the "Add custom provider" dialog.
Definition: AiProvider.cpp:200
std::shared_ptr< AiProvider > MakeAiProviderForShape(AiProviderShape shape, const wxString &displayName, const wxString &baseUrl, const wxString &apiKey, const wxString &model)
Builds a provider for a user-added custom entry: reuses the exact same request/response-shape impleme...
Definition: AiProvider.cpp:284
wxString AiProviderModelListUrl(AiProviderKind kind)
Where to see this provider's current list of available model ids – shown as a link next to Options' m...
Definition: AiProvider.cpp:240
std::shared_ptr< AiProvider > MakeAiProvider(AiProviderKind kind, const wxString &apiKey, const wxString &model)
Builds the provider for this kind, or nullptr for AiProviderKind::None. Never called with AiProviderK...
Definition: AiProvider.cpp:263
wxString SerializeAiCustomProviders(const std::vector< AiCustomProviderConfig > &providers)
The inverse of ParseAiCustomProviders(), for Configuration:: AiCustomProvidersJson()'s setter.
Definition: AiProvider.cpp:374
wxString NewAiCustomProviderId()
A fresh id for a new custom provider (see AiCustomProviderConfig::id) – never shown to the user,...
Definition: AiProvider.cpp:322
wxString AiProviderKindName(AiProviderKind kind)
Human-readable name for Options/the sidebar's status line.
Definition: AiProvider.cpp:184
wxString AiProviderDefaultModel(AiProviderKind kind)
This provider's built-in default model id, shown as Options' initial value for a not-yet-configured m...
Definition: AiProvider.cpp:209
AiProviderKind
Which AI service to talk to. Persisted as a plain int in Configuration; keep existing values stable a...
Definition: AiProvider.h:58
Thrown by ParseReply() for a response that doesn't parse or doesn't have the expected shape – distinc...
Definition: AiProvider.h:285
Talks to one external AI provider's chat completion HTTP API.
Definition: AiProvider.h:179
static bool NetworkingAvailable()
True if this build of wxWidgets has wxWebRequest at all (>= 3.1.5, built with a working backend) – if...
Definition: AiProvider.cpp:455
virtual std::vector< std::pair< wxString, wxString > > AuthHeaders() const =0
Any additional headers this provider's auth scheme needs, name/value pairs, beyond the "POST JSON" co...
static void SaveApiKey(const wxString &service, const wxString &apiKey)
Saves (or, for an empty key, deletes) an API key for service – a stable identifier: AiProviderKindNam...
Definition: AiProvider.cpp:409
static wxString BuiltinProviderSecretService(AiProviderKind kind)
The secret-store service name for one of the four built-in kinds – just AiProviderKindName(kind),...
Definition: AiProvider.cpp:397
wxString Name() const
AiProviderKindName(Kind()) for a built-in provider, or the user's own chosen name for a custom one (K...
Definition: AiProvider.h:191
static wxString CustomProviderSecretService(const wxString &id)
The secret-store service name for a custom provider's own id – kept distinct from a bare built-in kin...
Definition: AiProvider.cpp:393
virtual wxString BuildRequestBody(const wxString &context, const std::vector< AiChatMessage > &history) const =0
Builds the JSON request body for one chat turn.
virtual wxString RequestUrl() const
The full URL SendChat() will POST to. Virtual so Google's provider can append the model id (its endpo...
Definition: AiProvider.h:229
static void SendChat(std::shared_ptr< const AiProvider > self, wxEvtHandler *owner, const wxString &context, const std::vector< AiChatMessage > &history, std::function< void(bool ok, const wxString &replyOrError)> callback)
Sends one chat turn asynchronously via wxWebRequest and calls callback(ok, replyOrError) exactly once...
Definition: AiProvider.cpp:463
virtual wxString ParseReply(const wxString &responseBody) const =0
Extracts the assistant's reply text from a successful (2xx) response body.
static bool SecretStoreAvailable()
True if this build of wxWidgets has a working wxSecretStore backend (>= 3.1.1, compiled with wxUSE_SE...
Definition: AiProvider.cpp:401
static wxString LoadApiKey(const wxString &service)
Returns the stored key for service, or an empty string if none is stored (also the result if !...
Definition: AiProvider.cpp:429
One turn of a chat conversation.
Definition: AiProvider.h:44
wxString role
"user" or "assistant" – never "system"/"model"/etc.; each provider's own request-building maps this p...
Definition: AiProvider.h:48
One user-added custom provider, as persisted in Configuration's AiCustomProvidersJson() (everything e...
Definition: AiProvider.h:81
wxString id
Stable, internally-generated identifier – never shown in the UI and never reused, so renaming a provi...
Definition: AiProvider.h:86
wxString baseUrl
Full request URL for OpenAI-compatible/Anthropic shapes; for Google's shape, the part before "<model>...
Definition: AiProvider.h:93
wxString name
User-chosen display name, shown in the provider dropdown.
Definition: AiProvider.h:88
One well-known local AI server's connection defaults, offered as a quick-fill preset in the "Add cust...
Definition: AiProvider.h:105