Skip to content

Commit

Permalink
Merge pull request #1599 from nextcloud/update-assistant-api
Browse files Browse the repository at this point in the history
Update Assistant API
  • Loading branch information
tobiasKaminsky authored Dec 16, 2024
2 parents 64eb249 + 74d072c commit 638326e
Show file tree
Hide file tree
Showing 16 changed files with 507 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 Tobias Kaminsky <[email protected]>
* SPDX-FileCopyrightText: 2024 Alper Ozturk <[email protected]>
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.assistant
package com.owncloud.android.lib.resources.assistant.v1

import com.owncloud.android.AbstractIT
import com.owncloud.android.lib.resources.status.NextcloudVersion
Expand All @@ -15,15 +15,15 @@ import junit.framework.TestCase.assertTrue
import org.junit.Before
import org.junit.Test

class AssistantIT : AbstractIT() {
class AssistantV1Tests : AbstractIT() {
@Before
fun before() {
testOnlyOnServer(NextcloudVersion.nextcloud_28)
}

@Test
fun testGetTaskTypes() {
val result = GetTaskTypesRemoteOperation().execute(nextcloudClient)
val result = GetTaskTypesRemoteOperationV1().execute(nextcloudClient)
assertTrue(result.isSuccess)

val taskTypes = result.resultData.types
Expand All @@ -32,16 +32,16 @@ class AssistantIT : AbstractIT() {

@Test
fun testGetTaskList() {
var result = GetTaskListRemoteOperation("assistant").execute(nextcloudClient)
var result = GetTaskListRemoteOperationV1("assistant").execute(nextcloudClient)
assertTrue(result.isSuccess)
assertTrue(result.resultData.tasks.isEmpty())

// create one task
val input = "Give me some random output for test purpose"
val type = "OCP\\TextProcessing\\FreePromptTaskType"
assertTrue(CreateTaskRemoteOperation(input, type).execute(nextcloudClient).isSuccess)
assertTrue(CreateTaskRemoteOperationV1(input, type).execute(nextcloudClient).isSuccess)

result = GetTaskListRemoteOperation("assistant").execute(nextcloudClient)
result = GetTaskListRemoteOperationV1("assistant").execute(nextcloudClient)
assertTrue(result.isSuccess)

val taskList = result.resultData.tasks
Expand All @@ -53,18 +53,18 @@ class AssistantIT : AbstractIT() {
// create one task
val input = "Give me some random output for test purpose"
val type = "OCP\\TextProcessing\\FreePromptTaskType"
assertTrue(CreateTaskRemoteOperation(input, type).execute(nextcloudClient).isSuccess)
assertTrue(CreateTaskRemoteOperationV1(input, type).execute(nextcloudClient).isSuccess)

var result = GetTaskListRemoteOperation("assistant").execute(nextcloudClient)
var result = GetTaskListRemoteOperationV1("assistant").execute(nextcloudClient)
assertTrue(result.isSuccess)

val tasks = result.resultData.tasks
val countBefore = tasks.size

// delete
assertTrue(DeleteTaskRemoteOperation(tasks.first().id).execute(nextcloudClient).isSuccess)
assertTrue(DeleteTaskRemoteOperationV1(tasks.first().id).execute(nextcloudClient).isSuccess)

result = GetTaskListRemoteOperation("assistant").execute(nextcloudClient)
result = GetTaskListRemoteOperationV1("assistant").execute(nextcloudClient)
assertTrue(result.isSuccess)

assertEquals(countBefore - 1, result.resultData.tasks.size)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 Alper Ozturk <[email protected]>
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.assistant.v2

import com.owncloud.android.AbstractIT
import com.owncloud.android.lib.resources.assistant.v2.model.TaskInputShape
import com.owncloud.android.lib.resources.assistant.v2.model.TaskOutputShape
import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData
import com.owncloud.android.lib.resources.status.NextcloudVersion
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertTrue
import org.junit.Before
import org.junit.Test

class AssistantV2Tests : AbstractIT() {
@Before
fun before() {
testOnlyOnServer(NextcloudVersion.nextcloud_30)
}

private fun getTaskType(): TaskTypeData {
return TaskTypeData(
"core:text2text",
"Free text to text prompt",
"Runs an arbitrary prompt through a language model that returns a reply",
listOf(
TaskInputShape(
"Prompt",
"Describe a task that you want the assistant to do or ask a question",
"Text"
)
),
listOf(
TaskOutputShape(
"Generated reply",
"The generated text from the assistant",
"Text"
)
)
)
}

private fun getSelectedTaskType(): String = "core:text2text"

@Test
fun testGetTaskTypes() {
val result = GetTaskTypesRemoteOperationV2().execute(nextcloudClient)
assertTrue(result.isSuccess)
}

@Test
fun testGetTaskList() {
val taskType = getTaskType()
val selectedTaskType = getSelectedTaskType()

var result = GetTaskListRemoteOperationV2(selectedTaskType).execute(nextcloudClient)
assertTrue(result.isSuccess)
assertTrue(result.resultData.tasks.isEmpty())

// create one task
val input = "Give me some random output for test purpose"
assertTrue(CreateTaskRemoteOperationV2(input, taskType).execute(nextcloudClient).isSuccess)

result = GetTaskListRemoteOperationV2(selectedTaskType).execute(nextcloudClient)
assertTrue(result.isSuccess)

val taskList = result.resultData.tasks
assertTrue(taskList.isNotEmpty())
}

@Test
fun testDeleteTask() {
// create one task
val input = "Give me some random output for test purpose"
val taskType = getTaskType()
val selectedTaskType = getSelectedTaskType()

assertTrue(CreateTaskRemoteOperationV2(input, taskType).execute(nextcloudClient).isSuccess)

var result = GetTaskListRemoteOperationV2(selectedTaskType).execute(nextcloudClient)
assertTrue(result.isSuccess)

val tasks = result.resultData.tasks
val countBefore = tasks.size

// delete
assertTrue(DeleteTaskRemoteOperationV2(tasks.first().id).execute(nextcloudClient).isSuccess)

result = GetTaskListRemoteOperationV2(selectedTaskType).execute(nextcloudClient)
assertTrue(result.isSuccess)

assertEquals(countBefore - 1, result.resultData.tasks.size)
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.assistant
package com.owncloud.android.lib.resources.assistant.v1

import com.nextcloud.common.NextcloudClient
import com.nextcloud.operations.PostMethod
Expand All @@ -16,7 +16,7 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.apache.commons.httpclient.HttpStatus

class CreateTaskRemoteOperation(private val input: String, private val type: String) :
class CreateTaskRemoteOperationV1(private val input: String, private val type: String) :
RemoteOperation<Void>() {
override fun run(client: NextcloudClient): RemoteOperationResult<Void> {
val requestBody =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.assistant
package com.owncloud.android.lib.resources.assistant.v1

import com.nextcloud.common.NextcloudClient
import com.nextcloud.operations.DeleteMethod
Expand All @@ -17,7 +17,7 @@ import com.owncloud.android.lib.resources.users.DeletePrivateKeyRemoteOperation
import java.io.IOException
import java.net.HttpURLConnection

class DeleteTaskRemoteOperation(private val appId: Long) : RemoteOperation<Void>() {
class DeleteTaskRemoteOperationV1(private val appId: Long) : RemoteOperation<Void>() {
override fun run(client: NextcloudClient): RemoteOperationResult<Void> {
var postMethod: DeleteMethod? = null
var result: RemoteOperationResult<Void>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.assistant
package com.owncloud.android.lib.resources.assistant.v1

import com.google.gson.reflect.TypeToken
import com.nextcloud.common.NextcloudClient
Expand All @@ -15,10 +15,10 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.lib.ocs.ServerResponse
import com.owncloud.android.lib.resources.OCSRemoteOperation
import com.owncloud.android.lib.resources.assistant.model.TaskList
import com.owncloud.android.lib.resources.assistant.v1.model.TaskList
import org.apache.commons.httpclient.HttpStatus

class GetTaskListRemoteOperation(private val appId: String) : OCSRemoteOperation<TaskList>() {
class GetTaskListRemoteOperationV1(private val appId: String) : OCSRemoteOperation<TaskList>() {
@Suppress("TooGenericExceptionCaught")
override fun run(client: NextcloudClient): RemoteOperationResult<TaskList> {
var result: RemoteOperationResult<TaskList>
Expand Down Expand Up @@ -52,7 +52,7 @@ class GetTaskListRemoteOperation(private val appId: String) : OCSRemoteOperation
}

companion object {
private val TAG = GetTaskTypesRemoteOperation::class.java.simpleName
private val TAG = GetTaskListRemoteOperationV1::class.java.simpleName
private const val DIRECT_ENDPOINT = "/ocs/v2.php/textprocessing/tasks/app/"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.assistant
package com.owncloud.android.lib.resources.assistant.v1

import com.google.gson.reflect.TypeToken
import com.nextcloud.common.NextcloudClient
Expand All @@ -15,10 +15,10 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.lib.ocs.ServerResponse
import com.owncloud.android.lib.resources.OCSRemoteOperation
import com.owncloud.android.lib.resources.assistant.model.TaskTypes
import com.owncloud.android.lib.resources.assistant.v1.model.TaskTypes
import org.apache.commons.httpclient.HttpStatus

class GetTaskTypesRemoteOperation : OCSRemoteOperation<TaskTypes>() {
class GetTaskTypesRemoteOperationV1 : OCSRemoteOperation<TaskTypes>() {
@Suppress("TooGenericExceptionCaught")
override fun run(client: NextcloudClient): RemoteOperationResult<TaskTypes> {
var result: RemoteOperationResult<TaskTypes>
Expand Down Expand Up @@ -52,7 +52,7 @@ class GetTaskTypesRemoteOperation : OCSRemoteOperation<TaskTypes>() {
}

companion object {
private val TAG = GetTaskTypesRemoteOperation::class.java.simpleName
private val TAG = GetTaskTypesRemoteOperationV1::class.java.simpleName
private const val DIRECT_ENDPOINT = "/ocs/v2.php/textprocessing/tasktypes"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 Alper Ozturk <[email protected]>
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.assistant.v1.model

data class TaskList(
var tasks: List<Task>
)

data class Task(
val id: Long,
val type: String?,
val status: Long?,
val userId: String?,
val appId: String?,
val input: String?,
val output: String?,
val identifier: String?,
val completionExpectedAt: String? = null
)

fun TaskList.toV2(): com.owncloud.android.lib.resources.assistant.v2.model.TaskList {
return com.owncloud.android.lib.resources.assistant.v2.model.TaskList(
tasks =
tasks.map { task ->
com.owncloud.android.lib.resources.assistant.v2.model.Task(
id = task.id,
type = task.type,
status = task.status?.toString(),
userId = task.userId,
appId = task.appId,
input =
task.input?.let {
com.owncloud.android.lib.resources.assistant.v2.model.TaskInput(input = it)
},
output =
task.output?.let {
com.owncloud.android.lib.resources.assistant.v2.model.TaskOutput(output = it)
},
completionExpectedAt = task.completionExpectedAt?.toIntOrNull(),
progress = null,
lastUpdated = null,
scheduledAt = null,
endedAt = null
)
}
)
}
Loading

0 comments on commit 638326e

Please sign in to comment.