Backoffice API

kavalai.backoffice is the FastAPI service behind the management UI. It exposes agents, sessions, runs, metrics, model-call statistics and RAG indexes, and the project service that scopes them.

Copyright 2026 OÜ KAVAL AI (registry code 17393877)

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

kavalai.backoffice.server.get_backoffice_session()[source]

Context manager to provide a session for the backoffice database. Handles connection errors gracefully.

kavalai.backoffice.server.get_project_session(project: Project)[source]

Context manager to provide a session for a specific project’s agent database. Handles connection errors gracefully.

kavalai.backoffice.server.required_setting(name: str) → str[source]

A setting the backoffice cannot run without.

There is no development fallback on purpose: a cookie signed with a well-known key is a backoffice that looks as if it works right up to the moment it is exposed.

kavalai.backoffice.server.google_oauth_settings() → dict[str, str] | None[source]

The Google OAuth client, or None when Google sign-in is not configured.

Google sign-in is optional: with neither KAVALAI_BO_GOOGLE_CLIENT_ID nor KAVALAI_BO_GOOGLE_CLIENT_SECRET set, users sign in with a password only. One without the other is a mistake rather than a choice, and so is a client with nowhere to redirect to; both stop the start-up.

kavalai.backoffice.server.session_user_info(user: User) → dict[source]

What the session cookie carries about a signed-in user.

kavalai.backoffice.server.public_user(user: User) → dict[source]

A user as the API returns it: every column except the password hash.

kavalai.backoffice.server.backoffice_hosts(request: Request) → list[str][source]

The host names the backoffice is reached under, as far as a request shows.

The host the request was addressed to (the nginx in front forwards Host), the page that sent it (Origin) and the frontend URL that Google sign-in configures.

kavalai.backoffice.server.checked_picture(value: object, request: Request) → str | None[source]

A picture from a request body as it may be stored (see validate_picture_url()); 400 otherwise.

kavalai.backoffice.server.user_fields(data: dict, request: Request) → dict[source]

The columns a create/update body may set on a user.

password is hashed into password_hash (an empty one is left out, so an edit form that does not touch the password keeps it); a password_hash sent by the client is dropped, as is has_password, which public_user() adds and a form may send back. picture is checked by checked_picture().

async kavalai.backoffice.server.authenticate_and_sync_user(user_info: dict)[source]
kavalai.backoffice.server.is_logged_in(request: Request)[source]
kavalai.backoffice.server.assert_logged_in(request: Request)[source]
kavalai.backoffice.server.assert_is_admin(request: Request, detail: str = 'Only administrators can do this.') → None[source]
async kavalai.backoffice.server.assert_is_owner(session: AsyncSession, request: Request, project_id: UUID)[source]
async kavalai.backoffice.server.assert_is_admin_or_owner(session: AsyncSession, request: Request, project_id: UUID)[source]
async kavalai.backoffice.server.assert_is_member(session: AsyncSession, request: Request, project_id: UUID)[source]
async kavalai.backoffice.server.get_project_and_assert_access(request: Request, project_id: UUID) → Project[source]

Fetch project and assert user is a member.

kavalai.backoffice.server.assert_google_sign_in() → None[source]
async kavalai.backoffice.server.auth_methods()[source]

The sign-in methods this installation offers; read by the login page.

async kavalai.backoffice.server.password_login(request: Request, data: dict = Body(PydanticUndefined))[source]

Sign in with an address and a password.

Every failure — unknown address, no password set, wrong password — answers the same 401, so the endpoint does not reveal which addresses are registered.

async kavalai.backoffice.server.login(request: Request)[source]
async kavalai.backoffice.server.logout(request: Request)[source]

End the session.

A POST, because a GET would be sent by any image or link pointing at it, with the session cookie, and log the viewer out.

async kavalai.backoffice.server.user_details(request: Request)[source]
async kavalai.backoffice.server.google_auth_callback(request: Request)[source]
async kavalai.backoffice.server.set_active_project(project_id: UUID, request: Request)[source]
kavalai.backoffice.server.PROFILE_FIELDS = ('name', 'picture')

