MCPcopy Create free account
hub / github.com/cactus-compute/cactus / Java_com_cactus_CactusJNI_nativeComplete

Function Java_com_cactus_CactusJNI_nativeComplete

android/cactus_jni.cpp:93–153  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

91}
92
93JNIEXPORT jstring JNICALL
94Java_com_cactus_CactusJNI_nativeComplete(JNIEnv* env, jobject, jlong handle,
95 jstring messagesJson, jstring optionsJson,
96 jstring toolsJson, jobject callback,
97 jbyteArray pcmData) {
98 if (handle == 0) { throwCactusException(env, "Model not initialized"); return nullptr; }
99
100 const char* messages = jstring_to_cstr(env, messagesJson);
101 const char* options = jstring_to_cstr(env, optionsJson);
102 const char* tools = jstring_to_cstr(env, toolsJson);
103
104 std::vector<char> buffer(DEFAULT_BUFFER_SIZE);
105
106 TokenCallbackContext* ctx = nullptr;
107 cactus_token_callback cb = nullptr;
108
109 if (callback != nullptr) {
110 jclass callbackClass = env->GetObjectClass(callback);
111 jmethodID method = env->GetMethodID(callbackClass, "onToken", "(Ljava/lang/String;I)V");
112 if (method != nullptr) {
113 ctx = new TokenCallbackContext{env, callback, method};
114 cb = token_callback_bridge;
115 }
116 }
117
118 const uint8_t* pcmBuffer = nullptr;
119 size_t pcmSize = 0;
120 jbyte* pcmBytes = nullptr;
121
122 if (pcmData != nullptr) {
123 pcmSize = env->GetArrayLength(pcmData);
124 pcmBytes = env->GetByteArrayElements(pcmData, nullptr);
125 pcmBuffer = reinterpret_cast<const uint8_t*>(pcmBytes);
126 }
127
128 int result = cactus_complete(
129 reinterpret_cast<cactus_model_t>(handle),
130 messages,
131 buffer.data(),
132 buffer.size(),
133 options,
134 tools,
135 cb,
136 ctx,
137 pcmBuffer,
138 pcmSize
139 );
140
141 delete ctx;
142
143 if (pcmBytes != nullptr) {
144 env->ReleaseByteArrayElements(pcmData, pcmBytes, JNI_ABORT);
145 }
146
147 release_jstring(env, messagesJson, messages);
148 release_jstring(env, optionsJson, options);
149 release_jstring(env, toolsJson, tools);
150

Callers

nothing calls this directly

Calls 7

throwCactusExceptionFunction · 0.85
jstring_to_cstrFunction · 0.85
release_jstringFunction · 0.85
throwOnErrorFunction · 0.85
dataMethod · 0.80
sizeMethod · 0.80
cactus_completeFunction · 0.50

Tested by

no test coverage detected