@speeq/client 0.0.0-stage → 0.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 speeq contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,156 @@
1
- # Temporary Holding Version
1
+ # @speeq/client
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A headless client for a speeq server handler. Subscribe to conversation state and connect it to your own text, microphone and navigation controls.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ pnpm add @speeq/client@next
9
+ ```
10
+
11
+ ## Vanilla JavaScript
12
+
13
+ Mount the server handler at `/api/speeq`. This example uses a form with a `message` input, a separate microphone button and a transcript element.
14
+
15
+ ```js
16
+ import { createSpeeqClient } from "@speeq/client";
17
+
18
+ const client = createSpeeqClient({
19
+ endpoint: "/api/speeq",
20
+ onNavigate({ href }) {
21
+ const url = new URL(href, location.origin);
22
+ if (url.origin === location.origin) location.assign(url);
23
+ },
24
+ });
25
+
26
+ const form = document.querySelector("#chat");
27
+ const mic = document.querySelector("#microphone");
28
+ const transcript = document.querySelector("#transcript");
29
+ function render() {
30
+ const { messages, interimTranscript, support, error } = client.getSnapshot();
31
+ mic.disabled = !support.speechInput;
32
+ transcript.textContent = [
33
+ ...messages.map(({ role, text }) => role + ": " + text),
34
+ interimTranscript,
35
+ error?.message,
36
+ ]
37
+ .filter(Boolean)
38
+ .join("\n");
39
+ }
40
+ render();
41
+ const unsubscribe = client.subscribe(render);
42
+
43
+ form.addEventListener("submit", (event) => {
44
+ event.preventDefault();
45
+ const text = form.elements.namedItem("message").value;
46
+ void client.sendText(text);
47
+ });
48
+ mic.addEventListener("click", () => client.startListening());
49
+ ```
50
+
51
+ Call `unsubscribe()` and `client.destroy()` when removing the view. For in-app navigation, use your router in `onNavigate`.
52
+
53
+ ## Options
54
+
55
+ | Option | Behavior |
56
+ | ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
57
+ | `endpoint` | Required base URL of the mounted handler. |
58
+ | `language` | Initial language. Defaults to the first entry in `languages`, then `en-IN`. |
59
+ | `languages` | Host language choices. Session creation filters them against the server configuration and falls back to the server default language if needed. |
60
+ | `onNavigate` | Receives `{ routeId, href, params }` when the server requests navigation. |
61
+ | `getContext` | Returns optional `path`, `title` and `data` overrides for the page context sent with session and turn requests. |
62
+ | `speech` | Supply `input` or `output` independently. Omitted or undefined adapters use the Web Speech defaults. Set either key to `null` to disable it. |
63
+ | `speakReplies` | Defaults to `true`. Enables speech playback when a matching voice is available. |
64
+ | `autoSendOnFinal` | Defaults to `true`. Set to `false` to keep the final transcript in `interimTranscript` for editing before sending. |
65
+ | `persistSession` | Defaults to `"memory"`. Use `"sessionStorage"` to reuse a session across page reloads in the tab. |
66
+ | `fetch` | Optional transport implementing the Fetch API. Defaults to the global fetch function. |
67
+
68
+ ## Snapshot and events
69
+
70
+ `getSnapshot()` returns the same immutable snapshot until something changes. `subscribe(listener)` signals changes. Read the new snapshot inside the listener.
71
+
72
+ | Field | Contents |
73
+ | ----------------------- | ---------------------------------------------------------------------------------------------------------------- |
74
+ | `state` | Current activity shown below. |
75
+ | `language`, `languages` | Current language and enabled choices. Use `setLanguage(tag)` to switch. |
76
+ | `messages` | User and assistant messages with stable IDs, text and a status of `streaming`, `done`, `interrupted` or `error`. |
77
+ | `interimTranscript` | Live recognition text, or a completed transcript awaiting manual submission. |
78
+ | `pendingConfirmation` | The proposed write action awaiting a decision, or `null`. |
79
+ | `error` | A public error with a code, message, retryability and optional retry delay, or `null`. |
80
+ | `activeTools` | Running tools identified by call ID and name. |
81
+ | `support` | Input and output availability. `voiceForLanguage` is `null` before a voice check completes, then a boolean. |
82
+ | `config`, `sessionId` | Server configuration and session identifier, populated when a session is established. |
83
+
84
+ `client.on(name, callback)` returns an unsubscribe function. The `change` event carries the snapshot, `state` carries each new state, `event` carries individual protocol events, `navigate` carries a navigation action and `error` carries the public error.
85
+
86
+ ```text
87
+ idle → listening → thinking → speaking → idle
88
+ idle → thinking → idle (text-only reply)
89
+ listening → idle (no speech or manual submission)
90
+ listening / thinking / speaking → error
91
+ cancel() → idle
92
+ ```
93
+
94
+ A turn enters `thinking` while the client establishes the session and streams the response. With speech enabled, the first sentence can play while later text is still arriving. Sentences play in order, in the turn's language. The client returns to idle after the stream and playback finish successfully.
95
+
96
+ Start listening or send new text to interrupt an active reply. Barge-in cancels speech, aborts the turn request and marks a streaming message as interrupted. Use `stopListening()` to finish recognition and accept its transcript, or `cancel()` to discard it.
97
+
98
+ ## Confirming a write
99
+
100
+ When `pendingConfirmation` is set, show its `summary` and let the user approve or decline. Capture the ID for the action displayed. In the click handler, call:
101
+
102
+ ```js
103
+ await client.confirm(confirmationId, approved); // approved is the user's boolean choice
104
+ ```
105
+
106
+ The client clears the prompt immediately and streams the server's follow-up. Surface any error in the snapshot, including a conflict if the decision has already been handled.
107
+
108
+ ## Sessions
109
+
110
+ The first text turn, submitted voice transcript or confirmation creates a session. The client keeps the token in memory. With session storage enabled, the key is `speeq:session:<endpoint>`. If storage is unavailable, the client can still use memory. It renews a session within 30 seconds of expiry. If a turn or confirmation receives a 401 with `SESSION_EXPIRED` or `UNAUTHENTICATED`, the client creates a fresh session and retries the request once. The new server session starts with fresh history.
111
+
112
+ ## Custom speech adapters
113
+
114
+ Supply objects implementing the core `SpeechInput` or `SpeechOutput` interface. Input exposes `supported`, `start(options, handlers)`, `stop()` and `abort()`. Deliver the final transcript through `handlers.onFinal`. Output exposes `supported`, `hasVoiceFor(language)`, `speak(text, options)` and `cancel()`. Resolve pending playback when it ends or is cancelled.
115
+
116
+ To keep browser recognition and customize playback, pass only the output adapter:
117
+
118
+ ```js
119
+ import { createSpeeqClient, WebSpeechOutput } from "@speeq/client";
120
+
121
+ const client = createSpeeqClient({
122
+ endpoint: "/api/speeq",
123
+ speech: { output: new WebSpeechOutput({ rate: 0.95, pitch: 1 }) },
124
+ });
125
+ ```
126
+
127
+ `selectVoice(language, voices)` can choose a device voice. Return `undefined` to use exact language matching followed by a primary-language fallback. `voicesTimeoutMs` can shorten the default 1500 ms wait for an initially empty voice list.
128
+
129
+ ## Browser support
130
+
131
+ Speech recognition works best in Chrome and Edge. Firefox has no default `SpeechRecognition` support, so offer a text input. Chrome recognition uses a cloud service and needs a network connection. Microphone access requires a secure context (HTTPS or localhost) and user permission.
132
+
133
+ Some devices lack matching `te-IN` or `hi-IN` voices. When `support.voiceForLanguage` is `false`, replies stay text-only for that language. Recognition availability is tracked separately. Use `detectSpeechSupport()` to check the browser APIs, or the snapshot's support fields to account for custom adapters. React users should see `@speeq/react`.
134
+
135
+ ## Audit client
136
+
137
+ Use `createAuditClient` to read session summaries and ordered event history from your own server. Configure `audit.authorize` on the server. Requests include cookies by default.
138
+
139
+ ```ts
140
+ import { createAuditClient } from "@speeq/client";
141
+
142
+ const audit = createAuditClient({ endpoint: "/api/speeq" });
143
+ const { sessions, nextCursor } = await audit.listSessions({ limit: 25 });
144
+ const first = sessions[0];
145
+ if (first) {
146
+ const { session, events } = await audit.getSession(first.id);
147
+ console.log(session.id, events.length);
148
+ }
149
+ if (nextCursor) {
150
+ await audit.listSessions({ limit: 25, cursor: nextCursor });
151
+ }
152
+ ```
153
+
154
+ Both methods accept an abort signal and report HTTP errors as `SpeeqError`.
155
+
156
+ [Related guide or reference](../../docs/reference/client.md)