openapi: 3.1.2
info:
  title: シンプルAI (CR) API
  version: 0.1.0
  description: |
    シンプルAIサービスのAPIです。
    このAPIは、シンプルAIサービスのリソースに対するCRUD操作を行います。

servers:
  - url: https://secure.sakura.ad.jp/cloud/zone/{zone_id}/api/simpleai/1.0
    variables:
      zone_id:
        enum:
          - is1a
        default: is1a
        description: さくらのクラウド ゾーンID

security:
  - sakuraCloudApiKey: []

paths:
  /plans:
    get:
      summary: Plan一覧取得
      description: 選択可能なPlanの一覧を取得します。
      operationId: listPlans
      tags:
        - plans
      responses:
        '200':
          description: Plan一覧の取得成功
          content:
            application/json:
              schema:
                type: object
                required:
                  - plans
                properties:
                  plans:
                    type: array
                    items:
                      $ref: '#/components/schemas/Plan'
        default:
          description: エラー
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  /plans/{plan_id}:
    get:
      summary: Plan詳細取得
      description: 指定したPlanの情報を取得します。
      operationId: getPlan
      tags:
        - plans
      parameters:
        - $ref: '#/components/parameters/PlanId'
      responses:
        '200':
          description: Plan詳細の取得成功
          content:
            application/json:
              schema:
                type: object
                required:
                  - plan
                properties:
                  plan:
                    $ref: '#/components/schemas/Plan'
        default:
          description: エラー
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  /workspaces:
    get:
      summary: Workspace一覧取得
      description: プロジェクトが所有するWorkspaceの一覧を取得します。
      operationId: listWorkspaces
      tags:
        - workspaces
      responses:
        '200':
          description: Workspace一覧の取得成功
          content:
            application/json:
              schema:
                type: object
                required:
                  - workspaces
                properties:
                  workspaces:
                    type: array
                    items:
                      $ref: '#/components/schemas/Workspace'
        default:
          description: エラー
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

    post:
      summary: Workspace作成
      description: 指定されたPlanを適用した新しいWorkspaceを作成します。
      operationId: createWorkspace
      tags:
        - workspaces
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - name
                - plan_id
              properties:
                plan_id:
                  type: string
                  format: uuid
                  description: 作成時に適用するPlanのID
                name:
                  type: string
                  description: Workspaceの名前
                  minLength: 1
                  maxLength: 255
                  example: 'Production Workspace'
      responses:
        '201':
          description: Workspaceの作成成功
          content:
            application/json:
              schema:
                type: object
                required:
                  - workspace
                properties:
                  workspace:
                    $ref: '#/components/schemas/Workspace'
                    example:
                      id: '550e8400-e29b-41d4-a716-446655440000'
                      name: 'Production Workspace'
                      plan:
                        id: '11111111-1111-4111-8111-111111111111'
                        applied_at: '2026-01-14T12:34:56Z'
                      created_at: '2026-01-14T12:34:56Z'
        default:
          description: エラー
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  /workspaces/{workspace_id}:
    get:
      summary: Workspace詳細取得
      description: 指定したWorkspaceの情報を取得します。
      operationId: getWorkspace
      tags:
        - workspaces
      parameters:
        - $ref: '#/components/parameters/WorkspaceId'
      responses:
        '200':
          description: Workspace詳細の取得成功
          content:
            application/json:
              schema:
                type: object
                required:
                  - workspace
                properties:
                  workspace:
                    $ref: '#/components/schemas/Workspace'
        default:
          description: エラー
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

    put:
      summary: Workspace更新
      description: 指定したWorkspaceを更新します。
      operationId: updateWorkspace
      tags:
        - workspaces
      parameters:
        - $ref: '#/components/parameters/WorkspaceId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - name
              properties:
                name:
                  type: string
                  description: Workspaceの名前
                  minLength: 1
                  maxLength: 255
                  example: 'Production Workspace'
      responses:
        '200':
          description: Workspaceの更新成功
          content:
            application/json:
              schema:
                type: object
                required:
                  - workspace
                properties:
                  workspace:
                    $ref: '#/components/schemas/Workspace'
        default:
          description: エラー
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

    delete:
      summary: Workspace削除
      description: |
        指定したWorkspaceを削除します。
        有効なAPI Keyが残っている場合は削除できません。
        CR版の間は、GET /api-keysおよびPOST /api-keysを提供するため、デフォルトWorkspaceを削除できません。
      operationId: deleteWorkspace
      tags:
        - workspaces
      parameters:
        - $ref: '#/components/parameters/WorkspaceId'
      responses:
        '200':
          description: Workspaceの削除成功
        default:
          description: エラー
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  /workspaces/{workspace_id}/plan:
    put:
      summary: WorkspaceのPlan更新
      description: Workspaceに適用するPlanを更新します。
      operationId: updateWorkspacePlan
      tags:
        - workspaces
      parameters:
        - $ref: '#/components/parameters/WorkspaceId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - plan_id
              properties:
                plan_id:
                  type: string
                  format: uuid
                  description: 更新先Planの識別子（UUID v4）
                  example: '550e8400-e29b-41d4-a716-446655440000'
      responses:
        '200':
          description: WorkspaceのPlan更新成功
          content:
            application/json:
              schema:
                type: object
                required:
                  - workspace
                properties:
                  workspace:
                    $ref: '#/components/schemas/Workspace'
        default:
          description: エラー
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  /workspaces/{workspace_id}/api-keys:
    get:
      summary: WorkspaceのAPIキー一覧取得
      description: 指定したWorkspaceに所属するAPIキーの一覧を取得します。
      operationId: listWorkspaceApiKeys
      tags:
        - workspaces
      parameters:
        - $ref: '#/components/parameters/WorkspaceId'
      responses:
        '200':
          description: WorkspaceのAPIキー一覧の取得成功
          content:
            application/json:
              schema:
                type: object
                required:
                  - api_keys
                properties:
                  api_keys:
                    type: array
                    items:
                      $ref: '#/components/schemas/ApiKey'
        default:
          description: エラー
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

    post:
      summary: WorkspaceへのAPIキー発行
      description: |
        指定したWorkspaceに新しいAPIキーを発行します。シークレットは発行時に一度だけ返却されます。
        再度取得できないため、安全に保管してください。
      operationId: createWorkspaceApiKey
      tags:
        - workspaces
      parameters:
        - $ref: '#/components/parameters/WorkspaceId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - name
              properties:
                name:
                  type: string
                  description: APIキーの名前
                  example: 'Production API Key'
                  minLength: 1
                  maxLength: 255
                tags:
                  type: array
                  maxItems: 10
                  description: APIキーに紐付けるタグ
                  items:
                    type: string
                    maxLength: 32
                  example:
                    - 'production'
                    - 'team-a'
      responses:
        '201':
          description: WorkspaceへのAPIキー発行成功
          content:
            application/json:
              schema:
                type: object
                required:
                  - api_key
                properties:
                  api_key:
                    allOf:
                      - $ref: '#/components/schemas/ApiKey'
                      - type: object
                        required:
                          - secret
                        properties:
                          secret:
                            type: string
                            pattern: '^sakura_simpleai_'
                            description: |
                              APIキーのシークレット（一度だけ返却されます）。
                              形式: sakura_simpleai_xxx
                            example: 'sakura_simpleai_4My5bIlw53TBg0Tya4tVYbizxB8gKnSg6ZllH92lHW8='
        default:
          description: エラー
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  /api-keys:
    get:
      summary: APIキー一覧取得
      description: |
        デフォルトWorkspaceに所属するAPIキーの一覧を取得します。
        正式版リリース時に廃止します。GET /workspaces/{workspace_id}/api-keysを使用してください。
      operationId: listApiKeys
      tags:
        - api-keys
      deprecated: true
      responses:
        '200':
          description: APIキー一覧の取得成功
          content:
            application/json:
              schema:
                type: object
                required:
                  - api_keys
                properties:
                  api_keys:
                    type: array
                    items:
                      $ref: '#/components/schemas/ApiKey'
        default:
          description: エラー
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

    post:
      summary: APIキー作成
      description: |
        新しいAPIキーをデフォルトWorkspaceに作成します。シークレットは作成時に一度だけ返却されます。
        再度取得できないため、安全に保管してください。
        正式版リリース時に廃止します。POST /workspaces/{workspace_id}/api-keysを使用してください。
      operationId: createApiKey
      tags:
        - api-keys
      deprecated: true
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - name
              properties:
                name:
                  type: string
                  description: APIキーの名前
                  example: 'Production API Key'
                  minLength: 1
                  maxLength: 255
                tags:
                  type: array
                  maxItems: 10
                  description: APIキーに紐付けるタグ
                  items:
                    type: string
                    maxLength: 32
                  example:
                    - 'production'
                    - 'team-a'
      responses:
        '201':
          description: APIキーの作成成功
          content:
            application/json:
              schema:
                type: object
                required:
                  - api_key
                properties:
                  api_key:
                    allOf:
                      - $ref: '#/components/schemas/ApiKey'
                      - type: object
                        required:
                          - secret
                        properties:
                          secret:
                            type: string
                            pattern: '^sakura_simpleai_'
                            description: |
                              APIキーのシークレット（一度だけ返却されます）。
                              形式: sakura_simpleai_xxx
                            example: 'sakura_simpleai_4My5bIlw53TBg0Tya4tVYbizxB8gKnSg6ZllH92lHW8='
        default:
          description: エラー
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  /api-keys/{api_key_id}:
    get:
      summary: APIキー取得
      description: 指定したAPIキーの情報を取得します（シークレットは除く）
      operationId: getApiKey
      tags:
        - api-keys
      parameters:
        - $ref: '#/components/parameters/ApiKeyId'
      responses:
        '200':
          description: APIキー情報の取得成功
          content:
            application/json:
              schema:
                type: object
                required:
                  - api_key
                properties:
                  api_key:
                    $ref: '#/components/schemas/ApiKey'
        default:
          description: エラー
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

    put:
      summary: APIキー更新
      description: 既存のAPIキーの名前やタグを更新します
      operationId: updateApiKey
      tags:
        - api-keys
      parameters:
        - $ref: '#/components/parameters/ApiKeyId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - name
                - tags
              properties:
                name:
                  type: string
                  description: APIキーの新しい名前
                  example: 'Updated API Key'
                  minLength: 1
                  maxLength: 255
                tags:
                  type: array
                  maxItems: 10
                  description: APIキーの新しいタグ
                  items:
                    type: string
                    maxLength: 32
                  example:
                    - 'production'
                    - 'team-b'
      responses:
        '200':
          description: APIキーの更新成功
          content:
            application/json:
              schema:
                type: object
                required:
                  - api_key
                properties:
                  api_key:
                    $ref: '#/components/schemas/ApiKey'
        default:
          description: エラー
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

    delete:
      summary: APIキー削除
      description: APIキーを削除します
      operationId: deleteApiKey
      tags:
        - api-keys
      parameters:
        - $ref: '#/components/parameters/ApiKeyId'
      responses:
        '200':
          description: APIキーの削除成功
        default:
          description: エラー
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

