Spaces:
Sleeping
Sleeping
File size: 4,070 Bytes
16a84ba |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
import {
LitElement,
html,
} from 'https://cdn.jsdelivr.net/gh/lit/dist@3/core/lit-core.min.js';
import {GeminiLiveApi} from './gemini_live_api.js';
class GeminiLive extends LitElement {
static properties = {
api_config: {type: String},
enabled: {type: Boolean},
endpoint: {type: String},
startEvent: {type: String},
stopEvent: {type: String},
input_prompt: {type: String},
toolCallEvent: {type: String},
tool_call_responses: {type: String},
};
constructor() {
super();
this.onAudioInputReceived = (e) => {
if (!this.api) {
return;
}
this.api.sendAudioChunk(e.detail.data);
};
this.onVideoInputReceived = (e) => {
if (!this.api) {
return;
}
this.sendVideoChunk(e.detail.data);
};
}
connectedCallback() {
super.connectedCallback();
window.addEventListener('audio-input-received', this.onAudioInputReceived);
window.addEventListener('video-input-received', this.onVideoInputReceived);
}
disconnectedCallback() {
super.disconnectedCallback();
window.removeEventListener(
'audio-input-received',
this.onAudioInputReceived,
);
window.removeEventListener(
'video-input-received',
this.onVideoInputReceived,
);
if (this.api) {
this.api.ws.close();
}
}
firstUpdated() {
if (this.enabled) {
this.setupWebSocket();
}
}
setupWebSocket() {
if (this.api) {
return;
}
this.api = new GeminiLiveApi(
this.endpoint,
true,
JSON.parse(this.api_config),
);
this.api.onSetupComplete = () => {
console.log('Setup complete...');
};
this.api.onAudioData = (base64Data) => {
this.dispatchEvent(
new CustomEvent('audio-output-received', {
detail: {data: base64Data},
// Allow event to cross shadow DOM boundaries (both need to be true)
bubbles: true,
composed: true,
}),
);
};
this.api.onClose = () => {
console.log('Web socket closed...');
};
this.api.onToolCall = (toolCalls) => {
this.dispatchEvent(
new MesopEvent(this.toolCallEvent, {
toolCalls: JSON.stringify(toolCalls.functionCalls),
}),
);
};
}
updated(changedProperties) {
if (
changedProperties.has('tool_call_responses') &&
this.tool_call_responses.length > 0
) {
this.api.sendToolResponse(JSON.parse(this.tool_call_responses));
}
if (changedProperties.has('input_prompt') && this.input_prompt.length > 0) {
this.sendTextMessage(this.input_prompt);
}
}
start() {
if (!this.enabled) {
this.dispatchEvent(new MesopEvent(this.startEvent, {}));
this.dispatchEvent(
new CustomEvent('gemini-live-api-started', {
detail: {},
// Allow event to cross shadow DOM boundaries (both need to be true)
bubbles: true,
composed: true,
}),
);
}
this.setupWebSocket();
}
stop() {
this.dispatchEvent(new MesopEvent(this.stopEvent, {}));
this.dispatchEvent(
new CustomEvent('gemini-live-api-stopped', {
detail: {},
// Allow event to cross shadow DOM boundaries (both need to be true)
bubbles: true,
composed: true,
}),
);
if (this.api.ws) {
this.api.ws.close();
}
}
sendTextMessage(text) {
this.api.sendMessage({
client_content: {
turn_complete: true,
turns: [{role: 'user', parts: [{text: text}]}],
},
});
}
sendVideoChunk(base64Image) {
this.api.sendMessage({
realtimeInput: {
mediaChunks: [
{
mime_type: 'image/jpeg',
data: base64Image,
},
],
},
});
}
render() {
if (this.enabled) {
return html`<span @click="${this.stop}"><slot></slot></span>`;
}
return html`<span @click="${this.start}"><slot></slot></span>`;
}
}
customElements.define('gemini-live', GeminiLive);
|