git.iko.soy/opencode-android
on 066cded help sign in
69 lines 2.3 kB history raw
1package soy.iko.opencode.data.model
3import androidx.compose.runtime.Immutable
4import kotlinx.serialization.SerialName
5import kotlinx.serialization.Serializable
6import kotlinx.serialization.json.JsonElement
7import kotlinx.serialization.json.JsonClassDiscriminator
9/**
10 * The `info` object of a message. Discriminated on `role`, which differs from the
11 * global `type` discriminator used by [Part], so this hierarchy overrides it.
12 */
13@OptIn(kotlinx.serialization.ExperimentalSerializationApi::class)
14@Serializable
15@JsonClassDiscriminator("role")
16sealed interface MessageInfo {
17 val id: String
18 val sessionID: String
19 val time: TimeInfo?
22@Immutable
23@Serializable
24@SerialName("user")
25data class UserMessage(
26 override val id: String = "",
27 override val sessionID: String = "",
28 override val time: TimeInfo? = null,
29) : MessageInfo
31@Immutable
32@Serializable
33@SerialName("assistant")
34data class AssistantMessage(
35 override val id: String = "",
36 override val sessionID: String = "",
37 val parentID: String? = null,
38 val providerID: String? = null,
39 val modelID: String? = null,
40 val cost: Double? = null,
41 val tokens: Tokens? = null,
42 // JsonElement (not JsonObject) so a server that sends `error` as a bare string/array — or any
43 // non-object shape — decodes instead of throwing mid-AssistantMessage (the MessageInfo
44 // polymorphic fallback can't catch that, since `role` already matched). Mirrors
45 // SessionError.error. Not currently read by the UI, but kept tolerant for forward-compat.
46 val error: JsonElement? = null,
47 override val time: TimeInfo? = null,
48) : MessageInfo {
49 /** A run is complete once the server stamps a completion time. */
50 val isComplete: Boolean get() = time?.completed != null
53/** Fallback for an unrecognized `role`, so new server roles never crash decoding. */
54@Immutable
55@Serializable
56@SerialName("__unknown")
57data class UnknownMessage(
58 override val id: String = "",
59 override val sessionID: String = "",
60 override val time: TimeInfo? = null,
61) : MessageInfo
63/** A message together with its parts — the shape returned by message endpoints and the prompt response. */
64@Immutable
65@Serializable
66data class MessageWithParts(
67 val info: MessageInfo,
68 val parts: List<Part> = emptyList(),