@sbuilder/cli 0.3.0 → 0.5.1

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
@@ -15,6 +15,13 @@ sb dev upload an island's code to your DRAFT version
15
15
  sb login (or SB_TOKEN).
16
16
  sb deploy [--submit] push the version's payload in dependency order.
17
17
  --submit then sends it for review.
18
+ sb agent [--codex] run the editor's AI assistant on THIS machine's
19
+ Claude Code login — no API key (--codex: Codex too)
20
+ sb code pull [--site <id>] [--dir <path>] [--force]
21
+ mirror a site's Code Dock files into ./code
22
+ sb code push [--site <id>] [--dir <path>] [--force] [--dry-run] [--delete]
23
+ send local edits back; refuses a file changed
24
+ in the Code Dock since your pull unless --force
18
25
  ```
19
26
 
20
27
  Node 20 or newer. Written in TypeScript, published compiled — an installed copy
@@ -32,6 +39,8 @@ has no dependencies and nothing to build.
32
39
  - [`sb login` / `sb logout`](#sb-login--sb-logout--sign-in-through-the-browser)
33
40
  - [`sb dev`](#sb-dev--save-upload-refresh)
34
41
  - [`sb deploy`](#sb-deploy--the-whole-payload-in-dependency-order)
42
+ - [`sb code`](#sb-code--your-code-dock-files-as-a-folder)
43
+ - [`sb agent`](#sb-agent--the-editor-assistant-on-your-own-login)
35
44
  - [Environment variables](#environment-variables)
36
45
  - [CI](#ci)
37
46
  - [Refusals, and what they mean](#refusals-and-what-they-mean)
@@ -197,7 +206,7 @@ has not happened yet at `init` time. Write it by hand once you have them:
197
206
  {
198
207
  "orgId": "org_yourorg",
199
208
  "appId": "app_1a2b3c4d",
200
- "versionId": "ver_5e6f7a8b",
209
+ "versionId": "apv_5e6f7a8b",
201
210
  "island": "countdown",
202
211
  "entry": "islands/countdown.js"
203
212
  }
@@ -229,13 +238,16 @@ them in even if you are only deploying blocks.
229
238
  An `sb.json` with only the five required keys is valid: `sb dev` works, and
230
239
  `sb deploy` finds no steps to run.
231
240
 
241
+ `sb code` reads one more optional key from the same file, `siteId`, and nothing
242
+ else: it does not need the five required keys.
243
+
232
244
  A fuller one:
233
245
 
234
246
  ```json
