@hashgraphonline/conversational-agent 0.1.214 → 0.1.217
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/dist/CLIApp.d.ts +9 -0
- package/cli/dist/CLIApp.js +127 -0
- package/cli/dist/LocalConversationalAgent.d.ts +37 -0
- package/cli/dist/LocalConversationalAgent.js +58 -0
- package/cli/dist/app.d.ts +16 -0
- package/cli/dist/app.js +13 -0
- package/cli/dist/cli.d.ts +2 -0
- package/cli/dist/cli.js +51 -0
- package/cli/dist/components/AppContainer.d.ts +16 -0
- package/cli/dist/components/AppContainer.js +24 -0
- package/cli/dist/components/AppScreens.d.ts +2 -0
- package/cli/dist/components/AppScreens.js +259 -0
- package/cli/dist/components/ChatScreen.d.ts +15 -0
- package/cli/dist/components/ChatScreen.js +39 -0
- package/cli/dist/components/DebugLoadingScreen.d.ts +5 -0
- package/cli/dist/components/DebugLoadingScreen.js +31 -0
- package/cli/dist/components/LoadingScreen.d.ts +2 -0
- package/cli/dist/components/LoadingScreen.js +16 -0
- package/cli/dist/components/LoadingScreenDebug.d.ts +5 -0
- package/cli/dist/components/LoadingScreenDebug.js +27 -0
- package/cli/dist/components/MCPConfigScreen.d.ts +28 -0
- package/cli/dist/components/MCPConfigScreen.js +168 -0
- package/cli/dist/components/ScreenRouter.d.ts +12 -0
- package/cli/dist/components/ScreenRouter.js +22 -0
- package/cli/dist/components/SetupScreen.d.ts +15 -0
- package/cli/dist/components/SetupScreen.js +65 -0
- package/cli/dist/components/SingleLoadingScreen.d.ts +5 -0
- package/cli/dist/components/SingleLoadingScreen.js +27 -0
- package/cli/dist/components/StatusBadge.d.ts +7 -0
- package/cli/dist/components/StatusBadge.js +28 -0
- package/cli/dist/components/TerminalWindow.d.ts +8 -0
- package/cli/dist/components/TerminalWindow.js +24 -0
- package/cli/dist/components/WelcomeScreen.d.ts +11 -0
- package/cli/dist/components/WelcomeScreen.js +47 -0
- package/cli/dist/context/AppContext.d.ts +68 -0
- package/cli/dist/context/AppContext.js +363 -0
- package/cli/dist/hooks/useInitializeAgent.d.ts +19 -0
- package/cli/dist/hooks/useInitializeAgent.js +28 -0
- package/cli/dist/hooks/useStableState.d.ts +38 -0
- package/cli/dist/hooks/useStableState.js +68 -0
- package/cli/dist/managers/AgentManager.d.ts +57 -0
- package/cli/dist/managers/AgentManager.js +119 -0
- package/cli/dist/managers/ConfigManager.d.ts +53 -0
- package/cli/dist/managers/ConfigManager.js +173 -0
- package/cli/dist/types.d.ts +31 -0
- package/cli/dist/types.js +19 -0
- package/dist/cjs/base-agent.d.ts +2 -0
- package/dist/cjs/conversational-agent.d.ts +8 -0
- package/dist/cjs/core/ToolRegistry.d.ts +130 -0
- package/dist/cjs/execution/ExecutionPipeline.d.ts +81 -0
- package/dist/cjs/forms/FormEngine.d.ts +121 -0
- package/dist/cjs/forms/field-type-registry.d.ts +51 -0
- package/dist/cjs/forms/form-generator.d.ts +123 -0
- package/dist/cjs/forms/index.d.ts +2 -0
- package/dist/cjs/forms/types.d.ts +108 -0
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +5 -0
- package/dist/cjs/langchain/FormAwareAgentExecutor.d.ts +108 -0
- package/dist/cjs/langchain/FormValidatingToolWrapper.d.ts +81 -0
- package/dist/cjs/langchain-agent.d.ts +65 -0
- package/dist/cjs/memory/ContentStorage.d.ts +7 -0
- package/dist/cjs/memory/SmartMemoryManager.d.ts +1 -0
- package/dist/cjs/services/ContentStoreManager.d.ts +11 -1
- package/dist/cjs/utils/ResponseFormatter.d.ts +26 -0
- package/dist/esm/index.js +8 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index12.js +1 -1
- package/dist/esm/index12.js.map +1 -1
- package/dist/esm/index14.js +23 -5
- package/dist/esm/index14.js.map +1 -1
- package/dist/esm/index15.js +25 -4
- package/dist/esm/index15.js.map +1 -1
- package/dist/esm/index16.js +4 -2
- package/dist/esm/index16.js.map +1 -1
- package/dist/esm/index17.js +2 -7
- package/dist/esm/index17.js.map +1 -1
- package/dist/esm/index18.js +609 -36
- package/dist/esm/index18.js.map +1 -1
- package/dist/esm/index19.js +229 -84
- package/dist/esm/index19.js.map +1 -1
- package/dist/esm/index20.js +111 -17
- package/dist/esm/index20.js.map +1 -1
- package/dist/esm/index21.js +44 -7
- package/dist/esm/index21.js.map +1 -1
- package/dist/esm/index22.js +86 -157
- package/dist/esm/index22.js.map +1 -1
- package/dist/esm/index23.js +32 -150
- package/dist/esm/index23.js.map +1 -1
- package/dist/esm/index24.js +746 -80
- package/dist/esm/index24.js.map +1 -1
- package/dist/esm/index25.js +154 -45
- package/dist/esm/index25.js.map +1 -1
- package/dist/esm/index26.js +149 -24
- package/dist/esm/index26.js.map +1 -1
- package/dist/esm/index27.js +196 -217
- package/dist/esm/index27.js.map +1 -1
- package/dist/esm/index28.js +187 -0
- package/dist/esm/index28.js.map +1 -0
- package/dist/esm/index29.js +308 -0
- package/dist/esm/index29.js.map +1 -0
- package/dist/esm/index30.js +159 -0
- package/dist/esm/index30.js.map +1 -0
- package/dist/esm/index31.js +68 -0
- package/dist/esm/index31.js.map +1 -0
- package/dist/esm/index32.js +30 -0
- package/dist/esm/index32.js.map +1 -0
- package/dist/esm/index33.js +95 -0
- package/dist/esm/index33.js.map +1 -0
- package/dist/esm/index34.js +245 -0
- package/dist/esm/index34.js.map +1 -0
- package/dist/esm/index5.js +2 -2
- package/dist/esm/index5.js.map +1 -1
- package/dist/esm/index6.js +68 -25
- package/dist/esm/index6.js.map +1 -1
- package/dist/esm/index7.js.map +1 -1
- package/dist/esm/index8.js +744 -70
- package/dist/esm/index8.js.map +1 -1
- package/dist/types/base-agent.d.ts +2 -0
- package/dist/types/conversational-agent.d.ts +8 -0
- package/dist/types/core/ToolRegistry.d.ts +130 -0
- package/dist/types/execution/ExecutionPipeline.d.ts +81 -0
- package/dist/types/forms/FormEngine.d.ts +121 -0
- package/dist/types/forms/field-type-registry.d.ts +51 -0
- package/dist/types/forms/form-generator.d.ts +123 -0
- package/dist/types/forms/index.d.ts +2 -0
- package/dist/types/forms/types.d.ts +108 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/langchain/FormAwareAgentExecutor.d.ts +108 -0
- package/dist/types/langchain/FormValidatingToolWrapper.d.ts +81 -0
- package/dist/types/langchain-agent.d.ts +65 -0
- package/dist/types/memory/ContentStorage.d.ts +7 -0
- package/dist/types/memory/SmartMemoryManager.d.ts +1 -0
- package/dist/types/services/ContentStoreManager.d.ts +11 -1
- package/dist/types/utils/ResponseFormatter.d.ts +26 -0
- package/package.json +35 -34
- package/src/base-agent.ts +2 -0
- package/src/config/system-message.ts +14 -0
- package/src/context/ReferenceContextManager.ts +1 -1
- package/src/conversational-agent.ts +95 -38
- package/src/core/ToolRegistry.ts +358 -0
- package/src/execution/ExecutionPipeline.ts +301 -0
- package/src/forms/FormEngine.ts +443 -0
- package/src/forms/field-type-registry.ts +203 -0
- package/src/forms/form-generator.ts +841 -0
- package/src/forms/index.ts +2 -0
- package/src/forms/types.ts +125 -0
- package/src/index.ts +9 -0
- package/src/langchain/FormAwareAgentExecutor.ts +971 -0
- package/src/langchain/FormValidatingToolWrapper.ts +355 -0
- package/src/langchain-agent.ts +1034 -87
- package/src/mcp/ContentProcessor.ts +20 -4
- package/src/mcp/MCPClientManager.ts +1 -1
- package/src/mcp/adapters/langchain.ts +1 -1
- package/src/memory/ContentStorage.ts +25 -5
- package/src/memory/SmartMemoryManager.ts +27 -4
- package/src/memory/TokenCounter.ts +1 -1
- package/src/plugins/hbar/HbarPlugin.ts +0 -1
- package/src/scripts/test-external-tool-wrapper.ts +103 -0
- package/src/scripts/test-hedera-kit-wrapper.ts +265 -0
- package/src/scripts/test-inscribe-form-generation.ts +494 -0
- package/src/scripts/test-inscribe-wrapper-verification.ts +220 -0
- package/src/services/ContentStoreManager.ts +23 -9
- package/src/services/EntityResolver.ts +2 -9
- package/src/tools/EntityResolverTool.ts +5 -8
- package/src/utils/ResponseFormatter.ts +146 -0
- package/cli/readme.md +0 -181
- package/dist/cjs/langchain/ContentAwareAgentExecutor.d.ts +0 -14
- package/dist/types/langchain/ContentAwareAgentExecutor.d.ts +0 -14
- package/src/langchain/ContentAwareAgentExecutor.ts +0 -19
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { StructuredTool } from '@langchain/core/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { CallbackManagerForToolRun } from '@langchain/core/callbacks/manager';
|
|
4
|
+
import { Logger } from '@hashgraphonline/standards-sdk';
|
|
5
|
+
import { FormGenerator } from '../forms/form-generator';
|
|
6
|
+
import { isFormValidatable } from '@hashgraphonline/standards-agent-kit';
|
|
7
|
+
|
|
8
|
+
export interface FormValidationConfig {
|
|
9
|
+
requireAllFields?: boolean;
|
|
10
|
+
skipFields?: string[];
|
|
11
|
+
customValidation?: (input: unknown) => boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Generic wrapper for StructuredTools that intercepts execution to check for missing required fields
|
|
16
|
+
* and generates forms when validation would benefit from user input.
|
|
17
|
+
*
|
|
18
|
+
* Tools can implement the FormValidatable interface to provide custom validation logic.
|
|
19
|
+
* Otherwise, falls back to schema-based validation.
|
|
20
|
+
*/
|
|
21
|
+
export class FormValidatingToolWrapper<
|
|
22
|
+
TSchema extends z.ZodObject<z.ZodRawShape, z.UnknownKeysParam, z.ZodTypeAny>
|
|
23
|
+
> extends StructuredTool<TSchema> {
|
|
24
|
+
private originalTool: StructuredTool<TSchema>;
|
|
25
|
+
private formGenerator: FormGenerator;
|
|
26
|
+
private validationConfig: FormValidationConfig;
|
|
27
|
+
private logger: Logger;
|
|
28
|
+
|
|
29
|
+
name: string;
|
|
30
|
+
description: string;
|
|
31
|
+
schema: TSchema;
|
|
32
|
+
|
|
33
|
+
constructor(
|
|
34
|
+
originalTool: StructuredTool<TSchema>,
|
|
35
|
+
formGenerator: FormGenerator,
|
|
36
|
+
config: FormValidationConfig = {}
|
|
37
|
+
) {
|
|
38
|
+
super();
|
|
39
|
+
this.originalTool = originalTool;
|
|
40
|
+
this.formGenerator = formGenerator;
|
|
41
|
+
this.validationConfig = config;
|
|
42
|
+
this.logger = new Logger({ module: 'FormValidatingToolWrapper' });
|
|
43
|
+
|
|
44
|
+
this.name = originalTool.name;
|
|
45
|
+
this.description = originalTool.description;
|
|
46
|
+
this.schema = originalTool.schema;
|
|
47
|
+
|
|
48
|
+
this.logger.info(`🔧 FormValidatingToolWrapper created for tool: ${this.name}`, {
|
|
49
|
+
originalToolName: originalTool.name,
|
|
50
|
+
originalToolType: originalTool.constructor.name,
|
|
51
|
+
wrapperType: this.constructor.name
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Validate the input against the schema
|
|
57
|
+
*/
|
|
58
|
+
private validateInput(input: Record<string, unknown>): { isValid: boolean; errors?: string[] } {
|
|
59
|
+
try {
|
|
60
|
+
this.schema.parse(input);
|
|
61
|
+
return { isValid: true };
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if (error instanceof z.ZodError) {
|
|
64
|
+
const errors = error.errors
|
|
65
|
+
.filter(err => {
|
|
66
|
+
const fieldName = err.path[0] as string;
|
|
67
|
+
return !this.validationConfig.skipFields?.includes(fieldName);
|
|
68
|
+
})
|
|
69
|
+
.map(err => `${err.path.join('.')}: ${err.message}`);
|
|
70
|
+
return { isValid: false, errors };
|
|
71
|
+
}
|
|
72
|
+
return { isValid: false, errors: ['Validation failed'] };
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Gets the shape keys from the schema if it's a ZodObject
|
|
78
|
+
*/
|
|
79
|
+
private getSchemaShape(): string[] {
|
|
80
|
+
if (this.isZodObject(this.schema)) {
|
|
81
|
+
return Object.keys(this.schema.shape);
|
|
82
|
+
}
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Executes the wrapped tool's original implementation directly, bypassing wrapper logic.
|
|
88
|
+
*/
|
|
89
|
+
public async executeOriginal(
|
|
90
|
+
input: Record<string, unknown>,
|
|
91
|
+
runManager?: CallbackManagerForToolRun
|
|
92
|
+
): Promise<string> {
|
|
93
|
+
type CallableMethod = (
|
|
94
|
+
args: Record<string, unknown>,
|
|
95
|
+
runManager?: CallbackManagerForToolRun
|
|
96
|
+
) => Promise<string>;
|
|
97
|
+
|
|
98
|
+
const tool = this.originalTool as unknown as Record<string, unknown>;
|
|
99
|
+
|
|
100
|
+
if ('_call' in tool && typeof tool._call === 'function') {
|
|
101
|
+
return (tool._call as CallableMethod)(input, runManager);
|
|
102
|
+
}
|
|
103
|
+
if ('call' in tool && typeof tool.call === 'function') {
|
|
104
|
+
return (tool.call as CallableMethod)(input, runManager);
|
|
105
|
+
}
|
|
106
|
+
throw new Error('Original tool has no callable implementation');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Provides access to the wrapped tool instance for executors that want to bypass the wrapper.
|
|
111
|
+
*/
|
|
112
|
+
public getOriginalTool(): StructuredTool<TSchema> {
|
|
113
|
+
return this.originalTool;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Checks if tool implements FormValidatable method
|
|
118
|
+
*/
|
|
119
|
+
private hasFormValidatableMethod<T>(
|
|
120
|
+
tool: unknown,
|
|
121
|
+
methodName: string
|
|
122
|
+
): tool is Record<string, T> {
|
|
123
|
+
return (
|
|
124
|
+
tool !== null &&
|
|
125
|
+
typeof tool === 'object' &&
|
|
126
|
+
methodName in tool &&
|
|
127
|
+
typeof (tool as Record<string, unknown>)[methodName] === 'function'
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Expose FormValidatable methods by delegating to the underlying tool when available.
|
|
133
|
+
*/
|
|
134
|
+
public getFormSchema(): z.ZodSchema {
|
|
135
|
+
if (this.hasFormValidatableMethod<() => z.ZodSchema>(this.originalTool, 'getFormSchema')) {
|
|
136
|
+
return this.originalTool.getFormSchema();
|
|
137
|
+
}
|
|
138
|
+
return this.schema as z.ZodSchema;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public getEssentialFields(): string[] {
|
|
142
|
+
if (this.hasFormValidatableMethod<() => string[]>(this.originalTool, 'getEssentialFields')) {
|
|
143
|
+
return this.originalTool.getEssentialFields();
|
|
144
|
+
}
|
|
145
|
+
return [];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
public isFieldEmpty(fieldName: string, value: unknown): boolean {
|
|
149
|
+
if (this.hasFormValidatableMethod<(n: string, v: unknown) => boolean>(this.originalTool, 'isFieldEmpty')) {
|
|
150
|
+
return this.originalTool.isFieldEmpty(fieldName, value);
|
|
151
|
+
}
|
|
152
|
+
if (value === undefined || value === null || value === '') {
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
if (Array.isArray(value) && value.length === 0) {
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Calculates which fields are missing from the input
|
|
163
|
+
*/
|
|
164
|
+
private calculateMissingFields(
|
|
165
|
+
input: Record<string, unknown>,
|
|
166
|
+
isCustom: boolean
|
|
167
|
+
): Set<string> {
|
|
168
|
+
const missingFields = new Set<string>();
|
|
169
|
+
|
|
170
|
+
if (!isCustom) {
|
|
171
|
+
return missingFields;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const essentialFields = this.getEssentialFields();
|
|
175
|
+
for (const fieldName of essentialFields) {
|
|
176
|
+
const value = input[fieldName];
|
|
177
|
+
if (this.isFieldEmpty(fieldName, value)) {
|
|
178
|
+
missingFields.add(fieldName);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return missingFields;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Creates a form message with optional JSON schema
|
|
187
|
+
*/
|
|
188
|
+
private async createFormMessage(
|
|
189
|
+
schema: z.ZodSchema,
|
|
190
|
+
input: Record<string, unknown>,
|
|
191
|
+
missingFields: Set<string>
|
|
192
|
+
) {
|
|
193
|
+
let formMessage = await this.formGenerator.generateFormFromSchema(
|
|
194
|
+
schema,
|
|
195
|
+
input,
|
|
196
|
+
{
|
|
197
|
+
toolName: this.name,
|
|
198
|
+
toolDescription: this.description
|
|
199
|
+
},
|
|
200
|
+
missingFields
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
if (this.isZodObject(schema)) {
|
|
204
|
+
try {
|
|
205
|
+
const { jsonSchema, uiSchema } = this.formGenerator.generateJsonSchemaForm(
|
|
206
|
+
schema,
|
|
207
|
+
input,
|
|
208
|
+
missingFields
|
|
209
|
+
);
|
|
210
|
+
formMessage = {
|
|
211
|
+
...formMessage,
|
|
212
|
+
jsonSchema,
|
|
213
|
+
uiSchema
|
|
214
|
+
};
|
|
215
|
+
} catch (error) {
|
|
216
|
+
this.logger.warn('Failed to generate JSON Schema for RJSF:', error);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
formMessage.partialInput = input;
|
|
221
|
+
return formMessage;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Type guard to check if a schema is a ZodObject
|
|
226
|
+
*/
|
|
227
|
+
private isZodObject(schema: z.ZodSchema): schema is z.ZodObject<z.ZodRawShape> {
|
|
228
|
+
const def = (schema as z.ZodType)._def as { typeName?: string };
|
|
229
|
+
return !!(def && def.typeName === 'ZodObject');
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Check if we should generate a form for this tool invocation
|
|
234
|
+
*/
|
|
235
|
+
private shouldGenerateForm(input: Record<string, unknown>): boolean {
|
|
236
|
+
this.logger.info(`shouldGenerateForm called for ${this.name}/${this.originalTool.name}`, {
|
|
237
|
+
input,
|
|
238
|
+
hasCustomValidation: !!this.validationConfig.customValidation
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
if (this.validationConfig.customValidation) {
|
|
242
|
+
const result = !this.validationConfig.customValidation(input);
|
|
243
|
+
this.logger.info(`Custom validation result: ${result}`);
|
|
244
|
+
return result;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (isFormValidatable(this.originalTool)) {
|
|
248
|
+
this.logger.info(`Tool ${this.originalTool.name} implements FormValidatable, using custom logic`);
|
|
249
|
+
return this.originalTool.shouldGenerateForm(input);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
this.logger.info(`Tool ${this.originalTool.name} using schema validation only`);
|
|
253
|
+
const validation = this.validateInput(input);
|
|
254
|
+
this.logger.info(`Schema validation for ${this.originalTool.name}:`, {
|
|
255
|
+
isValid: validation.isValid,
|
|
256
|
+
errors: validation.errors
|
|
257
|
+
});
|
|
258
|
+
return !validation.isValid;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Checks if input has bypass flags that skip form generation
|
|
263
|
+
*/
|
|
264
|
+
private hasFormBypassFlags(input: Record<string, unknown>): boolean {
|
|
265
|
+
return (
|
|
266
|
+
(input.__fromForm === true) ||
|
|
267
|
+
(input.renderForm === false)
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Override _call to intercept tool execution
|
|
273
|
+
*/
|
|
274
|
+
protected async _call(
|
|
275
|
+
input: z.infer<TSchema>,
|
|
276
|
+
runManager?: CallbackManagerForToolRun
|
|
277
|
+
): Promise<string> {
|
|
278
|
+
this.logger.info(`🚨🚨🚨 FormValidatingToolWrapper._call INTERCEPTING ${this.name} 🚨🚨🚨`, {
|
|
279
|
+
input,
|
|
280
|
+
inputKeys: Object.keys(input as Record<string, unknown>),
|
|
281
|
+
schemaShape: this.getSchemaShape(),
|
|
282
|
+
stackTrace: new Error().stack?.split('\n').slice(0, 5)
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
const inputRecord = input as unknown as Record<string, unknown>;
|
|
286
|
+
|
|
287
|
+
if (this.hasFormBypassFlags(inputRecord)) {
|
|
288
|
+
this.logger.info('Bypassing form generation and executing original tool due to submission flags');
|
|
289
|
+
return this.executeOriginal(inputRecord, runManager);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const shouldGenerate = this.shouldGenerateForm(input as Record<string, unknown>);
|
|
293
|
+
this.logger.info(`FormValidatingToolWrapper decision for ${this.name}:`, {
|
|
294
|
+
shouldGenerateForm: shouldGenerate,
|
|
295
|
+
toolName: this.name,
|
|
296
|
+
originalToolName: this.originalTool.name
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
if (shouldGenerate) {
|
|
300
|
+
this.logger.info(`Generating form for incomplete input in ${this.name}`);
|
|
301
|
+
|
|
302
|
+
try {
|
|
303
|
+
const isCustom = isFormValidatable(this.originalTool);
|
|
304
|
+
const schemaToUse = isCustom ? this.getFormSchema() : this.schema;
|
|
305
|
+
const missingFields = this.calculateMissingFields(
|
|
306
|
+
input as Record<string, unknown>,
|
|
307
|
+
isCustom
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
const schemaFields = this.isZodObject(schemaToUse)
|
|
311
|
+
? Object.keys(schemaToUse.shape)
|
|
312
|
+
: [];
|
|
313
|
+
|
|
314
|
+
this.logger.info(`Using ${isCustom ? 'CUSTOM' : 'DEFAULT'} schema for form generation`, {
|
|
315
|
+
toolName: this.originalTool.name,
|
|
316
|
+
schemaType: schemaToUse.constructor?.name,
|
|
317
|
+
schemaFields,
|
|
318
|
+
isCustomSchema: isCustom
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
const formMessage = await this.createFormMessage(
|
|
322
|
+
schemaToUse,
|
|
323
|
+
input as Record<string, unknown>,
|
|
324
|
+
missingFields
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
const result = {
|
|
328
|
+
requiresForm: true,
|
|
329
|
+
formMessage,
|
|
330
|
+
message: `Please complete the form to provide the required information for ${this.name}.`
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
this.logger.info(`FormValidatingToolWrapper returning form result for ${this.name}`);
|
|
334
|
+
return JSON.stringify(result);
|
|
335
|
+
} catch (error) {
|
|
336
|
+
this.logger.error('Failed to generate form:', error);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
this.logger.info(`FormValidatingToolWrapper passing through to original tool ${this.name}`);
|
|
341
|
+
return this.executeOriginal(input as Record<string, unknown>, runManager);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Wrap a tool with form validation capabilities
|
|
348
|
+
*/
|
|
349
|
+
export function wrapToolWithFormValidation<TSchema extends z.ZodObject<z.ZodRawShape, z.UnknownKeysParam, z.ZodTypeAny>>(
|
|
350
|
+
tool: StructuredTool<TSchema>,
|
|
351
|
+
formGenerator: FormGenerator,
|
|
352
|
+
config: FormValidationConfig = {}
|
|
353
|
+
): FormValidatingToolWrapper<TSchema> {
|
|
354
|
+
return new FormValidatingToolWrapper(tool, formGenerator, config);
|
|
355
|
+
}
|