Building Custom Functions with GAME SDK: A TypeScript Guide
Introduction
Hey developers! 👋 Recently, we released the GAME Typescript SDK, which allows you to develop your agents powered by the GAME architecture in its most fullest and most flexible form. One can read more about the GAME framework in our whitepaper.
Since then, some of you have requested for more examples of custom functions built with the SDK.
In this article, we demonstrate how a custom function could be defined and used, by defining a receipe AI agent that uses a custom function to get recipes. We will use a simple recipe helper to demonstrate the power and flexibility of custom functions.
Key Components of a Custom Function 🛠
A custom function is typically made up of 4 main components. This is outlined below:
Function Definition
new GameFunction({ name: string, description: string, args: Array<{name: string, description: string}>, executable: async (args) => ExecutableGameFunctionResponse })Arguments Structure
args: [ { name: "parameter_name", description: "What this parameter does" } ]Function Logic The executable is where the main function logic lives. It is where you can call an API or manipulate data.
executable: async (args) => { try { // API calls const apiData = await fetchFromAPI(args.dish); // Database operations const dbResult = await saveToDatabase(apiData);Response Handling
// Success Response return new ExecutableGameFunctionResponse( ExecutableGameFunctionStatus.Done, JSON.stringify({ your: "data", goes: "here" }) ); // Error Response return new ExecutableGameFunctionResponse( ExecutableGameFunctionStatus.Failed, "Error message here" );
Anatomy of a Custom Function 🔍
Putting it all together, this is how a custom function could look like:
Best Practices
In addition, when building your custom function, we recommend the following best practices to ensure a maintainable and stable codebase for your AI agent:
Error Handling
Always use try/catch blocks
Return meaningful error messages
Log errors for debugging
Async operations
A Practical Example - Building a Recipe AI Agent with GAME Framework
Now, we put the concepts together to build a real AI agent with a custom function!
We'll show you how to create an AI-powered recipe assistant using the GAME framework and Spoonacular API. Let us break down each component and see how they work together.
The recipeFunction.ts file contains these 2 functions:
searchRecipeFunction:
Takes a food name (or generates one if not provided)
Queries the Spoonacular API
Returns detailed recipe information
getRecipeInstructionsFunction:
Takes recipe instructions as input
Formats them for easy reading
Returns structured cooking steps
Here is the implementation of the recipeFunction.ts file:
simpleRecipeWorker.ts:
simpleRecipeAgent.ts:
This is the result we get from the terminal:
Simple interpretation of the result:
When we run the agent, we see this workflow:
Agent initializes and plans its actions:
Agent executes the search:
Returns detailed recipe information:
Cooking time: 45 minutes
Cost per serving: $0.93
Health metrics: 26/100 health score
Dietary info: Gluten-free and dairy-free
This example demonstrates
How to create small, modular functions that interact with real-world APIs to retrieve information about recipes.
Each function uses the GameFunction class to define its purpose, input arguments, and execution logic.
The logic involves making HTTP requests to the Spoonacular API to fetch dynamic data, such as recipe details, search results, or related recipes, based on user input.
How the GAME SDK can seamlessly integrate with external APIs to build practical, feature-rich applications - therefore creating intelligent AI agents that work autonomously.
In our example, the agent doesn't just blindly fetch data - it actively makes decisions about which recipes to choose, transforms complex API responses into meaningful information, and plans its next actions based on the results.
Even with a simple implementation, we can see how the GAME framework enables sophisticated behaviors like decision-making and structured planning, demonstrating that even basic AI agents can provide significant value when properly architected.
Conclusion 🎉
In this article, we built a recipe AI agent using custom functions as our building blocks. Our journey from simple API calls to an autonomous recipe assistant demonstrates how the one can leverage the GAME framework to create intelligent AI agents even without deep AI skills.
From this example, we learn that to build a good custom function, we need to remember to:
Keep functions focused and single-purpose
Handle errors gracefully
Use TypeScript's type system
Document your code well
Test thoroughly
These principles will help you build your own AI agents that are robust, maintainable, and effective at their tasks.
We look forward to seeing what kind of cool AI agents you guys could build with the help of AI agents! Happy coding! 🚀
Stay Connected and Join the Virtuals Community! 🤖 🎈

X: @GAME_Virtuals
For updates and to join our live streaming jam sessions every Wednesday. Stay in the loop and engage with us in real time!

Discord: @Virtuals Protocol
Join Discord for tech support and troubleshooting, and don’t miss our GAME Jam session every Wednesday!

Telegram: @Virtuals Protocol
Join our Telegram group for non-tech support! Whether you need advice, a chat, or just a friendly space, we’re here to help!
Last updated