I am using the multimodal sample as a base but I can’t figure out how to log the result if Google Search is called as a tool
import { type FunctionDeclaration, SchemaType } from "@google/generative-ai";
import { useEffect, useRef, useState, memo } from "react";
import vegaEmbed from "vega-embed";
import { useLiveAPIContext } from "../../contexts/LiveAPIContext";
import { ToolCall } from "../../multimodal-live-types";
const declaration: FunctionDeclaration = {
name: "render_altair",
description: "Displays an altair graph in json format.",
parameters: {
type: SchemaType.OBJECT,
properties: {
json_graph: {
type: SchemaType.STRING,
description:
"JSON STRING representation of the graph to render. Must be a string, not a json object",
},
},
required: ["json_graph"],
},
};
const displayTextDeclaration: FunctionDeclaration = {
name: "display_text",
description: "Displays formatted text content in the main window",
parameters: {
type: SchemaType.OBJECT,
properties: {
text: {
type: SchemaType.STRING,
description: "The text content to display",
},
style: {
type: SchemaType.STRING,
description: "Optional CSS styling (e.g., 'font-size: 16px; line-height: 1.5')",
},
},
required: ["text"],
},
};
function AltairComponent() {
const [jsonString, setJSONString] = useState<string>("");
const { client, setConfig } = useLiveAPIContext();
const embedRef = useRef<HTMLDivElement>(null);
// Config useEffect
useEffect(() => {
setConfig({
model: "models/gemini-2.0-flash-exp",
generationConfig: {
responseModalities: "audio",
speechConfig: {
voiceConfig: { prebuiltVoiceConfig: { voiceName: "Aoede" } },
},
},
systemInstruction: {
parts: [
{
text: 'You are my helpful assistant. Any time I ask you for a graph call the "render_altair" function. Don\'t ask for additional information just make your best judgement.',
},
],
},
tools: [
{ googleSearch: {} },
{ functionDeclarations: [declaration, displayTextDeclaration] },
],
});
}, [setConfig]);
// Vega embed useEffect
useEffect(() => {
if (embedRef.current && jsonString) {
vegaEmbed(embedRef.current, JSON.parse(jsonString));
}
}, [embedRef, jsonString]);
// Tool call handler
useEffect(() => {
const onToolCall = (toolCall: ToolCall) => {
// Debug logging for entire tool call
console.log('Full Tool Call Object:', {
timestamp: new Date().toISOString(),
toolCall: JSON.parse(JSON.stringify(toolCall)), // Deep clone to see all properties
});
toolCall.functionCalls.forEach((fc, index) => {
console.log(`Function Call ${index} Details:`, {
name: fc.name,
id: fc.id,
args: fc.args,
// Log all properties of the function call for debugging
allProperties: Object.keys(fc),
fullFunctionCall: JSON.parse(JSON.stringify(fc))
});
// Enhanced Google Search Logging
if (fc.name === 'googleSearch') {
// Log when Google Search tool is invoked
console.log('Google Search Tool Invoked:', {
id: fc.id,
timestamp: new Date().toISOString(),
toolCallName: fc.name, // Explicitly log the tool name
status: 'invoked',
functionCallArgs: fc.args // Log arguments, might be empty but good to check
});
} else if (fc.name === declaration.name) { // your altair function
const str = (fc.args as any).json_graph;
setJSONString(str);
} else if (fc.name === "display_text") { // your display text function
const args = fc.args as any;
const div = document.createElement('div');
div.style.cssText = args.style || 'font-size: 16px; line-height: 1.5; padding: 20px;';
div.textContent = args.text;
if (embedRef.current) {
embedRef.current.innerHTML = '';
embedRef.current.appendChild(div);
}
}
});
// Send tool response and log results
if (toolCall.functionCalls.length) {
setTimeout(
() => {
const responses = toolCall.functionCalls.map((fc) => {
// Logging when sending tool response, especially for googleSearch
if (fc.name === 'googleSearch') {
console.log('Sending Google Search Tool Response:', {
timestamp: new Date().toISOString(),
functionCall: {
name: fc.name,
id: fc.id,
args: fc.args, // Log args again for context
},
responseStatus: 'success' // Assuming a successful response is sent back
});
}
return {
response: { output: { success: true } },
id: fc.id,
};
});
client.sendToolResponse({
functionResponses: responses,
});
},
200,
);
}
};
client.on("toolcall", onToolCall);
return () => {
client.off("toolcall", onToolCall);
};
}, [client, declaration, setJSONString, embedRef]); // Added dependencies
return <div className="vega-embed" ref={embedRef} />;
}
export const Altair = memo(AltairComponent);