81 lines
2.2 KiB
TypeScript
81 lines
2.2 KiB
TypeScript
|
|
import * as interfaces from './smartagent.interfaces.js';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Abstract base class for tool wrappers
|
||
|
|
* All tool implementations should extend this class
|
||
|
|
*/
|
||
|
|
export abstract class BaseToolWrapper implements interfaces.IAgentToolWrapper {
|
||
|
|
abstract name: string;
|
||
|
|
abstract description: string;
|
||
|
|
abstract actions: interfaces.IToolAction[];
|
||
|
|
|
||
|
|
protected isInitialized = false;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Initialize the tool and any required resources
|
||
|
|
*/
|
||
|
|
abstract initialize(): Promise<void>;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Cleanup any resources used by the tool
|
||
|
|
*/
|
||
|
|
abstract cleanup(): Promise<void>;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Execute an action with the given parameters
|
||
|
|
*/
|
||
|
|
abstract execute(
|
||
|
|
action: string,
|
||
|
|
params: Record<string, unknown>
|
||
|
|
): Promise<interfaces.IToolExecutionResult>;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Generate a human-readable summary of what the action will do
|
||
|
|
* This is used by the Guardian to understand the proposed action
|
||
|
|
*/
|
||
|
|
abstract getCallSummary(action: string, params: Record<string, unknown>): string;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Validate that an action exists for this tool
|
||
|
|
* @throws Error if the action is not valid
|
||
|
|
*/
|
||
|
|
protected validateAction(action: string): void {
|
||
|
|
const validAction = this.actions.find((a) => a.name === action);
|
||
|
|
if (!validAction) {
|
||
|
|
const availableActions = this.actions.map((a) => a.name).join(', ');
|
||
|
|
throw new Error(
|
||
|
|
`Unknown action "${action}" for tool "${this.name}". Available actions: ${availableActions}`
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check if the tool is initialized
|
||
|
|
*/
|
||
|
|
protected ensureInitialized(): void {
|
||
|
|
if (!this.isInitialized) {
|
||
|
|
throw new Error(`Tool "${this.name}" is not initialized. Call initialize() first.`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the full tool description including all actions
|
||
|
|
* Used for Driver's tool awareness
|
||
|
|
*/
|
||
|
|
public getFullDescription(): string {
|
||
|
|
const actionDescriptions = this.actions
|
||
|
|
.map((a) => ` - ${a.name}: ${a.description}`)
|
||
|
|
.join('\n');
|
||
|
|
|
||
|
|
return `${this.name}: ${this.description}\nActions:\n${actionDescriptions}`;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the JSON schema for a specific action
|
||
|
|
*/
|
||
|
|
public getActionSchema(action: string): Record<string, unknown> | undefined {
|
||
|
|
const actionDef = this.actions.find((a) => a.name === action);
|
||
|
|
return actionDef?.parameters;
|
||
|
|
}
|
||
|
|
}
|