@dialgem/client 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.
- package/CHANGELOG.md +13 -0
- package/LICENSE +202 -0
- package/README.md +148 -2
- package/dist/call-Cv2GrNfJ.d.cts +160 -0
- package/dist/call-Cv2GrNfJ.d.ts +160 -0
- package/dist/index.cjs +567 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +19 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +562 -0
- package/dist/index.js.map +1 -0
- package/dist/react.cjs +623 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.cts +41 -0
- package/dist/react.d.ts +41 -0
- package/dist/react.js +621 -0
- package/dist/react.js.map +1 -0
- package/package.json +97 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
The first stable release. The API is the one in `0.1.0-beta.1`, unchanged: nothing was added, removed or renamed. From here on, breaking changes only come in a new major version, and `npm install @dialgem/client` installs this release.
|
|
6
|
+
|
|
7
|
+
### What it does
|
|
8
|
+
|
|
9
|
+
- `DialgemCall`: microphone capture (AudioWorklet, converted to 16 kHz mono in the audio thread), streaming playback of the agent's voice, and typed events (`started`, `transcript`, `agent_speaking`, `interrupted`, `level`, `ended`, `error`). `start()`, `stop()` and `setMuted()`.
|
|
10
|
+
- `@dialgem/client/react`: `useDialgemCall`, a hook that makes a new call (and fetches a new token) on every `start()` and hangs up on unmount.
|
|
11
|
+
- Authenticates with the one-use token from `dialgem.webCalls.create` (in `@dialgem/sdk`); an API key is never needed in the browser.
|
|
12
|
+
- Refusals say why: `refused` (with `reason` `token_expired`, `token_already_used`, `token_invalid` or `origin_not_allowed`), `busy`, `call_refused` (with the server's reason).
|
|
13
|
+
- Not in this release: reconnecting a dropped call (the token is single-use and the conversation lives on the server for as long as the connection does), WebRTC, a script-tag build.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,149 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# Dialgem Client
|
|
4
|
+
|
|
5
|
+
**Talk to a [Dialgem](https://dialgem.com) voice agent from any web page.**
|
|
6
|
+
|
|
7
|
+
Microphone capture, streaming playback and live call events in the browser, with a small React hook. Your API key never reaches the page.
|
|
8
|
+
|
|
9
|
+
[](https://www.npmjs.com/package/@dialgem/client)
|
|
10
|
+
[](https://github.com/DialgemAI/dialgem-client-sdk/actions/workflows/ci.yml)
|
|
11
|
+
[](LICENSE)
|
|
12
|
+
[](https://www.typescriptlang.org/)
|
|
13
|
+
[](#react)
|
|
14
|
+
[](https://github.com/DialgemAI/dialgem-client-sdk/stargazers)
|
|
15
|
+
|
|
16
|
+
[Website](https://dialgem.com) · [Issues](https://github.com/DialgemAI/dialgem-client-sdk/issues) · [Changelog](CHANGELOG.md) · [Server SDK](https://github.com/DialgemAI/dialgem-sdk-typescript)
|
|
17
|
+
|
|
18
|
+
</div>
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { DialgemCall } from "@dialgem/client"
|
|
24
|
+
|
|
25
|
+
const call = new DialgemCall({ accessToken: () => fetch("/api/start-call").then((r) => r.text()) })
|
|
26
|
+
call.on("transcript", (t) => console.log(t.role, t.text))
|
|
27
|
+
button.onclick = () => call.start()
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Why this SDK
|
|
31
|
+
|
|
32
|
+
- **Your API key stays on your server.** Your backend mints a one-use token that lives 60 seconds; the page connects with that alone.
|
|
33
|
+
- **Voice in the browser, handled.** It opens the microphone, converts to 16 kHz in an audio worklet, streams the voice to the agent and plays the agent's voice as it arrives, with echo cancellation on by default.
|
|
34
|
+
- **Events for your UI.** Transcript (live as the caller speaks), who is speaking, interruptions, microphone level, and how the call ended.
|
|
35
|
+
- **React hook included.** `useDialgemCall` from `@dialgem/client/react`; React is an optional peer dependency (18+).
|
|
36
|
+
- **No runtime dependencies**, and typed.
|
|
37
|
+
- **Wide support.** Chrome 66+, Edge 79+, Firefox 76+, Safari 14.1+ (anything with `AudioWorklet`), over HTTPS or `localhost`.
|
|
38
|
+
|
|
39
|
+
## Contents
|
|
40
|
+
|
|
41
|
+
[Install](#install) · [How a call starts](#how-a-call-starts) · [React](#react) · [DialgemCall](#dialgemcall) · [Events](#events) · [Things worth knowing](#things-worth-knowing) · [License](#license)
|
|
42
|
+
|
|
43
|
+
## Install
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
npm install @dialgem/client
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## How a call starts
|
|
50
|
+
|
|
51
|
+
Your **server** asks Dialgem for a call with your API key and gives the page a one-use token. The page connects with that token alone, so **your API key never reaches the browser**.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
// On your server, with @dialgem/sdk. Called by your page when a visitor clicks.
|
|
55
|
+
import { Dialgem } from "@dialgem/sdk"
|
|
56
|
+
const dialgem = new Dialgem({ apiKey: process.env.DIALGEM_API_KEY })
|
|
57
|
+
|
|
58
|
+
export async function startCall() {
|
|
59
|
+
const { access_token } = await dialgem.webCalls.create({ agent_id: "AGENT_ID" })
|
|
60
|
+
return access_token // 60 seconds, one use
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
// On your page.
|
|
66
|
+
import { DialgemCall } from "@dialgem/client"
|
|
67
|
+
|
|
68
|
+
const call = new DialgemCall({
|
|
69
|
+
accessToken: () => fetch("/api/start-call").then((r) => r.text()),
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
call.on("transcript", (t) => console.log(t.role, t.text, t.final ? "" : "…"))
|
|
73
|
+
call.on("agent_speaking", ({ speaking }) => orb.classList.toggle("talking", speaking))
|
|
74
|
+
call.on("ended", ({ reason }) => console.log("call ended:", reason))
|
|
75
|
+
call.on("error", (e) => alert(e.message))
|
|
76
|
+
|
|
77
|
+
button.onclick = () => call.start() // from a click: browsers only allow sound after one
|
|
78
|
+
hangUp.onclick = () => call.stop()
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Before the first call, allow your page's origin (for example `https://app.example.com`) in the Dialgem dashboard under **Settings, Web calls**, or with `dialgem.webCalls.updateSettings({ allowed_origins: [...] })`.
|
|
82
|
+
|
|
83
|
+
### React
|
|
84
|
+
|
|
85
|
+
```tsx
|
|
86
|
+
import { useDialgemCall } from "@dialgem/client/react"
|
|
87
|
+
|
|
88
|
+
function Talk() {
|
|
89
|
+
const call = useDialgemCall({ getAccessToken: () => fetch("/api/start-call").then((r) => r.text()) })
|
|
90
|
+
return (
|
|
91
|
+
<>
|
|
92
|
+
<button onClick={call.start} disabled={call.state === "connecting" || call.state === "active"}>Talk to us</button>
|
|
93
|
+
{call.state === "active" && <button onClick={call.stop}>Hang up</button>}
|
|
94
|
+
{call.transcript.map((l) => <p key={l.id}>{l.role}: {l.text}</p>)}
|
|
95
|
+
{call.error && <p role="alert">{call.error.message}</p>}
|
|
96
|
+
</>
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## `DialgemCall`
|
|
102
|
+
|
|
103
|
+
`new DialgemCall({ accessToken, microphone?, baseURL? })`
|
|
104
|
+
|
|
105
|
+
- `accessToken`: the token, or a function returning it (sync or async). A function is called **after** the visitor has allowed the microphone, because the token lives 60 seconds and a permission prompt can take longer.
|
|
106
|
+
- `microphone`: `{ deviceId, echoCancellation, noiseSuppression, autoGainControl }`. The last three default to `true`: without echo cancellation the agent's own voice comes back through the microphone and it answers itself. Turn them off only for a headset in a quiet room.
|
|
107
|
+
- `baseURL`: only for a self-hosted deployment. Plain `ws://` is refused for anything but `localhost`, because the token would cross the network in the clear.
|
|
108
|
+
|
|
109
|
+
Methods: `start()` (returns when connected; rejects with the same error the `error` event carries), `stop()`, `setMuted(boolean)` (the agent hears silence; the call stays open). Properties: `state`, `callId`, `muted`.
|
|
110
|
+
|
|
111
|
+
A `DialgemCall` is used once. A token is single-use, so a second call needs a new object (the React hook does this for you on every `start()`).
|
|
112
|
+
|
|
113
|
+
### Events
|
|
114
|
+
|
|
115
|
+
| Event | Payload |
|
|
116
|
+
| --- | --- |
|
|
117
|
+
| `state` | `"idle" \| "connecting" \| "active" \| "ended" \| "error"` |
|
|
118
|
+
| `started` | `{ callId }`. The agent is live. |
|
|
119
|
+
| `transcript` | `{ role: "user" \| "agent", text, final }`. A caller's line arrives as `final: false` while they speak, then settles. |
|
|
120
|
+
| `agent_speaking` | `{ speaking }`. True from the first audio until it has played out. |
|
|
121
|
+
| `interrupted` | The caller talked over the agent and its speech was cut off. |
|
|
122
|
+
| `level` | `{ level }`, microphone level 0 to 1, about ten times a second. |
|
|
123
|
+
| `ended` | `{ reason: "hangup" \| "agent_ended" \| "connection_lost" }` |
|
|
124
|
+
| `error` | `{ code, message, reason? }` |
|
|
125
|
+
|
|
126
|
+
Error codes: `unsupported`, `mic_denied`, `mic_unavailable`, `token_unavailable`, `refused` (token invalid, expired or used, or this page's origin not allowed; `reason` says which), `busy` (too many calls running), `call_refused` (declined by the server, `reason` says why, for example `insufficient_credit`), `server_error`, `connection_failed`.
|
|
127
|
+
|
|
128
|
+
### There is no reconnecting
|
|
129
|
+
|
|
130
|
+
The conversation lives on the server for as long as the connection does, and the token is single-use. A call that drops has ended (`reason: "connection_lost"`); start a new one. A page that wants to carry on should tell the visitor and start again.
|
|
131
|
+
|
|
132
|
+
## Things worth knowing
|
|
133
|
+
|
|
134
|
+
- **Content-Security-Policy.** The audio processor is loaded from a `blob:` URL. If your page sets a CSP, `script-src` needs `blob:`, or `start()` fails with `unsupported`.
|
|
135
|
+
- **Sample rate.** The microphone is captured at the device's own rate and converted to 16 kHz in the audio thread; the agent's voice is 16 kHz and the browser resamples it for the speakers. Nothing to configure.
|
|
136
|
+
- **Autoplay.** Call `start()` straight from a click or tap handler. The audio output is created synchronously inside it, before any `await`, which is what browsers count as the visitor starting it.
|
|
137
|
+
- **Billing.** Browser calls are billed like any other call. `webCalls.create` fails with 402 if the organization cannot pay for one.
|
|
138
|
+
|
|
139
|
+
## License
|
|
140
|
+
|
|
141
|
+
Apache-2.0
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
<div align="center">
|
|
146
|
+
|
|
147
|
+
If this SDK saves you time, a ⭐ on [GitHub](https://github.com/DialgemAI/dialgem-client-sdk) helps other developers find it.
|
|
148
|
+
|
|
149
|
+
</div>
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/** A minimal typed event emitter. A handler that throws never stops the others or the call. */
|
|
2
|
+
declare class Emitter<Events extends Record<string, unknown>> {
|
|
3
|
+
private handlers;
|
|
4
|
+
on<K extends keyof Events>(event: K, handler: (payload: Events[K]) => void): () => void;
|
|
5
|
+
off<K extends keyof Events>(event: K, handler: (payload: Events[K]) => void): void;
|
|
6
|
+
protected emit<K extends keyof Events>(event: K, payload: Events[K]): void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
type CallState = "idle" | "connecting" | "active" | "ended" | "error";
|
|
10
|
+
/** How a call that started came to an end. */
|
|
11
|
+
type EndReason =
|
|
12
|
+
/** `stop()` was called. */
|
|
13
|
+
"hangup"
|
|
14
|
+
/** The agent ended the call, or a call limit (maximum length, silence) did. */
|
|
15
|
+
| "agent_ended"
|
|
16
|
+
/** The connection dropped. The call cannot be resumed: start a new one. */
|
|
17
|
+
| "connection_lost";
|
|
18
|
+
type ErrorCode =
|
|
19
|
+
/** The browser lacks something the call needs (WebSocket, Web Audio, AudioWorklet, a microphone API). */
|
|
20
|
+
"unsupported"
|
|
21
|
+
/** The visitor refused microphone access, or it is blocked for this page. */
|
|
22
|
+
| "mic_denied"
|
|
23
|
+
/** No microphone, or it could not be opened. */
|
|
24
|
+
| "mic_unavailable"
|
|
25
|
+
/** `accessToken` was a function and it threw or returned nothing. */
|
|
26
|
+
| "token_unavailable"
|
|
27
|
+
/** The token was invalid, expired or already used, or this page's origin is not allowed. */
|
|
28
|
+
| "refused"
|
|
29
|
+
/** The organization has too many calls running. */
|
|
30
|
+
| "busy"
|
|
31
|
+
/** The server declined the call; `reason` says why (for example `insufficient_credit`). */
|
|
32
|
+
| "call_refused" | "server_error" | "connection_failed";
|
|
33
|
+
interface CallError {
|
|
34
|
+
code: ErrorCode;
|
|
35
|
+
message: string;
|
|
36
|
+
/** For `call_refused`: the server's reason. */
|
|
37
|
+
reason?: string | undefined;
|
|
38
|
+
}
|
|
39
|
+
interface TranscriptEvent {
|
|
40
|
+
role: "user" | "agent";
|
|
41
|
+
text: string;
|
|
42
|
+
/** False while the caller is still speaking (the words so far); true once the line is settled. */
|
|
43
|
+
final: boolean;
|
|
44
|
+
}
|
|
45
|
+
interface CallEvents extends Record<string, unknown> {
|
|
46
|
+
state: CallState;
|
|
47
|
+
started: {
|
|
48
|
+
callId: string;
|
|
49
|
+
};
|
|
50
|
+
transcript: TranscriptEvent;
|
|
51
|
+
agent_speaking: {
|
|
52
|
+
speaking: boolean;
|
|
53
|
+
};
|
|
54
|
+
/** The caller talked over the agent and its speech was cut off. */
|
|
55
|
+
interrupted: Record<string, never>;
|
|
56
|
+
/** Microphone level, 0 to 1, about ten times a second. Not emitted while muted. */
|
|
57
|
+
level: {
|
|
58
|
+
level: number;
|
|
59
|
+
};
|
|
60
|
+
ended: {
|
|
61
|
+
reason: EndReason;
|
|
62
|
+
};
|
|
63
|
+
error: CallError;
|
|
64
|
+
}
|
|
65
|
+
interface MicrophoneOptions {
|
|
66
|
+
deviceId?: string | undefined;
|
|
67
|
+
/** Default true: without it the agent's own voice comes back through the microphone and it answers itself. */
|
|
68
|
+
echoCancellation?: boolean | undefined;
|
|
69
|
+
noiseSuppression?: boolean | undefined;
|
|
70
|
+
autoGainControl?: boolean | undefined;
|
|
71
|
+
}
|
|
72
|
+
interface DialgemCallOptions {
|
|
73
|
+
/**
|
|
74
|
+
* The access token from your backend's `POST /v1/calls/web`, or a function that returns it. A function
|
|
75
|
+
* is called **after** the visitor has allowed the microphone, because the token only lives 60 seconds and
|
|
76
|
+
* the permission prompt can take longer than that.
|
|
77
|
+
*/
|
|
78
|
+
accessToken: string | (() => string | Promise<string>);
|
|
79
|
+
/** Only for a self-hosted deployment. Default `wss://api.dialgem.com`. */
|
|
80
|
+
baseURL?: string | undefined;
|
|
81
|
+
microphone?: MicrophoneOptions | undefined;
|
|
82
|
+
/** @internal Stand-ins for the browser, for tests. */
|
|
83
|
+
platform?: Partial<Platform> | undefined;
|
|
84
|
+
}
|
|
85
|
+
/** What the call needs from the browser. */
|
|
86
|
+
interface Platform {
|
|
87
|
+
WebSocket: typeof WebSocket;
|
|
88
|
+
AudioContext: typeof AudioContext;
|
|
89
|
+
AudioWorkletNode: typeof AudioWorkletNode;
|
|
90
|
+
getUserMedia(constraints: MediaStreamConstraints): Promise<MediaStream>;
|
|
91
|
+
createObjectURL(blob: Blob): string;
|
|
92
|
+
revokeObjectURL(url: string): void;
|
|
93
|
+
Blob: typeof Blob;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* One conversation with one agent.
|
|
97
|
+
*
|
|
98
|
+
* const call = new DialgemCall({ accessToken: () => fetch("/api/start-call").then((r) => r.text()) })
|
|
99
|
+
* call.on("transcript", (t) => ...)
|
|
100
|
+
* await call.start() // from a click: browsers only let a page open the microphone and play sound after one
|
|
101
|
+
* ...
|
|
102
|
+
* call.stop()
|
|
103
|
+
*
|
|
104
|
+
* A call object is used once. Its token is single-use, so a second call needs a new object (and a new token).
|
|
105
|
+
* There is no reconnecting: the conversation lives on the server for as long as the socket does, and a call
|
|
106
|
+
* that drops has ended.
|
|
107
|
+
*/
|
|
108
|
+
declare class DialgemCall extends Emitter<CallEvents> {
|
|
109
|
+
private readonly options;
|
|
110
|
+
private _state;
|
|
111
|
+
private _muted;
|
|
112
|
+
private _callId;
|
|
113
|
+
private readonly platform;
|
|
114
|
+
private ctx;
|
|
115
|
+
private stream;
|
|
116
|
+
private ws;
|
|
117
|
+
private player;
|
|
118
|
+
private source;
|
|
119
|
+
private node;
|
|
120
|
+
private sink;
|
|
121
|
+
private moduleUrl;
|
|
122
|
+
private speakingTimer;
|
|
123
|
+
private drainTimer;
|
|
124
|
+
private ending;
|
|
125
|
+
private pendingRefusal;
|
|
126
|
+
private failure;
|
|
127
|
+
private lastLevelAt;
|
|
128
|
+
private speaking;
|
|
129
|
+
constructor(options: DialgemCallOptions);
|
|
130
|
+
get state(): CallState;
|
|
131
|
+
/** Set once the call has started. */
|
|
132
|
+
get callId(): string | null;
|
|
133
|
+
get muted(): boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Opens the microphone and the call. Call it directly from a click or tap: the browser decides whether the page
|
|
136
|
+
* may play sound at the moment this runs, and a call started from a timer or after other awaits can be silent.
|
|
137
|
+
* Resolves when the connection is open; the call has started when `started` fires. A failure is reported as an
|
|
138
|
+
* `error` event and also rejects.
|
|
139
|
+
*/
|
|
140
|
+
start(): Promise<void>;
|
|
141
|
+
/** Ends the call. Safe to call at any time, any number of times. */
|
|
142
|
+
stop(): void;
|
|
143
|
+
/** While muted the agent hears silence. The call stays open. */
|
|
144
|
+
setMuted(muted: boolean): void;
|
|
145
|
+
private onMessage;
|
|
146
|
+
private onStarted;
|
|
147
|
+
private onCaptured;
|
|
148
|
+
private onAudio;
|
|
149
|
+
private setSpeaking;
|
|
150
|
+
private onCallEnded;
|
|
151
|
+
private onClose;
|
|
152
|
+
private dead;
|
|
153
|
+
/** Ends the call as failed and reports it once. Returns the error, for `start()` to throw. */
|
|
154
|
+
private fail;
|
|
155
|
+
private finish;
|
|
156
|
+
private setState;
|
|
157
|
+
private cleanup;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export { type CallError as C, DialgemCall as D, type EndReason as E, type MicrophoneOptions as M, type TranscriptEvent as T, type CallEvents as a, type CallState as b, type DialgemCallOptions as c, type ErrorCode as d };
|