@hashgraphonline/conversational-agent 0.1.217 → 0.1.218
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/readme.md +181 -0
- package/dist/cjs/constants/entity-references.d.ts +18 -0
- package/dist/cjs/constants/form-priorities.d.ts +24 -0
- package/dist/cjs/constants/index.d.ts +4 -0
- package/dist/cjs/constants/messages.d.ts +19 -0
- package/dist/cjs/constants/test-constants.d.ts +42 -0
- package/dist/cjs/conversational-agent.d.ts +3 -8
- package/dist/cjs/core/{ToolRegistry.d.ts → tool-registry.d.ts} +10 -7
- package/dist/{types/execution/ExecutionPipeline.d.ts → cjs/execution/execution-pipeline.d.ts} +3 -3
- package/dist/cjs/forms/field-guidance-registry.d.ts +108 -0
- package/dist/cjs/forms/form-generator.d.ts +2 -7
- package/dist/cjs/forms/index.d.ts +3 -0
- package/dist/cjs/forms/types.d.ts +9 -1
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +7 -12
- package/dist/cjs/langchain/external-tool-wrapper.d.ts +101 -0
- package/dist/{types/langchain/FormAwareAgentExecutor.d.ts → cjs/langchain/form-aware-agent-executor.d.ts} +19 -12
- package/dist/cjs/langchain/index.d.ts +2 -0
- package/dist/{types → cjs/langchain}/langchain-agent.d.ts +15 -7
- package/dist/cjs/mcp/adapters/index.d.ts +1 -0
- package/dist/cjs/mcp/adapters/langchain.d.ts +1 -1
- package/dist/{types/mcp/ContentProcessor.d.ts → cjs/mcp/content-processor.d.ts} +1 -1
- package/dist/cjs/mcp/index.d.ts +5 -0
- package/dist/{types/mcp/MCPClientManager.d.ts → cjs/mcp/mcp-client-manager.d.ts} +1 -1
- package/dist/cjs/memory/{ContentStorage.d.ts → content-storage.d.ts} +4 -4
- package/dist/cjs/memory/index.d.ts +5 -7
- package/dist/{types/memory/MemoryWindow.d.ts → cjs/memory/memory-window.d.ts} +1 -1
- package/dist/{types/memory/SmartMemoryManager.d.ts → cjs/memory/smart-memory-manager.d.ts} +1 -1
- package/dist/cjs/services/{ContentStoreManager.d.ts → content-store-manager.d.ts} +6 -6
- package/dist/cjs/services/context/resolution-context.d.ts +49 -0
- package/dist/cjs/services/entity-resolver.d.ts +58 -0
- package/dist/cjs/services/formatters/converters/index.d.ts +2 -0
- package/dist/cjs/services/formatters/converters/string-normalization-converter.d.ts +13 -0
- package/dist/cjs/services/formatters/converters/topic-id-to-hrl-converter.d.ts +17 -0
- package/dist/cjs/services/formatters/format-converter-registry.d.ts +66 -0
- package/dist/cjs/services/formatters/index.d.ts +3 -0
- package/dist/cjs/services/formatters/types.d.ts +29 -0
- package/dist/cjs/services/index.d.ts +3 -0
- package/dist/cjs/services/resolution/resolution-pipeline.d.ts +44 -0
- package/dist/cjs/tools/index.d.ts +1 -0
- package/dist/cjs/utils/index.d.ts +1 -0
- package/dist/esm/index.js +40 -16
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index10.js +669 -13
- package/dist/esm/index10.js.map +1 -1
- package/dist/esm/index11.js +310 -95
- package/dist/esm/index11.js.map +1 -1
- package/dist/esm/index12.js +130 -95
- package/dist/esm/index12.js.map +1 -1
- package/dist/esm/index13.js +262 -153
- package/dist/esm/index13.js.map +1 -1
- package/dist/esm/index14.js +100 -664
- package/dist/esm/index14.js.map +1 -1
- package/dist/esm/index15.js +135 -408
- package/dist/esm/index15.js.map +1 -1
- package/dist/esm/index16.js +240 -122
- package/dist/esm/index16.js.map +1 -1
- package/dist/esm/index17.js +147 -135
- package/dist/esm/index17.js.map +1 -1
- package/dist/esm/index18.js +376 -533
- package/dist/esm/index18.js.map +1 -1
- package/dist/esm/index19.js +87 -214
- package/dist/esm/index19.js.map +1 -1
- package/dist/esm/index2.js +21 -4
- package/dist/esm/index2.js.map +1 -1
- package/dist/esm/index20.js +158 -92
- package/dist/esm/index20.js.map +1 -1
- package/dist/esm/index21.js +717 -44
- package/dist/esm/index21.js.map +1 -1
- package/dist/esm/index22.js +58 -96
- package/dist/esm/index22.js.map +1 -1
- package/dist/esm/index23.js +324 -34
- package/dist/esm/index23.js.map +1 -1
- package/dist/esm/index24.js +125 -712
- package/dist/esm/index24.js.map +1 -1
- package/dist/esm/index25.js +113 -133
- package/dist/esm/index25.js.map +1 -1
- package/dist/esm/index26.js +18 -152
- package/dist/esm/index26.js.map +1 -1
- package/dist/esm/index27.js +14 -210
- package/dist/esm/index27.js.map +1 -1
- package/dist/esm/index28.js +70 -173
- package/dist/esm/index28.js.map +1 -1
- package/dist/esm/index29.js +811 -224
- package/dist/esm/index29.js.map +1 -1
- package/dist/esm/index30.js +1245 -122
- package/dist/esm/index30.js.map +1 -1
- package/dist/esm/index31.js +126 -56
- package/dist/esm/index31.js.map +1 -1
- package/dist/esm/index32.js +111 -24
- package/dist/esm/index32.js.map +1 -1
- package/dist/esm/index33.js +41 -87
- package/dist/esm/index33.js.map +1 -1
- package/dist/esm/index34.js +88 -227
- package/dist/esm/index34.js.map +1 -1
- package/dist/esm/index35.js +24 -0
- package/dist/esm/index35.js.map +1 -0
- package/dist/esm/index36.js +15 -0
- package/dist/esm/index36.js.map +1 -0
- package/dist/esm/index37.js +10 -0
- package/dist/esm/index37.js.map +1 -0
- package/dist/esm/index38.js +8 -0
- package/dist/esm/index38.js.map +1 -0
- package/dist/esm/index39.js +227 -0
- package/dist/esm/index39.js.map +1 -0
- package/dist/esm/index40.js +187 -0
- package/dist/esm/index40.js.map +1 -0
- package/dist/esm/index41.js +30 -0
- package/dist/esm/index41.js.map +1 -0
- package/dist/esm/index42.js +95 -0
- package/dist/esm/index42.js.map +1 -0
- package/dist/esm/index5.js +2 -2
- package/dist/esm/index5.js.map +1 -1
- package/dist/esm/index6.js +44 -67
- package/dist/esm/index6.js.map +1 -1
- package/dist/esm/index7.js +9 -0
- package/dist/esm/index7.js.map +1 -1
- package/dist/esm/index8.js +13 -1095
- package/dist/esm/index8.js.map +1 -1
- package/dist/esm/index9.js +17 -13
- package/dist/esm/index9.js.map +1 -1
- package/dist/types/constants/entity-references.d.ts +18 -0
- package/dist/types/constants/form-priorities.d.ts +24 -0
- package/dist/types/constants/index.d.ts +4 -0
- package/dist/types/constants/messages.d.ts +19 -0
- package/dist/types/constants/test-constants.d.ts +42 -0
- package/dist/types/conversational-agent.d.ts +3 -8
- package/dist/types/core/{ToolRegistry.d.ts → tool-registry.d.ts} +10 -7
- package/dist/{cjs/execution/ExecutionPipeline.d.ts → types/execution/execution-pipeline.d.ts} +3 -3
- package/dist/types/forms/field-guidance-registry.d.ts +108 -0
- package/dist/types/forms/form-generator.d.ts +2 -7
- package/dist/types/forms/index.d.ts +3 -0
- package/dist/types/forms/types.d.ts +9 -1
- package/dist/types/index.d.ts +7 -12
- package/dist/types/langchain/external-tool-wrapper.d.ts +101 -0
- package/dist/{cjs/langchain/FormAwareAgentExecutor.d.ts → types/langchain/form-aware-agent-executor.d.ts} +19 -12
- package/dist/types/langchain/index.d.ts +2 -0
- package/dist/{cjs → types/langchain}/langchain-agent.d.ts +15 -7
- package/dist/types/mcp/adapters/index.d.ts +1 -0
- package/dist/types/mcp/adapters/langchain.d.ts +1 -1
- package/dist/{cjs/mcp/ContentProcessor.d.ts → types/mcp/content-processor.d.ts} +1 -1
- package/dist/types/mcp/index.d.ts +5 -0
- package/dist/{cjs/mcp/MCPClientManager.d.ts → types/mcp/mcp-client-manager.d.ts} +1 -1
- package/dist/types/memory/{ContentStorage.d.ts → content-storage.d.ts} +4 -4
- package/dist/types/memory/index.d.ts +5 -7
- package/dist/{cjs/memory/MemoryWindow.d.ts → types/memory/memory-window.d.ts} +1 -1
- package/dist/{cjs/memory/SmartMemoryManager.d.ts → types/memory/smart-memory-manager.d.ts} +1 -1
- package/dist/types/services/{ContentStoreManager.d.ts → content-store-manager.d.ts} +6 -6
- package/dist/types/services/context/resolution-context.d.ts +49 -0
- package/dist/types/services/entity-resolver.d.ts +58 -0
- package/dist/types/services/formatters/converters/index.d.ts +2 -0
- package/dist/types/services/formatters/converters/string-normalization-converter.d.ts +13 -0
- package/dist/types/services/formatters/converters/topic-id-to-hrl-converter.d.ts +17 -0
- package/dist/types/services/formatters/format-converter-registry.d.ts +66 -0
- package/dist/types/services/formatters/index.d.ts +3 -0
- package/dist/types/services/formatters/types.d.ts +29 -0
- package/dist/types/services/index.d.ts +3 -0
- package/dist/types/services/resolution/resolution-pipeline.d.ts +44 -0
- package/dist/types/tools/index.d.ts +1 -0
- package/dist/types/utils/index.d.ts +1 -0
- package/package.json +30 -27
- package/src/agent-factory.ts +1 -1
- package/src/base-agent.ts +9 -0
- package/src/config/system-message.ts +2 -15
- package/src/constants/entity-references.ts +23 -0
- package/src/constants/form-priorities.ts +25 -0
- package/src/constants/index.ts +4 -0
- package/src/constants/messages.ts +20 -0
- package/src/constants/test-constants.ts +49 -0
- package/src/conversational-agent.ts +42 -69
- package/src/core/{ToolRegistry.ts → tool-registry.ts} +70 -44
- package/src/examples/external-tool-wrapper-example.ts +56 -0
- package/src/execution/{ExecutionPipeline.ts → execution-pipeline.ts} +3 -3
- package/src/forms/field-guidance-registry.ts +415 -0
- package/src/forms/field-type-registry.ts +49 -48
- package/src/forms/{FormEngine.ts → form-engine.ts} +66 -43
- package/src/forms/form-generator.ts +91 -17
- package/src/forms/index.ts +4 -1
- package/src/forms/types.ts +9 -1
- package/src/index.ts +7 -37
- package/src/langchain/external-tool-wrapper.ts +90 -0
- package/src/langchain/{FormAwareAgentExecutor.ts → form-aware-agent-executor.ts} +579 -351
- package/src/langchain/index.ts +2 -0
- package/src/{langchain-agent.ts → langchain/langchain-agent.ts} +389 -113
- package/src/mcp/adapters/index.ts +1 -0
- package/src/mcp/adapters/langchain.ts +27 -18
- package/src/mcp/{ContentProcessor.ts → content-processor.ts} +71 -47
- package/src/mcp/index.ts +5 -0
- package/src/mcp/{MCPClientManager.ts → mcp-client-manager.ts} +2 -2
- package/src/memory/{ContentStorage.ts → content-storage.ts} +263 -167
- package/src/memory/index.ts +5 -8
- package/src/memory/{MemoryWindow.ts → memory-window.ts} +47 -24
- package/src/memory/{SmartMemoryManager.ts → smart-memory-manager.ts} +49 -22
- package/src/plugins/hbar/HbarPlugin.ts +1 -1
- package/src/plugins/hcs-10/HCS10Plugin.ts +46 -28
- package/src/scripts/test-external-tool-wrapper.ts +6 -6
- package/src/scripts/test-inscribe-form-generation.ts +22 -21
- package/src/scripts/test-inscribe-wrapper-verification.ts +5 -4
- package/src/services/{ContentStoreManager.ts → content-store-manager.ts} +75 -33
- package/src/services/context/resolution-context.ts +80 -0
- package/src/services/entity-resolver.ts +425 -0
- package/src/services/formatters/converters/index.ts +2 -0
- package/src/services/formatters/converters/string-normalization-converter.ts +106 -0
- package/src/services/formatters/converters/topic-id-to-hrl-converter.ts +25 -0
- package/src/services/formatters/format-converter-registry.ts +229 -0
- package/src/services/formatters/index.ts +3 -0
- package/src/services/formatters/types.ts +31 -0
- package/src/services/index.ts +3 -0
- package/src/services/resolution/resolution-pipeline.ts +106 -0
- package/src/tools/index.ts +1 -0
- package/src/types/content-reference.ts +87 -60
- package/src/utils/index.ts +1 -0
- package/cli/dist/CLIApp.d.ts +0 -9
- package/cli/dist/CLIApp.js +0 -127
- package/cli/dist/LocalConversationalAgent.d.ts +0 -37
- package/cli/dist/LocalConversationalAgent.js +0 -58
- package/cli/dist/app.d.ts +0 -16
- package/cli/dist/app.js +0 -13
- package/cli/dist/cli.d.ts +0 -2
- package/cli/dist/cli.js +0 -51
- package/cli/dist/components/AppContainer.d.ts +0 -16
- package/cli/dist/components/AppContainer.js +0 -24
- package/cli/dist/components/AppScreens.d.ts +0 -2
- package/cli/dist/components/AppScreens.js +0 -259
- package/cli/dist/components/ChatScreen.d.ts +0 -15
- package/cli/dist/components/ChatScreen.js +0 -39
- package/cli/dist/components/DebugLoadingScreen.d.ts +0 -5
- package/cli/dist/components/DebugLoadingScreen.js +0 -31
- package/cli/dist/components/LoadingScreen.d.ts +0 -2
- package/cli/dist/components/LoadingScreen.js +0 -16
- package/cli/dist/components/LoadingScreenDebug.d.ts +0 -5
- package/cli/dist/components/LoadingScreenDebug.js +0 -27
- package/cli/dist/components/MCPConfigScreen.d.ts +0 -28
- package/cli/dist/components/MCPConfigScreen.js +0 -168
- package/cli/dist/components/ScreenRouter.d.ts +0 -12
- package/cli/dist/components/ScreenRouter.js +0 -22
- package/cli/dist/components/SetupScreen.d.ts +0 -15
- package/cli/dist/components/SetupScreen.js +0 -65
- package/cli/dist/components/SingleLoadingScreen.d.ts +0 -5
- package/cli/dist/components/SingleLoadingScreen.js +0 -27
- package/cli/dist/components/StatusBadge.d.ts +0 -7
- package/cli/dist/components/StatusBadge.js +0 -28
- package/cli/dist/components/TerminalWindow.d.ts +0 -8
- package/cli/dist/components/TerminalWindow.js +0 -24
- package/cli/dist/components/WelcomeScreen.d.ts +0 -11
- package/cli/dist/components/WelcomeScreen.js +0 -47
- package/cli/dist/context/AppContext.d.ts +0 -68
- package/cli/dist/context/AppContext.js +0 -363
- package/cli/dist/hooks/useInitializeAgent.d.ts +0 -19
- package/cli/dist/hooks/useInitializeAgent.js +0 -28
- package/cli/dist/hooks/useStableState.d.ts +0 -38
- package/cli/dist/hooks/useStableState.js +0 -68
- package/cli/dist/managers/AgentManager.d.ts +0 -57
- package/cli/dist/managers/AgentManager.js +0 -119
- package/cli/dist/managers/ConfigManager.d.ts +0 -53
- package/cli/dist/managers/ConfigManager.js +0 -173
- package/cli/dist/types.d.ts +0 -31
- package/cli/dist/types.js +0 -19
- package/dist/cjs/context/ReferenceContextManager.d.ts +0 -84
- package/dist/cjs/context/ReferenceResponseProcessor.d.ts +0 -76
- package/dist/cjs/langchain/FormValidatingToolWrapper.d.ts +0 -81
- package/dist/cjs/services/EntityResolver.d.ts +0 -26
- package/dist/types/context/ReferenceContextManager.d.ts +0 -84
- package/dist/types/context/ReferenceResponseProcessor.d.ts +0 -76
- package/dist/types/langchain/FormValidatingToolWrapper.d.ts +0 -81
- package/dist/types/services/EntityResolver.d.ts +0 -26
- package/src/context/ReferenceContextManager.ts +0 -350
- package/src/context/ReferenceResponseProcessor.ts +0 -295
- package/src/langchain/FormValidatingToolWrapper.ts +0 -355
- package/src/scripts/test-hedera-kit-wrapper.ts +0 -265
- package/src/services/EntityResolver.ts +0 -128
- /package/dist/cjs/forms/{FormEngine.d.ts → form-engine.d.ts} +0 -0
- /package/dist/cjs/memory/{ReferenceIdGenerator.d.ts → reference-id-generator.d.ts} +0 -0
- /package/dist/cjs/memory/{TokenCounter.d.ts → token-counter.d.ts} +0 -0
- /package/dist/cjs/tools/{EntityResolverTool.d.ts → entity-resolver-tool.d.ts} +0 -0
- /package/dist/cjs/utils/{ResponseFormatter.d.ts → response-formatter.d.ts} +0 -0
- /package/dist/types/forms/{FormEngine.d.ts → form-engine.d.ts} +0 -0
- /package/dist/types/memory/{ReferenceIdGenerator.d.ts → reference-id-generator.d.ts} +0 -0
- /package/dist/types/memory/{TokenCounter.d.ts → token-counter.d.ts} +0 -0
- /package/dist/types/tools/{EntityResolverTool.d.ts → entity-resolver-tool.d.ts} +0 -0
- /package/dist/types/utils/{ResponseFormatter.d.ts → response-formatter.d.ts} +0 -0
- /package/src/memory/{ReferenceIdGenerator.ts → reference-id-generator.ts} +0 -0
- /package/src/memory/{TokenCounter.ts → token-counter.ts} +0 -0
- /package/src/tools/{EntityResolverTool.ts → entity-resolver-tool.ts} +0 -0
- /package/src/utils/{ResponseFormatter.ts → response-formatter.ts} +0 -0
|
@@ -1,295 +0,0 @@
|
|
|
1
|
-
import type { ContentReference, ReferenceId, ContentMetadata } from '../types/content-reference';
|
|
2
|
-
import type { ReferenceContextManager, ReferenceDisplayOptions } from './ReferenceContextManager';
|
|
3
|
-
import { Logger } from '@hashgraphonline/standards-sdk';
|
|
4
|
-
|
|
5
|
-
export interface ResponseProcessingOptions {
|
|
6
|
-
autoDisplayReferences?: boolean;
|
|
7
|
-
displayOptions?: ReferenceDisplayOptions;
|
|
8
|
-
includeReferenceInstructions?: boolean;
|
|
9
|
-
contextualizeReferences?: boolean;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface ProcessedResponse {
|
|
13
|
-
content: string;
|
|
14
|
-
hasReferences: boolean;
|
|
15
|
-
referenceCount: number;
|
|
16
|
-
contextIds: string[];
|
|
17
|
-
suggestedActions: string[];
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface ReferenceDetectionResult {
|
|
21
|
-
hasReferences: boolean;
|
|
22
|
-
references: Array<{
|
|
23
|
-
reference: ContentReference;
|
|
24
|
-
position: number;
|
|
25
|
-
originalText: string;
|
|
26
|
-
}>;
|
|
27
|
-
plainReferences: Array<{
|
|
28
|
-
referenceId: ReferenceId;
|
|
29
|
-
position: number;
|
|
30
|
-
originalText: string;
|
|
31
|
-
}>;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Processes agent responses to detect and display content references
|
|
36
|
-
* Handles both structured references and plain reference IDs
|
|
37
|
-
*/
|
|
38
|
-
export class ReferenceResponseProcessor {
|
|
39
|
-
private contextManager: ReferenceContextManager;
|
|
40
|
-
private logger: Logger;
|
|
41
|
-
|
|
42
|
-
constructor(contextManager: ReferenceContextManager, logger: Logger) {
|
|
43
|
-
this.contextManager = contextManager;
|
|
44
|
-
this.logger = logger;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Process an agent response to handle content references
|
|
49
|
-
*/
|
|
50
|
-
async processResponse(
|
|
51
|
-
responseContent: string,
|
|
52
|
-
options: ResponseProcessingOptions = {}
|
|
53
|
-
): Promise<ProcessedResponse> {
|
|
54
|
-
const {
|
|
55
|
-
autoDisplayReferences = true,
|
|
56
|
-
displayOptions = {},
|
|
57
|
-
includeReferenceInstructions = true,
|
|
58
|
-
contextualizeReferences = true
|
|
59
|
-
} = options;
|
|
60
|
-
|
|
61
|
-
try {
|
|
62
|
-
const detection = this.detectReferences(responseContent);
|
|
63
|
-
|
|
64
|
-
if (!detection.hasReferences) {
|
|
65
|
-
return {
|
|
66
|
-
content: responseContent,
|
|
67
|
-
hasReferences: false,
|
|
68
|
-
referenceCount: 0,
|
|
69
|
-
contextIds: [],
|
|
70
|
-
suggestedActions: []
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
let processedContent = responseContent;
|
|
75
|
-
const contextIds: string[] = [];
|
|
76
|
-
const suggestedActions: string[] = [];
|
|
77
|
-
|
|
78
|
-
if (autoDisplayReferences) {
|
|
79
|
-
for (const { reference, originalText } of detection.references) {
|
|
80
|
-
const displayResult = await this.contextManager.displayReference(reference, displayOptions);
|
|
81
|
-
|
|
82
|
-
processedContent = processedContent.replace(originalText, displayResult.displayText);
|
|
83
|
-
|
|
84
|
-
if (displayResult.contextId) {
|
|
85
|
-
contextIds.push(displayResult.contextId);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (displayResult.suggestedActions) {
|
|
89
|
-
suggestedActions.push(...displayResult.suggestedActions);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
for (const { referenceId, originalText } of detection.plainReferences) {
|
|
94
|
-
try {
|
|
95
|
-
const resolution = await this.contextManager['contentStorage'].resolveReference(referenceId);
|
|
96
|
-
if (resolution.success && resolution.metadata) {
|
|
97
|
-
const referenceMetadata: Pick<ContentMetadata, 'contentType' | 'sizeBytes' | 'source' | 'fileName' | 'mimeType'> = {
|
|
98
|
-
contentType: resolution.metadata.contentType,
|
|
99
|
-
sizeBytes: resolution.metadata.sizeBytes,
|
|
100
|
-
source: resolution.metadata.source
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
if (resolution.metadata.fileName !== undefined) {
|
|
104
|
-
referenceMetadata.fileName = resolution.metadata.fileName;
|
|
105
|
-
}
|
|
106
|
-
if (resolution.metadata.mimeType !== undefined) {
|
|
107
|
-
referenceMetadata.mimeType = resolution.metadata.mimeType;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const reference: ContentReference = {
|
|
111
|
-
referenceId,
|
|
112
|
-
state: 'active',
|
|
113
|
-
preview: this.createPreviewFromContent(resolution.content!, resolution.metadata.contentType),
|
|
114
|
-
metadata: referenceMetadata,
|
|
115
|
-
createdAt: resolution.metadata.createdAt,
|
|
116
|
-
format: 'ref://{id}' as const
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
const displayResult = await this.contextManager.displayReference(reference, displayOptions);
|
|
120
|
-
processedContent = processedContent.replace(originalText, displayResult.displayText);
|
|
121
|
-
|
|
122
|
-
if (displayResult.contextId) {
|
|
123
|
-
contextIds.push(displayResult.contextId);
|
|
124
|
-
}
|
|
125
|
-
} else {
|
|
126
|
-
processedContent = processedContent.replace(
|
|
127
|
-
originalText,
|
|
128
|
-
`❌ Reference unavailable: ${referenceId.substring(0, 12)}...`
|
|
129
|
-
);
|
|
130
|
-
suggestedActions.push('Request fresh content');
|
|
131
|
-
}
|
|
132
|
-
} catch (error) {
|
|
133
|
-
this.logger.warn(`Failed to resolve plain reference ${referenceId}:`, error);
|
|
134
|
-
processedContent = processedContent.replace(
|
|
135
|
-
originalText,
|
|
136
|
-
`⚠️ Reference error: ${referenceId.substring(0, 12)}...`
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
if (includeReferenceInstructions && (detection.references.length > 0 || detection.plainReferences.length > 0)) {
|
|
143
|
-
if (contextualizeReferences && contextIds.length === 1) {
|
|
144
|
-
processedContent += `\n\n💡 To inscribe this content, say "inscribe it" or "inscribe the content".`;
|
|
145
|
-
} else if (contextualizeReferences && contextIds.length > 1) {
|
|
146
|
-
processedContent += `\n\n💡 To inscribe any of this content, say "inscribe it" (uses most recent) or specify the reference.`;
|
|
147
|
-
} else {
|
|
148
|
-
processedContent += `\n\n💡 Referenced content can be inscribed using the "inscribe it" command.`;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return {
|
|
153
|
-
content: processedContent,
|
|
154
|
-
hasReferences: true,
|
|
155
|
-
referenceCount: detection.references.length + detection.plainReferences.length,
|
|
156
|
-
contextIds,
|
|
157
|
-
suggestedActions: [...new Set(suggestedActions)]
|
|
158
|
-
};
|
|
159
|
-
} catch (error) {
|
|
160
|
-
this.logger.error('Error processing response references:', error);
|
|
161
|
-
return {
|
|
162
|
-
content: responseContent,
|
|
163
|
-
hasReferences: false,
|
|
164
|
-
referenceCount: 0,
|
|
165
|
-
contextIds: [],
|
|
166
|
-
suggestedActions: ['Check reference system', 'Try again']
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Detect references in response content
|
|
173
|
-
*/
|
|
174
|
-
private detectReferences(content: string): ReferenceDetectionResult {
|
|
175
|
-
const references: Array<{
|
|
176
|
-
reference: ContentReference;
|
|
177
|
-
position: number;
|
|
178
|
-
originalText: string;
|
|
179
|
-
}> = [];
|
|
180
|
-
|
|
181
|
-
const plainReferences: Array<{
|
|
182
|
-
referenceId: ReferenceId;
|
|
183
|
-
position: number;
|
|
184
|
-
originalText: string;
|
|
185
|
-
}> = [];
|
|
186
|
-
|
|
187
|
-
const contentReferenceRegex = /"type":\s*"content_reference"[^}]*"referenceId":\s*"([^"]+)"[^}]*}/g;
|
|
188
|
-
let match: RegExpExecArray | null;
|
|
189
|
-
|
|
190
|
-
while ((match = contentReferenceRegex.exec(content)) !== null) {
|
|
191
|
-
try {
|
|
192
|
-
const refObject = JSON.parse(match[0]);
|
|
193
|
-
if (refObject.type === 'content_reference' && refObject.referenceId) {
|
|
194
|
-
const reference: ContentReference = {
|
|
195
|
-
referenceId: refObject.referenceId,
|
|
196
|
-
state: refObject.state || 'active',
|
|
197
|
-
preview: refObject.preview || '',
|
|
198
|
-
metadata: refObject.metadata || {},
|
|
199
|
-
createdAt: new Date(refObject.createdAt || Date.now()),
|
|
200
|
-
format: 'ref://{id}' as const
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
references.push({
|
|
204
|
-
reference,
|
|
205
|
-
position: match.index,
|
|
206
|
-
originalText: match[0]
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
} catch (error) {
|
|
210
|
-
this.logger.warn('Failed to parse content reference:', error);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
const plainRefRegex = /ref:\/\/([A-Za-z0-9_-]{43})|(?:^|\s)([A-Za-z0-9_-]{43})(?=\s|$)/g;
|
|
215
|
-
let plainMatch: RegExpExecArray | null;
|
|
216
|
-
|
|
217
|
-
while ((plainMatch = plainRefRegex.exec(content)) !== null) {
|
|
218
|
-
const referenceId = plainMatch[1] || plainMatch[2];
|
|
219
|
-
if (referenceId && this.isValidReferenceId(referenceId)) {
|
|
220
|
-
plainReferences.push({
|
|
221
|
-
referenceId,
|
|
222
|
-
position: plainMatch.index,
|
|
223
|
-
originalText: plainMatch[0]
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
return {
|
|
229
|
-
hasReferences: references.length > 0 || plainReferences.length > 0,
|
|
230
|
-
references,
|
|
231
|
-
plainReferences
|
|
232
|
-
};
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Create a preview from content buffer
|
|
237
|
-
*/
|
|
238
|
-
private createPreviewFromContent(content: Buffer, contentType: string): string {
|
|
239
|
-
const maxLength = 200;
|
|
240
|
-
let preview = content.toString('utf8', 0, Math.min(content.length, maxLength * 2));
|
|
241
|
-
|
|
242
|
-
if (contentType === 'html') {
|
|
243
|
-
preview = preview.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ');
|
|
244
|
-
} else if (contentType === 'json') {
|
|
245
|
-
try {
|
|
246
|
-
const parsed = JSON.parse(preview);
|
|
247
|
-
preview = JSON.stringify(parsed, null, 0);
|
|
248
|
-
} catch {
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
preview = preview.trim();
|
|
253
|
-
if (preview.length > maxLength) {
|
|
254
|
-
preview = preview.substring(0, maxLength) + '...';
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
return preview || '[Binary content]';
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Validate reference ID format
|
|
262
|
-
*/
|
|
263
|
-
private isValidReferenceId(id: string): boolean {
|
|
264
|
-
if (!id || typeof id !== 'string') {
|
|
265
|
-
return false;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
if (id.length !== 43) {
|
|
269
|
-
return false;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
return /^[A-Za-z0-9_-]+$/.test(id);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Get reference context statistics
|
|
277
|
-
*/
|
|
278
|
-
getContextStats() {
|
|
279
|
-
return this.contextManager.getContextStats();
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Validate all references in context
|
|
284
|
-
*/
|
|
285
|
-
async validateAllReferences() {
|
|
286
|
-
return await this.contextManager.validateReferences();
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* Clean up old references
|
|
291
|
-
*/
|
|
292
|
-
cleanupOldReferences(maxAgeMs?: number) {
|
|
293
|
-
return this.contextManager.cleanupOldReferences(maxAgeMs);
|
|
294
|
-
}
|
|
295
|
-
}
|
|
@@ -1,355 +0,0 @@
|
|
|
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
|
-
}
|