Skip to main content

Tool & Function Types

Types for defining tools, functions, and their execution configurations.

Overview

Tool types define how LLMs can call external functions, including schemas, parameters, HTTP configurations, and retry logic.

Tool

Union type for tool definitions (currently only functions).
type Tool = FunctionTool;
Example:
const tool: Tool = {
  type: 'function',
  definition: {
    schema: {
      name: 'get_weather',
      description: 'Get current weather',
      parameters: {
        type: 'object',
        properties: {
          city: { type: 'string' }
        },
        required: ['city']
      }
    }
  }
};

FunctionTool

Function/tool definition with schema and optional HTTP request configuration.
interface FunctionTool {
  type: 'function';
  definition: {
    schema: Function;
  };
  request?: FunctionRequest | null;
}
Example:
const functionTool: FunctionTool = {
  type: 'function',
  definition: {
    schema: {
      name: 'search_database',
      description: 'Search internal database',
      parameters: {
        type: 'object',
        properties: {
          query: { type: 'string', description: 'Search query' },
          limit: { type: 'number', default: 10 }
        },
        required: ['query']
      }
    }
  },
  request: {
    type: 'http',
    method: 'post',
    url: 'https://api.myapp.com/search',
    headers: {
      'Authorization': 'Bearer {{API_KEY}}'
    }
  }
};

Function

Function/tool schema definition for LLM function calling.
interface Function {
  name: string;                    // pattern: ^[a-zA-Z0-9_]{1,64}$
  description: string;             // max 4096 chars
  parameters: Record<string, any>; // JSON schema
  strict?: boolean | null;
}
Example:
const weatherFunction: Function = {
  name: 'get_weather',
  description: 'Get current weather for a city',
  parameters: {
    type: 'object',
    properties: {
      city: {
        type: 'string',
        description: 'City name'
      },
      units: {
        type: 'string',
        enum: ['celsius', 'fahrenheit'],
        default: 'celsius',
        description: 'Temperature units'
      }
    },
    required: ['city']
  },
  strict: true
};
JSON:
{
  "name": "get_weather",
  "description": "Get weather for a city",
  "parameters": {
    "type": "object",
    "properties": {
      "city": { "type": "string" }
    },
    "required": ["city"]
  }
}

FunctionParameters

Complete JSON schema for function parameters object.
interface FunctionParameters {
  type: 'object';
  title?: string | null;
  $defs?: Record<string, any> | null;
  properties?: Record<string, FunctionParameter> | null;
  required?: string[] | null;
}
Example:
const parameters: FunctionParameters = {
  type: 'object',
  title: 'WeatherParams',
  properties: {
    city: {
      type: 'string',
      description: 'City name'
    },
    units: {
      type: 'string',
      enum: ['celsius', 'fahrenheit'],
      default: 'celsius'
    },
    includeHourly: {
      type: 'boolean',
      default: false
    }
  },
  required: ['city']
};

FunctionParameter

JSON schema definition for a single function parameter.
interface FunctionParameter {
  anyOf?: object[] | null;
  type?: 'object' | 'array' | 'number' | 'string' | 'boolean' | 'null' | null;
  default?: any | null;
  title?: string | null;
  description?: string | null;     // max 4096 chars
  properties?: Record<string, any> | null;
  required?: string[] | null;
  minItems?: number | null;
  maxItems?: number | null;
  items?: object | null;
  enum?: (string | number | boolean | null)[] | null;
  minimum?: number | null;
  maximum?: number | null;
  minLength?: number | null;
  maxLength?: number | null;
}
Examples:
// String parameter
const cityParam: FunctionParameter = {
  type: 'string',
  description: 'City name',
  minLength: 1,
  maxLength: 100
};

// Number parameter
const temperatureParam: FunctionParameter = {
  type: 'number',
  description: 'Temperature in celsius',
  minimum: -50,
  maximum: 50
};

// Enum parameter
const unitsParam: FunctionParameter = {
  type: 'string',
  enum: ['celsius', 'fahrenheit', 'kelvin'],
  default: 'celsius',
  description: 'Temperature units'
};

