# habuskid/flightclaim architecture

> Architecture diagram of the GitHub repository habuskid/flightclaim, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/habuskid/flightclaim
- Repository: https://github.com/habuskid/flightclaim

## Overview

#### Purpose and Entry Points
FlightClaim is an OKX.AI Agent Service Provider that evaluates EU261 flight-delay compensation eligibility and produces a draft claim explanation. Its operational entry point is the Next.js route handler at `app/api/flightclaim/route.ts`; clients submit a flight number and date via `POST /api/flightclaim`. Public informational pages are rooted at `app/page.tsx`, with supporting documentation and policy pages under `app/docs/page.tsx` and `app/how-it-works/page.tsx`.

#### API Orchestration Boundary
`app/api/flightclaim/route.ts` is the backend composition layer. It is responsible for accepting the marketplace-facing request, enforcing payment and rate-limit gates, retrieving flight facts, invoking eligibility logic, generating the explanatory letter, and returning the structured JSON result. This route is the principal boundary between untrusted client input and internal/domain integrations.

#### Payment and Access Controls
The request pipeline begins with x402/OKX payment validation and then rate limiting backed by Upstash Redis, as described by the service contract. Payment-specific integration is isolated in `lib/okx-payment.ts`, allowing the API route to reject unpaid requests with HTTP 402 before spending telemetry or inference capacity. Required OKX credentials, payment destination, and price are environment-configured through `.env.example`.

#### Flight Telemetry Acquisition
`lib/aviationstack-client.ts` encapsulates AviationStack API access for live or historical flight telemetry. The API layer should consume normalized flight data from this client rather than coupling domain logic to the external provider’s payloads. AviationStack credentials, base URL, and monthly usage limit form an external-service and cost-control boundary.

#### EU261 Decision Engine
`lib/regulation-engine.ts` contains deterministic EU Regulation 261/2004 eligibility and compensation logic. It combines flight status/timing facts with route distance and applies thresholds such as delay duration and compensation bands. Supporting route-distance calculation resides in `lib/distance-calculator.ts`, while airport reference data is maintained in `lib/data/airports.json`. Domain request, telemetry, verdict, and response shapes are centralized in `lib/types.ts`.

#### Claim Explanation Generation
After the deterministic verdict is produced, `lib/cysic-client.ts` calls Cysic inference to draft a human-readable claim letter/explanation grounded in the verdict and telemetry. This is deliberately downstream of the regulation engine: AI augments presentation, not legal eligibility determination. Cysic API credentials and endpoint are independently configured, keeping inference failure/cost concerns separate from core rule evaluation.

#### Runtime and Delivery Surface
The application runs on Next.js 16 with React 19 and TypeScript. Server behavior is centered on the App Router API handler in `app/api/flightclaim/route.ts`; the React pages are primarily a documentation/marketing surface rather than the core agent workflow. Shared global styling and application shell are in `app/layout.tsx` and `app/globals.css`. External dependencies materially include AviationStack, Cysic, OKX/x402 payment infrastructure, and Upstash Redis.

