Schema, Identity, and Enums
TnsAI.Core provides typed schemas for LLM tool definitions, agent identity and communication style modeling, decentralized identifiers (DIDs), and a set of enums that control agent behavior.
ToolDefinition
com.tnsai.schema.ToolDefinition is a typed record representing an LLM tool/function definition. It replaces untyped Map<String, Object> schemas with compile-time safety.
Record Fields
public record ToolDefinition(
String name, // required, non-blank
String description, // defaults to ""
Map<String, Object> parameters, // JSON Schema object, never null
String endpoint, // optional API endpoint, defaults to ""
String apiKeyEnvVar // optional env var name for API key
)Creating Definitions
// Builder (recommended)
ToolDefinition tool = ToolDefinition.builder()
.name("search_weather")
.description("Get weather for a city")
.addParameter("city", "string", "City name", true)
.addParameter("unit", "string", "Temperature unit", false)
.addEnumParameter("format", "Output format", List.of("json", "text"), false)
.endpoint("https://api.weather.com/v1")
.apiKeyEnvVar("WEATHER_API_KEY")
.build();
// Simple (no parameters)
ToolDefinition simple = ToolDefinition.of("get_time", "Returns current UTC time");
// From OpenAI-format map
ToolDefinition parsed = ToolDefinition.fromMap(existingMap);Conversion
// To OpenAI function calling format
Map<String, Object> map = tool.toMap();
// {"type": "function", "function": {"name": ..., "description": ..., "parameters": ...}}
// Batch conversions
List<ToolDefinition> defs = ToolDefinition.fromMaps(listOfMaps);
List<Map<String, Object>> maps = ToolDefinition.toMaps(listOfDefs);Query Methods
| Method | Return | Description |
|---|---|---|
hasParameters() | boolean | True if properties map is non-empty |
requiredParameters() | List<String> | Names of required parameters |
properties() | Map<String, Object> | Parameter property schemas |
requiresApiKey() | boolean | True if apiKeyEnvVar is set |
Builder API
| Method | Description |
|---|---|
name(String) | Set tool name |
description(String) | Set description |
addParameter(name, type, description, required) | Add a parameter |
addEnumParameter(name, description, values, required) | Add enum-constrained parameter |
parameters(Map<String, Object>) | Set raw parameters schema |
endpoint(String) | Set API endpoint URL |
apiKeyEnvVar(String) | Set API key environment variable |
noApiKeyRequired() | Clear API key requirement |
ToolSchemaGenerator
com.tnsai.schema.ToolSchemaGenerator generates tool/function schemas for LLM function calling from roles and tools.
Generating Schemas
ToolSchemaGenerator generator = new ToolSchemaGenerator();
// Typed ToolDefinition records (preferred)
List<ToolDefinition> defs = generator.generateToolDefinitions(roles);
// Single action
ToolDefinition def = generator.generateToolDefinition(action);
// From a standalone Tool
ToolDefinition def = generator.generateToolDefinition(tool);
// Legacy Map format
List<Map<String, Object>> schemas = generator.generateToolSchemas(roles);
Map<String, Object> schema = generator.generateToolSchema(action);
// Human-readable description for system prompts
String desc = generator.generateToolsDescription(roles);Configuration
// Enable/disable example inclusion in schemas
generator.withExamples(true);Java-to-JSON-Schema Type Mapping
| Java Type | JSON Schema Type |
|---|---|
String, char, Character | "string" |
int, Integer, long, Long, short, byte | "integer" |
double, Double, float, Float | "number" |
boolean, Boolean | "boolean" |
Arrays, List | "array" |
| Enums | "string" (with enum constraint) |
| Other | "object" |
AgentIdentity
com.tnsai.models.agent.AgentIdentity represents a personality trait or characteristic that shapes agent behavior. Identities are included in the system prompt to influence communication style.
public final class AgentIdentity {
private final String name; // required, non-blank
private final String description; // required, non-blank
}Usage
AgentIdentity analytical = new AgentIdentity(
"analytical",
"Takes a data-driven approach to problem solving"
);
AgentIdentity empathetic = new AgentIdentity(
"empathetic",
"Shows understanding and consideration for user emotions"
);
// In an Agent subclass
@Override
protected List<AgentIdentity> getIdentities() {
return List.of(
new AgentIdentity("expert", "Deep knowledge in software engineering"),
new AgentIdentity("patient", "Takes time to explain concepts clearly"),
new AgentIdentity("thorough", "Considers all aspects before responding")
);
}Common Identity Types
| Category | Examples |
|---|---|
| Cognitive | analytical, creative, logical, intuitive |
| Social | friendly, professional, empathetic, direct |
| Behavioral | proactive, thorough, efficient, cautious |
| Domain | expert, specialist, generalist |
The class is immutable and thread-safe. It supports Jackson JSON serialization via @JsonCreator/@JsonProperty.
Communication (Style)
com.tnsai.models.agent.Communication is a record that defines how an agent expresses itself.
public record Communication(
Tone tone,
Formality formality,
Verbosity verbosity
)Usage
Communication style = new Communication(
Tone.FRIENDLY,
Formality.CASUAL,
Verbosity.CONCISE
);
// Default style
Communication defaultStyle = Communication.defaultStyle();
// Tone.PROFESSIONAL, Formality.NEUTRAL, Verbosity.MODERATE
// Get description
String desc = style.getDescription();
// "Tone: Warm and approachable, Formality: ..., Verbosity: ..."
// Generate prompt section for LLM
String promptSection = style.generatePromptSection();Null values default to: Tone.PROFESSIONAL, Formality.NEUTRAL, Verbosity.MODERATE.
DID (Decentralized Identifier)
com.tnsai.identity.DID implements W3C DID Core for agent identification.
Format: did:<method>:<method-specific-id>
// Parse from string
DID did = DID.parse("did:wba:example.com:agent-123");
// Create from components
DID did = new DID("wba", "example.com:agent-123");
// Factory methods
DID wba = DID.createWba("example.com", "agent-123");
// did:wba:example.com:agent-123
DID web = DID.createWeb("example.com");
// did:web:example.comMethods
| Method | Return | Description |
|---|---|---|
getMethod() | String | DID method (e.g., "wba", "web", "key") |
getMethodSpecificId() | String | Method-specific identifier |
getDidString() | String | Full DID string |
asString() | String | Alias for getDidString() |
toURI() | URI | DID as a java.net.URI |
isWba() | boolean | Check if did:wba |
isWeb() | boolean | Check if did:web |
Validation: method must be lowercase alphanumeric ([a-z0-9]+). Invalid format throws IllegalArgumentException.
Core Enums
ActionType
com.tnsai.enums.ActionType -- execution method for actions.
| Value | Description |
|---|---|
LOCAL | Direct Java method invocation |
WEB_SERVICE | HTTP API calls |
LLM | LLM with tool selection |
MCP_TOOL | Model Context Protocol |
AgentVariant
com.tnsai.enums.AgentVariant -- quality/speed/cost tiers.
| Variant | Quality | Speed | Cost | Description |
|---|---|---|---|---|
HIGH | MAX | SLOW | HIGH | Complex/critical tasks |
MEDIUM | BALANCED | NORMAL | MEDIUM | Regular development |
MINI | BASIC | FAST | LOW | Quick fixes |
AUTO | ADAPTIVE | ADAPTIVE | OPTIMAL | Task-based selection |
agent.setVariant(AgentVariant.HIGH);
// Auto-select based on task keywords
AgentVariant suggested = AgentVariant.forTask("Complex refactoring"); // HIGH
AgentVariant suggested = AgentVariant.forTask("Fix typo"); // MINIKey methods: isQualityFocused(), isSpeedFocused(), isCostOptimized(), forTask(String).
Sub-enums: Quality (MAX/BALANCED/BASIC/ADAPTIVE), Speed (SLOW/NORMAL/FAST/ADAPTIVE), Cost (HIGH/MEDIUM/LOW/OPTIMAL).
Tone
com.tnsai.enums.agent.Tone -- communication tone.
| Value | Description |
|---|---|
ANALYTICAL | Analytical and logical |
EMPATHETIC | Understanding and compassionate |
ASSERTIVE | Direct and confident |
FRIENDLY | Warm and approachable |
PROFESSIONAL | Formal and business-like |
CREATIVE | Imaginative and innovative |
Formality
com.tnsai.enums.agent.Formality -- language formality level.
Verbosity
com.tnsai.enums.agent.Verbosity -- response length preference.
AuthType
com.tnsai.enums.AuthType -- authentication types for web service actions: NO_AUTH, BEARER, BASIC.
HttpMethod
com.tnsai.enums.HttpMethod -- HTTP methods: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS.
Related Documentation
- Action System -- how ActionType routes to executors
- Tools -- the Tool interface that ToolDefinition describes
- Variants -- detailed variant configuration
- Advanced Agent Features -- how identities and variants are used
- Roles -- role-based action discovery with @ActionSpec
Error Handling
TnsAI.Core provides a structured exception hierarchy rooted in TnsAIException. Every exception carries an error code, retryability flag, and suggested retry parameters, enabling automated recovery decisions across the framework.
Advanced Agent Features
Beyond the basic Agent lifecycle (create, chat, stop), TnsAI.Core provides specialized subsystems for cognitive support, streaming, ensemble execution, hierarchy management, and chat orchestration. These are internal components extracted from the Agent class for cohesion; most are accessed through the Agent public API rather than directly.