Skip to content

Commit

Permalink
Handle OpenAI Error event
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscodr committed Oct 10, 2024
1 parent 9c0f587 commit 914c122
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 914c122

Please sign in to comment.