Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add completion fields #169

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/main/kotlin/ai/devchat/plugin/completion/agent/Agent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class Agent(val scope: CoroutineScope) {
@SerializedName("completion_id") val completionId: String,
@SerializedName("lines") val lines: Int,
@SerializedName("length") val length: Int,
@SerializedName("ide") val ide: String,
@SerializedName("language") val language: String
) {
enum class EventType {
@SerializedName("view") VIEW,
Expand Down Expand Up @@ -143,7 +145,7 @@ class Agent(val scope: CoroutineScope) {
val devChatEndpoint = CONFIG["providers.devchat.api_base"] as? String
val devChatAPIKey = CONFIG["providers.devchat.api_key"] as? String
val endpoint = "$devChatEndpoint/completions"
val endingChunk = "data:[DONE]"
val endingChunk = "[DONE]"
val payload = mapOf(
"model" to ((CONFIG["complete_model"] as? String) ?: defaultCompletionModel),
"prompt" to prompt,
Expand All @@ -161,8 +163,10 @@ class Agent(val scope: CoroutineScope) {
response.body?.charStream()?.buffered()?.use {reader ->
reader.lineSequence().asFlow()
.filter {it.isNotEmpty()}
.takeWhile { it.startsWith("data:") && it != endingChunk}
.map { gson.fromJson(it.drop(5).trim(), CompletionResponseChunk::class.java) }
.takeWhile { it.startsWith("data:") }
.map { it.drop(5).trim() }
.takeWhile { it.uppercase() != endingChunk }
.map { gson.fromJson(it, CompletionResponseChunk::class.java) }
.takeWhile {it != null}
.collect { emit(CodeCompletionChunk(it.id, it.choices[0].text!!)) }
}
Expand All @@ -172,11 +176,7 @@ class Agent(val scope: CoroutineScope) {
private fun toLines(chunks: Flow<CodeCompletionChunk>): Flow<CodeCompletionChunk> = flow {
var ongoingLine = ""
var latestId = ""
chunks.catch {
if (ongoingLine.isNotEmpty()) {
emit(CodeCompletionChunk(latestId, ongoingLine))
}
}.collect { chunk ->
chunks.catch { logger.warn(it) }.collect { chunk ->
var remaining = chunk.text
while (remaining.contains(LINE_SEPARATOR)) {
val parts = remaining.split(LINE_SEPARATOR, limit = 2)
Expand All @@ -187,6 +187,7 @@ class Agent(val scope: CoroutineScope) {
ongoingLine += remaining
latestId = chunk.id
}
if (ongoingLine.isNotEmpty()) emit(CodeCompletionChunk(latestId, ongoingLine))
}

private fun stopAtFirstBrace(chunks: Flow<CodeCompletionChunk>): Flow<CodeCompletionChunk> = flow {
Expand Down Expand Up @@ -264,7 +265,7 @@ class Agent(val scope: CoroutineScope) {
completionRequest.filepath,
completionRequest.text,
completionRequest.position
).createPrompt()
).createPrompt(CONFIG["complete_model"] as? String)

scope.launch {
val chunks = request(prompt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ class ContextBuilder(val filepath: String, val content: String, val offset: Int)
)
}

fun createPrompt(): String {
fun createPrompt(model: String?): String {
val (prefix, suffix) = buildFileContext()
return "<fim_prefix>$filepath\n\n$prefix<fim_suffix>$suffix<fim_middle>"
return if (!model.isNullOrEmpty() && model.contains("deepseek"))
"<|fim▁begin|>$filepath\n\n$prefix<|fim▁hole|>$suffix<|fim▁end|>"
else
"<fim_prefix>$filepath\n\n$prefix<fim_suffix>$suffix<fim_middle>"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.intellij.ui.JBColor
import com.intellij.util.ui.UIUtil
import ai.devchat.plugin.completion.agent.Agent
import ai.devchat.plugin.completion.agent.AgentService
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.codeStyle.CodeStyleManager
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -154,13 +155,16 @@ class InlineCompletionService {
shownInlineCompletion = InlineCompletion(editor, offset, completion, inlays, markups, id, displayAt)

val agentService = service<AgentService>()
val virtualFile = FileDocumentManager.getInstance().getFile(editor.document)
agentService.scope.launch {
agentService.postEvent(
Agent.LogEventRequest(
type = Agent.LogEventRequest.EventType.VIEW,
completionId = completion.id,
lines = textLines.size,
length = text.length,
ide = "intellij",
language = virtualFile?.extension ?: ""
)
)
}
Expand Down Expand Up @@ -217,13 +221,16 @@ class InlineCompletionService {
currentCompletion.inlays.forEach(Disposer::dispose)
}
val agentService = service<AgentService>()
val virtualFile = FileDocumentManager.getInstance().getFile(currentCompletion.editor.document)
agentService.scope.launch {
agentService.postEvent(
Agent.LogEventRequest(
type = Agent.LogEventRequest.EventType.SELECT,
completionId = currentCompletion.completion.id,
lines = text.lines().size,
length = text.length
length = text.length,
ide = "intellij",
language = virtualFile?.extension ?: ""
)
)
}
Expand Down
Loading