@akropolys/kiku 1.5.15 → 1.5.18

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/dist/index.mjs CHANGED
@@ -1507,6 +1507,18 @@ var getFriendlyError = (err) => {
1507
1507
  return str;
1508
1508
  }
1509
1509
  };
1510
+ var MK_ADJ = ["flying", "silent", "golden", "lucky", "brave", "swift", "royal", "cosmic"];
1511
+ var MK_NOUN = ["thunder", "falcon", "river", "ember", "tiger", "comet", "willow", "onyx"];
1512
+ function generateMagicWord() {
1513
+ return MK_ADJ[Math.floor(Math.random() * MK_ADJ.length)] + MK_NOUN[Math.floor(Math.random() * MK_NOUN.length)];
1514
+ }
1515
+ function parseMemoryKey(raw) {
1516
+ const cleaned = raw.trim().replace(/\s+/g, "");
1517
+ const m = cleaned.match(/^(\d{6,})([a-zA-Z].*)?$/);
1518
+ const phone = m?.[1] || cleaned.replace(/\D/g, "");
1519
+ const word = (m?.[2] || "").toLowerCase() || generateMagicWord();
1520
+ return { phone, word };
1521
+ }
1510
1522
  function parseThinking(text) {
1511
1523
  const openMatch = text.match(/<\s*thinking\s*>/i);
1512
1524
  if (!openMatch) {
@@ -1648,17 +1660,34 @@ function ChatModal({
1648
1660
  const SR = window.SpeechRecognition || window.webkitSpeechRecognition;
1649
1661
  const recognition = new SR();
1650
1662
  recognition.lang = "en-US";
1651
- recognition.interimResults = false;
1663
+ recognition.interimResults = true;
1652
1664
  recognition.maxAlternatives = 1;
1653
1665
  recognitionRef.current = recognition;
1654
1666
  recognition.onstart = () => setVoiceState("listening");
1655
1667
  recognition.onresult = (event) => {
1656
- const transcript = event.results[0][0].transcript;
1657
- setInput(transcript);
1658
- pendingVoiceRef.current = transcript;
1659
- setVoiceState("processing");
1668
+ let finalText = "";
1669
+ let interimText = "";
1670
+ for (let i = 0; i < event.results.length; i++) {
1671
+ const seg = event.results[i][0].transcript;
1672
+ if (event.results[i].isFinal) finalText += seg;
1673
+ else interimText += seg;
1674
+ }
1675
+ const live = (finalText + " " + interimText).trim();
1676
+ if (live) setInput(live);
1677
+ if (finalText.trim()) {
1678
+ pendingVoiceRef.current = finalText.trim();
1679
+ setVoiceState("processing");
1680
+ }
1681
+ };
1682
+ recognition.onerror = (event) => {
1683
+ const err = event?.error;
1684
+ if (err === "not-allowed" || err === "service-not-allowed") {
1685
+ setInput("Microphone access was blocked \u2014 enable it in your browser to use voice.");
1686
+ } else if (err === "no-speech") {
1687
+ setInput("Didn't catch that \u2014 tap the mic and try again.");
1688
+ }
1689
+ setVoiceState("idle");
1660
1690
  };
1661
- recognition.onerror = () => setVoiceState("idle");
1662
1691
  recognition.onend = () => {
1663
1692
  setVoiceState((prev) => prev === "listening" ? "idle" : prev);
1664
1693
  };
@@ -1686,6 +1715,7 @@ function ChatModal({
1686
1715
  });
1687
1716
  const [merchantRef, setMerchantRef] = useState5(null);
1688
1717
  const [paymentPhase, setPaymentPhase] = useState5("idle");
1718
+ const [memoryKey, setMemoryKey] = useState5(null);
1689
1719
  const [sessions, setSessions] = useState5(() => loadSessions());
1690
1720
  const [sidebarOpen, setSidebarOpen] = useState5(false);
1691
1721
  const [replayMessages, setReplayMessages] = useState5(null);
@@ -1721,26 +1751,14 @@ function ChatModal({
1721
1751
  } : void 0;
1722
1752
  const handlePhoneSubmit = async () => {
1723
1753
  if (!phoneInput.trim()) return;
1724
- try {
1725
- if (typeof window !== "undefined") {
1726
- localStorage.setItem("akropolys_user_phone", phoneInput.trim());
1727
- }
1728
- const isHistoryIntent = lastIntent === "capture" || lastIntent === "delete" || lastIntent === "view_history";
1729
- if (isHistoryIntent) {
1730
- setPaymentPhase("idle");
1731
- const lastUserMsg = [...messages].reverse().find((m) => m.role === "user");
1732
- if (lastUserMsg) {
1733
- await send(lastUserMsg.content);
1734
- }
1735
- return;
1736
- }
1737
- const res = await client.api.initiatePayment(phoneInput.trim());
1738
- setMerchantRef(res.merchantReference);
1739
- setPaymentPhase("awaiting");
1740
- } catch (e) {
1741
- console.error("[Akropolys] initiatePayment error", e);
1742
- setPaymentPhase("failed");
1743
- }
1754
+ const { phone, word } = parseMemoryKey(phoneInput);
1755
+ if (!phone) return;
1756
+ if (typeof window !== "undefined") localStorage.setItem("akropolys_user_phone", phone);
1757
+ client.setShopperIdentity(phone, word);
1758
+ setMemoryKey(phone + word);
1759
+ setPaymentPhase("idle");
1760
+ const lastUserMsg = [...messages].reverse().find((m) => m.role === "user");
1761
+ if (lastUserMsg) await send(lastUserMsg.content);
1744
1762
  };
1745
1763
  const msgsContainerRef = useRef5(null);
1746
1764
  useEffect3(() => {
@@ -2029,31 +2047,41 @@ function ChatModal({
2029
2047
  ] })
2030
2048
  ] }),
2031
2049
  error && /* @__PURE__ */ jsx7("div", { className: "hsk-cb-error", children: getFriendlyError(error) }),
2032
- !replayMessages && paymentPhase === "prompt_phone" && (() => {
2033
- const isHistoryIntent = lastIntent === "capture" || lastIntent === "delete" || lastIntent === "view_history";
2034
- if (isHistoryIntent) {
2035
- return null;
2036
- }
2037
- return /* @__PURE__ */ jsxs6("div", { className: "hsk-cb-ai-msg", children: [
2038
- /* @__PURE__ */ jsx7("div", { className: "hsk-cb-ai-icon", style: { display: "flex", alignItems: "center" }, children: /* @__PURE__ */ jsx7(SparkleIcon2, {}) }),
2039
- /* @__PURE__ */ jsx7("div", { className: "hsk-cb-ai-body", children: /* @__PURE__ */ jsx7("div", { className: "hsk-cb-ai-text", children: /* @__PURE__ */ jsxs6("div", { className: "hsk-cb-phone-form", children: [
2040
- /* @__PURE__ */ jsx7("label", { className: "hsk-cb-phone-label", children: "Enter your M-Pesa number to pay" }),
2041
- /* @__PURE__ */ jsx7(
2042
- "input",
2043
- {
2044
- type: "tel",
2045
- className: "hsk-cb-phone-input",
2046
- placeholder: "0712 345 678",
2047
- value: phoneInput,
2048
- onChange: (e) => setPhoneInput(e.target.value.replace(/\D/g, "")),
2049
- onKeyDown: (e) => e.key === "Enter" && handlePhoneSubmit(),
2050
- autoFocus: true
2050
+ !replayMessages && paymentPhase === "prompt_phone" && /* @__PURE__ */ jsxs6("div", { className: "hsk-cb-ai-msg", children: [
2051
+ /* @__PURE__ */ jsx7("div", { className: "hsk-cb-ai-icon", style: { display: "flex", alignItems: "center" }, children: /* @__PURE__ */ jsx7(SparkleIcon2, {}) }),
2052
+ /* @__PURE__ */ jsx7("div", { className: "hsk-cb-ai-body", children: /* @__PURE__ */ jsx7("div", { className: "hsk-cb-ai-text", children: /* @__PURE__ */ jsxs6("div", { className: "hsk-cb-phone-form", children: [
2053
+ /* @__PURE__ */ jsx7("label", { className: "hsk-cb-phone-label", children: "Your phone number \u2014 or paste your memory key" }),
2054
+ /* @__PURE__ */ jsx7(
2055
+ "input",
2056
+ {
2057
+ type: "text",
2058
+ className: "hsk-cb-phone-input",
2059
+ placeholder: "0712345678 or 0712345678flyingthunder",
2060
+ value: phoneInput,
2061
+ onChange: (e) => setPhoneInput(e.target.value.replace(/[^0-9a-zA-Z]/g, "")),
2062
+ onKeyDown: (e) => e.key === "Enter" && handlePhoneSubmit(),
2063
+ autoFocus: true
2064
+ }
2065
+ ),
2066
+ /* @__PURE__ */ jsx7("button", { className: "hsk-cb-phone-submit", onClick: handlePhoneSubmit, children: "Save & sync my items" })
2067
+ ] }) }) })
2068
+ ] }),
2069
+ !replayMessages && memoryKey && /* @__PURE__ */ jsxs6("div", { className: "hsk-cb-ai-msg", children: [
2070
+ /* @__PURE__ */ jsx7("div", { className: "hsk-cb-ai-icon", style: { display: "flex", alignItems: "center" }, children: /* @__PURE__ */ jsx7(SparkleIcon2, {}) }),
2071
+ /* @__PURE__ */ jsx7("div", { className: "hsk-cb-ai-body", children: /* @__PURE__ */ jsx7("div", { className: "hsk-cb-ai-text", children: /* @__PURE__ */ jsxs6("div", { style: { padding: "10px 12px", border: "1px solid var(--hsk-border, #e5e5e5)", borderRadius: 8 }, children: [
2072
+ /* @__PURE__ */ jsx7("div", { style: { fontSize: 12, fontWeight: 600, marginBottom: 4 }, children: "\u{1F511} Your memory key" }),
2073
+ /* @__PURE__ */ jsx7("code", { style: { display: "block", fontSize: 14, fontWeight: 700, marginBottom: 6, wordBreak: "break-all" }, children: memoryKey }),
2074
+ /* @__PURE__ */ jsxs6("div", { style: { display: "flex", gap: 8, alignItems: "center" }, children: [
2075
+ /* @__PURE__ */ jsx7("button", { className: "hsk-cb-phone-submit", style: { padding: "4px 10px" }, onClick: () => {
2076
+ try {
2077
+ navigator.clipboard?.writeText(memoryKey);
2078
+ } catch {
2051
2079
  }
2052
- ),
2053
- /* @__PURE__ */ jsx7("button", { className: "hsk-cb-phone-submit", onClick: handlePhoneSubmit, children: "Send STK push" })
2054
- ] }) }) })
2055
- ] });
2056
- })(),
2080
+ }, children: "Copy" }),
2081
+ /* @__PURE__ */ jsx7("span", { style: { fontSize: 11, opacity: 0.7 }, children: "Save it \u2014 enter it on any site to get your items back." })
2082
+ ] })
2083
+ ] }) }) })
2084
+ ] }),
2057
2085
  !replayMessages && paymentPhase === "awaiting" && /* @__PURE__ */ jsxs6("div", { className: "hsk-cb-payment-prompt hsk-cb-payment-prompt--awaiting", children: [
2058
2086
  /* @__PURE__ */ jsx7("div", { className: "hsk-cb-payment-pulse-ring" }),
2059
2087
  /* @__PURE__ */ jsx7("div", { className: "hsk-cb-payment-icon-wrap", children: /* @__PURE__ */ jsx7("span", { style: { fontSize: "2rem" }, children: "\u{1F4F1}" }) }),
@@ -2150,7 +2178,7 @@ function ChatModal({
2150
2178
  value: input,
2151
2179
  onChange: handleInput,
2152
2180
  onKeyDown: handleKeyDown,
2153
- placeholder: activePlaceholder,
2181
+ placeholder: voiceState === "listening" ? "\u{1F399}\uFE0F Listening\u2026 tap the mic to stop" : voiceState === "processing" ? "Got it \u2014 sending\u2026" : activePlaceholder,
2154
2182
  rows: 1,
2155
2183
  disabled: loading,
2156
2184
  autoFocus: true