Skip to content

Commit 0edf157

Browse files
Merge pull request #1761 from nextcloud/bugfix/filter-chat-type-assistant
BugFix - Filter Only One TextInput and one TextOutput Task Types
2 parents 3ae4c07 + 8fc329b commit 0edf157

4 files changed

Lines changed: 59 additions & 31 deletions

File tree

library/src/androidTest/java/com/owncloud/android/lib/resources/assistant/v2/AssistantV2Tests.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ package com.owncloud.android.lib.resources.assistant.v2
1010

1111
import com.owncloud.android.AbstractIT
1212
import com.owncloud.android.lib.resources.assistant.v2.model.Shape
13-
import com.owncloud.android.lib.resources.assistant.v2.model.TaskInputShape
14-
import com.owncloud.android.lib.resources.assistant.v2.model.TaskOutputShape
1513
import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData
1614
import com.owncloud.android.lib.resources.status.NextcloudVersion
1715
import junit.framework.TestCase.assertEquals
@@ -31,17 +29,17 @@ class AssistantV2Tests : AbstractIT() {
3129
"Free text to text prompt",
3230
"Runs an arbitrary prompt through a language model that returns a reply",
3331
inputShape =
34-
TaskInputShape(
35-
input =
32+
mapOf(
33+
"input" to
3634
Shape(
3735
"Prompt",
3836
"Describe a task that you want the assistant to do or ask a question",
3937
"Text"
4038
)
4139
),
4240
outputShape =
43-
TaskOutputShape(
44-
output =
41+
mapOf(
42+
"output" to
4543
Shape(
4644
"Generated reply",
4745
"The generated text from the assistant",

library/src/main/java/com/owncloud/android/lib/resources/assistant/v1/model/TaskTypes.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ data class TaskTypes(
1616

1717
data class TaskType(
1818
val id: String?,
19-
val name: String?,
19+
val name: String,
2020
val description: String?
2121
)
2222

@@ -26,7 +26,7 @@ fun TaskTypes.toV2(): List<TaskTypeData> =
2626
id = taskType.id,
2727
name = taskType.name,
2828
description = taskType.description,
29-
inputShape = null,
30-
outputShape = null
29+
inputShape = emptyMap(),
30+
outputShape = emptyMap()
3131
)
3232
}

library/src/main/java/com/owncloud/android/lib/resources/assistant/v2/GetTaskTypesRemoteOperationV2.kt

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,32 @@ import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData
1919
import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypes
2020
import org.apache.commons.httpclient.HttpStatus
2121

22+
/**
23+
* Returns a list of supported task types.
24+
*
25+
* Example JSON representation of one task type:
26+
* ```
27+
* {
28+
* "id": "core:text2text",
29+
* "name": "Free text to text prompt",
30+
* "description": "Runs an arbitrary prompt through a language model that returns a reply",
31+
* "inputShape": {
32+
* "input": {
33+
* "name": "Prompt",
34+
* "description": "Describe a task that you want the assistant to do or ask a question",
35+
* "type": "Text"
36+
* }
37+
* },
38+
* "outputShape": {
39+
* "output": {
40+
* "name": "Generated reply",
41+
* "description": "The generated text from the assistant",
42+
* "type": "Text"
43+
* }
44+
* }
45+
* }
46+
* ```
47+
*/
2248
class GetTaskTypesRemoteOperationV2 : OCSRemoteOperation<List<TaskTypeData>>() {
2349
private val supportedTaskType = "Text"
2450

@@ -36,21 +62,18 @@ class GetTaskTypesRemoteOperationV2 : OCSRemoteOperation<List<TaskTypeData>>() {
3662
getServerResponse(
3763
getMethod,
3864
object : TypeToken<ServerResponse<TaskTypes>>() {}
39-
)?.ocs?.data?.types
65+
)
4066

4167
val taskTypeList =
42-
response?.map { (key, value) ->
43-
value.copy(id = value.id ?: key)
44-
}
45-
46-
val supportedTaskTypeList =
47-
taskTypeList?.filter { taskType ->
48-
taskType.inputShape?.input?.type == supportedTaskType &&
49-
taskType.outputShape?.output?.type == supportedTaskType
50-
}
68+
response
69+
?.ocs
70+
?.data
71+
?.types
72+
?.map { (key, value) -> value.copy(id = value.id ?: key) }
73+
?.filter { taskType -> isSingleTextInputOutput(taskType) }
5174

5275
result = RemoteOperationResult(true, getMethod)
53-
result.setResultData(supportedTaskTypeList)
76+
result.resultData = taskTypeList
5477
} else {
5578
result = RemoteOperationResult(false, getMethod)
5679
}
@@ -67,6 +90,21 @@ class GetTaskTypesRemoteOperationV2 : OCSRemoteOperation<List<TaskTypeData>>() {
6790
return result
6891
}
6992

93+
private fun isSingleTextInputOutput(taskType: TaskTypeData): Boolean {
94+
val inputShape = taskType.inputShape
95+
val outputShape = taskType.outputShape
96+
97+
val hasOneTextInput =
98+
inputShape.size == 1 &&
99+
inputShape.values.first().type == supportedTaskType
100+
101+
val hasOneTextOutput =
102+
outputShape.size == 1 &&
103+
outputShape.values.first().type == supportedTaskType
104+
105+
return hasOneTextInput && hasOneTextOutput
106+
}
107+
70108
companion object {
71109
private val TAG = GetTaskTypesRemoteOperationV2::class.java.simpleName
72110
private const val DIRECT_ENDPOINT = "/ocs/v2.php/taskprocessing/tasktypes"

library/src/main/java/com/owncloud/android/lib/resources/assistant/v2/model/TaskTypes.kt

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,10 @@ data class TaskTypes(
1414

1515
data class TaskTypeData(
1616
val id: String?,
17-
val name: String?,
17+
val name: String,
1818
val description: String?,
19-
val inputShape: TaskInputShape?,
20-
val outputShape: TaskOutputShape?
21-
)
22-
23-
data class TaskInputShape(
24-
val input: Shape?
25-
)
26-
27-
data class TaskOutputShape(
28-
val output: Shape?
19+
val inputShape: Map<String, Shape>,
20+
val outputShape: Map<String, Shape>
2921
)
3022

3123
data class Shape(

0 commit comments

Comments
 (0)