@novastera-oss/llamarn 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (266) hide show
  1. package/README.md +80 -14
  2. package/RNLlamaCpp.podspec +10 -3
  3. package/android/CMakeLists.txt +8 -0
  4. package/android/src/main/cpp/include/llama.h +62 -125
  5. package/android/src/main/jniLibs/arm64-v8a/libggml-base.so +0 -0
  6. package/android/src/main/jniLibs/arm64-v8a/libggml-cpu.so +0 -0
  7. package/android/src/main/jniLibs/arm64-v8a/libggml.so +0 -0
  8. package/android/src/main/jniLibs/arm64-v8a/libllama.so +0 -0
  9. package/android/src/main/jniLibs/x86_64/libggml-base.so +0 -0
  10. package/android/src/main/jniLibs/x86_64/libggml-cpu.so +0 -0
  11. package/android/src/main/jniLibs/x86_64/libggml.so +0 -0
  12. package/android/src/main/jniLibs/x86_64/libllama.so +0 -0
  13. package/cpp/build-info.cpp +2 -2
  14. package/cpp/llama.cpp/README.md +11 -3
  15. package/cpp/llama.cpp/build-xcframework.sh +1 -0
  16. package/cpp/llama.cpp/common/CMakeLists.txt +8 -2
  17. package/cpp/llama.cpp/common/arg.cpp +153 -113
  18. package/cpp/llama.cpp/common/chat-parser.cpp +379 -0
  19. package/cpp/llama.cpp/common/chat-parser.h +117 -0
  20. package/cpp/llama.cpp/common/chat.cpp +847 -699
  21. package/cpp/llama.cpp/common/chat.h +73 -6
  22. package/cpp/llama.cpp/common/common.cpp +50 -82
  23. package/cpp/llama.cpp/common/common.h +21 -17
  24. package/cpp/llama.cpp/common/json-partial.cpp +255 -0
  25. package/cpp/llama.cpp/common/json-partial.h +37 -0
  26. package/cpp/llama.cpp/common/minja/chat-template.hpp +9 -5
  27. package/cpp/llama.cpp/common/minja/minja.hpp +69 -36
  28. package/cpp/llama.cpp/common/regex-partial.cpp +204 -0
  29. package/cpp/llama.cpp/common/regex-partial.h +56 -0
  30. package/cpp/llama.cpp/common/sampling.cpp +7 -8
  31. package/cpp/llama.cpp/convert_hf_to_gguf.py +453 -118
  32. package/cpp/llama.cpp/convert_hf_to_gguf_update.py +120 -68
  33. package/cpp/llama.cpp/ggml/CMakeLists.txt +2 -1
  34. package/cpp/llama.cpp/ggml/cmake/common.cmake +25 -0
  35. package/cpp/llama.cpp/ggml/include/ggml-opt.h +49 -28
  36. package/cpp/llama.cpp/ggml/include/ggml.h +26 -7
  37. package/cpp/llama.cpp/ggml/src/CMakeLists.txt +16 -10
  38. package/cpp/llama.cpp/ggml/src/ggml-backend.cpp +4 -1
  39. package/cpp/llama.cpp/ggml/src/ggml-cann/CMakeLists.txt +1 -0
  40. package/cpp/llama.cpp/ggml/src/ggml-cann/acl_tensor.cpp +2 -0
  41. package/cpp/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +604 -0
  42. package/cpp/llama.cpp/ggml/src/ggml-cann/aclnn_ops.h +42 -0
  43. package/cpp/llama.cpp/ggml/src/ggml-cann/ggml-cann.cpp +54 -2
  44. package/cpp/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +50 -51
  45. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.cpp +2 -2
  46. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +5 -9
  47. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +779 -19
  48. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +22 -0
  49. package/cpp/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.cpp +88 -5
  50. package/cpp/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.h +47 -12
  51. package/cpp/llama.cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +264 -69
  52. package/cpp/llama.cpp/ggml/src/ggml-cpu/ops.cpp +322 -100
  53. package/cpp/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +117 -1
  54. package/cpp/llama.cpp/ggml/src/ggml-cpu/vec.cpp +85 -16
  55. package/cpp/llama.cpp/ggml/src/ggml-cpu/vec.h +220 -49
  56. package/cpp/llama.cpp/ggml/src/ggml-cuda/acc.cu +40 -26
  57. package/cpp/llama.cpp/ggml/src/ggml-cuda/common.cuh +1 -1
  58. package/cpp/llama.cpp/ggml/src/ggml-cuda/cpy.cu +11 -1
  59. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-common.cuh +15 -7
  60. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-mma-f16.cuh +266 -64
  61. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-vec-f16.cuh +49 -4
  62. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-vec-f32.cuh +48 -4
  63. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn.cu +2 -1
  64. package/cpp/llama.cpp/ggml/src/ggml-cuda/ggml-cuda.cu +5 -1
  65. package/cpp/llama.cpp/ggml/src/ggml-cuda/mmq.cu +2 -0
  66. package/cpp/llama.cpp/ggml/src/ggml-cuda/quantize.cu +7 -6
  67. package/cpp/llama.cpp/ggml/src/ggml-cuda/sum.cu +1 -1
  68. package/cpp/llama.cpp/ggml/src/ggml-cuda/unary.cu +10 -0
  69. package/cpp/llama.cpp/ggml/src/ggml-cuda/unary.cuh +2 -0
  70. package/cpp/llama.cpp/ggml/src/ggml-impl.h +1 -1
  71. package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +4 -0
  72. package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal.m +99 -17
  73. package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal.metal +200 -2
  74. package/cpp/llama.cpp/ggml/src/ggml-musa/CMakeLists.txt +8 -2
  75. package/cpp/llama.cpp/ggml/src/ggml-musa/mudnn.cu +112 -0
  76. package/cpp/llama.cpp/ggml/src/ggml-musa/mudnn.cuh +12 -0
  77. package/cpp/llama.cpp/ggml/src/ggml-opencl/CMakeLists.txt +6 -0
  78. package/cpp/llama.cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +972 -178
  79. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/argsort.cl +86 -0
  80. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/div.cl +72 -0
  81. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/group_norm.cl +72 -0
  82. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/sigmoid.cl +29 -0
  83. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/sub.cl +72 -0
  84. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/sum_rows.cl +39 -0
  85. package/cpp/llama.cpp/ggml/src/ggml-opt.cpp +373 -190
  86. package/cpp/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +29 -23
  87. package/cpp/llama.cpp/ggml/src/ggml-sycl/binbcast.cpp +5 -10
  88. package/cpp/llama.cpp/ggml/src/ggml-sycl/common.hpp +101 -5
  89. package/cpp/llama.cpp/ggml/src/ggml-sycl/concat.cpp +31 -33
  90. package/cpp/llama.cpp/ggml/src/ggml-sycl/conv.cpp +1 -0
  91. package/cpp/llama.cpp/ggml/src/ggml-sycl/convert.cpp +29 -2
  92. package/cpp/llama.cpp/ggml/src/ggml-sycl/cpy.cpp +4 -5
  93. package/cpp/llama.cpp/ggml/src/ggml-sycl/dequantize.hpp +59 -21
  94. package/cpp/llama.cpp/ggml/src/ggml-sycl/dmmv.cpp +9 -1
  95. package/cpp/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +84 -72
  96. package/cpp/llama.cpp/ggml/src/ggml-sycl/element_wise.hpp +2 -0
  97. package/cpp/llama.cpp/ggml/src/ggml-sycl/gemm.hpp +37 -8
  98. package/cpp/llama.cpp/ggml/src/ggml-sycl/getrows.cpp +1 -3
  99. package/cpp/llama.cpp/ggml/src/ggml-sycl/ggml-sycl.cpp +324 -129
  100. package/cpp/llama.cpp/ggml/src/ggml-sycl/gla.cpp +1 -0
  101. package/cpp/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +31 -2
  102. package/cpp/llama.cpp/ggml/src/ggml-sycl/norm.cpp +95 -68
  103. package/cpp/llama.cpp/ggml/src/ggml-sycl/outprod.cpp +1 -0
  104. package/cpp/llama.cpp/ggml/src/ggml-sycl/quants.hpp +22 -0
  105. package/cpp/llama.cpp/ggml/src/ggml-sycl/rope.cpp +1 -2
  106. package/cpp/llama.cpp/ggml/src/ggml-sycl/softmax.cpp +1 -4
  107. package/cpp/llama.cpp/ggml/src/ggml-sycl/tsembd.cpp +2 -3
  108. package/cpp/llama.cpp/ggml/src/ggml-sycl/vecdotq.hpp +69 -43
  109. package/cpp/llama.cpp/ggml/src/ggml-sycl/wkv.cpp +2 -14
  110. package/cpp/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +81 -91
  111. package/cpp/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp +432 -181
  112. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +17 -0
  113. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq1_m.comp +1 -1
  114. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp +6 -152
  115. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_base.comp +162 -0
  116. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1.comp +360 -0
  117. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm2.comp +2 -118
  118. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm.comp +1 -1
  119. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +12 -1
  120. package/cpp/llama.cpp/ggml/src/ggml.c +107 -36
  121. package/cpp/llama.cpp/ggml/src/gguf.cpp +33 -33
  122. package/cpp/llama.cpp/gguf-py/gguf/constants.py +100 -15
  123. package/cpp/llama.cpp/gguf-py/gguf/gguf_reader.py +1 -1
  124. package/cpp/llama.cpp/gguf-py/gguf/gguf_writer.py +44 -12
  125. package/cpp/llama.cpp/gguf-py/gguf/scripts/gguf_editor_gui.py +21 -10
  126. package/cpp/llama.cpp/gguf-py/gguf/scripts/gguf_new_metadata.py +5 -2
  127. package/cpp/llama.cpp/gguf-py/gguf/tensor_mapping.py +128 -31
  128. package/cpp/llama.cpp/gguf-py/gguf/utility.py +1 -1
  129. package/cpp/llama.cpp/gguf-py/pyproject.toml +1 -1
  130. package/cpp/llama.cpp/include/llama.h +62 -125
  131. package/cpp/llama.cpp/models/ggml-vocab-bert-bge.gguf.inp +1 -1
  132. package/cpp/llama.cpp/models/ggml-vocab-bert-bge.gguf.out +1 -1
  133. package/cpp/llama.cpp/models/ggml-vocab-command-r.gguf.inp +1 -1
  134. package/cpp/llama.cpp/models/ggml-vocab-command-r.gguf.out +1 -1
  135. package/cpp/llama.cpp/models/ggml-vocab-deepseek-coder.gguf.inp +1 -1
  136. package/cpp/llama.cpp/models/ggml-vocab-deepseek-coder.gguf.out +1 -1
  137. package/cpp/llama.cpp/models/ggml-vocab-deepseek-llm.gguf.inp +1 -1
  138. package/cpp/llama.cpp/models/ggml-vocab-deepseek-llm.gguf.out +1 -1
  139. package/cpp/llama.cpp/models/ggml-vocab-falcon.gguf.inp +1 -1
  140. package/cpp/llama.cpp/models/ggml-vocab-falcon.gguf.out +1 -1
  141. package/cpp/llama.cpp/models/ggml-vocab-gpt-2.gguf.inp +1 -1
  142. package/cpp/llama.cpp/models/ggml-vocab-gpt-2.gguf.out +1 -1
  143. package/cpp/llama.cpp/models/ggml-vocab-llama-bpe.gguf.inp +1 -1
  144. package/cpp/llama.cpp/models/ggml-vocab-llama-bpe.gguf.out +1 -1
  145. package/cpp/llama.cpp/models/ggml-vocab-llama-spm.gguf.inp +1 -1
  146. package/cpp/llama.cpp/models/ggml-vocab-llama-spm.gguf.out +1 -1
  147. package/cpp/llama.cpp/models/ggml-vocab-mpt.gguf.inp +1 -1
  148. package/cpp/llama.cpp/models/ggml-vocab-mpt.gguf.out +1 -1
  149. package/cpp/llama.cpp/models/ggml-vocab-nomic-bert-moe.gguf +0 -0
  150. package/cpp/llama.cpp/models/ggml-vocab-phi-3.gguf.inp +1 -1
  151. package/cpp/llama.cpp/models/ggml-vocab-phi-3.gguf.out +1 -1
  152. package/cpp/llama.cpp/models/ggml-vocab-qwen2.gguf.inp +1 -1
  153. package/cpp/llama.cpp/models/ggml-vocab-qwen2.gguf.out +1 -1
  154. package/cpp/llama.cpp/models/ggml-vocab-refact.gguf.inp +1 -1
  155. package/cpp/llama.cpp/models/ggml-vocab-refact.gguf.out +1 -1
  156. package/cpp/llama.cpp/models/ggml-vocab-starcoder.gguf.inp +1 -1
  157. package/cpp/llama.cpp/models/ggml-vocab-starcoder.gguf.out +1 -1
  158. package/cpp/llama.cpp/models/templates/Qwen-QwQ-32B.jinja +62 -0
  159. package/cpp/llama.cpp/models/templates/Qwen-Qwen3-0.6B.jinja +85 -0
  160. package/cpp/llama.cpp/models/templates/README.md +2 -0
  161. package/cpp/llama.cpp/requirements/requirements-convert_hf_to_gguf.txt +5 -1
  162. package/cpp/llama.cpp/requirements/requirements-convert_hf_to_gguf_update.txt +5 -1
  163. package/cpp/llama.cpp/requirements/requirements-convert_lora_to_gguf.txt +2 -0
  164. package/cpp/llama.cpp/requirements/requirements-gguf_editor_gui.txt +1 -1
  165. package/cpp/llama.cpp/src/CMakeLists.txt +2 -0
  166. package/cpp/llama.cpp/src/llama-arch.cpp +6 -0
  167. package/cpp/llama.cpp/src/llama-arch.h +2 -0
  168. package/cpp/llama.cpp/src/llama-batch.cpp +3 -1
  169. package/cpp/llama.cpp/src/llama-context.cpp +340 -123
  170. package/cpp/llama.cpp/src/llama-context.h +30 -0
  171. package/cpp/llama.cpp/src/llama-cparams.cpp +4 -0
  172. package/cpp/llama.cpp/src/llama-cparams.h +2 -0
  173. package/cpp/llama.cpp/src/llama-grammar.cpp +12 -2
  174. package/cpp/llama.cpp/src/llama-graph.cpp +157 -247
  175. package/cpp/llama.cpp/src/llama-graph.h +52 -7
  176. package/cpp/llama.cpp/src/llama-hparams.cpp +17 -1
  177. package/cpp/llama.cpp/src/llama-hparams.h +37 -5
  178. package/cpp/llama.cpp/src/llama-kv-cache.cpp +742 -481
  179. package/cpp/llama.cpp/src/llama-kv-cache.h +196 -99
  180. package/cpp/llama.cpp/src/llama-kv-cells.h +379 -0
  181. package/cpp/llama.cpp/src/llama-memory.h +4 -3
  182. package/cpp/llama.cpp/src/llama-model-loader.cpp +22 -17
  183. package/cpp/llama.cpp/src/llama-model-saver.cpp +281 -0
  184. package/cpp/llama.cpp/src/llama-model-saver.h +37 -0
  185. package/cpp/llama.cpp/src/llama-model.cpp +529 -172
  186. package/cpp/llama.cpp/src/llama-model.h +6 -1
  187. package/cpp/llama.cpp/src/llama-quant.cpp +15 -13
  188. package/cpp/llama.cpp/src/llama-sampling.cpp +2 -2
  189. package/cpp/llama.cpp/src/llama-vocab.cpp +35 -8
  190. package/cpp/llama.cpp/src/llama-vocab.h +6 -0
  191. package/cpp/llama.cpp/src/llama.cpp +14 -0
  192. package/cpp/rn-completion.cpp +4 -2
  193. package/ios/include/chat.h +73 -6
  194. package/ios/include/common/minja/chat-template.hpp +9 -5
  195. package/ios/include/common/minja/minja.hpp +69 -36
  196. package/ios/include/common.h +21 -17
  197. package/ios/include/llama.h +62 -125
  198. package/ios/libs/llama.xcframework/Info.plist +19 -19
  199. package/ios/libs/llama.xcframework/ios-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  200. package/ios/libs/llama.xcframework/ios-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4617 -4487
  201. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/ggml-opt.h +237 -0
  202. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/ggml.h +26 -7
  203. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/llama.h +62 -125
  204. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/llama +0 -0
  205. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  206. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4638 -4508
  207. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3557 -3435
  208. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/ggml-opt.h +237 -0
  209. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +26 -7
  210. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/llama.h +62 -125
  211. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/llama +0 -0
  212. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  213. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4638 -4508
  214. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3559 -3437
  215. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/ggml-opt.h +237 -0
  216. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/ggml.h +26 -7
  217. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/llama.h +62 -125
  218. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/ggml-opt.h +237 -0
  219. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/ggml.h +26 -7
  220. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/llama.h +62 -125
  221. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/llama +0 -0
  222. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/ggml-opt.h +237 -0
  223. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/ggml.h +26 -7
  224. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/llama.h +62 -125
  225. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/llama +0 -0
  226. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/llama +0 -0
  227. package/ios/libs/llama.xcframework/tvos-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  228. package/ios/libs/llama.xcframework/tvos-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4616 -4487
  229. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/ggml-opt.h +237 -0
  230. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/ggml.h +26 -7
  231. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/llama.h +62 -125
  232. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/llama +0 -0
  233. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  234. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4637 -4508
  235. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3556 -3435
  236. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/ggml-opt.h +237 -0
  237. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +26 -7
  238. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/llama.h +62 -125
  239. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/llama +0 -0
  240. package/ios/libs/llama.xcframework/xros-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  241. package/ios/libs/llama.xcframework/xros-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4653 -4523
  242. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/ggml-opt.h +237 -0
  243. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/ggml.h +26 -7
  244. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/llama.h +62 -125
  245. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/llama +0 -0
  246. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  247. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4674 -4544
  248. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3587 -3465
  249. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/ggml-opt.h +237 -0
  250. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +26 -7
  251. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/llama.h +62 -125
  252. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/llama +0 -0
  253. package/package.json +1 -1
  254. package/cpp/llama.cpp/common/stb_image.h +0 -7988
  255. package/cpp/llama.cpp/models/ggml-vocab-chameleon.gguf.inp +0 -112
  256. package/cpp/llama.cpp/models/ggml-vocab-chameleon.gguf.out +0 -46
  257. package/cpp/llama.cpp/models/ggml-vocab-deepseek-r1-qwen.gguf.inp +0 -112
  258. package/cpp/llama.cpp/models/ggml-vocab-deepseek-r1-qwen.gguf.out +0 -46
  259. package/cpp/llama.cpp/models/ggml-vocab-gpt-4o.gguf.inp +0 -112
  260. package/cpp/llama.cpp/models/ggml-vocab-gpt-4o.gguf.out +0 -46
  261. package/cpp/llama.cpp/models/ggml-vocab-llama4.gguf.inp +0 -112
  262. package/cpp/llama.cpp/models/ggml-vocab-llama4.gguf.out +0 -46
  263. package/cpp/llama.cpp/models/ggml-vocab-pixtral.gguf.inp +0 -112
  264. package/cpp/llama.cpp/models/ggml-vocab-pixtral.gguf.out +0 -46
  265. package/cpp/llama.cpp/models/ggml-vocab-roberta-bpe.gguf.inp +0 -112
  266. package/cpp/llama.cpp/models/ggml-vocab-roberta-bpe.gguf.out +0 -46
