cactus-react-native 0.2.3 → 0.2.4

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 (80) hide show
  1. package/README.md +1 -1
  2. package/android/src/main/java/com/cactus/Cactus.java +35 -0
  3. package/android/src/main/java/com/cactus/LlamaContext.java +5 -0
  4. package/android/src/main/jni.cpp +45 -14
  5. package/android/src/main/jniLibs/arm64-v8a/libcactus.so +0 -0
  6. package/android/src/main/jniLibs/arm64-v8a/libcactus_v8.so +0 -0
  7. package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2.so +0 -0
  8. package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2_dotprod.so +0 -0
  9. package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2_dotprod_i8mm.so +0 -0
  10. package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2_i8mm.so +0 -0
  11. package/android/src/newarch/java/com/cactus/CactusModule.java +5 -0
  12. package/android/src/oldarch/java/com/cactus/CactusModule.java +5 -0
  13. package/ios/Cactus.mm +21 -0
  14. package/ios/CactusContext.h +1 -0
  15. package/ios/CactusContext.mm +4 -0
  16. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus_ffi.h +0 -12
  17. package/ios/cactus.xcframework/ios-arm64/cactus.framework/cactus +0 -0
  18. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/cactus_ffi.h +0 -12
  19. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/cactus +0 -0
  20. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/cactus_ffi.h +0 -12
  21. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/cactus +0 -0
  22. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/cactus_ffi.h +0 -12
  23. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/cactus +0 -0
  24. package/lib/commonjs/NativeCactus.js +0 -1
  25. package/lib/commonjs/NativeCactus.js.map +1 -1
  26. package/lib/commonjs/chat.js +33 -0
  27. package/lib/commonjs/chat.js.map +1 -1
  28. package/lib/commonjs/index.js +0 -23
  29. package/lib/commonjs/index.js.map +1 -1
  30. package/lib/commonjs/lm.js +26 -7
  31. package/lib/commonjs/lm.js.map +1 -1
  32. package/lib/commonjs/tools.js +0 -7
  33. package/lib/commonjs/tools.js.map +1 -1
  34. package/lib/commonjs/tts.js +1 -4
  35. package/lib/commonjs/tts.js.map +1 -1
  36. package/lib/commonjs/vlm.js +25 -7
  37. package/lib/commonjs/vlm.js.map +1 -1
  38. package/lib/module/NativeCactus.js +0 -3
  39. package/lib/module/NativeCactus.js.map +1 -1
  40. package/lib/module/chat.js +31 -0
  41. package/lib/module/chat.js.map +1 -1
  42. package/lib/module/index.js +1 -10
  43. package/lib/module/index.js.map +1 -1
  44. package/lib/module/lm.js +26 -7
  45. package/lib/module/lm.js.map +1 -1
  46. package/lib/module/tools.js +0 -7
  47. package/lib/module/tools.js.map +1 -1
  48. package/lib/module/tts.js +1 -4
  49. package/lib/module/tts.js.map +1 -1
  50. package/lib/module/vlm.js +25 -7
  51. package/lib/module/vlm.js.map +1 -1
  52. package/lib/typescript/NativeCactus.d.ts +1 -142
  53. package/lib/typescript/NativeCactus.d.ts.map +1 -1
  54. package/lib/typescript/chat.d.ts +10 -0
  55. package/lib/typescript/chat.d.ts.map +1 -1
  56. package/lib/typescript/index.d.ts +2 -4
  57. package/lib/typescript/index.d.ts.map +1 -1
  58. package/lib/typescript/lm.d.ts +9 -6
  59. package/lib/typescript/lm.d.ts.map +1 -1
  60. package/lib/typescript/tools.d.ts.map +1 -1
  61. package/lib/typescript/tts.d.ts.map +1 -1
  62. package/lib/typescript/vlm.d.ts +3 -1
  63. package/lib/typescript/vlm.d.ts.map +1 -1
  64. package/package.json +1 -1
  65. package/src/NativeCactus.ts +6 -175
  66. package/src/chat.ts +42 -1
  67. package/src/index.ts +6 -17
  68. package/src/lm.ts +33 -9
  69. package/src/tools.ts +0 -5
  70. package/src/tts.ts +1 -4
  71. package/src/vlm.ts +35 -13
  72. package/android/src/main/jniLibs/x86_64/libcactus.so +0 -0
  73. package/android/src/main/jniLibs/x86_64/libcactus_x86_64.so +0 -0
  74. package/lib/commonjs/grammar.js +0 -560
  75. package/lib/commonjs/grammar.js.map +0 -1
  76. package/lib/module/grammar.js +0 -553
  77. package/lib/module/grammar.js.map +0 -1
  78. package/lib/typescript/grammar.d.ts +0 -37
  79. package/lib/typescript/grammar.d.ts.map +0 -1
  80. package/src/grammar.ts +0 -854
