|
| 1 | +import { LiveServerMessage, Modality } from "@google/genai"; |
| 2 | +import * as FishjamGemini from "@fishjam-cloud/js-server-sdk/gemini"; |
| 3 | +import { type TrackId, type RoomId } from "@fishjam-cloud/js-server-sdk"; |
| 4 | + |
| 5 | +import { fishjam, genai } from "./clients.js"; |
| 6 | + |
| 7 | +export const createAgent = async ( |
| 8 | + roomId: RoomId, |
| 9 | + systemInstruction: string, |
| 10 | +) => { |
| 11 | + const { agent: fishjamAgent } = await fishjam.createAgent(roomId, { |
| 12 | + output: FishjamGemini.geminiInputAudioSettings, |
| 13 | + }); |
| 14 | + |
| 15 | + const agentTrack = fishjamAgent.createTrack( |
| 16 | + FishjamGemini.geminiOutputAudioSettings, |
| 17 | + ); |
| 18 | + |
| 19 | + let cleanup: CallableFunction | undefined; |
| 20 | + |
| 21 | + const session = await genai.live.connect({ |
| 22 | + model: "gemini-3.1-flash-live-preview", |
| 23 | + config: { |
| 24 | + responseModalities: [Modality.AUDIO], |
| 25 | + systemInstruction, |
| 26 | + tools: [ |
| 27 | + { googleSearch: {} }, |
| 28 | + { |
| 29 | + functionDeclarations: [ |
| 30 | + { |
| 31 | + name: "disconnect", |
| 32 | + description: `Disconnect yourself from the room. |
| 33 | + Use this when the user asks you to disconnect.`, |
| 34 | + }, |
| 35 | + ], |
| 36 | + }, |
| 37 | + ], |
| 38 | + }, |
| 39 | + callbacks: { |
| 40 | + onmessage: async (message: LiveServerMessage) => { |
| 41 | + if (message.data) { |
| 42 | + const audio = Buffer.from(message.data, "base64"); |
| 43 | + fishjamAgent.sendData(agentTrack.id, audio); |
| 44 | + } |
| 45 | + |
| 46 | + if (message.serverContent?.interrupted) { |
| 47 | + fishjamAgent.interruptTrack(agentTrack.id); |
| 48 | + } |
| 49 | + |
| 50 | + message.toolCall?.functionCalls?.forEach((call) => { |
| 51 | + if (call.name === "disconnect") { |
| 52 | + cleanup?.(); |
| 53 | + } |
| 54 | + }); |
| 55 | + }, |
| 56 | + }, |
| 57 | + }); |
| 58 | + |
| 59 | + const room = await fishjam.getRoom(roomId); |
| 60 | + |
| 61 | + const interval = setInterval(async () => { |
| 62 | + const humanPeerVideoTrack = room.peers |
| 63 | + .find(({ type }) => type === "webrtc") |
| 64 | + ?.tracks.find(({ type }) => type === "video"); |
| 65 | + |
| 66 | + if (!humanPeerVideoTrack?.id) return; |
| 67 | + |
| 68 | + const image = await fishjamAgent.captureImage( |
| 69 | + humanPeerVideoTrack.id as TrackId, |
| 70 | + ); |
| 71 | + |
| 72 | + session.sendRealtimeInput({ |
| 73 | + video: { |
| 74 | + data: Buffer.from(image.data).toString("base64"), |
| 75 | + mimeType: image.contentType, |
| 76 | + }, |
| 77 | + }); |
| 78 | + }, 1000); |
| 79 | + |
| 80 | + fishjamAgent.on("trackData", ({ data }) => { |
| 81 | + session.sendRealtimeInput({ |
| 82 | + audio: { |
| 83 | + data: Buffer.from(data).toString("base64"), |
| 84 | + mimeType: FishjamGemini.inputMimeType, |
| 85 | + }, |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + cleanup = () => { |
| 90 | + clearInterval(interval); |
| 91 | + session.close(); |
| 92 | + fishjamAgent.deleteTrack(agentTrack.id); |
| 93 | + fishjamAgent.removeAllListeners("trackData"); |
| 94 | + fishjamAgent.disconnect(); |
| 95 | + }; |
| 96 | +}; |
0 commit comments