Tool
Abstract base class for defining tools the model can call during generation.
Abstract Members
Subclasses must implement:
ts
abstract class Tool {
abstract readonly name: string;
abstract readonly description: string;
abstract readonly argumentsSchema: GenerationSchema;
abstract call(args: GeneratedContent): Promise<string>;
}| Member | Type | Description |
|---|---|---|
name | string | Unique tool identifier |
description | string | What the tool does (visible to the model) |
argumentsSchema | GenerationSchema | Schema defining the tool's arguments |
call(args) | async (GeneratedContent) => string | Handler invoked when the model calls this tool |
Methods
dispose()
Release the native callback. Call after all sessions using this tool are done.
ts
dispose(): voidExample
ts
import { Tool, GenerationSchema, GeneratedContent, GenerationGuide } from "tsfm-sdk";
class WeatherTool extends Tool {
readonly name = "get_weather";
readonly description = "Gets current weather for a city.";
readonly argumentsSchema = new GenerationSchema("WeatherParams", "")
.property("city", "string", { description: "City name" })
.property("units", "string", {
description: "Temperature units",
guides: [GenerationGuide.anyOf(["celsius", "fahrenheit"])],
});
async call(args: GeneratedContent): Promise<string> {
const city = args.value<string>("city");
const units = args.value<string>("units");
return `Sunny, 22°C in ${city} (${units})`;
}
}Lifecycle
- Create the tool instance
- Pass to
LanguageModelSession({ tools: [tool] }) - The tool's callback is registered internally when the session is created
- After all sessions are disposed, call
tool.dispose()
Tools can be shared across multiple sessions. The native callback remains registered until dispose() is called.