openapi: 3.1.0
info:
  title: BetterThread API
  version: 0.1.0
  description: "Customer scoring contract. Authenticate with a secret API key
    (`Authorization: Bearer sk_…`). These are the same four operations the
    Node.js server SDK wraps. Portal and auth routes are not part of this
    document."
servers:
  - url: http://localhost:3000/v1
    description: Local development API.
tags:
  - name: Policies
    description: Read the site policy used for scoring.
  - name: Comments
    description: Score drafts and keep the originality index in sync.
paths:
  /policies:
    get:
      tags:
        - Policies
      operationId: getPolicy
      summary: Read the site policy
      description: Criteria, thresholds, and which enabled criteria are advisory.
      security:
        - apiKey: []
      responses:
        "200":
          description: The site policy.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Policy"
              examples:
                default:
                  $ref: "#/components/examples/defaultPolicy"
        "401":
          $ref: "#/components/responses/InvalidKey"
  /comments/score:
    post:
      tags:
        - Comments
      operationId: scoreComment
      summary: Score a comment
      description: |-
        Score one comment against the site policy. Relevance compares it with the
        parent post, so the request includes that post's text. Send `contextId`
        when the post has a stable id so comments share one originality pool.
        Without it, the pool is the MD5 of the exact `contextText`.

        Each accepted call writes one decision and counts toward usage. The check
        is rate-limited per API key. `commentText` is at most 2000 characters;
        `contextText` is at most 8000.
      security:
        - apiKey: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/ScoreRequest"
            examples:
              withId:
                summary: Parent post with a stable id
                value:
                  contextId: article-42
                  contextText: A review of the 2024 local election results.
                  commentText: The third paragraph contradicts the study it cites.
              textOnly:
                summary: Parent post without an id
                value:
                  contextText: A review of the 2024 local election results.
                  commentText: Turnout fell in the wards that changed hands.
      responses:
        "201":
          description: The decision created for this check.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ScoreResponse"
              example:
                outcome: failed
                enforcement: shadow
                criteria:
                  - criterion: safe
                    score: 0.91
                    threshold: 0.5
                    result: passed
                    advisory: false
                  - criterion: clean
                    score: 1
                    threshold: 0.5
                    result: passed
                    advisory: false
                  - criterion: relevant
                    score: 0.22
                    threshold: 0.5
                    result: failed
                    advisory: false
                    message: This comment is not about the post. Say something that responds to it.
        "401":
          $ref: "#/components/responses/InvalidKey"
        "403":
          description: |
            The account cannot run this check. `trial-ended` is the Free Trial
            clock. `allowance-exhausted` is the monthly check cap.
            `subscription-inactive` is a paid account without a scoring
            subscription. `usage-cap` is the marketing demo account hitting
            its daily or monthly check limit.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
              examples:
                allowanceExhausted:
                  value:
                    code: allowance-exhausted
                    message:
                      This period's check allowance is used. Move to a plan with a higher
                      allowance, or wait for the next period.
                trialEnded:
                  value:
                    code: trial-ended
                    message: The free trial has ended. Choose a Pro plan to score again.
                subscriptionInactive:
                  value:
                    code: subscription-inactive
                    message:
                      This account does not have an active subscription. Choose a plan to
                      score again.
                usageCap:
                  value:
                    code: usage-cap
                    message: This account has reached its check limit.
        "422":
          $ref: "#/components/responses/ValidationFailed"
        "429":
          $ref: "#/components/responses/RateLimited"
  /comments/{commentId}:
    parameters:
      - $ref: "#/components/parameters/commentId"
    put:
      tags:
        - Comments
      operationId: putComment
      summary: Add or replace an indexed comment
      description: |-
        Call when a comment goes live or is edited. The comment text is embedded
        and discarded. Send `contextId` when the post has a stable id so comments
        share one originality pool. Without it, send `contextText`; the pool is
        the MD5 of that exact string. Both may be sent; the id wins for the pool.
        Rejected when originality is not included in the site's policy, because
        the index can be considered personal data the site has not agreed to
        store. Safe to repeat.
      security:
        - apiKey: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/CommentIndexWrite"
            examples:
              withId:
                summary: Parent post with a stable id
                value:
                  contextId: article-42
                  commentText: Turnout fell in the wards that changed hands.
              textOnly:
                summary: Parent post without an id
                value:
                  contextText: The full article body.
                  commentText: Turnout fell in the wards that changed hands.
      responses:
        "200":
          description: An existing index entry was replaced. Empty body.
        "201":
          description: The comment was added to the index. Empty body.
        "401":
          $ref: "#/components/responses/InvalidKey"
        "403":
          description: Originality is off for this site.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
              example:
                code: original-off
                message:
                  Turn original on in the site policy before adding comments to the
                  index.
        "422":
          $ref: "#/components/responses/ValidationFailed"
    delete:
      tags:
        - Comments
      operationId: deleteComment
      summary: Remove an indexed comment
      description: |-
        Call when a comment is deleted, unpublished, or erased. Removes the
        embedding. A comment that is not in the index still returns 204, including
        when original is off, so erasure can be retried.
      security:
        - apiKey: []
      responses:
        "204":
          description: The index entry is gone. Empty body.
        "401":
          $ref: "#/components/responses/InvalidKey"
