# GN-Apex Copilot — Widget + Studio

| Part | Path | What it is |
|---|---|---|
| Widget runtime | `src/` | Builds to ONE file `dist/copilot.js` (the CDN script customers embed) |
| Schema + defaults | `src/schema.ts` | `WidgetSchema`, `DEFAULTS`, `resolve()` (validates/clamps everything) |
| **Landing page** | `index.html` + `assets/` | Commercial site at `/`: hero, how-it-works (the account → project → turn-on walk), Studio tour, presentation modes, GORA AI models, compare (vs hosted suites / no-code builders / DIY), install snippets, plans &amp; usage, FAQ |
| **Lottie art** | `assets/lottie/` + `assets/vendor/lottie_light.min.js` | 10 decorative animations extracted from dotLottie zips into plain JSON (embedded art re-encoded to WebP/palette-PNG). Vendored `lottie-web` light player, lazy-loaded on idle and played only in view; skipped entirely under `prefers-reduced-motion` |
| **Studio** | `studio/` | Client-facing designer: templates, live preview, publish — served at `/studio/` |
| Studio field list | `studio/fields.ts` | Declares every control. Add a field here and it appears in the Studio |

## Run
    npm i
    npm run dev            # /          → landing page
                           # /studio/   → Studio (offline demo)
    npm run build          # dist/copilot.js -> copy to backend public/cdn/copilot.js
    npm run build          # dist/       -> site + Studio + SEO files + copilot.js (deploy this folder)
    npm run site:build     # dist/       -> site only (no copilot.js)
    npm run site:preview   # serve dist/ locally

Connect the Studio to a real project:

    http://localhost:5173/studio/?api=http://localhost:4000&project=<PROJECT_ID>&token=<JWT>&site=https://client-site.com&name=Acme

(Params are kept in sessionStorage and removed from the URL. The landing page forwards legacy
`/?api=…&project=…` links to `/studio/` automatically.)

## Landing page

`index.html` is the public marketing page for the widget, and `/studio/` is the designer.

- **SEO:** one `<h1>`, clean heading order, canonical, Open Graph + Twitter cards, a single JSON-LD
  `@graph` (Organization, WebSite, SoftwareApplication + offers, VideoObject, HowTo, FAQPage),
  `robots.txt`, `sitemap.xml`, `site.webmanifest` and `llms.txt`. Lighthouse: 100 / 100 / 100
  (SEO / accessibility / best practices).
- **Design system:** all styling lives in `assets/landing.css` (tokens at the top: `--acid`, `--cyan`,
  `--violet`, `--paper`). Sections alternating `.section--alt` flip to the light "paper" palette.
- **Motion:** `assets/lottie/*.json` are played by a vendored `assets/vendor/lottie_light.min.js`, loaded
  once the page is idle (or on first scroll) and never on the critical path. Each `[data-lottie]` well
  fetches its JSON only when it mounts, plays only while on screen, and is skipped when the visitor
  prefers reduced motion. The player is a build-order asset — see `RAW_ASSETS` in `vite.site.config.ts`.
- **Interactions:** `assets/landing.js` (no dependencies) — scroll reveals with failsafes, the animated
  hero conversation, presentation-mode switcher, install tabs, copy buttons, video modal, stat counters.
- **Deploy:** `npm run build` writes `dist/` with `index.html`, `studio/index.html`, hashed
  assets and the root SEO files. Serve that folder; keep `studio/` reachable for the designer.
- **Serve it compressed and immutable.** Every asset name is content-hashed (`landing-D4Ql_LP8.js`),
  so the host should send `Cache-Control: public, max-age=31536000, immutable` for `assets/*` and gzip
  (or brotli) for `html/js/css/json/svg`. Without encoding enabled, Lighthouse scores
  *Serve static assets with an efficient cache policy* and *Enable text compression* at 0 even though
  the page is already lean — `index.html` is ~20 KB gzip, the stylesheet ~11 KB, the runtime ~4 KB, and
  the lazy player ~46 KB gzip. A one-line `gzip_static on;` (nginx) or the equivalent in your CDN
  config is the whole fix.

### Replace the dummy content before launch
| What | Where |
|---|---|
| OG share image (1200×630) | `assets/img/og-cover.svg` |
| Walkthrough video + poster | `assets/video/copilot-studio.mp4` (placeholder clip), `assets/img/video-poster.svg` |
| Testimonial avatars & quotes | `assets/img/avatar-1..3.svg`, the three `<figure class="quote">` blocks in `index.html` |
| Pricing tiers | removed — the copilot is included with every GN-Apex project; `#plans` explains usage per plan |
| Model specs &amp; usage table | `#models` / `#plans` sections — keep in sync with `GORA_AI_MODELS` and `PRICING_MATRIX` (`backend/src/ai-agent/ai-agent.constants.ts`, `backend/src/billing/constants/pricing-matrix.constants.ts`) |
| Get-started links | currently `https://gnapex.com/signup` (nav, hero, plans, CTA, footer) |
| Contact / sales / socials | footer + `mailto:` links in `index.html`, `robots.txt`, `sitemap.xml` (domain `ai-widget.gnapex.com`) |
| Newsletter form | `#subscribe` — currently front-end validation only; post to your provider in `assets/landing.js` |

