/** * Example Tool Library * * This file demonstrates how to define and export a library of example tools * using the Persona SDK's createTool utility. These tools can be imported * and used in PersonaRuntimeProvider or other parts of the application to * showcase or test tool integration. */ import { createTool, PersonaConsoleLogger, PersonaTools } from '@applica-software-guru/persona-chat-sdk'; const logger = new PersonaConsoleLogger(); const toolLibrary = [ createTool({ name: 'set_field_value', title: 'Set Field Value', description: 'Sets the value of a specified field in the current form.', parameters: { name: { type: 'string', description: 'The name of the field to set', required: true }, value: { type: 'string', description: 'The value to set for the field', required: true }, }, output: { success: { type: 'boolean', description: 'Indicates if the field value was set successfully' }, }, implementation: async (name: string, value: string) => { const field = document.querySelector(`[name="${name}"]`); if (field) { (field as HTMLInputElement).value = value; return { success: true }; } logger.error(`Field with name "${name}" not found.`); return { success: false }; }, }), createTool({ name: 'sum', title: 'Sum two numbers', description: 'Sums two numbers and returns the result.', parameters: { a: { type: 'number', description: 'First number to sum', required: true }, b: { type: 'number', description: 'Second number to sum', required: true }, }, output: { result: { type: 'number', description: 'Sum of the two numbers' }, }, implementation: async (a: number, b: number) => { const result = a + b; return { result }; }, }), createTool({ name: 'concat', title: 'Concatenate two strings', description: 'Concatenates two strings and returns the result.', parameters: { str1: { type: 'string', description: 'First string to concatenate', required: true }, str2: { type: 'string', description: 'Second string to concatenate', required: true }, }, output: { result: { type: 'string', description: 'Concatenated result of the two strings' }, }, implementation: async (str1: string, str2: string) => { const result = str1 + str2; return { result }; }, }), // User agent tool: createTool({ name: 'userAgent', title: 'Get User Agent', description: 'Retrieves the user agent string from the client.', parameters: {}, output: { result: { type: 'string', description: 'User agent string' }, }, implementation: async () => { return { result: navigator.userAgent }; }, }), // Navigate to URL tool: createTool({ name: 'navigateTo', title: 'Navigate to URL', description: 'Navigates the browser to a specified URL.', parameters: { url: { type: 'string', description: 'The URL to navigate to', required: true }, }, output: { success: { type: 'boolean', description: 'Indicates if navigation was successful' }, }, implementation: async (url: string) => { window.location.href = url; return { success: true }; }, }), // Get current URL tool: createTool({ name: 'getCurrentUrl', title: 'Get Current URL', description: 'Retrieves the current browser URL.', parameters: {}, output: { url: { type: 'string', description: 'Current browser URL' }, }, implementation: async () => { return { url: window.location.href }; }, }), // Current client date and time tool: createTool({ name: 'getCurrentDateTime', title: 'Get Current Date and Time', description: 'Retrieves the current date and time from the client.', parameters: {}, output: { dateTime: { type: 'string', description: 'Current date and time in ISO format' }, }, implementation: async () => { return { dateTime: new Date().toISOString() }; }, }), ]; const tools: PersonaTools = { sum: function (a: number, b: number) { const result = a + b; return { result }; }, concat: function (str1: string, str2: string) { const result = str1 + str2; return { result }; }, navigateTo: function (url: string) { window.location.href = url; return { success: true }; }, getUserAgent: function () { return { result: navigator.userAgent }; }, getCurrentUrl: function () { return { url: window.location.href }; }, getCurrentDateTime: function () { return { dateTime: new Date().toISOString() }; }, }; export { toolLibrary, tools };