Compare commits
7 Commits
feat/autop
...
feat/ai-cu
| Author | SHA1 | Date | |
|---|---|---|---|
|
9913344bbf
|
|||
|
91631aa91a
|
|||
|
622054fc6d
|
|||
|
ca92d29179
|
|||
|
72aa603147
|
|||
|
15dfc2bdb4
|
|||
|
02e679c277
|
@@ -1435,13 +1435,17 @@ ai_session_switch(AISession* session, const gchar* provider_name,
|
||||
* This is the thread-safe variant used by _ai_request_thread() which holds
|
||||
* the session lock while calling it.
|
||||
*
|
||||
* Custom provider settings are merged into the payload as additional JSON
|
||||
* key-value pairs alongside "model" and "input".
|
||||
*
|
||||
* @param provider The AI provider (for custom settings)
|
||||
* @param model The model identifier
|
||||
* @param history GList of AIMessage* (must be held under session lock)
|
||||
* @param prompt The new user prompt to append
|
||||
* @return Newly allocated JSON string (caller must free)
|
||||
*/
|
||||
static gchar*
|
||||
_build_json_payload_from_list(const gchar* model, GList* history, const gchar* prompt)
|
||||
_build_json_payload_from_list(AIProvider* provider, const gchar* model, GList* history, const gchar* prompt)
|
||||
{
|
||||
GString* messages_json = g_string_new("");
|
||||
GList* curr = history;
|
||||
@@ -1468,11 +1472,37 @@ _build_json_payload_from_list(const gchar* model, GList* history, const gchar* p
|
||||
g_string_append_printf(messages_json, "{\"role\":\"user\",\"content\":\"%s\"}", escaped_prompt);
|
||||
|
||||
auto_gchar gchar* escaped_model = ai_json_escape(model);
|
||||
gchar* json_payload = g_strdup_printf(
|
||||
"{\"model\":\"%s\",\"input\":[%s],\"stream\":false,\"store\":false}",
|
||||
escaped_model, messages_json->str);
|
||||
|
||||
/* Build custom settings JSON fragment */
|
||||
GString* custom_json = g_string_new("");
|
||||
if (provider && provider->settings) {
|
||||
GHashTableIter iter;
|
||||
gpointer key, value;
|
||||
g_hash_table_iter_init(&iter, provider->settings);
|
||||
while (g_hash_table_iter_next(&iter, &key, &value)) {
|
||||
auto_gchar gchar* escaped_key = ai_json_escape((gchar*)key);
|
||||
auto_gchar gchar* escaped_val = ai_json_escape((gchar*)value);
|
||||
if (custom_json->len > 0) {
|
||||
g_string_append_c(custom_json, ',');
|
||||
}
|
||||
g_string_append_printf(custom_json, "\"%s\": %s", escaped_key, escaped_val);
|
||||
}
|
||||
}
|
||||
|
||||
/* Assemble final payload: model, messages, custom settings, then fixed keys */
|
||||
gchar* json_payload;
|
||||
if (custom_json->len > 0) {
|
||||
json_payload = g_strdup_printf(
|
||||
"{\"model\":\"%s\",\"messages\":[%s],%s,\"stream\":false}",
|
||||
escaped_model, messages_json->str, custom_json->str);
|
||||
} else {
|
||||
json_payload = g_strdup_printf(
|
||||
"{\"model\":\"%s\",\"messages\":[%s],\"stream\":false}",
|
||||
escaped_model, messages_json->str);
|
||||
}
|
||||
|
||||
g_string_free(messages_json, TRUE);
|
||||
g_string_free(custom_json, TRUE);
|
||||
return json_payload;
|
||||
}
|
||||
|
||||
@@ -1482,12 +1512,7 @@ ai_parse_response(const gchar* response_json)
|
||||
if (!response_json || strlen(response_json) == 0)
|
||||
return NULL;
|
||||
|
||||
/* Perplexity /v1/agent: {"output":[{"content":[{"text":"...","type":"output_text"}]}]}
|
||||
* Legacy OpenAI: {"choices":[{"message":{"content":"..."}}]} */
|
||||
gchar* text = _extract_json_string(response_json, "text");
|
||||
if (text)
|
||||
return text;
|
||||
|
||||
/* OpenAI /v1/chat/completions: {"choices":[{"message":{"content":"..."}}]} */
|
||||
return _extract_json_string(response_json, "content");
|
||||
}
|
||||
|
||||
@@ -1566,7 +1591,7 @@ _ai_request_thread(gpointer data)
|
||||
/* Build JSON payload from history + prompt (under lock), then add user message to history */
|
||||
log_debug("[AI-THREAD] Building JSON payload...");
|
||||
pthread_mutex_lock(&session->lock);
|
||||
auto_gchar gchar* json_payload = _build_json_payload_from_list(local_model, session->history, prompt);
|
||||
auto_gchar gchar* json_payload = _build_json_payload_from_list(local_provider, local_model, session->history, prompt);
|
||||
|
||||
/* Add user message to history now (it's in JSON but not yet in history) */
|
||||
AIMessage* user_msg = g_new0(AIMessage, 1);
|
||||
@@ -1587,7 +1612,7 @@ _ai_request_thread(gpointer data)
|
||||
|
||||
/* Build request URL */
|
||||
const gchar* api_url = local_provider->api_url;
|
||||
auto_gchar gchar* request_url = g_strdup_printf("%s%sv1/responses", api_url, g_str_has_suffix(api_url, "/") ? "" : "/");
|
||||
auto_gchar gchar* request_url = g_strdup_printf("%s%sv1/chat/completions", api_url, g_str_has_suffix(api_url, "/") ? "" : "/");
|
||||
log_debug("[AI-THREAD] API URL: %s", api_url);
|
||||
log_debug("[AI-THREAD] API Request URL: %s", request_url);
|
||||
log_debug("[AI-THREAD] Model: %s", local_model);
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
#ifndef PROF_GIT_BRANCH
|
||||
#define PROF_GIT_BRANCH @PROF_GIT_BRANCH@
|
||||
#endif
|
||||
#ifndef PROF_GIT_REVISION
|
||||
#define PROF_GIT_REVISION @PROF_GIT_REVISION@
|
||||
#endif
|
||||
@@ -519,13 +519,15 @@ p_gpg_sign(const gchar* const str, const gchar* const fp)
|
||||
}
|
||||
|
||||
gchar*
|
||||
p_gpg_encrypt(const gchar* const barejid, const gchar* const message, const gchar* const fp)
|
||||
p_gpg_encrypt(const gchar* const barejid, const gchar* const message, const gchar* const fp, gchar** err)
|
||||
{
|
||||
ProfPGPPubKeyId* pubkeyid = g_hash_table_lookup(pubkeys, barejid);
|
||||
if (!pubkeyid) {
|
||||
*err = g_strdup("No PGP key found for recipient");
|
||||
return NULL;
|
||||
}
|
||||
if (!pubkeyid->id) {
|
||||
*err = g_strdup("No key ID found for recipient");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -538,6 +540,7 @@ p_gpg_encrypt(const gchar* const barejid, const gchar* const message, const gcha
|
||||
gpgme_ctx_t ctx;
|
||||
gpgme_error_t error = gpgme_new(&ctx);
|
||||
if (error) {
|
||||
*err = g_strdup_printf("Failed to create GPGME context: %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
||||
log_error("GPG: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
||||
return NULL;
|
||||
}
|
||||
@@ -545,6 +548,7 @@ p_gpg_encrypt(const gchar* const barejid, const gchar* const message, const gcha
|
||||
gpgme_key_t receiver_key;
|
||||
error = gpgme_get_key(ctx, pubkeyid->id, &receiver_key, 0);
|
||||
if (error || receiver_key == NULL) {
|
||||
*err = g_strdup_printf("Failed to get receiver key '%s': %s %s", pubkeyid->id, gpgme_strsource(error), gpgme_strerror(error));
|
||||
log_error("GPG: Failed to get receiver_key. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
||||
gpgme_release(ctx);
|
||||
return NULL;
|
||||
@@ -554,8 +558,10 @@ p_gpg_encrypt(const gchar* const barejid, const gchar* const message, const gcha
|
||||
gpgme_key_t sender_key = NULL;
|
||||
error = gpgme_get_key(ctx, fp, &sender_key, 0);
|
||||
if (error || sender_key == NULL) {
|
||||
*err = g_strdup_printf("Failed to get sender key '%s': %s %s", fp, gpgme_strsource(error), gpgme_strerror(error));
|
||||
log_error("GPG: Failed to get sender_key. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
||||
gpgme_release(ctx);
|
||||
gpgme_key_unref(receiver_key);
|
||||
return NULL;
|
||||
}
|
||||
keys[1] = sender_key;
|
||||
@@ -574,7 +580,8 @@ p_gpg_encrypt(const gchar* const barejid, const gchar* const message, const gcha
|
||||
gpgme_key_unref(sender_key);
|
||||
|
||||
if (error) {
|
||||
log_error("GPG: Failed to encrypt message. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
||||
*err = g_strdup_printf("Encryption failed: (%s) %s", gpgme_strsource(error), gpgme_strerror(error));
|
||||
log_error("GPG: Failed to encrypt message. (%s) %s", gpgme_strsource(error), gpgme_strerror(error));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ gboolean p_gpg_available(const gchar* const barejid);
|
||||
const gchar* p_gpg_libver(void);
|
||||
gchar* p_gpg_sign(const gchar* const str, const gchar* const fp);
|
||||
void p_gpg_verify(const gchar* const barejid, const gchar* const sign);
|
||||
gchar* p_gpg_encrypt(const gchar* const barejid, const gchar* const message, const gchar* const fp);
|
||||
gchar* p_gpg_encrypt(const gchar* const barejid, const gchar* const message, const gchar* const fp, gchar** err);
|
||||
gchar* p_gpg_decrypt(const gchar* const cipher);
|
||||
void p_gpg_free_decrypted(gchar* decrypted);
|
||||
gchar* p_gpg_autocomplete_key(const gchar* const search_str, gboolean previous, void* context);
|
||||
|
||||
@@ -43,7 +43,7 @@
|
||||
#include "ai/ai_client.h"
|
||||
|
||||
static const int PAD_MIN_HEIGHT = 100;
|
||||
static const int PAD_THRESHOLD = 12000; // above buffer cap (~9000): reclaims dead pad space, never fires while scrolling
|
||||
static const int PAD_DEAD_SPACE_LIMIT = 2000; // reclaim once the pad holds this much dead space (cursor past live buffer); size-independent, so it never fires while scrolling
|
||||
static gboolean _in_redraw = FALSE;
|
||||
|
||||
static void
|
||||
@@ -56,9 +56,10 @@ _win_ensure_pad_capacity(ProfWin* window, WINDOW* win, int lines_needed)
|
||||
int cur_height = getmaxy(win);
|
||||
int cur_width = getmaxx(win);
|
||||
if (lines_needed >= cur_height - 1) {
|
||||
// If we are getting too large, trigger a redraw to clean up old lines
|
||||
// but only if we are not already in a redraw process.
|
||||
if (window && cur_height >= PAD_THRESHOLD && !_in_redraw) {
|
||||
// redraw to reclaim dead pad space (cursor far past live buffer, e.g. a long
|
||||
// append-only session); dead space never accrues while scrolling, only here
|
||||
int dead = window ? getcury(win) - window->layout->buffer->lines : 0;
|
||||
if (window && dead > PAD_DEAD_SPACE_LIMIT && !_in_redraw) {
|
||||
win_redraw(window);
|
||||
} else {
|
||||
// resize to required lines + some buffer for next messages
|
||||
|
||||
@@ -2432,7 +2432,8 @@ _disco_info_response_id_handler_onconnect(xmpp_stanza_t* const stanza, void* con
|
||||
}
|
||||
|
||||
// Prevent repetitions by avoiding checks of disco items (from connection_set_disco_items)
|
||||
if (from && g_ascii_strcasecmp(from, connection_get_domain()) == 0) {
|
||||
const char* domain = connection_get_domain();
|
||||
if (from && domain && g_ascii_strcasecmp(from, domain) == 0) {
|
||||
_disco_autoping_warning_message(features);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -467,12 +467,15 @@ message_send_chat_pgp(const char* const barejid, const char* const msg, gboolean
|
||||
auto_char char* jid = chat_session_get_jid(barejid);
|
||||
char* id = connection_create_stanza_id();
|
||||
|
||||
ProfWin* current = wins_get_current();
|
||||
|
||||
xmpp_stanza_t* message = NULL;
|
||||
#ifdef HAVE_LIBGPGME
|
||||
ProfAccount* account = accounts_get_account(session_get_account_name());
|
||||
if (account->pgp_keyid) {
|
||||
auto_jid Jid* jidp = jid_create(jid);
|
||||
auto_gchar gchar* encrypted = p_gpg_encrypt(jidp->barejid, msg, account->pgp_keyid);
|
||||
auto_gchar gchar* err = NULL;
|
||||
auto_gchar gchar* encrypted = p_gpg_encrypt(jidp->barejid, msg, account->pgp_keyid, &err);
|
||||
if (encrypted) {
|
||||
message = xmpp_message_new(ctx, STANZA_TYPE_CHAT, jid, id);
|
||||
xmpp_message_set_body(message, "This message is encrypted (XEP-0027).");
|
||||
@@ -486,18 +489,31 @@ message_send_chat_pgp(const char* const barejid, const char* const msg, gboolean
|
||||
xmpp_stanza_add_child(message, x);
|
||||
xmpp_stanza_release(x);
|
||||
} else {
|
||||
message = xmpp_message_new(ctx, STANZA_TYPE_CHAT, jid, id);
|
||||
xmpp_message_set_body(message, msg);
|
||||
if (current) {
|
||||
win_println(current, THEME_ERROR, "-", "Unable to encrypt message for %s: %s.", jid, err);
|
||||
}
|
||||
log_error("Message not encrypted for %s: %s.", jid, err);
|
||||
account_free(account);
|
||||
free(id);
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
message = xmpp_message_new(ctx, STANZA_TYPE_CHAT, jid, id);
|
||||
xmpp_message_set_body(message, msg);
|
||||
if (current) {
|
||||
win_println(current, THEME_ERROR, "-", "No PGP key configured for this account.");
|
||||
}
|
||||
log_error("No PGP key configured, message not sent.");
|
||||
account_free(account);
|
||||
free(id);
|
||||
return NULL;
|
||||
}
|
||||
account_free(account);
|
||||
#else
|
||||
// ?
|
||||
message = xmpp_message_new(ctx, STANZA_TYPE_CHAT, jid, id);
|
||||
xmpp_message_set_body(message, msg);
|
||||
if (current) {
|
||||
win_println(current, THEME_ERROR, "-", "LibGPGME not available, message not sent.");
|
||||
}
|
||||
log_error("LibGPGME not available, message not sent.");
|
||||
free(id);
|
||||
return NULL;
|
||||
#endif
|
||||
|
||||
if (state) {
|
||||
@@ -546,7 +562,10 @@ message_send_chat_ox(const char* const barejid, const char* const msg, gboolean
|
||||
xmpp_stanza_to_text(signcrypt, &c, &s);
|
||||
char* signcrypt_e = p_ox_gpg_signcrypt(account->jid, barejid, c);
|
||||
if (signcrypt_e == NULL) {
|
||||
cons_show("Unable to send OX message. Check log file and profanity-ox-setup man page for details.");
|
||||
ProfWin* current = wins_get_current();
|
||||
if (current) {
|
||||
win_println(current, THEME_ERROR, "-", "Unable to send OX message. Check log file and profanity-ox-setup man page for details.");
|
||||
}
|
||||
log_error("Message not signcrypted.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user