Skip to content

Commit 7e3b8c1

Browse files
authored
Fix/issue15 chat crash (#15) (#19)
* fix: add context validation to llm_chat_create function Added checks to ensure a valid context exists before proceeding in llm_chat_check_context and llm_chat_create. This prevents misuse by requiring llm_context_create to be called prior to llm_chat_create, improving error handling and robustness.
1 parent 2e0e251 commit 7e3b8c1

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,4 @@ help:
348348
@echo " xcframework - Build the Apple XCFramework"
349349
@echo " aar - Build the Android AAR package"
350350

351-
.PHONY: all clean test extension help version xcframework aar
351+
.PHONY: all clean test extension help version xcframework aar

src/sqlite-ai.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,11 @@ static void llm_text_generate (sqlite3_context *context, int argc, sqlite3_value
14761476
// MARK: - Chat -
14771477

14781478
static bool llm_chat_check_context (ai_context *ai) {
1479+
if (!ai || !ai->ctx) {
1480+
sqlite_common_set_error(ai ? ai->context : NULL, ai ? ai->vtab : NULL, SQLITE_MISUSE, "No context found. Please call llm_context_create() before llm_chat_create().");
1481+
return false;
1482+
}
1483+
14791484
// check sampler
14801485
if (!ai->sampler) {
14811486
llm_sampler_check(ai);
@@ -1862,6 +1867,8 @@ static void llm_chat_free (sqlite3_context *context, int argc, sqlite3_value **a
18621867
}
18631868

18641869
static void llm_chat_create (sqlite3_context *context, int argc, sqlite3_value **argv) {
1870+
if (llm_check_context(context) == false) return;
1871+
18651872
ai_context *ai = (ai_context *)sqlite3_user_data(context);
18661873

18671874
// clean-up old chat (if any)

0 commit comments

Comments
 (0)