Skip to content

Commit

Permalink
Add custom autocompleteResultDeserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
mkondratek committed Nov 5, 2024
1 parent 75b6ee3 commit cfa405c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
Empty file.
6 changes: 6 additions & 0 deletions src/main/kotlin/com/sourcegraph/cody/agent/CodyAgent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.intellij.openapi.util.SystemInfoRt
import com.intellij.util.net.HttpConfigurable
import com.intellij.util.system.CpuArch
import com.sourcegraph.cody.agent.protocol.*
import com.sourcegraph.cody.agent.protocol_generated.AutocompleteResult
import com.sourcegraph.cody.agent.protocol_generated.ClientCapabilities
import com.sourcegraph.cody.agent.protocol_generated.ClientInfo
import com.sourcegraph.cody.agent.protocol_generated.ProtocolTypeAdapters
Expand Down Expand Up @@ -293,6 +294,11 @@ private constructor(
// conversions
.registerTypeAdapter(URI::class.java, uriDeserializer)
.registerTypeAdapter(URI::class.java, uriSerializer)
// The agent sends a badly initialized completionEvent
// that is redundant for JetBrains client. The custom deserializer
// simply ignores that field so we get no parsing errors.
.registerTypeAdapter(
AutocompleteResult::class.java, autocompleteResultDeserializer)

ProtocolTypeAdapters.register(gsonBuilder)
// This ensures that by default all enums are always serialized to their string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.sourcegraph.cody.agent.protocol

import com.google.gson.GsonBuilder
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.sourcegraph.cody.agent.protocol_generated.AutocompleteResult
import java.lang.reflect.Type

private val gson = GsonBuilder().create()

val autocompleteResultDeserializer =
JsonDeserializer { jsonElement: JsonElement?, _: Type, _: JsonDeserializationContext ->
val j = jsonElement?.asJsonObject
if (j == null || j.isJsonNull) {
null
} else if (j.isJsonPrimitive) {
j.asString
} else {
val obj = JsonObject()
obj.add("items", j["items"])
gson.fromJson(obj, AutocompleteResult::class.java)
}
}

0 comments on commit cfa405c

Please sign in to comment.