# minxie-ng/trustkaki architecture

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

- Interactive diagram: https://gitdiagram.com/minxie-ng/trustkaki
- Repository: https://github.com/minxie-ng/trustkaki
- Video tour (about a minute): https://gitdiagram.com/minxie-ng/trustkaki/video

## Overview

TrustKaki is an AI-assisted check-in and care-coordination system for Active Ageing Centres. A senior initiates the main workflow by messaging through Telegram or WhatsApp; TrustKaki interprets the update with personal context, records care signals, and identifies patterns across time so staff can review a prioritised case and decide how to follow up. Scheduled Telegram outreach is also documented, but depends on an authorised recurring worker invocation.

#### Components

##### Messaging and check-ins
- **Senior** — **Path:** null (external actor). Sends conversational updates that start the primary workflow.
- **Telegram** — **Path:** null (external platform). Carries senior messages and replies; scheduled outreach is documented but not self-activating.
- **WhatsApp** — **Path:** null (external platform). Provides a second messaging channel; live delivery depends on external business verification.
- **Telegram webhook and service** — **Path:** `trustkaki-web/src/app/api/telegram/webhook/route.ts`. Receives Telegram webhook events; the repository also has a Telegram service for channel handling.
- **WhatsApp webhook and service** — **Path:** `trustkaki-web/src/app/api/whatsapp/webhook/route.ts`. Receives WhatsApp webhook events; the repository also has a WhatsApp service for channel handling.
- **Check-in processing** — **Path:** `trustkaki-web/src/lib/checkins/service.ts`. Implements check-in service and policy responsibilities; an internal endpoint exposes due-check-in processing.

##### Understanding, memory, and patterns
- **Senior context** — **Path:** `trustkaki-web/src/lib/persistence/seniorContextRepository.ts`. Loads senior context and, when enabled, retrieves relevant personal memory from the Neon-backed memory application.
- **Agent orchestration** — **Path:** `trustkaki-web/src/lib/agents/orchestrator.ts`. Implements the agent orchestration layer; prompts and contracts define context use and validated decision shapes.
- **Personal memory** — **Path:** `trustkaki-web/src/lib/memory/applicationRepository.ts`. Provides the application-facing memory operations; the context repository uses it for retrieval.
- **Pattern Watch** — **Path:** `trustkaki-web/src/lib/patterns/patternWatch.ts`. Detects supported changes across observations; README describes combining signals across days for staff review.
- **Orchestration persistence** — **Path:** `trustkaki-web/src/lib/persistence/orchestrationRepository.ts`. Persists orchestration results and related signals, and invokes automatic context updates and pattern watching.

##### Care workspace and staff operations
- **Dashboard API** — **Path:** `trustkaki-web/src/app/api/dashboard/state/route.ts`. Exposes dashboard state to the client.
- **Care workspace UI** — **Path:** `trustkaki-web/src/components/Dashboard.tsx`. Presents staff-facing care work; dashboard subcomponents cover cases, context, contacts, and follow-up.
- **Caregiver actions** — **Path:** `trustkaki-web/src/app/api/caregiver/queue-action/route.ts`. Provides an API entry point for caregiver queue actions.

##### State and external interfaces
- **Supabase persistence** — **Path:** `trustkaki-web/src/lib/persistence/persistenceSupport.ts`. Supplies the server-side Supabase client and persistence metadata used by persistence modules.
- **Neon memory database** — **Path:** `trustkaki-web/src/lib/memory/neonDatabase.ts`. Implements the Neon database layer for personal memory; the README distinguishes this memory store from operational care records.
- **Staff user** — **Path:** null (external actor). Authenticated care staff inspect cases and record follow-up decisions.
- **Internal scheduled invoker** — **Path:** null (external actor). Must invoke internal processing endpoints to activate scheduled work.

#### Relationships

