api_service.py


Overview

The api_service.py file provides service-layer abstractions to interact with the underlying database models related to API tokens and conversational sessions within the InfiniFlow system. It primarily defines two service classes — APITokenService and API4ConversationService — which encapsulate database operations such as querying, updating, and aggregating data related to API tokens and conversations.

This file acts as a bridge between the database models and higher-level application logic, providing reusable and transaction-safe methods to manage API tokens and conversation data efficiently. It leverages the Peewee ORM for database interactions and uses a shared database connection context for executing queries.


Classes and Methods

APITokenService

A service class for managing API tokens. It extends a common base service (CommonService) and operates on the APIToken model.

Properties:

Methods:

used(token: str) -> peewee.ModelUpdate

Marks an API token as used by updating its timestamp fields.


API4ConversationService

A service class managing conversational session records associated with API version 4 conversations. It extends CommonService and operates on the API4Conversation model.

Properties:

Methods:

get_list(dialog_id: int, tenant_id: int, page_number: int, items_per_page: int, orderby: str, desc: bool, id: int, user_id: Optional[int] = None, include_dsl: bool = True, keywords: str = "", from_date: Optional[str] = None, to_date: Optional[str] = None) -> Tuple[int, List[dict]]

Retrieves a paginated list of conversation sessions filtered by various parameters.


append_message(id: int, conversation: dict) -> int

Updates a conversation session with new message data and increments the round count.


stats(tenant_id: int, from_date: str, to_date: str, source: Optional[str] = None) -> List[dict]

Generates statistical aggregates of conversation sessions over a date range, optionally filtering by source.


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

The following Mermaid class diagram illustrates the structure of this file, showing the service classes, their inheritance from CommonService, and key methods:

classDiagram
    class CommonService {
        <<abstract>>
        +update_by_id(id, data)
        +<other common methods>()
    }
    class APITokenService {
        +model = APIToken
        +used(token)
    }
    class API4ConversationService {
        +model = API4Conversation
        +get_list(dialog_id, tenant_id, page_number, items_per_page, orderby, desc, id, user_id, include_dsl, keywords, from_date, to_date)
        +append_message(id, conversation)
        +stats(tenant_id, from_date, to_date, source)
    }
    CommonService <|-- APITokenService
    CommonService <|-- API4ConversationService

Summary

api_service.py provides robust service interfaces for managing API tokens and conversation sessions in the InfiniFlow system. It abstracts complex query logic, filtering, pagination, and aggregation into clear, reusable class methods. This design promotes clean separation between database access and application logic, facilitates maintainability, and supports scalable analytics and token management workflows.