@sbuilder/cli 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,29 +1,58 @@
1
1
  # @sbuilder/cli
2
2
 
3
- **`sb`** — scaffold, run and deploy an app for the **Store Builder** platform.
3
+ **`sb`** — the **Store Builder** command line: keep a site's pages in a GitHub repo, run the
4
+ editor's AI assistant on your own machine, and build apps for the platform.
4
5
 
5
6
  > 🇻🇳 Bản tiếng Việt: [**README.vi.md**](./README.vi.md)
6
7
 
7
8
  ```
8
- sb init [name] [--port 3000] scaffold an app that runs, registers, and opens
9
- inside a merchant's admin
10
- sb login sign in through the browser; stores a credential
11
- that renews itself
12
- sb logout sign this machine out and forget that credential
13
- sb dev upload an island's code to your DRAFT version
14
- every time you save it. Reads sb.json; needs
15
- sb login (or SB_TOKEN).
16
- sb deploy [--submit] push the version's payload in dependency order.
17
- --submit then sends it for review.
18
- sb new "<what to build>" make a site from one sentence: opens the editor
19
- and the assistant starts building
20
- sb agent [--codex] run the editor's AI assistant on THIS machine's
21
- Claude Code login — no API key (--codex: Codex too)
22
- sb code pull [--site <id>] [--dir <path>] [--force]
23
- mirror a site's Code Dock files into ./code
24
- sb code push [--site <id>] [--dir <path>] [--force] [--dry-run] [--delete]
25
- send local edits back; refuses a file changed
26
- in the Code Dock since your pull unless --force
9
+ Start
10
+ sb init ask what to start: a new website built by AI, a
11
+ repo for one of your sites, or an app (same as
12
+ npm create sbuilder-app)
13
+
14
+ Sites
15
+ sb site init [dir] [--site <id>] [--github]
16
+ a repo holding one site's pages, synced with
17
+ GitHub both ways
18
+ sb site pull | push [--force] [--dry-run]
19
+ editor edits into design/pages, or your edits
20
+ back as DRAFTS
21
+ sb site publish <file>… | --all
22
+ make pages LIVE — named files, or every page
23
+ sb site blocks the blocks your installed apps offer, with the line
24
+ to paste into a page file
25
+ sb site next serve the pages from a Next.js app, rendered by the
26
+ platform's own renderer
27
+ sb mcp the Store Builder MCP server, with this repo's key
28
+ sb code pull [--site <id>] [--dir <path>] [--force]
29
+ mirror a site's Code Dock files into ./code
30
+ sb code push [--site <id>] [--dir <path>] [--force] [--dry-run] [--delete]
31
+ send local edits back; refuses a file changed
32
+ in the Code Dock since your pull unless --force
33
+ sb new "<what to build>" make a site from one sentence: opens the editor
34
+ and the assistant starts building
35
+ sb export next <site-url> [--dir <path>]
36
+ a published site as a Next.js repo
37
+
38
+ AI assistant
39
+ sb agent [--codex] run the editor's AI assistant on THIS machine's
40
+ Claude Code login — no API key (--codex: Codex too)
41
+
42
+ Apps (for developers building on the platform)
43
+ sb init [name] [--port 3000] scaffold an app that runs, registers, and opens
44
+ inside a merchant's admin
45
+ sb dev upload an island's code to your DRAFT version
46
+ every time you save it. Reads sb.json; needs
47
+ sb login (or SB_TOKEN).
48
+ sb deploy [--submit] push the version's payload in dependency order.
49
+ --submit then sends it for review.
50
+
51
+ Account
52
+ sb login sign in through the browser; stores a credential
53
+ that renews itself
54
+ sb whoami which account (and platform) the CLI acts as
55
+ sb logout sign this machine out and forget that credential
27
56
  ```
28
57
 
29
58
  Node 20 or newer. Written in TypeScript, published compiled — an installed copy
