diff --git a/Makefile.am b/Makefile.am index e54b0f04..644a3320 100644 --- a/Makefile.am +++ b/Makefile.am @@ -33,6 +33,7 @@ core_sources = \ src/ui/window_list.c src/ui/window_list.h \ src/ui/rosterwin.c src/ui/occupantswin.c \ src/ui/buffer.c src/ui/buffer.h \ + src/ui/aiwin.c \ src/ui/chatwin.c \ src/ui/mucwin.c \ src/ui/privwin.c \ diff --git a/src/ai/ai_client.c b/src/ai/ai_client.c index 13d009e4..36fcfc33 100644 --- a/src/ai/ai_client.c +++ b/src/ai/ai_client.c @@ -14,8 +14,8 @@ #include "common.h" #include "log.h" #include "config/preferences.h" -#include "ui/window.h" #include "profanity.h" +#include "ui/ui.h" #include #include diff --git a/src/ui/aiwin.c b/src/ui/aiwin.c new file mode 100644 index 00000000..40e23667 --- /dev/null +++ b/src/ui/aiwin.c @@ -0,0 +1,65 @@ +/* + * aiwin.c - AI Chat Window Management + * + * Provides functions for managing the AI chat window (ProfAiWin) in the + * profanity client. This includes retrieving window strings for identification, + * displaying AI/LLM responses in the chat view, and handling error messages + * from AI interactions. + * + * Key functions: + * aiwin_get_string() - Returns a descriptive string for the window + * aiwin_display_response() - Displays an AI/LLM response and appends it + * to the conversation history + * aiwin_display_error() - Displays an error message from AI operations + * + * vim: expandtab:ts=4:sts=4:sw=4 + */ + +#include "config.h" + +#include + +#include "ai/ai_client.h" +#include "log.h" +#include "ui/ui.h" +#include "ui/win_types.h" + +char* +aiwin_get_string(ProfAiWin* win) +{ + assert(win->memcheck == PROFAIWIN_MEMCHECK); + return g_strdup_printf("AI Chat (%s: %s)", win->session->provider_name, win->session->model); +} + +void +aiwin_display_response(ProfAiWin* win, const char* response) +{ + log_debug("[AI-WIN] aiwin_display_response ENTER: win=%p, response='%s'", (void*)win, response); + assert(win->memcheck == PROFAIWIN_MEMCHECK); + + if (!response) { + log_error("[AI-WIN] FAIL: null response"); + return; + } + + // Display AI response + win_println(&win->window, THEME_DEFAULT, "<< LLM:", "%s", response); + log_debug("[AI-WIN] Displayed AI response"); + + // Add to conversation history + ai_session_add_message(win->session, "assistant", response); + log_debug("[AI-WIN] Added assistant message to session history"); +} + +void +aiwin_display_error(ProfAiWin* win, const char* error_msg) +{ + log_debug("[AI-WIN] aiwin_display_error ENTER: win=%p, error='%s'", (void*)win, error_msg); + assert(win->memcheck == PROFAIWIN_MEMCHECK); + + const char* msg = error_msg ? error_msg : "Unknown error"; + win_println(&win->window, THEME_ERROR, "[ERROR]", "%s", msg); + + // Also show error in console so user sees it regardless of active window + cons_show_error("AI Error: %s", msg); +} diff --git a/src/ui/ui.h b/src/ui/ui.h index 35a0c5b1..2420fadf 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -447,4 +447,9 @@ void notify_invite(const char* const from, const char* const room, const char* c void notify(const char* const message, int timeout, const char* const category); void notify_subscription(const char* const from); +/* AI window */ +char* aiwin_get_string(ProfAiWin* win); +void aiwin_display_response(ProfAiWin* win, const char* response); +void aiwin_display_error(ProfAiWin* win, const char* error_msg); + #endif diff --git a/src/ui/window.c b/src/ui/window.c index 8d604f28..a864e970 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -347,16 +347,7 @@ win_create_ai(AISession* session) return &new_win->window; } -void -win_ai_free(ProfAiWin* win) -{ - assert(win->memcheck == PROFAIWIN_MEMCHECK); - buffer_free(win->message_buffer); - g_string_free(win->conversation_history, TRUE); - if (win->session) { - ai_session_unref(win->session); - } -} + gchar* win_get_title(ProfWin* window) @@ -2424,45 +2415,3 @@ get_enc_char(prof_enc_t enc_mode, const char* alt) else return get_show_char(enc_mode); } - -// AI Window Functions - -char* -aiwin_get_string(ProfAiWin* win) -{ - assert(win->memcheck == PROFAIWIN_MEMCHECK); - return g_strdup_printf("AI Chat (%s: %s)", win->session->provider_name, win->session->model); -} - -void -aiwin_display_response(ProfAiWin* win, const char* response) -{ - log_debug("[AI-WIN] aiwin_display_response ENTER: win=%p, response='%s'", (void*)win, response); - assert(win->memcheck == PROFAIWIN_MEMCHECK); - - if (!response) { - log_error("[AI-WIN] FAIL: null response"); - return; - } - - // Display AI response - win_println(&win->window, THEME_DEFAULT, "<< LLM:", "%s", response); - log_debug("[AI-WIN] Displayed AI response"); - - // Add to conversation history - ai_session_add_message(win->session, "assistant", response); - log_debug("[AI-WIN] Added assistant message to session history"); -} - -void -aiwin_display_error(ProfAiWin* win, const char* error_msg) -{ - log_debug("[AI-WIN] aiwin_display_error ENTER: win=%p, error='%s'", (void*)win, error_msg); - assert(win->memcheck == PROFAIWIN_MEMCHECK); - - const char* msg = error_msg ? error_msg : "Unknown error"; - win_println(&win->window, THEME_ERROR, "[ERROR]", "%s", msg); - - // Also show error in console so user sees it regardless of active window - cons_show_error("AI Error: %s", msg); -} diff --git a/src/ui/window.h b/src/ui/window.h index 6659c621..a8f2c0c6 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -101,10 +101,5 @@ char* win_quote_autocomplete(ProfWin* window, const char* const input, gboolean char* get_show_char(prof_enc_t encryption_mode); char* get_enc_char(prof_enc_t enc_mode, const char* alt); -// AI window functions -void win_ai_free(ProfAiWin* win); -char* aiwin_get_string(ProfAiWin* win); -void aiwin_display_response(ProfAiWin* win, const char* response); -void aiwin_display_error(ProfAiWin* win, const char* error_msg); #endif