sil-openclaw 0.2.1 → 0.2.3
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 +68 -1
- package/README.md +7 -7
- package/dist/lib/sil-client.js +228 -35
- package/dist/tools/catalog.js +167 -68
- package/dist/tools/identity.js +32 -11
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
All notable changes to the `sil-openclaw` plugin (npm `sil-openclaw`, ClawHub
|
|
3
|
+
All notable changes to the `sil-openclaw` plugin (npm `sil-openclaw`, ClawHub `@4gpts/sil`) are documented here. The format follows
|
|
4
4
|
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the project adheres
|
|
5
5
|
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
6
|
|
|
@@ -10,6 +10,73 @@ release (`clawhub package publish --changelog`). See [README](./README.md#releas
|
|
|
10
10
|
|
|
11
11
|
## [Unreleased]
|
|
12
12
|
|
|
13
|
+
## [0.2.3] - 2026-06-16
|
|
14
|
+
|
|
15
|
+
### Added
|
|
16
|
+
|
|
17
|
+
- **`sil_search` and `sil_product_get` now surface evaluate-before-buy detail.**
|
|
18
|
+
Beyond the purchasable variant, results carry — where the source provides them —
|
|
19
|
+
the product/variant `url` (the page to VIEW / learn more, distinct from the
|
|
20
|
+
buy-committing `checkout_url`), a short `description`, `media`, the product
|
|
21
|
+
`options` menu, the `seller` with its policy/info links (shipping & return/refund
|
|
22
|
+
policies, terms), and arbitrary `metadata`. The tool descriptions teach the three
|
|
23
|
+
distinct actions — VIEW (`url`), DIG IN (`seller.links`), BUY (`checkout_url`) — so
|
|
24
|
+
an agent can compare, answer "what's their return policy?", and hand back the right
|
|
25
|
+
link without conflating viewing with buying. (#23)
|
|
26
|
+
|
|
27
|
+
### Changed
|
|
28
|
+
|
|
29
|
+
- **`sil_search`: replaced the `ships_from` filter with a `local_merchants` boolean
|
|
30
|
+
bias.** It nudges shops based in the shopper's own country up the ranking,
|
|
31
|
+
best-effort — it does NOT restrict results to them (some local shops go
|
|
32
|
+
undetected, some non-local ones still appear). The agent passes no country: sil
|
|
33
|
+
resolves it server-side from the registered address. To actually surface local
|
|
34
|
+
stores, issue the `query` in the shopper's own language (a French query surfaces
|
|
35
|
+
French shops). (#20)
|
|
36
|
+
|
|
37
|
+
### Fixed
|
|
38
|
+
|
|
39
|
+
- **A catalog `422 source_rejected` is now classified non-retryable
|
|
40
|
+
`invalid_request`, not source-named `retryable`.** A real source *rejection*
|
|
41
|
+
(the source looked at the request and refused it — it can never succeed
|
|
42
|
+
unchanged) was falling through to `retryableFromBody`, and because the 422 body
|
|
43
|
+
carries a `source` it surfaced as the source-named `retryable` (outcome b) — a
|
|
44
|
+
false instruction that told the agent to retry a doomed request while the real
|
|
45
|
+
upstream cause was buried as a transient `detail`. Both catalog classifiers
|
|
46
|
+
(`classifySearchResponse` / `classifyLookupResponse`, backing `sil_search` /
|
|
47
|
+
`sil_product_get`) now special-case `422 → invalid_request` carrying the
|
|
48
|
+
upstream `{ error, message }`, mirroring the existing `400` arm. The narrowing
|
|
49
|
+
is exact: a `5xx`/`429 source_unavailable` stays `retryable`.
|
|
50
|
+
|
|
51
|
+
- **`sil_search` / `sil_product_get` surface `user_not_provisioned` as a distinct
|
|
52
|
+
`forbidden` outcome and clear the structurally-dead token**, so the next
|
|
53
|
+
`sil_register` re-onboards the user instead of short-circuiting to
|
|
54
|
+
`already_registered`. A `principal_mismatch` or unknown 403 reason stays
|
|
55
|
+
recoverable and is NOT cleared. (#21)
|
|
56
|
+
|
|
57
|
+
- **Catalog transient (5xx) failures now name the failed source** when the wire
|
|
58
|
+
carries one — "the catalog source X is temporarily unavailable; sil itself is
|
|
59
|
+
fine" — instead of mis-attributing one degraded source to sil being down, which
|
|
60
|
+
would make the agent abandon a healthy platform. (#19)
|
|
61
|
+
|
|
62
|
+
## [0.2.2] - 2026-06-13
|
|
63
|
+
|
|
64
|
+
### Fixed
|
|
65
|
+
|
|
66
|
+
- **`sil_register` no longer aborts registration on the first premature claim
|
|
67
|
+
`404`.** The claim poll's first tick fires seconds in, before the user opens
|
|
68
|
+
the auth URL — but the session row is created server-side only when they do, so
|
|
69
|
+
the endpoint returns `404` (`not_found`) until then. That first `not_found` was
|
|
70
|
+
grouped with the genuine terminals, killing registration before the human could
|
|
71
|
+
act. `not_found` is now a keep-polling state (like `pending`), bounded by the
|
|
72
|
+
30-min deadline: a session that never appears settles as `timeout`, never as
|
|
73
|
+
`not_found`. Terminality now lives at the poll loop, not the wire classifier. (#18)
|
|
74
|
+
- **`400` / unexpected non-2xx claim responses now fail fast** via a new distinct
|
|
75
|
+
`invalid_request` outcome, instead of riding the keep-polling `not_found` path
|
|
76
|
+
and spinning to a misleading `timeout`. Unreachable from this client today (the
|
|
77
|
+
plugin always sends a valid verifier), so it logs at WARN as contract-drift
|
|
78
|
+
insurance.
|
|
79
|
+
|
|
13
80
|
## [0.2.1] - 2026-06-11
|
|
14
81
|
|
|
15
82
|
### Changed
|
package/README.md
CHANGED
|
@@ -14,13 +14,13 @@
|
|
|
14
14
|
*You say what you want. Your agent searches the catalog, compares, and hands you a ready-to-buy link.*
|
|
15
15
|
*No store. No tabs. No forms.*
|
|
16
16
|
|
|
17
|
-
[](
|
|
18
|
-
[](https://github.com/Context4GPTs/sil-openclaw/blob/main/LICENSE)
|
|
18
|
+
[](https://www.npmjs.com/package/sil-openclaw)
|
|
19
19
|
[](https://clawhub.com)
|
|
20
20
|
[](https://github.com/Context4GPTs/sil-openclaw)
|
|
21
|
-
[](https://github.com/Context4GPTs/sil-openclaw/commits)
|
|
21
|
+
[](https://github.com/Context4GPTs/sil-openclaw/commits/main)
|
|
22
22
|
|
|
23
|
-
**[Website](https://4gpts.com)** · **[Changelog](
|
|
23
|
+
**[Website](https://4gpts.com)** · **[Changelog](https://github.com/Context4GPTs/sil-openclaw/blob/main/CHANGELOG.md)** · **[Security](https://github.com/Context4GPTs/sil-openclaw/blob/main/SECURITY.md)** · **[Follow on X](https://x.com/4gpts)**
|
|
24
24
|
|
|
25
25
|
---
|
|
26
26
|
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
|
|
31
31
|
```bash
|
|
32
32
|
# ClawHub (recommended)
|
|
33
|
-
openclaw plugins install clawhub
|
|
33
|
+
openclaw plugins install clawhub:@4gpts/sil
|
|
34
34
|
|
|
35
35
|
# npm
|
|
36
36
|
openclaw plugins install sil-openclaw
|
|
@@ -117,8 +117,8 @@ Namespaced `sil_*` so they never collide with other plugins. Your agent calls th
|
|
|
117
117
|
|
|
118
118
|
| Tool | What it does |
|
|
119
119
|
|---|---|
|
|
120
|
-
| `sil_search` | Search for purchasable products. A free-text `query` plus optional filters: `category`, `price_min`/`price_max`, `ship_to` (delivery destination — leave empty to ship to your registered address), `
|
|
121
|
-
| `sil_product_get` | Resolve `ids` you already hold to full products in UCP shape — description
|
|
120
|
+
| `sil_search` | Search for purchasable products. A free-text `query` plus optional filters: `category`, `price_min`/`price_max`, `ship_to` (delivery destination — leave empty to ship to your registered address), `condition` (`new` / `secondhand`), `available`, and `local_merchants` (best-effort bias toward shops in the user's own country). Results are **localized** to the shopper — issue the `query` in their own language for the best local results (e.g. search in French for French stores). Returns a ranked list of variants — each with `id`, `title`, `price`, `availability`, `checkout_url`, `source`, plus (where the source provides them) evaluate-before-buy detail: a product/variant `url` to view, a `description`, `media`, the product `options`, and the `seller`'s **shipping & return policy links** — and a pagination `cursor`. Location filters take ISO codes (`US`, `CA`), not place names. |
|
|
121
|
+
| `sil_product_get` | Resolve `ids` you already hold to full products in UCP shape — `description`, `options`, `media`, the `seller`'s shipping & return policy links, and the featured purchasable variant with fresh `price`, `availability`, `url` (the page to view), and `checkout_url` (the link that buys). Re-fetch right before buying; those values are point-in-time, not guarantees. Ids that no longer resolve come back in `not_found`. |
|
|
122
122
|
|
|
123
123
|
---
|
|
124
124
|
|
package/dist/lib/sil-client.js
CHANGED
|
@@ -10,10 +10,17 @@
|
|
|
10
10
|
* POST /api/v1/sessions/{id}/claim body { code_verifier }
|
|
11
11
|
* 200 { access_token, refresh_token, user:{id} } → success (EXACTLY once)
|
|
12
12
|
* 200 { status: "pending" } → pending
|
|
13
|
-
* 409 → already_claimed
|
|
14
|
-
* 410 → expired
|
|
13
|
+
* 409 → already_claimed (terminal)
|
|
14
|
+
* 410 → expired (terminal)
|
|
15
15
|
* 404 → not_found (wrong verifier
|
|
16
|
-
* ≡ unknown session, uniform)
|
|
16
|
+
* ≡ unknown session, uniform);
|
|
17
|
+
* the NORMAL pre-session early
|
|
18
|
+
* state — KEEP POLLING at the
|
|
19
|
+
* loop (non-terminal); only the
|
|
20
|
+
* deadline ends it (as timeout)
|
|
21
|
+
* 400 / other unexpected non-2xx → invalid_request (non-polling
|
|
22
|
+
* terminal — fail fast; a bad
|
|
23
|
+
* request can't be re-polled)
|
|
17
24
|
* 5xx / network / abort → retryable
|
|
18
25
|
*
|
|
19
26
|
* POST /api/v1/auth/refresh body { refresh_token }
|
|
@@ -134,7 +141,16 @@ const REQUEST_TIMEOUT_MS = 15_000;
|
|
|
134
141
|
* Classify a claim response from its HTTP status AND body. The discriminant for
|
|
135
142
|
* the two-200s split is the PRESENCE OF BOTH TOKENS in the body — never the
|
|
136
143
|
* status code. A 200 that is not a complete token pair is `pending` (the safe
|
|
137
|
-
* non-terminal landing)
|
|
144
|
+
* non-terminal landing). 409/410 are distinct terminals; 5xx is retryable.
|
|
145
|
+
*
|
|
146
|
+
* 404 is `not_found` ON THE WIRE — but it is the NORMAL pre-session early state
|
|
147
|
+
* (the session row is INSERTed server-side only when the user opens the auth
|
|
148
|
+
* URL), so its terminality lives at the LOOP, not here: `claimStep` keeps polling
|
|
149
|
+
* on `not_found` and only the 30-min deadline ends a never-appearing session (as
|
|
150
|
+
* `timeout`). A 400 / any other unexpected non-{200,409,410,404,5xx} status is a
|
|
151
|
+
* structurally-malformed request that re-polling can never fix, so it is its OWN
|
|
152
|
+
* non-polling terminal `invalid_request` (fail fast) rather than riding the
|
|
153
|
+
* keep-polling `not_found` path and spinning for the full deadline.
|
|
138
154
|
*
|
|
139
155
|
* Pure and exported — this is the highest-risk subtle branch, unit-tested in
|
|
140
156
|
* isolation (sil-client.test.ts).
|
|
@@ -149,9 +165,11 @@ export function classifyClaimResponse(status, body) {
|
|
|
149
165
|
if (status >= 500)
|
|
150
166
|
return { kind: "retryable" };
|
|
151
167
|
if (status !== 200) {
|
|
152
|
-
// 400 (malformed request) or any other unexpected
|
|
153
|
-
//
|
|
154
|
-
|
|
168
|
+
// 400 (malformed request) or any other unexpected non-2xx: a NON-polling
|
|
169
|
+
// terminal — re-polling can't fix a structurally-bad request, so fail fast
|
|
170
|
+
// and loud rather than spinning to the deadline. Distinct from the early
|
|
171
|
+
// 404 `not_found`, which DOES keep polling.
|
|
172
|
+
return { kind: "invalid_request" };
|
|
155
173
|
}
|
|
156
174
|
// 200 — classify by body shape. Both tokens required for a clean success;
|
|
157
175
|
// anything short of that is the safe non-terminal `pending`.
|
|
@@ -227,6 +245,12 @@ export function classifyIdentityResponse(status, body) {
|
|
|
227
245
|
* retry or re-register)
|
|
228
246
|
* 401 → unauthorized (the refresh trigger — the caller drives transparent
|
|
229
247
|
* refresh-and-retry-once via `refreshAndRetryOnce`, not terminal)
|
|
248
|
+
* 403 → forbidden (terminal-but-recoverable; carries the actionable reason
|
|
249
|
+
* `user_not_provisioned`/`principal_mismatch`, defaulting to the generic
|
|
250
|
+
* `"forbidden"` marker on an unexpected body — parity with
|
|
251
|
+
* `classifyIdentityResponse`). A 403 is NOT a 401: it surfaces as the
|
|
252
|
+
* shared forbidden envelope, never the false-transient `retryable`, and a
|
|
253
|
+
* valid-but-unprovisioned token gains nothing from a refresh.
|
|
230
254
|
* 5xx / other non-200 → retryable
|
|
231
255
|
* 200 → read `products` off sil-api's FLAT envelope (`{ ucp, products,
|
|
232
256
|
* pagination? }` — top level, no `result` wrapper), normalize, and require
|
|
@@ -240,7 +264,7 @@ export function classifyIdentityResponse(status, body) {
|
|
|
240
264
|
*
|
|
241
265
|
* Pure and exported — unit-tested in isolation like `classifyIdentityResponse`;
|
|
242
266
|
* conflating the empty-match success with a partial-200 false-green, or with the
|
|
243
|
-
* 400/401/5xx error classes, is the highest-risk subtle bug in this flow.
|
|
267
|
+
* 400/401/403/5xx error classes, is the highest-risk subtle bug in this flow.
|
|
244
268
|
*/
|
|
245
269
|
export function classifySearchResponse(status, body) {
|
|
246
270
|
if (status === 400) {
|
|
@@ -249,8 +273,22 @@ export function classifySearchResponse(status, body) {
|
|
|
249
273
|
}
|
|
250
274
|
if (status === 401)
|
|
251
275
|
return { kind: "unauthorized" };
|
|
276
|
+
if (status === 403)
|
|
277
|
+
return { kind: "forbidden", reason: extractForbiddenReason(body) };
|
|
278
|
+
// 422 source_rejected: the source LOOKED at this exact request and refused it —
|
|
279
|
+
// it can never succeed unchanged, so it is non-retryable invalid_request carrying
|
|
280
|
+
// the real upstream cause, NOT the source-named `retryable` the fallthrough below
|
|
281
|
+
// would produce (a 422 body names a `source`, so `retryableFromBody` would emit
|
|
282
|
+
// outcome (b) and tell the agent to retry a doomed request). This is the third
|
|
283
|
+
// arm of the 401-vs-403-vs-422 split: 401 refresh-and-retry, 403 forbidden
|
|
284
|
+
// (refresh won't help), 422 invalid_request (fix the request, don't retry).
|
|
285
|
+
// Matches EXACTLY 422 — a 5xx/429 source_unavailable stays `retryable` (outcome b).
|
|
286
|
+
if (status === 422) {
|
|
287
|
+
const { error, message } = extractApiError(body);
|
|
288
|
+
return { kind: "invalid_request", error, message };
|
|
289
|
+
}
|
|
252
290
|
if (status !== 200)
|
|
253
|
-
return
|
|
291
|
+
return retryableFromBody(body);
|
|
254
292
|
const result = extractSearchResult(body);
|
|
255
293
|
if (result === null)
|
|
256
294
|
return { kind: "retryable" };
|
|
@@ -268,6 +306,12 @@ export function classifySearchResponse(status, body) {
|
|
|
268
306
|
* which never arises on the lookup route; do not special-case it.)
|
|
269
307
|
* 401 → unauthorized (the refresh trigger — the caller drives transparent
|
|
270
308
|
* refresh-and-retry-once via `refreshAndRetryOnce`, parity with search)
|
|
309
|
+
* 403 → forbidden (terminal-but-recoverable; carries the actionable reason
|
|
310
|
+
* `user_not_provisioned`/`principal_mismatch`, defaulting to the generic
|
|
311
|
+
* `"forbidden"` marker on an unexpected body — parity with search and
|
|
312
|
+
* `classifyIdentityResponse`). A 403 surfaces as the shared forbidden
|
|
313
|
+
* envelope, never the false-transient `retryable`; it is NOT a 401, so no
|
|
314
|
+
* refresh is triggered.
|
|
271
315
|
* 5xx / other non-200 → retryable
|
|
272
316
|
* 200 → read `products` off sil-api's FLAT envelope (`{ ucp, products,
|
|
273
317
|
* messages? }` — top level, no `result` wrapper), normalize, and require
|
|
@@ -294,8 +338,20 @@ export function classifyLookupResponse(status, body) {
|
|
|
294
338
|
}
|
|
295
339
|
if (status === 401)
|
|
296
340
|
return { kind: "unauthorized" };
|
|
341
|
+
if (status === 403)
|
|
342
|
+
return { kind: "forbidden", reason: extractForbiddenReason(body) };
|
|
343
|
+
// 422 source_rejected: the twin seam of `classifySearchResponse` — sil-api emits
|
|
344
|
+
// 422 on the SHARED source layer backing both catalog routes, so a source-rejected
|
|
345
|
+
// lookup is the same non-retryable invalid_request, carrying the upstream cause,
|
|
346
|
+
// NOT the source-named `retryable` the fallthrough would produce. Same one error
|
|
347
|
+
// vocabulary across both catalog tools (see this fn's doc-comment). Matches EXACTLY
|
|
348
|
+
// 422 — a 5xx/429 source_unavailable stays `retryable` (outcome b).
|
|
349
|
+
if (status === 422) {
|
|
350
|
+
const { error, message } = extractApiError(body);
|
|
351
|
+
return { kind: "invalid_request", error, message };
|
|
352
|
+
}
|
|
297
353
|
if (status !== 200)
|
|
298
|
-
return
|
|
354
|
+
return retryableFromBody(body);
|
|
299
355
|
const result = extractLookupResult(body);
|
|
300
356
|
if (result === null)
|
|
301
357
|
return { kind: "retryable" };
|
|
@@ -558,8 +614,8 @@ function extractForbiddenReason(body) {
|
|
|
558
614
|
* The tool clamps nothing and validates no cursor opacity — sil-api owns those.
|
|
559
615
|
*
|
|
560
616
|
* THE load-bearing rename: the agent arg `ship_to` (singular) becomes the wire key
|
|
561
|
-
* `filters.ships_to` (plural). `
|
|
562
|
-
*
|
|
617
|
+
* `filters.ships_to` (plural). `condition`/`available` keep their name under
|
|
618
|
+
* `filters.*`. These three ride sil-api's OPEN `SearchFilters`
|
|
563
619
|
* (`additionalProperties: true`), which SILENTLY accepts an unknown key — so the
|
|
564
620
|
* exact emitted key name is the only thing standing between a working serviceability
|
|
565
621
|
* filter and a no-op (a wrong rename fails GREEN). Each is forwarded ONLY when
|
|
@@ -568,13 +624,23 @@ function extractForbiddenReason(body) {
|
|
|
568
624
|
* `available: false` is narrowed with `typeof === "boolean"`, never `if (v)`, so the
|
|
569
625
|
* meaningful `false` (include unavailable items) survives.
|
|
570
626
|
*
|
|
627
|
+
* `local_merchants` is the ONE field that does NOT ride `filters`. It is a
|
|
628
|
+
* sil-PRIVATE ranking-bias signal, not a UCP/Global-Catalog filter — `filters` is
|
|
629
|
+
* forwarded to the cross-shop Global Catalog, whose OPEN schema would silently
|
|
630
|
+
* accept and ignore an unknown `local_merchants` key (the exact wrong-placement
|
|
631
|
+
* no-op the rename hazard above warns of). So it is emitted at the TOP LEVEL of the
|
|
632
|
+
* body (beside `query`/`filters`/`pagination`), and ONLY when exactly `true`: unlike
|
|
633
|
+
* `available: false` (a meaningful include-unavailable signal that survives), a
|
|
634
|
+
* `false` bias carries NO signal (== the server's unbiased default), so `false` and
|
|
635
|
+
* absent both omit the key (narrow on the VALUE, not just the type — emit iff true).
|
|
636
|
+
*
|
|
571
637
|
* COUNTRY-CASE NORMALIZATION is the wire layer's job (the read site owns FORMAT
|
|
572
|
-
* rejection): `ships_to.country`
|
|
573
|
-
*
|
|
574
|
-
*
|
|
575
|
-
*
|
|
576
|
-
*
|
|
577
|
-
*
|
|
638
|
+
* rejection): `ships_to.country` is emitted UPPERCASE (`us` → `US`), the alpha-2
|
|
639
|
+
* contract `@sil/schemas` `ShipTo` mirrors byte-for-byte. A lowercase code on the
|
|
640
|
+
* wire would diverge from the sibling and (depending on the server's
|
|
641
|
+
* case-sensitivity) silently mis-filter. `region` and `postal_code` are forwarded
|
|
642
|
+
* VERBATIM — they passed their pattern at the read site, and the wire normalizes
|
|
643
|
+
* only country case.
|
|
578
644
|
*/
|
|
579
645
|
function buildSearchBody(params) {
|
|
580
646
|
const body = {};
|
|
@@ -591,20 +657,25 @@ function buildSearchBody(params) {
|
|
|
591
657
|
price["max"] = params.price_max;
|
|
592
658
|
if (Object.keys(price).length > 0)
|
|
593
659
|
filters["price"] = price;
|
|
594
|
-
// The
|
|
595
|
-
// rename happens here; the other
|
|
596
|
-
//
|
|
660
|
+
// The three serviceability/localization filters — the `ship_to` → `ships_to`
|
|
661
|
+
// rename happens here; the other two keep their name. Country is uppercased on
|
|
662
|
+
// emit (region/postal forwarded verbatim); the other two pass through as-is.
|
|
597
663
|
if (params.ship_to !== undefined)
|
|
598
664
|
filters["ships_to"] = withUppercaseCountry(params.ship_to);
|
|
599
|
-
if (params.ships_from !== undefined) {
|
|
600
|
-
filters["ships_from"] = withUppercaseCountry(params.ships_from);
|
|
601
|
-
}
|
|
602
665
|
if (params.condition !== undefined)
|
|
603
666
|
filters["condition"] = params.condition;
|
|
604
667
|
if (typeof params.available === "boolean")
|
|
605
668
|
filters["available"] = params.available;
|
|
606
669
|
if (Object.keys(filters).length > 0)
|
|
607
670
|
body["filters"] = filters;
|
|
671
|
+
// `local_merchants` is a sil-PRIVATE ranking bias — it rides at the TOP LEVEL of
|
|
672
|
+
// the body (a sibling of `query`/`filters`/`pagination`), NEVER under `filters`
|
|
673
|
+
// (those are forwarded to the Global Catalog, which would silently swallow it).
|
|
674
|
+
// Emitted ONLY when exactly `true`: a `false`/absent bias carries no signal (it is
|
|
675
|
+
// the server's unbiased default), so — unlike `available:false` — it is omitted.
|
|
676
|
+
// Narrow on the VALUE, not just the type: `=== true`, never a truthiness coerce.
|
|
677
|
+
if (params.local_merchants === true)
|
|
678
|
+
body["local_merchants"] = true;
|
|
608
679
|
const pagination = {};
|
|
609
680
|
if (typeof params.cursor === "string")
|
|
610
681
|
pagination["cursor"] = params.cursor;
|
|
@@ -614,10 +685,10 @@ function buildSearchBody(params) {
|
|
|
614
685
|
body["pagination"] = pagination;
|
|
615
686
|
return body;
|
|
616
687
|
}
|
|
617
|
-
/** Emit a ship-to
|
|
618
|
-
*
|
|
619
|
-
* preserved verbatim. The spread-then-overwrite keeps the shape generic over
|
|
620
|
-
* {
|
|
688
|
+
/** Emit a ship-to object with its alpha-2 `country` UPPERCASED on the wire
|
|
689
|
+
* (`us` → `US`); every other field (`region`/`postal_code` on {@link ShipTo}) is
|
|
690
|
+
* preserved verbatim. The spread-then-overwrite keeps the shape generic over any
|
|
691
|
+
* `{ country }`-bearing value without reshaping or dropping a field. */
|
|
621
692
|
function withUppercaseCountry(value) {
|
|
622
693
|
return { ...value, country: value.country.toUpperCase() };
|
|
623
694
|
}
|
|
@@ -686,13 +757,20 @@ function projectProduct(raw) {
|
|
|
686
757
|
const variant = projectVariant(variants[0]);
|
|
687
758
|
if (variant === null)
|
|
688
759
|
return null;
|
|
689
|
-
|
|
760
|
+
const result = { id: productId, title: productTitle, source, variant };
|
|
761
|
+
attachProductEnrichment(result, product);
|
|
762
|
+
return result;
|
|
690
763
|
}
|
|
691
764
|
/**
|
|
692
765
|
* Project a `SilCatalogVariant` to the agent-facing {@link SearchVariant}, or
|
|
693
766
|
* null if it is not a usable purchasable variant. Requires `id`, `title`, a price
|
|
694
767
|
* object, and a non-empty `checkout_url`; `price`/`availability` pass through
|
|
695
|
-
* opaque.
|
|
768
|
+
* opaque. The PURCHASABILITY gate is unchanged — the enriched fields are additive
|
|
769
|
+
* context, never a new gate: a variant lacking a non-empty `checkout_url` is still
|
|
770
|
+
* dropped even when it carries `url`/`seller`/`media`.
|
|
771
|
+
*
|
|
772
|
+
* The enriched per-variant surface (`url`/`seller`/`media`/`metadata`) is attached
|
|
773
|
+
* via {@link attachVariantEnrichment}, each ONLY when present.
|
|
696
774
|
*/
|
|
697
775
|
function projectVariant(raw) {
|
|
698
776
|
const variant = asRecord(raw);
|
|
@@ -710,13 +788,15 @@ function projectVariant(raw) {
|
|
|
710
788
|
const price = extractPrice(variant["price"]);
|
|
711
789
|
if (price === null)
|
|
712
790
|
return null;
|
|
713
|
-
|
|
791
|
+
const result = {
|
|
714
792
|
id,
|
|
715
793
|
title,
|
|
716
794
|
price,
|
|
717
795
|
availability: extractAvailability(variant["availability"]),
|
|
718
796
|
checkout_url: checkoutUrl,
|
|
719
797
|
};
|
|
798
|
+
attachVariantEnrichment(result, variant);
|
|
799
|
+
return result;
|
|
720
800
|
}
|
|
721
801
|
/**
|
|
722
802
|
* Unwrap + narrow a sil-api catalog-LOOKUP response body to a typed
|
|
@@ -779,9 +859,6 @@ function projectLookupProduct(raw) {
|
|
|
779
859
|
return null;
|
|
780
860
|
if (typeof source !== "string" || source.length === 0)
|
|
781
861
|
return null;
|
|
782
|
-
const description = asRecord(product["description"]);
|
|
783
|
-
if (description === null)
|
|
784
|
-
return null;
|
|
785
862
|
const priceRange = product["price_range"];
|
|
786
863
|
if (asRecord(priceRange) === null)
|
|
787
864
|
return null;
|
|
@@ -794,7 +871,6 @@ function projectLookupProduct(raw) {
|
|
|
794
871
|
const result = {
|
|
795
872
|
id,
|
|
796
873
|
title,
|
|
797
|
-
description,
|
|
798
874
|
price_range: priceRange,
|
|
799
875
|
source,
|
|
800
876
|
variant,
|
|
@@ -805,6 +881,11 @@ function projectLookupProduct(raw) {
|
|
|
805
881
|
const handle = product["handle"];
|
|
806
882
|
if (typeof handle === "string" && handle.length > 0)
|
|
807
883
|
result.handle = handle;
|
|
884
|
+
// `description` is now the `{ plain }` lift (ONE vocabulary with search), NOT the
|
|
885
|
+
// whole opaque object — omitted entirely when `plain` is absent/empty. Reuses the
|
|
886
|
+
// SAME product-enrichment attach as search so both tools surface the identical
|
|
887
|
+
// enriched shape (`url`/`description`/`media`/`options`/`metadata`).
|
|
888
|
+
attachProductEnrichment(result, product);
|
|
808
889
|
return result;
|
|
809
890
|
}
|
|
810
891
|
/**
|
|
@@ -847,6 +928,10 @@ function projectLookupVariant(raw) {
|
|
|
847
928
|
const inputs = extractInputs(variant["inputs"]);
|
|
848
929
|
if (inputs !== null)
|
|
849
930
|
result.inputs = inputs;
|
|
931
|
+
// The SAME enriched per-variant surface as search (ONE vocabulary):
|
|
932
|
+
// `url`/`seller`/`media`/`metadata`, each only when present. The variant's
|
|
933
|
+
// `options` SELECTIONS above and these enriched fields are distinct, additive.
|
|
934
|
+
attachVariantEnrichment(result, variant);
|
|
850
935
|
return result;
|
|
851
936
|
}
|
|
852
937
|
/** Pass a wire array of objects through OPAQUE — filter to plain objects, preserve
|
|
@@ -863,6 +948,80 @@ function passThroughObjects(raw) {
|
|
|
863
948
|
const objects = raw.filter((o) => asRecord(o) !== null);
|
|
864
949
|
return objects.length === 0 ? null : objects;
|
|
865
950
|
}
|
|
951
|
+
/** Pass a single wire OBJECT through OPAQUE — forward it verbatim when it is a
|
|
952
|
+
* plain object, never reading or remapping individual fields. The single-object
|
|
953
|
+
* counterpart to {@link passThroughObjects}, for `seller` and `metadata`: contextual
|
|
954
|
+
* data the tool SURFACES but does not interpret. This is the load-bearing
|
|
955
|
+
* narrow-vs-pass-through guard ([[sil-shared-catalog-client]]) — a typed
|
|
956
|
+
* `{ name, links }` narrow on `seller` would silently DROP the Shopify extension
|
|
957
|
+
* keys (`seller.url`/`seller.domain`) the source attaches, and pass a naive fixture;
|
|
958
|
+
* opaque pass-through keeps both the base shape and any extension/drift. An ARRAY is
|
|
959
|
+
* NOT a usable object here (a `seller`/`metadata` that arrived as `[]` is garbage),
|
|
960
|
+
* so arrays return null and the caller omits the key. */
|
|
961
|
+
function passThroughObject(raw) {
|
|
962
|
+
return raw !== null && typeof raw === "object" && !Array.isArray(raw)
|
|
963
|
+
? raw
|
|
964
|
+
: null;
|
|
965
|
+
}
|
|
966
|
+
/** Lift the agent-facing {@link ProductDescription} (`{ plain }`) from a wire
|
|
967
|
+
* `description` object, or null to OMIT. Surfaces ONLY the `plain` UCP format, and
|
|
968
|
+
* ONLY when it is a non-empty string — `html`/`markdown` are never substituted into
|
|
969
|
+
* `plain` (the spec flags `html` as untrusted rich text), and an absent/empty `plain`
|
|
970
|
+
* omits the whole `description` key. This is the one SCALAR read in the enriched
|
|
971
|
+
* surface (a stable, named UCP sub-field), and lifting it identically on both catalog
|
|
972
|
+
* tools is what keeps them on ONE `description` shape. */
|
|
973
|
+
function liftDescriptionPlain(raw) {
|
|
974
|
+
const obj = asRecord(raw);
|
|
975
|
+
if (obj === null)
|
|
976
|
+
return null;
|
|
977
|
+
const plain = obj["plain"];
|
|
978
|
+
return typeof plain === "string" && plain.length > 0 ? { plain } : null;
|
|
979
|
+
}
|
|
980
|
+
/** Attach the enriched PRODUCT surface (`url`/`description`/`media`/`options`/
|
|
981
|
+
* `metadata`) onto a projected product, each ONLY when present on the wire object.
|
|
982
|
+
* Shared verbatim by `projectProduct` (search) and `projectLookupProduct` (lookup)
|
|
983
|
+
* so BOTH tools surface the IDENTICAL enriched shape — the ONE-vocabulary property.
|
|
984
|
+
* Mutates `target` in place (it is a freshly-built projection the caller owns). Every
|
|
985
|
+
* field is OPAQUE pass-through keyed on PRESENCE; the only scalar read is
|
|
986
|
+
* `description.plain`. Omit-when-absent is per-field — an absent / empty-`plain` /
|
|
987
|
+
* no-`metadata` / all-garbage-array field leaves its key off entirely (no null/''/[]). */
|
|
988
|
+
function attachProductEnrichment(target, product) {
|
|
989
|
+
const url = product["url"];
|
|
990
|
+
if (typeof url === "string" && url.length > 0)
|
|
991
|
+
target.url = url;
|
|
992
|
+
const description = liftDescriptionPlain(product["description"]);
|
|
993
|
+
if (description !== null)
|
|
994
|
+
target.description = description;
|
|
995
|
+
const media = passThroughObjects(product["media"]);
|
|
996
|
+
if (media !== null)
|
|
997
|
+
target.media = media;
|
|
998
|
+
const options = passThroughObjects(product["options"]);
|
|
999
|
+
if (options !== null)
|
|
1000
|
+
target.options = options;
|
|
1001
|
+
const metadata = passThroughObject(product["metadata"]);
|
|
1002
|
+
if (metadata !== null)
|
|
1003
|
+
target.metadata = metadata;
|
|
1004
|
+
}
|
|
1005
|
+
/** Attach the enriched per-VARIANT surface (`url`/`seller`/`media`/`metadata`) onto a
|
|
1006
|
+
* projected variant, each ONLY when present. Shared verbatim by `projectVariant`
|
|
1007
|
+
* (search) and `projectLookupVariant` (lookup) — the variant half of the
|
|
1008
|
+
* ONE-vocabulary property. `seller` (incl. `seller.links[]`) and `metadata` are
|
|
1009
|
+
* single-object opaque pass-through; `media` is array pass-through. Same
|
|
1010
|
+
* omit-when-absent, per-field, never-narrow discipline as the product surface. */
|
|
1011
|
+
function attachVariantEnrichment(target, variant) {
|
|
1012
|
+
const url = variant["url"];
|
|
1013
|
+
if (typeof url === "string" && url.length > 0)
|
|
1014
|
+
target.url = url;
|
|
1015
|
+
const seller = passThroughObject(variant["seller"]);
|
|
1016
|
+
if (seller !== null)
|
|
1017
|
+
target.seller = seller;
|
|
1018
|
+
const media = passThroughObjects(variant["media"]);
|
|
1019
|
+
if (media !== null)
|
|
1020
|
+
target.media = media;
|
|
1021
|
+
const metadata = passThroughObject(variant["metadata"]);
|
|
1022
|
+
if (metadata !== null)
|
|
1023
|
+
target.metadata = metadata;
|
|
1024
|
+
}
|
|
866
1025
|
/** Narrow a wire `inputs` correlation array to {@link LookupInput}[] — each entry
|
|
867
1026
|
* needs a non-empty `id` (the request id that resolved to this variant); a missing
|
|
868
1027
|
* or non-string `match` is dropped (not coerced). Returns null when the field is
|
|
@@ -950,6 +1109,40 @@ function extractCursor(raw) {
|
|
|
950
1109
|
const cursor = obj["cursor"];
|
|
951
1110
|
return typeof cursor === "string" && cursor.length > 0 ? cursor : null;
|
|
952
1111
|
}
|
|
1112
|
+
/**
|
|
1113
|
+
* Build the `retryable` outcome for a non-200, non-{400,401} response, attaching
|
|
1114
|
+
* the failed catalog `source` (+ a `detail` carrying the upstream cause) ONLY when
|
|
1115
|
+
* the 5xx body is a real `source_unavailable` SourceError that names a source.
|
|
1116
|
+
*
|
|
1117
|
+
* This is the seam where outcome (a) (sil/network down → bare retryable, generic
|
|
1118
|
+
* copy) and outcome (b) (a named source down → source-named retryable) become
|
|
1119
|
+
* distinguishable — see {@link SearchOutcome}. The gate is the PRESENCE of a real
|
|
1120
|
+
* non-empty-string `source` field on the body, NEVER the `message` prose: a
|
|
1121
|
+
* sil-internal 5xx (no `source`), a bodyless/garbage non-200, or a `source` that is
|
|
1122
|
+
* null/number/empty/object/array all fall back to the bare sourceless retryable.
|
|
1123
|
+
* Attaching a source to a non-source 5xx would re-introduce wrong attribution in
|
|
1124
|
+
* the opposite direction (a sil-down event falsely named as a source outage), so
|
|
1125
|
+
* the populate must never fabricate or coerce.
|
|
1126
|
+
*
|
|
1127
|
+
* `detail` is the upstream cause the consumer can relay — the body's `message`
|
|
1128
|
+
* when present, else its `error` code. It is only set alongside a real `source`
|
|
1129
|
+
* (an outcome-a retryable carries neither field).
|
|
1130
|
+
*/
|
|
1131
|
+
function retryableFromBody(body) {
|
|
1132
|
+
const obj = asRecord(body);
|
|
1133
|
+
const source = obj?.["source"];
|
|
1134
|
+
if (typeof source !== "string" || source.length === 0) {
|
|
1135
|
+
return { kind: "retryable" };
|
|
1136
|
+
}
|
|
1137
|
+
const message = obj?.["message"];
|
|
1138
|
+
const error = obj?.["error"];
|
|
1139
|
+
const detail = typeof message === "string" && message.length > 0
|
|
1140
|
+
? message
|
|
1141
|
+
: typeof error === "string" && error.length > 0
|
|
1142
|
+
? error
|
|
1143
|
+
: source;
|
|
1144
|
+
return { kind: "retryable", source, detail };
|
|
1145
|
+
}
|
|
953
1146
|
/** Pull sil-api's structured `{ error, message }` out of a 400 body, defaulting
|
|
954
1147
|
* each field when the shape is unexpected so the agent always gets an actionable
|
|
955
1148
|
* (if generic) hint rather than `undefined`. */
|
package/dist/tools/catalog.js
CHANGED
|
@@ -53,16 +53,16 @@ import { clearTokens, readTokens } from "../lib/credentials.js";
|
|
|
53
53
|
import { lookupCatalog, refreshAndRetryOnce, searchCatalog, } from "../lib/sil-client.js";
|
|
54
54
|
import { jsonResult } from "../lib/tool-result.js";
|
|
55
55
|
/**
|
|
56
|
-
* The TIGHTENED ship-to
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
56
|
+
* The TIGHTENED ship-to format contract (founder re-spec 2026-06-11), pinned as a
|
|
57
|
+
* `pattern` BOTH the local schema and sil-api's `@sil/schemas` `ShipTo` enforce
|
|
58
|
+
* IDENTICALLY (byte-for-byte) — so the plugin rejects a malformed value client-side
|
|
59
|
+
* instead of forwarding free text and eating an opaque, fail-late sil-api 400. These
|
|
60
|
+
* same patterns are the schema `pattern` (so the host validates) AND the read-site
|
|
61
|
+
* format gate (so a drifted on-disk call is rejected), defined once here to keep the
|
|
62
|
+
* two in lockstep. The sil-services sibling
|
|
63
63
|
* (`attach-buyer-ship-to-context-server-side-in-sil-ap`) MUST mirror these exactly;
|
|
64
64
|
* the sil-stage eval verifies it end-to-end.
|
|
65
|
-
* - country (ships_to
|
|
65
|
+
* - country (ships_to): ISO 3166-1 alpha-2, a 2-letter code.
|
|
66
66
|
* - region: ISO 3166-2 subdivision code (CA/NY/BY/97), bounded — NOT a place name.
|
|
67
67
|
* - postal_code: a single self-contained pattern (a length-cap lookahead 2–12
|
|
68
68
|
* chars, separators counted, + a structural body of alnum runs joined by single
|
|
@@ -84,7 +84,21 @@ function registerSearch(api) {
|
|
|
84
84
|
+ " and/or filters (`category`, `price_min`/`price_max` in the currency's"
|
|
85
85
|
+ " minor units, e.g. cents). Returns a ranked list (best match first —"
|
|
86
86
|
+ " present results in order, do not re-rank), each item a purchasable"
|
|
87
|
-
+ " variant with id, title, price, availability, checkout_url, and source
|
|
87
|
+
+ " variant with id, title, price, availability, checkout_url, and source,"
|
|
88
|
+
+ " plus (where the source provides them) the evaluate-before-buy fields:"
|
|
89
|
+
+ " product and variant `url`, a short `description`, `media`, the product"
|
|
90
|
+
+ " `options` (the menu of choices), the `seller` (with policy/info links),"
|
|
91
|
+
+ " and arbitrary `metadata`. THREE DISTINCT ACTIONS the agent must not"
|
|
92
|
+
+ " conflate: (1) VIEW — a product's or variant's `url` opens the PAGE to"
|
|
93
|
+
+ " view / learn more; hand it to the user to SEE the item, it is NOT a"
|
|
94
|
+
+ " purchase. (2) DIG IN — `seller.links` are the seller's policy / info"
|
|
95
|
+
+ " links (refund policy, shipping policy, terms); follow one to answer"
|
|
96
|
+
+ " \"what's their return policy?\" without a web search. (3) BUY —"
|
|
97
|
+
+ " `checkout_url` is the variant permalink that commits the purchase; use it"
|
|
98
|
+
+ " ONLY to buy. A variant's `url` and its `checkout_url` are DIFFERENT"
|
|
99
|
+
+ " targets: `url` is the page (view / learn more) whereas `checkout_url`"
|
|
100
|
+
+ " buys — never hand back `checkout_url` for a \"show me / learn more\""
|
|
101
|
+
+ " intent, and never stall on `url` when the user said \"buy\"."
|
|
88
102
|
+ " Use the returned `cursor` to fetch the next page (its absence means no"
|
|
89
103
|
+ " more results — never infer end-of-results from the page size). An empty"
|
|
90
104
|
+ " result list means nothing matched (a normal outcome, not an error)."
|
|
@@ -103,12 +117,17 @@ function registerSearch(api) {
|
|
|
103
117
|
+ " code, NOT a country name); `region` (optional) = the ISO 3166-2 subdivision"
|
|
104
118
|
+ " code (CA, NY, BY — a code, NOT a place name); `postal_code` (optional) ="
|
|
105
119
|
+ " the destination postal/ZIP code. The other optional filters need no prior"
|
|
106
|
-
+ " tool call: `
|
|
107
|
-
+ "
|
|
108
|
-
+ " (
|
|
109
|
-
+ " by
|
|
110
|
-
+ "
|
|
111
|
-
+ "
|
|
120
|
+
+ " tool call: `condition` (array; the values are exactly \"new\" or"
|
|
121
|
+
+ " \"secondhand\", lowercase) filters by product condition; `available`"
|
|
122
|
+
+ " (boolean) controls availability — the server returns only sale-ready items"
|
|
123
|
+
+ " by default, so set `available: false` to INCLUDE out-of-stock/unavailable"
|
|
124
|
+
+ " items. `local_merchants` (boolean) is a BEST-EFFORT BIAS toward shops based"
|
|
125
|
+
+ " in the user's OWN country (home-country / domestic / local sellers) — set it"
|
|
126
|
+
+ " ONLY when the shopper asks for local/domestic shops; it nudges local shops up"
|
|
127
|
+
+ " the ranking but does NOT restrict results to them, so never tell the user the"
|
|
128
|
+
+ " results are all local. To actually surface local shops, ALSO issue the"
|
|
129
|
+
+ " `query` in the USER'S LANGUAGE (a Greek-language query surfaces Greek shops);"
|
|
130
|
+
+ " pass NO country — sil resolves it server-side. Requires registration (run"
|
|
112
131
|
+ " sil_register first).",
|
|
113
132
|
parameters: Type.Object({
|
|
114
133
|
query: Type.Optional(Type.String({
|
|
@@ -156,15 +175,20 @@ function registerSearch(api) {
|
|
|
156
175
|
+ " default with a DIFFERENT destination than the registered address."
|
|
157
176
|
+ " Send ISO CODES, never free-text place names.",
|
|
158
177
|
})),
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
+ "
|
|
178
|
+
local_merchants: Type.Optional(Type.Boolean({
|
|
179
|
+
description: "Bias results toward shops based in the USER'S OWN country (home-country"
|
|
180
|
+
+ " / domestic / local sellers) — set true ONLY when the shopper asks for"
|
|
181
|
+
+ " local or domestic shops (e.g. \"buy from a Greek shop\", \"support"
|
|
182
|
+
+ " local businesses\"). This is a BEST-EFFORT BIAS, NOT a filter: it"
|
|
183
|
+
+ " nudges local shops up the ranking but does NOT restrict results to them"
|
|
184
|
+
+ " and does NOT guarantee every result is local — some local shops won't"
|
|
185
|
+
+ " be detected and some non-local shops may still appear, so never tell"
|
|
186
|
+
+ " the user the results are all local sellers. To actually surface local"
|
|
187
|
+
+ " shops, also issue the `query` in the USER'S LANGUAGE (a Greek-language"
|
|
188
|
+
+ " query surfaces Greek shops; an English query will not). You pass NO"
|
|
189
|
+
+ " country — sil resolves the user's country server-side from their"
|
|
190
|
+
+ " registered address, so do NOT call sil_whoami or pass a country. Omit"
|
|
191
|
+
+ " (or false) for the normal unbiased ranking.",
|
|
168
192
|
})),
|
|
169
193
|
condition: Type.Optional(Type.Array(Type.String(), {
|
|
170
194
|
description: "Filter by product condition. The values are exactly \"new\" or"
|
|
@@ -244,12 +268,30 @@ function mapSearchOutcome(api, outcome) {
|
|
|
244
268
|
switch (outcome.kind) {
|
|
245
269
|
case "ok":
|
|
246
270
|
return searchResult(outcome);
|
|
271
|
+
case "forbidden":
|
|
272
|
+
// A 403 surfaces the SAME forbidden envelope `sil_whoami` emits — never the
|
|
273
|
+
// false-transient `retryable`. On `user_not_provisioned` (and ONLY that exact
|
|
274
|
+
// reason) the held token maps to no account on this backend and a refresh
|
|
275
|
+
// cannot help — structurally dead, like the invalid_grant/second_unauthorized
|
|
276
|
+
// clears below — so clear it HERE (the tool call site, never the pure
|
|
277
|
+
// classifier) and the next sil_register re-onboards instead of short-circuiting
|
|
278
|
+
// to already_registered. A `principal_mismatch` / unknown reason can be
|
|
279
|
+
// transient and stays recoverable — it MUST NOT clear (the exact-equality gate
|
|
280
|
+
// is the correctness boundary: a startsWith/truthy check would wrongly wipe a
|
|
281
|
+
// good session).
|
|
282
|
+
api.logger.warn("sil_search_forbidden", { reason: outcome.reason });
|
|
283
|
+
if (outcome.reason === "user_not_provisioned")
|
|
284
|
+
clearTokens();
|
|
285
|
+
return forbidden(outcome.reason);
|
|
247
286
|
case "invalid_request":
|
|
248
287
|
api.logger.info("sil_search_invalid_request", { error: outcome.error });
|
|
249
288
|
return invalidRequest(outcome.error, outcome.message);
|
|
250
289
|
case "retryable":
|
|
251
|
-
|
|
252
|
-
|
|
290
|
+
// A source-attributed 5xx names the failed source (outcome b); a sil/network
|
|
291
|
+
// blip carries no source and stays the generic copy (outcome a). The log marker
|
|
292
|
+
// records the source when present so a flaky source is visible to operators.
|
|
293
|
+
api.logger.info("sil_search_retryable", outcome.source ? { source: outcome.source } : {});
|
|
294
|
+
return transient("sil_search", outcome.source, outcome.detail);
|
|
253
295
|
case "unauthorized":
|
|
254
296
|
return mustReregister("sil_search");
|
|
255
297
|
}
|
|
@@ -304,7 +346,17 @@ function registerProductGet(api) {
|
|
|
304
346
|
+ " Pass `ids` (one or more product/variant ids you already hold, e.g. from"
|
|
305
347
|
+ " a prior sil_search result) and get the matching products back with FRESH"
|
|
306
348
|
+ " detail: each product's description plus its featured purchasable variant"
|
|
307
|
-
+ " (id, title, price, availability, checkout_url, options)
|
|
349
|
+
+ " (id, title, price, availability, checkout_url, options), and — where the"
|
|
350
|
+
+ " source provides them — the same evaluate-before-buy fields sil_search"
|
|
351
|
+
+ " surfaces: product and variant `url`, `media`, the product `options` menu,"
|
|
352
|
+
+ " the `seller` (with policy/info links), and `metadata`. The SAME three"
|
|
353
|
+
+ " distinct actions apply: (1) VIEW — a product's or variant's `url` opens"
|
|
354
|
+
+ " the PAGE to view / learn more (NOT a purchase); (2) DIG IN —"
|
|
355
|
+
+ " `seller.links` follow the seller's policy / info links (refund, shipping,"
|
|
356
|
+
+ " terms); (3) BUY — `checkout_url` is the variant permalink that commits the"
|
|
357
|
+
+ " purchase. A variant's `url` and its `checkout_url` are DIFFERENT targets:"
|
|
358
|
+
+ " `url` is the page (view) whereas `checkout_url` buys — do not conflate"
|
|
359
|
+
+ " them. Re-fetch right"
|
|
308
360
|
+ " before the user buys — prices, availability, and checkout_url are"
|
|
309
361
|
+ " point-in-time, not guarantees. Each variant carries an `inputs` list"
|
|
310
362
|
+ " correlating it back to the id(s) you asked about (the response is NOT in"
|
|
@@ -371,12 +423,24 @@ function mapLookupOutcome(api, outcome) {
|
|
|
371
423
|
switch (outcome.kind) {
|
|
372
424
|
case "ok":
|
|
373
425
|
return lookupResult(outcome);
|
|
426
|
+
case "forbidden":
|
|
427
|
+
// Symmetric with sil_search (one vocabulary, AC2/AC7/AC10): a 403 is the shared
|
|
428
|
+
// forbidden envelope, and `user_not_provisioned` — and ONLY that exact reason —
|
|
429
|
+
// clears the structurally-dead token at this call site so recovery terminates
|
|
430
|
+
// regardless of which catalog tool revealed the state. `principal_mismatch` /
|
|
431
|
+
// unknown reasons survive (recoverable; the same exact-equality gate).
|
|
432
|
+
api.logger.warn("sil_product_get_forbidden", { reason: outcome.reason });
|
|
433
|
+
if (outcome.reason === "user_not_provisioned")
|
|
434
|
+
clearTokens();
|
|
435
|
+
return forbidden(outcome.reason);
|
|
374
436
|
case "invalid_request":
|
|
375
437
|
api.logger.info("sil_product_get_invalid_request", { error: outcome.error });
|
|
376
438
|
return invalidRequest(outcome.error, outcome.message);
|
|
377
439
|
case "retryable":
|
|
378
|
-
|
|
379
|
-
|
|
440
|
+
// Symmetric with sil_search: a source-attributed 5xx names the source (outcome
|
|
441
|
+
// b); a sil/network blip is the generic copy (outcome a). One error vocabulary.
|
|
442
|
+
api.logger.info("sil_product_get_retryable", outcome.source ? { source: outcome.source } : {});
|
|
443
|
+
return transient("sil_product_get", outcome.source, outcome.detail);
|
|
380
444
|
case "unauthorized":
|
|
381
445
|
return mustReregister("sil_product_get");
|
|
382
446
|
}
|
|
@@ -417,20 +481,21 @@ function invalidIds() {
|
|
|
417
481
|
* a drifted on-disk call must not slip a non-string into the query mapping. No
|
|
418
482
|
* `any`, no unchecked `as`.
|
|
419
483
|
*
|
|
420
|
-
* The
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
*
|
|
424
|
-
*
|
|
425
|
-
*
|
|
484
|
+
* The serviceability/localization filters narrow by their own type: `ship_to` to an
|
|
485
|
+
* object with a string `country` (a primitive/number is DROPPED), `condition` to a
|
|
486
|
+
* string[] (a bare string is dropped), `available` and `local_merchants` to a boolean
|
|
487
|
+
* (a string is dropped). The drop is TYPE-driven, never value-driven, so a valid
|
|
488
|
+
* `available: false` survives — it is a boolean, the meaningful "include unavailable
|
|
489
|
+
* items" signal, not a falsy value to discard.
|
|
426
490
|
*
|
|
427
|
-
* Beyond TYPE narrowing, `ship_to
|
|
428
|
-
*
|
|
429
|
-
*
|
|
430
|
-
*
|
|
431
|
-
*
|
|
432
|
-
*
|
|
433
|
-
*
|
|
491
|
+
* Beyond TYPE narrowing, `ship_to` also FORMAT-validates every provided string field
|
|
492
|
+
* (country alpha-2, region 3166-2, postal bounded) against the shared patterns: a
|
|
493
|
+
* present-but-malformed value returns an `invalid` read so the tool rejects the
|
|
494
|
+
* request client-side with a clear `invalid_filter` error (better agent UX than an
|
|
495
|
+
* opaque, fail-late sil-api 400). `condition` is NOT format-validated — its wire
|
|
496
|
+
* stays OPEN (an unrecognized value still forwards; steering to new/secondhand lives
|
|
497
|
+
* in the description only). `local_merchants` is a plain boolean bias (no format, no
|
|
498
|
+
* country) — its whole steer lives in the description. The first bad field wins. */
|
|
434
499
|
function readSearchParams(params) {
|
|
435
500
|
const result = {};
|
|
436
501
|
const query = params["query"];
|
|
@@ -456,17 +521,20 @@ function readSearchParams(params) {
|
|
|
456
521
|
return { kind: "invalid", field: shipTo.field };
|
|
457
522
|
if (shipTo.kind === "ok")
|
|
458
523
|
result.ship_to = shipTo.value;
|
|
459
|
-
const shipsFrom = readShipsFrom(params["ships_from"]);
|
|
460
|
-
if (shipsFrom.kind === "invalid")
|
|
461
|
-
return { kind: "invalid", field: shipsFrom.field };
|
|
462
|
-
if (shipsFrom.kind === "ok")
|
|
463
|
-
result.ships_from = shipsFrom.value;
|
|
464
524
|
const condition = readCondition(params["condition"]);
|
|
465
525
|
if (condition !== null)
|
|
466
526
|
result.condition = condition;
|
|
467
527
|
const available = params["available"];
|
|
468
528
|
if (typeof available === "boolean")
|
|
469
529
|
result.available = available;
|
|
530
|
+
// `local_merchants` is a plain boolean bias narrowed exactly like `available`:
|
|
531
|
+
// a wrong type is DROPPED (treated as absent), never coerced. A valid `false`
|
|
532
|
+
// survives the read site (type-driven, not value-driven); the omit-when-falsy
|
|
533
|
+
// discipline for the BIAS itself lives at the wire (`buildSearchBody`), since
|
|
534
|
+
// unlike `available:false`, a false bias carries no ranking signal.
|
|
535
|
+
const localMerchants = params["local_merchants"];
|
|
536
|
+
if (typeof localMerchants === "boolean")
|
|
537
|
+
result.local_merchants = localMerchants;
|
|
470
538
|
return { kind: "ok", params: result };
|
|
471
539
|
}
|
|
472
540
|
/** A non-null plain object, or null for anything else (arrays/primitives). */
|
|
@@ -508,22 +576,6 @@ function readShipTo(raw) {
|
|
|
508
576
|
}
|
|
509
577
|
return { kind: "ok", value: result };
|
|
510
578
|
}
|
|
511
|
-
/** Narrow a `ships_from` arg to {@link ShipsFrom}. A non-object/missing-country is
|
|
512
|
-
* `absent` (DROPPED, wrong-TYPE discipline); a provided `country` string that fails
|
|
513
|
-
* the alpha-2 pattern is `invalid` (REJECT). Country is uppercased on the wire, not
|
|
514
|
-
* here. */
|
|
515
|
-
function readShipsFrom(raw) {
|
|
516
|
-
const obj = asRecord(raw);
|
|
517
|
-
if (obj === null)
|
|
518
|
-
return { kind: "absent" };
|
|
519
|
-
const country = obj["country"];
|
|
520
|
-
if (typeof country !== "string" || country.length === 0)
|
|
521
|
-
return { kind: "absent" };
|
|
522
|
-
if (!matchesPattern(country, COUNTRY_PATTERN)) {
|
|
523
|
-
return { kind: "invalid", field: "ships_from.country" };
|
|
524
|
-
}
|
|
525
|
-
return { kind: "ok", value: { country } };
|
|
526
|
-
}
|
|
527
579
|
/** Test a value against one of the shared schema patterns, anchored exactly as the
|
|
528
580
|
* schema's `pattern` is (the constants carry their own `^…$`). A fresh `RegExp` per
|
|
529
581
|
* call keeps the patterns as plain shared strings (the schema needs the string form;
|
|
@@ -619,12 +671,59 @@ function mustReregister(tool) {
|
|
|
619
671
|
recovery: "sil_register",
|
|
620
672
|
});
|
|
621
673
|
}
|
|
622
|
-
/**
|
|
623
|
-
*
|
|
624
|
-
*
|
|
625
|
-
|
|
674
|
+
/** Terminal-but-distinct: a 403 — the token is valid but the user isn't provisioned
|
|
675
|
+
* (or a principal mismatch / other reason). Refreshing would not help (a 403 is not
|
|
676
|
+
* a 401). Surfaces the SAME envelope `sil_whoami`'s `forbidden()` (identity.ts) emits
|
|
677
|
+
* — `{ status:"forbidden", reason, message, recovery:"sil_register" }`, byte-identical
|
|
678
|
+
* including the reason-driven message branch — so the three sil-api tools speak ONE
|
|
679
|
+
* error vocabulary (AC5). `user_not_provisioned` reads as an actionable onboarding
|
|
680
|
+
* prompt; any other reason is the generic forbidden copy carrying the reason. The
|
|
681
|
+
* recovery hint is always `sil_register` (the message names it, tool-agnostic — the
|
|
682
|
+
* parity with `sil_whoami`'s envelope is byte-exact, so it carries no per-tool name).
|
|
683
|
+
* The dead-token CLEAR is NOT here — it is the caller's decision (gated on
|
|
684
|
+
* `user_not_provisioned`), so a `principal_mismatch` that still wants the legible
|
|
685
|
+
* envelope does not have its token wiped. */
|
|
686
|
+
function forbidden(reason) {
|
|
687
|
+
const message = reason === "user_not_provisioned"
|
|
688
|
+
? "Your sil account is not fully set up. Complete onboarding (run"
|
|
689
|
+
+ " sil_register) and try again."
|
|
690
|
+
: "sil rejected this request (" + reason + "). Run sil_register to"
|
|
691
|
+
+ " re-establish your session, then try again.";
|
|
692
|
+
return jsonResult({ status: "forbidden", reason, message, recovery: "sil_register" });
|
|
693
|
+
}
|
|
694
|
+
/** Transient: a retryable blip — try again, NOT a re-register (a false terminal on
|
|
695
|
+
* a transient would send the agent down a recovery path that can't fix it). The two
|
|
696
|
+
* causally-distinct transient failures share `status: "retryable"` (both succeed on
|
|
697
|
+
* backoff) but split on ATTRIBUTION, which is the whole point of this card:
|
|
698
|
+
*
|
|
699
|
+
* - `source` ABSENT (outcome a) — sil itself / the network / transport is down,
|
|
700
|
+
* OR a refresh-leg blip. The agent retries the same call; the copy stays the
|
|
701
|
+
* GENERIC "sil is temporarily unavailable" and names no source it cannot
|
|
702
|
+
* identify (attribution honesty).
|
|
703
|
+
* - `source` PRESENT (outcome b) — a specific catalog source is down or
|
|
704
|
+
* rate-limited. The copy NAMES that source and must NEVER say "sil is …
|
|
705
|
+
* unavailable" / "sil is down": sil is healthy, one source is degraded, often
|
|
706
|
+
* for seconds, and mis-attributing it to sil makes the agent abandon a working
|
|
707
|
+
* platform. `detail` (the upstream cause) is relayed when present.
|
|
708
|
+
*
|
|
709
|
+
* The source is named ONLY when the wire actually carried it (the classifier gates
|
|
710
|
+
* on a real `source` field — see `retryableFromBody` in sil-client). The named-source
|
|
711
|
+
* copy is SELF-AUTHORED around the source token rather than echoing the raw upstream
|
|
712
|
+
* `detail` prose verbatim — the upstream wording is sil-api's to change and could
|
|
713
|
+
* itself contain "sil is …" phrasing that would muddy the attribution; `detail` is
|
|
714
|
+
* surfaced as a separate, clearly-delimited field instead. The `tool` name keeps the
|
|
715
|
+
* retry guidance actionable; the status taxonomy is shared. */
|
|
716
|
+
function transient(tool, source, detail) {
|
|
717
|
+
if (source === undefined) {
|
|
718
|
+
return jsonResult({
|
|
719
|
+
status: "retryable",
|
|
720
|
+
message: `sil is temporarily unavailable. Please try ${tool} again.`,
|
|
721
|
+
});
|
|
722
|
+
}
|
|
626
723
|
return jsonResult({
|
|
627
724
|
status: "retryable",
|
|
628
|
-
message: `
|
|
725
|
+
message: `The catalog source "${source}" is temporarily unavailable.`
|
|
726
|
+
+ ` sil itself is fine — retry ${tool} shortly.`,
|
|
727
|
+
...(detail !== undefined ? { detail } : {}),
|
|
629
728
|
});
|
|
630
729
|
}
|
package/dist/tools/identity.js
CHANGED
|
@@ -19,9 +19,11 @@
|
|
|
19
19
|
* 4. Start a fire-and-forget bounded poll of the claim endpoint, then return
|
|
20
20
|
* promptly with `{ status: "awaiting_browser", auth_url, session_id }`.
|
|
21
21
|
* 5. On the poll's terminal success, persist tokens.json + config.json
|
|
22
|
-
* atomically. Other terminals (expired / already_claimed /
|
|
22
|
+
* atomically. Other terminals (expired / already_claimed / invalid_request /
|
|
23
23
|
* timeout) persist nothing; the agent learns the outcome by re-calling
|
|
24
|
-
* sil_register (→ already_registered) or sil_whoami.
|
|
24
|
+
* sil_register (→ already_registered) or sil_whoami. A claim 404 (`not_found`)
|
|
25
|
+
* is NOT a terminal — it is the normal pre-session early state and keeps the
|
|
26
|
+
* poll ticking; a session that never appears ends as `timeout` at the deadline.
|
|
25
27
|
*
|
|
26
28
|
* register() must stay synchronous and side-effect-free beyond registering the
|
|
27
29
|
* tool — no fetch, no timer, no unawaited promise here. The poll timer is armed
|
|
@@ -179,7 +181,18 @@ function identityOutcomeToResult(api, outcome) {
|
|
|
179
181
|
case "ok":
|
|
180
182
|
return identityResult(outcome.identity);
|
|
181
183
|
case "forbidden":
|
|
184
|
+
// Decision B — the dead-token clear is UNIFORM across all three sil-api tools.
|
|
185
|
+
// `sil_whoami` is the tool that is legible TODAY; an agent that diagnoses the
|
|
186
|
+
// 403 via whoami, follows `recovery:"sil_register"`, and hits
|
|
187
|
+
// `already_registered` is stranded identically to the catalog path. So clear on
|
|
188
|
+
// `user_not_provisioned` HERE too (the held token maps to no account on this
|
|
189
|
+
// backend; a refresh cannot help — structurally dead, like the invalid_grant
|
|
190
|
+
// clear above). Gated on EXACT equality: `principal_mismatch` / unknown reasons
|
|
191
|
+
// can be transient and must stay recoverable, so they keep the legible forbidden
|
|
192
|
+
// envelope WITHOUT a destructive clear (AC9 clears, AC10 does not).
|
|
182
193
|
api.logger.warn("sil_whoami_forbidden", { reason: outcome.reason });
|
|
194
|
+
if (outcome.reason === "user_not_provisioned")
|
|
195
|
+
clearTokens();
|
|
183
196
|
return forbidden(outcome.reason);
|
|
184
197
|
case "retryable":
|
|
185
198
|
api.logger.info("sil_whoami_retryable", {});
|
|
@@ -232,17 +245,22 @@ function transient() {
|
|
|
232
245
|
}
|
|
233
246
|
/**
|
|
234
247
|
* One poll step: claim, then map the outcome to the loop's done/continue signal.
|
|
235
|
-
* `pending`/`retryable` keep the loop alive (`done:false`).
|
|
236
|
-
* is
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
248
|
+
* `pending`/`retryable`/`not_found` keep the loop alive (`done:false`). The 404
|
|
249
|
+
* `not_found` is the NORMAL pre-session early state (the session row is INSERTed
|
|
250
|
+
* server-side only when the user opens the auth URL), so it keeps polling exactly
|
|
251
|
+
* like `pending`, bounded by the 30-min deadline — a session that never appears
|
|
252
|
+
* settles as `timeout`, never as `not_found`. A successful claim is persisted
|
|
253
|
+
* HERE — tokens.json + config.json written atomically inside this awaited tick —
|
|
254
|
+
* so the files are on disk before the loop settles; the step then carries only
|
|
255
|
+
* the user_id forward (the tokens never travel past this closure). The terminals
|
|
256
|
+
* (expired / already_claimed / invalid_request) persist nothing.
|
|
240
257
|
*/
|
|
241
258
|
async function claimStep(apiUrl, sessionId, verifier) {
|
|
242
259
|
const outcome = await claimSession(apiUrl, sessionId, verifier);
|
|
243
260
|
switch (outcome.kind) {
|
|
244
261
|
case "pending":
|
|
245
262
|
case "retryable":
|
|
263
|
+
case "not_found":
|
|
246
264
|
return { done: false };
|
|
247
265
|
case "success":
|
|
248
266
|
await writeTokens({
|
|
@@ -253,7 +271,7 @@ async function claimStep(apiUrl, sessionId, verifier) {
|
|
|
253
271
|
return { done: true, outcome: "success", user_id: outcome.user.id };
|
|
254
272
|
case "expired":
|
|
255
273
|
case "already_claimed":
|
|
256
|
-
case "
|
|
274
|
+
case "invalid_request":
|
|
257
275
|
return { done: true, outcome: outcome.kind };
|
|
258
276
|
}
|
|
259
277
|
}
|
|
@@ -275,11 +293,14 @@ function handleDone(api, sessionId, result) {
|
|
|
275
293
|
api.logger.info("sil_register_timeout", { session_id: sessionId });
|
|
276
294
|
return;
|
|
277
295
|
}
|
|
278
|
-
// expired / already_claimed /
|
|
279
|
-
// never silent; the agent learns it on its next sil_register / sil_whoami.
|
|
296
|
+
// expired / already_claimed / invalid_request — log the terminal so the outcome
|
|
297
|
+
// is never silent; the agent learns it on its next sil_register / sil_whoami.
|
|
298
|
+
// `invalid_request` logs at WARN: a malformed claim request is unreachable from
|
|
299
|
+
// this client today, so it signals a contract-drift bug, not a user outcome —
|
|
300
|
+
// make it loud (the genuine `expired`/`already_claimed` are routine, at info).
|
|
280
301
|
const outcome = typeof step.outcome === "string" ? step.outcome : "unknown";
|
|
281
302
|
const marker = `sil_register_${outcome}`;
|
|
282
|
-
if (outcome === "
|
|
303
|
+
if (outcome === "invalid_request") {
|
|
283
304
|
api.logger.warn(marker, { session_id: sessionId });
|
|
284
305
|
}
|
|
285
306
|
else {
|
package/openclaw.plugin.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"id": "sil",
|
|
3
3
|
"name": "sil",
|
|
4
4
|
"description": "sil commerce plugin for OpenClaw — register on sil, read your identity, and search and look up products in the sil catalog.",
|
|
5
|
-
"version": "0.2.
|
|
5
|
+
"version": "0.2.3",
|
|
6
6
|
"skills": ["./skill"],
|
|
7
7
|
"activation": {
|
|
8
8
|
"onCapabilities": ["tool"]
|
package/package.json
CHANGED