@@ -0,0 +1,255 @@
1
+ #include <json-partial.h>
2
+ #include "ggml.h"
3
+ #include "log.h"
4
+ #include <string>
5
+
6
+ #include <json.hpp>
7
+
8
+ using json = nlohmann::ordered_json;
9
+
10
+ enum common_json_stack_element_type {
11
+ COMMON_JSON_STACK_ELEMENT_OBJECT,
12
+ COMMON_JSON_STACK_ELEMENT_KEY,
13
+ COMMON_JSON_STACK_ELEMENT_ARRAY,
14
+ };
15
+
16
+ struct common_json_stack_element {
17
+ common_json_stack_element_type type;
18
+ std::string key;
19
+ };
20
+
21
+ bool common_json_parse(
22
+ const std::string & input,
23
+ const std::string & healing_marker,
24
+ common_json & out)
25
+ {
26
+ std::string::const_iterator it = input.begin();
27
+ const auto end = input.end();
28
+ return common_json_parse(it, end, healing_marker, out);
29
+ }
30
+
31
+ bool common_json_parse(
32
+ std::string::const_iterator & it,
33
+ const std::string::const_iterator & end,
34
+ const std::string & healing_marker,
35
+ common_json & out)
36
+ {
37
+ // // https://json.nlohmann.me/features/parsing/sax_interface/
38
+ struct json_error_locator : public nlohmann::json_sax<json> {
39
+ std::size_t position;
40
+ bool found_error;
41
+ std::string last_token;
42
+ std::string exception_message;
43
+ std::vector<common_json_stack_element> stack;
44
+
45
+ json_error_locator() : position(0), found_error(false) {}
46
+
47
+ bool parse_error(std::size_t position, const std::string & last_token, const json::exception & ex) override { // NOLINT
48
+ this->position = position - 1;
49
+ this->found_error = true;
50
+ this->last_token = last_token;
51
+ this->exception_message = ex.what();
52
+ return false;
53
+ }
54
+ void close_value() {
55
+ if (!stack.empty() && (stack.back().type == COMMON_JSON_STACK_ELEMENT_KEY)) {
56
+ stack.pop_back();
57
+ }
58
+ }
59
+ bool null() override { // NOLINT
60
+ close_value();
61
+ return true;
62
+ }
63
+ bool boolean(bool) override { // NOLINT
64
+ close_value();
65
+ return true;
66
+ }
67
+ bool number_integer(number_integer_t) override { // NOLINT
68
+ close_value();
69
+ return true;
70
+ }
71
+ bool number_unsigned(number_unsigned_t) override { // NOLINT
72
+ close_value();
73
+ return true;
74
+ }
75
+ bool number_float(number_float_t, const string_t &) override { // NOLINT
76
+ close_value();
77
+ return true;
78
+ }
79
+ bool string(string_t &) override { // NOLINT
80
+ close_value();
81
+ return true;
82
+ }
83
+ bool binary(binary_t &) override { // NOLINT
84
+ close_value();
85
+ return true;
86
+ }
87
+ bool start_object(std::size_t) override { // NOLINT
88
+ stack.push_back({COMMON_JSON_STACK_ELEMENT_OBJECT, ""});
89
+ return true;
90
+ }
91
+ bool end_object() override {
92
+ GGML_ASSERT(!stack.empty() && stack.back().type == COMMON_JSON_STACK_ELEMENT_OBJECT);
93
+ stack.pop_back();
94
+ close_value();
95
+ return true;
96
+ }
97
+ bool key(string_t & key) override { // NOLINT
98
+ stack.push_back({COMMON_JSON_STACK_ELEMENT_KEY, key});
99
+ return true;
100
+ }
101
+ bool start_array(std::size_t) override { // NOLINT
102
+ stack.push_back({COMMON_JSON_STACK_ELEMENT_ARRAY, ""});
103
+ return true;
104
+ }
105
+ bool end_array() override {
106
+ GGML_ASSERT(!stack.empty() && stack.back().type == COMMON_JSON_STACK_ELEMENT_ARRAY);
107
+ stack.pop_back();
108
+ close_value();
109
+ return true;
110
+ }
111
+ };
112
+ json_error_locator err_loc;
113
+ auto start = it;
114
+ json::sax_parse(it, end, &err_loc);
115
+
116
+ if (err_loc.found_error) {
117
+ it = start;
118
+ auto temptative_end = it + err_loc.position;
119
+ // LOG_DBG("Error at position %zu (is_end = %s): %s\n", err_loc.position, temptative_end == end ? "true" : "false", err_loc.exception_message.c_str());
120
+
121
+ auto input = std::string(it, temptative_end);
122
+ try {
123
+ out.json = json::parse(input);
124
+ // out.json = json::parse(it, temptative_end);
125
+ it = temptative_end;
126
+ return true;
127
+ } catch (const std::exception & ex) {
128
+ // No, needs healing.
129
+ LOG_DBG("Failed to parse up to error: %s: <<<%s>>>\n", ex.what(), std::string(it, temptative_end).c_str());
130
+ }
131
+ auto can_parse = [](const std::string & str) {
132
+ try {
133
+ auto _ = json::parse(str); // NOLINT
134
+ return true;
135
+ } catch (const std::exception &) {
136
+ return false;
137
+ }
138
+ };
139
+ if (!healing_marker.empty() && !err_loc.stack.empty()) {
140
+ std::string str(it, temptative_end);
141
+ auto last_non_sp_pos = str.find_last_not_of(" \n\r\t");
142
+ if (last_non_sp_pos == std::string::npos) {
143
+ throw std::runtime_error("Cannot heal a truncated JSON that stopped in an unknown location");
144
+ }
145
+ auto last_non_sp_char = str[last_non_sp_pos];
146
+ // Used to detect stops on a number, which may not be complete.
147
+ auto was_maybe_number = [&]() {
148
+ if (!str.empty() && std::isspace(str.back())) {
149
+ return false;
150
+ }
151
+ return std::isdigit(last_non_sp_char) ||
152
+ last_non_sp_char == '.' ||
153
+ last_non_sp_char == 'e' ||
154
+ last_non_sp_char == 'E' ||
155
+ last_non_sp_char == '-';
156
+ };
157
+
158
+ std::string closing;
159
+ for (size_t i = err_loc.stack.size(); i > 0; i--) {
160
+ auto & el = err_loc.stack[i - 1];
161
+ if (el.type == COMMON_JSON_STACK_ELEMENT_OBJECT) {
162
+ closing += "}";
163
+ } else if (el.type == COMMON_JSON_STACK_ELEMENT_ARRAY) {
164
+ closing += "]";
165
+ } else if (el.type != COMMON_JSON_STACK_ELEMENT_KEY) {
166
+ throw std::runtime_error("Unexpected stack element type");
167
+ }
168
+ }
169
+
170
+ const auto & magic_seed = out.healing_marker.marker = healing_marker;//"$llama.cpp.json$";
171
+
172
+ if (err_loc.stack.back().type == COMMON_JSON_STACK_ELEMENT_KEY) {
173
+ // We're inside an object value
174
+ if (last_non_sp_char == ':' && can_parse(str + "1" + closing)) {
175
+ // Was about to create an object value
176
+ str += (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\"" + closing;
177
+ } else if (can_parse(str + ": 1" + closing)) {
178
+ str += (out.healing_marker.json_dump_marker = ":\"" + magic_seed) + "\"" + closing;
179
+ } else if (last_non_sp_char == '{' && can_parse(str + closing)) {
180
+ // Was about to create an object
181
+ str += (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\": 1" + closing;
182
+ } else if (can_parse(str + "\"" + closing)) {
183
+ // Was inside an object value string
184
+ str += (out.healing_marker.json_dump_marker = magic_seed) + "\"" + closing;
185
+ } else if (str[str.length() - 1] == '\\' && can_parse(str + "\\\"" + closing)) {
186
+ // Was inside an object value string after an escape
187
+ str += (out.healing_marker.json_dump_marker = "\\" + magic_seed) + "\"" + closing;
188
+ } else {
189
+ // find last :
190
+ auto last_pos = str.find_last_of(':');
191
+ if (last_pos == std::string::npos) {
192
+ throw std::runtime_error("Cannot heal a truncated JSON that stopped in an unknown location");
193
+ }
194
+ // Cutting back to opening : for object value
195
+ str = str.substr(0, last_pos + 1) + (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\"" + closing;
196
+ }
197
+ } else if (err_loc.stack.back().type == COMMON_JSON_STACK_ELEMENT_ARRAY) {
198
+ if ((last_non_sp_char == ',' || last_non_sp_char == '[') && can_parse(str + "1" + closing)) {
199
+ // Was about to create an array value
200
+ str += (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\"" + closing;
201
+ } else if (can_parse(str + "\"" + closing)) {
202
+ // Was inside an array value string
203
+ str += (out.healing_marker.json_dump_marker = magic_seed) + "\"" + closing;
204
+ } else if (str[str.length() - 1] == '\\' && can_parse(str + "\\\"" + closing)) {
205
+ // Was inside an array value string after an escape
206
+ str += (out.healing_marker.json_dump_marker = "\\" + magic_seed) + "\"" + closing;
207
+ } else if (!was_maybe_number() && can_parse(str + ", 1" + closing)) {
208
+ // Had just finished a value
209
+ str += (out.healing_marker.json_dump_marker = ",\"" + magic_seed) + "\"" + closing;
210
+ } else {
211
+ auto last_pos = str.find_last_of("[,");
212
+ if (last_pos == std::string::npos) {
213
+ throw std::runtime_error("Cannot heal a truncated JSON array stopped in an unknown location");
214
+ }
215
+ // Cutting back to last [ or , for array value
216
+ str = str.substr(0, last_pos + 1) + (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\"" + closing;
217
+ }
218
+ } else if (err_loc.stack.back().type == COMMON_JSON_STACK_ELEMENT_OBJECT) {
219
+ if ((last_non_sp_char == '{' && can_parse(str + closing)) ||
220
+ (last_non_sp_char == ',' && can_parse(str + "\"\": 1" + closing))) {
221
+ // Was about to create an object key+value
222
+ str += (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\": 1" + closing;
223
+ } else if (!was_maybe_number() && can_parse(str + ",\"\": 1" + closing)) {
224
+ // Was about to create an object key+value
225
+ str += (out.healing_marker.json_dump_marker = ",\"" + magic_seed) + "\": 1" + closing;
226
+ } else if (can_parse(str + "\": 1" + closing)) {
227
+ // Was inside an object key string
228
+ str += (out.healing_marker.json_dump_marker = magic_seed) + "\": 1" + closing;
229
+ } else if (str[str.length() - 1] == '\\' && can_parse(str + "\\\": 1" + closing)) {
230
+ // Was inside an object key string after an escape
231
+ str += (out.healing_marker.json_dump_marker = "\\" + magic_seed) + "\": 1" + closing;
232
+ } else {
233
+ auto last_pos = str.find_last_of(':');
234
+ if (last_pos == std::string::npos) {
235
+ throw std::runtime_error("Cannot heal a truncated JSON object stopped in an unknown location");
236
+ }
237
+ // fprintf(stderr, "Cutting back to last : for object key+value\n");
238
+ str = str.substr(0, last_pos + 1) + (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\"" + closing;
239
+ }
240
+ } else {
241
+ throw std::runtime_error("Cannot heal a truncated JSON object stopped in an unknown location");
242
+ }
243
+ // fprintf(stderr, "HEALED:\nSTRING <<<\n%s\n>>>\n\nmagic_cut: <<<\n%s\n>>>\n\n", str.c_str(), out.healing_marker.json_dump_marker.c_str());
244
+ out.json = json::parse(str);
245
+ it = temptative_end;
246
+ return true;
247
+ }
248
+ // TODO: handle unclosed top-level primitive if the stack was empty but we got an error (e.g. "tru", "\"", etc...)
249
+ // fprintf(stderr, "Closing: TODO\n");
250
+ return false;
251
+ }
252
+ out.json = json::parse(it, end);
253
+ it = end;
254
+ return true;
255
+ }
@@ -0,0 +1,37 @@
1
+ #pragma once
2
+ #include <json.hpp>
3
+
4
+ // Healing marker (empty if the JSON was fully parsed / wasn't healed).
5
+ struct common_healing_marker {
6
+ // Raw marker.
7
+ std::string marker;
8
+
9
+ // Cutting the `common_json.json.dump()` string at the (only) occurrence of this marker should yield the original partial JSON string (modulo spaces / if it had the same dump format).
10
+ std::string json_dump_marker;
11
+ };
12
+
13
+ // Represents a parsed JSON object, with its optional healing marker (a JSON dump fragment that can be used to find the position of healing in the JSON dump string)
14
+ struct common_json {
15
+ nlohmann::ordered_json json;
16
+
17
+ common_healing_marker healing_marker;
18
+ };
19
+
20
+ // Parse the JSON string, healing (closing) any partial JSON if `healing_marker` is not empty.
21
+ //
22
+ // Healing completes partial JSON strings by adding a (possibly modified) healing marker, then whatever is needed to close the JSON.
23
+ // This allows to parse the resulting healed JSON string, yet be able to cut it again if needed at the healing marker.
24
+ // (this is used when parsing JSON outputs from the models, then crafting partial JSONs for the partial tool calls in OAI format).
25
+ //
26
+ // For instance, parsing `{` with a healing marker `foo` will produce a healed JSON `{"foo":1}`, w/ json_dump_marker = `"foo"` (which can be used to break the JSON again).
27
+ bool common_json_parse(
28
+ const std::string & input,
29
+ const std::string & healing_marker,
30
+ common_json & out);
31
+
32
+ // Parse the JSON string (see overload above), but advancing an iterator to the end of the input when the (potentially partial) parsing succeeds.
33
+ bool common_json_parse(
34
+ std::string::const_iterator & it,
35
+ const std::string::const_iterator & end,
36
+ const std::string & healing_marker,
37
+ common_json & out);
@@ -13,10 +13,12 @@
13
13
  #include <chrono>
14
14
  #include <cstddef>
15
15
  #include <cstdio>
16
+ #include <ctime>
16
17
  #include <exception>
17
18
  #include <iomanip>
18
19
  #include <memory>
19
20
  #include <sstream>
21
+ #include <stdexcept>
20
22
  #include <string>
21
23
  #include <vector>
22
24
 
@@ -393,8 +395,8 @@ class chat_template {
393
395
 
394
396
  for (const auto & message_ : adjusted_messages) {
395
397
  auto message = message_;
396
- if (!message.contains("role") || !message.contains("content")) {
397
- throw std::runtime_error("message must have 'role' and 'content' fields: " + message.dump());
398
+ if (!message.contains("role") || (!message.contains("content") && !message.contains("tool_calls"))) {
399
+ throw std::runtime_error("message must have 'role' and one of 'content' or 'tool_calls' fields: " + message.dump());
398
400
  }
399
401
  std::string role = message.at("role");
400
402
 
@@ -415,7 +417,6 @@ class chat_template {
415
417
  }
416
418
  }
417
419
  if (polyfill_tool_calls) {
418
- auto content = message.at("content");
419
420
  auto tool_calls = json::array();
420
421
  for (const auto & tool_call : message.at("tool_calls")) {
421
422
  if (tool_call.at("type") != "function") {
@@ -434,8 +435,11 @@ class chat_template {
434
435
  auto obj = json {
435
436
  {"tool_calls", tool_calls},
436
437
  };
437
- if (!content.is_null() && !content.empty()) {
438
- obj["content"] = content;
438
+ if (message.contains("content")) {
439
+ auto content = message.at("content");
440
+ if (!content.is_null() && !content.empty()) {
441
+ obj["content"] = content;
442
+ }
439
443
  }
440
444
  message["content"] = obj.dump(2);
441
445
  message.erase("tool_calls");
@@ -11,6 +11,7 @@
11
11
  #include <algorithm>
12
12
  #include <cctype>
13
13
  #include <cstddef>
14
+ #include <cstdint>
14
15
  #include <cmath>
15
16
  #include <exception>
16
17
  #include <functional>
@@ -233,7 +234,7 @@ public:
233
234
  }
234
235
  } else if (is_object()) {
235
236
  if (!index.is_hashable())
236
- throw std::runtime_error("Unashable type: " + index.dump());
237
+ throw std::runtime_error("Unhashable type: " + index.dump());
237
238
  auto it = object_->find(index.primitive_);
238
239
  if (it == object_->end())
239
240
  throw std::runtime_error("Key not found: " + index.dump());
@@ -252,7 +253,7 @@ public:
252
253
  auto index = key.get<int>();
253
254
  return array_->at(index < 0 ? array_->size() + index : index);
254
255
  } else if (object_) {
255
- if (!key.is_hashable()) throw std::runtime_error("Unashable type: " + dump());
256
+ if (!key.is_hashable()) throw std::runtime_error("Unhashable type: " + dump());
256
257
  auto it = object_->find(key.primitive_);
257
258
  if (it == object_->end()) return Value();
258
259
  return it->second;
@@ -261,7 +262,7 @@ public:
261
262
  }
262
263
  void set(const Value& key, const Value& value) {
263
264
  if (!object_) throw std::runtime_error("Value is not an object: " + dump());
264
- if (!key.is_hashable()) throw std::runtime_error("Unashable type: " + dump());
265
+ if (!key.is_hashable()) throw std::runtime_error("Unhashable type: " + dump());
265
266
  (*object_)[key.primitive_] = value;
266
267
  }
267
268
  Value call(const std::shared_ptr<Context> & context, ArgumentsValue & args) const {
@@ -398,7 +399,7 @@ public:
398
399
  }
399
400
  return false;
400
401
  } else if (object_) {
401
- if (!value.is_hashable()) throw std::runtime_error("Unashable type: " + value.dump());
402
+ if (!value.is_hashable()) throw std::runtime_error("Unhashable type: " + value.dump());
402
403
  return object_->find(value.primitive_) != object_->end();
403
404
  } else {
404
405
  throw std::runtime_error("contains can only be called on arrays and objects: " + dump());
@@ -416,7 +417,7 @@ public:
416
417
  return const_cast<Value*>(this)->at(index);
417
418
  }
418
419
  Value& at(const Value & index) {
419
- if (!index.is_hashable()) throw std::runtime_error("Unashable type: " + dump());
420
+ if (!index.is_hashable()) throw std::runtime_error("Unhashable type: " + dump());
420
421
  if (is_array()) return array_->at(index.get<int>());
421
422
  if (is_object()) return object_->at(index.primitive_);
422
423
  throw std::runtime_error("Value is not an array or object: " + dump());
@@ -676,8 +677,8 @@ public:
676
677
  class VariableExpr : public Expression {
677
678
  std::string name;
678
679
  public:
679
- VariableExpr(const Location & location, const std::string& n)
680
- : Expression(location), name(n) {}
680
+ VariableExpr(const Location & loc, const std::string& n)
681
+ : Expression(loc), name(n) {}
681
682
  std::string get_name() const { return name; }
682
683
  Value do_evaluate(const std::shared_ptr<Context> & context) const override {
683
684
  if (!context->contains(name)) {
@@ -1200,9 +1201,9 @@ public:
1200
1201
 
1201
1202
  class SliceExpr : public Expression {
1202
1203
  public:
1203
- std::shared_ptr<Expression> start, end;
1204
- SliceExpr(const Location & loc, std::shared_ptr<Expression> && s, std::shared_ptr<Expression> && e)
1205
- : Expression(loc), start(std::move(s)), end(std::move(e)) {}
1204
+ std::shared_ptr<Expression> start, end, step;
1205
+ SliceExpr(const Location & loc, std::shared_ptr<Expression> && s, std::shared_ptr<Expression> && e, std::shared_ptr<Expression> && st = nullptr)
1206
+ : Expression(loc), start(std::move(s)), end(std::move(e)), step(std::move(st)) {}
1206
1207
  Value do_evaluate(const std::shared_ptr<Context> &) const override {
1207
1208
  throw std::runtime_error("SliceExpr not implemented");
1208
1209
  }
@@ -1219,18 +1220,35 @@ public:
1219
1220
  if (!index) throw std::runtime_error("SubscriptExpr.index is null");
1220
1221
  auto target_value = base->evaluate(context);
1221
1222
  if (auto slice = dynamic_cast<SliceExpr*>(index.get())) {
1222
- auto start = slice->start ? slice->start->evaluate(context).get<int64_t>() : 0;
1223
- auto end = slice->end ? slice->end->evaluate(context).get<int64_t>() : (int64_t) target_value.size();
1223
+ auto len = target_value.size();
1224
+ auto wrap = [len](int64_t i) -> int64_t {
1225
+ if (i < 0) {
1226
+ return i + len;
1227
+ }
1228
+ return i;
1229
+ };
1230
+ int64_t step = slice->step ? slice->step->evaluate(context).get<int64_t>() : 1;
1231
+ if (!step) {
1232
+ throw std::runtime_error("slice step cannot be zero");
1233
+ }
1234
+ int64_t start = slice->start ? wrap(slice->start->evaluate(context).get<int64_t>()) : (step < 0 ? len - 1 : 0);
1235
+ int64_t end = slice->end ? wrap(slice->end->evaluate(context).get<int64_t>()) : (step < 0 ? -1 : len);
1224
1236
  if (target_value.is_string()) {
1225
1237
  std::string s = target_value.get<std::string>();
1226
- if (start < 0) start = s.size() + start;
1227
- if (end < 0) end = s.size() + end;
1228
- return s.substr(start, end - start);
1238
+
1239
+ std::string result;
1240
+ if (start < end && step == 1) {
1241
+ result = s.substr(start, end - start);
1242
+ } else {
1243
+ for (int64_t i = start; step > 0 ? i < end : i > end; i += step) {
1244
+ result += s[i];
1245
+ }
1246
+ }
1247
+ return result;
1248
+
1229
1249
  } else if (target_value.is_array()) {
1230
- if (start < 0) start = target_value.size() + start;
1231
- if (end < 0) end = target_value.size() + end;
1232
1250
  auto result = Value::array();
1233
- for (auto i = start; i < end; ++i) {
1251
+ for (int64_t i = start; step > 0 ? i < end : i > end; i += step) {
1234
1252
  result.push_back(target_value.at(i));
1235
1253
  }
1236
1254
  return result;
@@ -1305,6 +1323,8 @@ public:
1305
1323
  if (name == "iterable") return l.is_iterable();
1306
1324
  if (name == "sequence") return l.is_array();
1307
1325
  if (name == "defined") return !l.is_null();
1326
+ if (name == "true") return l.to_bool();
1327
+ if (name == "false") return !l.to_bool();
1308
1328
  throw std::runtime_error("Unknown type for 'is' operator: " + name);
1309
1329
  };
1310
1330
  auto value = eval();
@@ -1520,6 +1540,10 @@ public:
1520
1540
  vargs.expectArgs("endswith method", {1, 1}, {0, 0});
1521
1541
  auto suffix = vargs.args[0].get<std::string>();
1522
1542
  return suffix.length() <= str.length() && std::equal(suffix.rbegin(), suffix.rend(), str.rbegin());
1543
+ } else if (method->get_name() == "startswith") {
1544
+ vargs.expectArgs("startswith method", {1, 1}, {0, 0});
1545
+ auto prefix = vargs.args[0].get<std::string>();
1546
+ return prefix.length() <= str.length() && std::equal(prefix.begin(), prefix.end(), str.begin());
1523
1547
  } else if (method->get_name() == "title") {
1524
1548
  vargs.expectArgs("title method", {0, 0}, {0, 0});
1525
1549
  auto res = str;
@@ -2082,28 +2106,37 @@ private:
2082
2106
 
2083
2107
  while (it != end && consumeSpaces() && peekSymbols({ "[", "." })) {
2084
2108
  if (!consumeToken("[").empty()) {
2085
- std::shared_ptr<Expression> index;
2109
+ std::shared_ptr<Expression> index;
2110
+ auto slice_loc = get_location();
2111
+ std::shared_ptr<Expression> start, end, step;
2112
+ bool has_first_colon = false, has_second_colon = false;
2113
+
2114
+ if (!peekSymbols({ ":" })) {
2115
+ start = parseExpression();
2116
+ }
2117
+
2118
+ if (!consumeToken(":").empty()) {
2119
+ has_first_colon = true;
2120
+ if (!peekSymbols({ ":", "]" })) {
2121
+ end = parseExpression();
2122
+ }
2086
2123
  if (!consumeToken(":").empty()) {
2087
- auto slice_end = parseExpression();
2088
- index = std::make_shared<SliceExpr>(slice_end->location, nullptr, std::move(slice_end));
2089
- } else {
2090
- auto slice_start = parseExpression();
2091
- if (!consumeToken(":").empty()) {
2092
- consumeSpaces();
2093
- if (peekSymbols({ "]" })) {
2094
- index = std::make_shared<SliceExpr>(slice_start->location, std::move(slice_start), nullptr);
2095
- } else {
2096
- auto slice_end = parseExpression();
2097
- index = std::make_shared<SliceExpr>(slice_start->location, std::move(slice_start), std::move(slice_end));
2098
- }
2099
- } else {
2100
- index = std::move(slice_start);
2124
+ has_second_colon = true;
2125
+ if (!peekSymbols({ "]" })) {
2126
+ step = parseExpression();
2101
2127
  }
2102
2128
  }
2103
- if (!index) throw std::runtime_error("Empty index in subscript");
2104
- if (consumeToken("]").empty()) throw std::runtime_error("Expected closing bracket in subscript");
2129
+ }
2130
+
2131
+ if ((has_first_colon || has_second_colon) && (start || end || step)) {
2132
+ index = std::make_shared<SliceExpr>(slice_loc, std::move(start), std::move(end), std::move(step));
2133
+ } else {
2134
+ index = std::move(start);
2135
+ }
2136
+ if (!index) throw std::runtime_error("Empty index in subscript");
2137
+ if (consumeToken("]").empty()) throw std::runtime_error("Expected closing bracket in subscript");
2105
2138
 
2106
- value = std::make_shared<SubscriptExpr>(value->location, std::move(value), std::move(index));
2139
+ value = std::make_shared<SubscriptExpr>(value->location, std::move(value), std::move(index));
2107
2140
  } else if (!consumeToken(".").empty()) {
2108
2141
  auto identifier = parseIdentifier();
2109
2142
  if (!identifier) throw std::runtime_error("Expected identifier in subscript");