diff --git a/composeApp/src/androidMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentPicker.android.kt b/composeApp/src/androidMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentPicker.android.kt new file mode 100644 index 0000000..c9ac8fe --- /dev/null +++ b/composeApp/src/androidMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentPicker.android.kt @@ -0,0 +1,39 @@ +package cn.xiaobai.openclawapp.platform + +import android.provider.OpenableColumns +import androidx.activity.compose.rememberLauncherForActivityResult +import androidx.activity.result.contract.ActivityResultContracts +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import androidx.compose.ui.platform.LocalContext + +@Composable +actual fun rememberAttachmentPicker( + onPicked: (PickedAttachment) -> Unit, + onError: (String) -> Unit, +): () -> Unit { + val context = LocalContext.current + val resolver = context.contentResolver + val launcher = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri -> + if (uri == null) return@rememberLauncherForActivityResult + runCatching { + val name = resolver.query(uri, null, null, null, null)?.use { cursor -> + val index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) + if (index >= 0 && cursor.moveToFirst()) cursor.getString(index) else null + } ?: "attachment" + val mimeType = resolver.getType(uri) + val bytes = resolver.openInputStream(uri)?.use { it.readBytes() } + ?: error("无法读取所选文件") + PickedAttachment( + fileName = name, + bytes = bytes, + contentType = mimeType, + ) + }.onSuccess(onPicked).onFailure { + onError(it.message ?: "文件选择失败") + } + } + return remember(launcher) { + { launcher.launch("*/*") } + } +} diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt index a6feb9d..a9049c4 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt @@ -26,6 +26,7 @@ import cn.xiaobai.openclawapp.feature.config.ServerConfigScreen import cn.xiaobai.openclawapp.feature.sessions.SessionListScreen import cn.xiaobai.openclawapp.feature.settings.SettingsScreen import cn.xiaobai.openclawapp.feature.status.StatusScreen +import cn.xiaobai.openclawapp.platform.rememberAttachmentPicker import cn.xiaobai.openclawapp.ui.OpenClawTheme import cn.xiaobai.openclawapp.ui.StatusPill import kotlinx.coroutines.delay @@ -80,6 +81,22 @@ fun App() { private fun OpenClawRoot(appState: OpenClawAppState) { val activeSession = appState.activeSessionId?.let(appState.repository::findSession) val coroutineScope = rememberCoroutineScope() + val pickAttachment = rememberAttachmentPicker( + onPicked = { picked -> + val sessionId = activeSession?.id ?: return@rememberAttachmentPicker + coroutineScope.launch { + appState.uploadAttachmentStub( + sessionId = sessionId, + fileName = picked.fileName, + bytes = picked.bytes, + contentType = picked.contentType, + ) + } + }, + onError = { message -> + appState.feedbackMessage = message + }, + ) LaunchedEffect(appState.feedbackMessage) { if (appState.feedbackMessage != null) { @@ -178,13 +195,7 @@ private fun OpenClawRoot(appState: OpenClawAppState) { } }, onSendAttachmentStub = { - coroutineScope.launch { - appState.uploadAttachmentStub( - sessionId = activeSession.id, - fileName = "demo.txt", - bytes = "demo attachment from mobile".encodeToByteArray(), - ) - } + pickAttachment() }, ) diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt index a2e77b2..a888a37 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt @@ -138,9 +138,14 @@ class OpenClawAppState( } } - suspend fun uploadAttachmentStub(sessionId: String, fileName: String, bytes: ByteArray) { + suspend fun uploadAttachmentStub( + sessionId: String, + fileName: String, + bytes: ByteArray, + contentType: String? = null, + ) { runCatching { - repository.uploadAttachmentStub(sessionId, fileName, bytes) + repository.uploadAttachmentStub(sessionId, fileName, bytes, contentType) feedbackMessage = "附件已加入发送队列" }.onFailure { syncError = it.message ?: "附件上传失败" diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt index 0356c6d..9ac7b9a 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt @@ -14,5 +14,10 @@ interface OpenClawRepository { fun togglePinned(sessionId: String): Boolean fun sendMessage(sessionId: String, text: String) suspend fun updateDefaultAgent(agent: String) - suspend fun uploadAttachmentStub(sessionId: String, fileName: String, bytes: ByteArray) + suspend fun uploadAttachmentStub( + sessionId: String, + fileName: String, + bytes: ByteArray, + contentType: String? = null, + ) } diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt index d2d725a..e24f1cb 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt @@ -124,7 +124,12 @@ class RemoteOpenClawRepository( ) } - override suspend fun uploadAttachmentStub(sessionId: String, fileName: String, bytes: ByteArray) { + override suspend fun uploadAttachmentStub( + sessionId: String, + fileName: String, + bytes: ByteArray, + contentType: String? = null, + ) { val currentMessages = messageStore[sessionId].orEmpty() val localId = "local-attachment-$sessionId-${currentMessages.size + 1}" val pending = ChatMessage( @@ -139,7 +144,11 @@ class RemoteOpenClawRepository( updateSessionPreview(sessionId, pending) runCatching { - val remote = api.uploadAttachment(sessionId, fileName, bytes) + val remote = api.uploadAttachment( + sessionId = sessionId, + fileName = fileName, + bytes = bytes, + ) val uploaded = ChatMessage( id = remote.attachmentId, role = MessageRole.User, diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentPicker.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentPicker.kt new file mode 100644 index 0000000..fa62cf0 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentPicker.kt @@ -0,0 +1,15 @@ +package cn.xiaobai.openclawapp.platform + +import androidx.compose.runtime.Composable + +data class PickedAttachment( + val fileName: String, + val bytes: ByteArray, + val contentType: String? = null, +) + +@Composable +expect fun rememberAttachmentPicker( + onPicked: (PickedAttachment) -> Unit, + onError: (String) -> Unit, +): () -> Unit diff --git a/composeApp/src/iosMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentPicker.ios.kt b/composeApp/src/iosMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentPicker.ios.kt new file mode 100644 index 0000000..0796811 --- /dev/null +++ b/composeApp/src/iosMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentPicker.ios.kt @@ -0,0 +1,12 @@ +package cn.xiaobai.openclawapp.platform + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember + +@Composable +actual fun rememberAttachmentPicker( + onPicked: (PickedAttachment) -> Unit, + onError: (String) -> Unit, +): () -> Unit = remember(onPicked, onError) { + { onError("iOS 文件选择器还没接好") } +} diff --git a/composeApp/src/jsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentPicker.js.kt b/composeApp/src/jsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentPicker.js.kt new file mode 100644 index 0000000..7df5b32 --- /dev/null +++ b/composeApp/src/jsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentPicker.js.kt @@ -0,0 +1,12 @@ +package cn.xiaobai.openclawapp.platform + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember + +@Composable +actual fun rememberAttachmentPicker( + onPicked: (PickedAttachment) -> Unit, + onError: (String) -> Unit, +): () -> Unit = remember(onPicked, onError) { + { onError("Web 文件选择器还没接好") } +} diff --git a/composeApp/src/wasmJsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentPicker.wasmJs.kt b/composeApp/src/wasmJsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentPicker.wasmJs.kt new file mode 100644 index 0000000..c421e5d --- /dev/null +++ b/composeApp/src/wasmJsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentPicker.wasmJs.kt @@ -0,0 +1,12 @@ +package cn.xiaobai.openclawapp.platform + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember + +@Composable +actual fun rememberAttachmentPicker( + onPicked: (PickedAttachment) -> Unit, + onError: (String) -> Unit, +): () -> Unit = remember(onPicked, onError) { + { onError("Wasm 文件选择器还没接好") } +}