cactus-react-native 0.1.2 → 0.1.3

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 (65) hide show
  1. package/README.md +872 -146
  2. package/android/src/main/CMakeLists.txt +1 -1
  3. package/android/src/main/java/com/cactus/Cactus.java +0 -134
  4. package/android/src/main/java/com/cactus/LlamaContext.java +0 -22
  5. package/android/src/main/jni.cpp +1 -53
  6. package/android/src/main/jniLibs/arm64-v8a/libcactus.so +0 -0
  7. package/android/src/main/jniLibs/arm64-v8a/libcactus_v8.so +0 -0
  8. package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2.so +0 -0
  9. package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2_dotprod.so +0 -0
  10. package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2_dotprod_i8mm.so +0 -0
  11. package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2_i8mm.so +0 -0
  12. package/android/src/main/jniLibs/x86_64/libcactus.so +0 -0
  13. package/android/src/main/jniLibs/x86_64/libcactus_x86_64.so +0 -0
  14. package/android/src/newarch/java/com/cactus/CactusModule.java +0 -20
  15. package/android/src/oldarch/java/com/cactus/CactusModule.java +0 -20
  16. package/ios/CMakeLists.txt +6 -6
  17. package/ios/Cactus.mm +0 -80
  18. package/ios/CactusContext.h +0 -6
  19. package/ios/CactusContext.mm +0 -27
  20. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus.h +0 -6
  21. package/ios/cactus.xcframework/ios-arm64/cactus.framework/cactus +0 -0
  22. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/cactus.h +0 -6
  23. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/cactus +0 -0
  24. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/cactus.h +0 -6
  25. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/cactus +0 -0
  26. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/cactus.h +0 -6
  27. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/cactus +0 -0
  28. package/lib/commonjs/NativeCactus.js.map +1 -1
  29. package/lib/commonjs/index.js.map +1 -1
  30. package/lib/commonjs/lm.js.map +1 -0
  31. package/lib/commonjs/tts.js.map +1 -0
  32. package/lib/commonjs/vlm.js.map +0 -0
  33. package/lib/module/NativeCactus.js.map +1 -1
  34. package/lib/module/index.js.map +1 -1
  35. package/lib/module/lm.js.map +0 -0
  36. package/lib/module/tts.js.map +1 -0
  37. package/lib/module/vlm.js.map +1 -0
  38. package/lib/typescript/NativeCactus.d.ts +0 -10
  39. package/lib/typescript/NativeCactus.d.ts.map +1 -1
  40. package/lib/typescript/index.d.ts +6 -18
  41. package/lib/typescript/index.d.ts.map +1 -1
  42. package/lib/typescript/lm.d.ts +41 -0
  43. package/lib/typescript/lm.d.ts.map +1 -0
  44. package/lib/typescript/tts.d.ts +10 -0
  45. package/lib/typescript/tts.d.ts.map +1 -0
  46. package/lib/typescript/vlm.d.ts +44 -0
  47. package/lib/typescript/vlm.d.ts.map +1 -0
  48. package/package.json +2 -1
  49. package/src/NativeCactus.ts +0 -22
  50. package/src/index.ts +11 -37
  51. package/src/lm.ts +49 -0
  52. package/src/tts.ts +45 -0
  53. package/src/vlm.ts +70 -0
  54. package/lib/commonjs/NativeCactus.js +0 -11
  55. package/lib/commonjs/chat.js +0 -37
  56. package/lib/commonjs/grammar.js +0 -560
  57. package/lib/commonjs/index.js +0 -441
  58. package/lib/commonjs/tools.js +0 -118
  59. package/lib/commonjs/tools.js.map +0 -1
  60. package/lib/module/NativeCactus.js +0 -10
  61. package/lib/module/chat.js +0 -33
  62. package/lib/module/grammar.js +0 -553
  63. package/lib/module/index.js +0 -392
  64. package/lib/module/tools.js +0 -110
  65. package/lib/module/tools.js.map +0 -1