- **Senior -> Telegram**: sends a check-in message; evidence: `README.md`.
- **Senior -> WhatsApp**: sends a check-in message; evidence: `README.md`.
- **Telegram -> Telegram webhook and service**: delivers inbound events to the webhook entry point; evidence: `trustkaki-web/src/app/api/telegram/webhook/route.ts` (inferred from the route’s role; handler-to-service wiring was not excerpted).
- **WhatsApp -> WhatsApp webhook and service**: delivers inbound events to the webhook entry point; evidence: `trustkaki-web/src/app/api/whatsapp/webhook/route.ts` (inferred from the route’s role; handler-to-service wiring was not excerpted).
- **Telegram webhook and service -> Senior context**: resolves messaging identity and assembles context for channel conversations; evidence: `trustkaki-web/src/lib/persistence/seniorContextRepository.ts` (messaging-identity lookup is shown; caller wiring is not).
- **WhatsApp webhook and service -> Senior context**: resolves messaging identity and assembles context for channel conversations; evidence: `trustkaki-web/src/lib/persistence/seniorContextRepository.ts` (messaging-identity lookup is shown; caller wiring is not).
- **Senior context -> Personal memory**: retrieves relevant Neon personal memory when enabled; evidence: `trustkaki-web/src/lib/persistence/seniorContextRepository.ts`.
- **Telegram webhook and service -> Agent orchestration**: processes senior messages through the shared agent workflow; evidence: `README.md` (the README says channel services call the shared orchestrator directly).
- **WhatsApp webhook and service -> Agent orchestration**: processes senior messages through the shared agent workflow; evidence: `README.md`.
- **Agent orchestration -> Orchestration persistence**: saves validated orchestration outcomes; evidence: `README.md` (workflow documented; direct call site not sampled).
- **Orchestration persistence -> Pattern Watch**: invokes pattern detection for a senior; evidence: `trustkaki-web/src/lib/persistence/orchestrationRepository.ts`.
- **Orchestration persistence -> Supabase persistence**: writes orchestration-related records and signals; evidence: `trustkaki-web/src/lib/persistence/orchestrationRepository.ts`.
- **Senior context -> Supabase persistence**: reads senior and care context; evidence: `trustkaki-web/src/lib/persistence/seniorContextRepository.ts`.
- **Neon memory database -> Personal memory**: provides the database layer used by memory operations; evidence: none (inferred from component responsibilities; concrete application wiring was not excerpted).
- **Dashboard API -> Supabase persistence**: reads dashboard state; evidence: `trustkaki-web/src/app/api/dashboard/state/route.ts` (persistence call was not excerpted).
- **Dashboard API -> Care workspace UI**: supplies state for staff dashboard use; evidence: `README.md`.
- **Staff user -> Care workspace UI**: reviews prioritised cases and supporting context; evidence: `README.md`.
- **Staff user -> Caregiver actions**: submits follow-up or queue actions; evidence: `trustkaki-web/src/app/api/caregiver/queue-action/route.ts` (external request relationship).
- **Internal scheduled invoker -> Check-in processing**: invokes due-check-in processing; evidence: `README.md`.

#### Coverage limits

The sampled source excerpts do not show the webhook handlers’ service calls, the full agent orchestration implementation, or dashboard and caregiver route persistence calls. Those connections are identified as documented or inferred rather than asserted as directly observed. The README notes that scheduled activation and live channel availability depend on external configuration.

Main entry points: [`trustkaki-web/src/lib/persistence/seniorContextRepository.ts`](https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/lib/persistence/seniorContextRepository.ts) (Senior context), [`trustkaki-web/src/app/api/telegram/webhook/route.ts`](https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/app/api/telegram/webhook/route.ts) (Telegram webhook), [`trustkaki-web/src/app/api/whatsapp/webhook/route.ts`](https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/app/api/whatsapp/webhook/route.ts) (WhatsApp webhook), [`trustkaki-web/src/lib/persistence/orchestrationRepository.ts`](https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/lib/persistence/orchestrationRepository.ts) (Orchestration persistence)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_messaging["Messaging &amp; Check-ins"]
  node_senior(("Senior<br/>External actor"))
  node_telegram["Telegram<br/>Messaging platform"]
  node_whatsapp["WhatsApp<br/>Messaging platform"]
  node_telegram_webhook["Telegram webhook<br/>Webhook and service"]
  node_whatsapp_webhook["WhatsApp webhook<br/>Webhook and service"]
  node_checkin_processing["Check-in processing<br/>[service.ts]"]
end

