@mushi-mushi/core 1.14.0 → 1.16.0

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.js CHANGED
@@ -41,6 +41,7 @@ function formatBytes(n) {
41
41
  }
42
42
 
43
43
  // src/api-client.ts
44
+ var MUSHI_USER_TOKEN_HEADER = "X-Mushi-User-Token";
44
45
  var DEFAULT_API_ENDPOINT = "https://dxptnwrhwsqckaftyymj.supabase.co/functions/v1/api";
45
46
  var MUSHI_INTERNAL_HEADER = "X-Mushi-Internal";
46
47
  var MUSHI_INTERNAL_INIT_MARKER = "__mushiInternal";
@@ -52,13 +53,15 @@ function createApiClient(options) {
52
53
  apiKey,
53
54
  apiEndpoint = DEFAULT_API_ENDPOINT,
54
55
  timeout = DEFAULT_TIMEOUT,
55
- maxRetries = DEFAULT_MAX_RETRIES
56
+ maxRetries = DEFAULT_MAX_RETRIES,
57
+ getUserToken
56
58
  } = options;
57
59
  let baseUrl = apiEndpoint.replace(/\/$/, "");
58
60
  async function request(method, path, body, retries = maxRetries, internalKind, extraHeaders) {
59
61
  const url = `${baseUrl}${path}`;
60
62
  const controller = new AbortController();
61
63
  const timer = setTimeout(() => controller.abort(), timeout);
64
+ const userToken = getUserToken?.() ?? null;
62
65
  try {
63
66
  const response = await fetch(url, {
64
67
  method,
@@ -66,6 +69,7 @@ function createApiClient(options) {
66
69
  "Content-Type": "application/json",
67
70
  "X-Mushi-Api-Key": apiKey,
68
71
  "X-Mushi-Project": projectId,
72
+ ...userToken ? { [MUSHI_USER_TOKEN_HEADER]: userToken } : {},
69
73
  ...internalKind ? { [MUSHI_INTERNAL_HEADER]: internalKind } : {},
70
74
  ...extraHeaders
71
75
  },
@@ -168,6 +172,19 @@ function createApiClient(options) {
168
172
  async getSdkConfig() {
169
173
  return request("GET", "/v1/sdk/config", void 0, maxRetries, "sdk-config");
170
174
  },
175
+ async askAssistant(input) {
176
+ return request(
177
+ "POST",
178
+ "/v1/sdk/assistant",
179
+ {
180
+ message: input.message,
181
+ threadId: input.threadId ?? null,
182
+ context: input.context ?? null
183
+ },
184
+ 1,
185
+ "community"
186
+ );
187
+ },
171
188
  async getLatestSdkVersion(packageName) {
172
189
  const query = new URLSearchParams({ package: packageName }).toString();
173
190
  return request("GET", `/v1/sdk/latest-version?${query}`, void 0, maxRetries, "sdk-config");
@@ -445,6 +462,60 @@ function writeCache(storage, projectId, entry) {
445
462
  }
446
463
  }
447
464
 
465
+ // src/env-config.ts
466
+ function first(...values) {
467
+ return values.find((v) => v !== void 0 && v !== "");
468
+ }
469
+ function tryImportMetaEnv(key) {
470
+ try {
471
+ const env = import.meta.env;
472
+ return typeof env === "object" && env !== null ? env[key] : void 0;
473
+ } catch {
474
+ return void 0;
475
+ }
476
+ }
477
+ function readProcessEnv(read) {
478
+ try {
479
+ return read();
480
+ } catch {
481
+ return void 0;
482
+ }
483
+ }
484
+ function resolveEnvConfig() {
485
+ const projectId = first(
486
+ tryImportMetaEnv("VITE_MUSHI_PROJECT_ID"),
487
+ tryImportMetaEnv("NEXT_PUBLIC_MUSHI_PROJECT_ID"),
488
+ tryImportMetaEnv("NUXT_PUBLIC_MUSHI_PROJECT_ID"),
489
+ tryImportMetaEnv("EXPO_PUBLIC_MUSHI_PROJECT_ID"),
490
+ readProcessEnv(() => process.env.NEXT_PUBLIC_MUSHI_PROJECT_ID),
491
+ readProcessEnv(() => process.env.EXPO_PUBLIC_MUSHI_PROJECT_ID),
492
+ readProcessEnv(() => process.env.MUSHI_PROJECT_ID)
493
+ );
494
+ const apiKey = first(
495
+ tryImportMetaEnv("VITE_MUSHI_API_KEY"),
496
+ tryImportMetaEnv("NEXT_PUBLIC_MUSHI_API_KEY"),
497
+ tryImportMetaEnv("NUXT_PUBLIC_MUSHI_API_KEY"),
498
+ tryImportMetaEnv("EXPO_PUBLIC_MUSHI_API_KEY"),
499
+ readProcessEnv(() => process.env.NEXT_PUBLIC_MUSHI_API_KEY),
500
+ readProcessEnv(() => process.env.EXPO_PUBLIC_MUSHI_API_KEY),
501
+ readProcessEnv(() => process.env.MUSHI_API_KEY)
502
+ );
503
+ const apiEndpoint = first(
504
+ tryImportMetaEnv("VITE_MUSHI_API_ENDPOINT"),
505
+ tryImportMetaEnv("NEXT_PUBLIC_MUSHI_API_ENDPOINT"),
506
+ tryImportMetaEnv("NUXT_PUBLIC_MUSHI_API_ENDPOINT"),
507
+ tryImportMetaEnv("EXPO_PUBLIC_MUSHI_API_ENDPOINT"),
508
+ readProcessEnv(() => process.env.NEXT_PUBLIC_MUSHI_API_ENDPOINT),
509
+ readProcessEnv(() => process.env.EXPO_PUBLIC_MUSHI_API_ENDPOINT),
510
+ readProcessEnv(() => process.env.MUSHI_API_ENDPOINT)
511
+ );
512
+ const result = {};
513
+ if (projectId) result.projectId = projectId;
514
+ if (apiKey) result.apiKey = apiKey;
515
+ if (apiEndpoint) result.apiEndpoint = apiEndpoint;
516
+ return result;
517
+ }
518
+
448
519
  // src/pre-filter.ts
449
520
  var DEFAULT_MIN_LENGTH = 10;
450
521
  var DEFAULT_MAX_LENGTH = 2e3;
@@ -577,20 +648,27 @@ function formatPretty(entry) {
577
648
  function formatJson(entry) {
578
649
  return JSON.stringify(entry);
579
650
  }
580
- function emit(level, formatted) {
581
- switch (level) {
582
- case "error":
583
- case "fatal":
651
+ function makeEmitter(destination) {
652
+ if (destination === "stderr") {
653
+ return (level, formatted) => {
584
654
  console.error(formatted);
585
- break;
586
- case "warn":
587
- console.warn(formatted);
588
- break;
589
- default:
590
- console.log(formatted);
655
+ };
591
656
  }
657
+ return (level, formatted) => {
658
+ switch (level) {
659
+ case "error":
660
+ case "fatal":
661
+ console.error(formatted);
662
+ break;
663
+ case "warn":
664
+ console.warn(formatted);
665
+ break;
666
+ default:
667
+ console.log(formatted);
668
+ }
669
+ };
592
670
  }
593
- function buildLogger(scope, minLevel, baseMeta, formatter) {
671
+ function buildLogger(scope, minLevel, baseMeta, formatter, emit) {
594
672
  let currentLevel = minLevel;
595
673
  function log(level, msg, meta) {
596
674
  if (LEVEL_VALUE[level] < LEVEL_VALUE[currentLevel]) return;
@@ -615,7 +693,8 @@ function buildLogger(scope, minLevel, baseMeta, formatter) {
615
693
  `${scope}:${childScope}`,
616
694
  currentLevel,
617
695
  { ...baseMeta, ...childMeta },
618
- formatter
696
+ formatter,
697
+ emit
619
698
  );
620
699
  },
621
700
  setLevel(level) {
@@ -628,11 +707,13 @@ function createLogger(options) {
628
707
  scope,
629
708
  level = "info",
630
709
  meta = {},
631
- format = "auto"
710
+ format = "auto",
711
+ destination = "stdout"
632
712
  } = options;
633
713
  const resolvedFormat = format === "auto" ? detectFormat() : format;
634
714
  const formatter = resolvedFormat === "json" ? formatJson : formatPretty;
635
- return buildLogger(scope, level, meta, formatter);
715
+ const emit = makeEmitter(destination);
716
+ return buildLogger(scope, level, meta, formatter, emit);
636
717
  }
637
718
  var noopLogger = {
638
719
  debug: () => {
@@ -1490,6 +1571,159 @@ function normaliseThrown(thrown) {
1490
1571
  return { name: "Error", message: String(thrown) };
1491
1572
  }
1492
1573
 
1493
- export { DEFAULT_API_ENDPOINT, MAX_REPORT_PAYLOAD_BYTES, MAX_SCREENSHOT_DATA_URL_BYTES, MUSHI_INTERNAL_HEADER, MUSHI_INTERNAL_INIT_MARKER, REGION_ENDPOINTS, captureEnvironment, checkReportPayloadSize, createApiClient, createBreadcrumbBuffer, createLogger, createOfflineQueue, createPiiScrubber, createPreFilter, createRateLimiter, estimateJsonBytes, formatBytes, getDeviceFingerprintHash, getReporterToken, getSessionId, newUuid, noopLogger, normaliseThrown, resolveRegionEndpoint, scrubPii };
1574
+ // src/design-tokens.ts
1575
+ var MUSHI_COLORS_LIGHT = {
1576
+ paper: "#F8F4ED",
1577
+ paperRaised: "#FFFFFF",
1578
+ ink: "#0E0D0B",
1579
+ inkMuted: "#5C5852",
1580
+ inkFaint: "#9A9489",
1581
+ inkDim: "#6E6760",
1582
+ rule: "rgba(14,13,11,0.10)",
1583
+ ruleStrong: "rgba(14,13,11,0.16)",
1584
+ accent: "#E03C2C",
1585
+ accentWash: "rgba(224,60,44,0.08)",
1586
+ accentInk: "#7A1F15",
1587
+ ok: "#16A34A",
1588
+ danger: "#DC2626"
1589
+ };
1590
+ var MUSHI_COLORS_DARK = {
1591
+ paper: "#0F0E0C",
1592
+ paperRaised: "#1A1815",
1593
+ ink: "#F2EBDD",
1594
+ inkMuted: "#928B7E",
1595
+ inkFaint: "#5A5650",
1596
+ inkDim: "#7A7268",
1597
+ rule: "rgba(242,235,221,0.10)",
1598
+ ruleStrong: "rgba(242,235,221,0.18)",
1599
+ accent: "#FF5A47",
1600
+ accentWash: "rgba(255,90,71,0.12)",
1601
+ accentInk: "#FFE5E0",
1602
+ ok: "#4ADE80",
1603
+ danger: "#F87171"
1604
+ };
1605
+ function mushiPalette(mode) {
1606
+ return mode === "dark" ? MUSHI_COLORS_DARK : MUSHI_COLORS_LIGHT;
1607
+ }
1608
+ var MUSHI_SPACING = {
1609
+ hairline: 2,
1610
+ tight: 4,
1611
+ snug: 8,
1612
+ comfy: 12,
1613
+ roomy: 16,
1614
+ lounge: 20,
1615
+ open: 24,
1616
+ wide: 32
1617
+ };
1618
+ var MUSHI_RADIUS = {
1619
+ none: 0,
1620
+ control: 4,
1621
+ card: 10,
1622
+ sheet: 16,
1623
+ pill: 999
1624
+ };
1625
+ var MUSHI_TYPE = {
1626
+ fontDisplay: `'Iowan Old Style', 'Palatino Linotype', 'Palatino', 'Book Antiqua', 'Cambria', Georgia, 'Times New Roman', serif`,
1627
+ fontBody: `system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI Variable Display', 'Segoe UI', sans-serif`,
1628
+ fontMono: `ui-monospace, 'SF Mono', 'JetBrains Mono', Menlo, Consolas, 'Liberation Mono', monospace`,
1629
+ sizeBody: 14,
1630
+ sizeLabel: 12,
1631
+ sizeTitle: 16,
1632
+ lineBody: 1.55
1633
+ };
1634
+ var MUSHI_Z = {
1635
+ base: 99999,
1636
+ /** banner sits one below the trigger so an open FAB overlaps it */
1637
+ banner: 99998,
1638
+ /** report/assistant panel sits one above the trigger */
1639
+ panel: 1e5,
1640
+ /** element-selector overlay — always on top */
1641
+ overlay: 2147483647
1642
+ };
1643
+ var MUSHI_MOTION = {
1644
+ easeStamp: "cubic-bezier(0.22, 1, 0.36, 1)",
1645
+ durationFast: 200,
1646
+ durationPanel: 300
1647
+ };
1648
+ var MUSHI_GEOMETRY = {
1649
+ bannerHeight: 36,
1650
+ fabSize: 52,
1651
+ edgeTabWidth: 28,
1652
+ /** default gutter from the screen edge (added on top of safe-area insets) */
1653
+ gutter: 24,
1654
+ panelWidth: 384,
1655
+ /** below this viewport width the panel becomes a full-width bottom sheet */
1656
+ panelSheetBreakpoint: 480
1657
+ };
1658
+ var MUSHI_COPY = {
1659
+ bannerLabel: "Beta",
1660
+ bannerMessage: "Help us improve \u2014 your feedback shapes what we ship.",
1661
+ bugCta: "Report a bug",
1662
+ featureCta: "Feature request",
1663
+ triggerText: "\u{1F41B}",
1664
+ panelTitle: "Send feedback",
1665
+ myReportsTab: "My reports",
1666
+ assistantTab: "Ask"
1667
+ };
1668
+ function mushiTokens(mode) {
1669
+ return {
1670
+ mode,
1671
+ colors: mushiPalette(mode),
1672
+ spacing: MUSHI_SPACING,
1673
+ radius: MUSHI_RADIUS,
1674
+ type: MUSHI_TYPE,
1675
+ z: MUSHI_Z,
1676
+ motion: MUSHI_MOTION,
1677
+ geometry: MUSHI_GEOMETRY,
1678
+ copy: MUSHI_COPY
1679
+ };
1680
+ }
1681
+
1682
+ // src/identity.ts
1683
+ var MUSHI_IDENTITY_TOKEN_PREFIX = "mushi.identity";
1684
+ function buildIdentityClaims(input) {
1685
+ const now = Math.floor(Date.now() / 1e3);
1686
+ const ttl = input.ttlSeconds && input.ttlSeconds > 0 ? Math.min(input.ttlSeconds, 3600) : 600;
1687
+ return {
1688
+ projectId: input.projectId,
1689
+ sub: input.userId,
1690
+ ...input.email ? { email: input.email } : {},
1691
+ ...input.name ? { name: input.name } : {},
1692
+ iat: now,
1693
+ exp: now + ttl
1694
+ };
1695
+ }
1696
+ function base64UrlDecode(segment) {
1697
+ const pad = segment.length % 4 === 0 ? "" : "=".repeat(4 - segment.length % 4);
1698
+ const base64 = segment.replace(/-/g, "+").replace(/_/g, "/") + pad;
1699
+ if (typeof atob === "function") return atob(base64);
1700
+ const B = globalThis.Buffer;
1701
+ if (B) return B.from(base64, "base64").toString("binary");
1702
+ return "";
1703
+ }
1704
+ function parseIdentityToken(token) {
1705
+ if (typeof token !== "string") return null;
1706
+ const parts = token.split(".");
1707
+ if (parts.length !== 3) return null;
1708
+ try {
1709
+ const json = base64UrlDecode(parts[1]);
1710
+ const payload = JSON.parse(json);
1711
+ const projectId = typeof payload.projectId === "string" ? payload.projectId : void 0;
1712
+ const sub = typeof payload.sub === "string" ? payload.sub : void 0;
1713
+ if (!projectId || !sub) return null;
1714
+ return {
1715
+ projectId,
1716
+ sub,
1717
+ ...typeof payload.email === "string" ? { email: payload.email } : {},
1718
+ ...typeof payload.name === "string" ? { name: payload.name } : {},
1719
+ ...typeof payload.iat === "number" ? { iat: payload.iat } : {},
1720
+ ...typeof payload.exp === "number" ? { exp: payload.exp } : {}
1721
+ };
1722
+ } catch {
1723
+ return null;
1724
+ }
1725
+ }
1726
+
1727
+ export { DEFAULT_API_ENDPOINT, MAX_REPORT_PAYLOAD_BYTES, MAX_SCREENSHOT_DATA_URL_BYTES, MUSHI_COLORS_DARK, MUSHI_COLORS_LIGHT, MUSHI_COPY, MUSHI_GEOMETRY, MUSHI_IDENTITY_TOKEN_PREFIX, MUSHI_INTERNAL_HEADER, MUSHI_INTERNAL_INIT_MARKER, MUSHI_MOTION, MUSHI_RADIUS, MUSHI_SPACING, MUSHI_TYPE, MUSHI_Z, REGION_ENDPOINTS, buildIdentityClaims, captureEnvironment, checkReportPayloadSize, createApiClient, createBreadcrumbBuffer, createLogger, createOfflineQueue, createPiiScrubber, createPreFilter, createRateLimiter, estimateJsonBytes, formatBytes, getDeviceFingerprintHash, getReporterToken, getSessionId, mushiPalette, mushiTokens, newUuid, noopLogger, normaliseThrown, parseIdentityToken, resolveEnvConfig, resolveRegionEndpoint, scrubPii };
1494
1728
  //# sourceMappingURL=index.js.map
1495
1729
  //# sourceMappingURL=index.js.map