@vibes.diy/prompts 14.3.270 → 14.3.271
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/llms/backend.md +6 -6
- package/llms/stripe.md +162 -36
- package/package.json +5 -5
package/llms/backend.md
CHANGED
|
@@ -92,8 +92,9 @@ handler. A guest's `apiFetch` into the `fetch` handler arrives with `user` null,
|
|
|
92
92
|
and `ctx.requireBackend("fetch")` still passes for it, so a guest-checkout
|
|
93
93
|
record can be one only the handler writes; an `onChange` run triggered by a
|
|
94
94
|
signed-out writer passes `ctx.requireBackend("onChange")` the same way. The function still opts that anonymous
|
|
95
|
-
write in with `allowAnonymous: true`, and
|
|
96
|
-
|
|
95
|
+
write in with `allowAnonymous: true`, and that opt-in is all it takes: the
|
|
96
|
+
app's anonymous-write setting gates writes straight from a browser, never a
|
|
97
|
+
write the app's own backend.js makes. `user.backend` itself is read only off a
|
|
97
98
|
signed-in user, so gate with `ctx.requireBackend`.
|
|
98
99
|
|
|
99
100
|
`scheduled` always runs in **admin mode**, the same override the owner gets
|
|
@@ -954,10 +955,9 @@ handle). The page gates its add-a-teammate control on
|
|
|
954
955
|
teammates you add see these leads"), and your reply says the same.
|
|
955
956
|
|
|
956
957
|
Gate both handlers on the secret, so the app works fully before any Zap
|
|
957
|
-
exists, and say in your reply what the owner sets up: the secret
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
CLI push, `vibes-diy app anon-write <owner>/<slug> on`).
|
|
958
|
+
exists, and say in your reply what the owner sets up: the secret and the Zap's POST
|
|
959
|
+
address and header. The arrivals are written by backend.js with
|
|
960
|
+
`allowAnonymous: true`, so the app's anonymous-write setting plays no part.
|
|
961
961
|
|
|
962
962
|
### `backend.js`
|
|
963
963
|
```js
|
package/llms/stripe.md
CHANGED
|
@@ -274,10 +274,11 @@ once on the server from the product records the buyer names, and never trust an
|
|
|
274
274
|
amount the browser sends — the route takes *which products, and how many of
|
|
275
275
|
each*, and looks every price up in the owner's catalog.
|
|
276
276
|
|
|
277
|
-
A bag is paid in one Checkout Session: the
|
|
277
|
+
A bag is paid in one Checkout Session: backend.js prices every line of the bag, each product and its quantity, from the owner's catalog, the order keeps those lines, and the session carries one line item per bag line.
|
|
278
278
|
|
|
279
|
-
A lone Pay button is a bag of one (`[{ productId, qty: 1 }]`)
|
|
280
|
-
|
|
279
|
+
A lone Pay button is a bag of one (`[{ productId, qty: 1 }]`) that the page
|
|
280
|
+
sends to `/checkout`; a shop's bag is a record the buyer fills, paid by the
|
|
281
|
+
`/pay` link (*A shop with a bag*). Both price through the same `priceBag`. The order's `lines` hold
|
|
281
282
|
each line's title, unit amount and quantity as they were at checkout, its
|
|
282
283
|
`amount` is their total, and the read-back matches Stripe's `amount_total` to
|
|
283
284
|
that total. A bag is one currency; the route caps it at a sensible number of
|
|
@@ -1019,14 +1020,157 @@ where Stripe renews and retries, is the `stripe-subscriptions` skill.
|
|
|
1019
1020
|
### A shop with a bag
|
|
1020
1021
|
|
|
1021
1022
|
A shop where people fill a bag (the `ui-storefront` block's shape) keeps that
|
|
1022
|
-
block's page and swaps its **Place order** for this recipe's checkout
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
a
|
|
1023
|
+
block's page and swaps its **Place order** for this recipe's checkout. The
|
|
1024
|
+
products are this recipe's `product` records (the storefront adds a photo to
|
|
1025
|
+
each as `_files.photo`), and the order the bag becomes is this recipe's
|
|
1026
|
+
`order`, written by backend.js, so the storefront's own browser-written order
|
|
1027
|
+
goes away. Checkout charges the lines, so the storefront's shipping row goes
|
|
1028
|
+
too, unless backend.js prices shipping as a line of its own.
|
|
1029
|
+
|
|
1030
|
+
A shop's bag is a record in the `orders` database that the buyer writes as they shop, and its Pay is one link, `/_api/pay?bag=<_id>&rev=<rev>`, that prices that exact version of the bag in backend.js and sends the buyer to Stripe Checkout.
|
|
1031
|
+
|
|
1032
|
+
A record rather than page state, because the bag is the buyer's, not the
|
|
1033
|
+
tab's: it is there on their phone after they filled it on their laptop, and
|
|
1034
|
+
anyone helping them shop (their own agent filling the bag in a conversation,
|
|
1035
|
+
say) writes the same record the page does and hands them the same link. The
|
|
1036
|
+
page never sends a price and never a list of lines; the link names a bag and
|
|
1037
|
+
the version the buyer saw, and the shop's own `priceBag` reads every line
|
|
1038
|
+
from the owner's catalog, options and all.
|
|
1039
|
+
|
|
1040
|
+
**The bag record.** One open bag per buyer: `type: "bag"`, an `_id` of
|
|
1041
|
+
`bag-` and a fresh id, `buyerHandle` (access.js holds it to the writer),
|
|
1042
|
+
and `rev`, a fresh random string on every write, which is the version the
|
|
1043
|
+
Pay link names. Its lines take one of two shapes, and a shop picks the one
|
|
1044
|
+
that fits:
|
|
1045
|
+
|
|
1046
|
+
- **Lines on the bag.** `lines: [{ productId, qty }]` on the bag itself, and
|
|
1047
|
+
every change is one put of the whole bag with a new `rev`. Right for most
|
|
1048
|
+
shops: one record, one write, nothing to keep in step.
|
|
1049
|
+
- **A record per line.** Each line is its own `type: "line"` record with
|
|
1050
|
+
`bagId`, `productId`, `qty` and whatever options it carries (size, milk,
|
|
1051
|
+
engraving), and every line write also puts the bag with a new `rev` and
|
|
1052
|
+
`touchedAt`. Right when a line is rich enough to edit on its own screen.
|
|
1053
|
+
The line carries the `touchedAt` of the bag write that went with it, so a
|
|
1054
|
+
line newer than the bag was written after the link and is not in it.
|
|
1055
|
+
|
|
1056
|
+
A line's options are the shop's to price: `priceBag` reads them off the line
|
|
1057
|
+
and adds what the owner's product record says they cost, so a modifier is
|
|
1058
|
+
priced in exactly one place, the same place as the product.
|
|
1059
|
+
|
|
1060
|
+
### `access.js`
|
|
1061
|
+
```js partial
|
|
1062
|
+
// Added before the `order` rule: the buyer's own bag, and its lines when they are records. A record
|
|
1063
|
+
// keeps its type, so an order can never be rewritten as a line to slip past the order rule.
|
|
1064
|
+
if (oldDoc && oldDoc.type !== doc.type) throw { forbidden: "a record keeps its type" };
|
|
1065
|
+
if (doc.type === "bag" || doc.type === "line") {
|
|
1066
|
+
if (doc.buyerHandle !== user.userHandle || (oldDoc && oldDoc.buyerHandle !== doc.buyerHandle)) {
|
|
1067
|
+
throw { forbidden: "a bag is its buyer's own" };
|
|
1068
|
+
}
|
|
1069
|
+
if (doc.type === "bag" && (!doc._id?.startsWith("bag-") || typeof doc.rev !== "string")) {
|
|
1070
|
+
throw { forbidden: "a bag has a bag- id and a rev" };
|
|
1071
|
+
}
|
|
1072
|
+
if (doc.type === "line" && !doc.bagId?.startsWith("bag-")) throw { forbidden: "a line names its bag" };
|
|
1073
|
+
const mine = `user:${doc.buyerHandle}`;
|
|
1074
|
+
return { channels: [mine], grant: { users: { [doc.buyerHandle]: [mine] } } };
|
|
1075
|
+
}
|
|
1076
|
+
```
|
|
1077
|
+
|
|
1078
|
+
The `order` rule takes one change: a Pay link arrives as a plain link, so
|
|
1079
|
+
`/pay` writes the order while nobody is signed in. The new order's buyer is
|
|
1080
|
+
the signed-in writer when there is one, `ctx.requireBackend("fetch")` still
|
|
1081
|
+
gates its creation, and the rule returns `allowAnonymous: true`. That is all
|
|
1082
|
+
a write from the app's own backend needs: the app's anonymous-write setting
|
|
1083
|
+
gates browsers, not backend.js, so the owner has no step to take. The top line
|
|
1084
|
+
that refuses a signed-out writer lets an `order` through to that rule.
|
|
1085
|
+
|
|
1086
|
+
### `backend.js`
|
|
1087
|
+
```js partial
|
|
1088
|
+
// Pay a bag by link. The page's Pay button and anyone helping the buyer open the same address, so
|
|
1089
|
+
// the price is the one this route computes, from the owner's catalog, for the bag version named.
|
|
1090
|
+
// A link is opened signed out: the buyer is the bag's buyerHandle, which only they could write.
|
|
1091
|
+
if (url.pathname === "/pay" && request.method === "GET") {
|
|
1092
|
+
const back = (reason) => Response.redirect(appUrl(ctx, `#bag-problem=${reason}`), 303);
|
|
1093
|
+
const bagId = url.searchParams.get("bag") ?? "";
|
|
1094
|
+
const bag = bagId.startsWith("bag-") ? await ctx.db.get(bagId, { db: DB }) : null;
|
|
1095
|
+
if (bag?.type !== "bag") return back("not-found");
|
|
1096
|
+
// The version the buyer was shown, or nothing: a bag that moved since sends them back to look again.
|
|
1097
|
+
if (bag.rev !== url.searchParams.get("rev")) return back("bag-changed");
|
|
1098
|
+
const { mode, open } = await checkoutMode(ctx);
|
|
1099
|
+
const shown = url.searchParams.get("mode"); // the page adds the mode it showed; a link from elsewhere has none
|
|
1100
|
+
if (shown && shown !== mode) return back("mode-changed");
|
|
1101
|
+
if (!open) return back("not-open");
|
|
1102
|
+
let lines = bag.lines;
|
|
1103
|
+
if (!Array.isArray(lines)) {
|
|
1104
|
+
// A record per line: the bag's buyer's own lines naming this bag (anyone can name a bag id, so a
|
|
1105
|
+
// line someone else wrote is not in it), and none written after the bag's own write.
|
|
1106
|
+
// Every page: the query filters after its limit, and orders name the bag by bagId too.
|
|
1107
|
+
lines = [];
|
|
1108
|
+
let after;
|
|
1109
|
+
do {
|
|
1110
|
+
const page = await ctx.db.query({ db: DB, field: "bagId", key: bag._id, limit: 100, after });
|
|
1111
|
+
lines.push(...page.filter((l) => l.type === "line" && l.buyerHandle === bag.buyerHandle));
|
|
1112
|
+
after = page.next;
|
|
1113
|
+
} while (after && lines.length <= MAX_LINES);
|
|
1114
|
+
if (lines.some((l) => l.touchedAt > bag.touchedAt)) return back("bag-changed");
|
|
1115
|
+
}
|
|
1116
|
+
// Whole lines, options and all: priceBag prices each option from the owner's product record.
|
|
1117
|
+
const priced = await priceBag(ctx, lines);
|
|
1118
|
+
if (!priced) return back("unknown-item");
|
|
1119
|
+
const buyer = bag.buyerHandle;
|
|
1120
|
+
// One attempt per bag version: a second press of the same link lands on the session already open.
|
|
1121
|
+
const pending = await ctx.db.query({ db: DB, field: "status", key: "pending", limit: 100 });
|
|
1122
|
+
const attempt = pending.find((o) => o.bagId === bag._id && o.bagKey === priced.key && o.currency === priced.currency && o.mode === mode && o.sessionUrl && Date.now() - o.createdAt < SESSION_TTL_MS);
|
|
1123
|
+
if (attempt) return Response.redirect(attempt.sessionUrl, 303);
|
|
1124
|
+
// From here it is /checkout's order and session, with the bag's buyer and `bagId: bag._id` on the
|
|
1125
|
+
// order: the same order fields, the same form, the same line items, the same idempotency key.
|
|
1126
|
+
const { id, sessionUrl, failed } = await openSession(ctx, { priced, buyer, mode, bagId: bag._id });
|
|
1127
|
+
if (failed) return back(failed);
|
|
1128
|
+
return Response.redirect(sessionUrl, 303);
|
|
1129
|
+
}
|
|
1130
|
+
```
|
|
1131
|
+
|
|
1132
|
+
`openSession` is `/checkout`'s second half lifted into a function, so both
|
|
1133
|
+
routes write the same order and the same session: the order (now with
|
|
1134
|
+
`bagId` when a bag paid it), the session form, the second put with
|
|
1135
|
+
`sessionId` and `sessionUrl`. `config.fetch.unfilteredReads` already covers
|
|
1136
|
+
the `orders` database; its `why` names the bag too, since `/pay` reads a
|
|
1137
|
+
bag and its lines with nobody signed in.
|
|
1138
|
+
|
|
1139
|
+
**The page.** It reads the buyer's open bag with a live query, writes it on
|
|
1140
|
+
every Add, and its Pay is a link. The page runs in a frame, so the link is the
|
|
1141
|
+
app's full address: `/setup` adds `pay: appUrl(ctx, "/_api/pay")` to everyone's
|
|
1142
|
+
answer, and the page puts the bag on it.
|
|
1143
|
+
|
|
1144
|
+
### `App.jsx`
|
|
1145
|
+
```jsx partial
|
|
1146
|
+
// The buyer's open bag: theirs alone (access.js routes it to their own channel), and the newest one
|
|
1147
|
+
// no paid order names. Every Add is a put of the whole bag with a fresh rev.
|
|
1148
|
+
const { docs: bags } = useLiveQuery("type", { key: "bag" });
|
|
1149
|
+
const bag = bags.filter((b) => b.buyerHandle === me?.userHandle && !orders.some((o) => o.bagId === b._id && o.status === "paid")).sort((a, b) => b.touchedAt - a.touchedAt)[0];
|
|
1150
|
+
const lines = (bag?.lines ?? []).filter((l) => forSale.some((p) => p._id === l.productId));
|
|
1151
|
+
const add = (productId) => {
|
|
1152
|
+
const kept = bag?.lines ?? [];
|
|
1153
|
+
const next = kept.some((l) => l.productId === productId) ? kept.map((l) => (l.productId === productId ? { ...l, qty: l.qty + 1 } : l)) : [...kept, { productId, qty: 1 }];
|
|
1154
|
+
database.put({ ...(bag ?? { _id: "bag-" + crypto.randomUUID(), type: "bag", buyerHandle: me.userHandle }), lines: next, rev: crypto.randomUUID(), touchedAt: Date.now() });
|
|
1155
|
+
};
|
|
1156
|
+
// The same address anyone helping the buyer would hand them. The subtotal is a preview; /pay prices it.
|
|
1157
|
+
const payUrl = bag && setup?.pay && `${setup.pay}?bag=${encodeURIComponent(bag._id)}&rev=${encodeURIComponent(bag.rev)}&mode=${setup.mode}`;
|
|
1158
|
+
|
|
1159
|
+
// Pay waits for the server to hold this version: a put lands on this device first, and a link to a
|
|
1160
|
+
// rev the server has not accepted yet would come back as "bag changed".
|
|
1161
|
+
const saving = bag?._synced && bag._synced.status !== "accepted";
|
|
1162
|
+
<Button onClick={() => window.open(payUrl, "_blank")} disabled={!payUrl || lines.length === 0 || saving}>
|
|
1163
|
+
Pay {money(subtotal, CURRENCY)} — Stripe opens in a new tab
|
|
1164
|
+
</Button>
|
|
1165
|
+
```
|
|
1166
|
+
|
|
1167
|
+
`window.open` goes straight to the link inside the click, so there is no
|
|
1168
|
+
blank tab to fill and no popup warning. The link lands back on the shop
|
|
1169
|
+
with `#bag-problem=` when the bag moved, something went off sale or checkout
|
|
1170
|
+
is closed, and the page says so in the shop's words (*Your bag changed: check
|
|
1171
|
+
it and press Pay again*). The bag stays the buyer's until an order naming it
|
|
1172
|
+
comes back paid; a buyer who closes Stripe's tab still has their bag, and a
|
|
1173
|
+
paid bag drops out of the live query, so the next Add starts a fresh one.
|
|
1030
1174
|
|
|
1031
1175
|
A product listed with a period renews, so it sells on the `stripe-subscriptions` recipe: a line like *Monthly coffee bag, $22* or *Bread club, $30 a month* checks out in subscription mode with `price_data[recurring][interval]`, never as a one-time line in a payment-mode bag.
|
|
1032
1176
|
|
|
@@ -1044,19 +1188,6 @@ The header link reads like *Bag (3)*, its count is the sum of the quantities,
|
|
|
1044
1188
|
and it sits in the `Navbar` beside the owner's **Products** button, on every
|
|
1045
1189
|
screen where people shop.
|
|
1046
1190
|
|
|
1047
|
-
```jsx
|
|
1048
|
-
// The storefront's `lines` already leave out anything taken off sale. Its lead sends each line's
|
|
1049
|
-
// product and quantity; the backend prices every line from the catalog, so the page's sum is a
|
|
1050
|
-
// preview, never the charge.
|
|
1051
|
-
<Button onClick={() => pay(lines.map(({ productId, qty }) => ({ productId, qty })))} disabled={busy || lines.length === 0} loading={busy}>
|
|
1052
|
-
Pay {money(subtotal, CURRENCY)} — Stripe opens in a new tab
|
|
1053
|
-
</Button>
|
|
1054
|
-
```
|
|
1055
|
-
|
|
1056
|
-
The bag empties when the order comes back paid (the `#paid=` return, or the
|
|
1057
|
-
order moving to `paid` in the buyer's live query), never when the tab opens: a
|
|
1058
|
-
buyer who closes Stripe's tab still has their bag.
|
|
1059
|
-
|
|
1060
1191
|
## Guest checkout at a venue: a table's check
|
|
1061
1192
|
|
|
1062
1193
|
A restaurant check is the fullest venue page; a tip jar, a donation page or a
|
|
@@ -1083,13 +1214,12 @@ payment's creation only from `ctx.requireBackend("fetch")` and its settling
|
|
|
1083
1214
|
only from the backend, keeps each type at its own ids, and takes the go-live
|
|
1084
1215
|
record only from the owner. A page writes nothing but the owner's own checks.
|
|
1085
1216
|
|
|
1086
|
-
A guest's write through the route is
|
|
1087
|
-
|
|
1088
|
-
`allowAnonymous: true
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
filled in. With it off, `/checkout` answers `guests-closed` and the page
|
|
1217
|
+
A guest's write through the route is a signed-out write: `user` is null,
|
|
1218
|
+
`ctx.requireBackend("fetch")` passes, and the record returns
|
|
1219
|
+
`allowAnonymous: true`. That opt-in is the whole of it: a write from the app's
|
|
1220
|
+
own backend.js is not gated by the app's anonymous-write setting (that setting
|
|
1221
|
+
is for writes straight from a browser), so guests pay with no Setup step for
|
|
1222
|
+
it. A write the access function refuses answers `guests-closed`, and the page
|
|
1093
1223
|
reads *Checkout is not open yet*.
|
|
1094
1224
|
|
|
1095
1225
|
Before a Checkout Session is created, the payment reserves its share of the check with one revision-checked write.
|
|
@@ -1360,7 +1490,7 @@ export async function fetch(request, ctx) {
|
|
|
1360
1490
|
(err) => err
|
|
1361
1491
|
);
|
|
1362
1492
|
if (refused) {
|
|
1363
|
-
//
|
|
1493
|
+
// access.js refused the record; its reason is in the log.
|
|
1364
1494
|
ctx.log("warn", "check reservation refused", { code: refused.code, message: refused.message });
|
|
1365
1495
|
return json({ ok: false, reason: "guests-closed" }, 503);
|
|
1366
1496
|
}
|
|
@@ -1594,10 +1724,6 @@ function SetupScreen({ setup }) {
|
|
|
1594
1724
|
Open <a href={settingsUrl} target="_blank" rel="noreferrer">this vibe's Settings tab</a> (My Vibes → this vibe → Settings)
|
|
1595
1725
|
and press <b>{STRIPE_BUTTON[item.state] ?? "Connect Stripe"}</b> under <b>Payments</b>. Stripe opens its own setup pages.
|
|
1596
1726
|
</li>
|
|
1597
|
-
<li>
|
|
1598
|
-
Let guests pay without an account: publishing from the editor turns this on, and after a CLI push run{" "}
|
|
1599
|
-
<b>vibes-diy app anon-write {vibe} on</b>. The page never writes for them; this app's server records each payment.
|
|
1600
|
-
</li>
|
|
1601
1727
|
<li>
|
|
1602
1728
|
Come back here: tables can pay as soon as Stripe says your account is ready. In test mode the card 4242 4242 4242 4242 with any future date pays.
|
|
1603
1729
|
</li>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vibes.diy/prompts",
|
|
3
|
-
"version": "14.3.
|
|
3
|
+
"version": "14.3.271",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"exports": {
|
|
@@ -39,10 +39,10 @@
|
|
|
39
39
|
"license": "Apache-2.0",
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"@adviser/cement": "~0.5.34",
|
|
42
|
-
"@vibes.diy/api-types": "14.3.
|
|
43
|
-
"@vibes.diy/call-ai-v2": "14.3.
|
|
44
|
-
"@vibes.diy/identity": "14.3.
|
|
45
|
-
"@vibes.diy/use-vibes-types": "14.3.
|
|
42
|
+
"@vibes.diy/api-types": "14.3.271",
|
|
43
|
+
"@vibes.diy/call-ai-v2": "14.3.271",
|
|
44
|
+
"@vibes.diy/identity": "14.3.271",
|
|
45
|
+
"@vibes.diy/use-vibes-types": "14.3.271",
|
|
46
46
|
"arktype": "~2.2.8",
|
|
47
47
|
"json-schema-faker": "~0.6.3"
|
|
48
48
|
},
|