openapi: 3.1.0
info:
  title: Preuve Agent API
  version: '2.0'
  description: >
    HMAC-signed API for programmatic startup idea validation. Every request
    carries x-preuve-key, x-preuve-timestamp, x-preuve-nonce, and
    x-preuve-signature headers (HMAC-SHA256 over a canonical string - see the
    Authentication guide). The interactive playground cannot compute signatures;
    use it for shapes only.
servers:
  - url: https://preuve.ai
security:
  - preuveKey: []
paths:
  /api/agent/analyses:
    post:
      operationId: createAnalysis
      summary: Start an analysis
      description: >
        Create one analysis run. Async - poll with GET /api/agent/analyses/{id}.
        scanType is required and explicit: "starter" costs nothing, "deep"
        consumes paid account quota. clientRunId is your idempotency key:
        re-POSTing the same value replays the existing run.
      tags:
        - Analyses
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateAnalysisRequest'
      responses:
        '200':
          description: Run created (or replayed via clientRunId).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AnalysisRun'
        '402':
          $ref: '#/components/responses/InsufficientTokens'
        '403':
          description: Starter scan limit reached (STARTER_LIMIT_REACHED).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
        '429':
          $ref: '#/components/responses/RateLimited'
  /api/agent/analyses/{id}:
    get:
      operationId: getAnalysis
      summary: Poll an analysis
      description: >
        Returns run status, readyForExport, enrichment progress, per-module
        statuses (deep runs), and report/share URLs. Accepts the run id or the
        report id. Does not return the report payload - use export.
      tags:
        - Analyses
      parameters:
        - $ref: '#/components/parameters/AnalysisId'
      responses:
        '200':
          description: Current run state.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AnalysisRun'
        '404':
          $ref: '#/components/responses/NotFound'
  /api/agent/analyses/{id}/enrich:
    post:
      operationId: enrichAnalysis
      summary: Enrich an analysis
      description: >
        Idempotently generates the missing core export sections for a COMPLETED
        run. Optionally starts deep-only modules via body.modules (one
        successful generation per module per report). founderFit requires
        founderProfile and runs inline; the other modules run async - poll get.
        Partial failures return HTTP 207 with a success-shaped body.
      tags:
        - Analyses
      parameters:
        - $ref: '#/components/parameters/AnalysisId'
      requestBody:
        required: false
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/EnrichRequest'
      responses:
        '200':
          description: Enrichment (and any inline module) completed.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/EnrichResponse'
        '207':
          description: >-
            Partial success - some sections or modules failed; inspect failures
            and modules.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/EnrichResponse'
        '400':
          description: >-
            Invalid modules or founderProfile (INVALID_MODULES,
            INVALID_FOUNDER_PROFILE).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
        '403':
          description: Modules requested on a non-deep report (MODULES_REQUIRE_DEEP).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
        '409':
          description: Analysis not complete yet (ANALYSIS_NOT_COMPLETE).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
        '429':
          $ref: '#/components/responses/RateLimited'
  /api/agent/analyses/{id}/export:
    get:
      operationId: exportAnalysis
      summary: Export an analysis (ideas-json)
      description: >
        Structured JSON export (schemaVersion 2) for one COMPLETED, export-ready
        analysis. Deep runs include full sections, pivots, citations, and
        generated module payloads.
      tags:
        - Analyses
      parameters:
        - $ref: '#/components/parameters/AnalysisId'
        - name: format
          in: query
          required: false
          schema:
            type: string
            enum:
              - ideas-json
            default: ideas-json
      responses:
        '200':
          description: The export payload.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AnalysisExport'
        '404':
          $ref: '#/components/responses/NotFound'
        '409':
          description: >-
            Not exportable yet (ANALYSIS_NOT_COMPLETE or
            ENRICHMENT_NOT_COMPLETE).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
        '422':
          description: The analysis failed (ANALYSIS_FAILED).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
  /api/agent/analysis-batches:
    post:
      operationId: createBatch
      summary: Start a batch
      description: >
        Create up to 10 analysis runs in one batch. Idempotent on the batch
        clientRunId. Each item needs its own unique clientRunId and explicit
        scanType (mix free and deep intentionally).
      tags:
        - Batches
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateBatchRequest'
      responses:
        '200':
          description: Batch created (or replayed via clientRunId).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Batch'
        '429':
          $ref: '#/components/responses/RateLimited'
  /api/agent/analysis-batches/{batchId}:
    get:
      operationId: getBatch
      summary: Poll a batch
      tags:
        - Batches
      parameters:
        - $ref: '#/components/parameters/BatchId'
      responses:
        '200':
          description: Batch state with per-item statuses.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Batch'
        '404':
          $ref: '#/components/responses/NotFound'
  /api/agent/analysis-batches/{batchId}/export:
    get:
      operationId: exportBatch
      summary: Export a batch (ideas-json)
      description: >
        Structured export for a batch. Includes only completed, export-ready
        items; every skipped item is listed in omittedItems with a reason. A
        single exportedAt is pinned per export so the same batch state always
        serializes identically.
      tags:
        - Batches
      parameters:
        - $ref: '#/components/parameters/BatchId'
        - name: format
          in: query
          required: false
          schema:
            type: string
            enum:
              - ideas-json
            default: ideas-json
      responses:
        '200':
          description: The batch export payload.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/BatchExport'
        '404':
          $ref: '#/components/responses/NotFound'