components:
  securitySchemes:
    apiKey:
      type: http
      scheme: bearer
      bearerFormat: sk_<prefix>_<secret>
      description:
        "Secret API key for `/v1`. Format `sk_<prefix>_<secret>`. Send it
        as `Authorization: Bearer <key>`. A revoked or unknown key is `401`."
  parameters:
    commentId:
      name: commentId
      in: path
      required: true
      description: The customer's id for the comment.
      schema:
        type: string
        minLength: 1
  responses:
    InvalidKey:
      description: The API key is missing, unknown, or revoked.
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/Error"
          example:
            code: invalid-key
            message: Send a current secret key in the Authorization bearer header.
    ValidationFailed:
      description: The body or a query parameter failed a rule.
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/Error"
          example:
            code: validation
            field: commentText
            message: commentText is required.
    RateLimited:
      description: The key has exceeded the score rate limit.
      headers:
        Retry-After:
          description: Seconds to wait before retrying.
          schema:
            type: integer
            minimum: 0
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/Error"
          example:
            code: rate-limited
            message: Too many score checks for this key. Try again in a minute.
  examples:
    defaultPolicy:
      value:
        criteria:
          - criterion: safe
            enabled: true
            advisory: false
            threshold: 0.5
          - criterion: clean
            enabled: true
            advisory: false
            threshold: 0.5
          - criterion: relevant
            enabled: true
            advisory: false
            threshold: 0.5
          - criterion: original
            enabled: false
            advisory: false
            threshold: 0.5
          - criterion: thoughtful
            enabled: false
            advisory: false
            threshold: 0.5
          - criterion: clear
            enabled: false
            advisory: false
            threshold: 0.5
  schemas:
    ScoreRequest:
      description: |
        The parent post is required. Send `contextText`. Send `contextId` when
        the post has a stable id.
      type: object
      additionalProperties: false
      required:
        - commentText
        - contextText
      properties:
        commentText:
          type: string
          minLength: 1
          maxLength: 2000
          description: The comment to score. Used for the check. At most 2000 characters.
        contextText:
          type: string
          minLength: 1
          maxLength: 8000
          description: |-
            The parent post. Used for the check. Stored on the decision only when
            the site's comment retention policy is `temp`. When `contextId` is
            omitted, comments are matched by the MD5 of this exact string. At most
            8000 characters.
        contextId:
          type: string
          minLength: 1
          description: |
            The customer's id for the parent post. When present, comments on
            this post share one originality pool even if the text is edited.
    ScoreResponse:
      type: object
      additionalProperties: false
      required:
        - outcome
        - enforcement
        - criteria
      properties:
        outcome:
          $ref: "#/components/schemas/DecisionOutcome"
        enforcement:
          $ref: "#/components/schemas/Enforcement"
          description: |
            Copied from the site at score time. The caller applies the decision
            only when this is `enforce`.
        criteria:
          type: array
          description: |
            Enabled criteria that ran. A criterion that is off is omitted. So is
            one that did not run because an earlier stop ended the check.
          items:
            $ref: "#/components/schemas/CriterionResult"
    Error:
      type: object
      additionalProperties: false
      required:
        - code
        - message
      properties:
        code:
          type: string
          description: Stable name of the rule that failed.
          enum:
            - invalid-key
            - validation
            - original-off
            - allowance-exhausted
            - trial-ended
            - subscription-inactive
            - usage-cap
            - rate-limited
        field:
          type: string
          description: The request field that failed, when the failure is on one field.
        message:
          type: string
          description: What failed and what to change.
    Policy:
      type: object
      additionalProperties: false
      required:
        - criteria
      properties:
        criteria:
          type: array
          minItems: 6
          maxItems: 6
          description: All six criteria, each once.
          items:
            $ref: "#/components/schemas/PolicyCriterion"
    CommentIndexWrite:
      description: |
        Send `contextId` when the post has a stable id. Otherwise send
        `contextText` (the pool is the MD5 of that text). Both may be sent; the
        id wins for the pool. `commentText` is embedded and discarded.
      oneOf:
        - type: object
          additionalProperties: false
          required:
            - commentText
            - contextId
          properties:
            commentText:
              type: string
              minLength: 1
              description: Embedded, then discarded. Not stored.
            contextId:
              type: string
              minLength: 1
              description: |
                The customer's id for the parent post. Comments on this post
                share one originality pool even if the text is edited.
            contextText:
              type: string
              minLength: 1
              maxLength: 8000
              description: |-
                The parent post. Required when `contextId` is omitted; ignored for
                pooling when it is present. At most 8000 characters.
        - type: object
          additionalProperties: false
          required:
            - commentText
            - contextText
          properties:
            commentText:
              type: string
              minLength: 1
              description: Embedded, then discarded. Not stored.
            contextText:
              type: string
              minLength: 1
              maxLength: 8000
              description: |-
                The parent post. Required when `contextId` is omitted; ignored for
                pooling when it is present. At most 8000 characters.
    DecisionOutcome:
      type: string
      enum:
        - passed
        - warning
        - failed
      description: |
        `failed` when any criterion that is not advisory failed. `warning` when
        at least one advisory criterion failed and every other criterion that ran
        passed. Otherwise `passed`.
    Enforcement:
      type: string
      enum:
        - shadow
        - enforce
    CriterionResult:
      type: object
      additionalProperties: false
      required:
        - criterion
        - score
        - threshold
        - result
        - advisory
      properties:
        criterion:
          $ref: "#/components/schemas/Criterion"
        score:
          type: number
          minimum: 0
          maximum: 1
          description: |
            0 to 1. Profanity is 0 on a listed-word match and 1 otherwise.
        threshold:
          type: number
          minimum: 0
          maximum: 1
          description: The site's threshold at score time.
        result:
          $ref: "#/components/schemas/ScoreResult"
        advisory:
          type: boolean
          description: Whether this criterion was advisory at score time.
        message:
          type: string
          description: |
            The coaching sentence for this criterion. Present when `result` is
            `failed`. Looked up from a resource file the API ships, keyed by
            criterion. Not generated by the model.
        similarCommentId:
          type: string
          description: |
            The customer's id of the closest published comment. Present only on
            the original result, and only when original flags a near-duplicate.
    PolicyCriterion:
      type: object
      additionalProperties: false
      required:
        - criterion
        - enabled
        - advisory
        - threshold
      properties:
        criterion:
          $ref: "#/components/schemas/Criterion"
        enabled:
          type: boolean
        advisory:
          type: boolean
          description: Applies when `enabled` is true. A new site starts with this false.
        threshold:
          type: number
          minimum: 0
          maximum: 1
    Criterion:
      type: string
      enum:
        - safe
        - clean
        - relevant
        - original
        - thoughtful
        - clear
    ScoreResult:
      type: string
      enum:
        - passed
        - failed
