# sinsinpurin/osanpo-story architecture

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

- Interactive diagram: https://gitdiagram.com/sinsinpurin/osanpo-story
- Repository: https://github.com/sinsinpurin/osanpo-story

## Overview

#### Purpose and deployment shape
おさんぽストーリー is an Expo React Native iOS/Android walking app. A user selects a map destination or desired distance; the system generates a detour/round-trip pedestrian course with location-triggered checkpoint trivia. The repository has two deployable boundaries: mobile client in `app/` and Supabase database plus Edge Function in `supabase/`.

#### Mobile entry and navigation
The application starts at `app/index.ts`, with provider composition and navigation rooted in `app/App.tsx`. A stack flow in `app/src/navigation/RootNavigator.tsx` drives Start → destination input → route preview → active walk → completion. Navigation contracts are centralized in `app/src/navigation/types.ts`.

#### Course request and preview
`app/src/screens/DestinationInputScreen.tsx` collects a pinned destination or distance-based walk request, obtains current location, and invokes the backend through `app/src/services/api.ts`. `app/src/screens/RoutePreviewScreen.tsx` renders the returned route/checkpoints before the user commits to walking. Shared course payload definitions reside in `app/src/types/course.ts`.

#### Active-walk state machine
`app/src/screens/WalkScreen.tsx` is the runtime walking experience: it starts location/geofence observation, tracks checkpoint arrivals, and displays trivia. `app/src/services/location.ts` owns GPS permissions, current-position acquisition, and checkpoint proximity monitoring. This separates device/location lifecycle concerns from screen presentation. Geographic calculations used client-side are in `app/src/utils/geo.ts`.

#### Local persistence and progression
The app persists generated courses, visited checkpoints, aggregate user progress, and individual walk sessions in local SQLite via `app/src/services/storage.ts`. Completion is handled by `app/src/screens/WalkCompleteScreen.tsx`, which calculates and presents earned XP/levels using `app/src/utils/xp.ts`. Thus walking can update durable local progression independently of backend course-generation availability.

#### Edge Function generation pipeline
`supabase/functions/generate-course/index.ts` is the server entry point and orchestrator. It resolves request mode, computes a walking route, finds route-adjacent places, enriches them, caches results, and returns a course. Route geometry utilities and suitability checks live in `supabase/functions/generate-course/geo.ts`; destination-name reverse geocoding is isolated in `supabase/functions/generate-course/nominatim.ts`.

#### External enrichment and persistence
The Edge Function calls OpenRouteService through `supabase/functions/generate-course/ors.ts` for point-to-point detours or `round_trip` routes, then Overpass through `supabase/functions/generate-course/overpass.ts` for checkpoint candidates. It generates/cache trivia with Anthropic Claude through `supabase/functions/generate-course/trivia.ts`. Images prefer tagged Wikipedia metadata/search in `supabase/functions/generate-course/wikipedia.ts`, with Google Places Text Search/photo resources in `supabase/functions/generate-course/places.ts` as an optional higher-fidelity fallback. Supabase Postgres caches courses and OSM-ID-scoped trivia, initialized in `supabase/migrations/0001_init.sql` and extended for checkpoint image/photo metadata in `supabase/migrations/0002_add_checkpoint_image.sql` and `supabase/migrations/0005_add_place_photo_name.sql`.

#### Runtime and service boundaries
Client runtime is Expo/React Native with SQLite and device location/geofencing; production background detection requires a development/EAS-style native build rather than Expo Go. Backend runtime is a Supabase Edge Function using auto-provided Supabase URL/service credentials. Required secrets are ORS, Anthropic, and a policy-compliant Nominatim User-Agent; Google Places is optional. The client uses public Supabase URL and publishable key to invoke the Edge Function.

