diff --git a/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/MetricManagement.kt b/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/MetricManagement.kt index 91d8e948a..32f85fd43 100644 --- a/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/MetricManagement.kt +++ b/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/MetricManagement.kt @@ -102,6 +102,7 @@ suspend fun RunDelta.addMetrics(metric: Metric): RunDelta { is RunDelta.RunStepDelta, is RunDelta.RunSubmitToolOutputs, is RunDelta.ThreadCreated, + is RunDelta.Error, is RunDelta.Unknown -> {} } return this diff --git a/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/RunDelta.kt b/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/RunDelta.kt index fec396e02..9212bdc19 100644 --- a/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/RunDelta.kt +++ b/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/RunDelta.kt @@ -134,15 +134,22 @@ sealed interface RunDelta { /** [RunDeltaEvent.thread_message_incomplete] */ @JvmInline @Serializable value class MessageIncomplete(val message: MessageObject) : RunDelta + /** [RunDeltaEvent.error] */ + @JvmInline + @Serializable + value class Error(val error: com.xebia.functional.openai.generated.model.Error) : RunDelta + @JvmInline @Serializable value class Unknown(val event: ServerSentEvent) : RunDelta companion object { + private val typeExcludedCharactersRegex = "[._]".toRegex() + fun fromServerSentEvent(serverEvent: ServerSentEvent): RunDelta { val data = serverEvent.data ?: error("Expected data in ServerSentEvent for RunDelta") val type = serverEvent.event ?: error("Expected event in ServerSentEvent for RunDelta") val event = - RunDeltaEvent.values().find { - type.replace(".", "").replace("_", "").equals(it.name, ignoreCase = true) + RunDeltaEvent.entries.find { + type.replace(typeExcludedCharactersRegex, "").equals(it.name, ignoreCase = true) } val json = Config.DEFAULT.json return when (event) { @@ -190,7 +197,13 @@ sealed interface RunDelta { MessageCompleted(json.decodeFromJsonElement(MessageObject.serializer(), data)) RunDeltaEvent.ThreadMessageIncomplete -> MessageIncomplete(json.decodeFromJsonElement(MessageObject.serializer(), data)) - RunDeltaEvent.Error -> Unknown(serverEvent) + RunDeltaEvent.Error -> + Error( + json.decodeFromJsonElement( + com.xebia.functional.openai.generated.model.Error.serializer(), + data + ) + ) null -> Unknown(serverEvent) } }