@hashgraphonline/conversational-agent 0.1.206 → 0.1.208
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/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/langchain/ContentAwareAgentExecutor.d.ts +14 -0
- package/dist/esm/index12.js +1 -1
- package/dist/esm/index15.js +7 -172
- package/dist/esm/index15.js.map +1 -1
- package/dist/esm/index16.js +159 -118
- package/dist/esm/index16.js.map +1 -1
- package/dist/esm/index17.js +156 -231
- package/dist/esm/index17.js.map +1 -1
- package/dist/esm/index18.js +225 -84
- package/dist/esm/index18.js.map +1 -1
- package/dist/esm/index19.js +2 -2
- package/dist/esm/index23.js +83 -56
- package/dist/esm/index23.js.map +1 -1
- package/dist/esm/index24.js +62 -32
- package/dist/esm/index24.js.map +1 -1
- package/dist/esm/index25.js +38 -0
- package/dist/esm/index25.js.map +1 -0
- package/dist/esm/index8.js +6 -5
- package/dist/esm/index8.js.map +1 -1
- package/dist/types/langchain/ContentAwareAgentExecutor.d.ts +14 -0
- package/package.json +1 -1
- package/src/langchain/ContentAwareAgentExecutor.ts +20 -0
- package/src/langchain-agent.ts +4 -3
- package/src/mcp/adapters/langchain.ts +32 -0
package/dist/esm/index24.js
CHANGED
|
@@ -1,38 +1,68 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
storageBackend: "memory",
|
|
14
|
-
cleanupPolicies: {
|
|
15
|
-
recent: { maxAgeMs: 30 * 60 * 1e3, priority: 1 },
|
|
16
|
-
// 30 minutes, highest priority
|
|
17
|
-
userContent: { maxAgeMs: 2 * 60 * 60 * 1e3, priority: 2 },
|
|
18
|
-
// 2 hours
|
|
19
|
-
agentGenerated: { maxAgeMs: 60 * 60 * 1e3, priority: 3 },
|
|
20
|
-
// 1 hour
|
|
21
|
-
default: { maxAgeMs: 60 * 60 * 1e3, priority: 4 }
|
|
22
|
-
// 1 hour, lowest priority
|
|
1
|
+
import { createHash } from "crypto";
|
|
2
|
+
class ReferenceIdGenerator {
|
|
3
|
+
/**
|
|
4
|
+
* Generate a content-based reference ID using SHA-256 hashing
|
|
5
|
+
*
|
|
6
|
+
* @param content The content to generate a reference ID for
|
|
7
|
+
* @returns Deterministic reference ID based on content hash
|
|
8
|
+
*/
|
|
9
|
+
static generateId(content) {
|
|
10
|
+
const hash = createHash("sha256");
|
|
11
|
+
hash.update(content);
|
|
12
|
+
return hash.digest("base64url");
|
|
23
13
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Validate that a string is a properly formatted reference ID
|
|
16
|
+
*
|
|
17
|
+
* @param id The ID to validate
|
|
18
|
+
* @returns true if the ID is valid format
|
|
19
|
+
*/
|
|
20
|
+
static isValidReferenceId(id) {
|
|
21
|
+
if (!id || typeof id !== "string") {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
if (id.length !== 43) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
return /^[A-Za-z0-9_-]+$/.test(id);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Extract reference ID from ref:// format
|
|
31
|
+
*
|
|
32
|
+
* @param input Input string that may contain a reference ID
|
|
33
|
+
* @returns Extracted reference ID or null if not found
|
|
34
|
+
*/
|
|
35
|
+
static extractReferenceId(input) {
|
|
36
|
+
if (!input || typeof input !== "string") {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
const refFormatMatch = input.match(/^ref:\/\/([A-Za-z0-9_-]{43})$/);
|
|
40
|
+
if (refFormatMatch) {
|
|
41
|
+
return refFormatMatch[1];
|
|
42
|
+
}
|
|
43
|
+
return this.isValidReferenceId(input) ? input : null;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Format a reference ID in the standard ref:// format
|
|
47
|
+
*
|
|
48
|
+
* @param referenceId The reference ID to format
|
|
49
|
+
* @returns Formatted reference string
|
|
50
|
+
*/
|
|
51
|
+
static formatReference(referenceId) {
|
|
52
|
+
return `ref://${referenceId}`;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Generate a test reference ID (for testing purposes only)
|
|
56
|
+
*
|
|
57
|
+
* @param testSeed A test seed to generate a fake but valid ID format
|
|
58
|
+
* @returns A valid format reference ID for testing
|
|
59
|
+
*/
|
|
60
|
+
static generateTestId(testSeed) {
|
|
61
|
+
const content = Buffer.from(`test-${testSeed}-${Date.now()}`);
|
|
62
|
+
return this.generateId(content);
|
|
32
63
|
}
|
|
33
64
|
}
|
|
34
65
|
export {
|
|
35
|
-
|
|
36
|
-
DEFAULT_CONTENT_REFERENCE_CONFIG
|
|
66
|
+
ReferenceIdGenerator
|
|
37
67
|
};
|
|
38
68
|
//# sourceMappingURL=index24.js.map
|
package/dist/esm/index24.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index24.js","sources":["../../src/
|
|
1
|
+
{"version":3,"file":"index24.js","sources":["../../src/memory/ReferenceIdGenerator.ts"],"sourcesContent":["import { createHash } from 'crypto';\nimport { ReferenceId } from '../types/content-reference';\n\n/**\n * Content-based reference ID generator using SHA-256 (HCS-1 style)\n * \n * Generates deterministic reference IDs based on content hashing.\n * Same content always produces the same reference ID.\n */\nexport class ReferenceIdGenerator {\n /**\n * Generate a content-based reference ID using SHA-256 hashing\n * \n * @param content The content to generate a reference ID for\n * @returns Deterministic reference ID based on content hash\n */\n static generateId(content: Buffer): ReferenceId {\n const hash = createHash('sha256');\n hash.update(content);\n return hash.digest('base64url');\n }\n \n /**\n * Validate that a string is a properly formatted reference ID\n * \n * @param id The ID to validate\n * @returns true if the ID is valid format\n */\n static isValidReferenceId(id: string): id is ReferenceId {\n if (!id || typeof id !== 'string') {\n return false;\n }\n \n // Check length (base64url encoding of SHA-256 hash = 43 chars)\n if (id.length !== 43) {\n return false;\n }\n \n // Check character set (base64url: A-Z, a-z, 0-9, -, _)\n return /^[A-Za-z0-9_-]+$/.test(id);\n }\n \n /**\n * Extract reference ID from ref:// format\n * \n * @param input Input string that may contain a reference ID\n * @returns Extracted reference ID or null if not found\n */\n static extractReferenceId(input: string): ReferenceId | null {\n if (!input || typeof input !== 'string') {\n return null;\n }\n \n // Check for ref:// format\n const refFormatMatch = input.match(/^ref:\\/\\/([A-Za-z0-9_-]{43})$/);\n if (refFormatMatch) {\n return refFormatMatch[1] as ReferenceId;\n }\n \n // Check if input is directly a valid reference ID\n return this.isValidReferenceId(input) ? input : null;\n }\n \n /**\n * Format a reference ID in the standard ref:// format\n * \n * @param referenceId The reference ID to format\n * @returns Formatted reference string\n */\n static formatReference(referenceId: ReferenceId): string {\n return `ref://${referenceId}`;\n }\n \n /**\n * Generate a test reference ID (for testing purposes only)\n * \n * @param testSeed A test seed to generate a fake but valid ID format\n * @returns A valid format reference ID for testing\n */\n static generateTestId(testSeed: string): ReferenceId {\n const content = Buffer.from(`test-${testSeed}-${Date.now()}`);\n return this.generateId(content);\n }\n}"],"names":[],"mappings":";AASO,MAAM,qBAAqB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOhC,OAAO,WAAW,SAA8B;AAC9C,UAAM,OAAO,WAAW,QAAQ;AAChC,SAAK,OAAO,OAAO;AACnB,WAAO,KAAK,OAAO,WAAW;AAAA,EAChC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAO,mBAAmB,IAA+B;AACvD,QAAI,CAAC,MAAM,OAAO,OAAO,UAAU;AACjC,aAAO;AAAA,IACT;AAGA,QAAI,GAAG,WAAW,IAAI;AACpB,aAAO;AAAA,IACT;AAGA,WAAO,mBAAmB,KAAK,EAAE;AAAA,EACnC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAO,mBAAmB,OAAmC;AAC3D,QAAI,CAAC,SAAS,OAAO,UAAU,UAAU;AACvC,aAAO;AAAA,IACT;AAGA,UAAM,iBAAiB,MAAM,MAAM,+BAA+B;AAClE,QAAI,gBAAgB;AAClB,aAAO,eAAe,CAAC;AAAA,IACzB;AAGA,WAAO,KAAK,mBAAmB,KAAK,IAAI,QAAQ;AAAA,EAClD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAO,gBAAgB,aAAkC;AACvD,WAAO,SAAS,WAAW;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAO,eAAe,UAA+B;AACnD,UAAM,UAAU,OAAO,KAAK,QAAQ,QAAQ,IAAI,KAAK,IAAA,CAAK,EAAE;AAC5D,WAAO,KAAK,WAAW,OAAO;AAAA,EAChC;AACF;"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const DEFAULT_CONTENT_REFERENCE_CONFIG = {
|
|
2
|
+
sizeThresholdBytes: 10 * 1024,
|
|
3
|
+
// 10KB
|
|
4
|
+
maxAgeMs: 60 * 60 * 1e3,
|
|
5
|
+
// 1 hour
|
|
6
|
+
maxReferences: 100,
|
|
7
|
+
maxTotalStorageBytes: 100 * 1024 * 1024,
|
|
8
|
+
// 100MB
|
|
9
|
+
enableAutoCleanup: true,
|
|
10
|
+
cleanupIntervalMs: 5 * 60 * 1e3,
|
|
11
|
+
// 5 minutes
|
|
12
|
+
enablePersistence: false,
|
|
13
|
+
storageBackend: "memory",
|
|
14
|
+
cleanupPolicies: {
|
|
15
|
+
recent: { maxAgeMs: 30 * 60 * 1e3, priority: 1 },
|
|
16
|
+
// 30 minutes, highest priority
|
|
17
|
+
userContent: { maxAgeMs: 2 * 60 * 60 * 1e3, priority: 2 },
|
|
18
|
+
// 2 hours
|
|
19
|
+
agentGenerated: { maxAgeMs: 60 * 60 * 1e3, priority: 3 },
|
|
20
|
+
// 1 hour
|
|
21
|
+
default: { maxAgeMs: 60 * 60 * 1e3, priority: 4 }
|
|
22
|
+
// 1 hour, lowest priority
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
class ContentReferenceError extends Error {
|
|
26
|
+
constructor(message, type, referenceId, suggestedActions) {
|
|
27
|
+
super(message);
|
|
28
|
+
this.type = type;
|
|
29
|
+
this.referenceId = referenceId;
|
|
30
|
+
this.suggestedActions = suggestedActions;
|
|
31
|
+
this.name = "ContentReferenceError";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export {
|
|
35
|
+
ContentReferenceError,
|
|
36
|
+
DEFAULT_CONTENT_REFERENCE_CONFIG
|
|
37
|
+
};
|
|
38
|
+
//# sourceMappingURL=index25.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index25.js","sources":["../../src/types/content-reference.ts"],"sourcesContent":["/**\n * Content Reference System Types\n * \n * Shared interfaces for the Reference-Based Content System that handles\n * large content storage with unique reference IDs to optimize context window usage.\n */\n\n/**\n * Unique identifier for stored content references\n * Format: Cryptographically secure 32-byte identifier with base64url encoding\n */\nexport type ReferenceId = string;\n\n/**\n * Lifecycle state of a content reference\n */\nexport type ReferenceLifecycleState = 'active' | 'expired' | 'cleanup_pending' | 'invalid';\n\n/**\n * Content types supported by the reference system\n */\nexport type ContentType = 'text' | 'json' | 'html' | 'markdown' | 'binary' | 'unknown';\n\n/**\n * Sources that created the content reference\n */\nexport type ContentSource = 'mcp_tool' | 'user_upload' | 'agent_generated' | 'system';\n\n/**\n * Metadata associated with stored content\n */\nexport interface ContentMetadata {\n /** Content type classification */\n contentType: ContentType;\n \n /** MIME type of the original content */\n mimeType?: string;\n \n /** Size in bytes of the stored content */\n sizeBytes: number;\n \n /** When the content was originally stored */\n createdAt: Date;\n \n /** Last time the content was accessed via reference resolution */\n lastAccessedAt: Date;\n \n /** Source that created this content reference */\n source: ContentSource;\n \n /** Name of the MCP tool that generated the content (if applicable) */\n mcpToolName?: string;\n \n /** Original filename or suggested name for the content */\n fileName?: string;\n \n /** Number of times this reference has been resolved */\n accessCount: number;\n \n /** Tags for categorization and cleanup policies */\n tags?: string[];\n \n /** Custom metadata from the source */\n customMetadata?: Record<string, unknown>;\n}\n\n/**\n * Core content reference object passed through agent context\n * Designed to be lightweight (<100 tokens) while providing enough \n * information for agent decision-making\n */\nexport interface ContentReference {\n /** Unique identifier for resolving the content */\n referenceId: ReferenceId;\n \n /** Current lifecycle state */\n state: ReferenceLifecycleState;\n \n /** Brief description or preview of the content (max 200 chars) */\n preview: string;\n \n /** Essential metadata for agent decision-making */\n metadata: Pick<ContentMetadata, 'contentType' | 'sizeBytes' | 'source' | 'fileName' | 'mimeType'>;\n \n /** When this reference was created */\n createdAt: Date;\n \n /** Special format indicator for reference IDs in content */\n readonly format: 'ref://{id}';\n}\n\n/**\n * Result of attempting to resolve a content reference\n */\nexport interface ReferenceResolutionResult {\n /** Whether the resolution was successful */\n success: boolean;\n \n /** The resolved content if successful */\n content?: Buffer;\n \n /** Complete metadata if successful */\n metadata?: ContentMetadata;\n \n /** Error message if resolution failed */\n error?: string;\n \n /** Specific error type for targeted error handling */\n errorType?: 'not_found' | 'expired' | 'corrupted' | 'access_denied' | 'system_error';\n \n /** Suggested actions for recovery */\n suggestedActions?: string[];\n}\n\n/**\n * Configuration for content reference storage and lifecycle\n */\nexport interface ContentReferenceConfig {\n /** Size threshold above which content should be stored as references (default: 10KB) */\n sizeThresholdBytes: number;\n \n /** Maximum age for unused references before cleanup (default: 1 hour) */\n maxAgeMs: number;\n \n /** Maximum number of references to store simultaneously */\n maxReferences: number;\n \n /** Maximum total storage size for all references */\n maxTotalStorageBytes: number;\n \n /** Whether to enable automatic cleanup */\n enableAutoCleanup: boolean;\n \n /** Interval for cleanup checks in milliseconds */\n cleanupIntervalMs: number;\n \n /** Whether to persist references across restarts */\n enablePersistence: boolean;\n \n /** Storage backend configuration */\n storageBackend: 'memory' | 'filesystem' | 'hybrid';\n \n /** Cleanup policies for different content types */\n cleanupPolicies: {\n /** Policy for content marked as \"recent\" from MCP tools */\n recent: { maxAgeMs: number; priority: number };\n \n /** Policy for user-uploaded content */\n userContent: { maxAgeMs: number; priority: number };\n \n /** Policy for agent-generated content */\n agentGenerated: { maxAgeMs: number; priority: number };\n \n /** Default policy for other content */\n default: { maxAgeMs: number; priority: number };\n };\n}\n\n/**\n * Default configuration values\n */\nexport const DEFAULT_CONTENT_REFERENCE_CONFIG: ContentReferenceConfig = {\n sizeThresholdBytes: 10 * 1024, // 10KB\n maxAgeMs: 60 * 60 * 1000, // 1 hour\n maxReferences: 100,\n maxTotalStorageBytes: 100 * 1024 * 1024, // 100MB\n enableAutoCleanup: true,\n cleanupIntervalMs: 5 * 60 * 1000, // 5 minutes\n enablePersistence: false,\n storageBackend: 'memory',\n cleanupPolicies: {\n recent: { maxAgeMs: 30 * 60 * 1000, priority: 1 }, // 30 minutes, highest priority\n userContent: { maxAgeMs: 2 * 60 * 60 * 1000, priority: 2 }, // 2 hours\n agentGenerated: { maxAgeMs: 60 * 60 * 1000, priority: 3 }, // 1 hour\n default: { maxAgeMs: 60 * 60 * 1000, priority: 4 } // 1 hour, lowest priority\n }\n};\n\n/**\n * Statistics about content reference usage and storage\n */\nexport interface ContentReferenceStats {\n /** Total number of active references */\n activeReferences: number;\n \n /** Total storage used by all references in bytes */\n totalStorageBytes: number;\n \n /** Number of references cleaned up in last cleanup cycle */\n recentlyCleanedUp: number;\n \n /** Number of successful reference resolutions since startup */\n totalResolutions: number;\n \n /** Number of failed resolution attempts */\n failedResolutions: number;\n \n /** Average content size in bytes */\n averageContentSize: number;\n \n /** Most frequently accessed reference ID */\n mostAccessedReferenceId?: ReferenceId;\n \n /** Storage utilization percentage */\n storageUtilization: number;\n \n /** Performance metrics */\n performanceMetrics: {\n /** Average time to create a reference in milliseconds */\n averageCreationTimeMs: number;\n \n /** Average time to resolve a reference in milliseconds */\n averageResolutionTimeMs: number;\n \n /** Average cleanup time in milliseconds */\n averageCleanupTimeMs: number;\n };\n}\n\n/**\n * Error types for content reference operations\n */\nexport class ContentReferenceError extends Error {\n constructor(\n message: string,\n public readonly type: ReferenceResolutionResult['errorType'],\n public readonly referenceId?: ReferenceId,\n public readonly suggestedActions?: string[]\n ) {\n super(message);\n this.name = 'ContentReferenceError';\n }\n}\n\n/**\n * Interface for content reference storage implementations\n */\nexport interface ContentReferenceStore {\n /**\n * Store content and return a reference\n */\n storeContent(\n content: Buffer,\n metadata: Omit<ContentMetadata, 'createdAt' | 'lastAccessedAt' | 'accessCount'>\n ): Promise<ContentReference>;\n \n /**\n * Resolve a reference to its content\n */\n resolveReference(referenceId: ReferenceId): Promise<ReferenceResolutionResult>;\n \n /**\n * Check if a reference exists and is valid\n */\n hasReference(referenceId: ReferenceId): Promise<boolean>;\n \n /**\n * Mark a reference for cleanup\n */\n cleanupReference(referenceId: ReferenceId): Promise<boolean>;\n \n /**\n * Get current storage statistics\n */\n getStats(): Promise<ContentReferenceStats>;\n \n /**\n * Update configuration\n */\n updateConfig(config: Partial<ContentReferenceConfig>): Promise<void>;\n \n /**\n * Perform cleanup based on current policies\n */\n performCleanup(): Promise<{ cleanedUp: number; errors: string[] }>;\n \n /**\n * Dispose of resources\n */\n dispose(): Promise<void>;\n}"],"names":[],"mappings":"AAiKO,MAAM,mCAA2D;AAAA,EACtE,oBAAoB,KAAK;AAAA;AAAA,EACzB,UAAU,KAAK,KAAK;AAAA;AAAA,EACpB,eAAe;AAAA,EACf,sBAAsB,MAAM,OAAO;AAAA;AAAA,EACnC,mBAAmB;AAAA,EACnB,mBAAmB,IAAI,KAAK;AAAA;AAAA,EAC5B,mBAAmB;AAAA,EACnB,gBAAgB;AAAA,EAChB,iBAAiB;AAAA,IACf,QAAQ,EAAE,UAAU,KAAK,KAAK,KAAM,UAAU,EAAA;AAAA;AAAA,IAC9C,aAAa,EAAE,UAAU,IAAI,KAAK,KAAK,KAAM,UAAU,EAAA;AAAA;AAAA,IACvD,gBAAgB,EAAE,UAAU,KAAK,KAAK,KAAM,UAAU,EAAA;AAAA;AAAA,IACtD,SAAS,EAAE,UAAU,KAAK,KAAK,KAAM,UAAU,EAAA;AAAA;AAAA,EAAE;AAErD;AA8CO,MAAM,8BAA8B,MAAM;AAAA,EAC/C,YACE,SACgB,MACA,aACA,kBAChB;AACA,UAAM,OAAO;AAJG,SAAA,OAAA;AACA,SAAA,cAAA;AACA,SAAA,mBAAA;AAGhB,SAAK,OAAO;AAAA,EACd;AACF;"}
|
package/dist/esm/index8.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { createOpenAIToolsAgent
|
|
1
|
+
import { createOpenAIToolsAgent } from "langchain/agents";
|
|
2
|
+
import { ContentAwareAgentExecutor } from "./index15.js";
|
|
2
3
|
import { ChatPromptTemplate, MessagesPlaceholder } from "@langchain/core/prompts";
|
|
3
4
|
import { ChatOpenAI } from "@langchain/openai";
|
|
4
5
|
import { TokenUsageCallbackHandler, calculateTokenCostSync, getAllHederaCorePlugins, HederaAgentKit } from "hedera-agent-kit";
|
|
5
6
|
import { BaseAgent } from "./index7.js";
|
|
6
|
-
import { MCPClientManager } from "./
|
|
7
|
-
import { convertMCPToolToLangChain } from "./
|
|
8
|
-
import { SmartMemoryManager } from "./
|
|
7
|
+
import { MCPClientManager } from "./index16.js";
|
|
8
|
+
import { convertMCPToolToLangChain } from "./index17.js";
|
|
9
|
+
import { SmartMemoryManager } from "./index18.js";
|
|
9
10
|
class LangChainAgent extends BaseAgent {
|
|
10
11
|
constructor() {
|
|
11
12
|
super(...arguments);
|
|
@@ -234,7 +235,7 @@ class LangChainAgent extends BaseAgent {
|
|
|
234
235
|
tools: langchainTools,
|
|
235
236
|
prompt
|
|
236
237
|
});
|
|
237
|
-
this.executor = new
|
|
238
|
+
this.executor = new ContentAwareAgentExecutor({
|
|
238
239
|
agent,
|
|
239
240
|
tools: langchainTools,
|
|
240
241
|
verbose: this.config.debug?.verbose ?? false,
|
package/dist/esm/index8.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index8.js","sources":["../../src/langchain-agent.ts"],"sourcesContent":["import type { BaseChatModel } from '@langchain/core/language_models/chat_models';\nimport type { StructuredTool } from '@langchain/core/tools';\nimport { createOpenAIToolsAgent, AgentExecutor } from 'langchain/agents';\nimport {\n ChatPromptTemplate,\n MessagesPlaceholder,\n} from '@langchain/core/prompts';\nimport { ChatOpenAI } from '@langchain/openai';\nimport {\n calculateTokenCostSync,\n getAllHederaCorePlugins,\n HederaAgentKit,\n TokenUsageCallbackHandler,\n} from 'hedera-agent-kit';\nimport type { TokenUsage, CostCalculation } from 'hedera-agent-kit';\nimport {\n BaseAgent,\n type ConversationContext,\n type ChatResponse,\n type OperationalMode,\n type UsageStats,\n} from './base-agent';\nimport { MCPClientManager } from './mcp/MCPClientManager';\nimport { convertMCPToolToLangChain } from './mcp/adapters/langchain';\nimport { SmartMemoryManager } from './memory/SmartMemoryManager';\n\nexport class LangChainAgent extends BaseAgent {\n private executor: AgentExecutor | undefined;\n private systemMessage = '';\n private mcpManager?: MCPClientManager;\n private smartMemory: SmartMemoryManager | undefined;\n\n async boot(): Promise<void> {\n if (this.initialized) {\n this.logger.warn('Agent already initialized');\n return;\n }\n\n try {\n this.agentKit = await this.createAgentKit();\n await this.agentKit.initialize();\n\n const modelName =\n this.config.ai?.modelName ||\n process.env.OPENAI_MODEL_NAME ||\n 'gpt-4o-mini';\n this.tokenTracker = new TokenUsageCallbackHandler(modelName);\n\n const allTools = this.agentKit.getAggregatedLangChainTools();\n this.tools = this.filterTools(allTools);\n\n if (this.config.mcp?.servers && this.config.mcp.servers.length > 0) {\n await this.initializeMCP();\n }\n\n this.smartMemory = new SmartMemoryManager({\n modelName,\n maxTokens: 90000,\n reserveTokens: 10000,\n storageLimit: 1000\n });\n \n this.logger.info('SmartMemoryManager initialized:', {\n modelName,\n toolsCount: this.tools.length,\n maxTokens: 90000,\n reserveTokens: 10000\n });\n\n this.systemMessage = this.buildSystemPrompt();\n \n // Set the system prompt in smart memory\n this.smartMemory.setSystemPrompt(this.systemMessage);\n\n await this.createExecutor();\n\n this.initialized = true;\n this.logger.info('LangChain Hedera agent initialized');\n } catch (error) {\n this.logger.error('Failed to initialize agent:', error);\n throw error;\n }\n }\n\n async chat(\n message: string,\n context?: ConversationContext\n ): Promise<ChatResponse> {\n if (!this.initialized || !this.executor || !this.smartMemory) {\n throw new Error('Agent not initialized. Call boot() first.');\n }\n\n try {\n this.logger.info('LangChainAgent.chat called with:', { message, contextLength: context?.messages?.length || 0 });\n \n // If context is provided, populate smart memory with previous messages\n if (context?.messages && context.messages.length > 0) {\n // Clear existing memory and add messages from context\n this.smartMemory.clear();\n \n for (const msg of context.messages) {\n this.smartMemory.addMessage(msg);\n }\n }\n \n // Add the current user message to memory\n const { HumanMessage } = await import('@langchain/core/messages');\n this.smartMemory.addMessage(new HumanMessage(message));\n \n const memoryStats = this.smartMemory.getMemoryStats();\n this.logger.info('Memory stats before execution:', {\n totalMessages: memoryStats.totalActiveMessages,\n currentTokens: memoryStats.currentTokenCount,\n maxTokens: memoryStats.maxTokens,\n usagePercentage: memoryStats.usagePercentage,\n toolsCount: this.tools.length\n });\n \n const result = await this.executor.invoke({\n input: message,\n chat_history: this.smartMemory.getMessages(),\n });\n\n this.logger.info('LangChainAgent executor result:', result);\n\n let response: ChatResponse = {\n output: result.output || '',\n message: result.output || '',\n notes: [],\n intermediateSteps: result.intermediateSteps,\n };\n\n // Extract tool calls from intermediate steps\n if (result.intermediateSteps && Array.isArray(result.intermediateSteps)) {\n const toolCalls = result.intermediateSteps.map((step: any, index: number) => ({\n id: `call_${index}`,\n name: step.action?.tool || 'unknown',\n args: step.action?.toolInput || {},\n output: typeof step.observation === 'string' ? step.observation : JSON.stringify(step.observation)\n }));\n \n if (toolCalls.length > 0) {\n response.tool_calls = toolCalls;\n }\n }\n\n const parsedSteps = result?.intermediateSteps?.[0]?.observation;\n if (\n parsedSteps &&\n typeof parsedSteps === 'string' &&\n this.isJSON(parsedSteps)\n ) {\n try {\n const parsed = JSON.parse(parsedSteps);\n response = { ...response, ...parsed };\n } catch (error) {\n this.logger.error('Error parsing intermediate steps:', error);\n }\n }\n\n if (!response.output || response.output.trim() === '') {\n response.output = 'Agent action complete.';\n }\n\n // Add the AI response to memory\n if (response.output) {\n const { AIMessage } = await import('@langchain/core/messages');\n this.smartMemory.addMessage(new AIMessage(response.output));\n }\n\n if (this.tokenTracker) {\n const tokenUsage = this.tokenTracker.getLatestTokenUsage();\n if (tokenUsage) {\n response.tokenUsage = tokenUsage;\n response.cost = calculateTokenCostSync(tokenUsage);\n }\n }\n\n const finalMemoryStats = this.smartMemory.getMemoryStats();\n response.metadata = {\n ...response.metadata,\n memoryStats: {\n activeMessages: finalMemoryStats.totalActiveMessages,\n tokenUsage: finalMemoryStats.currentTokenCount,\n maxTokens: finalMemoryStats.maxTokens,\n usagePercentage: finalMemoryStats.usagePercentage\n }\n };\n\n this.logger.info('LangChainAgent.chat returning response:', response);\n return response;\n } catch (error) {\n this.logger.error('LangChainAgent.chat error:', error);\n return this.handleError(error);\n }\n }\n\n async shutdown(): Promise<void> {\n if (this.mcpManager) {\n await this.mcpManager.disconnectAll();\n }\n\n if (this.smartMemory) {\n this.smartMemory.dispose();\n this.smartMemory = undefined;\n }\n\n this.executor = undefined;\n this.agentKit = undefined;\n this.tools = [];\n this.initialized = false;\n this.logger.info('Agent cleaned up');\n }\n\n switchMode(mode: OperationalMode): void {\n if (this.config.execution) {\n this.config.execution.operationalMode = mode;\n } else {\n this.config.execution = { operationalMode: mode };\n }\n\n if (this.agentKit) {\n this.agentKit.operationalMode = mode;\n }\n\n this.systemMessage = this.buildSystemPrompt();\n this.logger.info(`Operational mode switched to: ${mode}`);\n }\n\n getUsageStats(): UsageStats {\n if (!this.tokenTracker) {\n return {\n promptTokens: 0,\n completionTokens: 0,\n totalTokens: 0,\n cost: { totalCost: 0 } as CostCalculation,\n };\n }\n\n const usage = this.tokenTracker.getTotalTokenUsage();\n const cost = calculateTokenCostSync(usage);\n return { ...usage, cost };\n }\n\n getUsageLog(): UsageStats[] {\n if (!this.tokenTracker) {\n return [];\n }\n\n return this.tokenTracker.getTokenUsageHistory().map((usage) => ({\n ...usage,\n cost: calculateTokenCostSync(usage),\n }));\n }\n\n clearUsageStats(): void {\n if (this.tokenTracker) {\n this.tokenTracker.reset();\n this.logger.info('Usage statistics cleared');\n }\n }\n\n\n private async createAgentKit(): Promise<HederaAgentKit> {\n const corePlugins = getAllHederaCorePlugins();\n const extensionPlugins = this.config.extensions?.plugins || [];\n const plugins = [...corePlugins, ...extensionPlugins];\n\n const operationalMode =\n this.config.execution?.operationalMode || 'returnBytes';\n const modelName = this.config.ai?.modelName || 'gpt-4o';\n\n return new HederaAgentKit(\n this.config.signer,\n { plugins },\n operationalMode,\n this.config.execution?.userAccountId,\n this.config.execution?.scheduleUserTransactionsInBytesMode ?? true,\n undefined,\n modelName,\n this.config.extensions?.mirrorConfig,\n this.config.debug?.silent ?? false\n );\n }\n\n private async createExecutor(): Promise<void> {\n let llm: BaseChatModel;\n if (this.config.ai?.provider && this.config.ai.provider.getModel) {\n llm = this.config.ai.provider.getModel() as BaseChatModel;\n } else if (this.config.ai?.llm) {\n llm = this.config.ai.llm as BaseChatModel;\n } else {\n const apiKey = this.config.ai?.apiKey || process.env.OPENAI_API_KEY;\n if (!apiKey) {\n throw new Error('OpenAI API key required');\n }\n\n llm = new ChatOpenAI({\n apiKey,\n modelName: this.config.ai?.modelName || 'gpt-4o-mini',\n temperature: this.config.ai?.temperature ?? 0.1,\n callbacks: this.tokenTracker ? [this.tokenTracker] : [],\n });\n }\n\n const prompt = ChatPromptTemplate.fromMessages([\n ['system', this.systemMessage],\n new MessagesPlaceholder('chat_history'),\n ['human', '{input}'],\n new MessagesPlaceholder('agent_scratchpad'),\n ]);\n\n const langchainTools = this.tools as unknown as StructuredTool[];\n\n const agent = await createOpenAIToolsAgent({\n llm,\n tools: langchainTools,\n prompt,\n });\n\n // Create executor without memory - we handle memory manually with SmartMemoryManager\n this.executor = new AgentExecutor({\n agent,\n tools: langchainTools,\n verbose: this.config.debug?.verbose ?? false,\n returnIntermediateSteps: true,\n });\n }\n\n private handleError(error: unknown): ChatResponse {\n const errorMessage =\n error instanceof Error ? error.message : 'Unknown error';\n this.logger.error('Chat error:', error);\n\n let tokenUsage: TokenUsage | undefined;\n let cost: CostCalculation | undefined;\n\n if (this.tokenTracker) {\n tokenUsage = this.tokenTracker.getLatestTokenUsage();\n if (tokenUsage) {\n cost = calculateTokenCostSync(tokenUsage);\n }\n }\n\n const errorResponse: ChatResponse = {\n output: 'Sorry, I encountered an error processing your request.',\n message: 'Error processing request.',\n error: errorMessage,\n notes: [],\n };\n\n if (tokenUsage) {\n errorResponse.tokenUsage = tokenUsage;\n }\n\n if (cost) {\n errorResponse.cost = cost;\n }\n\n return errorResponse;\n }\n\n private async initializeMCP(): Promise<void> {\n this.mcpManager = new MCPClientManager(this.logger);\n\n for (const serverConfig of this.config.mcp!.servers!) {\n if (serverConfig.autoConnect === false) {\n this.logger.info(\n `Skipping MCP server ${serverConfig.name} (autoConnect=false)`\n );\n continue;\n }\n\n const status = await this.mcpManager.connectServer(serverConfig);\n\n if (status.connected) {\n this.logger.info(\n `Connected to MCP server ${status.serverName} with ${status.tools.length} tools`\n );\n\n for (const mcpTool of status.tools) {\n const langchainTool = convertMCPToolToLangChain(\n mcpTool,\n this.mcpManager,\n serverConfig\n );\n this.tools.push(langchainTool);\n }\n } else {\n this.logger.error(\n `Failed to connect to MCP server ${status.serverName}: ${status.error}`\n );\n }\n }\n }\n\n /**\n * Check if a string is valid JSON\n */\n private isJSON(str: string): boolean {\n if (typeof str !== 'string') return false;\n\n const trimmed = str.trim();\n if (!trimmed) return false;\n\n if (\n !(trimmed.startsWith('{') && trimmed.endsWith('}')) &&\n !(trimmed.startsWith('[') && trimmed.endsWith(']'))\n ) {\n return false;\n }\n\n try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AEF,eAAK,MAAM,KAAK,aAAa;AAAA,QAC/B;AAAA,MACF,OAAO;AACL,aAAK,OAAO;AAAA,UACV,mCAAmC,OAAO,UAAU,KAAK,OAAO,KAAK;AAAA,QAAA;AAAA,MAEzE;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKQ,OAAO,KAAsB;AACnC,QAAI,OAAO,QAAQ,SAAU,QAAO;AAEpC,UAAM,UAAU,IAAI,KAAA;AACpB,QAAI,CAAC,QAAS,QAAO;AAErB,QACE,EAAE,QAAQ,WAAW,GAAG,KAAK,QAAQ,SAAS,GAAG,MACjD,EAAE,QAAQ,WAAW,GAAG,KAAK,QAAQ,SAAS,GAAG,IACjD;AACA,aAAO;AAAA,IACT;AAEA,QAAI;AACF,WAAK,MAAM,OAAO;AAClB,aAAO;AAAA,IACT,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF;AACF;"}
|
|
1
|
+
{"version":3,"file":"index8.js","sources":["../../src/langchain-agent.ts"],"sourcesContent":["import type { BaseChatModel } from '@langchain/core/language_models/chat_models';\nimport type { StructuredTool } from '@langchain/core/tools';\nimport { createOpenAIToolsAgent } from 'langchain/agents';\nimport { ContentAwareAgentExecutor } from './langchain/ContentAwareAgentExecutor';\nimport {\n ChatPromptTemplate,\n MessagesPlaceholder,\n} from '@langchain/core/prompts';\nimport { ChatOpenAI } from '@langchain/openai';\nimport {\n calculateTokenCostSync,\n getAllHederaCorePlugins,\n HederaAgentKit,\n TokenUsageCallbackHandler,\n} from 'hedera-agent-kit';\nimport type { TokenUsage, CostCalculation } from 'hedera-agent-kit';\nimport {\n BaseAgent,\n type ConversationContext,\n type ChatResponse,\n type OperationalMode,\n type UsageStats,\n} from './base-agent';\nimport { MCPClientManager } from './mcp/MCPClientManager';\nimport { convertMCPToolToLangChain } from './mcp/adapters/langchain';\nimport { SmartMemoryManager } from './memory/SmartMemoryManager';\n\nexport class LangChainAgent extends BaseAgent {\n private executor: ContentAwareAgentExecutor | undefined;\n private systemMessage = '';\n private mcpManager?: MCPClientManager;\n private smartMemory: SmartMemoryManager | undefined;\n\n async boot(): Promise<void> {\n if (this.initialized) {\n this.logger.warn('Agent already initialized');\n return;\n }\n\n try {\n this.agentKit = await this.createAgentKit();\n await this.agentKit.initialize();\n\n const modelName =\n this.config.ai?.modelName ||\n process.env.OPENAI_MODEL_NAME ||\n 'gpt-4o-mini';\n this.tokenTracker = new TokenUsageCallbackHandler(modelName);\n\n const allTools = this.agentKit.getAggregatedLangChainTools();\n this.tools = this.filterTools(allTools);\n\n if (this.config.mcp?.servers && this.config.mcp.servers.length > 0) {\n await this.initializeMCP();\n }\n\n this.smartMemory = new SmartMemoryManager({\n modelName,\n maxTokens: 90000,\n reserveTokens: 10000,\n storageLimit: 1000\n });\n \n this.logger.info('SmartMemoryManager initialized:', {\n modelName,\n toolsCount: this.tools.length,\n maxTokens: 90000,\n reserveTokens: 10000\n });\n\n this.systemMessage = this.buildSystemPrompt();\n \n // Set the system prompt in smart memory\n this.smartMemory.setSystemPrompt(this.systemMessage);\n\n await this.createExecutor();\n\n this.initialized = true;\n this.logger.info('LangChain Hedera agent initialized');\n } catch (error) {\n this.logger.error('Failed to initialize agent:', error);\n throw error;\n }\n }\n\n async chat(\n message: string,\n context?: ConversationContext\n ): Promise<ChatResponse> {\n if (!this.initialized || !this.executor || !this.smartMemory) {\n throw new Error('Agent not initialized. Call boot() first.');\n }\n\n try {\n this.logger.info('LangChainAgent.chat called with:', { message, contextLength: context?.messages?.length || 0 });\n \n // If context is provided, populate smart memory with previous messages\n if (context?.messages && context.messages.length > 0) {\n // Clear existing memory and add messages from context\n this.smartMemory.clear();\n \n for (const msg of context.messages) {\n this.smartMemory.addMessage(msg);\n }\n }\n \n // Add the current user message to memory\n const { HumanMessage } = await import('@langchain/core/messages');\n this.smartMemory.addMessage(new HumanMessage(message));\n \n const memoryStats = this.smartMemory.getMemoryStats();\n this.logger.info('Memory stats before execution:', {\n totalMessages: memoryStats.totalActiveMessages,\n currentTokens: memoryStats.currentTokenCount,\n maxTokens: memoryStats.maxTokens,\n usagePercentage: memoryStats.usagePercentage,\n toolsCount: this.tools.length\n });\n \n const result = await this.executor.invoke({\n input: message,\n chat_history: this.smartMemory.getMessages(),\n });\n\n this.logger.info('LangChainAgent executor result:', result);\n\n let response: ChatResponse = {\n output: result.output || '',\n message: result.output || '',\n notes: [],\n intermediateSteps: result.intermediateSteps,\n };\n\n // Extract tool calls from intermediate steps\n if (result.intermediateSteps && Array.isArray(result.intermediateSteps)) {\n const toolCalls = result.intermediateSteps.map((step: any, index: number) => ({\n id: `call_${index}`,\n name: step.action?.tool || 'unknown',\n args: step.action?.toolInput || {},\n output: typeof step.observation === 'string' ? step.observation : JSON.stringify(step.observation)\n }));\n \n if (toolCalls.length > 0) {\n response.tool_calls = toolCalls;\n }\n }\n\n const parsedSteps = result?.intermediateSteps?.[0]?.observation;\n if (\n parsedSteps &&\n typeof parsedSteps === 'string' &&\n this.isJSON(parsedSteps)\n ) {\n try {\n const parsed = JSON.parse(parsedSteps);\n response = { ...response, ...parsed };\n } catch (error) {\n this.logger.error('Error parsing intermediate steps:', error);\n }\n }\n\n if (!response.output || response.output.trim() === '') {\n response.output = 'Agent action complete.';\n }\n\n // Add the AI response to memory\n if (response.output) {\n const { AIMessage } = await import('@langchain/core/messages');\n this.smartMemory.addMessage(new AIMessage(response.output));\n }\n\n if (this.tokenTracker) {\n const tokenUsage = this.tokenTracker.getLatestTokenUsage();\n if (tokenUsage) {\n response.tokenUsage = tokenUsage;\n response.cost = calculateTokenCostSync(tokenUsage);\n }\n }\n\n const finalMemoryStats = this.smartMemory.getMemoryStats();\n response.metadata = {\n ...response.metadata,\n memoryStats: {\n activeMessages: finalMemoryStats.totalActiveMessages,\n tokenUsage: finalMemoryStats.currentTokenCount,\n maxTokens: finalMemoryStats.maxTokens,\n usagePercentage: finalMemoryStats.usagePercentage\n }\n };\n\n this.logger.info('LangChainAgent.chat returning response:', response);\n return response;\n } catch (error) {\n this.logger.error('LangChainAgent.chat error:', error);\n return this.handleError(error);\n }\n }\n\n async shutdown(): Promise<void> {\n if (this.mcpManager) {\n await this.mcpManager.disconnectAll();\n }\n\n if (this.smartMemory) {\n this.smartMemory.dispose();\n this.smartMemory = undefined;\n }\n\n this.executor = undefined;\n this.agentKit = undefined;\n this.tools = [];\n this.initialized = false;\n this.logger.info('Agent cleaned up');\n }\n\n switchMode(mode: OperationalMode): void {\n if (this.config.execution) {\n this.config.execution.operationalMode = mode;\n } else {\n this.config.execution = { operationalMode: mode };\n }\n\n if (this.agentKit) {\n this.agentKit.operationalMode = mode;\n }\n\n this.systemMessage = this.buildSystemPrompt();\n this.logger.info(`Operational mode switched to: ${mode}`);\n }\n\n getUsageStats(): UsageStats {\n if (!this.tokenTracker) {\n return {\n promptTokens: 0,\n completionTokens: 0,\n totalTokens: 0,\n cost: { totalCost: 0 } as CostCalculation,\n };\n }\n\n const usage = this.tokenTracker.getTotalTokenUsage();\n const cost = calculateTokenCostSync(usage);\n return { ...usage, cost };\n }\n\n getUsageLog(): UsageStats[] {\n if (!this.tokenTracker) {\n return [];\n }\n\n return this.tokenTracker.getTokenUsageHistory().map((usage) => ({\n ...usage,\n cost: calculateTokenCostSync(usage),\n }));\n }\n\n clearUsageStats(): void {\n if (this.tokenTracker) {\n this.tokenTracker.reset();\n this.logger.info('Usage statistics cleared');\n }\n }\n\n\n private async createAgentKit(): Promise<HederaAgentKit> {\n const corePlugins = getAllHederaCorePlugins();\n const extensionPlugins = this.config.extensions?.plugins || [];\n const plugins = [...corePlugins, ...extensionPlugins];\n\n const operationalMode =\n this.config.execution?.operationalMode || 'returnBytes';\n const modelName = this.config.ai?.modelName || 'gpt-4o';\n\n return new HederaAgentKit(\n this.config.signer,\n { plugins },\n operationalMode,\n this.config.execution?.userAccountId,\n this.config.execution?.scheduleUserTransactionsInBytesMode ?? true,\n undefined,\n modelName,\n this.config.extensions?.mirrorConfig,\n this.config.debug?.silent ?? false\n );\n }\n\n private async createExecutor(): Promise<void> {\n let llm: BaseChatModel;\n if (this.config.ai?.provider && this.config.ai.provider.getModel) {\n llm = this.config.ai.provider.getModel() as BaseChatModel;\n } else if (this.config.ai?.llm) {\n llm = this.config.ai.llm as BaseChatModel;\n } else {\n const apiKey = this.config.ai?.apiKey || process.env.OPENAI_API_KEY;\n if (!apiKey) {\n throw new Error('OpenAI API key required');\n }\n\n llm = new ChatOpenAI({\n apiKey,\n modelName: this.config.ai?.modelName || 'gpt-4o-mini',\n temperature: this.config.ai?.temperature ?? 0.1,\n callbacks: this.tokenTracker ? [this.tokenTracker] : [],\n });\n }\n\n const prompt = ChatPromptTemplate.fromMessages([\n ['system', this.systemMessage],\n new MessagesPlaceholder('chat_history'),\n ['human', '{input}'],\n new MessagesPlaceholder('agent_scratchpad'),\n ]);\n\n const langchainTools = this.tools as unknown as StructuredTool[];\n\n const agent = await createOpenAIToolsAgent({\n llm,\n tools: langchainTools,\n prompt,\n });\n\n // Create executor without memory - we handle memory manually with SmartMemoryManager\n this.executor = new ContentAwareAgentExecutor({\n agent,\n tools: langchainTools,\n verbose: this.config.debug?.verbose ?? false,\n returnIntermediateSteps: true,\n });\n }\n\n private handleError(error: unknown): ChatResponse {\n const errorMessage =\n error instanceof Error ? error.message : 'Unknown error';\n this.logger.error('Chat error:', error);\n\n let tokenUsage: TokenUsage | undefined;\n let cost: CostCalculation | undefined;\n\n if (this.tokenTracker) {\n tokenUsage = this.tokenTracker.getLatestTokenUsage();\n if (tokenUsage) {\n cost = calculateTokenCostSync(tokenUsage);\n }\n }\n\n const errorResponse: ChatResponse = {\n output: 'Sorry, I encountered an error processing your request.',\n message: 'Error processing request.',\n error: errorMessage,\n notes: [],\n };\n\n if (tokenUsage) {\n errorResponse.tokenUsage = tokenUsage;\n }\n\n if (cost) {\n errorResponse.cost = cost;\n }\n\n return errorResponse;\n }\n\n private async initializeMCP(): Promise<void> {\n this.mcpManager = new MCPClientManager(this.logger);\n\n for (const serverConfig of this.config.mcp!.servers!) {\n if (serverConfig.autoConnect === false) {\n this.logger.info(\n `Skipping MCP server ${serverConfig.name} (autoConnect=false)`\n );\n continue;\n }\n\n const status = await this.mcpManager.connectServer(serverConfig);\n\n if (status.connected) {\n this.logger.info(\n `Connected to MCP server ${status.serverName} with ${status.tools.length} tools`\n );\n\n for (const mcpTool of status.tools) {\n const langchainTool = convertMCPToolToLangChain(\n mcpTool,\n this.mcpManager,\n serverConfig\n );\n this.tools.push(langchainTool);\n }\n } else {\n this.logger.error(\n `Failed to connect to MCP server ${status.serverName}: ${status.error}`\n );\n }\n }\n }\n\n /**\n * Check if a string is valid JSON\n */\n private isJSON(str: string): boolean {\n if (typeof str !== 'string') return false;\n\n const trimmed = str.trim();\n if (!trimmed) return false;\n\n if (\n !(trimmed.startsWith('{') && trimmed.endsWith('}')) &&\n !(trimmed.startsWith('[') && trimmed.endsWith(']'))\n ) {\n return false;\n }\n\n try {\n JSON.parse(trimmed);\n return true;\n } catch {\n return false;\n }\n }\n}\n"],"names":[],"mappings":";;;;;;;;;AA2BO,MAAM,uBAAuB,UAAU;AAAA,EAAvC,cAAA;AAAA,UAAA,GAAA,SAAA;AAEL,SAAQ,gBAAgB;AAAA,EAAA;AAAA,EAIxB,MAAM,OAAsB;AAC1B,QAAI,KAAK,aAAa;AACpB,WAAK,OAAO,KAAK,2BAA2B;AAC5C;AAAA,IACF;AAEA,QAAI;AACF,WAAK,WAAW,MAAM,KAAK,eAAA;AAC3B,YAAM,KAAK,SAAS,WAAA;AAEpB,YAAM,YACJ,KAAK,OAAO,IAAI,aAChB,QAAQ,IAAI,qBACZ;AACF,WAAK,eAAe,IAAI,0BAA0B,SAAS;AAE3D,YAAM,WAAW,KAAK,SAAS,4BAAA;AAC/B,WAAK,QAAQ,KAAK,YAAY,QAAQ;AAEtC,UAAI,KAAK,OAAO,KAAK,WAAW,KAAK,OAAO,IAAI,QAAQ,SAAS,GAAG;AAClE,cAAM,KAAK,cAAA;AAAA,MACb;AAEA,WAAK,cAAc,IAAI,mBA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custom AgentExecutor that intercepts large tool outputs and converts them to content references
|
|
5
|
+
* before they are sent to the LLM to avoid token limit issues.
|
|
6
|
+
*
|
|
7
|
+
* Note: The content reference conversion is already handled in the MCP adapter,
|
|
8
|
+
* so this class currently just extends AgentExecutor without modifications.
|
|
9
|
+
* We keep it as a placeholder for future enhancements.
|
|
10
|
+
*/
|
|
11
|
+
export declare class ContentAwareAgentExecutor extends AgentExecutor {
|
|
12
|
+
private logger;
|
|
13
|
+
constructor(config: any);
|
|
14
|
+
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AgentExecutor } from 'langchain/agents';
|
|
2
|
+
import type { ChainValues } from '@langchain/core/utils/types';
|
|
3
|
+
import { Logger } from '@hashgraphonline/standards-sdk';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Custom AgentExecutor that intercepts large tool outputs and converts them to content references
|
|
7
|
+
* before they are sent to the LLM to avoid token limit issues.
|
|
8
|
+
*
|
|
9
|
+
* Note: The content reference conversion is already handled in the MCP adapter,
|
|
10
|
+
* so this class currently just extends AgentExecutor without modifications.
|
|
11
|
+
* We keep it as a placeholder for future enhancements.
|
|
12
|
+
*/
|
|
13
|
+
export class ContentAwareAgentExecutor extends AgentExecutor {
|
|
14
|
+
private logger: Logger;
|
|
15
|
+
|
|
16
|
+
constructor(config: any) {
|
|
17
|
+
super(config);
|
|
18
|
+
this.logger = new Logger({ module: 'ContentAwareAgentExecutor' });
|
|
19
|
+
}
|
|
20
|
+
}
|
package/src/langchain-agent.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
|
2
2
|
import type { StructuredTool } from '@langchain/core/tools';
|
|
3
|
-
import { createOpenAIToolsAgent
|
|
3
|
+
import { createOpenAIToolsAgent } from 'langchain/agents';
|
|
4
|
+
import { ContentAwareAgentExecutor } from './langchain/ContentAwareAgentExecutor';
|
|
4
5
|
import {
|
|
5
6
|
ChatPromptTemplate,
|
|
6
7
|
MessagesPlaceholder,
|
|
@@ -25,7 +26,7 @@ import { convertMCPToolToLangChain } from './mcp/adapters/langchain';
|
|
|
25
26
|
import { SmartMemoryManager } from './memory/SmartMemoryManager';
|
|
26
27
|
|
|
27
28
|
export class LangChainAgent extends BaseAgent {
|
|
28
|
-
private executor:
|
|
29
|
+
private executor: ContentAwareAgentExecutor | undefined;
|
|
29
30
|
private systemMessage = '';
|
|
30
31
|
private mcpManager?: MCPClientManager;
|
|
31
32
|
private smartMemory: SmartMemoryManager | undefined;
|
|
@@ -319,7 +320,7 @@ export class LangChainAgent extends BaseAgent {
|
|
|
319
320
|
});
|
|
320
321
|
|
|
321
322
|
// Create executor without memory - we handle memory manually with SmartMemoryManager
|
|
322
|
-
this.executor = new
|
|
323
|
+
this.executor = new ContentAwareAgentExecutor({
|
|
323
324
|
agent,
|
|
324
325
|
tools: langchainTools,
|
|
325
326
|
verbose: this.config.debug?.verbose ?? false,
|
|
@@ -2,6 +2,8 @@ import { DynamicStructuredTool } from '@langchain/core/tools';
|
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import type { MCPToolInfo, MCPServerConfig } from '../types';
|
|
4
4
|
import type { MCPClientManager } from '../MCPClientManager';
|
|
5
|
+
import { ContentStoreService, shouldUseReference } from '@hashgraphonline/standards-sdk';
|
|
6
|
+
import type { ContentSource } from '../../types/content-reference';
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* Convert an MCP tool to a LangChain DynamicStructuredTool
|
|
@@ -69,6 +71,36 @@ export function convertMCPToolToLangChain(
|
|
|
69
71
|
responseText = JSON.stringify(result);
|
|
70
72
|
}
|
|
71
73
|
|
|
74
|
+
// Check if content should be stored as reference
|
|
75
|
+
const responseBuffer = Buffer.from(responseText, 'utf8');
|
|
76
|
+
console.log(`[MCP Adapter] Response size: ${responseBuffer.length} bytes, tool: ${tool.serverName}_${tool.name}`);
|
|
77
|
+
|
|
78
|
+
// Use a lower threshold for MCP tools (10KB) to avoid token limit issues
|
|
79
|
+
const MCP_REFERENCE_THRESHOLD = 10 * 1024; // 10KB
|
|
80
|
+
const shouldStoreMCPContent = responseBuffer.length > MCP_REFERENCE_THRESHOLD;
|
|
81
|
+
|
|
82
|
+
if (shouldStoreMCPContent || shouldUseReference(responseBuffer)) {
|
|
83
|
+
console.log(`[MCP Adapter] Content exceeds threshold (${responseBuffer.length} > ${MCP_REFERENCE_THRESHOLD}), storing as reference`);
|
|
84
|
+
const contentStore = ContentStoreService.getInstance();
|
|
85
|
+
if (contentStore) {
|
|
86
|
+
try {
|
|
87
|
+
const referenceId = await contentStore.storeContent(responseBuffer, {
|
|
88
|
+
contentType: 'text' as ContentSource,
|
|
89
|
+
source: 'mcp',
|
|
90
|
+
mcpToolName: `${tool.serverName}_${tool.name}`,
|
|
91
|
+
originalSize: responseBuffer.length
|
|
92
|
+
});
|
|
93
|
+
console.log(`[MCP Adapter] Stored content as reference: content-ref:${referenceId}`);
|
|
94
|
+
return `content-ref:${referenceId}`;
|
|
95
|
+
} catch (storeError) {
|
|
96
|
+
// If storage fails, fall back to returning the content
|
|
97
|
+
console.warn('Failed to store large MCP content as reference:', storeError);
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
console.warn('[MCP Adapter] ContentStoreService not available');
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
72
104
|
return responseText;
|
|
73
105
|
} catch (error) {
|
|
74
106
|
const errorMessage =
|