# yazeedr1425/ahsem architecture

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

- Interactive diagram: https://gitdiagram.com/yazeedr1425/ahsem
- Repository: https://github.com/yazeedr1425/ahsem

## Overview

#### Purpose and runtime
Arabic-first decision-assistant built with Next.js App Router/React. It combines deterministic weighted scoring with Gemini-generated explanations and decision agents; Supabase supplies identity, PostgreSQL persistence, RLS, and live group-vote signaling. Primary UI entry is `app/page.js`; specialized flows are `/plan`, `/analyze`, and `/vote/[code]`.

#### Individual decision flow
The main client flow collects 2–5 options, category, weighted criteria, ratings, and mood, then renders a recommendation and transparent score breakdown. Deterministic logic must remain authoritative so the UI can complete if AI fails.
- `app/page.js`
- `app/components/QuestionStep.js`
- `lib/engine/score.js`
- `lib/engine/categories.js`
- `app/api/decide/route.js`

Mood modifies one criterion weight; local “oversized decision” detection diverts consequential questions into a staged breakdown rather than the standard three-question flow.
- `lib/engine/mood.js`
- `lib/engine/oversized.js`
- `app/components/BreakdownFlow.js`
- `app/api/breakdown/route.js`

#### AI boundary and decision agents
Gemini 2.5 Flash is server-only and is used for constrained JSON outputs: final recommendation, third-option suggestion, large-decision decomposition/verdict, voice assistance, plan generation, and risk analysis. Local engines calculate scores/statistics; model output explains or structures results rather than inventing numerical facts.
- `app/api/decide/route.js`
- `app/api/third/route.js`
- `lib/engine/explain.js`
- `lib/insight/prompt.js`

Rate limiting and cache behavior protect selected expensive endpoints.
- `lib/rate-limit.js`
- `lib/services/third.js`

#### Persistence, identity, and history insights
Supabase browser and service-role clients separate client access from trusted server operations. The schema models profiles, decisions/options/answers, feedback, analyses, votes, and an immutable winner history. Authenticated history is aggregated in code, then presented by Gemini as a “decision personality.”
- `lib/supabase.js`
- `lib/supabase-server.js`
- `lib/services/decisions.js`
- `lib/insight/stats.js`
- `supabase/migrations/20260818140000_add_decision_winners.sql`

Signup is a server flow integrating Supabase Auth and Mailtrap verification email; failure is compensated by removing the incomplete account.
- `app/api/signup/route.js`
- `app/api/magic-link/route.js`
- `email send/verification.html`

#### Collaborative voting
A creator creates or closes a decision group; guests join through a short code without accounts and cast votes through database security-definer functions. Realtime Broadcast is only a refresh signal and Presence reports active participants; clients re-fetch authoritative vote state rather than trusting broadcast payloads.
- `app/vote/[code]/page.js`
- `app/api/group/route.js`
- `lib/services/group.js`
- `supabase/migrations/20260818120000_harden_functions_and_policies.sql`

#### Planning and risk-analysis products
`/plan` orchestrates Gemini planning with external factual enrichment: Google Places, Google Routes travel times, and Open-Meteo weather, with graceful degradation around weather. `/analyze` produces persisted SWOT and path-tree analysis for one major decision.
- `app/plan/page.js`
- `app/api/plan/route.js`
- `lib/places/client.js`
- `lib/routing/travel.js`
- `lib/weather/client.js`
- `app/api/analyze/route.js`
- `lib/analyze/agents.js`

#### Client interaction layers
The UI is RTL-oriented and composed as staged decision screens, result/reveal views, optional voice interaction, and global auth/mood providers. Browser speech recognition/synthesis is encapsulated separately from the server AI assistant endpoint.
- `app/layout.js`
- `app/components/Result.js`
- `lib/auth/AuthProvider.js`
- `lib/theme/MoodProvider.js`
- `lib/voice/VoiceProvider.js`
- `app/api/assist/route.js`

