feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
# retoor <retoor@molodetz.nl>
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
from typing import Any, Callable
|
|
|
|
|
|
|
|
|
|
from devplacepy.database import db
|
|
|
|
|
|
|
|
|
|
from .agentic import LessonStore
|
2026-06-11 00:17:25 +02:00
|
|
|
from .behavior import BehaviorStore
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
from .config import Settings
|
|
|
|
|
from .cost.tracker import Pricing
|
|
|
|
|
from .llm import LLMClient
|
|
|
|
|
from .session import DeviiSession
|
|
|
|
|
from .store import ConversationStore, TurnAudit, UsageLedger
|
|
|
|
|
from .tasks.store import TaskStore, memory_db
|
2026-06-09 16:06:02 +02:00
|
|
|
from .virtual_tools import VirtualToolStore
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
|
|
|
|
|
logger = logging.getLogger("devii.hub")
|
|
|
|
|
|
2026-06-09 00:30:25 +02:00
|
|
|
SettingsBuilder = Callable[[str, str, str, bool], "tuple[Settings, Pricing]"]
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class DeviiHub:
|
|
|
|
|
def __init__(self, settings_builder: SettingsBuilder) -> None:
|
|
|
|
|
self._build = settings_builder
|
|
|
|
|
self._stores: dict[str, Any] = {
|
|
|
|
|
"conversations": ConversationStore(),
|
|
|
|
|
"ledger": UsageLedger(),
|
|
|
|
|
"turns": TurnAudit(),
|
|
|
|
|
}
|
2026-06-13 23:37:13 +02:00
|
|
|
self._sessions: dict[tuple[str, str, str], DeviiSession] = {}
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def ledger(self) -> UsageLedger:
|
|
|
|
|
return self._stores["ledger"]
|
|
|
|
|
|
|
|
|
|
def get_or_create(
|
2026-06-09 18:48:08 +02:00
|
|
|
self,
|
|
|
|
|
owner_kind: str,
|
|
|
|
|
owner_id: str,
|
|
|
|
|
username: str,
|
|
|
|
|
api_key: str,
|
|
|
|
|
base_url: str,
|
feat: add project file system with CRUD, upload, inline editing, and video attachment support
- Add new `/projects/{slug}/files` endpoint group for per-project filesystem operations including directory and file CRUD, upload, and inline editing with public read and owner write access
- Extend attachment system to support video formats (webm, ogv, mov, m4v) with proper file icons and MIME types
- Implement configurable allowed file types via `allowed_file_types` site setting, replacing hardcoded `ALLOWED_UPLOAD_TYPES` with dynamic `allowed_extensions()` and `is_extension_allowed()` functions
- Add `delete_all_project_files()` call in `delete_content_item()` to clean up project files when a project is deleted
- Create database indexes on `project_files` table for `(project_uid, path)` and `(project_uid, parent_path)` to optimize file lookups
- Introduce `docs_prose.py` module with `render_prose()` function that renders Markdown content inside `data-render` divs using mistune, enabling dynamic prose rendering in documentation pages
- Enhance docs search with Markdown-aware text stripping (`_demarkdown()`) and improved HTML/script/style sanitization for better search indexing
- Update documentation API samples to reflect new attachment response fields (`is_image`, `is_video`, `mime_type`) and note video format support
- Update README to document the new project files endpoint and clarify AI gateway attribution for guest Devii sessions
2026-06-08 22:51:09 +02:00
|
|
|
is_admin: bool = False,
|
2026-06-19 00:09:34 +02:00
|
|
|
is_primary_admin: bool = False,
|
2026-06-13 23:37:13 +02:00
|
|
|
channel: str = "main",
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
) -> DeviiSession:
|
2026-06-13 23:37:13 +02:00
|
|
|
key = (owner_kind, owner_id, channel)
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
session = self._sessions.get(key)
|
|
|
|
|
if session is not None:
|
|
|
|
|
return session
|
2026-06-09 00:30:25 +02:00
|
|
|
settings, pricing = self._build(api_key, base_url, owner_kind, is_admin)
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
llm = LLMClient(settings)
|
|
|
|
|
# Persistent, owner-isolated stores for signed-in users; ephemeral in-memory for guests.
|
2026-06-13 23:37:13 +02:00
|
|
|
# The `docs` channel (Docii) is a self-contained documentation assistant: it gets its own
|
|
|
|
|
# ephemeral stores so Devii's tasks, lessons, behavior and virtual tools never leak into it
|
|
|
|
|
# (and a Docii reflection never pollutes the user's Devii memory). Only the conversation
|
|
|
|
|
# thread persists, keyed per channel in the shared ConversationStore.
|
2026-06-19 00:09:34 +02:00
|
|
|
owned_db = (
|
|
|
|
|
db
|
|
|
|
|
if (owner_kind == "user" and channel in ("main", "telegram"))
|
|
|
|
|
else memory_db()
|
|
|
|
|
)
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
task_store = TaskStore(owned_db, owner_kind, owner_id)
|
|
|
|
|
lessons = LessonStore(owned_db, owner_kind, owner_id)
|
2026-06-09 16:06:02 +02:00
|
|
|
virtual_tool_store = VirtualToolStore(owned_db, owner_kind, owner_id)
|
2026-06-11 00:17:25 +02:00
|
|
|
behavior_store = BehaviorStore(owned_db, owner_kind, owner_id)
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
session = DeviiSession(
|
2026-06-09 18:48:08 +02:00
|
|
|
owner_kind,
|
|
|
|
|
owner_id,
|
|
|
|
|
username,
|
|
|
|
|
settings,
|
|
|
|
|
llm,
|
|
|
|
|
lessons,
|
|
|
|
|
pricing,
|
|
|
|
|
task_store,
|
|
|
|
|
virtual_tool_store,
|
2026-06-11 00:17:25 +02:00
|
|
|
behavior_store,
|
2026-06-09 18:48:08 +02:00
|
|
|
self._stores,
|
|
|
|
|
is_admin=is_admin,
|
2026-06-19 00:09:34 +02:00
|
|
|
is_primary_admin=is_primary_admin,
|
2026-06-13 23:37:13 +02:00
|
|
|
channel=channel,
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
)
|
|
|
|
|
if owner_kind == "user":
|
2026-06-13 23:37:13 +02:00
|
|
|
saved = self._stores["conversations"].load(owner_kind, owner_id, channel)
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
if saved:
|
|
|
|
|
session.restore_history(saved)
|
|
|
|
|
self._sessions[key] = session
|
2026-06-09 18:48:08 +02:00
|
|
|
logger.info(
|
2026-06-13 23:37:13 +02:00
|
|
|
"Created session %s/%s [%s] (total %d)",
|
2026-06-09 18:48:08 +02:00
|
|
|
owner_kind,
|
|
|
|
|
owner_id,
|
2026-06-13 23:37:13 +02:00
|
|
|
channel,
|
2026-06-09 18:48:08 +02:00
|
|
|
len(self._sessions),
|
|
|
|
|
)
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
return session
|
|
|
|
|
|
2026-06-13 23:37:13 +02:00
|
|
|
def find(
|
|
|
|
|
self, owner_kind: str, owner_id: str, channel: str = "main"
|
|
|
|
|
) -> DeviiSession | None:
|
|
|
|
|
return self._sessions.get((owner_kind, owner_id, channel))
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
|
|
|
|
|
def active_sessions(self) -> int:
|
|
|
|
|
return len(self._sessions)
|
|
|
|
|
|
|
|
|
|
def connection_count(self) -> int:
|
|
|
|
|
return sum(s.connection_count for s in self._sessions.values())
|
|
|
|
|
|
|
|
|
|
async def gc_idle(self) -> int:
|
|
|
|
|
removed = 0
|
|
|
|
|
for key, session in list(self._sessions.items()):
|
2026-06-19 00:09:34 +02:00
|
|
|
if session.connection_count == 0 and not session.has_pending_tasks():
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
await session.aclose()
|
|
|
|
|
del self._sessions[key]
|
|
|
|
|
removed += 1
|
|
|
|
|
if removed:
|
|
|
|
|
logger.info("GC removed %d idle session(s)", removed)
|
|
|
|
|
return removed
|
|
|
|
|
|
|
|
|
|
async def aclose(self) -> None:
|
|
|
|
|
for session in list(self._sessions.values()):
|
|
|
|
|
await session.aclose()
|
|
|
|
|
self._sessions.clear()
|