Commit c3fd345
authored
Fix/memory leaks, add functions to report context usage, add unittest (#20)
* fix(chat): avoid memory leaks and unnecessary re-initialization in llm_chat_check_context
Adds a check to return early if the chat struct is already initialized, preventing memory leaks and unnecessary re-initialization and ensuring correct chat context handling.
* fix(chat): remove the duplicate_content arg in llm_messages_append to avoid a crash on llm_chat_free()
Refactored llm_messages_append to always duplicate message content using sqlite_strdup, removing the duplicate_content parameter. This option was causing a double-free crash in llm_chat_free with non-duplicated content. Also, the ai->chat.response buffer is reset on new prompts in the same chat so we cannot rely on that buffer to store previous response messages.
* test: add C unit tests
Introduces a new C unit test runner at tests/c/unittest.c with basic tests for the SQLite AI extension. The Makefile is updated to build and run these tests, including logic to download a test model if needed and link against the appropriate SQLite libraries.
* ci: add GGUF model caching to workflow
Introduces a new 'download-model' job in the GitHub Actions workflow to cache and restore the GGUF model, reducing redundant downloads. Updates the Makefile to use GGUF model variables, simplifies test model handling, and ensures the test binary is built with the correct SQLite source.
Add SQLite amalgamation for testing.
* feat: add llm_context_used and llm_context_size functions to report context usage
Add two functions: llm_context_size, which returns the total context size, and llm_context_used, which returns the number of tokens used.1 parent 7e3b8c1 commit c3fd345
File tree
7 files changed
+256457
-14
lines changed- .github/workflows
- src
- tests/c
7 files changed
+256457
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
10 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
11 | 50 | | |
| 51 | + | |
12 | 52 | | |
13 | 53 | | |
14 | 54 | | |
| |||
48 | 88 | | |
49 | 89 | | |
50 | 90 | | |
51 | | - | |
| 91 | + | |
52 | 92 | | |
53 | 93 | | |
54 | 94 | | |
55 | 95 | | |
56 | | - | |
| 96 | + | |
57 | 97 | | |
58 | 98 | | |
59 | 99 | | |
| |||
65 | 105 | | |
66 | 106 | | |
67 | 107 | | |
68 | | - | |
| 108 | + | |
69 | 109 | | |
70 | 110 | | |
71 | 111 | | |
72 | | - | |
| 112 | + | |
73 | 113 | | |
74 | 114 | | |
75 | 115 | | |
76 | | - | |
| 116 | + | |
77 | 117 | | |
78 | 118 | | |
79 | 119 | | |
80 | 120 | | |
81 | | - | |
| 121 | + | |
82 | 122 | | |
83 | 123 | | |
84 | 124 | | |
| |||
106 | 146 | | |
107 | 147 | | |
108 | 148 | | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
109 | 158 | | |
110 | 159 | | |
111 | 160 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
245 | 245 | | |
246 | 246 | | |
247 | 247 | | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
248 | 277 | | |
249 | 278 | | |
250 | 279 | | |
| |||
546 | 575 | | |
547 | 576 | | |
548 | 577 | | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
549 | 594 | | |
550 | 595 | | |
551 | 596 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
40 | 49 | | |
41 | 50 | | |
42 | 51 | | |
| |||
55 | 64 | | |
56 | 65 | | |
57 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
58 | 75 | | |
59 | 76 | | |
60 | 77 | | |
| |||
210 | 227 | | |
211 | 228 | | |
212 | 229 | | |
213 | | - | |
214 | | - | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
215 | 251 | | |
216 | 252 | | |
217 | 253 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
784 | 784 | | |
785 | 785 | | |
786 | 786 | | |
787 | | - | |
| 787 | + | |
788 | 788 | | |
789 | 789 | | |
790 | 790 | | |
| |||
796 | 796 | | |
797 | 797 | | |
798 | 798 | | |
799 | | - | |
| 799 | + | |
800 | 800 | | |
801 | 801 | | |
802 | 802 | | |
| |||
1490 | 1490 | | |
1491 | 1491 | | |
1492 | 1492 | | |
| 1493 | + | |
| 1494 | + | |
| 1495 | + | |
1493 | 1496 | | |
1494 | 1497 | | |
1495 | 1498 | | |
| |||
1509 | 1512 | | |
1510 | 1513 | | |
1511 | 1514 | | |
1512 | | - | |
| 1515 | + | |
1513 | 1516 | | |
1514 | 1517 | | |
1515 | 1518 | | |
| |||
1640 | 1643 | | |
1641 | 1644 | | |
1642 | 1645 | | |
1643 | | - | |
| 1646 | + | |
1644 | 1647 | | |
1645 | 1648 | | |
1646 | 1649 | | |
| |||
1976 | 1979 | | |
1977 | 1980 | | |
1978 | 1981 | | |
1979 | | - | |
| 1982 | + | |
1980 | 1983 | | |
1981 | 1984 | | |
1982 | 1985 | | |
| |||
2369 | 2372 | | |
2370 | 2373 | | |
2371 | 2374 | | |
| 2375 | + | |
| 2376 | + | |
| 2377 | + | |
| 2378 | + | |
| 2379 | + | |
| 2380 | + | |
| 2381 | + | |
| 2382 | + | |
| 2383 | + | |
| 2384 | + | |
| 2385 | + | |
| 2386 | + | |
| 2387 | + | |
| 2388 | + | |
| 2389 | + | |
| 2390 | + | |
| 2391 | + | |
| 2392 | + | |
| 2393 | + | |
| 2394 | + | |
| 2395 | + | |
2372 | 2396 | | |
2373 | 2397 | | |
2374 | 2398 | | |
| |||
2707 | 2731 | | |
2708 | 2732 | | |
2709 | 2733 | | |
| 2734 | + | |
| 2735 | + | |
| 2736 | + | |
| 2737 | + | |
| 2738 | + | |
| 2739 | + | |
2710 | 2740 | | |
2711 | 2741 | | |
2712 | 2742 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| |||
0 commit comments