@hashgraphonline/conversational-agent 0.1.209 → 0.1.210

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.
Files changed (106) hide show
  1. package/bin/conversational-agent-cli.js +30 -0
  2. package/cli/readme.md +181 -0
  3. package/dist/cjs/base-agent.d.ts +3 -1
  4. package/dist/cjs/conversational-agent.d.ts +14 -22
  5. package/dist/cjs/index.cjs +1 -1
  6. package/dist/cjs/index.cjs.map +1 -1
  7. package/dist/cjs/index.d.ts +5 -3
  8. package/dist/cjs/langchain-agent.d.ts +6 -3
  9. package/dist/cjs/memory/SmartMemoryManager.d.ts +7 -1
  10. package/dist/cjs/memory/TokenCounter.d.ts +1 -1
  11. package/dist/cjs/plugins/hbar/AirdropToolWrapper.d.ts +43 -0
  12. package/dist/{types/plugins/hbar-transfer/HbarTransferPlugin.d.ts → cjs/plugins/hbar/HbarPlugin.d.ts} +2 -1
  13. package/dist/{types/plugins/hbar-transfer → cjs/plugins/hbar}/TransferHbarTool.d.ts +1 -1
  14. package/dist/cjs/plugins/hbar/index.d.ts +3 -0
  15. package/dist/cjs/plugins/index.d.ts +2 -1
  16. package/dist/cjs/services/EntityResolver.d.ts +26 -0
  17. package/dist/cjs/tools/EntityResolverTool.d.ts +104 -0
  18. package/dist/cjs/types/inscription.d.ts +37 -0
  19. package/dist/esm/index.js +8 -2
  20. package/dist/esm/index.js.map +1 -1
  21. package/dist/esm/index12.js +1 -4
  22. package/dist/esm/index12.js.map +1 -1
  23. package/dist/esm/index13.js +0 -1
  24. package/dist/esm/index13.js.map +1 -1
  25. package/dist/esm/index14.js +2 -7
  26. package/dist/esm/index14.js.map +1 -1
  27. package/dist/esm/index15.js +31 -69
  28. package/dist/esm/index15.js.map +1 -1
  29. package/dist/esm/index16.js +126 -39
  30. package/dist/esm/index16.js.map +1 -1
  31. package/dist/esm/index17.js +148 -13
  32. package/dist/esm/index17.js.map +1 -1
  33. package/dist/esm/index18.js +44 -146
  34. package/dist/esm/index18.js.map +1 -1
  35. package/dist/esm/index19.js +100 -6
  36. package/dist/esm/index19.js.map +1 -1
  37. package/dist/esm/index20.js +20 -174
  38. package/dist/esm/index20.js.map +1 -1
  39. package/dist/esm/index21.js +7 -151
  40. package/dist/esm/index21.js.map +1 -1
  41. package/dist/esm/index22.js +154 -45
  42. package/dist/esm/index22.js.map +1 -1
  43. package/dist/esm/index23.js +149 -24
  44. package/dist/esm/index23.js.map +1 -1
  45. package/dist/esm/index24.js +56 -83
  46. package/dist/esm/index24.js.map +1 -1
  47. package/dist/esm/index25.js +24 -236
  48. package/dist/esm/index25.js.map +1 -1
  49. package/dist/esm/index26.js +95 -0
  50. package/dist/esm/index26.js.map +1 -0
  51. package/dist/esm/index27.js +242 -0
  52. package/dist/esm/index27.js.map +1 -0
  53. package/dist/esm/index5.js +32 -19
  54. package/dist/esm/index5.js.map +1 -1
  55. package/dist/esm/index6.js +156 -195
  56. package/dist/esm/index6.js.map +1 -1
  57. package/dist/esm/index7.js +2 -2
  58. package/dist/esm/index7.js.map +1 -1
  59. package/dist/esm/index8.js +80 -48
  60. package/dist/esm/index8.js.map +1 -1
  61. package/dist/types/base-agent.d.ts +3 -1
  62. package/dist/types/conversational-agent.d.ts +14 -22
  63. package/dist/types/index.d.ts +5 -3
  64. package/dist/types/langchain-agent.d.ts +6 -3
  65. package/dist/types/memory/SmartMemoryManager.d.ts +7 -1
  66. package/dist/types/memory/TokenCounter.d.ts +1 -1
  67. package/dist/types/plugins/hbar/AirdropToolWrapper.d.ts +43 -0
  68. package/dist/{cjs/plugins/hbar-transfer/HbarTransferPlugin.d.ts → types/plugins/hbar/HbarPlugin.d.ts} +2 -1
  69. package/dist/{cjs/plugins/hbar-transfer → types/plugins/hbar}/TransferHbarTool.d.ts +1 -1
  70. package/dist/types/plugins/hbar/index.d.ts +3 -0
  71. package/dist/types/plugins/index.d.ts +2 -1
  72. package/dist/types/services/EntityResolver.d.ts +26 -0
  73. package/dist/types/tools/EntityResolverTool.d.ts +104 -0
  74. package/dist/types/types/inscription.d.ts +37 -0
  75. package/package.json +13 -4
  76. package/src/base-agent.ts +14 -9
  77. package/src/config/system-message.ts +11 -2
  78. package/src/context/ReferenceContextManager.ts +1 -1
  79. package/src/conversational-agent.ts +221 -254
  80. package/src/index.ts +17 -3
  81. package/src/langchain-agent.ts +130 -78
  82. package/src/mcp/ContentProcessor.ts +0 -2
  83. package/src/mcp/adapters/langchain.ts +0 -1
  84. package/src/memory/ContentStorage.ts +0 -5
  85. package/src/memory/MemoryWindow.ts +0 -1
  86. package/src/memory/ReferenceIdGenerator.ts +4 -4
  87. package/src/memory/SmartMemoryManager.ts +53 -92
  88. package/src/memory/TokenCounter.ts +4 -7
  89. package/src/plugins/hbar/AirdropToolWrapper.ts +157 -0
  90. package/src/plugins/hbar/HbarPlugin.ts +86 -0
  91. package/src/plugins/{hbar-transfer → hbar}/TransferHbarTool.ts +3 -3
  92. package/src/plugins/hbar/index.ts +3 -0
  93. package/src/plugins/index.ts +2 -1
  94. package/src/services/EntityResolver.ts +135 -0
  95. package/src/tools/EntityResolverTool.ts +170 -0
  96. package/src/types/inscription.ts +40 -0
  97. package/dist/cjs/plugins/hbar-transfer/index.d.ts +0 -1
  98. package/dist/types/plugins/hbar-transfer/index.d.ts +0 -1
  99. package/src/plugins/hbar-transfer/HbarTransferPlugin.ts +0 -66
  100. package/src/plugins/hbar-transfer/index.ts +0 -1
  101. /package/dist/cjs/plugins/{hbar-transfer → hbar}/AccountBuilder.d.ts +0 -0
  102. /package/dist/cjs/plugins/{hbar-transfer → hbar}/types.d.ts +0 -0
  103. /package/dist/types/plugins/{hbar-transfer → hbar}/AccountBuilder.d.ts +0 -0
  104. /package/dist/types/plugins/{hbar-transfer → hbar}/types.d.ts +0 -0
  105. /package/src/plugins/{hbar-transfer → hbar}/AccountBuilder.ts +0 -0
  106. /package/src/plugins/{hbar-transfer → hbar}/types.ts +0 -0
