Skip to content

Commit 4bd6840

Browse files
committed
feat(custom-agent): add support for custom request format and improve serialization #%1
The `CustomAgentExecutor` class has been updated to support a custom request format and to use Kotlinx serialization for improved performance and readability. The request body is now serialized using `Json.encodeToString`, and the `CustomRequest` class has been added to represent the custom request structure. This change also includes the import of necessary Kotlinx serialization and OkHttp libraries.
1 parent 59f54c9 commit 4bd6840

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/main/kotlin/cc/unitmesh/devti/counit/CustomAgentExecutor.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,28 @@ package cc.unitmesh.devti.counit
33
import cc.unitmesh.devti.counit.configurable.customAgentSetting
44
import cc.unitmesh.devti.counit.model.AuthType
55
import cc.unitmesh.devti.counit.model.CustomAgentConfig
6+
import cc.unitmesh.devti.llms.custom.CustomRequest
7+
import cc.unitmesh.devti.llms.custom.Message
68
import com.intellij.openapi.components.Service
79
import com.intellij.openapi.project.Project
10+
import kotlinx.serialization.encodeToString
11+
import kotlinx.serialization.json.Json
812
import okhttp3.MediaType.Companion.toMediaTypeOrNull
913
import okhttp3.OkHttpClient
1014
import okhttp3.Request
11-
import okhttp3.RequestBody
15+
import okhttp3.RequestBody.Companion.toRequestBody
1216

1317
@Service(Service.Level.PROJECT)
1418
class CustomAgentExecutor(val project: Project) {
1519
private var client = OkHttpClient()
1620

1721
fun execute(input: String, agent: CustomAgentConfig): String? {
1822
val serverAddress = project.customAgentSetting.serverAddress ?: return null
19-
val body = RequestBody.create("application/json; charset=utf-8".toMediaTypeOrNull(), input)
23+
24+
val customRequest = CustomRequest(listOf(Message("user", input)))
25+
val request = Json.encodeToString<CustomRequest>(customRequest)
26+
27+
val body = request.toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
2028
val builder = Request.Builder()
2129

2230
val auth = agent.auth

src/main/kotlin/cc/unitmesh/devti/counit/model/StandardTextChunk.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ import kotlinx.serialization.Serializable
44

55
typealias Embedding = List<Float>
66

7+
/**
8+
* `StandardTextChunk` is a data class that represents a chunk of text within a document. It includes the following fields:
9+
*
10+
* - `id`: A string that uniquely identifies the text chunk.
11+
* - `text`: The actual text content of the chunk.
12+
* - `embedding`: An `Embedding` object that contains the vector representation of the text chunk.
13+
* - `score`: A floating-point number that represents the score or importance of the text chunk.
14+
*/
715
@Serializable
816
data class StandardTextChunk(
917
val id: String?,

0 commit comments

Comments
 (0)