# dunkeln/mobisim architecture

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

- Interactive diagram: https://gitdiagram.com/dunkeln/mobisim
- Repository: https://github.com/dunkeln/mobisim

## Overview

1. **What this repo is**
   - `mobisim` is a SvelteKit application that serves a **Chrome-first 360° vehicle inspection shell**.
   - The core experience is an **asset-scoped viewer**: one selected vehicle asset at a time, with chat, voice, inspection tools, semantic overlays, and deterministic edit/highlight behavior.

2. **Top-level app boundaries**
   - The route structure is split into:
     - **Public routes** for sign-in.
     - **Authenticated app routes** under `/(app)/app`, including the inspection page.
     - **API routes** under `/api/...` for chat, audio, semantic operations, and asset-specific actions.
   - This is a strong boundary in the architecture: the UI shell is gated, while most stateful operations happen through server routes.

3. **Viewer and inspection UI**
   - The 3D/inspection surface is built with **Threlte**-based components in `src/lib/components/threlte`.
   - Key UI areas include:
     - the **inspection viewport**
     - the **inspector sidebar**
     - a **footer chat/orb interaction area**
     - semantic/selection overlays and controls
   - The UI is designed to reflect the same asset state the backend uses, especially for selection, highlight, and semantic group visibility.

4. **Client-side state management**
   - The app uses Svelte stores for cross-component state:
     - request gating
     - chat request status
     - vehicle node selection
     - vehicle patches
     - semantic runtime state
     - inspector sidebar and supplementary footer state
   - These stores form the client-side coordination layer between the viewport, sidebar, footer, and async API results.

5. **Asset-scoped semantic model**
   - The repository is built around a **per-asset semantic boundary**: all intent, selection, overlays, and edits are scoped to the current vehicle asset.
   - The code and README emphasize that the **semantic overlay is the source of truth** for IDs, paths, meshes, and validation.
   - When overlay data is absent or stale, the system can fall back to GLTF-derived structure/summary data, but the overlay remains authoritative when present.

6. **Semantic ingestion and overlay pipeline**
   - The semantic stack is split into server connectors for:
     - **semantic ingress**: receiving/organizing semantic data
     - **semantic assignments**: binding semantic labels to asset structures
     - **semantic proposals**: producing candidate semantic mappings
     - **semantic overlay**: storing and serving the accepted overlay
   - There is also support for **GLTF preprocessing and structure extraction**, which feeds the semantic system when a richer overlay is unavailable.
   - This is the architecture’s main “asset understanding” pipeline.

7. **Chat and intent execution pipeline**
   - The chat system is not just freeform messaging; it routes through an **intent/execution engine**.
   - Server-side connectors under `openai-chat` and `vehicle-intents` handle:
     - prompt construction
     - model routing
     - intent resolution
     - tool definitions and arguments
     - patch generation
     - execution policy
     - normalized output for the UI
   - The important boundary here is that user text/voice becomes **deterministic vehicle actions** rather than ad hoc conversation.

8. **Voice orb and audio paths**
   - The footer orb supports two audio flows:
     - **buffered audio**: record → transcribe → execute the same vehicle chat/tool loop → synthesize a spoken reply
     - **full-duplex realtime voice**: live session over WebRTC when supported
   - The realtime path falls back to buffered audio if session setup fails.
   - This means the voice subsystem is an alternate front end to the same backend execution pipeline, not a separate feature.

9. **External services and persistence**
   - The main external integrations are:
     - **GitHub auth / auth secret infrastructure** for the gated app shell
     - **OpenAI** for chat, tool planning, transcription, TTS, and realtime voice
     - **DynamoDB** for user/context history in production, with DynamoDB Local for local/demo use
   - Semantic overlays are persisted separately in local storage paths by default, with a configurable local manifest directory.

10. **Context history layering**
   - Footer chat context is assembled from multiple tiers:
     - current request context
     - current asset snapshot
     - current asset recent history
     - user-global summary
   - That ordering is important to the architecture: it ensures the model sees the **current asset and current selection first**, while still allowing longer-term memory beneath it.

11. **Server route shape**
   - The API surface is asset-centric:
     - chat and audio endpoints
     - vehicle asset download/highlight/inspection/intent/paint
     - semantic ingress and overlay endpoints
     - window tint and other asset modifications
   - These routes are the bridge between the browser viewer and the server-side connectors that compute overlay changes, patches, and intent results.

