Overview
Function modules are TypeScript/JavaScript files that export functions, classes, or objects that the voice agent can call. They enable custom app behaviors beyond navigation.What Are Function Modules?
Function modules are:- Auto-discovered from configured folders
- Transformed into agent tools with normalized names
- Executable via voice commands or the
execute_app_functiontool - Available on frontend, backend, or both
Export Patterns
NAVAI supports three export shapes:1. Exported Functions
The simplest pattern - export one or more functions:src/ai/functions-modules/session/logout.fn.ts
src/ai/functions-modules/utils/math.ts
sumar(add two numbers)es_mayor_de_edad(check if age >= 18)
2. Exported Classes
Export a class to create tools from its instance methods:src/ai/functions-modules/system/ai-service.ts
servicio_ia_iniciar
To call with constructor args:
3. Exported Objects
Export an object with callable members:src/ai/functions-modules/support/open-help.fn.ts
open_help_center
Name Normalization
All function names are normalized to snake_case:| Export Name | Tool Name |
|---|---|
logout_user | logout_user |
logoutUser | logout_user |
LogoutUser | logout_user |
LOGOUT_USER | logout_user |
sumar | sumar |
esMayorDeEdad | es_mayor_de_edad |
Reserved Names
These names are reserved and cannot be used as direct tools:navigate_toexecute_app_function
execute_app_function if exported.
Context Injection
Functions can optionally receive a context object as the last parameter:- Function arity (parameter count) expects one more argument than provided
Argument Mapping
When the agent calls a function, arguments are resolved in this order:-
payload.args- Direct argument array -
payload.arguments- Alias forargs -
payload.value- Single value becomes first argument -
Full payload - Entire payload becomes first argument
- Context appended - If function arity indicates one more param
File Organization
Organize functions by domain:The
.fn.ts suffix is a convention, not required. Any .ts, .js, .mjs, .cjs, .mts, or .cts file works.Folder Configuration
Configure which folders are scanned:Environment Variable
.env
Backend Options
Frontend Options
Backend vs Frontend Functions
Frontend Functions
Location: Web or mobile app codebase Execution: In the browser or React Native runtime Use cases:- Navigation
- UI state changes
- Client-side storage
- App-specific actions
src/ai/functions-modules/session/logout.fn.ts
Backend Functions
Location: Express backend codebase Execution: On the server Use cases:- Database queries
- API calls to external services
- Server-side computations
- Sensitive operations
src/ai/functions-modules/database/get-user.ts
Execution Precedence
When both frontend and backend have a function with the same name:- Frontend function wins
- Backend function is ignored with a warning
Function Discovery
The runtime scans configured folders and:- Includes files matching
includeExtensions(default:ts,js,mjs,cjs,mts,cts) - Excludes patterns from
exclude(default:node_modules,dist, hidden files) - Ignores
*.d.tsdeclaration files - Builds module loaders for matched files
Function registry is lazy-loaded on first request and cached in-memory. Restart the server to pick up changes.
Module Loaders
Frontend/Mobile
Generate loaders before running:src/ai/generated-module-loaders.ts
Backend
Backend loaders are built dynamically at runtime - no generation needed.Return Values
Functions can return:- Primitives:
string,number,boolean - Objects:
{ ok: true, data: ... } - Promises:
asyncfunctions are supported - Errors: Throw to signal failure
Best Practices
Troubleshooting
”No generated module loaders were found”
Run the loader generation command:Function not available to agent
Check:- File is in a configured
NAVAI_FUNCTIONS_FOLDERSpath - Function is exported (not internal)
- Module loaders were regenerated
- Server was restarted (backend only)
Name collision warnings
Ensure function names are unique across all modules. NAVAI will append suffixes (_2, _3) but this can be confusing.
Context not passed correctly
Verify function signature expects one more parameter than provided args. Example:Next Steps
- Backend Setup - Configure backend function execution
- Web Integration - Use functions in web apps
- Mobile Integration - Use functions in mobile apps