All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.aallam.openai.client.internal.api.ThreadsApi.kt Maven / Gradle / Ivy

The newest version!
package com.aallam.openai.client.internal.api

import com.aallam.openai.api.BetaOpenAI
import com.aallam.openai.api.core.DeleteResponse
import com.aallam.openai.api.core.RequestOptions
import com.aallam.openai.api.exception.OpenAIAPIException
import com.aallam.openai.api.thread.Thread
import com.aallam.openai.api.thread.ThreadId
import com.aallam.openai.api.thread.ThreadRequest
import com.aallam.openai.client.Threads
import com.aallam.openai.client.internal.extension.beta
import com.aallam.openai.client.internal.extension.requestOptions
import com.aallam.openai.client.internal.http.HttpRequester
import com.aallam.openai.client.internal.http.perform
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import kotlinx.serialization.json.putJsonObject

internal class ThreadsApi(val requester: HttpRequester) : Threads {

    @BetaOpenAI
    override suspend fun thread(request: ThreadRequest?, requestOptions: RequestOptions?): Thread {
        return requester.perform {
            it.post {
                url(path = ApiPath.Threads)
                request?.let { req ->
                    setBody(req)
                    contentType(ContentType.Application.Json)
                }
                beta("assistants", 2)
                requestOptions(requestOptions)
            }.body()
        }
    }

    @BetaOpenAI
    override suspend fun thread(id: ThreadId, requestOptions: RequestOptions?): Thread? {
        try {
            return requester.perform {
                it.get {
                    url(path = "${ApiPath.Threads}/${id.id}")
                    beta("assistants", 2)
                    requestOptions(requestOptions)
                }
            }.body()
        } catch (e: OpenAIAPIException) {
            if (e.statusCode == HttpStatusCode.NotFound.value) return null
            throw e
        }
    }

    @BetaOpenAI
    override suspend fun thread(id: ThreadId, metadata: Map, requestOptions: RequestOptions?): Thread {
        val request = buildJsonObject {
            putJsonObject("metadata") {
                metadata.forEach { (key, value) ->
                    put(key, value)
                }
            }
        }
        return requester.perform {
            it.post {
                url(path = "${ApiPath.Threads}/${id.id}")
                setBody(request)
                contentType(ContentType.Application.Json)
                beta("assistants", 2)
                requestOptions(requestOptions)
            }.body()
        }
    }

    @BetaOpenAI
    override suspend fun delete(id: ThreadId, requestOptions: RequestOptions?): Boolean {
        val response = requester.perform {
            it.delete {
                url(path = "${ApiPath.Threads}/${id.id}")
                beta("assistants", 2)
                requestOptions(requestOptions)
            }
        }
        return when (response.status) {
            HttpStatusCode.NotFound -> false
            else -> response.body().deleted
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy