@hashgraphonline/conversational-agent
Version:
Hashgraph Online conversational AI agent implementing HCS-10 communication, HCS-2 registries, and content inscription on Hedera. https://hol.org
82 lines (80 loc) • 3.09 kB
TypeScript
import { StructuredTool } from '@langchain/core/tools';
import { z } from 'zod';
import { CallbackManagerForToolRun } from '@langchain/core/callbacks/manager';
import { FormGenerator } from '../forms/form-generator';
export interface FormValidationConfig {
requireAllFields?: boolean;
skipFields?: string[];
customValidation?: (input: unknown) => boolean;
}
/**
* Generic wrapper for StructuredTools that intercepts execution to check for missing required fields
* and generates forms when validation would benefit from user input.
*
* Tools can implement the FormValidatable interface to provide custom validation logic.
* Otherwise, falls back to schema-based validation.
*/
export declare class FormValidatingToolWrapper<TSchema extends z.ZodObject<z.ZodRawShape, z.UnknownKeysParam, z.ZodTypeAny>> extends StructuredTool<TSchema> {
private originalTool;
private formGenerator;
private validationConfig;
private logger;
name: string;
description: string;
schema: TSchema;
constructor(originalTool: StructuredTool<TSchema>, formGenerator: FormGenerator, config?: FormValidationConfig);
/**
* Validate the input against the schema
*/
private validateInput;
/**
* Gets the shape keys from the schema if it's a ZodObject
*/
private getSchemaShape;
/**
* Executes the wrapped tool's original implementation directly, bypassing wrapper logic.
*/
executeOriginal(input: Record<string, unknown>, runManager?: CallbackManagerForToolRun): Promise<string>;
/**
* Provides access to the wrapped tool instance for executors that want to bypass the wrapper.
*/
getOriginalTool(): StructuredTool<TSchema>;
/**
* Checks if tool implements FormValidatable method
*/
private hasFormValidatableMethod;
/**
* Expose FormValidatable methods by delegating to the underlying tool when available.
*/
getFormSchema(): z.ZodSchema;
getEssentialFields(): string[];
isFieldEmpty(fieldName: string, value: unknown): boolean;
/**
* Calculates which fields are missing from the input
*/
private calculateMissingFields;
/**
* Creates a form message with optional JSON schema
*/
private createFormMessage;
/**
* Type guard to check if a schema is a ZodObject
*/
private isZodObject;
/**
* Check if we should generate a form for this tool invocation
*/
private shouldGenerateForm;
/**
* Checks if input has bypass flags that skip form generation
*/
private hasFormBypassFlags;
/**
* Override _call to intercept tool execution
*/
protected _call(input: z.infer<TSchema>, runManager?: CallbackManagerForToolRun): Promise<string>;
}
/**
* Wrap a tool with form validation capabilities
*/
export declare function wrapToolWithFormValidation<TSchema extends z.ZodObject<z.ZodRawShape, z.UnknownKeysParam, z.ZodTypeAny>>(tool: StructuredTool<TSchema>, formGenerator: FormGenerator, config?: FormValidationConfig): FormValidatingToolWrapper<TSchema>;