merge: B-M4e Android usage/storage dashboards + settings

# Conflicts:
#	android/app/src/main/kotlin/app/hapi/companion/Navigation.kt
This commit is contained in:
weishu
2026-08-18 10:45:42 +08:00
27 changed files with 3033 additions and 2 deletions
@@ -0,0 +1,76 @@
package app.hapi.protocol.wire
import kotlinx.serialization.Serializable
/**
* Wire types of the owner-only usage/storage dashboards
* (`docs/api/client-contract/rest.md#usage--storage-owner-only`;
* `UsageSummaryResponse` / `SqliteStorageUsageResponse` in
* `shared/src/apiTypes.ts`). Both endpoints answer `403` unless the caller's
* namespace is `default` (hub owner).
*
* Token counts are [Long]: `range=all` totals over a long-lived hub can pass
* 2^31.
*/
/** One aggregation bucket (a day / an agent / a model), keyed by [key]. */
@Serializable
data class UsageSummaryBucket(
/** Day buckets: `YYYY-MM-DD` in the requested timeZone; else agent/model id. */
val key: String,
val inputTokens: Long,
val outputTokens: Long,
val cacheReadTokens: Long,
val cacheCreationTokens: Long,
val totalTokens: Long,
/** Input that missed the cache: `inputTokens - cacheReadTokens`. */
val uncachedTokens: Long,
val requests: Long,
)
/** Epoch-ms window the summary covers; [from] is null for `range=all`. */
@Serializable
data class UsageSummaryRange(
val from: Long? = null,
val to: Long? = null,
)
/** Grand totals over the window (bucket counters + distinct [sessions]). */
@Serializable
data class UsageSummaryTotals(
val inputTokens: Long,
val outputTokens: Long,
val cacheReadTokens: Long,
val cacheCreationTokens: Long,
val totalTokens: Long,
val uncachedTokens: Long,
val requests: Long,
val sessions: Long,
)
/**
* `GET /api/usage/summary?range=7d|30d|all&timeZone=<IANA>`.
*
* [daily] is ascending by key and **sparse** — only days with usage appear.
* [byAgent]/[byModel] are sorted by descending [UsageSummaryBucket.totalTokens]
* (the web dashboard shows the top 8).
*/
@Serializable
data class UsageSummaryResponse(
val range: UsageSummaryRange,
val totals: UsageSummaryTotals,
val daily: List<UsageSummaryBucket>,
val byAgent: List<UsageSummaryBucket>,
val byModel: List<UsageSummaryBucket>,
val updatedAt: Long,
)
/** `GET /api/storage/sqlite` — hub database file sizes in bytes. */
@Serializable
data class SqliteStorageUsageResponse(
val path: String,
val databaseBytes: Long,
val walBytes: Long,
val shmBytes: Long,
val totalBytes: Long,
)
@@ -0,0 +1,123 @@
package app.hapi.protocol.wire
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue
/**
* Decoding of the owner-only dashboard payloads (`GET /api/usage/summary`,
* `GET /api/storage/sqlite`) — shaped like `hub/src/sync/usageService.ts`
* output: sparse ascending `daily`, desc-sorted `byAgent`/`byModel`,
* `range.from = null` on `range=all`, plus the [HapiJson] unknown-key rule.
*/
class UsageDecodingTest {
@Test
fun `decodes a realistic usage summary`() {
val summary = HapiJson.decodeFromString(
UsageSummaryResponse.serializer(),
"""
{
"range": {"from": 1754995200000, "to": 1755600000000},
"totals": {
"inputTokens": 12400000,
"outputTokens": 310000,
"cacheReadTokens": 9800000,
"cacheCreationTokens": 1100000,
"totalTokens": 12710000,
"uncachedTokens": 2600000,
"requests": 1842,
"sessions": 23
},
"daily": [
{"key": "2026-08-12", "inputTokens": 1200000, "outputTokens": 40000,
"cacheReadTokens": 900000, "cacheCreationTokens": 100000,
"totalTokens": 1240000, "uncachedTokens": 300000, "requests": 210},
{"key": "2026-08-16", "inputTokens": 5100000, "outputTokens": 120000,
"cacheReadTokens": 4000000, "cacheCreationTokens": 500000,
"totalTokens": 5220000, "uncachedTokens": 1100000, "requests": 720}
],
"byAgent": [
{"key": "claude", "inputTokens": 8600000, "outputTokens": 220000,
"cacheReadTokens": 7000000, "cacheCreationTokens": 800000,
"totalTokens": 8820000, "uncachedTokens": 1600000, "requests": 1300}
],
"byModel": [
{"key": "claude-sonnet-4-5", "inputTokens": 8600000, "outputTokens": 220000,
"cacheReadTokens": 7000000, "cacheCreationTokens": 800000,
"totalTokens": 8820000, "uncachedTokens": 1600000, "requests": 1300,
"futureField": {"nested": true}}
],
"updatedAt": 1755600000000,
"unknownTopLevel": "ignored"
}
""".trimIndent(),
)
assertEquals(1754995200000, summary.range.from)
assertEquals(1755600000000, summary.range.to)
assertEquals(12710000, summary.totals.totalTokens)
assertEquals(2600000, summary.totals.uncachedTokens)
assertEquals(23, summary.totals.sessions)
assertEquals(listOf("2026-08-12", "2026-08-16"), summary.daily.map { it.key })
assertEquals(720, summary.daily[1].requests)
assertEquals("claude", summary.byAgent.single().key)
assertEquals(8820000, summary.byModel.single().totalTokens)
assertEquals(1755600000000, summary.updatedAt)
}
@Test
fun `range=all carries a null from and token counts may exceed Int range`() {
val summary = HapiJson.decodeFromString(
UsageSummaryResponse.serializer(),
"""
{
"range": {"from": null, "to": 1755600000000},
"totals": {
"inputTokens": 5100000000,
"outputTokens": 200000000,
"cacheReadTokens": 4000000000,
"cacheCreationTokens": 300000000,
"totalTokens": 5300000000,
"uncachedTokens": 1100000000,
"requests": 250000,
"sessions": 4100
},
"daily": [],
"byAgent": [],
"byModel": [],
"updatedAt": 1755600000000
}
""".trimIndent(),
)
assertNull(summary.range.from)
assertEquals(5_300_000_000, summary.totals.totalTokens)
assertTrue(summary.totals.totalTokens > Int.MAX_VALUE.toLong())
assertTrue(summary.daily.isEmpty())
}
@Test
fun `decodes sqlite storage usage`() {
val storage = HapiJson.decodeFromString(
SqliteStorageUsageResponse.serializer(),
"""
{
"path": "/home/hapi/.hapi/hapi.db",
"databaseBytes": 84930560,
"walBytes": 4194304,
"shmBytes": 32768,
"totalBytes": 89157632,
"futureField": 1
}
""".trimIndent(),
)
assertEquals("/home/hapi/.hapi/hapi.db", storage.path)
assertEquals(84_930_560, storage.databaseBytes)
assertEquals(4_194_304, storage.walBytes)
assertEquals(32_768, storage.shmBytes)
assertEquals(89_157_632, storage.totalBytes)
}
}