@sts-ai/sdk 0.0.0-stage → 1.0.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.
@@ -0,0 +1,242 @@
1
+ const ACTIVE_ATTR = 'data-sts-active';
2
+ const GAP_BETWEEN_STEPS_MS = 600;
3
+ /** Follow mode: a section counts as reached once it sits in the middle of the screen this long. */
4
+ const DWELL_MS = 600;
5
+ function readingTimeMs(text) {
6
+ const words = text.trim().split(/\s+/).filter(Boolean).length;
7
+ return Math.min(14000, Math.max(2500, words * 380));
8
+ }
9
+ function findTarget(step) {
10
+ if (!step.target || typeof document === 'undefined')
11
+ return null;
12
+ return document.querySelector(`[data-sts-step="${CSS.escape(step.target)}"]`);
13
+ }
14
+ /**
15
+ * Plays a tour on the creator's own page, marking the active `[data-sts-step]` section with
16
+ * `data-sts-active` (style it however you like). Two ways to run it:
17
+ * - `start()`: the tour leads. It scrolls to each section in turn and narrates it.
18
+ * - `follow()`: the visitor leads. Nothing scrolls; each section's step plays once, when the
19
+ * visitor scrolls it into the middle of the screen. A step with no target plays at the start.
20
+ * Steps whose target is missing on this page are skipped, so one tour can cover many pages.
21
+ */
22
+ export function createWizardPlayer(wizard, events = {}) {
23
+ const audio = typeof Audio === 'undefined' ? null : new Audio();
24
+ let runId = 0;
25
+ let muted = false;
26
+ let cancelCurrent = null;
27
+ let observer = null;
28
+ let dwellTimer = null;
29
+ const played = new Set();
30
+ let playable = wizard.steps.filter((step) => !step.target || findTarget(step));
31
+ function clearActive() {
32
+ document.querySelectorAll(`[${ACTIVE_ATTR}]`).forEach((el) => el.removeAttribute(ACTIVE_ATTR));
33
+ }
34
+ function wait(ms) {
35
+ return new Promise((resolve) => {
36
+ const timer = setTimeout(resolve, ms);
37
+ cancelCurrent = () => {
38
+ clearTimeout(timer);
39
+ resolve();
40
+ };
41
+ });
42
+ }
43
+ function speakWithBrowser(text) {
44
+ if (typeof speechSynthesis === 'undefined')
45
+ return wait(readingTimeMs(text));
46
+ const lang = wizard.language === 'bn' ? 'bn' : 'en';
47
+ const voice = speechSynthesis.getVoices().find((item) => item.lang.toLowerCase().startsWith(lang)) ?? null;
48
+ if (lang === 'bn' && !voice)
49
+ return wait(readingTimeMs(text));
50
+ return new Promise((resolve) => {
51
+ const utterance = new SpeechSynthesisUtterance(text);
52
+ utterance.lang = lang === 'bn' ? 'bn-BD' : 'en-US';
53
+ if (voice)
54
+ utterance.voice = voice;
55
+ const guard = setTimeout(resolve, readingTimeMs(text) + 4000);
56
+ const done = () => {
57
+ clearTimeout(guard);
58
+ resolve();
59
+ };
60
+ utterance.onend = done;
61
+ utterance.onerror = done;
62
+ cancelCurrent = () => {
63
+ speechSynthesis.cancel();
64
+ done();
65
+ };
66
+ speechSynthesis.speak(utterance);
67
+ });
68
+ }
69
+ function speakWithClip(url) {
70
+ return new Promise((resolve) => {
71
+ if (!audio)
72
+ return resolve(false);
73
+ audio.onended = () => resolve(true);
74
+ audio.onerror = () => resolve(false);
75
+ cancelCurrent = () => {
76
+ audio.pause();
77
+ resolve(true);
78
+ };
79
+ audio.src = url;
80
+ audio.play().catch(() => resolve(false));
81
+ });
82
+ }
83
+ async function narrate(step) {
84
+ if (muted)
85
+ return wait(readingTimeMs(step.narration));
86
+ if (step.audioUrl && (await speakWithClip(step.audioUrl)))
87
+ return;
88
+ return speakWithBrowser(step.narration);
89
+ }
90
+ async function playFrom(index) {
91
+ const id = ++runId;
92
+ for (let i = index; i < playable.length; i += 1) {
93
+ const step = playable[i];
94
+ clearActive();
95
+ const target = findTarget(step);
96
+ if (target) {
97
+ target.setAttribute(ACTIVE_ATTR, 'true');
98
+ const reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
99
+ target.scrollIntoView({ behavior: reduce ? 'auto' : 'smooth', block: 'center' });
100
+ }
101
+ events.onStep?.(step, i);
102
+ events.onSpeakingChange?.(true);
103
+ await narrate(step);
104
+ if (id !== runId)
105
+ return;
106
+ events.onSpeakingChange?.(false);
107
+ await wait(GAP_BETWEEN_STEPS_MS);
108
+ if (id !== runId)
109
+ return;
110
+ }
111
+ clearActive();
112
+ events.onEnd?.();
113
+ }
114
+ function unlockAudio() {
115
+ if (!audio)
116
+ return;
117
+ audio.muted = true;
118
+ void audio.play().catch(() => undefined);
119
+ audio.pause();
120
+ audio.muted = false;
121
+ }
122
+ function silence() {
123
+ runId += 1;
124
+ cancelCurrent?.();
125
+ audio?.pause();
126
+ if (typeof speechSynthesis !== 'undefined')
127
+ speechSynthesis.cancel();
128
+ }
129
+ function stopFollowing() {
130
+ observer?.disconnect();
131
+ observer = null;
132
+ if (dwellTimer)
133
+ clearTimeout(dwellTimer);
134
+ dwellTimer = null;
135
+ }
136
+ /**
137
+ * Plays one step, cutting off whichever step was still talking. Follow mode plays each step
138
+ * once; `again` plays it even if it already spoke (a step triggered by your own event).
139
+ */
140
+ async function playOnce(step, again = false) {
141
+ if (played.has(step.id) && !again)
142
+ return;
143
+ played.add(step.id);
144
+ silence();
145
+ const id = runId;
146
+ clearActive();
147
+ findTarget(step)?.setAttribute(ACTIVE_ATTR, 'true');
148
+ events.onStep?.(step, playable.indexOf(step));
149
+ events.onSpeakingChange?.(true);
150
+ await narrate(step);
151
+ if (id !== runId)
152
+ return;
153
+ events.onSpeakingChange?.(false);
154
+ if (playable.every((item) => played.has(item.id)))
155
+ events.onEnd?.();
156
+ }
157
+ function follow() {
158
+ stopFollowing();
159
+ playable = wizard.steps.filter((step) => !step.target || findTarget(step));
160
+ const byTarget = new Map(playable.filter((step) => step.target).map((step) => [step.target, step]));
161
+ const opener = playable.find((step) => !step.target);
162
+ if (opener)
163
+ void playOnce(opener);
164
+ if (typeof IntersectionObserver === 'undefined')
165
+ return;
166
+ let current = null;
167
+ observer = new IntersectionObserver((entries) => {
168
+ const visible = entries.filter((entry) => entry.isIntersecting);
169
+ if (visible.length === 0)
170
+ return;
171
+ const target = visible[visible.length - 1].target.getAttribute('data-sts-step');
172
+ if (!target || target === current)
173
+ return;
174
+ current = target;
175
+ if (dwellTimer)
176
+ clearTimeout(dwellTimer);
177
+ dwellTimer = setTimeout(() => {
178
+ const step = current ? byTarget.get(current) : undefined;
179
+ if (step)
180
+ void playOnce(step);
181
+ }, DWELL_MS);
182
+ },
183
+ // Only the middle fifth of the screen counts, so a section is "reached", not just peeking in.
184
+ { rootMargin: '-40% 0px -40% 0px' });
185
+ byTarget.forEach((_step, target) => {
186
+ const element = document.querySelector(`[data-sts-step="${CSS.escape(target)}"]`);
187
+ if (element)
188
+ observer?.observe(element);
189
+ });
190
+ }
191
+ return {
192
+ get steps() {
193
+ return playable;
194
+ },
195
+ /** The tour leads. Call from a click handler (browsers block audio without a user gesture). */
196
+ start(index = 0) {
197
+ stopFollowing();
198
+ unlockAudio();
199
+ void playFrom(index);
200
+ },
201
+ /**
202
+ * The visitor leads. Call from a click handler the first time so sound is allowed. Call it
203
+ * again after your site changes page without a reload, to pick up that page's sections.
204
+ */
205
+ follow() {
206
+ unlockAudio();
207
+ follow();
208
+ },
209
+ /**
210
+ * Plays one step from your own code, by its id or its `data-sts-step` name, for example from
211
+ * `window.addEventListener('my-event', () => tour.play('pricing'))`. Browsers only allow sound
212
+ * after the visitor clicked once on the page, so start or follow the tour from a click first
213
+ * (or call `unlock()` from any click). Returns false when there is no such step.
214
+ */
215
+ play(step) {
216
+ const found = wizard.steps.find((item) => item.id === step || item.target === step);
217
+ if (!found)
218
+ return false;
219
+ void playOnce(found, true);
220
+ return true;
221
+ },
222
+ /** Call from any click to allow sound for steps you play later with `play()`. */
223
+ unlock() {
224
+ unlockAudio();
225
+ },
226
+ /** Lets every step play again, for example when the visitor restarts the tour. */
227
+ reset() {
228
+ played.clear();
229
+ },
230
+ stop() {
231
+ stopFollowing();
232
+ silence();
233
+ clearActive();
234
+ events.onSpeakingChange?.(false);
235
+ },
236
+ setMuted(value) {
237
+ muted = value;
238
+ if (value)
239
+ silence();
240
+ },
241
+ };
242
+ }
package/package.json CHANGED
@@ -1,6 +1,72 @@
1
1
  {
2
2
  "name": "@sts-ai/sdk",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "One package for every sts-ai service on your own website: Chat AI, Voice AI, Form, Tour and business details. Your design, our services. React, Vue/Nuxt or plain JavaScript.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.js"
15
+ },
16
+ "./react": {
17
+ "types": "./dist/react.d.ts",
18
+ "import": "./dist/react.js"
19
+ },
20
+ "./vue": {
21
+ "types": "./dist/vue.d.ts",
22
+ "import": "./dist/vue.js"
23
+ }
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "README.md"
28
+ ],
29
+ "scripts": {
30
+ "build": "tsc -p tsconfig.json",
31
+ "prepublishOnly": "npm run build"
32
+ },
33
+ "peerDependencies": {
34
+ "react": "^18.0.0 || ^19.0.0",
35
+ "vue": "^3.3.0"
36
+ },
37
+ "peerDependenciesMeta": {
38
+ "react": {
39
+ "optional": true
40
+ },
41
+ "vue": {
42
+ "optional": true
43
+ }
44
+ },
45
+ "devDependencies": {
46
+ "@types/react": "^19",
47
+ "livekit-client": "^2.17.2",
48
+ "react": "^19.0.0",
49
+ "typescript": "^5",
50
+ "vue": "^3.5.0"
51
+ },
52
+ "dependencies": {
53
+ "livekit-client": "^2.17.2"
54
+ },
55
+ "keywords": [
56
+ "sts-ai",
57
+ "chat",
58
+ "voice",
59
+ "livekit",
60
+ "headless",
61
+ "react",
62
+ "vue",
63
+ "nuxt"
64
+ ],
65
+ "homepage": "https://sudotechserve.com",
66
+ "publishConfig": {
67
+ "access": "public"
68
+ },
69
+ "engines": {
70
+ "node": ">=18.17"
71
+ }
72
+ }