// Array parameter
const citiesParam: FunctionParameter = {
  type: 'array',
  items: { type: 'string' },
  minItems: 1,
  maxItems: 10,
  description: 'List of cities'
};

// Object parameter
const locationParam: FunctionParameter = {
  type: 'object',
  properties: {
    lat: { type: 'number' },
    lon: { type: 'number' }
  },
  required: ['lat', 'lon'],
  description: 'Geographic coordinates'
};

FunctionRequest

Union type for function execution methods.
type FunctionRequest = FunctionRequestHttp;

FunctionRequestHttp

HTTP request configuration for executing functions via REST API.
interface FunctionRequestHttp {
  type: 'http';
  method: 'get' | 'post';
  url: string;
  headers?: Record<string, string> | null;
  query?: Record<string, string> | null;
  body?: Record<string, any> | null;
  proxyUrl?: string | null;
  proxyHeaders?: Record<string, string> | null;
  retry?: FunctionRequestRetry | null;
}
Example:
const httpRequest: FunctionRequestHttp = {
  type: 'http',
  method: 'post',
  url: 'https://api.weather.com/v1/current',
  headers: {
    'Authorization': 'Bearer {{API_KEY}}',
    'Content-Type': 'application/json'
  },
  query: {
    'units': 'metric'
  },
  body: {
    location: '{{city}}'
  },
  retry: {
    maxAttempts: 3,
    initialDelay: 1000,
    exponentialFactor: 2
  }
};
JSON:
{
  "type": "http",
  "method": "post",
  "url": "https://api.example.com/function",
  "headers": {
    "Authorization": "Bearer token"
  },
  "retry": {
    "maxAttempts": 3,
    "initialDelay": 1000,
    "exponentialFactor": 2
  }
}

FunctionRequestRetry

Retry configuration with exponential backoff.
interface FunctionRequestRetry {
  maxAttempts: number;             // minimum: 1
  initialDelay: number;            // minimum: 1 (milliseconds)
  exponentialFactor: number;       // minimum: 1
}
Example:
const retryConfig: FunctionRequestRetry = {
  maxAttempts: 3,
  initialDelay: 1000,              // Start with 1 second
  exponentialFactor: 2             // Double each retry: 1s, 2s, 4s
};

// More aggressive
const aggressiveRetry: FunctionRequestRetry = {
  maxAttempts: 5,
  initialDelay: 500,
  exponentialFactor: 3             // 0.5s, 1.5s, 4.5s, 13.5s, 40.5s
};

Complete Example

import type {
  Tool,
  FunctionTool,
  Function,
  FunctionParameters,
  FunctionRequestHttp,
  FunctionRequestRetry
} from '@adaline/api';

// Define retry configuration
const retry: FunctionRequestRetry = {
  maxAttempts: 3,
  initialDelay: 1000,
  exponentialFactor: 2
};

// Define HTTP request
const request: FunctionRequestHttp = {
  type: 'http',
  method: 'post',
  url: 'https://api.weather.com/current',
  headers: {
    'Authorization': 'Bearer sk_abc123',
    'Content-Type': 'application/json'
  },
  body: {
    city: '{{city}}',
    units: '{{units}}'
  },
  retry
};

// Define function schema
const schema: Function = {
  name: 'get_weather',
  description: 'Get current weather for a city',
  parameters: {
    type: 'object',
    properties: {
      city: {
        type: 'string',
        description: 'City name'
      },
      units: {
        type: 'string',
        enum: ['celsius', 'fahrenheit'],
        default: 'celsius'
      }
    },
    required: ['city']
  }
};

// Complete tool definition
const tool: Tool = {
  type: 'function',
  definition: { schema },
  request
};

// Use in deployment
const deployment = await adaline.getLatestDeployment({
  promptId: 'prompt_with_tools',
  deploymentEnvironmentId: 'production'
});

console.log(`Tools available: ${deployment.prompt.tools.length}`);
deployment.prompt.tools.forEach(t => {
  const fn = t.definition.schema;
  console.log(`  - ${fn.name}: ${fn.description}`);
});