openapi: 3.1.0
info:
  title: 1C Agent API
  version: 0.1.0
  description: Independent 1C agent API for project/chat orchestration with model calls, RAG and adapter calls.
servers:
  - url: http://localhost:8090
paths:
  /v1/health:
    get:
      operationId: getHealth
      summary: Service health
      responses:
        "200":
          description: Service is alive.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/HealthResponse"
  /v1/state:
    get:
      operationId: getState
      summary: Service state and counters
      responses:
        "200":
          description: Current internal state.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/StateResponse"
  /v1/projects:
    get:
      operationId: listProjects
      summary: List projects
      responses:
        "200":
          description: Project list
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ProjectsListResponse"
    post:
      operationId: createProject
      summary: Create project
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/ProjectCreateRequest"
      responses:
        "201":
          description: Project created
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ProjectResponse"
  /v1/projects/{project_id}:
    get:
      operationId: getProject
      summary: Get project by id
      parameters:
        - $ref: "#/components/parameters/ProjectId"
      responses:
        "200":
          description: Project
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ProjectResponse"
    patch:
      operationId: updateProject
      summary: Update project metadata
      parameters:
        - $ref: "#/components/parameters/ProjectId"
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/ProjectUpdateRequest"
      responses:
        "200":
          description: Updated project
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ProjectResponse"
    delete:
      operationId: deleteProject
      summary: Delete project
      parameters:
        - $ref: "#/components/parameters/ProjectId"
      responses:
        "204":
          description: Project deleted
  /v1/projects/{project_id}/chats:
    get:
      operationId: listChats
      summary: List chats in project
      parameters:
        - $ref: "#/components/parameters/ProjectId"
      responses:
        "200":
          description: Chats list
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ChatsListResponse"
    post:
      operationId: createChat
      summary: Create chat in project
      parameters:
        - $ref: "#/components/parameters/ProjectId"
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/ChatCreateRequest"
      responses:
        "201":
          description: Chat created
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ChatResponse"
  /v1/projects/{project_id}/chats/{chat_id}:
    get:
      operationId: getChat
      summary: Get chat config
      parameters:
        - $ref: "#/components/parameters/ProjectId"
        - $ref: "#/components/parameters/ChatId"
      responses:
        "200":
          description: Chat
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ChatResponse"
    patch:
      operationId: updateChat
      summary: Update chat config
      parameters:
        - $ref: "#/components/parameters/ProjectId"
        - $ref: "#/components/parameters/ChatId"
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/ChatUpdateRequest"
      responses:
        "200":
          description: Updated chat
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ChatResponse"
    delete:
      operationId: deleteChat
      summary: Delete chat
      parameters:
        - $ref: "#/components/parameters/ProjectId"
        - $ref: "#/components/parameters/ChatId"
      responses:
        "204":
          description: Chat deleted
  /v1/projects/{project_id}/chats/{chat_id}/messages:
    get:
      operationId: listMessages
      summary: List chat messages
      parameters:
        - $ref: "#/components/parameters/ProjectId"
        - $ref: "#/components/parameters/ChatId"
        - name: limit
          in: query
          required: false
          schema:
            type: integer
            minimum: 1
            default: 200
      responses:
        "200":
          description: Message history
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/MessagesListResponse"
    post:
      operationId: addMessage
      summary: Add message to chat
      parameters:
        - $ref: "#/components/parameters/ProjectId"
        - $ref: "#/components/parameters/ChatId"
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/AddMessageRequest"
      responses:
        "201":
          description: Message added
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/MessageResponse"
  /v1/projects/{project_id}/chats/{chat_id}/runtime:
    get:
      operationId: getChatRuntime
      summary: Resolve runtime context for project/chat
      parameters:
        - $ref: "#/components/parameters/ProjectId"
        - $ref: "#/components/parameters/ChatId"
      responses:
        "200":
          description: Runtime snapshot
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ChatRuntimeResponse"
  /v1/projects/{project_id}/chats/{chat_id}/turn:
    post:
      operationId: chatTurn
      summary: Make one agent turn
      parameters:
        - $ref: "#/components/parameters/ProjectId"
        - $ref: "#/components/parameters/ChatId"
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/TurnRequest"
      responses:
        "200":
          description: Turn result
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/TurnResponse"
  /v1/projects/{project_id}/chats/{chat_id}/onec-tool:
    post:
      operationId: callOneCTool
      summary: Call 1C adapter tool
      parameters:
        - $ref: "#/components/parameters/ProjectId"
        - $ref: "#/components/parameters/ChatId"
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/ToolCallRequest"
      responses:
        "200":
          description: Tool result
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ToolCallResponse"
  /v1/models:
    get:
      operationId: listModels
      summary: List candidate models from registry
      responses:
        "200":
          description: Models list
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ModelsResponse"
  /v1/providers:
    get:
      operationId: listProviders
      summary: List provider registry
      responses:
        "200":
          description: Providers list
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ProvidersResponse"
components:
  parameters:
    ProjectId:
      name: project_id
      in: path
      required: true
      schema:
        type: string
    ChatId:
      name: chat_id
      in: path
      required: true
      schema:
        type: string
  schemas:
    TraceEnvelope:
      type: object
      properties:
        trace_id:
          type: string
      required:
        - trace_id
    ErrorPayload:
      allOf:
        - $ref: "#/components/schemas/TraceEnvelope"
        - type: object
          properties:
            status:
              type: string
              enum: [error]
            error:
              type: object
              required:
                - message
              properties:
                message:
                  type: string
                code:
                  type: string
          required: [status, error]
    Project:
      type: object
      required: [id, name, description, metadata, created_at, updated_at]
      properties:
        id:
          type: string
        name:
          type: string
        description:
          type: string
        metadata:
          type: object
          additionalProperties: true
        created_at:
          type: string
        updated_at:
          type: string
    Chat:
      type: object
      required: [id, project_id, title, provider_id, temperature, max_tokens, rag_profile, created_at, updated_at]
      properties:
        id:
          type: string
        project_id:
          type: string
        title:
          type: string
        model_id:
          type: string
          nullable: true
        provider_id:
          type: string
        base_url:
          type: string
          nullable: true
        served_model_name:
          type: string
          nullable: true
        temperature:
          type: number
        max_tokens:
          type: integer
        rag_profile:
          type: string
        rag_limit:
          type: integer
          nullable: true
        system_prompt:
          type: string
        metadata:
          type: object
          additionalProperties: true
        created_at:
          type: string
        updated_at:
          type: string
    Message:
      type: object
      required: [id, project_id, chat_id, role, content, payload, created_at]
      properties:
        id:
          type: string
        project_id:
          type: string
        chat_id:
          type: string
        role:
          type: string
          enum: [user, assistant, system, tool]
        content:
          type: string
        payload:
          type: object
          additionalProperties: true
        created_at:
          type: string
    ProjectCreateRequest:
      type: object
      required: [name]
      properties:
        name:
          type: string
        description:
          type: string
        metadata:
          type: object
          additionalProperties: true
    ProjectUpdateRequest:
      type: object
      properties:
        name:
          type: string
        description:
          type: string
        metadata:
          type: object
          additionalProperties: true
    ChatCreateRequest:
      type: object
      required: [title]
      properties:
        title:
          type: string
        model_id:
          type: string
        provider_id:
          type: string
        base_url:
          type: string
        served_model_name:
          type: string
        temperature:
          type: number
        max_tokens:
          type: integer
        rag_profile:
          type: string
        rag_limit:
          type: integer
        system_prompt:
          type: string
        metadata:
          type: object
          additionalProperties: true
    ChatUpdateRequest:
      type: object
      properties:
        title:
          type: string
        model_id:
          type: string
        provider_id:
          type: string
        base_url:
          type: string
        served_model_name:
          type: string
        temperature:
          type: number
        max_tokens:
          type: integer
        rag_profile:
          type: string
        rag_limit:
          type: integer
        system_prompt:
          type: string
        metadata:
          type: object
          additionalProperties: true
    AddMessageRequest:
      type: object
      required: [role, content]
      properties:
        role:
          type: string
          enum: [user, assistant, system, tool]
        content:
          type: string
        payload:
          type: object
          additionalProperties: true
    ToolCallRequest:
      type: object
      required: [method]
      properties:
        method:
          type: string
        params:
          type: object
          additionalProperties: true
    TurnRequest:
      type: object
      required: [message]
      properties:
        message:
          type: string
        use_rag:
          type: boolean
          default: true
        rag_profile:
          type: string
        rag_limit:
          type: integer
        provider_id:
          type: string
        temperature:
          type: number
        max_tokens:
          type: integer
        history_limit:
          type: integer
          minimum: 1
        adapter_calls:
          type: array
          items:
            type: object
            required: [method]
            properties:
              method:
                type: string
              params:
                type: object
                additionalProperties: true
        served_model_name:
          type: string
        base_url:
          type: string
    HealthResponse:
      allOf:
        - $ref: "#/components/schemas/TraceEnvelope"
        - type: object
          properties:
            status:
              type: string
            service:
              type: string
            time:
              type: string
          required: [status, service, time]
    StateResponse:
      allOf:
        - $ref: "#/components/schemas/TraceEnvelope"
        - type: object
          required:
            - service
            - started_at
            - uptime_seconds
            - state
            - providers
            - paths
          properties:
            service:
              type: string
            started_at:
              type: string
            uptime_seconds:
              type: number
            state:
              type: object
              required: [projects, chats, messages]
              properties:
                projects:
                  type: integer
                chats:
                  type: integer
                messages:
                  type: integer
            providers:
              type: object
              required: [count, ids]
              properties:
                count:
                  type: integer
                ids:
                  type: array
                  items:
                    type: string
            paths:
              type: object
              required: [adapter_url, database]
              properties:
                adapter_url:
                  type: string
                  nullable: true
                database:
                  type: string
    ProjectsListResponse:
      allOf:
        - $ref: "#/components/schemas/TraceEnvelope"
        - type: object
          properties:
            projects:
              type: array
              items:
                $ref: "#/components/schemas/Project"
    ProjectResponse:
      allOf:
        - $ref: "#/components/schemas/TraceEnvelope"
        - type: object
          properties:
            project:
              $ref: "#/components/schemas/Project"
    ChatsListResponse:
      allOf:
        - $ref: "#/components/schemas/TraceEnvelope"
        - type: object
          properties:
            project_id:
              type: string
            chats:
              type: array
              items:
                $ref: "#/components/schemas/Chat"
    ChatResponse:
      allOf:
        - $ref: "#/components/schemas/TraceEnvelope"
        - type: object
          properties:
            chat:
              $ref: "#/components/schemas/Chat"
            routing:
              type: object
              nullable: true
    MessageResponse:
      allOf:
        - $ref: "#/components/schemas/TraceEnvelope"
        - type: object
          properties:
            message:
              $ref: "#/components/schemas/Message"
    MessagesListResponse:
      allOf:
        - $ref: "#/components/schemas/TraceEnvelope"
        - type: object
          properties:
            project_id:
              type: string
            chat_id:
              type: string
            messages:
              type: array
              items:
                $ref: "#/components/schemas/Message"
    ChatRuntimeResponse:
      allOf:
        - $ref: "#/components/schemas/TraceEnvelope"
        - type: object
          properties:
            project_id:
              type: string
            chat_id:
              type: string
            runtime:
              type: object
              required:
                - project
                - chat
                - provider
                - model
                - runtime_settings
                - adapter
              properties:
                project:
                  type: object
                  properties:
                    id:
                      type: string
                    name:
                      type: string
                chat:
                  type: object
                  properties:
                    id:
                      type: string
                    title:
                      type: string
                provider:
                  type: object
                  properties:
                    id:
                      type: string
                    type:
                      type: string
                    base_url:
                      type: string
                    model:
                      type: string
                model:
                  type: object
                  properties:
                    chat_model_id:
                      type: string
                      nullable: true
                    selected_model_id:
                      type: string
                    served_model:
                      type: string
                    base_url:
                      type: string
                    route:
                      type: object
                      additionalProperties: true
                runtime_settings:
                  type: object
                  properties:
                    temperature:
                      type: number
                    max_tokens:
                      type: integer
                    rag_profile:
                      type: string
                    rag_limit:
                      type: integer
                      nullable: true
                    system_prompt:
                      type: string
                adapter:
                  type: object
                  properties:
                    url:
                      type: string
                      nullable: true
    TurnResponse:
      allOf:
        - $ref: "#/components/schemas/TraceEnvelope"
        - type: object
          properties:
            project_id:
              type: string
            chat_id:
              type: string
            user_message:
              $ref: "#/components/schemas/Message"
            assistant_message:
              $ref: "#/components/schemas/Message"
            rag:
              type: object
              nullable: true
            tools:
              type: array
              items:
                type: object
              nullable: true
            guardrail:
              type: object
              nullable: true
    ToolCallResponse:
      allOf:
        - $ref: "#/components/schemas/TraceEnvelope"
        - type: object
          properties:
            tool_result:
              type: object
            message:
              $ref: "#/components/schemas/Message"
    ModelsResponse:
      allOf:
        - $ref: "#/components/schemas/TraceEnvelope"
        - type: object
          properties:
            models:
              type: array
              items:
                type: object
    ProvidersResponse:
      allOf:
        - $ref: "#/components/schemas/TraceEnvelope"
        - type: object
          properties:
            providers:
              type: object
              additionalProperties:
                type: object