12. **Determinism and side-effect control**
   - The repo’s design strongly prefers **deterministic, asset-local execution**:
     - semantic groups drive matching
     - highlights and selections are mirrored between UI and model context
     - whole-asset effects like x-ray/UV/wireframe are treated as deterministic view modes
   - This is a key architectural boundary: model outputs are constrained by the asset’s semantic graph and current UI state, rather than by global or cross-asset context.

13. **What to draw in an architecture diagram**
   - A useful diagram should show:
     - browser UI shell and Threlte viewport
     - Svelte stores as client state coordinator
     - authenticated SvelteKit server routes
     - connector layer for OpenAI, semantic overlay, vehicle intent, and context history
     - persistence for overlays and history
     - asset-specific semantic data flowing back into selection/highlight/patch UI
   - The central path is: **user action → client state/UI → API route → connector/model/storage → normalized result → viewer/store update**.

Main entry points: [`src/lib/stores/semantic-runtime.ts`](https://github.com/dunkeln/mobisim/blob/mucho/src/lib/stores/semantic-runtime.ts) (Stores), [`src/routes/(app)/app/inspect/[assetId]/+page.svelte`](https://github.com/dunkeln/mobisim/blob/mucho/src/routes/(app)/app/inspect/[assetId]/+page.svelte) (Inspect asset), [`src/lib/server/connectors/vehicle-semantic-overlay/index.ts`](https://github.com/dunkeln/mobisim/blob/mucho/src/lib/server/connectors/vehicle-semantic-overlay/index.ts) (Overlay store), [`src/routes/api/vehicle-assets/+server.ts`](https://github.com/dunkeln/mobisim/blob/mucho/src/routes/api/vehicle-assets/+server.ts) (Asset API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client app"]
  node_viewport["Viewport<br/>Threlte scene"]
  node_sidebar["Inspector<br/>sidebar ui"]
  node_footer_orb["Footer orb<br/>voice/chat ui<br/>[footer-orb.svelte]"]
  node_request_gate["Request gate<br/>client guard"]
  node_stores["Stores<br/>svelte state"]
  node_runtime_selection["Selection runtime<br/>viewer state"]
  node_semantic_runtime["Semantic runtime<br/>overlay state"]
end

subgraph group_routes["Routes"]
  node_public_signin["Sign in<br/>public page<br/>[+page.svelte]"]
  node_app_shell["App shell<br/>authenticated layout<br/>[+layout.svelte]"]
  node_inspection_page["Inspect asset<br/>asset page<br/>[+page.svelte]"]
end

subgraph group_server["Server connectors"]
  node_api_chat["Chat API<br/>server route<br/>[+server.ts]"]
  node_api_audio["Audio API<br/>server route<br/>[+server.ts]"]
  node_api_vehicle_assets["Asset API<br/>server routes<br/>[+server.ts]"]
  node_openai_chat["OpenAI chat<br/>intent engine<br/>[index.ts]"]
  node_vehicle_intents["Vehicle intents<br/>action planner<br/>[index.ts]"]
  node_semantic_ingress["Semantic ingress<br/>ingest connector<br/>[index.ts]"]
  node_semantic_overlay["Overlay store<br/>semantic overlay<br/>[index.ts]"]
  node_gltf_pipeline["GLTF pipeline<br/>structure extractor<br/>[index.ts]"]
  node_context_history["Context history<br/>memory layer<br/>[index.ts]"]
  node_vehicle_registry["Vehicle registry<br/>asset storage<br/>[index.ts]"]
  node_auth["Auth identity<br/>auth boundary<br/>[identity.ts]"]
end

subgraph group_storage["Persistence"]
  node_storage[("Data stores<br/>persistence")]
end

node_public_signin -->|"sign in"| node_auth
node_auth -->|"gate"| node_app_shell
node_app_shell -->|"route"| node_inspection_page
node_inspection_page -->|"render"| node_viewport
node_inspection_page -->|"render"| node_sidebar
node_inspection_page -->|"render"| node_footer_orb
node_footer_orb -->|"gate"| node_request_gate
node_viewport -->|"sync"| node_runtime_selection
node_sidebar -->|"update"| node_stores
node_footer_orb -->|"update"| node_stores
node_runtime_selection -->|"mirror"| node_semantic_runtime
node_stores -->|"submit"| node_api_chat
node_stores -->|"voice"| node_api_audio
node_stores -->|"asset ops"| node_api_vehicle_assets
node_api_chat -->|"plan"| node_openai_chat
node_api_audio -->|"transcribe/tts"| node_openai_chat
node_api_vehicle_assets -->|"execute"| node_vehicle_intents
node_api_vehicle_assets -->|"ingest"| node_semantic_ingress
node_vehicle_intents -->|"patch"| node_semantic_overlay
node_semantic_ingress -->|"fallback"| node_gltf_pipeline
node_semantic_overlay -->|"drive ui"| node_viewport
node_semantic_overlay -->|"source"| node_semantic_runtime
node_openai_chat -->|"context"| node_context_history
node_context_history -->|"read/write"| node_storage
node_semantic_overlay -->|"persist"| node_storage
node_vehicle_registry -->|"lookup"| node_api_vehicle_assets

click node_public_signin "https://github.com/dunkeln/mobisim/blob/mucho/src/routes/(public)/signin/+page.svelte"
click node_app_shell "https://github.com/dunkeln/mobisim/blob/mucho/src/routes/(app)/app/+layout.svelte"
click node_inspection_page "https://github.com/dunkeln/mobisim/blob/mucho/src/routes/(app)/app/inspect/[assetId]/+page.svelte"
click node_viewport "https://github.com/dunkeln/mobisim/blob/mucho/src/lib/components/threlte/inspection-viewport.svelte"
click node_sidebar "https://github.com/dunkeln/mobisim/blob/mucho/src/lib/components/inspector/inspector-sidebar.svelte"
click node_footer_orb "https://github.com/dunkeln/mobisim/blob/mucho/src/lib/components/ui/footer-orb.svelte"
click node_request_gate "https://github.com/dunkeln/mobisim/blob/mucho/src/lib/components/ui/request-gate.svelte"
click node_stores "https://github.com/dunkeln/mobisim/blob/mucho/src/lib/stores/semantic-runtime.ts"
click node_runtime_selection "https://github.com/dunkeln/mobisim/blob/mucho/src/lib/components/threlte/runtime-selection.ts"
click node_semantic_runtime "https://github.com/dunkeln/mobisim/blob/mucho/src/lib/stores/semantic-runtime.ts"
click node_api_chat "https://github.com/dunkeln/mobisim/blob/mucho/src/routes/api/chat/+server.ts"
click node_api_audio "https://github.com/dunkeln/mobisim/blob/mucho/src/routes/api/chat/audio/realtime/+server.ts"
click node_api_vehicle_assets "https://github.com/dunkeln/mobisim/blob/mucho/src/routes/api/vehicle-assets/+server.ts"
click node_openai_chat "https://github.com/dunkeln/mobisim/blob/mucho/src/lib/server/connectors/openai-chat/index.ts"
click node_vehicle_intents "https://github.com/dunkeln/mobisim/blob/mucho/src/lib/server/connectors/vehicle-intents/index.ts"
click node_semantic_ingress "https://github.com/dunkeln/mobisim/blob/mucho/src/lib/server/connectors/semantic-ingress/index.ts"
click node_semantic_overlay "https://github.com/dunkeln/mobisim/blob/mucho/src/lib/server/connectors/vehicle-semantic-overlay/index.ts"
click node_gltf_pipeline "https://github.com/dunkeln/mobisim/blob/mucho/src/lib/server/connectors/gltf-structure/index.ts"
click node_context_history "https://github.com/dunkeln/mobisim/blob/mucho/src/lib/server/connectors/context-history/index.ts"
click node_vehicle_registry "https://github.com/dunkeln/mobisim/blob/mucho/src/lib/server/connectors/vehicle-registry/index.ts"
click node_auth "https://github.com/dunkeln/mobisim/blob/mucho/src/lib/server/auth/identity.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_viewport,node_sidebar,node_footer_orb,node_request_gate,node_stores,node_runtime_selection,node_semantic_runtime toneBlue
class node_public_signin,node_app_shell,node_inspection_page toneAmber
class node_api_chat,node_api_audio,node_api_vehicle_assets,node_openai_chat,node_vehicle_intents,node_semantic_ingress,node_semantic_overlay,node_gltf_pipeline,node_context_history,node_vehicle_registry,node_auth toneMint
class node_storage toneRose
```

## Components

### Client app

SvelteKit browser shell, viewer, sidebar, footer, and shared client state.

- [Viewport](https://github.com/dunkeln/mobisim/blob/mucho/src/lib/components/threlte/inspection-viewport.svelte): Threlte scene · 3D inspection surface and selection/highlight rendering. · `src/lib/components/threlte/inspection-viewport.svelte`
- [Inspector](https://github.com/dunkeln/mobisim/blob/mucho/src/lib/components/inspector/inspector-sidebar.svelte): sidebar ui · Controls for selection, camera, lighting, and semantic ingress state. · `src/lib/components/inspector/inspector-sidebar.svelte`
- [Footer orb](https://github.com/dunkeln/mobisim/blob/mucho/src/lib/components/ui/footer-orb.svelte): voice/chat ui · Chat and voice interaction surface in the footer. · `src/lib/components/ui/footer-orb.svelte`
- [Request gate](https://github.com/dunkeln/mobisim/blob/mucho/src/lib/components/ui/request-gate.svelte): client guard · Coordinates request availability and interaction gating. · `src/lib/components/ui/request-gate.svelte`
- [Stores](https://github.com/dunkeln/mobisim/blob/mucho/src/lib/stores/semantic-runtime.ts): svelte state · Shared client state for chat, selection, patches, semantic runtime, and sidebar/footer coordination. · `src/lib/stores/semantic-runtime.ts`
- [Selection runtime](https://github.com/dunkeln/mobisim/blob/mucho/src/lib/components/threlte/runtime-selection.ts): viewer state · Mirrors selected vehicle nodes and highlight state into the viewport. · `src/lib/components/threlte/runtime-selection.ts`
- [Semantic runtime](https://github.com/dunkeln/mobisim/blob/mucho/src/lib/stores/semantic-runtime.ts): overlay state · Client-side semantic overlay and visibility coordination. · `src/lib/stores/semantic-runtime.ts`

### Routes

Public app routes and authenticated inspection entry points.

- [Sign in](https://github.com/dunkeln/mobisim/blob/mucho/src/routes/(public)/signin/+page.svelte): public page · Public entry point for GitHub-gated access. · `src/routes/(public)/signin/+page.svelte`
- [App shell](https://github.com/dunkeln/mobisim/blob/mucho/src/routes/(app)/app/+layout.svelte): authenticated layout · Authenticated SvelteKit shell for the app area. · `src/routes/(app)/app/+layout.svelte`
- [Inspect asset](https://github.com/dunkeln/mobisim/blob/mucho/src/routes/(app)/app/inspect/[assetId]/+page.svelte): asset page · Asset-scoped inspection route for one selected vehicle at a time. · `src/routes/(app)/app/inspect/[assetId]/+page.svelte`

### Server connectors

API handlers and backend orchestration for chat, semantic operations, and vehicle actions.

- [Chat API](https://github.com/dunkeln/mobisim/blob/mucho/src/routes/api/chat/+server.ts): server route · Main text chat endpoint that turns user intent into normalized actions. · `src/routes/api/chat/+server.ts`
- [Audio API](https://github.com/dunkeln/mobisim/blob/mucho/src/routes/api/chat/audio/realtime/+server.ts): server route · Buffered and realtime voice endpoints for the same execution loop. · `src/routes/api/chat/audio/realtime/+server.ts`
- [Asset API](https://github.com/dunkeln/mobisim/blob/mucho/src/routes/api/vehicle-assets/+server.ts): server routes · Asset-centric endpoints for inspection, intent, highlight, paint, tint, and semantic operations. · `src/routes/api/vehicle-assets/+server.ts`
- [OpenAI chat](https://github.com/dunkeln/mobisim/blob/mucho/src/lib/server/connectors/openai-chat/index.ts): intent engine · Prompting, routing, tool definitions, execution policy, and normalized model output. · `src/lib/server/connectors/openai-chat/index.ts`
- [Vehicle intents](https://github.com/dunkeln/mobisim/blob/mucho/src/lib/server/connectors/vehicle-intents/index.ts): action planner · Resolves chat output into deterministic vehicle actions and patch ops. · `src/lib/server/connectors/vehicle-intents/index.ts`
- [Semantic ingress](https://github.com/dunkeln/mobisim/blob/mucho/src/lib/server/connectors/semantic-ingress/index.ts): ingest connector · Receives and organizes semantic data for an asset. · `src/lib/server/connectors/semantic-ingress/index.ts`
- [Overlay store](https://github.com/dunkeln/mobisim/blob/mucho/src/lib/server/connectors/vehicle-semantic-overlay/index.ts): semantic overlay · Serves the authoritative per-asset overlay when present. · `src/lib/server/connectors/vehicle-semantic-overlay/index.ts`
- [GLTF pipeline](https://github.com/dunkeln/mobisim/blob/mucho/src/lib/server/connectors/gltf-structure/index.ts): structure extractor · Preprocesses models and derives structure when richer overlays are unavailable. · `src/lib/server/connectors/gltf-structure/index.ts`
- [Context history](https://github.com/dunkeln/mobisim/blob/mucho/src/lib/server/connectors/context-history/index.ts): memory layer · Builds current-request, asset, recent-history, and global-summary context. · `src/lib/server/connectors/context-history/index.ts`
- [Vehicle registry](https://github.com/dunkeln/mobisim/blob/mucho/src/lib/server/connectors/vehicle-registry/index.ts): asset storage · Lookup and storage layer for vehicle asset metadata. · `src/lib/server/connectors/vehicle-registry/index.ts`
- [Auth identity](https://github.com/dunkeln/mobisim/blob/mucho/src/lib/server/auth/identity.ts): auth boundary · GitHub-backed identity gate for the authenticated shell. · `src/lib/server/auth/identity.ts`

### Persistence

History and overlay storage used by server-side connectors.

- **Data stores**: persistence · DynamoDB history and local overlay storage used by server connectors.

## Connections

- Sign in → Auth identity: sign in · Public access flows into the auth boundary.
- Auth identity → App shell: gate · Authenticated users reach the app shell.
- App shell → Inspect asset: route · Shell loads the asset-scoped inspection page.
- Inspect asset → Viewport: render · Page renders the Threlte inspection surface.
- Inspect asset → Inspector: render · Page renders the inspector controls.
- Inspect asset → Footer orb: render · Page renders chat and voice interaction.
- Footer orb → Request gate: gate · Voice/chat interactions are gated by request state.
- Viewport → Selection runtime: sync · Selection and highlight state are mirrored into the scene.
- Inspector → Stores: update · Inspector controls update shared client state.
- Footer orb → Stores: update · Chat and voice interactions update shared client state.
- Selection runtime → Semantic runtime: mirror · Viewer selection is reflected in semantic runtime state.
- Stores → Chat API: submit · Chat requests flow from client state to the server.
- Stores → Audio API: voice · Buffered or realtime voice requests flow to audio endpoints.
- Stores → Asset API: asset ops · Selection, highlight, paint, tint, and semantic operations go to asset routes.
- Chat API → OpenAI chat: plan · Chat route delegates to intent planning and model routing.
- Audio API → OpenAI chat: transcribe/tts · Audio routes reuse the same model-backed chat pipeline.
- Asset API → Vehicle intents: execute · Asset actions are normalized into deterministic vehicle intents.
- Asset API → Semantic ingress: ingest · Semantic ingress endpoints feed the semantic pipeline.
- Vehicle intents → Overlay store: patch · Intent execution produces overlay and asset-local patch results.
- Semantic ingress → GLTF pipeline: fallback · Structure extraction supports semantic ingestion when overlays are incomplete.
- Overlay store → Viewport: drive ui · Overlay data determines what the viewer can select and show.
- Overlay store → Semantic runtime: source · Overlay state is the source of truth for semantic visibility and IDs.
- OpenAI chat → Context history: context · Prompt assembly uses layered asset and user history.
- Context history → Data stores: read/write · History is backed by DynamoDB or local storage.
- Overlay store → Data stores: persist · Accepted overlays are stored separately from chat history.
- Vehicle registry → Asset API: lookup · Asset routes resolve the current vehicle and its metadata.
