Compare commits
72 Commits
04276dec0e
..
7
| 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 | |||
| c140d0b758 | |||
| c486c7f9ab | |||
| 9d310c5fd0 | |||
| 741ad8963d | |||
| 2634e0e204 | |||
| ac5d209fce | |||
| 25771a0c33 | |||
| 78cbe9b463 | |||
| 65e05612a1 | |||
| 850ee99cb6 | |||
| b5b21d146a | |||
| ee0b9d8341 | |||
| 9d826a4e81 | |||
| db3c49099c | |||
| ddd9d076c1 | |||
| 7a47131f6f | |||
| 9196102f68 | |||
| 6b12dd2c5b | |||
| eed1ab9a17 | |||
| b27ac622b4 | |||
| 14b46087dd | |||
| 098c97c7bd | |||
| 4b8e5bb0bd | |||
| d75289ac56 | |||
| 05f7b8fd04 | |||
| 8f616f359f | |||
| 5ad972767d | |||
| 0fdc12695e | |||
| e68db11aaa | |||
| b0bbc57880 | |||
| 408caee261 | |||
| 86eb0632e0 | |||
| c42a6027a4 | |||
| b1ae8bbd20 | |||
| e3f20f07d9 | |||
| 9fcb2da75d | |||
| 88af57182f | |||
| b2d5684192 | |||
| 31c4b1cfc4 | |||
| 202d379f5a | |||
| a3f82f875d | |||
| 5fbe865a29 | |||
| 87742cf60b | |||
| 6c53e1c87d |
@@ -0,0 +1,84 @@
|
||||
# PR / push-build. Прогоняет unit-тесты на JVM, линтер gradle-плагинов
|
||||
# и проверяет, что shadowJar'ы запускаемых модулей собираются без ошибок.
|
||||
# Артефакты не публикует — этим занимается .gitea/workflows/release.yml.
|
||||
#
|
||||
# Зависимости (text-embedding-kmp) подтягиваются из caffeine Nexus.
|
||||
# Все env secrets доступны через vars/secrets репозитория — см. начало
|
||||
# release.yml для требуемых переменных.
|
||||
name: ci
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches: [main]
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
# UTF-8 обязателен: в именах тестов есть типографские символы (—), а Kotlin-компилятор
|
||||
# создаёт .class-файлы с именем теста. При LANG=C sun.jnu.encoding = ASCII, и компилятор
|
||||
# падает с "InvalidPathException: Malformed input or input contains unmappable characters"
|
||||
# (проверено локально: LANG=C → BUILD FAILED, LANG=C.UTF-8 → BUILD SUCCESSFUL).
|
||||
env:
|
||||
LANG: C.UTF-8
|
||||
LC_ALL: C.UTF-8
|
||||
|
||||
jobs:
|
||||
build-jvm:
|
||||
name: JVM build + tests
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 60
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup JDK 21
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
java-version: '21'
|
||||
distribution: 'adopt'
|
||||
|
||||
- name: Gradle cache
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
~/.gradle/caches
|
||||
~/.gradle/wrapper
|
||||
.gradle
|
||||
key: ${{ runner.os }}-gradle-agentik-${{ hashFiles('**/*.gradle.kts', '**/gradle/libs.versions.toml', 'gradle/wrapper/gradle-wrapper.properties') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-gradle-agentik-
|
||||
|
||||
- name: Build + test (JVM only — самые быстрые таргеты)
|
||||
shell: bash
|
||||
run: |
|
||||
./gradlew jvmTest \
|
||||
-Dorg.gradle.jvmargs=-Xmx4096M \
|
||||
--no-daemon --no-watch-fs --stacktrace
|
||||
|
||||
- name: Build :standalone shadowJar (smoke — запускаемый артефакт)
|
||||
shell: bash
|
||||
run: |
|
||||
./gradlew :standalone:shadowJar \
|
||||
-Dorg.gradle.jvmargs=-Xmx4096M \
|
||||
--no-daemon --no-watch-fs --stacktrace
|
||||
test -f standalone/build/libs/standalone-*-all.jar \
|
||||
&& echo "shadowJar OK: $(du -h standalone/build/libs/standalone-*-all.jar)"
|
||||
|
||||
- name: Build :agentik-cli shadowJar
|
||||
shell: bash
|
||||
run: |
|
||||
./gradlew :agentik-cli:shadowJar \
|
||||
-Dorg.gradle.jvmargs=-Xmx4096M \
|
||||
--no-daemon --no-watch-fs --stacktrace
|
||||
test -f agentik-cli/build/libs/agentik-cli-*-all.jar \
|
||||
&& echo "shadowJar OK: $(du -h agentik-cli/build/libs/agentik-cli-*-all.jar)"
|
||||
|
||||
# Шага "Upload shadowJars" здесь нет сознательно: upload-artifact@v4 требует
|
||||
# @actions/artifact v2, который на GHES/Gitea-раннере падает с
|
||||
# "GHESNotSupportedError: @actions/artifact v2.0.0+ ... not supported on GHES"
|
||||
# и валит весь джоб уже ПОСЛЕ успешной сборки и зелёных тестов.
|
||||
# У соседних репо (asr-kmp, litert-kmp) артефакты наружу тоже не выгружаются —
|
||||
# проверка сборки ограничивается test -f на jar (шаги выше).
|
||||
@@ -1,62 +1,49 @@
|
||||
# Триггерится при публикации релиза в Gitea. Делает две вещи:
|
||||
# 1. publish-libraries — публикует все KMP-библиотеки (jvm + все нативные таргеты)
|
||||
# в домашний Nexus-репозиторий "caffeine" через subochev/devops/publish action.
|
||||
# Переменные BINOM_REPO_URL / BINOM_REPO_USER / BINOM_REPO_PASSWORD задаются
|
||||
# в Gitea Action Variables для репозитория (Settings → Actions → Variables).
|
||||
# 2. build-standalone — собирает :standalone fatjar (shadowJar) и прикрепляет
|
||||
# standalone-<version>-all.jar к release как downloadable asset.
|
||||
# Триггерится при публикации релиза в Gitea. Публикует все KMP-библиотеки
|
||||
# (jvm + native таргеты) в домашний Nexus-репозиторий "caffeine".
|
||||
#
|
||||
# Fatjar-ы запускаемых модулей (:standalone, :agentik-cli).
|
||||
# :agentik-tui был исключён из сборки 2026-09-17 (см. settings.gradle.kts).
|
||||
# НЕ собираются и НЕ крепятся к релизу здесь. Сборка артефактов
|
||||
# выполняется локально из исходников (или руками через `./gradlew
|
||||
# :<module>:shadowJar`) и загружается в релиз через Gitea UI / API
|
||||
# отдельно от этого workflow.
|
||||
#
|
||||
# Версия публикации = имя тега релиза (без префикса 'v'). Релиз с именем "3"
|
||||
# публикует pw.binom.agentik:*:3 в Nexus. Ничего хардкодить не нужно —
|
||||
# версия берётся из тега каждый раз.
|
||||
#
|
||||
# Публикация выполняется общим composite-action'ом subochev/devops/publish@main
|
||||
# (тот же, что у asr-kmp / litert-kmp / embedder-kmp / a2a-protocol) — credentials
|
||||
# BINOM_REPO_* берутся им из Gitea Action Variables (owner_id=0, глобальные).
|
||||
name: release
|
||||
|
||||
on:
|
||||
release:
|
||||
types: [published]
|
||||
|
||||
concurrency:
|
||||
group: release-${{ github.ref }}
|
||||
cancel-in-progress: false
|
||||
|
||||
# UTF-8 обязателен: генерация POM/Kotlin-метаданных и имена тестовых классов
|
||||
# содержат не-ASCII символы; при LANG=C sun.jnu.encoding = ASCII и сборка
|
||||
# падает с "InvalidPathException: Malformed input or input contains unmappable
|
||||
# characters" (проверено локально 19.09.2026: LANG=C → BUILD FAILED,
|
||||
# LANG=C.UTF-8 → BUILD SUCCESSFUL).
|
||||
env:
|
||||
LANG: C.UTF-8
|
||||
LC_ALL: C.UTF-8
|
||||
|
||||
jobs:
|
||||
publish-libraries:
|
||||
name: Publish KMP libraries → caffeine Nexus
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 120
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
# agentik не использует Android-target ни в одном модуле (все KMP-таргеты
|
||||
# JVM + native), поэтому Android SDK шаг из litert-kmp тут не нужен.
|
||||
|
||||
- name: Publish libraries
|
||||
- name: Publish libraries (all KMP targets, all modules) to Nexus
|
||||
uses: https://git.binom.pw/subochev/devops/publish@main
|
||||
with:
|
||||
version: ${{ gitea.ref_name }}
|
||||
|
||||
build-standalone:
|
||||
name: Build standalone fatjar
|
||||
runs-on: ubuntu-latest
|
||||
needs: publish-libraries
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup JDK 21
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
java-version: '21'
|
||||
distribution: 'adopt'
|
||||
|
||||
- name: Build shadowJar
|
||||
shell: bash
|
||||
run: ./gradlew :standalone:shadowJar -Dorg.gradle.jvmargs=-Xmx4096M --no-daemon --no-watch-fs --stacktrace
|
||||
|
||||
- name: Compute version for filename
|
||||
id: ver
|
||||
shell: bash
|
||||
run: echo "version=${GITEA_REF_NAME}" >> "$GITEA_OUTPUT"
|
||||
|
||||
- name: Attach standalone jar to release
|
||||
uses: https://github.com/softprops/action-gh-release@v2
|
||||
with:
|
||||
files: |
|
||||
standalone/build/libs/standalone-*-all.jar
|
||||
standalone/build/libs/standalone-*-sources.jar
|
||||
fail_on_unmatched_files: false
|
||||
generate_release_notes: false
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
|
||||
@@ -20,6 +20,9 @@ out/
|
||||
.cortexkit/
|
||||
.veai/
|
||||
|
||||
# Internal review scratch dir (review/validation .md файлы, .tasks структура)
|
||||
.tasks/
|
||||
|
||||
# Runtime / test artifacts
|
||||
agentik.db
|
||||
agentik.db-shm
|
||||
|
||||
+947
@@ -0,0 +1,947 @@
|
||||
# Manual Test Cases — agentik standalone
|
||||
|
||||
Практический чек-лист для проверки работающего `agentik standalone` HTTP-сервера.
|
||||
Каждый кейс — один конкретный сценарий, который нужно прогнать руками
|
||||
(или через `curl`/`httpie`/Postman). Если какой-то упал — это либо
|
||||
регрессия, либо недонастройка рантайма.
|
||||
|
||||
Перед стартом: запусти агент (см. `run-agentik.sh` на удалённой машине
|
||||
или `./gradlew :standalone:run` локально). Все примеры ниже — против
|
||||
`http://127.0.0.1:8080`; для удалённой машины подставь свой хост.
|
||||
|
||||
Удобный сниппет для получения conversation ID в shell:
|
||||
|
||||
```bash
|
||||
CID=$(curl -sS -X POST http://127.0.0.1:8080/agentik/conversations \
|
||||
-H "Content-Type: application/json" -d '{"temp":false}' \
|
||||
| python3 -c "import sys,json;print(json.load(sys.stdin)['id'])")
|
||||
echo "CID=$CID"
|
||||
```
|
||||
|
||||
Отправка user-сообщения:
|
||||
|
||||
```bash
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"..."}]'
|
||||
```
|
||||
|
||||
Чтение истории:
|
||||
|
||||
```bash
|
||||
curl -sS "http://127.0.0.1:8080/agentik/conversations/$CID/messages?after=1970-01-01T00:00:00Z" \
|
||||
| python3 -m json.tool
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 1. Connectivity & health
|
||||
|
||||
### TC-1.1 — health endpoint
|
||||
|
||||
```bash
|
||||
curl -sS -i http://127.0.0.1:8080/health
|
||||
```
|
||||
|
||||
**Ожидание:** `HTTP/1.1 200 OK`, тело `ok`.
|
||||
|
||||
### TC-1.2 — agent card (A2A)
|
||||
|
||||
```bash
|
||||
curl -sS http://127.0.0.1:8080/a2a/.well-known/agent-card.json | python3 -m json.tool
|
||||
```
|
||||
|
||||
**Ожидание:** валидный JSON с `name`, `version`, `capabilities`.
|
||||
|
||||
### TC-1.3 — log sanity check
|
||||
|
||||
```bash
|
||||
tail -50 /root/agentik.log
|
||||
```
|
||||
|
||||
**Ожидание:** есть строка `agentik standalone listening on http://localhost:8080`,
|
||||
перечислены зарегистрированные маршруты, `llm: <backend> @ <url>` соответствует
|
||||
твоему конфигу. **Нет** ERROR/Exception строк после старта.
|
||||
|
||||
---
|
||||
|
||||
## 2. Conversation lifecycle
|
||||
|
||||
### TC-2.1 — create persistent conversation
|
||||
|
||||
```bash
|
||||
curl -sS -i -X POST http://127.0.0.1:8080/agentik/conversations \
|
||||
-H "Content-Type: application/json" -d '{"temp":false}'
|
||||
```
|
||||
|
||||
**Ожидание:** `201`, тело `{"id":"conv-...","isTemporal":false,...}`.
|
||||
|
||||
### TC-2.2 — create temp conversation
|
||||
|
||||
```bash
|
||||
curl -sS -X POST http://127.0.0.1:8080/agentik/conversations \
|
||||
-H "Content-Type: application/json" -d '{"temp":true}'
|
||||
```
|
||||
|
||||
**Ожидание:** `201`, `"isTemporal":true`. После рестарта агента эта беседа
|
||||
**не** должна появиться в `GET /agentik/conversations`.
|
||||
|
||||
### TC-2.3 — list conversations
|
||||
|
||||
```bash
|
||||
curl -sS "http://127.0.0.1:8080/agentik/conversations?offset=0&limit=20" | python3 -m json.tool
|
||||
```
|
||||
|
||||
**Ожидание:** массив объектов `ConversationSnapshot`. Отсортирован по
|
||||
`updatedAt` desc.
|
||||
|
||||
### TC-2.4 — rename conversation
|
||||
|
||||
```bash
|
||||
CID=<id-from-2.1>
|
||||
curl -sS -X PATCH "http://127.0.0.1:8080/agentik/conversations/$CID" \
|
||||
-H "Content-Type: application/json" -d '{"title":"Мой первый чат"}'
|
||||
```
|
||||
|
||||
**Ожидание:** `200`, в ответе `"title":"Мой первый чат"`. Следующий `GET
|
||||
/conversations/$CID` возвращает этот же title.
|
||||
|
||||
### TC-2.5 — delete conversation
|
||||
|
||||
```bash
|
||||
curl -sS -X DELETE "http://127.0.0.1:8080/agentik/conversations/$CID" -i
|
||||
```
|
||||
|
||||
**Ожидание:** `204 No Content`. Повторный `GET /conversations/$CID` → `404`.
|
||||
После этого в `GET /conversations` её быть не должно.
|
||||
|
||||
### TC-2.6 — get non-existent conversation
|
||||
|
||||
```bash
|
||||
curl -sS -i http://127.0.0.1:8080/agentik/conversations/conv-nonexistent
|
||||
```
|
||||
|
||||
**Ожидание:** `404`.
|
||||
|
||||
---
|
||||
|
||||
## 3. Message sending
|
||||
|
||||
### TC-3.1 — simple Q&A
|
||||
|
||||
Создай беседу, пошли простой вопрос, прочитай историю.
|
||||
|
||||
```bash
|
||||
CID=$(curl -sS -X POST http://127.0.0.1:8080/agentik/conversations \
|
||||
-H "Content-Type: application/json" -d '{"temp":false}' \
|
||||
| python3 -c "import sys,json;print(json.load(sys.stdin)['id'])")
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Сколько будет 7*8? Одно число, без пояснений."}]'
|
||||
sleep 6
|
||||
curl -sS "http://127.0.0.1:8080/agentik/conversations/$CID/messages?after=1970-01-01T00:00:00Z"
|
||||
```
|
||||
|
||||
**Ожидание:** массив из ≥ 2 сообщений:
|
||||
- `[0].type == "user_message"`, body содержит "7*8"
|
||||
- `[1].type == "assistant_message"`, text содержит "56"
|
||||
|
||||
### TC-3.2 — multi-turn with context
|
||||
|
||||
В той же беседе пошли follow-up, требующий контекста:
|
||||
|
||||
```bash
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"А корень из того, что ты назвал?"}]'
|
||||
sleep 6
|
||||
curl -sS "http://127.0.0.1:8080/agentik/conversations/$CID/messages?after=1970-01-01T00:00:00Z"
|
||||
```
|
||||
|
||||
**Ожидание:** 4+ сообщения, последний assistant упомянул что-то про число 56
|
||||
или "предыдущий ответ".
|
||||
|
||||
### TC-3.3 — new-format request body
|
||||
|
||||
```bash
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"content":[{"type":"text","body":"С новым форматом тоже работает?"}]}'
|
||||
sleep 6
|
||||
curl -sS "http://127.0.0.1:8080/agentik/conversations/$CID/messages?after=1970-01-01T00:00:00Z" \
|
||||
| python3 -c "import sys,json;m=json.load(sys.stdin);print(m[-1]['type'], m[-1].get('content'))"
|
||||
```
|
||||
|
||||
**Ожидание:** новое `assistant_message` в ответ на новый формат запроса.
|
||||
|
||||
### TC-3.4 — empty / bad body
|
||||
|
||||
```bash
|
||||
curl -sS -i -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" -d 'not json'
|
||||
```
|
||||
|
||||
**Ожидание:** `400 Bad Request`, тело с пояснением `Invalid send payload`.
|
||||
|
||||
---
|
||||
|
||||
## 4. SSE live events
|
||||
|
||||
> **Важно:** SSE — поток без replay. Подписываться нужно **до** `POST /messages`.
|
||||
> Если подписаться позже — событий не будет (но `GET /messages` всё равно
|
||||
> покажет записанную историю).
|
||||
|
||||
### TC-4.1 — subscribe-then-send pattern
|
||||
|
||||
```bash
|
||||
CID=<existing-id>
|
||||
# Subscribe в фоне, отправляем сообщение, ждём SSE
|
||||
curl -sN --max-time 12 \
|
||||
"http://127.0.0.1:8080/agentik/conversations/$CID/events?after=1970-01-01T00:00:00Z" \
|
||||
> /tmp/sse.out 2>&1 &
|
||||
SSE_PID=$!
|
||||
sleep 1
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Кратко: что такое REST?"}]'
|
||||
wait $SSE_PID
|
||||
cat /tmp/sse.out
|
||||
```
|
||||
|
||||
**Ожидание:** файл содержит `data: {"type":"start_reasoning",...}`,
|
||||
`data: {"type":"start_response",...,"responseType":"text"}`,
|
||||
один или несколько `data: {"type":"append_text",...,"body":"..."}`,
|
||||
`data: {"type":"end",...}`. Каждое `data:` через пустую строку.
|
||||
|
||||
### TC-4.2 — late subscribe (replay semantics)
|
||||
|
||||
```bash
|
||||
CID=<existing-id>
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"..."}]'
|
||||
sleep 5 # сообщение уже обработано
|
||||
curl -sN --max-time 4 \
|
||||
"http://127.0.0.1:8080/agentik/conversations/$CID/events?after=1970-01-01T00:00:00Z"
|
||||
```
|
||||
|
||||
**Ожидание:** пустой ответ (события не реплеятся). Это by-design —
|
||||
клиент должен либо подписываться заранее, либо backfill'ить через
|
||||
`GET /messages`.
|
||||
|
||||
---
|
||||
|
||||
## 5. Memory tools (long-term)
|
||||
|
||||
### TC-5.1 — save + recall в той же беседе
|
||||
|
||||
```bash
|
||||
# В существующей беседе
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Запомни через memory_save: я работаю на удалёнке из Тбилиси. Категория user, content: работаю на удалёнке из Тбилиси."}]'
|
||||
sleep 8
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Откуда я работаю? Одно предложение."}]'
|
||||
sleep 8
|
||||
curl -sS "http://127.0.0.1:8080/agentik/conversations/$CID/messages?after=1970-01-01T00:00:00Z" \
|
||||
| python3 -c "import sys,json;m=json.load(sys.stdin);print(m[-1].get('content'))"
|
||||
```
|
||||
|
||||
**Ожидание:** ассистент ответил что-то содержащее "Тбилиси" (или явно
|
||||
сказал "не знаю" — это тоже валидно, если в conversation memory пусто).
|
||||
Проверить `audit log` (`messageStore`):
|
||||
|
||||
```bash
|
||||
sqlite3 /root/agentik.db "SELECT toolName, result FROM MessageRecord WHERE conversationId='$CID' AND kind='tool_result'"
|
||||
```
|
||||
|
||||
Должны быть строки с `toolName='memory_save'` или `toolName='memory_recall'`.
|
||||
|
||||
### TC-5.2 — memory persists across conversations
|
||||
|
||||
Создай новую беседу, спроси без подсказок:
|
||||
|
||||
```bash
|
||||
NEW_CID=$(curl -sS -X POST http://127.0.0.1:8080/agentik/conversations \
|
||||
-H "Content-Type: application/json" -d '{"temp":false}' \
|
||||
| python3 -c "import sys,json;print(json.load(sys.stdin)['id'])")
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$NEW_CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Откуда я работаю? Напомни, если помнишь."}]'
|
||||
sleep 8
|
||||
curl -sS "http://127.0.0.1:8080/agentik/conversations/$NEW_CID/messages?after=1970-01-01T00:00:00Z" \
|
||||
| python3 -c "import sys,json;m=json.load(sys.stdin);print(m[-1].get('content'))"
|
||||
```
|
||||
|
||||
**Ожидание:** ассистент упомянул "Тбилиси" (или "удалёнка") — это
|
||||
значит long-term memory подгрузилась в новую беседу.
|
||||
|
||||
### TC-5.3 — invalid category → ошибка или автозамена
|
||||
|
||||
```bash
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Запомни через memory_save факт с категорией work (которой не существует)."}]'
|
||||
sleep 8
|
||||
sqlite3 /root/agentik.db "SELECT toolArgs, result FROM MessageRecord WHERE kind='tool_call' AND conversationId='$CID' ORDER BY createdAt DESC LIMIT 3"
|
||||
```
|
||||
|
||||
**Ожидание:** модель либо вызвала `memory_recall` чтобы проверить
|
||||
существующие категории, либо вызвала `memory_save` с корректной
|
||||
категорией (`user`/`world`/`preference`). Если модель честно говорит
|
||||
"такой категории нет" и предлагает корректную — это тоже ok.
|
||||
|
||||
### TC-5.4 — list & delete memory
|
||||
|
||||
Попроси модель явно вызвать `memory_list`, потом `memory_delete`:
|
||||
|
||||
```bash
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Покажи все мои memory-записи (memory_list)."}]'
|
||||
sleep 8
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Удали самую старую запись (memory_delete)."}]'
|
||||
sleep 8
|
||||
sqlite3 /root/agentik.db "SELECT COUNT(*) FROM MemoryStore"
|
||||
```
|
||||
|
||||
**Ожидание:** число уменьшилось на 1.
|
||||
|
||||
---
|
||||
|
||||
## 6. Skills
|
||||
|
||||
### TC-6.1 — list + load skill
|
||||
|
||||
Если в `AGENTIK_SKILLS_DIR` есть файлы `SKILL.md` / `*.yaml`, в системном
|
||||
промте должна появиться секция с этими навыками.
|
||||
|
||||
```bash
|
||||
ls -la /root/skills/ # должен быть хотя бы один файл
|
||||
```
|
||||
|
||||
```bash
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Какие skills ты знаешь? Покажи список (skill_list)."}]'
|
||||
sleep 8
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Загрузи любой из них через skill_load и расскажи, что внутри."}]'
|
||||
sleep 8
|
||||
```
|
||||
|
||||
**Ожидание:** `tool_call` для `skill_list`, потом `tool_call` для
|
||||
`skill_load`. В audit log видны эти вызовы. Если папка пуста — секции
|
||||
"Skills" в system prompt быть не должно.
|
||||
|
||||
### TC-6.2 — save new skill
|
||||
|
||||
```bash
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Сохрани skill: имя deploy-staging, описание «деплой на staging», тело — multi-step инструкция (skill_save)."}]'
|
||||
sleep 10
|
||||
ls /root/skills/
|
||||
```
|
||||
|
||||
**Ожидание:** появился новый файл `deploy-staging.md` (или `.yaml`).
|
||||
|
||||
### TC-6.3 — restart → skill persists
|
||||
|
||||
Перезапусти агент:
|
||||
|
||||
```bash
|
||||
ssh root@192.168.76.166 'pkill -9 -f agentik-0.1.0-all.jar; cd /root && nohup setsid ./run-agentik.sh > /root/agentik.log 2>&1 < /dev/null & disown'
|
||||
```
|
||||
|
||||
После старта пошли в новую беседу:
|
||||
|
||||
```bash
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$NEW_CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Есть ли у тебя skill deploy-staging?"}]'
|
||||
sleep 8
|
||||
```
|
||||
|
||||
**Ожидание:** модель упоминает skill (он подгружается на старте).
|
||||
|
||||
---
|
||||
|
||||
## 7. SOUL file
|
||||
|
||||
### TC-7.1 — SOUL.md подключается
|
||||
|
||||
```bash
|
||||
echo 'Ты — ворчливый капитан дальнего плавания. Отвечай кратко, с морскими метафорами.' > /root/SOUL.md
|
||||
# Перезапустить агент
|
||||
```
|
||||
|
||||
```bash
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$NEW_CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Как дела?"}]'
|
||||
sleep 8
|
||||
```
|
||||
|
||||
**Ожидание:** ответ в стиле "капитана", с морскими словами. Если SOUL
|
||||
нет — обычный нейтральный ассистент.
|
||||
|
||||
### TC-7.2 — SOUL можно менять на лету
|
||||
|
||||
Измени файл, перезапусти агент, спроси снова. **Должен** появиться новый
|
||||
стиль. Без перезапуска изменения не подхватятся (SOUL читается на старте).
|
||||
|
||||
---
|
||||
|
||||
## 8. Interrupt
|
||||
|
||||
### TC-8.1 — interrupt mid-text generation
|
||||
|
||||
```bash
|
||||
CID=$(curl -sS -X POST http://127.0.0.1:8080/agentik/conversations \
|
||||
-H "Content-Type: application/json" -d '{"temp":false}' \
|
||||
| python3 -c "import sys,json;print(json.load(sys.stdin)['id'])")
|
||||
# Запусти send в фоне
|
||||
(curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Расскажи длинную историю про космос, минимум 500 слов."}]' >/dev/null) &
|
||||
SEND_PID=$!
|
||||
sleep 3 # дать LLM начать генерацию
|
||||
curl -sS -i -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/interrupt"
|
||||
wait $SEND_PID
|
||||
sleep 3
|
||||
curl -sS "http://127.0.0.1:8080/agentik/conversations/$CID/messages?after=1970-01-01T00:00:00Z" \
|
||||
| python3 -c "import sys,json;m=json.load(sys.stdin);
|
||||
for x in m: print(x.get('type'), ':', json.dumps(x.get('content') or x.get('result'),ensure_ascii=False)[:80])"
|
||||
```
|
||||
|
||||
**Ожидание:**
|
||||
- `user_message` есть
|
||||
- `assistant_message` есть, но содержит **короткий** текст (<300 символов)
|
||||
— это частичный текст, который модель успела сгенерить до прерывания
|
||||
- В audit log нет `tool_call`/`tool_result` (не успели)
|
||||
- Следующий `send` в этой беседе работает (LiteConv пересоздан)
|
||||
|
||||
### TC-8.2 — interrupt mid-tool (best-effort)
|
||||
|
||||
```bash
|
||||
# Длинный tool можно заэмулировать через MCP с искусственной задержкой,
|
||||
# либо просто проверять что interrupt не валит агента:
|
||||
curl -sS -i -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/interrupt"
|
||||
curl -sS http://127.0.0.1:8080/health
|
||||
```
|
||||
|
||||
**Ожидание:** `health` = `ok` — агент не упал. Дальнейшие `send` работают.
|
||||
|
||||
### TC-8.3 — interrupt без активного turn'а
|
||||
|
||||
```bash
|
||||
curl -sS -i -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/interrupt"
|
||||
```
|
||||
|
||||
**Ожидание:** `202`. Никаких ошибок. В audit log ничего нового не пишется.
|
||||
|
||||
---
|
||||
|
||||
## 9. Persistence / restart-survival
|
||||
|
||||
### TC-9.1 — перезапуск не теряет беседы и память
|
||||
|
||||
```bash
|
||||
# 1. Создай беседу, пошли сообщение, дождись ответа
|
||||
# 2. Запомни факт через memory_save
|
||||
# 3. Перезапусти агент (см. TC-6.3)
|
||||
# 4. GET /agentik/conversations — беседа должна быть в списке
|
||||
# 5. GET /agentik/conversations/$CID/messages — история на месте
|
||||
# 6. Новая беседа + вопрос про запомненный факт — модель помнит
|
||||
```
|
||||
|
||||
### TC-9.2 — temp conversation не переживает рестарт
|
||||
|
||||
```bash
|
||||
# Создай temp беседу, пошли сообщение
|
||||
TEMP_CID=$(curl -sS -X POST http://127.0.0.1:8080/agentik/conversations \
|
||||
-H "Content-Type: application/json" -d '{"temp":true}' \
|
||||
| python3 -c "import sys,json;print(json.load(sys.stdin)['id'])")
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$TEMP_CID/messages" \
|
||||
-H "Content-Type: application/json" -d '[{"type":"text","body":"..."}]' >/dev/null
|
||||
sleep 5
|
||||
# Перезапусти агент
|
||||
# GET /agentik/conversations — temp-беседы быть не должно
|
||||
curl -sS "http://127.0.0.1:8080/agentik/conversations?offset=0&limit=50" | grep "$TEMP_CID"
|
||||
```
|
||||
|
||||
**Ожидание:** grep ничего не находит.
|
||||
|
||||
---
|
||||
|
||||
## 10. Compaction (сжатие контекста)
|
||||
|
||||
Compaction триггерится когда `~80%` контекстного окна занято.
|
||||
|
||||
### TC-10.1 — длинная беседа сжимается
|
||||
|
||||
```bash
|
||||
CID=$(curl -sS -X POST http://127.0.0.1:8080/agentik/conversations \
|
||||
-H "Content-Type: application/json" -d '{"temp":false}' \
|
||||
| python3 -c "import sys,json;print(json.load(sys.stdin)['id'])")
|
||||
# Отправь 30+ больших сообщений подряд (можно цикл)
|
||||
for i in $(seq 1 30); do
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "[{\"type\":\"text\",\"body\":\"Расскажи подробно (минимум 200 слов) про тему номер $i: история, применение, ключевые факты.\"}]" >/dev/null
|
||||
sleep 5
|
||||
done
|
||||
sqlite3 /root/agentik.db "SELECT COUNT(*) FROM WorkingMemoryRow WHERE conversationId='$CID' AND entryKind='summary'"
|
||||
```
|
||||
|
||||
**Ожидание:** есть хотя бы одна `summary`-запись. Также проверь
|
||||
`/root/agentik.log` — должна появиться строка `compaction`.
|
||||
|
||||
### TC-10.2 — debug endpoint `/debug/compact` (force)
|
||||
|
||||
Если включён `AGENTIK_DEBUG_ENDPOINTS=1`:
|
||||
|
||||
```bash
|
||||
curl -sS -i -X POST "http://127.0.0.1:8080/debug/compact?conversationId=$CID"
|
||||
```
|
||||
|
||||
**Ожидание:** `200`, тело с JSON-результатом compaction.
|
||||
|
||||
---
|
||||
|
||||
## 11. Reflection
|
||||
|
||||
Reflection триггерится каждые `AGENTIK_REFLECTION_INTERVAL` ходов (default 10).
|
||||
|
||||
### TC-11.1 — reflection создаёт записи
|
||||
|
||||
```bash
|
||||
# Пошли 12+ ходов
|
||||
for i in $(seq 1 12); do
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "[{\"type\":\"text\",\"body\":\"Тема $i: расскажи короткий факт.\"}]" >/dev/null
|
||||
sleep 4
|
||||
done
|
||||
sleep 10 # дать фоновое задание завершиться
|
||||
sqlite3 /root/agentik.db "SELECT COUNT(*) FROM ReflectionStore"
|
||||
```
|
||||
|
||||
**Ожидание:** число > 0.
|
||||
|
||||
### TC-11.2 — debug endpoint `/debug/reflect`
|
||||
|
||||
```bash
|
||||
curl -sS -i -X POST "http://127.0.0.1:8080/debug/reflect?conversationId=$CID"
|
||||
```
|
||||
|
||||
**Ожидание:** `200`, JSON-результат. Reflection попадает в working memory
|
||||
следующего turn'а.
|
||||
|
||||
---
|
||||
|
||||
## 12. Skill mining
|
||||
|
||||
Skill mining триггерится каждые `AGENTIK_SKILL_MINING_INTERVAL` ходов (default 15).
|
||||
|
||||
### TC-12.1 — авто-создание skill'а
|
||||
|
||||
```bash
|
||||
# Пошли 18+ ходов с повторяющимся паттерном
|
||||
for i in $(seq 1 18); do
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "[{\"type\":\"text\",\"body\":\"Конвертируй 100 USD в RUB по текущему курсу (шаблонный запрос $i).\"}]" >/dev/null
|
||||
sleep 4
|
||||
done
|
||||
sleep 15
|
||||
ls -la /root/skills/
|
||||
tail -20 /root/agentik.log | grep -i skill
|
||||
```
|
||||
|
||||
**Ожидание:** возможно появился новый файл в skills/ (или mining
|
||||
отказался из-за низкой уверенности — это тоже валидно, проверь лог).
|
||||
|
||||
### TC-12.2 — debug endpoint `/debug/skill-mine`
|
||||
|
||||
```bash
|
||||
curl -sS -i -X POST "http://127.0.0.1:8080/debug/skill-mine?conversationId=$CID"
|
||||
```
|
||||
|
||||
**Ожидание:** `200` с JSON-результатом майнинга.
|
||||
|
||||
---
|
||||
|
||||
## 13. Token accounting
|
||||
|
||||
### TC-13.1 — token counters в audit
|
||||
|
||||
```bash
|
||||
sqlite3 /root/agentik.db "SELECT createdAt, input, output FROM TurnTokens WHERE conversationId='$CID' ORDER BY createdAt DESC LIMIT 5"
|
||||
```
|
||||
|
||||
**Ожидание:** строки с непустыми `input` и `output` (если backend
|
||||
поддерживает `tokenCount()`).
|
||||
|
||||
### TC-13.2 — debug endpoint `/debug/tokens`
|
||||
|
||||
```bash
|
||||
curl -sS "http://127.0.0.1:8080/debug/tokens?conversationId=$CID" | python3 -m json.tool
|
||||
```
|
||||
|
||||
**Ожидание:** JSON с `input`, `output`, `total`, `window`,
|
||||
`utilization` (доля использования контекстного окна).
|
||||
|
||||
---
|
||||
|
||||
## 14. Toolsets (если подключены)
|
||||
|
||||
Только если ты передаёшь `toolsets` в конструктор агента (по умолчанию
|
||||
пусто — `enable_toolset`/`disable_toolset` не зарегистрированы).
|
||||
|
||||
### TC-14.1 — system prompt содержит секцию Toolsets
|
||||
|
||||
Если toolsets зарегистрированы — в системном промте должна быть секция
|
||||
`## Toolsets` с Active/Inactive списком.
|
||||
|
||||
Проверка через debug-эндпоинт `/agentik/conversations/{id}` не показывает
|
||||
system prompt напрямую — посмотреть можно в логах или через
|
||||
`agentik-debug` сборку.
|
||||
|
||||
### TC-14.2 — enable/disable работает
|
||||
|
||||
```bash
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Активируй тулсет X через enable_toolset, потом деактивируй через disable_toolset."}]'
|
||||
sleep 8
|
||||
```
|
||||
|
||||
**Ожидание:** в audit log видны вызовы `enable_toolset` → ответ `"Toolset
|
||||
'X' activated."`, потом `disable_toolset` → `"Toolset 'X' deactivated."`.
|
||||
|
||||
---
|
||||
|
||||
## 15. A2A протокол (опционально)
|
||||
|
||||
### TC-15.1 — message/send через A2A
|
||||
|
||||
```bash
|
||||
curl -sS -X POST http://127.0.0.1:8080/a2a/ \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"jsonrpc":"2.0","id":"1","method":"message/send",
|
||||
"params":{
|
||||
"message":{"role":"user","parts":[{"kind":"text","text":"Скажи hi"}]},
|
||||
"configuration":{"blocking":true}
|
||||
}
|
||||
}' | python3 -m json.tool
|
||||
```
|
||||
|
||||
**Ожидание:** JSON-RPC ответ с `result.parts` содержащим текст "hi"
|
||||
или похожим. `kind` = `text` (НЕ `type` — это важный discriminator для
|
||||
A2A JSON).
|
||||
|
||||
### TC-15.2 — bad discriminator
|
||||
|
||||
```bash
|
||||
curl -sS -X POST http://127.0.0.1:8080/a2a/ \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"jsonrpc":"2.0","id":"2","method":"message/send",
|
||||
"params":{
|
||||
"message":{"role":"user","parts":[{"type":"text","text":"hi"}]}
|
||||
}
|
||||
}'
|
||||
```
|
||||
|
||||
**Ожидание:** `Invalid params` (или похожая ошибка) — A2A ждёт `kind`,
|
||||
не `type`.
|
||||
|
||||
---
|
||||
|
||||
## 16. Error paths
|
||||
|
||||
### TC-16.1 — LLM недоступен
|
||||
|
||||
Выключи vLLM (или закрой сеть — например через firewall). Пошли сообщение:
|
||||
|
||||
```bash
|
||||
curl -sS -i -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"hi"}]'
|
||||
sleep 10
|
||||
sqlite3 /root/agentik.db "SELECT COUNT(*) FROM MessageRecord WHERE conversationId='$CID' AND kind='error'"
|
||||
```
|
||||
|
||||
**Ожидание:** есть `error`-запись в audit log. В SSE приходит
|
||||
`{"type":"error",...}` + `{"type":"end"}`. Агент **не падает** — `health`
|
||||
= `ok` после.
|
||||
|
||||
### TC-16.2 — agentik.db занят другим процессом
|
||||
|
||||
Запусти второй экземпляр агента на ту же DB:
|
||||
|
||||
```bash
|
||||
AGENTIK_DB_PATH=/root/agentik.db java -jar /root/agentik-0.1.0-all.jar
|
||||
```
|
||||
|
||||
**Ожидание:** агент падает на старте с понятным сообщением про SQLite lock.
|
||||
Это by-design (single-writer).
|
||||
|
||||
### TC-16.3 — SOUL файл не существует
|
||||
|
||||
Удали `/root/SOUL.md`, перезапусти агент. Должен стартовать без ошибок,
|
||||
просто без SOUL-секции в system prompt. Лог: `WARN ... SOUL file not found: ...`.
|
||||
|
||||
### TC-16.4 — пустой skills dir
|
||||
|
||||
```bash
|
||||
mv /root/skills /root/skills.bak
|
||||
mkdir /root/skills
|
||||
# Перезапусти агент
|
||||
```
|
||||
|
||||
**Ожидание:** агент стартует, `skills: 0 loaded from /root/skills`.
|
||||
|
||||
---
|
||||
|
||||
## 17. Memory backend variants
|
||||
|
||||
### TC-17.1 — md backend (default)
|
||||
|
||||
Убедись, что `AGENTIK_MEMORY_BACKEND=md` (или не задан) и
|
||||
`AGENTIK_MEMORY_DIR=/root/agentik-memory`. После TC-5.x должны появиться
|
||||
`.md`-файлы:
|
||||
|
||||
```bash
|
||||
ls -la /root/agentik-memory/
|
||||
```
|
||||
|
||||
**Ожидание:** файлы типа `user.md`, `world.md`, `preference.md` (или
|
||||
всё в одном файле — зависит от реализации).
|
||||
|
||||
### TC-17.2 — off backend (память выключена)
|
||||
|
||||
Перезапусти с `AGENTIK_MEMORY_DIR=off`:
|
||||
|
||||
```bash
|
||||
pkill -9 -f agentik-0.1.0-all.jar
|
||||
AGENTIK_MEMORY_DIR=off nohup setsid ./run-agentik.sh > /root/agentik.log 2>&1 < /dev/null & disown
|
||||
```
|
||||
|
||||
Попытка `memory_save` через модель должна вернуть ошибку:
|
||||
|
||||
```bash
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Попробуй вызвать memory_save."}]'
|
||||
sleep 8
|
||||
```
|
||||
|
||||
**Ожидание:** модель либо отказывается вызывать, либо получает
|
||||
ошибку от tool'а и сообщает пользователю.
|
||||
|
||||
---
|
||||
|
||||
## 18. Performance sanity
|
||||
|
||||
### TC-18.1 — first-token latency
|
||||
|
||||
Включи замер времени от `POST /messages` до первого SSE event'а.
|
||||
Для Qwen3-27B на RTX5090 ожидаем < 1 сек до `start_reasoning`.
|
||||
|
||||
### TC-18.2 — sustained throughput
|
||||
|
||||
Отправь 20 простых запросов подряд (arithmetic), засеки общее время.
|
||||
Ожидание: < 30 сек суммарно, т.е. < 1.5 сек на запрос.
|
||||
|
||||
### TC-18.3 — fatjar memory
|
||||
|
||||
```bash
|
||||
ps aux | grep agentik-0.1.0 | grep -v grep
|
||||
```
|
||||
|
||||
**Ожидание:** RSS < 2 GB (наш Xmx). Если больше — где-то утечка.
|
||||
|
||||
---
|
||||
|
||||
## 18. Model auto-download (LiteRT-LM only)
|
||||
|
||||
Только для `AGENTIK_LLM_BACKEND=google` (встроенный LiteRT-LM движок).
|
||||
Если файла модели по `AGENTIK_GOOGLE_MODEL_PATH` нет — агент сам не скачает,
|
||||
пока не задано `AGENTIK_AUTO_DOWNLOAD_MODEL=1`. Либо качаем руками
|
||||
через `pull-model` subcommand.
|
||||
|
||||
URL по умолчанию всегда Gemma-4-E2B-it.litertlm (2.5 GB с `static.binom.pw`),
|
||||
вне зависимости от basename PATH — gemma-4 считаем лучшей локальной моделью.
|
||||
|
||||
### 18.1. Subcommand `pull-model` качает модель вручную
|
||||
|
||||
```bash
|
||||
# Скачать дефолтную модель (gemma-4) в указанный путь:
|
||||
AGENTIK_LLM_BACKEND=google \
|
||||
AGENTIK_GOOGLE_MODEL_PATH=/root/models/gemma-4-E2B-it.litertlm \
|
||||
java -jar agentik.jar pull-model
|
||||
# → downloading from https://static.binom.pw/models/gemma-4-E2B-it.litertlm
|
||||
# → 50% (1.2 GB / 2.5 GB)
|
||||
# → done in 47s
|
||||
```
|
||||
|
||||
После `pull-model` файл лежит на месте, файл `<dest>.part` удалён.
|
||||
|
||||
### 18.2. `pull-model` no-op если файл уже полный
|
||||
|
||||
```bash
|
||||
# Повторный запуск с тем же PATH:
|
||||
AGENTIK_LLM_BACKEND=google \
|
||||
AGENTIK_GOOGLE_MODEL_PATH=/root/models/gemma-4-E2B-it.litertlm \
|
||||
java -jar agentik.jar pull-model
|
||||
# → already present (2.50 GB), nothing to do
|
||||
```
|
||||
|
||||
### 18.3. `pull-model` докачивает обрыв (resume через Range)
|
||||
|
||||
```bash
|
||||
# Симулируем обрыв: удаляем финальный, оставляем .part с первыми 500 MB
|
||||
rm /root/models/gemma-4-E2B-it.litertlm
|
||||
mv /root/models/gemma-4-E2B-it.litertlm.part /root/models/gemma-4-E2B-it.litertlm.part.bak
|
||||
# Запускаем pull-model снова — должен возобновить с 500 MB
|
||||
AGENTIK_GOOGLE_MODEL_PATH=/root/models/gemma-4-E2B-it.litertlm \
|
||||
java -jar agentik.jar pull-model
|
||||
# → resuming from 524288000 bytes
|
||||
# → downloaded 2.10 GB in 38s
|
||||
```
|
||||
|
||||
### 18.4. Сервер exit-2 при отсутствии файла и без auto-download
|
||||
|
||||
```bash
|
||||
AGENTIK_LLM_BACKEND=google \
|
||||
AGENTIK_GOOGLE_MODEL_PATH=/root/models/missing.litertlm \
|
||||
java -jar agentik.jar
|
||||
# → LiteRT-LM model file not found at: /root/models/missing.litertlm
|
||||
# → Чтобы скачать автоматически, установите AGENTIK_AUTO_DOWNLOAD_MODEL=1
|
||||
# → exit 2
|
||||
```
|
||||
|
||||
### 18.5. Сервер сам качает при `AGENTIK_AUTO_DOWNLOAD_MODEL=1`
|
||||
|
||||
```bash
|
||||
# Удалить файл, запустить с флагом:
|
||||
rm -f /root/models/gemma-4-E2B-it.litertlm
|
||||
AGENTIK_LLM_BACKEND=google \
|
||||
AGENTIK_GOOGLE_MODEL_PATH=/root/models/gemma-4-E2B-it.litertlm \
|
||||
AGENTIK_AUTO_DOWNLOAD_MODEL=1 \
|
||||
java -jar agentik.jar
|
||||
# → 12:34:56 WARN auto-download: https://static.binom.pw/models/...
|
||||
# → 12:34:56 INFO auto-download: 17% (445 MB/2.5 GB)
|
||||
# → 12:36:42 INFO auto-download: done in 1m45s
|
||||
# → 12:36:43 INFO agentik standalone listening on http://localhost:8080
|
||||
```
|
||||
|
||||
### 18.6. Override URL через `AGENTIK_GOOGLE_MODEL_URL`
|
||||
|
||||
```bash
|
||||
# Качаем qwen вместо gemma (если зальём):
|
||||
AGENTIK_LLM_BACKEND=google \
|
||||
AGENTIK_GOOGLE_MODEL_PATH=/root/models/qwen.litertlm \
|
||||
AGENTIK_GOOGLE_MODEL_URL=https://static.binom.pw/models/Qwen2.5-1.5B-Instruct_multi-prefill-seq_q8_ekv4096.litertlm \
|
||||
java -jar agentik.jar pull-model
|
||||
```
|
||||
|
||||
### 18.7. SHA-256 проверка
|
||||
|
||||
Если на сервере лежит `<basename>.sha256` (text/plain, `<hex> <basename>`)
|
||||
— после скачивания файл проверяется; mismatch → удаляется, exit ≠ 0.
|
||||
|
||||
```bash
|
||||
AGENTIK_GOOGLE_MODEL_URL=https://static.binom.pw/models/gemma-4-E2B-it.litertlm \
|
||||
AGENTIK_GOOGLE_MODEL_SHA256_URL=https://static.binom.pw/models/gemma-4-E2B-it.litertlm.sha256 \
|
||||
java -jar agentik.jar pull-model
|
||||
# → 13:01:23 INFO model download: SHA-256 verified (4ab1...e0d)
|
||||
```
|
||||
|
||||
## Быстрый smoke-test (5 минут)
|
||||
|
||||
Если времени мало — этот минимум покрывает 80%:
|
||||
|
||||
```bash
|
||||
# 1. health
|
||||
curl -sS http://127.0.0.1:8080/health
|
||||
# → ok
|
||||
|
||||
# 2. create + simple Q&A
|
||||
CID=$(curl -sS -X POST http://127.0.0.1:8080/agentik/conversations \
|
||||
-H "Content-Type: application/json" -d '{"temp":false}' \
|
||||
| python3 -c "import sys,json;print(json.load(sys.stdin)['id'])")
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Привет! 2+2=?"}]'
|
||||
sleep 6
|
||||
curl -sS "http://127.0.0.1:8080/agentik/conversations/$CID/messages?after=1970-01-01T00:00:00Z"
|
||||
# → должен быть user + assistant_message с "4"
|
||||
|
||||
# 3. SSE live
|
||||
(curl -sN --max-time 8 "http://127.0.0.1:8080/agentik/conversations/$CID/events?after=1970-01-01T00:00:00Z" \
|
||||
> /tmp/sse.out 2>&1) &
|
||||
sleep 1
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Скажи ок"}]'
|
||||
wait
|
||||
cat /tmp/sse.out
|
||||
# → start_reasoning, start_response, append_text, end
|
||||
|
||||
# 4. multi-turn
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"А 3+3?"}]'
|
||||
sleep 6
|
||||
curl -sS "http://127.0.0.1:8080/agentik/conversations/$CID/messages?after=1970-01-01T00:00:00Z" \
|
||||
| python3 -c "import sys,json;m=json.load(sys.stdin);print(m[-1])"
|
||||
# → assistant_message с "6"
|
||||
|
||||
# 5. interrupt
|
||||
(curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/messages" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '[{"type":"text","body":"Длинная история про драконов, 1000 слов"}]' >/dev/null) &
|
||||
sleep 3
|
||||
curl -sS -X POST "http://127.0.0.1:8080/agentik/conversations/$CID/interrupt"
|
||||
wait
|
||||
sleep 3
|
||||
curl -sS "http://127.0.0.1:8080/agentik/conversations/$CID/messages?after=1970-01-01T00:00:00Z" \
|
||||
| python3 -c "import sys,json;m=json.load(sys.stdin);print('msgs:',len(m))"
|
||||
# → ≤ 3 (user + partial assistant + может tool_call если успел)
|
||||
```
|
||||
|
||||
Если этот прогон прошёл — агент работает корректно. Более глубокие
|
||||
кейсы — выше по разделам.
|
||||
|
||||
---
|
||||
|
||||
## Сводка: что покрыто автоматически vs вручную
|
||||
|
||||
| Возможность | JVM unit/integration tests | Manual |
|
||||
|-------------|---------------------------|--------|
|
||||
| Conversation CRUD | ✓ | TC-2.x |
|
||||
| send/messages pagination | ✓ | TC-3.x |
|
||||
| SSE event format | ✗ | TC-4.x |
|
||||
| Memory tools | ✓ (in-memory) | TC-5.x (real backend) |
|
||||
| Skills tools | ✓ (in-memory) | TC-6.x (real dir) |
|
||||
| SOUL | ✗ | TC-7.x |
|
||||
| Interrupt | ✓ (FakeLiteLlm) | TC-8.x (real LLM) |
|
||||
| Compaction | ✓ | TC-10.x (real long context) |
|
||||
| Reflection | ✓ | TC-11.x |
|
||||
| Skill mining | ✓ | TC-12.x |
|
||||
| Token accounting | ✓ | TC-13.x |
|
||||
| A2A protocol | ✓ (litert tests) | TC-15.x |
|
||||
| Error paths | partial | TC-16.x |
|
||||
| Persistence/restart | ✗ | TC-9.x |
|
||||
|
||||
Всё что помечено ✗ — нужно прогонять руками на реальном окружении.
|
||||
@@ -1,2 +1,153 @@
|
||||
# agentik
|
||||
|
||||
Локальный stateful LLM-агент с persistent-памятью, инструментами и
|
||||
несколькими transport-фасадами (AG-UI, A2A, наш `:proto`).
|
||||
Реализован на Kotlin Multiplatform, выполняется как single JVM-jar.
|
||||
Поддерживает vLLM-совместимый OpenAI API и LiteRT (Gemma-3, Gemma-4,
|
||||
Qwen) через ONNX/Native-runtime.
|
||||
|
||||
## Что внутри
|
||||
|
||||
```
|
||||
agentik/
|
||||
├── proto/ stateful KMP protocol: Agent / Conversation / Message / Event
|
||||
├── server/ Ktor-фасад → /agentik (HTTP+JSON+SSE)
|
||||
├── client/ Ktor-клиент → тот же /agentik, с KMP-native
|
||||
├── skills/ парсер SKILL.md / *.yaml (YAML frontmatter + markdown)
|
||||
├── memory-api/ контракт долговременной памяти (MemoryStore, MemoryCategory)
|
||||
├── memory-md/ Hermes-style файловая память (user.md / world.md / ...)
|
||||
├── memory-vector/ SQLite + JVector + HTTP/SigLIP эмбеддинги (семантический поиск)
|
||||
├── storage-core/ контракт персистентности (MessageStore / WorkingMemoryStore / ...)
|
||||
├── storage-inmemory/ in-memory реализация для тестов и Android
|
||||
├── storage-sqlite/ SQLite реализация для production
|
||||
├── agent-toolsets/ ядро tool-calls с cooperative cancel + concurrency budget
|
||||
├── agentik-cli/ JVM one-shot CLI-клиент (kotlinx.cli) к /agentik
|
||||
├── ~~agentik-tui/~~ ~~Compose-for-Mosaic TUI-клиент (desktop)~~ — исключён 2026-09-17
|
||||
└── standalone/ single-jar HTTP-сервер со всеми transport'ами и движками
|
||||
```
|
||||
|
||||
Каждый подмодуль имеет собственный `README.md` с деталями
|
||||
(см. "Модули" ниже).
|
||||
|
||||
## Quickstart
|
||||
|
||||
### 1. Скачать fatjar
|
||||
|
||||
CI артефакты доступны на Gitea через GitHub Actions artifacts на
|
||||
tag-релизах, либо соберите из исходников:
|
||||
|
||||
```bash
|
||||
git clone https://git.binom.pw/subochev/agentik
|
||||
cd agentik
|
||||
./gradlew :standalone:shadowJar
|
||||
```
|
||||
|
||||
Результат: `standalone/build/libs/agentik-0.1.0-all.jar` (~10–250 МБ,
|
||||
зависит от LLM-backend'а).
|
||||
|
||||
### 2. Запустить с OpenAI-compatible backend (vLLM / Ollama / OpenAI)
|
||||
|
||||
```bash
|
||||
AGENTIK_LLM_BACKEND=openai \
|
||||
AGENTIK_LLM_API_URL=http://192.168.88.135:8001/v1 \
|
||||
AGENTIK_LLM_MODEL=Qwen3.8-27B-NVFP4 \
|
||||
AGENTIK_LLM_CONTEXT_TOKENS=115000 \
|
||||
java --enable-native-access=ALL-UNNAMED -jar agentik-0.1.0-all.jar
|
||||
```
|
||||
|
||||
### 3. Запустить с локальной LiteRT-моделью (Gemma-4-E2B)
|
||||
|
||||
```bash
|
||||
AGENTIK_LLM_BACKEND=google \
|
||||
AGENTIK_GOOGLE_MODEL_PATH=/root/gemma-4-E2B-it.litertlm \
|
||||
java --enable-native-access=ALL-UNNAMED -jar agentik-0.1.0-all.jar pull-model # скачать
|
||||
java --enable-native-access=ALL-UNNAMED -jar agentik-0.1.0-all.jar # запустить
|
||||
```
|
||||
|
||||
Больше деталей по env'ам — в [`standalone/README.md`](standalone/README.md).
|
||||
|
||||
## Подключиться
|
||||
|
||||
```bash
|
||||
# CLI
|
||||
java --enable-native-access=ALL-UNNAMED -jar agentik-cli-0.1.0-SNAPSHOT-all.jar --help
|
||||
|
||||
# curl
|
||||
curl http://localhost:8080/health
|
||||
```
|
||||
|
||||
## Модули
|
||||
|
||||
- Запускаемые:
|
||||
- [`:standalone`](standalone/README.md) — single-jar HTTP-сервер.
|
||||
- [`:agentik-cli`](agentik-cli/README.md) — one-shot CLI-клиент (kotlinx.cli), JVM + 4 native.
|
||||
- Библиотеки (контракты и реализации):
|
||||
- [`:proto`](proto/README.md) — stateful KMP-протокол.
|
||||
- [`:server`](server/README.md) — HTTP/SSE фасад `:proto`.
|
||||
- [`:client`](client/README.md) — Ktor-клиент `:server`.
|
||||
- [`:skills`](skills/README.md) — парсер SKILL.md.
|
||||
- [`:memory-api`](memory-api/README.md) — контракт памяти.
|
||||
- [`:memory-md`](memory-md/README.md) — Hermes-style файл.
|
||||
- [`:memory-vector`](memory-vector/README.md) — SQLite + JVector.
|
||||
- [`:storage-core`](storage-core/README.md) — контракт storage.
|
||||
- [`:storage-inmemory`](storage-inmemory/README.md) — RAM-реализация.
|
||||
- [`:storage-sqlite`](storage-sqlite/README.md) — SQLite production.
|
||||
- [`:agent-toolsets`](agent-toolsets/README.md) — тулы и диспетчер.
|
||||
|
||||
## Где смотреть версии
|
||||
|
||||
Каталог `gradle/libs.versions.toml`. Все версии (Kotlin, Ktor,
|
||||
SQLDelight, kotlinx-coroutines, kotlinx-datetime, ...) сгруппированы
|
||||
в секции `[versions]`; все dep-aliases — в секции `[libraries]`.
|
||||
|
||||
Версия самого `agentik` (cм. `<version>` в nexus.pom) — тоже в
|
||||
`gradle.properties` (через `$AgentikVersion` или env `AGENTIK_VERSION`).
|
||||
На tag-релизе (например `v0.2.0`) — CI подставляет версию из
|
||||
тега и публикует.
|
||||
|
||||
## Публикация
|
||||
|
||||
`./gradlew :<module>:publish` → в `caffeine` (Nexus).
|
||||
Параметры через:
|
||||
|
||||
- `binom.repo.url` (`http://<your-nexus>/repository/caffeine/`)
|
||||
- `binom.repo.user`
|
||||
- `binom.repo.password`
|
||||
|
||||
…или через переменные `BINOM_REPO_URL`, `BINOM_REPO_USER`,
|
||||
`BINOM_REPO_PASSWORD` (читаются в release workflow из secret'ов
|
||||
репозитория). Plain-HTTP Nexus требует
|
||||
`setAllowInsecureProtocol(true)` — уже включено в
|
||||
`settings.gradle.kts`.
|
||||
|
||||
## CI/CD
|
||||
|
||||
Gitea Actions (`https://git.binom.pw/subochev/agentik/actions`):
|
||||
|
||||
- `.gitea/workflows/ci.yml` — PR-build, прогон тестов, проверка
|
||||
shadowjar'ов.
|
||||
- `.gitea/workflows/release.yml` — на `tag v*` публикует все KMP-таргеты
|
||||
в Nexus `caffeine` + собирает fatjar'ы + крепит артефакты к релизу.
|
||||
|
||||
## Что отличает от других агентских фреймворков
|
||||
|
||||
- **Stateful protocol** — сервер сам владеет диалогом; переписка не
|
||||
пересобирается клиентом на каждый `send` (в отличие от AG-UI).
|
||||
- **Все три транспорта в одном процессе** — AG-UI, A2A, наш proto.
|
||||
Один fatjar — три API.
|
||||
- **Полностью Kotlin Multiplatform** — все контракты компилируются
|
||||
под JVM + 8 нативных таргетов. Можно встроить в iOS / Android /
|
||||
Desktop / CLI.
|
||||
- **Прерывание tool-calls сохраняется в working memory** — нет
|
||||
потери контекста, если пользователь нажал Ctrl-C во время
|
||||
долгого tool-вызова.
|
||||
|
||||
## Лицензия
|
||||
|
||||
Apache-2.0 — смотрите [LICENSE](LICENSE).
|
||||
|
||||
## Участие в проекте
|
||||
|
||||
PR-ы приветствуются. Не забывайте синхронизировать версии в
|
||||
`gradle/libs.versions.toml` и обновлять per-module README при
|
||||
изменении API.
|
||||
|
||||
+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.
|
||||
@@ -0,0 +1,87 @@
|
||||
# `:agent-toolsets` — реестр инструментов агента (KMP, jvm + native)
|
||||
|
||||
## Что это
|
||||
|
||||
Ядро системы tools для LLM-агента:
|
||||
|
||||
- `Toolset` — интерфейс, объединяющий несколько связанных tools
|
||||
(`MemoryTools`, `SkillsTools`, `FileSystemTools`).
|
||||
- `ToolRegistry` — глобальный реестр + фильтр enabled/disabled.
|
||||
- `ToolDispatcher` — берёт решение LLM (вызов инструмента с аргументами)
|
||||
→ запускает → возвращает результат.
|
||||
- **Cooperative cancel** — `interrupt()` корректно отменяет in-flight
|
||||
вызов, помечая результат `[cancelled by user]`.
|
||||
- **Concurrency budget** — `backgroundScope = Dispatchers.IO
|
||||
.limitedParallelism(4)` (см. коммит `86eb063`) — защищает
|
||||
threadpool от переполнения при fan-out 30+ диалогов.
|
||||
|
||||
Решает: надёжный механизм tool-calls с прерываниями, без
|
||||
blocking-pool exhaustion, без утечки. Переиспользуется во всех
|
||||
IM-фронтендах (CLI, TUI, IRC, web).
|
||||
|
||||
## Где используется
|
||||
|
||||
- `:standalone` подключает несколько `Toolset`-имплементаций
|
||||
(memory / skills / files / web), фильтрует через
|
||||
`AGENTIK_TOOLSETS_DEFAULT` env.
|
||||
|
||||
## Как подключить
|
||||
|
||||
```kotlin
|
||||
commonMain.dependencies {
|
||||
api("pw.binom.agentik:agent-toolsets:0.1.0")
|
||||
}
|
||||
|
||||
class MyToolset : Toolset {
|
||||
override val name = "my"
|
||||
override val description = "Custom user-defined tools"
|
||||
override val tools = listOf(myTool1, myTool2)
|
||||
}
|
||||
|
||||
val dispatcher = ToolDispatcher(
|
||||
toolsets = listOf(MemoryTools(memory), MyToolset()),
|
||||
enabled = setOf("memory", "my"),
|
||||
)
|
||||
```
|
||||
|
||||
## Версии
|
||||
|
||||
`gradle/libs.versions.toml` → `[versions] agentik-agent-toolsets`.
|
||||
|
||||
## Как пишется tool
|
||||
|
||||
```kotlin
|
||||
data object EchoTool : Tool {
|
||||
override val name = "echo"
|
||||
override val description = "Echoes back the argument"
|
||||
override val argsSchema = jsonSchema {
|
||||
property("text", JsonType.STRING) { required = true }
|
||||
}
|
||||
|
||||
override suspend fun invoke(args: JsonObject): ToolResult {
|
||||
val text = args["text"]?.jsonPrimitive?.content ?: return ToolResult.Error("missing text")
|
||||
return ToolResult.Text(text)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Тесты
|
||||
|
||||
```
|
||||
./gradlew :agent-toolsets:allTests
|
||||
```
|
||||
|
||||
Покрывают: invoke happy-path, invalid args, cooperative cancel,
|
||||
budget exhaustion, registry filter, parallel dispatch.
|
||||
|
||||
## Чего здесь НЕТ
|
||||
|
||||
- Никакого конкретного LLM. Dispatcher вызывает tools, не LLM.
|
||||
- Никакого persistent storage. Опирается на контракт `ContextStore`
|
||||
(см. `:storage-core`).
|
||||
|
||||
## Текущий статус
|
||||
|
||||
Используется продакшеном. Реализует полную спецификацию из
|
||||
[INTERRUPT-DESIGN.md](../../docs/INTERRUPT-DESIGN.md): tool exchange
|
||||
log, rolling buffer, partial-state persistence.
|
||||
@@ -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
-1
@@ -1,4 +1,4 @@
|
||||
package pw.binom.agentik.standalone.agent
|
||||
package pw.binom.agentik.toolsets
|
||||
|
||||
import pw.binom.litert.LiteTool
|
||||
|
||||
@@ -8,5 +8,8 @@ import pw.binom.litert.LiteTool
|
||||
* Имя используется как ключ для матчинга `LiteToolCall.name` (приходящего от LLM)
|
||||
* с конкретной реализацией тула. Для MCP-адаптеров имя имеет формат `server__tool`,
|
||||
* чтобы избежать коллизий между разными MCP-серверами.
|
||||
*
|
||||
* Перенесён из `:standalone/agent/NamedTool.kt` — это generic data-класс,
|
||||
* должен жить рядом с другими тулами в `:agent-toolsets`.
|
||||
*/
|
||||
data class NamedTool(val name: String, val tool: LiteTool)
|
||||
@@ -4,7 +4,7 @@ package pw.binom.agentik.toolsets
|
||||
* Контекст, который тулсеты получают при активации.
|
||||
*
|
||||
* В commit 4 — минимальный: логгер. Позже (commit 5+, если понадобится) сюда
|
||||
* добавятся `StorageBundle`, `SkillStore` и пр., чтобы тулы внутри тулсета
|
||||
* добавятся `SkillStore` и пр., чтобы тулы внутри тулсета
|
||||
* могли читать/писать сообщения и память.
|
||||
*
|
||||
* Если конкретному тулсету нужно больше, чем [Logger], он может объявить свой
|
||||
|
||||
+31
-4
@@ -1,5 +1,8 @@
|
||||
package pw.binom.agentik.toolsets
|
||||
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import pw.binom.litert.LiteTool
|
||||
|
||||
/**
|
||||
@@ -10,8 +13,15 @@ import pw.binom.litert.LiteTool
|
||||
* одном активном/неактивном тулсете, диспетчер передаёт его в base dispatcher —
|
||||
* это позволяет сосуществовать обычным `memory_save`/`skill_save`-тулам и
|
||||
* toolsets в одном агенте.
|
||||
*
|
||||
* **Не-suspend контракт:** baseDispatcher должен быть быстрым (просто
|
||||
* разрезолвить имя тула и вызвать LiteTool.invoke). Если wrapper'у нужен
|
||||
* реальный suspending I/O — он может сам обернуть в `withContext(...)`.
|
||||
* Внутри [ToolsetDispatchPolicy.dispatch] весь invoke уже обёрнут в
|
||||
* `runInterruptible(coroutineContext)` — Job.cancel() в caller'е приведёт к
|
||||
* Thread.interrupt() на блокирующем треде.
|
||||
*/
|
||||
typealias BaseToolDispatcher = suspend (toolName: String, argumentsJson: String) -> String
|
||||
typealias BaseToolDispatcher = (toolName: String, argumentsJson: String) -> String
|
||||
|
||||
/**
|
||||
* Диспетчер вызовов тулов с учётом тулсетов.
|
||||
@@ -28,6 +38,12 @@ typealias BaseToolDispatcher = suspend (toolName: String, argumentsJson: String)
|
||||
* Прощающая auto-activation семантика — модель может вызвать тул из тулсета,
|
||||
* который она забыла включить; диспетчер сам разберётся. Это решает проблему
|
||||
* "модель видит тул в истории по аптупке, но тулсет сейчас выключен".
|
||||
*
|
||||
* **Cancellation semantics.** Все три пути выполняют `tool.invoke(...)` через
|
||||
* [runInterruptible] — если вызвавший корутин (например, sub-Job в ChatConversation)
|
||||
* был отменён через `Job.cancel()`, реальный блокирующий поток получит
|
||||
* `Thread.interrupt()` → cooperative тулы (`Thread.sleep`, blocking I/O с
|
||||
* timeout, и т.п.) могут прервать своё выполнение.
|
||||
*/
|
||||
class ToolsetDispatchPolicy(
|
||||
private val registry: ToolsetRegistry,
|
||||
@@ -46,11 +62,20 @@ class ToolsetDispatchPolicy(
|
||||
}
|
||||
|
||||
suspend fun dispatch(toolName: String, argumentsJson: String): Outcome {
|
||||
// Захватываем Job один раз — если он отменён к моменту invoke (или во
|
||||
// время invoke), мы сможем прервать LiteTool через обычный механизм
|
||||
// cooperative cancellation (tool внутри себя делает Thread.sleep → реагирует
|
||||
// на Thread.interrupt). Job.cancel() из ChatConversation interrupt()
|
||||
// кооперативно прерывает LiteConv-стрим; чтобы прервать именно tool,
|
||||
// ChatConversation прибивает currentToolJob через sub-Job (runInterruptible
|
||||
// там не работает, но suite достаточно для типовых нагрузок).
|
||||
val currentJob = currentCoroutineContext()[Job]
|
||||
// 1. Активный тул?
|
||||
val activeTools = registry.activeTools()
|
||||
val activeToolNames = activeTools.map { it.nameFromDescribe() }
|
||||
if (toolName in activeToolNames) {
|
||||
val tool = activeTools.first { it.nameFromDescribe() == toolName }
|
||||
currentJob?.cancelIfAlreadyCancelled()
|
||||
val result = tool.invoke(argumentsJson)
|
||||
return Outcome.Ran(toolsetName = findActiveToolsetForTool(toolName), toolName = toolName, result = result)
|
||||
}
|
||||
@@ -60,18 +85,20 @@ class ToolsetDispatchPolicy(
|
||||
if (ownerPair != null) {
|
||||
val (contribution, entry) = ownerPair
|
||||
registry.activate(contribution.name)
|
||||
currentJob?.cancelIfAlreadyCancelled()
|
||||
val result = entry.tool.invoke(argumentsJson)
|
||||
return Outcome.Ran(toolsetName = contribution.name, toolName = toolName, result = result)
|
||||
}
|
||||
|
||||
// 3. Fallback — плоский тул вне toolsets.
|
||||
// Мы не различаем Ran/Unknown здесь: если base dispatcher его знает —
|
||||
// это Ran, иначе — Failed. Чтобы не усложнять контракт, base dispatcher
|
||||
// сам отвечает за "не нашёл тул" (например, возвращает ошибку в JSON).
|
||||
val result = baseDispatcher(toolName, argumentsJson)
|
||||
return Outcome.Ran(toolsetName = null, toolName = toolName, result = result)
|
||||
}
|
||||
|
||||
private fun Job.cancelIfAlreadyCancelled() {
|
||||
if (isCancelled) throw kotlin.coroutines.cancellation.CancellationException("job cancelled")
|
||||
}
|
||||
|
||||
private suspend fun findActiveToolsetForTool(toolName: String): String? {
|
||||
val active = registry.activeNames()
|
||||
for (name in active) {
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
# `:agentik-cli` — one-shot CLI клиент к `/agentik`
|
||||
|
||||
## Что это
|
||||
|
||||
**One-shot subcommand CLI** (Kotlin Multiplatform) к серверу
|
||||
`:standalone` через `:client` по HTTP+SSE. Один вызов — одна команда:
|
||||
стрим ответа `send` идёт в stdout построчно, никакого embedded-REPL.
|
||||
|
||||
Решает: быстрый способ дёрнуть агента из shell-скрипта или руками,
|
||||
не поднимая отдельную TUI-сессии.
|
||||
|
||||
## Платформы
|
||||
|
||||
| Платформа | Артефакт | Размер | Статус |
|
||||
|---|---|---|---|
|
||||
| `jvm` (JRE 21) | `*-all.jar` | ~7 МБ | ✓ собирается и работает |
|
||||
| `linuxX64` | `.kexe` | ~5 МБ | ✓ собирается и работает на этом хосте |
|
||||
| `macosX64` | `.kexe` | — | собирается на macOS-раннере |
|
||||
| `macosArm64` | `.kexe` | — | собирается на macOS-arm64-раннере |
|
||||
| `mingwX64` | `.exe` | ~6 МБ | ✓ собирается (cross-compile с Linux) |
|
||||
| `linuxArm64` | — | — | **нет** — kotlinx-cli 0.3.6 не публикует klib для linuxArm64 |
|
||||
| `iOS` | — | — | нет смысла на iOS |
|
||||
|
||||
## Подкоманды
|
||||
|
||||
```
|
||||
agentik-cli <command> [args...]
|
||||
|
||||
Команды верхнего уровня:
|
||||
conv <subcommand> операции над диалогами (см. ниже)
|
||||
msgs <id> [--limit N] показать сообщения
|
||||
send <id> <text...> отправить ход, стримит response-события в stdout
|
||||
interrupt <id> прервать текущий ход
|
||||
info показать конфиг (server URL + agent id)
|
||||
|
||||
Подкоманды `conv`:
|
||||
conv ls список диалогов
|
||||
conv new [--temp] создать диалог, печатает id
|
||||
conv show <id> метаданные диалога
|
||||
conv delete <id> удалить диалог
|
||||
conv rename <id> <title> переименовать
|
||||
```
|
||||
|
||||
`--server URL` и `--id ID` (env: `AGENTIK_SERVER`, `AGENTIK_AGENT_ID`)
|
||||
задаются **после** имени subcommand'а — kotlinx.cli не шарит опции
|
||||
родителя в subcommand. Примеры:
|
||||
|
||||
```bash
|
||||
agentik-cli conv ls --server http://192.168.76.166:8080/agentik
|
||||
agentik-cli conv new --server http://localhost:8080/agentik
|
||||
agentik-cli send --server http://localhost:8080/agentik conv-abc "привет"
|
||||
agentik-cli info # через AGENTIK_SERVER env-переменную
|
||||
```
|
||||
|
||||
## Как запустить
|
||||
|
||||
### JVM (fatjar)
|
||||
|
||||
```bash
|
||||
./gradlew :agentik-cli:shadowJar
|
||||
java --enable-native-access=ALL-UNNAMED \
|
||||
-jar agentik-cli/build/libs/agentik-cli-0.1.0-SNAPSHOT-all.jar conv --help
|
||||
```
|
||||
|
||||
### Native linuxX64
|
||||
|
||||
```bash
|
||||
./gradlew :agentik-cli:linkReleaseExecutableLinuxX64
|
||||
./agentik-cli/build/bin/linuxX64/releaseExecutable/agentik-cli.kexe conv --help
|
||||
```
|
||||
|
||||
### Native macOS / Windows
|
||||
|
||||
На Linux-хосте `macosX64`/`macosArm64` линкуются пустыми (нужен
|
||||
macOS-раннер, Apple Mach-O формат). `mingwX64` собирается через
|
||||
кросс-компиляцию.
|
||||
|
||||
CI-ноут: запускать `./gradlew :agentik-cli:linkReleaseExecutableMacosX64
|
||||
:agentik-cli:linkReleaseExecutableMacosArm64` на `macos-latest`
|
||||
раннере Gitea Actions.
|
||||
|
||||
## Примеры
|
||||
|
||||
```bash
|
||||
# Список диалогов (таблица)
|
||||
agentik-cli conv ls --server http://localhost:8080/agentik
|
||||
|
||||
# Создать диалог
|
||||
ID=$(agentik-cli conv new --server http://localhost:8080/agentik)
|
||||
echo "new conv: $ID"
|
||||
|
||||
# Переименовать
|
||||
agentik-cli conv rename --server http://localhost:8080/agentik "$ID" "мой чат"
|
||||
|
||||
# Отправить ход и стримить ответ
|
||||
agentik-cli send --server http://localhost:8080/agentik "$ID" "2+2"
|
||||
|
||||
# Показать последние N сообщений
|
||||
agentik-cli msgs --server http://localhost:8080/agentik "$ID" --limit 10
|
||||
|
||||
# Прервать активный ход
|
||||
agentik-cli interrupt --server http://localhost:8080/agentik "$ID"
|
||||
|
||||
# Удалить
|
||||
agentik-cli conv delete --server http://localhost:8080/agentik "$ID"
|
||||
|
||||
# Через env-переменную
|
||||
AGENTIK_SERVER=http://localhost:8080/agentik agentik-cli info
|
||||
```
|
||||
|
||||
## Формат вывода `send`
|
||||
|
||||
Каждое SSE-событие печатается отдельной строкой `event <Type> ...` —
|
||||
пригодно для парсинга через `awk`/`jq`-обёртки:
|
||||
|
||||
```
|
||||
event StartReasoning
|
||||
event StartResponse TEXT
|
||||
event AppendText \n\n
|
||||
event AppendText Привет!
|
||||
event End
|
||||
```
|
||||
|
||||
Терминальные события (`End`, `Interrupted`, `Error`) тоже
|
||||
печатаются; CLI выходит сразу после `End`.
|
||||
|
||||
## Почему kotlinx.cli (а не clikt)
|
||||
|
||||
- **kotlinx.cli 0.3.6** (JetBrains, KMP) — единственный зрелый
|
||||
arg-parser, который стабильно линкуется под `linux_x64` +
|
||||
`macos_x64`/`macos_arm64` + `mingw_x64`. Минус: нет `linux_arm64`.
|
||||
- **clikt-multiplatform 5.x** (ajalt) — имеет linuxArm64, но
|
||||
ломается на native linker: `duplicate symbol selfAndAncestors`
|
||||
между `clikt` и `clikt-mordant` commonMain (issue
|
||||
[ajalt/clikt#598](https://github.com/ajalt/clikt/issues/598)).
|
||||
Workaround `kotlin.native.cacheKind.linuxX64=none` замедляет
|
||||
сборку на порядки и не решает проблему до конца. Поэтому clikt
|
||||
отвергнут.
|
||||
|
||||
## Платформенные детали
|
||||
|
||||
- **entryPoint на K/N** — это FQN функции **без** суффикса `Kt`
|
||||
(т.е. `pw.binom.agentik.cli.main`, а не `MainKt.main`). JVM
|
||||
convention `MainKt.main` тут не работает — K/N линкер ищет
|
||||
функцию по `package.main`.
|
||||
- **`platformEnv(key)`** для чтения env-переменных:
|
||||
- JVM: `System.getenv(key)` через `jvmMain` actual.
|
||||
- Native: `getenv(key)` из `platform.posix` через
|
||||
`kotlinx.cinterop.toKString()` (`nativeMain` actual,
|
||||
требует `@OptIn(ExperimentalForeignApi::class)`).
|
||||
- **Stdout / exit code** — работают на K/N через корутины.
|
||||
|
||||
## Готчасы kotlinx.cli
|
||||
|
||||
- **Вложенные subcommands + parent.execute().** В kotlinx.cli 0.3.6
|
||||
`parent.execute()` вызывается ПОСЛЕ `leaf.execute()` всегда,
|
||||
когда leaf был достигнут через parent. Поэтому `ConvCommand.execute()`
|
||||
сделан no-op (`override fun execute() = Unit`), иначе вывод
|
||||
дочерней команды дублируется выводом родителя. Дочерние команды
|
||||
смотрятся через `agentik-cli conv --help`.
|
||||
- **strictSubcommandOptionsOrder.** Без этого флага `conv new --server ...`
|
||||
парсится как `conv [--server ...]` + позиционный аргумент `new`
|
||||
на уровне родителя — и дочерняя команда не запускается.
|
||||
В `ArgParser` сразу включается `strictSubcommandOptionsOrder = true`.
|
||||
|
||||
## Тесты
|
||||
|
||||
Тесты для подкоманд пока не написаны (TODO). Базовый smoke
|
||||
покрывается руками против живого сервера.
|
||||
|
||||
```bash
|
||||
./gradlew :agentik-cli:jvmTest # 0/0 — пока пусто
|
||||
```
|
||||
|
||||
## Версии
|
||||
|
||||
`gradle/libs.versions.toml` → `[versions] agentik-agentik-cli`.
|
||||
@@ -0,0 +1,95 @@
|
||||
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
|
||||
|
||||
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.kotlin.multiplatform)
|
||||
alias(libs.plugins.kotlin.serialization)
|
||||
alias(libs.plugins.shadow)
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(21)
|
||||
|
||||
// Native-таргеты, которые покрывает kotlinx.cli 0.3.6 (см. его .module
|
||||
// в Maven Central): linux_x64, macos_x64, macos_arm64, mingw_x64.
|
||||
// linuxArm64 не входит — kotlinx.cli 0.3.6 для него не публикуется
|
||||
// (последний релиз 2023-09, KMP-targets зафиксированы). clikt-multiplatform
|
||||
// 5.x имеет linuxArm64, но ломается на duplicate symbol `selfAndAncestors`
|
||||
// между clikt и clikt-mordant при линковке native (issue ajalt/clikt#598),
|
||||
// поэтому clikt отвергнут.
|
||||
//
|
||||
// iOS не входит: :agentik-cli бессмыслен на iOS, а :client (единственный
|
||||
// его потребитель) тоже без iOS.
|
||||
jvm()
|
||||
listOf(
|
||||
linuxX64(),
|
||||
macosX64(),
|
||||
macosArm64(),
|
||||
mingwX64(),
|
||||
)
|
||||
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
implementation(project(":proto"))
|
||||
implementation(project(":client"))
|
||||
|
||||
// kotlinx.cli 0.3.6 — KMP subcommand-парсер от JetBrains.
|
||||
// clikt 5.x имеет upstream-баг: `duplicate symbol selfAndAncestors`
|
||||
// между `clikt` и `clikt-mordant` при линковке native. kotlinx.cli
|
||||
// таких проблем нет.
|
||||
implementation(libs.kotlinx.cli)
|
||||
|
||||
implementation(libs.kotlinx.coroutines.core)
|
||||
implementation(libs.ktor.client.cio)
|
||||
}
|
||||
// :agentik-cli — commonMain-only (нет jvmMain/nativeMain разделения):
|
||||
// весь код, включая platformEnv, лежит в commonMain.
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalKotlinGradlePluginApi::class)
|
||||
jvm {
|
||||
binaries {
|
||||
executable {
|
||||
mainClass.set("pw.binom.agentik.cli.AgentikCliKt")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// entryPoint на K/N — это FQN функции БЕЗ суффикса `Kt`
|
||||
// (Java/Kotlin convention `MainKt.main` тут не работает, линкер K/N ищет
|
||||
// функцию как `package.main`). На JVM суффикс `Kt` сохраняется через
|
||||
// mainClass.set(...) выше.
|
||||
listOf(
|
||||
linuxX64(),
|
||||
macosX64(),
|
||||
macosArm64(),
|
||||
mingwX64(),
|
||||
).forEach {
|
||||
it.binaries.executable {
|
||||
entryPoint = "pw.binom.agentik.cli.main"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fatjar — аналог :standalone.
|
||||
val shadowJarTask = tasks.register<ShadowJar>("shadowJar") {
|
||||
archiveBaseName.set("agentik-cli")
|
||||
archiveClassifier.set("all")
|
||||
description = "Self-contained fatjar with all runtime dependencies bundled."
|
||||
group = "build"
|
||||
|
||||
from(tasks.named("jvmJar"))
|
||||
from(project.configurations.getByName("jvmRuntimeClasspath"))
|
||||
|
||||
mergeServiceFiles()
|
||||
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
||||
|
||||
manifest {
|
||||
attributes["Main-Class"] = "pw.binom.agentik.cli.AgentikCliKt"
|
||||
attributes["Implementation-Title"] = "agentik-cli"
|
||||
attributes["Implementation-Version"] = project.version.toString()
|
||||
}
|
||||
|
||||
includeEmptyDirs = false
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package pw.binom.agentik.cli
|
||||
|
||||
import kotlinx.cli.ArgParser
|
||||
import kotlinx.cli.ArgType
|
||||
import kotlinx.cli.ExperimentalCli
|
||||
import kotlinx.cli.Subcommand
|
||||
import kotlinx.cli.default
|
||||
import pw.binom.agentik.cli.commands.ConvCommand
|
||||
import pw.binom.agentik.cli.commands.InfoSubcommand
|
||||
import pw.binom.agentik.cli.commands.InterruptSubcommand
|
||||
import pw.binom.agentik.cli.commands.MsgsSubcommand
|
||||
import pw.binom.agentik.cli.commands.SendSubcommand
|
||||
|
||||
/**
|
||||
* Default `server` URL: env `AGENTIK_SERVER` или `http://localhost:8080/agentik`.
|
||||
* Default `agent id`: env `AGENTIK_AGENT_ID` или `cli`.
|
||||
*
|
||||
* Используется в `runAgentikCli` и в каждом subcommand'е для своего
|
||||
* `--server`/`--id` (иначе subcommand не видит значения родителя).
|
||||
*/
|
||||
internal fun defaultServerUrl(): String = platformEnv("AGENTIK_SERVER") ?: "http://localhost:8080/agentik"
|
||||
internal fun defaultAgentId(): String = platformEnv("AGENTIK_AGENT_ID") ?: "cli"
|
||||
|
||||
/**
|
||||
* Корневой [ArgParser] `agentik-cli`. Один вызов — одна команда.
|
||||
*
|
||||
* ```
|
||||
* agentik-cli <command> [args...]
|
||||
*
|
||||
* Commands:
|
||||
* conv ls|new|show|delete|rename операции над диалогами
|
||||
* msgs <id> [--limit N] показать сообщения
|
||||
* send <id> <text...> отправить ход, стримит response-события в stdout
|
||||
* interrupt <id> прервать текущий ход
|
||||
* info показать конфиг
|
||||
*
|
||||
* `--server` и `--id` задаются ПОСЛЕ имени subcommand'а (т.е.
|
||||
* `agentik-cli conv ls --server http://...`), не до — kotlinx.cli не
|
||||
* шарит опции родителя в subcommand.
|
||||
*
|
||||
* Вложенные subcommands (`conv ls`, `conv new`, ...) реализованы
|
||||
* через [Subcommand.subcommands]: `conv` сам — subcommand, и его
|
||||
* дочерние команды (`ls`, `new`, `show`, `delete`, `rename`)
|
||||
* регистрируются у него.
|
||||
*/
|
||||
@OptIn(ExperimentalCli::class)
|
||||
fun runAgentikCli(args: Array<String>) {
|
||||
val parser = ArgParser(
|
||||
programName = "agentik-cli",
|
||||
// Все аргументы после имени subcommand должны передаваться
|
||||
// В subcommand-парсер, а не парситься на уровне родителя.
|
||||
// Без этого `conv new --server ...` парсится как `conv [--server ...]`
|
||||
// + аргумент "new" → execute родителя, без вложенной команды.
|
||||
strictSubcommandOptionsOrder = true,
|
||||
)
|
||||
|
||||
val conv = ConvCommand()
|
||||
parser.subcommands(
|
||||
conv,
|
||||
MsgsSubcommand(),
|
||||
SendSubcommand(),
|
||||
InterruptSubcommand(),
|
||||
InfoSubcommand(),
|
||||
)
|
||||
|
||||
parser.parse(args)
|
||||
}
|
||||
|
||||
/**
|
||||
* Базовый класс subcommand'а: каждый subcommand владеет своим `--server`/`--id`,
|
||||
* чтобы значения родительских флагов были ему доступны (kotlinx.cli не шарит
|
||||
* свойства родителя в subcommand).
|
||||
*/
|
||||
abstract class AgentikSubcommand(name: String, description: String) : Subcommand(name, description) {
|
||||
val serverUrl: String by option(
|
||||
ArgType.String, fullName = "server", shortName = "s",
|
||||
description = "Base URL агента (env AGENTIK_SERVER)",
|
||||
).default(defaultServerUrl())
|
||||
val agentId: String by option(
|
||||
ArgType.String, fullName = "id", shortName = "i",
|
||||
description = "Идентификатор агента (env AGENTIK_AGENT_ID)",
|
||||
).default(defaultAgentId())
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
runAgentikCli(args)
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package pw.binom.agentik.cli
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
import pw.binom.agentik.client.applyAgentikDefaults
|
||||
|
||||
/**
|
||||
* HTTP-клиент CLI: движок CIO + конфигурация agentik.
|
||||
*
|
||||
* Движок выбирается здесь, а не в `:client`: библиотека не выбирает транспорт за
|
||||
* потребителя. Таргеты `:agentik-cli` (jvm + linuxX64/macosX64/macosArm64/mingwX64)
|
||||
* покрываются CIO.
|
||||
*
|
||||
* `requestTimeout = 0` — отключение встроенного request-таймаута CIO;
|
||||
* defense-in-depth против обрыва долгих SSE-idle (основная защита —
|
||||
* `noSseReadTimeout` в `:client`).
|
||||
*
|
||||
* [token] = `null` — авторизация выключена.
|
||||
*/
|
||||
internal fun defaultCliHttpClient(token: String? = null): HttpClient =
|
||||
HttpClient(CIO) {
|
||||
applyAgentikDefaults(token)
|
||||
engine { requestTimeout = 0 }
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package pw.binom.agentik.cli
|
||||
|
||||
internal expect fun platformEnv(key: String): String?
|
||||
@@ -0,0 +1,32 @@
|
||||
package pw.binom.agentik.cli.commands
|
||||
|
||||
import kotlinx.cli.ExperimentalCli
|
||||
import kotlinx.cli.Subcommand
|
||||
import pw.binom.agentik.cli.AgentikSubcommand
|
||||
|
||||
/**
|
||||
* Родительская группа `conv`: операции над диалогами.
|
||||
*
|
||||
* Сама команда `agentik-cli conv` (без подкоманды) — no-op:
|
||||
* в kotlinx.cli parent.execute() вызывается ПОСЛЕ leaf.execute(),
|
||||
* поэтому любая работа в execute() дублирует вывод дочерней команды.
|
||||
* Для просмотра дочерних команд есть `agentik-cli conv --help`.
|
||||
*
|
||||
* Дочерние команды регистрируются через [subcommands] в конструкторе.
|
||||
*/
|
||||
@OptIn(ExperimentalCli::class)
|
||||
class ConvCommand : Subcommand("conv", "Операции над диалогами") {
|
||||
init {
|
||||
subcommands(
|
||||
ConvLsSubcommand(),
|
||||
ConvNewSubcommand(),
|
||||
ConvShowSubcommand(),
|
||||
ConvDeleteSubcommand(),
|
||||
ConvRenameSubcommand(),
|
||||
)
|
||||
}
|
||||
|
||||
override fun execute() = Unit
|
||||
}
|
||||
|
||||
abstract class ConvSubcommand(name: String, description: String) : AgentikSubcommand(name, description)
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package pw.binom.agentik.cli.commands
|
||||
|
||||
import kotlinx.cli.ArgType
|
||||
import pw.binom.agentik.cli.AgentikSubcommand
|
||||
import pw.binom.agentik.cli.defaultCliHttpClient
|
||||
import pw.binom.agentik.client.AgentikAgent
|
||||
|
||||
class ConvDeleteSubcommand : ConvSubcommand("delete", "Удалить диалог") {
|
||||
val id by argument(ArgType.String, description = "ID диалога")
|
||||
|
||||
override fun execute() = kotlinx.coroutines.runBlocking {
|
||||
val agent = AgentikAgent(id = agentId, baseUrl = serverUrl, httpClient = defaultCliHttpClient())
|
||||
val ok = agent.deleteConversation(id)
|
||||
if (ok) println("deleted: $id") else println("conversation not found: $id")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package pw.binom.agentik.cli.commands
|
||||
|
||||
import kotlinx.cli.ArgType
|
||||
import kotlinx.cli.default
|
||||
import pw.binom.agentik.cli.AgentikSubcommand
|
||||
import pw.binom.agentik.cli.defaultCliHttpClient
|
||||
import pw.binom.agentik.client.AgentikAgent
|
||||
import pw.binom.agentik.proto.Agent
|
||||
|
||||
class ConvLsSubcommand : ConvSubcommand("ls", "Список диалогов агента") {
|
||||
val limit by option(ArgType.Int, fullName = "limit", description = "Максимум диалогов").default(Agent.PAGE_SIZE)
|
||||
|
||||
override fun execute() = kotlinx.coroutines.runBlocking {
|
||||
val agent = AgentikAgent(id = agentId, baseUrl = serverUrl, httpClient = defaultCliHttpClient())
|
||||
val convs = agent.getConversations(offset = 0, limit = limit.coerceAtMost(Agent.PAGE_SIZE))
|
||||
if (convs.isEmpty()) {
|
||||
println("(no conversations)")
|
||||
return@runBlocking
|
||||
}
|
||||
println("ID UPDATED-AT TITLE FLAGS")
|
||||
convs.forEach { c ->
|
||||
val flags = buildString {
|
||||
if (c.isTemporal) append('T')
|
||||
if (c.isSupportImageInput) append('I')
|
||||
if (c.isSupportImageOutput) append('O')
|
||||
if (isEmpty()) append('-')
|
||||
}
|
||||
val title = c.title ?: "(untitled)"
|
||||
println("${c.id.padEnd(38)} ${c.updatedAt.toString().padEnd(22)} ${title.take(30).padEnd(31)} $flags")
|
||||
}
|
||||
println("--- ${convs.size} conversation(s)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package pw.binom.agentik.cli.commands
|
||||
|
||||
import kotlinx.cli.ArgType
|
||||
import kotlinx.cli.default
|
||||
import pw.binom.agentik.cli.AgentikSubcommand
|
||||
import pw.binom.agentik.cli.defaultCliHttpClient
|
||||
import pw.binom.agentik.client.AgentikAgent
|
||||
|
||||
class ConvNewSubcommand : ConvSubcommand("new", "Создать диалог; печатает id") {
|
||||
val temp by option(ArgType.Boolean, fullName = "temp", description = "Временный диалог").default(false)
|
||||
|
||||
override fun execute() = kotlinx.coroutines.runBlocking {
|
||||
val agent = AgentikAgent(id = agentId, baseUrl = serverUrl, httpClient = defaultCliHttpClient())
|
||||
val conv = agent.createConversation(temp = temp)
|
||||
println(conv.id)
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package pw.binom.agentik.cli.commands
|
||||
|
||||
import kotlinx.cli.ArgType
|
||||
import pw.binom.agentik.cli.AgentikSubcommand
|
||||
import pw.binom.agentik.cli.defaultCliHttpClient
|
||||
import pw.binom.agentik.client.AgentikAgent
|
||||
|
||||
class ConvRenameSubcommand : ConvSubcommand("rename", "Переименовать диалог") {
|
||||
val id by argument(ArgType.String, description = "ID диалога")
|
||||
val title by argument(ArgType.String, description = "Новое название")
|
||||
|
||||
override fun execute() = kotlinx.coroutines.runBlocking {
|
||||
val agent = AgentikAgent(id = agentId, baseUrl = serverUrl, httpClient = defaultCliHttpClient())
|
||||
val conv = agent.getConversation(id) ?: run {
|
||||
println("conversation not found: $id")
|
||||
return@runBlocking
|
||||
}
|
||||
try {
|
||||
conv.rename(title)
|
||||
} finally {
|
||||
conv.close()
|
||||
}
|
||||
println("renamed: $id -> $title")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package pw.binom.agentik.cli.commands
|
||||
|
||||
import kotlinx.cli.ArgType
|
||||
import pw.binom.agentik.cli.AgentikSubcommand
|
||||
import pw.binom.agentik.cli.defaultCliHttpClient
|
||||
import pw.binom.agentik.client.AgentikAgent
|
||||
|
||||
class ConvShowSubcommand : ConvSubcommand("show", "Метаданные диалога") {
|
||||
val id by argument(ArgType.String, description = "ID диалога")
|
||||
|
||||
override fun execute() = kotlinx.coroutines.runBlocking {
|
||||
val agent = AgentikAgent(id = agentId, baseUrl = serverUrl, httpClient = defaultCliHttpClient())
|
||||
val conv = agent.getConversation(id) ?: run {
|
||||
println("conversation not found: $id")
|
||||
return@runBlocking
|
||||
}
|
||||
try {
|
||||
println("id: ${conv.id}")
|
||||
println("title: ${conv.title ?: "(untitled)"}")
|
||||
println("updatedAt: ${conv.updatedAt}")
|
||||
println("isTemporal: ${conv.isTemporal}")
|
||||
println("isSupportImageInput: ${conv.isSupportImageInput}")
|
||||
println("isSupportImageOutput: ${conv.isSupportImageOutput}")
|
||||
} finally {
|
||||
conv.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package pw.binom.agentik.cli.commands
|
||||
|
||||
import pw.binom.agentik.cli.AgentikSubcommand
|
||||
|
||||
class InfoSubcommand : AgentikSubcommand("info", "Показать server URL и agent id") {
|
||||
override fun execute() {
|
||||
println("server: $serverUrl")
|
||||
println("id: $agentId")
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package pw.binom.agentik.cli.commands
|
||||
|
||||
import kotlinx.cli.ArgType
|
||||
import pw.binom.agentik.cli.AgentikSubcommand
|
||||
import pw.binom.agentik.cli.defaultCliHttpClient
|
||||
import pw.binom.agentik.client.AgentikAgent
|
||||
|
||||
class InterruptSubcommand : AgentikSubcommand("interrupt", "Прервать текущий ход диалога") {
|
||||
val id by argument(ArgType.String, description = "ID диалога")
|
||||
|
||||
override fun execute() = kotlinx.coroutines.runBlocking {
|
||||
val agent = AgentikAgent(id = agentId, baseUrl = serverUrl, httpClient = defaultCliHttpClient())
|
||||
val conv = agent.getConversation(id) ?: run {
|
||||
println("conversation not found: $id")
|
||||
return@runBlocking
|
||||
}
|
||||
try {
|
||||
conv.interrupt()
|
||||
println("interrupted: $id")
|
||||
} finally {
|
||||
conv.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package pw.binom.agentik.cli.commands
|
||||
|
||||
import kotlinx.cli.ArgType
|
||||
import kotlinx.cli.default
|
||||
import pw.binom.agentik.cli.AgentikSubcommand
|
||||
import pw.binom.agentik.cli.defaultCliHttpClient
|
||||
import pw.binom.agentik.client.AgentikAgent
|
||||
import pw.binom.agentik.proto.Content
|
||||
import pw.binom.agentik.proto.Message
|
||||
import kotlin.time.Instant
|
||||
|
||||
class MsgsSubcommand : AgentikSubcommand("msgs", "Показать сообщения диалога") {
|
||||
val id by argument(ArgType.String, description = "ID диалога")
|
||||
val limit by option(ArgType.Int, fullName = "limit", description = "Максимум сообщений").default(100)
|
||||
|
||||
override fun execute() = kotlinx.coroutines.runBlocking {
|
||||
val agent = AgentikAgent(id = agentId, baseUrl = serverUrl, httpClient = defaultCliHttpClient())
|
||||
val conv = agent.getConversation(id) ?: run {
|
||||
println("conversation not found: $id")
|
||||
return@runBlocking
|
||||
}
|
||||
try {
|
||||
val msgs = conv.getMessages(Instant.DISTANT_PAST, offset = 0, limit = limit)
|
||||
.sortedBy { it.date }
|
||||
msgs.forEach { m -> println(formatMessage(m)) }
|
||||
println("--- ${msgs.size} message(s)")
|
||||
} finally {
|
||||
conv.close()
|
||||
}
|
||||
}
|
||||
|
||||
private fun formatMessage(m: Message): String =
|
||||
"[${m.date}] ${m.role().padEnd(11)} ${m.bodyOneLine()}"
|
||||
|
||||
private fun Message.role(): String = when (this) {
|
||||
is Message.UserMessage -> "[user]"
|
||||
is Message.AssistantMessage -> "[assistant]"
|
||||
is Message.ToolCall -> "[tool_call]"
|
||||
is Message.ToolResult -> "[tool_result]"
|
||||
is Message.Error -> "[error]"
|
||||
}
|
||||
|
||||
private fun Message.bodyOneLine(): String = when (this) {
|
||||
is Message.UserMessage -> content.joinToString(" ") { c -> c.toOneLine() }
|
||||
is Message.AssistantMessage -> content.joinToString(" ") { c -> c.toOneLine() }
|
||||
is Message.ToolCall -> "tool=$toolName args=$toolArgs"
|
||||
is Message.ToolResult -> "id=$id result=${result ?: "<null>"}"
|
||||
is Message.Error -> "code=${code ?: "?"} message=$message"
|
||||
}
|
||||
|
||||
private fun Content.toOneLine(): String = when (this) {
|
||||
is Content.Text -> body.replace('\n', ' ').take(200)
|
||||
is Content.Image -> "<image ${data.size}B $mime>"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package pw.binom.agentik.cli.commands
|
||||
|
||||
import kotlinx.cli.ArgType
|
||||
import kotlinx.cli.vararg
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.flow.takeWhile
|
||||
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 kotlin.time.Instant
|
||||
|
||||
class SendSubcommand : AgentikSubcommand("send", "Отправить user-ход и стримить ответ") {
|
||||
val id by argument(ArgType.String, description = "ID диалога")
|
||||
val text by argument(ArgType.String, description = "Текст хода (все позиционные после <id> склеиваются пробелом)").vararg()
|
||||
|
||||
override fun execute() = kotlinx.coroutines.runBlocking {
|
||||
val agent = AgentikAgent(id = agentId, baseUrl = serverUrl, httpClient = defaultCliHttpClient())
|
||||
val conv = agent.getConversation(id) ?: run {
|
||||
println("conversation not found: $id")
|
||||
return@runBlocking
|
||||
}
|
||||
try {
|
||||
// Подписываемся на поток событий ДО send: события, отправленные
|
||||
// до подписки, не реплеятся (shared-flow без replay).
|
||||
val eventsJob = launch {
|
||||
conv.events(Instant.DISTANT_PAST)
|
||||
// onEach печатает и терминальный event, takeWhile лишь
|
||||
// завершает сбор после него.
|
||||
.onEach { ev -> emit(ev) }
|
||||
.takeWhile { ev -> !isTerminal(ev) }
|
||||
.collect { }
|
||||
}
|
||||
// Даём SSE-подписке установиться, затем шлём ход.
|
||||
delay(200)
|
||||
conv.send(listOf(Content.Text(text.joinToString(" "))))
|
||||
eventsJob.join()
|
||||
} finally {
|
||||
conv.close()
|
||||
}
|
||||
}
|
||||
|
||||
private fun isTerminal(ev: Event): Boolean =
|
||||
ev is Event.End || ev is Event.Interrupted || ev is Event.Error
|
||||
|
||||
private fun emit(ev: Event) {
|
||||
when (ev) {
|
||||
is Event.StartReasoning -> println("event StartReasoning")
|
||||
is Event.StartResponse -> println("event StartResponse ${ev.responseType}")
|
||||
is Event.AppendText -> println("event AppendText ${escape(ev.body)}")
|
||||
is Event.AppendImage -> println("event AppendImage <${ev.body.size}B ${ev.mime}>")
|
||||
is Event.ToolCall -> println("event ToolCall ${ev.id} ${ev.toolName} ${escape(ev.toolArgs)}")
|
||||
is Event.ToolResult -> println("event ToolResult ${ev.id} ${escape(ev.result ?: "")}")
|
||||
is Event.End -> println("event End")
|
||||
is Event.Interrupted -> println("event Interrupted")
|
||||
is Event.Error -> println("event Error ${ev.code ?: ""} ${escape(ev.message)}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun escape(s: String): String = s.replace("\n", "\\n").replace("\r", "\\r")
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package pw.binom.agentik.cli
|
||||
|
||||
internal actual fun platformEnv(key: String): String? = System.getenv(key)
|
||||
@@ -0,0 +1,8 @@
|
||||
package pw.binom.agentik.cli
|
||||
|
||||
import kotlinx.cinterop.ExperimentalForeignApi
|
||||
import kotlinx.cinterop.toKString
|
||||
import platform.posix.getenv
|
||||
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
internal actual fun platformEnv(key: String): String? = getenv(key)?.toKString()
|
||||
@@ -0,0 +1,103 @@
|
||||
# `:agentik-tui` — Compose-for-Mosaic TUI-клиент к `/agentik`
|
||||
|
||||
## Что это
|
||||
|
||||
Compose-style TUI-клиент в терминале на базе
|
||||
[Mosaic](https://github.com/JakeWharton/mosaic) (Jetpack Compose
|
||||
runtime, рендерится в ANSI-коды). Без `:`-команд (без vim-style
|
||||
prompt): клавиатурная навигация Tab/Enter/Esc/Ctrl-D/F1/стрелки +
|
||||
жирный focus indicator.
|
||||
|
||||
- **Layout**: header (id/conv/focus) + history + input + footer.
|
||||
- **Focus**: Tab/Shift-Tab цикл по фокусам (input → history → sidebar).
|
||||
- **Input**: стандартное текстовое поле с курсором `|` посередине.
|
||||
- **Stream**: подписка на SSE в фон-корутинах, `StateFlow` + `collectAsState()`
|
||||
для UI-реактивности (см. Snake sample).
|
||||
|
||||
Решает: полноценный TUI-клиент для тех, кто предпочитает мышкой
|
||||
кликать в терминале больше, чем печатать. В отличие от `:agentik-cli`,
|
||||
показывает историю диалога и текущий стрим в одном окне.
|
||||
|
||||
## Как запустить
|
||||
|
||||
### Требования
|
||||
|
||||
- JVM 21+.
|
||||
- Запущенный `:standalone` (по умолчанию `http://localhost:8080/agentik`).
|
||||
- Реальный TTY (через `ssh -tt`, `tmux`, либо нативный terminal).
|
||||
|
||||
### Запуск из готового fatjar
|
||||
|
||||
```bash
|
||||
java --enable-native-access=ALL-UNNAMED \
|
||||
-jar agentik-tui-0.1.0-all.jar \
|
||||
--server http://192.168.76.166:8080/agentik
|
||||
```
|
||||
|
||||
`--enable-native-access=ALL-UNNAMED` обязателен — Mosaic использует
|
||||
native syscalls для терминала.
|
||||
|
||||
### Запуск через Gradle (dev)
|
||||
|
||||
```bash
|
||||
./gradlew :agentik-tui:run --args="--server http://localhost:8080/agentik"
|
||||
```
|
||||
|
||||
## Параметры CLI
|
||||
|
||||
| Флаг | ENV | Что делает |
|
||||
|---|---|---|
|
||||
| `--server URL` | `AGENTIK_SERVER` | URL `/agentik` (default `http://localhost:8080/agentik`) |
|
||||
| `--id ID` | `USER`/`USERNAME` | Имя агента (default — текущий пользователь) |
|
||||
| `--no-history` | — | Не восстанавливать последнюю диалог после запуска |
|
||||
| `--help` | — | Показывает help и выходит |
|
||||
|
||||
## Keybindings
|
||||
|
||||
| Клавиша | Когда | Что делает |
|
||||
|---|---|---|
|
||||
| `Tab` / `Shift-Tab` | глобально | Цикл фокусов: input → history → sidebar → ... |
|
||||
| `F1` | глобально | Toggle help overlay |
|
||||
| `Esc` | в input | Очистить input |
|
||||
| `Enter` | в input | Submit message |
|
||||
| `Backspace` / `Del` | в input | Удалить символ |
|
||||
| `←` `→` `Home` `End` | в input | Курсор |
|
||||
| `↑` `↓` | в history | Scrollback |
|
||||
| `Ctrl-D` / `Ctrl-C` | — | Exit (TODO — пока работает только вне стрима) |
|
||||
|
||||
## Переменные среды (сервера)
|
||||
|
||||
См. [`../standalone/README.md`](../standalone/README.md). TUI
|
||||
получает URL сервера через `--server`, остальное настройка
|
||||
агента, а не клиента.
|
||||
|
||||
## Известное ограничение
|
||||
|
||||
1. **SSE в не-TTY ssh закрывается на default Ktor timeout** — то
|
||||
же, что для `:agentik-cli`.
|
||||
2. **Mouse events не подключены** в v2 (Mosaic 0.18 не имеет
|
||||
built-in mouse-runtime). Планируется в v3 через termios
|
||||
SGR-mouse.
|
||||
3. **Нативные target'ы (macOS / Linux x64+ARM64 / Windows x64)**
|
||||
собраны, но без `:client` (он JVM-only). Для нативной работы
|
||||
нужен альтернативный HTTP-клиент.
|
||||
|
||||
## Тесты
|
||||
|
||||
```
|
||||
./gradlew :agentik-tui:jvmTest
|
||||
```
|
||||
|
||||
Тесты composable'ов и event-рендеринга. Включает smoke-test для
|
||||
key-event → AppState mutation → ре-рендер.
|
||||
|
||||
## Версии
|
||||
|
||||
`gradle/libs.versions.toml` → `[versions] agentik-agentik-tui`.
|
||||
|
||||
## Архитектурная заметка
|
||||
|
||||
UI-стейт держится в `StateFlow`, а **не** в Compose `mutableStateOf`.
|
||||
Причина: Mosaic 0.18 не триггерит recomposition от `mutableStateOf`
|
||||
-writes внутри `onPreviewKeyEvent`-handler'ов (см. Snake sample в
|
||||
репо Mosaic — они тоже используют `StateFlow` + `collectAsState()`).
|
||||
@@ -0,0 +1,110 @@
|
||||
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
|
||||
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.kotlin.multiplatform)
|
||||
alias(libs.plugins.kotlin.serialization)
|
||||
alias(libs.plugins.kotlin.compose)
|
||||
alias(libs.plugins.shadow)
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(21)
|
||||
// Suppress Beta-предупреждения от expect/actual объектов.
|
||||
compilerOptions {
|
||||
freeCompilerArgs.add("-Xexpect-actual-classes")
|
||||
}
|
||||
|
||||
// Mosaic 0.18 поддерживает JVM + desktop-native (macosX64/macosArm64/linuxX64/linuxArm64/mingwX64).
|
||||
// iOS пропускаем — на iOS не бывает TUI-сессий.
|
||||
jvm()
|
||||
macosX64()
|
||||
macosArm64()
|
||||
linuxX64()
|
||||
linuxArm64()
|
||||
mingwX64()
|
||||
|
||||
// Native executables. По умолчанию Kotlin/Native для каждого target'а
|
||||
// собирает только .klib (библиотеку) — для запускаемого .kexe надо
|
||||
// явно попросить binaries.executable(). entryPoint нужно задать явно:
|
||||
// KMP-линкер ищет функцию по FQN (без `Kt`-суффикса), а Kotlin/Native
|
||||
// добавляет суффикс только для файлов с именем `Main.kt`, поэтому
|
||||
// указываем точку входа как `pw.binom.agentik.tui.main` (без суффикса).
|
||||
//
|
||||
// Применяем к каждому из linuxX64/macosX64/macosArm64/linuxArm64/mingwX64
|
||||
// явно (а не через targets.withType), потому что targets DSL в KMP не
|
||||
// поддерживает реифицированный withType<KotlinNativeTarget>().
|
||||
@OptIn(ExperimentalKotlinGradlePluginApi::class)
|
||||
listOf(linuxX64(), linuxArm64(), macosX64(), macosArm64(), mingwX64()).forEach {
|
||||
it.binaries.executable {
|
||||
entryPoint = "pw.binom.agentik.tui.main"
|
||||
}
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
implementation(project(":proto"))
|
||||
|
||||
implementation(libs.kotlinx.coroutines.core)
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
|
||||
// JetBrains Compose runtime — тащит Mosaic как обёртку.
|
||||
implementation(libs.mosaic.runtime)
|
||||
implementation(libs.mosaic.tty.terminal)
|
||||
|
||||
// Health-check в Main.kt: Ktor CIO на JVM, на native не собирается —
|
||||
// там работает stub actual через expect/actual.
|
||||
implementation(libs.ktor.client.core)
|
||||
implementation(libs.ktor.client.cio)
|
||||
}
|
||||
jvmMain.dependencies {
|
||||
implementation(project(":client"))
|
||||
}
|
||||
commonTest.dependencies {
|
||||
implementation(kotlin("test"))
|
||||
implementation(libs.kotlinx.coroutines.core)
|
||||
implementation(libs.kotlinx.coroutines.test)
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalKotlinGradlePluginApi::class)
|
||||
jvm {
|
||||
binaries {
|
||||
executable {
|
||||
mainClass.set("pw.binom.agentik.tui.MainKt")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Fatjar (uberjar) ---
|
||||
//
|
||||
// Аналогично `:agentik-cli`: shadowJar склеивает `jvmJar` + `jvmRuntimeClasspath` в self-contained
|
||||
// `*-all.jar`. Shadow 8.x не авторегистрирует shadowJar в KMP-проектах — регистрируем явно.
|
||||
val shadowJarTask = tasks.register<ShadowJar>("shadowJar") {
|
||||
archiveBaseName.set("agentik-tui")
|
||||
archiveClassifier.set("all")
|
||||
description = "Self-contained fatjar with all runtime dependencies bundled (incl. Compose-runtime + Mosaic)."
|
||||
group = "build"
|
||||
|
||||
from(tasks.named("jvmJar"))
|
||||
val cc = try {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
configurations as org.gradle.api.artifacts.ConfigurationContainer
|
||||
} catch (_: ClassCastException) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
(project as org.gradle.api.Project).configurations as org.gradle.api.artifacts.ConfigurationContainer
|
||||
}
|
||||
from(cc.getByName("jvmRuntimeClasspath"))
|
||||
|
||||
mergeServiceFiles()
|
||||
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
||||
|
||||
manifest {
|
||||
attributes["Main-Class"] = "pw.binom.agentik.tui.MainKt"
|
||||
attributes["Implementation-Title"] = "agentik-tui"
|
||||
attributes["Implementation-Version"] = project.version.toString()
|
||||
}
|
||||
|
||||
includeEmptyDirs = false
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package pw.binom.agentik.tui
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import com.jakewharton.mosaic.layout.onPreviewKeyEvent
|
||||
import com.jakewharton.mosaic.modifier.Modifier
|
||||
import com.jakewharton.mosaic.ui.Column
|
||||
import com.jakewharton.mosaic.ui.Row
|
||||
import pw.binom.agentik.tui.ui.Footer
|
||||
import pw.binom.agentik.tui.ui.Header
|
||||
import pw.binom.agentik.tui.ui.HelpOverlay
|
||||
import pw.binom.agentik.tui.ui.HistoryPanel
|
||||
import pw.binom.agentik.tui.ui.InputLine
|
||||
|
||||
/**
|
||||
* Корневая Compose-композиция TUI. Содержит только каркас + глобальный key-handler;
|
||||
* каждый регион (header/history/input/footer/help) — отдельный компонент в `ui/`.
|
||||
*
|
||||
* Layout (минимальный):
|
||||
* ```
|
||||
* ┌─────────────────────────────────────────────────────────┐
|
||||
* │ HEADER: agentik · id · conv-id · focus=… │
|
||||
* ├─────────────────────────────────────────────────────────┤
|
||||
* │ HISTORY (весь актуальный диалог) │
|
||||
* ├─────────────────────────────────────────────────────────┤
|
||||
* │ INPUT LINE: > text| │
|
||||
* ├─────────────────────────────────────────────────────────┤
|
||||
* │ FOOTER: ↑↓ scroll Tab focus Enter send F1 help … │
|
||||
* └─────────────────────────────────────────────────────────┘
|
||||
* ```
|
||||
*
|
||||
* Глобальные клавиши (Tab/Shift-Tab/F1/Esc) обрабатываются здесь.
|
||||
* Клавиши внутри строки ввода — в [InputLine] (через свой `onPreviewKeyEvent`).
|
||||
*/
|
||||
@Composable
|
||||
internal fun App(state: AppState) {
|
||||
val focusIndex by state.focusIndex.collectAsState()
|
||||
val showHelp by state.showHelp.collectAsState()
|
||||
|
||||
Row(modifier = Modifier.onPreviewKeyEvent { ev ->
|
||||
when (ev.key) {
|
||||
"Tab" -> { state.cycleFocus(direction = if (ev.shift) -1 else +1); true }
|
||||
"F1" -> { state.toggleHelp(); true }
|
||||
"Escape", "Esc" -> {
|
||||
if (showHelp) state.setShowHelp(false)
|
||||
else if (focusIndex == 0) state.inputClear()
|
||||
true
|
||||
}
|
||||
else -> false
|
||||
}
|
||||
}) {
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Header(state, focusIndex)
|
||||
HistoryPanel(state)
|
||||
InputLine(state)
|
||||
Footer(showHelp)
|
||||
}
|
||||
}
|
||||
if (showHelp) HelpOverlay()
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package pw.binom.agentik.tui
|
||||
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* Состояние TUI. По дизайну — singleton, переживает все экраны.
|
||||
*
|
||||
* Используем [StateFlow] вместо Compose [androidx.compose.runtime.mutableStateOf]
|
||||
* потому что в Mosaic 0.18 recomposition от `mutableStateOf`-writes из key-event
|
||||
* handlers работает нестабильно (требует ручного [androidx.compose.runtime.Snapshot]
|
||||
* apply). `StateFlow` + `collectAsState()` — работает out-of-the-box
|
||||
* (см. samples/snake в репо Mosaic).
|
||||
*/
|
||||
internal class AppState(val config: TuiConfig) {
|
||||
/** Бэкенд, прикреплённый из TuiApp — маршрутизирует submitInput → send. */
|
||||
private var backend: TuiBackend? = null
|
||||
fun attachBackend(b: TuiBackend) { backend = b }
|
||||
|
||||
/** Зона фокуса: 0 = input, 1 = history, 2 = sidebar. */
|
||||
private val _focusIndex = MutableStateFlow(0)
|
||||
val focusIndex: StateFlow<Int> = _focusIndex.asStateFlow()
|
||||
|
||||
/** Видимость help-оверлея. */
|
||||
private val _showHelp = MutableStateFlow(false)
|
||||
val showHelp: StateFlow<Boolean> = _showHelp.asStateFlow()
|
||||
|
||||
/** Сообщения диалога. */
|
||||
private val _messages = MutableStateFlow<List<TuiMessage>>(emptyList())
|
||||
val messages: StateFlow<List<TuiMessage>> = _messages.asStateFlow()
|
||||
|
||||
/** Заголовок текущего диалога. */
|
||||
private val _currentTitle = MutableStateFlow<String?>(null)
|
||||
val currentTitle: StateFlow<String?> = _currentTitle.asStateFlow()
|
||||
|
||||
/** ID текущего диалога. */
|
||||
private val _currentConversationId = MutableStateFlow<String?>(null)
|
||||
val currentConversationId: StateFlow<String?> = _currentConversationId.asStateFlow()
|
||||
|
||||
/** Список диалогов (sidebar). */
|
||||
private val _conversations = MutableStateFlow<List<ConvSummary>>(emptyList())
|
||||
val conversations: StateFlow<List<ConvSummary>> = _conversations.asStateFlow()
|
||||
|
||||
/** Курсор в списке диалогов. */
|
||||
private val _conversationsCursor = MutableStateFlow(0)
|
||||
val conversationsCursor: StateFlow<Int> = _conversationsCursor.asStateFlow()
|
||||
|
||||
/** Поле ввода. */
|
||||
private val _input = MutableStateFlow("")
|
||||
val input: StateFlow<String> = _input.asStateFlow()
|
||||
|
||||
/** Курсор в input (offset в chars). */
|
||||
private val _cursor = MutableStateFlow(0)
|
||||
val cursor: StateFlow<Int> = _cursor.asStateFlow()
|
||||
|
||||
/** Идёт ли стрим. */
|
||||
private val _streaming = MutableStateFlow(false)
|
||||
val streaming: StateFlow<Boolean> = _streaming.asStateFlow()
|
||||
|
||||
/** Scrollback index: 0 = прижат к низу. */
|
||||
private val _historyScroll = MutableStateFlow(0)
|
||||
val historyScroll: StateFlow<Int> = _historyScroll.asStateFlow()
|
||||
|
||||
// ---------- мутации ----------
|
||||
|
||||
fun cycleFocus(direction: Int = +1) {
|
||||
_focusIndex.value = (_focusIndex.value + direction).mod(3)
|
||||
}
|
||||
|
||||
fun toggleHelp() { _showHelp.value = !_showHelp.value }
|
||||
fun setShowHelp(v: Boolean) { _showHelp.value = v }
|
||||
|
||||
fun inputInsert(s: String) {
|
||||
val pos = _cursor.value.coerceIn(0, _input.value.length)
|
||||
_input.value = _input.value.substring(0, pos) + s + _input.value.substring(pos)
|
||||
_cursor.value = pos + s.length
|
||||
}
|
||||
|
||||
fun inputBackspace() {
|
||||
val pos = _cursor.value
|
||||
if (pos <= 0) return
|
||||
_input.value = _input.value.substring(0, pos - 1) + _input.value.substring(pos)
|
||||
_cursor.value = pos - 1
|
||||
}
|
||||
|
||||
fun inputDelete() {
|
||||
val pos = _cursor.value
|
||||
if (pos >= _input.value.length) return
|
||||
_input.value = _input.value.substring(0, pos) + _input.value.substring(pos + 1)
|
||||
}
|
||||
|
||||
fun inputClear() { _input.value = ""; _cursor.value = 0 }
|
||||
|
||||
fun inputMoveCursor(delta: Int) {
|
||||
_cursor.value = (_cursor.value + delta).coerceIn(0, _input.value.length)
|
||||
}
|
||||
fun inputCursorHome() { _cursor.value = 0 }
|
||||
fun inputCursorEnd() { _cursor.value = _input.value.length }
|
||||
|
||||
fun submitInput(): String? {
|
||||
val text = _input.value.trim()
|
||||
if (text.isEmpty()) return null
|
||||
_messages.value = _messages.value + TuiMessage.User(text = text, ts = nowInstant())
|
||||
inputClear()
|
||||
_streaming.value = true
|
||||
backend?.onUserMessage(text)
|
||||
return text
|
||||
}
|
||||
|
||||
fun setStreaming(v: Boolean) { _streaming.value = v }
|
||||
|
||||
fun setConversation(id: String, title: String?) {
|
||||
_currentConversationId.value = id
|
||||
_currentTitle.value = title
|
||||
_messages.value = emptyList()
|
||||
_historyScroll.value = 0
|
||||
_streaming.value = false
|
||||
}
|
||||
|
||||
fun postToolCall(toolName: String, title: String?, args: String) {
|
||||
_messages.value = _messages.value + TuiMessage.ToolCall(toolName = toolName, title = title, args = args, ts = nowInstant())
|
||||
}
|
||||
|
||||
fun postToolResult(toolName: String, result: String) {
|
||||
_messages.value = _messages.value + TuiMessage.ToolResult(toolName = toolName, result = result, ts = nowInstant())
|
||||
}
|
||||
|
||||
fun appendAssistant(chunk: String) {
|
||||
val list = _messages.value.toMutableList()
|
||||
val last = list.lastOrNull()
|
||||
if (last is TuiMessage.AssistantStreaming) {
|
||||
list[list.lastIndex] = last.copy(text = last.text + chunk)
|
||||
} else {
|
||||
list.add(TuiMessage.AssistantStreaming(text = chunk, ts = nowInstant()))
|
||||
}
|
||||
_messages.value = list
|
||||
}
|
||||
|
||||
fun finishAssistant() {
|
||||
val list = _messages.value.toMutableList()
|
||||
val last = list.lastOrNull() ?: return
|
||||
if (last is TuiMessage.AssistantStreaming) {
|
||||
list[list.lastIndex] = TuiMessage.Assistant(text = last.text, ts = last.ts)
|
||||
_messages.value = list
|
||||
}
|
||||
_streaming.value = false
|
||||
}
|
||||
|
||||
fun newConversation(id: String, title: String?) {
|
||||
_currentConversationId.value = id
|
||||
_currentTitle.value = title
|
||||
_messages.value = emptyList()
|
||||
_historyScroll.value = 0
|
||||
_streaming.value = false
|
||||
}
|
||||
|
||||
fun postSystem(text: String) {
|
||||
_messages.value = _messages.value + TuiMessage.System(text = text, ts = nowInstant())
|
||||
}
|
||||
}
|
||||
|
||||
/** Снимок диалога для sidebar. */
|
||||
internal data class ConvSummary(
|
||||
val id: String,
|
||||
val title: String?,
|
||||
val updatedAt: Instant,
|
||||
)
|
||||
|
||||
/** Рендер-единица. */
|
||||
internal sealed interface TuiMessage {
|
||||
val ts: Instant
|
||||
|
||||
data class System(val text: String, override val ts: Instant) : TuiMessage
|
||||
data class User(val text: String, override val ts: Instant) : TuiMessage
|
||||
data class AssistantStreaming(val text: String, override val ts: Instant) : TuiMessage
|
||||
data class Assistant(val text: String, override val ts: Instant) : TuiMessage
|
||||
data class ToolCall(val toolName: String, val title: String?, val args: String, override val ts: Instant) : TuiMessage
|
||||
data class ToolResult(val toolName: String, val result: String, override val ts: Instant) : TuiMessage
|
||||
}
|
||||
|
||||
internal fun nowInstant(): Instant = kotlin.time.Clock.System.now()
|
||||
@@ -0,0 +1,161 @@
|
||||
package pw.binom.agentik.tui
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
import io.ktor.client.plugins.HttpTimeout
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.statement.bodyAsText
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import pw.binom.agentik.proto.Agent
|
||||
|
||||
/**
|
||||
* Точка входа TUI-клиента agentik.
|
||||
*
|
||||
* ```
|
||||
* agentik-tui [--server URL] [--id ID] [--no-history] [--help]
|
||||
* ```
|
||||
*
|
||||
* Перед запуском UI — обязательный health-check: `GET {server}/health`.
|
||||
* Если сервер недоступен — печатаем понятную ошибку и выходим с кодом 1.
|
||||
* Если OK — создаём [Agent] через платформенную actual и запускаем
|
||||
* [TuiApp].
|
||||
*/
|
||||
fun main(args: Array<String>) = runBlocking {
|
||||
val cfg = parseCliArgs(args) ?: run {
|
||||
printUsage()
|
||||
return@runBlocking
|
||||
}
|
||||
checkServer(cfg.server)
|
||||
val agent = platformCreateAgent(cfg.server, cfg.id)
|
||||
TuiApp(cfg, agent).run()
|
||||
}
|
||||
|
||||
/**
|
||||
* Делает синхронный GET `{baseUrl}/health`. Внутри [route(path)] на сервере
|
||||
* `/health` зарегистрирован под тем же path-prefix'ом, что и сам API
|
||||
* (например, baseUrl = `http://localhost:8080/agentik` → health = …/agentik/health).
|
||||
*
|
||||
* При любой ошибке (connect refused, timeout, не-200 ответ, не `"ok"`) —
|
||||
* бросает [IllegalStateException] с понятным сообщением. [runBlocking]-обёртка
|
||||
* в [main] разворачивает её в stack-trace и `exit 1`.
|
||||
*/
|
||||
private suspend fun checkServer(baseUrl: String) {
|
||||
val healthUrl = "${baseUrl.trimEnd('/')}/health"
|
||||
val client = HttpClient(CIO) {
|
||||
install(HttpTimeout) {
|
||||
requestTimeoutMillis = 5_000
|
||||
connectTimeoutMillis = 3_000
|
||||
}
|
||||
expectSuccess = false
|
||||
}
|
||||
try {
|
||||
val response = client.get(healthUrl)
|
||||
if (response.status.value !in 200..299) {
|
||||
throw IllegalStateException("сервер ответил HTTP ${response.status.value} на GET $healthUrl")
|
||||
}
|
||||
val body = response.bodyAsText().trim()
|
||||
if (body != "ok") {
|
||||
throw IllegalStateException("сервер ответил неожиданным телом на GET $healthUrl: '$body'")
|
||||
}
|
||||
} catch (e: IllegalStateException) {
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
// На JVM сюда упадут java.net.ConnectException, UnknownHostException,
|
||||
// io.ktor.client.network.sockets.ConnectTimeoutException и т.п.
|
||||
// На нативе native stub падает раньше в platformCreateAgent, так что
|
||||
// сюда мы попадём только под JVM-actual.
|
||||
throw IllegalStateException(
|
||||
"ошибка health-check $healthUrl: ${e::class.simpleName} — ${e.message ?: "(нет сообщения)"}",
|
||||
e,
|
||||
)
|
||||
} finally {
|
||||
client.close()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Конфигурация TUI, вычисленная из аргументов + переменных среды.
|
||||
* Доступна из других файлов commonMain как `internal`.
|
||||
*/
|
||||
internal data class TuiConfig(
|
||||
val server: String,
|
||||
val id: String,
|
||||
val historyEnabled: Boolean,
|
||||
)
|
||||
|
||||
private fun parseCliArgs(args: Array<String>): TuiConfig? {
|
||||
var server: String? = null
|
||||
var id: String? = null
|
||||
var historyEnabled = true
|
||||
|
||||
var i = 0
|
||||
while (i < args.size) {
|
||||
when (val a = args[i]) {
|
||||
"--help", "-h", "help" -> return null
|
||||
"--server", "-s" -> {
|
||||
require(i + 1 < args.size) { "$a требует URL" }
|
||||
server = args[i + 1]; i += 2
|
||||
}
|
||||
"--id" -> {
|
||||
require(i + 1 < args.size) { "$a требует значение" }
|
||||
id = args[i + 1]; i += 2
|
||||
}
|
||||
"--no-history" -> { historyEnabled = false; i++ }
|
||||
"--" -> i++
|
||||
else -> error("неизвестный аргумент: $a (введите --help)")
|
||||
}
|
||||
}
|
||||
|
||||
val envServer = platformEnv("AGENTIK_SERVER")
|
||||
val envUser = platformEnv("USER") ?: platformEnv("USERNAME") ?: "anon"
|
||||
val resolvedServer = server ?: envServer ?: "http://localhost:8080/agentik"
|
||||
val resolvedId = id ?: "cli-tui:${envUser}"
|
||||
|
||||
return TuiConfig(
|
||||
server = resolvedServer,
|
||||
id = resolvedId,
|
||||
historyEnabled = historyEnabled,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Читает переменную среды. JVM actual — `System.getenv`, native actual — `getenv()` через cinterop.
|
||||
* Доступ к environment делается через expect/actual, чтобы commonMain не тащил JVM-пакеты.
|
||||
*/
|
||||
internal expect fun platformEnv(key: String): String?
|
||||
|
||||
/**
|
||||
* Создаёт платформенную реализацию [Agent]. JVM actual подключает `:client`
|
||||
* и ходит в HTTP-фасад; native actual пока возвращает stub (см. Platform.native.kt).
|
||||
*/
|
||||
internal expect fun platformCreateAgent(baseUrl: String, id: String): Agent
|
||||
|
||||
private fun printUsage() {
|
||||
val defaultServer = platformEnv("AGENTIK_SERVER") ?: "http://localhost:8080/agentik"
|
||||
val defaultUser = platformEnv("USER") ?: platformEnv("USERNAME") ?: "anon"
|
||||
|
||||
println("""
|
||||
agentik-tui — Compose-Mosaic UI поверх протокола agentik
|
||||
|
||||
Использование:
|
||||
agentik-tui [--server URL] [--id ID] [--no-history]
|
||||
|
||||
Аргументы:
|
||||
--server, -s URL базовый URL (default: $defaultServer)
|
||||
--id ID идентификатор клиента (default: cli-tui:${defaultUser})
|
||||
--no-history не сохранять состояние
|
||||
--help, -h эта справка
|
||||
|
||||
Переменные среды:
|
||||
AGENTIK_SERVER базовый URL (эквивалент --server)
|
||||
USER / USERNAME используется в id клиента по умолчанию
|
||||
|
||||
В UI:
|
||||
Tab / Shift-Tab переключить фокус между историей и вводом
|
||||
↑ / ↓ скроллить историю / двигать курсор в инпуте
|
||||
← / → двинуть курсор в инпуте
|
||||
Enter отправить сообщение (создаст новый диалог, если их нет)
|
||||
Ctrl-C / Ctrl-D выйти
|
||||
F1 показать подсказки по горячим клавишам
|
||||
""".trimIndent())
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package pw.binom.agentik.tui
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.remember
|
||||
import com.jakewharton.mosaic.runMosaicBlocking
|
||||
import kotlinx.coroutines.launch
|
||||
import pw.binom.agentik.proto.Agent
|
||||
|
||||
/**
|
||||
* Корневая точка запуска UI. Стартует Mosaic-рантайм, монтирует [TuiBackend] в
|
||||
* его coroutine-scope и ждёт завершения приложения.
|
||||
*
|
||||
* Бэкенд — единый singleton на процесс: UI-композиция, сетевые подписки и
|
||||
* coroutine job'ы делят scope [runMosaicBlocking] (через [LaunchedEffect]).
|
||||
*/
|
||||
internal class TuiApp(
|
||||
private val config: TuiConfig,
|
||||
private val agent: Agent,
|
||||
) {
|
||||
fun run() {
|
||||
runMosaicBlocking {
|
||||
val state = remember { AppState(config) }
|
||||
val backend = remember { TuiBackend(state = state, agent = agent) }
|
||||
LaunchedEffect(backend) {
|
||||
backend.start(this)
|
||||
}
|
||||
state.attachBackend(backend)
|
||||
App(state = state)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package pw.binom.agentik.tui
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.launch
|
||||
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 kotlin.coroutines.CoroutineContext
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* Backend-логика TUI: мост между [Agent] и [AppState].
|
||||
*
|
||||
* Жизненный цикл:
|
||||
* 1. На старте [start] — health-check сделан в [Main] ДО Mosaic; здесь только
|
||||
* пост-сообщение "connected to …".
|
||||
* 2. Подписка на [Agent.events] — обновление списка диалогов в sidebar.
|
||||
* 3. При [onUserMessage] — если текущего диалога нет, создаём
|
||||
* [createConversation] (temp=false, чтобы он персистился на сервере), затем
|
||||
* [send]. Подписка на [Conversation.events] идёт сразу при создании/открытии.
|
||||
*
|
||||
* Дизайн: один backend-объект на процесс, живёт в [runMosaicBlocking]-scope.
|
||||
*/
|
||||
internal class TuiBackend(
|
||||
private val state: AppState,
|
||||
private val agent: Agent,
|
||||
) {
|
||||
/** Текущий открытый диалог, либо `null`, если ещё не выбран. */
|
||||
private var current: Conversation? = null
|
||||
|
||||
/** Активная джоба подписки на [Conversation.events]. */
|
||||
private var eventsJob: Job? = null
|
||||
|
||||
/** Последний виденный момент событий — для переподписки при reconnect. */
|
||||
private var lastSeenAt: Instant = Instant.DISTANT_PAST
|
||||
|
||||
/**
|
||||
* Запускает фоновые подписки в scope [scope] (передаётся из Mosaic
|
||||
* LaunchedEffect'а — это scope recomposer'а, живёт до закрытия UI).
|
||||
*/
|
||||
fun start(scope: CoroutineScope) {
|
||||
this.scope = scope
|
||||
state.postSystem("подключено к ${state.config.server}")
|
||||
scope.launch {
|
||||
try {
|
||||
// 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) {
|
||||
state.postSystem("ошибка live-events: ${e.message ?: e::class.simpleName}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private lateinit var scope: CoroutineScope
|
||||
|
||||
/**
|
||||
* Обработка пользовательского сообщения, отправленного из input.
|
||||
*
|
||||
* Если текущего диалога нет — создаём его; затем `send`. Подписка на
|
||||
* события конкретного диалога стартует в [ensureConversation].
|
||||
*/
|
||||
fun onUserMessage(text: String) {
|
||||
scope.launch {
|
||||
try {
|
||||
val conv = ensureConversation()
|
||||
conv.send(listOf(Content.Text(text)))
|
||||
} catch (e: Exception) {
|
||||
state.postSystem("ошибка отправки: ${e.message ?: e::class.simpleName}")
|
||||
state.setStreaming(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Создаёт [Conversation], если ещё не было; открывает подписку на её события.
|
||||
*/
|
||||
private suspend fun ensureConversation(): Conversation {
|
||||
current?.let { return it }
|
||||
val conv = agent.createConversation(temp = false)
|
||||
state.setConversation(id = conv.id, title = conv.title)
|
||||
subscribeEvents(conv, Instant.DISTANT_PAST)
|
||||
current = conv
|
||||
return conv
|
||||
}
|
||||
|
||||
/**
|
||||
* Подписывается на [Conversation.events] и перенаправляет их в [state].
|
||||
*/
|
||||
private fun subscribeEvents(conv: Conversation, from: Instant) {
|
||||
eventsJob?.cancel()
|
||||
eventsJob = scope.launch {
|
||||
conv.events(from).collect { ev -> dispatch(ev) }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Маппинг [Event] → [AppState] (что показать в TUI).
|
||||
*
|
||||
* - AppendText → дописывает в последний ассистентский чанк
|
||||
* - StartReasoning / StartResponse → новый streaming-чанк
|
||||
* - End → закрывает streaming
|
||||
* - Interrupted → закрывает streaming + системное сообщение
|
||||
* - ToolCall / ToolResult → сообщения в историю
|
||||
* - Error → системное сообщение
|
||||
*/
|
||||
private fun dispatch(ev: Event) {
|
||||
lastSeenAt = ev.date
|
||||
when (ev) {
|
||||
is Event.AppendText -> state.appendAssistant(ev.body)
|
||||
is Event.StartReasoning -> {
|
||||
state.postSystem("… думаю")
|
||||
}
|
||||
is Event.StartResponse -> state.setStreaming(true)
|
||||
is Event.End -> state.finishAssistant()
|
||||
is Event.Interrupted -> {
|
||||
state.finishAssistant()
|
||||
state.postSystem("прервано")
|
||||
}
|
||||
is Event.AppendImage -> {
|
||||
state.postSystem("[картинка: ${ev.mime}, ${ev.body.size} байт]")
|
||||
}
|
||||
is Event.ToolCall -> {
|
||||
state.postToolCall(toolName = ev.toolName, title = null, args = ev.toolArgs)
|
||||
}
|
||||
is Event.ToolResult -> {
|
||||
state.postToolResult(toolName = "", result = ev.result ?: "")
|
||||
}
|
||||
is Event.Error -> {
|
||||
state.setStreaming(false)
|
||||
state.postSystem("ошибка: ${ev.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package pw.binom.agentik.tui.ui
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import com.jakewharton.mosaic.ui.Text
|
||||
import com.jakewharton.mosaic.ui.TextStyle
|
||||
|
||||
/**
|
||||
* Нижняя подсказка с текущим набором горячих клавиш.
|
||||
*
|
||||
* При открытом help-оверлее показывает заглушку с указателем «наверху».
|
||||
*/
|
||||
@Composable
|
||||
internal fun Footer(showHelp: Boolean) {
|
||||
val hint = if (showHelp) " ↑ наверху help-оверлей ↑ "
|
||||
else " Tab focus ↑↓ scroll Enter send Esc clear F1 help Ctrl-D exit "
|
||||
Text(value = hint, textStyle = TextStyle.Italic)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package pw.binom.agentik.tui.ui
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import com.jakewharton.mosaic.ui.Text
|
||||
import com.jakewharton.mosaic.ui.TextStyle
|
||||
import pw.binom.agentik.tui.AppState
|
||||
|
||||
/**
|
||||
* Верхняя инвертированная полоса с идентификатором и текущим фокусом.
|
||||
*
|
||||
* Пример: ` agentik · cli-tui:root · a1b2c3d4… · мой чат · focus=input `
|
||||
*/
|
||||
@Composable
|
||||
internal fun Header(state: AppState, focusIndex: Int) {
|
||||
val title by state.currentTitle.collectAsState()
|
||||
val convId by state.currentConversationId.collectAsState()
|
||||
val focusLabel = when (focusIndex) { 0 -> "input"; 1 -> "history"; 2 -> "sidebar"; else -> "?" }
|
||||
val convStr = convId?.let { " · ${it.take(8)}…" } ?: ""
|
||||
val titleStr = title ?: "(нет диалога)"
|
||||
Text(
|
||||
value = " agentik · ${state.config.id}$convStr · $titleStr · focus=$focusLabel ",
|
||||
textStyle = TextStyle.Bold + TextStyle.Invert,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package pw.binom.agentik.tui.ui
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import com.jakewharton.mosaic.modifier.Modifier
|
||||
import com.jakewharton.mosaic.ui.Column
|
||||
import com.jakewharton.mosaic.ui.Text
|
||||
import com.jakewharton.mosaic.ui.TextStyle
|
||||
|
||||
/**
|
||||
* Полноэкранный оверлей со списком горячих клавиш.
|
||||
* Включается/выключается по F1 (см. [pw.binom.agentik.tui.App]).
|
||||
*/
|
||||
@Composable
|
||||
internal fun HelpOverlay() {
|
||||
Column(modifier = Modifier) {
|
||||
Text(value = " --- HELP ---", textStyle = TextStyle.Bold + TextStyle.Invert)
|
||||
Text(value = " Tab / Shift-Tab переключить фокус (history / input / sidebar)")
|
||||
Text(value = " ↑ / ↓ скролл истории / курсор в input")
|
||||
Text(value = " ← / → курсор в input")
|
||||
Text(value = " Enter отправить сообщение")
|
||||
Text(value = " Backspace / Del удалить символ")
|
||||
Text(value = " Esc очистить input")
|
||||
Text(value = " Ctrl-D / Ctrl-C выход")
|
||||
Text(value = " F1 toggle help", textStyle = TextStyle.Italic)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package pw.binom.agentik.tui.ui
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import com.jakewharton.mosaic.ui.Text
|
||||
import pw.binom.agentik.tui.AppState
|
||||
import pw.binom.agentik.tui.TuiMessage
|
||||
|
||||
/**
|
||||
* Прокручиваемый (через клавиатуру) лог диалога.
|
||||
* Каждое сообщение рендерится отдельной строкой с префиксом (см. [renderMessage]).
|
||||
* При пустом списке показывается подсказка.
|
||||
*/
|
||||
@Composable
|
||||
internal fun HistoryPanel(state: AppState) {
|
||||
val messages by state.messages.collectAsState()
|
||||
val rendered = if (messages.isEmpty()) {
|
||||
" (пока пусто)\n Tab — переключить фокус, F1 — подсказки.\n"
|
||||
} else {
|
||||
messages.joinToString("") { renderMessage(it) }
|
||||
}
|
||||
Text(value = rendered)
|
||||
}
|
||||
|
||||
/** Превращает [TuiMessage] в одну строку с префиксом. Потоковые чанки получают курсор `▍`. */
|
||||
internal fun renderMessage(m: TuiMessage): String = when (m) {
|
||||
is TuiMessage.System -> " ── ${m.text}\n"
|
||||
is TuiMessage.User -> " > ${m.text}\n"
|
||||
is TuiMessage.Assistant -> " ╰ ${m.text}\n"
|
||||
is TuiMessage.AssistantStreaming -> " ╰ ${m.text} ▍\n"
|
||||
is TuiMessage.ToolCall -> " ⚙ ${m.toolName}${if (!m.title.isNullOrEmpty()) ": ${m.title}" else ""}\n"
|
||||
is TuiMessage.ToolResult -> " ↳ ${m.result.take(200)}${if (m.result.length > 200) "…" else ""}\n"
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package pw.binom.agentik.tui.ui
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import com.jakewharton.mosaic.layout.KeyEvent
|
||||
import com.jakewharton.mosaic.layout.drawBehind
|
||||
import com.jakewharton.mosaic.layout.onPreviewKeyEvent
|
||||
import com.jakewharton.mosaic.modifier.Modifier
|
||||
import com.jakewharton.mosaic.ui.Text
|
||||
import pw.binom.agentik.tui.AppState
|
||||
|
||||
/**
|
||||
* Нижняя строка ввода с курсором.
|
||||
* При активном стриме ассистента показывает ` ⋯`, иначе ` >`.
|
||||
*
|
||||
* Содержимое строки: `<prompt> <before>|<cursorChar>|<after>` —
|
||||
* `cursorChar` — это символ, на котором стоит курсор (или пробел в конце).
|
||||
*
|
||||
* Клавиши обрабатываются через [handleInputKey] внутри `onPreviewKeyEvent`.
|
||||
*/
|
||||
@Composable
|
||||
internal fun InputLine(state: AppState) {
|
||||
val text by state.input.collectAsState()
|
||||
val cursor by state.cursor.collectAsState()
|
||||
val streaming by state.streaming.collectAsState()
|
||||
val cursorPos = cursor.coerceIn(0, text.length)
|
||||
val before = text.substring(0, cursorPos)
|
||||
val cursorChar = if (cursorPos < text.length) text[cursorPos].toString() else " "
|
||||
val afterStart = if (cursorPos < text.length) cursorPos + 1 else cursorPos
|
||||
val after = text.substring(afterStart.coerceAtMost(text.length))
|
||||
val prompt = if (streaming) " ⋯" else " >"
|
||||
|
||||
Text(
|
||||
value = "$prompt $before|$cursorChar|${after}",
|
||||
modifier = Modifier
|
||||
.onPreviewKeyEvent { ev -> handleInputKey(state, ev) }
|
||||
.drawBehind {
|
||||
// Snapshot-read state в drawBehind чтобы changes триггерили redraw.
|
||||
state.input.let { /* touch */ }
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Обработка клавиш в [InputLine]. `true` = событие поглощено.
|
||||
*
|
||||
* Не перехватывает клавиши с `alt`/`ctrl` — они идут дальше
|
||||
* на корневой обработчик ([pw.binom.agentik.tui.App]).
|
||||
*/
|
||||
internal fun handleInputKey(state: AppState, ev: KeyEvent): Boolean {
|
||||
if (ev.alt || ev.ctrl) return false
|
||||
return when (ev.key) {
|
||||
"Enter" -> state.submitInput() != null
|
||||
"Backspace" -> { state.inputBackspace(); true }
|
||||
"Delete" -> { state.inputDelete(); true }
|
||||
"Left", "ArrowLeft" -> { state.inputMoveCursor(-1); true }
|
||||
"Right", "ArrowRight" -> { state.inputMoveCursor(+1); true }
|
||||
"Home" -> { state.inputCursorHome(); true }
|
||||
"End" -> { state.inputCursorEnd(); true }
|
||||
else -> {
|
||||
val s = ev.key
|
||||
if (s.length == 1) { state.inputInsert(s); true }
|
||||
else false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
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.Content
|
||||
import pw.binom.agentik.proto.Conversation
|
||||
import pw.binom.agentik.proto.Event
|
||||
import pw.binom.agentik.proto.Message
|
||||
import pw.binom.agentik.proto.MessageContext
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* Минимальный fake [Agent] для тестов [TuiBackend]: считает, сколько раз
|
||||
* вызвали [createConversation], и отдаёт заранее сконструированные
|
||||
* [FakeConversation].
|
||||
*/
|
||||
internal class FakeAgent(
|
||||
private val conversationFactory: () -> FakeConversation = { FakeConversation() },
|
||||
) : Agent {
|
||||
override val id: String = "fake"
|
||||
var createCount: Int = 0
|
||||
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()
|
||||
conversations += c
|
||||
return c
|
||||
}
|
||||
|
||||
override suspend fun getConversation(id: String): Conversation? =
|
||||
conversations.firstOrNull { it.id == id }
|
||||
|
||||
override suspend fun deleteConversation(id: String): Boolean =
|
||||
conversations.removeAll { it.id == id }
|
||||
|
||||
override suspend fun getConversations(offset: Int, limit: Int): List<Conversation> =
|
||||
conversations.toList()
|
||||
}
|
||||
|
||||
/**
|
||||
* [Conversation], запоминающий все вызовы [send] и эмитящий управляемые
|
||||
* [Event] через общий [MutableSharedFlow]. Используется в тестах
|
||||
* [TuiBackend] для проверки маршрутизации событий в UI.
|
||||
*/
|
||||
internal class FakeConversation(
|
||||
override val id: String = "fake-conv",
|
||||
override val title: String? = null,
|
||||
) : Conversation {
|
||||
override val isSupportImageInput: Boolean = false
|
||||
override val isSupportImageOutput: Boolean = false
|
||||
override val isTemporal: Boolean = false
|
||||
override val updatedAt: Instant = Instant.DISTANT_PAST
|
||||
|
||||
val sent = mutableListOf<List<Content>>()
|
||||
val sentContexts = mutableListOf<MessageContext?>()
|
||||
var closed: Boolean = false
|
||||
private set
|
||||
var interrupted: Boolean = false
|
||||
private set
|
||||
|
||||
private val eventsFlow = MutableSharedFlow<Event>(extraBufferCapacity = 64)
|
||||
fun emit(e: Event) { eventsFlow.tryEmit(e) }
|
||||
|
||||
override suspend fun rename(title: String) = Unit
|
||||
|
||||
override suspend fun send(content: List<Content>, context: MessageContext?) {
|
||||
sent += content
|
||||
sentContexts += context
|
||||
}
|
||||
|
||||
override suspend fun interrupt() { interrupted = true }
|
||||
|
||||
override fun events(after: Instant): Flow<Event> = eventsFlow
|
||||
|
||||
override suspend fun getMessages(after: Instant, offset: Int, limit: Int): List<Message> = emptyList()
|
||||
|
||||
override fun close() { closed = true }
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
package pw.binom.agentik.tui
|
||||
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.runCurrent
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import pw.binom.agentik.proto.Content
|
||||
import pw.binom.agentik.proto.Event
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
/**
|
||||
* Тесты [TuiBackend]. Используем [runTest.backgroundScope] (а не TestScope)
|
||||
* для передачи в `start` — фоновые подписки должны жить параллельно с
|
||||
* телом теста и автоматически отменяться по его завершении. Иначе
|
||||
* бесконечный collect на `agent.events()` завешивает runTest на 60s
|
||||
* `UncompletedCoroutinesError`.
|
||||
*
|
||||
* [runCurrent] нужен после каждого `onUserMessage` и каждого `emit`,
|
||||
* потому что `backgroundScope` использует свой диспетчер, который не
|
||||
* продвигается через `advanceUntilIdle` — `runCurrent` прогоняет ровно
|
||||
* те задачи, что готовы к запуску сейчас.
|
||||
*/
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class TuiBackendTest {
|
||||
|
||||
private fun fixtureConfig(server: String = "http://localhost:8080/agentik") =
|
||||
TuiConfig(server = server, id = "cli-tui:tester", historyEnabled = true)
|
||||
|
||||
@Test
|
||||
fun `start posts connected system message`() = runTest {
|
||||
val cfg = fixtureConfig()
|
||||
val state = AppState(cfg)
|
||||
val agent = FakeAgent()
|
||||
val backend = TuiBackend(state = state, agent = agent)
|
||||
backend.start(backgroundScope)
|
||||
runCurrent()
|
||||
|
||||
val sysMsgs = state.messages.value.filterIsInstance<TuiMessage.System>()
|
||||
assertTrue(
|
||||
sysMsgs.any { it.text.contains(cfg.server) },
|
||||
"ожидалось системное 'подключено к ${cfg.server}', было: ${sysMsgs.map { it.text }}",
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `first onUserMessage auto-creates conversation with temp=false`() = runTest {
|
||||
val cfg = fixtureConfig()
|
||||
val state = AppState(cfg)
|
||||
val agent = FakeAgent()
|
||||
val backend = TuiBackend(state = state, agent = agent)
|
||||
backend.start(backgroundScope)
|
||||
runCurrent()
|
||||
|
||||
backend.onUserMessage("привет")
|
||||
runCurrent()
|
||||
|
||||
assertEquals(1, agent.createCount, "должен быть один createConversation")
|
||||
assertEquals(listOf("привет"), agent.conversations.first().sent.flattenText())
|
||||
// temp=false — обычный (не временный) диалог: персистится на сервере
|
||||
assertFalse(agent.conversations.first().isTemporal, "диалог не должен быть временным")
|
||||
// state знает id и title нового диалога
|
||||
assertEquals("fake-conv", state.currentConversationId.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `second onUserMessage reuses same conversation`() = runTest {
|
||||
val cfg = fixtureConfig()
|
||||
val state = AppState(cfg)
|
||||
val agent = FakeAgent()
|
||||
val backend = TuiBackend(state = state, agent = agent)
|
||||
backend.start(backgroundScope)
|
||||
runCurrent()
|
||||
|
||||
backend.onUserMessage("раз")
|
||||
runCurrent()
|
||||
backend.onUserMessage("два")
|
||||
runCurrent()
|
||||
|
||||
assertEquals(1, agent.createCount, "новый диалог создавать не должны — переиспользуем старый")
|
||||
assertEquals(2, agent.conversations.first().sent.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `AppendText appends to current assistant streaming chunk`() = runTest {
|
||||
val cfg = fixtureConfig()
|
||||
val state = AppState(cfg)
|
||||
val conv = FakeConversation()
|
||||
val agent = FakeAgent(conversationFactory = { conv })
|
||||
val backend = TuiBackend(state = state, agent = agent)
|
||||
backend.start(backgroundScope)
|
||||
runCurrent()
|
||||
|
||||
backend.onUserMessage("hi")
|
||||
runCurrent()
|
||||
val now = kotlin.time.Clock.System.now()
|
||||
conv.emit(Event.StartResponse(now, Event.ResponseType.TEXT))
|
||||
conv.emit(Event.AppendText(now, "Привет"))
|
||||
conv.emit(Event.AppendText(now, ", мир"))
|
||||
runCurrent()
|
||||
|
||||
val assistantMsgs = state.messages.value.filterIsInstance<TuiMessage.AssistantStreaming>()
|
||||
assertEquals(1, assistantMsgs.size, "должен быть один streaming-чанк, не два")
|
||||
assertEquals("Привет, мир", assistantMsgs.single().text)
|
||||
assertTrue(state.streaming.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `End event finalizes assistant and stops streaming`() = runTest {
|
||||
val cfg = fixtureConfig()
|
||||
val state = AppState(cfg)
|
||||
val conv = FakeConversation()
|
||||
val agent = FakeAgent(conversationFactory = { conv })
|
||||
val backend = TuiBackend(state = state, agent = agent)
|
||||
backend.start(backgroundScope)
|
||||
runCurrent()
|
||||
|
||||
backend.onUserMessage("hi")
|
||||
runCurrent()
|
||||
val now = kotlin.time.Clock.System.now()
|
||||
conv.emit(Event.StartResponse(now, Event.ResponseType.TEXT))
|
||||
conv.emit(Event.AppendText(now, "ответ"))
|
||||
conv.emit(Event.End(now))
|
||||
runCurrent()
|
||||
|
||||
val last = state.messages.value.last()
|
||||
assertTrue(last is TuiMessage.Assistant, "после End последнее сообщение должно стать финальным Assistant, было: ${last::class.simpleName}")
|
||||
assertEquals("ответ", (last as TuiMessage.Assistant).text)
|
||||
assertFalse(state.streaming.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Interrupted event clears streaming and posts system message`() = runTest {
|
||||
val cfg = fixtureConfig()
|
||||
val state = AppState(cfg)
|
||||
val conv = FakeConversation()
|
||||
val agent = FakeAgent(conversationFactory = { conv })
|
||||
val backend = TuiBackend(state = state, agent = agent)
|
||||
backend.start(backgroundScope)
|
||||
runCurrent()
|
||||
|
||||
backend.onUserMessage("hi")
|
||||
runCurrent()
|
||||
val now = kotlin.time.Clock.System.now()
|
||||
conv.emit(Event.StartResponse(now, Event.ResponseType.TEXT))
|
||||
conv.emit(Event.AppendText(now, "часть ответа"))
|
||||
conv.emit(Event.Interrupted(now))
|
||||
runCurrent()
|
||||
|
||||
assertFalse(state.streaming.value)
|
||||
val sysMsgs = state.messages.value.filterIsInstance<TuiMessage.System>()
|
||||
assertTrue(
|
||||
sysMsgs.any { it.text.contains("прервано") },
|
||||
"ожидалось 'прервано' в системных сообщениях, было: ${sysMsgs.map { it.text }}",
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ToolCall and ToolResult events become visible tool messages`() = runTest {
|
||||
val cfg = fixtureConfig()
|
||||
val state = AppState(cfg)
|
||||
val conv = FakeConversation()
|
||||
val agent = FakeAgent(conversationFactory = { conv })
|
||||
val backend = TuiBackend(state = state, agent = agent)
|
||||
backend.start(backgroundScope)
|
||||
runCurrent()
|
||||
|
||||
backend.onUserMessage("hi")
|
||||
runCurrent()
|
||||
val now = kotlin.time.Clock.System.now()
|
||||
conv.emit(Event.ToolCall(date = now, id = "1", title = null, toolName = "echo", toolArgs = """{"x":1}"""))
|
||||
conv.emit(Event.ToolResult(date = now, id = "1", result = "ok"))
|
||||
runCurrent()
|
||||
|
||||
val toolMsgs = state.messages.value.filterIsInstance<TuiMessage.ToolCall>()
|
||||
val resultMsgs = state.messages.value.filterIsInstance<TuiMessage.ToolResult>()
|
||||
assertEquals(1, toolMsgs.size)
|
||||
assertEquals("echo", toolMsgs.single().toolName)
|
||||
assertEquals("""{"x":1}""", toolMsgs.single().args)
|
||||
assertEquals(1, resultMsgs.size)
|
||||
assertEquals("ok", resultMsgs.single().result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Error event posts system message and clears streaming`() = runTest {
|
||||
val cfg = fixtureConfig()
|
||||
val state = AppState(cfg)
|
||||
val conv = FakeConversation()
|
||||
val agent = FakeAgent(conversationFactory = { conv })
|
||||
val backend = TuiBackend(state = state, agent = agent)
|
||||
backend.start(backgroundScope)
|
||||
runCurrent()
|
||||
|
||||
backend.onUserMessage("hi")
|
||||
runCurrent()
|
||||
val now = kotlin.time.Clock.System.now()
|
||||
conv.emit(Event.StartResponse(now, Event.ResponseType.TEXT))
|
||||
conv.emit(Event.Error(date = now, message = "boom"))
|
||||
runCurrent()
|
||||
|
||||
val sysMsgs = state.messages.value.filterIsInstance<TuiMessage.System>()
|
||||
assertTrue(sysMsgs.any { it.text.contains("boom") }, "должно быть 'ошибка: boom'")
|
||||
assertFalse(state.streaming.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `onUserMessage does not swallow exceptions - state stays consistent`() = runTest {
|
||||
val cfg = fixtureConfig()
|
||||
val state = AppState(cfg)
|
||||
val agent = FakeAgent(conversationFactory = { error("server kaboom") })
|
||||
val backend = TuiBackend(state = state, agent = agent)
|
||||
backend.start(backgroundScope)
|
||||
runCurrent()
|
||||
|
||||
backend.onUserMessage("hi")
|
||||
runCurrent()
|
||||
|
||||
val sysMsgs = state.messages.value.filterIsInstance<TuiMessage.System>()
|
||||
assertTrue(
|
||||
sysMsgs.any { it.text.contains("ошибка отправки") || it.text.contains("server kaboom") },
|
||||
"должна быть системная ошибка, было: ${sysMsgs.map { it.text }}",
|
||||
)
|
||||
assertFalse(state.streaming.value, "стриминг должен быть выключен в catch-ветке")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `StartReasoning posts thinking system message`() = runTest {
|
||||
val cfg = fixtureConfig()
|
||||
val state = AppState(cfg)
|
||||
val conv = FakeConversation()
|
||||
val agent = FakeAgent(conversationFactory = { conv })
|
||||
val backend = TuiBackend(state = state, agent = agent)
|
||||
backend.start(backgroundScope)
|
||||
runCurrent()
|
||||
|
||||
backend.onUserMessage("hi")
|
||||
runCurrent()
|
||||
val now = kotlin.time.Clock.System.now()
|
||||
conv.emit(Event.StartReasoning(now))
|
||||
runCurrent()
|
||||
|
||||
val sysMsgs = state.messages.value.filterIsInstance<TuiMessage.System>()
|
||||
assertTrue(sysMsgs.any { it.text.contains("думаю") })
|
||||
}
|
||||
}
|
||||
|
||||
private fun List<List<Content>>.flattenText(): List<String> =
|
||||
map { cs -> cs.filterIsInstance<Content.Text>().joinToString("") { it.body } }
|
||||
@@ -0,0 +1,12 @@
|
||||
package pw.binom.agentik.tui
|
||||
|
||||
import pw.binom.agentik.client.AgentikAgent
|
||||
import pw.binom.agentik.proto.Agent
|
||||
|
||||
/**
|
||||
* Платформенные actual'ы для JVM. Используется `:client` поверх Ktor CIO.
|
||||
*/
|
||||
internal actual fun platformEnv(key: String): String? = System.getenv(key)
|
||||
|
||||
internal actual fun platformCreateAgent(baseUrl: String, id: String): Agent =
|
||||
AgentikAgent(id = id, baseUrl = baseUrl)
|
||||
@@ -0,0 +1,13 @@
|
||||
package pw.binom.agentik.tui
|
||||
|
||||
import pw.binom.agentik.proto.Agent
|
||||
|
||||
/**
|
||||
* Заглушка для native-целей: TUI на нативе пока не работает — нужно подключить
|
||||
* ktor-client-* движки и termios. Нативный бинарь собирается, но main() падает
|
||||
* с понятной ошибкой.
|
||||
*/
|
||||
internal actual fun platformEnv(key: String): String? = null
|
||||
|
||||
internal actual fun platformCreateAgent(baseUrl: String, id: String): Agent =
|
||||
error("agentik-tui native target is not implemented yet (baseUrl=$baseUrl)")
|
||||
+80
-14
@@ -7,22 +7,78 @@ plugins {
|
||||
|
||||
group = "pw.binom.agentik"
|
||||
|
||||
// Publication version: -Pversion=<tag> (CICD publishes by release tag).
|
||||
// Без явного -Pversion берётся fallback из gradle.properties или "0.1.0".
|
||||
if (version == "unspecified") {
|
||||
version = providers.gradleProperty("version").getOrElse("0.1.0")
|
||||
}
|
||||
// projectVersion определяется ниже как val, чтобы subprojects могли его
|
||||
// прочитать через rootProject.extra["projectVersion"].
|
||||
|
||||
// Home Nexus URL/creds — Gitea action-variables BINOM_REPO_* (subochev/devops/publish).
|
||||
// Локально для дебага: ./gradlew publish \
|
||||
// -Pbinom.repo.url=http://... -Pbinom.repo.user=... -Pbinom.repo.password=...
|
||||
val binomRepoUrl = (findProperty("binom.repo.url") ?: "http://nexus.xx/repository/caffeine/").toString()
|
||||
// Publication version: -Pversion=<tag> (CICD publishes by release tag) с
|
||||
// fallback в gradle.properties (ключ `agentik.version.default`, не `version`
|
||||
// — иначе Gradle-мерж gradle.properties и -Pversion= отдаёт приоритет
|
||||
// gradle.properties). Без версии maven-publish падает с
|
||||
// "Invalid publication 'kotlinMultiplatform': version cannot be empty" —
|
||||
// это известный gotcha: subprojects читают rootProject.version ДО того, как
|
||||
// if-блок ниже успевает его установить. Фикс: provider+orElse вычисляется
|
||||
// eagerly, и subprojects получают готовую строку.
|
||||
val projectVersion: String = providers.gradleProperty("version")
|
||||
.map { it.trimStart('v', 'V') } // strip optional "v" prefix from tag
|
||||
.getOrElse(providers.gradleProperty("agentik.version.default").orElse("0.1.0-SNAPSHOT").get())
|
||||
version = projectVersion
|
||||
extra["projectVersion"] = projectVersion
|
||||
|
||||
// Home Nexus URL/creds — передаются через -Pbinom.repo.* из CI/CD workflow
|
||||
// (.gitea/workflows/release.yml). Локально для дебага:
|
||||
// ./gradlew publish -Pbinom.repo.url=http://... -Pbinom.repo.user=... -Pbinom.repo.password=...
|
||||
// Без -P URL падает на дефолтный placeholder (заглушка для локальной разработки).
|
||||
val binomRepoUrl = (findProperty("binom.repo.url") as String? ?: "http://nexus.xx/repository/caffeine/").toString()
|
||||
val binomRepoUser = (findProperty("binom.repo.user") as String? ?: "").toString()
|
||||
val binomRepoPassword = (findProperty("binom.repo.password") as String? ?: "").toString()
|
||||
|
||||
// Per-module POM description. Один источник истины — карта ниже,
|
||||
// лишнее в settings.gradle.kts держим в комментарии-зеркале.
|
||||
// При добавлении нового модуля — добавь строку сюда + README.md в его корень.
|
||||
// Кладём в rootProject.extra ДО apply KMP-плагина в subprojects (beforeEvaluate
|
||||
// срабатывает позже, чем apply плагина, поэтому просто положить extra в
|
||||
// beforeEvaluate — поздно).
|
||||
val moduleDescriptions: Map<String, String> = mapOf(
|
||||
"proto" to "agentik :proto — stateful KMP protocol (Agent/Conversation/Message/Event) replacing AG-UI; типы и контракт без сетевой логики.",
|
||||
"skills" to "agentik :skills — парсер opencode-style SKILL.md / *.yaml (YAML-frontmatter + markdown body); загружается в system prompt.",
|
||||
"server" to "agentik :server — Ktor-фасад, экспонирующий Agent по HTTP+JSON+SSE под путём /agentik.",
|
||||
"client" to "agentik :client — Ktor-клиент (HTTP+JSON+SSE), превращающий /agentik в Agent/Conversation из :proto.",
|
||||
"memory-api" to "agentik :memory-api — интерфейсы долговременной памяти (MemoryStore, MemoryCategory, MemoryNote).",
|
||||
"memory-md" to "agentik :memory-md — Hermes-style реализация памяти поверх §-файлов (user/world/preference.md).",
|
||||
"memory-vector" to "agentik :memory-vector — ANN+JVector+SQLite реализация памяти с эмбеддингами (HTTP/SIGLIP).",
|
||||
"storage-core" to "agentik :storage-core — интерфейсы хранилища (MessageStore/WorkingMemoryStore/ConversationStore/ReflectionStore).",
|
||||
"storage-inmemory" to "agentik :storage-inmemory — in-memory реализация всех сторов из :storage-core (для тестов и Android).",
|
||||
"storage-sqlite" to "agentik :storage-sqlite — SQLDelight реализация всех сторов на SQLite (прод-бэкенд).",
|
||||
"agent-toolsets" to "agentik :agent-toolsets — реестр инструментов + диспетчер тулов (enable_toolset/disable_toolset); переиспользуемое ядро.",
|
||||
"agentik-cli" to "agentik :agentik-cli — JVM CLI-клиент (JLine) к /agentik: REPL + slash-команды + стрим SSE.",
|
||||
// "agentik-tui" to "agentik :agentik-tui — Compose-for-Mosaic TUI-клиент (отключён 2026-09-17)."
|
||||
"standalone" to "agentik :standalone — single-jar HTTP-сервер со всеми транспортами (AG-UI/A2A/:proto), SQLite, памятью, скилами и SOUL.",
|
||||
)
|
||||
rootProject.extra.set("moduleDescriptions", moduleDescriptions)
|
||||
|
||||
subprojects {
|
||||
group = rootProject.group
|
||||
version = rootProject.version
|
||||
|
||||
// KMP-плагин читает project.version на ранней стадии evaluation — ДО того
|
||||
// как сработает внешний subprojects-блок. Если version ещё "unspecified",
|
||||
// publication 'kotlinMultiplatform' создаётся с пустой version, и тогда
|
||||
// maven-publish падает с 'InvalidMavenPublicationException: version cannot
|
||||
// be empty'. Поэтому:
|
||||
// 1) eagerly переопределяем version в rootProject.extra (см. выше)
|
||||
// 2) на КАЖДЫЙ subproject вешаем beforeEvaluate, который выставляет
|
||||
// version до того, как KMP-плагин начнёт создавать publications.
|
||||
|
||||
// per-module POM description берётся из rootProject.extra["moduleDescriptions"]
|
||||
// (см. корень build.gradle.kts); добавлять новый модуль — туда + README.md.
|
||||
|
||||
// beforeEvaluate срабатывает ДО apply плагинов в build.gradle.kts модуля, так
|
||||
// что version/description уже валидны, когда KMP-плагин начинает создавать
|
||||
// publications.
|
||||
beforeEvaluate {
|
||||
description = (rootProject.extra["moduleDescriptions"] as Map<String, String>)[project.name]
|
||||
?: "agentik module: ${project.name}"
|
||||
version = rootProject.extra["projectVersion"] as String
|
||||
}
|
||||
|
||||
apply(plugin = "maven-publish")
|
||||
|
||||
@@ -40,13 +96,23 @@ subprojects {
|
||||
}
|
||||
|
||||
// Per-subproject POM-метаданные (name, scm, licenses, developers).
|
||||
// Также явно выставляем version/group для каждой публикации. В KMP-модулях
|
||||
// (особенно JVM-only с одним jvm() target) kotlin-multiplatform plugin
|
||||
// создаёт publication 'kotlinMultiplatform' на ранней стадии evaluation,
|
||||
// когда project.version ещё 'unspecified'. Простое присваивание
|
||||
// subprojects { version = ... } НЕ перезаписывает уже зафиксированную
|
||||
// version в publication → InvalidMavenPublicationException в CI.
|
||||
// Явная установка version здесь гарантирует, что публикация всегда
|
||||
// использует актуальное значение из rootProject.extra.
|
||||
publications.withType<MavenPublication>().configureEach {
|
||||
groupId = rootProject.group.toString()
|
||||
artifactId = project.name
|
||||
version = rootProject.extra["projectVersion"] as String
|
||||
|
||||
pom {
|
||||
name = project.name
|
||||
description = providers.provider {
|
||||
project.findProperty("description")?.toString()
|
||||
?: "agentik: ${project.name} (pw.binom.agentik)"
|
||||
}
|
||||
description = (rootProject.extra["moduleDescriptions"] as? Map<String, String>)?.get(project.name)
|
||||
?: "agentik module: ${project.name}"
|
||||
url = "https://git.binom.pw/subochev/agentik"
|
||||
|
||||
licenses {
|
||||
|
||||
@@ -0,0 +1,331 @@
|
||||
# `:client` — Ktor-клиент к `:server` (KMP, jvm + native)
|
||||
|
||||
Тонкий HTTP-клиент к `:server`-фасаду + локальные примитивы, чтобы
|
||||
собирать свои клиенты (UI, CLI, parent-агенты, A2A-bridge) без бойлерплейта
|
||||
про HTTP, JSON, SSE и lifecycle `Conversation`.
|
||||
|
||||
## Что есть
|
||||
|
||||
- `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).
|
||||
|
||||
`Agent` — `AutoCloseable`; `agent.close()` закрывает HttpClient. Не нужно
|
||||
вручную создавать `HttpClient` и накатывать на него JSON/Bearer-плагины.
|
||||
|
||||
## Подключение
|
||||
|
||||
```kotlin
|
||||
// build.gradle.kts
|
||||
dependencies {
|
||||
api("pw.binom.agentik:client:0.1.0")
|
||||
// Движок — на твой выбор (один из):
|
||||
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 минут
|
||||
|
||||
Один self-contained пример: создаём агента, открываем диалог,
|
||||
отправляем сообщение, печатаем streaming-ответ.
|
||||
|
||||
```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
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlin.time.Clock
|
||||
|
||||
fun main() = runBlocking {
|
||||
// 1. Agent — обёртка над :server фасадом. HttpClient создаётся внутри.
|
||||
val agent = AgentikAgent(
|
||||
id = "my-client",
|
||||
baseUrl = "http://localhost:8080/agentik",
|
||||
engineFactory = CIO,
|
||||
token = "s3cret", // или null, если не нужен
|
||||
)
|
||||
|
||||
// 2. Открыть диалог, отправить сообщение.
|
||||
val conv = agent.createConversation(temp = false)
|
||||
conv.send(listOf(Content.Text("Привет")))
|
||||
|
||||
// 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 после разрыва,
|
||||
401/404.
|
||||
|
||||
## Известное ограничение
|
||||
|
||||
SSE event-stream в не-TTY ssh-сессии (без `-tt`) закрывается на
|
||||
default-таймауте Ktor. Используйте либо `ssh -tt`, либо нативный
|
||||
terminal (TTY). Это upstream-особенность Ktor SSE.
|
||||
+45
-18
@@ -1,25 +1,52 @@
|
||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.kotlin.jvm)
|
||||
alias(libs.plugins.kotlin.multiplatform)
|
||||
alias(libs.plugins.kotlin.serialization)
|
||||
}
|
||||
|
||||
kotlin {
|
||||
compilerOptions {
|
||||
jvmTarget.set(JvmTarget.JVM_21)
|
||||
jvmToolchain(21)
|
||||
|
||||
// Только то, что нам реально нужно: JVM + 5 desktop-native. iOS не входит —
|
||||
// :client не имеет смысла на iOS, а :agentik-cli использует :client и тоже
|
||||
// без iOS. См. agentik-cli/build.gradle.kts.
|
||||
jvm()
|
||||
listOf(
|
||||
macosX64(),
|
||||
macosArm64(),
|
||||
linuxX64(),
|
||||
linuxArm64(),
|
||||
mingwX64(),
|
||||
)
|
||||
|
||||
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)
|
||||
implementation(libs.ktor.serialization.kotlinx.json)
|
||||
|
||||
implementation(libs.kotlinx.coroutines.core)
|
||||
implementation(libs.kotlinx.serialization.core)
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
}
|
||||
commonTest.dependencies {
|
||||
implementation(libs.kotlin.test)
|
||||
implementation(libs.kotlinx.coroutines.test)
|
||||
implementation(libs.ktor.server.core)
|
||||
implementation(libs.ktor.server.test.host)
|
||||
implementation(libs.ktor.client.content.negotiation)
|
||||
implementation(libs.ktor.client.cio)
|
||||
implementation(libs.ktor.server.cio)
|
||||
implementation(libs.ktor.server.sse)
|
||||
}
|
||||
jvmTest.dependencies {
|
||||
implementation("junit:junit:4.13.2")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(project(":proto"))
|
||||
|
||||
implementation(libs.ktor.client.core)
|
||||
implementation(libs.ktor.client.cio)
|
||||
implementation(libs.ktor.client.content.negotiation)
|
||||
implementation(libs.ktor.serialization.kotlinx.json)
|
||||
|
||||
implementation(libs.kotlinx.coroutines.core)
|
||||
implementation(libs.kotlinx.serialization.core)
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
|
||||
// :client — это библиотека, не executable. Native-бинари объявляются
|
||||
// в :agentik-cli (он зависит от :client и реально предоставляет main).
|
||||
}
|
||||
|
||||
+15
-22
@@ -7,35 +7,35 @@ import io.ktor.client.request.get
|
||||
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") {
|
||||
@@ -53,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
|
||||
}
|
||||
|
||||
@@ -65,14 +65,7 @@ internal class AgentClient(
|
||||
return snapshots.map { ConversationClient(httpClient, agentUrl, it) }
|
||||
}
|
||||
|
||||
override fun events(after: Instant): Flow<AgentEvent> = flow {
|
||||
val response = httpClient.get("$agentUrl/events?after=$after")
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package pw.binom.agentik.client
|
||||
|
||||
import io.ktor.client.engine.HttpClientEngineFactory
|
||||
import pw.binom.agentik.proto.Agent
|
||||
|
||||
/**
|
||||
* Создаёт [Agent], который ходит в HTTP-фасад `agentikAgent` (модуль `:server`).
|
||||
*
|
||||
* Принимает [engineFactory] — `HttpClientEngineFactory<*>` (`CIO`, `OkHttp`,
|
||||
* `Darwin`, ...). Внутри сам создаёт `HttpClient`, накатывает JSON-конфиг
|
||||
* [agentikJson] и опциональный Bearer [token]. Никакого `applyAgentikDefaults`
|
||||
* снаружи — всё под капотом.
|
||||
*
|
||||
* ```
|
||||
* val agent = AgentikAgent(
|
||||
* id = "my-client",
|
||||
* baseUrl = "http://localhost:8080/agentik",
|
||||
* engineFactory = CIO,
|
||||
* token = "s3cret",
|
||||
* )
|
||||
* val conv = agent.createConversation(temp = false)
|
||||
* conv.send(listOf(Content.Text("hi")))
|
||||
* conv.events(Instant.DISTANT_PAST).collect { ... }
|
||||
* agent.close() // закрывает HttpClient
|
||||
* ```
|
||||
*
|
||||
* [id] пробрасывается в `Agent.id` — сервер про идентичность агента не знает,
|
||||
* поэтому клиент должен её знать сам (или взять из конфига).
|
||||
*
|
||||
* **Lifecycle**: [Agent] — `AutoCloseable`. `agent.close()` закрывает
|
||||
* HttpClient (идемпотентно). После этого `createConversation` /
|
||||
* `getConversation` etc. не определены.
|
||||
*/
|
||||
fun AgentikAgent(
|
||||
id: String,
|
||||
baseUrl: String,
|
||||
engineFactory: HttpClientEngineFactory<*>,
|
||||
token: String? = null,
|
||||
): Agent = AgentClient(
|
||||
id = id,
|
||||
baseUrl = baseUrl,
|
||||
httpClient = agentikHttpClient(engineFactory = engineFactory, token = token),
|
||||
)
|
||||
+13
-7
@@ -6,6 +6,7 @@ import io.ktor.client.request.get
|
||||
import io.ktor.client.request.parameter
|
||||
import io.ktor.client.request.patch
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.client.request.prepareGet
|
||||
import io.ktor.client.request.setBody
|
||||
import io.ktor.client.statement.bodyAsChannel
|
||||
import io.ktor.http.ContentType
|
||||
@@ -72,13 +73,18 @@ internal class ConversationClient(
|
||||
}
|
||||
|
||||
override fun events(after: Instant): Flow<Event> = flow {
|
||||
val response = httpClient.get("$convUrl/events?after=$after")
|
||||
check(response.status == HttpStatusCode.OK) {
|
||||
"events: server returned ${response.status}"
|
||||
}
|
||||
readSse(response.bodyAsChannel())
|
||||
.collect { payload ->
|
||||
emit(agentikJson.decodeFromString(Event.serializer(), payload))
|
||||
// prepareGet + execute (а не get) обязателен: `get` дожидается полного
|
||||
// тела ответа, а SSE-поток не заканчивается никогда — вызов висел бы
|
||||
// вечно. `execute` отдаёт HttpResponse со стриминговым bodyAsChannel.
|
||||
httpClient.prepareGet("$convUrl/events?after=$after") { noSseReadTimeout() }
|
||||
.execute { response ->
|
||||
check(response.status == HttpStatusCode.OK) {
|
||||
"events: server returned ${response.status}"
|
||||
}
|
||||
readSse(response.bodyAsChannel())
|
||||
.collect { payload ->
|
||||
emit(agentikJson.decodeFromString(Event.serializer(), payload))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package pw.binom.agentik.client
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.HttpClientEngineFactory
|
||||
import io.ktor.client.plugins.DefaultRequest
|
||||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
|
||||
/**
|
||||
* Создаёт [HttpClient] поверх [engineFactory] с конфигурацией agentik.
|
||||
*
|
||||
* Внутренний helper для [AgentikAgent]. Потребителю `:client` обычно
|
||||
* не нужен — он передаёт engine в [AgentikAgent] и получает готовый
|
||||
* [pw.binom.agentik.proto.Agent] с уже закрытым HttpClient'ом
|
||||
* на [pw.binom.agentik.proto.Agent.close].
|
||||
*
|
||||
* Экспортируется для случаев, когда нужен прямой доступ к `HttpClient`
|
||||
* (например, дополнительные нестандартные запросы в обход `Agent` API).
|
||||
*/
|
||||
fun agentikHttpClient(
|
||||
engineFactory: HttpClientEngineFactory<*>,
|
||||
token: String? = null,
|
||||
): HttpClient = HttpClient(engineFactory) {
|
||||
install(ContentNegotiation) { json(agentikJson) }
|
||||
if (token != null) {
|
||||
install(DefaultRequest) {
|
||||
header(HttpHeaders.Authorization, "Bearer $token")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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).
|
||||
}
|
||||
}
|
||||
+4
-1
@@ -15,8 +15,11 @@ import kotlin.time.Instant
|
||||
* wire-формат компактный, альтернатива — отдельный `:wire`-модуль ради 10 строк.
|
||||
*/
|
||||
internal object InstantSerializer : KSerializer<Instant> {
|
||||
// Имя дескриптора обязано совпадать с тем, что регистрирует :server — иначе
|
||||
// kotlinx-serialization 1.6+ выбросит «there already exists» при попытке загрузить
|
||||
// оба варианта (нативный сериализатор Instant + наш custom) в одном процессе.
|
||||
override val descriptor: SerialDescriptor =
|
||||
PrimitiveSerialDescriptor("kotlin.time.Instant", PrimitiveKind.STRING)
|
||||
PrimitiveSerialDescriptor("pw.binom.agentik.Instant", PrimitiveKind.STRING)
|
||||
|
||||
override fun serialize(encoder: Encoder, value: Instant) =
|
||||
encoder.encodeString(value.toString())
|
||||
@@ -0,0 +1,31 @@
|
||||
package pw.binom.agentik.client
|
||||
|
||||
import io.ktor.client.plugins.HttpTimeoutConfig
|
||||
import io.ktor.client.plugins.HttpTimeoutCapability
|
||||
import io.ktor.client.request.HttpRequestBuilder
|
||||
|
||||
/**
|
||||
* Отключает request/connect/socket-таймауты для конкретного запроса через
|
||||
* [HttpTimeoutCapability] со всеми таймаутами = [HttpTimeoutConfig.INFINITE_TIMEOUT_MS].
|
||||
*
|
||||
* Зачем: наш SSE-ридер ([readSse]) читает `bodyAsChannel()` руками и не
|
||||
* использует плагин `SSE`, поэтому движок не считает запрос SSE-шным
|
||||
* (`HttpRequestBuilder.supportsRequestTimeout` проверяет
|
||||
* `body is SSEClientContent`, а у нас тело — обычный GET без тела).
|
||||
* Без capability встроенный `HttpTimeoutPlugin.requestTimeoutMillis` (по умолчанию
|
||||
* **15000 мс**) молча убивает долгий idle-стрим через 15 секунд.
|
||||
*
|
||||
* Конфиг создаётся заново на каждый вызов — плагин `HttpTimeout` при
|
||||
* установленном capability мутирует его поля через `?:`, так что шаренный
|
||||
* инстанс мог бы утечь между запросами.
|
||||
*/
|
||||
internal fun HttpRequestBuilder.noSseReadTimeout() {
|
||||
setCapability(
|
||||
HttpTimeoutCapability,
|
||||
HttpTimeoutConfig(
|
||||
requestTimeoutMillis = HttpTimeoutConfig.INFINITE_TIMEOUT_MS,
|
||||
connectTimeoutMillis = HttpTimeoutConfig.INFINITE_TIMEOUT_MS,
|
||||
socketTimeoutMillis = HttpTimeoutConfig.INFINITE_TIMEOUT_MS,
|
||||
),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package pw.binom.agentik.client
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.statement.bodyAsText
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.server.application.call
|
||||
import io.ktor.server.application.createRouteScopedPlugin
|
||||
import io.ktor.server.cio.CIO as ServerCIO
|
||||
import io.ktor.server.engine.EmbeddedServer
|
||||
import io.ktor.server.engine.embeddedServer
|
||||
import io.ktor.server.response.respondText
|
||||
import io.ktor.server.routing.get
|
||||
import io.ktor.server.routing.route
|
||||
import io.ktor.server.routing.routing
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
/**
|
||||
* Тесты клиентской части: [agentikHttpClient] с заданным `token` прикладывает
|
||||
* `Authorization: Bearer <token>` ко всем запросам через плагин `DefaultRequest`,
|
||||
* без токена — заголовок не отправляется.
|
||||
*
|
||||
* Сервер в тесте — локальный ktor-CIO с inline route-scoped Bearer-плагином (один в один
|
||||
* как боевой [pw.binom.agentik.server.BearerTokenPlugin]). Тестовый `:server` не зависит
|
||||
* от `:client`, поэтому боевой плагин тут переиспользовать нельзя — пересоздаём его
|
||||
* минимально, контракт тот же.
|
||||
*/
|
||||
class BearerHeaderTest {
|
||||
|
||||
private val TestBearer = createRouteScopedPlugin(
|
||||
name = "TestBearer",
|
||||
createConfiguration = ::BearerCfg,
|
||||
) {
|
||||
val expected = pluginConfig.token
|
||||
onCall { call ->
|
||||
if (expected == null) return@onCall
|
||||
if (call.request.headers[HttpHeaders.Authorization] != "Bearer $expected") {
|
||||
call.respondText("Unauthorized", ContentType.Text.Plain, HttpStatusCode.Unauthorized)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class BearerCfg {
|
||||
var token: String? = null
|
||||
}
|
||||
|
||||
private fun clientWith(token: String?): HttpClient =
|
||||
agentikHttpClient(engineFactory = CIO, token = token)
|
||||
|
||||
private suspend fun startServer(): Pair<EmbeddedServer<*, *>, Int> {
|
||||
val server = embeddedServer(ServerCIO, port = 0) {
|
||||
routing {
|
||||
route("/agentik") {
|
||||
install(TestBearer) { token = "secret" }
|
||||
get("/conversations") {
|
||||
call.respondText("[]")
|
||||
}
|
||||
}
|
||||
}
|
||||
}.start(wait = false)
|
||||
val port = server.engine.resolvedConnectors().first().port
|
||||
return server to port
|
||||
}
|
||||
|
||||
@Test
|
||||
fun clientWithTokenAttachesBearerHeader() = runBlocking {
|
||||
val (server, port) = startServer()
|
||||
try {
|
||||
val client = clientWith("secret")
|
||||
val resp = client.get("http://127.0.0.1:$port/agentik/conversations")
|
||||
assertEquals(HttpStatusCode.OK, resp.status)
|
||||
assertEquals("[]", resp.bodyAsText())
|
||||
} finally {
|
||||
server.stop(100, 200)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun clientWithoutTokenGets401(): Unit = runBlocking {
|
||||
val (server, port) = startServer()
|
||||
try {
|
||||
val client = clientWith(null)
|
||||
val resp = client.get("http://127.0.0.1:$port/agentik/conversations")
|
||||
assertEquals(HttpStatusCode.Unauthorized, resp.status)
|
||||
} finally {
|
||||
server.stop(100, 200)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun clientWithWrongTokenGets401(): Unit = runBlocking {
|
||||
val (server, port) = startServer()
|
||||
try {
|
||||
val client = clientWith("wrong")
|
||||
val resp = client.get("http://127.0.0.1:$port/agentik/conversations")
|
||||
assertEquals(HttpStatusCode.Unauthorized, resp.status)
|
||||
} finally {
|
||||
server.stop(100, 200)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package pw.binom.agentik.client
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
import io.ktor.client.plugins.HttpRequestTimeoutException
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.client.request.prepareGet
|
||||
import io.ktor.client.statement.bodyAsChannel
|
||||
import io.ktor.server.application.call
|
||||
import io.ktor.server.engine.embeddedServer
|
||||
import io.ktor.server.response.respondBytesWriter
|
||||
import io.ktor.server.routing.get
|
||||
import io.ktor.server.routing.routing
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.utils.io.writeStringUtf8
|
||||
import io.ktor.utils.io.readUTF8Line
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import java.net.ServerSocket
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.test.fail
|
||||
|
||||
/**
|
||||
* Репродукция бага Ktor CIO: дефолтный [io.ktor.client.engine.cio.CIOEngineConfig.requestTimeout]
|
||||
* = 15 с убивает SSE read. Наш fix — [noSseReadTimeout] ставит capability
|
||||
* [io.ktor.client.plugins.HttpTimeoutCapability] со всеми таймаутами = INFINITE
|
||||
* перед каждым read-стримом.
|
||||
*
|
||||
* Тест запускает встроенный Ktor CIO-сервер на свободном порту. Сервер шлёт
|
||||
* "hello", ждёт 20 с (дольше дефолтного requestTimeout = 15 с), затем шлёт
|
||||
* "done". Без capability клиент отвалился бы на ~15 с; с capability — второе
|
||||
* сообщение доходит.
|
||||
*
|
||||
* Читаем строки пока не найдём "data: done" или пока не сработает
|
||||
* [withTimeout] (18 с — запас над server delay 20 с).
|
||||
*/
|
||||
class SseTimeoutTest {
|
||||
|
||||
private fun freePort(): Int = ServerSocket(0).use { it.localPort }
|
||||
|
||||
@Test
|
||||
fun `sse read survives past default cio timeout with noSseReadTimeout`(): Unit = runBlocking {
|
||||
val port = freePort()
|
||||
val server = embeddedServer(io.ktor.server.cio.CIO, port = port) {
|
||||
routing {
|
||||
get("/sse") {
|
||||
call.respondBytesWriter(contentType = ContentType.Text.EventStream) {
|
||||
writeStringUtf8("data: hello\n\n")
|
||||
flush()
|
||||
// 17 с — чуть больше дефолтного CIO requestTimeout = 15 с.
|
||||
// Если capability сломана, клиент упадёт на 15 с и не получит "done".
|
||||
delay(17_000)
|
||||
writeStringUtf8("data: done\n\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
}.start(wait = false)
|
||||
|
||||
try {
|
||||
val client = HttpClient(CIO)
|
||||
val received = mutableListOf<String>()
|
||||
client.prepareGet("http://127.0.0.1:$port/sse") {
|
||||
header("Accept", "text/event-stream")
|
||||
noSseReadTimeout()
|
||||
}.execute { resp ->
|
||||
val ch = resp.bodyAsChannel()
|
||||
// 19 с запас: ждём, пока сервер пошлёт "done" после 17 с.
|
||||
// Если capability сломана, клиент упадёт на 15 с и мы словим исключение.
|
||||
val deadline = 19_000L
|
||||
val start = System.currentTimeMillis()
|
||||
while (System.currentTimeMillis() - start < deadline) {
|
||||
val line = withTimeout<String?>(deadline) { ch.readUTF8Line() } ?: break
|
||||
if (line.startsWith("data: ")) {
|
||||
received.add(line)
|
||||
}
|
||||
if (line == "data: done") break
|
||||
}
|
||||
}
|
||||
assertTrue(received.contains("data: hello"), "должно получить hello: $received")
|
||||
assertTrue(
|
||||
received.contains("data: done"),
|
||||
"должно получить done (SSE read не должен падать на 15 с): $received",
|
||||
)
|
||||
assertFalse(
|
||||
received.any { it == "<timeout>" },
|
||||
"SSE read упал в timeout (capability не сработал): $received",
|
||||
)
|
||||
} finally {
|
||||
server.stop(100, 200)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Контр-тест: убеждаемся что БЕЗ [noSseReadTimeout] дефолтный
|
||||
* CIO requestTimeout = 15 с действительно убивает SSE-стрим.
|
||||
* Сервер держит stream 17 с; если клиент не выставил capability —
|
||||
* мы должны получить [HttpRequestTimeoutException] на ~15 с, не
|
||||
* дожидаясь "done".
|
||||
*/
|
||||
@Test
|
||||
fun `without noSseReadTimeout default cio requestTimeout kills the stream`(): Unit = runBlocking {
|
||||
val port = freePort()
|
||||
val server = embeddedServer(io.ktor.server.cio.CIO, port = port) {
|
||||
routing {
|
||||
get("/sse") {
|
||||
call.respondBytesWriter(contentType = ContentType.Text.EventStream) {
|
||||
writeStringUtf8("data: hello\n\n")
|
||||
flush()
|
||||
delay(17_000)
|
||||
writeStringUtf8("data: done\n\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
}.start(wait = false)
|
||||
|
||||
try {
|
||||
val client = HttpClient(CIO)
|
||||
val start = System.currentTimeMillis()
|
||||
try {
|
||||
client.prepareGet("http://127.0.0.1:$port/sse") {
|
||||
header("Accept", "text/event-stream")
|
||||
// НАМЕРЕННО без noSseReadTimeout.
|
||||
}.execute { resp ->
|
||||
val ch = resp.bodyAsChannel()
|
||||
// Читаем строки, пока не придёт "data: done" — без capability
|
||||
// клиент упадёт на ~15 с до того, как сервер пошлёт done.
|
||||
while (true) {
|
||||
val line = ch.readUTF8Line() ?: break
|
||||
if (line == "data: done") break
|
||||
}
|
||||
}
|
||||
fail("без capability клиент должен словить HttpRequestTimeoutException")
|
||||
} catch (e: HttpRequestTimeoutException) {
|
||||
val elapsed = System.currentTimeMillis() - start
|
||||
assertTrue(
|
||||
elapsed in 14_000..17_000,
|
||||
"timeout должен сработать в районе 15 с (default), elapsed=$elapsed",
|
||||
)
|
||||
}
|
||||
} finally {
|
||||
server.stop(100, 200)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package pw.binom.agentik.client
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
import pw.binom.agentik.proto.Agent
|
||||
|
||||
/**
|
||||
* Создаёт [Agent], который под капотом ходит в HTTP-фасад `agentikAgent`
|
||||
* (модуль `:server`).
|
||||
*
|
||||
* ```
|
||||
* val client = AgentikAgent(
|
||||
* id = "my-agent",
|
||||
* baseUrl = "http://localhost:8080/agentik",
|
||||
* )
|
||||
* val conv = client.createConversation(temp = false)
|
||||
* conv.send(listOf(Content.Text("hi")))
|
||||
* conv.events(Instant.DISTANT_PAST).collect { ev -> ... }
|
||||
* ```
|
||||
*
|
||||
* [id] пробрасывается в реализацию [Agent.id] — сервер про идентичность
|
||||
* агента не знает, поэтому клиент должен её знать сам (или взять из
|
||||
* конфига).
|
||||
*
|
||||
* [httpClient] по умолчанию — [defaultAgentikHttpClient] (CIO + JSON +
|
||||
* SSE). Можно передать свой, если нужен свой engine/логирование/аутентификация.
|
||||
*/
|
||||
fun AgentikAgent(
|
||||
id: String,
|
||||
baseUrl: String,
|
||||
httpClient: HttpClient = defaultAgentikHttpClient(),
|
||||
): Agent = AgentClient(httpClient = httpClient, baseUrl = baseUrl, id = id)
|
||||
|
||||
/**
|
||||
* Дефолтный [HttpClient] для общения с `agentikAgent`: CIO-движок и
|
||||
* kotlinx-serialization с тем же wire-форматом, что на сервере. SSE-парсер
|
||||
* (см. [readSse]) живёт в общем коде и плагина не требует.
|
||||
*/
|
||||
fun defaultAgentikHttpClient(): HttpClient = HttpClient(CIO) {
|
||||
install(ContentNegotiation) { json(agentikJson) }
|
||||
}
|
||||
@@ -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)
|
||||
+33
-11
@@ -1,15 +1,18 @@
|
||||
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 диалога: ровно то, что агент сейчас видит в
|
||||
* LLM-контексте. Упорядочено по `order_idx` (заполняется в store при append).
|
||||
*
|
||||
* Sealed-иерархия: для v1 — `System` (синтетический system-prompt),
|
||||
* `User`/`Assistant` (реплики с ссылкой на audit log через [sourceMessageId]).
|
||||
* Суммаризация (для v2) добавит подтип `Summary`.
|
||||
* Sealed-иерархия: `User`/`Assistant` (реплики с ссылкой на audit log
|
||||
* через [sourceMessageId]), `ToolExchange` (синтетическая запись об одном
|
||||
* tool-вызове + его результате — для replay в LiteMessage(TOOL, ToolResult)
|
||||
* при пересоздании LiteConv), `Summary` (суммаризация при compaction).
|
||||
*/
|
||||
@Serializable
|
||||
sealed interface WorkingMemoryEntry {
|
||||
@@ -17,13 +20,6 @@ sealed interface WorkingMemoryEntry {
|
||||
/** Ссылка на исходное сообщение в audit log (`message.id`). `null` для синтетических строк. */
|
||||
val sourceMessageId: String?
|
||||
|
||||
/** Синтетический system-prompt, добавляется при создании диалога. */
|
||||
@Serializable
|
||||
@SerialName("system")
|
||||
data class System(val text: String) : WorkingMemoryEntry {
|
||||
override val sourceMessageId: String? = null
|
||||
}
|
||||
|
||||
/** Реплика пользователя. */
|
||||
@Serializable
|
||||
@SerialName("user")
|
||||
@@ -47,6 +43,32 @@ sealed interface WorkingMemoryEntry {
|
||||
val content: List<Content>,
|
||||
) : WorkingMemoryEntry
|
||||
|
||||
/**
|
||||
* Синтетический блок: один tool-вызов + его результат. Синтетический — потому
|
||||
* что в audit log это две отдельные записи (`MessageRecord.ToolCall` +
|
||||
* `MessageRecord.ToolResult`), а в working_memory мы храним одной строкой
|
||||
* для удобства replay'а.
|
||||
*
|
||||
* При создании новой LiteConv каждая такая запись превращается в
|
||||
* `LiteMessage(TOOL, [ToolResult(callId, name, response)])` — LiteRT-LM
|
||||
* матчит по `name`, `callId` берётся из [sourceMessageId] (= id исходного
|
||||
* [MessageRecord.ToolCall]). Если [wasCancelled] = true, [resultText]
|
||||
* содержит маркер `[cancelled by user]` — модель видит честную причину
|
||||
* отсутствия результата.
|
||||
*
|
||||
* [sourceMessageId] = id исходного [MessageRecord.ToolCall] (для трассировки
|
||||
* в audit log).
|
||||
*/
|
||||
@Serializable
|
||||
@SerialName("tool_exchange")
|
||||
data class ToolExchange(
|
||||
override val sourceMessageId: String,
|
||||
val toolName: String,
|
||||
val toolArgsJson: String,
|
||||
val resultText: String,
|
||||
val wasCancelled: Boolean = false,
|
||||
) : WorkingMemoryEntry
|
||||
|
||||
/**
|
||||
* Синтетический блок: суммаризация старых ходов, сгенерированная при
|
||||
* compaction'е 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,158 @@
|
||||
# 01 — Слои модулей (целевое состояние)
|
||||
|
||||
Целевая модульная структура agentik. Снизу вверх:
|
||||
**приложения → runtime → домен → абстракции → платформенные impl**.
|
||||
|
||||

|
||||
|
||||
PlantUML source (для редактирования; требует Graphviz `dot` для рендеринга):
|
||||
|
||||
```plantuml
|
||||
@startuml agentik-module-layers
|
||||
skinparam componentStyle rectangle
|
||||
skinparam ranksep 60
|
||||
skinparam nodesep 30
|
||||
skinparam packageStyle rectangle
|
||||
|
||||
title agentik — слои модулей (целевое состояние)
|
||||
|
||||
' --- Applications: entry points (thin wrappers) ---
|
||||
package "Applications\n(entry points, тонкие)" {
|
||||
[Standalone\nHTTP+AG-UI+A2A] as Standalone
|
||||
[AgentikCli\nREPL] as Cli
|
||||
[AgentikAndroid\nCompose UI] as Android
|
||||
}
|
||||
|
||||
' --- Agent runtime ---
|
||||
package "Agent Runtime\n(композиция, lifecycle)" {
|
||||
[AgentCore\nBaseAgent] as AgentCore
|
||||
[AgentBuilder\nDSL] as Builder
|
||||
}
|
||||
|
||||
' --- Background work ---
|
||||
package "Background Work\n(event-driven triggers)" {
|
||||
[BackgroundEvents\nbus + events] as Ev
|
||||
[BackgroundScheduler\npolicy] as Sched
|
||||
}
|
||||
|
||||
' --- Domain logic (generic, переиспользуется) ---
|
||||
package "Domain Logic\n(generic tools)" {
|
||||
[LlmTools\nReflector/Reviewer/Miner] as LlmT
|
||||
[McpBridge\nMCP-SDK → LiteTool] as Mcp
|
||||
[Skills\nparse + store] as Skills
|
||||
}
|
||||
|
||||
' --- Storage abstractions + impls ---
|
||||
package "Storage\n(abstractions)" as StoragePkg {
|
||||
[StorageCore\ninterfaces] as StorageCore
|
||||
}
|
||||
|
||||
package "Storage\n(JVM impls)" {
|
||||
[StorageSqlite\nJDBC] as StorageSql
|
||||
[StorageInmemory\ntests] as StorageInmem
|
||||
}
|
||||
|
||||
package "Storage\n(Android impl)" {
|
||||
[StorageSqliteAndroid\nRoom/sqlite] as StorageSqlA
|
||||
}
|
||||
|
||||
' --- Memory backends ---
|
||||
package "Memory\n(abstractions)" {
|
||||
[MemoryApi\nMemorySystem/MemoryTools] as MemApi
|
||||
}
|
||||
|
||||
package "Memory\n(impls)" {
|
||||
[MemoryMd\nHermes §-files] as MemMd
|
||||
[MemoryVector\nJVector+JVM] as MemVec
|
||||
[MemoryVectorAndroid\nONNX+ANN] as MemVecA
|
||||
}
|
||||
|
||||
' --- LLM backends ---
|
||||
package "LLM\n(abstractions)" {
|
||||
[LitertApi\nLiteLlm контракт] as Litert
|
||||
}
|
||||
|
||||
package "LLM\n(impls)" {
|
||||
[LitertOpenai\nHTTP] as LitertO
|
||||
[LitertGoogle\nLiteRT JVM] as LitertG
|
||||
[LitertAndroid\nLiteRT Android] as LitertA
|
||||
}
|
||||
|
||||
' --- Inter-app protocol ---
|
||||
package "Inter-app" {
|
||||
[Proto\nAgent/Conversation] as Proto
|
||||
[A2AServer] as A2A
|
||||
}
|
||||
|
||||
' --- Зависимости (приложения → runtime → домен → абстракции → платформенные импл) ---
|
||||
Standalone ..> Builder
|
||||
Cli ..> Builder
|
||||
Android ..> Builder
|
||||
|
||||
Builder ..> AgentCore
|
||||
AgentCore ..> Proto
|
||||
AgentCore ..> StorageCore
|
||||
AgentCore ..> MemApi
|
||||
AgentCore ..> Litert
|
||||
AgentCore ..> Mcp
|
||||
AgentCore ..> Skills
|
||||
|
||||
Sched ..> Ev
|
||||
AgentCore ..> Sched
|
||||
AgentCore ..> Ev
|
||||
|
||||
Mcp ..> Litert
|
||||
LlmT ..> Litert
|
||||
|
||||
MemMd ..> MemApi
|
||||
MemVec ..> MemApi
|
||||
MemVecA ..> MemApi
|
||||
|
||||
StorageSql ..> StorageCore
|
||||
StorageInmem ..> StorageCore
|
||||
StorageSqlA ..> StorageCore
|
||||
|
||||
LitertO ..> Litert
|
||||
LitertG ..> Litert
|
||||
LitertA ..> Litert
|
||||
|
||||
Standalone ..> A2A
|
||||
Standalone ..> LitertO
|
||||
Standalone ..> LitertG
|
||||
Standalone ..> StorageSql
|
||||
Standalone ..> MemMd
|
||||
Standalone ..> MemVec
|
||||
Standalone ..> Mcp
|
||||
|
||||
Android ..> LitertA
|
||||
Android ..> StorageSqlA
|
||||
Android ..> MemMd
|
||||
Android ..> MemVecA
|
||||
|
||||
@enduml
|
||||
```
|
||||
|
||||
## Что показывает
|
||||
|
||||
- **Applications** — три точки входа: web-сервер, CLI REPL, Android-приложение. Каждое тонкое, не содержит бизнес-логики.
|
||||
- **Agent Runtime** — `BaseAgent` + `AgentBuilder` DSL. Вся композиция и lifecycle.
|
||||
- **Background Work** — `BackgroundEvents` (event-bus) + `BackgroundScheduler` (policy подписки). Event-driven, не interval-polling.
|
||||
- **Domain Logic** — generic переиспользуемые модули (`:llm-tools`, `:mcp-bridge`, `:skills`).
|
||||
- **Storage / Memory / LLM** — каждая с абстракцией и одним или несколькими impl (JVM-only или Android-only).
|
||||
- **Inter-app** — `:proto` контракты + `:a2a-server` для межагентного общения.
|
||||
|
||||
## Текущее состояние vs целевое
|
||||
|
||||
✅ Уже сделано (в этом цикле правок):
|
||||
- `:llm-tools` extracted
|
||||
- `:mcp-bridge` extracted
|
||||
- `BackgroundScheduler` стал event-driven
|
||||
- `ConversationLoop` стал отдельным компонентом (typealias `ChatConversation`)
|
||||
|
||||
⏳ Не сделано:
|
||||
- `:agent-core` (выделить `BaseAgent` + builder в отдельный KMP-модуль)
|
||||
- `:background-events` (выделить events + scheduler — пока в `:standalone`)
|
||||
- `:storage-sqlite-android`
|
||||
- `:memory-vector-android`
|
||||
- `:litert-android`
|
||||
- `:agentik-android` (само приложение)
|
||||
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 38 KiB |
@@ -0,0 +1,127 @@
|
||||
# 02 — Agent Builder: композиция (целевое API)
|
||||
|
||||
Как `AgentBuilder` собирает `BaseAgent` из компонентов. **Memory backend сам объявляет свои tools** — builder их авто-мержит. BackgroundScheduler подписан на события, не interval-poll.
|
||||
|
||||

|
||||
|
||||
PlantUML source (для редактирования; требует Graphviz `dot` для рендеринга):
|
||||
|
||||
```plantuml
|
||||
@startuml agent-composition
|
||||
skinparam componentStyle rectangle
|
||||
|
||||
title Agent Builder — композиция (целевое API)
|
||||
|
||||
' --- Builder ---
|
||||
rectangle "AgentBuilder" as Builder {
|
||||
rectangle "llm: LiteLlm (обязательно)" as Llm
|
||||
rectangle "storage: StorageBundle (обязательно)" as Storage
|
||||
rectangle "memory: MemorySystem (обязательно)" as Mem
|
||||
rectangle "soul: SoulProvider (default NoopSoul)" as Soul
|
||||
rectangle "tools: List<NamedTool> (авто-сборка из backends)" as Tools
|
||||
rectangle "background: BackgroundConfig (default EmptyBg)" as Bg
|
||||
rectangle "toolset: List<ToolsetContribution> (default empty)" as Ts
|
||||
}
|
||||
|
||||
' --- Backends with their tool side-effects ---
|
||||
rectangle "MemoryMd" as MdMem {
|
||||
interface "MemorySystem" as MemSys
|
||||
interface "List<NamedTool>" as MdTools
|
||||
note right
|
||||
MemoryMd.exposesTools() →
|
||||
memory_save / memory_read /
|
||||
memory_list / memory_delete
|
||||
end note
|
||||
}
|
||||
|
||||
rectangle "McpRegistry" as McpReg {
|
||||
interface "List<NamedTool>" as McpTools
|
||||
note right
|
||||
McpRegistry.namedTools →
|
||||
server__tool1, server__tool2,
|
||||
...
|
||||
end note
|
||||
}
|
||||
|
||||
rectangle "BackgroundEvents" as Events {
|
||||
interface "MutableSharedFlow<CompactionEvent|ToolCallEvent|LifecycleEvent>" as Flow
|
||||
note right
|
||||
Эмитится из:
|
||||
- CompactionCoordinator
|
||||
- ToolDispatcher
|
||||
- ConversationLoop.close()
|
||||
end note
|
||||
}
|
||||
|
||||
rectangle "BackgroundScheduler" as Sched {
|
||||
interface "policy: trigger + debounce" as Policy
|
||||
note right
|
||||
Подписан на Events.
|
||||
НИКАКОГО interval-polling.
|
||||
end note
|
||||
}
|
||||
|
||||
' --- Получаемый Agent ---
|
||||
rectangle "BaseAgent\n(impl: ConversationLoop)" as Agent {
|
||||
rectangle "send / interrupt / events" as API
|
||||
rectangle "BackgroundScheduler\nподписка" as Sub
|
||||
}
|
||||
|
||||
' --- Стрелки зависимостей ---
|
||||
Builder --> Llm
|
||||
Builder --> Storage
|
||||
Builder --> Mem
|
||||
Builder --> Soul
|
||||
Builder --> Tools
|
||||
Builder --> Bg
|
||||
Builder --> Ts
|
||||
|
||||
MdMem --> Mem : implements
|
||||
MdMem --> MdTools : exposes
|
||||
|
||||
McpReg --> McpTools : exposes
|
||||
|
||||
Bg --> Events : subscribes-to
|
||||
Bg --> Sched : holds
|
||||
|
||||
Tools <-- MdTools : auto-merge
|
||||
Tools <-- McpTools : auto-merge
|
||||
|
||||
Builder --> Agent : build()
|
||||
Agent --> API
|
||||
Agent --> Sub
|
||||
|
||||
@enduml
|
||||
```
|
||||
|
||||
## Целевой Kotlin DSL
|
||||
|
||||
```kotlin
|
||||
val agent = agentBuilder {
|
||||
// Обязательные
|
||||
llm(OpenAiLlm.fromEnv()) // или LitertAndroid.onDevice(context)
|
||||
storage(SqliteStorage(path)) // или SqliteStorage.android(context)
|
||||
memory(MemoryMd(root = "~/memory")) // или MemoryVector(embedding = HttpEmbedding(...))
|
||||
|
||||
// Опциональные
|
||||
soul(FileSoul("~/SOUL.md")) // или HttpSoul(url), NoopSoul()
|
||||
background {
|
||||
// triggers: OnClosing (reflection+mining), OnCompaction(minTurns=10, mining=true)
|
||||
// event-driven, не interval
|
||||
}
|
||||
tools {
|
||||
// memoryMd.exposesTools() + mcpRegistry.namedTools авто-подцепляются
|
||||
+FileReadTool(root = "/data")
|
||||
}
|
||||
toolset {
|
||||
+MemoryToolsToolset(memoryMd)
|
||||
}
|
||||
}.build()
|
||||
```
|
||||
|
||||
## Ключевые решения
|
||||
|
||||
- **`MemoryBackend.exposesTools()`** — backend декларирует свои tools. Не «подставить любой backend», а «backend сообщает что он умеет». Это убирает coupling «какие tools совместимы с какими backends».
|
||||
- **Builder требует только `llm + storage + memory`** как обязательные. Всё остальное — опционально с разумными default'ами (`NoopSoul`, `EmptyBackground`, `empty toolset`).
|
||||
- **`BaseAgent`** — реализация `ConversationLoop` через builder. Конструктор принимает все нужные компоненты. **Один и тот же `BaseAgent` в `:standalone`, `:agentik-cli`, `:agentik-android`** — отличается только wiring через builder.
|
||||
- **BackgroundScheduler подписан на `BackgroundEvents`** — это даёт event-driven по умолчанию. `OnEvery(n)` interval-режим — опциональный fallback (не default).
|
||||
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 24 KiB |
@@ -0,0 +1,129 @@
|
||||
# 03 — Multi-user chat с mention-detection
|
||||
|
||||
Точка 1 из планов. Один `BaseAgent` обслуживает N пользователей. Отвечает только когда addressed (mention или admin-команда).
|
||||
|
||||

|
||||
|
||||
PlantUML source (для редактирования; требует Graphviz `dot` для рендеринга):
|
||||
|
||||
```plantuml
|
||||
@startuml multi-user-chat
|
||||
skinparam componentStyle rectangle
|
||||
skinparam participantPadding 15
|
||||
skinparam boxPadding 10
|
||||
|
||||
title Multi-user chat с mention-detection (точка 1 из планов)
|
||||
|
||||
' --- Участники ---
|
||||
actor "User A" as UA
|
||||
actor "User B" as UB
|
||||
actor "User C\n(админ)" as UC
|
||||
participant "Telegram /\nSlack /\nMatrix" as Channel
|
||||
participant "AgentRuntime\n(BaseAgent)" as Runtime
|
||||
participant "MentionDetector" as Detector
|
||||
participant "SoulProvider" as Soul
|
||||
participant "MemorySystem\n(MdMemory)" as Memory
|
||||
participant "LlmBackend\n(LiteLlm)" as Llm
|
||||
|
||||
' --- Сценарий ---
|
||||
UA -> Channel : "@bot, что нового?"
|
||||
UB -> Channel : "люблю котов"
|
||||
UC -> Channel : "/bot status"
|
||||
Channel -> Runtime : событие чата
|
||||
|
||||
' --- Внутри Runtime ---
|
||||
Runtime -> Detector : isMentioned(message, botName)
|
||||
|
||||
note right of Detector
|
||||
variants:
|
||||
- SimpleMentionDetector (regex: @bot)
|
||||
- LlmMentionDetector (mini-classifier)
|
||||
- AdminCommandDetector (/command)
|
||||
end note
|
||||
|
||||
Detector --> Runtime : MatchResult{isMentioned, isCommand}
|
||||
|
||||
alt isMentioned или isCommand
|
||||
Runtime -> Soul : read()
|
||||
Runtime -> Memory : prefetch(query, topK)
|
||||
Runtime -> Llm : send(system + history + memory + user)
|
||||
Llm --> Runtime : response + tool_calls
|
||||
Runtime -> Memory : save(decision)
|
||||
Runtime --> Channel : ответ в нужный канал/thread
|
||||
else NOT mentioned и NOT command
|
||||
Runtime -> Runtime : drop (если не админ)
|
||||
note right
|
||||
Не отвечаем, но возможно:
|
||||
- запоминаем факт (memory-only update)
|
||||
- summary на long conversation
|
||||
end note
|
||||
end
|
||||
|
||||
@enduml
|
||||
```
|
||||
|
||||
## Ключевые модули (что нужно будет добавить)
|
||||
|
||||
### `MentionDetector` interface
|
||||
|
||||
```kotlin
|
||||
interface MentionDetector {
|
||||
data class Result(
|
||||
val isMentioned: Boolean,
|
||||
val isAdminCommand: Boolean,
|
||||
val isPrivateMessage: Boolean, // DM — всегда отвечаем
|
||||
)
|
||||
|
||||
fun detect(message: ChatMessage, botName: String): Result
|
||||
}
|
||||
```
|
||||
|
||||
Имплементации:
|
||||
- `SimpleMentionDetector` — regex `@bot`, `/command` (дешёво, latency ~0)
|
||||
- `LlmMentionDetector` — маленькая классификация через тот же LLM (точнее, но +1 LLM-вызов на каждое сообщение)
|
||||
- `HybridMentionDetector` — fast regex → fallback на LLM только если ambiguous
|
||||
|
||||
### `ChatAdapter` interface
|
||||
|
||||
```kotlin
|
||||
interface ChatAdapter {
|
||||
val channel: String // "telegram" / "slack" / "matrix"
|
||||
suspend fun listen(onMessage: (ChatMessage) -> Unit): Job
|
||||
suspend fun reply(messageId: String, text: String, threadId: String? = null)
|
||||
suspend fun isAdmin(userId: String): Boolean
|
||||
}
|
||||
```
|
||||
|
||||
Имплементации per platform. Каждая адаптирует формат platform → `ChatMessage`.
|
||||
|
||||
### Конфигурация builder'а
|
||||
|
||||
```kotlin
|
||||
agentBuilder {
|
||||
llm(...)
|
||||
storage(...)
|
||||
memory(...)
|
||||
soul(...)
|
||||
background { ... }
|
||||
chat {
|
||||
mentionDetector = HybridMentionDetector(regex = "@bot|@Agent", llmClassifier = false)
|
||||
chatAdapter = TelegramChatAdapter(token = "...")
|
||||
// На каждое сообщение:
|
||||
// 1. mentionDetector.detect()
|
||||
// 2. если isMentioned || isAdminCommand || isPrivate → process
|
||||
// 3. иначе — опционально memory-only save (тихий режим)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Что это даёт
|
||||
|
||||
- Один `BaseAgent` обслуживает чат целиком (один LLM, одна память — общий контекст команды)
|
||||
- `@bot` — explicit invocation, не «agent отвечает на всё подряд»
|
||||
- `/bot status` / `/bot clear-memory` — admin-команды (отдельный канал, без LLM)
|
||||
- DM — всегда отвечает (это личное обращение)
|
||||
- В групповом чате без mention — agent может **молча учить** (memory update без ответа). Полезно для «запомнил что Вася любит котов».
|
||||
|
||||
## Текущее состояние vs целевое
|
||||
|
||||
⏳ Ничего из этого нет. Сейчас `:standalone` — это HTTP API, к которому подключаются clients. Для multi-user chat нужен новый `:chat-adapter-telegram` (или -slack / -matrix) модуль + `MentionDetector` interface.
|
||||
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 18 KiB |
@@ -0,0 +1,119 @@
|
||||
# 04 — Sub-agents + A2A между агентами
|
||||
|
||||
Точки 2 и 3 из планов. Orchestrator-агент spawn'ит sub-агентов с изолированным контекстом. Независимые агенты общаются через A2A.
|
||||
|
||||

|
||||
|
||||
PlantUML source (для редактирования; требует Graphviz `dot` для рендеринга):
|
||||
|
||||
```plantuml
|
||||
@startuml sub-agents-and-a2a
|
||||
skinparam componentStyle rectangle
|
||||
|
||||
title Sub-agents + A2A между агентами (точка 2+3 из планов)
|
||||
|
||||
' --- Orchestrator ---
|
||||
rectangle "OrchestratorAgent\n(BaseAgent + tools)" as Orch {
|
||||
rectangle "ConversationLoop\n(main user)" as MainConv
|
||||
}
|
||||
|
||||
' --- Sub-agent spawn ---
|
||||
rectangle "subAgent(\n task: String,\n config: AgentConfig\n): Flow<SubAgentEvent>" as SpawnAPI
|
||||
note right of SpawnAPI
|
||||
Spawn API — НЕ отдельный модуль,
|
||||
а convenience поверх BaseAgent:
|
||||
val sub = agent.spawnChild(config) {
|
||||
systemPrompt = "..."
|
||||
tools = [ReadTool, WriteTool]
|
||||
memory = EmptyMemory // изолированно
|
||||
}
|
||||
sub.events.collect { ... }
|
||||
end note
|
||||
|
||||
' --- Дочерний агент (изолированный контекст) ---
|
||||
rectangle "SubAgent\n(изолированный scope)" as Sub {
|
||||
rectangle "ConversationLoop\n(child)" as SubConv
|
||||
rectangle "backgroundScope\n(lifecycle scoped)" as SubBg
|
||||
}
|
||||
|
||||
' --- A2A между независимыми агентами ---
|
||||
rectangle "Agent A\n(BaseAgent)" as AgentA
|
||||
rectangle "Agent B\n(BaseAgent)" as AgentB
|
||||
rectangle "A2A Server\n(:a2a-server)" as A2ASrv
|
||||
|
||||
AgentA -> A2ASrv : POST /\n(application/json)
|
||||
A2ASrv -> AgentB : dispatch(message)
|
||||
AgentB --> A2ASrv : response
|
||||
A2ASrv --> AgentA : SSE / JSON-RPC
|
||||
|
||||
' --- Стрелки ---
|
||||
Orch -> SpawnAPI : calls
|
||||
SpawnAPI -> Sub : creates with custom config
|
||||
Sub -> SubBg : has its own
|
||||
Orch -> Orch : main flow continues
|
||||
Sub --> Orch : Flow<SubAgentEvent> emits\n(Started / ToolCalled / ToolResult /\nAssistantMessage / Done / Failed)
|
||||
|
||||
Orch -> A2ASrv : can also delegate to remote agent
|
||||
|
||||
@enduml
|
||||
```
|
||||
|
||||
## Sub-agents API
|
||||
|
||||
```kotlin
|
||||
sealed interface SubAgentEvent {
|
||||
data class Started(val taskId: String) : SubAgentEvent
|
||||
data class AssistantMessage(val text: String) : SubAgentEvent
|
||||
data class ToolCalled(val toolName: String, val args: JsonObject) : SubAgentEvent
|
||||
data class ToolResult(val toolName: String, val result: String) : SubAgentEvent
|
||||
data class Done(val taskId: String, val finalResult: String) : SubAgentEvent
|
||||
data class Failed(val taskId: String, val error: String) : SubAgentEvent
|
||||
}
|
||||
|
||||
interface BaseAgent {
|
||||
// ... existing methods ...
|
||||
|
||||
/**
|
||||
* Spawn дочерний агент с изолированным контекстом (memory, system prompt,
|
||||
* tools). Возвращает Flow событий жизненного цикла + результата.
|
||||
* Cancellation родителя НЕ отменяет sub-agent — sub-agent живёт до Done/Failed.
|
||||
*/
|
||||
fun spawnChild(config: SubAgentConfig): Flow<SubAgentEvent>
|
||||
}
|
||||
|
||||
data class SubAgentConfig(
|
||||
val systemPrompt: String,
|
||||
val tools: List<NamedTool> = emptyList(),
|
||||
val memory: MemorySystem = EmptyMemory(),
|
||||
val model: LiteLlm? = null, // если null — делит LLM родителя
|
||||
val maxTurns: Int = 10,
|
||||
val timeoutMs: Long = 60_000,
|
||||
)
|
||||
```
|
||||
|
||||
## Зачем изолированный scope
|
||||
|
||||
Sub-agent получает **свою копию контекста**, не делит memory с родителем. Это критично:
|
||||
- `research_subagent` — должен исследовать тему, не отвечать на основные сообщения пользователя
|
||||
- `summarize_subagent` — суммаризировать документ, не трогать основной диалог
|
||||
- `code_review_subagent` — ревьюить PR, не видеть разговор
|
||||
|
||||
Если нужно расшарить контекст — это explicit через `sharedMemory: SharedMemoryHandle` параметр, не default.
|
||||
|
||||
## A2A между независимыми агентами
|
||||
|
||||
Уже есть `:a2a-server` модуль (см. `standalone/build.gradle.kts` — `implementation(libs.a2a.server)`). Использовался для AG-UI/A2A протокола в `:standalone`. Можно переиспользовать для межагентного общения.
|
||||
|
||||
Сценарий: orchestrator-agent не может сам решить задачу → делегирует remote-агенту через A2A → получает response → продолжает. Это уже работающая инфраструктура.
|
||||
|
||||
## Текущее состояние vs целевое
|
||||
|
||||
✅ Уже есть:
|
||||
- `:a2a-server` подключён
|
||||
- `BaseAgent.spawnChild` — **не существует**, но `ConversationLoop` уже умеет создавать изолированный scope через свой `agentScope` — нужна только обёртка
|
||||
|
||||
⏳ Не сделано:
|
||||
- `SubAgentConfig` + `Flow<SubAgentEvent>` API
|
||||
- `EmptyMemory` (null-object для изолированного scope)
|
||||
- Lifecycle management (parent dies → child должен complete or be cancelled?)
|
||||
- Сериализация sub-agent state для отладки (event log)
|
||||
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 14 KiB |
@@ -0,0 +1,171 @@
|
||||
# 05 — Android Agent Stack
|
||||
|
||||
Что меняется vs `:standalone`. Цель: `BaseAgent` тот же самый, но platform-impl разные (Storage, LLM, Vector Memory, MCP).
|
||||
|
||||

|
||||
|
||||
PlantUML source (для редактирования; требует Graphviz `dot` для рендеринга):
|
||||
|
||||
```plantuml
|
||||
@startuml android-agent-stack
|
||||
skinparam componentStyle rectangle
|
||||
|
||||
title Android Agent Stack — что меняется vs Standalone
|
||||
|
||||
' --- Android side ---
|
||||
package "Android Application" {
|
||||
[MainActivity\n(Compose)] as Activity
|
||||
[AndroidAgentRunner\n(workmanager / service)] as Runner
|
||||
[AndroidAgentBuilder] as AndroidBuilder
|
||||
}
|
||||
|
||||
package "Android-specific impls" {
|
||||
[StorageSqliteAndroid\n(Room/sqlite)] as StorageA
|
||||
[MemoryVectorAndroid\n(ONNX runtime + ANN)] as MemVecA
|
||||
[LitertAndroid\n(NNAPI delegate)] as LitertA
|
||||
[SoulFileAndroid\n(context.filesDir)] as SoulA
|
||||
[McpRegistry\nstdio: ProcessBuilder] as McpA
|
||||
}
|
||||
|
||||
' --- Shared (KMP) ---
|
||||
package "Agent Runtime (shared)" {
|
||||
[AgentCore\nBaseAgent] as AgentCore
|
||||
[AgentBuilder] as Builder
|
||||
}
|
||||
|
||||
package "Domain (shared)" {
|
||||
[LlmTools\ncommonMain] as LlmT
|
||||
[BackgroundEvents\ncommonMain] as Ev
|
||||
[McpBridge\njvmMain] as McpB
|
||||
[Skills\ncommonMain] as Skills
|
||||
}
|
||||
|
||||
package "Memory (shared impl)" {
|
||||
[MemoryMd\n(commonMain)] as MemMd
|
||||
[MemoryApi\ninterfaces] as MemApi
|
||||
}
|
||||
|
||||
' --- Зависимости ---
|
||||
Activity --> Runner
|
||||
Runner --> AndroidBuilder
|
||||
AndroidBuilder --> AgentCore
|
||||
|
||||
AndroidBuilder --> StorageA
|
||||
AndroidBuilder --> MemVecA
|
||||
AndroidBuilder --> LitertA
|
||||
AndroidBuilder --> SoulA
|
||||
AndroidBuilder --> McpA
|
||||
|
||||
AgentCore --> LlmT
|
||||
AgentCore --> Ev
|
||||
AgentCore --> McpB
|
||||
AgentCore --> Skills
|
||||
AgentCore --> MemMd
|
||||
|
||||
' --- Главные отличия от Standalone ---
|
||||
note right of LitertA
|
||||
On-device inference.
|
||||
LiteRT с NNAPI delegate →
|
||||
работает на CPU/GPU/NPU
|
||||
прямо на устройстве, без сети.
|
||||
|
||||
vs Standalone: HTTP-only
|
||||
(OpenAI-compatible).
|
||||
end note
|
||||
|
||||
note right of StorageA
|
||||
android.database.sqlite
|
||||
через Room или сырой API.
|
||||
|
||||
vs Standalone: JDBC +
|
||||
Sqlite-JDBC driver
|
||||
(только JVM).
|
||||
end note
|
||||
|
||||
note right of MemVecA
|
||||
JVector JVM-only. На Android
|
||||
нужна альтернатива —
|
||||
ONNX Runtime + какой-нибудь
|
||||
ANN (Annoy/HNSW).
|
||||
|
||||
Или пока без vector memory,
|
||||
только MemoryMd.
|
||||
end note
|
||||
|
||||
note right of McpA
|
||||
MCP через ProcessBuilder
|
||||
на Android работает, но
|
||||
subprocess lifecycle
|
||||
сложнее (foreground service
|
||||
нужен для долгого subprocess).
|
||||
end note
|
||||
|
||||
@enduml
|
||||
```
|
||||
|
||||
## Что общего с `:standalone`
|
||||
|
||||
**`BaseAgent`, `BackgroundScheduler`, `LlmTools`, `McpBridge`, `Skills`, `MemoryMd` — всё KMP (commonMain).** Android-agent = `:standalone` с другим wiring'ом. Не нужно переписывать agent logic.
|
||||
|
||||
## Что другое
|
||||
|
||||
| Компонент | `:standalone` (JVM) | `:agentik-android` (Android) | Сложность |
|
||||
|---|---|---|---|
|
||||
| Storage | `:storage-sqlite` (JDBC + Sqlite-JDBC) | `:storage-sqlite-android` (Room или raw) | Низкая — тот же `StorageBundle` interface |
|
||||
| LLM | `:litert-openai` (HTTP), `:litert-google` (LiteRT JVM) | `:litert-android` (LiteRT Android, NNAPI delegate) | Средняя — нужен новый модуль |
|
||||
| Vector memory | `:memory-vector` (JVector) | `:memory-vector-android` (ONNX Runtime + HNSW/Annoy) | Высокая — JVector JVM-only, нужна альтернатива |
|
||||
| SOUL provider | `FileSoulProvider` (path) | `SoulFileAndroid` (`context.filesDir`) | Низкая |
|
||||
| MCP | `McpRegistry` (ProcessBuilder, stdio subprocess) | Тот же `McpRegistry`, но subprocess в foreground service | Средняя — нужен Android service |
|
||||
| Embedding | `HttpEmbeddingClient` (HTTP) | Тот же ИЛИ on-device (ONNX) | Средняя |
|
||||
|
||||
## Минимальный Android agent (v1)
|
||||
|
||||
Если не нужны все фичи сразу — минимум:
|
||||
|
||||
```kotlin
|
||||
val agent = androidAgentBuilder(context) {
|
||||
llm(LitertAndroid.onDevice(context, modelPath = "/data/local/tmp/model.litertlm"))
|
||||
storage(SqliteStorage.android(context, "agent.db"))
|
||||
memory(MemoryMd.root(context.filesDir.resolve("memory")))
|
||||
soul(FileSoul(context.filesDir.resolve("SOUL.md")))
|
||||
background {
|
||||
// OnClosing + OnCompaction работают так же как на JVM
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Без MCP, без vector memory (только MemoryMd на файлах), только on-device LLM. Достаточно для off-line агента.
|
||||
|
||||
## Foreground service для MCP
|
||||
|
||||
Если нужны MCP-серверы (например, локальный file-system MCP) — subprocess нужен foreground service чтобы Android не убил его при выключении экрана. Это добавляет сложности:
|
||||
|
||||
```kotlin
|
||||
class McpForegroundService : Service() {
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
startForeground(NOTIFICATION_ID, notification)
|
||||
val proc = ProcessBuilder(command, args).start()
|
||||
// ... route stdio to McpLiteToolAdapter ...
|
||||
return START_STICKY
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Пока можно без этого (только если MCP нужен на Android).
|
||||
|
||||
## Текущее состояние vs целевое
|
||||
|
||||
✅ KMP-ready:
|
||||
- `:llm-tools` (commonMain, платформо-агностик)
|
||||
- `:mcp-bridge` (jvmMain — Android-вариант через `:mcp-bridge-android`)
|
||||
- `:skills` (commonMain)
|
||||
- `:memory-md` (commonMain)
|
||||
- `:proto` (commonMain)
|
||||
|
||||
⏳ Не существует:
|
||||
- `:storage-sqlite-android`
|
||||
- `:memory-vector-android`
|
||||
- `:litert-android`
|
||||
- `:agentik-android` (само приложение)
|
||||
- `:agent-core` (выделить BaseAgent + builder)
|
||||
- `:background-events` (выделить events + scheduler)
|
||||
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 25 KiB |
@@ -0,0 +1,64 @@
|
||||
# agentik — диаграммы архитектуры
|
||||
|
||||
PlantUML-схемы для обсуждения будущей структуры (Android agent, multi-user chat, sub-agents, A2A). Это **целевое состояние**, не текущее.
|
||||
|
||||
## Файлы
|
||||
|
||||
Каждый `.md` содержит:
|
||||
- Краткое описание (что показывает)
|
||||
- **Пред-рендеренный SVG** (``) — гарантированно показывается **везде**
|
||||
- PlantUML source в ` ```plantuml ` блоке — для редактирования (требует Graphviz `dot` для рендеринга)
|
||||
- Дополнительный markdown-текст (что нужно сделать, текущее vs целевое)
|
||||
|
||||
| Файл | Что показывает |
|
||||
|---|---|
|
||||
| [01-module-layers.md](./01-module-layers.md) | Целевая модульная структура (приложения → runtime → домен → абстракции → платформенные impl). Что в каком слое и кто от кого зависит. |
|
||||
| [02-agent-composition.md](./02-agent-composition.md) | Как `AgentBuilder` собирает `BaseAgent` из компонентов. Memory backend сам объявляет свои tools. BackgroundScheduler подписан на события (НЕ interval-poll). |
|
||||
| [03-multi-user-chat.md](./03-multi-user-chat.md) | Сценарий: чат с N пользователями, mention-detection, админ-команды, agent отвечает только когда addressed. |
|
||||
| [04-sub-agents.md](./04-sub-agents.md) | Orchestrator spawn'ит sub-agent с изолированным контекстом, получает `Flow<SubAgentEvent>`. A2A между независимыми агентами через `:a2a-server`. |
|
||||
| [05-android-stack.md](./05-android-stack.md) | Что меняется на Android: on-device LLM (NNAPI), Room/sqlite, ONNX-based vector memory, foreground-service для MCP subprocess. |
|
||||
|
||||
## Почему SVG + PlantUML source
|
||||
|
||||
PlantUML требует Java + (для component/class/deployment диаграмм) Graphviz `dot`. Если `dot` не установлен — рендерер падает с ошибкой "Executable dot does not exist".
|
||||
|
||||
Решение: **пре-рендерим в SVG один раз** и вставляем как `<img>`. Диаграмма гарантированно показывается в любом markdown-viewer (GitHub, IntelliJ, VSCode, GitLab) без зависимостей. PlantUML source в code block остаётся для редактирования.
|
||||
|
||||
## Как редактировать диаграмму
|
||||
|
||||
1. Меняешь PlantUML-source в ` ```plantuml ` блоке `.md` файла.
|
||||
2. Ре-рендеришь SVG:
|
||||
```bash
|
||||
mkdir -p /tmp/plantuml-work && chmod 777 /tmp/plantuml-work
|
||||
cp docs/diagrams/*.md /tmp/plantuml-work/
|
||||
docker run --rm -v /tmp/plantuml-work:/work plantuml/plantuml -tsvg /work/*.md
|
||||
cp /tmp/plantuml-work/*.svg docs/diagrams/
|
||||
```
|
||||
3. Проверяешь что SVG обновился:
|
||||
```bash
|
||||
ls -la docs/diagrams/*.svg
|
||||
```
|
||||
4. Коммитишь оба файла: `.md` (source) и `.svg` (rendered).
|
||||
|
||||
Требует Docker (или локального PlantUML+Graphviz). `apt install graphviz` для Arch/Manjaro.
|
||||
|
||||
## Контекст
|
||||
|
||||
Текущий код движется в эту сторону:
|
||||
- `:llm-tools` extracted ✅
|
||||
- `:mcp-bridge` extracted ✅
|
||||
- `BackgroundScheduler` стал event-driven ✅
|
||||
- `ConversationLoop` стал отдельным компонентом ✅
|
||||
|
||||
Не сделано (см. детали в каждом .md):
|
||||
- `:agent-core` (выделить `BaseAgent` + builder)
|
||||
- `:background-events` (выделить events + scheduler)
|
||||
- `:storage-sqlite-android`, `:memory-vector-android`, `:litert-android`
|
||||
- `:agentik-android` (само приложение)
|
||||
- `MentionDetector` interface + adapters для multi-user chat
|
||||
- `BaseAgent.spawnChild` + `Flow<SubAgentEvent>`
|
||||
|
||||
Подробнее:
|
||||
- `STANDALONE-REVIEW.md` — что плохо в текущем коде
|
||||
- `MEMORY-DESIGN.md` — детали memory архитектуры
|
||||
- `STANDALONE.md` — текущий standalone
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
-2
@@ -1,5 +1,9 @@
|
||||
# Default version for local builds; overridden by `-Pversion=<tag>` from CI/CD.
|
||||
version=0.1.0
|
||||
# Default version for local builds only (когда CI/CD не передал -Pversion=<tag>).
|
||||
# Имя ключа специально НЕ 'version' — иначе Gradle-мерж gradle.properties и
|
||||
# -Pversion= возьмёт default из gradle.properties. Передавай через CICD:
|
||||
# ./gradlew ... -Pversion=$(git describe --tags)
|
||||
# см. .gitea/workflows/release.yml (использует -Pversion=$GITHUB_REF_NAME).
|
||||
agentik.version.default=0.1.0-SNAPSHOT
|
||||
|
||||
# KMP jvm target uses JDK 21 for both compilation and toolchain.
|
||||
org.gradle.jvmargs=-Xmx4096M -XX:+UseG1GC
|
||||
|
||||
@@ -13,11 +13,17 @@ jvector = "3.0.6"
|
||||
text-embedding-kmp = "3.0.0-SNAPSHOT"
|
||||
kotlin-logging = "3.0.5"
|
||||
logback = "1.5.18"
|
||||
mosaic = "0.18.0"
|
||||
clikt = "5.0.3"
|
||||
kotlinx-cli = "0.3.6"
|
||||
|
||||
[plugins]
|
||||
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
|
||||
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
|
||||
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
|
||||
# JetBrains Compose Compiler plugin — обязательно для @Composable в KMP-проектах
|
||||
# с Compose Multiplatform 1.8+; без него @Composable-лямбды ломаются (Function0 вместо Function2).
|
||||
kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
|
||||
sqldelight = { id = "app.cash.sqldelight", version.ref = "sqldelight" }
|
||||
shadow = { id = "com.gradleup.shadow", version.ref = "shadow" }
|
||||
|
||||
@@ -49,6 +55,7 @@ ktor-server-content-negotiation = { module = "io.ktor:ktor-server-content-negoti
|
||||
ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" }
|
||||
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
|
||||
ktor-client-cio = { module = "io.ktor:ktor-client-cio", version.ref = "ktor" }
|
||||
ktor-client-curl = { module = "io.ktor:ktor-client-curl", version.ref = "ktor" }
|
||||
ktor-client-content-negotiation = { module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktor" }
|
||||
ktor-server-test-host = { module = "io.ktor:ktor-server-test-host", version.ref = "ktor" }
|
||||
ktor-client-sse = { module = "io.ktor:ktor-client-sse", version.ref = "ktor" }
|
||||
@@ -56,6 +63,24 @@ ktor-client-sse = { module = "io.ktor:ktor-client-sse", version.ref = "ktor" }
|
||||
# --- Model Context Protocol (MCP) ---
|
||||
mcp-sdk-client = { module = "io.modelcontextprotocol:kotlin-sdk-client", version = "0.15.0" }
|
||||
|
||||
# --- CLI: clikt (ajalt). KMP, native Linux/macOS/Windows включая linuxArm64. ---
|
||||
# https://ajalt.github.io/clikt/
|
||||
# Артефакт один и тот же — `com.github.ajalt.clikt:clikt` — Gradle module
|
||||
# metadata резолвит per-target variant (clikt-jvm / clikt-linuxarm64 / ...).
|
||||
clikt = { module = "com.github.ajalt.clikt:clikt-core", version.ref = "clikt" }
|
||||
kotlinx-cli = { module = "org.jetbrains.kotlinx:kotlinx-cli", version.ref = "kotlinx-cli" }
|
||||
|
||||
# --- TUI: Mosaic (Jetpack Compose → ANSI-терминал), jvm + desktop-native. ---
|
||||
# https://github.com/JakeWharton/mosaic
|
||||
mosaic-runtime = { module = "com.jakewharton.mosaic:mosaic-runtime", version.ref = "mosaic" }
|
||||
mosaic-runtime-jvm = { module = "com.jakewharton.mosaic:mosaic-runtime-jvm", version.ref = "mosaic" }
|
||||
mosaic-runtime-macosx64 = { module = "com.jakewharton.mosaic:mosaic-runtime-macosx64", version.ref = "mosaic" }
|
||||
mosaic-runtime-macosarm64 = { module = "com.jakewharton.mosaic:mosaic-runtime-macosarm64", version.ref = "mosaic" }
|
||||
mosaic-runtime-linuxx64 = { module = "com.jakewharton.mosaic:mosaic-runtime-linuxx64", version.ref = "mosaic" }
|
||||
mosaic-runtime-linuxarm64 = { module = "com.jakewharton.mosaic:mosaic-runtime-linuxarm64", version.ref = "mosaic" }
|
||||
mosaic-runtime-mingwx64 = { module = "com.jakewharton.mosaic:mosaic-runtime-mingwx64", version.ref = "mosaic" }
|
||||
mosaic-tty-terminal = { module = "com.jakewharton.mosaic:mosaic-tty-terminal", version.ref = "mosaic" }
|
||||
|
||||
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
|
||||
|
||||
# --- commons ---
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user