|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <script src="../../dist/umd/deepgram.js"></script> |
| 5 | + </head> |
| 6 | + <body> |
| 7 | + Running test... check the developer console. |
| 8 | + <button type="button">Start</button> |
| 9 | + </body> |
| 10 | + <script> |
| 11 | + const { createClient, AgentEvents } = deepgram; |
| 12 | + const _deepgram = createClient("put yo key here"); |
| 13 | + |
| 14 | + console.log("Deepgram Instance: ", _deepgram); |
| 15 | + |
| 16 | + (async () => { |
| 17 | + const connection = _deepgram.agent(); |
| 18 | + connection.on(AgentEvents.Welcome, () => { |
| 19 | + console.log("WS Connected"); |
| 20 | + }); |
| 21 | + connection.on(AgentEvents.Open, async () => { |
| 22 | + console.log("Connection opened"); |
| 23 | + |
| 24 | + await connection.configure({ |
| 25 | + audio: { |
| 26 | + input: { |
| 27 | + encoding: "linear16", |
| 28 | + sampleRate: 44100, |
| 29 | + }, |
| 30 | + output: { |
| 31 | + encoding: "linear16", |
| 32 | + bitrate: 32000, |
| 33 | + }, |
| 34 | + }, |
| 35 | + agent: { |
| 36 | + listen: { |
| 37 | + model: "nova-2", |
| 38 | + }, |
| 39 | + speak: { |
| 40 | + model: "aura-asteria-en", |
| 41 | + }, |
| 42 | + think: { |
| 43 | + provider: { |
| 44 | + type: "anthropic", |
| 45 | + }, |
| 46 | + model: "claude-3-haiku-20240307", |
| 47 | + }, |
| 48 | + }, |
| 49 | + }); |
| 50 | + console.log("Deepgram Agent configured."); |
| 51 | + |
| 52 | + setInterval(() => { |
| 53 | + console.log("Keep alive!"); |
| 54 | + void connection.keepAlive(); |
| 55 | + }, 5000); |
| 56 | + }); |
| 57 | + |
| 58 | + connection.on(AgentEvents.Close, () => { |
| 59 | + console.log("Connection closed"); |
| 60 | + }); |
| 61 | + |
| 62 | + connection.on(AgentEvents.UserStartedSpeaking, () => { |
| 63 | + console.log("Interrupting agent."); |
| 64 | + }); |
| 65 | + |
| 66 | + connection.on(AgentEvents.AgentThinking, () => { |
| 67 | + console.log("Agent thinking."); |
| 68 | + }); |
| 69 | + |
| 70 | + connection.on(AgentEvents.AgentStartedSpeaking, () => { |
| 71 | + console.log("Agent started speaking."); |
| 72 | + }); |
| 73 | + |
| 74 | + connection.on(AgentEvents.ConversationText, (data) => { |
| 75 | + console.log(JSON.stringify(data, null, 2)); |
| 76 | + }); |
| 77 | + |
| 78 | + connection.on(AgentEvents.Metadata, (data) => { |
| 79 | + console.dir(data); |
| 80 | + }); |
| 81 | + |
| 82 | + connection.on(AgentEvents.Audio, (data) => { |
| 83 | + console.log("Playing audio."); |
| 84 | + const source = audio.createBufferSource(data); |
| 85 | + source.connect(audio.destination); |
| 86 | + source.start(); |
| 87 | + }); |
| 88 | + |
| 89 | + connection.on(AgentEvents.Error, (err) => { |
| 90 | + console.error("Error!"); |
| 91 | + console.error(err); |
| 92 | + console.error(err.message); |
| 93 | + }); |
| 94 | + |
| 95 | + connection.on(AgentEvents.AgentAudioDone, async () => { |
| 96 | + console.log("Agent audio done."); |
| 97 | + }); |
| 98 | + |
| 99 | + connection.on(AgentEvents.Unhandled, (data) => { |
| 100 | + console.dir(data); |
| 101 | + }); |
| 102 | + |
| 103 | + const media = await navigator.mediaDevices.getUserMedia({ |
| 104 | + audio: true, |
| 105 | + video: false, |
| 106 | + }); |
| 107 | + const mic = new MediaRecorder(media); |
| 108 | + const btn = document.querySelector("button"); |
| 109 | + console.log(btn); |
| 110 | + btn.addEventListener("click", (event) => { |
| 111 | + if (mic.state === "recording") { |
| 112 | + mic.stop(); |
| 113 | + event.target.innerText = "Start"; |
| 114 | + } else { |
| 115 | + mic.start(); |
| 116 | + event.target.innerText = "Stop"; |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + mic.ondataavailable = async (event) => { |
| 121 | + console.log("Data available."); |
| 122 | + const chunk = await event.data.getReader(); |
| 123 | + data = data.concat(chunk); |
| 124 | + await connection.send(data); |
| 125 | + }; |
| 126 | + })(); |
| 127 | + // ... |
| 128 | + </script> |
| 129 | +</html> |
0 commit comments