components:
  securitySchemes:
    preuveKey:
      type: apiKey
      in: header
      name: x-preuve-key
      description: >
        API key id. Requests are additionally signed with x-preuve-timestamp,
        x-preuve-nonce, and x-preuve-signature (HMAC-SHA256) - see the
        Authentication guide.
  parameters:
    AnalysisId:
      name: id
      in: path
      required: true
      description: Run id (returned by create) or report id.
      schema:
        type: string
        format: uuid
    BatchId:
      name: batchId
      in: path
      required: true
      description: Batch id returned by create.
      schema:
        type: string
        format: uuid
  responses:
    RateLimited:
      description: Rate limited (RATE_LIMITED or DAILY_LIMIT_REACHED).
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiError'
    InsufficientTokens:
      description: No quota and no tokens for a deep scan (INSUFFICIENT_TOKENS).
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiError'
    NotFound:
      description: >-
        Unknown id, or the resource belongs to another account
        (ANALYSIS_NOT_FOUND).
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiError'
  schemas:
    ApiError:
      type: object
      required:
        - error
        - code
      properties:
        error:
          type: string
          description: Human-readable message.
        code:
          type: string
          description: Stable machine-readable code. Branch on this.
        details:
          description: Optional context (validation errors, limits, ...).
    CreateAnalysisRequest:
      type: object
      required:
        - clientRunId
        - scanType
        - idea
      properties:
        clientRunId:
          type: string
          description: Caller-chosen idempotency id. Reusing it replays the existing run.
        scanType:
          type: string
          enum:
            - starter
            - deep
          description: >-
            "starter" costs nothing; "deep" runs the full paid analysis and
            consumes account quota. "free" is accepted as a legacy alias of
            "starter".
        idea:
          type: string
          description: The startup idea, in plain language.
        targetMarket:
          type: string
        targetCountry:
          type: string
          description: ISO country code, e.g. "US" or "FR".
        title:
          type: string
        stage:
          type: string
        budget:
          type: string
        language:
          type: string
          default: en
        enrichmentMode:
          type: string
          enum:
            - core
            - none
          default: core
          description: >-
            "core" also generates the export-required sections; "none" returns
            the raw completion only.
        publish:
          type: boolean
          description: When true, creates a public share URL for the report.
      example:
        clientRunId: my-run-001
        scanType: free
        idea: >-
          A scheduling assistant that helps independent coffee shops coordinate
          weekly local supplier orders with lower waste.
        targetMarket: Independent coffee shops
        targetCountry: US
        enrichmentMode: core
        publish: true
    AnalysisRun:
      type: object
      properties:
        id:
          type: string
          format: uuid
        reportId:
          type: string
          format: uuid
        clientRunId:
          type: string
        scanType:
          type: string
          enum:
            - starter
            - deep
          description: Your request type. Reliable from creation, even while PROCESSING.
        reportType:
          type: string
          enum:
            - quick
            - deep_dive
        analysisTier:
          type: string
          enum:
            - basic
            - advanced
          description: >-
            Derived from the report. Deep runs read "basic" until deep sections
            land.
        status:
          type: string
          enum:
            - PENDING
            - PROCESSING
            - COMPLETED
            - FAILED
        readyForExport:
          type: boolean
        enrichment:
          $ref: '#/components/schemas/EnrichmentState'
        modules:
          type: object
          nullable: true
          description: Per-module statuses. Deep reports only; null on free runs.
          properties:
            proofOfDemand:
              $ref: '#/components/schemas/ModuleStatus'
            founderFit:
              $ref: '#/components/schemas/ModuleStatus'
            playbook:
              $ref: '#/components/schemas/ModuleStatus'
            trends:
              $ref: '#/components/schemas/ModuleStatus'
        reportUrl:
          type: string
          format: uri
        shareUrl:
          type: string
          format: uri
          nullable: true
    EnrichmentState:
      type: object
      properties:
        mode:
          type: string
          enum:
            - core
            - none
        status:
          type: string
          enum:
            - pending
            - processing
            - completed
            - failed
        required:
          type: array
          items:
            type: string
        completed:
          type: array
          items:
            type: string
    ModuleStatus:
      type: object
      properties:
        status:
          type: string
          enum:
            - not_generated
            - generating
            - failed
            - completed
    FounderProfile:
      type: object
      required:
        - hoursPerWeek
        - runwayMonths
        - domainYears
        - shippedBefore
        - audienceSize
        - teamStatus
      properties:
        hoursPerWeek:
          type: number
          minimum: 1
          maximum: 168
        runwayMonths:
          type: number
          minimum: 0
          maximum: 120
        domainYears:
          type: number
          minimum: 0
          maximum: 60
        shippedBefore:
          type: string
          enum:
            - never
            - side_project
            - launched
        audienceSize:
          type: string
          enum:
            - none
            - under_1k
            - 1k_10k
            - over_10k
        teamStatus:
          type: string
          enum:
            - solo
            - cofounded_friends
            - cofounded_colleagues
            - team_with_hires
        twelveMonthGoal:
          type: string
          enum:
            - replace_income
            - side_income
            - vc_scale
        sabotagePattern:
          type: string
          maxLength: 280
    EnrichRequest:
      type: object
      properties:
        modules:
          type: array
          description: Deep-only modules to generate. Omit for core enrichment only.
          items:
            type: string
            enum:
              - proofOfDemand
              - founderFit
              - playbook
              - trends
        founderProfile:
          $ref: '#/components/schemas/FounderProfile'
      example:
        modules:
          - founderFit
          - playbook
        founderProfile:
          hoursPerWeek: 20
          runwayMonths: 6
          domainYears: 3
          shippedBefore: side_project
          audienceSize: under_1k
          teamStatus: solo
    ModuleResult:
      type: object
      properties:
        ok:
          type: boolean
        status:
          type: integer
          description: >-
            Per-module HTTP-style status (200 inline success, 202 accepted, 409
            cap, ...).
        code:
          type: string
          nullable: true
        state:
          type: string
          enum:
            - not_generated
            - generating
            - failed
            - completed
        alreadyStarted:
          type: boolean
          description: True when a generation started elsewhere was already in flight.
    EnrichResponse:
      type: object
      properties:
        analysis:
          $ref: '#/components/schemas/AnalysisRun'
        enrichment:
          $ref: '#/components/schemas/EnrichmentState'
        failures:
          type: array
          description: Core sections that failed this pass. Retry by calling enrich again.
          items:
            type: object
        modules:
          type: object
          nullable: true
          description: Per-module results for the modules requested in this call.
          additionalProperties:
            $ref: '#/components/schemas/ModuleResult'
    AnalysisExport:
      type: object
      description: >
        ideas-json export, schemaVersion 2. Deep-only fields are null on free
        runs. See the Scans and Enrichment guide for the field-by-field
        contract.
      properties:
        schemaVersion:
          type: integer
          const: 2
        exportType:
          type: string
          enum:
            - analysis
        exportedAt:
          type: string
          format: date-time
        analysis:
          $ref: '#/components/schemas/AnalysisRun'
        idea:
          type: object
          properties:
            rank:
              type: integer
            verdict:
              type: string
            score:
              type: number
            title:
              type: string
            risk:
              type: string
            quote:
              type: string
            evidence:
              type: string
            domains:
              type: array
              items:
                type: string
            reportId:
              type: string
            reportUrl:
              type: string
            shareUrl:
              type: string
              nullable: true
            details:
              type: object
              description: >-
                Summary contract on every tier;
                sections/pivots/communityDemand/founderFit/playbook/proofOfDemand
                on deep runs.
    CreateBatchRequest:
      type: object
      required:
        - clientRunId
        - items
      properties:
        clientRunId:
          type: string
          description: Idempotency id for the whole batch.
        name:
          type: string
        scanType:
          type: string
          enum:
            - starter
            - deep
          description: Default scanType applied to every item; each item may override.
        enrichmentMode:
          type: string
          enum:
            - core
            - none
        items:
          type: array
          minItems: 1
          maxItems: 10
          items:
            $ref: '#/components/schemas/CreateAnalysisRequest'
    Batch:
      type: object
      properties:
        id:
          type: string
          format: uuid
        clientRunId:
          type: string
        name:
          type: string
          nullable: true
        status:
          type: string
        counts:
          type: object
          additionalProperties:
            type: integer
        items:
          type: array
          items:
            $ref: '#/components/schemas/AnalysisRun'
    BatchExport:
      type: object
      properties:
        schemaVersion:
          type: integer
          const: 2
        exportType:
          type: string
          enum:
            - batch
        exportedAt:
          type: string
          format: date-time
        counts:
          type: object
          properties:
            exported:
              type: integer
            omitted:
              type: integer
        ideas:
          type: array
          items:
            type: object
        omittedItems:
          type: array
          description: Every incomplete item with the reason it was skipped.
          items:
            type: object
            properties:
              id:
                type: string
              clientRunId:
                type: string
              reportId:
                type: string
                nullable: true
              status:
                type: string
              reason:
                type: string
