Initial import: Stitch 0.4.3 (versionCode 16)
The project's first commit, six days and 16 releases after it started. Everything up to this point existed only as files on one machine, with no history and no undo. Contents: single Gradle module, 62 Kotlin files (11,244 lines) and the Needle 2 JNI bridge, 70 tools, 87 passing unit tests, and the five documents in docs/ plus DEVELOPMENT_HISTORY.md and README.md. Not tracked: build outputs (build/, .cxx/, .gradle/), the release APKs in dist/ (~800 MB, rebuildable), local.properties, and .claude/settings.local.json — all machine-specific. The prebuilt libneedle.a files (38 MB) *are* tracked on purpose, so a checkout builds without depending on Hugging Face still serving them.
This commit is contained in:
commit
61409cb444
111 files changed
+15481
No files matched your search
@@ -0,0 +1,18 @@
|
||||
cmake_minimum_required(VERSION 3.22.1)
|
||||
project(stitch_needle LANGUAGES C CXX)
|
||||
|
||||
# Prebuilt Needle 2 engine from huggingface.co/Cactus-Compute/needle2 (android-arm64 / android-armv7).
|
||||
# Fetched by scripts/fetch-needle.sh into needle/<ABI>/libneedle.a.
|
||||
set(NEEDLE_LIB ${CMAKE_CURRENT_SOURCE_DIR}/needle/${ANDROID_ABI}/libneedle.a)
|
||||
|
||||
if(EXISTS ${NEEDLE_LIB})
|
||||
add_library(needle STATIC IMPORTED)
|
||||
set_target_properties(needle PROPERTIES IMPORTED_LOCATION ${NEEDLE_LIB})
|
||||
add_library(stitch_needle SHARED needle_jni.cpp)
|
||||
target_link_libraries(stitch_needle needle android log m)
|
||||
else()
|
||||
# No Needle build for this CPU (e.g. x86_64 emulator): link a stub that reports "unavailable".
|
||||
add_library(stitch_needle SHARED needle_jni.cpp needle_stub.cpp)
|
||||
target_link_libraries(stitch_needle android log m)
|
||||
endif()
|
||||
target_include_directories(stitch_needle PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/needle)
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,35 @@
|
||||
#ifndef NEEDLE_H
|
||||
#define NEEDLE_H
|
||||
|
||||
#ifndef NEEDLE_API
|
||||
#define NEEDLE_API __attribute__((visibility("default")))
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
NEEDLE_API int needle_init(
|
||||
const char* system_prompt,
|
||||
const char* tools_json,
|
||||
const char* tool_index_path
|
||||
);
|
||||
|
||||
NEEDLE_API int needle_complete(
|
||||
const char* input,
|
||||
int max_new_tokens,
|
||||
char* out,
|
||||
int out_capacity
|
||||
);
|
||||
|
||||
NEEDLE_API void needle_reset(void);
|
||||
|
||||
NEEDLE_API int needle_load(
|
||||
const unsigned char* cact,
|
||||
unsigned long long n
|
||||
);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,71 @@
|
||||
// JNI bridge to the Needle 2 C API (needle.h). Needle keeps one process-global conversation,
|
||||
// so the Kotlin side calls these from a single thread only.
|
||||
// Strings cross JNI as UTF-8 byte arrays: JNI's own string functions use "modified UTF-8",
|
||||
// which mangles emoji and other 4-byte characters.
|
||||
#include <jni.h>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "needle.h"
|
||||
|
||||
namespace {
|
||||
// needle_load may keep pointers into the model bytes, so they live for the life of the process.
|
||||
unsigned char* g_model = nullptr;
|
||||
|
||||
std::string toString(JNIEnv* env, jbyteArray bytes) {
|
||||
if (bytes == nullptr) return {};
|
||||
const jsize n = env->GetArrayLength(bytes);
|
||||
std::string out(static_cast<size_t>(n), '\0');
|
||||
env->GetByteArrayRegion(bytes, 0, n, reinterpret_cast<jbyte*>(out.data()));
|
||||
return out;
|
||||
}
|
||||
|
||||
jbyteArray toBytes(JNIEnv* env, const char* s) {
|
||||
const auto n = static_cast<jsize>(std::strlen(s));
|
||||
jbyteArray arr = env->NewByteArray(n);
|
||||
env->SetByteArrayRegion(arr, 0, n, reinterpret_cast<const jbyte*>(s));
|
||||
return arr;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_app_stitch_needle_NeedleNative_nativeLoad(JNIEnv* env, jclass, jbyteArray model) {
|
||||
if (g_model != nullptr) return 0;
|
||||
const jsize n = env->GetArrayLength(model);
|
||||
g_model = static_cast<unsigned char*>(std::malloc(static_cast<size_t>(n)));
|
||||
if (g_model == nullptr) return -1000;
|
||||
env->GetByteArrayRegion(model, 0, n, reinterpret_cast<jbyte*>(g_model));
|
||||
const int rc = needle_load(g_model, static_cast<unsigned long long>(n));
|
||||
if (rc < 0) {
|
||||
std::free(g_model);
|
||||
g_model = nullptr;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_app_stitch_needle_NeedleNative_nativeInit(JNIEnv* env, jclass, jbyteArray system, jbyteArray tools, jbyteArray indexPath) {
|
||||
const std::string sys = toString(env, system);
|
||||
const std::string tl = toString(env, tools);
|
||||
const std::string idx = toString(env, indexPath);
|
||||
return needle_init(sys.empty() ? nullptr : sys.c_str(), tl.c_str(), idx.empty() ? nullptr : idx.c_str());
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jbyteArray JNICALL
|
||||
Java_app_stitch_needle_NeedleNative_nativeComplete(JNIEnv* env, jclass, jbyteArray input, jint maxTokens) {
|
||||
const std::string in = toString(env, input);
|
||||
std::vector<char> out(64 * 1024, 0);
|
||||
const int rc = needle_complete(in.c_str(), maxTokens, out.data(), static_cast<int>(out.size()));
|
||||
if (rc < 0) {
|
||||
const std::string err = "{\"type\":\"error\",\"error_code\":" + std::to_string(rc) + "}";
|
||||
return toBytes(env, err.c_str());
|
||||
}
|
||||
out.back() = '\0';
|
||||
return toBytes(env, out.data());
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL
|
||||
Java_app_stitch_needle_NeedleNative_nativeReset(JNIEnv*, jclass) {
|
||||
needle_reset();
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Stand-in for CPU types Needle has no prebuilt engine for (x86_64 emulators): every call reports failure,
|
||||
// so the app runs normally with the on-device command model shown as unavailable.
|
||||
#include "needle.h"
|
||||
|
||||
extern "C" {
|
||||
int needle_init(const char*, const char*, const char*) { return -1; }
|
||||
int needle_complete(const char*, int, char*, int) { return -1; }
|
||||
void needle_reset(void) {}
|
||||
int needle_load(const unsigned char*, unsigned long long) { return -1; }
|
||||
}
|
||||
Reference in new issue
Block a user