Main entry points: [`supabase/functions/generate-course/index.ts`](https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/functions/generate-course/index.ts) (Generate course), [`app/src/screens/WalkScreen.tsx`](https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/screens/WalkScreen.tsx) (Active walk), [`app/src/services/api.ts`](https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/services/api.ts) (Course API client), [`app/src/screens/DestinationInputScreen.tsx`](https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/screens/DestinationInputScreen.tsx) (Destination input)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_mobile["Expo mobile client"]
  node_entry["App entry<br/>Expo entry<br/>[index.ts]"]
  node_app["Providers &amp; app shell<br/>React app<br/>[App.tsx]"]
  node_navigation["Walk flow navigator<br/>stack navigation<br/>[RootNavigator.tsx]"]
  node_destination["Destination input<br/>screen"]
  node_preview["Route preview<br/>screen"]
  node_walk["Active walk<br/>screen<br/>[WalkScreen.tsx]"]
  node_complete["Walk completion<br/>screen"]
  node_location["Location lifecycle<br/>device location service<br/>[location.ts]"]
  node_client_api["Course API client<br/>Supabase function client<br/>[api.ts]"]
  node_local_store[("Local walk store<br/>SQLite persistence<br/>[storage.ts]")]
end

subgraph group_backend["Supabase backend"]
  node_edge{{"Generate course<br/>Supabase Edge Function<br/>[index.ts]"}}
  node_route_logic["Route geometry<br/>geospatial logic<br/>[geo.ts]"]
  node_course_cache[("Course &amp; trivia cache<br/>Supabase Postgres<br/>[0001_init.sql]")]
end

subgraph group_external["External services"]
  node_ors["OpenRouteService<br/>walking routing API<br/>[ors.ts]"]
  node_overpass["Overpass<br/>OSM places API<br/>[overpass.ts]"]
  node_trivia["Claude trivia generation<br/>Anthropic API integration<br/>[trivia.ts]"]
  node_wikipedia["Wikipedia images<br/>metadata/search integration<br/>[wikipedia.ts]"]
  node_places["Google Places fallback<br/>places/photo integration<br/>[places.ts]"]
  node_nominatim["Nominatim<br/>reverse geocoding integration<br/>[nominatim.ts]"]
end

node_entry -->|"boots"| node_app
node_app -->|"hosts"| node_navigation
node_navigation -->|"starts request"| node_destination
node_destination -->|"gets current position"| node_location
node_destination -->|"requests course"| node_client_api
node_client_api -->|"invokes"| node_edge
node_edge -->|"reads/writes cache"| node_course_cache
node_edge -->|"evaluates route"| node_route_logic
node_edge -->|"requests walking route"| node_ors
node_edge -->|"finds checkpoint candidates"| node_overpass
node_edge -->|"reverse geocodes destination"| node_nominatim
node_edge -->|"generates trivia"| node_trivia
node_edge -->|"enriches images"| node_wikipedia
node_edge -.->|"optional fallback"| node_places
node_edge -->|"returns course"| node_client_api
node_client_api -->|"supplies course"| node_preview
node_preview -->|"commits walk"| node_walk
node_walk -->|"starts monitoring"| node_location
node_walk -->|"persists visits/session"| node_local_store
node_walk -->|"finishes walk"| node_complete
node_complete -->|"updates progression"| node_local_store

