Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

docs(web): add development guidelines README

Naiyuan Qing3548c91488ab
diff --git a/apps/web/README.md b/apps/web/README.md
index e215bc4ccf..df9cf726e6 100644
--- a/apps/web/README.md
+++ b/apps/web/README.md
@@ -1,36 +1,182 @@
-This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
+# @multica/web
-## Getting Started
+Next.js web client for Super Multica. This app is a **thin shell** — it contains only layout and page entry points. All business logic, state management, UI components, and network requests live in shared packages.
-First, run the development server:
+## Architecture
-```bash
-npm run dev
-# or
-yarn dev
-# or
-pnpm dev
-# or
-bun dev
+```
+apps/web/app/
+├── layout.tsx — Root layout, setConfig(), providers
+├── page.tsx — Page entry, renders <Chat />
+└── icon.png — Favicon
+```
+
+Everything else comes from packages:
+
+| Package | Responsibility | Examples |
+|---------|---------------|----------|
+| `@multica/store` | Global state (Zustand) | Hub, Messages, Gateway, DeviceId |
+| `@multica/ui` | Components & UI hooks | Chat, HubSidebar, Skeleton, useScrollFade |
+| `@multica/fetch` | HTTP client & URL config | `consoleApi`, `setConfig()` |
+| `@multica/sdk` | WebSocket client | `GatewayClient` |
+
+### Where does new code go?
+
+- **Page-scoped UI hook** (e.g. form toggle, scroll position) → `@multica/ui/hooks/`
+- **Cross-component state** (e.g. user preferences, notifications) → `@multica/store`
+- **Reusable component** → `@multica/ui/components/`
+- **HTTP request helper** → `@multica/fetch`
+- **This app** → Only if it's Next.js-specific (middleware, route handlers, `next.config`)
+
+> Principle: desktop also consumes these packages, so anything reusable must NOT live in `apps/web`.
+
+## Network Requests
+
+Two communication channels, two packages:
+
+```
+HTTP → @multica/fetch (consoleApi) → Console :4000 (Hub, Agent CRUD)
+WS → @multica/store (gateway) → Gateway :3000 (Chat messages)
+```
+
+Rules:
+
+1. **Never hardcode URLs.** Use `consoleApi` for HTTP, `useGatewayStore` for WS. Both read from `setConfig()` in `layout.tsx`.
+2. **HTTP for management, WS for real-time.** Creating/deleting agents is HTTP. Sending/receiving chat messages is WS.
+3. **Future: gateway may proxy HTTP.** The current two-endpoint setup may merge into one. Because all requests go through `@multica/fetch` and `@multica/store`, business code won't need changes.
+
+## State Management
+
+We use Zustand. Follow these rules:
+
+### Subscribe only to what you render
+
+```tsx
+// Good — component re-renders only when status changes
+const status = useHubStore((s) => s.status)
+
+// Bad — re-renders on ANY store change
+const { status } = useHubStore()
+```
+
+### Use getState() in callbacks
+
+Don't subscribe to state that's only used inside event handlers. Read it at call time instead.
+
+```tsx
+// Good — no subscription, no re-render
+const handleSend = useCallback((text: string) => {
+ const hub = useHubStore.getState().hub
+ const agentId = useHubStore.getState().activeAgentId
+ if (!hub?.hubId || !agentId) return
+ useMessagesStore.getState().addUserMessage(text, agentId)
+ useGatewayStore.getState().send(hub.hubId, "message", { agentId, content: text })
+}, [])
+
+// Bad — subscribes to hub and activeAgentId just to use them in onClick
+const hub = useHubStore((s) => s.hub)
+const activeAgentId = useHubStore((s) => s.activeAgentId)
+```
+
+### Subscribe to derived values, not raw objects
+
+```tsx
+// Good — re-renders only when the boolean flips
+const isConnected = useHubStore((s) => s.status === "connected")
+
+// Bad — re-renders when any field of hub changes
+const hub = useHubStore((s) => s.hub)
+const isConnected = hub !== null
+```
+
+### Filter/derive with useMemo, not inside selectors
+
+Selectors that return new references (`.filter()`, `.map()`) cause infinite re-renders. Derive outside the selector.
+
+```tsx
+// Good
+const messages = useMessagesStore((s) => s.messages)
+const filtered = useMemo(
+ () => messages.filter((m) => m.agentId === activeAgentId),
+ [messages, activeAgentId]
+)
+
+// Bad — .filter() returns a new array every time, triggers infinite loop
+const filtered = useMessagesStore((s) => s.messages.filter(...))
+```
+
+### Initialize once
+
+Side-effectful operations (WS connection, SDK init) must have guards to prevent double execution.
+
+```tsx
+// Inside store
+connect: (deviceId) => {
+ if (client) return // Already connected, skip
+ client = new GatewayClient(...)
+ client.connect()
+}
+```
+
+## Imports
+
+### Use direct paths for @multica/ui
+
+```tsx
+// Good
+import { Chat } from "@multica/ui/components/chat"
+import { Button } from "@multica/ui/components/ui/button"
+import { useScrollFade } from "@multica/ui/hooks/use-scroll-fade"
+
+// Bad — barrel import pulls in everything
+import { Chat, Button, useScrollFade } from "@multica/ui"
```
-Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
+`@multica/store` barrel import is fine — it has few exports and all are lightweight Zustand stores.
-You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
+### Heavy components: use dynamic import
-This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
+For large dependencies (code editors, chart libraries, PDF viewers), lazy-load to keep the initial bundle small.
-## Learn More
+```tsx
+import dynamic from "next/dynamic"
-To learn more about Next.js, take a look at the following resources:
+const CodeEditor = dynamic(
+ () => import("@multica/ui/components/code-editor"),
+ { ssr: false }
+)
+```
+
+## Conditional Rendering
-- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
-- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
+Use ternary expressions, not `&&`, to avoid rendering `0` or `""` as visible content.
-You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!
+```tsx
+// Good
+{status === "connected" ? <AgentList /> : null}
-## Deploy on Vercel
+// Bad — if agents is 0, renders "0" on screen
+{agents.length && <AgentList />}
+```
+
+## Development
+
+```bash
+# Start web dev server (port 3001)
+multica dev web
+
+# Or start all services
+multica dev
+
+# Typecheck
+cd apps/web && npx tsc --noEmit
+```
-The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
+## Adding a New Feature — Checklist
-Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
+1. Does it need global state? → Create a store in `@multica/store`
+2. Does it need HTTP calls? → Use `consoleApi` from `@multica/fetch`
+3. Does it need a UI component? → Add to `@multica/ui/components/`
+4. Does it need a UI hook? → Add to `@multica/ui/hooks/`
+5. Is it Next.js-specific? → Only then add to `apps/web`
+6. Is the component heavy (>50KB)? → Use `next/dynamic` with `{ ssr: false }`