## Embed the Studio in your Next.js dashboard (the real product flow)
Copy or link this package into the dashboard, then:

```tsx
'use client';
import { useEffect, useRef } from 'react';
import { mountStudio } from 'gnapex-copilot-widget/studio/studio';

export default function CopilotStudioPage({ project, accessToken }: any) {
  const ref = useRef<HTMLDivElement>(null);
  useEffect(() => mountStudio(ref.current!, {
    apiUrl: process.env.NEXT_PUBLIC_API_URL, projectId: project.id, projectName: project.name,
    siteUrl: project.websiteUrl, getToken: () => accessToken,
  }), [project.id]);
  return <div ref={ref} style={{ height: 'calc(100vh - 64px)' }} />;
}
```
Omit `getToken` if your API uses cookie auth (the Studio then sends `credentials: 'include'`).

## Flow
Client edits in Studio → **Publish** → `PATCH /ai-agent/project/:id/config { ui }` →
stored in `aiConfig.ui` → public `GET /copilot/:id/config` returns it → `copilot.js` on the client's site applies it.

## Changing the default design for ALL new projects
`DEFAULTS` in `src/schema.ts` (values) and `src/styles.ts` (look of components). Rebuild, copy `dist/copilot.js`.
Clients who saved their own `ui` keep it; anything they never touched follows your new defaults only if you stop
saving the full schema — see "Partial overrides" in BACKEND.md.

## Release the CDN file to the backend
    BACKEND_CDN_DIR=../backend/public/cdn npm run release     # build + copy copilot.js
Make sure `public/cdn/copilot.js` is included in the backend's Docker image / deploy artifact.

## Dashboard
See `dashboard/INTEGRATION.md` (3 files + a small edit in Aitab.tsx).


## What’s new in v3

See **CHANGELOG.md** for the full list. Headlines: a rewritten width model (no more random wrapping, badges match their bubble), a composer with the send button inside the box, image/file uploads for Gemini/Gemma, an Auto theme that follows the host website (plus separate light/dark colours), 9 soft sounds, working auto-open, configurable auto-toasts, a clear-conversation button, a full markdown + LaTeX renderer, and a Studio with area-aware right-click menus and 22 shortcuts (press `?`).

## What’s new in this Studio (v2)

### Presentation modes (schema-driven)
Clients pick how the assistant opens — no code required:

| Mode | Behaviour |
|------|-----------|
| **Panel** | Classic floating chat near the launcher |
| **Drawer** | Slides from left / right / bottom |
| **Sheet** | Mobile-style bottom sheet |
| **Dialog** | Centered modal with backdrop |
| **Popover** | Compact popover anchored to the button |
| **Fullscreen** | Covers the entire viewport |

**Mobile behaviour** can be set independently (`auto` smart-picks sheet/fullscreen from the desktop mode).

### Richer design controls
- Brand + accent colours, density, glass/frosted surfaces, elevation
- Launcher: shape (rounded / circle / pill / square), 6 icons + custom URL, optional label, pulse attention ring, X/Y insets
- Header styles: gradient · solid · minimal · glass
- Message bubbles: soft / rounded / bubble / sharp; alignment; typing indicator
- Backdrop dim + blur, close-on-outside, Escape, open animations (fade / slide / scale / spring)
- 8 starter templates covering every presentation mode

### Still 100% schema-driven
Add a field in `studio/fields.ts` + a key in `src/schema.ts` and it appears in the Studio automatically. The widget runtime only reads the resolved schema — no one-off UI branches.

### Run
```bash
npm i
npm run dev   # landing at http://localhost:5173/ , Studio at http://localhost:5173/studio/
```

## Studio v2 (theme-aware)
- **Dark/light:** follows the host app's `dark` class on `<html>` (Tailwind `darkMode: "class"`), so it switches with your app theme. The toolbar button cycles Auto / Light / Dark; pass `theme` to `mountStudio` to force one. Dark is pure black (`#000`) with neutral greys.
- **Schema-driven:** every control, group, search hit, ⌘K entry and "customised" badge is generated from `studio/fields.ts`. Use `when: (s) => …` on a field to show it only when relevant.
- **Tools:** ⌘K palette, `/` setting search, per-field reset, WCAG contrast checker, template thumbnails, export/import JSON, embed-code copy, undo/redo, ⌘S publish.
# copilot-widget


## Production checklist
1. `npm ci && npm run build` → deploy the contents of `dist/` to https://ai-widget.gnapex.com/ (HTTPS, redirect http→https). Cache headers: see `_headers` or `deploy/nginx.conf`.
2. Point the newsletter form at your API: `<form id="subscribe" data-endpoint="https://api.gnapex.com/…">` (POST `{ "email": "…" }`). Without it the form opens the visitor's mail app.
3. Replace `assets/video/copilot-studio.mp4` with the real tour, then re-add a `VideoObject` to the JSON-LD in `index.html`.
4. Replace the illustrative testimonials in section 08 with real customer quotes when you have them.
5. Submit `https://ai-widget.gnapex.com/sitemap.xml` in Google Search Console and Bing Webmaster Tools.
6. Have the privacy and terms pages reviewed by counsel before launch.