subgraph group_understanding["Understanding &amp; Patterns"]
  node_senior_context["Senior context"]
  node_agent_orchestration["Agent orchestration<br/>Orchestrator"]
  node_personal_memory["Personal memory"]
  node_pattern_watch["Pattern Watch<br/>Pattern detection"]
  node_orchestration_persistence["Orchestration persistence"]
end

subgraph group_care["Care Workspace"]
  node_dashboard_api["Dashboard API<br/>[route.ts]"]
  node_care_workspace["Care workspace UI<br/>Dashboard"]
  node_caregiver_actions["Caregiver actions<br/>API"]
  node_staff_user(("Staff user<br/>External actor"))
end

subgraph group_state["State &amp; External Interfaces"]
  node_supabase_persistence["Supabase persistence<br/>Persistence support"]
  node_neon_memory_database[("Neon memory database<br/>[neonDatabase.ts]")]
  node_scheduled_invoker(("Internal scheduled invoker<br/>External actor"))
end

node_senior -->|"sends check-in"| node_telegram
node_senior -->|"sends check-in"| node_whatsapp
node_telegram -.->|"delivers events"| node_telegram_webhook
node_whatsapp -.->|"delivers events"| node_whatsapp_webhook
node_telegram_webhook -.->|"resolves context"| node_senior_context
node_whatsapp_webhook -.->|"resolves context"| node_senior_context
node_senior_context -->|"retrieves memory"| node_personal_memory
node_telegram_webhook -->|"runs workflow"| node_agent_orchestration
node_whatsapp_webhook -->|"runs workflow"| node_agent_orchestration
node_agent_orchestration -->|"saves outcomes"| node_orchestration_persistence
node_orchestration_persistence -->|"invokes detection"| node_pattern_watch
node_orchestration_persistence -->|"writes records"| node_supabase_persistence
node_senior_context -->|"reads context"| node_supabase_persistence
node_neon_memory_database -.->|"provides database"| node_personal_memory
node_dashboard_api -.->|"reads state"| node_supabase_persistence
node_dashboard_api -->|"supplies state"| node_care_workspace
node_staff_user -->|"reviews cases"| node_care_workspace
node_staff_user -->|"submits actions"| node_caregiver_actions
node_scheduled_invoker -->|"invokes processing"| node_checkin_processing