@@ -0,0 +1,135 @@
1
+ import { ChatOpenAI } from '@langchain/openai';
2
+ import type { EntityAssociation } from '../memory/SmartMemoryManager';
3
+
4
+ export interface EntityResolverConfig {
5
+ apiKey: string;
6
+ modelName?: string;
7
+ }
8
+
9
+ /**
10
+ * LLM-based entity resolver that replaces brittle regex patterns
11
+ */
12
+ export class EntityResolver {
13
+ private llm: ChatOpenAI;
14
+
15
+ constructor(config: EntityResolverConfig) {
16
+ this.llm = new ChatOpenAI({
17
+ apiKey: config.apiKey,
18
+ modelName: config.modelName || 'gpt-4o-mini',
19
+ temperature: 0,
20
+ });
21
+ }
22
+
23
+ /**
24
+ * Resolve entity references using LLM instead of regex
25
+ */
26
+ async resolveReferences(
27
+ message: string,
28
+ entities: EntityAssociation[]
29
+ ): Promise<string> {
30
+ if (!entities || entities.length === 0) {
31
+ return message;
32
+ }
33
+
34
+ // Group by type for context
35
+ const byType = entities.reduce((acc, e) => {
36
+ if (!acc[e.entityType]) acc[e.entityType] = [];
37
+ acc[e.entityType].push(e);
38
+ return acc;
39
+ }, {} as Record<string, EntityAssociation[]>);
40
+
41
+ let context = 'Available entities in memory:\n';
42
+ for (const [type, list] of Object.entries(byType)) {
43
+ const recent = list[0];
44
+ context += `Most recent ${type}: "${recent.entityName}" = ${recent.entityId}\n`;
45
+ if (list.length > 1) {
46
+ context += ` (${list.length - 1} other ${type}s in memory)\n`;
47
+ }
48
+ }
49
+
50
+ const prompt = `Task: Replace entity references with their IDs from memory.
51
+
52
+ ${context}
53
+
54
+ User message: "${message}"
55
+
56
+ Instructions:
57
+ - If the user says "the topic" or "that topic" → replace with the most recent topic ID
58
+ - If the user says "the token" or "that token" → replace with the most recent token ID
59
+ - If the user says "it" or "that" after an action verb → replace with the most recent entity ID
60
+ - Examples:
61
+ * "submit on the topic" → "submit on 0.0.6543472"
62
+ * "airdrop the token" → "airdrop 0.0.123456"
63
+ * "send a message to it" → "send a message to 0.0.6543472"
64
+
65
+ Return ONLY the message with replacements made. Do not add any explanations.
66
+ Resolved message:`;
67
+
68
+ try {
69
+ const response = await this.llm.invoke(prompt);
70
+ const resolved = (response.content as string).trim();
71
+
72
+ if (resolved !== message && resolved.includes('0.0.')) {
73
+ console.log(`[EntityResolver] Resolved: "${message}" → "${resolved}"`);
74
+ return resolved;
75
+ }
76
+
77
+ return message;
78
+ } catch (error) {
79
+ console.error('[EntityResolver] Failed:', error);
80
+ return message;
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Extract entities from agent response using LLM
86
+ */
87
+ async extractEntities(
88
+ response: unknown,
89
+ userMessage: string
90
+ ): Promise<
91
+ Array<{ id: string; name: string; type: string; transactionId?: string }>
92
+ > {
93
+ const text =
94
+ typeof response === 'string' ? response : JSON.stringify(response);
95
+
96
+ const prompt = `Analyze this agent response and extract ONLY newly created entities.
97
+
98
+ User asked: "${userMessage.substring(0, 200)}"
99
+
100
+ Agent response: ${text.substring(0, 3000)}
101
+
102
+ Look for:
103
+ 1. Success messages with entity IDs (e.g., "Successfully created topic 0.0.6543472")
104
+ 2. Transaction confirmations that created new entities
105
+ 3. Entity IDs that appear after words like "created", "new", "successfully"
106
+
107
+ DO NOT include:
108
+ - Account IDs that already existed (like sender/receiver accounts)
109
+ - Entity IDs that were parameters to the operation
110
+ - Failed operations
111
+
112
+ Return a JSON array of newly created entities:
113
+ [{"id": "0.0.XXX", "name": "descriptive_name", "type": "topic|token|account"}]
114
+
115
+ If no entities were created, return: []
116
+
117
+ JSON:`;
118
+
119
+ try {
120
+ const response = await this.llm.invoke(prompt);
121
+ const content = response.content as string;
122
+ const match = content.match(/\[[\s\S]*?\]/);
123
+ if (match) {
124
+ const entities = JSON.parse(match[0]);
125
+ if (entities.length > 0) {
126
+ console.log('[EntityResolver] Extracted entities:', entities);
127
+ }
128
+ return entities;
129
+ }
130
+ } catch (error) {
131
+ console.error('[EntityResolver] Extract failed:', error);
132
+ }
133
+ return [];
134
+ }
135
+ }
@@ -0,0 +1,170 @@
1
+ import { StructuredTool } from '@langchain/core/tools';
2
+ import { z } from 'zod';
3
+ import { ChatOpenAI } from '@langchain/openai';
4
+ import type { EntityAssociation } from '../memory/SmartMemoryManager';
5
+
6
+ const ResolveEntitiesSchema = z.object({
7
+ message: z.string().describe('The message containing entity references to resolve'),
8
+ entities: z.array(z.object({
9
+ entityId: z.string(),
10
+ entityName: z.string(),
11
+ entityType: z.string(),
12
+ })).describe('Available entities in memory'),
13
+ });
14
+
15
+ const ExtractEntitiesSchema = z.object({
16
+ response: z.string().describe('Agent response text to extract entities from'),
17
+ userMessage: z.string().describe('Original user message for context'),
18
+ });
19
+
20
+ export class ResolveEntitiesTool extends StructuredTool {
21
+ name = 'resolve_entities';
22
+ description = 'Resolves entity references like "the topic", "it", "that" to actual entity IDs';
23
+ schema = ResolveEntitiesSchema;
24
+
25
+ private llm: ChatOpenAI;
26
+
27
+ constructor(apiKey: string, modelName = 'gpt-4o-mini') {
28
+ super();
29
+ this.llm = new ChatOpenAI({
30
+ apiKey,
31
+ modelName,
32
+ temperature: 0,
33
+ });
34
+ }
35
+
36
+ async _call(input: z.infer<typeof ResolveEntitiesSchema>): Promise<string> {
37
+ const { message, entities } = input;
38
+
39
+ if (!entities || entities.length === 0) {
40
+ return message;
41
+ }
42
+
43
+ const byType = this.groupEntitiesByType(entities);
44
+ const context = this.buildEntityContext(byType);
45
+
46
+ const prompt = `Task: Replace entity references with IDs.
47
+
48
+ ${context}
49
+
50
+ Message: "${message}"
51
+
52
+ Rules:
53
+ - "the topic" or "that topic" → replace with most recent topic ID
54
+ - "the token" or "that token" → replace with most recent token ID
55
+ - "it" or "that" after action verb → replace with most recent entity ID
56
+ - "airdrop X" without token ID → add most recent token ID as first parameter
57
+ - Token operations without explicit token → use most recent token ID
58
+
59
+ Examples:
60
+ - "submit on the topic" → "submit on 0.0.6543472"
61
+ - "airdrop the token" → "airdrop 0.0.123456"
62
+ - "airdrop 10 to 0.0.5842697" → "airdrop 0.0.123456 10 to 0.0.5842697"
63
+ - "mint 100" → "mint 0.0.123456 100"
64
+
65
+ Return ONLY the resolved message:`;
66
+
67
+ try {
68
+ const response = await this.llm.invoke(prompt);
69
+ return (response.content as string).trim();
70
+ } catch (error) {
71
+ console.error('[ResolveEntitiesTool] Failed:', error);
72
+ return message;
73
+ }
74
+ }
75
+
76
+ private groupEntitiesByType(entities: EntityGroup): GroupedEntities {
77
+ return entities.reduce((acc, entity) => {
78
+ if (!acc[entity.entityType]) {
79
+ acc[entity.entityType] = [];
80
+ }
81
+ acc[entity.entityType].push(entity);
82
+ return acc;
83
+ }, {} as GroupedEntities);
84
+ }
85
+
86
+ private buildEntityContext(groupedEntities: GroupedEntities): string {
87
+ let context = 'Available entities:\n';
88
+ for (const [type, list] of Object.entries(groupedEntities)) {
89
+ const recent = list[0];
90
+ context += `- Most recent ${type}: "${recent.entityName}" = ${recent.entityId}\n`;
91
+ }
92
+ return context;
93
+ }
94
+ }
95
+
96
+ export class ExtractEntitiesTool extends StructuredTool {
97
+ name = 'extract_entities';
98
+ description = 'Extracts newly created entities from agent responses';
99
+ schema = ExtractEntitiesSchema;
100
+
101
+ private llm: ChatOpenAI;
102
+
103
+ constructor(apiKey: string, modelName = 'gpt-4o-mini') {
104
+ super();
105
+ this.llm = new ChatOpenAI({
106
+ apiKey,
107
+ modelName,
108
+ temperature: 0,
109
+ });
110
+ }
111
+
112
+ async _call(input: z.infer<typeof ExtractEntitiesSchema>): Promise<string> {
113
+ const { response, userMessage } = input;
114
+
115
+ const prompt = `Extract ONLY newly created entities from this response.
116
+
117
+ User asked: "${userMessage.substring(0, 200)}"
118
+ Response: ${response.substring(0, 3000)}
119
+
120
+ Look for:
121
+ - Success messages with new entity IDs
122
+ - Words like "created", "new", "successfully" followed by entity IDs
123
+
124
+ Return JSON array of created entities:
125
+ [{"id": "0.0.XXX", "name": "name", "type": "topic|token|account"}]
126
+
127
+ If none created, return: []
128
+
129
+ JSON:`;
130
+
131
+ try {
132
+ const llmResponse = await this.llm.invoke(prompt);
133
+ const content = llmResponse.content as string;
134
+ const match = content.match(/\[[\s\S]*?\]/);
135
+ if (match) {
136
+ return match[0];
137
+ }
138
+ return '[]';
139
+ } catch (error) {
140
+ console.error('[ExtractEntitiesTool] Failed:', error);
141
+ return '[]';
142
+ }
143
+ }
144
+ }
145
+
146
+ export function createEntityTools(apiKey: string, modelName = 'gpt-4o-mini'): {
147
+ resolveEntities: ResolveEntitiesTool;
148
+ extractEntities: ExtractEntitiesTool;
149
+ } {
150
+ return {
151
+ resolveEntities: new ResolveEntitiesTool(apiKey, modelName),
152
+ extractEntities: new ExtractEntitiesTool(apiKey, modelName),
153
+ };
154
+ }
155
+
156
+ interface EntityReference {
157
+ entityId: string;
158
+ entityName: string;
159
+ entityType: string;
160
+ }
161
+
162
+ type EntityGroup = EntityReference[];
163
+
164
+ type GroupedEntities = Record<string, EntityGroup>;
165
+
166
+ interface ExtractedEntity {
167
+ id: string;
168
+ name: string;
169
+ type: string;
170
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Inscription quote response from standards-agent-kit
3
+ */
4
+ export interface InscriptionQuote {
5
+ totalCostHbar: string;
6
+ validUntil: string;
7
+ breakdown: {
8
+ transfers: Array<{
9
+ type: string;
10
+ amount: string;
11
+ description: string;
12
+ }>;
13
+ };
14
+ }
15
+
16
+ /**
17
+ * Parameters for inscription tool calls with quote support
18
+ */
19
+ export interface InscriptionToolParams {
20
+ [key: string]: unknown;
21
+ quoteOnly?: boolean;
22
+ }
23
+
24
+ /**
25
+ * Result from inscription tool execution
26
+ */
27
+ export interface InscriptionResult {
28
+ success: boolean;
29
+ transactionId?: string;
30
+ message: string;
31
+ quote?: InscriptionQuote;
32
+ }
33
+
34
+ /**
35
+ * User confirmation status for inscription
36
+ */
37
+ export interface InscriptionConfirmation {
38
+ confirmed: boolean;
39
+ reason?: string;
40
+ }
@@ -1 +0,0 @@
1
- export { HbarTransferPlugin } from './HbarTransferPlugin';
@@ -1 +0,0 @@
1
- export { HbarTransferPlugin } from './HbarTransferPlugin';
@@ -1,66 +0,0 @@
1
- import {
2
- GenericPluginContext,
3
- HederaTool,
4
- BasePlugin,
5
- HederaAgentKit,
6
- } from 'hedera-agent-kit';
7
- import { TransferHbarTool } from './TransferHbarTool';
8
-
9
- export class HbarTransferPlugin extends BasePlugin {
10
- id = 'hbar-transfer';
11
- name = 'HBAR Transfer Plugin';
12
- description =
13
- 'HBAR transfer tool with proper decimal handling for multi-signature transactions';
14
- version = '1.0.0';
15
- author = 'Hashgraph Online';
16
- namespace = 'account';
17
-
18
- private tools: HederaTool[] = [];
19
-
20
- override async initialize(context: GenericPluginContext): Promise<void> {
21
- await super.initialize(context);
22
-
23
- const hederaKit = context.config.hederaKit as HederaAgentKit;
24
- if (!hederaKit) {
25
- this.context.logger.warn(
26
- 'HederaKit not found in context. HBAR transfer tools will not be available.'
27
- );
28
- return;
29
- }
30
-
31
- try {
32
- this.initializeTools();
33
-
34
- this.context.logger.info(
35
- 'HBAR Transfer Plugin initialized successfully'
36
- );
37
- } catch (error) {
38
- this.context.logger.error(
39
- 'Failed to initialize HBAR Transfer plugin:',
40
- error
41
- );
42
- }
43
- }
44
-
45
- private initializeTools(): void {
46
- const hederaKit = this.context.config.hederaKit as HederaAgentKit;
47
- if (!hederaKit) {
48
- throw new Error('HederaKit not found in context config');
49
- }
50
-
51
- this.tools = [
52
- new TransferHbarTool({
53
- hederaKit: hederaKit,
54
- logger: this.context.logger,
55
- }),
56
- ];
57
- }
58
-
59
- override getTools(): HederaTool[] {
60
- return this.tools;
61
- }
62
-
63
- async shutdown(): Promise<void> {
64
- this.tools = [];
65
- }
66
- }
@@ -1 +0,0 @@
1
- export { HbarTransferPlugin } from './HbarTransferPlugin';
File without changes