# Apply AI library organization

> Adds valid Studio tag and collection links, refines kind only when it
is other, and fills nextAction only when empty. Existing links and
scalar values are preserved. Archived or stale items are rejected.


`POST /library/profiles/{studioId}/items/{itemId}/ai/organize`

## OpenAPI

````yaml openapi/agata-v1.yaml post /library/profiles/{studioId}/items/{itemId}/ai/organize
openapi: 3.0.3
info:
  title: Agata canonical API
  version: 1.0.0
  description: Canonical contract of the Agata Go API. Every documented route is
    registered in internal/delivery/http/handler.go. Monetary amounts are
    expressed in cents and local date keys use YYYY-MM-DD.
servers:
  - url: http://localhost:{port}/api
    description: Local server
    variables:
      port:
        default: "8080"
        description: Local HTTP port
  - url: "{scheme}://{host}/api"
    description: Configurable server
    variables:
      scheme:
        default: https
        enum:
          - http
          - https
      host:
        default: api.example.com
tags:
  - name: Health check
    description: Health-check operations.
  - name: Users
    description: Users domain operations.
  - name: Profiles
    description: Studio domain operations.
  - name: Organizations
    description: Company accounts that own people and Studios. Not followable.
  - name: Public profiles
    description: Public studio reads and one-way follows. Membership is not required.
  - name: Membership
    description: Studio membership plans, Connect status, and fan subscriptions.
  - name: Messages
    description: 1:1 owner chat between a studio owner and a subscriber with the
      owner_chat perk.
  - name: Feedback
    description: Feedback domain operations.
  - name: Internal system
    description: Internal system operations.
  - name: AI capabilities
    description: Durable leases, outcomes, and reversible effects for integrated AI actions.
  - name: Finance
    description: Finance domain operations.
  - name: Goals
    description: Goals domain operations.
  - name: Pomodoro
    description: Pomodoro domain operations.
  - name: Activities
    description: Activities domain operations.
  - name: Calendar
    description: Calendar domain operations.
  - name: Library
    description: Library domain operations.
  - name: Health
    description: Health domain operations.
  - name: Nutrition
    description: Nutrition domain operations.
  - name: Notebook
    description: Notebook domain operations.
  - name: Week plan
    description: Week-plan domain operations.
  - name: Files
    description: Files domain operations.
  - name: Tags
    description: Tags domain operations.
  - name: Tasks
    description: Tasks domain operations.
  - name: Projects
    description: Projects domain operations.
  - name: Internal
    description: Internal integrations protected by Bearer session.
  - name: Internal cron
    description: Internal cron operations.
paths:
  /library/profiles/{studioId}/items/{itemId}/ai/organize:
    post:
      tags:
        - Library
        - AI capabilities
      operationId: organizeLibraryItemWithAI
      summary: Apply AI library organization
      description: |
        Adds valid Studio tag and collection links, refines kind only when it
        is other, and fills nextAction only when empty. Existing links and
        scalar values are preserved. Archived or stale items are rejected.
      security:
        - sessionBearer: []
      parameters:
        - $ref: "#/components/parameters/AIStudioId"
        - $ref: "#/components/parameters/AILibraryItemId"
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/AILibraryOrganizationInput"
      responses:
        "200":
          description: Applied and skipped organization effects.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/AILibraryOrganizationResultResponse"
        "400":
          $ref: "#/components/responses/BadRequest"
        "401":
          $ref: "#/components/responses/Unauthorized"
        "403":
          $ref: "#/components/responses/Forbidden"
        "404":
          $ref: "#/components/responses/NotFound"
        "409":
          $ref: "#/components/responses/Conflict"
        "500":
          $ref: "#/components/responses/ServerError"
