openapi: 3.1.0 info: title: Dexwize Insight AI Service version: 1.0.0 description: > Evidence-bound explanations and report drafts (ADR-0018). Callers pass finding and recommendation ids; the service loads them for the caller's tenant. Every statement cites finding ids and is validated before it is returned; rejected model output falls back to deterministic templates and a human-review item. All endpoints require a bearer token; ids of another tenant are indistinguishable from missing ids (404). security: - bearer: [] paths: /v1/insights/explanations: post: summary: Explain findings (FindingsRead) operationId: explainFindings requestBody: required: true content: application/json: schema: type: object required: - findingIds properties: findingIds: type: array minItems: 1 maxItems: 50 items: type: string format: uuid language: $ref: '#/components/schemas/Language' responses: '200': description: OK content: application/json: schema: $ref: '#/components/schemas/InsightResponse' '400': description: No ids or too many ids '403': description: Missing FindingsRead '404': description: A finding id is unknown or belongs to another tenant /v1/insights/report-sections: post: summary: Draft one report section (ReportsRead) operationId: draftReportSection requestBody: required: true content: application/json: schema: type: object required: - section - findingIds properties: section: type: string enum: - ExecutiveSummary - Conflicts - Performance - Usage - Recommendations findingIds: type: array minItems: 1 maxItems: 50 items: type: string format: uuid recommendationIds: type: array items: type: string format: uuid language: $ref: '#/components/schemas/Language' responses: '200': description: OK content: application/json: schema: $ref: '#/components/schemas/InsightResponse' '400': description: Invalid ids, or a recommendation whose finding is not included '404': description: A finding or recommendation id is unknown or belongs to another tenant /v1/insights/reviews: get: summary: Human review queue (InsightsReview) operationId: listReviews parameters: - name: status in: query schema: type: string enum: - Pending - Approved - Edited - Rejected responses: '200': description: OK content: application/json: schema: type: array items: $ref: '#/components/schemas/ReviewItem' /v1/insights/reviews/{id}/approve: post: operationId: approveReview parameters: - $ref: '#/components/parameters/Id' responses: '200': description: Approved content: application/json: schema: $ref: '#/components/schemas/ReviewItem' '404': description: Not found '409': description: Already decided /v1/insights/reviews/{id}/reject: post: operationId: rejectReview parameters: - $ref: '#/components/parameters/Id' responses: '200': description: Rejected content: application/json: schema: $ref: '#/components/schemas/ReviewItem' '404': description: Not found '409': description: Already decided /v1/insights/reviews/{id}/edit: post: summary: Replace the draft with reviewer text (must cite the item's findings; no URLs or commands) operationId: editReview parameters: - $ref: '#/components/parameters/Id' requestBody: required: true content: application/json: schema: type: object required: - statements properties: statements: type: array items: $ref: '#/components/schemas/Statement' responses: '200': description: Edited content: application/json: schema: $ref: '#/components/schemas/ReviewItem' '400': description: Statement failed validation '404': description: Not found '409': description: Already decided /v1/insights/usage: get: summary: AI token usage this month (TenantRead) operationId: getAiUsage responses: '200': description: OK content: application/json: schema: type: object properties: month: type: string example: 2026-11 used: type: integer cap: type: integer aiEnabled: type: boolean /v1/insights/settings: put: summary: Enable or disable AI and set the token cap and review threshold (TenantManage) operationId: putAiSettings requestBody: required: true content: application/json: schema: type: object required: - aiEnabled properties: aiEnabled: type: boolean monthlyTokenCap: type: integer minimum: 0 description: At most the platform maximum (Ai:MaxMonthlyTokenCap) reviewConfidenceThreshold: type: number minimum: 0 maximum: 1 responses: '200': description: Saved content: application/json: schema: $ref: '#/components/schemas/AiSettings' '400': description: Outside platform limits /v1/insights/investigations: post: summary: Answer a natural-language question from query-service rows (FindingsRead) description: > The model turns the question into a structured plan (never SQL); the plan is validated against the query-service catalog and run through query-service with the caller's token, so the caller's tenant and permissions apply. Requests to change devices are refused before any model call. When AI is disabled, over budget or the output fails validation, the response carries rows only or query templates (ADR-0031). operationId: investigate requestBody: required: true content: application/json: schema: type: object required: - question properties: question: type: string maxLength: 500 language: $ref: '#/components/schemas/Language' responses: '200': description: Answer, rows only, refusal or templates content: application/json: schema: $ref: '#/components/schemas/InvestigationResult' '401': description: No bearer token '403': { $ref: '#/components/responses/FeatureNotEntitled' } '502': description: query-service failed '503': { $ref: '#/components/responses/EntitlementsUnavailable' } /v1/insights/investigations/templates: get: summary: Query templates offered when AI is unavailable (FindingsRead) operationId: listInvestigationTemplates responses: '200': description: OK content: application/json: schema: type: array items: $ref: '#/components/schemas/QueryTemplate' '403': { $ref: '#/components/responses/FeatureNotEntitled' } '503': { $ref: '#/components/responses/EntitlementsUnavailable' } components: securitySchemes: bearer: type: http scheme: bearer bearerFormat: JWT parameters: Id: name: id in: path required: true schema: type: string format: uuid responses: FeatureNotEntitled: description: The caller lacks the permission (empty body), or the tenant's plan, add-ons, licence and overrides do not include the feature (error feature_not_entitled, ADR-0053). content: { application/json: { schema: { $ref: '#/components/schemas/EntitlementRefusal' } } } EntitlementsUnavailable: description: The tenant's entitlements could not be confirmed and nothing usable was cached, so the paid feature is refused (fail closed, ADR-0053). Retry-After is set. content: { application/json: { schema: { $ref: '#/components/schemas/EntitlementRefusal' } } } schemas: EntitlementRefusal: description: Module licensing refusal (ADR-0053). type: object required: [error, feature] properties: error: { type: string, enum: [feature_not_entitled, entitlements_unavailable] } feature: { type: string, description: Catalogue feature code, for example query } Language: type: string enum: - English - Arabic default: English Statement: type: object required: - text - cites properties: text: type: string maxLength: 1000 cites: type: array minItems: 1 items: type: string format: uuid InsightResponse: type: object properties: statements: type: array items: $ref: '#/components/schemas/Statement' outcome: type: string enum: - Validated - RulesOnly - Fallback - Empty provider: type: string description: azure:, local: or rules-only promptVersion: type: string tokensUsed: type: integer reviewItemId: type: - string - 'null' format: uuid pendingReview: type: boolean reasons: type: array items: type: string ReviewItem: type: object properties: id: type: string format: uuid kind: type: string reason: type: string language: $ref: '#/components/schemas/Language' draft: type: array items: $ref: '#/components/schemas/Statement' findingIds: type: array items: type: string format: uuid status: type: string enum: - Pending - Approved - Edited - Rejected createdAt: type: string format: date-time reviewedBy: type: - string - 'null' reviewedAt: type: - string - 'null' format: date-time AiSettings: type: object properties: tenantId: type: string format: uuid aiEnabled: type: boolean monthlyTokenCap: type: integer reviewConfidenceThreshold: type: number required: - tenantId - aiEnabled - monthlyTokenCap - reviewConfidenceThreshold QueryTemplate: type: object properties: key: type: string titleEn: type: string titleAr: type: string plan: type: object description: Structured plan in the same shape the model must produce InvestigationResult: type: object properties: outcome: type: string enum: - Answered - RowsOnly - Refused - NoPlan - RulesOnly reason: type: - string - 'null' plan: type: - object - 'null' rows: type: - object - 'null' properties: queryId: type: string pattern: '^[0-9a-f]{16}$' columns: type: array items: type: string rows: type: array items: type: object additionalProperties: true truncated: type: boolean statements: type: array items: type: object properties: text: type: string cites: type: array description: 'Row references q:{queryId}#r{n}, or q:{queryId} for the whole result' items: type: string templates: type: array items: $ref: '#/components/schemas/QueryTemplate' provider: type: string