package/src/tools.ts CHANGED
@@ -56,22 +56,18 @@ export class Tools {
56
56
 
57
57
  export async function parseAndExecuteTool(result: NativeCompletionResult, tools: Tools): Promise<{toolCalled: boolean, toolName?: string, toolInput?: any, toolOutput?: any}> {
58
58
  if (!result.tool_calls || result.tool_calls.length === 0) {
59
- // console.log('No tool calls found');
60
59
  return {toolCalled: false};
61
60
  }
62
61
 
63
62
  try {
64
63
  const toolCall = result.tool_calls[0];
65
64
  if (!toolCall) {
66
- // console.log('No tool call found');
67
65
  return {toolCalled: false};
68
66
  }
69
67
  const toolName = toolCall.function.name;
70
68
  const toolInput = JSON.parse(toolCall.function.arguments);
71
69
 
72
- // console.log('Calling tool:', toolName, toolInput);
73
70
  const toolOutput = await tools.execute(toolName, toolInput);
74
- // console.log('Tool called result:', toolOutput);
75
71
 
76
72
  return {
77
73
  toolCalled: true,
@@ -80,7 +76,6 @@ export async function parseAndExecuteTool(result: NativeCompletionResult, tools:
80
76
  toolOutput
81
77
  };
82
78
  } catch (error) {
83
- // console.error('Error parsing tool call:', error);
84
79
  return {toolCalled: false};
85
80
  }
86
81
  }
package/src/tts.ts CHANGED
@@ -31,10 +31,7 @@ export class CactusTTS {
31
31
  speakerJsonStr,
32
32
  textToSpeak,
33
33
  )
34
- // This part is simplified. In a real scenario, the tokens from
35
- // the main model would be generated and passed to decodeAudioTokens.
36
- // For now, we are assuming a direct path which may not be fully functional
37
- // without the main model's token output for TTS.
34
+ // To-DO: Fix
38
35
  const tokens = (await this.context.tokenize(formatted_prompt)).tokens
39
36
  return decodeAudioTokens(this.context.id, tokens)
40
37
  }
package/src/vlm.ts CHANGED
@@ -3,15 +3,15 @@ import {
3
3
  initMultimodal,
4
4
  multimodalCompletion,
5
5
  LlamaContext,
6
+ type ContextParams,
7
+ type CompletionParams,
8
+ type CactusOAICompatibleMessage,
9
+ type NativeCompletionResult,
6
10
  } from './index'
7
- import type {
8
- ContextParams,
9
- CompletionParams,
10
- CactusOAICompatibleMessage,
11
- NativeCompletionResult,
12
- } from './index'
11
+
13
12
  import { Telemetry } from './telemetry'
14
13
  import { setCactusToken, getTextCompletion, getVisionCompletion } from './remote'
14
+ import { ConversationHistoryManager } from './chat'
15
15
 
16
16
  interface CactusVLMReturn {
17
17
  vlm: CactusVLM | null
@@ -29,9 +29,11 @@ export type VLMCompletionParams = Omit<CompletionParams, 'prompt'> & {
29
29
 
30
30
  export class CactusVLM {
31
31
  private context: LlamaContext
32
-
32
+ protected conversationHistoryManager: ConversationHistoryManager
33
+
33
34
  private constructor(context: LlamaContext) {
34
35
  this.context = context
36
+ this.conversationHistoryManager = new ConversationHistoryManager()
35
37
  }
36
38
 
37
39
  static async init(
@@ -111,26 +113,47 @@ export class CactusVLM {
111
113
  return result;
112
114
  }
113
115
 
114
- private async _handleLocalCompletion(
116
+ private _handleLocalCompletion = async(
115
117
  messages: CactusOAICompatibleMessage[],
116
118
  params: VLMCompletionParams,
117
119
  callback?: (data: any) => void,
118
- ): Promise<NativeCompletionResult> {
120
+ ): Promise<NativeCompletionResult> => {
121
+ const { newMessages, requiresReset } =
122
+ this.conversationHistoryManager.processNewMessages(messages);
123
+
124
+ if (requiresReset) {
125
+ this.context?.rewind();
126
+ this.conversationHistoryManager.reset();
127
+ }
128
+
129
+ if (newMessages.length === 0) {
130
+ console.warn('No messages to complete!');
131
+ }
132
+
133
+ let result: NativeCompletionResult;
134
+
119
135
  if (params.images && params.images.length > 0) {
120
- const formattedPrompt = await this.context.getFormattedChat(messages)
136
+ const formattedPrompt = await this.context.getFormattedChat(newMessages)
121
137
  const prompt =
122
138
  typeof formattedPrompt === 'string'
123
139
  ? formattedPrompt
124
140
  : formattedPrompt.prompt
125
- return await multimodalCompletion(
141
+ result = await multimodalCompletion(
126
142
  this.context.id,
127
143
  prompt,
128
144
  params.images,
129
145
  { ...params, prompt, emit_partial_completion: !!callback },
130
146
  )
131
147
  } else {
132
- return await this.context.completion({ messages, ...params }, callback)
148
+ result = await this.context.completion({ messages: newMessages, ...params }, callback)
133
149
  }
150
+
151
+ this.conversationHistoryManager.update(newMessages, {
152
+ role: 'assistant',
153
+ content: result.content || result.text,
154
+ });
155
+
156
+ return result;
134
157
  }
135
158
 
136
159
  private async _handleRemoteCompletion(
@@ -181,7 +204,6 @@ export class CactusVLM {
181
204
  }
182
205
 
183
206
  async rewind(): Promise<void> {
184
- // @ts-ignore
185
207
  return this.context?.rewind()
186
208
  }
187
209