Main entry points: [`app/api/decide/route.js`](https://github.com/yazeedr1425/ahsem/blob/main/app/api/decide/route.js) (Decision endpoint), [`lib/engine/explain.js`](https://github.com/yazeedr1425/ahsem/blob/main/lib/engine/explain.js) (Gemini decision agents), [`app/page.js`](https://github.com/yazeedr1425/ahsem/blob/main/app/page.js) (Decision questionnaire), [`lib/services/decisions.js`](https://github.com/yazeedr1425/ahsem/blob/main/lib/services/decisions.js) (Decisions and personality)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["RTL client experience"]
  node_app_shell["App shell<br/>Next.js layout<br/>[layout.js]"]
  node_decision_ui["Decision questionnaire<br/>React flow<br/>[page.js]"]
  node_question_step["Question stages<br/>React component<br/>[QuestionStep.js]"]
  node_breakdown_ui["Oversized breakdown<br/>React flow<br/>[BreakdownFlow.js]"]
  node_result_ui["Recommendation reveal<br/>React component<br/>[Result.js]"]
  node_auth_mood_voice["Auth, mood, and voice<br/>client providers<br/>[AuthProvider.js]"]
end

subgraph group_api["Server decision APIs"]
  node_decide_api{{"Decision endpoint<br/>API route<br/>[route.js]"}}
  node_breakdown_api{{"Breakdown endpoint<br/>API route<br/>[route.js]"}}
  node_assist_api{{"Voice assistant endpoint<br/>API route<br/>[route.js]"}}
end

subgraph group_ai["AI and local intelligence"]
  node_score_engine["Weighted score engine<br/>deterministic engine<br/>[score.js]"]
  node_decision_rules["Categories and mood rules<br/>local rules<br/>[mood.js]"]
  node_oversized_rules["Oversized detector<br/>local rules<br/>[oversized.js]"]
  node_gemini_agents{{"Gemini decision agents<br/>server-only AI<br/>[explain.js]"}}
end

subgraph group_data["Identity and persistence"]
  node_supabase_clients[("Supabase clients<br/>browser and service clients<br/>[supabase-server.js]")]
  node_auth_signup{{"Auth and signup<br/>API flows<br/>[route.js]"}}
  node_decision_history[("Decisions and personality<br/>persistence service<br/>[decisions.js]")]
  node_winner_history[("Immutable winners<br/>database schema")]
end

subgraph group_products["Specialized products"]
  node_vote_product["Group voting<br/>vote page and API<br/>[page.js]"]
  node_vote_service[("Authoritative vote state<br/>group service<br/>[group.js]")]
  node_plan_product{{"Travel-aware planning<br/>plan page and API<br/>[route.js]"}}
  node_risk_product{{"SWOT and path analysis<br/>analysis API<br/>[route.js]"}}
end

node_app_shell --> node_decision_ui
node_app_shell -->|"mounts providers"| node_auth_mood_voice
node_decision_ui -->|"renders"| node_question_step
node_decision_ui -->|"screens decision"| node_oversized_rules
node_oversized_rules -->|"diverts large decisions"| node_breakdown_ui
node_decision_ui -->|"submits decision"| node_decide_api
node_breakdown_ui -->|"submits stages"| node_breakdown_api
node_decide_api -->|"calculates scores"| node_score_engine
node_decide_api -->|"applies weights"| node_decision_rules
node_decide_api -->|"requests explanation"| node_gemini_agents
node_score_engine -->|"authoritative result"| node_result_ui
node_decide_api -->|"returns explanation"| node_result_ui
node_breakdown_api -->|"requests decomposition"| node_gemini_agents
node_auth_mood_voice -->|"voice assistance"| node_assist_api
node_assist_api -->|"constrained response"| node_gemini_agents
node_decide_api -->|"persists decision"| node_decision_history
node_decision_history -->|"records winner"| node_winner_history
node_decision_history -->|"history insight prompt"| node_gemini_agents
node_auth_mood_voice -->|"browser auth"| node_supabase_clients
node_auth_signup -->|"Auth and service role"| node_supabase_clients
node_vote_product -->|"join, vote, refresh"| node_vote_service
node_vote_service -->|"security-definer functions and realtime"| node_supabase_clients
node_plan_product -->|"generates plan"| node_gemini_agents
node_plan_product -.-> node_risk_product
node_risk_product -->|"generates SWOT and paths"| node_gemini_agents
node_risk_product -->|"persists analysis"| node_decision_history

click node_app_shell "https://github.com/yazeedr1425/ahsem/blob/main/app/layout.js"
click node_decision_ui "https://github.com/yazeedr1425/ahsem/blob/main/app/page.js"
click node_question_step "https://github.com/yazeedr1425/ahsem/blob/main/app/components/QuestionStep.js"
click node_breakdown_ui "https://github.com/yazeedr1425/ahsem/blob/main/app/components/BreakdownFlow.js"
click node_result_ui "https://github.com/yazeedr1425/ahsem/blob/main/app/components/Result.js"
click node_auth_mood_voice "https://github.com/yazeedr1425/ahsem/blob/main/lib/auth/AuthProvider.js"
click node_decide_api "https://github.com/yazeedr1425/ahsem/blob/main/app/api/decide/route.js"
click node_breakdown_api "https://github.com/yazeedr1425/ahsem/blob/main/app/api/breakdown/route.js"
click node_assist_api "https://github.com/yazeedr1425/ahsem/blob/main/app/api/assist/route.js"
click node_score_engine "https://github.com/yazeedr1425/ahsem/blob/main/lib/engine/score.js"
click node_decision_rules "https://github.com/yazeedr1425/ahsem/blob/main/lib/engine/mood.js"
click node_oversized_rules "https://github.com/yazeedr1425/ahsem/blob/main/lib/engine/oversized.js"
click node_gemini_agents "https://github.com/yazeedr1425/ahsem/blob/main/lib/engine/explain.js"
click node_supabase_clients "https://github.com/yazeedr1425/ahsem/blob/main/lib/supabase-server.js"
click node_auth_signup "https://github.com/yazeedr1425/ahsem/blob/main/app/api/signup/route.js"
click node_decision_history "https://github.com/yazeedr1425/ahsem/blob/main/lib/services/decisions.js"
click node_winner_history "https://github.com/yazeedr1425/ahsem/blob/main/supabase/migrations/20260818140000_add_decision_winners.sql"
click node_vote_product "https://github.com/yazeedr1425/ahsem/blob/main/app/vote/%5Bcode%5D/page.js"
click node_vote_service "https://github.com/yazeedr1425/ahsem/blob/main/lib/services/group.js"
click node_plan_product "https://github.com/yazeedr1425/ahsem/blob/main/app/api/plan/route.js"
click node_risk_product "https://github.com/yazeedr1425/ahsem/blob/main/app/api/analyze/route.js"

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_app_shell,node_decision_ui,node_question_step,node_breakdown_ui,node_result_ui,node_auth_mood_voice toneBlue
class node_decide_api,node_breakdown_api,node_assist_api toneAmber
class node_score_engine,node_decision_rules,node_oversized_rules,node_gemini_agents toneMint
class node_supabase_clients,node_auth_signup,node_decision_history,node_winner_history toneRose
class node_vote_product,node_vote_service,node_plan_product,node_risk_product toneIndigo
```

## Components

### RTL client experience

App Router screens and client-side decision flows.

- [App shell](https://github.com/yazeedr1425/ahsem/blob/main/app/layout.js): Next.js layout · RTL layout and global providers. · `app/layout.js`
- [Decision questionnaire](https://github.com/yazeedr1425/ahsem/blob/main/app/page.js): React flow · Collects options, criteria, ratings, and mood. · `app/page.js`
- [Question stages](https://github.com/yazeedr1425/ahsem/blob/main/app/components/QuestionStep.js): React component · `app/components/QuestionStep.js`
- [Oversized breakdown](https://github.com/yazeedr1425/ahsem/blob/main/app/components/BreakdownFlow.js): React flow · Staged alternative for consequential decisions. · `app/components/BreakdownFlow.js`
- [Recommendation reveal](https://github.com/yazeedr1425/ahsem/blob/main/app/components/Result.js): React component · Shows recommendation and score transparency. · `app/components/Result.js`
- [Auth, mood, and voice](https://github.com/yazeedr1425/ahsem/blob/main/lib/auth/AuthProvider.js): client providers · Global identity, mood, and browser speech interaction. · `lib/auth/AuthProvider.js`

### Server decision APIs

Trusted routes coordinate engines, AI, and persistence.

- [Decision endpoint](https://github.com/yazeedr1425/ahsem/blob/main/app/api/decide/route.js): API route · Combines authoritative scores with constrained AI explanation. · `app/api/decide/route.js`
- [Breakdown endpoint](https://github.com/yazeedr1425/ahsem/blob/main/app/api/breakdown/route.js): API route · `app/api/breakdown/route.js`
- [Voice assistant endpoint](https://github.com/yazeedr1425/ahsem/blob/main/app/api/assist/route.js): API route · Server-side Gemini assistance for voice UI. · `app/api/assist/route.js`

### AI and local intelligence

Deterministic scoring remains authoritative over generated explanations.

- [Weighted score engine](https://github.com/yazeedr1425/ahsem/blob/main/lib/engine/score.js): deterministic engine · Calculates recommendation and transparent numerical breakdown. · `lib/engine/score.js`
- [Categories and mood rules](https://github.com/yazeedr1425/ahsem/blob/main/lib/engine/mood.js): local rules · Category criteria and mood-adjusted weight logic. · `lib/engine/mood.js`
- [Oversized detector](https://github.com/yazeedr1425/ahsem/blob/main/lib/engine/oversized.js): local rules · Routes large decisions away from the standard flow. · `lib/engine/oversized.js`
- [Gemini decision agents](https://github.com/yazeedr1425/ahsem/blob/main/lib/engine/explain.js): server-only AI · Produces constrained JSON explanations, decompositions, and verdicts. · `lib/engine/explain.js`

### Identity and persistence

Supabase provides auth, RLS-protected data, functions, and realtime.

- [Supabase clients](https://github.com/yazeedr1425/ahsem/blob/main/lib/supabase-server.js): browser and service clients · Separates browser access from trusted service-role operations. · `lib/supabase-server.js`
- [Auth and signup](https://github.com/yazeedr1425/ahsem/blob/main/app/api/signup/route.js): API flows · Signup compensates by deleting incomplete accounts after email failure. · `app/api/signup/route.js`
- [Decisions and personality](https://github.com/yazeedr1425/ahsem/blob/main/lib/services/decisions.js): persistence service · Persists decisions and aggregates history into decision insights. · `lib/services/decisions.js`
- [Immutable winners](https://github.com/yazeedr1425/ahsem/blob/main/supabase/migrations/20260818140000_add_decision_winners.sql): database schema · `supabase/migrations/20260818140000_add_decision_winners.sql`

### Specialized products

Planning, analysis, and collaborative voting flows.

- [Group voting](https://github.com/yazeedr1425/ahsem/blob/main/app/vote/%5Bcode%5D/page.js): vote page and API · Creator-managed groups joined by short code. · `app/vote/[code]/page.js`
- [Authoritative vote state](https://github.com/yazeedr1425/ahsem/blob/main/lib/services/group.js): group service · Clients re-fetch state after Broadcast; Presence reports participants. · `lib/services/group.js`
- [Travel-aware planning](https://github.com/yazeedr1425/ahsem/blob/main/app/api/plan/route.js): plan page and API · Gemini plans are enriched with places, routes, and optional weather. · `app/api/plan/route.js`
- [SWOT and path analysis](https://github.com/yazeedr1425/ahsem/blob/main/app/api/analyze/route.js): analysis API · Persists analysis for a major decision. · `app/api/analyze/route.js`

## Connections

- App shell → Decision questionnaire
- App shell → Auth, mood, and voice: mounts providers
- Decision questionnaire → Question stages: renders
- Decision questionnaire → Oversized detector: screens decision
- Oversized detector → Oversized breakdown: diverts large decisions
- Decision questionnaire → Decision endpoint: submits decision
- Oversized breakdown → Breakdown endpoint: submits stages
- Decision endpoint → Weighted score engine: calculates scores
- Decision endpoint → Categories and mood rules: applies weights
- Decision endpoint → Gemini decision agents: requests explanation
- Weighted score engine → Recommendation reveal: authoritative result
- Decision endpoint → Recommendation reveal: returns explanation
- Breakdown endpoint → Gemini decision agents: requests decomposition
- Auth, mood, and voice → Voice assistant endpoint: voice assistance
- Voice assistant endpoint → Gemini decision agents: constrained response
- Decision endpoint → Decisions and personality: persists decision
- Decisions and personality → Immutable winners: records winner
- Decisions and personality → Gemini decision agents: history insight prompt
- Auth, mood, and voice → Supabase clients: browser auth
- Auth and signup → Supabase clients: Auth and service role
- Group voting → Authoritative vote state: join, vote, refresh
- Authoritative vote state → Supabase clients: security-definer functions and realtime
- Travel-aware planning → Gemini decision agents: generates plan
- Travel-aware planning → SWOT and path analysis
- SWOT and path analysis → Gemini decision agents: generates SWOT and paths
- SWOT and path analysis → Decisions and personality: persists analysis