What a signed-in user may change on their own record through PATCH /user/me.

async kavalai.backoffice.server.signed_in_user(session: AsyncSession, request: Request) → User[source]

The signed-in user’s own row; a session whose user was deleted is refused.

kavalai.backoffice.server.profile_fields(data: dict, request: Request) → dict[source]

The name and picture from a profile body, checked.

Any other key is refused rather than ignored, so a client that tries to change its address or its administrator flag here learns that it cannot. An empty picture clears it; any other goes through checked_picture().

async kavalai.backoffice.server.user_me(request: Request)[source]

The signed-in user’s own record, including has_password.

async kavalai.backoffice.server.user_me_update(request: Request, data: dict = Body(PydanticUndefined))[source]

Change the signed-in user’s own name and picture.

The session carries the name and picture the shell shows, so it is refreshed with the saved values.

async kavalai.backoffice.server.user_me_password(request: Request, data: dict = Body(PydanticUndefined))[source]

Set or change the signed-in user’s own password.

A user who has a password must give it; a missing and a wrong one get the same answer. A user who signs in through Google only has none to give and may set a first one.

async kavalai.backoffice.server.projects_create(request: Request, data: dict = Body(PydanticUndefined))[source]
async kavalai.backoffice.server.projects_get_by_id(project_id: UUID, request: Request)[source]
async kavalai.backoffice.server.projects_get_all(request: Request)[source]
async kavalai.backoffice.server.projects_update(project_id: UUID, request: Request, data: dict = Body(PydanticUndefined))[source]
async kavalai.backoffice.server.projects_reveal_password(project_id: UUID, request: Request)[source]

The project’s database password, for an administrator who asks for it.

Project listings never carry it (public_project()). An administrator can create projects and so may see their credentials; the request is a POST so nothing caches or prefetches it, and each one is logged.

async kavalai.backoffice.server.projects_delete(project_id: UUID, request: Request)[source]
async kavalai.backoffice.server.users_get_all(request: Request)[source]
async kavalai.backoffice.server.users_create(request: Request, data: dict = Body(PydanticUndefined))[source]
async kavalai.backoffice.server.users_update(user_id: UUID, request: Request, data: dict = Body(PydanticUndefined))[source]
async kavalai.backoffice.server.users_delete(user_id: UUID, request: Request)[source]
async kavalai.backoffice.server.agents_get_by_id(project_id: UUID, agent_id: UUID, request: Request)[source]
async kavalai.backoffice.server.workflows_render_svg(request: Request, data: dict = Body(PydanticUndefined))[source]

Render a workflow graph to an SVG diagram.

Accepts {"workflow": {...}} (or a bare workflow dict) and returns an image/svg+xml document. The agents page uses this to show an agent’s workflow as a backend-generated diagram (replacing the client-side build).

async kavalai.backoffice.server.agents_get_all(project_id: UUID, request: Request)[source]

Fetch all agents belonging to a specific project.

async kavalai.backoffice.server.agents_get_stats(project_id: UUID, request: Request, days: int = 7, agent_id: UUID | None = None)[source]

Fetch daily stats for agents in a specific project.

async kavalai.backoffice.server.agents_get_summary_stats(project_id: UUID, request: Request, agent_id: UUID | None = None)[source]

Fetch summary stats (last 30 days) for agents in a specific project.

async kavalai.backoffice.server.agents_get_sessions(project_id: UUID, request: Request, agent_id: UUID | None = None, search: str | None = None, external_id: str | None = None, start_date: datetime | None = None, end_date: datetime | None = None, limit: int = 50, offset: int = 0)[source]

Fetch session summaries for a specific project.

external_id matches the session’s caller-supplied key as a prefix. Evaluation runs record their sessions as eval:{tag}:{case}, so this is the click-through from a failing case to the conversation that produced it.

