sbuilder-mcp 0.23.0 → 0.24.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/CHANGELOG.md CHANGED
@@ -6,6 +6,28 @@ All notable changes to this project are documented in this file.
6
6
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
7
7
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
8
8
 
9
+ ## [0.24.0] - 2026-09-10
10
+
11
+ ### Added
12
+ - sb_template_use now fills a picture slot in a built-in layout with a real image from the site's own media library, preferring a landscape image where the layout wants one, instead of leaving a placeholder note asking the caller to add one by hand.
13
+ - sb_templates gained a new built-in layout, sb_gallery, a row of up to six images pulled from the site's media library.
14
+ - The API catalog gained three operations: `POST /api/media/{siteId}/from-url` (reachable through sb_api_call as well as sb_media_upload), `GET /api/sites/{siteId}/forms/submissions`, and `POST /api/sites/{siteId}/translations/review/bulk`.
15
+
16
+ ### Changed
17
+ - sb_template_use no longer repeats a photo already placed on the page: each picture slot it fills draws only from images the page is not already showing, so a hero added first and a gallery added second no longer show the same image twice.
18
+ - sb_template_use now reports `images_available` (the count of unused images in the site's library) in its dry-run preview, and names sb_media_upload in its result when the site's library has no images to draw from.
19
+
20
+ ## [0.23.1] - 2026-09-10
21
+
22
+ ### Added
23
+ - sb_media_upload now asks the platform to fetch a URL source itself (`POST /api/media/{siteId}/from-url`) before falling back to downloading and re-uploading the bytes, so the file makes one network hop instead of two and its content type comes from the origin's own answer instead of being guessed from a header or extension.
24
+
25
+ ### Changed
26
+ - sb_media_upload now raises rather than working around it when the platform refuses a URL source as `remote_blocked` (an address outside the public internet, such as a private range or the cloud metadata endpoint), since fetching that same URL from this server's own machine would defeat the platform's guard rather than respect it.
27
+
28
+ ### Fixed
29
+ - sb_media_upload now reports the platform's own reason when it cannot reach a URL source (for example, "www.blender.org answered 404") instead of the generic "the platform could not read it".
30
+
9
31
  ## [0.23.0] - 2026-09-10
10
32
 
11
33
  ### Added
package/CHANGELOG.vi.md CHANGED
@@ -6,6 +6,28 @@ Mọi thay đổi đáng chú ý của dự án được ghi lại trong file n
6
6
  Định dạng dựa trên [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
7
7
  và dự án tuân theo [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
8
8
 
9
+ ## [0.24.0] - 2026-09-10
10
+
11
+ ### Added
12
+ - sb_template_use giờ lấp ô chứa ảnh trong một layout dựng sẵn bằng ảnh thật từ thư viện media của chính site, ưu tiên ảnh nằm ngang khi layout cần, thay vì để lại một ghi chú yêu cầu người gọi tự thêm ảnh.
13
+ - sb_templates có thêm một layout dựng sẵn mới, sb_gallery, một hàng tối đa sáu ảnh lấy từ thư viện media của site.
14
+ - Catalog API có thêm ba operation: `POST /api/media/{siteId}/from-url` (gọi được qua sb_api_call cũng như qua sb_media_upload), `GET /api/sites/{siteId}/forms/submissions`, và `POST /api/sites/{siteId}/translations/review/bulk`.
15
+
16
+ ### Changed
17
+ - sb_template_use giờ không còn lặp lại một ảnh đã đặt trên trang: mỗi ô chứa ảnh nó lấp chỉ lấy từ những ảnh trang chưa hiển thị, nên một hero thêm trước và một dải ảnh thêm sau không còn hiện cùng một ảnh hai lần.
18
+ - sb_template_use giờ báo `images_available` (số ảnh chưa dùng trong thư viện site) trong bản xem trước dry-run, và nêu tên sb_media_upload trong kết quả khi thư viện site không có ảnh nào để lấy.
19
+
20
+ ## [0.23.1] - 2026-09-10
21
+
22
+ ### Added
23
+ - sb_media_upload giờ yêu cầu chính nền tảng tải nguồn URL trước (`POST /api/media/{siteId}/from-url`) rồi mới lùi về cách cũ là tải xuống rồi upload lại, nên file chỉ đi một chặng mạng thay vì hai, và content type được lấy từ câu trả lời của chính nguồn thay vì đoán từ header hay phần mở rộng.
24
+
25
+ ### Changed
26
+ - sb_media_upload giờ báo lỗi thay vì tìm cách lách khi nền tảng từ chối một nguồn URL với mã `remote_blocked` (địa chỉ không thuộc internet công khai, như dải private hoặc endpoint metadata của cloud), vì việc tự tải URL đó từ máy chạy server này sẽ phá vỡ lớp bảo vệ của nền tảng thay vì tuân theo nó.
27
+
28
+ ### Fixed
29
+ - sb_media_upload giờ báo đúng lý do của chính nền tảng khi không tải được một nguồn URL (ví dụ "www.blender.org answered 404") thay vì thông báo chung "the platform could not read it".
30
+
9
31
  ## [0.23.0] - 2026-09-10
10
32
 
11
33
  ### Added
@@ -1,8 +1,8 @@
1
1
  export const SWAGGER_SOURCE = {
2
- "operations": 495,
2
+ "operations": 498,
3
3
  "definitions": 104,
4
- "bodyCarrying": 177,
5
- "bodyUndescribed": 63,
4
+ "bodyCarrying": 178,
5
+ "bodyUndescribed": 64,
6
6
  "generatedFrom": "server/docs/swagger.json"
7
7
  };
8
8
  export const API_OPERATIONS = [
@@ -1882,6 +1882,34 @@ export const API_OPERATIONS = [
1882
1882
  "bodyRef": null,
1883
1883
  "credential": "siteScoped"
1884
1884
  },
1885
+ {
1886
+ "id": "post:/api/media/{siteId}/from-url",
1887
+ "method": "POST",
1888
+ "path": "/api/media/{siteId}/from-url",
1889
+ "tags": [
1890
+ "media"
1891
+ ],
1892
+ "summary": "Upload media by URL, fetched server-side",
1893
+ "params": [
1894
+ {
1895
+ "name": "siteId",
1896
+ "in": "path",
1897
+ "required": true,
1898
+ "type": "string",
1899
+ "description": "Site ID"
1900
+ },
1901
+ {
1902
+ "name": "body",
1903
+ "in": "body",
1904
+ "required": true,
1905
+ "type": "object",
1906
+ "description": "url, and optionally folderId and name"
1907
+ }
1908
+ ],
1909
+ "bodyDescribed": false,
1910
+ "bodyRef": null,
1911
+ "credential": "siteScoped"
1912
+ },
1885
1913
  {
1886
1914
  "id": "get:/api/notifications",
1887
1915
  "method": "GET",
@@ -6653,6 +6681,41 @@ export const API_OPERATIONS = [
6653
6681
  "bodyRef": null,
6654
6682
  "credential": "siteScoped"
6655
6683
  },
6684
+ {
6685
+ "id": "get:/api/sites/{siteId}/forms/submissions",
6686
+ "method": "GET",
6687
+ "path": "/api/sites/{siteId}/forms/submissions",
6688
+ "tags": [
6689
+ "forms"
6690
+ ],
6691
+ "summary": "Search a site's responses across all of its forms",
6692
+ "params": [
6693
+ {
6694
+ "name": "siteId",
6695
+ "in": "path",
6696
+ "required": true,
6697
+ "type": "string",
6698
+ "description": "Site id"
6699
+ },
6700
+ {
6701
+ "name": "q",
6702
+ "in": "query",
6703
+ "required": false,
6704
+ "type": "string",
6705
+ "description": "Folded substring over every answer"
6706
+ },
6707
+ {
6708
+ "name": "limit",
6709
+ "in": "query",
6710
+ "required": false,
6711
+ "type": "integer",
6712
+ "description": "Page size (max 200)"
6713
+ }
6714
+ ],
6715
+ "bodyDescribed": false,
6716
+ "bodyRef": null,
6717
+ "credential": "siteScoped"
6718
+ },
6656
6719
  {
6657
6720
  "id": "get:/api/sites/{siteId}/forms/{id}",
6658
6721
  "method": "GET",
@@ -12500,6 +12563,19 @@ export const API_OPERATIONS = [
12500
12563
  "bodyRef": null,
12501
12564
  "credential": "siteScoped"
12502
12565
  },
12566
+ {
12567
+ "id": "post:/api/sites/{siteId}/translations/review/bulk",
12568
+ "method": "POST",
12569
+ "path": "/api/sites/{siteId}/translations/review/bulk",
12570
+ "tags": [
12571
+ "translations"
12572
+ ],
12573
+ "summary": "Approve many machine-written values at once",
12574
+ "params": [],
12575
+ "bodyDescribed": false,
12576
+ "bodyRef": null,
12577
+ "credential": "siteScoped"
12578
+ },
12503
12579
  {
12504
12580
  "id": "delete:/api/sites/{siteId}/trash",
12505
12581
  "method": "DELETE",
@@ -18195,8 +18195,7 @@ export const ELEMENTS = {
18195
18195
  "overflow": "hidden"
18196
18196
  },
18197
18197
  "config": {
18198
- "defaultNum": 1,
18199
- "activeButton": ""
18198
+ "defaultNum": 1
18200
18199
  },
18201
18200
  "specials": {
18202
18201
  "button": true
@@ -19100,7 +19099,6 @@ export const ELEMENTS = {
19100
19099
  "quantity_default",
19101
19100
  "toggle_button",
19102
19101
  "defaultNum",
19103
- "activeButton",
19104
19102
  "quantityButtonId",
19105
19103
  "quantityInputId"
19106
19104
  ]
@@ -19179,7 +19177,6 @@ export const ELEMENTS = {
19179
19177
  "quantity_default",
19180
19178
  "toggle_button",
19181
19179
  "defaultNum",
19182
- "activeButton",
19183
19180
  "quantityButtonId",
19184
19181
  "quantityInputId",
19185
19182
  "width_select",
@@ -1,10 +1,10 @@
1
1
  export const SHAPE_SOURCE = {
2
- "writeOperations": 216,
3
- "shaped": 166,
4
- "fromHandlers": 166,
2
+ "writeOperations": 218,
3
+ "shaped": 168,
4
+ "fromHandlers": 168,
5
5
  "fromSwaggerOnly": 0,
6
6
  "withReadOnly": 26,
7
- "structsRead": 1647
7
+ "structsRead": 1649
8
8
  };
9
9
  export const REQUEST_SHAPES = {
10
10
  "post:/api/sites/{siteId}/api-keys": {
@@ -3669,6 +3669,24 @@ export const REQUEST_SHAPES = {
3669
3669
  "source": "go",
3670
3670
  "goType": "(inline)"
3671
3671
  },
3672
+ "post:/api/media/{siteId}/from-url": {
3673
+ "fields": [
3674
+ {
3675
+ "name": "url",
3676
+ "type": "string"
3677
+ },
3678
+ {
3679
+ "name": "folderId",
3680
+ "type": "string"
3681
+ },
3682
+ {
3683
+ "name": "name",
3684
+ "type": "string"
3685
+ }
3686
+ ],
3687
+ "source": "go",
3688
+ "goType": "fromURLRequest"
3689
+ },
3672
3690
  "put:/api/sites/{siteId}/media/assets/{assetId}": {
3673
3691
  "fields": [
3674
3692
  {
@@ -6785,6 +6803,33 @@ export const REQUEST_SHAPES = {
6785
6803
  "source": "go",
6786
6804
  "goType": "(inline)"
6787
6805
  },
6806
+ "post:/api/sites/{siteId}/translations/review/bulk": {
6807
+ "fields": [
6808
+ {
6809
+ "name": "locale",
6810
+ "type": "string"
6811
+ },
6812
+ {
6813
+ "name": "Entries",
6814
+ "type": "object"
6815
+ },
6816
+ {
6817
+ "name": "all",
6818
+ "type": "boolean",
6819
+ "note": "All asks for the whole match set instead of a list."
6820
+ },
6821
+ {
6822
+ "name": "search",
6823
+ "type": "string"
6824
+ },
6825
+ {
6826
+ "name": "entity",
6827
+ "type": "string"
6828
+ }
6829
+ ],
6830
+ "source": "go",
6831
+ "goType": "(inline)"
6832
+ },
6788
6833
  "post:/api/sites/{siteId}/translations/auto": {
6789
6834
  "fields": [
6790
6835
  {
@@ -1,4 +1,29 @@
1
1
  import { toSpecs } from './importmap.js';
2
+ /**
3
+ * A REAL IMAGE, from the site's own library, or nothing.
4
+ *
5
+ * The instinct a pattern library invites is a placeholder — a grey box, a stock
6
+ * photo keyed off a word — and both are worse than an empty slot. This repo
7
+ * already records why stock is not a source: `loremflickr` answered
8
+ * "kids,clothing" with a cat statue and a photo of an adult, and ten photos from
9
+ * ten sources read as a scrape. A grey box reads as unfinished, which it is.
10
+ *
11
+ * The library is the honest source: those are the merchant's own images, already
12
+ * uploaded, already the right subject. Measured on a live store: 164 assets, 50
13
+ * of them images. A `want` of "landscape" is a shape request, not a subject one —
14
+ * a hero panel filled with a portrait crop is the aspect-ratio mistake rule 6 is
15
+ * about — and it degrades to any unused image rather than to none.
16
+ */
17
+ function pick(pool, used, want) {
18
+ if (!pool?.length)
19
+ return null;
20
+ const free = pool.filter((m) => m.url && !used.has(m.url));
21
+ const wide = free.filter((m) => (m.width ?? 0) > (m.height ?? 0));
22
+ const chosen = (want === 'landscape' && wide[0]) || free[0] || null;
23
+ if (chosen)
24
+ used.add(chosen.url);
25
+ return chosen;
26
+ }
2
27
  /**
3
28
  * THE LOOK A PAGE HAS BEFORE IT HAS ANYTHING, and it is not nothing.
4
29
  *
@@ -47,15 +72,17 @@ const row = (children, wrap = false) => ({
47
72
  const h = (text, level = 2) => ({ kind: 'heading', level, text });
48
73
  const p = (text) => ({ kind: 'text', text });
49
74
  const cta = (text, href = '#') => ({ kind: 'button', variant: 'cta', text, href });
50
- const img = (alt) => ({
51
- // A PLACEHOLDER WITH NO SRC IS DROPPED by the mapper, which is right — an
52
- // empty frame is not content. So a pattern that wants a picture says so in
53
- // words and the caller points sb_set at it, rather than the page shipping a
54
- // grey box nobody chose.
55
- kind: 'image',
56
- src: '',
57
- alt,
58
- });
75
+ /**
76
+ * The picture slot: a real image when the site has one, and WORDS when it does
77
+ * not.
78
+ *
79
+ * An image with no src is dropped by the mapper, which is right — an empty frame
80
+ * is not content — so the fallback is a sentence naming the call that fills it,
81
+ * not a grey box. A grey box reads as unfinished because it is.
82
+ */
83
+ const img = (m, alt, hint) => m
84
+ ? { kind: 'image', src: m.url, alt: m.name ?? alt }
85
+ : { kind: 'text', text: hint };
59
86
  /**
60
87
  * The set. Deliberately small and deliberately ordinary: these are the bands
61
88
  * every commercial page is built from, and a library that tried to be clever
@@ -66,12 +93,21 @@ export const LAYOUT_PATTERNS = [
66
93
  id: 'sb_hero_split',
67
94
  name: 'Hero — hai cột',
68
95
  use: 'Mở đầu trang: tiêu đề, một câu, nút hành động bên trái; chỗ cho ảnh bên phải',
69
- build: (t) => section([
70
- row([
71
- { kind: 'group', direction: 'column', children: [h('Tiêu đề chính', 1), p('Một câu nói rõ bạn bán gì và cho ai.'), cta('Mua ngay')] },
72
- { kind: 'group', direction: 'column', children: [p('Đặt ảnh vào đây — sb_add một image, hoặc sb_set src lên khối này.')] },
73
- ]),
74
- ], t),
96
+ build: (t, pool) => {
97
+ const used = new Set();
98
+ return section([
99
+ row([
100
+ { kind: 'group', direction: 'column', children: [h('Tiêu đề chính', 1), p('Một câu nói rõ bạn bán gì và cho ai.'), cta('Mua ngay')] },
101
+ {
102
+ kind: 'group',
103
+ direction: 'column',
104
+ children: [
105
+ img(pick(pool, used, 'landscape'), 'Ảnh mở đầu', 'Chưa có ảnh nào trong thư viện — sb_media_upload một URL, rồi sb_set src lên khối này.'),
106
+ ],
107
+ },
108
+ ]),
109
+ ], t);
110
+ },
75
111
  },
76
112
  {
77
113
  id: 'sb_hero_centered',
@@ -110,6 +146,31 @@ export const LAYOUT_PATTERNS = [
110
146
  use: 'Một câu và một nút, đặt cuối trang hoặc giữa hai band nội dung',
111
147
  build: (t) => section([h('Sẵn sàng bắt đầu?'), p('Một câu nhắc lại lời hứa chính.'), cta('Mua ngay')], t, { alignItems: 'center', textAlign: 'center' }, { alignItems: 'center' }),
112
148
  },
149
+ {
150
+ id: 'sb_gallery',
151
+ name: 'Dải ảnh',
152
+ use: 'Một hàng ảnh cuốn dòng, lấy từ thư viện ảnh của chính site',
153
+ build: (t, pool) => {
154
+ const used = new Set();
155
+ const shots = [];
156
+ // SIX IS A WALL, THREE IS A ROW. Bounded because a gallery is a design
157
+ // decision and not a dump of the library — a merchant with 164 assets does
158
+ // not want 164 of them in one band.
159
+ for (let i = 0; i < 6; i += 1) {
160
+ const m = pick(pool, used, 'any');
161
+ if (!m)
162
+ break;
163
+ shots.push({ kind: 'image', src: m.url, alt: m.name ?? 'Ảnh' });
164
+ }
165
+ if (shots.length === 0) {
166
+ return section([
167
+ h('Thư viện ảnh'),
168
+ p('Chưa có ảnh nào trong thư viện — sb_media_upload một URL rồi dựng lại dải này.'),
169
+ ], t);
170
+ }
171
+ return section([h('Thư viện ảnh'), row(shots, true)], t);
172
+ },
173
+ },
113
174
  {
114
175
  id: 'sb_faq',
115
176
  name: 'Câu hỏi thường gặp',
@@ -20,7 +20,7 @@ import { readinessGaps, READINESS_NOTICE } from '../domains/site/readiness.js';
20
20
  import { gatherReadiness } from '../domains/site/readiness-fetch.js';
21
21
  import { globalWarning, restampPatches, RESPONSIVE_NOTICE } from '../domains/site/traps.js';
22
22
  import { catalogMatches, traitsFor } from '../catalog/element-search.js';
23
- import { LAYOUT_PATTERNS, PATTERN_BY_ID, THEME_TOKENS } from '../domains/site/patterns.js';
23
+ import { LAYOUT_PATTERNS, PATTERN_BY_ID, THEME_TOKENS, } from '../domains/site/patterns.js';
24
24
  import { tokensFromPage } from '../domains/site/importmap.js';
25
25
  import { middleEnd } from '../domains/site/traps.js';
26
26
  import { applyPatches } from '../core/patch.js';
@@ -703,7 +703,42 @@ export function registerPageTools(server, ctx) {
703
703
  // goes on following the theme instead of freezing today's hex into it.
704
704
  const fromTheme = Object.keys(read).length === 0;
705
705
  const tokens = fromTheme ? THEME_TOKENS : read;
706
- const spec = pattern.build(tokens);
706
+ // REAL IMAGES, from the site's own library. The instinct a pattern
707
+ // library invites is a placeholder — a grey box or a stock photo keyed
708
+ // off a word — and this repo already records why the second is not a
709
+ // source (`loremflickr` answered "kids,clothing" with a cat statue). The
710
+ // merchant's own uploads are the honest answer, and a failure to read
711
+ // them costs the picture, never the band.
712
+ let pool = [];
713
+ try {
714
+ const listed = (await request({
715
+ base: ctx.base,
716
+ method: 'GET',
717
+ path: `/api/sites/${encodeURIComponent(site_id)}/media?limit=50`,
718
+ token: siteToken(ctx),
719
+ fetchImpl: ctx.fetchImpl,
720
+ }));
721
+ const rows = listed.media ?? listed.assets ?? [];
722
+ pool = rows
723
+ .filter((m) => m.mediaType === 'image' && typeof m.url === 'string' && m.state !== 'deleted')
724
+ .map((m) => ({
725
+ url: String(m.url),
726
+ name: typeof m.name === 'string' ? m.name : undefined,
727
+ width: typeof m.width === 'number' ? m.width : undefined,
728
+ height: typeof m.height === 'number' ? m.height : undefined,
729
+ }));
730
+ }
731
+ catch {
732
+ // A library that cannot be read leaves the picture slot saying so.
733
+ }
734
+ // WHAT THE PAGE IS ALREADY SHOWING comes out of the pool. Each call
735
+ // starts its own selection, so a hero added first and a gallery added
736
+ // second both reached for the same best landscape — and the page then
737
+ // showed one photo twice, which reads as a mistake because it is one.
738
+ const onPage = new Set(Object.values(doc.doc.nodes)
739
+ .map((n) => n.specials?.src)
740
+ .filter((v) => typeof v === 'string' && v.length > 0));
741
+ const spec = pattern.build(tokens, pool.filter((m) => !onPage.has(m.url)));
707
742
  if (!spec) {
708
743
  throw new Error(`sbuilder: the built-in "${template_id}" produced nothing to add.`);
709
744
  }
@@ -715,6 +750,7 @@ export function registerPageTools(server, ctx) {
715
750
  nodes: ids.length,
716
751
  into: page_id,
717
752
  tokens_from: fromTheme ? "this site's theme — the page has no look of its own yet" : 'this page',
753
+ images_available: pool.length,
718
754
  note: 'Composed against THIS page\'s tokens, not copied — the same heading ink, button ' +
719
755
  'fill and section padding the page already uses. Pass dry_run:false to add it.',
720
756
  });
@@ -726,6 +762,13 @@ export function registerPageTools(server, ctx) {
726
762
  nodes: ids.length,
727
763
  into: page_id,
728
764
  rev: doc.rev,
765
+ ...(pool.length === 0
766
+ ? {
767
+ images: 'This site has no images in its library, so any picture slot in this band says ' +
768
+ 'so in words rather than showing a grey box. sb_media_upload takes a URL and ' +
769
+ 'the platform fetches it server-side.',
770
+ }
771
+ : {}),
729
772
  ...(fromTheme
730
773
  ? {
731
774
  tokens_from: "this site's theme — the page had no heading, button or section to read a look " +
@@ -41,8 +41,87 @@ function typeForName(name) {
41
41
  const dot = name.lastIndexOf('.');
42
42
  return dot < 0 ? '' : (TYPE_BY_EXT[name.slice(dot).toLowerCase()] ?? '');
43
43
  }
44
+ /**
45
+ * Ask the platform to fetch the URL itself.
46
+ *
47
+ * Returns the asset when the server took it, or null when the ROUTE is not
48
+ * there — an older deployment — which is the one case the caller may answer by
49
+ * downloading the bytes and posting them the old way.
50
+ *
51
+ * A REFUSAL IS TERMINAL, and that is a security rule rather than tidiness. The
52
+ * server refuses an address that is not on the public internet (`remote_blocked`
53
+ * — loopback, private ranges, the cloud metadata endpoint), and a client that
54
+ * answered by fetching that same URL from its OWN machine and uploading the
55
+ * bytes would walk straight around the guard. The agent's network is not the
56
+ * server's, but "the caller does it instead" is exactly the bypass the check
57
+ * exists to prevent, so the error is raised rather than worked around.
58
+ */
59
+ async function fromUrl(ctx, siteId, url, source) {
60
+ const doFetch = ctx.fetchImpl ?? fetch;
61
+ const res = await doFetch(`${ctx.base.replace(/\/$/, '')}/api/media/${encodeURIComponent(siteId)}/from-url`, {
62
+ method: 'POST',
63
+ headers: {
64
+ Authorization: `Bearer ${siteToken(ctx)}`,
65
+ 'Content-Type': 'application/json',
66
+ Accept: 'application/json',
67
+ ...identityHeaders(),
68
+ },
69
+ body: JSON.stringify({
70
+ url,
71
+ ...(source.name ? { name: source.name } : {}),
72
+ ...(source.folderId ? { folderId: source.folderId } : {}),
73
+ }),
74
+ });
75
+ // 404/405 is "this build has no such route". Anything else is an answer.
76
+ if (res.status === 404 || res.status === 405)
77
+ return null;
78
+ const raw = await res.text();
79
+ let parsed = {};
80
+ try {
81
+ parsed = raw ? JSON.parse(raw) : {};
82
+ }
83
+ catch {
84
+ // A build that routes this path to something else entirely. Fall back rather
85
+ // than reporting a parse error the caller cannot act on.
86
+ return null;
87
+ }
88
+ if (!res.ok) {
89
+ const env = (parsed ?? {});
90
+ if (env.code === 'remote_blocked') {
91
+ throw new ApiError(res.status, 'remote_blocked', `sbuilder: the platform refuses to fetch ${url} — it is not an address on the public ` +
92
+ 'internet. This server will not fetch it on the platform\'s behalf either: that would ' +
93
+ 'walk around the check rather than satisfy it. Give a public URL, or upload the file ' +
94
+ 'with a local path.');
95
+ }
96
+ if (env.code === 'remote_unreachable') {
97
+ // THE PLATFORM'S OWN REASON, not a sentence of ours over the top of it. It
98
+ // says which host answered what — a 404 is a wrong URL, a 403 is an origin
99
+ // refusing this fetcher, a timeout is worth retrying — and replacing that
100
+ // with "could not read it" sends all of them to check their network.
101
+ throw new ApiError(res.status, 'remote_unreachable', `sbuilder: the platform could not fetch ${url} — ${env.error ?? 'no reason given'}.`);
102
+ }
103
+ // Any OTHER refusal — 401, 403, 413, an unsupported type — is one the older
104
+ // path answers with its own, better-worded diagnosis. Let it try.
105
+ return null;
106
+ }
107
+ const body = (parsed ?? {});
108
+ return body.asset ?? parsed;
109
+ }
44
110
  export async function uploadMedia(ctx, siteId, source) {
45
111
  const doFetch = ctx.fetchImpl ?? fetch;
112
+ // THE SERVER'S OWN DOOR FIRST, when the source is a URL.
113
+ //
114
+ // `POST /api/media/{siteId}/from-url` fetches it where the platform already
115
+ // guards outbound requests, so the bytes make ONE hop instead of two and the
116
+ // content type is decided by the origin's own answer rather than reconstructed
117
+ // here. Everything below this is the older path, kept because a deployment
118
+ // without that route must still be able to upload — the same shape the partner
119
+ // -surface retry below has, and for the same reason.
120
+ if (source.url) {
121
+ const viaServer = await fromUrl(ctx, siteId, source.url, source);
122
+ if (viaServer)
123
+ return viaServer;
124
+ }
46
125
  let bytes;
47
126
  let filename;
48
127
  let declared = '';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sbuilder-mcp",
3
- "version": "0.23.0",
3
+ "version": "0.24.0",
4
4
  "description": "MCP server that designs and operates a Store Builder site — pages, data, theme and publish — through the platform's own API and live-edit protocol.",
5
5
  "mcpName": "io.github.vuluu2k/sbuilder-mcp",
6
6
  "type": "module",