235
247
  {
236
248
  "orgId": "org_yourorg",
237
249
  "appId": "app_1a2b3c4d",
238
- "versionId": "ver_5e6f7a8b",
250
+ "versionId": "apv_5e6f7a8b",
239
251
  "island": "countdown",
240
252
  "entry": "islands/countdown.js",
241
253
  "version": {
@@ -282,8 +294,8 @@ local and a production sign-in coexist), in
282
294
  only (mode `0600`). `sb login` is a separate signed-in device on your account,
283
295
  listed with your other sessions; signing out everywhere ends it too.
284
296
 
285
- **It renews itself.** When the server refuses the access token, `sb dev` and
286
- `sb deploy` rotate the pair once, save it, and retry the request. Two `sb`
297
+ **It renews itself.** When the server refuses the access token, `sb dev`,
298
+ `sb deploy` and `sb code` rotate the pair once, save it, and retry the request. Two `sb`
287
299
  processes renewing at the same moment take turns through a lock file beside
288
300
  the credentials, and the second one adopts the pair the first one saved — a
289
301
  refresh token presented twice would end the session. If the renewal itself is
@@ -308,7 +320,7 @@ every time you save. It uploads once immediately on start rather than waiting fo
308
320
  your first save, then runs until you interrupt it.
309
321
 
310
322
  ```
311
- watching islands/countdown.js — save it and this uploads to version ver_5e6f7a8b.
323
+ watching islands/countdown.js — save it and this uploads to version apv_5e6f7a8b.
312
324
  This is not hot reload: after each upload, refresh the page.
313
325
  uploaded countdown — refresh the page to see it
314
326
  ```
@@ -398,6 +410,193 @@ earlier, with more room to explain than a status code has.
398
410
 
399
411
  ---
400
412
 
413
+ ## `sb code` — your Code Dock files as a folder
414
+
415
+ ```bash
416
+ sb code pull --site <site-id>
417
+ # edit code/site/css/main.css in your own editor, then
418
+ sb code push
419
+ ```
420
+
421
+ Mirrors a site's **Code Dock** files — the site-wide and per-page CSS, JavaScript
422
+ and HTML — into a folder, so you can edit them in your own editor, keep them in
423
+ git and push them back.
424
+
425
+ ### Layout, and the manifest
426
+
427
+ ```
428
+ code/
429
+ .sb-code.json the manifest — commit it
430
+ site/css/main.css the site-wide file named "css/main.css"
431
+ pages/about/track.js the file "track.js" on the page whose slug is "about"
432
+ ```
433
+
434
+ A `/` in a file's name becomes a folder. A page's folder is its slug **at the
435
+ first pull** (the homepage is `home`; an empty or clashing slug becomes
436
+ `page-<id>`) and is then recorded in the manifest, so renaming the slug later does
437
+ not move your files. Renaming a file in the Code Dock does not move it either: it
438
+ keeps its local path, because it is tracked by its id, and its content still syncs.
439
+ A Code Dock file with no name pulls as `unnamed-<id>.<ext>` and is
440
+ tracked by its id.
441
+
442
+ Symbolic links never lead `sb code` outside the root: a file that is a link, or whose
443
+ folder leads outside the root through one, is named and skipped — pull writes nothing
444
+ through it, push sends nothing from it — while the other files still sync and the run
445
+ exits `1`. Two links are refused before anything is sent: a `.sb-code.json` that is a
446
+ link, and, when `--dir` is omitted, a `code` folder that is a link — pass `--dir <path>`
447
+ to sync through it deliberately.
448
+
449
+ `.sb-code.json` records, per file, the server's id and metadata, its `updatedAt`
450
+ and a sha256 of the content **as last pulled or pushed**. That is the base both
451
+ directions compare against, which is how `sb code` tells your edit from an edit
452
+ made in the Code Dock. **Commit it**: a CI push then judges conflicts against the
453
+ last pull you committed.
454
+
455
+ ### Pull
456
+
457
+ `sb code pull` writes every file the server has. It **never overwrites a local
458
+ edit**:
459
+
460
+ - Your file changed and the server's did not: it is left alone and printed as
461
+ `pending` — `sb code push` sends it. Exit `0`.
462
+ - Both changed: the file is kept, named, and the run exits `1`.
463
+ - `sb code pull --force` overwrites every local edit with the server's version,
464
+ in both cases.
465
+ - A file deleted on the server is removed locally only if you never edited it.
466
+
467
+ It ends with a summary line: written, pending, kept, removed.
468
+
469
+ ### Push
470
+
471
+ `sb code push` builds a plan against the manifest:
472
+
473
+ | Plan | When | Needs |
474
+ | --- | --- | --- |
475
+ | update | a tracked file whose content differs from its base | nothing |
476
+ | create | an untracked file under `site/` or `pages/<page>/` | nothing — the extension picks the language (below); any other extension, or none, makes a **disabled** `text` file |
477
+ | delete | a tracked file that is missing locally | `--delete`; without it the file is printed as `missing` and kept on the server |
478
+
479
+ Creates run first, then updates, then deletes. Each update and delete carries the
480
+ `updatedAt` from your last pull, and **the server compares it inside the write**:
481
+ a file changed in the Code Dock since then answers `409 code_file_changed`, that
482
+ one file is refused ("changed in the Code Dock since your pull; run sb code pull,
483
+ or push --force"), the rest of the run continues, and the exit code is `1`.
484
+
485
+ **The manifest is written after every operation**, so a run that stops halfway —
486
+ a server error or a lost connection stops it — is resumed by running it again;
487
+ nothing is created twice.
488
+
489
+ Only the **content** syncs. Placement, enabled, position and the language of an
490
+ existing file stay in the Code Dock, where their effect is visible. A new file gets
491
+ the server's defaults, the same as in the Code Dock — and the line printed for it
492
+ says which, so a `.js` that lands in `body_end` is never a surprise:
493
+
494
+ | Extension | Language | Default placement |
495
+ | --- | --- | --- |
496
+ | `.css` | css | `head` |
497
+ | `.scss` | scss | `head` |
498
+ | `.js`, `.mjs` | javascript | `body_end` |
499
+ | `.ts`, `.tsx` | typescript | `body_end` |
500
+ | `.html`, `.htm` | html | `body_end` |
501
+ | `.json`, `.webmanifest` | json | `none` |
502
+ | `.txt`, `.md`, `.xml`, `.svg`, `.csv`, anything else | text | `none` |
503
+
504
+ `none` is never injected into a page; an **enabled site** file with placement `none`
505
+ is served on the store host at `/_wb/code/<name>`, and push prints `served at
506
+ /_wb/code/<name>` for it. A file whose extension is **not in the table** (`.yaml`,
507
+ `README`, …) is created as text and **disabled**, so nothing is served until you
508
+ decide it should be: push prints `created disabled (unknown extension) — enable it
509
+ in the Code Dock to serve it at /_wb/code/<name>`. Code files are text: a file
510
+ containing a NUL byte is refused before anything is sent for it (exit `1`).
511
+
512
+ An `.scss` or `.ts` source is compiled by the
513
+ server when it is written; when that fails the source **is** stored, the site keeps
514
+ the previous output, and push prints `compile error <path>: <message>` and exits
515
+ `1`. A moved or renamed local file is a create plus a `--delete`able delete, and
516
+ `--dry-run` shows it as exactly that.
517
+
518
+ ### Flags
519
+
520
+ | Flag | Applies to | Effect |
521
+ | --- | --- | --- |
522
+ | `--site <id>` | pull, push | The site to sync. Needed with `sb login` or a user token unless `sb.json` or the manifest names it; a `wbk_` key implies its site |
523
+ | `--dir <path>` | pull, push | The root folder, `code` by default |
524
+ | `--force` | pull, push | Pull overwrites every local edit with the server's version. Push sends no `expectedUpdatedAt`, so the **content** you have wins; it still sends the server's current metadata, so it never reverts what the Code Dock changed |
525
+ | `--dry-run` | push | Print the plan and send nothing |
526
+ | `--delete` | push | Also delete the server files whose local copy is gone |
527
+
528
+ Deleting something you still depend on is a decision, never a default — which is
529
+ why `--delete` exists at all rather than push inferring it.
530
+
531
+ ### Which API it talks to
532
+
533
+ - **`SB_TOKEN` starting `wbk_`** — a merchant API key — uses `/api/v1/code-files`.
534
+ The key implies its site, so `--site` is not needed.
535
+ - **Anything else** — the credential stored by `sb login`, or a pasted user token
536
+ — uses `/api/sites/{siteId}/code-files`. The site comes from `--site`, else
537
+ the optional `siteId` in `sb.json`, else the manifest's. Two sources that name
538
+ different sites are refused before any request.
539
+ - **`SB_TOKEN` starting `sbdk_`** is refused before any request. A deploy key
540
+ belongs to a developer organization and only reaches its apps' version routes;
541
+ site code is a merchant's content.
542
+
543
+ A `wbk_` key whose manifest shares **no** file with the server's list is refused
544
+ as belonging to a different site, so a CI key cannot write site A's files onto
545
+ site B. `--force` overrides it.
546
+
547
+ ### CI for `sb code`
548
+
549
+ Create an API key in the merchant's **Settings → API keys** with `codefiles.write`
550
+ (add `pages.read` if CI creates files in a page folder the manifest does not
551
+ know yet), and store it as the CI secret `SB_TOKEN`:
552
+
553
+ ```bash
554
+ SB_TOKEN="$SB_API_KEY" sb code push
555
+ ```
556
+
557
+ A key without `codefiles.write` is refused by the server and nothing is written.
558
+ `codefiles.write` means "may add a script to every published page", so give it
559
+ to a key only when a pipeline needs it. Commit `code/`, manifest included.
560
+
561
+ ### Exit codes
562
+
563
+ `0` when every planned operation succeeded. `1` when anything was refused or skipped
564
+ against the server's will: a `409`, a validation error, a compile error on a pushed
565
+ `.scss`/`.ts` file, a file containing a NUL byte, a pull that kept a file you and the server both changed, an `sbdk_` key, a site
566
+ mismatch or a refused token. A pending local edit seen by pull, or a tracked
567
+ file missing locally without `--delete`, is printed and is **not** a failure.
568
+
569
+ ---
570
+
571
+ ## `sb agent` — the editor assistant on your own login
572
+
573
+ ```
574
+ sb login
575
+ sb agent
576
+ ```
577
+
578
+ Then, in the editor: **AI assistant → Settings → Provider → "Claude Code (sb
579
+ agent)"** (or "Codex (sb agent)"). Every round the assistant needs runs `claude` / `codex`
580
+ here, on the subscription you are logged in with, and the answer goes back to
581
+ the browser. No API key, no platform credits. Keep it running while you work;
582
+ Ctrl-C stops it, and it reconnects on its own if the network drops.
583
+
584
+ - macOS and Linux. Windows is not supported yet (npm's `.cmd` shims cannot be
585
+ started without a shell, and a shell would re-parse the prompt).
586
+ - Install and log in to the CLI first (`claude`, or `codex login`).
587
+ - The platform admin must switch the option on (Admin → Settings); until then
588
+ `sb agent` stops and says so.
589
+ - Claude Code runs with every built-in tool off. **Codex jobs are refused unless
590
+ you start `sb agent --codex`**: Codex can read files on this machine through
591
+ read-only shell commands, the prompt carries page content other editors of
592
+ the site wrote, and the provider is a SITE setting any of them can change —
593
+ so whether Codex runs here is your call, not theirs. Each round prints the
594
+ site it ran for.
595
+ - The CLI gets a trimmed environment: your shell's secrets and vendor API keys
596
+ are not passed to it.
597
+
598
+ ---
599
+
401
600
  ## CI
402
601
 
403
602
  A user access token expires, so it is the wrong credential for a pipeline. Use a
@@ -407,7 +606,7 @@ A user access token expires, so it is the wrong credential for a pipeline. Use a
407
606
  (an owner or admin can do this), then create one. The secret starts with
408
607
  `sbdk_` and is shown **once**.
409
608
  2. Store it as the CI secret `SB_TOKEN`. `sb dev` and `sb deploy` accept it
410
- exactly like a pasted token.
609
+ exactly like a pasted token. (`sb code` does not: see its own CI recipe.)
411
610
 
412
611
  ```bash
413
612
  SB_TOKEN="$SB_DEPLOY_KEY" sb deploy
@@ -430,18 +629,23 @@ Two are read by the CLI itself:
430
629
 
431
630
  | Variable | Required by | Default |
432
631
  | --- | --- | --- |
433
- | `SB_TOKEN` | `sb dev`, `sb deploy` — optional | none — the `sb login` credential is used; with neither, both stop and name `sb login` |
632
+ | `SB_TOKEN` | `sb dev`, `sb deploy`, `sb code` — optional | none — the `sb login` credential is used; with neither, each stops and names `sb login` |
434
633
  | `SB_API` | every command but `sb init` | `http://localhost:8080` |
435
634
 
436
- `SB_TOKEN` **overrides** the stored `sb login` credential when set. It is a
437
- token for the **organization that owns the app** — the same user access token
438
- you would put in an `Authorization: Bearer` header against the org-scoped
439
- routes. It is **not** a `wba_` app token; those belong to an install, and an
440
- install cannot edit the app it is an install of.
441
-
442
- A pasted token cannot renew itself. When the server refuses it, the CLI says
443
- `SB_TOKEN was refused: it has expired or been revoked` and points you at
444
- `sb login` — it never tries to refresh it.
635
+ `SB_TOKEN` **overrides** the stored `sb login` credential when set. For `sb dev`
636
+ and `sb deploy` it is a token for the **organization that owns the app** — the
637
+ same user access token you would put in an `Authorization: Bearer` header against
638
+ the org-scoped routes. It is **not** a `wba_` app token; those belong to an
639
+ install, and an install cannot edit the app it is an install of. For `sb code` it
640
+ is a user token with access to the site, or a merchant `wbk_` API key (see
641
+ [`sb code`](#sb-code--your-code-dock-files-as-a-folder)).
642
+
643
+ A pasted token cannot renew itself, and the CLI never tries to refresh it. When
644
+ the server refuses a pasted token, `sb dev` and `sb deploy` say
645
+ `SB_TOKEN was refused: it has expired or been revoked` and point you at
646
+ `sb login`. `sb code` says
647
+ ``Not signed in, or the token was refused (expired or revoked). Run `sb login`, or set SB_TOKEN to an API key (wbk_…) with codefiles.read / codefiles.write from Settings -> API keys.``
648
+ instead, and never suggests a deploy key, because `sb code` refuses one.
445
649
 
446
650
  Three more are read by the app `sb init` writes, from its own `.env`:
447
651
 
@@ -461,7 +665,10 @@ Every command exits `0` on success and `1` on a refusal it can explain. Anything
461
665
  else is a bug in the tool and gets a stack trace, because a scaffolder that
462
666
  swallows its own failures is one nobody can report.
463
667
 
464
- The server codes the CLI translates, rather than passing through verbatim:
668
+ `sb dev` and `sb deploy` translate the server codes below rather than passing
669
+ them through verbatim. `sb code` words its own refusals: the refused-token one
670
+ is above, and the [`sb code`](#sb-code--your-code-dock-files-as-a-folder)
671
+ section describes the command.
465
672
 
466
673
  | Server code | What `sb` says instead |
467
674
  | --- | --- |
@@ -491,7 +698,6 @@ Stated so you can plan around it rather than discover it.
491
698
  - **`sb dev` is not hot reload**, and no future version of it can be while an
492
699
  island's code is served from the platform's object store.
493
700
  - **`sb deploy` is not atomic**, deliberately.
494
- - **It is not on npm yet.**
495
701
 
496
702
  ---
497
703
 
@@ -502,6 +708,7 @@ Stated so you can plan around it rather than discover it.
502
708
  | `Not signed in. Run sb login …` | `dev`/`deploy` have no credential | `sb login`, or set `SB_TOKEN` |
503
709
  | `Your sb login has expired or was revoked.` | The stored session ended (signed out everywhere, or it lapsed) | `sb login` again |
504
710
  | `SB_TOKEN was refused: …` | The pasted token expired or was revoked | `sb login`, or a fresh token |
711
+ | `Not signed in, or the token was refused …` | `sb code` has no credential, the pasted `SB_TOKEN` was refused, or the server still answered 401 after renewing your `sb login` | `sb login`, or set `SB_TOKEN` to a `wbk_` API key with `codefiles.read` / `codefiles.write` from Settings → API keys |
505
712
  | `No sb.json here.` | Run from the wrong directory | `cd` to the folder holding `sb.json` |
506
713
  | `sb.json is missing "versionId".` | A required key is absent or empty | Add all five required keys |
507
714
  | `unknown command: …` | Typo, or a stale `dist/` | `npm run build --workspace cli` |
package/README.vi.md CHANGED
@@ -14,6 +14,13 @@ sb dev tải code của một island lên bản DRAFT m
14
14
  lưu file. Đọc sb.json; cần sb login (hoặc SB_TOKEN).
15
15
  sb deploy [--submit] đẩy toàn bộ payload của version theo đúng thứ tự
16
16
  phụ thuộc. --submit thì gửi luôn đi duyệt.
17
+ sb agent [--codex] chạy trợ lý AI của editor bằng Claude Code đã đăng
18
+ nhập trên MÁY NÀY — không cần API key (--codex: cả Codex)
19
+ sb code pull [--site <id>] [--dir <path>] [--force]
20
+ kéo các file Code Dock của một site về ./code
21
+ sb code push [--site <id>] [--dir <path>] [--force] [--dry-run] [--delete]
22
+ gửi các chỗ sửa ở máy ngược lại; từ chối file đã
23
+ đổi trong Code Dock kể từ lần pull, trừ khi --force
17
24
  ```
18
25
 
19
26
  Cần Node 20 trở lên. Viết bằng TypeScript, **publish ở dạng đã biên dịch** — bản
@@ -31,6 +38,8 @@ cài về không có dependency nào và không phải build gì cả.
31
38
  - [`sb login` / `sb logout`](#sb-login--sb-logout--đăng-nhập-qua-trình-duyệt)
32
39
  - [`sb dev`](#sb-dev--lưu-tải-lên-refresh)
33
40
  - [`sb deploy`](#sb-deploy--toàn-bộ-payload-theo-thứ-tự-phụ-thuộc)
41
+ - [`sb code`](#sb-code--file-code-dock-của-bạn-dưới-dạng-một-thư-mục)
42
+ - [`sb agent`](#sb-agent--trợ-lý-của-editor-bằng-tài-khoản-của-bạn)
34
43
  - [Biến môi trường](#biến-môi-trường)
35
44
  - [CI](#ci)
36
45
  - [Các từ chối, và ý nghĩa của chúng](#các-từ-chối-và-ý-nghĩa-của-chúng)
@@ -196,7 +205,7 @@ việc chưa xảy ra ở thời điểm `init`. Hãy tự viết một lần kh
196
205
  {
197
206
  "orgId": "org_yourorg",
198
207
  "appId": "app_1a2b3c4d",
199
- "versionId": "ver_5e6f7a8b",
208
+ "versionId": "apv_5e6f7a8b",
200
209
  "island": "countdown",
201
210
  "entry": "islands/countdown.js"
202
211
  }
@@ -228,13 +237,16 @@ hãy điền đủ ngay cả khi bạn chỉ deploy blocks.
228
237
  Một `sb.json` chỉ có năm khoá bắt buộc vẫn hợp lệ: `sb dev` chạy được, còn
229
238
  `sb deploy` sẽ không tìm thấy bước nào để chạy.
230
239
 
240
+ `sb code` đọc thêm đúng một khoá tuỳ chọn từ cùng file này, `siteId`, và không đọc
241
+ gì khác: nó không cần năm khoá bắt buộc.
242
+
231
243
  Một file đầy đủ hơn:
232
244
 
233
245
  ```json
234
246
  {
235
247
  "orgId": "org_yourorg",
236
248
  "appId": "app_1a2b3c4d",
237
- "versionId": "ver_5e6f7a8b",
249
+ "versionId": "apv_5e6f7a8b",
238
250
  "island": "countdown",
239
251
  "entry": "islands/countdown.js",
240
252
  "version": {
@@ -281,8 +293,8 @@ bạn đọc được (mode `0600`). Mỗi lần `sb login` là một thiết b
281
293
  trên tài khoản, nằm cùng danh sách phiên với các thiết bị khác; đăng xuất mọi
282
294
  nơi cũng kết thúc nó.
283
295
 
284
- **Nó tự gia hạn.** Khi server từ chối access token, `sb dev` và `sb deploy` xoay
285
- cặp token một lần, lưu lại, rồi gửi lại request. Hai tiến trình `sb` cùng gia
296
+ **Nó tự gia hạn.** Khi server từ chối access token, `sb dev`, `sb deploy` và
297
+ `sb code` xoay cặp token một lần, lưu lại, rồi gửi lại request. Hai tiến trình `sb` cùng gia
286
298
  hạn một lúc sẽ lần lượt đi qua một file khoá đặt cạnh file credential, và tiến
287
299
  trình thứ hai dùng luôn cặp mà tiến trình đầu đã lưu — một refresh token bị dùng
288
300
  hai lần sẽ kết thúc phiên. Nếu chính việc gia hạn bị từ chối, mục đó bị xoá và
@@ -307,7 +319,7 @@ lần bạn lưu. Nó tải lên ngay một lần lúc khởi động chứ khô
307
319
  tiên, rồi chạy cho tới khi bạn ngắt.
308
320
 
309
321
  ```
310
- watching islands/countdown.js — save it and this uploads to version ver_5e6f7a8b.
322
+ watching islands/countdown.js — save it and this uploads to version apv_5e6f7a8b.
311
323
  This is not hot reload: after each upload, refresh the page.
312
324
  uploaded countdown — refresh the page to see it
313
325
  ```
@@ -398,6 +410,196 @@ kiểm tra thật, và nếu hai bên có mâu thuẫn thì server thắng; cái
398
410
 
399
411
  ---
400
412
 
413
+ ## `sb code` — file Code Dock của bạn dưới dạng một thư mục
414
+
415
+ ```bash
416
+ sb code pull --site <site-id>
417
+ # sửa code/site/css/main.css bằng trình soạn thảo của bạn, rồi
418
+ sb code push
419
+ ```
420
+
421
+ Đồng bộ các file **Code Dock** của một site — CSS, JavaScript và HTML dùng chung
422
+ toàn site hoặc riêng từng trang — về một thư mục, để bạn sửa bằng trình soạn
423
+ thảo của mình, lưu trong git và đẩy ngược lại.
424
+
425
+ ### Bố cục, và manifest
426
+
427
+ ```
428
+ code/
429
+ .sb-code.json manifest — hãy commit nó
430
+ site/css/main.css file toàn site tên "css/main.css"
431
+ pages/about/track.js file "track.js" của trang có slug là "about"
432
+ ```
433
+
434
+ Dấu `/` trong tên file trở thành thư mục. Thư mục của một trang là slug của nó
435
+ **tại lần pull đầu tiên** (trang chủ là `home`; slug rỗng hoặc trùng thì thành
436
+ `page-<id>`) rồi được ghi vào manifest, nên đổi slug về sau không làm file của
437
+ bạn đổi chỗ. Đổi tên một file trong Code Dock cũng không làm nó đổi chỗ: nó giữ
438
+ nguyên đường dẫn ở máy, vì được theo dõi theo id, và nội dung của nó vẫn được đồng
439
+ bộ. Một file Code Dock không có tên sẽ được pull về thành
440
+ `unnamed-<id>.<ext>` và được theo dõi theo id.
441
+
442
+ Symlink không bao giờ dẫn `sb code` ra ngoài thư mục gốc: một file là symlink, hoặc nằm
443
+ trong thư mục dẫn ra ngoài thư mục gốc qua một symlink, được nêu tên và bỏ qua — pull
444
+ không ghi gì qua nó, push không gửi gì từ nó — các file khác vẫn đồng bộ và lần chạy
445
+ thoát `1`. Hai symlink bị từ chối trước khi gửi bất kỳ thứ gì: một `.sb-code.json` là
446
+ symlink, và, khi không có `--dir`, một thư mục `code` là symlink — truyền `--dir <path>`
447
+ để cố ý đồng bộ qua nó.
448
+
449
+ `.sb-code.json` ghi, cho mỗi file, id và metadata phía server, `updatedAt` của nó
450
+ và sha256 của nội dung **như lần pull hoặc push gần nhất**. Đó là mốc gốc mà cả
451
+ hai chiều so sánh với, nhờ vậy `sb code` phân biệt được chỗ bạn sửa với chỗ sửa
452
+ trong Code Dock. **Hãy commit nó**: push từ CI khi đó xét xung đột theo lần pull
453
+ gần nhất mà bạn đã commit.
454
+
455
+ ### Pull
456
+
457
+ `sb code pull` ghi mọi file mà server có. Nó **không bao giờ ghi đè chỗ bạn đã
458
+ sửa**:
459
+
460
+ - File của bạn đã đổi còn server thì không: để nguyên và in là `pending` —
461
+ `sb code push` sẽ gửi nó. Thoát `0`.
462
+ - Cả hai cùng đổi: file được giữ lại, được nêu tên, và lần chạy thoát `1`.
463
+ - `sb code pull --force` ghi đè mọi chỗ bạn đã sửa bằng bản của server, trong
464
+ cả hai trường hợp.
465
+ - File bị xoá trên server chỉ bị xoá ở máy bạn nếu bạn chưa từng sửa nó.
466
+
467
+ Cuối cùng là một dòng tổng kết: đã ghi, pending, giữ lại, đã xoá.
468
+
469
+ ### Push
470
+
471
+ `sb code push` lập một kế hoạch dựa trên manifest:
472
+
473
+ | Kế hoạch | Khi nào | Điều kiện |
474
+ | --- | --- | --- |
475
+ | update | file đã theo dõi mà nội dung khác mốc gốc | không |
476
+ | create | file chưa theo dõi nằm trong `site/` hoặc `pages/<page>/` | không — đuôi file quyết định ngôn ngữ (bảng bên dưới); đuôi khác, hoặc không có đuôi, tạo file `text` **bị tắt** |
477
+ | delete | file đã theo dõi nhưng không còn ở máy | `--delete`; không có nó thì file được in là `missing` và vẫn giữ trên server |
478
+
479
+ Create chạy trước, rồi update, rồi delete. Mỗi update và delete mang theo
480
+ `updatedAt` từ lần pull gần nhất của bạn, và **server so sánh nó ngay bên trong
481
+ lệnh ghi**: một file đã bị đổi trong Code Dock kể từ đó sẽ trả `409
482
+ code_file_changed`, riêng file đó bị từ chối ("changed in the Code Dock since your
483
+ pull; run sb code pull, or push --force"), phần còn lại vẫn chạy tiếp, và mã
484
+ thoát là `1`.
485
+
486
+ **Manifest được ghi sau mỗi thao tác**, nên một lần chạy dừng giữa chừng — lỗi
487
+ server hoặc mất kết nối sẽ làm nó dừng — được tiếp tục bằng cách chạy lại;
488
+ không có gì bị tạo hai lần.
489
+
490
+ Chỉ **nội dung** được đồng bộ. Vị trí chèn (placement), bật/tắt, thứ tự và ngôn
491
+ ngữ của một file đã có vẫn nằm trong Code Dock, nơi hiệu ứng của chúng nhìn thấy
492
+ được. File mới nhận giá trị mặc định của server, giống như trong Code Dock — và
493
+ dòng in ra cho nó nói rõ là giá trị nào, nên một file `.js` rơi vào `body_end`
494
+ không bao giờ là bất ngờ:
495
+
496
+ | Đuôi file | Ngôn ngữ | Vị trí chèn mặc định |
497
+ | --- | --- | --- |
498
+ | `.css` | css | `head` |
499
+ | `.scss` | scss | `head` |
500
+ | `.js`, `.mjs` | javascript | `body_end` |
501
+ | `.ts`, `.tsx` | typescript | `body_end` |
502
+ | `.html`, `.htm` | html | `body_end` |
503
+ | `.json`, `.webmanifest` | json | `none` |
504
+ | `.txt`, `.md`, `.xml`, `.svg`, `.csv`, đuôi khác | text | `none` |
505
+
506
+ `none` không bao giờ được chèn vào trang; một file **site đang bật** có vị trí chèn
507
+ `none` được phục vụ trên host của store tại `/_wb/code/<name>`, và push in `served
508
+ at /_wb/code/<name>` cho nó. Một file có đuôi **không nằm trong bảng** (`.yaml`,
509
+ `README`, …) được tạo dưới dạng text và **bị tắt**, nên không có gì được phục vụ
510
+ cho tới khi bạn quyết định: push in `created disabled (unknown extension) — enable
511
+ it in the Code Dock to serve it at /_wb/code/<name>`. File code là văn bản: một file
512
+ chứa byte NUL bị từ chối trước khi gửi bất cứ thứ gì cho nó (thoát `1`).
513
+
514
+ Mã nguồn `.scss` hoặc `.ts` được server
515
+ biên dịch khi ghi; nếu biên dịch lỗi thì mã nguồn **vẫn** được lưu, site giữ bản
516
+ biên dịch trước đó, và push in `compile error <path>: <message>` rồi thoát `1`.
517
+ File bị di chuyển hay đổi tên ở máy là một create cộng với một delete (cần
518
+ `--delete`), và `--dry-run` hiển thị đúng như vậy.
519
+
520
+ ### Flags
521
+
522
+ | Flag | Áp dụng cho | Tác dụng |
523
+ | --- | --- | --- |
524
+ | `--site <id>` | pull, push | Site cần đồng bộ. Cần khi dùng `sb login` hoặc user token, trừ khi `sb.json` hay manifest đã nêu; key `wbk_` ngầm chỉ định site của nó |
525
+ | `--dir <path>` | pull, push | Thư mục gốc, mặc định `code` |
526
+ | `--force` | pull, push | Pull ghi đè mọi chỗ bạn đã sửa bằng bản của server. Push không gửi `expectedUpdatedAt`, nên **nội dung** của bạn thắng; nó vẫn gửi metadata hiện tại của server, nên không bao giờ hoàn tác những gì Code Dock đã đổi |
527
+ | `--dry-run` | push | In kế hoạch và không gửi gì |
528
+ | `--delete` | push | Xoá luôn các file trên server mà bản ở máy đã mất |
529
+
530
+ Xoá thứ mà bạn còn đang dựa vào là một quyết định, không bao giờ là mặc định —
531
+ vì thế `--delete` tồn tại, thay vì để push tự suy ra.
532
+
533
+ ### Nó nói chuyện với API nào
534
+
535
+ - **`SB_TOKEN` bắt đầu bằng `wbk_`** — một API key của merchant — dùng
536
+ `/api/v1/code-files`. Key ngầm chỉ định site của nó, nên không cần `--site`.
537
+ - **Mọi trường hợp khác** — credential do `sb login` lưu, hoặc một user token dán
538
+ tay — dùng `/api/sites/{siteId}/code-files`. Site lấy từ `--site`, nếu không
539
+ thì khoá `siteId` tuỳ chọn trong `sb.json`, nếu không nữa thì từ manifest. Hai
540
+ nguồn nêu hai site khác nhau thì bị từ chối trước khi gửi bất kỳ request nào.
541
+ - **`SB_TOKEN` bắt đầu bằng `sbdk_`** bị từ chối trước khi gửi bất kỳ request nào.
542
+ Deploy key thuộc về một tổ chức developer và chỉ chạm tới các route version của
543
+ app nó; code của site là nội dung của merchant.
544
+
545
+ Một key `wbk_` mà manifest **không** có file nào trùng với danh sách của server bị
546
+ từ chối vì thuộc về site khác, nên một key CI không thể ghi file của site A lên
547
+ site B. `--force` bỏ qua điều đó.
548
+
549
+ ### CI cho `sb code`
550
+
551
+ Tạo một API key trong **Settings → API keys** của merchant với `codefiles.write`
552
+ (thêm `pages.read` nếu CI tạo file trong một thư mục trang mà manifest chưa
553
+ biết), rồi lưu nó thành secret `SB_TOKEN` của CI:
554
+
555
+ ```bash
556
+ SB_TOKEN="$SB_API_KEY" sb code push
557
+ ```
558
+
559
+ Key không có `codefiles.write` bị server từ chối và không có gì được ghi.
560
+ `codefiles.write` nghĩa là "được thêm script vào mọi trang đã publish", nên chỉ
561
+ cấp cho key khi pipeline thật sự cần. Hãy commit `code/`, kể cả manifest.
562
+
563
+ ### Mã thoát
564
+
565
+ `0` khi mọi thao tác đã lên kế hoạch đều thành công. `1` khi có gì đó bị từ chối
566
+ hoặc bỏ qua trái ý server: một `409`, lỗi validate, lỗi biên dịch của file
567
+ `.scss`/`.ts` vừa push, một file chứa byte NUL, một lần pull
568
+ đã giữ lại file mà cả bạn và server cùng sửa, key `sbdk_`, site không khớp, hoặc
569
+ token bị từ chối. Một chỗ sửa ở máy đang chờ mà pull nhìn thấy, hoặc một file đã
570
+ theo dõi bị mất ở máy khi không có `--delete`, được in ra và **không** phải lỗi.
571
+
572
+ ---
573
+
574
+ ## `sb agent` — trợ lý của editor bằng tài khoản của bạn
575
+
576
+ ```
577
+ sb login
578
+ sb agent
579
+ ```
580
+
581
+ Rồi trong editor: **Trợ lý AI → Cài đặt → Nhà cung cấp → "Claude Code (sb
582
+ agent)"** (hoặc "Codex (sb agent)"). Mỗi lượt trợ lý cần sẽ chạy `claude` / `codex` ngay trên
583
+ máy này, bằng gói subscription bạn đang đăng nhập, rồi gửi câu trả lời về trình
584
+ duyệt. Không cần API key, không tốn credit của nền tảng. Để lệnh chạy trong lúc
585
+ làm việc; Ctrl-C để dừng, mất mạng thì nó tự kết nối lại.
586
+
587
+ - Chạy trên macOS và Linux. Chưa hỗ trợ Windows (shim `.cmd` của npm không chạy
588
+ được nếu không qua shell, mà shell thì sẽ phân tích lại prompt).
589
+ - Cài và đăng nhập CLI trước (`claude`, hoặc `codex login`).
590
+ - Admin nền tảng phải bật tuỳ chọn này (Admin → Cài đặt); khi chưa bật,
591
+ `sb agent` dừng và nói rõ lý do.
592
+ - Claude Code chạy với mọi tool có sẵn đã tắt. **Job Codex bị từ chối trừ khi
593
+ bạn chạy `sb agent --codex`**: Codex đọc được file trên máy này qua lệnh shell
594
+ chỉ-đọc, prompt chứa nội dung trang do người cùng sửa site viết, và nhà cung
595
+ cấp là cài đặt của SITE mà ai trong số họ cũng đổi được — nên Codex có chạy
596
+ trên máy bạn hay không là quyết định của bạn, không phải của họ. Mỗi lượt đều
597
+ in ra site mà nó chạy cho.
598
+ - CLI chỉ nhận một env đã lọc: secret trong shell của bạn và API key của các hãng
599
+ không được truyền vào.
600
+
601
+ ---
602
+
401
603
  ## CI
402
604
 
403
605
  User access token sẽ hết hạn, nên không hợp cho pipeline. Hãy dùng **deploy key**:
@@ -406,7 +608,7 @@ User access token sẽ hết hạn, nên không hợp cho pipeline. Hãy dùng *
406
608
  hoặc admin mới làm được), rồi tạo một key. Secret bắt đầu bằng `sbdk_` và chỉ
407
609
  hiện **một lần**.
408
610
  2. Lưu nó thành secret `SB_TOKEN` của CI. `sb dev` và `sb deploy` nhận nó y như
409
- một token dán tay.
611
+ một token dán tay. (`sb code` thì không: xem công thức CI riêng của nó.)
410
612
 
411
613
  ```bash
412
614
  SB_TOKEN="$SB_DEPLOY_KEY" sb deploy
@@ -429,18 +631,23 @@ Hai biến do chính CLI đọc:
429
631
 
430
632
  | Biến | Lệnh cần nó | Mặc định |
431
633
  | --- | --- | --- |
432
- | `SB_TOKEN` | `sb dev`, `sb deploy` — tuỳ chọn | không có — dùng credential của `sb login`; thiếu cả hai thì cả hai lệnh dừng và nhắc `sb login` |
634
+ | `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` |
433
635
  | `SB_API` | mọi lệnh trừ `sb init` | `http://localhost:8080` |
434
636
 
435
- `SB_TOKEN`, khi được đặt, **ghi đè** credential đã lưu của `sb login`. Nó là
436
- token của **tổ chức sở hữu app** — đúng cái user access token bạn sẽ đặt vào
437
- header `Authorization: Bearer` khi gọi các route theo tổ chức. Nó **không phải**
438
- app token dạng `wba_`; loại đó thuộc về một install, và một install không thể
439
- sửa chính cái app mà nó là install của.
637
+ `SB_TOKEN`, khi được đặt, **ghi đè** credential đã lưu của `sb login`. Với
638
+ `sb dev` và `sb deploy`, nó là token của **tổ chức sở hữu app** — đúng cái user
639
+ access token bạn sẽ đặt vào header `Authorization: Bearer` khi gọi các route theo
640
+ tổ chức. Nó **không phải** app token dạng `wba_`; loại đó thuộc về một install,
641
+ và một install không thể sửa chính cái app mà nó là install của. Với `sb code`,
642
+ nó là user token có quyền truy cập site, hoặc một API key `wbk_` của merchant
643
+ (xem [`sb code`](#sb-code--file-code-dock-của-bạn-dưới-dạng-một-thư-mục)).
440
644
 
441
- Token dán tay không tự gia hạn được. Khi server từ chối nó, CLI báo
645
+ Token dán tay không tự gia hạn được, và CLI không bao giờ thử refresh nó. Khi
646
+ server từ chối một token dán tay, `sb dev` và `sb deploy` báo
442
647
  `SB_TOKEN was refused: it has expired or been revoked` và chỉ bạn sang
443
- `sb login` — nó không bao giờ thử refresh token đó.
648
+ `sb login`. `sb code` thì báo
649
+ ``Not signed in, or the token was refused (expired or revoked). Run `sb login`, or set SB_TOKEN to an API key (wbk_…) with codefiles.read / codefiles.write from Settings -> API keys.``
650
+ và không bao giờ gợi ý deploy key, vì `sb code` từ chối deploy key.
444
651
 
445
652
  Ba biến còn lại do app mà `sb init` sinh ra tự đọc, từ `.env` của chính nó:
446
653
 
@@ -461,7 +668,11 @@ Ngoài hai giá trị đó là bug của chính công cụ, và khi đó nó in
461
668
  một công cụ tạo khung mà nuốt lỗi của chính mình là công cụ không ai báo lỗi
462
669
  được.
463
670
 
464
- Các mã lỗi từ server mà CLI dịch lại thay vì bê nguyên văn:
671
+ `sb dev` và `sb deploy` dịch lại các mã lỗi từ server trong bảng dưới đây thay vì
672
+ bê nguyên văn. `sb code` có những câu từ chối riêng: câu về token bị từ chối nằm
673
+ ở phía trên, và mục
674
+ [`sb code`](#sb-code--file-code-dock-của-bạn-dưới-dạng-một-thư-mục) mô tả lệnh
675
+ này.
465
676
 
466
677
  | Mã từ server | `sb` nói gì thay vào đó |
467
678
  | --- | --- |
@@ -492,7 +703,6 @@ Nói ra để bạn tính đường trước, thay vì tự phát hiện giữa
492
703
  thể là hot reload chừng nào code island còn được phục vụ từ object store của
493
704
  nền tảng.
494
705
  - **`sb deploy` không nguyên tử**, một cách có chủ đích.
495
- - **Chưa có trên npm.**
496
706
 
497
707
  ---
498
708
 
@@ -503,6 +713,7 @@ Nói ra để bạn tính đường trước, thay vì tự phát hiện giữa
503
713
  | `Not signed in. Run sb login …` | `dev`/`deploy` không có credential nào | `sb login`, hoặc đặt `SB_TOKEN` |
504
714
  | `Your sb login has expired or was revoked.` | Phiên đã lưu đã kết thúc (đăng xuất mọi nơi, hoặc hết hạn) | Chạy lại `sb login` |
505
715
  | `SB_TOKEN was refused: …` | Token dán tay đã hết hạn hoặc bị thu hồi | `sb login`, hoặc lấy token mới |
716
+ | `Not signed in, or the token was refused …` | `sb code` không có credential nào, `SB_TOKEN` dán tay bị từ chối, hoặc server vẫn trả 401 sau khi đã gia hạn `sb login` của bạn | `sb login`, hoặc đặt `SB_TOKEN` là một API key `wbk_` có `codefiles.read` / `codefiles.write` từ Settings → API keys |
506
717
  | `No sb.json here.` | Chạy sai thư mục | `cd` về đúng thư mục chứa `sb.json` |
507
718
  | `sb.json is missing "versionId".` | Thiếu hoặc để rỗng một khoá bắt buộc | Điền đủ cả năm khoá bắt buộc |
508
719
  | `unknown command: …` | Gõ nhầm, hoặc `dist/` đã cũ | `npm run build --workspace cli` |