@agent-cards/commerce-sdk 0.18.0 → 0.18.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/ack.d.ts +7 -0
- package/dist/ack.js +42 -0
- package/dist/buy-tool.d.ts +10 -1
- package/dist/buy-tool.js +28 -8
- package/dist/client.d.ts +14 -0
- package/dist/client.js +1 -1
- package/dist/direct.js +15 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -136,7 +136,7 @@ console.log(await result.getText());
|
|
|
136
136
|
|
|
137
137
|
A purchase needs a link at three moments: the shopper approves the purchase on an approval page, a new shopper adds a card on a Vault page, and a shopper signs in to a store's own account on its login page before Agentcard can buy there. Each link's `kind` says which: `approval`, `vault` or `store_login`, and a `store_login` link names the store in `store`. The tool hands each link to `sendLink` and waits for it, so the shopper holds the link before the model answers. The model reads `link_sent: true` and never the link itself, because a tool runner puts each answer straight into the model's context, and a model that holds a link writes it into the chat.
|
|
138
138
|
|
|
139
|
-
The tool also hands `sendLink` a `product` link for each product a search finds, up to eight per answer. The link carries the product's page at its store in `url`, the product's name in `title`, its price in `price`, such as `$12.99`, or `from $10.69` for a product priced by a size or another choice the shopper makes, such as a burger whose patty count sets its price, Agentcard's id for it in `productId`, what taking it does in `action` (`reserve` for a restaurant table, `book` for a flight, room, class or ticket, `buy` for anything else, so a button can say Reserve instead of Buy this), its picture in `imageUrl`, or null when the store sent none, and every photo of the product in `images`, `imageUrl` first and at most eight, so a card can show the others as thumbnails that swap the main picture. `images` holds one entry for a product with one photo and none for a product with none. Every price the tool writes, in a link or in the model's view, uses the currency Agentcard names for the cart or the product, in that currency's own minor units: `1331` is `$13.31` in USD, `CA$13.31` in CAD and `¥1,331` in JPY. A price whose currency Agentcard does not name shows with no symbol. The link arrives beside the reply, and you decide how to show it: as a card, in a list, or as a text with the URL. Each product gets its own link, so the three dishes a restaurant's reply offers are three cards, though they share the store's one page. A flight has no page at a store, so each flight a search finds is a `product` link whose `url` is null, with the airline, the route and its times in `title`: show it as a card with no link to open, and hand its `productId` back as the shopper's pick. The model reads `product_links_sent` and never a URL. The tool's description tells it that the shopper already sees those products as cards, so its reply never lists them again by name or price: it writes one short line, such as which one they want, and a shopper who asks for links hears that the links are there. When a later answer in the conversation only stands beside the same products, as a cart answer does, the tool sends none of their links again. When a later turn's reply presents them again, as the answer to a repeated ask does, the tool sends their links again, so every product the reply names arrives with its link. A product link your `sendLink` throws on counts as not sent, and the call goes on.
|
|
139
|
+
The tool also hands `sendLink` a `product` link for each product a search finds, up to eight per answer. The link carries the product's page at its store in `url`, the product's name in `title`, its price in `price`, such as `$12.99`, or `from $10.69` for a product priced by a size or another choice the shopper makes, such as a burger whose patty count sets its price, Agentcard's id for it in `productId`, what taking it does in `action` (`reserve` for a restaurant table, `book` for a flight, room, class or ticket, `buy` for anything else, so a button can say Reserve instead of Buy this), its picture in `imageUrl`, or null when the store sent none, and every photo of the product in `images`, `imageUrl` first and at most eight, so a card can show the others as thumbnails that swap the main picture. `images` holds one entry for a product with one photo and none for a product with none. The link names the store that sells the product in `store`, such as `Walmart`, and the store's own website in `storeDomain`, such as `walmart.com` or `jadeyoga.com`, or null when Agentcard knows none, such as a restaurant's: show `storeDomain` on the card as where the product comes from, never the host of `url`, which can be a tracking link. Every price the tool writes, in a link or in the model's view, uses the currency Agentcard names for the cart or the product, in that currency's own minor units: `1331` is `$13.31` in USD, `CA$13.31` in CAD and `¥1,331` in JPY. A price whose currency Agentcard does not name shows with no symbol. The link arrives beside the reply, and you decide how to show it: as a card, in a list, or as a text with the URL. Each product gets its own link, so the three dishes a restaurant's reply offers are three cards, though they share the store's one page. A flight has no page at a store, so each flight a search finds is a `product` link whose `url` is null, with the airline, the route and its times in `title`: show it as a card with no link to open, and hand its `productId` back as the shopper's pick. The model reads `product_links_sent` and never a URL. The tool's description tells it that the shopper already sees those products as cards, so its reply never lists them again by name or price: it writes one short line, such as which one they want, and a shopper who asks for links hears that the links are there. When a later answer in the conversation only stands beside the same products, as a cart answer does, the tool sends none of their links again. When a later turn's reply presents them again, as the answer to a repeated ask does, the tool sends their links again, so every product the reply names arrives with its link. A product link your `sendLink` throws on counts as not sent, and the call goes on.
|
|
140
140
|
|
|
141
141
|
`productId` holds no link: it is the same `product_id` the model reads for that product, and the page is `url`. An answer that only adds a product the shopper picked lists that product alone, so it sends no card for a product nobody offered on that turn, and the pages the search already sent still count as sent on the answers after it.
|
|
142
142
|
|
|
@@ -362,7 +362,7 @@ async function runModel(shopperId: string, turn: BuyToolTurn) {
|
|
|
362
362
|
| Saving a card, with no yes and no pick standing | The total and the ask for the yes: `your Souvla chicken salad comes to $17.43 with fees. should i go ahead and place the order?` |
|
|
363
363
|
| Signing in to the store, with a card still to add | The add-card link: `next, add a card with the link i sent. it works for about 24 hours.` |
|
|
364
364
|
| Signing in to the store, with a card on file | The total and the ask for the yes, or the approval link after a yes that stopped for the sign-in |
|
|
365
|
-
| Signing in or saving a card, after a yes, when the store priced its fees and tax in since | The new total
|
|
365
|
+
| Signing in or saving a card, after a yes, when the store priced its fees and tax in since | The new total and the ask for the yes, with no link: `The cart changed after your yes: it comes to $17.89 with tax and shipping now. Nothing was paid. Say yes to pay $17.89.` |
|
|
366
366
|
| Declining the approval | `you declined the payment, so you haven't been charged. your chicken salad is still in the cart, so just say yes if you want a new approval link, or tell me what to change` |
|
|
367
367
|
|
|
368
368
|
With `direct` and `replyAsWritten` on the tool, a step needs no model when its answer is a link: send `step.run(step.direct.input)` first, and when `step.replyAsWritten` is set, post it, with `step.links`, in place of the model's answer. When it is null, as for the ask for a yes, run your model as above.
|
|
@@ -370,8 +370,8 @@ With `direct` and `replyAsWritten` on the tool, a step needs no model when its a
|
|
|
370
370
|
The turn moves the purchase only up to the shopper's own decision, and the tool holds it there whatever the model calls:
|
|
371
371
|
|
|
372
372
|
- No call of the turn sends an ask to Agentcard, which could read the model's words as the shopper's go-ahead. Each call reads where the purchase stands and answers with the next step: the card link the sign-in held back, or the cart and the ask for the shopper's yes.
|
|
373
|
-
- A confirm goes in only for the yes the shopper already gave in the chat, such as "lets place it", when that confirm stopped for the card or the sign-in they just finished, and the shopper is never asked for that yes again. It goes on past each step, the sign-in and then the card, as long as the cart holds the same items, in the same quantities and at the same prices, at the same store: a total that moved
|
|
374
|
-
- A shopper who picked what is in the cart, by tapping a product's card or asking to buy it, and since answered only the checkout's questions (an address, a phone number, a name, an email, a yes to an address read back to them) or said a plain go-ahead, gave the same go-ahead: saving the card or signing in to the store confirms that cart by its hash as it stands, with no ask for a yes (`yes_carried.picked`). A cart whose items, quantities, item prices or store changed since the pick, any other message, such as "actually wait" or a question, and a declined approval take it back.
|
|
373
|
+
- A confirm goes in only for the yes the shopper already gave in the chat, such as "lets place it", when that confirm stopped for the card or the sign-in they just finished, and the shopper is never asked for that yes again. It goes on past each step, the sign-in and then the card, as long as the cart holds the same items, in the same quantities and at the same prices, at the same store. Each confirm names the cart's hash as it stands, and in `carried` the hash of the cart the shopper said yes to: Agentcard pays only that cart, so a total that moved because the store priced its fees, tax or delivery in comes back with the new total and the ask for the shopper's yes, and nothing is paid. A cart whose items, quantities or store changed, a shopper message after the yes that is not a plain go-ahead, such as "wait, make it two", and a declined approval each take the yes back, and the shopper is asked again. Any other confirm the model sends is refused by the tool, and nothing reaches Agentcard.
|
|
374
|
+
- A shopper who picked what is in the cart, by tapping a product's card or asking to buy it, and since answered only the checkout's questions (an address, a phone number, a name, an email, a yes to an address read back to them) or said a plain go-ahead, gave the same go-ahead: saving the card or signing in to the store confirms that cart by its hash as it stands, with no ask for a yes (`yes_carried.picked`). The confirm names in `carried` the cart the chat showed, and a cart the store priced again since comes back with its total and the ask for a yes. A cart whose items, quantities, item prices or store changed since the pick, any other message, such as "actually wait" or a question, and a declined approval take it back.
|
|
375
375
|
- The approval in the Vault is still the shopper's: the confirm sends the approval link, which names the most they can be charged, and the order goes in only once they approve it.
|
|
376
376
|
- A shopper who was not connected yet when they asked gets their own first ask run, in their words, once their card is saved.
|
|
377
377
|
|
package/dist/ack.d.ts
CHANGED
|
@@ -20,6 +20,13 @@ export declare const CHITCHAT: RegExp;
|
|
|
20
20
|
* can you buy aa batteries from`, is a search and keeps its reply.
|
|
21
21
|
*/
|
|
22
22
|
export declare const CAPABILITY: RegExp;
|
|
23
|
+
/**
|
|
24
|
+
* True when the shopper's words ask for other products like one they saw, such as `More like the KODAK AA Alkaline Batteries (60
|
|
25
|
+
* Pack)`, `something similar to the first one`, `alternatives to the Duracell` or `cheaper than the KODAK one`: a new search,
|
|
26
|
+
* never a pick of the product it names. `names` are the products the shopper saw: their words are read out first, so a product
|
|
27
|
+
* whose own name says `alternative` or `similar` is still picked by its tap.
|
|
28
|
+
*/
|
|
29
|
+
export declare function asksForSimilar(text: string, names?: readonly string[]): boolean;
|
|
23
30
|
/**
|
|
24
31
|
* True when the message names one of these stores and nothing else to buy: the store's whole name, or
|
|
25
32
|
* two words of it in a row, such as `el grullense` for `Tacos El Grullense`, alone or after a frame such
|
package/dist/ack.js
CHANGED
|
@@ -140,6 +140,45 @@ const PLACE = '(?:first|second|third|fourth|fifth|sixth|last|other|\\d{1,2}(?:st
|
|
|
140
140
|
const PICK_REF = new RegExp(`^(?:(?:the|that|this) (?:[\\w'$.-]+ ){0,3}one\\b|(?:that|this|those|these|both|it)(?: ones?)?$|the ${PLACE}(?: (?:one|option|item|product|choice|result))?$|${PLACE}(?: (?:one|option|item|product|choice|result)\\b|$)|(?:option|number|item|choice|#) ?#?\\d{1,2}\\b|\\d{1,2}$)`);
|
|
141
141
|
/** A budget word in front of a price, so `under $13.49` stays a search. */
|
|
142
142
|
const BUDGET = /(?:under|below|than|max|maximum|around|about|to|budget|within|over|above|for|most)\s*$/;
|
|
143
|
+
/** What a request for other products opens with before `more like`: `show me more like the KODAK one`, `any more like it`. */
|
|
144
|
+
const MORE_LIKE_LEAD = "(?:(?:ok|okay|hey|hi|so|um|uh|also|and|cool|great|nice|hmm|actually|yes|yeah|can you|could you|can i|could i|i'?d like to|i want to|please)\\s+)*(?:(?:show|find|get|give|send)(?: me| us)?\\s+|(?:i want|i'?d like|i would like|i need|i'?m looking for|im looking for|looking for|search for|look for|do you have|got|have you got|are there|is there|see)\\s+)?(?:some\\s+|any\\s+)?";
|
|
145
|
+
/**
|
|
146
|
+
* Words that ask for other products like one the shopper saw, never for that product: `more like the KODAK AA (60 Pack)`,
|
|
147
|
+
* `something similar`, `alternatives to the Duracell`, `cheaper than the KODAK one`, `like the first one but in blue`, and in
|
|
148
|
+
* Spanish `algo parecido al KODAK`, `más como este`. A number or a price after the words is a correction (`more like $20`,
|
|
149
|
+
* `something like 2`), and stays out.
|
|
150
|
+
*/
|
|
151
|
+
const SIMILAR = [
|
|
152
|
+
new RegExp(`^${MORE_LIKE_LEAD}more (?:like|similar)\\b(?!\\s*(?:\\$|\\d))`),
|
|
153
|
+
/\bmore like (?:the|this|that|these|those|it|them|one|ones)\b/,
|
|
154
|
+
/\b(?:something|anything|stuff|things|ones|others|other ones|other options|options|products|items|other products|other items|more)(?: else)? (?:like|similar to)\b(?!\s*(?:\$|\d))/,
|
|
155
|
+
/\bsimilar (?:to|ones?|items?|products?|options?|stuff|things|styles?|kinds?|types?|brands?|models?)\b|\b(?:something|anything|more|some|any|show me|find|see|got|have|want|need|others?) similar\b|^similar\b/,
|
|
156
|
+
/\b(?:an?|some|any|other|cheaper|good|better|similar) alternatives?\b|\balternatives? (?:to|for|like)\b|^alternatives?\b/,
|
|
157
|
+
/\b(?:cheaper|less expensive|more affordable|lower[- ]priced|lower cost) than\b(?!\s*(?:\$|\d|(?:that|this|my|the) (?:price|budget)))/,
|
|
158
|
+
/\blike (?:the|this|that|these|those|it)\b.*\bbut\b|\bbut (?:cheaper|less expensive|more affordable)\b/,
|
|
159
|
+
/\b(?:anything|something|others?|options?|ones?|products?|items?)(?: else)? (?:other than|besides|apart from)\b/,
|
|
160
|
+
/\bparecid[oa]s?\b|\bsimilar(?:es)? a[ls]?\b|\balgo (?:como|parecido|similar|as[ií])\b(?!\s*(?:\$|\d))|\bm[aá]s (?:como|del estilo)\b|\botr[oa]s? (?:opciones |cosas |productos |marcas )?(?:como|parecid[oa]s?|similares)\b|\balternativas?\b|\bm[aá]s barat[oa]s? que\b|\bcomo (?:el|la|los|las|este|esta|estos|estas|ese|esa)\b.*\bpero\b/,
|
|
161
|
+
];
|
|
162
|
+
/** Text as lowercase words with single spaces, accents kept, as `asksForSimilar` reads it. */
|
|
163
|
+
function similarWords(text) {
|
|
164
|
+
return plain(String(text ?? '')).replace(/[^\p{L}\p{N}\s$#'-]+/gu, ' ').replace(/\s+/g, ' ').trim();
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* True when the shopper's words ask for other products like one they saw, such as `More like the KODAK AA Alkaline Batteries (60
|
|
168
|
+
* Pack)`, `something similar to the first one`, `alternatives to the Duracell` or `cheaper than the KODAK one`: a new search,
|
|
169
|
+
* never a pick of the product it names. `names` are the products the shopper saw: their words are read out first, so a product
|
|
170
|
+
* whose own name says `alternative` or `similar` is still picked by its tap.
|
|
171
|
+
*/
|
|
172
|
+
export function asksForSimilar(text, names = []) {
|
|
173
|
+
let said = ` ${similarWords(text)} `;
|
|
174
|
+
for (const name of names) {
|
|
175
|
+
const own = similarWords(name);
|
|
176
|
+
if (own.length >= 3)
|
|
177
|
+
said = said.split(` ${own} `).join(' ');
|
|
178
|
+
}
|
|
179
|
+
said = said.replace(/\s+/g, ' ').trim();
|
|
180
|
+
return said !== '' && SIMILAR.some((pattern) => pattern.test(said));
|
|
181
|
+
}
|
|
143
182
|
/**
|
|
144
183
|
* True for a pick of a product the shopper already saw: a frame such as `i'll take` or `i want the`, a
|
|
145
184
|
* reference such as `that one`, `the second` or `#2`, or a product title with its price, as a product
|
|
@@ -147,6 +186,9 @@ const BUDGET = /(?:under|below|than|max|maximum|around|about|to|budget|within|ov
|
|
|
147
186
|
* gets no instant reply.
|
|
148
187
|
*/
|
|
149
188
|
function isPick(line, words) {
|
|
189
|
+
// `More like the KODAK AA (60 Pack) ($26.05)` names a card to search for others like it: a new search, with its instant reply.
|
|
190
|
+
if (asksForSimilar(line))
|
|
191
|
+
return false;
|
|
150
192
|
if (/\(\s*\$\s*\d[\d,]*(?:\.\d{2})?\s*\)/.test(line))
|
|
151
193
|
return true;
|
|
152
194
|
const price = line.match(/\$\s*\d[\d,]*\.\d{2}\s*$/);
|
package/dist/buy-tool.d.ts
CHANGED
|
@@ -76,9 +76,16 @@ export interface BuyToolLink {
|
|
|
76
76
|
expiresAt: string | null;
|
|
77
77
|
/**
|
|
78
78
|
* On a store login link: the store's name, or null when Agentcard named none. On an approval link:
|
|
79
|
-
* the store whose cart it approves, when the answer names the cart.
|
|
79
|
+
* the store whose cart it approves, when the answer names the cart. On a product link: the name of
|
|
80
|
+
* the store that sells it, such as `Walmart` or `jadeyoga.com`, or null when Agentcard named none.
|
|
80
81
|
*/
|
|
81
82
|
store?: string | null;
|
|
83
|
+
/**
|
|
84
|
+
* On a product link: the website of the store that sells it, such as `walmart.com` or `jadeyoga.com`, to show on the card
|
|
85
|
+
* as where the product comes from. It is never the host of `url`, which can be a tracking link that names no store. Null
|
|
86
|
+
* when Agentcard knows no website for the store, such as a restaurant, a venue or an airline.
|
|
87
|
+
*/
|
|
88
|
+
storeDomain?: string | null;
|
|
82
89
|
/** On a product link: the product's name. */
|
|
83
90
|
title?: string;
|
|
84
91
|
/**
|
|
@@ -156,6 +163,8 @@ export interface BuyToolProduct {
|
|
|
156
163
|
/** Every photo of the product, `imageUrl` first, at most eight; empty when it has none. */
|
|
157
164
|
images: string[];
|
|
158
165
|
store: string | null;
|
|
166
|
+
/** The website of the store that sells it, such as `walmart.com`, or null when Agentcard knows none. */
|
|
167
|
+
storeDomain?: string | null;
|
|
159
168
|
/** The product's page at its store, or null when Agentcard sent none. */
|
|
160
169
|
url: string | null;
|
|
161
170
|
/**
|
package/dist/buy-tool.js
CHANGED
|
@@ -39,7 +39,7 @@ import { choiceLines, leadsIntoList, messageForShopper, messagesListChoice, repl
|
|
|
39
39
|
import { joinLeadIns, leadInJoiner, messageStream, paceOf, paragraphsOf, sleep, toMessages, typingHook } from './messages.js';
|
|
40
40
|
import { isStale } from './shopper-message.js';
|
|
41
41
|
import { actionLinkId, isActionLink } from './action-links.js';
|
|
42
|
-
import { ACK_REACTION, ackAt, answersCheckout, answersQuestion, holdsBack, instantAck as instantAckOf, isNewRequest, isPickText, keepsYes, namesStore, nextTemplate, readsAsAckLine, slowStepLine } from './ack.js';
|
|
42
|
+
import { ACK_REACTION, ackAt, answersCheckout, answersQuestion, asksForSimilar, holdsBack, instantAck as instantAckOf, isNewRequest, isPickText, keepsYes, namesStore, nextTemplate, readsAsAckLine, slowStepLine } from './ack.js';
|
|
43
43
|
/** The action a product of `merchant` takes (BuyToolProductAction). */
|
|
44
44
|
export function productAction(merchant) {
|
|
45
45
|
if (merchant === 'reservations')
|
|
@@ -210,7 +210,7 @@ function yesAfter(result, yes) {
|
|
|
210
210
|
const seen = yes.shown ?? stopped;
|
|
211
211
|
if (!seen || (yes.shown && stopped && !sameOrder(yes.shown, stopped)))
|
|
212
212
|
return null;
|
|
213
|
-
return { conversationId: result.conversation_id, cart: stopped ?? seen, totalCents: seen.totalCents };
|
|
213
|
+
return { conversationId: result.conversation_id, cart: stopped ?? seen, totalCents: seen.totalCents, consentedHash: seen.hash };
|
|
214
214
|
}
|
|
215
215
|
/** The words of the call a `nextStep` turn's `direct` makes. They never reach Agentcard: the turn's calls send no ask. */
|
|
216
216
|
const NEXT_STEP_ASK = 'take the next step';
|
|
@@ -270,7 +270,22 @@ export function productsOf(turn) {
|
|
|
270
270
|
/** A product's card as `sendLink` hears it: the id the model reads and a pick takes, never the store page, which is `url`. A flight option has no page at a store: its card carries no url, and its productId picks it. */
|
|
271
271
|
function productLink(product) {
|
|
272
272
|
const productId = pickId(product);
|
|
273
|
-
return { kind: 'product', url: product.url || null, title: product.name, price: productPrice(product), ...(productId ? { productId } : {}), action: product.action ?? 'buy', imageUrl: product.imageUrl, images: product.images ?? (product.imageUrl ? [product.imageUrl] : []), expiresAt: null, ...(product.availability ? { availability: product.availability } : {}) };
|
|
273
|
+
return { kind: 'product', url: product.url || null, title: product.name, price: productPrice(product), ...(productId ? { productId } : {}), action: product.action ?? 'buy', imageUrl: product.imageUrl, images: product.images ?? (product.imageUrl ? [product.imageUrl] : []), store: product.store ?? null, storeDomain: product.storeDomain ?? null, expiresAt: null, ...(product.availability ? { availability: product.availability } : {}) };
|
|
274
|
+
}
|
|
275
|
+
/** A host name: dot-separated labels of letters, digits and hyphens, ending in a letters-only top-level label. */
|
|
276
|
+
const STORE_HOST = /^(?=.{1,253}$)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$/;
|
|
277
|
+
/**
|
|
278
|
+
* The website of the store a catalog item is at: the `domain` Agentcard sent, else, from an older API that sends none, the
|
|
279
|
+
* store's id when the id is itself a website (`jadeyoga.com`). Never Channel3's tracking host (buy.trychannel3.com). Null
|
|
280
|
+
* when neither names one: a store id such as `walmart` names no website by itself.
|
|
281
|
+
*/
|
|
282
|
+
function storeDomainOf(store) {
|
|
283
|
+
for (const raw of [store?.domain, store?.id]) {
|
|
284
|
+
const host = typeof raw === 'string' ? raw.trim().toLowerCase().replace(/^www\./, '').replace(/\.$/, '') : '';
|
|
285
|
+
if (STORE_HOST.test(host) && host !== 'trychannel3.com' && !host.endsWith('.trychannel3.com'))
|
|
286
|
+
return host;
|
|
287
|
+
}
|
|
288
|
+
return null;
|
|
274
289
|
}
|
|
275
290
|
function productsOfCatalog(catalog) {
|
|
276
291
|
return (catalog.items ?? [])
|
|
@@ -286,6 +301,7 @@ function productsOfCatalog(catalog) {
|
|
|
286
301
|
images: item.image_urls ?? (item.image_url ? [item.image_url] : []),
|
|
287
302
|
...(item.flight ? { flight: item.flight } : {}),
|
|
288
303
|
store: item.store?.name ?? item.store?.id ?? catalog.merchant_name ?? null,
|
|
304
|
+
storeDomain: storeDomainOf(item.store),
|
|
289
305
|
url: item.url ?? null,
|
|
290
306
|
action: productAction(catalog.merchant),
|
|
291
307
|
linkSent: false,
|
|
@@ -915,8 +931,10 @@ export function buyTool(options) {
|
|
|
915
931
|
let shownStoreNames = [];
|
|
916
932
|
/** True for a pick of a product or a store the shopper saw, by its name. */
|
|
917
933
|
const namesShown = (text) => namesShownProduct(text) || namesStore(text, shownStoreNames);
|
|
934
|
+
/** Whether a message asks for other products like one shown (`More like the <card's name>`): a new search, never a pick. */
|
|
935
|
+
const asksLikeShown = (text) => asksForSimilar(text, [...lastTappable, ...lastAnswerProducts].map((product) => product.name));
|
|
918
936
|
/** What a message picks: a product it names (`product`), a store or a place in a list (`pick`), or nothing (null). */
|
|
919
|
-
const pickOf = (text) => namesStore(text, shownStoreNames) ? 'pick' : namesShownProduct(text) ? 'product' : isPickText(text) ? 'pick' : null;
|
|
937
|
+
const pickOf = (text) => asksLikeShown(text) ? null : namesStore(text, shownStoreNames) ? 'pick' : namesShownProduct(text) ? 'product' : isPickText(text) ? 'pick' : null;
|
|
920
938
|
/** How the shopper's message reads for the instant reply: what it picks, and the reply it gets, or null. */
|
|
921
939
|
const ackOfMessage = (shopperText) => {
|
|
922
940
|
const text = String(shopperText ?? '');
|
|
@@ -1020,7 +1038,7 @@ export function buyTool(options) {
|
|
|
1020
1038
|
};
|
|
1021
1039
|
}
|
|
1022
1040
|
// A confirm, or a pick of what the shopper saw, gets fixed words that never say `looking for`.
|
|
1023
|
-
const slow = step ?? t.picked ?? (!t.neutral && (namesShown(ask) || isPickText(ask)) ? 'pick' : null);
|
|
1041
|
+
const slow = step ?? t.picked ?? (!t.neutral && !asksLikeShown(ask) && (namesShown(ask) || isPickText(ask)) ? 'pick' : null);
|
|
1024
1042
|
// A short answer to the question the last reply asked, such as `medium` after `what size?`, is no
|
|
1025
1043
|
// search: like `yes`, a slow call still tells the shopper, in words that look for nothing.
|
|
1026
1044
|
const neutral = t.neutral || answersQuestion(ask, lastReply);
|
|
@@ -1585,7 +1603,7 @@ export function buyTool(options) {
|
|
|
1585
1603
|
const inPurchase = (at) => at === null || at === conversationId;
|
|
1586
1604
|
if (yes && one && conversationId && inPurchase(yes.conversationId) && sameOrder(one, yes.cart)) {
|
|
1587
1605
|
carriedYes = null;
|
|
1588
|
-
return { body: { confirm: one.hash, conversation_id: conversationId, payment_source: 'vault', ...who }, yes: { ...yes, cart: one, conversationId } };
|
|
1606
|
+
return { body: { confirm: one.hash, conversation_id: conversationId, payment_source: 'vault', carried: { from: yes.picked ? 'pick' : 'yes', cart_hash: yes.consentedHash }, ...who }, yes: { ...yes, cart: one, conversationId } };
|
|
1589
1607
|
}
|
|
1590
1608
|
// The shopper picked what is in the cart and finished the step outside the chat: finishing it is their go-ahead for the
|
|
1591
1609
|
// same order, confirmed by the cart's hash as it stands now, and the approval link stays their consent to the payment
|
|
@@ -1597,7 +1615,7 @@ export function buyTool(options) {
|
|
|
1597
1615
|
if (picked?.cart && one && conversationId && inPurchase(picked.conversationId) && (event.kind === 'card_added' || event.kind === 'store_connected')) {
|
|
1598
1616
|
pickedOrder = null;
|
|
1599
1617
|
if (sameOrder(one, picked.cart)) {
|
|
1600
|
-
return { body: { confirm: one.hash, conversation_id: conversationId, payment_source: 'vault', ...who }, yes: { conversationId, cart: one, totalCents: picked.cart.totalCents, picked: true } };
|
|
1618
|
+
return { body: { confirm: one.hash, conversation_id: conversationId, payment_source: 'vault', carried: { from: 'pick', cart_hash: picked.cart.hash }, ...who }, yes: { conversationId, cart: one, totalCents: picked.cart.totalCents, consentedHash: picked.cart.hash, picked: true } };
|
|
1601
1619
|
}
|
|
1602
1620
|
}
|
|
1603
1621
|
// A checkout the shopper already moved past their yes (an approval link waiting, or the order going through) is reported as
|
|
@@ -1640,7 +1658,9 @@ export function buyTool(options) {
|
|
|
1640
1658
|
if (!confirm && ask === '')
|
|
1641
1659
|
throw new TypeError("Put the shopper's words in ask.");
|
|
1642
1660
|
// A pick chooses from the products or stores this conversation showed: never on a confirm, and never on a new order, which shows none yet.
|
|
1643
|
-
|
|
1661
|
+
// A request for other products like one shown is a new search: a `product_id` or `store_id` the model sent beside it would
|
|
1662
|
+
// put that product in the cart (2026-10-27, "More like <a card's whole title>" on 0.18.0), so none goes.
|
|
1663
|
+
const picks = !confirm && !newOrder && !asksLikeShown(ask);
|
|
1644
1664
|
const pick = picks && input.product_id ? { product_id: input.product_id } : picks && input.store_id ? { store_id: input.store_id } : null;
|
|
1645
1665
|
const body = {
|
|
1646
1666
|
resume: true,
|
package/dist/client.d.ts
CHANGED
|
@@ -250,6 +250,15 @@ export interface BuyRequest {
|
|
|
250
250
|
hand_back?: true;
|
|
251
251
|
/** With `hand_back`: what Agentcard's last answer showed and asked, which decides whether a pick or an answer is shopping. */
|
|
252
252
|
last_answer?: BuyLastAnswer;
|
|
253
|
+
/**
|
|
254
|
+
* On a confirm the app sends on its own after a step outside the chat (a saved card, a store sign-in): the go-ahead it
|
|
255
|
+
* carries, a `yes` to a cart or the `pick` of what is in it, and `cart_hash`, the cart it was given for. Agentcard pays
|
|
256
|
+
* only that cart; a cart that changed since is answered with its total and `next_action: 'confirm'`, and nothing is paid.
|
|
257
|
+
*/
|
|
258
|
+
carried?: {
|
|
259
|
+
from: 'yes' | 'pick';
|
|
260
|
+
cart_hash: string;
|
|
261
|
+
};
|
|
253
262
|
}
|
|
254
263
|
/** What the shopper's last answer from Agentcard showed and asked, sent with `hand_back`. */
|
|
255
264
|
export interface BuyLastAnswer {
|
|
@@ -267,9 +276,14 @@ export interface BuyLastAnswer {
|
|
|
267
276
|
}>;
|
|
268
277
|
}
|
|
269
278
|
export type BuyStatus = 'needs_input' | 'needs_approval' | 'order_placed' | 'partially_placed' | 'declined';
|
|
279
|
+
/**
|
|
280
|
+
* The store a product or a cart is at. `domain` is the store's own website, such as `walmart.com` or `jadeyoga.com`: on a
|
|
281
|
+
* catalog item, null when Agentcard knows none (a restaurant, a venue, an airline), and absent from an older API and on a cart.
|
|
282
|
+
*/
|
|
270
283
|
export interface BuyStore {
|
|
271
284
|
id: string;
|
|
272
285
|
name?: string;
|
|
286
|
+
domain?: string | null;
|
|
273
287
|
}
|
|
274
288
|
export interface BuyCartLine {
|
|
275
289
|
name: string;
|
package/dist/client.js
CHANGED
|
@@ -136,7 +136,7 @@ function imagesOf(main, list) {
|
|
|
136
136
|
function storeOf(v) {
|
|
137
137
|
if (!isRecord(v) || !str(v.id))
|
|
138
138
|
return null;
|
|
139
|
-
return { id: v.id, ...(str(v.name) ? { name: v.name } : {}) };
|
|
139
|
+
return { id: v.id, ...(str(v.name) ? { name: v.name } : {}), ...(Object.hasOwn(v, 'domain') ? { domain: str(v.domain) } : {}) };
|
|
140
140
|
}
|
|
141
141
|
function feeLinesOf(v) {
|
|
142
142
|
if (!Array.isArray(v))
|
package/dist/direct.js
CHANGED
|
@@ -12,7 +12,11 @@
|
|
|
12
12
|
* answer such as `medium` or `size 9`, or a plain yes to the total it asked the shopper to confirm;
|
|
13
13
|
* - a price limit for the products Agentcard just showed: `only options under $20`, `under 20`, `cheaper ones`.
|
|
14
14
|
* Agentcard holds its cards to the limit and shows the ones inside it again as cards; your model, answering
|
|
15
|
-
* from the list it saw, named two products with no card to tap (2026-10-27)
|
|
15
|
+
* from the list it saw, named two products with no card to tap (2026-10-27);
|
|
16
|
+
* - a request for other products like one Agentcard just showed: `More like the KODAK AA Alkaline Batteries (60 Pack)`,
|
|
17
|
+
* `something similar to the first one`, `cheaper than the KODAK one`. It goes as a new search with no `product_id`:
|
|
18
|
+
* on 2026-10-27 (the playground chat on 0.18.0, production /buy) "More like <a card's whole title>" went as a pick of
|
|
19
|
+
* that card, and Agentcard put it in the cart and asked for the shipping address.
|
|
16
20
|
*
|
|
17
21
|
* Everything else goes to the model: a question, small talk, a change of mind (`no`, `wait`, `cancel`),
|
|
18
22
|
* a command to the cart, and anything about the account, an order or how the chat works. A pick or an
|
|
@@ -23,7 +27,7 @@
|
|
|
23
27
|
* purchase reply to small talk, while one the model reads by mistake costs only the model's call. On
|
|
24
28
|
* the 188 labelled messages in `test/fixtures/direct-messages.json`, it sends none it should not.
|
|
25
29
|
*/
|
|
26
|
-
import { ABOUT, ACKNOWLEDGED, CAPABILITY, CART_COMMAND, CHECKOUT, CHITCHAT, FILLER, asksForDetails, isNewRequest, isPickText, namesStore, plain } from './ack.js';
|
|
30
|
+
import { ABOUT, ACKNOWLEDGED, CAPABILITY, CART_COMMAND, CHECKOUT, CHITCHAT, FILLER, asksForDetails, asksForSimilar, isNewRequest, isPickText, namesStore, plain } from './ack.js';
|
|
27
31
|
/**
|
|
28
32
|
* A price limit put on what was just shown: `under $20`, `under 20`, `less than 15 bucks`, `between 10 and 20`, `$20 or
|
|
29
33
|
* less`, `over $50`, `cheaper ones`, `menos de 20`. Only whether the message states one: Agentcard reads the limit itself.
|
|
@@ -164,6 +168,9 @@ function pickOf(raw, bare, context) {
|
|
|
164
168
|
const byName = context.tappable?.length ? context.tappable : products;
|
|
165
169
|
const stores = context.stores ?? [];
|
|
166
170
|
const call = (fields) => ({ input: { ask: raw, confirm: false, new_order: false, product_id: null, store_id: null, ...fields }, reason: 'pick' });
|
|
171
|
+
// `More like the <card's whole name>` names a card to find others like it: it picks nothing.
|
|
172
|
+
if (asksForSimilar(raw, [...byName, ...products].map((product) => product.name)))
|
|
173
|
+
return null;
|
|
167
174
|
if (stores.length > 0) {
|
|
168
175
|
const said = ` ${nameWords(raw)} `;
|
|
169
176
|
// A store's card names it whole; a store named with the dishes to order from it (`five guys, bacon cheeseburger`) picks it too.
|
|
@@ -236,6 +243,12 @@ export function directCall(shopperText, context = {}) {
|
|
|
236
243
|
const told = CHITCHAT.test(words) || ABOUT.test(words) || CART_COMMAND.test(words) || CAPABILITY.test(words);
|
|
237
244
|
if (ctx.nextAction === 'confirm' && isYes(words))
|
|
238
245
|
return plainCall('confirm', true);
|
|
246
|
+
// A request for other products like one just shown is a new search, never a pick of the one it names: it goes with no
|
|
247
|
+
// `product_id`, and Agentcard searches for products like it and leaves it out.
|
|
248
|
+
const shown = [...(ctx.tappable ?? []), ...(ctx.products ?? [])];
|
|
249
|
+
if (!told && (shown.length > 0 || (ctx.stores ?? []).length > 0) && asksForSimilar(raw, shown.map((product) => product.name))) {
|
|
250
|
+
return NOT_SHOPPING.test(words) || TAKEBACK.test(words) ? null : plainCall('product_ask');
|
|
251
|
+
}
|
|
239
252
|
// A question, a story of what happened, an objection or a change of mind is the model's to answer, whatever came before.
|
|
240
253
|
const aside = QUESTION_START.test(bare) || REFERS.test(words) || COMPLAINT.test(words) || PAST.test(words) || NOT_SHOPPING.test(words) || OBJECTION.test(words) || CORRECTION.test(words);
|
|
241
254
|
if (!told && !aside && !TAKEBACK.test(words)) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agent-cards/commerce-sdk",
|
|
3
|
-
"version": "0.18.
|
|
3
|
+
"version": "0.18.2",
|
|
4
4
|
"description": "Give your agent one tool that buys for your shoppers at real stores, and post each purchase update Agentcard sends into the shopper's chat, over a connection your server opens or a webhook endpoint.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|