Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f946186ef5 | |||
| fb963bfb6b | |||
| 818f022ba3 | |||
| 0d1be42919 | |||
| 2d6cf89c52 | |||
| 8f85612665 | |||
| aef5083801 | |||
| 499db812ef | |||
| d6afc05c20 | |||
| acc7237e51 | |||
| bd65c29b48 | |||
| 2cc1d923b3 | |||
| 8f8f7f1020 | |||
| 3f260f3ac8 | |||
| d74af621d8 | |||
| 15f3952eba | |||
| a0b1209457 | |||
| a05e260457 | |||
| 7e66baf9e3 | |||
| 1134e32ea2 | |||
| e2f0e434d1 | |||
| 1f85cde1b8 | |||
| 8bb24dab3c | |||
| 7358175499 | |||
| 2d9ad526bb | |||
| dd7aec8df1 | |||
| bf2649a856 | |||
| 4ad59d5f5d |
+37
@@ -0,0 +1,37 @@
|
||||
Status of message-log-api migration:
|
||||
|
||||
DONE:
|
||||
1. Created :message-log-api module with build.gradle.kts (KMP, jvm + linuxX64 + mingwX64, kotlinx-serialization plugin).
|
||||
2. Created 5 files in message-log-api/src/commonMain/kotlin/pw/binom/agentik/messageLog/:
|
||||
- Content.kt (sealed: Text, Image)
|
||||
- MessageRecord.kt (sealed: UserMessage, AssistantMessage, ToolCall, ToolResult, Error; plus TurnTokens)
|
||||
- MessageStore.kt (interface, TokenStats, MessageEvent)
|
||||
- MessageContext.kt (MessageOrigin enum + MessageContext data class)
|
||||
- Payload.kt (bodyJson, MessageBodyPayload, encode/decodeBodyPayload, BodyDecoded)
|
||||
3. Added include(":message-log-api") in settings.gradle.kts (right after message-store-api).
|
||||
4. Added api(project(":message-log-api")) to working-memory-api/build.gradle.kts.
|
||||
5. Wrote /tmp/rename_imports.py with 12 FQN renames (MessageRecord, MessageStore, Content, MessageContext, MessageOrigin, TurnTokens, TokenStats, MessageEvent, MessageBodyPayload, BodyDecoded, encodeBodyPayload, decodeBodyPayload).
|
||||
6. Wrote /tmp/run_rename.sh that runs the python script.
|
||||
|
||||
PENDING:
|
||||
- Run /tmp/run_rename.sh to apply the renames across all consumer files.
|
||||
- Delete the 5 originals from message-store-api/src/commonMain/kotlin/pw/binom/agentik/messageStore/.
|
||||
- Add api(project(":message-log-api")) to storage-inmemory/sqlite/ksqlite gradle files.
|
||||
- Verify build compiles (run standalone tests).
|
||||
|
||||
Files that need import updates (per search):
|
||||
- storage-inmemory/src/commonMain/.../InMemoryMessageStore.kt
|
||||
- storage-inmemory/src/commonTest/.../InMemoryMessageStoreTest.kt
|
||||
- storage-sqlite/src/jvmMain/.../SqliteMessageStore.kt
|
||||
- storage-ksqlite/src/commonMain/.../KsqliteMessageStore.kt
|
||||
- storage-ksqlite/src/commonMain/.../MessageCodecs.kt
|
||||
- storage-ksqlite/src/commonTest/.../KsqliteMessageStoreTest.kt
|
||||
- standalone/src/jvmMain/.../ToolDispatcher.kt
|
||||
- standalone/src/jvmMain/.../ConversationLoop.kt
|
||||
- standalone/src/jvmTest/.../ChatAgentTest.kt
|
||||
- standalone/src/jvmTest/.../persistence/PersistenceTest.kt
|
||||
- standalone/src/jvmTest/.../persistence/SqliteStoresMigrationTest.kt
|
||||
- standalone/src/jvmTest/.../persistence/TokenStatsTest.kt
|
||||
|
||||
Tool issue: run_command keeps failing JSON validation (safe_to_run field required).
|
||||
Workaround needed before continuing the migration.
|
||||
@@ -77,7 +77,7 @@ budget exhaustion, registry filter, parallel dispatch.
|
||||
## Чего здесь НЕТ
|
||||
|
||||
- Никакого конкретного LLM. Dispatcher вызывает tools, не LLM.
|
||||
- Никакого persistent storage. Опирается на контракт `WorkingMemoryStore`
|
||||
- Никакого persistent storage. Опирается на контракт `ContextStore`
|
||||
(см. `:storage-core`).
|
||||
|
||||
## Текущий статус
|
||||
|
||||
@@ -21,8 +21,9 @@ kotlin {
|
||||
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
// :storage-core — для StorageBundle в ToolsetContext (commit 5+)
|
||||
api(project(":storage-core"))
|
||||
api(project(":journal-api"))
|
||||
api(project(":reflection-api"))
|
||||
api(project(":context-api"))
|
||||
|
||||
// litert-kmp: LiteTool интерфейс (sync describe/invoke)
|
||||
api(libs.litert.api)
|
||||
|
||||
@@ -4,7 +4,7 @@ package pw.binom.agentik.toolsets
|
||||
* Контекст, который тулсеты получают при активации.
|
||||
*
|
||||
* В commit 4 — минимальный: логгер. Позже (commit 5+, если понадобится) сюда
|
||||
* добавятся `StorageBundle`, `SkillStore` и пр., чтобы тулы внутри тулсета
|
||||
* добавятся `SkillStore` и пр., чтобы тулы внутри тулсета
|
||||
* могли читать/писать сообщения и память.
|
||||
*
|
||||
* Если конкретному тулсету нужно больше, чем [Logger], он может объявить свой
|
||||
|
||||
@@ -2,12 +2,12 @@ package pw.binom.agentik.cli
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
import pw.binom.agentik.client.agentikHttpClient
|
||||
import pw.binom.agentik.client.applyAgentikDefaults
|
||||
|
||||
/**
|
||||
* HTTP-клиент CLI: движок CIO + конфигурация agentik.
|
||||
*
|
||||
* Движок живёт здесь, а не в `:client`: библиотека не выбирает транспорт за
|
||||
* Движок выбирается здесь, а не в `:client`: библиотека не выбирает транспорт за
|
||||
* потребителя. Таргеты `:agentik-cli` (jvm + linuxX64/macosX64/macosArm64/mingwX64)
|
||||
* покрываются CIO.
|
||||
*
|
||||
@@ -18,6 +18,7 @@ import pw.binom.agentik.client.agentikHttpClient
|
||||
* [token] = `null` — авторизация выключена.
|
||||
*/
|
||||
internal fun defaultCliHttpClient(token: String? = null): HttpClient =
|
||||
agentikHttpClient(engineFactory = CIO, token = token) {
|
||||
HttpClient(CIO) {
|
||||
applyAgentikDefaults(token)
|
||||
engine { requestTimeout = 0 }
|
||||
}
|
||||
@@ -9,8 +9,8 @@ import kotlinx.coroutines.launch
|
||||
import pw.binom.agentik.cli.AgentikSubcommand
|
||||
import pw.binom.agentik.cli.defaultCliHttpClient
|
||||
import pw.binom.agentik.client.AgentikAgent
|
||||
import pw.binom.agentik.outbox.Event
|
||||
import pw.binom.agentik.proto.Content
|
||||
import pw.binom.agentik.proto.Event
|
||||
import kotlin.time.Instant
|
||||
|
||||
class SendSubcommand : AgentikSubcommand("send", "Отправить user-ход и стримить ответ") {
|
||||
|
||||
@@ -46,7 +46,11 @@ internal class TuiBackend(
|
||||
state.postSystem("подключено к ${state.config.server}")
|
||||
scope.launch {
|
||||
try {
|
||||
agent.events(Instant.DISTANT_PAST).collect { /* sidebar refresh */ }
|
||||
// agent.outbox.agentEvents(after) возвращает Flow<CommonEvent.Agent>;
|
||||
// распаковываем .event для получения AgentEvent (раньше был
|
||||
// отдельный метод agent.events(), теперь упразднён — события
|
||||
// живут в outbox-сущности).
|
||||
agent.outbox.agentEvents(Instant.DISTANT_PAST).collect { /* sidebar refresh */ }
|
||||
} catch (_: kotlinx.coroutines.CancellationException) {
|
||||
// штатная отмена при закрытии UI
|
||||
} catch (e: Exception) {
|
||||
|
||||
@@ -3,8 +3,9 @@ package pw.binom.agentik.tui
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.emptyFlow
|
||||
import pw.binom.agentik.journal.JournalStore
|
||||
import pw.binom.agentik.outbox.OutboxStore
|
||||
import pw.binom.agentik.proto.Agent
|
||||
import pw.binom.agentik.proto.AgentEvent
|
||||
import pw.binom.agentik.proto.Content
|
||||
import pw.binom.agentik.proto.Conversation
|
||||
import pw.binom.agentik.proto.Event
|
||||
@@ -25,6 +26,16 @@ internal class FakeAgent(
|
||||
private set
|
||||
val conversations = mutableListOf<FakeConversation>()
|
||||
|
||||
// Storage handles не используются тестами TuiBackend — тесты проверяют
|
||||
// маршрутизацию Conversation.events в UI state. Outbox stub-ы возвращают
|
||||
// emptyFlow, journal — error-on-access (никто не должен его трогать).
|
||||
override val journal: JournalStore = error("journal not used in TuiBackend tests")
|
||||
override val outbox: OutboxStore = object : OutboxStore {
|
||||
override fun events(after: Instant?) = emptyFlow<pw.binom.agentik.proto.CommonEvent>()
|
||||
override suspend fun earliestEventDate(): Instant = Instant.DISTANT_PAST
|
||||
override fun close() {}
|
||||
}
|
||||
|
||||
override fun createConversation(temp: Boolean): Conversation {
|
||||
createCount++
|
||||
val c = conversationFactory()
|
||||
@@ -40,8 +51,6 @@ internal class FakeAgent(
|
||||
|
||||
override suspend fun getConversations(offset: Int, limit: Int): List<Conversation> =
|
||||
conversations.toList()
|
||||
|
||||
override fun events(after: Instant): Flow<AgentEvent> = emptyFlow()
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+296
-66
@@ -1,101 +1,331 @@
|
||||
# `:client` — Ktor-клиент к `:server`/`:proto` (KMP, jvm + native)
|
||||
# `:client` — Ktor-клиент к `:server` (KMP, jvm + native)
|
||||
|
||||
## Что это
|
||||
Тонкий HTTP-клиент к `:server`-фасаду + локальные примитивы, чтобы
|
||||
собирать свои клиенты (UI, CLI, parent-агенты, A2A-bridge) без бойлерплейта
|
||||
про HTTP, JSON, SSE и lifecycle `Conversation`.
|
||||
|
||||
Ktor client (`io.ktor.client.HttpClient` + `ContentNegotiation(json) +
|
||||
Sse`), превращающий HTTP/SSE-фасад `:server` в `Agent`/`Conversation`
|
||||
интерфейсы `:proto`:
|
||||
## Что есть
|
||||
|
||||
- `AgentikAgent(id, baseUrl)` — entry-point фабрики.
|
||||
- `AgentClient` — список и lifecycle диалогов.
|
||||
- `ConversationClient` — `send()`, `events()`, `interrupt()`,
|
||||
`getMessages()`, `rename()`, `close()`.
|
||||
- Внутренний парсер SSE → `Flow<Event>`.
|
||||
- `AgentikAgent(id, baseUrl, engineFactory, token?)` — entry-point. Возвращает
|
||||
`Agent` (тот же интерфейс, что в `:proto`). HttpClient создаётся внутри
|
||||
из переданной `engineFactory` (`CIO`, `OkHttp`, `Darwin`).
|
||||
- `Agent`: `createConversation` / `getConversation` / `getConversations` /
|
||||
`deleteConversation` / `journal` / `outbox` / `close`.
|
||||
- `Conversation`: `send(content, context?)` / `events(after)` (SSE `Flow<Event>`)
|
||||
/ `getMessages(after, offset, limit)` / `rename` / `interrupt` / `close`.
|
||||
- `HttpJournalStore` — `list(convId, after, offset, limit)` → `List<MessageRecord>`
|
||||
со всеми типами записей (User/Assistant/ToolCall/ToolResult/Error + tokens).
|
||||
- `HttpEventStore` — `events` / `agentEvents` / `conversationEvents` (SSE).
|
||||
|
||||
Решает: пишем нативный Kotlin-клиент, без curl/JS/Python boilerplate,
|
||||
с теми же типами, что и сервер. Один и тот же клиент работает на
|
||||
JVM, iOS, macOS, Linux, Windows.
|
||||
`Agent` — `AutoCloseable`; `agent.close()` закрывает HttpClient. Не нужно
|
||||
вручную создавать `HttpClient` и накатывать на него JSON/Bearer-плагины.
|
||||
|
||||
## Где используется
|
||||
|
||||
- `:agentik-cli` — REPL.
|
||||
- `:agentik-cli` — JVM/native CLI-клиент поверх `:client`.
|
||||
- Любой внешний KMP-проект, который хочет встроить агента в свой UI.
|
||||
|
||||
## Как подключить
|
||||
## Подключение
|
||||
|
||||
```kotlin
|
||||
// build.gradle.kts
|
||||
kotlin {
|
||||
sourceSets.commonMain.dependencies {
|
||||
dependencies {
|
||||
api("pw.binom.agentik:client:0.1.0")
|
||||
}
|
||||
}
|
||||
|
||||
// ваш код:
|
||||
val agent = AgentikAgent(id = "agentik", baseUrl = "http://192.168.76.166:8080/agentik")
|
||||
val conv = agent.createConversation(title = "test")
|
||||
conv.send(listOf(Content.Text("hello"))).collect { event ->
|
||||
when (event) {
|
||||
is Event.AppendText -> print(event.body)
|
||||
is Event.End -> println("\n--- end ---")
|
||||
is Event.Error -> error("agent error: ${event.message}")
|
||||
else -> Unit
|
||||
}
|
||||
// Движок — на твой выбор (один из):
|
||||
implementation("io.ktor:ktor-client-cio:3.x") // JVM/Native
|
||||
implementation("io.ktor:ktor-client-okhttp:3.x") // JVM
|
||||
implementation("io.ktor:ktor-client-darwin:3.x") // iOS/macOS
|
||||
// Опционально — только если будешь использовать `InMemoryJournalStore`
|
||||
// как клиентский кэш. Свой `MutableJournalStore` — не нужен.
|
||||
api("pw.binom.agentik:journal-inmemory:0.1.0")
|
||||
}
|
||||
```
|
||||
|
||||
## Версии
|
||||
## Быстрый старт: свой клиент за 5 минут
|
||||
|
||||
`gradle/libs.versions.toml` → `[versions] agentik-client`.
|
||||
|
||||
Поддерживает все KMP-таргеты, что и `:proto`.
|
||||
|
||||
## Примеры API
|
||||
Один self-contained пример: создаём агента, открываем диалог,
|
||||
отправляем сообщение, печатаем streaming-ответ.
|
||||
|
||||
```kotlin
|
||||
// список диалогов
|
||||
agent.getConversations().collect { println(it.id to it.title) }
|
||||
import pw.binom.agentik.client.AgentikAgent
|
||||
import pw.binom.agentik.proto.Content
|
||||
import pw.binom.agentik.proto.Event
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlin.time.Clock
|
||||
|
||||
// live-подписка на события отдельного диалога
|
||||
val sub = conversation.events(after = Instant.parse("2026-09-01T00:00:00Z")).collect { }
|
||||
fun main() = runBlocking {
|
||||
// 1. Agent — обёртка над :server фасадом. HttpClient создаётся внутри.
|
||||
val agent = AgentikAgent(
|
||||
id = "my-client",
|
||||
baseUrl = "http://localhost:8080/agentik",
|
||||
engineFactory = CIO,
|
||||
token = "s3cret", // или null, если не нужен
|
||||
)
|
||||
|
||||
// прерывание текущего хода
|
||||
conversation.interrupt()
|
||||
// 2. Открыть диалог, отправить сообщение.
|
||||
val conv = agent.createConversation(temp = false)
|
||||
conv.send(listOf(Content.Text("Привет")))
|
||||
|
||||
// история
|
||||
conversation.getMessages(offset = 0).collect { msg ->
|
||||
when (msg) {
|
||||
is Message.UserMessage -> println("user: ${msg.content}")
|
||||
is Message.AssistantMessage -> println("assistant: ${msg.content}")
|
||||
// 3. Собирать streaming-ответ.
|
||||
conv.events(after = Clock.System.now()).collect { ev ->
|
||||
when (ev) {
|
||||
is Event.StartResponse -> println("[start]")
|
||||
is Event.AppendText -> print(ev.body)
|
||||
is Event.End -> println("[end]")
|
||||
is Event.Error -> println("[error: ${ev.message}]")
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Чистый shutdown.
|
||||
conv.close()
|
||||
agent.close()
|
||||
}
|
||||
```
|
||||
|
||||
**Это весь клиент.** `:server` сам хранит историю, контекст, события.
|
||||
Ты только получаешь типизированный `Flow<Event>` и рендеришь как хочешь.
|
||||
|
||||
`HttpClient`, `applyAgentikDefaults`, выбор engine'а — всё скрыто
|
||||
внутри `AgentikAgent`. Один вызов — один готовый `Agent`.
|
||||
|
||||
### Добавить локальный кэш истории (ещё 4 строки)
|
||||
|
||||
```kotlin
|
||||
import pw.binom.agentik.journal.inmemory.InMemoryJournalStore
|
||||
import kotlin.time.Instant
|
||||
|
||||
// Свой кэш. Хочешь SQLite/JSON/etc. — реализуй MutableJournalStore сам.
|
||||
val cache = InMemoryJournalStore()
|
||||
|
||||
// Backfill + live-refresh в одном фоне:
|
||||
launch {
|
||||
agent.journal.listFlow(conv.id, Instant.DISTANT_PAST)
|
||||
.collect { cache.append(it) }
|
||||
}
|
||||
|
||||
// История — теперь из кэша, без HTTP:
|
||||
val history = cache.list(conv.id, Instant.DISTANT_PAST, 0, Int.MAX_VALUE)
|
||||
history.forEach { rec ->
|
||||
when (rec) {
|
||||
is pw.binom.agentik.journal.MessageRecord.UserMessage -> print("user> ${rec.content.text()}")
|
||||
is pw.binom.agentik.journal.MessageRecord.AssistantMessage -> print("agent> ${rec.content.text()}")
|
||||
is pw.binom.agentik.journal.MessageRecord.ToolCall -> print("[tool: ${rec.toolName}]")
|
||||
is pw.binom.agentik.journal.MessageRecord.ToolResult -> print("[result]")
|
||||
is pw.binom.agentik.journal.MessageRecord.Error -> print("[error: ${rec.message}]")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Шаблон "remote.listFlow → local.append" работает с любым
|
||||
`MutableJournalStore` (см. `:journal-api`). Это и есть кэширование
|
||||
"без геморроя".
|
||||
|
||||
### Что вообще не нужно писать самому
|
||||
|
||||
- HTTP-сериализация `Event`/`Message` — `agentikHttpClient` регистрирует
|
||||
`agentikJson` и `InstantSerializer`.
|
||||
- SSE-парсер — `readSse()` внутри `:client`.
|
||||
- Cursor-менеджмент для `listFlow` — дефолтная имплементация в
|
||||
`JournalStore.listFlow` сама пагинирует.
|
||||
- Lifecycle подписок на `events()` — `Conversation.close()` отменяет SSE-job.
|
||||
- HTTP-клиент и Bearer — `AgentikAgent` создаёт `HttpClient(engineFactory)`
|
||||
с Bearer'ом из `token=` под капотом; `agent.close()` его закрывает.
|
||||
- Движковые настройки (requestTimeout и пр.) — `HttpClient(engineFactory) { ... }`
|
||||
создаётся здесь; для нестандартных движковых настроек используй
|
||||
`agentikHttpClient(engineFactory, token)` напрямую (он экспортирован).
|
||||
|
||||
### Что нужно написать самому
|
||||
|
||||
- UI-рендеринг `Event`'ов — это твоё (Compose/HTML/CLI).
|
||||
- Диалог с пользователем — ввод текста, отображение кнопок и т.п.
|
||||
- Persist кэша между запусками (если нужно) — замени `InMemoryJournalStore`
|
||||
на свой `MutableJournalStore` (см. `:journal-ksqlite` как пример).
|
||||
|
||||
|
||||
## Базовый пример: send + collect events
|
||||
|
||||
```kotlin
|
||||
import pw.binom.agentik.client.AgentikAgent
|
||||
import pw.binom.agentik.proto.Content
|
||||
import pw.binom.agentik.proto.Event
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
|
||||
val agent = AgentikAgent(
|
||||
id = "agentik",
|
||||
baseUrl = "http://localhost:8080/agentik",
|
||||
engineFactory = CIO,
|
||||
)
|
||||
|
||||
val conv = agent.createConversation(temp = false)
|
||||
conv.send(listOf(Content.Text("Привет, расскажи про себя")))
|
||||
|
||||
conv.events(after = kotlin.time.Clock.System.now()).collect { ev ->
|
||||
when (ev) {
|
||||
is Event.AppendText -> print(ev.body) // streaming чанки
|
||||
is Event.End -> println("\n--- end ---")
|
||||
is Event.Error -> error("agent error: ${ev.message}")
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## История с локальным кэшем
|
||||
|
||||
Главный паттерн: **клиент держит свой `MutableJournalStore` и периодически
|
||||
(или разово) синхронизирует с удалённым через `listFlow`**. Дальше всё
|
||||
чтение истории — из локального кэша.
|
||||
|
||||
`InMemoryJournalStore` — это `MutableJournalStore`, ты можешь реализовать
|
||||
свой (например с персистентностью в SQLite/JSON/whatever) — главное чтобы
|
||||
реализовывал интерфейс.
|
||||
|
||||
```kotlin
|
||||
import pw.binom.agentik.journal.inmemory.InMemoryJournalStore
|
||||
import pw.binom.agentik.proto.Content
|
||||
import pw.binom.agentik.proto.Event
|
||||
import kotlin.time.Instant
|
||||
|
||||
class ChatSession(
|
||||
private val agent: pw.binom.agentik.proto.Agent,
|
||||
val conversationId: String,
|
||||
) : AutoCloseable {
|
||||
|
||||
// Локальный кэш. Замените InMemoryJournalStore на свой, если нужна
|
||||
// персистентность (SQLite/JSON/etc.) — контракт `MutableJournalStore`
|
||||
// (модуль `:journal-api`).
|
||||
val cache = InMemoryJournalStore()
|
||||
|
||||
// Подписка на live-события этого диалога — будем обновлять кэш на `End`.
|
||||
private val scope = kotlinx.coroutines.CoroutineScope(
|
||||
kotlinx.coroutines.SupervisorJob() +
|
||||
kotlinx.coroutines.Dispatchers.Default,
|
||||
)
|
||||
|
||||
init {
|
||||
// 1. Backfill: забираем всю историю разговора с сервера.
|
||||
scope.launch {
|
||||
agent.journal.listFlow(
|
||||
conversationId = conversationId,
|
||||
after = Instant.DISTANT_PAST,
|
||||
).collect { cache.append(it) }
|
||||
}
|
||||
// 2. Live: на каждом `End` хода просим у сервера новые записи.
|
||||
scope.launch {
|
||||
agent.getConversation(conversationId)!!.events(Instant.DISTANT_PAST).collect { ev ->
|
||||
if (ev is Event.End) {
|
||||
val newest = cache.let {
|
||||
// last-seen курсор — последний createdAt в кэше
|
||||
it.list(conversationId, Instant.DISTANT_PAST, 0, 1).lastOrNull()?.createdAt
|
||||
?: Instant.DISTANT_PAST
|
||||
}
|
||||
agent.journal.list(conversationId, newest, offset = 0, limit = 100)
|
||||
.forEach { cache.append(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun history() = kotlinx.coroutines.runBlocking {
|
||||
cache.list(conversationId, Instant.DISTANT_PAST, 0, Int.MAX_VALUE)
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
scope.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// Использование:
|
||||
val session = ChatSession(agent, conv.id)
|
||||
|
||||
// История — из кэша:
|
||||
session.history().forEach { rec ->
|
||||
when (rec) {
|
||||
is MessageRecord.UserMessage -> println("user: ${rec.content.text()}")
|
||||
is MessageRecord.AssistantMessage -> println("assistant: ${rec.content.text()}")
|
||||
is MessageRecord.ToolCall -> println("tool-call: ${rec.toolName}")
|
||||
is MessageRecord.ToolResult -> println("tool-result: ${rec.result}")
|
||||
is MessageRecord.Error -> println("error: ${rec.message}")
|
||||
}
|
||||
}
|
||||
|
||||
// Отправить новое сообщение:
|
||||
session.scope.launch {
|
||||
agent.getConversation(conversationId)!!.send(listOf(Content.Text("Привет ещё раз")))
|
||||
}
|
||||
```
|
||||
|
||||
`InMemoryJournalStore` отдаёт `MessageRecord` со всем payload'ом
|
||||
(текст + tool-call/tool-result + tokens). UI сам решает что показать —
|
||||
`rec is MessageRecord.UserMessage` для реплик пользователя,
|
||||
`rec is MessageRecord.ToolCall` для отрисовки tool-call баббла, и т.п.
|
||||
|
||||
## Стриминг live-ответа
|
||||
|
||||
Для streaming-рендера текущего хода подписывайся на `events()` и
|
||||
собирай `Event.AppendText`-чанки в свой буфер. Это **не идёт в кэш** —
|
||||
только для UI-feedback во время хода. После `End` хода запись уже
|
||||
появится в кэше через refresh-блок выше.
|
||||
|
||||
```kotlin
|
||||
import pw.binom.agentik.proto.Event
|
||||
|
||||
agent.getConversation(convId)!!.events(Instant.DISTANT_PAST).collect { ev ->
|
||||
when (ev) {
|
||||
is Event.StartResponse -> println("[start]")
|
||||
is Event.AppendText -> print(ev.body)
|
||||
is Event.AppendImage -> showImage(ev.body)
|
||||
is Event.ToolCall -> println("[tool: ${ev.toolName}]")
|
||||
is Event.ToolResult -> println("[result]")
|
||||
is Event.End -> println("[end]")
|
||||
is Event.Error -> println("[error: ${ev.message}]")
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Прерывание хода
|
||||
|
||||
```kotlin
|
||||
agent.getConversation(convId)!!.interrupt()
|
||||
```
|
||||
|
||||
## Multi-conversation
|
||||
|
||||
Один `Agent`, много `ChatSession`:
|
||||
|
||||
```kotlin
|
||||
val sessions = mutableMapOf<String, ChatSession>()
|
||||
|
||||
fun open(convId: String): ChatSession =
|
||||
sessions.getOrPut(convId) { ChatSession(agent, convId) }
|
||||
|
||||
fun close(convId: String) {
|
||||
sessions.remove(convId)?.close()
|
||||
}
|
||||
```
|
||||
|
||||
Подписка на lifecycle диалогов (`agent.outbox.agentEvents(...)`) +
|
||||
UI-обновление списка — отдельная задача, решается `Flow<CommonEvent.Agent>`.
|
||||
|
||||
## Где `:client` НЕ помогает
|
||||
|
||||
- **UI-рендеринг** — это твоя зона (Compose/HTML/etc.), `:client` только
|
||||
отдаёт типы и потоки.
|
||||
- **Персистентность кэша** — `InMemoryJournalStore` хранит в RAM. Для
|
||||
диска пиши свой `MutableJournalStore` (см. `KsqliteJournalStore` в
|
||||
`:journal-ksqlite` как образец).
|
||||
- **Нестандартные движковые настройки** — для `requestTimeout`,
|
||||
прокси и т.п. используй `agentikHttpClient(engineFactory, token)`
|
||||
напрямую.
|
||||
|
||||
## Тесты
|
||||
|
||||
```
|
||||
./gradlew :client:jvmTest
|
||||
```
|
||||
|
||||
Покрывают: JSON-парсинг Event'ов, SSE-стрим, recovery после разрыва,
|
||||
Покрывают: JSON-парсинг `Event`-ов, SSE-стрим, recovery после разрыва,
|
||||
401/404.
|
||||
|
||||
## Чего здесь НЕТ
|
||||
|
||||
- Никакого LLM-кода. Это просто клиент.
|
||||
- Никакого persistent state. История хранится у сервера, клиент её
|
||||
запрашивает через `getMessages` или подписывается через `events`.
|
||||
|
||||
## Текущий статус
|
||||
|
||||
Используется продакшеном. Бэкендом служит `:server` поверх `:standalone`,
|
||||
но клиент совместим с любым сервером, который держит wire-контракт
|
||||
`:server`.
|
||||
|
||||
## Известное ограничение
|
||||
|
||||
SSE event-stream в не-TTY ssh-сессии (без `-tt`) закрывается на
|
||||
default-таймауте Ktor. Используйте либо ssh -tt, либо нативный
|
||||
default-таймауте Ktor. Используйте либо `ssh -tt`, либо нативный
|
||||
terminal (TTY). Это upstream-особенность Ktor SSE.
|
||||
|
||||
@@ -21,6 +21,8 @@ kotlin {
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
api(project(":proto"))
|
||||
api(project(":outbox-api"))
|
||||
api(project(":journal-api"))
|
||||
|
||||
api(libs.ktor.client.core)
|
||||
implementation(libs.ktor.client.content.negotiation)
|
||||
|
||||
@@ -4,39 +4,38 @@ import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.request.delete
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.prepareGet
|
||||
import io.ktor.client.request.parameter
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.client.request.setBody
|
||||
import io.ktor.client.statement.bodyAsChannel
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.http.contentType
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import pw.binom.agentik.journal.JournalStore
|
||||
import pw.binom.agentik.outbox.OutboxStore
|
||||
import pw.binom.agentik.proto.Agent
|
||||
import pw.binom.agentik.proto.AgentEvent
|
||||
import pw.binom.agentik.proto.Conversation
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* HTTP-реализация [Agent]. Ходит в `:server`-фасад, см. `agentikAgent(...)`.
|
||||
*
|
||||
* Замечание по [createConversation]: интерфейс [Agent] объявлен не-suspend
|
||||
* (in-process кейс этого не требует), но HTTP-вариант обязан ждать ответа
|
||||
* POST `/conversations`. Используем `runBlocking` — это одноразовая
|
||||
* операция (открытие чата), не горячий путь. В UI-контексте вызывающий сам
|
||||
* решает, что делать.
|
||||
* HttpClient создаётся внутри из переданного engine и закрывается в [close].
|
||||
*
|
||||
* **Storage handles** ([journal], [outbox]) — read-only views на серверные
|
||||
* хранилища.
|
||||
*/
|
||||
internal class AgentClient(
|
||||
private val httpClient: HttpClient,
|
||||
private val baseUrl: String,
|
||||
override val id: String,
|
||||
private val baseUrl: String,
|
||||
private val httpClient: HttpClient,
|
||||
) : Agent {
|
||||
|
||||
private val agentUrl: String = baseUrl.trimEnd('/')
|
||||
|
||||
override val outbox: OutboxStore = HttpEventStore(httpClient = httpClient, baseUrl = agentUrl)
|
||||
override val journal: JournalStore = HttpJournalStore(httpClient = httpClient, baseUrl = agentUrl)
|
||||
|
||||
override fun createConversation(temp: Boolean): Conversation =
|
||||
runBlocking {
|
||||
val snapshot: ConversationSnapshot = httpClient.post("$agentUrl/conversations") {
|
||||
@@ -54,7 +53,7 @@ internal class AgentClient(
|
||||
}
|
||||
|
||||
override suspend fun deleteConversation(id: String): Boolean {
|
||||
val response = httpClient.delete("$agentUrl/conversations/$id")
|
||||
val response: HttpResponse = httpClient.delete("$agentUrl/conversations/$id")
|
||||
return response.status == HttpStatusCode.NoContent
|
||||
}
|
||||
|
||||
@@ -66,16 +65,7 @@ internal class AgentClient(
|
||||
return snapshots.map { ConversationClient(httpClient, agentUrl, it) }
|
||||
}
|
||||
|
||||
override fun events(after: Instant): Flow<AgentEvent> = flow {
|
||||
httpClient.prepareGet("$agentUrl/events?after=$after") { noSseReadTimeout() }
|
||||
.execute { response ->
|
||||
check(response.status == HttpStatusCode.OK) {
|
||||
"events: server returned ${response.status}"
|
||||
}
|
||||
readSse(response.bodyAsChannel())
|
||||
.collect { payload ->
|
||||
emit(agentikJson.decodeFromString(AgentEvent.serializer(), payload))
|
||||
}
|
||||
}
|
||||
override fun close() {
|
||||
httpClient.close()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +1,43 @@
|
||||
package pw.binom.agentik.client
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.HttpClientEngineFactory
|
||||
import pw.binom.agentik.proto.Agent
|
||||
|
||||
/**
|
||||
* Создаёт [Agent], который под капотом ходит в HTTP-фасад `agentikAgent`
|
||||
* (модуль `:server`).
|
||||
* Создаёт [Agent], который ходит в HTTP-фасад `agentikAgent` (модуль `:server`).
|
||||
*
|
||||
* Принимает [engineFactory] — `HttpClientEngineFactory<*>` (`CIO`, `OkHttp`,
|
||||
* `Darwin`, ...). Внутри сам создаёт `HttpClient`, накатывает JSON-конфиг
|
||||
* [agentikJson] и опциональный Bearer [token]. Никакого `applyAgentikDefaults`
|
||||
* снаружи — всё под капотом.
|
||||
*
|
||||
* ```
|
||||
* val http = HttpClient(CIO) { applyAgentikDefaults(token = "s3cret") }
|
||||
* val client = AgentikAgent(
|
||||
* id = "my-agent",
|
||||
* val agent = AgentikAgent(
|
||||
* id = "my-client",
|
||||
* baseUrl = "http://localhost:8080/agentik",
|
||||
* httpClient = http,
|
||||
* engineFactory = CIO,
|
||||
* token = "s3cret",
|
||||
* )
|
||||
* val conv = client.createConversation(temp = false)
|
||||
* val conv = agent.createConversation(temp = false)
|
||||
* conv.send(listOf(Content.Text("hi")))
|
||||
* conv.events(Instant.DISTANT_PAST).collect { ev -> ... }
|
||||
* conv.events(Instant.DISTANT_PAST).collect { ... }
|
||||
* agent.close() // закрывает HttpClient
|
||||
* ```
|
||||
*
|
||||
* [id] пробрасывается в реализацию [Agent.id] — сервер про идентичность
|
||||
* агента не знает, поэтому клиент должен её знать сам (или взять из
|
||||
* конфига).
|
||||
* [id] пробрасывается в `Agent.id` — сервер про идентичность агента не знает,
|
||||
* поэтому клиент должен её знать сам (или взять из конфига).
|
||||
*
|
||||
* Клиент приходит снаружи: `:client` не выбирает движок. Собрать [HttpClient]
|
||||
* можно через [agentikHttpClient] (фабрика движка + опциональные движковые
|
||||
* настройки) или вручную, применив к блоку конфигурации [applyAgentikDefaults]
|
||||
* (JSON + опциональный Bearer-токен).
|
||||
* **Lifecycle**: [Agent] — `AutoCloseable`. `agent.close()` закрывает
|
||||
* HttpClient (идемпотентно). После этого `createConversation` /
|
||||
* `getConversation` etc. не определены.
|
||||
*/
|
||||
fun AgentikAgent(
|
||||
id: String,
|
||||
baseUrl: String,
|
||||
httpClient: HttpClient,
|
||||
): Agent = AgentClient(httpClient = httpClient, baseUrl = baseUrl, id = id)
|
||||
engineFactory: HttpClientEngineFactory<*>,
|
||||
token: String? = null,
|
||||
): Agent = AgentClient(
|
||||
id = id,
|
||||
baseUrl = baseUrl,
|
||||
httpClient = agentikHttpClient(engineFactory = engineFactory, token = token),
|
||||
)
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package pw.binom.agentik.client
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.HttpClientConfig
|
||||
import io.ktor.client.engine.HttpClientEngineConfig
|
||||
import io.ktor.client.engine.HttpClientEngineFactory
|
||||
import io.ktor.client.plugins.DefaultRequest
|
||||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
||||
@@ -11,26 +9,20 @@ import io.ktor.http.HttpHeaders
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
|
||||
/**
|
||||
* Общая конфигурация HTTP-клиента agentik — платформо-независимая часть.
|
||||
* Создаёт [HttpClient] поверх [engineFactory] с конфигурацией agentik.
|
||||
*
|
||||
* `:client` НЕ выбирает движок: его приносит потребитель. Здесь живёт только то,
|
||||
* без чего клиент несовместим с `/agentik`:
|
||||
* - JSON-конфиг [agentikJson] (обязан совпадать с серверным);
|
||||
* - при заданном [token] — `Authorization: Bearer <token>` на ВСЕ запросы
|
||||
* через [DefaultRequest] (накрывает 10 REST-вызовов и оба SSE-потока;
|
||||
* заголовок живёт на клиенте, а не в отдельных запросах).
|
||||
* Внутренний helper для [AgentikAgent]. Потребителю `:client` обычно
|
||||
* не нужен — он передаёт engine в [AgentikAgent] и получает готовый
|
||||
* [pw.binom.agentik.proto.Agent] с уже закрытым HttpClient'ом
|
||||
* на [pw.binom.agentik.proto.Agent.close].
|
||||
*
|
||||
* `null` — авторизация выключена, заголовок не отправляется.
|
||||
*
|
||||
* Потребитель, знающий свой движок, добавляет к этому движковые настройки, напр.:
|
||||
* ```
|
||||
* val http = HttpClient(CIO) {
|
||||
* engine { requestTimeout = 0 } // CIO-специфика, живёт у потребителя
|
||||
* applyAgentikDefaults(token)
|
||||
* }
|
||||
* ```
|
||||
* Экспортируется для случаев, когда нужен прямой доступ к `HttpClient`
|
||||
* (например, дополнительные нестандартные запросы в обход `Agent` API).
|
||||
*/
|
||||
fun HttpClientConfig<*>.applyAgentikDefaults(token: String? = null) {
|
||||
fun agentikHttpClient(
|
||||
engineFactory: HttpClientEngineFactory<*>,
|
||||
token: String? = null,
|
||||
): HttpClient = HttpClient(engineFactory) {
|
||||
install(ContentNegotiation) { json(agentikJson) }
|
||||
if (token != null) {
|
||||
install(DefaultRequest) {
|
||||
@@ -38,23 +30,3 @@ fun HttpClientConfig<*>.applyAgentikDefaults(token: String? = null) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Создаёт [HttpClient] из фабрики движка потребителя и сразу применяет к нему
|
||||
* конфигурацию agentik ([applyAgentikDefaults]).
|
||||
*
|
||||
* Это точка, где `:client` НЕ привязан к реализации транспорта: [engineFactory]
|
||||
* выбирает потребитель (CIO, OkHttp, Darwin, …), а `:client` только конфигурирует
|
||||
* созданный клиент.
|
||||
*
|
||||
* [configure] — опциональный последний штрих потребителя (движковые настройки:
|
||||
* таймауты, прокси, логирование). Вызывается ПОСЛЕ [applyAgentikDefaults].
|
||||
*/
|
||||
fun <T : HttpClientEngineConfig> agentikHttpClient(
|
||||
engineFactory: HttpClientEngineFactory<T>,
|
||||
token: String? = null,
|
||||
configure: (HttpClientConfig<T>.() -> Unit)? = null,
|
||||
): HttpClient = HttpClient(engineFactory) {
|
||||
applyAgentikDefaults(token)
|
||||
configure?.invoke(this)
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package pw.binom.agentik.client
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.request.prepareGet
|
||||
import io.ktor.client.statement.bodyAsChannel
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import pw.binom.agentik.outbox.OutboxStore
|
||||
import pw.binom.agentik.outbox.AgentEvent
|
||||
import pw.binom.agentik.outbox.CommonEvent
|
||||
import pw.binom.agentik.outbox.Event
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* HTTP-реализация [OutboxStore] (= [pw.binom.agentik.outbox.OutboxStore]),
|
||||
* ходящая в `:server`-фасад.
|
||||
*
|
||||
* **Endpoint-раскладка** (новый дизайн — storage handles на [Agent]):
|
||||
* - [events] → `GET {baseUrl}/outbox/events?after=` (полный поток
|
||||
* [CommonEvent], bounded-tail + live SSE, см. [pw.binom.agentik.server.outboxRoutes])
|
||||
* - [agentEvents] → `GET {baseUrl}/events?after=` (legacy proto-роут:
|
||||
* сервер пробрасывает [pw.binom.agentik.outbox.agentEvents] и распаковывает
|
||||
* `.event` для обратной совместимости с форматом AgentEvent)
|
||||
* - [conversationEvents] с `conversationId != null` → `GET /conversations/{id}/events`
|
||||
*
|
||||
* Для [conversationEvents] с `conversationId == null` (события всех диалогов)
|
||||
* fallback на default [OutboxStore.conversationEvents] — общий поток
|
||||
* `/outbox/events` + filter. Это редкий кейс (admin-дашборды), и
|
||||
* оптимизировать его отдельно нерационально.
|
||||
*
|
||||
* [earliestEventDate] не имеет своего endpoint'а; возвращает `Clock.System.now()`
|
||||
* (см. KDoc [OutboxStore.earliestEventDate] — для пустого буфера это и есть
|
||||
* контрактное значение). Клиент, который полагался на gap detection через
|
||||
* message store, продолжит работать — просто fallback никогда не сработает.
|
||||
*
|
||||
* **Импорты [CommonEvent]/[AgentEvent]/[Event] идут напрямую из
|
||||
* `pw.binom.agentik.outbox`** — typealias'ы в `:proto.CommonEvent` и т.п.
|
||||
* НЕ поддерживают nested-class access (`CommonEvent.Agent` через alias
|
||||
* даёт "Unresolved qualified name"), поэтому приходится использовать
|
||||
* конкретный пакет. Типы идентичны, alias только для удобства внешнего API.
|
||||
*/
|
||||
internal class HttpEventStore(
|
||||
private val httpClient: HttpClient,
|
||||
private val baseUrl: String,
|
||||
) : OutboxStore {
|
||||
|
||||
private val agentUrl: String = baseUrl.trimEnd('/')
|
||||
|
||||
override fun events(after: Instant?): Flow<CommonEvent> = flow {
|
||||
val url = buildString {
|
||||
append("$agentUrl/outbox/events")
|
||||
if (after != null) append("?after=$after")
|
||||
}
|
||||
httpClient.prepareGet(url) { noSseReadTimeout() }
|
||||
.execute { response ->
|
||||
check(response.status == HttpStatusCode.OK) {
|
||||
"events: server returned ${response.status}"
|
||||
}
|
||||
readSse(response.bodyAsChannel())
|
||||
.collect { payload ->
|
||||
emit(agentikJson.decodeFromString(CommonEvent.serializer(), payload))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Override: идём в `/events` напрямую — сервер фильтрует только lifecycle-события.
|
||||
* Default из [EventStore.agentEvents] читал бы `/events/all` + `filterIsInstance`.
|
||||
*/
|
||||
override fun agentEvents(after: Instant?): Flow<CommonEvent.Agent> = flow {
|
||||
val url = buildString {
|
||||
append("$agentUrl/events")
|
||||
if (after != null) append("?after=$after")
|
||||
}
|
||||
httpClient.prepareGet(url) { noSseReadTimeout() }
|
||||
.execute { response ->
|
||||
check(response.status == HttpStatusCode.OK) {
|
||||
"agentEvents: server returned ${response.status}"
|
||||
}
|
||||
readSse(response.bodyAsChannel())
|
||||
.collect { payload ->
|
||||
val event = agentikJson.decodeFromString(AgentEvent.serializer(), payload)
|
||||
emit(CommonEvent.Agent(date = event.date, event = event))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Override с `conversationId != null` — идём в `/conversations/{id}/events`.
|
||||
* С `null` (события всех диалогов) — fallback на default impl из [EventStore]:
|
||||
* общий `/events/all` + filter.
|
||||
*/
|
||||
override fun conversationEvents(
|
||||
after: Instant?,
|
||||
conversationId: String?,
|
||||
): Flow<CommonEvent.Conversation> {
|
||||
if (conversationId == null) {
|
||||
return super.conversationEvents(after, null)
|
||||
}
|
||||
return flow {
|
||||
val url = buildString {
|
||||
append("$agentUrl/conversations/$conversationId/events")
|
||||
if (after != null) append("?after=$after")
|
||||
}
|
||||
httpClient.prepareGet(url) { noSseReadTimeout() }
|
||||
.execute { response ->
|
||||
check(response.status == HttpStatusCode.OK) {
|
||||
"conversationEvents: server returned ${response.status}"
|
||||
}
|
||||
readSse(response.bodyAsChannel())
|
||||
.collect { payload ->
|
||||
val event = agentikJson.decodeFromString(Event.serializer(), payload)
|
||||
emit(CommonEvent.Conversation(date = event.date, conversationId = conversationId, event = event))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* У HTTP-варианта нет своего endpoint'а для earliest-event-date.
|
||||
* Контракт [EventStore.earliestEventDate] для пустого буфера говорит
|
||||
* "сейчас" — для HTTP-клиента буфер на нашей стороне всегда "пуст"
|
||||
* (мы не держим своё состояние), поэтому возвращаем `Clock.System.now()`.
|
||||
*/
|
||||
override suspend fun earliestEventDate(): Instant = Clock.System.now()
|
||||
|
||||
override fun close() {
|
||||
// HttpClient закрывает владелец (AgentClient / AgentikAgent).
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package pw.binom.agentik.client
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.parameter
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import pw.binom.agentik.journal.JournalStore
|
||||
import pw.binom.agentik.journal.MessageRecord
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* HTTP-реализация [JournalStore] (append-only audit log сообщений диалога),
|
||||
* ходящая в `:server`-фасад.
|
||||
*
|
||||
* **Endpoint**: `GET {baseUrl}/journal/conversations/{id}/messages?after=&offset=&limit=`
|
||||
* (см. [pw.binom.agentik.server.journalRoutes]).
|
||||
*
|
||||
* Возвращает raw [MessageRecord] (все типы: UserMessage / AssistantMessage /
|
||||
* ToolCall / ToolResult / Error). В отличие от `GET /conversations/{id}/messages`
|
||||
* в `:server`'s proto-роутах (который отдаёт project'нутые
|
||||
* [pw.binom.agentik.proto.Message]), здесь клиент получает полный transcript
|
||||
* с tool-call/tool-result/error payload'ами, turn-tokens и context'ом.
|
||||
*
|
||||
* **listFlow** — default cold-flow paging через [list] (N+1 round-trip,
|
||||
* дефолтная реализация из [JournalStore]). Для remote/SQL-backed store'а
|
||||
* это OK: server-side paging + client-side flow compose'ится естественно.
|
||||
*
|
||||
* **Read-only**: [JournalStore] не имеет `append` — запись только через
|
||||
* writer-референс, который ChatAgent держит внутри (тип
|
||||
* `MutableJournalStore`, не выставлен наружу через [pw.binom.agentik.proto.Agent]).
|
||||
*/
|
||||
internal class HttpJournalStore(
|
||||
private val httpClient: HttpClient,
|
||||
private val baseUrl: String,
|
||||
) : JournalStore {
|
||||
|
||||
private val agentUrl: String = baseUrl.trimEnd('/')
|
||||
|
||||
override suspend fun list(
|
||||
conversationId: String,
|
||||
after: Instant,
|
||||
offset: Int,
|
||||
limit: Int,
|
||||
): List<MessageRecord> {
|
||||
val response = httpClient.get("$agentUrl/journal/conversations/$conversationId/messages") {
|
||||
parameter("after", after.toString())
|
||||
parameter("offset", offset)
|
||||
parameter("limit", limit)
|
||||
}
|
||||
check(response.status == HttpStatusCode.OK) {
|
||||
"journal.list: server returned ${response.status}"
|
||||
}
|
||||
return response.body<List<MessageRecord>>()
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
// HttpClient закрывает владелец (AgentClient / AgentikAgent).
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import io.ktor.client.request.HttpRequestBuilder
|
||||
* использует плагин `SSE`, поэтому движок не считает запрос SSE-шным
|
||||
* (`HttpRequestBuilder.supportsRequestTimeout` проверяет
|
||||
* `body is SSEClientContent`, а у нас тело — обычный GET без тела).
|
||||
* Без capability встроенный `CIOEngineConfig.requestTimeout` (по умолчанию
|
||||
* Без capability встроенный `HttpTimeoutPlugin.requestTimeoutMillis` (по умолчанию
|
||||
* **15000 мс**) молча убивает долгий idle-стрим через 15 секунд.
|
||||
*
|
||||
* Конфиг создаётся заново на каждый вызов — плагин `HttpTimeout` при
|
||||
|
||||
@@ -21,7 +21,7 @@ import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
/**
|
||||
* Тесты клиентской части: [applyAgentikDefaults] с заданным `token` прикладывает
|
||||
* Тесты клиентской части: [agentikHttpClient] с заданным `token` прикладывает
|
||||
* `Authorization: Bearer <token>` ко всем запросам через плагин `DefaultRequest`,
|
||||
* без токена — заголовок не отправляется.
|
||||
*
|
||||
@@ -50,7 +50,7 @@ class BearerHeaderTest {
|
||||
}
|
||||
|
||||
private fun clientWith(token: String?): HttpClient =
|
||||
HttpClient(CIO) { applyAgentikDefaults(token) }
|
||||
agentikHttpClient(engineFactory = CIO, token = token)
|
||||
|
||||
private suspend fun startServer(): Pair<EmbeddedServer<*, *>, Int> {
|
||||
val server = embeddedServer(ServerCIO, port = 0) {
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
plugins {
|
||||
alias(libs.plugins.kotlin.multiplatform)
|
||||
alias(libs.plugins.kotlin.serialization)
|
||||
}
|
||||
|
||||
// Public API для runtime context агента (compaction, order_idx, summary entries).
|
||||
// Зависит от :journal-api для типов `Content` / `MessageContext` (audit-log
|
||||
// payload'ы, которые рабочая память ссылает).
|
||||
//
|
||||
// НЕ нужен тонким клиентам — только серверному рантайму (`:standalone`, `:agentik-cli`,
|
||||
// будущий `:android-agent` core).
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(21)
|
||||
|
||||
jvm()
|
||||
macosX64()
|
||||
macosArm64()
|
||||
iosX64()
|
||||
iosArm64()
|
||||
iosSimulatorArm64()
|
||||
linuxX64()
|
||||
linuxArm64()
|
||||
mingwX64()
|
||||
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
api(project(":journal-api"))
|
||||
api(libs.kotlinx.coroutines.core)
|
||||
api(libs.kotlinx.serialization.core)
|
||||
api(libs.kotlinx.serialization.json)
|
||||
}
|
||||
commonTest.dependencies {
|
||||
implementation(kotlin("test"))
|
||||
implementation(libs.kotlinx.coroutines.test)
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
package pw.binom.agentik.storage
|
||||
package pw.binom.agentik.context
|
||||
|
||||
import kotlin.time.Instant
|
||||
|
||||
@@ -7,7 +7,7 @@ import kotlin.time.Instant
|
||||
*
|
||||
* Используется для тестов и для перестроения [WorkingMemoryEntry] из row.
|
||||
* Агент не должен с этим типом работать напрямую — он работает с
|
||||
* [WorkingMemoryEntry] через [WorkingMemoryStore].
|
||||
* [WorkingMemoryEntry] через [ContextStore].
|
||||
*/
|
||||
data class WorkingMemoryRow(
|
||||
val id: String,
|
||||
@@ -28,7 +28,7 @@ data class WorkingMemoryRow(
|
||||
*
|
||||
* Суммаризация / чистка — один атомарный вызов [compact].
|
||||
*/
|
||||
interface WorkingMemoryStore : AutoCloseable {
|
||||
interface ContextStore : AutoCloseable {
|
||||
|
||||
/** Добавить запись в конец working memory (новый максимальный `order_idx`). */
|
||||
suspend fun append(conversationId: String, entry: WorkingMemoryEntry, now: Instant)
|
||||
+3
-1
@@ -1,7 +1,9 @@
|
||||
package pw.binom.agentik.storage
|
||||
package pw.binom.agentik.context
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import pw.binom.agentik.journal.Content
|
||||
import pw.binom.agentik.journal.MessageContext
|
||||
|
||||
/**
|
||||
* Запись в working memory диалога: ровно то, что агент сейчас видит в
|
||||
@@ -0,0 +1,41 @@
|
||||
plugins {
|
||||
alias(libs.plugins.kotlin.multiplatform)
|
||||
alias(libs.plugins.kotlin.serialization)
|
||||
}
|
||||
|
||||
// KMP-реализация :context-api (ContextStore) поверх ksqlite.
|
||||
// Минимальная — только таблица `working_memory` + 2 индекса по ней.
|
||||
// Остальные таблицы (`conversation`, `message`, `reflection`) живут в
|
||||
// других ksqlite-модулях; этот модуль не претендует на полную схему
|
||||
// агента.
|
||||
//
|
||||
// Цели сборки — jvm() + linuxX64() + mingwX64(); Apple targets auto-disabled
|
||||
// на Linux (см. KDoc :storage-ksqlite).
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(21)
|
||||
|
||||
jvm()
|
||||
linuxX64()
|
||||
mingwX64()
|
||||
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
implementation("pw.binom.db:ksqlite:0.1.1-SNAPSHOT")
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
|
||||
api(project(":context-api"))
|
||||
api(project(":journal-api"))
|
||||
api(project(":reflection-api"))
|
||||
// :context-api ссылается на Content / MessageContext из
|
||||
// :message-log-api (старый canonical). Транзитивно через api,
|
||||
// но фиксируем явно чтобы тестовый код видел Content без
|
||||
// обхода через :context-api.
|
||||
// api(project(":message-log-api"))
|
||||
}
|
||||
commonTest.dependencies {
|
||||
implementation(kotlin("test"))
|
||||
implementation(libs.kotlinx.coroutines.test)
|
||||
}
|
||||
}
|
||||
}
|
||||
+200
@@ -0,0 +1,200 @@
|
||||
package pw.binom.agentik.context.ksqlite
|
||||
|
||||
import kotlinx.serialization.json.Json
|
||||
import pw.binom.agentik.context.ContextStore
|
||||
import pw.binom.agentik.context.WorkingMemoryEntry
|
||||
import pw.binom.agentik.context.WorkingMemoryRow
|
||||
import pw.binom.agentik.journal.Ids
|
||||
import pw.binom.db.ksqlite.SQLiteConnection
|
||||
import pw.binom.db.ksqlite.SQLitePreparedStatement
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.Instant
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
/**
|
||||
* ksqlite-реализация [ContextStore] (таблица `working_memory`).
|
||||
*
|
||||
* Структура — копия [pw.binom.agentik.storage.ksqlite.KsqliteWorkingMemoryStore]
|
||||
* из `:storage-ksqlite`, но:
|
||||
* - лежит в собственном модуле `:context-ksqlite`;
|
||||
* - реализует переименованный [ContextStore] (раньше был `WorkingMemoryStore`,
|
||||
* теперь главный класс — `ContextStore`); сами типы строк
|
||||
* [WorkingMemoryEntry] / [WorkingMemoryRow] не переименовывались.
|
||||
*
|
||||
* ВНИМАНИЕ: `:storage-ksqlite/KsqliteWorkingMemoryStore.kt` остаётся на диске —
|
||||
* это копия, не замена. Не удалять старый файл; миграция consumers'ов — отдельно.
|
||||
*/
|
||||
class KsqliteContextStore(
|
||||
private val connection: SQLiteConnection,
|
||||
) : ContextStore {
|
||||
|
||||
private val mutex = Mutex()
|
||||
private val json = Json { ignoreUnknownKeys = true }
|
||||
|
||||
// pre-prepare (см. KsqliteMessageStore KDoc — почему это критично против
|
||||
// SIGSEGV в StmtHolder.finalize на закрытой connection).
|
||||
private val insertStmt: SQLitePreparedStatement = connection.prepare(
|
||||
"""
|
||||
INSERT INTO ${Schema.TABLE_WORKING_MEMORY}
|
||||
(${Schema.COL_ID}, ${Schema.COL_CONVERSATION_ID}, ${Schema.COL_ORDER_IDX},
|
||||
${Schema.COL_SOURCE_MESSAGE_ID}, ${Schema.COL_KIND},
|
||||
${Schema.COL_PAYLOAD_JSON}, ${Schema.COL_CREATED_AT})
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
""".trimIndent()
|
||||
)
|
||||
private val listStmt: SQLitePreparedStatement = connection.prepare(
|
||||
"""
|
||||
SELECT ${Schema.COL_ID}, ${Schema.COL_CONVERSATION_ID}, ${Schema.COL_ORDER_IDX},
|
||||
${Schema.COL_SOURCE_MESSAGE_ID}, ${Schema.COL_PAYLOAD_JSON}, ${Schema.COL_CREATED_AT}
|
||||
FROM ${Schema.TABLE_WORKING_MEMORY}
|
||||
WHERE ${Schema.COL_CONVERSATION_ID} = ?
|
||||
ORDER BY ${Schema.COL_ORDER_IDX} ASC
|
||||
""".trimIndent()
|
||||
)
|
||||
private val clearStmt: SQLitePreparedStatement = connection.prepare(
|
||||
"DELETE FROM ${Schema.TABLE_WORKING_MEMORY} WHERE ${Schema.COL_CONVERSATION_ID} = ?"
|
||||
)
|
||||
private val maxOrderIdxStmt: SQLitePreparedStatement = connection.prepare(
|
||||
"""
|
||||
SELECT COALESCE(MAX(${Schema.COL_ORDER_IDX}), 0)
|
||||
FROM ${Schema.TABLE_WORKING_MEMORY}
|
||||
WHERE ${Schema.COL_CONVERSATION_ID} = ?
|
||||
""".trimIndent()
|
||||
)
|
||||
private val dropFromIdxStmt: SQLitePreparedStatement = connection.prepare(
|
||||
"""
|
||||
DELETE FROM ${Schema.TABLE_WORKING_MEMORY}
|
||||
WHERE ${Schema.COL_CONVERSATION_ID} = ? AND ${Schema.COL_ORDER_IDX} >= ?
|
||||
""".trimIndent()
|
||||
)
|
||||
private val insertSummaryStmt: SQLitePreparedStatement = connection.prepare(
|
||||
"""
|
||||
INSERT INTO ${Schema.TABLE_WORKING_MEMORY}
|
||||
(${Schema.COL_ID}, ${Schema.COL_CONVERSATION_ID}, ${Schema.COL_ORDER_IDX},
|
||||
${Schema.COL_SOURCE_MESSAGE_ID}, ${Schema.COL_KIND},
|
||||
${Schema.COL_PAYLOAD_JSON}, ${Schema.COL_CREATED_AT})
|
||||
VALUES (?, ?, ?, NULL, ?, ?, ?)
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
override suspend fun append(conversationId: String, entry: WorkingMemoryEntry, now: Instant): Unit = withContext(Dispatchers.Default) {
|
||||
mutex.withLock {
|
||||
val newIdx = maxOrderIdx(conversationId) + 1
|
||||
insertStmt.reset()
|
||||
insertStmt.clearBindings()
|
||||
insertStmt.bindText(1, Ids.new("wm"))
|
||||
insertStmt.bindText(2, conversationId)
|
||||
insertStmt.bindLong(3, newIdx)
|
||||
val srcId = entry.sourceMessageId
|
||||
if (srcId != null) insertStmt.bindText(4, srcId) else insertStmt.bindNull(4)
|
||||
insertStmt.bindText(5, entryKind(entry))
|
||||
insertStmt.bindText(6, json.encodeToString(WorkingMemoryEntry.serializer(), entry))
|
||||
insertStmt.bindLong(7, now.toEpochMilliseconds())
|
||||
insertStmt.executeUpdate()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun list(conversationId: String): List<WorkingMemoryRow> = withContext(Dispatchers.Default) {
|
||||
mutex.withLock {
|
||||
listStmt.reset()
|
||||
listStmt.clearBindings()
|
||||
listStmt.bindText(1, conversationId)
|
||||
val out = mutableListOf<WorkingMemoryRow>()
|
||||
listStmt.executeQuery().use { rs ->
|
||||
while (rs.next()) {
|
||||
out.add(
|
||||
WorkingMemoryRow(
|
||||
id = rs.getText(0)!!,
|
||||
conversationId = rs.getText(1)!!,
|
||||
orderIdx = rs.getLong(2)!!,
|
||||
sourceMessageId = rs.getText(3),
|
||||
entry = Json.decodeFromString(WorkingMemoryEntry.serializer(), rs.getText(4)!!),
|
||||
createdAt = Instant.fromEpochMilliseconds(rs.getLong(5)!!),
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun clear(conversationId: String): Unit = withContext(Dispatchers.Default) {
|
||||
mutex.withLock {
|
||||
clearStmt.reset()
|
||||
clearStmt.clearBindings()
|
||||
clearStmt.bindText(1, conversationId)
|
||||
clearStmt.executeUpdate()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun compact(
|
||||
dropFromOrderIdx: Long,
|
||||
conversationId: String,
|
||||
summaryText: String?,
|
||||
): Long = withContext(Dispatchers.Default) {
|
||||
mutex.withLock {
|
||||
var newMax = 0L
|
||||
val nowMs = Clock.System.now().toEpochMilliseconds()
|
||||
val summaryId = Ids.new("wm")
|
||||
connection.exec("BEGIN")
|
||||
try {
|
||||
dropFromIdxStmt.reset()
|
||||
dropFromIdxStmt.clearBindings()
|
||||
dropFromIdxStmt.bindText(1, conversationId)
|
||||
dropFromIdxStmt.bindLong(2, dropFromOrderIdx)
|
||||
dropFromIdxStmt.executeUpdate()
|
||||
|
||||
if (!summaryText.isNullOrBlank()) {
|
||||
val afterDelete = maxOrderIdx(conversationId)
|
||||
val newIdx = afterDelete + 1
|
||||
insertSummaryStmt.reset()
|
||||
insertSummaryStmt.clearBindings()
|
||||
insertSummaryStmt.bindText(1, summaryId)
|
||||
insertSummaryStmt.bindText(2, conversationId)
|
||||
insertSummaryStmt.bindLong(3, newIdx)
|
||||
insertSummaryStmt.bindText(4, "summary")
|
||||
insertSummaryStmt.bindText(5, json.encodeToString(WorkingMemoryEntry.serializer(), WorkingMemoryEntry.Summary(text = summaryText)))
|
||||
insertSummaryStmt.bindLong(6, nowMs)
|
||||
insertSummaryStmt.executeUpdate()
|
||||
newMax = newIdx
|
||||
} else {
|
||||
newMax = maxOrderIdx(conversationId)
|
||||
}
|
||||
connection.exec("COMMIT")
|
||||
} catch (t: Throwable) {
|
||||
runCatching { connection.exec("ROLLBACK") }
|
||||
throw t
|
||||
}
|
||||
newMax
|
||||
}
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
insertStmt.close()
|
||||
listStmt.close()
|
||||
clearStmt.close()
|
||||
maxOrderIdxStmt.close()
|
||||
dropFromIdxStmt.close()
|
||||
insertSummaryStmt.close()
|
||||
}
|
||||
|
||||
private fun maxOrderIdx(conversationId: String): Long {
|
||||
maxOrderIdxStmt.reset()
|
||||
maxOrderIdxStmt.clearBindings()
|
||||
maxOrderIdxStmt.bindText(1, conversationId)
|
||||
maxOrderIdxStmt.executeQuery().use { rs ->
|
||||
if (rs.next()) return rs.getLong(0) ?: 0L
|
||||
}
|
||||
return 0L
|
||||
}
|
||||
|
||||
private fun entryKind(e: WorkingMemoryEntry): String = when (e) {
|
||||
is WorkingMemoryEntry.User -> "user"
|
||||
is WorkingMemoryEntry.Assistant -> "assistant"
|
||||
is WorkingMemoryEntry.ToolExchange -> "tool_exchange"
|
||||
is WorkingMemoryEntry.Summary -> "summary"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package pw.binom.agentik.context.ksqlite
|
||||
|
||||
import pw.binom.db.ksqlite.SQLiteConnection
|
||||
|
||||
/**
|
||||
* Имена таблиц/колонок/индексов для ksqlite-бэкенда `:context-api`.
|
||||
*
|
||||
* Минимум — только то, что относится к `working_memory` (реализация
|
||||
* [KsqliteContextStore]). Остальные таблицы агента (`conversation`,
|
||||
* `message`, `reflection`) живут в других ksqlite-модулях.
|
||||
*
|
||||
* Все DDL/DML в этом модуле должны ссылаться на эти константы — никаких
|
||||
* хардкоженных литералов в `prepare("SELECT ... FROM foo ...")` в store'е.
|
||||
*/
|
||||
internal object Schema {
|
||||
|
||||
/** Версия схемы модуля. Увеличивать при ЛЮБОМ изменении DDL. */
|
||||
const val CURRENT_VERSION: Int = 1
|
||||
|
||||
// ───── Таблица ─────
|
||||
const val TABLE_WORKING_MEMORY = "working_memory"
|
||||
|
||||
// ───── Колонки ─────
|
||||
const val COL_ID = "id"
|
||||
const val COL_CONVERSATION_ID = "conversation_id"
|
||||
const val COL_ORDER_IDX = "order_idx"
|
||||
const val COL_SOURCE_MESSAGE_ID = "source_message_id"
|
||||
const val COL_KIND = "kind"
|
||||
const val COL_PAYLOAD_JSON = "payload_json"
|
||||
const val COL_CREATED_AT = "created_at"
|
||||
|
||||
// ───── Индексы ─────
|
||||
const val IDX_WM_UNIQUE = "idx_wm_unique"
|
||||
const val IDX_WM_CONV = "idx_wm_conv"
|
||||
|
||||
private val v1Ddl = """
|
||||
CREATE TABLE IF NOT EXISTS $TABLE_WORKING_MEMORY (
|
||||
$COL_ID TEXT NOT NULL PRIMARY KEY,
|
||||
$COL_CONVERSATION_ID TEXT NOT NULL,
|
||||
$COL_ORDER_IDX INTEGER NOT NULL,
|
||||
$COL_SOURCE_MESSAGE_ID TEXT,
|
||||
$COL_KIND TEXT NOT NULL,
|
||||
$COL_PAYLOAD_JSON TEXT NOT NULL,
|
||||
$COL_CREATED_AT INTEGER NOT NULL
|
||||
);
|
||||
""".trimIndent()
|
||||
|
||||
private val v1IndexesDdl = """
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS $IDX_WM_UNIQUE
|
||||
ON $TABLE_WORKING_MEMORY($COL_CONVERSATION_ID, $COL_ORDER_IDX);
|
||||
CREATE INDEX IF NOT EXISTS $IDX_WM_CONV
|
||||
ON $TABLE_WORKING_MEMORY($COL_CONVERSATION_ID, $COL_ORDER_IDX);
|
||||
""".trimIndent()
|
||||
|
||||
/**
|
||||
* Прогоняет миграцию схемы до [CURRENT_VERSION] на пустой или существующей БД.
|
||||
*
|
||||
* Версия хранится в `PRAGMA user_version` (стандартный SQLite-механизм,
|
||||
* 32-bit int в заголовке БД — без своей таблицы). Каждая миграция —
|
||||
* блок DDL под номером `fromV+1`, выполняется в транзакции. Если миграция
|
||||
* упадёт посередине — `ROLLBACK` оставит БД на предыдущей версии.
|
||||
*
|
||||
* Идемпотентен: повторный вызов на уже мигрированной БД — no-op.
|
||||
*/
|
||||
fun migrate(conn: SQLiteConnection) {
|
||||
val current = readUserVersion(conn)
|
||||
if (current >= CURRENT_VERSION) return
|
||||
|
||||
conn.exec("BEGIN")
|
||||
try {
|
||||
if (current < 1) {
|
||||
conn.exec(v1Ddl)
|
||||
conn.exec(v1IndexesDdl)
|
||||
}
|
||||
// future: if (current < 2) { conn.exec(v2Ddl) }
|
||||
writeUserVersion(conn, CURRENT_VERSION)
|
||||
conn.exec("COMMIT")
|
||||
} catch (t: Throwable) {
|
||||
runCatching { conn.exec("ROLLBACK") }
|
||||
throw t
|
||||
}
|
||||
}
|
||||
|
||||
private fun readUserVersion(conn: SQLiteConnection): Int {
|
||||
conn.prepare("PRAGMA user_version").use { stmt ->
|
||||
stmt.executeQuery().use { rs ->
|
||||
if (rs.next()) return rs.getLong(0)?.toInt() ?: 0
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
private fun writeUserVersion(conn: SQLiteConnection, version: Int) {
|
||||
// SQLite PRAGMA с literal-аргументом нельзя параметризовать через `?`,
|
||||
// поэтому собираем SQL строкой (значение контролируемое, не user input).
|
||||
conn.exec("PRAGMA user_version = $version")
|
||||
}
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
package pw.binom.agentik.context.ksqlite
|
||||
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import pw.binom.agentik.context.WorkingMemoryEntry
|
||||
import pw.binom.agentik.journal.Content
|
||||
import pw.binom.db.ksqlite.SQLiteConnection
|
||||
import kotlin.test.AfterTest
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* Тесты для [KsqliteContextStore] — точная копия
|
||||
* `KsqliteWorkingMemoryStoreTest` из `:storage-ksqlite`, с переименованием
|
||||
* типов (`WorkingMemoryStore` → `ContextStore`) и обновлённым пакетом для
|
||||
* `Content` (`pw.binom.agentik.journal` — новый canonical, но структура
|
||||
* та же).
|
||||
*
|
||||
* Тестовая фикстура: in-memory SQLiteConnection, [Schema.migrate] в @BeforeTest,
|
||||
* `KsqliteContextStore(conn)` + ручной close в @AfterTest. Никакой внешней
|
||||
* зависимости от `KsqliteStores` из `:storage-ksqlite` — этот модуль
|
||||
* автономный.
|
||||
*/
|
||||
class KsqliteContextStoreTest {
|
||||
|
||||
private lateinit var conn: SQLiteConnection
|
||||
private lateinit var store: KsqliteContextStore
|
||||
|
||||
@BeforeTest
|
||||
fun setup() {
|
||||
conn = SQLiteConnection.memory("ctx-${kotlin.random.Random.nextLong()}")
|
||||
Schema.migrate(conn)
|
||||
store = KsqliteContextStore(conn)
|
||||
}
|
||||
|
||||
@AfterTest
|
||||
fun tearDown() {
|
||||
store.close()
|
||||
conn.close()
|
||||
}
|
||||
|
||||
private fun userMsg(content: String, srcId: String = "m-${content.hashCode()}"): WorkingMemoryEntry.User =
|
||||
WorkingMemoryEntry.User(sourceMessageId = srcId, content = listOf(Content.Text(content)))
|
||||
|
||||
private fun asstMsg(content: String): WorkingMemoryEntry.Assistant =
|
||||
WorkingMemoryEntry.Assistant(sourceMessageId = "m-${content.hashCode()}", content = listOf(Content.Text(content)))
|
||||
|
||||
@Test
|
||||
fun testAppendAndListReturnsInOrder() = runTest {
|
||||
val t = Instant.parse("2026-09-15T10:00:00Z")
|
||||
store.append("c1", userMsg("first"), t)
|
||||
store.append("c1", asstMsg("reply"), t)
|
||||
val list = store.list("c1")
|
||||
assertEquals(2, list.size)
|
||||
assertEquals(1L, list[0].orderIdx)
|
||||
assertEquals(2L, list[1].orderIdx)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testListIsolatesConversations() = runTest {
|
||||
val t = Instant.parse("2026-09-15T10:00:00Z")
|
||||
store.append("c1", userMsg("c1-msg"), t)
|
||||
store.append("c2", userMsg("c2-msg"), t)
|
||||
assertEquals(1, store.list("c1").size)
|
||||
assertEquals(1, store.list("c2").size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testClearRemovesAllForConversation() = runTest {
|
||||
val t = Instant.parse("2026-09-15T10:00:00Z")
|
||||
store.append("c1", userMsg("a"), t)
|
||||
store.append("c1", userMsg("b"), t)
|
||||
store.clear("c1")
|
||||
assertEquals(emptyList(), store.list("c1"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCompactDeletesAndInsertsSummary() = runTest {
|
||||
val t = Instant.parse("2026-09-15T10:00:00Z")
|
||||
store.append("c1", userMsg("a"), t)
|
||||
store.append("c1", userMsg("b"), t)
|
||||
store.append("c1", userMsg("c"), t)
|
||||
// dropFromOrderIdx=2: удаляет idx=2 и idx=3 (b и c), остаётся idx=1 (a).
|
||||
// Summary встаёт на idx=2 (= max(remaining)+1). Возвращает newMax=2.
|
||||
val newMax = store.compact(dropFromOrderIdx = 2, conversationId = "c1", summaryText = "summary")
|
||||
assertEquals(2L, newMax)
|
||||
val remaining = store.list("c1")
|
||||
assertEquals(2, remaining.size)
|
||||
assertEquals(1L, remaining[0].orderIdx)
|
||||
assertEquals(2L, remaining[1].orderIdx)
|
||||
assertTrue(remaining[1].entry is WorkingMemoryEntry.Summary)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCompactWithoutSummaryKeepsTailBelow() = runTest {
|
||||
val t = Instant.parse("2026-09-15T10:00:00Z")
|
||||
store.append("c1", userMsg("a"), t)
|
||||
// dropFromOrderIdx=2: удаляет idx >= 2, остаётся idx=1.
|
||||
val newMax = store.compact(dropFromOrderIdx = 2, conversationId = "c1", summaryText = null)
|
||||
assertEquals(1L, newMax)
|
||||
val remaining = store.list("c1")
|
||||
assertEquals(1, remaining.size)
|
||||
assertEquals(1L, remaining[0].orderIdx)
|
||||
}
|
||||
}
|
||||
@@ -118,7 +118,7 @@ Main.kt
|
||||
- `compact(dropFromOrderIdx, conversationId)` — v1: DELETE rows ≥ order_idx,
|
||||
summarization-вставка отложена (нужен дизайн-проработка).
|
||||
|
||||
**`MessageStore`** — append-only аудит. На каждый ход дописываются
|
||||
**`JournalStore`** — append-only аудит. На каждый ход дописываются
|
||||
`UserMessage`, `AssistantMessage`, `ToolCall`, `ToolResult`, `Error`. Никаких
|
||||
update/delete кроме каскада из `ConversationStore.delete`.
|
||||
|
||||
|
||||
+2
-2
@@ -175,7 +175,7 @@ fun main() {
|
||||
|
||||
**Ошибки хода персистятся.** Если ход провалился (LLM/движок недоступны — например, HTTP 400 от endpoint'а), `ChatConversation.failTurn` пишет терминальную запись `MessageRecord.Error` в audit и эмитит `Event.Error` + `Event.End`. Благодаря audit-записи ошибка видна не только подписчику live-SSE, но и клиенту, который делает backfill через `getMessages` (polling/переподключение): в истории будет `Message.Error(id, message, code?)`, а для этого user-сообщения не будет `AssistantMessage`. При ошибке стрима живой `LiteConversation` сбрасывается — следующий `send` пересоберёт его из `working_memory`. В working_memory `Error` не пишется (модель не должна видеть ошибки прошлых ходов).
|
||||
|
||||
### `MessageStore`
|
||||
### `JournalStore`
|
||||
|
||||
```kotlin
|
||||
suspend fun append(record: MessageRecord)
|
||||
@@ -183,7 +183,7 @@ suspend fun list(conversationId: String, after: Instant, offset: Int, limit: Int
|
||||
suspend fun listAll(conversationId: String): List<MessageRecord>
|
||||
```
|
||||
|
||||
### `WorkingMemoryStore`
|
||||
### `ContextStore`
|
||||
|
||||
```kotlin
|
||||
suspend fun append(conversationId: String, entry: WorkingMemoryEntry, now: Instant)
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
# `:event-store` — bounded-tail event log (KMP)
|
||||
|
||||
## Что это
|
||||
|
||||
Двухуровневое хранилище событий агента. Этот модуль — **короткий
|
||||
bounded tail** для live-SSE и недавнего replay. Полный audit log
|
||||
живёт в `:message-store-api` (никогда не эвиктится, source of truth).
|
||||
|
||||
Три принципа:
|
||||
|
||||
1. **Tail управляет TTL сам.** Никаких `prune`/`cleanup` методов наружу —
|
||||
implementation решает, когда выкинуть старый event. Caller'ы не
|
||||
могут забыть cleanup.
|
||||
2. **Catchup + live в одном Flow.** `events(after)` сначала отдаёт
|
||||
буферизованный диапазон, потом переключается на live tail — клиент
|
||||
не должен знать, где у него "разрыв".
|
||||
3. **Read-only контракт для consumer'ов.** Запись через
|
||||
[MutableEventStore], чтение через [EventStore]. Compile-time
|
||||
гарантия что observer не сможет писать в store.
|
||||
|
||||
## Где используется
|
||||
|
||||
- `:standalone` ChatAgent — append через `MutableOutboxStore` (заменяет
|
||||
текущий `agentEvents: MutableSharedFlow` + `persistAgentEvent`).
|
||||
- `:server` Routes.kt — `/events/all` SSE endpoint читает через
|
||||
`EventStore.events(after)`.
|
||||
- Будущий `:android-agent` core — same интерфейс для локального
|
||||
bounded tail без dedicated server connection.
|
||||
|
||||
## Архитектура
|
||||
|
||||
```
|
||||
┌─ :event-store (этот модуль) ────────────────────────┐
|
||||
│ Bounded tail с auto-TTL: │
|
||||
│ • append(event) ← producer │
|
||||
│ • events(after): Flow ← consumer │
|
||||
│ • earliestEventDate() для gap detection │
|
||||
│ TTL/cap eviction — внутри impl │
|
||||
└───────────────────────────────────────────────────┘
|
||||
▲ gap detected
|
||||
│
|
||||
┌─ :message-store-api (полный audit log) ───────────┐
|
||||
│ MessageStore: query(after, before, limit) │
|
||||
│ Никогда не эвиктится. Source of truth. │
|
||||
└───────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
**Reconnect pattern** (caller делает):
|
||||
|
||||
```kotlin
|
||||
val earliest = eventStore.earliestEventDate()
|
||||
if (client.lastSeen < earliest) {
|
||||
// gap: догоняем через :message-store-api
|
||||
val gap = messageStore.query(after = client.lastSeen, before = earliest)
|
||||
applyAll(gap)
|
||||
client.lastSeen = gap.last().createdAt
|
||||
}
|
||||
eventStore.events(after = client.lastSeen).collect { apply(it) }
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### `OutboxStore` (read-only, для consumer'ов)
|
||||
|
||||
```kotlin
|
||||
interface EventStore : AutoCloseable {
|
||||
fun events(after: Instant?): Flow<CommonEvent>
|
||||
fun conversationEvents(
|
||||
after: Instant?,
|
||||
conversationId: String? = null, // null = все диалоги
|
||||
): Flow<CommonEvent.Conversation>
|
||||
fun agentEvents(after: Instant?): Flow<CommonEvent.Agent>
|
||||
suspend fun earliestEventDate(): Instant // non-null: now() для пустого буфера
|
||||
override fun close()
|
||||
}
|
||||
```
|
||||
|
||||
**Семантика фильтров**:
|
||||
- `conversationEvents(null)` — все диалоги.
|
||||
- `conversationEvents("c-123")` — один конкретный диалог.
|
||||
- `agentEvents(...)` — только lifecycle (Created/Deleted/Renamed).
|
||||
|
||||
Все три возвращают **типизированные** subtype'ы [CommonEvent], так что
|
||||
caller'у не нужно `.filterIsInstance` на клиентской стороне.
|
||||
|
||||
### `MutableEventStore : EventStore` (для producer'ов)
|
||||
|
||||
```kotlin
|
||||
interface MutableEventStore : EventStore {
|
||||
suspend fun append(event: CommonEvent)
|
||||
}
|
||||
```
|
||||
|
||||
**Append НЕ идемпотентен**: [CommonEvent] не имеет уникального id,
|
||||
retry даст дубликат. Для exactly-once — dedup через
|
||||
`:message-store-api` (там есть монотонный `id`).
|
||||
|
||||
**Silently evicted**: implementation может выкинуть event сразу после
|
||||
append по TTL/cap. Producer не должен полагаться на то, что event
|
||||
дойдёт до клиента, если он вне retention window.
|
||||
|
||||
## Когда использовать какой интерфейс
|
||||
|
||||
| Caller | Method |
|
||||
|---|---|
|
||||
| Server `/events/all` SSE (mixed) | `events(after)` |
|
||||
| Server `/conversations/{id}/events` SSE | `conversationEvents(after, conversationId)` |
|
||||
| Server `/agent/events` SSE (lifecycle only) | `agentEvents(after)` |
|
||||
| Admin dashboard (lifecycle) | `agentEvents(after)` |
|
||||
| Parent orchestrator (mixed) | `events(after)` |
|
||||
| Тесты | `events(after)` + projection через фильтр |
|
||||
|
||||
## Как добавить новый implementation
|
||||
|
||||
1. Создать класс с конструктором и lifecycle (`close()` обязан
|
||||
освободить ресурсы).
|
||||
2. Реализовать минимум: append (с TTL eviction), events (Flow с
|
||||
catchup + live), earliestEventDate (non-null Instant, now() если
|
||||
буфер пуст).
|
||||
3. Для persistent impl: SQL/ksqlite таблица с индексом по date,
|
||||
вставка = `INSERT OR IGNORE` для дедупликации на уровне БД
|
||||
(если в схеме будет id).
|
||||
|
||||
## Текущее состояние
|
||||
|
||||
- ✅ Interface дизайн (`OutboxStore` + `MutableOutboxStore`)
|
||||
- ✅ KMP build (jvm + linuxX64 + mingwX64)
|
||||
- ⏳ Нет implementations (next: `InMemoryEventStore` для тестов)
|
||||
- ⏳ Не интегрирован в `:standalone`/`:server`
|
||||
|
||||
## Зависимости
|
||||
|
||||
- `:proto` (api) — тип `CommonEvent` (3 AgentEvent + 9 Conversation.Event вариантов).
|
||||
- `kotlinx-coroutines-core` (api) — `Flow`.
|
||||
|
||||
Никаких `kotlinx-serialization`, `kotlin-logging`, platform-specific
|
||||
зависимостей — этот модуль намеренно minimal.
|
||||
@@ -0,0 +1,32 @@
|
||||
plugins {
|
||||
alias(libs.plugins.kotlin.multiplatform)
|
||||
}
|
||||
|
||||
// KMP-интерфейс bounded-tail event log'а. Implementation-specific TTL/cap
|
||||
// eviction — caller's responsibility НЕ вызывать cleanup() (метод не существует).
|
||||
//
|
||||
// Тип [AllEvent] из :proto — typed envelope (3 AgentEvent + 9 Conversation.Event
|
||||
// вариантов). :event-store отвечает за bounded-tail с auto-TTL, но не за
|
||||
// сериализацию envelope'а — это делает :proto (уже @Serializable).
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(21)
|
||||
|
||||
jvm()
|
||||
macosX64()
|
||||
macosArm64()
|
||||
linuxX64()
|
||||
linuxArm64()
|
||||
mingwX64()
|
||||
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
api(project(":proto"))
|
||||
api(libs.kotlinx.coroutines.core)
|
||||
}
|
||||
commonTest.dependencies {
|
||||
implementation(kotlin("test"))
|
||||
implementation(libs.kotlinx.coroutines.test)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,6 @@ plugins {
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(21)
|
||||
|
||||
// Зеркалит набор :proto / :server / :memory-api — KMP-модуль с интерфейсами
|
||||
// хранилища и разговорной истории, без платформенного IO. Конкретные
|
||||
// реализации (sqlite, in-memory, android) живут в отдельных модулях.
|
||||
jvm()
|
||||
macosX64()
|
||||
macosArm64()
|
||||
+4
-9
@@ -1,17 +1,12 @@
|
||||
package pw.binom.agentik.storage
|
||||
package pw.binom.agentik.journal
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
/**
|
||||
* Часть контента сообщения на уровне хранилища.
|
||||
*
|
||||
* Намеренно НЕ зависит от [pw.binom.agentik.proto.Content] — маппинг
|
||||
* `:proto.Content ↔ Content` живёт в `Mapping.kt`. Структурно типы
|
||||
* идентичны, но даёт возможность заменить transport-протокол без миграции
|
||||
* таблиц.
|
||||
*
|
||||
* Image сериализуется в JSON через base64 (стандарт для kotlinx-serialization).
|
||||
* Часть контента сообщения на уровне хранилища. Намеренно НЕ зависит от
|
||||
* `pw.binom.agentik.proto.Content` — маппинг `:proto.Content ↔ Content` живёт
|
||||
* в `Mapping.kt` storage impl'ов.
|
||||
*/
|
||||
@Serializable
|
||||
sealed interface Content {
|
||||
@@ -0,0 +1,41 @@
|
||||
package pw.binom.agentik.journal
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* Append-only audit log сообщений — read-only представление.
|
||||
*
|
||||
* Producer-операция [MutableJournalStore.append] находится на
|
||||
* [MutableJournalStore] — этот интерфейс только для чтения, чтобы
|
||||
* consumer'ы физически не могли писать в audit log.
|
||||
*
|
||||
* Никаких обновлений, никакого удаления (кроме каскадного вместе
|
||||
* с ConversationStore.delete).
|
||||
*/
|
||||
interface JournalStore : AutoCloseable {
|
||||
|
||||
suspend fun list(conversationId: String, after: Instant, offset: Int, limit: Int): List<MessageRecord>
|
||||
|
||||
/**
|
||||
* Cold-flow paging через [list]. Default-реализация делает N+1 round-trip
|
||||
* (по странице через `list()` пока не получит короткую страницу). Для
|
||||
* in-memory backend'ов это OK; remote/SQLite impl'ы могут override'нуть
|
||||
* на `Channel` / cursor-батчинг, чтобы избежать per-page round-trip.
|
||||
*/
|
||||
fun listFlow(conversationId: String, after: Instant, pageSize: Int = PAGE_SIZE): Flow<MessageRecord> = flow {
|
||||
var offset = 0
|
||||
while (true) {
|
||||
val page = list(conversationId, after, offset, pageSize)
|
||||
if (page.isEmpty()) return@flow
|
||||
for (rec in page) emit(rec)
|
||||
if (page.size < pageSize) return@flow
|
||||
offset += page.size
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val PAGE_SIZE = 100
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
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,
|
||||
)
|
||||
@@ -0,0 +1,20 @@
|
||||
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,
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package pw.binom.agentik.journal
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* Запись в таблице `message` (append-only audit).
|
||||
*/
|
||||
@Serializable
|
||||
sealed interface MessageRecord {
|
||||
val id: String
|
||||
val conversationId: String
|
||||
val createdAt: Instant
|
||||
|
||||
@Serializable
|
||||
sealed interface Body : MessageRecord {
|
||||
val content: List<Content>
|
||||
}
|
||||
|
||||
@Serializable
|
||||
@SerialName("user")
|
||||
data class UserMessage(
|
||||
override val id: String,
|
||||
override val conversationId: String,
|
||||
override val content: List<Content>,
|
||||
override val createdAt: Instant,
|
||||
val context: MessageContext? = null,
|
||||
) : Body
|
||||
|
||||
@Serializable
|
||||
@SerialName("assistant")
|
||||
data class AssistantMessage(
|
||||
override val id: String,
|
||||
override val conversationId: String,
|
||||
override val content: List<Content>,
|
||||
override val createdAt: Instant,
|
||||
val tokens: TurnTokens? = null,
|
||||
) : Body
|
||||
|
||||
@Serializable
|
||||
@SerialName("tool_call")
|
||||
data class ToolCall(
|
||||
override val id: String,
|
||||
override val conversationId: String,
|
||||
val toolName: String,
|
||||
val toolTitle: String?,
|
||||
val toolArgsJson: String,
|
||||
override val createdAt: Instant,
|
||||
) : MessageRecord
|
||||
|
||||
@Serializable
|
||||
@SerialName("tool_result")
|
||||
data class ToolResult(
|
||||
override val id: String,
|
||||
override val conversationId: String,
|
||||
val toolCallId: String,
|
||||
val result: String?,
|
||||
override val createdAt: Instant,
|
||||
) : MessageRecord
|
||||
|
||||
@Serializable
|
||||
@SerialName("error")
|
||||
data class Error(
|
||||
override val id: String,
|
||||
override val conversationId: String,
|
||||
val message: String,
|
||||
val code: String?,
|
||||
override val createdAt: Instant,
|
||||
) : MessageRecord
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package pw.binom.agentik.journal
|
||||
|
||||
/**
|
||||
* Mutable вариант [JournalStore] — добавляет producer-операцию [append].
|
||||
*
|
||||
* Этот интерфейс предназначен **только для producer'ов** (ChatAgent,
|
||||
* ConversationLoop, ToolDispatcher, sub-agents, A2A-bridge).
|
||||
* Consumer'ы (DebugRoutes, admin dashboards, parent agents) должны
|
||||
* принимать **read-only** [JournalStore] — тогда невозможно случайно
|
||||
* записать в audit log из observer'а.
|
||||
*
|
||||
* **Append семантика**: см. KDoc [MessageStore.append][JournalStore] —
|
||||
* на этом интерфейсе (не дублируем).
|
||||
*/
|
||||
interface MutableJournalStore : JournalStore {
|
||||
suspend fun append(record: MessageRecord)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package pw.binom.agentik.journal
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.builtins.ListSerializer
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
private val bodyJson = Json {
|
||||
ignoreUnknownKeys = true
|
||||
encodeDefaults = true
|
||||
explicitNulls = false
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class MessageBodyPayload(
|
||||
val content: List<Content>,
|
||||
@SerialName("context")
|
||||
val context: MessageContext? = null,
|
||||
val tokens: TurnTokens? = null,
|
||||
)
|
||||
|
||||
fun encodeBodyPayload(
|
||||
content: List<Content>,
|
||||
context: MessageContext? = null,
|
||||
tokens: TurnTokens? = null,
|
||||
): String = bodyJson.encodeToString(
|
||||
MessageBodyPayload.serializer(),
|
||||
MessageBodyPayload(content = content, context = context, tokens = tokens),
|
||||
)
|
||||
|
||||
fun decodeBodyPayload(json: String): BodyDecoded = readPayload(json)
|
||||
|
||||
data class BodyDecoded(
|
||||
val content: List<Content>,
|
||||
val context: MessageContext?,
|
||||
val tokens: TurnTokens? = null,
|
||||
)
|
||||
|
||||
private fun readPayload(json: String): BodyDecoded {
|
||||
return try {
|
||||
val p = bodyJson.decodeFromString(MessageBodyPayload.serializer(), json)
|
||||
BodyDecoded(p.content, p.context, p.tokens)
|
||||
} catch (e: kotlinx.serialization.SerializationException) {
|
||||
val arr = bodyJson.decodeFromString(ListSerializer(Content.serializer()), json)
|
||||
BodyDecoded(arr, null, null)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
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" }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
plugins {
|
||||
alias(libs.plugins.kotlin.multiplatform)
|
||||
}
|
||||
|
||||
// KMP-реализация [MutableJournalStore] на `MutableList` + `Mutex` — для
|
||||
// тестов, dev-режима, embedded-сценариев (Android core, CLI, in-process кэш
|
||||
// в клиенте) и как образец для своей реализации.
|
||||
//
|
||||
// `list` фильтрует по `conversationId`+`createdAt>after` и сортирует
|
||||
// по `createdAt ASC`. Paging — поверх отфильтрованного списка.
|
||||
//
|
||||
// Зависимости: только `:journal-api`. Никакого I/O — pure in-memory.
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(21)
|
||||
|
||||
jvm()
|
||||
linuxX64()
|
||||
mingwX64()
|
||||
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
api(project(":journal-api"))
|
||||
}
|
||||
commonTest.dependencies {
|
||||
implementation(kotlin("test"))
|
||||
implementation(libs.kotlinx.coroutines.test)
|
||||
}
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package pw.binom.agentik.journal.inmemory
|
||||
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import pw.binom.agentik.journal.JournalStore
|
||||
import pw.binom.agentik.journal.MessageRecord
|
||||
import pw.binom.agentik.journal.MutableJournalStore
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* Простая in-memory [MutableJournalStore] для тестов, dev-режима и
|
||||
* клиентских in-process кэшей.
|
||||
*
|
||||
* **Thread-safety**: `Mutex` поверх `MutableList<MessageRecord>`. Для
|
||||
* embedded/CLI сценариев достаточно; для hot-path на сервере используйте
|
||||
* [pw.binom.agentik.journal.ksqlite.KsqliteJournalStore].
|
||||
*
|
||||
* **Контракт `list`**: возвращает подмножество с
|
||||
* `conversationId == conversationId && createdAt > after`, отсортированное
|
||||
* по `createdAt ASC`. `offset/limit` — paging поверх отфильтрованного списка.
|
||||
*
|
||||
* **Очистка**: [clear] сбрасывает кэш (например, когда диалог удалён
|
||||
* на сервере). [close] — no-op.
|
||||
*
|
||||
* Типичный кэш-паттерн в клиенте:
|
||||
* ```
|
||||
* val local = InMemoryJournalStore()
|
||||
* val remote = HttpJournalStore(httpClient, baseUrl)
|
||||
* // backfill + кэширование:
|
||||
* remote.listFlow(convId, Instant.DISTANT_PAST).collect { local.append(it) }
|
||||
* // после этого `local.list(convId, after, offset, limit)` отдаёт из кэша.
|
||||
* ```
|
||||
*/
|
||||
class InMemoryJournalStore : MutableJournalStore {
|
||||
|
||||
private val mutex = Mutex()
|
||||
private val records: MutableList<MessageRecord> = mutableListOf()
|
||||
|
||||
override suspend fun append(record: MessageRecord): Unit = mutex.withLock {
|
||||
records.add(record)
|
||||
}
|
||||
|
||||
override suspend fun list(
|
||||
conversationId: String,
|
||||
after: Instant,
|
||||
offset: Int,
|
||||
limit: Int,
|
||||
): List<MessageRecord> = mutex.withLock {
|
||||
records.asSequence()
|
||||
.filter { it.conversationId == conversationId && it.createdAt > after }
|
||||
.sortedBy { it.createdAt }
|
||||
.drop(offset)
|
||||
.take(limit)
|
||||
.toList()
|
||||
}
|
||||
|
||||
/** Сбросить кэш (например, когда диалог удалён). */
|
||||
suspend fun clear(): Unit = mutex.withLock {
|
||||
records.clear()
|
||||
}
|
||||
|
||||
/** Сколько записей сейчас в кэше. Для тестов/диагностики. */
|
||||
suspend fun size(): Int = mutex.withLock { records.size }
|
||||
|
||||
override fun close() {
|
||||
// no-op: lifecycle HttpClient'а — снаружи.
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package pw.binom.agentik.journal.inmemory
|
||||
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import pw.binom.agentik.journal.MessageRecord
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
import kotlin.time.Instant
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class InMemoryJournalStoreTest {
|
||||
|
||||
private fun userMsg(id: String, convId: String, text: String, at: Instant) =
|
||||
MessageRecord.UserMessage(
|
||||
id = id,
|
||||
conversationId = convId,
|
||||
content = listOf(pw.binom.agentik.journal.Content.Text(text)),
|
||||
createdAt = at,
|
||||
)
|
||||
|
||||
@Test
|
||||
fun `append then list returns records sorted by createdAt ASC`() = runTest {
|
||||
val store = InMemoryJournalStore()
|
||||
val t0 = Instant.parse("2026-09-21T10:00:00Z")
|
||||
store.append(userMsg("m1", "c1", "first", t0))
|
||||
store.append(userMsg("m2", "c1", "second", t0 + 1.seconds))
|
||||
store.append(userMsg("m3", "c1", "third", t0 + 2.seconds))
|
||||
|
||||
val all = store.list("c1", Instant.DISTANT_PAST, 0, 100)
|
||||
assertEquals(3, all.size)
|
||||
assertEquals(listOf("m1", "m2", "m3"), all.map { it.id })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `list filters by conversationId`() = runTest {
|
||||
val store = InMemoryJournalStore()
|
||||
val t0 = Instant.parse("2026-09-21T10:00:00Z")
|
||||
store.append(userMsg("m1", "c1", "a", t0))
|
||||
store.append(userMsg("m2", "c2", "b", t0 + 1.seconds))
|
||||
store.append(userMsg("m3", "c1", "c", t0 + 2.seconds))
|
||||
|
||||
assertEquals(2, store.list("c1", Instant.DISTANT_PAST, 0, 100).size)
|
||||
assertEquals(1, store.list("c2", Instant.DISTANT_PAST, 0, 100).size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `list filters by after cursor`() = runTest {
|
||||
val store = InMemoryJournalStore()
|
||||
val t0 = Instant.parse("2026-09-21T10:00:00Z")
|
||||
store.append(userMsg("m1", "c1", "a", t0))
|
||||
store.append(userMsg("m2", "c1", "b", t0 + 10.seconds))
|
||||
store.append(userMsg("m3", "c1", "c", t0 + 20.seconds))
|
||||
|
||||
val afterT0 = store.list("c1", t0, 0, 100)
|
||||
assertEquals(listOf("m2", "m3"), afterT0.map { it.id })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `list applies offset and limit`() = runTest {
|
||||
val store = InMemoryJournalStore()
|
||||
val t0 = Instant.parse("2026-09-21T10:00:00Z")
|
||||
repeat(10) { i -> store.append(userMsg("m$i", "c1", "x", t0 + i.seconds)) }
|
||||
|
||||
val page = store.list("c1", Instant.DISTANT_PAST, offset = 3, limit = 4)
|
||||
assertEquals(listOf("m3", "m4", "m5", "m6"), page.map { it.id })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `clear empties the cache`() = runTest {
|
||||
val store = InMemoryJournalStore()
|
||||
val t0 = Instant.parse("2026-09-21T10:00:00Z")
|
||||
store.append(userMsg("m1", "c1", "x", t0))
|
||||
assertEquals(1, store.size())
|
||||
store.clear()
|
||||
assertEquals(0, store.size())
|
||||
assertTrue(store.list("c1", Instant.DISTANT_PAST, 0, 100).isEmpty())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
plugins {
|
||||
alias(libs.plugins.kotlin.multiplatform)
|
||||
alias(libs.plugins.kotlin.serialization)
|
||||
}
|
||||
|
||||
// KMP-реализация :journal-api (JournalStore / MutableJournalStore) поверх ksqlite.
|
||||
// Минимальная — только таблица `message` для append-only audit log'а.
|
||||
// ConversationStore / ReflectionStore / WorkingMemoryStore живут в своих
|
||||
// собственных ksqlite-модулях.
|
||||
//
|
||||
// Цели сборки — jvm() + linuxX64() + mingwX64(); Apple targets auto-disabled
|
||||
// на Linux (см. KDoc :storage-ksqlite).
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(21)
|
||||
|
||||
jvm()
|
||||
linuxX64()
|
||||
mingwX64()
|
||||
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
implementation("pw.binom.db:ksqlite:0.1.1-SNAPSHOT")
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
|
||||
api(project(":journal-api"))
|
||||
}
|
||||
commonTest.dependencies {
|
||||
implementation(kotlin("test"))
|
||||
implementation(libs.kotlinx.coroutines.test)
|
||||
}
|
||||
}
|
||||
}
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
package pw.binom.agentik.journal.ksqlite
|
||||
|
||||
import kotlinx.serialization.json.Json
|
||||
import pw.binom.agentik.journal.MessageRecord
|
||||
import pw.binom.agentik.journal.MutableJournalStore
|
||||
import pw.binom.db.ksqlite.SQLiteConnection
|
||||
import pw.binom.db.ksqlite.SQLitePreparedStatement
|
||||
import kotlin.time.Instant
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
/**
|
||||
* ksqlite-реализация [MutableJournalStore] (append-only audit log).
|
||||
*
|
||||
* Структура — копия [pw.binom.agentik.storage.ksqlite.KsqliteMessageStore]
|
||||
* из `:storage-ksqlite`, но:
|
||||
* - лежит в собственном модуле `:journal-ksqlite`;
|
||||
* - реализует переименованный [MutableJournalStore] (раньше был
|
||||
* `MutableMessageStore`, теперь главный класс — `JournalStore` /
|
||||
* `MutableJournalStore`); сам тип записи [MessageRecord] не
|
||||
* переименовывался.
|
||||
*
|
||||
* Prepared statements (insert / list / clear) препарируются один раз в
|
||||
* конструкторе и закрываются в [close]. Без этого GC финалайзеры каждого
|
||||
* StmtHolder'а пытаются `sqlite3_finalize` stmt, чей parent connection уже
|
||||
* закрыт → SIGSEGV в `pthread_mutex_lock` (см. [pw.binom.db.ksqlite.StmtHolder]).
|
||||
*
|
||||
* `payloadJson` хранит JSON-сериализованные kind-specific поля. encoding
|
||||
* helpers (`encodeRecord` / `toMessageRecord` / `CallPayload` / ...) лежат
|
||||
* в [MessageCodecs.kt] рядом.
|
||||
*
|
||||
* ВНИМАНИЕ: `:storage-ksqlite/KsqliteMessageStore.kt` остаётся на диске —
|
||||
* это копия, не замена. Не удалять старый файл; миграция consumers'ов —
|
||||
* отдельно.
|
||||
*/
|
||||
class KsqliteJournalStore internal constructor(
|
||||
private val connection: SQLiteConnection,
|
||||
) : MutableJournalStore {
|
||||
|
||||
private val mutex = Mutex()
|
||||
private val json = Json { ignoreUnknownKeys = true }
|
||||
|
||||
private val insertStmt: SQLitePreparedStatement = connection.prepare(
|
||||
"""
|
||||
INSERT INTO ${Schema.TABLE_MESSAGE}
|
||||
(${Schema.COL_ID}, ${Schema.COL_CONVERSATION_ID}, ${Schema.COL_KIND},
|
||||
${Schema.COL_PAYLOAD_JSON}, ${Schema.COL_CREATED_AT})
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
""".trimIndent()
|
||||
)
|
||||
private val listStmt: SQLitePreparedStatement = connection.prepare(
|
||||
"""
|
||||
SELECT ${Schema.COL_ID}, ${Schema.COL_CONVERSATION_ID}, ${Schema.COL_KIND},
|
||||
${Schema.COL_PAYLOAD_JSON}, ${Schema.COL_CREATED_AT}
|
||||
FROM ${Schema.TABLE_MESSAGE}
|
||||
WHERE ${Schema.COL_CONVERSATION_ID} = ?
|
||||
AND ${Schema.COL_CREATED_AT} > ?
|
||||
ORDER BY ${Schema.COL_CREATED_AT} ASC, ${Schema.COL_ID} ASC
|
||||
LIMIT ? OFFSET ?
|
||||
""".trimIndent()
|
||||
)
|
||||
private val clearStmt: SQLitePreparedStatement = connection.prepare(
|
||||
"DELETE FROM ${Schema.TABLE_MESSAGE} WHERE ${Schema.COL_CONVERSATION_ID} = ?"
|
||||
)
|
||||
|
||||
override suspend fun append(record: MessageRecord): Unit = withContext(Dispatchers.Default) {
|
||||
val (kind, payload) = encodeRecord(record)
|
||||
mutex.withLock {
|
||||
insertStmt.reset()
|
||||
insertStmt.clearBindings()
|
||||
insertStmt.bindText(1, record.id)
|
||||
insertStmt.bindText(2, record.conversationId)
|
||||
insertStmt.bindText(3, kind)
|
||||
insertStmt.bindText(4, payload)
|
||||
insertStmt.bindLong(5, record.createdAt.toEpochMilliseconds())
|
||||
insertStmt.executeUpdate()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun list(
|
||||
conversationId: String,
|
||||
after: Instant,
|
||||
offset: Int,
|
||||
limit: Int,
|
||||
): List<MessageRecord> = withContext(Dispatchers.Default) {
|
||||
mutex.withLock {
|
||||
listStmt.reset()
|
||||
listStmt.clearBindings()
|
||||
listStmt.bindText(1, conversationId)
|
||||
listStmt.bindLong(2, after.toEpochMilliseconds())
|
||||
listStmt.bindLong(3, limit.toLong())
|
||||
listStmt.bindLong(4, offset.toLong())
|
||||
val out = mutableListOf<MessageRecord>()
|
||||
listStmt.executeQuery().use { rs ->
|
||||
while (rs.next()) out.add(rs.toMessageRecord(json))
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
internal suspend fun clear(conversationId: String): Unit = withContext(Dispatchers.Default) {
|
||||
mutex.withLock {
|
||||
clearStmt.reset()
|
||||
clearStmt.clearBindings()
|
||||
clearStmt.bindText(1, conversationId)
|
||||
clearStmt.executeUpdate()
|
||||
}
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
insertStmt.close()
|
||||
listStmt.close()
|
||||
clearStmt.close()
|
||||
}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package pw.binom.agentik.journal.ksqlite
|
||||
|
||||
import kotlinx.serialization.json.Json
|
||||
import pw.binom.agentik.journal.MessageRecord
|
||||
import pw.binom.agentik.journal.decodeBodyPayload
|
||||
import pw.binom.agentik.journal.encodeBodyPayload
|
||||
import pw.binom.db.ksqlite.SQLiteResultSet
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* Кодирование [MessageRecord] → пара (kind, payloadJson) для SQLite.
|
||||
*
|
||||
* Копия `MessageCodecs.kt` из `:storage-ksqlite` — `internal` helpers
|
||||
* нельзя переиспользовать между модулями, поэтому в каждом backend свой набор.
|
||||
* Чтобы избежать дрейфа при изменении формата payload'а, оба набора синхронизируются
|
||||
* через эти data class'ы (CallPayload/ResultPayload/ErrorPayload).
|
||||
*/
|
||||
internal fun encodeRecord(record: MessageRecord): Pair<String, String> = when (record) {
|
||||
is MessageRecord.UserMessage -> "user" to encodeBodyPayload(
|
||||
content = record.content,
|
||||
context = record.context,
|
||||
)
|
||||
is MessageRecord.AssistantMessage -> "assistant" to encodeBodyPayload(
|
||||
content = record.content,
|
||||
tokens = record.tokens,
|
||||
)
|
||||
is MessageRecord.ToolCall -> "tool_call" to Json.encodeToString(
|
||||
CallPayload.serializer(),
|
||||
CallPayload(name = record.toolName, title = record.toolTitle, argsJson = record.toolArgsJson),
|
||||
)
|
||||
is MessageRecord.ToolResult -> "tool_result" to Json.encodeToString(
|
||||
ResultPayload.serializer(),
|
||||
ResultPayload(toolCallId = record.toolCallId, result = record.result),
|
||||
)
|
||||
is MessageRecord.Error -> "error" to Json.encodeToString(
|
||||
ErrorPayload.serializer(),
|
||||
ErrorPayload(message = record.message, code = record.code),
|
||||
)
|
||||
}
|
||||
|
||||
internal fun SQLiteResultSet.toMessageRecord(json: Json): MessageRecord {
|
||||
val id = getText(0)!!
|
||||
val convId = getText(1)!!
|
||||
val kind = getText(2)!!
|
||||
val payload = getText(3)!!
|
||||
val createdAt = Instant.fromEpochMilliseconds(getLong(4)!!)
|
||||
return when (kind) {
|
||||
"user" -> {
|
||||
val d = decodeBodyPayload(payload)
|
||||
MessageRecord.UserMessage(id = id, conversationId = convId, content = d.content, createdAt = createdAt, context = d.context)
|
||||
}
|
||||
"assistant" -> {
|
||||
val d = decodeBodyPayload(payload)
|
||||
MessageRecord.AssistantMessage(id = id, conversationId = convId, content = d.content, createdAt = createdAt, tokens = d.tokens)
|
||||
}
|
||||
"tool_call" -> {
|
||||
val p = Json.decodeFromString(CallPayload.serializer(), payload)
|
||||
MessageRecord.ToolCall(id = id, conversationId = convId, toolName = p.name, toolTitle = p.title, toolArgsJson = p.argsJson, createdAt = createdAt)
|
||||
}
|
||||
"tool_result" -> {
|
||||
val p = Json.decodeFromString(ResultPayload.serializer(), payload)
|
||||
MessageRecord.ToolResult(id = id, conversationId = convId, toolCallId = p.toolCallId, result = p.result, createdAt = createdAt)
|
||||
}
|
||||
"error" -> {
|
||||
val p = Json.decodeFromString(ErrorPayload.serializer(), payload)
|
||||
MessageRecord.Error(id = id, conversationId = convId, message = p.message, code = p.code, createdAt = createdAt)
|
||||
}
|
||||
else -> error("Unknown message kind in audit log: $kind")
|
||||
}
|
||||
}
|
||||
|
||||
@kotlinx.serialization.Serializable
|
||||
internal data class CallPayload(val name: String, val title: String?, val argsJson: String)
|
||||
|
||||
@kotlinx.serialization.Serializable
|
||||
internal data class ResultPayload(val toolCallId: String, val result: String?)
|
||||
|
||||
@kotlinx.serialization.Serializable
|
||||
internal data class ErrorPayload(val message: String, val code: String?)
|
||||
@@ -0,0 +1,91 @@
|
||||
package pw.binom.agentik.journal.ksqlite
|
||||
|
||||
import pw.binom.db.ksqlite.SQLiteConnection
|
||||
|
||||
/**
|
||||
* Имена таблиц/колонок/индексов для ksqlite-бэкенда `:journal-api`.
|
||||
*
|
||||
* Минимум — только то, что относится к `message` (append-only audit log).
|
||||
* Остальные таблицы агента (`conversation`, `working_memory`, `reflection`)
|
||||
* живут в других ksqlite-модулях.
|
||||
*
|
||||
* Все DDL/DML в этом модуле должны ссылаться на эти константы — никаких
|
||||
* хардкоженных литералов в `prepare("SELECT ... FROM foo ...")` в store'е.
|
||||
*/
|
||||
internal object Schema {
|
||||
|
||||
/** Версия схемы модуля. Увеличивать при ЛЮБОМ изменении DDL. */
|
||||
const val CURRENT_VERSION: Int = 1
|
||||
|
||||
// ───── Таблица ─────
|
||||
const val TABLE_MESSAGE = "message"
|
||||
|
||||
// ───── Колонки ─────
|
||||
const val COL_ID = "id"
|
||||
const val COL_CONVERSATION_ID = "conversation_id"
|
||||
const val COL_KIND = "kind"
|
||||
const val COL_PAYLOAD_JSON = "payload_json"
|
||||
const val COL_CREATED_AT = "created_at"
|
||||
|
||||
// ───── Индексы ─────
|
||||
const val IDX_MSG_CONV = "idx_msg_conv"
|
||||
|
||||
private val v1Ddl = """
|
||||
CREATE TABLE IF NOT EXISTS $TABLE_MESSAGE (
|
||||
$COL_ID TEXT NOT NULL PRIMARY KEY,
|
||||
$COL_CONVERSATION_ID TEXT NOT NULL,
|
||||
$COL_KIND TEXT NOT NULL,
|
||||
$COL_PAYLOAD_JSON TEXT NOT NULL,
|
||||
$COL_CREATED_AT INTEGER NOT NULL
|
||||
);
|
||||
""".trimIndent()
|
||||
|
||||
private val v1IndexesDdl = """
|
||||
CREATE INDEX IF NOT EXISTS $IDX_MSG_CONV
|
||||
ON $TABLE_MESSAGE($COL_CONVERSATION_ID, $COL_CREATED_AT);
|
||||
""".trimIndent()
|
||||
|
||||
/**
|
||||
* Прогоняет миграцию схемы до [CURRENT_VERSION] на пустой или существующей БД.
|
||||
*
|
||||
* Версия хранится в `PRAGMA user_version` (стандартный SQLite-механизм,
|
||||
* 32-bit int в заголовке БД — без своей таблицы). Каждая миграция —
|
||||
* блок DDL под номером `fromV+1`, выполняется в транзакции. Если миграция
|
||||
* упадёт посередине — `ROLLBACK` оставит БД на предыдущей версии.
|
||||
*
|
||||
* Идемпотентен: повторный вызов на уже мигрированной БД — no-op.
|
||||
*/
|
||||
fun migrate(conn: SQLiteConnection) {
|
||||
val current = readUserVersion(conn)
|
||||
if (current >= CURRENT_VERSION) return
|
||||
|
||||
conn.exec("BEGIN")
|
||||
try {
|
||||
if (current < 1) {
|
||||
conn.exec(v1Ddl)
|
||||
conn.exec(v1IndexesDdl)
|
||||
}
|
||||
// future: if (current < 2) { conn.exec(v2Ddl) }
|
||||
writeUserVersion(conn, CURRENT_VERSION)
|
||||
conn.exec("COMMIT")
|
||||
} catch (t: Throwable) {
|
||||
runCatching { conn.exec("ROLLBACK") }
|
||||
throw t
|
||||
}
|
||||
}
|
||||
|
||||
private fun readUserVersion(conn: SQLiteConnection): Int {
|
||||
conn.prepare("PRAGMA user_version").use { stmt ->
|
||||
stmt.executeQuery().use { rs ->
|
||||
if (rs.next()) return rs.getLong(0)?.toInt() ?: 0
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
private fun writeUserVersion(conn: SQLiteConnection, version: Int) {
|
||||
// SQLite PRAGMA с literal-аргументом нельзя параметризовать через `?`,
|
||||
// поэтому собираем SQL строкой (значение контролируемое, не user input).
|
||||
conn.exec("PRAGMA user_version = $version")
|
||||
}
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
package pw.binom.agentik.journal.ksqlite
|
||||
|
||||
import kotlinx.coroutines.flow.toList
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import pw.binom.agentik.journal.Content
|
||||
import pw.binom.agentik.journal.MessageRecord
|
||||
import pw.binom.agentik.journal.TurnTokens
|
||||
import pw.binom.db.ksqlite.SQLiteConnection
|
||||
import kotlin.test.AfterTest
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* Тесты для [KsqliteJournalStore] — точная копия
|
||||
* `KsqliteMessageStoreTest` из `:storage-ksqlite`, с переименованием типов
|
||||
* (`MessageStore` → `JournalStore`) и автономной фикстурой (in-memory
|
||||
* SQLiteConnection + Schema.migrate).
|
||||
*
|
||||
* Тест `testClearRemovesByConversation` из оригинала использовал
|
||||
* `stores.conversations.delete(...)` (cascade через `KsqliteStores`) — здесь
|
||||
* он заменён на прямой вызов `store.clear(...)`, потому что `:journal-ksqlite`
|
||||
* автономен и не знает про ConversationStore.
|
||||
*/
|
||||
class KsqliteJournalStoreTest {
|
||||
|
||||
private lateinit var conn: SQLiteConnection
|
||||
private lateinit var store: KsqliteJournalStore
|
||||
|
||||
@BeforeTest
|
||||
fun setup() {
|
||||
conn = SQLiteConnection.memory("journal-${kotlin.random.Random.nextLong()}")
|
||||
Schema.migrate(conn)
|
||||
store = KsqliteJournalStore(conn)
|
||||
}
|
||||
|
||||
@AfterTest
|
||||
fun tearDown() {
|
||||
store.close()
|
||||
conn.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAppendUserAndRetrieve() = runTest {
|
||||
store.append(MessageRecord.UserMessage(
|
||||
id = "m1",
|
||||
conversationId = "conv1",
|
||||
content = listOf(Content.Text("hello")),
|
||||
createdAt = Instant.parse("2026-09-15T10:01:00Z"),
|
||||
context = null,
|
||||
))
|
||||
val list = store.listFlow("conv1", Instant.DISTANT_PAST).toList()
|
||||
assertEquals(1, list.size)
|
||||
val msg = list[0]
|
||||
assertEquals("m1", msg.id)
|
||||
assertEquals(MessageRecord.UserMessage::class, msg::class)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAppendAssistantWithTokens() = runTest {
|
||||
store.append(MessageRecord.AssistantMessage(
|
||||
id = "m1",
|
||||
conversationId = "conv1",
|
||||
content = listOf(Content.Text("hi")),
|
||||
createdAt = Instant.parse("2026-09-15T10:01:00Z"),
|
||||
tokens = TurnTokens(input = 50, output = 30),
|
||||
))
|
||||
val list = store.listFlow("conv1", Instant.DISTANT_PAST).toList()
|
||||
assertEquals(1, list.size)
|
||||
val msg = list[0] as MessageRecord.AssistantMessage
|
||||
assertEquals(TurnTokens(input = 50, output = 30), msg.tokens)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testListAfterFiltersByTimestamp() = runTest {
|
||||
val t1 = Instant.parse("2026-09-15T10:01:00Z")
|
||||
val t2 = Instant.parse("2026-09-15T10:02:00Z")
|
||||
val t3 = Instant.parse("2026-09-15T10:03:00Z")
|
||||
store.append(MessageRecord.UserMessage("m1", "conv1", listOf(Content.Text("a")), t1, null))
|
||||
store.append(MessageRecord.UserMessage("m2", "conv1", listOf(Content.Text("b")), t2, null))
|
||||
store.append(MessageRecord.UserMessage("m3", "conv1", listOf(Content.Text("c")), t3, null))
|
||||
|
||||
val after = store.list("conv1", after = t1, offset = 0, limit = 10)
|
||||
assertEquals(2, after.size)
|
||||
assertEquals(listOf("m2", "m3"), after.map { it.id })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testListFlowReturnsAllInOrder() = runTest {
|
||||
val t = Instant.parse("2026-09-15T10:00:00Z")
|
||||
for (i in 1..3) store.append(
|
||||
MessageRecord.UserMessage("m$i", "conv1", listOf(Content.Text("x$i")), t + kotlin.time.Duration.parse("PT${i}S"), null)
|
||||
)
|
||||
assertEquals(listOf("m1", "m2", "m3"), store.listFlow("conv1", Instant.DISTANT_PAST).toList().map { it.id })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testClearRemovesByConversation() = runTest {
|
||||
store.append(MessageRecord.UserMessage("m1", "conv1", listOf(Content.Text("a")), Instant.parse("2026-09-15T10:00:00Z"), null))
|
||||
store.append(MessageRecord.UserMessage("m2", "conv2", listOf(Content.Text("b")), Instant.parse("2026-09-15T10:00:00Z"), null))
|
||||
store.clear("conv1")
|
||||
assertEquals(emptyList(), store.listFlow("conv1", Instant.DISTANT_PAST).toList())
|
||||
assertEquals(1, store.listFlow("conv2", Instant.DISTANT_PAST).toList().size)
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,9 @@ kotlin {
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
api(project(":memory-api"))
|
||||
api(project(":storage-core"))
|
||||
api(project(":journal-api"))
|
||||
api(project(":reflection-api"))
|
||||
api(project(":context-api"))
|
||||
api(project(":skills"))
|
||||
api(libs.litert.api)
|
||||
implementation(libs.kotlinx.coroutines.core)
|
||||
|
||||
@@ -10,7 +10,7 @@ import pw.binom.agentik.memory.MemoryStore
|
||||
import pw.binom.agentik.memory.MemoryStoreEvent
|
||||
import pw.binom.agentik.memory.NewMemoryNote
|
||||
import pw.binom.agentik.memory.ReviewedTurn
|
||||
import pw.binom.agentik.storage.Ids
|
||||
import pw.binom.agentik.journal.Ids
|
||||
import pw.binom.litert.LiteLlm
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.Instant
|
||||
|
||||
@@ -5,8 +5,8 @@ import kotlinx.coroutines.withContext
|
||||
import pw.binom.agentik.memory.ConversationTurn
|
||||
import pw.binom.litert.LiteConversationConfig
|
||||
import pw.binom.litert.LiteLlm
|
||||
import pw.binom.agentik.storage.Ids
|
||||
import pw.binom.agentik.storage.Reflection
|
||||
import pw.binom.agentik.journal.Ids
|
||||
import pw.binom.agentik.reflection.Reflection
|
||||
import kotlin.time.Clock
|
||||
|
||||
/**
|
||||
@@ -57,7 +57,7 @@ class LlmReflector(
|
||||
val parsed = ReflectionParser.parse(raw)
|
||||
?: return@withContext null
|
||||
Reflection(
|
||||
id = Ids.reflection(),
|
||||
id = pw.binom.agentik.reflection.Ids.new(),
|
||||
conversationId = null, // будет проставлен caller'ом ChatConversation
|
||||
createdAt = clock.now(),
|
||||
turnsAnalyzed = turns.size,
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
plugins {
|
||||
alias(libs.plugins.kotlin.multiplatform)
|
||||
alias(libs.plugins.kotlin.serialization)
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(21)
|
||||
jvm()
|
||||
macosX64()
|
||||
macosArm64()
|
||||
iosX64()
|
||||
iosArm64()
|
||||
iosSimulatorArm64()
|
||||
linuxX64()
|
||||
linuxArm64()
|
||||
mingwX64()
|
||||
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
api(libs.kotlinx.coroutines.core)
|
||||
api(libs.kotlinx.serialization.core)
|
||||
api(libs.kotlinx.serialization.json)
|
||||
// typealias-обёртки указывают на :journal-api — без него
|
||||
// компиляция падает на Unresolved reference 'journal'.
|
||||
api(project(":journal-api"))
|
||||
}
|
||||
commonTest.dependencies {
|
||||
implementation(kotlin("test"))
|
||||
implementation(libs.kotlinx.coroutines.test)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
plugins {
|
||||
alias(libs.plugins.kotlin.multiplatform)
|
||||
// Нужен для @Serializable на AgentEvent/CommonEvent/Event — все
|
||||
// три типа теперь живут в :outbox-api (см. миграцию из :proto).
|
||||
alias(libs.plugins.kotlin.serialization)
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(21)
|
||||
|
||||
jvm()
|
||||
macosX64()
|
||||
macosArm64()
|
||||
iosX64()
|
||||
iosArm64()
|
||||
iosSimulatorArm64()
|
||||
linuxX64()
|
||||
linuxArm64()
|
||||
mingwX64()
|
||||
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
// :proto больше не нужен — AgentEvent/CommonEvent/Event перенесены
|
||||
// сюда, и они self-contained (Event ссылается только на kotlinx-serialization).
|
||||
api(libs.kotlinx.coroutines.core)
|
||||
api(libs.kotlinx.serialization.core)
|
||||
api(libs.kotlinx.serialization.json)
|
||||
}
|
||||
commonTest.dependencies {
|
||||
implementation(kotlin("test"))
|
||||
implementation(libs.kotlinx.coroutines.test)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package pw.binom.agentik.outbox
|
||||
|
||||
/**
|
||||
* Mutable вариант [OutboxStore] — добавляет producer-операцию [append].
|
||||
*
|
||||
* Этот интерфейс предназначен **только для producer'ов** (ChatAgent,
|
||||
* sub-agents, A2A-bridge). Consumer'ы (server SSE endpoints, admin
|
||||
* dashboards, parent agents) должны принимать **read-only** [OutboxStore]
|
||||
* — тогда невозможно случайно писать в store из observer'а.
|
||||
*
|
||||
* Типичное использование:
|
||||
* ```
|
||||
* // Producer
|
||||
* class ChatAgent(private val events: MutableEventStore) {
|
||||
* suspend fun doSomething() {
|
||||
* events.append(CommonEvent.Agent(date = now, event = AgentEvent.Created(...)))
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* // Consumer
|
||||
* class EventStreamEndpoint(private val events: EventStore) {
|
||||
* fun stream() = events.events(after = null)
|
||||
* // Ошибка компиляции если раскомментировать:
|
||||
* // events.append(...) // ← нельзя, MutableEventStore нет в типе
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* **Append НЕ идемпотентен**: [CommonEvent] не имеет уникального id,
|
||||
* поэтому retry с тем же logical event (например, после network failure
|
||||
* между producer и store) приведёт к дубликату в tail'е. Это OK для
|
||||
* use case'a bounded-tail — клиент, делающий catchup через [events](after),
|
||||
* получит свой диапазон ровно один раз при подключении, а последующие
|
||||
* retry producer'а просто насытят tail повторами, не задевая уже
|
||||
* обработанные. Для гарантированной exactly-once — dedup через
|
||||
* [message-store] (там есть монотонный `id`).
|
||||
*
|
||||
* **Silently evicted**: implementation может выкинуть этот event сразу
|
||||
* после append (TTL/cap) без уведомления producer'а. Producer **не
|
||||
* должен** полагаться на то, что event дойдёт до клиента, если он
|
||||
* вне retention window.
|
||||
*/
|
||||
interface MutableOutboxStore : OutboxStore {
|
||||
/**
|
||||
* Положить event в log.
|
||||
*
|
||||
* - **Не идемпотентно** — см. KDoc интерфейса.
|
||||
* - **Suspend** для KMP I/O impl'ов (SQLite через JNI).
|
||||
*/
|
||||
suspend fun append(event: CommonEvent)
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package pw.binom.agentik.outbox
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.filter
|
||||
import kotlinx.coroutines.flow.filterIsInstance
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* Bounded-tail event log с автоматическим управлением TTL.
|
||||
*
|
||||
* **Архитектура двухуровневого хранилища событий**:
|
||||
* 1. **Этот store** = короткий bounded tail (live SSE + недавний replay).
|
||||
* События автоматически эвиктятся по TTL/cap (implementation-defined).
|
||||
* 2. **Message store (`:message-store-api`)** = полный audit log, никогда не
|
||||
* эвиктится. Source of truth для всего прошлого.
|
||||
*
|
||||
* **Паттерн reconnect** (caller'ы):
|
||||
* ```
|
||||
* val earliest = store.earliestEventDate()
|
||||
* if (client.lastSeen < earliest) {
|
||||
* // gap обнаружен — идём в message store за прошлым
|
||||
* val gap = messageStore.query(after = client.lastSeen, before = earliest)
|
||||
* applyAll(gap)
|
||||
* }
|
||||
* store.events(after = client.lastSeen).collect { apply(it) }
|
||||
* ```
|
||||
*
|
||||
* **Нет delete/cleanup методов** — TTL/cap eviction полностью на стороне
|
||||
* implementation. Это:
|
||||
* - Убирает single source of truth дублирование (caller не может забыть cleanup).
|
||||
* - Позволяет impl выбирать retention strategy (TTL, size cap, sliding window).
|
||||
* - Сохраняет контракт clean: интерфейс только о put/get.
|
||||
*
|
||||
* **Read-only**: этот интерфейс предоставляет только read-операции.
|
||||
* Для записи см. [MutableOutboxStore].
|
||||
*
|
||||
* **Подписки нереентрантные**: каждый вызов [events] создаёт **новую
|
||||
* подписку** (cold Flow). Один [events] НЕ видит события, добавленные до
|
||||
* его вызова, если [after] == null. Если нужен catchup — передавайте
|
||||
* `after = lastSeenDate` явно.
|
||||
*
|
||||
* **Multi-consumer**: разные [events] подписки видят одно и то же live
|
||||
* tail. Каждая подписка — независимая projection.
|
||||
*/
|
||||
interface OutboxStore : AutoCloseable {
|
||||
|
||||
/**
|
||||
* Subscribe на events.
|
||||
*
|
||||
* **`after == null`** → только **live** (события с момента вызова
|
||||
* `events()`). Каждое новое событие от любого producer'а немедленно
|
||||
* появится в Flow. Буфер replay не отдаётся.
|
||||
*
|
||||
* **`after != null`** → сначала **catchup**: эмитт все буферизованные
|
||||
* события с `date > after`, порядок `date ASC` (ties по `id ASC`).
|
||||
* Затем **live** (как null-case).
|
||||
*
|
||||
* Cold Flow: каждый вызов — новая подписка. Вызов **после** append'а
|
||||
* не увидит этот конкретный event (если `after == null`); для catchup
|
||||
* передавайте явный `after`.
|
||||
*
|
||||
* ВАЖНО: `Flow` НЕ бросает ошибку при потере сети между producer и
|
||||
* store — такие события просто не дойдут до этого Flow. Для гарантии
|
||||
* полноты клиент обязан cross-check с [earliestEventDate] и fallback
|
||||
* в message store при gap'е (см. KDoc интерфейса).
|
||||
*/
|
||||
fun events(after: Instant?): Flow<CommonEvent>
|
||||
|
||||
/**
|
||||
* Subscribe на **только conversation events** (т.е. [CommonEvent.Conversation]).
|
||||
*
|
||||
* - [conversationId] == null → события **всех** диалогов.
|
||||
* - [conversationId] != null → события **только этого** диалога.
|
||||
*
|
||||
* Семантика `after` идентична [events] (catchup + live).
|
||||
* Возвращаемый тип — конкретный subtype [CommonEvent.Conversation].
|
||||
*/
|
||||
/**
|
||||
* **Default implementation** (читает все events + фильтрует).
|
||||
*
|
||||
* Простая реализация через [events] + filterIsInstance. Реализации
|
||||
* могут override'нуть для эффективности (например, добавить SQL
|
||||
* `WHERE conversation_id = ?` чтобы не тянуть всё в память), но
|
||||
* контракт корректен и без override.
|
||||
*/
|
||||
fun conversationEvents(after: Instant?, conversationId: String? = null): Flow<CommonEvent.Conversation> =
|
||||
events(after)
|
||||
.filterIsInstance<CommonEvent.Conversation>()
|
||||
.let { filtered ->
|
||||
if (conversationId == null) filtered
|
||||
else filtered.filter { it.conversationId == conversationId }
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe на **только agent events** ([CommonEvent.Agent] —
|
||||
* создание/удаление/переименование диалога).
|
||||
*
|
||||
* Семантика `after` идентична [events] (catchup + live).
|
||||
* Возвращаемый тип — конкретный subtype [CommonEvent.Agent].
|
||||
*
|
||||
* Полезно для admin-дашборда, который хочет видеть только lifecycle
|
||||
* диалогов без деталей ходов.
|
||||
*/
|
||||
/**
|
||||
* **Default implementation** (читает все events + фильтрует по типу).
|
||||
*
|
||||
* Простая реализация через [events] + filterIsInstance. Реализации
|
||||
* могут override'нуть для эффективности (например, читать только agent
|
||||
* row'ы из БД), но контракт корректен и без override.
|
||||
*/
|
||||
fun agentEvents(after: Instant?): Flow<CommonEvent.Agent> =
|
||||
events(after).filterIsInstance<CommonEvent.Agent>()
|
||||
|
||||
/**
|
||||
* Date **стартовой точки** буфера.
|
||||
*
|
||||
* - Если буфер не пуст → `date` самого старого буферизованного event'а.
|
||||
* - Если буфер пуст → текущее время (`Clock.System.now()` на момент вызова).
|
||||
*
|
||||
* **Семантика "now если пусто"** важна: позволяет клиенту безопасно
|
||||
* подписаться на [events](after = earliest) сразу — он получит только
|
||||
* новые live event'ы, без ложного catchup. Если бы возвращалось
|
||||
* `Instant.DISTANT_PAST` или `null` (с проверкой), клиент мог бы
|
||||
* ошибочно подписаться на несуществующий catchup и зависнуть в ожидании.
|
||||
*
|
||||
* **Используется клиентом для gap detection**:
|
||||
* - `lastSeen < earliest` → есть дыра в покрытии, нужен fallback
|
||||
* в message store за диапазоном `[lastSeen, earliest)`.
|
||||
* - `lastSeen >= earliest` → всё доступно через [events](after),
|
||||
* fallback не нужен.
|
||||
* - `lastSeen == earliest` → OK, первый live event будет > earliest.
|
||||
*
|
||||
* **Edge case**: клиент, подключившийся до того как store увидел хоть
|
||||
* один event, получает `earliest ≈ now`. Его `lastSeen` будет < earliest
|
||||
* — адаптируется в первом же poll'е и пойдёт через fallback если
|
||||
* сообщения audit log существуют (для consistency с прошлым).
|
||||
*
|
||||
* Suspend потому что в persistent impl'ах требует SQL query (`MIN(date)`
|
||||
* или `Clock.now()` для пустого буфера).
|
||||
*/
|
||||
suspend fun earliestEventDate(): Instant
|
||||
|
||||
override fun close()
|
||||
}
|
||||
+1
-1
@@ -114,7 +114,7 @@ sealed interface Event {
|
||||
|
||||
- Никакого HTTP/SSE/JSON. Это контракт. Сериализация живёт в `:server`
|
||||
и `:client`.
|
||||
- Никакого хранения. Реализации `MessageStore` живут в `:storage-*`.
|
||||
- Никакого хранения. Реализации `JournalStore` живут в `:storage-*`.
|
||||
- Никакой логики прерывания / инструментов / LLM-вызовов. Это всё
|
||||
внутри `:standalone` (ChatAgent) и выше.
|
||||
|
||||
|
||||
@@ -23,6 +23,18 @@ kotlin {
|
||||
api(libs.kotlinx.serialization.core)
|
||||
// для JsonElement в MessageContext.metadata
|
||||
api(libs.kotlinx.serialization.json)
|
||||
// Read-only storage handles, выставляемые через Agent.journal
|
||||
// и Agent.outbox. Типы JournalStore/OutboxStore фигурируют в
|
||||
// public-сигнатуре Agent, поэтому api-висимости.
|
||||
//
|
||||
// Линейный граф зависимостей (без циклов):
|
||||
// :proto ──► :outbox-api (нет обратной зависимости)
|
||||
// :proto ──► :journal-api (нет обратной зависимости)
|
||||
// Добились переносом AgentEvent/CommonEvent/Event из :proto в
|
||||
// :outbox-api — они теперь self-contained в outbox (не нужны
|
||||
// :proto-типы), а :proto использует их через :outbox-api.
|
||||
api(project(":journal-api"))
|
||||
api(project(":outbox-api"))
|
||||
}
|
||||
commonTest.dependencies {
|
||||
implementation(kotlin("test"))
|
||||
|
||||
@@ -2,6 +2,8 @@ package pw.binom.agentik.proto
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import pw.binom.agentik.journal.JournalStore
|
||||
import pw.binom.agentik.outbox.OutboxStore
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
@@ -10,12 +12,53 @@ import kotlin.time.Instant
|
||||
* Транспортно-агностично. [Agent] — фабрика stateful-диалогов:
|
||||
* [createConversation] возвращает [Conversation], который сам хранит историю
|
||||
* и которому отправляют ходы через [Conversation.send].
|
||||
*
|
||||
* **Хранилища вынесены в [Agent.journal] и [Agent.outbox]**: оба read-only.
|
||||
* События больше НЕ часть [Agent] (раньше были `events()`/`allEvents()`) —
|
||||
* они теперь живут в [outbox] как `OutboxStore.events(after)` /
|
||||
* `outbox.agentEvents(after)`. Это даёт единый путь для всех read-операций
|
||||
* по хранилищу и убирает дублирование между протоколом и хранилищем.
|
||||
*/
|
||||
public interface Agent {
|
||||
interface Agent : AutoCloseable {
|
||||
|
||||
/** Идентификатор агента. */
|
||||
val id: String
|
||||
|
||||
/**
|
||||
* Освобождает ресурсы агента (HTTP-клиент, сетевые handles, подписки).
|
||||
* После [close] вызовы [createConversation] / [getConversation] и т.п.
|
||||
* не определены. Idempotent.
|
||||
*/
|
||||
override fun close()
|
||||
|
||||
/**
|
||||
* Append-only audit log всех сообщений диалогов (read-only view).
|
||||
*
|
||||
* Используется HTTP-фасадом `:server` для endpoint'а
|
||||
* `GET /{path}/journal/conversations/{id}/messages` — внешние клиенты
|
||||
* (дашборды, parent-агенты, A2A-bridge) могут читать полный transcript
|
||||
* диалога, включая tool-call/tool-result/error, без необходимости идти
|
||||
* через `Conversation.getMessages` (который возвращает уже
|
||||
* project'нутый proto-Message).
|
||||
*
|
||||
* **Read-only**: write-доступ только через `MutableJournalStore`
|
||||
* внутри ChatAgent / ConversationLoop, не через [Agent] interface.
|
||||
*/
|
||||
val journal: JournalStore
|
||||
|
||||
/**
|
||||
* Bounded-tail live event stream агента (read-only view).
|
||||
*
|
||||
* Используется HTTP-фасадом `:server` для endpoint'а
|
||||
* `GET /{path}/outbox/events?after=` (SSE) — внешние клиенты подписываются
|
||||
* на agent lifecycle + conversation events. Catchup+live контракт — см.
|
||||
* KDoc `OutboxStore.events`.
|
||||
*
|
||||
* **Read-only**: write-доступ только через `MutableOutboxStore` внутри
|
||||
* ChatAgent / ConversationLoop, не через [Agent] interface.
|
||||
*/
|
||||
val outbox: OutboxStore
|
||||
|
||||
/** Создаёт новый stateful-диалог с агентом. */
|
||||
fun createConversation(temp: Boolean): Conversation
|
||||
|
||||
@@ -39,16 +82,6 @@ public interface Agent {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Live-подписка на изменения в множестве диалогов агента: создание,
|
||||
* удаление, переименование (см. [AgentEvent]). События внутри конкретного
|
||||
* диалога приходят через [Conversation.events].
|
||||
*
|
||||
* **Не реплеит** прошлое — для снимка множества используй [getConversations]
|
||||
* или [getConversation].
|
||||
*/
|
||||
fun events(after: Instant): Flow<AgentEvent>
|
||||
|
||||
companion object {
|
||||
|
||||
const val PAGE_SIZE: Int = 100
|
||||
|
||||
@@ -1,43 +1,10 @@
|
||||
package pw.binom.agentik.proto
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* Live-события уровня [Agent]: изменения в множестве диалогов
|
||||
* (создание, удаление, переименование). События, происходящие **внутри**
|
||||
* конкретного диалога, приходят через [Conversation.events], а не сюда.
|
||||
* Backward-compat typealias: `AgentEvent` теперь живёт в `:outbox-api`
|
||||
* (логически принадлежит сущности outbox, не wire-протоколу `:proto`).
|
||||
*
|
||||
* Каждое событие несёт [date] — момент эмиссии в UTC. Семантика подписки
|
||||
* идентична [Conversation.events]: поток **не реплеит** прошлое, для бэкфилла
|
||||
* используются `getConversations`/`getConversation`.
|
||||
* Существующие импорты `pw.binom.agentik.proto.AgentEvent` продолжают
|
||||
* работать транспарентно. Использовать typealias в новом коде.
|
||||
*/
|
||||
@Serializable
|
||||
sealed interface AgentEvent {
|
||||
/** Момент эмиссии события в UTC. */
|
||||
val date: Instant
|
||||
|
||||
/**
|
||||
* Создан новый диалог. Передаётся его id — handle можно получить через
|
||||
* [Agent.getConversation]. Подписчик после [Created] может сразу открыть
|
||||
* live-подписку на этот диалог через [Conversation.events].
|
||||
*/
|
||||
@Serializable
|
||||
@SerialName("created")
|
||||
data class Created(override val date: Instant, val conversationId: String) : AgentEvent
|
||||
|
||||
/**
|
||||
* Диалог удалён. Переданный [Conversation]-handle реализация обязана
|
||||
* закрыть (`close()`) до эмиссии этого события — после [Deleted]
|
||||
* пользоваться handle нельзя.
|
||||
*/
|
||||
@Serializable
|
||||
@SerialName("deleted")
|
||||
data class Deleted(override val date: Instant, val id: String) : AgentEvent
|
||||
|
||||
/** У диалога сменился заголовок. */
|
||||
@Serializable
|
||||
@SerialName("renamed")
|
||||
data class Renamed(override val date: Instant, val id: String, val title: String?) : AgentEvent
|
||||
}
|
||||
typealias AgentEvent = pw.binom.agentik.outbox.AgentEvent
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package pw.binom.agentik.proto
|
||||
|
||||
/**
|
||||
* Backward-compat typealias: `CommonEvent` теперь живёт в `:outbox-api`.
|
||||
*
|
||||
* Существующие импорты `pw.binom.agentik.proto.CommonEvent` продолжают
|
||||
* работать транспарентно.
|
||||
*/
|
||||
typealias CommonEvent = pw.binom.agentik.outbox.CommonEvent
|
||||
@@ -1,88 +1,11 @@
|
||||
package pw.binom.agentik.proto
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* Элемент live-потока [Conversation.events].
|
||||
* Backward-compat typealias: `Event` теперь живёт в `:outbox-api`.
|
||||
*
|
||||
* Каждое событие несёт [date] — момент эмиссии в UTC. Используется клиентом
|
||||
* для трекинга «где остановился» при обрыве/переподключении и для разрешения
|
||||
* порядка при равных timestamps.
|
||||
*
|
||||
* Базовая структура хода:
|
||||
* `StartReasoning?` → `StartResponse(TEXT|IMAGE)` → ...контент... → `End` | `Interrupted` | `Error`.
|
||||
* `StartReasoning` может отсутствовать, если агент не показывал рассуждения.
|
||||
* Существующие импорты `pw.binom.agentik.proto.Event` продолжают
|
||||
* работать транспарентно. `Conversation.events(after): Flow<Event>` в
|
||||
* `:proto.Conversation` теперь фактически возвращает
|
||||
* `pw.binom.agentik.outbox.Event` — тот же тип, другое имя.
|
||||
*/
|
||||
@Serializable
|
||||
sealed interface Event {
|
||||
/** Момент эмиссии события в UTC. */
|
||||
val date: Instant
|
||||
|
||||
@Serializable
|
||||
enum class ResponseType {
|
||||
@SerialName("text") TEXT,
|
||||
@SerialName("image") IMAGE
|
||||
}
|
||||
|
||||
/** Ассистент начал рассуждение (опциональный маркер; контент рассуждения приходит через [AppendText]). */
|
||||
@Serializable
|
||||
@SerialName("start_reasoning")
|
||||
data class StartReasoning(override val date: Instant) : Event
|
||||
|
||||
/** Начало ответа ассистента заданного типа. После него идут соответствующие `Append*`/`Tool*`-события, потом [End]/[Interrupted]/[Error]. */
|
||||
@Serializable
|
||||
@SerialName("start_response")
|
||||
data class StartResponse(override val date: Instant, val responseType: ResponseType) : Event
|
||||
|
||||
/** Ход завершён нормально. Соответствующий [Message.AssistantMessage] появится в `getMessages`. */
|
||||
@Serializable
|
||||
@SerialName("end")
|
||||
data class End(override val date: Instant) : Event
|
||||
|
||||
/** Ход прерван через [Conversation.interrupt]. Частичный ответ НЕ сохраняется в истории. */
|
||||
@Serializable
|
||||
@SerialName("interrupted")
|
||||
data class Interrupted(override val date: Instant) : Event
|
||||
|
||||
@Serializable
|
||||
@SerialName("append_text")
|
||||
data class AppendText(override val date: Instant, val body: String) : Event
|
||||
|
||||
@Serializable
|
||||
@SerialName("append_image")
|
||||
data class AppendImage(override val date: Instant, val body: ByteArray, val mime: String) : Event
|
||||
|
||||
/**
|
||||
* Агент начал вызов тула. Аргументы приходят целиком — стриминга нет.
|
||||
* [id] совпадает с id соответствующего [Message.ToolCall] в истории
|
||||
* после завершения хода.
|
||||
*/
|
||||
@Serializable
|
||||
@SerialName("tool_call")
|
||||
data class ToolCall(
|
||||
override val date: Instant,
|
||||
val id: String,
|
||||
val title: String?,
|
||||
val toolName: String,
|
||||
val toolArgs: String,
|
||||
) : Event
|
||||
|
||||
/**
|
||||
* Результат вызова тула. Приходит целиком после завершения исполнения.
|
||||
* [id] совпадает с [ToolCall.id], к которому относится результат, и
|
||||
* с id [Message.ToolResult] в истории.
|
||||
*/
|
||||
@Serializable
|
||||
@SerialName("tool_result")
|
||||
data class ToolResult(override val date: Instant, val id: String, val result: String?) : Event
|
||||
|
||||
/**
|
||||
* Ошибка хода. После неё поток завершается; дальнейшие события могут
|
||||
* прийти, но ход считается проваленным.
|
||||
*/
|
||||
@Serializable
|
||||
@SerialName("error")
|
||||
data class Error(override val date: Instant, val message: String, val code: String? = null) : Event
|
||||
}
|
||||
typealias Event = pw.binom.agentik.outbox.Event
|
||||
|
||||
@@ -24,6 +24,16 @@ kotlin {
|
||||
commonMain.dependencies {
|
||||
implementation(project(":proto"))
|
||||
|
||||
// Read-only storage handles, которые HTTP-фасад выставляет наружу
|
||||
// под {path}/journal/* и {path}/outbox/*. Типы JournalStore/
|
||||
// OutboxStore фигурируют в сигнатурах internal-функций
|
||||
// journalRoutes/outboxRoutes, поэтому нужны в compile classpath.
|
||||
// Транзитивные api-висимости :proto (:journal-api, :outbox-api)
|
||||
// не доходят до :server из-за implementation(:proto), поэтому
|
||||
// объявляем напрямую.
|
||||
implementation(project(":journal-api"))
|
||||
implementation(project(":outbox-api"))
|
||||
|
||||
// Ktor (без engine — engine подключает потребитель, см. :standalone).
|
||||
implementation(libs.ktor.server.core)
|
||||
implementation(libs.ktor.server.content.negotiation)
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package pw.binom.agentik.server
|
||||
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.server.response.respond
|
||||
import io.ktor.server.routing.Route
|
||||
import io.ktor.server.routing.get
|
||||
import io.ktor.server.routing.route
|
||||
import pw.binom.agentik.journal.JournalStore
|
||||
|
||||
/**
|
||||
* HTTP-фасад для [JournalStore] (append-only audit log сообщений диалога).
|
||||
*
|
||||
* **Основа:** функция-продолжение для [Route.agentikAgent] — внутри неё уже
|
||||
* создан роут под `{path}` агента. [journalRoutes] добавляет под-prefix
|
||||
* [path] (по умолчанию `"/journal"`) к **этому же** родительскому роуту,
|
||||
* итоговый URL = `{path агента}/journal/...`.
|
||||
*
|
||||
* **Endpoint'ы под `{path}/journal`:**
|
||||
* - `GET /conversations/{id}/messages?after=&offset=&limit=` — список raw
|
||||
* [pw.binom.agentik.journal.MessageRecord] (все типы: UserMessage /
|
||||
* AssistantMessage / ToolCall / ToolResult / Error). В отличие от
|
||||
* `GET /conversations/{id}/messages` в [agentikRoutes] (который отдаёт
|
||||
* project'нутые proto-[pw.binom.agentik.proto.Message]), здесь клиент
|
||||
* получает полный transcript с tool-call/tool-result/error payload-ами,
|
||||
* turn-tokens и context-метаданными.
|
||||
*
|
||||
* **Read-only:** [JournalStore] не имеет `append` — запись только через
|
||||
* writer-референс, который ChatAgent держит внутри (тип `MutableJournalStore`,
|
||||
* не выставлен наружу через [pw.binom.agentik.proto.Agent]).
|
||||
*/
|
||||
fun Route.journalRoutes(
|
||||
journal: JournalStore,
|
||||
path: String = "/journal",
|
||||
) {
|
||||
route(path) {
|
||||
get("/conversations/{id}/messages") {
|
||||
val id = call.parameters["id"]!!
|
||||
val after = call.parseAfter() ?: return@get
|
||||
val offset = call.request.queryParameters["offset"]?.toIntOrNull() ?: 0
|
||||
val limit = call.request.queryParameters["limit"]?.toIntOrNull() ?: JournalStore.PAGE_SIZE
|
||||
call.respond(journal.list(id, after, offset, limit))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,20 +20,36 @@ import pw.binom.agentik.proto.Agent
|
||||
* }.start(wait = true)
|
||||
* ```
|
||||
*
|
||||
* Под префиксом [path] монтируются:
|
||||
* - `POST /conversations` — создать диалог
|
||||
* - `GET /conversations` — список
|
||||
* - `GET /conversations/{id}` — один диалог
|
||||
* - `PATCH /conversations/{id}` — переименовать
|
||||
* - `DELETE /conversations/{id}` — удалить
|
||||
* - `POST /conversations/{id}/messages` — `send` (202 Accepted)
|
||||
* - `POST /conversations/{id}/interrupt` — `interrupt`
|
||||
* - `GET /conversations/{id}/messages` — история
|
||||
* - `GET /conversations/{id}/events` — SSE: события хода
|
||||
* - `GET /events` — SSE: события агента
|
||||
* - `GET /health` — `"ok"`
|
||||
* **Все** дочерние фасады ([agentikRoutes] / [journalRoutes] / [outboxRoutes])
|
||||
* монтируются внутри `{path}` — внутренний `route(path)` создаёт родительский
|
||||
* роут агента, и storage-фасады добавляют свои под-prefix'ы **к этому же**
|
||||
* роуту, а не к корню. Итоговая раскладка:
|
||||
*
|
||||
* ```
|
||||
* POST {path}/conversations
|
||||
* GET {path}/conversations
|
||||
* GET {path}/conversations/{id}
|
||||
* PATCH {path}/conversations/{id}
|
||||
* DELETE {path}/conversations/{id}
|
||||
* POST {path}/conversations/{id}/messages
|
||||
* POST {path}/conversations/{id}/interrupt
|
||||
* GET {path}/conversations/{id}/messages
|
||||
* GET {path}/conversations/{id}/events (SSE: события хода)
|
||||
* GET {path}/events (SSE: agent-level events)
|
||||
* GET {path}/health
|
||||
* GET {path}/journal/conversations/{id}/messages
|
||||
* GET {path}/outbox/events (SSE: outbox catchup+live)
|
||||
* ```
|
||||
*
|
||||
* Под-prefix'ы `/journal` и `/outbox` выбраны чтобы не пересекаться с
|
||||
* существующим `/events` (agent-level SSE) и
|
||||
* `/conversations/{id}/messages` (proto-Message'ы, не raw records).
|
||||
*/
|
||||
fun Route.agentikAgent(agent: Agent, path: String = "/agentik", token: String? = null) {
|
||||
fun Route.agentikAgent(
|
||||
agent: Agent,
|
||||
path: String = "/agentik",
|
||||
token: String? = null,
|
||||
) {
|
||||
route(path) {
|
||||
install(ContentNegotiation) {
|
||||
json(agentikJson)
|
||||
@@ -43,6 +59,11 @@ fun Route.agentikAgent(agent: Agent, path: String = "/agentik", token: String? =
|
||||
this.token = token
|
||||
}
|
||||
}
|
||||
// Proto-роуты: диалоги, send/interrupt, events (agent-level).
|
||||
agentikRoutes(agent)
|
||||
|
||||
// Storage-фасады: те же `this` (роут агента), свои под-prefix'ы.
|
||||
journalRoutes(agent.journal)
|
||||
outboxRoutes(agent.outbox)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package pw.binom.agentik.server
|
||||
|
||||
import io.ktor.server.routing.Route
|
||||
import io.ktor.server.routing.get
|
||||
import io.ktor.server.routing.route
|
||||
import pw.binom.agentik.outbox.OutboxStore
|
||||
import pw.binom.agentik.proto.CommonEvent
|
||||
|
||||
/**
|
||||
* HTTP-фасад для [OutboxStore] (bounded-tail live event stream агента).
|
||||
*
|
||||
* **Основа:** функция-продолжение для [Route.agentikAgent] — внутри неё уже
|
||||
* создан роут под `{path}` агента. [outboxRoutes] добавляет под-prefix
|
||||
* [path] (по умолчанию `"/outbox"`) к **этому же** родительскому роуту,
|
||||
* итоговый URL = `{path агента}/outbox/...`.
|
||||
*
|
||||
* **Endpoint'ы под `{path}/outbox`:**
|
||||
* - `GET /events?after=` — SSE (catchup + live) в формате `data: <json>\n\n`,
|
||||
* где `<json>` — сериализованный [CommonEvent].
|
||||
* Семантика `after` идентична [OutboxStore.events]:
|
||||
* - `after` отсутствует → только live (события с момента подписки).
|
||||
* - `after` задан → сначала catchup всех буферизованных событий с
|
||||
* `date > after`, потом live.
|
||||
*
|
||||
* **Покрытие:** outbox — это короткий bounded tail с auto-TTL. Для событий
|
||||
* старше буфера клиент должен идти в `/journal/conversations/{id}/messages`
|
||||
* (полный audit log), см. KDoc [OutboxStore].
|
||||
*
|
||||
* **Read-only:** [OutboxStore] не имеет `append` — запись только через
|
||||
* writer-референс, который ChatAgent держит внутри (тип `MutableOutboxStore`,
|
||||
* не выставлен наружу через [pw.binom.agentik.proto.Agent]).
|
||||
*/
|
||||
fun Route.outboxRoutes(
|
||||
outbox: OutboxStore,
|
||||
path: String = "/outbox",
|
||||
) {
|
||||
route(path) {
|
||||
get("/events") {
|
||||
val after = call.parseAfter() ?: return@get
|
||||
// SSE-стрим: catchup (если `after` != DISTANT_PAST) + live tail.
|
||||
call.streamJsonSse(outbox.events(after), CommonEvent.serializer())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package pw.binom.agentik.server
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.server.application.ApplicationCall
|
||||
import io.ktor.server.application.call
|
||||
import io.ktor.server.request.receive
|
||||
import io.ktor.server.request.receiveText
|
||||
import io.ktor.server.response.respond
|
||||
@@ -17,10 +16,12 @@ import io.ktor.server.routing.post
|
||||
import io.ktor.utils.io.writeStringUtf8
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.json.Json
|
||||
import pw.binom.agentik.proto.Agent
|
||||
import pw.binom.agentik.proto.AgentEvent
|
||||
import pw.binom.agentik.proto.CommonEvent
|
||||
import pw.binom.agentik.proto.Conversation
|
||||
import pw.binom.agentik.proto.Content
|
||||
import pw.binom.agentik.proto.Event
|
||||
@@ -132,7 +133,23 @@ internal fun Route.agentikRoutes(agent: Agent) {
|
||||
|
||||
get("/events") {
|
||||
val after = call.parseAfter() ?: return@get
|
||||
call.streamJsonSse(agent.events(after), AgentEvent.serializer())
|
||||
// agent.outbox.agentEvents(after) возвращает Flow<CommonEvent.Agent>;
|
||||
// распаковываем .event для обратной совместимости с прежним
|
||||
// форматом (когда был Agent.events(): Flow<AgentEvent>).
|
||||
call.streamJsonSse(
|
||||
agent.outbox.agentEvents(after).map { it.event },
|
||||
AgentEvent.serializer(),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Все события в одном потоке: agent lifecycle + все conversation events.
|
||||
* Для admin-дашборда, debug-инструментов, parent-агента.
|
||||
* Для UI достаточно `/events` + `/conversations/{id}/events`.
|
||||
*/
|
||||
get("/events/all") {
|
||||
val after = call.parseAfter() ?: return@get
|
||||
call.streamJsonSse(agent.outbox.events(after), CommonEvent.serializer())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,7 +159,7 @@ internal fun Route.agentikRoutes(agent: Agent) {
|
||||
* Парсит query-параметр `after` как ISO-8601 [Instant]. Отсутствие = [Instant.DISTANT_PAST].
|
||||
* При невалидном значении отвечает 400 и возвращает `null`.
|
||||
*/
|
||||
private suspend fun ApplicationCall.parseAfter(): Instant? {
|
||||
internal suspend fun ApplicationCall.parseAfter(): Instant? {
|
||||
val raw = request.queryParameters["after"]
|
||||
if (raw == null) return Instant.DISTANT_PAST
|
||||
return try {
|
||||
@@ -153,7 +170,7 @@ private suspend fun ApplicationCall.parseAfter(): Instant? {
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun <T> ApplicationCall.streamJsonSse(
|
||||
internal suspend fun <T> ApplicationCall.streamJsonSse(
|
||||
flow: Flow<T>,
|
||||
serializer: KSerializer<T>,
|
||||
json: Json = agentikJson,
|
||||
|
||||
@@ -11,15 +11,17 @@ import io.ktor.server.cio.CIO as ServerCIO
|
||||
import io.ktor.server.engine.EmbeddedServer
|
||||
import io.ktor.server.engine.embeddedServer
|
||||
import io.ktor.server.routing.routing
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.emptyFlow
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import pw.binom.agentik.journal.JournalStore
|
||||
import pw.binom.agentik.journal.MessageRecord
|
||||
import pw.binom.agentik.outbox.CommonEvent
|
||||
import pw.binom.agentik.outbox.OutboxStore
|
||||
import pw.binom.agentik.proto.Agent
|
||||
import pw.binom.agentik.proto.AgentEvent
|
||||
import pw.binom.agentik.proto.Conversation
|
||||
import kotlin.time.Instant
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* Тесты route-scoped плагина [BearerTokenPlugin]:
|
||||
@@ -29,12 +31,33 @@ import kotlin.time.Instant
|
||||
*/
|
||||
class BearerTokenTest {
|
||||
|
||||
private class FakeAgent(override val id: String = "test") : Agent {
|
||||
/**
|
||||
* Stub-реализации storage-handle для теста bearer-токена — контент не
|
||||
* используется, тесты проверяют только что endpoint'ы закрыты/открыты
|
||||
* по токену. Storage routes регистрируются сразу в [agentikAgent] и
|
||||
* вычитывают [journal]/[outbox] eagerly, поэтому возвращаем noop-реализации
|
||||
* (а не `error("...")`), иначе старт сервера валится на инициализации.
|
||||
*/
|
||||
private class FakeAgent(
|
||||
override val id: String = "test",
|
||||
) : Agent {
|
||||
override val journal: JournalStore = object : JournalStore {
|
||||
override suspend fun list(conversationId: String, after: Instant, offset: Int, limit: Int) = emptyList<MessageRecord>()
|
||||
override fun listFlow(conversationId: String, after: Instant, pageSize: Int) = emptyFlow<MessageRecord>()
|
||||
override fun close() {}
|
||||
}
|
||||
override val outbox: OutboxStore = object : OutboxStore {
|
||||
override fun events(after: Instant?) = emptyFlow<CommonEvent>()
|
||||
override fun agentEvents(after: Instant?) = emptyFlow<CommonEvent.Agent>()
|
||||
override fun conversationEvents(after: Instant?, conversationId: String?) = emptyFlow<CommonEvent.Conversation>()
|
||||
override suspend fun earliestEventDate(): Instant = Instant.DISTANT_PAST
|
||||
override fun close() {}
|
||||
}
|
||||
override fun createConversation(temp: Boolean): Conversation = TODO("not needed by tests")
|
||||
override suspend fun getConversation(id: String): Conversation? = null
|
||||
override suspend fun deleteConversation(id: String): Boolean = false
|
||||
override suspend fun getConversations(offset: Int, limit: Int): List<Conversation> = emptyList()
|
||||
override fun events(after: Instant): Flow<AgentEvent> = emptyFlow()
|
||||
override fun close() {}
|
||||
}
|
||||
|
||||
private suspend fun startServer(token: String?): Pair<EmbeddedServer<*, *>, Int> {
|
||||
|
||||
+54
-9
@@ -46,7 +46,8 @@ include(":server")
|
||||
include(":client")
|
||||
// CLI-клиент поверх :client — REPL со slash-командами и стримингом ответов.
|
||||
// KMP со всеми целями (jvm + весь натив), jvm-таргет собирается как shadowJar.
|
||||
include(":agentik-cli")
|
||||
// include(":agentik-cli") — отключено 2026-09-21: пользователь временно вывел
|
||||
// из сборки. Папка agentik-cli/ осталась на диске для возможного возврата.
|
||||
// TUI-клиент поверх :client — Compose-style UI (Mosaic от Jake Wharton),
|
||||
// рендерится в ANSI-терминал. KMP со всеми desktop-целями (без ios).
|
||||
// include(":agentik-tui") — отключено 2026-09-17: пользователь признал TUI-подход неудачным.
|
||||
@@ -63,17 +64,61 @@ include(":memory-vector")
|
||||
// Интерфейсы хранилища и разговорной истории: MessageStore / WorkingMemoryStore /
|
||||
// ConversationStore / ReflectionStore + StorageBundle агрегатор. Реализации —
|
||||
// в :storage-inmemory / :storage-sqlite / :storage-android (последний deferred).
|
||||
include(":storage-core")
|
||||
// In-memory реализация всех 4 store'ов из :storage-core. KMP, без платформенных
|
||||
// In-memory реализация store'ов из :journal-api и :context-api. KMP,
|
||||
// IO-зависимостей. Используется в тестах (быстрый setup, без JDBC) и будет
|
||||
// использоваться в Android-сборке (JVector/SQLite не подходят для ART out-of-box).
|
||||
//include(":message-store-api") — перенесено в :journal-api (ConversationRecord/ConversationStore/Ids)
|
||||
// и :reflection-api (Reflection/ReflectionStore/ReflectionEvent/Ids)
|
||||
// Модуль удалён с диска.
|
||||
include(":reflection-api")
|
||||
//include(":message-log-api")
|
||||
//include(":message-log-ksqlite")
|
||||
// Новые API-модули трёх сущностей (canonical имена):
|
||||
// - :journal-api — append-only audit log (бывший :message-log-api)
|
||||
// - :context-api — то что видит LLM (бывший :working-memory-api)
|
||||
// - :outbox-api — bounded-tail event stream (бывший :event-store)
|
||||
// Старые модули :message-log-api / :working-memory-api / :event-store
|
||||
// остаются на диске — миграция consumers'ов по чуть-чуть, отдельно.
|
||||
include(":journal-api")
|
||||
include(":context-api")
|
||||
include(":outbox-api")
|
||||
// Bounded-tail event log с auto-TTL. Двухуровневое хранилище: этот модуль —
|
||||
// короткий live tail + recent replay; полный audit log живёт в :message-store-api
|
||||
// (там — MessageStore + ConversationStore). EventStore сам управляет eviction,
|
||||
// никаких prune-методов наружу. KMP, без implementations пока.
|
||||
//include(":event-store")
|
||||
// KMP in-memory реализация MutableEventStore. ConcurrentLinkedDeque + TTL/size
|
||||
// eviction. Для тестов, dev-режима, embedded-сценариев (Android core).
|
||||
include(":outbox-inmemory")
|
||||
include(":journal-inmemory")
|
||||
//include(":event-store-in-memory")
|
||||
//include(":working-memory-api")
|
||||
include(":storage-inmemory")
|
||||
// SQLDelight-реализация всех 4 store'ов из :storage-core. JVM-only (SQLDelight
|
||||
// native драйверов для KMP вне JVM пока не публикует). Содержит 4 .sq-файла +
|
||||
// 5 классов: SqliteStores, SqliteConversationStore, SqliteMessageStore,
|
||||
// SqliteWorkingMemoryStore, SqliteReflectionStore. Бэкенд для прод-запуска
|
||||
// :standalone (путь к .db файлу в AGENTIK_DB).
|
||||
include(":storage-sqlite")
|
||||
// SQLDelight-реализация store'ов из :journal-api и :context-api. JVM-only
|
||||
// KMP-реализация EventStore поверх ksqlite (https://github.com/caffeine-mgn/ksqlite).
|
||||
// Цель: проверить что pure-Kotlin SQLite с sqlite-vec заменяет SQLDelight+JVector
|
||||
// на KMP-таргетах (JVM + linuxX64 + mingwX64). Apple targets auto-disabled на
|
||||
// Linux — собираются локально на macOS. Пока покрывает только EventStore;
|
||||
// остальные store'ы мигрируют после стабилизации ksqlite и реального
|
||||
// использования на Android-агенте.
|
||||
include(":storage-ksqlite")
|
||||
// ksqlite-реализация :context-api (ContextStore / working_memory table).
|
||||
// Минимальный модуль: только таблица `working_memory` + 2 индекса.
|
||||
// Параллельно существует :storage-ksqlite/KsqliteWorkingMemoryStore.kt —
|
||||
// миграция consumers'ов по чуть-чуть, отдельно.
|
||||
include(":context-ksqlite")
|
||||
// ksqlite-реализация :journal-api (JournalStore / message table).
|
||||
// Минимальный модуль: только таблица `message` + 1 индекс
|
||||
// `(conversation_id, created_at)`. Параллельно существует
|
||||
// :storage-ksqlite/KsqliteMessageStore.kt — миграция consumers'ов
|
||||
// по чуть-чуть, отдельно.
|
||||
include(":journal-ksqlite")
|
||||
// KMP-реализация EventStore через ksqlite (https://github.com/caffeine-mgn/ksqlite).
|
||||
// Цель: проверить что pure-Kotlin SQLite с sqlite-vec заменяет SQLDelight+JVector
|
||||
// на KMP-таргетах (JVM + linuxX64 + mingwX64). Apple targets auto-disabled на
|
||||
// Linux — собираются локально на macOS. На этом этапе покрывает только EventStore;
|
||||
// остальные store'ы мигрируют после стабилизации ksqlite (>=0.2) и реального
|
||||
// использования на Android-агенте.
|
||||
// Ядро механики toolsets: ToolsetRegistry + ToolsetDispatchPolicy + встроенные
|
||||
// тулы enable_toolset/disable_toolset. KMP, не зависит от :standalone, может быть
|
||||
// переиспользован в Android-сборке. Интеграция с ChatAgent — commit 5+.
|
||||
|
||||
+28
-23
@@ -1,10 +1,9 @@
|
||||
@file:OptIn(org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi::class)
|
||||
|
||||
import org.gradle.api.artifacts.ConfigurationContainer
|
||||
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
|
||||
import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
|
||||
|
||||
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
|
||||
import org.gradle.api.artifacts.ConfigurationContainer
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.kotlin.multiplatform)
|
||||
@@ -19,6 +18,12 @@ val skipVectorMemory: Boolean =
|
||||
(project.findProperty("skipVectorMemory") == "true") ||
|
||||
System.getenv("SKIP_VECTOR_MEMORY") == "1"
|
||||
|
||||
// :standalone — единственный entry-point проекта, JVM-only по факту (один
|
||||
// `jvm()` target). Чтобы не таскать лишние sourceSet'ы, ВСЁ живёт в
|
||||
// commonMain + commonTest — даже зависимости, которые формально JVM-only
|
||||
// (ktor-server-cio, litert-openai, storage-ksqlite, ...). KMP-плагин тут
|
||||
// только ради бесшовного потребления KMP-зависимостей (`:proto`, `:server`,
|
||||
// `:journal-api`, ...); компилируется всё ровно в одну JVM-таргет.
|
||||
kotlin {
|
||||
jvmToolchain(21)
|
||||
|
||||
@@ -32,21 +37,18 @@ kotlin {
|
||||
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
// Протокол + KMP storage API
|
||||
implementation(project(":proto"))
|
||||
implementation(project(":server"))
|
||||
implementation(project(":journal-api"))
|
||||
implementation(project(":outbox-api"))
|
||||
implementation(project(":context-api"))
|
||||
implementation(project(":reflection-api"))
|
||||
|
||||
// Commons
|
||||
implementation(libs.kotlinx.coroutines.core)
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
|
||||
// litert-kmp: контракт (commonMain)
|
||||
api(libs.litert.api)
|
||||
|
||||
// litert-openai: JVM-реализация
|
||||
implementation(libs.litert.openai)
|
||||
}
|
||||
|
||||
jvmMain.dependencies {
|
||||
// Парсер и загрузчик скилов (YAML frontmatter + markdown body).
|
||||
implementation(project(":skills"))
|
||||
|
||||
@@ -56,16 +58,25 @@ kotlin {
|
||||
if (!skipVectorMemory) {
|
||||
implementation(project(":memory-vector"))
|
||||
}
|
||||
implementation(project(":storage-core"))
|
||||
implementation(project(":storage-sqlite"))
|
||||
|
||||
// SQLite-бэкенд (ksqlite JNI) — JVM-only по факту.
|
||||
implementation(project(":storage-ksqlite"))
|
||||
|
||||
// Bounded-tail live event stream + per-event TTL.
|
||||
implementation(project(":outbox-inmemory"))
|
||||
|
||||
// LLM-toolsets + generic LLM-side tools (LlmReflector, SkillMiner,
|
||||
// LlmMemoryReviewer, ContextCompactor, парсеры/промпты).
|
||||
implementation(project(":agent-toolsets"))
|
||||
// Generic LLM-side tools (LlmReflector, SkillMiner, LlmMemoryReviewer,
|
||||
// ContextCompactor, парсеры/промпты). Вынесены из :standalone.
|
||||
implementation(project(":llm-tools"))
|
||||
|
||||
// Generic MCP-bridge (McpConfig, McpRegistry, McpLiteToolAdapter).
|
||||
// Вынесен из :standalone — generic мост между MCP-SDK и LiteTool.
|
||||
implementation(project(":mcp-bridge"))
|
||||
|
||||
// litert-kmp: контракт (commonMain)
|
||||
api(libs.litert.api)
|
||||
// litert-openai: JVM-реализация
|
||||
implementation(libs.litert.openai)
|
||||
// litert-google: встроенный LiteRT-LM движок, нужен только на runtime
|
||||
runtimeOnly(libs.litert.google)
|
||||
|
||||
@@ -87,7 +98,6 @@ kotlin {
|
||||
implementation(libs.ktor.client.core)
|
||||
implementation(libs.ktor.client.cio)
|
||||
implementation(libs.ktor.client.content.negotiation)
|
||||
implementation(libs.ktor.serialization.kotlinx.json)
|
||||
|
||||
// Логирование: kotlin-logging (тонкая обёртка slf4j-api) + logback-classic
|
||||
// (binding для JVM; без него slf4j-api NOP-логирует и не падает).
|
||||
@@ -96,11 +106,6 @@ kotlin {
|
||||
}
|
||||
|
||||
commonTest.dependencies {
|
||||
implementation(libs.kotlinx.coroutines.core)
|
||||
implementation(libs.kotlin.test)
|
||||
}
|
||||
|
||||
jvmTest.dependencies {
|
||||
implementation(libs.kotlinx.coroutines.core)
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
implementation(libs.kotlin.test)
|
||||
@@ -142,11 +147,11 @@ val shadowJarTask = tasks.register<ShadowJar>("shadowJar") {
|
||||
// из любого типа, который сейчас прикидывается `configurations`.
|
||||
val cc = try {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
configurations as org.gradle.api.artifacts.ConfigurationContainer
|
||||
configurations as ConfigurationContainer
|
||||
} catch (_: ClassCastException) {
|
||||
// KGP-generated dependency accessor; обходим через raw project.
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
(project as org.gradle.api.Project).configurations as org.gradle.api.artifacts.ConfigurationContainer
|
||||
(project as org.gradle.api.Project).configurations as ConfigurationContainer
|
||||
}
|
||||
from(cc.getByName("jvmRuntimeClasspath"))
|
||||
|
||||
|
||||
+1
-1
@@ -10,10 +10,10 @@ import pw.binom.a2a.model.Message
|
||||
import pw.binom.a2a.model.Role
|
||||
import pw.binom.a2a.model.TextPart
|
||||
import pw.binom.a2a.server.AgentHandler
|
||||
import pw.binom.agentik.outbox.Event
|
||||
import pw.binom.agentik.proto.Agent
|
||||
import pw.binom.agentik.proto.Content
|
||||
import pw.binom.agentik.proto.Conversation
|
||||
import pw.binom.agentik.proto.Event
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
+17
-26
@@ -9,13 +9,14 @@ import io.ktor.server.routing.post
|
||||
import kotlinx.serialization.json.buildJsonObject
|
||||
import kotlinx.serialization.json.put
|
||||
import pw.binom.agentik.memory.ConversationTurn
|
||||
import pw.binom.agentik.reflection.ReflectionStore
|
||||
import pw.binom.agentik.proto.Agent
|
||||
import pw.binom.agentik.standalone.agent.ChatConversation
|
||||
import pw.binom.agentik.llm.tools.LlmReflector
|
||||
import pw.binom.agentik.llm.tools.SkillMiner
|
||||
import pw.binom.agentik.standalone.agent.memory.Curator
|
||||
import pw.binom.agentik.storage.StorageBundle
|
||||
import pw.binom.agentik.skills.SkillStore
|
||||
import pw.binom.agentik.context.ContextStore
|
||||
|
||||
/**
|
||||
* Debug-эндпоинты для ручного триггерирования фоновых фич (без ожидания
|
||||
@@ -25,7 +26,10 @@ import pw.binom.agentik.skills.SkillStore
|
||||
* - `POST /debug/skill-mine?conversationId=...` — прогон [SkillMiner] прямо сейчас
|
||||
* - `POST /debug/curate` — прогон [Curator.runPass] прямо сейчас
|
||||
* - `POST /debug/compact?conversationId=...` — принудительный compaction
|
||||
* - `GET /debug/tokens?conversationId=...` — token-статистика диалога из БД
|
||||
*
|
||||
* Token-stats эндпоинт убран вместе с `MessageStore.tokenStats()` (см.
|
||||
* :message-log-api/MessageStore.kt). Сейчас token accounting доступен
|
||||
* только через assistant-сообщения с `TurnTokens` (см. MessageRecord).
|
||||
*
|
||||
* Каждый возвращает JSON с результатом (что сохранил / нашёл / сжал), чтобы в
|
||||
* тестах было видно не только "триггер сработал", а что именно LLM намайнила.
|
||||
@@ -33,7 +37,8 @@ import pw.binom.agentik.skills.SkillStore
|
||||
*/
|
||||
internal fun Route.debugRoutes(
|
||||
agent: Agent,
|
||||
storage: pw.binom.agentik.storage.StorageBundle,
|
||||
workingMemoryStore: ContextStore,
|
||||
reflectionStore: ReflectionStore,
|
||||
reflector: LlmReflector?,
|
||||
skillMiner: SkillMiner?,
|
||||
skillStore: SkillStore?,
|
||||
@@ -44,14 +49,14 @@ internal fun Route.debugRoutes(
|
||||
?: return@post call.respondText("conversationId required", status = HttpStatusCode.BadRequest)
|
||||
val minerReflector = reflector
|
||||
if (minerReflector == null) return@post call.respondText("reflection disabled", status = HttpStatusCode.NotFound)
|
||||
val turns = recentTurns(storage, convId, maxTurns = 6)
|
||||
val turns = recentTurns(workingMemoryStore, convId, maxTurns = 6)
|
||||
if (turns.isEmpty()) return@post call.respondText("no turns in conversation", status = HttpStatusCode.NotFound)
|
||||
val reflection = minerReflector.reflect(turns)
|
||||
if (reflection == null) {
|
||||
call.respondText("""{"reflected":false,"reason":"unparseable LLM reply"}""", contentType = ContentType.Application.Json)
|
||||
} else {
|
||||
val stamped = reflection.copy(conversationId = convId)
|
||||
storage.reflectionStore.insert(stamped)
|
||||
reflectionStore.insert(stamped)
|
||||
call.respondText(
|
||||
buildJsonObject {
|
||||
put("reflected", true)
|
||||
@@ -71,7 +76,7 @@ internal fun Route.debugRoutes(
|
||||
val miner = skillMiner
|
||||
val store = skillStore
|
||||
if (miner == null || store == null) return@post call.respondText("skill mining disabled", status = HttpStatusCode.NotFound)
|
||||
val turns = recentTurns(storage, convId, maxTurns = miner.maxTurns)
|
||||
val turns = recentTurns(workingMemoryStore, convId, maxTurns = miner.maxTurns)
|
||||
if (turns.isEmpty()) return@post call.respondText("no turns in conversation", status = HttpStatusCode.NotFound)
|
||||
val mined = miner.mine(turns, store.catalog.skills)
|
||||
for (s in mined) store.upsert(s)
|
||||
@@ -106,20 +111,6 @@ internal fun Route.debugRoutes(
|
||||
val ok = chatConv.forceCompactNow()
|
||||
call.respondText("""{"compacted":$ok}""", contentType = ContentType.Application.Json)
|
||||
}
|
||||
|
||||
get("/debug/tokens") {
|
||||
val convId = call.parameters["conversationId"]
|
||||
?: return@get call.respondText("conversationId required", status = HttpStatusCode.BadRequest)
|
||||
val stats = storage.messageStore.tokenStats(convId)
|
||||
val json = buildJsonObject {
|
||||
put("conversationId", convId)
|
||||
put("turns", stats.turns.toString())
|
||||
put("inputTokens", stats.inputTokens.toString())
|
||||
put("outputTokens", stats.outputTokens.toString())
|
||||
put("totalTokens", (stats.inputTokens + stats.outputTokens).toString())
|
||||
}.toString()
|
||||
call.respondText(json, contentType = ContentType.Application.Json)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -127,14 +118,14 @@ internal fun Route.debugRoutes(
|
||||
* (для debug-триггеров reflector/miner; та же логика, что у хуков
|
||||
* [ChatConversation]).
|
||||
*/
|
||||
internal suspend fun recentTurns(storage: pw.binom.agentik.storage.StorageBundle, conversationId: String, maxTurns: Int): List<ConversationTurn> {
|
||||
val rows = storage.workingMemoryStore.list(conversationId)
|
||||
internal suspend fun recentTurns(workingMemoryStore: ContextStore, conversationId: String, maxTurns: Int): List<ConversationTurn> {
|
||||
val rows = workingMemoryStore.list(conversationId)
|
||||
val pairs = mutableListOf<ConversationTurn>()
|
||||
var pendingUser: String? = null
|
||||
for (row in rows) {
|
||||
when (val e = row.entry) {
|
||||
is pw.binom.agentik.storage.WorkingMemoryEntry.User -> pendingUser = e.content.text()
|
||||
is pw.binom.agentik.storage.WorkingMemoryEntry.Assistant -> {
|
||||
is pw.binom.agentik.context.WorkingMemoryEntry.User -> pendingUser = e.content.text()
|
||||
is pw.binom.agentik.context.WorkingMemoryEntry.Assistant -> {
|
||||
val user = pendingUser ?: ""
|
||||
pendingUser = null
|
||||
pairs += ConversationTurn(userMessage = user, assistantMessage = e.content.text())
|
||||
@@ -146,5 +137,5 @@ internal suspend fun recentTurns(storage: pw.binom.agentik.storage.StorageBundle
|
||||
}
|
||||
|
||||
/** Текстовое содержимое записей working memory (Text-контент, без картинок). */
|
||||
internal fun List<pw.binom.agentik.storage.Content>.text(): String =
|
||||
filterIsInstance<pw.binom.agentik.storage.Content.Text>().joinToString("\n") { it.body }
|
||||
internal fun List<pw.binom.agentik.journal.Content>.text(): String =
|
||||
filterIsInstance<pw.binom.agentik.journal.Content.Text>().joinToString("\n") { it.body }
|
||||
+19
-24
@@ -29,7 +29,7 @@ import pw.binom.agentik.standalone.config.AppConfig.MemoryBackend
|
||||
import pw.binom.agentik.standalone.llm.LlmBackend
|
||||
import pw.binom.agentik.standalone.llm.ModelDownloader
|
||||
import pw.binom.agentik.mcp.bridge.McpRegistry
|
||||
import pw.binom.agentik.storage.sqlite.SqliteStores
|
||||
import pw.binom.agentik.storage.ksqlite.KsqliteStores
|
||||
import java.io.File
|
||||
import pw.binom.agentik.llm.tools.SkillMiner
|
||||
/**
|
||||
@@ -198,7 +198,7 @@ private fun runServer() {
|
||||
}
|
||||
|
||||
val llm = config.llm.createLlm()
|
||||
val storage = SqliteStores.open(dbPath = config.agent.dbPath).asBundle()
|
||||
val sqliteStores = KsqliteStores.open(path = config.agent.dbPath)
|
||||
val mcpRegistry = McpRegistry.fromConfig(config.mcp)
|
||||
|
||||
// Хранилище скилов: если skillsDir задан, читаем каталог + создаём
|
||||
@@ -317,14 +317,17 @@ private fun runServer() {
|
||||
// и interval > 0. Загружаем top-K последних рефлексий из SQLite в system prompt.
|
||||
val reflector: pw.binom.agentik.llm.tools.LlmReflector? =
|
||||
if (config.reflection.interval > 0) LlmReflector(llm = llm) else null
|
||||
val recentReflections: List<pw.binom.agentik.storage.Reflection> =
|
||||
val recentReflections: List<pw.binom.agentik.reflection.Reflection> =
|
||||
if (config.reflection.topK > 0) kotlinx.coroutines.runBlocking {
|
||||
storage.reflectionStore.listRecent(config.reflection.topK)
|
||||
sqliteStores.reflections.listRecent(config.reflection.topK)
|
||||
} else emptyList()
|
||||
|
||||
val agent = ChatAgent(
|
||||
id = "agentik",
|
||||
storage = storage,
|
||||
conversationStore = sqliteStores.conversations,
|
||||
messageStore = sqliteStores.messages,
|
||||
workingMemoryStore = sqliteStores.workingMemory,
|
||||
reflectionStore = sqliteStores.reflections,
|
||||
llm = llm,
|
||||
llmConfig = config.llm,
|
||||
tools = mcpRegistry.namedTools,
|
||||
@@ -365,7 +368,8 @@ private fun runServer() {
|
||||
if (config.debug.endpoints) {
|
||||
debugRoutes(
|
||||
agent = agent,
|
||||
storage = storage,
|
||||
workingMemoryStore = sqliteStores.workingMemory,
|
||||
reflectionStore = sqliteStores.reflections,
|
||||
reflector = reflector,
|
||||
skillMiner = skillMiner,
|
||||
skillStore = skillStore,
|
||||
@@ -400,29 +404,20 @@ private fun runServer() {
|
||||
if (config.debug.endpoints) {
|
||||
println(" debug endpoints: enabled (/debug/reflect, /debug/skill-mine, /debug/curate, /debug/compact, /debug/tokens)")
|
||||
}
|
||||
// Token stats по существующим диалогам (агрегат на старте — каждая запись
|
||||
// парсится из payload_json, ну >100 turns и БД приличная — но в рамках
|
||||
// стартапа это терпимо).
|
||||
val existingConvs = kotlinx.coroutines.runBlocking { storage.conversationStore.list(offset = 0, limit = 1000) }
|
||||
// Раньше здесь был агрегат tokenStats() по всем conv'ам при старте. Метод
|
||||
// убран из :message-log-api (MessageStore стал чисто read-only list+listFlow);
|
||||
// см. agentik :message-log-api/MessageStore.kt. Token accounting теперь
|
||||
// доступен через assistant-сообщения с TurnTokens (см. MessageRecord.AssistantMessage).
|
||||
val existingConvs = kotlinx.coroutines.runBlocking { sqliteStores.conversations.list(offset = 0, limit = 1000) }
|
||||
if (existingConvs.isNotEmpty()) {
|
||||
var totalTurns = 0
|
||||
var totalIn = 0L
|
||||
var totalOut = 0L
|
||||
for (c in existingConvs) {
|
||||
if (c.isTemporal) continue
|
||||
val s = kotlinx.coroutines.runBlocking { storage.messageStore.tokenStats(c.id) }
|
||||
totalTurns += s.turns
|
||||
totalIn += s.inputTokens
|
||||
totalOut += s.outputTokens
|
||||
}
|
||||
if (totalTurns > 0) {
|
||||
println(" tokens: ${existingConvs.size} convs, $totalTurns turns, in=${totalIn}, out=${totalOut}, total=${totalIn + totalOut}")
|
||||
}
|
||||
println(" conversations: ${existingConvs.size} (active)")
|
||||
}
|
||||
Runtime.getRuntime().addShutdownHook(Thread {
|
||||
agent.close()
|
||||
mcpRegistry.close()
|
||||
storage.close()
|
||||
})
|
||||
Runtime.getRuntime().addShutdownHook(Thread {
|
||||
sqliteStores.close()
|
||||
llm.close()
|
||||
memorySystem?.close()
|
||||
})
|
||||
+5
-5
@@ -12,10 +12,10 @@ import pw.binom.agentik.memory.ConversationTurn
|
||||
import pw.binom.agentik.memory.MemoryReviewer
|
||||
import pw.binom.agentik.memory.MemoryStore
|
||||
import pw.binom.agentik.skills.SkillStore
|
||||
import pw.binom.agentik.storage.Content
|
||||
import pw.binom.agentik.storage.ReflectionStore
|
||||
import pw.binom.agentik.storage.WorkingMemoryEntry
|
||||
import pw.binom.agentik.storage.WorkingMemoryStore
|
||||
import pw.binom.agentik.journal.Content
|
||||
import pw.binom.agentik.reflection.ReflectionStore
|
||||
import pw.binom.agentik.context.WorkingMemoryEntry
|
||||
import pw.binom.agentik.context.ContextStore
|
||||
import pw.binom.agentik.llm.tools.LlmReflector
|
||||
import pw.binom.agentik.llm.tools.SkillMiner
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
@@ -46,7 +46,7 @@ internal data class BackgroundConfig(
|
||||
|
||||
internal class BackgroundScheduler(
|
||||
private val state: ConversationState,
|
||||
private val workingMemory: WorkingMemoryStore,
|
||||
private val workingMemory: ContextStore,
|
||||
private val config: BackgroundConfig,
|
||||
private val backgroundEvents: BackgroundEventBus,
|
||||
) {
|
||||
+95
-35
@@ -1,38 +1,47 @@
|
||||
package pw.binom.agentik.standalone.agent
|
||||
|
||||
import kotlin.time.Instant
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import kotlinx.coroutines.flow.emitAll
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import pw.binom.agentik.llm.tools.ContextCompactor
|
||||
import pw.binom.agentik.llm.tools.LlmReflector
|
||||
import pw.binom.agentik.llm.tools.SkillMiner
|
||||
import pw.binom.agentik.memory.MemoryPrefetcher
|
||||
import pw.binom.agentik.memory.MemoryReviewer
|
||||
import pw.binom.agentik.memory.MemorySystemGuidance
|
||||
import pw.binom.agentik.proto.Agent as ProtoAgent
|
||||
import pw.binom.agentik.proto.AgentEvent
|
||||
import pw.binom.agentik.outbox.AgentEvent
|
||||
import pw.binom.agentik.outbox.CommonEvent
|
||||
import pw.binom.agentik.outbox.MutableOutboxStore
|
||||
import pw.binom.agentik.journal.JournalStore
|
||||
import pw.binom.agentik.outbox.OutboxStore
|
||||
import pw.binom.agentik.proto.Conversation as ProtoConversation
|
||||
import pw.binom.agentik.proto.Event as ProtoEvent
|
||||
import pw.binom.agentik.skills.SkillCatalog
|
||||
import pw.binom.agentik.skills.renderSystemPromptSection
|
||||
import pw.binom.agentik.standalone.agent.memory.MemoryToolsFactory
|
||||
import pw.binom.agentik.standalone.llm.LlmConfig
|
||||
import pw.binom.agentik.storage.ConversationRecord
|
||||
import pw.binom.agentik.storage.Reflection
|
||||
import pw.binom.agentik.storage.WorkingMemoryEntry
|
||||
import pw.binom.agentik.storage.StorageBundle
|
||||
import pw.binom.agentik.toolsets.EnableToolsetTool
|
||||
import pw.binom.agentik.journal.ConversationRecord
|
||||
import pw.binom.agentik.journal.ConversationStore
|
||||
import pw.binom.agentik.journal.Ids
|
||||
import pw.binom.agentik.reflection.Reflection
|
||||
import pw.binom.agentik.reflection.ReflectionStore
|
||||
import pw.binom.agentik.journal.MutableJournalStore
|
||||
import pw.binom.agentik.context.ContextStore
|
||||
import pw.binom.agentik.toolsets.DisableToolsetTool
|
||||
import pw.binom.agentik.toolsets.EnableToolsetTool
|
||||
import pw.binom.agentik.toolsets.NamedTool
|
||||
import pw.binom.agentik.toolsets.SystemPromptToolsetSection
|
||||
import pw.binom.agentik.toolsets.ToolsetContribution
|
||||
import pw.binom.agentik.toolsets.ToolsetDispatchPolicy
|
||||
import pw.binom.agentik.toolsets.ToolsetRegistry
|
||||
import pw.binom.litert.LiteLlm
|
||||
import kotlin.time.Instant
|
||||
import pw.binom.agentik.llm.tools.LlmReflector
|
||||
import pw.binom.agentik.llm.tools.SkillMiner
|
||||
import pw.binom.agentik.llm.tools.LlmMemoryReviewer
|
||||
import pw.binom.agentik.llm.tools.ContextCompactor
|
||||
import pw.binom.agentik.toolsets.NamedTool
|
||||
|
||||
/**
|
||||
* Stateful [ProtoAgent] на базе SQLite (история + working memory) и
|
||||
@@ -56,7 +65,10 @@ import pw.binom.agentik.toolsets.NamedTool
|
||||
*/
|
||||
class ChatAgent(
|
||||
override val id: String,
|
||||
private val storage: pw.binom.agentik.storage.StorageBundle,
|
||||
private val conversationStore: ConversationStore,
|
||||
private val messageStore: MutableJournalStore,
|
||||
private val workingMemoryStore: ContextStore,
|
||||
private val reflectionStore: ReflectionStore,
|
||||
private val llm: LiteLlm,
|
||||
private val llmConfig: LlmConfig,
|
||||
private val tools: List<NamedTool> = emptyList(),
|
||||
@@ -188,25 +200,57 @@ class ChatAgent(
|
||||
},
|
||||
)
|
||||
|
||||
private val agentEvents = MutableSharedFlow<AgentEvent>(
|
||||
extraBufferCapacity = 64,
|
||||
/**
|
||||
* Единый канал всех событий агента — bounded tail с auto-TTL.
|
||||
*
|
||||
* Заменил ранее существовавшие два канала:
|
||||
* - `agentEvents: MutableSharedFlow<AgentEvent>` (agent lifecycle)
|
||||
* - per-conv `ConversationEvents._flow: MutableSharedFlow<ProtoEvent>`
|
||||
*
|
||||
* Теперь оба пишут сюда через [MutableOutboxStore.append], а consumer'ы
|
||||
* читают через [EventStore.events]/[conversationEvents]/[agentEvents].
|
||||
*
|
||||
* **Live tail + auto-TTL** — клиенты больше не должны заботиться о persistence
|
||||
* или подписке на два отдельных канала.
|
||||
*/
|
||||
private val eventStore: MutableOutboxStore = pw.binom.agentik.outbox.inmemory.InMemoryOutboxStore(
|
||||
maxMessages = null,
|
||||
ttl = null,
|
||||
)
|
||||
|
||||
/**
|
||||
* Read-only view of [messageStore] для HTTP-фасада в `:server`
|
||||
* (`Route.agentikAgent` → `/journal/...` endpoint'ы).
|
||||
*
|
||||
* [MutableJournalStore] == [MutableJournalStore] (typealias), поэтому
|
||||
* narrowing с мутабельного writer'а на read-only view тривиальна.
|
||||
* Используется [Agent.journal] override'ом из `:proto`.
|
||||
*/
|
||||
override val journal: JournalStore
|
||||
get() = messageStore
|
||||
|
||||
/**
|
||||
* Read-only view of [eventStore] для HTTP-фасада в `:server`
|
||||
* (`Route.agentikAgent` → `/outbox/...` endpoint'ы).
|
||||
*
|
||||
* [MutableOutboxStore] == [MutableOutboxStore] (typealias), narrowing тривиальна.
|
||||
* Используется [Agent.outbox] override'ом из `:proto`.
|
||||
*/
|
||||
override val outbox: OutboxStore
|
||||
get() = eventStore
|
||||
|
||||
/** Защищает карту живых диалогов. */
|
||||
private val liveLock = Mutex()
|
||||
private val live: MutableMap<String, ChatConversation> = HashMap()
|
||||
|
||||
/** Live-подписка на события уровня агента (создание/удаление/переименование). */
|
||||
override fun events(after: Instant): Flow<AgentEvent> {
|
||||
// Реализация событийной шины упрощённая: возвращаем общий поток.
|
||||
// Фильтр по `after` не делаем — для v1 после-семантика не нужна
|
||||
// (см. Memory #3704: replay-free, бэкфилл через getConversations/getConversation).
|
||||
return agentEvents.asSharedFlow()
|
||||
}
|
||||
// events()/allEvents() больше НЕ override'ятся — эти методы удалены
|
||||
// из :proto.Agent после миграции событий в outbox-сущность.
|
||||
// Live-подписки теперь идут через Agent.outbox.events() / .agentEvents()
|
||||
// (см. README :proto для контракта catchup+live).
|
||||
|
||||
override fun createConversation(temp: Boolean): ProtoConversation {
|
||||
val now = now()
|
||||
val id = pw.binom.agentik.storage.Ids.new("conv")
|
||||
val id = pw.binom.agentik.journal.Ids.new("conv")
|
||||
val rec = ConversationRecord(
|
||||
id = id,
|
||||
title = null,
|
||||
@@ -218,12 +262,16 @@ class ChatAgent(
|
||||
// и не переживают рестарт агента (см. Memory #3709).
|
||||
if (!temp) {
|
||||
runBlocking {
|
||||
storage.conversationStore.upsert(rec)
|
||||
conversationStore.upsert(rec)
|
||||
}
|
||||
}
|
||||
val conv = ChatConversation(
|
||||
record = rec,
|
||||
storage = storage,
|
||||
conversationStore = conversationStore,
|
||||
messageStore = messageStore,
|
||||
workingMemoryStore = workingMemoryStore,
|
||||
reflectionStore = reflectionStore,
|
||||
eventStore = eventStore,
|
||||
llm = llm,
|
||||
systemPrompt = systemPrompt,
|
||||
tools = allTools,
|
||||
@@ -234,7 +282,6 @@ class ChatAgent(
|
||||
contextWindow = contextWindow,
|
||||
compressionThreshold = compressionThreshold,
|
||||
contextCompactor = contextCompactor,
|
||||
reflectionStore = storage.reflectionStore,
|
||||
reflector = reflector,
|
||||
skillMiner = skillMiner,
|
||||
skillMiningStore = skillStore,
|
||||
@@ -242,13 +289,20 @@ class ChatAgent(
|
||||
runBlocking {
|
||||
liveLock.withLock { live[conv.id] = conv }
|
||||
}
|
||||
agentEvents.tryEmit(AgentEvent.Created(date = now(), conversationId = conv.id))
|
||||
runBlocking {
|
||||
eventStore.append(
|
||||
CommonEvent.Agent(
|
||||
date = now(),
|
||||
event = AgentEvent.Created(date = now(), conversationId = conv.id),
|
||||
)
|
||||
)
|
||||
}
|
||||
return conv
|
||||
}
|
||||
|
||||
override suspend fun getConversation(id: String): ProtoConversation? {
|
||||
liveLock.withLock { live[id] }?.let { if (!it.isClosed) return it }
|
||||
val rec = storage.conversationStore.get(id) ?: return null
|
||||
val rec = conversationStore.get(id) ?: return null
|
||||
return newConversation(rec).also {
|
||||
liveLock.withLock { live[id] = it }
|
||||
}
|
||||
@@ -257,13 +311,16 @@ class ChatAgent(
|
||||
override suspend fun deleteConversation(id: String): Boolean {
|
||||
val conv = liveLock.withLock { live.remove(id) }
|
||||
conv?.close()
|
||||
val ok = storage.conversationStore.delete(id)
|
||||
if (ok) agentEvents.tryEmit(AgentEvent.Deleted(date = now(), id = id))
|
||||
val ok = conversationStore.delete(id)
|
||||
if (ok) {
|
||||
val event = AgentEvent.Deleted(date = now(), id = id)
|
||||
eventStore.append(CommonEvent.Agent(date = now(), event = event))
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
override suspend fun getConversations(offset: Int, limit: Int): List<ProtoConversation> =
|
||||
storage.conversationStore.list(offset = offset, limit = limit).map { rec ->
|
||||
conversationStore.list(offset = offset, limit = limit).map { rec ->
|
||||
liveLock.withLock { live[rec.id] }
|
||||
?: newConversation(rec).also {
|
||||
liveLock.withLock { live[rec.id] = it }
|
||||
@@ -272,7 +329,11 @@ class ChatAgent(
|
||||
|
||||
private fun newConversation(rec: ConversationRecord): ChatConversation = ChatConversation(
|
||||
record = rec,
|
||||
storage = storage,
|
||||
conversationStore = conversationStore,
|
||||
messageStore = messageStore,
|
||||
workingMemoryStore = workingMemoryStore,
|
||||
reflectionStore = reflectionStore,
|
||||
eventStore = eventStore,
|
||||
llm = llm,
|
||||
systemPrompt = systemPrompt,
|
||||
tools = allTools,
|
||||
@@ -283,7 +344,6 @@ class ChatAgent(
|
||||
contextWindow = contextWindow,
|
||||
compressionThreshold = compressionThreshold,
|
||||
contextCompactor = contextCompactor,
|
||||
reflectionStore = storage.reflectionStore,
|
||||
reflector = reflector,
|
||||
skillMiner = skillMiner,
|
||||
skillMiningStore = skillStore,
|
||||
+5
-5
@@ -6,10 +6,10 @@ import pw.binom.agentik.memory.ConversationTurn
|
||||
import pw.binom.agentik.memory.MemoryReviewer
|
||||
import pw.binom.agentik.memory.MemoryStore
|
||||
import pw.binom.agentik.standalone.agent.memory.materializeReviewNote
|
||||
import pw.binom.agentik.storage.Content
|
||||
import pw.binom.agentik.storage.WorkingMemoryEntry
|
||||
import pw.binom.agentik.storage.WorkingMemoryRow
|
||||
import pw.binom.agentik.storage.WorkingMemoryStore
|
||||
import pw.binom.agentik.journal.Content
|
||||
import pw.binom.agentik.context.WorkingMemoryEntry
|
||||
import pw.binom.agentik.context.WorkingMemoryRow
|
||||
import pw.binom.agentik.context.ContextStore
|
||||
import pw.binom.litert.LiteContentPart
|
||||
import pw.binom.litert.LiteConversation
|
||||
import pw.binom.litert.LiteConversationConfig
|
||||
@@ -26,7 +26,7 @@ internal class CompactionCoordinator(
|
||||
private val contextCompactor: ContextCompactor?,
|
||||
private val memoryReviewer: MemoryReviewer?,
|
||||
private val memoryStoreForReview: MemoryStore?,
|
||||
private val workingMemory: WorkingMemoryStore,
|
||||
private val workingMemory: ContextStore,
|
||||
private val liteLlm: LiteLlm,
|
||||
private val systemPrompt: String,
|
||||
private val backgroundEvents: BackgroundEventBus,
|
||||
+2
-2
@@ -3,8 +3,8 @@ package pw.binom.agentik.standalone.agent
|
||||
import mu.KotlinLogging
|
||||
import pw.binom.agentik.memory.MemoryPrefetcher
|
||||
import pw.binom.litert.LiteContentPart
|
||||
import pw.binom.agentik.storage.MessageContext
|
||||
import pw.binom.agentik.storage.MessageOrigin
|
||||
import pw.binom.agentik.journal.MessageContext
|
||||
import pw.binom.agentik.journal.MessageOrigin
|
||||
|
||||
internal class ContextBuilder(
|
||||
private val memoryPrefetcher: MemoryPrefetcher?,
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package pw.binom.agentik.standalone.agent
|
||||
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import pw.binom.agentik.outbox.MutableOutboxStore
|
||||
import pw.binom.agentik.outbox.CommonEvent
|
||||
import pw.binom.agentik.proto.Event as ProtoEvent
|
||||
|
||||
internal class ConversationEvents(
|
||||
private val globalEventStore: MutableOutboxStore,
|
||||
private val conversationId: String,
|
||||
) {
|
||||
fun tryEmit(event: ProtoEvent): Boolean {
|
||||
runBlocking {
|
||||
globalEventStore.append(
|
||||
CommonEvent.Conversation(
|
||||
date = event.date,
|
||||
conversationId = conversationId,
|
||||
event = event,
|
||||
)
|
||||
)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fun events(after: kotlin.time.Instant?): Flow<ProtoEvent> =
|
||||
globalEventStore.conversationEvents(after = after, conversationId = conversationId)
|
||||
.map { it.event }
|
||||
}
|
||||
+47
-39
@@ -24,22 +24,21 @@ import pw.binom.agentik.memory.MemoryReviewer
|
||||
import pw.binom.agentik.memory.MemoryStore
|
||||
import pw.binom.agentik.proto.Content as ProtoContent
|
||||
import pw.binom.agentik.proto.Conversation as ProtoConversation
|
||||
import pw.binom.agentik.proto.Event as ProtoEvent
|
||||
import pw.binom.agentik.outbox.Event as ProtoEvent
|
||||
import pw.binom.agentik.reflection.ReflectionStore
|
||||
import pw.binom.agentik.proto.Message as ProtoMessage
|
||||
import pw.binom.agentik.proto.MessageContext as ProtoMessageContext
|
||||
import pw.binom.agentik.skills.SkillStore
|
||||
import pw.binom.agentik.storage.Content
|
||||
import pw.binom.agentik.storage.ConversationRecord
|
||||
import pw.binom.agentik.storage.ConversationStore
|
||||
import pw.binom.agentik.storage.MessageContext
|
||||
import pw.binom.agentik.storage.MessageOrigin
|
||||
import pw.binom.agentik.storage.MessageRecord
|
||||
import pw.binom.agentik.storage.MessageStore
|
||||
import pw.binom.agentik.storage.ReflectionStore
|
||||
import pw.binom.agentik.storage.StorageBundle
|
||||
import pw.binom.agentik.storage.TurnTokens
|
||||
import pw.binom.agentik.storage.WorkingMemoryEntry
|
||||
import pw.binom.agentik.storage.WorkingMemoryStore
|
||||
import pw.binom.agentik.journal.Content
|
||||
import pw.binom.agentik.journal.ConversationRecord
|
||||
import pw.binom.agentik.journal.ConversationStore
|
||||
import pw.binom.agentik.journal.MessageContext
|
||||
import pw.binom.agentik.journal.MessageOrigin
|
||||
import pw.binom.agentik.journal.MessageRecord
|
||||
import pw.binom.agentik.journal.MutableJournalStore as MutableJournalStore
|
||||
import pw.binom.agentik.journal.TurnTokens
|
||||
import pw.binom.agentik.context.WorkingMemoryEntry
|
||||
import pw.binom.agentik.context.ContextStore
|
||||
import pw.binom.agentik.toolsets.ToolsetDispatchPolicy
|
||||
import pw.binom.litert.LiteContentPart
|
||||
import pw.binom.litert.LiteConversation
|
||||
@@ -55,7 +54,11 @@ import pw.binom.agentik.toolsets.NamedTool
|
||||
|
||||
class ConversationLoop(
|
||||
record: ConversationRecord,
|
||||
private val storage: StorageBundle,
|
||||
private val conversationStore: ConversationStore,
|
||||
private val messageStore: MutableJournalStore,
|
||||
private val workingMemoryStore: ContextStore,
|
||||
private val reflectionStore: ReflectionStore?,
|
||||
private val eventStore: pw.binom.agentik.outbox.MutableOutboxStore,
|
||||
private val llm: LiteLlm,
|
||||
private val systemPrompt: String,
|
||||
private val tools: List<NamedTool> = emptyList(),
|
||||
@@ -66,7 +69,6 @@ class ConversationLoop(
|
||||
private val contextWindow: Int? = null,
|
||||
private val compressionThreshold: Double = 0.8,
|
||||
private val contextCompactor: ContextCompactor? = null,
|
||||
private val reflectionStore: ReflectionStore? = null,
|
||||
private val reflector: LlmReflector? = null,
|
||||
private val skillMiner: SkillMiner? = null,
|
||||
private val skillMiningStore: SkillStore? = null,
|
||||
@@ -84,15 +86,14 @@ class ConversationLoop(
|
||||
agentScope = agentScope,
|
||||
)
|
||||
|
||||
private val events = ConversationEvents()
|
||||
private val events = ConversationEvents(
|
||||
globalEventStore = eventStore,
|
||||
conversationId = state.id,
|
||||
)
|
||||
|
||||
/** Per-conversation background event bus. Lifecycle scoped к этому ConversationLoop. */
|
||||
private val backgroundEvents = BackgroundEventBus()
|
||||
|
||||
private val conversationStore: ConversationStore get() = storage.conversationStore
|
||||
private val messageStore: MessageStore get() = storage.messageStore
|
||||
private val workingMemory: WorkingMemoryStore get() = storage.workingMemoryStore
|
||||
|
||||
private val toolsByName: MutableMap<String, NamedTool> = tools.associateBy { it.name }.toMutableMap()
|
||||
|
||||
private val contextBuilder = ContextBuilder(memoryPrefetcher = memoryPrefetcher)
|
||||
@@ -104,7 +105,7 @@ class ConversationLoop(
|
||||
contextCompactor = contextCompactor,
|
||||
memoryReviewer = memoryReviewer,
|
||||
memoryStoreForReview = memoryStoreForReview,
|
||||
workingMemory = workingMemory,
|
||||
workingMemory = workingMemoryStore,
|
||||
liteLlm = llm,
|
||||
systemPrompt = systemPrompt,
|
||||
backgroundEvents = backgroundEvents,
|
||||
@@ -124,7 +125,7 @@ class ConversationLoop(
|
||||
|
||||
private val backgroundScheduler = BackgroundScheduler(
|
||||
state = state,
|
||||
workingMemory = workingMemory,
|
||||
workingMemory = workingMemoryStore,
|
||||
config = BackgroundConfig(
|
||||
memoryReviewer = memoryReviewer,
|
||||
memoryStore = memoryStoreForReview,
|
||||
@@ -175,7 +176,7 @@ class ConversationLoop(
|
||||
|
||||
if (!state.isTemporal) {
|
||||
messageStore.append(userRecord)
|
||||
workingMemory.append(
|
||||
workingMemoryStore.append(
|
||||
conversationId = id,
|
||||
entry = WorkingMemoryEntry.User(
|
||||
sourceMessageId = userMessageId,
|
||||
@@ -195,8 +196,23 @@ class ConversationLoop(
|
||||
}
|
||||
|
||||
override suspend fun interrupt() {
|
||||
if (activeTurn?.isActive != true) {
|
||||
log.info { "interrupt() no-op: no active turn for $id" }
|
||||
// Всегда ставим флаг — даже если activeTurn ещё не стартовал.
|
||||
// runTurn проверяет interrupted.get() при входе (short-circuit) и в
|
||||
// каждой итерации цикла + finally. Если turn запустится ПОСЛЕ нашего
|
||||
// interrupt() — он увидит флаг на entry и сразу завершится без
|
||||
// реального LLM-вызова. Если turn уже идёт — Interrupted + End придут
|
||||
// в finally.
|
||||
//
|
||||
// Раньше здесь был early-return при `!activeTurn?.isActive`, но это
|
||||
// давало race с точки зрения тестов: send() может завершиться
|
||||
// быстрее (например, на ksqlite-бэкенде, где messageStore.append
|
||||
// практически мгновенный), и interrupt(), вызванный после
|
||||
// delay(200) от launch send(), видел completed Job → no-op →
|
||||
// Interrupted event не эмитится.
|
||||
val wasActive = activeTurn?.isActive
|
||||
if (wasActive != null && !wasActive) {
|
||||
// Предыдущий turn уже завершился — interrupt() действительно no-op.
|
||||
log.info { "interrupt() no-op: previous turn already completed for $id" }
|
||||
return
|
||||
}
|
||||
interrupted.set(true)
|
||||
@@ -205,7 +221,7 @@ class ConversationLoop(
|
||||
}
|
||||
|
||||
override fun events(after: Instant): Flow<ProtoEvent> =
|
||||
events.flow
|
||||
events.events(after)
|
||||
|
||||
override suspend fun getMessages(after: Instant, offset: Int, limit: Int): List<ProtoMessage> =
|
||||
messageStore.list(conversationId = id, after = after, offset = offset, limit = limit)
|
||||
@@ -408,7 +424,7 @@ class ConversationLoop(
|
||||
)
|
||||
messageStore.append(assistantRecord)
|
||||
|
||||
workingMemory.append(
|
||||
workingMemoryStore.append(
|
||||
conversationId = id,
|
||||
entry = WorkingMemoryEntry.Assistant(
|
||||
sourceMessageId = assistantId,
|
||||
@@ -418,7 +434,7 @@ class ConversationLoop(
|
||||
)
|
||||
|
||||
for (ex in toolExchanges) {
|
||||
workingMemory.append(
|
||||
workingMemoryStore.append(
|
||||
conversationId = id,
|
||||
entry = ex,
|
||||
now = assistantAt,
|
||||
@@ -466,7 +482,7 @@ class ConversationLoop(
|
||||
private fun now(): Instant =
|
||||
Instant.fromEpochMilliseconds(System.currentTimeMillis())
|
||||
|
||||
private fun newId(prefix: String): String = pw.binom.agentik.storage.Ids.new(prefix)
|
||||
private fun newId(prefix: String): String = pw.binom.agentik.journal.Ids.new(prefix)
|
||||
|
||||
private fun encodeArgsJson(arguments: Map<String, Any?>): String {
|
||||
val el = JsonElement.serializer()
|
||||
@@ -560,16 +576,8 @@ internal fun MessageRecord.toProto(): ProtoMessage = when (this) {
|
||||
message = message,
|
||||
code = code,
|
||||
)
|
||||
is MessageRecord.Summary -> ProtoMessage.AssistantMessage(
|
||||
id = id,
|
||||
date = createdAt,
|
||||
content = listOf(ProtoContent.Text(body = text)),
|
||||
)
|
||||
is MessageRecord.System -> ProtoMessage.UserMessage(
|
||||
id = id,
|
||||
date = createdAt,
|
||||
content = listOf(ProtoContent.Text(body = text)),
|
||||
)
|
||||
// MessageRecord sealed — все варианты покрыты выше (User/Assistant/ToolCall/ToolResult/Error).
|
||||
// Summary/System из :working-memory НЕ попадают в audit log (:message-log-api).
|
||||
}
|
||||
|
||||
private fun readTokenCount(liteConv: LiteConversation): Int? = try {
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
package pw.binom.agentik.standalone.agent
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import pw.binom.agentik.storage.ConversationRecord
|
||||
import pw.binom.agentik.journal.ConversationRecord
|
||||
import pw.binom.litert.LiteConversation
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
import pw.binom.agentik.toolsets.NamedTool
|
||||
+5
-5
@@ -4,10 +4,10 @@ import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.async
|
||||
import mu.KotlinLogging
|
||||
import pw.binom.agentik.proto.Event as ProtoEvent
|
||||
import pw.binom.agentik.storage.MessageRecord
|
||||
import pw.binom.agentik.storage.MessageStore
|
||||
import pw.binom.agentik.storage.WorkingMemoryEntry
|
||||
import pw.binom.agentik.outbox.Event as ProtoEvent
|
||||
import pw.binom.agentik.journal.MessageRecord
|
||||
import pw.binom.agentik.journal.MutableJournalStore as MutableJournalStore
|
||||
import pw.binom.agentik.context.WorkingMemoryEntry
|
||||
import pw.binom.agentik.toolsets.ToolsetDispatchPolicy
|
||||
import pw.binom.litert.LiteToolCall
|
||||
import pw.binom.litert.LiteTool
|
||||
@@ -16,7 +16,7 @@ import pw.binom.agentik.toolsets.NamedTool
|
||||
|
||||
internal class ToolDispatcher(
|
||||
private val state: ConversationState,
|
||||
private val messageStore: MessageStore,
|
||||
private val messageStore: MutableJournalStore,
|
||||
private val events: ConversationEvents,
|
||||
private val backgroundEvents: BackgroundEventBus,
|
||||
private val toolsByName: MutableMap<String, NamedTool>,
|
||||
@@ -1,21 +0,0 @@
|
||||
package pw.binom.agentik.standalone.agent
|
||||
|
||||
import kotlinx.coroutines.channels.BufferOverflow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import pw.binom.agentik.proto.Event as ProtoEvent
|
||||
|
||||
internal class ConversationEvents {
|
||||
private val _flow = MutableSharedFlow<ProtoEvent>(
|
||||
replay = 0,
|
||||
extraBufferCapacity = 4096,
|
||||
onBufferOverflow = BufferOverflow.DROP_OLDEST,
|
||||
)
|
||||
|
||||
val flow: SharedFlow<ProtoEvent> get() = _flow.asSharedFlow()
|
||||
|
||||
fun tryEmit(event: ProtoEvent): Boolean = _flow.tryEmit(event)
|
||||
|
||||
suspend fun emit(event: ProtoEvent) = _flow.emit(event)
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
Logback-конфиг для standalone.
|
||||
Уровень управляется env var AGENTIK_LOG_LEVEL (default INFO).
|
||||
Формат: timestamp [level] [thread] logger — message
|
||||
-->
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- Default уровень — INFO. Можно перебить через env: AGENTIK_LOG_LEVEL=DEBUG -->
|
||||
<root level="${AGENTIK_LOG_LEVEL:-INFO}">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
|
||||
<!-- Шумные библиотеки уводим в WARN. -->
|
||||
<logger name="io.netty" level="WARN"/>
|
||||
<logger name="io.ktor" level="INFO"/>
|
||||
<logger name="ai.onnxruntime" level="WARN"/>
|
||||
</configuration>
|
||||
-74
@@ -1,74 +0,0 @@
|
||||
package pw.binom.agentik.standalone.agent
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.time.Instant
|
||||
import pw.binom.agentik.skills.SkillCatalog
|
||||
import pw.binom.agentik.storage.Reflection
|
||||
|
||||
class ChatAgentReflectionTest {
|
||||
|
||||
@Test
|
||||
fun `buildSystemPrompt omits section when reflections empty`() {
|
||||
val prompt = buildSystemPrompt(
|
||||
base = "base",
|
||||
skills = SkillCatalog.EMPTY,
|
||||
memoryEnabled = false,
|
||||
soulBody = null,
|
||||
reflections = emptyList(),
|
||||
)
|
||||
assertFalse(prompt.contains("Self-reflection"))
|
||||
assertFalse(prompt.contains("слабые места"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `buildSystemPrompt includes section with weak spots when reflections non-empty`() {
|
||||
val r = Reflection(
|
||||
id = "r1",
|
||||
conversationId = null,
|
||||
createdAt = Instant.parse("2026-09-15T12:00:00Z"),
|
||||
turnsAnalyzed = 10,
|
||||
score = 2,
|
||||
summary = "плохо",
|
||||
weakSpots = listOf("медленно отвечаю на X", "путаю A и B"),
|
||||
)
|
||||
val prompt = buildSystemPrompt(
|
||||
base = "base",
|
||||
skills = SkillCatalog.EMPTY,
|
||||
memoryEnabled = false,
|
||||
soulBody = null,
|
||||
reflections = listOf(r),
|
||||
)
|
||||
assertTrue(prompt.contains("Self-reflection"))
|
||||
assertTrue(prompt.contains("слабые места"))
|
||||
assertTrue(prompt.contains("медленно отвечаю на X"))
|
||||
assertTrue(prompt.contains("путаю A и B"))
|
||||
assertTrue(prompt.contains("score=2/5"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `buildSystemPrompt places reflection section after memory and before soul prepend`() {
|
||||
val r = Reflection(
|
||||
id = "r1",
|
||||
conversationId = null,
|
||||
createdAt = Instant.parse("2026-09-15T12:00:00Z"),
|
||||
turnsAnalyzed = 5,
|
||||
score = 4,
|
||||
summary = "ok",
|
||||
weakSpots = listOf("minor issue"),
|
||||
)
|
||||
val prompt = buildSystemPrompt(
|
||||
base = "base",
|
||||
skills = SkillCatalog.EMPTY,
|
||||
memoryEnabled = true,
|
||||
soulBody = "I am a soul",
|
||||
reflections = listOf(r),
|
||||
)
|
||||
// soul первый, reflection последняя
|
||||
val soulIdx = prompt.indexOf("I am a soul")
|
||||
val reflIdx = prompt.indexOf("Self-reflection")
|
||||
assertTrue(soulIdx >= 0 && reflIdx >= 0)
|
||||
assertTrue(soulIdx < reflIdx, "soul должен идти перед reflection")
|
||||
}
|
||||
}
|
||||
@@ -1,621 +0,0 @@
|
||||
package pw.binom.agentik.standalone.agent
|
||||
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.flow.toList
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import pw.binom.agentik.proto.AgentEvent
|
||||
import pw.binom.agentik.proto.Content
|
||||
import pw.binom.agentik.proto.Event as ProtoEvent
|
||||
import pw.binom.agentik.skills.SkillCatalog
|
||||
import pw.binom.agentik.skills.SkillFile
|
||||
import pw.binom.agentik.standalone.llm.LlmBackend
|
||||
import pw.binom.agentik.standalone.llm.LlmConfig
|
||||
import pw.binom.agentik.storage.MessageRecord
|
||||
import pw.binom.agentik.storage.WorkingMemoryEntry
|
||||
import pw.binom.agentik.storage.sqlite.SqliteStores
|
||||
import pw.binom.litert.LiteContentPart
|
||||
import pw.binom.litert.LiteConversation
|
||||
import pw.binom.litert.LiteConversationConfig
|
||||
import pw.binom.litert.LiteDelta
|
||||
import pw.binom.litert.LiteLlm
|
||||
import pw.binom.litert.LiteMessage
|
||||
import pw.binom.litert.LiteRole
|
||||
import pw.binom.litert.LiteTool
|
||||
import pw.binom.litert.LiteToolCall
|
||||
import pw.binom.agentik.standalone.llm.OpenAiConfig
|
||||
import kotlin.test.AfterTest
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertIs
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.time.Instant
|
||||
import pw.binom.agentik.toolsets.NamedTool
|
||||
|
||||
class ChatAgentTest {
|
||||
|
||||
private lateinit var storage: pw.binom.agentik.storage.StorageBundle
|
||||
private lateinit var fakeLlm: FakeLiteLlm
|
||||
|
||||
@BeforeTest
|
||||
fun setup() {
|
||||
storage = SqliteStores.inMemory().asBundle()
|
||||
fakeLlm = FakeLiteLlm()
|
||||
}
|
||||
|
||||
@AfterTest
|
||||
fun tearDown() {
|
||||
storage.close()
|
||||
}
|
||||
|
||||
private fun newAgent(
|
||||
storage: pw.binom.agentik.storage.StorageBundle = this.storage,
|
||||
llm: LiteLlm = this.fakeLlm,
|
||||
tools: List<NamedTool> = emptyList(),
|
||||
skills: SkillCatalog = SkillCatalog.EMPTY,
|
||||
): ChatAgent = ChatAgent(
|
||||
id = "agentik",
|
||||
storage = storage,
|
||||
llm = llm,
|
||||
llmConfig = LlmConfig(
|
||||
backend = LlmBackend.OPENAI,
|
||||
systemPrompt = "be brief",
|
||||
openai = OpenAiConfig(baseUrl = "http://test", apiKey = "test", model = "test"),
|
||||
),
|
||||
tools = tools,
|
||||
skills = skills,
|
||||
)
|
||||
|
||||
@Test
|
||||
fun `createConversation does NOT seed system prompt into working memory`() = runTest {
|
||||
// System prompt живёт ТОЛЬКО in-memory в ChatConversation.systemPrompt
|
||||
// и едет в LLM через LiteConversationConfig.systemInstruction. В
|
||||
// working_memory ничего не пишется — старт system prompt чистый,
|
||||
// 0 entries.
|
||||
val agent = newAgent()
|
||||
val conv = agent.createConversation(temp = false) as ChatConversation
|
||||
|
||||
val wm = storage.workingMemoryStore.list(conv.id)
|
||||
assertEquals(0, wm.size)
|
||||
// System prompt виден через LiteConversationConfig, который LLM получит
|
||||
// при первом send (см. `send passes system prompt and past history to LLM on first send`).
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `skills are appended to the system prompt passed to LLM`() = runTest {
|
||||
// Skills добавляются в system prompt на лету при сборке ChatAgent.
|
||||
// Проверяем это через то, что увидит LLM — systemInstruction в
|
||||
// LiteConversationConfig (а не через working_memory, куда теперь
|
||||
// ничего про system prompt не пишется).
|
||||
val skills = SkillCatalog(
|
||||
listOf(SkillFile(name = "lint", description = "lint things", body = "SECRET BODY")),
|
||||
)
|
||||
val agent = newAgent(skills = skills)
|
||||
val conv = agent.createConversation(temp = false)
|
||||
fakeLlm.reply = "ok"
|
||||
conv.send(listOf(Content.Text("hi")))
|
||||
|
||||
val system = fakeLlm.lastConfig!!.systemInstruction
|
||||
assertNotNull(system)
|
||||
assertTrue("be brief" in system!!, "base prompt missing: $system")
|
||||
assertTrue("## Навыки" in system, "skills section missing: $system")
|
||||
assertTrue("lint" in system, "skill name missing: $system")
|
||||
assertTrue("lint things" in system, "skill description missing: $system")
|
||||
assertFalse("SECRET BODY" in system, "system prompt must not leak the skill body")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `read_skill tool is registered when skills present`() = runTest {
|
||||
val skills = SkillCatalog(
|
||||
listOf(SkillFile(name = "lint", description = "lint things", body = "SECRET BODY")),
|
||||
)
|
||||
val agent = newAgent(skills = skills)
|
||||
val conv = agent.createConversation(temp = false)
|
||||
fakeLlm.reply = "ok"
|
||||
conv.send(listOf(Content.Text("hi")))
|
||||
|
||||
val descriptors = fakeLlm.lastConfig!!.tools.map { it.describe() }
|
||||
assertTrue(descriptors.any { SkillReadTool.NAME in it }, "expected read_skill tool: $descriptors")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `no read_skill tool when skills absent`() = runTest {
|
||||
val agent = newAgent()
|
||||
val conv = agent.createConversation(temp = false)
|
||||
fakeLlm.reply = "ok"
|
||||
conv.send(listOf(Content.Text("hi")))
|
||||
|
||||
val descriptors = fakeLlm.lastConfig?.tools?.map { it.describe() } ?: emptyList()
|
||||
assertTrue(descriptors.none { SkillReadTool.NAME in it }, "unexpected read_skill tool: $descriptors")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getConversation returns null for unknown id`() = runTest {
|
||||
val agent = newAgent()
|
||||
assertNull(agent.getConversation("nope"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getConversations returns all stored persistent conversations`() = runTest {
|
||||
val agent = newAgent()
|
||||
agent.createConversation(temp = false)
|
||||
agent.createConversation(temp = true)
|
||||
val list = agent.getConversations(0, 10)
|
||||
// temp-беседы не персистятся, в списке только persistent
|
||||
assertEquals(1, list.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deleteConversation removes conversation and data`() = runTest {
|
||||
val agent = newAgent()
|
||||
val conv = agent.createConversation(temp = false)
|
||||
val id = conv.id
|
||||
|
||||
// добавим сообщение, чтобы потом убедиться, что каскад сработал
|
||||
storage.messageStore.append(
|
||||
pw.binom.agentik.storage.MessageRecord.UserMessage(
|
||||
id = "m1",
|
||||
conversationId = id,
|
||||
content = listOf(pw.binom.agentik.storage.Content.Text("hi")),
|
||||
createdAt = Instant.fromEpochMilliseconds(1_700_000_000_000),
|
||||
),
|
||||
)
|
||||
assertTrue(agent.deleteConversation(id))
|
||||
assertNull(agent.getConversation(id))
|
||||
assertNull(storage.conversationStore.get(id))
|
||||
assertEquals(emptyList(), storage.messageStore.listAll(id))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deleteConversation returns false for unknown id`() = runTest {
|
||||
val agent = newAgent()
|
||||
assertEquals(false, agent.deleteConversation("nope"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `send emits start_reasoning, start_response, append_text, end`() = runTest {
|
||||
val agent = newAgent()
|
||||
fakeLlm.reply = "hello back"
|
||||
val conv = agent.createConversation(temp = false)
|
||||
|
||||
conv.send(listOf(Content.Text("hi")))
|
||||
|
||||
// user message записан в audit + working memory
|
||||
val msgs = storage.messageStore.listAll(conv.id)
|
||||
assertEquals(2, msgs.size)
|
||||
assertEquals("hi", (msgs[0] as pw.binom.agentik.storage.MessageRecord.UserMessage).content.let {
|
||||
(it[0] as pw.binom.agentik.storage.Content.Text).body
|
||||
})
|
||||
assertEquals("hello back", (msgs[1] as pw.binom.agentik.storage.MessageRecord.AssistantMessage).content.let {
|
||||
(it[0] as pw.binom.agentik.storage.Content.Text).body
|
||||
})
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `send reconstructs conversation history from working memory`() = runTest {
|
||||
val agent = newAgent()
|
||||
fakeLlm.rememberHistory = true
|
||||
fakeLlm.reply = "first reply"
|
||||
val conv1 = agent.createConversation(temp = false)
|
||||
conv1.send(listOf(Content.Text("first user")))
|
||||
|
||||
// Новая беседа не должна видеть историю первой
|
||||
val conv2 = agent.createConversation(temp = false)
|
||||
fakeLlm.reply = "second reply"
|
||||
conv2.send(listOf(Content.Text("second user")))
|
||||
|
||||
// В working_memory теперь НЕТ System-entries — только user + assistant.
|
||||
// Системный промт живёт в ChatConversation.systemPrompt и едет в LLM
|
||||
// через LiteConversationConfig.systemInstruction.
|
||||
val wm1 = storage.workingMemoryStore.list(conv1.id)
|
||||
assertEquals(2, wm1.size)
|
||||
val wm2 = storage.workingMemoryStore.list(conv2.id)
|
||||
assertEquals(2, wm2.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `send passes system prompt and past history to LLM on first send`() = runTest {
|
||||
val agent = newAgent()
|
||||
fakeLlm.rememberHistory = true
|
||||
val conv = agent.createConversation(temp = false)
|
||||
fakeLlm.reply = "hi"
|
||||
conv.send(listOf(Content.Text("hello")))
|
||||
|
||||
// Длинно-живущий LiteConversation: первый send создаёт его с systemInstruction
|
||||
// и пустыми initialMessages (свежее user-сообщение пойдёт через sendStreamContents).
|
||||
assertNotNull(fakeLlm.lastConfig)
|
||||
assertEquals("be brief", fakeLlm.lastConfig!!.systemInstruction)
|
||||
assertEquals(0, fakeLlm.lastConfig!!.initialMessages.size)
|
||||
// Свежее user-сообщение отправлено через sendStreamContents
|
||||
assertEquals(1, fakeLlm.conversations.size)
|
||||
val sent = fakeLlm.lastContents
|
||||
assertNotNull(sent)
|
||||
assertEquals(1, sent.size)
|
||||
assertEquals("hello", (sent[0] as LiteContentPart.Text).text)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `multi-turn conversation accumulates history but recreates LiteConv each turn`() = runTest {
|
||||
// Новая семантика (radical close+recreate после commit 7):
|
||||
// каждый turn закрывает LiteConv и на следующем send() создаёт новую
|
||||
// через getOrCreateLiteConversation, которая пересобирает initialMessages
|
||||
// из working memory. То есть LiteConv — один на turn, не на диалог.
|
||||
// Преимущество: interrupt можно сделать тривиально (close + cancelProcess),
|
||||
// KV-cache жертвуем ради предсказуемости (~2s prefill на Gemma-4-E2B).
|
||||
val agent = newAgent()
|
||||
fakeLlm.rememberHistory = true
|
||||
val conv = agent.createConversation(temp = false)
|
||||
|
||||
fakeLlm.reply = "first reply"
|
||||
conv.send(listOf(Content.Text("first user")))
|
||||
// первый turn: WM = [user, assistant]
|
||||
assertEquals(2, storage.workingMemoryStore.list(conv.id).size)
|
||||
|
||||
fakeLlm.reply = "second reply"
|
||||
conv.send(listOf(Content.Text("second user")))
|
||||
// второй turn: WM должен вырасти до [user, assistant, user, assistant]
|
||||
val wm = storage.workingMemoryStore.list(conv.id)
|
||||
System.err.println("[TEST] wm.size=${wm.size}")
|
||||
wm.forEachIndexed { i, row -> System.err.println("[TEST] $i: ${row.entry::class.simpleName} id=${row.id}") }
|
||||
assertEquals(4, wm.size)
|
||||
// Новая семантика: один LiteConv на turn → два LiteConv после двух send'ов.
|
||||
assertEquals(2, fakeLlm.conversations.size)
|
||||
// Второй LiteConv создан с initialMessages из working memory, ИСКЛЮЧАЯ pending user2
|
||||
// (он передаётся в sendStreamContents, чтобы не дублироваться).
|
||||
val reopened = fakeLlm.conversations.last()
|
||||
assertEquals(2, reopened.initialMessages.size)
|
||||
assertEquals("first user", reopened.initialMessages[0].text)
|
||||
assertEquals(LiteRole.USER, reopened.initialMessages[0].role)
|
||||
assertEquals("first reply", reopened.initialMessages[1].text)
|
||||
assertEquals(LiteRole.MODEL, reopened.initialMessages[1].role)
|
||||
// После sendStreamContents (с user2 + сгенерированный asst2) mutableHistory = 4
|
||||
assertEquals(4, reopened.history.size)
|
||||
assertEquals("second reply", reopened.history.last().text)
|
||||
assertEquals(LiteRole.MODEL, reopened.history.last().role)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `reloaded conversation reconstructs LiteConversation from working memory`() = runTest {
|
||||
val agent = newAgent()
|
||||
fakeLlm.rememberHistory = true
|
||||
val conv = agent.createConversation(temp = false)
|
||||
fakeLlm.reply = "first reply"
|
||||
conv.send(listOf(Content.Text("first user")))
|
||||
val convId = conv.id
|
||||
conv.close()
|
||||
|
||||
// Открываем новое ChatConversation с тем же id — LiteConversation должен
|
||||
// быть создан заново из working memory (первый user+assistant как initial).
|
||||
val reopened = agent.getConversation(convId)!!
|
||||
fakeLlm.reply = "second reply"
|
||||
reopened.send(listOf(Content.Text("second user")))
|
||||
|
||||
val allConvs = fakeLlm.conversations
|
||||
assertEquals(2, allConvs.size) // original + reopened
|
||||
val reopenedLite = allConvs.last()
|
||||
// Initial messages: только прошлые user+assistant (НЕ включая текущий "second user")
|
||||
assertEquals(2, reopenedLite.initialMessages.size)
|
||||
assertEquals("first user", reopenedLite.initialMessages[0].text)
|
||||
assertEquals(LiteRole.USER, reopenedLite.initialMessages[0].role)
|
||||
assertEquals("first reply", reopenedLite.initialMessages[1].text)
|
||||
assertEquals(LiteRole.MODEL, reopenedLite.initialMessages[1].role)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `LLM failure emits error event and persists error message`() = runTest {
|
||||
val agent = newAgent()
|
||||
fakeLlm.failMessage = "boom from llm"
|
||||
val conv = agent.createConversation(temp = false) as ChatConversation
|
||||
|
||||
val events = mutableListOf<ProtoEvent>()
|
||||
val job = launch(start = kotlinx.coroutines.CoroutineStart.UNDISPATCHED) {
|
||||
conv.events(Instant.DISTANT_PAST).collect { events.add(it) }
|
||||
}
|
||||
conv.send(listOf(Content.Text("hi")))
|
||||
delay(50)
|
||||
job.cancel()
|
||||
|
||||
assertTrue(events.any { it is ProtoEvent.Error && it.message == "boom from llm" }, "events=$events")
|
||||
assertTrue(events.any { it is ProtoEvent.End }, "events=$events")
|
||||
|
||||
val msgs = storage.messageStore.listAll(conv.id)
|
||||
assertEquals(2, msgs.size)
|
||||
assertIs<pw.binom.agentik.storage.MessageRecord.UserMessage>(msgs[0])
|
||||
val err = assertIs<pw.binom.agentik.storage.MessageRecord.Error>(msgs[1])
|
||||
assertEquals("boom from llm", err.message)
|
||||
|
||||
// backfill через getMessages (polling/reconnect) тоже видит ошибку
|
||||
val proto = conv.getMessages(Instant.DISTANT_PAST, offset = 0, limit = 10)
|
||||
assertTrue(
|
||||
proto.any { it is pw.binom.agentik.proto.Message.Error && it.message == "boom from llm" },
|
||||
"history=$proto",
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `interrupt mid-slow-stream preserves user message and no assistant`() = runTest {
|
||||
// Новая семантика interrupt (commit 7): ставится флаг, LiteConv.cancel()
|
||||
// бросает CancellationException в стриме, runTurn выходит через finally.
|
||||
// Если turn не успел ничего сгенерить (reply.isEmpty() && toolExchanges.isEmpty())
|
||||
// — AssistantMessage в audit log НЕ пишется. Только user + End/Interrupted.
|
||||
val agent = newAgent()
|
||||
fakeLlm.slow = true
|
||||
val conv = agent.createConversation(temp = false)
|
||||
|
||||
// Подписываемся на events ДО send() — SharedFlow без replay, после
|
||||
// отправки событий подписка ничего не увидит.
|
||||
val events = mutableListOf<ProtoEvent>()
|
||||
val eventsJob = launch(start = kotlinx.coroutines.CoroutineStart.UNDISPATCHED) {
|
||||
conv.events(Instant.DISTANT_PAST).collect { events.add(it) }
|
||||
}
|
||||
|
||||
val sendJob = launch {
|
||||
try {
|
||||
conv.send(listOf(Content.Text("hi")))
|
||||
} catch (_: kotlinx.coroutines.CancellationException) {
|
||||
// ok
|
||||
}
|
||||
}
|
||||
// ждём, пока корутина дойдёт до sendStreamContents и повиснет на slow-эмиссии
|
||||
delay(200)
|
||||
conv.interrupt()
|
||||
sendJob.join()
|
||||
eventsJob.cancel()
|
||||
|
||||
// audit: только user (assistant не успел сгенериться)
|
||||
val msgs = storage.messageStore.listAll(conv.id)
|
||||
assertEquals(1, msgs.size)
|
||||
assertIs<pw.binom.agentik.storage.MessageRecord.UserMessage>(msgs[0])
|
||||
|
||||
// working memory: только user (assistant skipped because пустой)
|
||||
val wm = storage.workingMemoryStore.list(conv.id)
|
||||
assertEquals(1, wm.size)
|
||||
assertTrue(wm[0].entry is WorkingMemoryEntry.User)
|
||||
|
||||
// events: должны включать Interrupted + End
|
||||
assertTrue(events.any { it is ProtoEvent.Interrupted }, "events=$events")
|
||||
assertTrue(events.any { it is ProtoEvent.End }, "events=$events")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `interrupt after tool execution preserves tool result in working memory`() = runTest {
|
||||
// Сценарий "LLM вызвал тул, инструмент выполнился, потом interrupt()":
|
||||
// 1. LLM скриптован на ToolCalls([echo_tool])
|
||||
// 2. Tool реально вызывается через toolsetDispatch.dispatch()
|
||||
// 3. interrupt() приходит в окне между финальным text и завершением turn'а
|
||||
//
|
||||
// В audit log: user + ToolCall + ToolResult (инструмент выполнился).
|
||||
// В working_memory: user + ToolExchange(result=echo output, wasCancelled=false).
|
||||
// В events: ToolCall + ToolResult + Interrupted + End.
|
||||
val agent = newAgent()
|
||||
val conv = agent.createConversation(temp = false)
|
||||
|
||||
// LLM скриптован: tool call.
|
||||
fakeLlm.scriptedReplies = mutableListOf(
|
||||
FakeLiteLlm.Reply.ToolCalls(listOf("echo_tool" to mapOf("q" to "hi"))),
|
||||
)
|
||||
|
||||
val echoTool = object : LiteTool {
|
||||
override fun describe(): String = """{"name":"echo_tool","description":"echoes args"}"""
|
||||
override fun invoke(arguments: String): String = """{"echo":$arguments}"""
|
||||
}
|
||||
agent.registerToolForTest("echo_tool", echoTool)
|
||||
|
||||
// Подписываемся ДО send — SharedFlow без replay
|
||||
val events = mutableListOf<ProtoEvent>()
|
||||
val eventsJob = launch(start = kotlinx.coroutines.CoroutineStart.UNDISPATCHED) {
|
||||
conv.events(Instant.DISTANT_PAST).collect { events.add(it) }
|
||||
}
|
||||
|
||||
val sendJob = launch {
|
||||
try {
|
||||
conv.send(listOf(Content.Text("run echo tool")))
|
||||
} catch (_: kotlinx.coroutines.CancellationException) {}
|
||||
}
|
||||
// Ждём пока инструмент выполнится (turn завершится нормально)
|
||||
sendJob.join()
|
||||
// interrupt() ПОСЛЕ завершения turn — не должно ничего менять в БД,
|
||||
// но проверяем что events включает все ожидаемые типы.
|
||||
conv.interrupt()
|
||||
eventsJob.cancel()
|
||||
|
||||
// audit: user + toolcall + toolresult (tool выполнился), assistant может быть
|
||||
val msgs = storage.messageStore.listAll(conv.id)
|
||||
val toolResult = msgs.filterIsInstance<pw.binom.agentik.storage.MessageRecord.ToolResult>().firstOrNull()
|
||||
assertNotNull(toolResult, "tool result должен быть в audit — tool выполнился нормально")
|
||||
val toolResultResult = toolResult!!.result!!
|
||||
assertTrue(toolResultResult.contains("echo"), "tool result содержит реальный ответ тулы: $toolResultResult")
|
||||
|
||||
// working memory: user + tool_exchange
|
||||
val wm = storage.workingMemoryStore.list(conv.id)
|
||||
val exchanges = wm.mapNotNull { (it.entry as? WorkingMemoryEntry.ToolExchange) }
|
||||
assertEquals(1, exchanges.size)
|
||||
assertEquals("echo_tool", exchanges[0].toolName)
|
||||
assertFalse(exchanges[0].wasCancelled, "tool реально выполнился, не был отменён")
|
||||
assertTrue(exchanges[0].resultText.contains("echo"))
|
||||
|
||||
// events должны включать ToolCall + ToolResult. End — обязательно (turn завершился).
|
||||
assertTrue(events.any { it is ProtoEvent.ToolCall }, "events=$events")
|
||||
assertTrue(events.any { it is ProtoEvent.ToolResult }, "events=$events")
|
||||
assertTrue(events.any { it is ProtoEvent.End }, "events=$events")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `temp conversation is not persisted across agent instances`() = runTest {
|
||||
// Поднимаем file-backed БД, создаём temp-беседу
|
||||
storage.close()
|
||||
val dbPath = (System.getProperty("java.io.tmpdir") + "/agentik-test-${System.nanoTime()}.db")
|
||||
storage = SqliteStores.open(dbPath).asBundle()
|
||||
val agent1 = ChatAgent(
|
||||
id = "agentik",
|
||||
storage = storage,
|
||||
llm = FakeLiteLlm().also { fakeLlm = it },
|
||||
llmConfig = LlmConfig(
|
||||
backend = pw.binom.agentik.standalone.llm.LlmBackend.OPENAI,
|
||||
systemPrompt = "be brief",
|
||||
openai = OpenAiConfig(baseUrl = "http://test", apiKey = "test", model = "test"),
|
||||
),
|
||||
)
|
||||
val tempConv = agent1.createConversation(temp = true)
|
||||
val tempId = tempConv.id
|
||||
assertNotNull(agent1.getConversation(tempId))
|
||||
|
||||
// Переоткрываем БД — temp-беседа не должна пережить рестарт
|
||||
storage.close()
|
||||
storage = SqliteStores.open(dbPath).asBundle()
|
||||
val agent2 = ChatAgent(
|
||||
id = "agentik",
|
||||
storage = storage,
|
||||
llm = fakeLlm,
|
||||
llmConfig = LlmConfig(
|
||||
backend = pw.binom.agentik.standalone.llm.LlmBackend.OPENAI,
|
||||
systemPrompt = "be brief",
|
||||
openai = OpenAiConfig(baseUrl = "http://test", apiKey = "test", model = "test"),
|
||||
),
|
||||
)
|
||||
assertNull(agent2.getConversation(tempId))
|
||||
java.io.File(dbPath).delete()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `non-temp conversation persists across agent instances`() = runTest {
|
||||
storage.close()
|
||||
val dbPath = (System.getProperty("java.io.tmpdir") + "/agentik-test-${System.nanoTime()}.db")
|
||||
storage = SqliteStores.open(dbPath).asBundle()
|
||||
val agent1 = ChatAgent(
|
||||
id = "agentik",
|
||||
storage = storage,
|
||||
llm = FakeLiteLlm().also { fakeLlm = it },
|
||||
llmConfig = LlmConfig(
|
||||
backend = pw.binom.agentik.standalone.llm.LlmBackend.OPENAI,
|
||||
systemPrompt = "be brief",
|
||||
openai = OpenAiConfig(baseUrl = "http://test", apiKey = "test", model = "test"),
|
||||
),
|
||||
)
|
||||
val conv = agent1.createConversation(temp = false)
|
||||
val id = conv.id
|
||||
|
||||
storage.close()
|
||||
storage = SqliteStores.open(dbPath).asBundle()
|
||||
val agent2 = ChatAgent(
|
||||
id = "agentik",
|
||||
storage = storage,
|
||||
llm = fakeLlm,
|
||||
llmConfig = LlmConfig(
|
||||
backend = pw.binom.agentik.standalone.llm.LlmBackend.OPENAI,
|
||||
systemPrompt = "be brief",
|
||||
openai = OpenAiConfig(baseUrl = "http://test", apiKey = "test", model = "test"),
|
||||
),
|
||||
)
|
||||
assertNotNull(agent2.getConversation(id))
|
||||
java.io.File(dbPath).delete()
|
||||
}
|
||||
|
||||
private fun fakeLiteLlmForReload(): LiteLlm = object : LiteLlm {
|
||||
override val backendName: String = "fake"
|
||||
override val capabilities: pw.binom.litert.LiteCapabilities = pw.binom.litert.LiteCapabilities(pw.binom.litert.LiteInputModalities.TextOnly, false, false, null)
|
||||
override fun isInitialized(): Boolean = true
|
||||
override fun createConversation(config: LiteConversationConfig): LiteConversation =
|
||||
error("not used in reload test")
|
||||
override fun infer(request: pw.binom.litert.LiteRequest): String = error("not used")
|
||||
override fun inferStream(request: pw.binom.litert.LiteRequest): Flow<LiteDelta> = error("not used")
|
||||
override fun close() {}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `tool-call loop executes registered tool and feeds result back`() = runTest {
|
||||
val echoTool = object : LiteTool {
|
||||
override fun describe(): String = """{"type":"function","function":{"name":"echo"}}"""
|
||||
override fun invoke(arguments: String): String = "echoed: $arguments"
|
||||
}
|
||||
val toolLlm = ToolLoopFakeLiteLlm()
|
||||
val agent = newAgent(llm = toolLlm, tools = listOf(NamedTool("echo", echoTool)))
|
||||
val conv = agent.createConversation(temp = false)
|
||||
|
||||
conv.send(listOf(pw.binom.agentik.proto.Content.Text("call the tool")))
|
||||
|
||||
// sendStreamContents вызывается дважды: первый раз с user-сообщением
|
||||
// (LLM отвечает tool_call), второй раз — после addToolResult — для
|
||||
// триггера continuation у stateless-бэкендов (OpenAI). На этой fake
|
||||
// LiteLlm оба попадают в счётчик.
|
||||
assertEquals(2, toolLlm.toolCallCount, "expected user send + post-tool continuation")
|
||||
assertEquals("echoed: {\"x\":\"hi\"}", toolLlm.lastToolResult,
|
||||
"expected echo tool invoked with the LLM's args, result fed back via addToolResult")
|
||||
assertEquals("final reply", toolLlm.finalReplyEmitted,
|
||||
"expected continuation send after tool result to emit final text")
|
||||
|
||||
agent.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `agentEvents - Created + Deleted flow`() = runTest {
|
||||
val agent = newAgent()
|
||||
val events = mutableListOf<AgentEvent>()
|
||||
val job = launch(start = kotlinx.coroutines.CoroutineStart.UNDISPATCHED) {
|
||||
agent.events(Instant.DISTANT_PAST).collect { events.add(it) }
|
||||
}
|
||||
val conv = agent.createConversation(temp = false)
|
||||
agent.deleteConversation(conv.id)
|
||||
delay(50)
|
||||
job.cancel()
|
||||
|
||||
assertEquals(2, events.size)
|
||||
val created = events[0] as AgentEvent.Created
|
||||
val deleted = events[1] as AgentEvent.Deleted
|
||||
assertEquals(conv.id, created.conversationId)
|
||||
assertEquals(conv.id, deleted.id)
|
||||
}
|
||||
}
|
||||
|
||||
/** Поддельный LiteLlm: возвращает fakeLlm.reply в sendStreamContents, опционально запоминает history. */
|
||||
private class ToolLoopFakeLiteLlm : LiteLlm {
|
||||
override val backendName: String = "fake-tool"
|
||||
override val capabilities: pw.binom.litert.LiteCapabilities? = null
|
||||
|
||||
var toolCallCount: Int = 0
|
||||
var lastToolResult: String? = null
|
||||
var finalReplyEmitted: String? = null
|
||||
|
||||
override fun isInitialized(): Boolean = true
|
||||
|
||||
override fun createConversation(config: LiteConversationConfig): LiteConversation {
|
||||
return object : LiteConversation {
|
||||
private val hist = mutableListOf<LiteMessage>()
|
||||
override val history: List<LiteMessage> get() = hist.toList()
|
||||
override fun sendStream(prompt: String) = sendStreamContents(listOf(LiteContentPart.Text(prompt)))
|
||||
override fun sendStreamContents(contents: List<LiteContentPart>): Flow<LiteDelta> {
|
||||
hist.add(LiteMessage(LiteRole.USER, contents))
|
||||
toolCallCount++
|
||||
return flowOf(
|
||||
LiteDelta(
|
||||
text = "",
|
||||
isDone = true,
|
||||
toolCalls = listOf(LiteToolCall(name = "echo", arguments = mapOf("x" to "hi"))),
|
||||
),
|
||||
)
|
||||
}
|
||||
override fun send(prompt: String): String = "unused"
|
||||
override fun sendContents(contents: List<LiteContentPart>): String = "unused"
|
||||
override fun cancel() {}
|
||||
override fun tokenCount(): Int = hist.size
|
||||
override fun addToolResult(callId: String?, name: String, result: String): LiteDelta {
|
||||
lastToolResult = result
|
||||
val reply = "final reply"
|
||||
finalReplyEmitted = reply
|
||||
return LiteDelta(text = reply, isDone = true)
|
||||
}
|
||||
override fun close() {}
|
||||
}
|
||||
}
|
||||
|
||||
override fun infer(request: pw.binom.litert.LiteRequest): String = error("not used")
|
||||
override fun inferStream(request: pw.binom.litert.LiteRequest): Flow<LiteDelta> = error("not used")
|
||||
override fun close() {}
|
||||
}
|
||||
-197
@@ -1,197 +0,0 @@
|
||||
package pw.binom.agentik.standalone.agent
|
||||
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import pw.binom.agentik.standalone.llm.LlmConfig
|
||||
import pw.binom.agentik.storage.sqlite.SqliteStores
|
||||
import pw.binom.agentik.toolsets.ToolsetContribution
|
||||
import pw.binom.litert.LiteLlm
|
||||
import pw.binom.litert.LiteTool
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
/**
|
||||
* Интеграционные тесты ChatAgent + toolsets: проверяем что при пустом
|
||||
* toolsets=List (дефолт) агент ведёт себя как раньше (нет enable/disable тулов,
|
||||
* нет секции в system prompt), а при non-empty — добавляет их и регистрирует
|
||||
* диспетчер.
|
||||
*/
|
||||
class ChatAgentToolsetsTest {
|
||||
|
||||
private fun stubTool(name: String, response: String = "ok:$name"): LiteTool = object : LiteTool {
|
||||
override fun describe() = """{"name":"$name","description":"stub","parameters":{"type":"object","properties":{}}}"""
|
||||
override fun invoke(arguments: String) = response
|
||||
}
|
||||
|
||||
private fun stubLlm(): LiteLlm = object : LiteLlm {
|
||||
override val backendName: String = "stub"
|
||||
override fun isInitialized(): Boolean = true
|
||||
override fun createConversation(config: pw.binom.litert.LiteConversationConfig): pw.binom.litert.LiteConversation =
|
||||
throw UnsupportedOperationException("not used in this test")
|
||||
override fun infer(request: pw.binom.litert.LiteRequest): String =
|
||||
throw UnsupportedOperationException("not used in this test")
|
||||
override fun inferStream(request: pw.binom.litert.LiteRequest): kotlinx.coroutines.flow.Flow<pw.binom.litert.LiteDelta> =
|
||||
throw UnsupportedOperationException("not used in this test")
|
||||
override val capabilities: pw.binom.litert.LiteCapabilities? = null
|
||||
override fun close() {}
|
||||
}
|
||||
|
||||
private fun newAgent(
|
||||
toolsets: List<ToolsetContribution> = emptyList(),
|
||||
): Pair<ChatAgent, pw.binom.agentik.storage.StorageBundle> {
|
||||
val storage = SqliteStores.inMemory().asBundle()
|
||||
val agent = ChatAgent(
|
||||
id = "test-agent",
|
||||
storage = storage,
|
||||
llm = stubLlm(),
|
||||
llmConfig = LlmConfig(
|
||||
backend = pw.binom.agentik.standalone.llm.LlmBackend.GOOGLE,
|
||||
systemPrompt = "base",
|
||||
google = pw.binom.agentik.standalone.llm.GoogleConfig(modelPath = "/tmp/fake.gguf"),
|
||||
),
|
||||
toolsets = toolsets,
|
||||
)
|
||||
return agent to storage
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `default (empty toolsets) does not register enable or disable tools`() {
|
||||
val (agent, storage) = newAgent()
|
||||
try {
|
||||
// Проверяем через allTools-эквивалент: вызываем enable_toolset
|
||||
// и ожидаем что он не найден — это значит тул не зарегистрирован.
|
||||
val conv = agent.createConversation(temp = true)
|
||||
// system prompt не должен содержать секции Toolsets
|
||||
val prompt = (conv as ChatConversation).let { it.systemPromptForTest() }
|
||||
assertFalse(prompt.contains("## Toolsets"), "toolsets section must NOT appear when toolsets empty")
|
||||
assertFalse(prompt.contains("enable_toolset"), "enable_toolset must NOT be mentioned when toolsets empty")
|
||||
} finally { storage.close() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `non-empty toolsets registers enable and disable tools and includes section`() {
|
||||
val toolsets = listOf(
|
||||
ToolsetContribution(
|
||||
name = "media",
|
||||
description = "image and video processing",
|
||||
tools = listOf(ToolsetContribution.ToolEntry("resize_image", stubTool("resize_image"))),
|
||||
),
|
||||
ToolsetContribution(
|
||||
name = "web",
|
||||
description = "fetch and parse web pages",
|
||||
tools = listOf(ToolsetContribution.ToolEntry("fetch_url", stubTool("fetch_url"))),
|
||||
),
|
||||
)
|
||||
val (agent, storage) = newAgent(toolsets = toolsets)
|
||||
try {
|
||||
val conv = agent.createConversation(temp = true) as ChatConversation
|
||||
val prompt = conv.systemPromptForTest()
|
||||
assertTrue(prompt.contains("## Toolsets"), "toolsets section MUST appear when toolsets non-empty")
|
||||
assertTrue(prompt.contains("- media — image and video processing"))
|
||||
assertTrue(prompt.contains("- web — fetch and parse web pages"))
|
||||
// Оба тула — в списке allTools (через system prompt проверяем только prompt;
|
||||
// наличие тулов проверим отдельно — см. ниже).
|
||||
} finally { storage.close() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `non-empty toolsets - enable_toolset activates and dispatches auto-included tools`() {
|
||||
val mediaTool = stubTool("resize_image", "image-resized-100x100")
|
||||
val toolsets = listOf(
|
||||
ToolsetContribution(
|
||||
name = "media",
|
||||
description = "image and video processing",
|
||||
tools = listOf(ToolsetContribution.ToolEntry("resize_image", mediaTool)),
|
||||
),
|
||||
)
|
||||
val (agent, storage) = newAgent(toolsets = toolsets)
|
||||
try {
|
||||
val conv = agent.createConversation(temp = true) as ChatConversation
|
||||
|
||||
// Вызываем enable_toolset через диспетчер (минуя LLM, напрямую)
|
||||
val enabled = runBlocking {
|
||||
conv.dispatchToolForTest("enable_toolset", """{"name":"media"}""")
|
||||
}
|
||||
assertEquals("Toolset 'media' activated.", enabled)
|
||||
|
||||
// Теперь resize_image должен работать (тулсет активен)
|
||||
val resized = runBlocking {
|
||||
conv.dispatchToolForTest("resize_image", "{}")
|
||||
}
|
||||
assertEquals("image-resized-100x100", resized)
|
||||
} finally { storage.close() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `non-empty toolsets - calling tool from inactive toolset triggers auto-activation`() {
|
||||
val mediaTool = stubTool("resize_image", "auto-activated-and-ran")
|
||||
val toolsets = listOf(
|
||||
ToolsetContribution(
|
||||
name = "media",
|
||||
description = "x",
|
||||
tools = listOf(ToolsetContribution.ToolEntry("resize_image", mediaTool)),
|
||||
),
|
||||
)
|
||||
val (agent, storage) = newAgent(toolsets = toolsets)
|
||||
try {
|
||||
val conv = agent.createConversation(temp = true) as ChatConversation
|
||||
|
||||
// Без enable сразу вызываем resize_image — диспетчер должен auto-activate.
|
||||
val result = runBlocking {
|
||||
conv.dispatchToolForTest("resize_image", "{}")
|
||||
}
|
||||
assertEquals("auto-activated-and-ran", result)
|
||||
} finally { storage.close() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `non-empty toolsets - disable_toolset removes from active list`() {
|
||||
// Тест проверяет только сайд-эффект на реестре (active set),
|
||||
// а не реальный dispatch — потому что диспетчер auto-activate'ит тулсет
|
||||
// обратно при следующем вызове (это by design: если модель забыла что
|
||||
// тулсет выключен, мы прощаем и включаем заново).
|
||||
val toolsets = listOf(
|
||||
ToolsetContribution(
|
||||
name = "media",
|
||||
description = "x",
|
||||
tools = listOf(ToolsetContribution.ToolEntry("resize_image", stubTool("resize_image"))),
|
||||
),
|
||||
)
|
||||
val (agent, storage) = newAgent(toolsets = toolsets)
|
||||
try {
|
||||
val conv = agent.createConversation(temp = true) as ChatConversation
|
||||
|
||||
runBlocking { conv.dispatchToolForTest("enable_toolset", """{"name":"media"}""") }
|
||||
// disable
|
||||
val disabled = runBlocking {
|
||||
conv.dispatchToolForTest("disable_toolset", """{"name":"media"}""")
|
||||
}
|
||||
assertEquals("Toolset 'media' deactivated.", disabled)
|
||||
// После disable реестр уже не содержит media в active
|
||||
// (проверяем косвенно: disable повторно всё ещё возвращает тот же uniform message)
|
||||
val disabledAgain = runBlocking {
|
||||
conv.dispatchToolForTest("disable_toolset", """{"name":"media"}""")
|
||||
}
|
||||
assertEquals("Toolset 'media' deactivated.", disabledAgain)
|
||||
} finally { storage.close() }
|
||||
}
|
||||
}
|
||||
|
||||
// Вспомогательные extension'ы — открываем systemPrompt/dispatch наружу для тестов.
|
||||
internal fun ChatConversation.systemPromptForTest(): String {
|
||||
// Через рефлексию достаём private val systemPrompt.
|
||||
val f = this::class.java.getDeclaredField("systemPrompt").apply { isAccessible = true }
|
||||
return f.get(this) as String
|
||||
}
|
||||
|
||||
internal suspend fun ChatConversation.dispatchToolForTest(toolName: String, argsJson: String): String {
|
||||
// Через toolsetDispatch (если есть) или прямой toolsByName.
|
||||
val dispatchField = this::class.java.declaredFields.first { it.name == "toolsetDispatch" }.apply { isAccessible = true }
|
||||
val dispatch = dispatchField.get(this) ?: error("toolsetDispatch must be set when toolsets present")
|
||||
val outcome = (dispatch as pw.binom.agentik.toolsets.ToolsetDispatchPolicy).dispatch(toolName, argsJson)
|
||||
return when (outcome) {
|
||||
is pw.binom.agentik.toolsets.ToolsetDispatchPolicy.Outcome.Ran -> outcome.result
|
||||
is pw.binom.agentik.toolsets.ToolsetDispatchPolicy.Outcome.Unknown -> "[tool not found: $toolName]"
|
||||
}
|
||||
}
|
||||
@@ -1,183 +0,0 @@
|
||||
package pw.binom.agentik.standalone.agent
|
||||
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import pw.binom.agentik.memory.MemoryCategory
|
||||
import pw.binom.agentik.memory.MemoryNote
|
||||
import pw.binom.agentik.memory.MemorySource
|
||||
import pw.binom.agentik.memory.MemoryStore
|
||||
import pw.binom.agentik.memory.NewMemoryNote
|
||||
import pw.binom.agentik.memory.ReviewedTurn
|
||||
import pw.binom.agentik.memory.md.KeywordMdReviewer
|
||||
import pw.binom.agentik.standalone.llm.LlmBackend
|
||||
import pw.binom.agentik.standalone.llm.LlmConfig
|
||||
import pw.binom.agentik.standalone.llm.OpenAiConfig
|
||||
import pw.binom.agentik.storage.sqlite.SqliteStores
|
||||
import pw.binom.agentik.proto.Content as ProtoContent
|
||||
import pw.binom.litert.LiteConversation
|
||||
import pw.binom.litert.LiteConversationConfig
|
||||
import pw.binom.litert.LiteLlm
|
||||
import pw.binom.litert.LiteMessage
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import pw.binom.agentik.memory.MemoryStoreEvent
|
||||
import kotlin.test.AfterTest
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertIs
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.time.Instant
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import pw.binom.agentik.llm.tools.ContextCompactor
|
||||
import pw.binom.agentik.llm.tools.SummaryTurn
|
||||
|
||||
/**
|
||||
* Тесты для [ChatConversation.compactPreTurnIfNeeded]: триггер compaction'а
|
||||
* при превышении порога, вызов суммаризатора, триггер memory review, и
|
||||
* атомарный replace в working memory.
|
||||
*/
|
||||
class CompactionTest {
|
||||
|
||||
private lateinit var storage: pw.binom.agentik.storage.StorageBundle
|
||||
private lateinit var fakeLlm: FakeLiteLlm
|
||||
|
||||
@BeforeTest
|
||||
fun setup() {
|
||||
storage = SqliteStores.inMemory().asBundle()
|
||||
fakeLlm = FakeLiteLlm()
|
||||
}
|
||||
|
||||
@AfterTest
|
||||
fun tearDown() {
|
||||
storage.close()
|
||||
}
|
||||
|
||||
private fun newAgent(
|
||||
contextWindow: Int? = null,
|
||||
compressionThreshold: Double = 0.8,
|
||||
compactor: ContextCompactor? = null,
|
||||
memoryStore: MemoryStore? = null,
|
||||
): ChatAgent {
|
||||
val reviewer = if (memoryStore != null) KeywordMdReviewer() else null
|
||||
return ChatAgent(
|
||||
id = "test",
|
||||
storage = storage,
|
||||
llm = fakeLlm,
|
||||
llmConfig = LlmConfig(
|
||||
backend = LlmBackend.OPENAI,
|
||||
systemPrompt = "be brief",
|
||||
openai = OpenAiConfig(baseUrl = "http://test", apiKey = "test", model = "test"),
|
||||
),
|
||||
memoryStore = memoryStore,
|
||||
memoryReviewer = reviewer,
|
||||
contextWindow = contextWindow,
|
||||
compressionThreshold = compressionThreshold,
|
||||
contextCompactor = compactor,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `compaction is no-op when contextWindow is null`() = runTest {
|
||||
// contextWindow=null → даже с огромной историей compaction не запустится.
|
||||
fakeLlm.reply = "hi"
|
||||
val agent = newAgent(contextWindow = null, compactor = RecordingCompactor("summary"))
|
||||
val conv = agent.createConversation(temp = false) as ChatConversation
|
||||
repeat(10) {
|
||||
conv.send(listOf(ProtoContent.Text("turn $it: ${"x".repeat(200)}")))
|
||||
}
|
||||
val wm = storage.workingMemoryStore.list(conv.id)
|
||||
// Без compaction все ходы остаются в памяти (System + 10 user/assistant = 21 строк).
|
||||
val summaries = wm.filter { it.entry is pw.binom.agentik.storage.WorkingMemoryEntry.Summary }
|
||||
assertEquals(0, summaries.size, "compaction must not run without contextWindow")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `compaction is no-op when compactor is null but window is set`() = runTest {
|
||||
fakeLlm.reply = "hi"
|
||||
val agent = newAgent(contextWindow = 10, compactor = null)
|
||||
val conv = agent.createConversation(temp = false) as ChatConversation
|
||||
conv.send(listOf(ProtoContent.Text("first")))
|
||||
val wm = storage.workingMemoryStore.list(conv.id)
|
||||
// System + User + Assistant = 3. Без compactor — никаких Summary.
|
||||
val summaries = wm.filter { it.entry is pw.binom.agentik.storage.WorkingMemoryEntry.Summary }
|
||||
assertEquals(0, summaries.size, "no compaction runs without compactor")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `compaction triggers when estimated tokens exceed threshold`() = runTest {
|
||||
fakeLlm.reply = "ok"
|
||||
val compactor = RecordingCompactor("**Goal**: x\n**Active**: y\n**Resolved**: z")
|
||||
// contextWindow = 20 chars → ~5 токенов. С порогом 0.5 (50%) — почти любой ход пробивает.
|
||||
val agent = newAgent(contextWindow = 20, compressionThreshold = 0.5, compactor = compactor)
|
||||
val conv = agent.createConversation(temp = false) as ChatConversation
|
||||
|
||||
conv.send(listOf(ProtoContent.Text("user message one — long enough to cross threshold")))
|
||||
|
||||
// Compactor должен был быть вызван хотя бы раз.
|
||||
assertTrue(compactor.calls > 0, "compactor must be called at least once when above threshold")
|
||||
// В working memory должна появиться Summary.
|
||||
val wm = storage.workingMemoryStore.list(conv.id)
|
||||
val summaries = wm.filter { it.entry is pw.binom.agentik.storage.WorkingMemoryEntry.Summary }
|
||||
assertTrue(summaries.isNotEmpty(), "at least one Summary entry should be present after compaction")
|
||||
// Summary-текст — то, что вернул наш compactor.
|
||||
val summaryText = (summaries.first().entry as pw.binom.agentik.storage.WorkingMemoryEntry.Summary).text
|
||||
assertTrue(summaryText.startsWith("**Goal**"), "summary text should come from compactor: $summaryText")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `compaction calls memoryReviewer reviewPreCompaction`() = runTest {
|
||||
fakeLlm.reply = "ok"
|
||||
val memStore = TestInMemoryMemoryStore()
|
||||
val compactor = RecordingCompactor("compacted summary")
|
||||
val agent = newAgent(
|
||||
contextWindow = 30,
|
||||
compressionThreshold = 0.5,
|
||||
compactor = compactor,
|
||||
memoryStore = memStore,
|
||||
)
|
||||
val conv = agent.createConversation(temp = false) as ChatConversation
|
||||
|
||||
conv.send(listOf(ProtoContent.Text("Я обычно предпочитаю kotlin для бэкенда.")))
|
||||
|
||||
// Память должна получить хотя бы одну заметку от reviewPreCompaction.
|
||||
val notes = memStore.list()
|
||||
assertTrue(notes.any { it.category == MemoryCategory.PREFERENCE && it.content.contains("kotlin") },
|
||||
"memory should capture a preference fact before compaction drops the turn")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `compaction preserves recent turns (KEEP_RECENT_TURNS)`() = runTest {
|
||||
fakeLlm.reply = "ok"
|
||||
val compactor = RecordingCompactor("compacted summary")
|
||||
val agent = newAgent(contextWindow = 30, compressionThreshold = 0.3, compactor = compactor)
|
||||
val conv = agent.createConversation(temp = false) as ChatConversation
|
||||
|
||||
conv.send(listOf(ProtoContent.Text("first turn")))
|
||||
conv.send(listOf(ProtoContent.Text("second turn")))
|
||||
conv.send(listOf(ProtoContent.Text("third turn — long content ${"y".repeat(150)}")))
|
||||
|
||||
val wm = storage.workingMemoryStore.list(conv.id)
|
||||
// Должны быть: System + хотя бы один Summary + последние KEEP_RECENT_TURNS ходов.
|
||||
// KEEP_RECENT_TURNS = 4 → user/assistant последних двух ходов (third + second) могут быть не тронуты.
|
||||
val userAssistantCount = wm.count {
|
||||
it.entry is pw.binom.agentik.storage.WorkingMemoryEntry.User ||
|
||||
it.entry is pw.binom.agentik.storage.WorkingMemoryEntry.Assistant
|
||||
}
|
||||
// Минимум 1 ход остаётся (KEEP_RECENT_TURNS).
|
||||
assertTrue(userAssistantCount >= 1, "at least one recent turn must be preserved")
|
||||
}
|
||||
}
|
||||
|
||||
private class RecordingCompactor(private val result: String) : ContextCompactor {
|
||||
var calls = 0
|
||||
override suspend fun summarize(turns: List<SummaryTurn>): String {
|
||||
calls++
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* (InMemoryMemoryStore вынесен в [TestInMemoryMemoryStore].)
|
||||
*/
|
||||
@@ -1,116 +0,0 @@
|
||||
package pw.binom.agentik.standalone.agent
|
||||
|
||||
import pw.binom.agentik.storage.MessageContext
|
||||
import pw.binom.agentik.storage.MessageOrigin.EVENT
|
||||
import pw.binom.agentik.storage.MessageOrigin.SYSTEM
|
||||
import pw.binom.agentik.storage.MessageOrigin.USER
|
||||
import pw.binom.litert.LiteContentPart
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertIs
|
||||
import kotlin.test.assertNull
|
||||
|
||||
/**
|
||||
* Тесты для префикса контекста инициации хода в user-сообщениях.
|
||||
* Только для не-USER origin'ов. USER — без изменений.
|
||||
*/
|
||||
class ContextPrefixTest {
|
||||
|
||||
@Test
|
||||
fun `USER origin produces no prefix`() {
|
||||
val ctx = MessageContext(origin = USER)
|
||||
val parts = listOf(LiteContentPart.Text("hello"))
|
||||
val out = applyContextPrefix(parts, ctx)
|
||||
assertEquals(parts, out, "USER should not modify content")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `null context produces no prefix`() {
|
||||
val parts = listOf(LiteContentPart.Text("hello"))
|
||||
val out = applyContextPrefix(parts, null)
|
||||
assertEquals(parts, out)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `SYSTEM origin prepends label to first text part`() {
|
||||
val ctx = MessageContext(origin = SYSTEM, description = "agent startup greeting")
|
||||
val parts = listOf(LiteContentPart.Text("boot"))
|
||||
val out = applyContextPrefix(parts, ctx)
|
||||
assertEquals(1, out.size)
|
||||
val text = assertIs<LiteContentPart.Text>(out[0])
|
||||
assertEquals("[SYSTEM] agent startup greeting\nboot", text.text)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `EVENT origin with sourceId includes it`() {
|
||||
val ctx = MessageContext(
|
||||
origin = EVENT,
|
||||
description = "scheduled cron morning-briefing",
|
||||
sourceId = "cron-42",
|
||||
)
|
||||
val parts = listOf(LiteContentPart.Text("wake up"))
|
||||
val out = applyContextPrefix(parts, ctx)
|
||||
val text = assertIs<LiteContentPart.Text>(out[0])
|
||||
assertEquals("[EVENT] scheduled cron morning-briefing (sourceId=cron-42)\nwake up", text.text)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `prefix only added to first text part, others untouched`() {
|
||||
val ctx = MessageContext(origin = EVENT, description = "test")
|
||||
val parts = listOf(
|
||||
LiteContentPart.Text("first"),
|
||||
LiteContentPart.Text("second"),
|
||||
)
|
||||
val out = applyContextPrefix(parts, ctx)
|
||||
assertEquals(2, out.size)
|
||||
val first = assertIs<LiteContentPart.Text>(out[0])
|
||||
val second = assertIs<LiteContentPart.Text>(out[1])
|
||||
assertEquals("[EVENT] test\nfirst", first.text)
|
||||
assertEquals("second", second.text)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `prefix with no text parts is prepended as standalone text`() {
|
||||
val ctx = MessageContext(origin = SYSTEM, description = "ping")
|
||||
// Симулируем: модель получает картинку + контекст — контекст идёт первой Text-частью.
|
||||
val parts = listOf<LiteContentPart>(LiteContentPart.Text("just prefix"))
|
||||
val out = applyContextPrefix(parts, ctx)
|
||||
assertEquals(1, out.size)
|
||||
val text = assertIs<LiteContentPart.Text>(out[0])
|
||||
assertEquals("[SYSTEM] ping\njust prefix", text.text)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `formatContextPrefix formats name + description + sourceId`() {
|
||||
val ctx = MessageContext(origin = EVENT, description = "wake", sourceId = "cron-1")
|
||||
assertEquals("[EVENT] wake (sourceId=cron-1)", formatContextPrefix(ctx))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `formatContextPrefix omits blank description and sourceId`() {
|
||||
val ctx = MessageContext(origin = SYSTEM)
|
||||
assertEquals("[SYSTEM]", formatContextPrefix(ctx))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `formatContextPrefix omits blank sourceId even if description is set`() {
|
||||
val ctx = MessageContext(origin = SYSTEM, description = "boot", sourceId = "")
|
||||
assertEquals("[SYSTEM] boot", formatContextPrefix(ctx))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `USER origin with context fields still produces no prefix`() {
|
||||
// Контекст с USER-происхождением, но с заполненным description/sourceId:
|
||||
// не должен триггерить префикс (UI-метаданные для логирования).
|
||||
val ctx = MessageContext(origin = USER, sourceId = "irc:agentik", description = "PRIVMSG")
|
||||
val parts = listOf(LiteContentPart.Text("hi"))
|
||||
val out = applyContextPrefix(parts, ctx)
|
||||
assertEquals(parts, out)
|
||||
}
|
||||
|
||||
// Вспомогательное для теста
|
||||
@Test
|
||||
fun `null-context assert helper`() {
|
||||
assertNull(null as String?)
|
||||
}
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
package pw.binom.agentik.standalone.agent
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import pw.binom.litert.LiteContentPart
|
||||
import pw.binom.litert.LiteConversation
|
||||
import pw.binom.litert.LiteConversationConfig
|
||||
import pw.binom.litert.LiteDelta
|
||||
import pw.binom.litert.LiteLlm
|
||||
import pw.binom.litert.LiteMessage
|
||||
import pw.binom.litert.LiteRole
|
||||
import pw.binom.litert.LiteToolCall
|
||||
|
||||
/**
|
||||
* Тестовая [LiteLlm], запоминающая последний конфиг/контент и отвечающая
|
||||
* заданной строкой [reply] двумя фрагментами + done.
|
||||
*/
|
||||
internal class FakeLiteLlm : LiteLlm {
|
||||
sealed class Reply {
|
||||
data class Text(val text: String) : Reply()
|
||||
data class ToolCalls(val calls: List<Pair<String, Map<String, Any?>>>) : Reply()
|
||||
}
|
||||
|
||||
override val backendName: String = "fake"
|
||||
override val capabilities: pw.binom.litert.LiteCapabilities = pw.binom.litert.LiteCapabilities(pw.binom.litert.LiteInputModalities.TextOnly, false, false, null)
|
||||
var reply: String = ""
|
||||
var rememberHistory: Boolean = false
|
||||
var slow: Boolean = false
|
||||
var failMessage: String? = null
|
||||
|
||||
/**
|
||||
* Если задан, LLM проходит по этому списку ответов по порядку: первый
|
||||
* sendStreamContents → первый Reply, второй → второй и т.д. Если список
|
||||
* кончился — fallback на [reply] (text).
|
||||
*/
|
||||
var scriptedReplies: MutableList<Reply> = mutableListOf()
|
||||
|
||||
var lastConfig: LiteConversationConfig? = null
|
||||
var lastContents: List<LiteContentPart>? = null
|
||||
val conversations = mutableListOf<FakeLiteConversation>()
|
||||
|
||||
override fun isInitialized(): Boolean = true
|
||||
|
||||
override fun createConversation(config: LiteConversationConfig): LiteConversation {
|
||||
lastConfig = config
|
||||
val conv = FakeLiteConversation(this, config)
|
||||
conversations.add(conv)
|
||||
return conv
|
||||
}
|
||||
|
||||
override fun infer(request: pw.binom.litert.LiteRequest): String =
|
||||
throw UnsupportedOperationException("not used in test")
|
||||
|
||||
override fun inferStream(request: pw.binom.litert.LiteRequest): Flow<LiteDelta> =
|
||||
throw UnsupportedOperationException("not used in test")
|
||||
|
||||
override fun close() {}
|
||||
|
||||
fun nextReply(): Reply =
|
||||
if (scriptedReplies.isNotEmpty()) scriptedReplies.removeAt(0) else Reply.Text(reply)
|
||||
}
|
||||
|
||||
internal class FakeLiteConversation(
|
||||
private val parent: FakeLiteLlm,
|
||||
config: LiteConversationConfig,
|
||||
) : LiteConversation {
|
||||
val initialMessages: List<LiteMessage> = config.initialMessages
|
||||
private val mutableHistory: MutableList<LiteMessage> = config.initialMessages.toMutableList()
|
||||
override val history: List<LiteMessage> get() = mutableHistory.toList()
|
||||
|
||||
override fun sendStream(prompt: String): Flow<LiteDelta> =
|
||||
sendStreamContents(listOf(LiteContentPart.Text(prompt)))
|
||||
|
||||
override fun sendStreamContents(contents: List<LiteContentPart>): Flow<LiteDelta> {
|
||||
parent.lastContents = contents
|
||||
parent.failMessage?.let { msg ->
|
||||
return kotlinx.coroutines.flow.flow { throw RuntimeException(msg) }
|
||||
}
|
||||
mutableHistory.add(LiteMessage(LiteRole.USER, contents))
|
||||
val next = parent.nextReply()
|
||||
return when (next) {
|
||||
is FakeLiteLlm.Reply.Text -> {
|
||||
if (parent.slow) {
|
||||
kotlinx.coroutines.flow.flow {
|
||||
emit(LiteDelta(text = next.text.substring(0, next.text.length / 2)))
|
||||
kotlinx.coroutines.delay(10_000)
|
||||
emit(LiteDelta(text = next.text.substring(next.text.length / 2), isDone = true))
|
||||
mutableHistory.add(LiteMessage.model(next.text))
|
||||
}
|
||||
} else {
|
||||
val first = next.text.substring(0, next.text.length / 2)
|
||||
val second = next.text.substring(next.text.length / 2)
|
||||
flowOf(
|
||||
LiteDelta(text = first),
|
||||
LiteDelta(text = second, isDone = true),
|
||||
).also { mutableHistory.add(LiteMessage.model(next.text)) }
|
||||
}
|
||||
}
|
||||
is FakeLiteLlm.Reply.ToolCalls -> {
|
||||
val calls = next.calls.map { (name, args) ->
|
||||
LiteToolCall(name = name, arguments = args)
|
||||
}
|
||||
flowOf(LiteDelta(text = "", toolCalls = calls, isDone = true))
|
||||
}
|
||||
}
|
||||
}
|
||||
override fun send(prompt: String): String {
|
||||
parent.lastContents = listOf(LiteContentPart.Text(prompt))
|
||||
return parent.reply
|
||||
}
|
||||
override fun sendContents(contents: List<LiteContentPart>): String {
|
||||
parent.lastContents = contents
|
||||
return parent.reply
|
||||
}
|
||||
override fun cancel() {}
|
||||
override fun tokenCount(): Int = history.size
|
||||
override fun addToolResult(callId: String?, name: String, result: String): LiteDelta =
|
||||
LiteDelta(text = "", isDone = true)
|
||||
override fun close() {}
|
||||
}
|
||||
@@ -1,339 +0,0 @@
|
||||
package pw.binom.agentik.standalone.agent
|
||||
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import kotlinx.io.files.Path
|
||||
import kotlinx.io.files.SystemFileSystem
|
||||
import kotlinx.io.files.SystemTemporaryDirectory
|
||||
import pw.binom.agentik.memory.ConversationTurn
|
||||
import pw.binom.agentik.memory.MemoryCategory
|
||||
import pw.binom.agentik.memory.MemoryNote
|
||||
import pw.binom.agentik.memory.MemoryPrefetcher
|
||||
import pw.binom.agentik.memory.MemoryReviewDecision
|
||||
import pw.binom.agentik.memory.MemoryReviewer
|
||||
import pw.binom.agentik.memory.MemorySearchQuery
|
||||
import pw.binom.agentik.memory.MemorySearchResult
|
||||
import pw.binom.agentik.memory.MemoryStore
|
||||
import pw.binom.agentik.memory.MemorySource
|
||||
import pw.binom.agentik.memory.MemorySystemGuidance
|
||||
import pw.binom.agentik.memory.NewMemoryNote
|
||||
import pw.binom.agentik.memory.ReviewedTurn
|
||||
import pw.binom.agentik.memory.md.openMdMemorySystem
|
||||
import pw.binom.agentik.proto.Content
|
||||
import pw.binom.agentik.standalone.agent.memory.MemoryToolsFactory
|
||||
import pw.binom.agentik.standalone.llm.LlmBackend
|
||||
import pw.binom.agentik.standalone.llm.LlmConfig
|
||||
import pw.binom.agentik.standalone.llm.OpenAiConfig
|
||||
import pw.binom.agentik.storage.sqlite.SqliteStores
|
||||
import pw.binom.agentik.llm.tools.ContextCompactor
|
||||
import pw.binom.agentik.llm.tools.SummaryTurn
|
||||
import kotlin.test.AfterTest
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* Интеграция памяти в :standalone:
|
||||
* - тулы memory_save/read/list/delete регистрируются у агента
|
||||
* - prefetcher вставляет контекст в первое user-сообщение
|
||||
* - reviewer пишет факты в store после хода
|
||||
*/
|
||||
class MemoryWiringTest {
|
||||
|
||||
private lateinit var storage: pw.binom.agentik.storage.StorageBundle
|
||||
private lateinit var fakeLlm: FakeLiteLlm
|
||||
private lateinit var root: Path
|
||||
|
||||
@BeforeTest
|
||||
fun setup() {
|
||||
storage = SqliteStores.inMemory().asBundle()
|
||||
fakeLlm = FakeLiteLlm()
|
||||
root = Path(SystemTemporaryDirectory.toString(), "agentik-mem-${java.util.UUID.randomUUID()}")
|
||||
SystemFileSystem.createDirectories(root, mustCreate = true)
|
||||
}
|
||||
|
||||
@AfterTest
|
||||
fun tearDown() {
|
||||
storage.close()
|
||||
runCatching { SystemFileSystem.delete(root, mustExist = false) }
|
||||
}
|
||||
|
||||
private fun newAgent(
|
||||
memoryStore: MemoryStore,
|
||||
prefetcher: MemoryPrefetcher,
|
||||
reviewer: MemoryReviewer,
|
||||
contextWindow: Int? = null,
|
||||
compressionThreshold: Double = 0.8,
|
||||
contextCompactor: ContextCompactor = EchoCompactor,
|
||||
): ChatAgent = ChatAgent(
|
||||
id = "agentik",
|
||||
storage = storage,
|
||||
llm = fakeLlm,
|
||||
llmConfig = LlmConfig(
|
||||
backend = LlmBackend.OPENAI,
|
||||
systemPrompt = "be brief",
|
||||
openai = OpenAiConfig(baseUrl = "http://test", apiKey = "test", model = "test"),
|
||||
),
|
||||
memoryStore = memoryStore,
|
||||
memoryPrefetcher = prefetcher,
|
||||
memoryReviewer = reviewer,
|
||||
contextWindow = contextWindow,
|
||||
compressionThreshold = compressionThreshold,
|
||||
contextCompactor = contextCompactor,
|
||||
)
|
||||
|
||||
@Test
|
||||
fun `system prompt includes memory guidance when memory is enabled`() = runBlocking {
|
||||
val system = openMdMemorySystem(root)
|
||||
val agent = newAgent(system.store, system.prefetcher, system.reviewer)
|
||||
val conv = agent.createConversation(temp = false)
|
||||
fakeLlm.reply = "ok"
|
||||
conv.send(listOf(Content.Text("hi")))
|
||||
// System prompt не пишется в working_memory — читаем то, что увидит LLM
|
||||
val text = fakeLlm.lastConfig?.systemInstruction
|
||||
assertNotNull(text)
|
||||
assertTrue(text!!.contains(MemorySystemGuidance.MEMORY_GUIDANCE.take(80)),
|
||||
"system prompt should contain MEMORY_GUIDANCE; got first 200 chars: ${text.take(200)}")
|
||||
agent.close()
|
||||
system.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `soul body is prepended to system prompt and wins over base`() = runBlocking {
|
||||
val soulBody = "I am a helpful test persona. I always answer in one short line."
|
||||
val agent = ChatAgent(
|
||||
id = "agentik",
|
||||
storage = storage,
|
||||
llm = fakeLlm,
|
||||
llmConfig = LlmConfig(
|
||||
backend = LlmBackend.OPENAI,
|
||||
systemPrompt = "be brief",
|
||||
openai = OpenAiConfig(baseUrl = "http://test", apiKey = "test", model = "test"),
|
||||
),
|
||||
soulBody = soulBody,
|
||||
)
|
||||
val conv = agent.createConversation(temp = false)
|
||||
fakeLlm.reply = "ok"
|
||||
conv.send(listOf(Content.Text("hi")))
|
||||
val text = fakeLlm.lastConfig?.systemInstruction
|
||||
assertNotNull(text)
|
||||
assertTrue(text!!.startsWith(soulBody),
|
||||
"soul should be the very first section; got first 60 chars: ${text.take(60)}")
|
||||
assertTrue(text.contains("be brief"),
|
||||
"base prompt should still follow the soul; got: $text")
|
||||
agent.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `soul body not added when null`() = runBlocking {
|
||||
val agent = ChatAgent(
|
||||
id = "agentik",
|
||||
storage = storage,
|
||||
llm = fakeLlm,
|
||||
llmConfig = LlmConfig(
|
||||
backend = LlmBackend.OPENAI,
|
||||
systemPrompt = "be brief",
|
||||
openai = OpenAiConfig(baseUrl = "http://test", apiKey = "test", model = "test"),
|
||||
),
|
||||
)
|
||||
val conv = agent.createConversation(temp = false)
|
||||
fakeLlm.reply = "ok"
|
||||
conv.send(listOf(Content.Text("hi")))
|
||||
val text = fakeLlm.lastConfig?.systemInstruction
|
||||
assertNotNull(text)
|
||||
assertTrue(text!!.startsWith("be brief"),
|
||||
"without soul, prompt should start with base; got first 60 chars: ${text.take(60)}")
|
||||
agent.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `agent exposes memory tools when store is configured`() {
|
||||
val system = openMdMemorySystem(root)
|
||||
val tools = MemoryToolsFactory.create(system.store)
|
||||
assertEquals(4, tools.size)
|
||||
val names = tools.map { it.name }.toSet()
|
||||
assertEquals(setOf("memory_save", "memory_read", "memory_list", "memory_delete"), names)
|
||||
// Каждый tool описывается валидной JSON-схемой:
|
||||
for (t in tools) {
|
||||
assertTrue(t.tool.describe().contains("\"description\""), "describe() for ${t.name}")
|
||||
}
|
||||
system.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `memory_save tool round-trips a note through the store`() = runBlocking {
|
||||
val system = openMdMemorySystem(root)
|
||||
val tools = MemoryToolsFactory.create(system.store).associateBy { it.name }
|
||||
val saveResult = tools.getValue("memory_save").tool.invoke(
|
||||
"""{"category":"preference","content":"prefers tabs over spaces"}""",
|
||||
)
|
||||
assertTrue(saveResult.contains("\"ok\":true"), "save returned: $saveResult")
|
||||
assertTrue(saveResult.contains("\"id\":\"mem-"), "save returned: $saveResult")
|
||||
|
||||
val listResult = tools.getValue("memory_list").tool.invoke("""{"limit":10}""")
|
||||
assertTrue(listResult.contains("prefers tabs over spaces"),
|
||||
"list returned: $listResult")
|
||||
|
||||
val readResult = tools.getValue("memory_read").tool.invoke(
|
||||
"""{"query":"tabs","top_k":3}""",
|
||||
)
|
||||
assertTrue(readResult.contains("prefers tabs over spaces"),
|
||||
"read returned: $readResult")
|
||||
|
||||
val deleteResult = tools.getValue("memory_delete").tool.invoke(
|
||||
Regex("\"id\":\"(mem-[^\"]+)\"").find(saveResult)?.let { m ->
|
||||
"""{"id":"${m.groupValues[1]}"}"""
|
||||
} ?: error("save did not return id"),
|
||||
)
|
||||
assertTrue(deleteResult.contains("\"ok\":true"), "delete returned: $deleteResult")
|
||||
system.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `prefetch inserts memory context into first user message`() = runTest {
|
||||
// Сидим факт в store.
|
||||
val store = openMdMemorySystem(root).also {
|
||||
it.store.upsert(
|
||||
MemoryNote(
|
||||
id = "mem-pre",
|
||||
category = MemoryCategory.USER,
|
||||
content = "User runs k3s on Debian",
|
||||
createdAt = Instant.parse("2026-09-14T10:00:00Z"),
|
||||
lastUsedAt = Instant.parse("2026-09-14T10:00:00Z"),
|
||||
useCount = 0,
|
||||
source = MemorySource.AGENT_SAVE,
|
||||
),
|
||||
)
|
||||
}
|
||||
val prefetcher = StaticPrefetcher { q, k ->
|
||||
store.store.search(MemorySearchQuery(query = q, topK = k, category = null))
|
||||
}
|
||||
fakeLlm.reply = "ok"
|
||||
|
||||
val agent = newAgent(store.store, prefetcher, NoopReviewer())
|
||||
val conv = agent.createConversation(temp = false) as ChatConversation
|
||||
conv.send(listOf(Content.Text("what's my k3s setup?")))
|
||||
|
||||
val sentText = fakeLlm.lastContents?.filterIsInstance<pw.binom.litert.LiteContentPart.Text>()
|
||||
?.joinToString("\n") { it.text }
|
||||
assertNotNull(sentText)
|
||||
assertTrue(sentText.startsWith("[Memory context"),
|
||||
"user message should start with memory prefix, got: $sentText")
|
||||
assertTrue(sentText.contains("User runs k3s on Debian"),
|
||||
"user message should include the prefetched note, got: $sentText")
|
||||
assertTrue(sentText.contains("what's my k3s setup?"),
|
||||
"user message should still contain the original text after the prefix, got: $sentText")
|
||||
agent.close()
|
||||
store.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `prefetch does not add prefix when no hits`() = runTest {
|
||||
val store = openMdMemorySystem(root)
|
||||
val prefetcher = StaticPrefetcher { _, _ -> emptyList() }
|
||||
fakeLlm.reply = "ok"
|
||||
|
||||
val agent = newAgent(store.store, prefetcher, NoopReviewer())
|
||||
val conv = agent.createConversation(temp = false) as ChatConversation
|
||||
conv.send(listOf(Content.Text("hello")))
|
||||
|
||||
val sentText = fakeLlm.lastContents?.filterIsInstance<pw.binom.litert.LiteContentPart.Text>()
|
||||
?.joinToString("\n") { it.text }
|
||||
assertNotNull(sentText)
|
||||
assertTrue(!sentText.startsWith("[Memory context"),
|
||||
"user message should not start with prefix when no hits, got: $sentText")
|
||||
assertTrue(sentText.contains("hello"))
|
||||
agent.close()
|
||||
store.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `reviewer fires on compaction (event-driven), not on every turn`() = runTest {
|
||||
// После перехода на event-driven: review fires ТОЛЬКО в CompactionCoordinator.compactPreTurn()
|
||||
// (через reviewer.reviewPreCompaction()) — не на каждый turn, не по interval'у.
|
||||
// На простом turn без compaction review НЕ запускается.
|
||||
val store = openMdMemorySystem(root)
|
||||
fakeLlm.reply = "Sure, I'll remember that."
|
||||
val reviewerInvoked = CompletableDeferred<Unit>()
|
||||
val reviewer = object : MemoryReviewer {
|
||||
override suspend fun review(turn: ReviewedTurn): MemoryReviewDecision =
|
||||
error("review() не должен вызываться — только reviewPreCompaction() на compaction")
|
||||
override suspend fun reviewPreCompaction(turns: List<ConversationTurn>): MemoryReviewDecision {
|
||||
reviewerInvoked.complete(Unit)
|
||||
return MemoryReviewDecision(
|
||||
toSave = listOf(NewMemoryNote(MemoryCategory.PREFERENCE, "prefers k8s")),
|
||||
toDelete = emptyList(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
val agent = newAgent(
|
||||
store.store,
|
||||
StaticPrefetcher { _, _ -> emptyList() },
|
||||
reviewer,
|
||||
contextWindow = 1024, // forceCompactNow требует contextWindow
|
||||
)
|
||||
val conv = agent.createConversation(temp = false) as ChatConversation
|
||||
|
||||
// Отправляем turn — ничего не должно триггериться, т.к. event-driven review
|
||||
// fires только на compaction/closing.
|
||||
conv.send(listOf(Content.Text("please note: I prefer k8s over docker swarm")))
|
||||
|
||||
// Ждём немного, проверяем что reviewer.review() НЕ был вызван
|
||||
withContext(Dispatchers.Default.limitedParallelism(1)) {
|
||||
delay(200)
|
||||
}
|
||||
assertTrue(store.store.list(category = MemoryCategory.PREFERENCE).isEmpty(),
|
||||
"review НЕ должен был вызваться на простом turn без compaction")
|
||||
|
||||
// Триггерим compaction вручную (через debug-API ConversationLoop.forceCompactNow).
|
||||
// Это вызывает CompactionCoordinator.compactPreTurn → reviewer.reviewPreCompaction.
|
||||
conv.forceCompactNow()
|
||||
|
||||
// Дожидаемся reviewPreCompaction и upsert'а в IO-диспетчере.
|
||||
withContext(Dispatchers.Default.limitedParallelism(1)) {
|
||||
withTimeout(2_000) { reviewerInvoked.await() }
|
||||
withTimeout(2_000) {
|
||||
while (store.store.list(category = MemoryCategory.PREFERENCE).isEmpty()) delay(20)
|
||||
}
|
||||
}
|
||||
|
||||
val notes = store.store.list(category = MemoryCategory.PREFERENCE)
|
||||
assertEquals(1, notes.size, "reviewPreCompaction должен сохранить заметку")
|
||||
assertEquals("prefers k8s", notes[0].content)
|
||||
assertEquals(MemorySource.AUTO_REVIEW, notes[0].source)
|
||||
agent.close()
|
||||
store.close()
|
||||
}
|
||||
}
|
||||
|
||||
// --- helpers ---
|
||||
|
||||
private class StaticPrefetcher(
|
||||
private val fn: suspend (String, Int) -> List<MemorySearchResult>,
|
||||
) : MemoryPrefetcher {
|
||||
override suspend fun prefetch(query: String, topK: Int, category: MemoryCategory?): List<MemoryNote> {
|
||||
if (query.isBlank()) return emptyList()
|
||||
return fn(query, topK).map { it.note }
|
||||
}
|
||||
}
|
||||
|
||||
private class NoopReviewer : MemoryReviewer {
|
||||
override suspend fun review(turn: ReviewedTurn): MemoryReviewDecision =
|
||||
MemoryReviewDecision(toSave = emptyList(), toDelete = emptyList())
|
||||
}
|
||||
|
||||
/** Простой compactor для тестов: возвращает статичную строку. */
|
||||
private object EchoCompactor : ContextCompactor {
|
||||
override suspend fun summarize(turns: List<SummaryTurn>): String =
|
||||
if (turns.isEmpty()) "" else "compacted-${turns.size}-turns"
|
||||
}
|
||||
-71
@@ -1,71 +0,0 @@
|
||||
package pw.binom.agentik.standalone.agent
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
import pw.binom.agentik.llm.tools.ReflectionParser
|
||||
|
||||
class ReflectionParserTest {
|
||||
|
||||
@Test
|
||||
fun `parses clean JSON`() {
|
||||
val raw = """{"score": 4, "summary": "ok", "weakSpots": ["a", "b"]}"""
|
||||
val p = ReflectionParser.parse(raw)
|
||||
assertNotNull(p)
|
||||
assertEquals(4, p.score)
|
||||
assertEquals("ok", p.summary)
|
||||
assertEquals(listOf("a", "b"), p.weakSpots)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parses JSON wrapped in json fences`() {
|
||||
val raw = "```json\n" +
|
||||
"{\"score\": 3, \"summary\": \"norm\", \"weakSpots\": []}\n" +
|
||||
"```"
|
||||
val p = ReflectionParser.parse(raw)
|
||||
assertNotNull(p)
|
||||
assertEquals(3, p.score)
|
||||
assertEquals(listOf<String>(), p.weakSpots)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parses JSON with leading and trailing text`() {
|
||||
val raw = "Вот мой ответ:\n" +
|
||||
"{\"score\": 2, \"summary\": \"плохо\", \"weakSpots\": [\"путаю\", \"медленно\"]}\n" +
|
||||
"Конец."
|
||||
val p = ReflectionParser.parse(raw)
|
||||
assertNotNull(p)
|
||||
assertEquals(2, p.score)
|
||||
assertEquals(listOf("путаю", "медленно"), p.weakSpots)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `accepts score as string`() {
|
||||
val raw = """{"score": "5", "summary": "ok", "weakSpots": []}"""
|
||||
val p = ReflectionParser.parse(raw)
|
||||
assertNotNull(p)
|
||||
assertEquals(5, p.score)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `returns null on missing score`() {
|
||||
val raw = """{"summary": "x", "weakSpots": []}"""
|
||||
assertNull(ReflectionParser.parse(raw))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `returns null on invalid JSON`() {
|
||||
assertNull(ReflectionParser.parse("not even json"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `handles escape sequences in weakSpots`() {
|
||||
// raw содержит 4 backslashes подряд; парсер \\ → \, итого 2 backslashes в результате
|
||||
val raw = """{"score": 3, "summary": "ok", "weakSpots": ["path\\\\file"]}"""
|
||||
val p = ReflectionParser.parse(raw)
|
||||
assertNotNull(p)
|
||||
// парсер снимает один escape: \\\\ → \\
|
||||
assertEquals(listOf("path\\\\file"), p.weakSpots)
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
package pw.binom.agentik.standalone.agent
|
||||
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import pw.binom.agentik.memory.ConversationTurn
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
import pw.binom.agentik.llm.tools.SkillMiner
|
||||
|
||||
class SkillMinerTest {
|
||||
|
||||
private fun turns(n: Int): List<ConversationTurn> = (1..n).map {
|
||||
ConversationTurn(userMessage = "q$it", assistantMessage = "a$it")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `mine returns parsed skills from model JSON reply`() = runBlocking {
|
||||
val llm = FakeLiteLlm()
|
||||
llm.reply = """{"skills": [{"name": "n", "description": "d", "body": "b"}]}"""
|
||||
val miner = SkillMiner(llm, maxTurns = 10, dispatcher = kotlinx.coroutines.Dispatchers.Unconfined)
|
||||
val out = miner.mine(turns(5), existing = emptyList())
|
||||
assertEquals(1, out.size)
|
||||
assertEquals("n", out[0].name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `mine with empty reply returns empty`() = runBlocking {
|
||||
val llm = FakeLiteLlm()
|
||||
llm.reply = "{}"
|
||||
val miner = SkillMiner(llm, dispatcher = kotlinx.coroutines.Dispatchers.Unconfined)
|
||||
assertTrue(miner.mine(turns(5), emptyList()).isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `miner failure degrades to empty list`() = runBlocking {
|
||||
val llm = FakeLiteLlm()
|
||||
llm.failMessage = "onnx died"
|
||||
val miner = SkillMiner(llm, dispatcher = kotlinx.coroutines.Dispatchers.Unconfined)
|
||||
assertTrue(miner.mine(turns(5), emptyList()).isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `miner respects maxTurns`() = runBlocking {
|
||||
val llm = FakeLiteLlm()
|
||||
llm.reply = """{"skills": []}"""
|
||||
val miner = SkillMiner(llm, maxTurns = 2, dispatcher = kotlinx.coroutines.Dispatchers.Unconfined)
|
||||
miner.mine(turns(30), emptyList())
|
||||
val prompt = llm.lastContents!!.first().let {
|
||||
val p = it as pw.binom.litert.LiteContentPart.Text
|
||||
p.text
|
||||
}
|
||||
// В промпт попало только последние 2 хода из 30.
|
||||
assertTrue(prompt.contains("q29"), "last turns missing: $prompt")
|
||||
assertTrue(!prompt.contains("q1\n"), "old turn leaked: $prompt")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `empty turns short-circuit without LLM call`() = runBlocking {
|
||||
val llm = FakeLiteLlm()
|
||||
llm.failMessage = "should not be called"
|
||||
val miner = SkillMiner(llm, dispatcher = kotlinx.coroutines.Dispatchers.Unconfined)
|
||||
val out = miner.mine(emptyList(), emptyList())
|
||||
assertTrue(out.isEmpty())
|
||||
assertTrue(llm.conversations.isEmpty(), "LLM must not be called for empty input")
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user