Pet Store Sales Agent
How I built a sales agent, a bilingual chat assistant that recommends real products, quotes exact prices and places cash-on-delivery orders, without letting the AI touch the money.

Most "AI shop assistants" I've tried have the same problem: they sound
confident and get the price wrong. For a real shop that's a real problem. If
the bot tells a customer a bag of kitten food costs ৳550 and the shop charges
৳600, the shop has a complaint. If the bot invents a 30% discount, the shop
loses money.
So when I built chat-pawfect, a chat assistant for an online pet shop in
Bangladesh, I started from one rule: the model talks, the code decides. You
can try it live at chat-pawfect.sheihoise.com
(https://chat-pawfect.sheihoise.com/).

What it does
A customer opens a small chat window on the website and talks to it like
they'd talk to someone at the counter. They can:
- Ask what food suits a 3-month-old kitten, or compare pack sizes
- See real products as cards, with photo, price and an Add to order button
- Get an exact quote, including the delivery charge for their area
- Place a cash-on-delivery order, then check its status later
- Do all of this in English, Bangla or Banglish
Behind the window there's a React app, a small Node server, Google Gemini,
nine tools and MongoDB. The catalog is the shop's real export: 383 products,
which become 419 orderable items.
The architecture

- Chat app (React + Vite): message bubbles, product cards, order summary and
confirmation cards.
- HTTP server (src/server.ts): one endpoint, POST /api/chat, which answers as
Server-Sent Events.
- Sales agent (src/agent.ts): the persona, the conversation history and the
tool loop.
- Gemini via @google/genai: understands the customer, asks questions, picks
tools and writes the reply.
- Tools + MongoDB (src/tools.ts): the only way anything touches products,
customers, discounts or orders.
Life of a message
Here's what happens when a customer types "I need food for my kitten" and
presses send.
1. The browser POSTs { sessionId, message } to /api/chat. The session ID lives
in sessionStorage, so a refresh keeps the conversation and a new tab
starts fresh.
2. The server rejects empty messages, anything over 2,000 characters and bad
JSON. If that session is still answering, it returns 409 so two replies
can't get mixed together.
3. The server opens an event stream and the agent calls Gemini with the system
prompt, the full history and the nine tool declarations.
4. Gemini writes text, asks for tool calls such as search_products({ query:
"kitten food" }), or both.
5. The agent runs each tool, streams a tool event ("Searching the shop…") and,
if the tool produced one, a ui card.
6. That loop repeats until Gemini answers without asking for more tools, up to
15 steps. Then the server sends done.

The stream carries six event types: session, text, tool, ui, error and done.
Text streams in as it's generated, and cards arrive in the same order they
happened, so a reply can be "some text, a row of products, more text".
Tools are the only way to act
Gemini can reach the shop through exactly nine tools. Each one has a
description written for the model, a Zod schema and a function. The schema is
converted to JSON Schema for Gemini, then used again to validate the arguments
when the call comes back.
- search_products, get_product, show_products, check_stock: find and display
products
- list_offerable_discounts, quote: work out what the customer would pay
- save_customer_details, create_order, get_order_status: take and look up
orders
A tool returns two things: data, which goes to the model as JSON, and an
optional UI card, which goes only to the browser. That split matters. The
model sees what it needs to reason, and the customer sees a proper product
card instead of a wall of text.

If a tool rejects its input (an unknown SKU, a bad phone number, not enough
stock), the agent doesn't throw. It sends { error: "…" } back to Gemini, which
then explains the problem to the customer or fixes the call.
The code does all the money math
Every price, delivery charge and total comes from one function, priceCart. It
re-reads everything from the database on every call, and both quote and
create_order use it. So the total the customer agreed to and the total they're
charged are always the same number.
- Money is stored as whole poisha (1 taka = 100 poisha), so there are no
floating-point rounding errors.
- Duplicate SKUs are merged before stock is checked, so "1 bag" plus "1 bag"
is checked as 2.
- Promo codes must exist, be active, be unexpired and meet their minimum
spend, and they're capped by MAX_DISCOUNT_PERCENT.
- Total = subtotal − discount + delivery.

Inside or outside Dhaka?
Delivery costs more outside Dhaka city, and this is where an LLM is tempting
and wrong. "Mirpur" sounds like Dhaka, but Kushtia has its own Mirpur. "Savar,
Dhaka" has Dhaka in it, but couriers price Savar as outside.
So src/delivery.ts decides the zone in code, never the model:
1. Suburbs first. If Savar, Ashulia, Keraniganj, Gazipur, Tongi, Narayanganj
and similar appear anywhere, it's outside.
2. Then city, area, street, in that order. The first field that names a known
place decides.
3. Whole words only. "Bandarban" doesn't match a Dhaka area.
4. No guessing. If no known place is found, the tool refuses and the agent
asks for the city or district.
About 150 Dhaka city areas are listed, in English and Bangla.

Placing an order safely
The ordering flow is fixed: agree on items, collect details, quote, get a
clear yes, then place the order. If anything changes after the quote, the
agent quotes again.
The prompt asks for that, but the code enforces it:
- create_order refuses unless customer details were saved in this conversation
and customerConfirmed is literally true.
- Stock is reserved per SKU in a single atomic database operation (decrease
only if stock ≥ quantity), so two customers can't both buy the last bag.
- If any item sold out in the meantime, or saving the order fails, every
reservation already made is put back.
Simplified, the reservation step looks like this:
// Atomic per-SKU reservation: only decrements if enough stock is left
const res = await products.updateOne(
{ sku, stock: { $gte: quantity } },
{ $inc: { stock: -quantity } },
);
if (res.modifiedCount === 0) {
await rollback(reserved); // put back everything reserved so far
return { error: `${sku} just sold out` };
}
Speaking the customer's language
Many customers in Bangladesh write in Bangla script, and many write Bangla in
English letters. The agent replies in whatever the customer used: Bangla gets
Bangla, English gets English, and Banglish gets a casual mix. Product names
stay exactly as they are in the catalog so they still match the cards.
Search works in both languages too. Results are ranked by how many query words
match, then by matches in the product name, then by stock.

Staying in its lane
A shop assistant shouldn't write homework or explain politics. Off-topic
requests get one short, friendly decline and an offer to help with the
customer's pet. Requests to "ignore your instructions", switch roles or enter
"developer mode" are ignored, and it never reveals its instructions.

The prompt can only do so much, though. The rules that really matter are in
the tools:
- Order lookups need both the order number and the matching phone number.
- Saving details with an existing phone number never overwrites that
customer's stored profile.
- The cost price (tp_price) is never imported, so the model can't leak
margins.
- Every argument is validated: quantities 1–50, at most 25 lines, phone and
email formats, length limits.
- Tool results are marked as data, so text inside a product description can't
redirect the agent.
Things I learned
- Keep the model's whole turn. Gemini returns hidden "thought signature" data
with function calls. If you don't send it back on the next step, multi-step
tool use breaks in confusing ways.
- Run tool calls in order. When Gemini asks for several at once, I run them
one at a time so an order can never race the saving of customer details.
- Errors are better as data. Returning { error } to the model gets you a
helpful sentence for the customer instead of a crash.
- Show friendly errors, log real ones. Customers see "Sorry, something went
wrong on our side". The server terminal shows the 429 or 503 from Gemini.
- Smaller models skip UI tools. Lite models sometimes describe products in
text instead of calling show_products. A larger Flash model follows that
instruction more reliably.