click node_telegram_webhook "https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/app/api/telegram/webhook/route.ts"
click node_whatsapp_webhook "https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/app/api/whatsapp/webhook/route.ts"
click node_checkin_processing "https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/lib/checkins/service.ts"
click node_senior_context "https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/lib/persistence/seniorContextRepository.ts"
click node_agent_orchestration "https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/lib/agents/orchestrator.ts"
click node_personal_memory "https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/lib/memory/applicationRepository.ts"
click node_pattern_watch "https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/lib/patterns/patternWatch.ts"
click node_orchestration_persistence "https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/lib/persistence/orchestrationRepository.ts"
click node_dashboard_api "https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/app/api/dashboard/state/route.ts"
click node_care_workspace "https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/components/Dashboard.tsx"
click node_caregiver_actions "https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/app/api/caregiver/queue-action/route.ts"
click node_supabase_persistence "https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/lib/persistence/persistenceSupport.ts"
click node_neon_memory_database "https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/lib/memory/neonDatabase.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_senior,node_telegram,node_whatsapp,node_telegram_webhook,node_whatsapp_webhook,node_checkin_processing toneBlue
class node_senior_context,node_agent_orchestration,node_personal_memory,node_pattern_watch,node_orchestration_persistence toneAmber
class node_dashboard_api,node_care_workspace,node_caregiver_actions,node_staff_user toneMint
class node_supabase_persistence,node_neon_memory_database,node_scheduled_invoker toneRose
```

## Components

### Messaging & Check-ins

- **Senior**: External actor
- **Telegram**: Messaging platform
- **WhatsApp**: Messaging platform · Live delivery depends on external business verification.
- [Telegram webhook](https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/app/api/telegram/webhook/route.ts): Webhook and service · `trustkaki-web/src/app/api/telegram/webhook/route.ts`
- [WhatsApp webhook](https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/app/api/whatsapp/webhook/route.ts): Webhook and service · `trustkaki-web/src/app/api/whatsapp/webhook/route.ts`
- [Check-in processing](https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/lib/checkins/service.ts): Service · `trustkaki-web/src/lib/checkins/service.ts`

### Understanding & Patterns

- [Senior context](https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/lib/persistence/seniorContextRepository.ts): Repository · `trustkaki-web/src/lib/persistence/seniorContextRepository.ts`
- [Agent orchestration](https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/lib/agents/orchestrator.ts): Orchestrator · `trustkaki-web/src/lib/agents/orchestrator.ts`
- [Personal memory](https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/lib/memory/applicationRepository.ts): Repository · `trustkaki-web/src/lib/memory/applicationRepository.ts`
- [Pattern Watch](https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/lib/patterns/patternWatch.ts): Pattern detection · `trustkaki-web/src/lib/patterns/patternWatch.ts`
- [Orchestration persistence](https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/lib/persistence/orchestrationRepository.ts): Repository · `trustkaki-web/src/lib/persistence/orchestrationRepository.ts`

### Care Workspace

- [Dashboard API](https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/app/api/dashboard/state/route.ts): API · `trustkaki-web/src/app/api/dashboard/state/route.ts`
- [Care workspace UI](https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/components/Dashboard.tsx): Dashboard · `trustkaki-web/src/components/Dashboard.tsx`
- [Caregiver actions](https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/app/api/caregiver/queue-action/route.ts): API · `trustkaki-web/src/app/api/caregiver/queue-action/route.ts`
- **Staff user**: External actor

### State & External Interfaces

- [Supabase persistence](https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/lib/persistence/persistenceSupport.ts): Persistence support · `trustkaki-web/src/lib/persistence/persistenceSupport.ts`
- [Neon memory database](https://github.com/minxie-ng/trustkaki/blob/main/trustkaki-web/src/lib/memory/neonDatabase.ts): Database · `trustkaki-web/src/lib/memory/neonDatabase.ts`
- **Internal scheduled invoker**: External actor · Must invoke internal processing endpoints to activate scheduled work.

## Connections

- Senior → Telegram: sends check-in (evidence: `README.md`)
- Senior → WhatsApp: sends check-in (evidence: `README.md`)
- Telegram → Telegram webhook: delivers events · Inferred; not shown in the code that was read. (evidence: `trustkaki-web/src/app/api/telegram/webhook/route.ts`)
- WhatsApp → WhatsApp webhook: delivers events · Inferred; not shown in the code that was read. (evidence: `trustkaki-web/src/app/api/whatsapp/webhook/route.ts`)
- Telegram webhook → Senior context: resolves context · Inferred; not shown in the code that was read. (evidence: `trustkaki-web/src/lib/persistence/seniorContextRepository.ts`)
- WhatsApp webhook → Senior context: resolves context · Inferred; not shown in the code that was read. (evidence: `trustkaki-web/src/lib/persistence/seniorContextRepository.ts`)
- Senior context → Personal memory: retrieves memory (evidence: `trustkaki-web/src/lib/persistence/seniorContextRepository.ts`)
- Telegram webhook → Agent orchestration: runs workflow (evidence: `README.md`)
- WhatsApp webhook → Agent orchestration: runs workflow (evidence: `README.md`)
- Agent orchestration → Orchestration persistence: saves outcomes (evidence: `README.md`)
- Orchestration persistence → Pattern Watch: invokes detection (evidence: `trustkaki-web/src/lib/persistence/orchestrationRepository.ts`)
- Orchestration persistence → Supabase persistence: writes records (evidence: `trustkaki-web/src/lib/persistence/orchestrationRepository.ts`)
- Senior context → Supabase persistence: reads context (evidence: `trustkaki-web/src/lib/persistence/seniorContextRepository.ts`)
- Neon memory database → Personal memory: provides database · Inferred; not shown in the code that was read.
- Dashboard API → Supabase persistence: reads state · Inferred; not shown in the code that was read. (evidence: `trustkaki-web/src/app/api/dashboard/state/route.ts`)
- Dashboard API → Care workspace UI: supplies state (evidence: `README.md`)
- Staff user → Care workspace UI: reviews cases (evidence: `README.md`)
- Staff user → Caregiver actions: submits actions (evidence: `trustkaki-web/src/app/api/caregiver/queue-action/route.ts`)
- Internal scheduled invoker → Check-in processing: invokes processing (evidence: `README.md`)
