Skip to content

Commit c472173

Browse files
committed
feat: make enable to send request
1 parent e8ac525 commit c472173

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

src/main/kotlin/cc/unitmesh/devti/connector/custom/CustomConnector.kt

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import cc.unitmesh.devti.connector.CodeCopilot
44
import cc.unitmesh.devti.settings.DevtiSettingsState
55
import kotlinx.serialization.decodeFromString
66
import kotlinx.serialization.json.Json
7+
import okhttp3.OkHttpClient
8+
import okhttp3.Request
79

810

911
class CustomConnector : CodeCopilot {
10-
val devtiSettingsState = DevtiSettingsState.getInstance()
11-
12-
val url = devtiSettingsState?.customEngineServer ?: ""
13-
val key = devtiSettingsState?.customEngineToken ?: ""
14-
var promptConfig: PromptConfig? = null
12+
private val devtiSettingsState = DevtiSettingsState.getInstance()
13+
private val url = devtiSettingsState?.customEngineServer ?: ""
14+
private val key = devtiSettingsState?.customEngineToken ?: ""
15+
private var promptConfig: PromptConfig? = null
16+
private var client = OkHttpClient()
1517

1618
init {
1719
val prompts = devtiSettingsState?.customEnginePrompts
@@ -33,13 +35,30 @@ class CustomConnector : CodeCopilot {
3335
}
3436
}
3537

36-
private fun prompt(instruction: String, input: String): String {
37-
// val retrofit = Retrofit.Builder()
38-
// .baseUrl(url)
39-
// .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
40-
// .build()
38+
fun prompt(instruction: String, input: String): String {
39+
val body = okhttp3.RequestBody.create(
40+
okhttp3.MediaType.parse("application/json; charset=utf-8"),
41+
"""
42+
{
43+
"instruction": "$instruction",
44+
"input": "$input",
45+
}
46+
""".trimIndent()
47+
)
48+
49+
val builder = Request.Builder()
50+
if (key.isNotEmpty()) {
51+
builder.addHeader("Authorization", "Bearer $key")
52+
}
53+
54+
val request = builder
55+
.url(url)
56+
.post(body)
57+
.build()
58+
59+
val response = client.newCall(request).execute()
4160

42-
return ""
61+
return response.body()?.string() ?: ""
4362
}
4463

4564
override fun codeCompleteFor(text: String, className: String): String {

0 commit comments

Comments
 (0)