|
| 1 | +const hand = new Hand(); |
| 2 | + |
| 3 | +function talkToTheHand() { |
| 4 | + hand |
| 5 | + .connect() |
| 6 | + .then(() => console.log('Hand is ready')) |
| 7 | + .catch((err) => console.error(err)); |
| 8 | +} |
| 9 | + |
| 10 | +const fns = { |
| 11 | + getPageHTML: () => { |
| 12 | + return { success: true, html: document.documentElement.outerHTML }; |
| 13 | + }, |
| 14 | + changeBackgroundColor: ({ color }) => { |
| 15 | + document.body.style.backgroundColor = color; |
| 16 | + return { success: true, color }; |
| 17 | + }, |
| 18 | + changeTextColor: ({ color }) => { |
| 19 | + document.body.style.color = color; |
| 20 | + return { success: true, color }; |
| 21 | + }, |
| 22 | + showFingers: async ({ numberOfFingers }) => { |
| 23 | + await hand.sendCommand(numberOfFingers); |
| 24 | + return { success: true, numberOfFingers }; |
| 25 | + }, |
| 26 | +}; |
| 27 | + |
| 28 | +// Create a WebRTC Agent |
| 29 | +const peerConnection = new RTCPeerConnection(); |
| 30 | + |
| 31 | +// On inbound audio add to page |
| 32 | +peerConnection.ontrack = (event) => { |
| 33 | + const el = document.createElement('audio'); |
| 34 | + el.srcObject = event.streams[0]; |
| 35 | + el.autoplay = el.controls = true; |
| 36 | + document.body.appendChild(el); |
| 37 | +}; |
| 38 | + |
| 39 | +const dataChannel = peerConnection.createDataChannel('response'); |
| 40 | + |
| 41 | +function configureData() { |
| 42 | + console.log('Configuring data channel'); |
| 43 | + const event = { |
| 44 | + type: 'session.update', |
| 45 | + session: { |
| 46 | + modalities: ['text', 'audio'], |
| 47 | + // Provide the tools. Note they match the keys in the `fns` object above |
| 48 | + tools: [ |
| 49 | + { |
| 50 | + type: 'function', |
| 51 | + name: 'changeBackgroundColor', |
| 52 | + description: 'Changes the background color of a web page', |
| 53 | + parameters: { |
| 54 | + type: 'object', |
| 55 | + properties: { |
| 56 | + color: { type: 'string', description: 'A hex value of the color' }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + type: 'function', |
| 62 | + name: 'changeTextColor', |
| 63 | + description: 'Changes the text color of a web page', |
| 64 | + parameters: { |
| 65 | + type: 'object', |
| 66 | + properties: { |
| 67 | + color: { type: 'string', description: 'A hex value of the color' }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + { |
| 72 | + type: 'function', |
| 73 | + name: 'showFingers', |
| 74 | + description: 'Controls a robot hand to show a specific number of fingers', |
| 75 | + parameters: { |
| 76 | + type: 'object', |
| 77 | + properties: { |
| 78 | + numberOfFingers: { type: 'string', description: 'Values 1 through 5 of the number of fingers to hold up' }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + { |
| 83 | + type: 'function', |
| 84 | + name: 'getPageHTML', |
| 85 | + description: 'Gets the HTML for the current page', |
| 86 | + }, |
| 87 | + ], |
| 88 | + }, |
| 89 | + }; |
| 90 | + dataChannel.send(JSON.stringify(event)); |
| 91 | +} |
| 92 | + |
| 93 | +dataChannel.addEventListener('open', (ev) => { |
| 94 | + console.log('Opening data channel', ev); |
| 95 | + configureData(); |
| 96 | +}); |
| 97 | + |
| 98 | +// { |
| 99 | +// "type": "response.function_call_arguments.done", |
| 100 | +// "event_id": "event_Ad2gt864G595umbCs2aF9", |
| 101 | +// "response_id": "resp_Ad2griUWUjsyeLyAVtTtt", |
| 102 | +// "item_id": "item_Ad2gsxA84w9GgEvFwW1Ex", |
| 103 | +// "output_index": 1, |
| 104 | +// "call_id": "call_PG12S5ER7l7HrvZz", |
| 105 | +// "name": "get_weather", |
| 106 | +// "arguments": "{\"location\":\"Portland, Oregon\"}" |
| 107 | +// } |
| 108 | + |
| 109 | +dataChannel.addEventListener('message', async (ev) => { |
| 110 | + const msg = JSON.parse(ev.data); |
| 111 | + // Handle function calls |
| 112 | + if (msg.type === 'response.function_call_arguments.done') { |
| 113 | + const fn = fns[msg.name]; |
| 114 | + if (fn !== undefined) { |
| 115 | + console.log(`Calling local function ${msg.name} with ${msg.arguments}`); |
| 116 | + const args = JSON.parse(msg.arguments); |
| 117 | + const result = await fn(args); |
| 118 | + console.log('result', result); |
| 119 | + // Let OpenAI know that the function has been called and share it's output |
| 120 | + const event = { |
| 121 | + type: 'conversation.item.create', |
| 122 | + item: { |
| 123 | + type: 'function_call_output', |
| 124 | + call_id: msg.call_id, // call_id from the function_call message |
| 125 | + output: JSON.stringify(result), // result of the function |
| 126 | + }, |
| 127 | + }; |
| 128 | + dataChannel.send(JSON.stringify(event)); |
| 129 | + } |
| 130 | + } |
| 131 | +}); |
| 132 | + |
| 133 | +// Capture microphone |
| 134 | +navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => { |
| 135 | + // Add microphone to PeerConnection |
| 136 | + stream.getTracks().forEach((track) => peerConnection.addTransceiver(track, { direction: 'sendrecv' })); |
| 137 | + |
| 138 | + peerConnection.createOffer().then((offer) => { |
| 139 | + peerConnection.setLocalDescription(offer); |
| 140 | + |
| 141 | + // Send WebRTC Offer to Workers Realtime WebRTC API Relay |
| 142 | + fetch('/rtc-connect', { |
| 143 | + method: 'POST', |
| 144 | + body: offer.sdp, |
| 145 | + headers: { |
| 146 | + 'Content-Type': 'application/sdp', |
| 147 | + }, |
| 148 | + }) |
| 149 | + .then((r) => r.text()) |
| 150 | + .then((answer) => { |
| 151 | + // Accept answer from Realtime WebRTC API |
| 152 | + peerConnection.setRemoteDescription({ |
| 153 | + sdp: answer, |
| 154 | + type: 'answer', |
| 155 | + }); |
| 156 | + }); |
| 157 | + }); |
| 158 | +}); |
0 commit comments