components:
  securitySchemes:
    sakuraCloudApiKey:
      type: http
      scheme: basic
      description: |
        さくらのクラウドのAPIキーを利用した認証（HTTP Basic認証）です。
        ユーザー名にはさくらのクラウドAPIのアクセストークンを、
        パスワードにはアクセスシークレットを指定してください。

  schemas:
    ApiKey:
      type: object
      required:
        - id
        - name
        - created_at
        - tags
      properties:
        id:
          type: string
          format: uuid
          description: APIキーの一意識別子（UUID v4）
          example: '550e8400-e29b-41d4-a716-446655440000'
        workspace_id:
          type: string
          format: uuid
          description: APIキーが所属するWorkspaceの識別子（UUID v4）
        name:
          type: string
          description: APIキーの名前
          example: 'Production API Key'
        created_at:
          type: string
          format: date-time
          description: 作成日時（RFC3339形式）
          example: '2026-01-14T12:34:56Z'
        tags:
          type: array
          maxItems: 10
          description: APIキーに紐付けるタグ
          items:
            type: string
            maxLength: 32
          example:
            - 'production'
            - 'team-a'

    Plan:
      type: object
      required:
        - id
        - name
        - credit_limits
      properties:
        id:
          type: string
          format: uuid
          description: Planの一意識別子（UUID v4）
          example: '550e8400-e29b-41d4-a716-446655440000'
        name:
          type: string
          description: Planの名前
          example: 'Standard'
        credit_limits:
          type: array
          description: Planに適用されるクレジット制限の一覧
          items:
            $ref: '#/components/schemas/CreditLimit'

    CreditLimit:
      type: object
      required:
        - category
        - window_seconds
        - credits
      properties:
        category:
          type: string
          description: 推論種別
          enum:
            - chat
            - embedding
          example: chat
        window_seconds:
          type: integer
          minimum: 1
          description: クレジット制限の時間窓（秒）
          example: 3600
        credits:
          type: integer
          minimum: -1
          description: 時間窓内のクレジット上限。-1は無制限を表します。
          example: 100

    Workspace:
      type: object
      required:
        - id
        - name
        - plan
        - created_at
      properties:
        id:
          type: string
          format: uuid
          description: Workspaceの一意識別子（UUID v4）
          example: '550e8400-e29b-41d4-a716-446655440000'
        name:
          type: string
          description: Workspaceの名前
          example: 'Production Workspace'
        plan:
          $ref: '#/components/schemas/AppliedPlan'
          description: Workspaceに現在適用されているPlan。
        created_at:
          type: string
          format: date-time
          description: 作成日時（RFC3339形式）
          example: '2026-01-14T12:34:56Z'

    AppliedPlan:
      type: object
      required:
        - id
        - applied_at
      properties:
        id:
          type: string
          format: uuid
          description: 適用されているPlanの識別子（UUID v4）
          example: '550e8400-e29b-41d4-a716-446655440000'
        applied_at:
          type: string
          format: date-time
          description: Planが適用された日時（RFC3339形式）
          example: '2026-01-14T12:34:56Z'

    ErrorResponse:
      type: object
      required:
        - is_fatal
        - error_code
        - error_msg
      properties:
        is_fatal:
          type: boolean
          description: エラーレスポンスでは常にtrue
          example: true
        error_code:
          type: string
          description: エラーコード識別子
          example: 'unauthorized'
        error_msg:
          type: string
          description: エラーメッセージ
          example: 'API key is invalid'

  parameters:
    ApiKeyId:
      name: api_key_id
      in: path
      required: true
      description: APIキーの識別子（UUID v4）
      schema:
        type: string
        format: uuid
      example: '550e8400-e29b-41d4-a716-446655440000'
    PlanId:
      name: plan_id
      in: path
      required: true
      description: Planの識別子（UUID v4）
      schema:
        type: string
        format: uuid
      example: '550e8400-e29b-41d4-a716-446655440000'
    WorkspaceId:
      name: workspace_id
      in: path
      required: true
      description: Workspaceの識別子（UUID v4）
      schema:
        type: string
        format: uuid
      example: '550e8400-e29b-41d4-a716-446655440000'

tags:
  - name: api-keys
    description: APIキー管理操作
  - name: plans
    description: Plan管理操作
  - name: workspaces
    description: Workspace管理操作
