@svadmin/ai-elements 0.11.3 → 0.12.3

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.
@@ -4,7 +4,7 @@
4
4
  import type { Snippet } from 'svelte';
5
5
  import type { HTMLAttributes } from 'svelte/elements';
6
6
  import { BarChart3, ChevronDown, ChevronUp, CircleAlert } from '@lucide/svelte';
7
- import { cn } from '../../utils.js';
7
+ import { cn, formatCompactNumber } from '../../utils.js';
8
8
 
9
9
  interface Props extends Omit<HTMLAttributes<HTMLElement>, 'children' | 'class' | 'title'> {
10
10
  used?: number;
@@ -104,10 +104,7 @@
104
104
  return () => document.removeEventListener('pointerdown', dismiss);
105
105
  });
106
106
 
107
- function formatTokens(value?: number): string {
108
- if (value === undefined || !Number.isFinite(value)) return '-';
109
- return new Intl.NumberFormat(undefined, { notation: 'compact', maximumFractionDigits: 1 }).format(value);
110
- }
107
+ const formatTokens = formatCompactNumber;
111
108
  </script>
112
109
 
113
110
  <section
@@ -12,12 +12,10 @@
12
12
 
13
13
  <script lang="ts">
14
14
  import { definedOptions } from '@svadmin/core/options';
15
- import { cn } from '../../utils.js';
15
+ import { cn, formatCompactNumber } from '../../utils.js';
16
16
 
17
17
  let { tokens, costText, class: className = '', children, ...rest }: TokensWithCostProps = $props();
18
- const formattedTokens = $derived(tokens === undefined
19
- ? '-'
20
- : new Intl.NumberFormat(undefined, { notation: 'compact', maximumFractionDigits: 1 }).format(tokens));
18
+ const formattedTokens = $derived(formatCompactNumber(tokens));
21
19
  </script>
22
20
 
23
21
  <span {...rest} class={cn('svadmin-ai-tokens-with-cost', className)} data-slot="tokens-with-cost">
@@ -108,6 +108,8 @@
108
108
  function createPreviewSession(text: string): VoicePreviewSession | null {
109
109
  if (typeof document === 'undefined') return null;
110
110
  const frame = document.createElement('iframe');
111
+ // srcdoc keeps an isolated about:blank window available before the frame is mounted.
112
+ frame.srcdoc = '<!doctype html><html><body></body></html>';
111
113
  frame.hidden = true;
112
114
  frame.tabIndex = -1;
113
115
  frame.setAttribute('aria-hidden', 'true');
package/dist/utils.d.ts CHANGED
@@ -1,5 +1,13 @@
1
1
  export { cn } from './classnames.js';
2
2
  export type SnippetLike = import('svelte').Snippet;
3
+ /**
4
+ * Deterministic compact number formatting (`1250 -> "1.3K"`).
5
+ *
6
+ * `Intl.NumberFormat({ notation: 'compact' })` silently degrades to plain digits
7
+ * on small-ICU runtimes, so the same usage string read "1250" in one environment
8
+ * and "1.3K" in another. Token counters must read the same everywhere.
9
+ */
10
+ export declare function formatCompactNumber(value?: number): string;
3
11
  export declare function stopPropagation(event: Event): void;
4
12
  export declare function isPromiseLike(value: unknown): value is PromiseLike<unknown>;
5
13
  export declare function safeResourceUrl(value: string | undefined): string | undefined;
package/dist/utils.js CHANGED
@@ -1,5 +1,27 @@
1
1
  export { cn } from './classnames.js';
2
2
  const RESOURCE_PROTOCOLS = new Set(['http:', 'https:', 'blob:']);
3
+ /**
4
+ * Deterministic compact number formatting (`1250 -> "1.3K"`).
5
+ *
6
+ * `Intl.NumberFormat({ notation: 'compact' })` silently degrades to plain digits
7
+ * on small-ICU runtimes, so the same usage string read "1250" in one environment
8
+ * and "1.3K" in another. Token counters must read the same everywhere.
9
+ */
10
+ export function formatCompactNumber(value) {
11
+ if (value === undefined || !Number.isFinite(value))
12
+ return '-';
13
+ const units = [
14
+ [1_000_000_000, 'B'],
15
+ [1_000_000, 'M'],
16
+ [1_000, 'K'],
17
+ ];
18
+ for (const [scale, suffix] of units) {
19
+ if (Math.abs(value) < scale)
20
+ continue;
21
+ return `${Math.round((value / scale) * 10) / 10}${suffix}`;
22
+ }
23
+ return String(value);
24
+ }
3
25
  export function stopPropagation(event) {
4
26
  event.stopPropagation();
5
27
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@svadmin/ai-elements",
3
- "version": "0.11.3",
3
+ "version": "0.12.3",
4
4
  "description": "Composable Svelte 5 AI interaction components for SVAdmin",
5
5
  "type": "module",
6
6
  "sideEffects": [
@@ -56,7 +56,7 @@
56
56
  }
57
57
  },
58
58
  "peerDependencies": {
59
- "@svadmin/core": "^0.61.1",
59
+ "@svadmin/core": "^0.65.0",
60
60
  "@tanstack/svelte-query": "^6.1.48",
61
61
  "svelte": "^5.56.10"
62
62
  },
@@ -101,7 +101,7 @@
101
101
  "@testing-library/svelte": "^5.4.2",
102
102
  "@testing-library/user-event": "^14.6.6",
103
103
  "@types/bun": "^1.4.2",
104
- "happy-dom": "^20.11.12",
104
+ "happy-dom": "^21.0.4",
105
105
  "vitest": "^4.1.11"
106
106
  },
107
107
  "scripts": {