Skip to content

Commit

Permalink
v4.13
Browse files Browse the repository at this point in the history
  • Loading branch information
AndraxDev committed Jun 8, 2024
1 parent 1413321 commit 497fda0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ android {
applicationId "org.teslasoft.assistant"
minSdk 28
targetSdk 34
versionCode 412
versionName "4.12"
versionCode 413
versionName "4.13"
}

buildTypes {
Expand Down Expand Up @@ -83,7 +83,7 @@ dependencies {
implementation 'com.google.mlkit:language-id:17.0.5'
implementation 'cat.ereza:customactivityoncrash:2.4.0'
implementation 'org.conscrypt:conscrypt-android:2.5.2'
implementation 'com.aallam.ktoken:ktoken:0.3.0'
implementation 'com.aallam.ktoken:ktoken:0.4.0'
implementation 'androidx.core:core-splashscreen:1.0.1'
implementation ('io.noties:prism4j:2.0.0') {
exclude group: 'org.jetbrains', module: 'annotations'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1913,7 +1913,7 @@ class ChatActivity : FragmentActivity(), ChatAdapter.OnUpdateListener {
completions.collect { v ->
run {
if (!coroutineContext.isActive) throw CancellationException()
else if (v.choices[0].delta.content != "null") {
else if (v.choices[0].delta != null && v.choices[0].delta.content != null && v.choices[0].delta.content.toString() != "null") {
response += v.choices[0].delta.content
if (response != "null") {
messages[messages.size - 1]["message"] = response
Expand Down Expand Up @@ -1966,7 +1966,7 @@ class ChatActivity : FragmentActivity(), ChatAdapter.OnUpdateListener {
completions.collect { v ->
run {
if (!coroutineContext.isActive) throw CancellationException()
else if (v.choices[0].text != "null") {
else if (v.choices[0] != null && v.choices[0].text != null && v.choices[0].text.toString() != "null") {
response += v.choices[0].text
messages[messages.size - 1]["message"] = response
if (messages.size > 2) {
Expand Down Expand Up @@ -2231,7 +2231,7 @@ class ChatActivity : FragmentActivity(), ChatAdapter.OnUpdateListener {
completions.collect { v ->
run {
if (!coroutineContext.isActive) throw CancellationException()
else if (v.choices[0].delta.content != null) {
else if (v.choices[0].delta != null && v.choices[0].delta.content != null && v.choices[0].delta.content.toString() != "null") {
response += v.choices[0].delta.content
messages[messages.size - 1]["message"] = response
if (messages.size > 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1046,10 +1046,18 @@ class AssistantFragment : BottomSheetDialogFragment(), ChatAdapter.OnUpdateListe
} else if ((mContext as Activity?)?.intent?.action == Intent.ACTION_SEND && ((mContext as Activity?)?.intent?.type == "image/png" || (mContext as Activity?)?.intent?.type == "image/jpeg")) {
val uri = (mContext as Activity?)?.intent?.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
if (uri != null) {
val bitmap = readFile(uri)
var bitmap: Bitmap? = null

val th = Thread {
bitmap = readFile(uri)
}

th.start()
th.join()

if (bitmap != null) {
attachedImage?.visibility = View.VISIBLE
selectedImage?.setImageBitmap(roundCorners(bitmap, 80f))
selectedImage?.setImageBitmap(roundCorners(bitmap!!, 80f))
imageIsSelected = true

val mimeType = mContext?.contentResolver?.getType(uri)
Expand All @@ -1066,19 +1074,31 @@ class AssistantFragment : BottomSheetDialogFragment(), ChatAdapter.OnUpdateListe

// Step 3: Convert the Bitmap to a Base64-encoded string
val outputStream = ByteArrayOutputStream()
bitmap.compress(format, 100, outputStream) // Note: Adjust the quality as necessary
val base64Image = android.util.Base64.encodeToString(outputStream.toByteArray(), android.util.Base64.DEFAULT)

// Step 4: Generate the data URL
val imageType = when(format) {
Bitmap.CompressFormat.JPEG -> "jpeg"
Bitmap.CompressFormat.PNG -> "png"
// Add more mappings as necessary
else -> ""
}

baseImageString = "data:image/$imageType;base64,$base64Image"
showKeyboard()
Thread {
bitmap!!.compress(format, 100, outputStream) // Note: Adjust the quality as necessary
val base64Image = android.util.Base64.encodeToString(outputStream.toByteArray(), android.util.Base64.DEFAULT)

// Step 4: Generate the data URL
val imageType = when (format) {
Bitmap.CompressFormat.JPEG -> {
selectedImageType = "jpg"
"jpeg"
}
Bitmap.CompressFormat.PNG -> {
selectedImageType = "png"
"png"
}
// Add more mappings as necessary
else -> ""
}

baseImageString = "data:image/$imageType;base64,$base64Image"

(mContext as Activity?)?.runOnUiThread {
showKeyboard()
}
}.start()
}
}
} else {
Expand Down Expand Up @@ -1175,8 +1195,10 @@ class AssistantFragment : BottomSheetDialogFragment(), ChatAdapter.OnUpdateListe
val m = prefix + message + endSeparator

if (imageIsSelected) {
val bytes = android.util.Base64.decode(baseImageString!!.split(",")[1], android.util.Base64.DEFAULT)
writeImageToCache(bytes, selectedImageType!!)

val bytes = android.util.Base64.decode(baseImageString?.split(",")?.get(1) ?: return, android.util.Base64.DEFAULT)

writeImageToCache(bytes, selectedImageType ?: return)

val encoded = java.util.Base64.getEncoder().encodeToString(bytes)

Expand Down Expand Up @@ -1364,7 +1386,7 @@ class AssistantFragment : BottomSheetDialogFragment(), ChatAdapter.OnUpdateListe
stopper = false
return@collect
}
if (v.choices[0].delta.content != "null") {
if (v.choices[0].delta != null && v.choices[0].delta.content != null && v.choices[0].delta.content.toString() != "null") {
response += v.choices[0].delta.content
if (response != "null") {
messages[messages.size - 1]["message"] = response
Expand Down Expand Up @@ -1422,7 +1444,7 @@ class AssistantFragment : BottomSheetDialogFragment(), ChatAdapter.OnUpdateListe
stopper = false
return@collect
}
if (v.choices[0].text != "null") {
if (v.choices[0] != null && v.choices[0].text != null && v.choices[0].text.toString() != "null") {
response += v.choices[0].text
messages[messages.size - 1]["message"] = response
if (messages.size > 2) {
Expand Down Expand Up @@ -1684,7 +1706,7 @@ class AssistantFragment : BottomSheetDialogFragment(), ChatAdapter.OnUpdateListe
stopper = false
return@collect
}
if (v.choices[0].delta.content != null) {
if (v.choices[0].delta != null && v.choices[0].delta.content != null && v.choices[0].delta.content.toString() != "null") {
response += v.choices[0].delta.content
messages[messages.size - 1]["message"] = response
scroll(false)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_assistant.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginBottom="329dp"
android:layout_marginBottom="16dp"
android:elevation="12dp"
android:orientation="vertical"
android:visibility="gone"
Expand Down

0 comments on commit 497fda0

Please sign in to comment.