async kavalai.backoffice.server.agents_get_session_details(project_id: UUID, session_id: UUID, request: Request)[source]

Fetch all details (messages, runs, tasks) for a specific session.

async kavalai.backoffice.server.projects_get_llm_call_stats(project_id: UUID, request: Request, call_type: str | None = None, limit: int = 50, offset: int = 0, agent_id: UUID | None = None, session_id: UUID | None = None, run_id: UUID | None = None, search: str | None = None, start_date: datetime | None = None, end_date: datetime | None = None)[source]

Fetch paginated model call stats for a specific project.

agent_id, session_id and run_id narrow the list to the calls attributed to that agent, conversation or run — the run’s own calls are what the task debugger shows beside its tasks. search matches the model name and the request and response text; start_date and end_date bound when the call was recorded.

async kavalai.backoffice.server.projects_get_llm_call_summary(project_id: UUID, request: Request, call_type: str | None = None, agent_id: UUID | None = None, session_id: UUID | None = None, run_id: UUID | None = None, search: str | None = None, start_date: datetime | None = None, end_date: datetime | None = None)[source]

The token usage of the calls the list endpoint would return with the same filters — totals, per model and per day — across all of them, not one page.

kavalai.backoffice.server.rag_service_for_project(project: Project, model: str | None = None, normalizer=None, session_factory=None) → CollectionRagService[source]

The RAG service over the project’s agent database.

Both backends share one storage model, so the explorer endpoints only differ in how they reach the database: a SQLite project’s index lives in the same file as its agent tables, a PostgreSQL project’s in the project’s rag_schema (its agent schema unless one is set), reached through session_factory (the project’s sessionmaker unless one is given).

The service never provisions: the explorer only reads, and a registry or collection it would create is a write to a database that may be production. A read-only project’s SQLite file is opened with PRAGMA query_only besides.

async kavalai.backoffice.server.projects_rag_query(project_id: UUID, request: Request, query_data: dict = Body(PydanticUndefined))[source]

Execute a RAG query for a specific project.

model is optional: an existing collection is embedded with the model recorded for it, so a query needs only text.

async kavalai.backoffice.server.projects_rag_stats(project_id: UUID, request: Request)[source]

Fetch RAG statistics for a specific project.

async kavalai.backoffice.server.projects_rag_collections(project_id: UUID, request: Request)[source]

List a project’s RAG collections with their model, dimension and size.

The explorer takes the model a query is embedded with from here.

async kavalai.backoffice.server.projects_train_pca(project_id: UUID, request: Request, data: dict = Body(PydanticUndefined))[source]

Train the PCA projection of a collection, streaming progress as SSE.

The body is {"collection_name": ...}. A POST, because training is expensive and writes to the backoffice database, and a GET would be started by any image pointing at it.

async kavalai.backoffice.server.projects_test_connection(project_id: str, request: Request, data: dict = Body({}))[source]

Test the connection to a project’s database.

For new the body describes the database, and only administrators — who can create projects — may have the backoffice connect to an address of their choosing. For an existing project an empty body tests the saved settings, open to any member; a body tests those settings with the body’s changes applied, before they are saved, which only an owner — who can save them — may do. The stored password is reused under the rules of connection_fields(), so a changed host cannot be sent it.

async kavalai.backoffice.server.projects_get_members(project_id: UUID, request: Request)[source]
async kavalai.backoffice.server.projects_add_member(project_id: UUID, request: Request, data: dict = Body(PydanticUndefined))[source]
async kavalai.backoffice.server.projects_update_member(project_id: UUID, request: Request, data: dict = Body(PydanticUndefined))[source]
async kavalai.backoffice.server.projects_remove_member(project_id: UUID, user_id: UUID, request: Request)[source]

Copyright 2026 OÜ KAVAL AI (registry code 17393877)

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

kavalai.backoffice.project_service.PROJECT_FIELDS = frozenset({'db_host', 'db_name', 'db_password', 'db_port', 'db_schema', 'db_type', 'db_user', 'description', 'name', 'rag_schema', 'read_only'})