components:
  parameters:
    AIStudioId:
      name: studioId
      in: path
      required: true
      description: Studio identifier used for authorization and operation scope.
      schema:
        type: string
        format: uuid
    AILibraryItemId:
      name: itemId
      in: path
      required: true
      schema:
        type: string
        format: uuid
  schemas:
    AIEffectLease:
      type: object
      required:
        - operationId
        - leaseOwner
        - fencingToken
      properties:
        operationId:
          type: string
          format: uuid
        leaseOwner:
          type: string
          format: uuid
        fencingToken:
          type: integer
          format: int64
          minimum: 1
    AILibraryItem:
      type: object
      required:
        - id
        - studioId
        - createdBy
        - kind
        - primaryUrl
        - title
        - status
        - revision
        - visibility
        - createdAt
        - updatedAt
      properties:
        id:
          type: string
          format: uuid
        studioId:
          type: string
          format: uuid
        createdBy:
          type: string
          format: uuid
        kind:
          type: string
        primaryUrl:
          type: string
        title:
          type: string
        status:
          type: string
        archivedAt:
          type: string
          format: date-time
        nextAction:
          type: string
        revision:
          type: integer
          format: int64
          minimum: 1
        note:
          type: string
        thumbnailUrl:
          type: string
        emailReminderEnabled:
          type: boolean
        emailReminderOffsetDays:
          type: integer
        emailReminderTime:
          type: string
        emailReminderTimeZone:
          type: string
        visibility:
          type: string
          enum:
            - members
            - subscribers
            - followers
            - public
        createdAt:
          type: string
          format: date-time
        updatedAt:
          type: string
          format: date-time
    AILibraryOrganizationInput:
      allOf:
        - $ref: "#/components/schemas/AIEffectLease"
        - type: object
          required:
            - expectedRevision
            - sourceFingerprint
            - tagIds
            - collectionIds
          properties:
            expectedRevision:
              type: integer
              format: int64
              minimum: 1
            sourceFingerprint:
              type: string
              minLength: 1
            tagIds:
              type: array
              maxItems: 3
              uniqueItems: true
              items:
                type: string
                format: uuid
            collectionIds:
              type: array
              maxItems: 2
              uniqueItems: true
              items:
                type: string
                format: uuid
            kind:
              type: string
              nullable: true
              enum:
                - video
                - music
                - series
                - movie
                - book
            nextAction:
              type: string
              nullable: true
              enum:
                - read
                - watch
                - listen
                - review
    AILibraryOrganizationResult:
      type: object
      required:
        - item
        - changedCount
        - skippedCount
      properties:
        item:
          $ref: "#/components/schemas/AILibraryItem"
        changedCount:
          type: integer
          minimum: 0
        skippedCount:
          type: integer
          minimum: 0
    AILibraryOrganizationResultResponse:
      type: object
      required:
        - data
      properties:
        data:
          $ref: "#/components/schemas/AILibraryOrganizationResult"
        request_id:
          type: string
          format: uuid
    ErrorDetail:
      type: object
      required:
        - message
      properties:
        code:
          type: string
          example: VALIDATION_ERROR
        message:
          type: string
          example: The request is not valid.
        limit_key:
          type: string
        limit:
          type: integer
        required_plan:
          type: string
          enum:
            - pro
            - business
        resolution:
          type: string
          enum:
            - upgrade_plan
            - purchase_addon
        addon_key:
          type: string
          enum:
            - extra_members
            - ai_credit_packs
    ErrorResponse:
      type: object
      required:
        - error
      properties:
        error:
          $ref: "#/components/schemas/ErrorDetail"
        request_id:
          type: string
          format: uuid
  responses:
    BadRequest:
      description: Invalid request.
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/ErrorResponse"
    Unauthorized:
      description: Missing or invalid authentication.
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/ErrorResponse"
    Forbidden:
      description: Acceso denegado.
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/ErrorResponse"
    NotFound:
      description: Recurso no encontrado.
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/ErrorResponse"
    Conflict:
      description: Conflict with the current state.
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/ErrorResponse"
    ServerError:
      description: Internal server error.
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/ErrorResponse"
````