@@ -43,6 +72,8 @@ has no dependencies and nothing to build.
43
72
  - [`sb deploy`](#sb-deploy--the-whole-payload-in-dependency-order)
44
73
  - [`sb code`](#sb-code--your-code-dock-files-as-a-folder)
45
74
  - [`sb new`](#sb-new--a-site-from-one-sentence)
75
+ - [`sb site`](#sb-site--a-site-as-a-repo-you-build-with-ai-synced-with-github)
76
+ - [`sb export next`](#sb-export-next--a-published-site-as-a-nextjs-repo)
46
77
  - [`sb agent`](#sb-agent--the-editor-assistant-on-your-own-login)
47
78
  - [Environment variables](#environment-variables)
48
79
  - [CI](#ci)
@@ -272,6 +303,7 @@ A fuller one:
272
303
 
273
304
  ```bash
274
305
  sb login
306
+ sb whoami
275
307
  sb logout
276
308
  ```
277
309
 
@@ -307,6 +339,15 @@ refused, the entry is deleted and the CLI says to run `sb login` again.
307
339
  `sb logout` revokes this device's session on the server and deletes its entry
308
340
  from the file. Your other devices stay signed in.
309
341
 
342
+ **On a terminal it opens the page for you** — the `/device` page only, never the
343
+ code, which you still type. It stays closed in CI, when the output is not a
344
+ terminal, or with `SB_NO_BROWSER=1`; the address is printed either way. Once
345
+ approved, it says which account it signed in as.
346
+
347
+ `sb whoami` prints that account and the platform address (`me@shop.vn on
348
+ https://api.sbuilder.io.vn`), or that `SB_TOKEN` holds an API key or a deploy
349
+ key, or `Not signed in` (exit 1).
350
+
310
351
  `SB_TOKEN`, when set, **takes precedence** over the stored credential — see
311
352
  [Environment variables](#environment-variables).
312
353
 
@@ -593,6 +634,128 @@ assistant on that site stops and says so, rather than falling back to the
593
634
  platform's key. Keep it running (`sb agent install`) or change the provider in the
594
635
  assistant's settings.
595
636
 
637
+ ## `sb site` — a site as a repo you build with AI, synced with GitHub
638
+
639
+ ```
640
+ npm create sbuilder-app # or: sb init — "a new website, built by AI" is choice 1
641
+ sb site init my-shop --github # a repo for one of your sites, on a private GitHub repo
642
+ sb site pull # take what was edited in the editor
643
+ sb site push # save edited page files as DRAFTS
644
+ sb site publish --all # make pages live (or name files)
645
+ sb site next # serve the pages from a Next.js app (npm run dev)
646
+ sb mcp # the Store Builder MCP server, with this repo's key
647
+ ```
648
+
649
+ **Building with AI.** Choice 1 of `sb init` asks you to describe the website, creates
650
+ the site, writes the repo below and — when Claude Code is installed — starts it in
651
+ that folder with your description, so building starts right there. Any agent works:
652
+ the repo carries `.mcp.json`, `.cursor/mcp.json` and `.vscode/mcp.json`, so Claude
653
+ Code, Cursor and VS Code see the Store Builder tools as soon as the folder is opened
654
+ (approve the `sbuilder` server once), and `AGENTS.md` (imported by `CLAUDE.md`) tells
655
+ the agent how to work: native elements first, look at every section on desktop and
656
+ mobile, ask for real facts, pull the result into the repo, **never publish unasked**.
657
+ The agent edits the site's drafts; you review what it did as a git diff.
658
+
659
+ **What `sb site init` writes.** It signs you in if needed, lets you pick a site (or
660
+ takes `--site <id>`), and writes:
661
+
662
+ | Path | What it is |
663
+ | --- | --- |
664
+ | `design/pages/*.page.tsx` | One page's draft as source (see *Page files* below) |
665
+ | `code/` | The site's Code Dock files (`sb code pull --dir code`) |
666
+ | `.sb-site.json` | What was last synced — commit it |
667
+ | `.env` | An API key minted for this repo, mode `0600`, git-ignored |
668
+ | `.mcp.json`, `.cursor/mcp.json`, `.vscode/mcp.json` | The MCP server, started as `sb mcp` |
669
+ | `AGENTS.md`, `CLAUDE.md` | How an AI agent works in this repo |
670
+ | `.github/workflows/sbuilder.yml` | The GitHub half of the sync |
671
+
672
+ The key is named `AI agent · <folder>` and carries every content permission your
673
+ account may delegate — never members, roles, site settings or export. Revoke it in
674
+ Settings -> API keys. If it cannot be minted, init says so and carries on. Inside the
675
+ repo, `sb site`, `sb code` and `sb mcp` read `.env`; a variable already set in your
676
+ shell wins. `--github` then runs `git init`, the first commit, `gh repo create
677
+ --private --push` and `gh secret set SB_TOKEN` (the key goes in on stdin, never on a
678
+ command line) — the workflow is live with nothing left to paste.
679
+
680
+ **Page files.** Each page is a `.page.tsx` file that reads like React — one tag per element,
681
+ its settings as props:
682
+
683
+ ```tsx
684
+ export default (
685
+ <Page id="ROOT">
686
+ <FlexSection id="fs_a5608576" style={{ padding: "40px 24px" }} mobile={{ style: { padding: "24px 16px" } }}>
687
+ <Heading id="he_91c2" level={1} text="Trà Thái Nguyên" />
688
+ <Button text="Mua ngay" />
689
+ </FlexSection>
690
+ </Page>
691
+ );
692
+ ```
693
+
694
+ Edit text and settings, move or add elements (a new one needs no `id` — it gets one on push),
695
+ then `sb site push`. Keep the existing `id`s: they are how an edit made in the editor and one made
696
+ here are told apart. A page file holds **data, not code** — no logic, no variables, no text
697
+ between tags (`text="…"` instead); anything else is refused with `file:line:col` and nothing is
698
+ sent. The format is exact both ways: every page the platform has stored prints to source and
699
+ reads back identical, so pulling and pushing cannot damage a page. Elements kept outside the
700
+ page tree (a list's empty state, quantity buttons) are under `export const detached`.
701
+
702
+ **Installed apps.** An app's block arrives in the page file as its full subtree, its root
703
+ carrying `appBlockId`. It belongs to the app: the platform discards edits inside it, so
704
+ `sb site push` refuses them before sending, naming the block — change only its
705
+ `appBlockValues`, move it, or remove it whole. `sb site blocks` lists the blocks your
706
+ installed apps offer, each with the line to paste (`<FlexBlock appBlockRef="ins_…/badge" />`)
707
+ and the settings it takes. Their app data and island scripts render on the Next.js host
708
+ too. Every warning the platform returns on a save is printed, never swallowed.
709
+
710
+ **Run it as a Next.js app.** `sb site next` adds a Next.js host to the repo: `npm install`,
711
+ then `npm run dev`, and `http://localhost:3000` serves every page file **rendered by the
712
+ platform's own renderer** — the Go code compiled to WebAssembly (`@sbuilder/render`), so a page
713
+ looks here byte for byte as it does on the store, never "close". Save a `.page.tsx`, refresh,
714
+ see it — before anything is pushed. The data a page shows (products, articles, theme, settings)
715
+ comes from `POST /api/v1/pages/{id}/render-context` with the repo's key; `/_wb/*` (cart,
716
+ checkout, forms, the islands runtime) and any path with no page file go to the live store
717
+ (`SB_STOREFRONT` in `.env`). A route you add under `app/` wins over the page catch-all, so
718
+ hand-written React pages live beside the builder's. Deploy it anywhere Next.js runs.
719
+
720
+ **The workflow is the whole GitHub integration** — no GitHub App, no webhook. A push to
721
+ `main` touching `design/` or `code/` saves drafts; every 30 minutes (and on demand) it
722
+ pulls what changed in the editor and commits it. Running it by hand with *publish*
723
+ ticked makes every page live.
724
+
725
+ **Nothing is overwritten silently.** `.sb-site.json` records each page's server `rev`
726
+ and the file's hash as last synced. A pull overwrites a file only if you have not
727
+ edited it; a push sends `baseRev` and the server refuses a page edited in the editor
728
+ since. A page edited on both sides is named and skipped (exit 1); `--force` picks a
729
+ side — the editor's on pull, yours on push.
730
+
731
+ **Push saves drafts; `sb site publish` is the only thing here that goes live**, and it
732
+ publishes only the files you name or, with `--all`, every page. Pages are not created
733
+ or deleted from files: create one in the editor (or let the agent), then pull; a page
734
+ deleted on the platform keeps its file and is named. With an API key it uses
735
+ `/api/v1/pages/{id}/document` and needs no `--site`.
736
+
737
+ ## `sb export next` — a published site as a Next.js repo
738
+
739
+ ```
740
+ sb export next https://your-shop.example --dir my-shop
741
+ ```
742
+
743
+ Reads the site's `sitemap.xml`, fetches every page it lists and writes a Next.js
744
+ repo (into `./<host>` without `--dir`; the folder must be empty or new). Each
745
+ page is kept exactly as the site published it, in `site/`, and served by a static
746
+ route in `app/`. Nothing is re-rendered.
747
+
748
+ Everything the export does not hold — cart, checkout, forms, images, the
749
+ published-page runtime under `/_wb/`, and any page missing from the sitemap — is
750
+ forwarded to the live site by a fallback rewrite in `next.config.mjs`
751
+ (`SITE_ORIGIN` overrides the address). The browser sees one origin, so shopper
752
+ sign-in keeps working on the new host.
753
+
754
+ It is a snapshot: prices and stock shown in the HTML are as of the export, so
755
+ run it again after you publish. A page the site does not answer with HTML, or
756
+ whose path starts with a character Next.js reserves for folders (`_`, `(`, `[`,
757
+ `@`), is skipped and named in the output.
758
+
596
759
  ## `sb agent` — the editor assistant on your own login
597
760
 
598
761
  ```
@@ -609,7 +772,7 @@ Ctrl-C stops it, and it reconnects on its own if the network drops.
609
772
  - macOS and Linux. Windows is not supported yet (npm's `.cmd` shims cannot be
610
773
  started without a shell, and a shell would re-parse the prompt).
611
774
  - Install and log in to the CLI first (`claude`, or `codex login`).
612
- - The platform admin must switch the option on (Admin → Settings); until then
775
+ - The platform admin must switch the option on (Admin → AI assistant); until then
613
776
  `sb agent` stops and says so.
614
777
  - Claude Code runs with every built-in tool off. **Codex jobs are refused unless
615
778
  you start `sb agent --codex`**: Codex can read files on this machine through
@@ -673,12 +836,14 @@ refused.
673
836
 
674
837
  ## Environment variables
675
838
 
676
- Two are read by the CLI itself:
839
+ Three are read by the CLI itself:
677
840
 
678
841
  | Variable | Required by | Default |
679
842
  | --- | --- | --- |
680
843
  | `SB_TOKEN` | `sb dev`, `sb deploy`, `sb code` — optional | none — the `sb login` credential is used; with neither, each stops and names `sb login` |
681
844
  | `SB_API` | every command but `sb init` | `https://api.sbuilder.io.vn` — set `http://localhost:8080` for a local platform |
845
+ | `SB_NO_BROWSER` | `sb login` | unset — set it to stop `sb login` opening the `/device` page (CI and non-terminals never open it) |
846
+ | `SB_STOREFRONT` | the `sb site next` app | the live store's address (`sb site init` writes it into `.env`) — where `/_wb/*` and paths with no page file are forwarded |
682
847
 
683
848
  `SB_TOKEN` **overrides** the stored `sb login` credential when set. For `sb dev`
684
849
  and `sb deploy` it is a token for the **organization that owns the app** — the
package/README.vi.md CHANGED
@@ -1,27 +1,56 @@
1
1
  # @sbuilder/cli
2
2
 
3
- **`sb`** — tạo khung, chạy và triển khai một app cho nền tảng **Store Builder**.
3
+ **`sb`** — dòng lệnh của **Store Builder**: giữ các trang của site trong một repo GitHub, chạy
4
+ trợ lý AI của editor trên máy bạn, và xây app cho nền tảng.
4
5
 
5
6
  > 🇬🇧 English version: [**README.md**](./README.md)
6
7
 
7
8
  ```
8
- sb init [tên] [--port 3000] tạo khung một app chạy được, đăng ký được, và mở
9
- ngay trong trang quản trị của merchant
10
- sb login đăng nhập qua trình duyệt; lưu một credential tự
11
- gia hạn
12
- sb logout đăng xuất máy này và xoá credential đó
13
- sb dev tải code của một island lên bản DRAFT mỗi lần bạn
14
- lưu file. Đọc sb.json; cần sb login (hoặc SB_TOKEN).
15
- sb deploy [--submit] đẩy toàn bộ payload của version theo đúng thứ tự
16
- phụ thuộc. --submit thì gửi luôn đi duyệt.
17
- sb new "<muốn dựng gì>" tạo site từ một câu: mở editor và trợ lý bắt đầu dựng
18
- sb agent [--codex] chạy trợ lý AI của editor bằng Claude Code đã đăng
19
- nhập trên MÁY NÀY — không cần API key (--codex: cả Codex)
20
- sb code pull [--site <id>] [--dir <path>] [--force]
21
- kéo các file Code Dock của một site về ./code
22
- sb code push [--site <id>] [--dir <path>] [--force] [--dry-run] [--delete]
23
- gửi các chỗ sửa ở máy ngược lại; từ chối file đã
24
- đổi trong Code Dock kể từ lần pull, trừ khi --force
9
+ Bắt đầu
10
+ sb init hỏi bạn muốn bắt đầu gì: website mới do AI dựng,
11
+ repo cho một site của bạn, hay app (giống npm
12
+ create sbuilder-app)
13
+
14
+ Site
15
+ sb site init [dir] [--site <id>] [--github]
16
+ repo chứa các trang của một site, đồng bộ hai
17
+ chiều với GitHub
18
+ sb site pull | push [--force] [--dry-run]
19
+ lấy chỗ sửa trong editor về design/pages, hoặc gửi
20
+ chỗ sửa của bạn lên thành BẢN NHÁP
21
+ sb site publish <file>… | --all
22
+ đưa trang lên LIVE — file chỉ định, hoặc mọi trang
23
+ sb site blocks các block app đã cài cho phép đặt, kèm dòng để dán
24
+ vào file trang
25
+ sb site next phục vụ các trang từ một app Next.js, render bằng
26
+ chính renderer của nền tảng
27
+ sb mcp MCP server của Store Builder, với key của repo này
28
+ sb code pull [--site <id>] [--dir <path>] [--force]
29
+ kéo các file Code Dock của một site về ./code
30
+ sb code push [--site <id>] [--dir <path>] [--force] [--dry-run] [--delete]
31
+ gửi các chỗ sửa ở máy ngược lại; từ chối file đã
32
+ đổi trong Code Dock kể từ lần pull, trừ khi --force
33
+ sb new "<muốn dựng gì>" tạo site từ một câu: mở editor và trợ lý bắt đầu dựng
34
+ sb export next <site-url> [--dir <path>]
35
+ site đã xuất bản thành một repo Next.js
36
+
37
+ Trợ lý AI
38
+ sb agent [--codex] chạy trợ lý AI của editor bằng Claude Code đã đăng
39
+ nhập trên MÁY NÀY — không cần API key (--codex: cả Codex)
40
+
41
+ App (cho developer xây app trên nền tảng)
42
+ sb init [tên] [--port 3000] tạo khung một app chạy được, đăng ký được, và mở
43
+ ngay trong trang quản trị của merchant
44
+ sb dev tải code của một island lên bản DRAFT mỗi lần bạn
45
+ lưu file. Đọc sb.json; cần sb login (hoặc SB_TOKEN).
46
+ sb deploy [--submit] đẩy toàn bộ payload của version theo đúng thứ tự
47
+ phụ thuộc. --submit thì gửi luôn đi duyệt.
48
+
49
+ Tài khoản
50
+ sb login đăng nhập qua trình duyệt; lưu một credential tự
51
+ gia hạn
52
+ sb whoami CLI đang dùng tài khoản (và platform) nào
53
+ sb logout đăng xuất máy này và xoá credential đó
25
54
  ```
26
55
 
27
56
  Cần Node 20 trở lên. Viết bằng TypeScript, **publish ở dạng đã biên dịch** — bản
@@ -41,6 +70,8 @@ cài về không có dependency nào và không phải build gì cả.
41
70
  - [`sb deploy`](#sb-deploy--toàn-bộ-payload-theo-thứ-tự-phụ-thuộc)
42
71
  - [`sb code`](#sb-code--file-code-dock-của-bạn-dưới-dạng-một-thư-mục)
43
72
  - [`sb new`](#sb-new--tạo-site-từ-một-câu)
73
+ - [`sb site`](#sb-site--site-thành-một-repo-dựng-bằng-ai-đồng-bộ-với-github)
74
+ - [`sb export next`](#sb-export-next--site-đã-xuất-bản-thành-một-repo-nextjs)
44
75
  - [`sb agent`](#sb-agent--trợ-lý-của-editor-bằng-tài-khoản-của-bạn)
45
76
  - [Biến môi trường](#biến-môi-trường)
46
77
  - [CI](#ci)
@@ -268,8 +299,18 @@ Một file đầy đủ hơn:
268
299
 
269
300
  ## `sb login` / `sb logout` — đăng nhập qua trình duyệt
270
301
 
302
+ **Trên terminal, nó tự mở trang cho bạn** — chỉ trang `/device`, không bao giờ kèm
303
+ mã; mã vẫn do bạn gõ. Nó không mở khi chạy trong CI, khi đầu ra không phải
304
+ terminal, hoặc khi có `SB_NO_BROWSER=1`; địa chỉ luôn được in ra. Khi được duyệt,
305
+ nó nói rõ đã đăng nhập bằng tài khoản nào.
306
+
307
+ `sb whoami` in tài khoản đó và địa chỉ platform (`me@shop.vn on
308
+ https://api.sbuilder.io.vn`), hoặc cho biết `SB_TOKEN` đang là API key hay deploy
309
+ key, hoặc `Not signed in` (exit 1).
310
+
271
311
  ```bash
272
312
  sb login
313
+ sb whoami
273
314
  sb logout
274
315
  ```
275
316
 
@@ -594,6 +635,126 @@ Claude Code của bạn. Lựa chọn đó ở lại với site: khi không có
594
635
  lý của site đó dừng và báo rõ, chứ không chuyển sang key của nền tảng. Hãy để nó
595
636
  chạy (`sb agent install`) hoặc đổi nhà cung cấp trong cài đặt trợ lý.
596
637
 
638
+ ## `sb site` — site thành một repo dựng bằng AI, đồng bộ với GitHub
639
+
640
+ ```
641
+ npm create sbuilder-app # hoặc: sb init — lựa chọn 1 là "website mới do AI dựng"
642
+ sb site init my-shop --github # repo cho một site của bạn, trên repo GitHub riêng tư
643
+ sb site pull # lấy những gì đã sửa trong editor
644
+ sb site push # lưu file trang đã sửa thành BẢN NHÁP
645
+ sb site publish --all # xuất bản trang (hoặc chỉ định file)
646
+ sb site next # phục vụ các trang từ một app Next.js (npm run dev)
647
+ sb mcp # MCP server của Store Builder, với key của repo này
648
+ ```
649
+
650
+ **Dựng bằng AI.** Lựa chọn 1 của `sb init` hỏi bạn mô tả website, tạo site, viết repo
651
+ dưới đây và — nếu máy có Claude Code — mở nó ngay trong thư mục đó với mô tả của bạn,
652
+ để việc dựng bắt đầu luôn. Agent nào cũng dùng được: repo có sẵn `.mcp.json`,
653
+ `.cursor/mcp.json` và `.vscode/mcp.json`, nên Claude Code, Cursor và VS Code thấy bộ
654
+ công cụ Store Builder ngay khi mở thư mục (duyệt server `sbuilder` một lần), còn
655
+ `AGENTS.md` (được `CLAUDE.md` nạp) chỉ cho agent cách làm: ưu tiên phần tử có sẵn, xem
656
+ từng section trên desktop và mobile, hỏi dữ liệu thật, kéo kết quả về repo, **không
657
+ bao giờ tự xuất bản**. Agent sửa bản nháp của site; bạn duyệt lại bằng git diff.
658
+
659
+ **`sb site init` viết ra những gì.** Nó đăng nhập nếu cần, cho bạn chọn site (hoặc nhận
660
+ `--site <id>`), rồi viết:
661
+
662
+ | Đường dẫn | Là gì |
663
+ | --- | --- |
664
+ | `design/pages/*.page.tsx` | Bản nháp của một trang dưới dạng mã nguồn (xem *File trang* bên dưới) |
665
+ | `code/` | File Code Dock của site (`sb code pull --dir code`) |
666
+ | `.sb-site.json` | Lần đồng bộ cuối — hãy commit |
667
+ | `.env` | API key tạo riêng cho repo, quyền `0600`, đã nằm trong .gitignore |
668
+ | `.mcp.json`, `.cursor/mcp.json`, `.vscode/mcp.json` | MCP server, chạy qua `sb mcp` |
669
+ | `AGENTS.md`, `CLAUDE.md` | Cách một AI agent làm việc trong repo |
670
+ | `.github/workflows/sbuilder.yml` | Nửa GitHub của việc đồng bộ |
671
+
672
+ Key có tên `AI agent · <thư mục>` và mang mọi quyền nội dung mà tài khoản bạn được
673
+ giao — không bao giờ có thành viên, vai trò, cài đặt site hay export. Thu hồi trong
674
+ Settings -> API keys. Nếu không tạo được key, init nói rõ rồi làm tiếp. Trong repo,
675
+ `sb site`, `sb code` và `sb mcp` đọc `.env`; biến đã đặt sẵn trong shell được ưu tiên.
676
+ `--github` chạy tiếp `git init`, commit đầu tiên, `gh repo create --private --push` và
677
+ `gh secret set SB_TOKEN` (key đi qua stdin, không bao giờ nằm trên dòng lệnh) — workflow
678
+ chạy được ngay, không phải dán gì.
679
+
680
+ **File trang.** Mỗi trang là một file `.page.tsx` đọc như React — mỗi phần tử một thẻ, cài
681
+ đặt của nó là prop:
682
+
683
+ ```tsx
684
+ export default (
685
+ <Page id="ROOT">
686
+ <FlexSection id="fs_a5608576" style={{ padding: "40px 24px" }} mobile={{ style: { padding: "24px 16px" } }}>
687
+ <Heading id="he_91c2" level={1} text="Trà Thái Nguyên" />
688
+ <Button text="Mua ngay" />
689
+ </FlexSection>
690
+ </Page>
691
+ );
692
+ ```
693
+
694
+ Sửa chữ và cài đặt, di chuyển hay thêm phần tử (phần tử mới không cần `id` — có khi push), rồi
695
+ `sb site push`. Giữ nguyên các `id` đã có: đó là cách phân biệt chỗ sửa trong editor với chỗ sửa ở
696
+ đây. File trang chứa **dữ liệu, không phải code** — không logic, không biến, không chữ nằm giữa
697
+ thẻ (dùng `text="…"`); thứ khác bị từ chối kèm `file:dòng:cột` và không gửi gì. Định dạng chính
698
+ xác cả hai chiều: mọi trang nền tảng từng lưu đều in ra mã nguồn và đọc lại y hệt, nên pull rồi
699
+ push không thể làm hỏng trang. Các phần tử nằm ngoài cây trang (trạng thái rỗng của danh sách, nút
700
+ số lượng) nằm trong `export const detached`.
701
+
702
+ **App đã cài.** Block của một app nằm trong file trang với toàn bộ cây con của nó, gốc mang
703
+ `appBlockId`. Nó thuộc về app: nền tảng bỏ đi mọi chỗ sửa bên trong, nên `sb site push` từ chối
704
+ những chỗ sửa đó trước khi gửi và nêu tên block — chỉ đổi `appBlockValues`, di chuyển, hoặc xoá
705
+ cả block. `sb site blocks` liệt kê các block app đã cài cho phép đặt, kèm sẵn dòng để dán
706
+ (`<FlexBlock appBlockRef="ins_…/badge" />`) và các cài đặt nó nhận. Dữ liệu của app và script
707
+ island của nó cũng render được trên host Next.js. Mọi cảnh báo nền tảng trả về khi lưu đều được
708
+ in ra, không bao giờ bị nuốt.
709
+
710
+ **Chạy như một app Next.js.** `sb site next` thêm một host Next.js vào repo: `npm install`, rồi
711
+ `npm run dev`, và `http://localhost:3000` phục vụ mọi file trang **được render bằng chính renderer
712
+ của nền tảng** — code Go biên dịch sang WebAssembly (`@sbuilder/render`), nên trang ở đây giống
713
+ từng byte với trên cửa hàng, không phải "gần giống". Lưu một `.page.tsx`, refresh, thấy ngay — trước
714
+ khi push gì cả. Dữ liệu trang hiển thị (sản phẩm, bài viết, theme, cài đặt) lấy từ
715
+ `POST /api/v1/pages/{id}/render-context` bằng key của repo; `/_wb/*` (giỏ hàng, thanh toán, form,
716
+ runtime islands) và đường dẫn không có file trang thì chuyển về cửa hàng thật (`SB_STOREFRONT`
717
+ trong `.env`). Route bạn tự thêm trong `app/` được ưu tiên hơn route bắt mọi trang, nên trang React
718
+ viết tay nằm cạnh trang kéo thả. Deploy ở bất cứ đâu chạy được Next.js.
719
+
720
+ **Workflow là toàn bộ phần tích hợp GitHub** — không GitHub App, không webhook. Push lên
721
+ `main` có đụng `design/` hoặc `code/` thì lưu bản nháp; cứ 30 phút (hoặc bấm tay) nó kéo
722
+ những gì đã đổi trong editor rồi commit. Chạy tay và tick *publish* thì mọi trang được
723
+ xuất bản.
724
+
725
+ **Không có gì bị ghi đè âm thầm.** `.sb-site.json` ghi `rev` của server và hash của file ở
726
+ lần đồng bộ cuối. Pull chỉ ghi đè file bạn chưa sửa; push gửi `baseRev` và server từ chối
727
+ trang đã được sửa trong editor kể từ đó. Trang bị sửa ở cả hai phía được nêu tên và bỏ
728
+ qua (exit 1); `--force` chọn một phía — của editor khi pull, của bạn khi push.
729
+
730
+ **Push chỉ lưu bản nháp; `sb site publish` là thứ duy nhất ở đây đưa trang lên live**, và
731
+ chỉ xuất bản những file bạn nêu tên, hoặc mọi trang với `--all`. Không tạo hay xoá trang
732
+ từ file: tạo trang trong editor (hoặc để agent làm) rồi pull; trang bị xoá trên nền tảng
733
+ vẫn giữ file và được nêu tên. Với API key nó dùng `/api/v1/pages/{id}/document` và không
734
+ cần `--site`.
735
+
736
+ ## `sb export next` — site đã xuất bản thành một repo Next.js
737
+
738
+ ```
739
+ sb export next https://your-shop.example --dir my-shop
740
+ ```
741
+
742
+ Lệnh đọc `sitemap.xml` của site, tải mọi trang trong đó và ghi ra một repo
743
+ Next.js (vào `./<host>` nếu không có `--dir`; thư mục phải trống hoặc chưa có).
744
+ Mỗi trang được giữ nguyên như site đã xuất bản, trong `site/`, và được phục vụ bởi
745
+ một route tĩnh trong `app/`. Không render lại gì cả.
746
+
747
+ Mọi thứ bản xuất không chứa — giỏ hàng, thanh toán, form, ảnh, runtime của trang
748
+ xuất bản dưới `/_wb/`, và trang nào không có trong sitemap — được chuyển tiếp về
749
+ site đang chạy bằng một fallback rewrite trong `next.config.mjs` (`SITE_ORIGIN`
750
+ đổi địa chỉ đó). Trình duyệt chỉ thấy một origin, nên đăng nhập của khách vẫn
751
+ chạy trên host mới.
752
+
753
+ Đây là một bản chụp: giá và tồn kho trong HTML là của lúc xuất, nên chạy lại sau
754
+ mỗi lần xuất bản. Trang nào site không trả về HTML, hoặc có đường dẫn bắt đầu bằng
755
+ ký tự Next.js dành cho thư mục (`_`, `(`, `[`, `@`), sẽ bị bỏ qua và được nêu tên
756
+ trong output.
757
+
597
758
  ## `sb agent` — trợ lý của editor bằng tài khoản của bạn
598
759
 
599
760
  ```
@@ -610,7 +771,7 @@ làm việc; Ctrl-C để dừng, mất mạng thì nó tự kết nối lại.
610
771
  - Chạy trên macOS và Linux. Chưa hỗ trợ Windows (shim `.cmd` của npm không chạy
611
772
  được nếu không qua shell, mà shell thì sẽ phân tích lại prompt).
612
773
  - Cài và đăng nhập CLI trước (`claude`, hoặc `codex login`).
613
- - Admin nền tảng phải bật tuỳ chọn này (Admin → Cài đặt); khi chưa bật,
774
+ - Admin nền tảng phải bật tuỳ chọn này (Admin → Trợ lý AI); khi chưa bật,
614
775
  `sb agent` dừng và nói rõ lý do.
615
776
  - Claude Code chạy với mọi tool có sẵn đã tắt. **Job Codex bị từ chối trừ khi
616
777
  bạn chạy `sb agent --codex`**: Codex đọc được file trên máy này qua lệnh shell
@@ -672,12 +833,14 @@ Nếu key bị lộ, hãy thu hồi nó ngay trên cùng màn hình; yêu cầu
672
833
 
673
834
  ## Biến môi trường
674
835
 
675
- Hai biến do chính CLI đọc:
836
+ Ba biến do chính CLI đọc:
676
837
 
677
838
  | Biến | Lệnh cần nó | Mặc định |
678
839
  | --- | --- | --- |
679
840
  | `SB_TOKEN` | `sb dev`, `sb deploy`, `sb code` — tuỳ chọn | không có — dùng credential của `sb login`; thiếu cả hai thì mỗi lệnh đều dừng và nhắc `sb login` |
680
841
  | `SB_API` | mọi lệnh trừ `sb init` | `https://api.sbuilder.io.vn` — đặt `http://localhost:8080` khi chạy platform ở máy |
842
+ | `SB_NO_BROWSER` | `sb login` | không đặt — đặt nó để `sb login` không tự mở trang `/device` (CI và đầu ra không phải terminal thì không bao giờ mở) |
843
+ | `SB_STOREFRONT` | app của `sb site next` | địa chỉ cửa hàng thật (`sb site init` ghi vào `.env`) — nơi `/_wb/*` và đường dẫn không có file trang được chuyển tới |
681
844
 
682
845
  `SB_TOKEN`, khi được đặt, **ghi đè** credential đã lưu của `sb login`. Với
683
846
  `sb dev` và `sb deploy`, nó là token của **tổ chức sở hữu app** — đúng cái user