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

Handle OpenAI Error server event accordingly #795

Merged
merged 1 commit into from
Oct 10, 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
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)
franciscodr marked this conversation as resolved.
Show resolved Hide resolved
}
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
Loading