Изменение выносит типы контента в :content-api и разделяет потоки событий.
This commit is contained in:
@@ -17,6 +17,7 @@ kotlin {
|
||||
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
api(project(":content-api"))
|
||||
api(libs.kotlinx.coroutines.core)
|
||||
api(libs.kotlinx.serialization.core)
|
||||
api(libs.kotlinx.serialization.json)
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
package pw.binom.agentik.journal
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
/**
|
||||
* Часть контента сообщения на уровне хранилища. Намеренно НЕ зависит от
|
||||
* `pw.binom.agentik.proto.Content` — маппинг `:proto.Content ↔ Content` живёт
|
||||
* в `Mapping.kt` storage impl'ов.
|
||||
*/
|
||||
@Serializable
|
||||
sealed interface Content {
|
||||
@Serializable
|
||||
@SerialName("text")
|
||||
data class Text(val body: String) : Content
|
||||
|
||||
@Serializable
|
||||
@SerialName("image")
|
||||
data class Image(val data: ByteArray, val mime: String) : Content {
|
||||
override fun equals(other: Any?): Boolean =
|
||||
this === other || (other is Image && mime == other.mime && data.contentEquals(other.data))
|
||||
override fun hashCode(): Int = 31 * mime.hashCode() + data.contentHashCode()
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package pw.binom.agentik.journal
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
|
||||
@Serializable
|
||||
data class MessageContext(
|
||||
val origin: MessageOrigin,
|
||||
val sourceId: String? = null,
|
||||
val description: String? = null,
|
||||
val metadata: JsonElement? = null,
|
||||
)
|
||||
@@ -1,20 +0,0 @@
|
||||
package pw.binom.agentik.journal
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
/**
|
||||
* Контекст инициации хода (кто/что и почему). Дубликат типа из `:proto` —
|
||||
* живёт здесь чтобы не тащить `:proto` в слой хранения данных.
|
||||
*/
|
||||
@Serializable
|
||||
enum class MessageOrigin {
|
||||
@SerialName("user")
|
||||
USER,
|
||||
|
||||
@SerialName("system")
|
||||
SYSTEM,
|
||||
|
||||
@SerialName("event")
|
||||
EVENT,
|
||||
}
|
||||
@@ -2,6 +2,9 @@ package pw.binom.agentik.journal
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import pw.binom.agentik.content.Content
|
||||
import pw.binom.agentik.content.MessageContext
|
||||
import pw.binom.agentik.content.TurnTokens
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
@@ -36,6 +39,12 @@ sealed interface MessageRecord {
|
||||
override val content: List<Content>,
|
||||
override val createdAt: Instant,
|
||||
val tokens: TurnTokens? = null,
|
||||
/**
|
||||
* Текст размышлений модели (chain-of-thought / reasoning), если провайдер
|
||||
* его отдаёт. Опционально: null, если размышлений не было или провайдер
|
||||
* их не раскрывает.
|
||||
*/
|
||||
val reasoning: String? = null,
|
||||
) : Body
|
||||
|
||||
@Serializable
|
||||
|
||||
@@ -4,6 +4,9 @@ import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.builtins.ListSerializer
|
||||
import kotlinx.serialization.json.Json
|
||||
import pw.binom.agentik.content.Content
|
||||
import pw.binom.agentik.content.MessageContext
|
||||
import pw.binom.agentik.content.TurnTokens
|
||||
|
||||
private val bodyJson = Json {
|
||||
ignoreUnknownKeys = true
|
||||
@@ -17,15 +20,17 @@ data class MessageBodyPayload(
|
||||
@SerialName("context")
|
||||
val context: MessageContext? = null,
|
||||
val tokens: TurnTokens? = null,
|
||||
val reasoning: String? = null,
|
||||
)
|
||||
|
||||
fun encodeBodyPayload(
|
||||
content: List<Content>,
|
||||
context: MessageContext? = null,
|
||||
tokens: TurnTokens? = null,
|
||||
reasoning: String? = null,
|
||||
): String = bodyJson.encodeToString(
|
||||
MessageBodyPayload.serializer(),
|
||||
MessageBodyPayload(content = content, context = context, tokens = tokens),
|
||||
MessageBodyPayload(content = content, context = context, tokens = tokens, reasoning = reasoning),
|
||||
)
|
||||
|
||||
fun decodeBodyPayload(json: String): BodyDecoded = readPayload(json)
|
||||
@@ -34,14 +39,15 @@ data class BodyDecoded(
|
||||
val content: List<Content>,
|
||||
val context: MessageContext?,
|
||||
val tokens: TurnTokens? = null,
|
||||
val reasoning: String? = null,
|
||||
)
|
||||
|
||||
private fun readPayload(json: String): BodyDecoded {
|
||||
return try {
|
||||
val p = bodyJson.decodeFromString(MessageBodyPayload.serializer(), json)
|
||||
BodyDecoded(p.content, p.context, p.tokens)
|
||||
BodyDecoded(p.content, p.context, p.tokens, p.reasoning)
|
||||
} catch (e: kotlinx.serialization.SerializationException) {
|
||||
val arr = bodyJson.decodeFromString(ListSerializer(Content.serializer()), json)
|
||||
BodyDecoded(arr, null, null)
|
||||
BodyDecoded(arr, null, null, null)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package pw.binom.agentik.journal
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
/**
|
||||
* Token usage одного assistant turn'а.
|
||||
*/
|
||||
@Serializable
|
||||
data class TurnTokens(
|
||||
val input: Int,
|
||||
val output: Int,
|
||||
) {
|
||||
val total: Int get() = input + output
|
||||
init {
|
||||
require(input >= 0) { "input tokens must be non-negative, got $input" }
|
||||
require(output >= 0) { "output tokens must be non-negative, got $output" }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user