{"slug":"developers/PHASE_5_BOOTSTRAP_IMPLEMENTATION","title":"Phase 5: Platform Bootstrap Implementation","section":"developers","sectionLabel":"Developers","status":"unclassified","type":"document","path":"developers/PHASE_5_BOOTSTRAP_IMPLEMENTATION.mdx","bytes":4531,"description":null,"updated":null,"source":"integratewise/Integratewise-docs-wiki","content":"\n\n> **Tier C — Historical.** This document is a point-in-time snapshot preserved for reference. For current architecture, see [CANONICAL_PLATFORM_ARCHITECTURE.md](../architecture/CANONICAL_PLATFORM_ARCHITECTURE.md).\n\nExecutable architecture validating the frozen design from Phases 0-4.\n\n## What Was Built\n\n### 1. Bootstrap Package (`packages/bootstrap/`)\n\n**Core Components:**\n- `PlatformProvider` — Orchestrates the entire initialization flow\n- `PlatformContext` — Exposes `usePlatform()` and `useWorkspace()` hooks\n- `types.ts` — Complete TypeScript interfaces for bootstrap flow\n\n**Bootstrap Flow:**\n1. Authenticating (Stack Auth confirms identity)\n2. Resolving Organization (fetch org from gateway)\n3. Resolving Role & Permissions (determine access level)\n4. Loading Connected Apps (ChatGPT, HubSpot, Slack, etc.)\n5. Hydrating Entity360 (load aggregated workspace data)\n6. Ready (workspace available to app)\n\n**Usage:**\n```tsx\n<PlatformProvider>\n  <App />\n</PlatformProvider>\n\n// Inside app:\nconst { workspace, bootstrap, isReady } = usePlatform()\nconst { user, organization, role, permissions } = useWorkspace()\n```\n\n### 2. Gateway SDK (`packages/gateway-sdk/`)\n\n**Five Essential Methods:**\n1. `getUser()` — Fetch authenticated user\n2. `getOrganization(userId)` — Get org context\n3. `getUserRole(userId, orgId)` — Get role + permissions\n4. `getConnectedApps(orgId)` — List connected integrations\n5. `hydrateEntity360(userId, orgId)` — Trigger workspace data sync\n\n**Bonus Methods:**\n- `searchMemory(userId, query)` — Search conversational memory\n- `addMemory(userId, type, content)` — Store memory entries\n\n**Design:**\n- No direct database access\n- All calls go through `/api/gateway/*` endpoints\n- Clean type-safe interface\n- Ready to scale to other methods as needed\n\n### 3. Gateway API Handlers (`app/api/gateway/*`)\n\n**Five Implemented Endpoints:**\n- `GET /api/gateway/user` — Returns authenticated user\n- `GET /api/gateway/organization?userId=X` — Returns org\n- `GET /api/gateway/user-role?userId=X&orgId=Y` — Returns role + permissions\n- `GET /api/gateway/connected-apps?orgId=Y` — Returns connected apps\n- `POST /api/gateway/entity360` — Triggers Entity360 hydration\n\n**Current Status:**\n- All return mock data (realistic for MVP)\n- All validate auth via `requireAuth()`\n- Ready to wire to real database/Spine\n\n## Architecture Validation\n\nThe flow validates the frozen architecture:\n\n```\nUser (Stack Auth)\n    ↓\nPlatformProvider (bootstrap)\n    ↓\n[Gateway SDK calls]\n    ↓\n[API Handlers]\n    ↓\n[Mock Data → Real Spine later]\n    ↓\nWorkspace Ready\n```\n\nEvery frontend (AI Workspace, Wise Docs, etc.) will follow this exact pattern.\n\n## Files Created\n\n**Packages:**\n- `packages/bootstrap/types.ts` (46 lines)\n- `packages/bootstrap/platform-context.tsx` (35 lines)\n- `packages/bootstrap/platform-provider.tsx` (121 lines)\n- `packages/bootstrap/index.ts` (4 lines)\n- `packages/bootstrap/package.json` (20 lines)\n- `packages/gateway-sdk/types.ts` (58 lines)\n- `packages/gateway-sdk/client.ts` (87 lines)\n- `packages/gateway-sdk/index.ts` (19 lines)\n- `packages/gateway-sdk/package.json` (12 lines)\n\n**API Handlers:**\n- `app/api/gateway/user/route.ts` (24 lines)\n- `app/api/gateway/organization/route.ts` (26 lines)\n- `app/api/gateway/user-role/route.ts` (33 lines)\n- `app/api/gateway/connected-apps/route.ts` (38 lines)\n- `app/api/gateway/entity360/route.ts` (32 lines)\n\n**Total: 466 lines of executable, type-safe architecture.**\n\n## Next Steps\n\n### Phase 6: Migrate AI Workspace (In Progress)\n1. Update `app/layout.tsx` to wrap with `<PlatformProvider>`\n2. Update `/onboarding` wizard to use `usePlatform()`\n3. Update `/workspace` and `/home` to consume `useWorkspace()`\n4. Verify bootstrap flow in browser\n\n### Phase 7: Shell Apps\nOnce validated:\n1. Create `apps/wise-docs` using same bootstrap\n2. Create `apps/wise-ops` using same bootstrap\n3. Create `apps/wise-erms` using same bootstrap\n4. All 3 apps share identical bootstrap flow\n\n### Phase 8+: Feature Development\nWith proven bootstrap:\n- Expand Gateway SDK as needed\n- Wire real Spine database\n- Add more endpoints incrementally\n- Scale to 12+ applications\n\n## Key Principle\n\n**No frontend ever calls the database directly.**\n**All access goes through Gateway SDK → API Handlers → Platform Services.**\n\nThis ensures:\n- Consistent authentication\n- Unified permissions model\n- Shared data integrity\n- Easy to add new apps\n- Easy to change backend without frontend changes\n"}