@@ -1,441 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.LlamaContext = void 0;
7
- Object.defineProperty(exports, "SchemaGrammarConverter", {
8
- enumerable: true,
9
- get: function () {
10
- return _grammar.SchemaGrammarConverter;
11
- }
12
- });
13
- Object.defineProperty(exports, "Tools", {
14
- enumerable: true,
15
- get: function () {
16
- return _tools.Tools;
17
- }
18
- });
19
- exports.addNativeLogListener = addNativeLogListener;
20
- Object.defineProperty(exports, "convertJsonSchemaToGrammar", {
21
- enumerable: true,
22
- get: function () {
23
- return _grammar.convertJsonSchemaToGrammar;
24
- }
25
- });
26
- exports.initContext = exports.getTTSType = exports.getFormattedAudioCompletion = exports.getAudioCompletionGuideTokens = exports.decodeAudioTokens = void 0;
27
- exports.initLlama = initLlama;
28
- exports.isVocoderEnabled = exports.isMultimodalSupportVision = exports.isMultimodalSupportAudio = exports.isMultimodalEnabled = exports.initVocoder = exports.initMultimodal = void 0;
29
- exports.loadLlamaModelInfo = loadLlamaModelInfo;
30
- exports.multimodalCompletion = void 0;
31
- exports.releaseAllLlama = releaseAllLlama;
32
- exports.releaseVocoder = exports.releaseMultimodal = void 0;
33
- exports.setContextLimit = setContextLimit;
34
- exports.toggleNativeLog = toggleNativeLog;
35
- exports.tokenize = void 0;
36
- var _reactNative = require("react-native");
37
- var _NativeCactus = _interopRequireDefault(require("./NativeCactus"));
38
- var _grammar = require("./grammar");
39
- var _chat = require("./chat");
40
- var _tools = require("./tools");
41
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
42
- const EVENT_ON_INIT_CONTEXT_PROGRESS = '@Cactus_onInitContextProgress';
43
- const EVENT_ON_TOKEN = '@Cactus_onToken';
44
- const EVENT_ON_NATIVE_LOG = '@Cactus_onNativeLog';
45
- let EventEmitter;
46
- if (_reactNative.Platform.OS === 'ios') {
47
- // @ts-ignore
48
- EventEmitter = new _reactNative.NativeEventEmitter(_NativeCactus.default);
49
- }
50
- if (_reactNative.Platform.OS === 'android') {
51
- EventEmitter = _reactNative.DeviceEventEmitter;
52
- }
53
- const logListeners = [];
54
-
55
- // @ts-ignore
56
- if (EventEmitter) {
57
- EventEmitter.addListener(EVENT_ON_NATIVE_LOG, evt => {
58
- logListeners.forEach(listener => listener(evt.level, evt.text));
59
- });
60
- // Trigger unset to use default log callback
61
- _NativeCactus.default?.toggleNativeLog?.(false)?.catch?.(() => {});
62
- }
63
- const getJsonSchema = responseFormat => {
64
- if (responseFormat?.type === 'json_schema') {
65
- return responseFormat.json_schema?.schema;
66
- }
67
- if (responseFormat?.type === 'json_object') {
68
- return responseFormat.schema || {};
69
- }
70
- return null;
71
- };
72
- class LlamaContext {
73
- gpu = false;
74
- reasonNoGPU = '';
75
- constructor({
76
- contextId,
77
- gpu,
78
- reasonNoGPU,
79
- model
80
- }) {
81
- this.id = contextId;
82
- this.gpu = gpu;
83
- this.reasonNoGPU = reasonNoGPU;
84
- this.model = model;
85
- }
86
-
87
- /**
88
- * Load cached prompt & completion state from a file.
89
- */
90
- async loadSession(filepath) {
91
- let path = filepath;
92
- if (path.startsWith('file://')) path = path.slice(7);
93
- return _NativeCactus.default.loadSession(this.id, path);
94
- }
95
-
96
- /**
97
- * Save current cached prompt & completion state to a file.
98
- */
99
- async saveSession(filepath, options) {
100
- return _NativeCactus.default.saveSession(this.id, filepath, options?.tokenSize || -1);
101
- }
102
- isLlamaChatSupported() {
103
- return !!this.model.chatTemplates.llamaChat;
104
- }
105
- isJinjaSupported() {
106
- const {
107
- minja
108
- } = this.model.chatTemplates;
109
- return !!minja?.toolUse || !!minja?.default;
110
- }
111
- async getFormattedChat(messages, template, params) {
112
- const chat = (0, _chat.formatChat)(messages);
113
- const useJinja = this.isJinjaSupported() && params?.jinja;
114
- let tmpl = this.isLlamaChatSupported() || useJinja ? undefined : 'chatml';
115
- if (template) tmpl = template; // Force replace if provided
116
- const jsonSchema = getJsonSchema(params?.response_format);
117
- return _NativeCactus.default.getFormattedChat(this.id, JSON.stringify(chat), tmpl, {
118
- jinja: useJinja,
119
- json_schema: jsonSchema ? JSON.stringify(jsonSchema) : undefined,
120
- tools: params?.tools ? JSON.stringify(params.tools) : undefined,
121
- parallel_tool_calls: params?.parallel_tool_calls ? JSON.stringify(params.parallel_tool_calls) : undefined,
122
- tool_choice: params?.tool_choice
123
- });
124
- }
125
- async completionWithTools(params, callback, recursionCount = 0, recursionLimit = 3) {
126
- if (!params.messages) {
127
- // tool calling only works with messages
128
- return this.completion(params, callback);
129
- }
130
- if (!params.tools) {
131
- // no tools => default completion
132
- return this.completion(params, callback);
133
- }
134
- if (recursionCount >= recursionLimit) {
135
- return this.completion(params, callback);
136
- }
137
- let messages = [...(params?.messages || [])]; // avoid mutating the original
138
-
139
- if (recursionCount === 0) {
140
- messages = (0, _tools.injectToolsIntoMessages)(messages, params.tools);
141
- }
142
- const result = await this.completion({
143
- ...params,
144
- messages
145
- }, callback);
146
- const {
147
- toolCalled,
148
- toolName,
149
- toolInput,
150
- toolOutput
151
- } = await (0, _tools.parseAndExecuteTool)(result, params.tools);
152
- if (toolCalled && toolName && toolInput) {
153
- const newMessages = (0, _tools.updateMessagesWithToolCall)(messages, toolName, toolInput, toolOutput);
154
- return await this.completionWithTools({
155
- ...params,
156
- messages: newMessages
157
- }, callback, recursionCount + 1, recursionLimit);
158
- }
159
- return result;
160
- }
161
- async completion(params, callback) {
162
- const nativeParams = {
163
- ...params,
164
- prompt: params.prompt || '',
165
- emit_partial_completion: !!callback
166
- };
167
- if (params.messages) {
168
- // messages always win
169
- const formattedResult = await this.getFormattedChat(params.messages, params.chat_template || params.chatTemplate, {
170
- jinja: params.jinja,
171
- tools: params.tools,
172
- parallel_tool_calls: params.parallel_tool_calls,
173
- tool_choice: params.tool_choice
174
- });
175
- if (typeof formattedResult === 'string') {
176
- nativeParams.prompt = formattedResult || '';
177
- } else {
178
- nativeParams.prompt = formattedResult.prompt || '';
179
- if (typeof formattedResult.chat_format === 'number') nativeParams.chat_format = formattedResult.chat_format;
180
- if (formattedResult.grammar) nativeParams.grammar = formattedResult.grammar;
181
- if (typeof formattedResult.grammar_lazy === 'boolean') nativeParams.grammar_lazy = formattedResult.grammar_lazy;
182
- if (formattedResult.grammar_triggers) nativeParams.grammar_triggers = formattedResult.grammar_triggers;
183
- if (formattedResult.preserved_tokens) nativeParams.preserved_tokens = formattedResult.preserved_tokens;
184
- if (formattedResult.additional_stops) {
185
- if (!nativeParams.stop) nativeParams.stop = [];
186
- nativeParams.stop.push(...formattedResult.additional_stops);
187
- }
188
- }
189
- } else {
190
- nativeParams.prompt = params.prompt || '';
191
- }
192
- if (nativeParams.response_format && !nativeParams.grammar) {
193
- const jsonSchema = getJsonSchema(params.response_format);
194
- if (jsonSchema) nativeParams.json_schema = JSON.stringify(jsonSchema);
195
- }
196
- let tokenListener = callback && EventEmitter.addListener(EVENT_ON_TOKEN, evt => {
197
- const {
198
- contextId,
199
- tokenResult
200
- } = evt;
201
- if (contextId !== this.id) return;
202
- callback(tokenResult);
203
- });
204
- if (!nativeParams.prompt) throw new Error('Prompt is required');
205
- const promise = _NativeCactus.default.completion(this.id, nativeParams);
206
- return promise.then(completionResult => {
207
- tokenListener?.remove();
208
- tokenListener = null;
209
- return completionResult;
210
- }).catch(err => {
211
- tokenListener?.remove();
212
- tokenListener = null;
213
- throw err;
214
- });
215
- }
216
- stopCompletion() {
217
- return _NativeCactus.default.stopCompletion(this.id);
218
- }
219
-
220
- // New conversation management methods
221
- /**
222
- * Generate a response to a user message (simple text return)
223
- */
224
- async generateResponse(userMessage, maxTokens = 200) {
225
- return _NativeCactus.default.generateResponse(this.id, userMessage, maxTokens);
226
- }
227
-
228
- /**
229
- * Continue conversation with detailed timing information
230
- */
231
- async continueConversation(userMessage, maxTokens = 200) {
232
- return _NativeCactus.default.continueConversation(this.id, userMessage, maxTokens);
233
- }
234
-
235
- /**
236
- * Clear conversation history and reset KV cache
237
- */
238
- async clearConversation() {
239
- return _NativeCactus.default.clearConversation(this.id);
240
- }
241
-
242
- /**
243
- * Check if conversation is currently active
244
- */
245
- async isConversationActive() {
246
- return _NativeCactus.default.isConversationActive(this.id);
247
- }
248
- tokenize(text) {
249
- return _NativeCactus.default.tokenize(this.id, text);
250
- }
251
- detokenize(tokens) {
252
- return _NativeCactus.default.detokenize(this.id, tokens);
253
- }
254
- embedding(text, params) {
255
- return _NativeCactus.default.embedding(this.id, text, params || {});
256
- }
257
- async bench(pp, tg, pl, nr) {
258
- const result = await _NativeCactus.default.bench(this.id, pp, tg, pl, nr);
259
- const [modelDesc, modelSize, modelNParams, ppAvg, ppStd, tgAvg, tgStd] = JSON.parse(result);
260
- return {
261
- modelDesc,
262
- modelSize,
263
- modelNParams,
264
- ppAvg,
265
- ppStd,
266
- tgAvg,
267
- tgStd
268
- };
269
- }
270
- async applyLoraAdapters(loraList) {
271
- let loraAdapters = [];
272
- if (loraList) loraAdapters = loraList.map(l => ({
273
- path: l.path.replace(/file:\/\//, ''),
274
- scaled: l.scaled
275
- }));
276
- return _NativeCactus.default.applyLoraAdapters(this.id, loraAdapters);
277
- }
278
- async removeLoraAdapters() {
279
- return _NativeCactus.default.removeLoraAdapters(this.id);
280
- }
281
- async getLoadedLoraAdapters() {
282
- return _NativeCactus.default.getLoadedLoraAdapters(this.id);
283
- }
284
- async release() {
285
- return _NativeCactus.default.releaseContext(this.id);
286
- }
287
- }
288
- exports.LlamaContext = LlamaContext;
289
- async function toggleNativeLog(enabled) {
290
- return _NativeCactus.default.toggleNativeLog(enabled);
291
- }
292
- function addNativeLogListener(listener) {
293
- logListeners.push(listener);
294
- return {
295
- remove: () => {
296
- logListeners.splice(logListeners.indexOf(listener), 1);
297
- }
298
- };
299
- }
300
- async function setContextLimit(limit) {
301
- return _NativeCactus.default.setContextLimit(limit);
302
- }
303
- let contextIdCounter = 0;
304
- const contextIdRandom = () => process.env.NODE_ENV === 'test' ? 0 : Math.floor(Math.random() * 100000);
305
- const modelInfoSkip = [
306
- // Large fields
307
- 'tokenizer.ggml.tokens', 'tokenizer.ggml.token_type', 'tokenizer.ggml.merges', 'tokenizer.ggml.scores'];
308
- async function loadLlamaModelInfo(model) {
309
- let path = model;
310
- if (path.startsWith('file://')) path = path.slice(7);
311
- return _NativeCactus.default.modelInfo(path, modelInfoSkip);
312
- }
313
- const poolTypeMap = {
314
- // -1 is unspecified as undefined
315
- none: 0,
316
- mean: 1,
317
- cls: 2,
318
- last: 3,
319
- rank: 4
320
- };
321
- async function initLlama({
322
- model,
323
- is_model_asset: isModelAsset,
324
- pooling_type: poolingType,
325
- lora,
326
- lora_list: loraList,
327
- ...rest
328
- }, onProgress) {
329
- let path = model;
330
- if (path.startsWith('file://')) path = path.slice(7);
331
- let loraPath = lora;
332
- if (loraPath?.startsWith('file://')) loraPath = loraPath.slice(7);
333
- let loraAdapters = [];
334
- if (loraList) loraAdapters = loraList.map(l => ({
335
- path: l.path.replace(/file:\/\//, ''),
336
- scaled: l.scaled
337
- }));
338
- const contextId = contextIdCounter + contextIdRandom();
339
- contextIdCounter += 1;
340
- let removeProgressListener = null;
341
- if (onProgress) {
342
- removeProgressListener = EventEmitter.addListener(EVENT_ON_INIT_CONTEXT_PROGRESS, evt => {
343
- if (evt.contextId !== contextId) return;
344
- onProgress(evt.progress);
345
- });
346
- }
347
- const poolType = poolTypeMap[poolingType];
348
- const {
349
- gpu,
350
- reasonNoGPU,
351
- model: modelDetails,
352
- androidLib
353
- } = await _NativeCactus.default.initContext(contextId, {
354
- model: path,
355
- is_model_asset: !!isModelAsset,
356
- use_progress_callback: !!onProgress,
357
- pooling_type: poolType,
358
- lora: loraPath,
359
- lora_list: loraAdapters,
360
- ...rest
361
- }).catch(err => {
362
- removeProgressListener?.remove();
363
- throw err;
364
- });
365
- removeProgressListener?.remove();
366
- return new LlamaContext({
367
- contextId,
368
- gpu,
369
- reasonNoGPU,
370
- model: modelDetails,
371
- androidLib
372
- });
373
- }
374
- async function releaseAllLlama() {
375
- return _NativeCactus.default.releaseAllContexts();
376
- }
377
- const initContext = async params => {
378
- return await _NativeCactus.default.initContext(contextIdCounter++, params);
379
- };
380
- exports.initContext = initContext;
381
- const initMultimodal = async (contextId, mmprojPath, useGpu = false) => {
382
- return await _NativeCactus.default.initMultimodal(contextId, mmprojPath, useGpu);
383
- };
384
- exports.initMultimodal = initMultimodal;
385
- const isMultimodalEnabled = async contextId => {
386
- return await _NativeCactus.default.isMultimodalEnabled(contextId);
387
- };
388
- exports.isMultimodalEnabled = isMultimodalEnabled;
389
- const isMultimodalSupportVision = async contextId => {
390
- return await _NativeCactus.default.isMultimodalSupportVision(contextId);
391
- };
392
- exports.isMultimodalSupportVision = isMultimodalSupportVision;
393
- const isMultimodalSupportAudio = async contextId => {
394
- return await _NativeCactus.default.isMultimodalSupportAudio(contextId);
395
- };
396
- exports.isMultimodalSupportAudio = isMultimodalSupportAudio;
397
- const releaseMultimodal = async contextId => {
398
- return await _NativeCactus.default.releaseMultimodal(contextId);
399
- };
400
- exports.releaseMultimodal = releaseMultimodal;
401
- const multimodalCompletion = async (contextId, prompt, mediaPaths, params) => {
402
- return await _NativeCactus.default.multimodalCompletion(contextId, prompt, mediaPaths, params);
403
- };
404
- exports.multimodalCompletion = multimodalCompletion;
405
- const initVocoder = async (contextId, vocoderModelPath) => {
406
- return await _NativeCactus.default.initVocoder(contextId, vocoderModelPath);
407
- };
408
- exports.initVocoder = initVocoder;
409
- const isVocoderEnabled = async contextId => {
410
- return await _NativeCactus.default.isVocoderEnabled(contextId);
411
- };
412
- exports.isVocoderEnabled = isVocoderEnabled;
413
- const getTTSType = async contextId => {
414
- return await _NativeCactus.default.getTTSType(contextId);
415
- };
416
- exports.getTTSType = getTTSType;
417
- const getFormattedAudioCompletion = async (contextId, speakerJsonStr, textToSpeak) => {
418
- return await _NativeCactus.default.getFormattedAudioCompletion(contextId, speakerJsonStr, textToSpeak);
419
- };
420
- exports.getFormattedAudioCompletion = getFormattedAudioCompletion;
421
- const getAudioCompletionGuideTokens = async (contextId, textToSpeak) => {
422
- return await _NativeCactus.default.getAudioCompletionGuideTokens(contextId, textToSpeak);
423
- };
424
- exports.getAudioCompletionGuideTokens = getAudioCompletionGuideTokens;
425
- const decodeAudioTokens = async (contextId, tokens) => {
426
- return await _NativeCactus.default.decodeAudioTokens(contextId, tokens);
427
- };
428
- exports.decodeAudioTokens = decodeAudioTokens;
429
- const releaseVocoder = async contextId => {
430
- return await _NativeCactus.default.releaseVocoder(contextId);
431
- };
432
- exports.releaseVocoder = releaseVocoder;
433
- const tokenize = async (contextId, text, mediaPaths) => {
434
- if (mediaPaths && mediaPaths.length > 0) {
435
- return await _NativeCactus.default.tokenize(contextId, text, mediaPaths);
436
- } else {
437
- return await _NativeCactus.default.tokenize(contextId, text);
438
- }
439
- };
440
- exports.tokenize = tokenize;
441
- //# sourceMappingURL=index.js.map
@@ -1,118 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.Tools = void 0;
7
- exports.injectToolsIntoMessages = injectToolsIntoMessages;
8
- exports.parseAndExecuteTool = parseAndExecuteTool;
9
- exports.updateMessagesWithToolCall = updateMessagesWithToolCall;
10
- class Tools {
11
- tools = new Map();
12
- add(func, description, parameters) {
13
- this.tools.set(func.name, {
14
- func,
15
- description,
16
- parameters,
17
- required: Object.entries(parameters).filter(([_, param]) => param.required).map(([key, _]) => key)
18
- });
19
- return func;
20
- }
21
- getSchemas() {
22
- return Array.from(this.tools.entries()).map(([name, {
23
- description,
24
- parameters,
25
- required
26
- }]) => ({
27
- type: "function",
28
- function: {
29
- name,
30
- description,
31
- parameters: {
32
- type: "object",
33
- properties: parameters,
34
- required
35
- }
36
- }
37
- }));
38
- }
39
- async execute(name, args) {
40
- const tool = this.tools.get(name);
41
- if (!tool) throw new Error(`Tool ${name} not found`);
42
- return await tool.func(...Object.values(args));
43
- }
44
- }
45
- exports.Tools = Tools;
46
- function injectToolsIntoMessages(messages, tools) {
47
- const newMessages = [...messages];
48
- const toolsSchemas = tools.getSchemas();
49
- const promptToolInjection = `You have access to the following functions. Use them if required -
50
- ${JSON.stringify(toolsSchemas, null, 2)}
51
- Only use an available tool if needed. If a tool is chosen, respond ONLY with a JSON object matching the following schema:
52
- \`\`\`json
53
- {
54
- "tool_name": "<name of the tool>",
55
- "tool_input": {
56
- "<parameter_name>": "<parameter_value>",
57
- ...
58
- }
59
- }
60
- \`\`\`
61
- Remember, if you are calling a tool, you must respond with the JSON object and the JSON object ONLY!
62
- If no tool is needed, respond normally.
63
- `;
64
- const systemMessage = newMessages.find(m => m.role === 'system');
65
- if (!systemMessage) {
66
- newMessages.unshift({
67
- role: 'system',
68
- content: promptToolInjection
69
- });
70
- } else {
71
- systemMessage.content = `${systemMessage.content}\n\n${promptToolInjection}`;
72
- }
73
- return newMessages;
74
- }
75
- async function parseAndExecuteTool(result, tools) {
76
- const match = result.content.match(/```json\s*([\s\S]*?)\s*```/);
77
- if (!match || !match[1]) return {
78
- toolCalled: false
79
- };
80
- try {
81
- const jsonContent = JSON.parse(match[1]);
82
- const {
83
- tool_name,
84
- tool_input
85
- } = jsonContent;
86
- // console.log('Calling tool:', tool_name, tool_input);
87
- const toolOutput = (await tools.execute(tool_name, tool_input)) || true;
88
- // console.log('Tool called result:', toolOutput);
89
-
90
- return {
91
- toolCalled: true,
92
- toolName: tool_name,
93
- toolInput: tool_input,
94
- toolOutput
95
- };
96
- } catch (error) {
97
- // console.error('Error parsing JSON:', match, error);
98
- return {
99
- toolCalled: false
100
- };
101
- }
102
- }
103
- function updateMessagesWithToolCall(messages, toolName, toolInput, toolOutput) {
104
- const newMessages = [...messages];
105
- newMessages.push({
106
- role: 'function-call',
107
- content: JSON.stringify({
108
- name: toolName,
109
- arguments: toolInput
110
- }, null, 2)
111
- });
112
- newMessages.push({
113
- role: 'function-response',
114
- content: JSON.stringify(toolOutput, null, 2)
115
- });
116
- return newMessages;
117
- }
118
- //# sourceMappingURL=tools.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["Tools","tools","Map","add","func","description","parameters","set","name","required","Object","entries","filter","_","param","map","key","getSchemas","Array","from","type","function","properties","execute","args","tool","get","Error","values","exports","injectToolsIntoMessages","messages","newMessages","toolsSchemas","promptToolInjection","JSON","stringify","systemMessage","find","m","role","unshift","content","parseAndExecuteTool","result","match","toolCalled","jsonContent","parse","tool_name","tool_input","toolOutput","toolName","toolInput","error","updateMessagesWithToolCall","push","arguments"],"sourceRoot":"../../src","sources":["tools.ts"],"mappings":"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","ignoreList":[]}
@@ -1,10 +0,0 @@
1
- "use strict";
2
-
3
- import { TurboModuleRegistry } from 'react-native';
4
-
5
- // New TTS/Audio types
6
-
7
- // New conversation management types
8
-
9
- export default TurboModuleRegistry.get('Cactus');
10
- //# sourceMappingURL=NativeCactus.js.map
@@ -1,33 +0,0 @@
1
- "use strict";
2
-
3
- export function formatChat(messages) {
4
- const chat = [];
5
- messages.forEach(currMsg => {
6
- const role = currMsg.role || '';
7
- let content = '';
8
- if ('content' in currMsg) {
9
- if (typeof currMsg.content === 'string') {
10
- ;
11
- ({
12
- content
13
- } = currMsg);
14
- } else if (Array.isArray(currMsg.content)) {
15
- currMsg.content.forEach(part => {
16
- if ('text' in part) {
17
- content += `${content ? '\n' : ''}${part.text}`;
18
- }
19
- });
20
- } else {
21
- throw new TypeError("Invalid 'content' type (ref: https://github.com/ggerganov/llama.cpp/issues/8367)");
22
- }
23
- } else {
24
- throw new Error("Missing 'content' (ref: https://github.com/ggerganov/llama.cpp/issues/8367)");
25
- }
26
- chat.push({
27
- role,
28
- content
29
- });
30
- });
31
- return chat;
32
- }
33
- //# sourceMappingURL=chat.js.map