Main entry points: [`app/api/flightclaim/route.ts`](https://github.com/habuskid/flightclaim/blob/main/app/api/flightclaim/route.ts) (Flight claim endpoint), [`lib/aviationstack-client.ts`](https://github.com/habuskid/flightclaim/blob/main/lib/aviationstack-client.ts) (AviationStack client), [`.env.example`](https://github.com/habuskid/flightclaim/blob/main/.env.example) (Service configuration), [`lib/regulation-engine.ts`](https://github.com/habuskid/flightclaim/blob/main/lib/regulation-engine.ts) (EU261 decision engine)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public["Public Surface"]
  node_marketing["Marketing page<br/>Next.js page<br/>[page.tsx]"]
  node_docs["Docs page<br/>Next.js page<br/>[page.tsx]"]
  node_how_it_works["How it works page<br/>Next.js page<br/>[page.tsx]"]
  node_app_shell["App shell and styles<br/>Next.js layout<br/>[layout.tsx]"]
end

subgraph group_api["Agent API"]
  node_flightclaim_route{{"Flight claim endpoint<br/>Next.js route handler<br/>[route.ts]"}}
  node_payment["OKX payment validation<br/>payment adapter<br/>[okx-payment.ts]"]
  node_rate_limit["Rate limiting<br/>access control"]
  node_aviation_client["AviationStack client<br/>telemetry adapter"]
  node_cysic_client["Cysic claim generator<br/>inference adapter<br/>[cysic-client.ts]"]
  node_environment["Service configuration<br/>environment contract<br/>[.env.example]"]
end

subgraph group_domain["Eligibility Domain"]
  node_regulation_engine{{"EU261 decision engine<br/>deterministic rules"}}
  node_distance["Route distance calculator<br/>domain calculation"]
  node_airports[("Airport reference data<br/>airport dataset<br/>[airports.json]")]
  node_types["Domain contracts<br/>TypeScript types<br/>[types.ts]"]
end

subgraph group_external["External Services"]
  node_redis[("Upstash Redis<br/>rate-limit store")]
  node_aviationstack["AviationStack<br/>flight telemetry API"]
  node_cysic["Cysic inference<br/>AI service"]
end

node_marketing -->|"renders in"| node_app_shell
node_docs -->|"renders in"| node_app_shell
node_how_it_works -->|"renders in"| node_app_shell
node_flightclaim_route -->|"validates payment"| node_payment
node_flightclaim_route -->|"enforces limit"| node_rate_limit
node_rate_limit -->|"uses"| node_redis
node_flightclaim_route -->|"retrieves flight facts"| node_aviation_client
node_aviation_client -->|"queries"| node_aviationstack
node_flightclaim_route -->|"evaluates eligibility"| node_regulation_engine
node_regulation_engine -->|"calculates route distance"| node_distance
node_distance -->|"reads airports"| node_airports
node_flightclaim_route -->|"generates explanation"| node_cysic_client
node_cysic_client -->|"invokes"| node_cysic
node_regulation_engine -->|"produces verdict"| node_types
node_aviation_client -->|"normalizes telemetry"| node_types
node_flightclaim_route -->|"returns response"| node_types
node_environment -.->|"configures"| node_payment
node_environment -.->|"configures"| node_aviation_client
node_environment -.->|"configures"| node_cysic_client

click node_marketing "https://github.com/habuskid/flightclaim/blob/main/app/page.tsx"
click node_docs "https://github.com/habuskid/flightclaim/blob/main/app/docs/page.tsx"
click node_how_it_works "https://github.com/habuskid/flightclaim/blob/main/app/how-it-works/page.tsx"
click node_app_shell "https://github.com/habuskid/flightclaim/blob/main/app/layout.tsx"
click node_flightclaim_route "https://github.com/habuskid/flightclaim/blob/main/app/api/flightclaim/route.ts"
click node_payment "https://github.com/habuskid/flightclaim/blob/main/lib/okx-payment.ts"
click node_aviation_client "https://github.com/habuskid/flightclaim/blob/main/lib/aviationstack-client.ts"
click node_regulation_engine "https://github.com/habuskid/flightclaim/blob/main/lib/regulation-engine.ts"
click node_distance "https://github.com/habuskid/flightclaim/blob/main/lib/distance-calculator.ts"
click node_airports "https://github.com/habuskid/flightclaim/blob/main/lib/data/airports.json"
click node_types "https://github.com/habuskid/flightclaim/blob/main/lib/types.ts"
click node_cysic_client "https://github.com/habuskid/flightclaim/blob/main/lib/cysic-client.ts"
click node_environment "https://github.com/habuskid/flightclaim/blob/main/.env.example"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_marketing,node_docs,node_how_it_works,node_app_shell toneBlue
class node_flightclaim_route,node_payment,node_rate_limit,node_aviation_client,node_cysic_client,node_environment toneAmber
class node_regulation_engine,node_distance,node_airports,node_types toneMint
class node_redis,node_aviationstack,node_cysic toneRose
```

## Components

### Public Surface

- [Marketing page](https://github.com/habuskid/flightclaim/blob/main/app/page.tsx): Next.js page · `app/page.tsx`
- [Docs page](https://github.com/habuskid/flightclaim/blob/main/app/docs/page.tsx): Next.js page · `app/docs/page.tsx`
- [How it works page](https://github.com/habuskid/flightclaim/blob/main/app/how-it-works/page.tsx): Next.js page · `app/how-it-works/page.tsx`
- [App shell and styles](https://github.com/habuskid/flightclaim/blob/main/app/layout.tsx): Next.js layout · `app/layout.tsx`

### Agent API

- [Flight claim endpoint](https://github.com/habuskid/flightclaim/blob/main/app/api/flightclaim/route.ts): Next.js route handler · POST /api/flightclaim orchestration boundary. · `app/api/flightclaim/route.ts`
- [OKX payment validation](https://github.com/habuskid/flightclaim/blob/main/lib/okx-payment.ts): payment adapter · Rejects unpaid requests before telemetry or inference work. · `lib/okx-payment.ts`
- **Rate limiting**: access control
- [AviationStack client](https://github.com/habuskid/flightclaim/blob/main/lib/aviationstack-client.ts): telemetry adapter · Normalizes provider flight data for the API layer. · `lib/aviationstack-client.ts`
- [Cysic claim generator](https://github.com/habuskid/flightclaim/blob/main/lib/cysic-client.ts): inference adapter · Drafts an explanation from the verdict and telemetry. · `lib/cysic-client.ts`
- [Service configuration](https://github.com/habuskid/flightclaim/blob/main/.env.example): environment contract · Contains payment, telemetry, and inference credentials and limits. · `.env.example`

### Eligibility Domain

- [EU261 decision engine](https://github.com/habuskid/flightclaim/blob/main/lib/regulation-engine.ts): deterministic rules · Determines eligibility and compensation independently of AI. · `lib/regulation-engine.ts`
- [Route distance calculator](https://github.com/habuskid/flightclaim/blob/main/lib/distance-calculator.ts): domain calculation · `lib/distance-calculator.ts`
- [Airport reference data](https://github.com/habuskid/flightclaim/blob/main/lib/data/airports.json): airport dataset · `lib/data/airports.json`
- [Domain contracts](https://github.com/habuskid/flightclaim/blob/main/lib/types.ts): TypeScript types · Request, telemetry, verdict, and response shapes. · `lib/types.ts`

### External Services

- **Upstash Redis**: rate-limit store
- **AviationStack**: flight telemetry API
- **Cysic inference**: AI service

## Connections

- Marketing page → App shell and styles: renders in
- Docs page → App shell and styles: renders in
- How it works page → App shell and styles: renders in
- Flight claim endpoint → OKX payment validation: validates payment
- Flight claim endpoint → Rate limiting: enforces limit
- Rate limiting → Upstash Redis: uses
- Flight claim endpoint → AviationStack client: retrieves flight facts
- AviationStack client → AviationStack: queries
- Flight claim endpoint → EU261 decision engine: evaluates eligibility
- EU261 decision engine → Route distance calculator: calculates route distance
- Route distance calculator → Airport reference data: reads airports
- Flight claim endpoint → Cysic claim generator: generates explanation
- Cysic claim generator → Cysic inference: invokes
- EU261 decision engine → Domain contracts: produces verdict
- AviationStack client → Domain contracts: normalizes telemetry
- Flight claim endpoint → Domain contracts: returns response
- Service configuration → OKX payment validation: configures
- Service configuration → AviationStack client: configures
- Service configuration → Cysic claim generator: configures
