Spaces:
Running
Running
Download src/lib/bots/engine.ts from varunm2004/botcompany: direct link, hf CLI and curl.
- Browser
- Download file 4.43 kB
-
https://huggingface.co/spaces/varunm2004/botcompany/resolve/main/src/lib/bots/engine.ts
- Command line
-
hf download hf://spaces/varunm2004/botcompany/src/lib/bots/engine.ts
-
curl -L -o engine.ts https://huggingface.co/spaces/varunm2004/botcompany/resolve/main/src/lib/bots/engine.ts
4.43 kB
| // BotCompany β pure rule-based chat engine. | |
| // Intent classification (regex) + persona-aware bot replies + deterministic scoring. | |
| // Thresholds: score >= 65 -> READY, 35..64 -> WAITING, < 35 -> CANCELLED. | |
| import { OFFER, PORTFOLIO, portfolioMessage } from "./business"; | |
| import type { Intent, Lead, LeadStatus } from "./types"; | |
| // ---------- intent classification ---------- | |
| const RX: Array<[Intent, RegExp]> = [ | |
| ["STOP", /\b(stop|do not (msg|message|text)|remove my number|unsubscribe|block)\b/i], | |
| ["NO", /\b(no|not interested|nope|nahi|later|not now|don'?t need|do not need|pass)\b/i], | |
| ["PRICE", /\b(price|cost|charge|kitna|rate|budget|how much|fees|quotation)\b/i], | |
| ["TIME", /\b(time|timeline|how long|delivery|days|week|duration|kab tak)\b/i], | |
| ["YES", /\b(yes|ok(ay)?|done|deal|i am in|let us|let's|haan|sure|proceed|go ahead|start)\b/i], | |
| ["INTEREST", /\b(sample|portfolio|example|show|work|site|website|interested|share|send|demo)\b/i], | |
| ]; | |
| export function classifyIntent(text: string): Intent { | |
| for (const [intent, rx] of RX) { | |
| if (rx.test(text)) return intent; | |
| } | |
| return "NEUTRAL"; | |
| } | |
| // ---------- bot replies ---------- | |
| export const PRICE_ANSWER = [ | |
| `Flat ${OFFER.priceLabel} for a complete website β no monthly fee from me for the build.`, | |
| `Hosting + domain is a small add-on: ${OFFER.addOn}, billed monthly. Cancel anytime.`, | |
| `Delivery in ${OFFER.delivery}. Include: ${OFFER.includes[0]}, WhatsApp button, mobile-first pages.`, | |
| `Shall I send the proposal?`, | |
| ].join("\n"); | |
| export const TIME_ANSWER = [ | |
| `Typical build: ${OFFER.delivery}.`, | |
| `Day 1-2 content + design lock, then build, then your review.`, | |
| ].join("\n"); | |
| export function botReply(intent: Intent, lead: Lead): string[] { | |
| const name = lead.owner; | |
| switch (intent) { | |
| case "PRICE": | |
| return [PRICE_ANSWER]; | |
| case "TIME": | |
| return [TIME_ANSWER]; | |
| case "YES": | |
| return [ | |
| `Great ${name} ji π I will WhatsApp you the proposal today. One question β which of my 4 demo styles do you like?`, | |
| portfolioMessage(), | |
| ]; | |
| case "INTEREST": | |
| return [ | |
| portfolioMessage(), | |
| `All 4 are live client sites. For ${lead.business} I would do a similar style with your photos + WhatsApp enquiry button.`, | |
| ]; | |
| case "NO": | |
| return [ | |
| `No problem ${name} ji π If you ever need a site, message me anytime. Have a good day!`, | |
| ]; | |
| case "STOP": | |
| return [`Understood β you will not hear from me again. Sorry to disturb. π`]; | |
| default: | |
| return [ | |
| `Quick context: I build websites for ${lead.niche.toLowerCase()}s β like ${PORTFOLIO[2].name} and ${PORTFOLIO[1].name}. Want samples?`, | |
| ]; | |
| } | |
| } | |
| // ---------- scoring ---------- | |
| export const SCORE_DELTA: Record<Intent, number> = { | |
| YES: 25, | |
| INTEREST: 15, | |
| PRICE: 10, | |
| TIME: 10, | |
| NEUTRAL: 2, | |
| NO: -15, | |
| STOP: -100, | |
| }; | |
| export function applyScore(lead: Lead, intent: Intent): number { | |
| lead.score = Math.max(0, Math.min(100, lead.score + SCORE_DELTA[intent])); | |
| return lead.score; | |
| } | |
| export function decideStatus(lead: Lead): LeadStatus { | |
| if (lead.score <= 20) return "CANCELLED"; | |
| if (lead.score >= 65 && lead.turns >= 2) return "READY"; | |
| if (lead.turns >= 5) return lead.score >= 35 ? "WAITING" : "CANCELLED"; | |
| return "IN_CHAT"; | |
| } | |
| /** Farewell / hand-off message appended when a terminal status is set. */ | |
| export function farewellFor(status: LeadStatus, lead: Lead): string | null { | |
| switch (status) { | |
| case "READY": | |
| return [ | |
| `Perfect ${lead.owner} ji β I am sending the full proposal on WhatsApp now π²`, | |
| lead.callSlot | |
| ? `My developer Varun will call you at your allocated slot: ${lead.callSlot}. Thank you!` | |
| : `My developer Varun will call you shortly to lock the design. Thank you!`, | |
| ].join("\n"); | |
| case "WAITING": | |
| return [ | |
| `No rush ${lead.owner} ji β I will follow up next week with more samples. π`, | |
| ].join("\n"); | |
| case "CANCELLED": | |
| return `Thanks for your time ${lead.owner} ji π All the best for ${lead.business}!`; | |
| default: | |
| return null; | |
| } | |
| } | |
| /** Human label used in timeline events. */ | |
| export const INTENT_LABEL: Record<Intent, string> = { | |
| YES: "agreed to proceed", | |
| NO: "declined", | |
| STOP: "asked to stop", | |
| PRICE: "asked for price", | |
| TIME: "asked for timeline", | |
| INTEREST: "asked for samples", | |
| NEUTRAL: "neutral reply", | |
| }; | |