click node_entry "https://github.com/sinsinpurin/osanpo-story/blob/master/app/index.ts"
click node_app "https://github.com/sinsinpurin/osanpo-story/blob/master/app/App.tsx"
click node_navigation "https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/navigation/RootNavigator.tsx"
click node_destination "https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/screens/DestinationInputScreen.tsx"
click node_preview "https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/screens/RoutePreviewScreen.tsx"
click node_walk "https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/screens/WalkScreen.tsx"
click node_complete "https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/screens/WalkCompleteScreen.tsx"
click node_location "https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/services/location.ts"
click node_client_api "https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/services/api.ts"
click node_local_store "https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/services/storage.ts"
click node_edge "https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/functions/generate-course/index.ts"
click node_route_logic "https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/functions/generate-course/geo.ts"
click node_course_cache "https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/migrations/0001_init.sql"
click node_ors "https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/functions/generate-course/ors.ts"
click node_overpass "https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/functions/generate-course/overpass.ts"
click node_trivia "https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/functions/generate-course/trivia.ts"
click node_wikipedia "https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/functions/generate-course/wikipedia.ts"
click node_places "https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/functions/generate-course/places.ts"
click node_nominatim "https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/functions/generate-course/nominatim.ts"

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_entry,node_app,node_navigation,node_destination,node_preview,node_walk,node_complete,node_location,node_client_api,node_local_store toneBlue
class node_edge,node_route_logic,node_course_cache toneAmber
class node_ors,node_overpass,node_trivia,node_wikipedia,node_places,node_nominatim toneMint
```

## Components

### Expo mobile client

React Native walking experience.

- [App entry](https://github.com/sinsinpurin/osanpo-story/blob/master/app/index.ts): Expo entry · `app/index.ts`
- [Providers & app shell](https://github.com/sinsinpurin/osanpo-story/blob/master/app/App.tsx): React app · `app/App.tsx`
- [Walk flow navigator](https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/navigation/RootNavigator.tsx): stack navigation · Start through completion flow and route contracts. · `app/src/navigation/RootNavigator.tsx`
- [Destination input](https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/screens/DestinationInputScreen.tsx): screen · Collects destination or distance requests. · `app/src/screens/DestinationInputScreen.tsx`
- [Route preview](https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/screens/RoutePreviewScreen.tsx): screen · Shows generated route and checkpoints before commitment. · `app/src/screens/RoutePreviewScreen.tsx`
- [Active walk](https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/screens/WalkScreen.tsx): screen · Tracks arrivals and presents checkpoint trivia. · `app/src/screens/WalkScreen.tsx`
- [Walk completion](https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/screens/WalkCompleteScreen.tsx): screen · Calculates and presents earned XP and levels. · `app/src/screens/WalkCompleteScreen.tsx`
- [Location lifecycle](https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/services/location.ts): device location service · Owns permissions, positioning, and checkpoint monitoring. · `app/src/services/location.ts`
- [Course API client](https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/services/api.ts): Supabase function client · `app/src/services/api.ts`
- [Local walk store](https://github.com/sinsinpurin/osanpo-story/blob/master/app/src/services/storage.ts): SQLite persistence · Persists courses, visits, progress, and sessions. · `app/src/services/storage.ts`

### Supabase backend

Course-generation boundary.

- [Generate course](https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/functions/generate-course/index.ts): Supabase Edge Function · Orchestrates routing, place enrichment, trivia, and caching. · `supabase/functions/generate-course/index.ts`
- [Route geometry](https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/functions/generate-course/geo.ts): geospatial logic · Applies route suitability and geographic calculations. · `supabase/functions/generate-course/geo.ts`
- [Course & trivia cache](https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/migrations/0001_init.sql): Supabase Postgres · Caches courses, OSM-scoped trivia, and checkpoint image metadata. · `supabase/migrations/0001_init.sql`

### External services

- [OpenRouteService](https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/functions/generate-course/ors.ts): walking routing API · Provides detour and round-trip route geometry. · `supabase/functions/generate-course/ors.ts`
- [Overpass](https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/functions/generate-course/overpass.ts): OSM places API · Finds route-adjacent checkpoint candidates. · `supabase/functions/generate-course/overpass.ts`
- [Claude trivia generation](https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/functions/generate-course/trivia.ts): Anthropic API integration · Generates checkpoint trivia for caching. · `supabase/functions/generate-course/trivia.ts`
- [Wikipedia images](https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/functions/generate-course/wikipedia.ts): metadata/search integration · Preferred tagged image source. · `supabase/functions/generate-course/wikipedia.ts`
- [Google Places fallback](https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/functions/generate-course/places.ts): places/photo integration · Optional higher-fidelity image and place fallback. · `supabase/functions/generate-course/places.ts`
- [Nominatim](https://github.com/sinsinpurin/osanpo-story/blob/master/supabase/functions/generate-course/nominatim.ts): reverse geocoding integration · Resolves destination names. · `supabase/functions/generate-course/nominatim.ts`

## Connections

- App entry → Providers & app shell: boots
- Providers & app shell → Walk flow navigator: hosts
- Walk flow navigator → Destination input: starts request
- Destination input → Location lifecycle: gets current position
- Destination input → Course API client: requests course
- Course API client → Generate course: invokes
- Generate course → Course & trivia cache: reads/writes cache
- Generate course → Route geometry: evaluates route
- Generate course → OpenRouteService: requests walking route
- Generate course → Overpass: finds checkpoint candidates
- Generate course → Nominatim: reverse geocodes destination
- Generate course → Claude trivia generation: generates trivia
- Generate course → Wikipedia images: enriches images
- Generate course → Google Places fallback: optional fallback
- Generate course → Course API client: returns course
- Course API client → Route preview: supplies course
- Route preview → Active walk: commits walk
- Active walk → Location lifecycle: starts monitoring
- Active walk → Local walk store: persists visits/session
- Active walk → Walk completion: finishes walk
- Walk completion → Local walk store: updates progression
