fix(android): fix android build error (#13117)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **New Features**
* Improved chat session and history retrieval with support for paginated
results in the chat interface.

* **Bug Fixes**
* Enhanced reliability when loading large numbers of chat messages and
histories.

* **Refactor**
* Updated chat data handling to align with the latest backend schema and
pagination model.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
EYHN
2025-07-09 20:52:05 +08:00
committed by GitHub
parent 45c016af8b
commit f54cb5c296
2 changed files with 22 additions and 10 deletions

View File

@@ -1,6 +1,8 @@
package app.affine.pro.ai.chat
import com.affine.pro.graphql.GetCopilotHistoriesQuery
import com.affine.pro.graphql.fragment.CopilotChatHistory
import com.affine.pro.graphql.fragment.CopilotChatMessage
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
@@ -51,11 +53,11 @@ data class ChatMessage(
createAt = Clock.System.now(),
)
fun from(message: GetCopilotHistoriesQuery.Message) = ChatMessage(
fun from(message: CopilotChatMessage) = ChatMessage(
id = message.id,
role = Role.fromValue(message.role),
content = message.content,
createAt = message.createdAt,
)
}
}
}

View File

@@ -9,7 +9,8 @@ import com.affine.pro.graphql.GetCopilotHistoryIdsQuery
import com.affine.pro.graphql.GetCopilotSessionsQuery
import com.affine.pro.graphql.type.CreateChatMessageInput
import com.affine.pro.graphql.type.CreateChatSessionInput
import com.affine.pro.graphql.type.QueryChatSessionsInput
import com.affine.pro.graphql.type.PaginationInput
import com.affine.pro.graphql.type.QueryChatHistoriesInput
import com.apollographql.apollo.ApolloClient
import com.apollographql.apollo.api.Mutation
import com.apollographql.apollo.api.Optional
@@ -29,12 +30,15 @@ class GraphQLService @Inject constructor() {
GetCopilotSessionsQuery(
workspaceId = workspaceId,
docId = Optional.present(docId),
options = Optional.present(QueryChatSessionsInput(action = Optional.present(false)))
pagination = PaginationInput(
first = Optional.present(100)
),
options = Optional.present(QueryChatHistoriesInput(action = Optional.present(false)))
)
).mapCatching { data ->
data.currentUser?.copilot?.sessions?.find {
data.currentUser?.copilot?.chats?.paginatedCopilotChats?.edges?.map { item -> item.node.copilotChatHistory }?.find {
it.parentSessionId == null
}?.id ?: error(ERROR_NULL_SESSION_ID)
}?.sessionId ?: error(ERROR_NULL_SESSION_ID)
}
suspend fun createCopilotSession(
@@ -60,12 +64,15 @@ class GraphQLService @Inject constructor() {
) = query(
GetCopilotHistoriesQuery(
workspaceId = workspaceId,
pagination = PaginationInput(
first = Optional.present(100)
),
docId = Optional.present(docId),
)
).mapCatching { data ->
data.currentUser?.copilot?.histories?.firstOrNull { history ->
history.sessionId == sessionId
}?.messages ?: emptyList()
data.currentUser?.copilot?.chats?.paginatedCopilotChats?.edges?.map { item -> item.node.copilotChatHistory }?.firstOrNull { history ->
history.sessionId == sessionId
}?.messages?.map { msg -> msg.copilotChatMessage } ?: emptyList()
}
suspend fun getCopilotHistoryIds(
@@ -76,9 +83,12 @@ class GraphQLService @Inject constructor() {
GetCopilotHistoryIdsQuery(
workspaceId = workspaceId,
docId = Optional.present(docId),
pagination = PaginationInput(
first = Optional.present(100)
),
)
).mapCatching { data ->
data.currentUser?.copilot?.histories?.firstOrNull { history ->
data.currentUser?.copilot?.chats?.edges?.map { item -> item.node }?.firstOrNull { history ->
history.sessionId == sessionId
}?.messages ?: emptyList()
}