its name, description and connection settings. Everything else a project row carries — id, the timestamps, the caller’s role the list endpoint adds — is the server’s.

Type:

The fields of a project a client may set

kavalai.backoffice.project_service.project_fields(data: dict[str, Any]) → dict[str, Any][source]

The part of a request body that may become project columns.

The projects page sends the whole project back as it listed it, so a body may carry created_at as an ISO string, which the database refuses for a timestamp column; keys outside PROJECT_FIELDS are dropped rather than written.

kavalai.backoffice.project_service.CONNECTION_IDENTITY = ('db_type', 'db_host', 'db_port', 'db_user', 'db_name')

The fields naming the server and account a stored password belongs to. A stored password is reused only while all of them stay the same, so changing the host cannot send the existing password to another server.

kavalai.backoffice.project_service.public_project(project: Project, role: str | None = None) → dict[str, Any][source]

A project as the API returns it: every settable column except db_password, plus has_password.

The database password never leaves the server through a project listing: every member of a project receives these fields, viewers included, and the password would let them connect to the agent database directly, around their role and around the read-only setting, which the backoffice applies to its own connections only. role is the caller’s role, which the list endpoint adds.

kavalai.backoffice.project_service.connection_fields(stored: Project, data: dict[str, Any]) → dict[str, Any][source]

The columns a change to stored sets, with the password rules applied.

The password is write-only, so a form that never received it sends it empty or not at all: an empty or missing db_password keeps the stored one, and None clears it. The stored password is kept only while the fields of CONNECTION_IDENTITY stay the same; a change to any of them on a PostgreSQL project needs the password again, and without one the change is refused with a 400.

kavalai.backoffice.project_service.merged_project(stored: Project, fields: dict[str, Any]) → Project[source]

A detached copy of stored with fields applied, for testing a connection with settings that are not saved yet.

class kavalai.backoffice.project_service.ProjectService(session_maker: async_sessionmaker[AsyncSession])[source]

Bases: object

async get_user_projects(user_id: UUID) → list[dict[str, Any]][source]
async create_project(data: dict[str, Any], owner_id: UUID) → Project[source]
async update_project(project_id: UUID, data: dict[str, Any]) → Project | None[source]

Apply data to the project, keeping its password as connection_fields() decides; None when there is no such project.

async delete_project(project_id: UUID) → bool[source]
async get_members(project_id: UUID) → list[dict[str, Any]][source]
async add_member(project_id: UUID, user_id: UUID, role: ProjectRole) → None[source]
async update_member_role(project_id: UUID, user_id: UUID, new_role: ProjectRole) → None[source]
async remove_member(project_id: UUID, user_id: UUID) → None[source]
async test_connection(project: Project) → dict[str, str][source]
kavalai.backoffice.project_service.project_db_uri(project: Project) → str[source]

The URI of the agent database a project points at.

db_type decides how the connection fields are read: sqlite takes db_name as a file path, anything else builds a PostgreSQL URI from host, port, user, password and database.

kavalai.backoffice.project_service.project_schema(project: Project) → str | None[source]

The schema of the project’s agent database (None for SQLite).

kavalai.backoffice.project_service.project_rag_schema(project: Project) → str | None[source]

The schema of the project’s RAG collections: rag_schema when set, otherwise the agent database’s own schema (None for SQLite).

kavalai.backoffice.project_service.project_read_only(project: Project) → bool[source]

Whether the backoffice may only read the project’s agent database.

On unless the project says False: a project built from a form (Project(**data) on the connection test) carries no column default, and the safe reading of “unsaid” is read-only.

kavalai.backoffice.project_service.project_sessionmaker(project: Project) → async_sessionmaker[AsyncSession][source]

A sessionmaker for the agent database the project points at.

A read-only project gets connections the database itself holds to reads (get_sessionmaker()).

kavalai.backoffice.project_service.describe_project_database(project: Project) → str[source]

A log-safe description of the project’s database (no password).