From 548ae88d52beebded974d46690ba908a87e16082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=AD=A3=E4=BA=AE?= Date: Wed, 9 Apr 2025 08:37:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(devti):=20=E4=BC=98=E5=8C=96=20SSE=20?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=A7=A3=E6=9E=90=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 dataBuilder 用于合并多行 data 内容 - 调整逻辑以处理非标准格式的 SSE 数据 --- .../devti/llms/custom/ResponseBodyCallback.kt | 90 +++++++++---------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/core/src/main/kotlin/cc/unitmesh/devti/llms/custom/ResponseBodyCallback.kt b/core/src/main/kotlin/cc/unitmesh/devti/llms/custom/ResponseBodyCallback.kt index ead98b8620..4e1e730e5e 100644 --- a/core/src/main/kotlin/cc/unitmesh/devti/llms/custom/ResponseBodyCallback.kt +++ b/core/src/main/kotlin/cc/unitmesh/devti/llms/custom/ResponseBodyCallback.kt @@ -59,62 +59,62 @@ class ResponseBodyCallback(private val emitter: FlowableEmitter, private va reader = BufferedReader(InputStreamReader(inputStream, StandardCharsets.UTF_8)) var line: String? = null var sse: SSE? = null - while (!emitter.isCancelled && reader.readLine().also { line = it } != null) { - sse = when { - line!!.startsWith("data:") -> { - val data = line!!.substring(5).trim { it <= ' ' } - SSE(data) - } - line == "" && sse != null -> { - if (sse.isDone) { - if (emitDone) { - emitter.onNext(sse) - } - break + //用于兼容非标准格式的SSE:一条数据拆成了多行,每行都以data:开头 + val dataBuilder = StringBuilder() // 用于合并多行 data 内容 + while (!emitter.isCancelled && reader.readLine().also { line = it } != null) { + if (line!!.startsWith("data:")) { + val dataPart = line!!.substring(5).trim { it <= ' ' } + dataBuilder.append(dataPart) // 追加 data 内容 + } else if (line == "" && dataBuilder.isNotEmpty()) { + // 遇到空行且有累积的 data 内容,创建 SSE 对象 + val data = dataBuilder.toString() + sse = SSE(data) + if (sse.isDone) { + if (emitDone) { + emitter.onNext(sse) } - emitter.onNext(sse) - null + break } - // starts with event: - line!!.startsWith("event:") -> { - // https://github.com/sysid/sse-starlette/issues/16 - val eventName = line!!.substring(6).trim { it <= ' ' } - if (eventName == "ping") { - // skip ping event and data - emitter.onNext(sse ?: SSE("")) - emitter.onNext(sse ?: SSE("")) + emitter.onNext(sse) + dataBuilder.clear() // 清空 data 内容 + sse = null + } else { + // 其他情况,按照原逻辑处理 + sse = when { + // starts with event: + line!!.startsWith("event:") -> { + // https://github.com/sysid/sse-starlette/issues/16 + val eventName = line!!.substring(6).trim { it <= ' ' } + if (eventName == "ping") { + // skip ping event and data + emitter.onNext(sse ?: SSE("")) + emitter.onNext(sse ?: SSE("")) + } + null } - null - } - - // skip `: ping` comments for: https://github.com/sysid/sse-starlette/issues/16 - line!!.startsWith(": ping") -> { - null - } - - else -> { - when { - // sometimes the server maybe returns empty line - line == "" -> { - null - } + // skip `: ping` comments for: https://github.com/sysid/sse-starlette/issues/16 + line!!.startsWith(": ping") -> { + null + } - // : is comment - // https://html.spec.whatwg.org/multipage/server-sent-events.html#parsing-an-event-stream - line!!.startsWith(":") -> { - null - } + // sometimes the server maybe returns empty line + line == "" -> { + null + } - else -> { - throw AutoDevHttpException("Invalid sse format! '$line'", response.code) - } + // : is comment + // https://html.spec.whatwg.org/multipage/server-sent-events.html#parsing-an-event-stream + line!!.startsWith(":") -> { + null + } + else -> { + throw AutoDevHttpException("Invalid sse format! '$line'", response.code) } } } } - emitter.onComplete() } catch (t: Throwable) { logger().error("Error while reading SSE", t) From c90145438ce21e2531e7c37bb477527db7f0c146 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Sat, 26 Apr 2025 14:52:49 +0800 Subject: [PATCH 2/2] feat(highlight): add syntax highlighting for Shire language Add DevInHighlightingAnnotator to provide basic identifier highlighting for Shire language in DevIn plugin. This enhances code readability in the IDE. --- .../highlight/DevInHighlightingAnnotator.kt | 21 +++++++++++++++++++ .../resources/cc.unitmesh.devti.language.xml | 2 ++ 2 files changed, 23 insertions(+) create mode 100644 exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/highlight/DevInHighlightingAnnotator.kt diff --git a/exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/highlight/DevInHighlightingAnnotator.kt b/exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/highlight/DevInHighlightingAnnotator.kt new file mode 100644 index 0000000000..311de242cb --- /dev/null +++ b/exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/highlight/DevInHighlightingAnnotator.kt @@ -0,0 +1,21 @@ +package cc.unitmesh.devti.language.highlight + +import cc.unitmesh.devti.language.psi.DevInTypes +import com.intellij.lang.annotation.AnnotationHolder +import com.intellij.lang.annotation.Annotator +import com.intellij.lang.annotation.HighlightSeverity +import com.intellij.openapi.editor.DefaultLanguageHighlighterColors +import com.intellij.psi.PsiElement +import com.intellij.psi.util.PsiUtilCore + +class DevInHighlightingAnnotator : Annotator { + override fun annotate(element: PsiElement, holder: AnnotationHolder) { + when (PsiUtilCore.getElementType(element)) { + DevInTypes.IDENTIFIER -> { + holder.newSilentAnnotation(HighlightSeverity.INFORMATION) + .textAttributes(DefaultLanguageHighlighterColors.IDENTIFIER) + .create() + } + } + } +} diff --git a/exts/devins-lang/src/main/resources/cc.unitmesh.devti.language.xml b/exts/devins-lang/src/main/resources/cc.unitmesh.devti.language.xml index 18aaba9ad0..d8971f69b7 100644 --- a/exts/devins-lang/src/main/resources/cc.unitmesh.devti.language.xml +++ b/exts/devins-lang/src/main/resources/cc.unitmesh.devti.language.xml @@ -24,6 +24,8 @@ + +