@hashgraphonline/conversational-agent 0.0.1 → 0.1.0
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/README.md +206 -38
- package/dist/cjs/agent-factory.d.ts +5 -0
- package/dist/cjs/base-agent.d.ts +95 -0
- package/dist/cjs/config/system-message.d.ts +1 -1
- package/dist/cjs/conversational-agent.d.ts +62 -5
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +8 -0
- package/dist/cjs/langchain-agent.d.ts +22 -0
- package/dist/cjs/mcp/MCPClientManager.d.ts +40 -0
- package/dist/cjs/mcp/adapters/langchain.d.ts +8 -0
- package/dist/cjs/mcp/helpers.d.ts +45 -0
- package/dist/cjs/mcp/types.d.ts +27 -0
- package/dist/cjs/plugins/hbar-transfer/AccountBuilder.d.ts +13 -0
- package/dist/cjs/plugins/hbar-transfer/HbarTransferPlugin.d.ts +15 -0
- package/dist/cjs/plugins/hbar-transfer/TransferHbarTool.d.ts +61 -0
- package/dist/cjs/plugins/hbar-transfer/index.d.ts +1 -0
- package/dist/cjs/plugins/hbar-transfer/types.d.ts +10 -0
- package/dist/cjs/plugins/index.d.ts +1 -0
- package/dist/cjs/providers.d.ts +48 -0
- package/dist/esm/index.js +18 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index10.js +22 -0
- package/dist/esm/index10.js.map +1 -0
- package/dist/esm/index11.js +104 -0
- package/dist/esm/index11.js.map +1 -0
- package/dist/esm/index12.js +36 -0
- package/dist/esm/index12.js.map +1 -0
- package/dist/esm/index13.js +16 -0
- package/dist/esm/index13.js.map +1 -0
- package/dist/esm/index14.js +127 -0
- package/dist/esm/index14.js.map +1 -0
- package/dist/esm/index15.js +132 -0
- package/dist/esm/index15.js.map +1 -0
- package/dist/esm/index16.js +95 -0
- package/dist/esm/index16.js.map +1 -0
- package/dist/esm/index5.js +42 -202
- package/dist/esm/index5.js.map +1 -1
- package/dist/esm/index6.js +295 -13
- package/dist/esm/index6.js.map +1 -1
- package/dist/esm/index7.js +87 -0
- package/dist/esm/index7.js.map +1 -0
- package/dist/esm/index8.js +255 -0
- package/dist/esm/index8.js.map +1 -0
- package/dist/esm/index9.js +18 -0
- package/dist/esm/index9.js.map +1 -0
- package/dist/types/agent-factory.d.ts +5 -0
- package/dist/types/base-agent.d.ts +95 -0
- package/dist/types/config/system-message.d.ts +1 -1
- package/dist/types/conversational-agent.d.ts +62 -5
- package/dist/types/index.d.ts +8 -0
- package/dist/types/langchain-agent.d.ts +22 -0
- package/dist/types/mcp/MCPClientManager.d.ts +40 -0
- package/dist/types/mcp/adapters/langchain.d.ts +8 -0
- package/dist/types/mcp/helpers.d.ts +45 -0
- package/dist/types/mcp/types.d.ts +27 -0
- package/dist/types/plugins/hbar-transfer/AccountBuilder.d.ts +13 -0
- package/dist/types/plugins/hbar-transfer/HbarTransferPlugin.d.ts +15 -0
- package/dist/types/plugins/hbar-transfer/TransferHbarTool.d.ts +61 -0
- package/dist/types/plugins/hbar-transfer/index.d.ts +1 -0
- package/dist/types/plugins/hbar-transfer/types.d.ts +10 -0
- package/dist/types/plugins/index.d.ts +1 -0
- package/dist/types/providers.d.ts +48 -0
- package/package.json +35 -27
- package/src/agent-factory.ts +21 -0
- package/src/base-agent.ts +222 -0
- package/src/config/system-message.ts +1 -1
- package/src/conversational-agent.ts +205 -103
- package/src/index.ts +24 -0
- package/src/langchain-agent.ts +333 -0
- package/src/mcp/MCPClientManager.ts +148 -0
- package/src/mcp/adapters/langchain.ts +185 -0
- package/src/mcp/helpers.ts +122 -0
- package/src/mcp/types.ts +29 -0
- package/src/plugins/hbar-transfer/AccountBuilder.ts +154 -0
- package/src/plugins/hbar-transfer/HbarTransferPlugin.ts +66 -0
- package/src/plugins/hbar-transfer/TransferHbarTool.ts +49 -0
- package/src/plugins/hbar-transfer/index.ts +1 -0
- package/src/plugins/hbar-transfer/types.ts +11 -0
- package/src/plugins/index.ts +2 -1
- package/src/providers.ts +82 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Tool } from '@modelcontextprotocol/sdk/types.js';
|
|
2
|
+
|
|
3
|
+
export interface MCPServerConfig {
|
|
4
|
+
name: string;
|
|
5
|
+
command: string;
|
|
6
|
+
args: string[];
|
|
7
|
+
env?: Record<string, string>;
|
|
8
|
+
transport?: 'stdio' | 'http' | 'websocket';
|
|
9
|
+
autoConnect?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Additional context to help the AI understand when to use this server's tools
|
|
12
|
+
*/
|
|
13
|
+
additionalContext?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Tool-specific descriptions to enhance or override default tool descriptions
|
|
16
|
+
*/
|
|
17
|
+
toolDescriptions?: Record<string, string>;
|
|
18
|
+
}
|
|
19
|
+
export interface MCPToolInfo extends Tool {
|
|
20
|
+
serverName: string;
|
|
21
|
+
}
|
|
22
|
+
export interface MCPConnectionStatus {
|
|
23
|
+
serverName: string;
|
|
24
|
+
connected: boolean;
|
|
25
|
+
error?: string;
|
|
26
|
+
tools: MCPToolInfo[];
|
|
27
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { HederaAgentKit, BaseServiceBuilder } from 'hedera-agent-kit';
|
|
2
|
+
import { HbarTransferParams } from './types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Custom AccountBuilder that properly handles HBAR decimal conversion
|
|
6
|
+
*/
|
|
7
|
+
export declare class AccountBuilder extends BaseServiceBuilder {
|
|
8
|
+
constructor(hederaKit: HederaAgentKit);
|
|
9
|
+
/**
|
|
10
|
+
* Transfers HBAR between accounts with proper decimal handling
|
|
11
|
+
*/
|
|
12
|
+
transferHbar(params: HbarTransferParams, isUserInitiated?: boolean): this;
|
|
13
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { GenericPluginContext, HederaTool, BasePlugin } from 'hedera-agent-kit';
|
|
2
|
+
|
|
3
|
+
export declare class HbarTransferPlugin extends BasePlugin {
|
|
4
|
+
id: string;
|
|
5
|
+
name: string;
|
|
6
|
+
description: string;
|
|
7
|
+
version: string;
|
|
8
|
+
author: string;
|
|
9
|
+
namespace: string;
|
|
10
|
+
private tools;
|
|
11
|
+
initialize(context: GenericPluginContext): Promise<void>;
|
|
12
|
+
private initializeTools;
|
|
13
|
+
getTools(): HederaTool[];
|
|
14
|
+
shutdown(): Promise<void>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { BaseHederaTransactionTool, BaseServiceBuilder } from 'hedera-agent-kit';
|
|
3
|
+
|
|
4
|
+
declare const TransferHbarZodSchemaCore: z.ZodObject<{
|
|
5
|
+
transfers: z.ZodArray<z.ZodObject<{
|
|
6
|
+
accountId: z.ZodString;
|
|
7
|
+
amount: z.ZodUnion<[z.ZodNumber, z.ZodString]>;
|
|
8
|
+
}, "strip", z.ZodTypeAny, {
|
|
9
|
+
accountId: string;
|
|
10
|
+
amount: string | number;
|
|
11
|
+
}, {
|
|
12
|
+
accountId: string;
|
|
13
|
+
amount: string | number;
|
|
14
|
+
}>, "many">;
|
|
15
|
+
memo: z.ZodOptional<z.ZodString>;
|
|
16
|
+
}, "strip", z.ZodTypeAny, {
|
|
17
|
+
transfers: {
|
|
18
|
+
accountId: string;
|
|
19
|
+
amount: string | number;
|
|
20
|
+
}[];
|
|
21
|
+
memo?: string | undefined;
|
|
22
|
+
}, {
|
|
23
|
+
transfers: {
|
|
24
|
+
accountId: string;
|
|
25
|
+
amount: string | number;
|
|
26
|
+
}[];
|
|
27
|
+
memo?: string | undefined;
|
|
28
|
+
}>;
|
|
29
|
+
export declare class TransferHbarTool extends BaseHederaTransactionTool<typeof TransferHbarZodSchemaCore> {
|
|
30
|
+
name: string;
|
|
31
|
+
description: string;
|
|
32
|
+
specificInputSchema: z.ZodObject<{
|
|
33
|
+
transfers: z.ZodArray<z.ZodObject<{
|
|
34
|
+
accountId: z.ZodString;
|
|
35
|
+
amount: z.ZodUnion<[z.ZodNumber, z.ZodString]>;
|
|
36
|
+
}, "strip", z.ZodTypeAny, {
|
|
37
|
+
accountId: string;
|
|
38
|
+
amount: string | number;
|
|
39
|
+
}, {
|
|
40
|
+
accountId: string;
|
|
41
|
+
amount: string | number;
|
|
42
|
+
}>, "many">;
|
|
43
|
+
memo: z.ZodOptional<z.ZodString>;
|
|
44
|
+
}, "strip", z.ZodTypeAny, {
|
|
45
|
+
transfers: {
|
|
46
|
+
accountId: string;
|
|
47
|
+
amount: string | number;
|
|
48
|
+
}[];
|
|
49
|
+
memo?: string | undefined;
|
|
50
|
+
}, {
|
|
51
|
+
transfers: {
|
|
52
|
+
accountId: string;
|
|
53
|
+
amount: string | number;
|
|
54
|
+
}[];
|
|
55
|
+
memo?: string | undefined;
|
|
56
|
+
}>;
|
|
57
|
+
namespace: string;
|
|
58
|
+
protected getServiceBuilder(): BaseServiceBuilder;
|
|
59
|
+
protected callBuilderMethod(builder: BaseServiceBuilder, specificArgs: z.infer<typeof TransferHbarZodSchemaCore>): Promise<void>;
|
|
60
|
+
}
|
|
61
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { HbarTransferPlugin } from './HbarTransferPlugin';
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { BaseLanguageModelCallOptions } from '@langchain/core/language_models/base';
|
|
2
|
+
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Framework-agnostic AI provider interface for multi-framework support
|
|
6
|
+
*/
|
|
7
|
+
export interface AIProvider {
|
|
8
|
+
/**
|
|
9
|
+
* Generate a response from the AI model
|
|
10
|
+
*/
|
|
11
|
+
generate(prompt: string, options?: BaseLanguageModelCallOptions): Promise<string>;
|
|
12
|
+
/**
|
|
13
|
+
* Stream a response from the AI model
|
|
14
|
+
*/
|
|
15
|
+
stream?(prompt: string, options?: BaseLanguageModelCallOptions): AsyncGenerator<string>;
|
|
16
|
+
/**
|
|
17
|
+
* Get the underlying model if available
|
|
18
|
+
*/
|
|
19
|
+
getModel?(): BaseChatModel | unknown;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* LangChain AI provider implementation
|
|
23
|
+
*/
|
|
24
|
+
export declare class LangChainProvider implements AIProvider {
|
|
25
|
+
private model;
|
|
26
|
+
constructor(model: BaseChatModel);
|
|
27
|
+
generate(prompt: string, options?: BaseLanguageModelCallOptions): Promise<string>;
|
|
28
|
+
stream(prompt: string, options?: BaseLanguageModelCallOptions): AsyncGenerator<string>;
|
|
29
|
+
getModel(): BaseChatModel;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Vercel AI SDK provider interface (forward-thinking)
|
|
33
|
+
*/
|
|
34
|
+
export interface VercelAIProvider extends AIProvider {
|
|
35
|
+
/**
|
|
36
|
+
* Use Vercel AI SDK's streamText function
|
|
37
|
+
*/
|
|
38
|
+
streamText?(prompt: string, options?: unknown): Promise<unknown>;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* BAML provider interface (forward-thinking)
|
|
42
|
+
*/
|
|
43
|
+
export interface BAMLProvider extends AIProvider {
|
|
44
|
+
/**
|
|
45
|
+
* Execute a BAML function
|
|
46
|
+
*/
|
|
47
|
+
executeFunction?(name: string, args: Record<string, unknown>): Promise<unknown>;
|
|
48
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hashgraphonline/conversational-agent",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/cjs/index.cjs",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -23,6 +23,29 @@
|
|
|
23
23
|
"LICENSE",
|
|
24
24
|
"README.md"
|
|
25
25
|
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"test": "vitest run",
|
|
28
|
+
"test:integration": "vitest run tests/integration",
|
|
29
|
+
"clean": "rimraf dist",
|
|
30
|
+
"build:es": "BUILD_FORMAT=es vite build",
|
|
31
|
+
"build:cjs": "BUILD_FORMAT=cjs vite build",
|
|
32
|
+
"build:umd": "BUILD_FORMAT=umd vite build",
|
|
33
|
+
"build": "pnpm run clean && pnpm run build:es && pnpm run build:cjs",
|
|
34
|
+
"prepublishOnly": "pnpm run build",
|
|
35
|
+
"release": "pnpm publish --access public",
|
|
36
|
+
"release:canary": "pnpm run prepublishOnly && pnpm publish --tag canary --access public",
|
|
37
|
+
"version:canary": "pnpm version prerelease --preid canary --no-git-tag-version",
|
|
38
|
+
"publish:canary": "pnpm run version:canary && pnpm run release:canary",
|
|
39
|
+
"lint": "eslint .",
|
|
40
|
+
"lint:fix": "eslint . --fix",
|
|
41
|
+
"typecheck": "tsc --noEmit",
|
|
42
|
+
"cli:build": "pnpm run build && cd cli && pnpm install && pnpm build",
|
|
43
|
+
"cli": "tsx cli/scripts/run-cli.ts",
|
|
44
|
+
"cli:dev": "cd cli && pnpm dev",
|
|
45
|
+
"example:mcp-filesystem": "tsx examples/mcp-filesystem-demo.ts",
|
|
46
|
+
"example:mcp-multi": "tsx examples/mcp-multi-server-demo.ts",
|
|
47
|
+
"postinstall": "cd cli && pnpm install --silent 2>/dev/null || true"
|
|
48
|
+
},
|
|
26
49
|
"keywords": [
|
|
27
50
|
"hedera",
|
|
28
51
|
"hashgraph",
|
|
@@ -47,8 +70,8 @@
|
|
|
47
70
|
"@typescript-eslint/eslint-plugin": "^8.38.0",
|
|
48
71
|
"@typescript-eslint/parser": "^8.38.0",
|
|
49
72
|
"dotenv": "^17.2.1",
|
|
50
|
-
"eslint": "^
|
|
51
|
-
"eslint-config-next": "
|
|
73
|
+
"eslint": "^9.3",
|
|
74
|
+
"eslint-config-next": "15.4.0",
|
|
52
75
|
"eslint-plugin-sonarjs": "^3.0.4",
|
|
53
76
|
"rimraf": "^6.0.1",
|
|
54
77
|
"rollup-plugin-polyfill-node": "^0.13.0",
|
|
@@ -63,11 +86,15 @@
|
|
|
63
86
|
},
|
|
64
87
|
"dependencies": {
|
|
65
88
|
"@hashgraph/sdk": "^2.69.0",
|
|
66
|
-
"@hashgraphonline/standards-agent-kit": "
|
|
67
|
-
"@hashgraphonline/standards-sdk": "^0.0.
|
|
89
|
+
"@hashgraphonline/standards-agent-kit": "0.2.105",
|
|
90
|
+
"@hashgraphonline/standards-sdk": "^0.0.156",
|
|
68
91
|
"@langchain/core": "^0.3.66",
|
|
92
|
+
"@langchain/openai": "^0.6.3",
|
|
93
|
+
"@modelcontextprotocol/sdk": "^1.17.0",
|
|
94
|
+
"bignumber.js": "^9.3.1",
|
|
69
95
|
"hedera-agent-kit": "^2.0.3",
|
|
70
|
-
"
|
|
96
|
+
"langchain": "^0.3.3",
|
|
97
|
+
"zod": "^3.23.8"
|
|
71
98
|
},
|
|
72
99
|
"repository": {
|
|
73
100
|
"type": "git",
|
|
@@ -80,24 +107,5 @@
|
|
|
80
107
|
"url": "https://github.com/hashgraph-online/conversational-agent/issues"
|
|
81
108
|
},
|
|
82
109
|
"homepage": "https://github.com/hashgraph-online/conversational-agent#readme",
|
|
83
|
-
"
|
|
84
|
-
|
|
85
|
-
"test:integration": "vitest run tests/integration",
|
|
86
|
-
"clean": "rimraf dist",
|
|
87
|
-
"build:es": "BUILD_FORMAT=es vite build",
|
|
88
|
-
"build:cjs": "BUILD_FORMAT=cjs vite build",
|
|
89
|
-
"build:umd": "BUILD_FORMAT=umd vite build",
|
|
90
|
-
"build": "pnpm run clean && pnpm run build:es && pnpm run build:cjs",
|
|
91
|
-
"release": "pnpm publish --access public",
|
|
92
|
-
"release:canary": "pnpm run prepublishOnly && pnpm publish --tag canary --access public",
|
|
93
|
-
"version:canary": "pnpm version prerelease --preid canary --no-git-tag-version",
|
|
94
|
-
"publish:canary": "pnpm run version:canary && pnpm run release:canary",
|
|
95
|
-
"lint": "eslint .",
|
|
96
|
-
"lint:fix": "eslint . --fix",
|
|
97
|
-
"typecheck": "tsc --noEmit",
|
|
98
|
-
"cli:build": "pnpm run build && cd cli && pnpm install && pnpm build",
|
|
99
|
-
"cli": "tsx cli/scripts/run-cli.ts",
|
|
100
|
-
"cli:dev": "cd cli && pnpm dev",
|
|
101
|
-
"postinstall": "cd cli && pnpm install --silent 2>/dev/null || true"
|
|
102
|
-
}
|
|
103
|
-
}
|
|
110
|
+
"packageManager": "pnpm@10.11.1+sha512.e519b9f7639869dc8d5c3c5dfef73b3f091094b0a006d7317353c72b124e80e1afd429732e28705ad6bfa1ee879c1fce46c128ccebd3192101f43dd67c667912"
|
|
111
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { BaseAgent, type HederaAgentConfiguration } from './base-agent';
|
|
2
|
+
import { LangChainAgent } from './langchain-agent';
|
|
3
|
+
|
|
4
|
+
export function createAgent(
|
|
5
|
+
config: HederaAgentConfiguration & {
|
|
6
|
+
framework?: 'langchain' | 'vercel' | 'baml';
|
|
7
|
+
}
|
|
8
|
+
): BaseAgent {
|
|
9
|
+
const framework = config.framework || 'langchain';
|
|
10
|
+
|
|
11
|
+
switch (framework) {
|
|
12
|
+
case 'langchain':
|
|
13
|
+
return new LangChainAgent(config);
|
|
14
|
+
case 'vercel':
|
|
15
|
+
throw new Error('Vercel AI SDK support coming soon');
|
|
16
|
+
case 'baml':
|
|
17
|
+
throw new Error('BAML support coming soon');
|
|
18
|
+
default:
|
|
19
|
+
throw new Error(`Unknown framework: ${framework}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import type { BaseMessage } from '@langchain/core/messages';
|
|
2
|
+
import type { StructuredTool } from '@langchain/core/tools';
|
|
3
|
+
import type { TransactionReceipt } from '@hashgraph/sdk';
|
|
4
|
+
import {
|
|
5
|
+
HederaAgentKit,
|
|
6
|
+
ServerSigner,
|
|
7
|
+
TokenUsageCallbackHandler,
|
|
8
|
+
TokenUsage,
|
|
9
|
+
BasePlugin,
|
|
10
|
+
} from 'hedera-agent-kit';
|
|
11
|
+
import type { CostCalculation } from 'hedera-agent-kit';
|
|
12
|
+
import type { AIProvider, VercelAIProvider, BAMLProvider } from './providers';
|
|
13
|
+
import { Logger } from '@hashgraphonline/standards-sdk';
|
|
14
|
+
import type { MCPServerConfig } from './mcp/types';
|
|
15
|
+
|
|
16
|
+
export interface ToolFilterConfig {
|
|
17
|
+
namespaceWhitelist?: string[];
|
|
18
|
+
toolBlacklist?: string[];
|
|
19
|
+
toolPredicate?: (tool: StructuredTool) => boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type ExecutionMode = 'direct' | 'bytes';
|
|
23
|
+
export type OperationalMode = 'autonomous' | 'returnBytes';
|
|
24
|
+
|
|
25
|
+
export interface HederaAgentConfiguration {
|
|
26
|
+
signer: ServerSigner;
|
|
27
|
+
execution?: {
|
|
28
|
+
mode?: ExecutionMode;
|
|
29
|
+
operationalMode?: OperationalMode;
|
|
30
|
+
userAccountId?: string;
|
|
31
|
+
scheduleUserTransactions?: boolean;
|
|
32
|
+
scheduleUserTransactionsInBytesMode?: boolean;
|
|
33
|
+
};
|
|
34
|
+
ai?: {
|
|
35
|
+
provider?: AIProvider;
|
|
36
|
+
llm?: unknown;
|
|
37
|
+
apiKey?: string;
|
|
38
|
+
modelName?: string;
|
|
39
|
+
temperature?: number;
|
|
40
|
+
};
|
|
41
|
+
filtering?: ToolFilterConfig;
|
|
42
|
+
messaging?: {
|
|
43
|
+
systemPreamble?: string;
|
|
44
|
+
systemPostamble?: string;
|
|
45
|
+
conciseMode?: boolean;
|
|
46
|
+
};
|
|
47
|
+
extensions?: {
|
|
48
|
+
plugins?: BasePlugin[];
|
|
49
|
+
mirrorConfig?: Record<string, unknown>;
|
|
50
|
+
modelCapability?: string;
|
|
51
|
+
};
|
|
52
|
+
mcp?: {
|
|
53
|
+
servers?: MCPServerConfig[];
|
|
54
|
+
autoConnect?: boolean;
|
|
55
|
+
};
|
|
56
|
+
debug?: {
|
|
57
|
+
verbose?: boolean;
|
|
58
|
+
silent?: boolean;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface ConversationContext {
|
|
63
|
+
messages: BaseMessage[];
|
|
64
|
+
metadata?: Record<string, unknown>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface ChatResponse {
|
|
68
|
+
output: string;
|
|
69
|
+
message?: string;
|
|
70
|
+
transactionBytes?: string;
|
|
71
|
+
receipt?: TransactionReceipt | object;
|
|
72
|
+
scheduleId?: string;
|
|
73
|
+
transactionId?: string;
|
|
74
|
+
notes?: string[];
|
|
75
|
+
error?: string;
|
|
76
|
+
intermediateSteps?: unknown;
|
|
77
|
+
rawToolOutput?: unknown;
|
|
78
|
+
tokenUsage?: TokenUsage;
|
|
79
|
+
cost?: CostCalculation;
|
|
80
|
+
metadata?: Record<string, unknown>;
|
|
81
|
+
[key: string]: unknown;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface UsageStats extends TokenUsage {
|
|
85
|
+
cost: CostCalculation;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export abstract class BaseAgent {
|
|
89
|
+
protected logger: Logger;
|
|
90
|
+
protected agentKit: HederaAgentKit | undefined;
|
|
91
|
+
protected tools: StructuredTool[] = [];
|
|
92
|
+
protected initialized = false;
|
|
93
|
+
protected tokenTracker: TokenUsageCallbackHandler | undefined;
|
|
94
|
+
|
|
95
|
+
constructor(protected config: HederaAgentConfiguration) {
|
|
96
|
+
this.logger = new Logger({
|
|
97
|
+
module: 'BaseAgent',
|
|
98
|
+
silent: config.debug?.silent || false,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
abstract boot(): Promise<void>;
|
|
103
|
+
abstract chat(
|
|
104
|
+
message: string,
|
|
105
|
+
context?: ConversationContext
|
|
106
|
+
): Promise<ChatResponse>;
|
|
107
|
+
abstract shutdown(): Promise<void>;
|
|
108
|
+
abstract switchMode(mode: OperationalMode): void;
|
|
109
|
+
abstract getUsageStats(): UsageStats;
|
|
110
|
+
abstract getUsageLog(): UsageStats[];
|
|
111
|
+
abstract clearUsageStats(): void;
|
|
112
|
+
|
|
113
|
+
getCore(): HederaAgentKit | undefined {
|
|
114
|
+
return this.agentKit;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
protected filterTools(
|
|
118
|
+
tools: StructuredTool[]
|
|
119
|
+
): StructuredTool[] {
|
|
120
|
+
let filtered = [...tools];
|
|
121
|
+
const filter = this.config.filtering;
|
|
122
|
+
if (!filter) return filtered;
|
|
123
|
+
|
|
124
|
+
if (filter.namespaceWhitelist?.length) {
|
|
125
|
+
filtered = filtered.filter((tool) => {
|
|
126
|
+
const namespace = (tool as StructuredTool & { namespace?: string })
|
|
127
|
+
.namespace;
|
|
128
|
+
return !namespace || filter.namespaceWhitelist!.includes(namespace);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (filter.toolBlacklist?.length) {
|
|
133
|
+
filtered = filtered.filter(
|
|
134
|
+
(tool) => !filter.toolBlacklist!.includes(tool.name)
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (filter.toolPredicate) {
|
|
139
|
+
filtered = filtered.filter(filter.toolPredicate);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
this.logger.debug(`Filtered tools: ${tools.length} → ${filtered.length}`);
|
|
143
|
+
return filtered;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
protected buildSystemPrompt(): string {
|
|
147
|
+
const parts: string[] = [];
|
|
148
|
+
const operatorId = this.config.signer.getAccountId().toString();
|
|
149
|
+
const userAccId = this.config.execution?.userAccountId;
|
|
150
|
+
|
|
151
|
+
if (this.config.messaging?.systemPreamble) {
|
|
152
|
+
parts.push(this.config.messaging.systemPreamble);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
parts.push(
|
|
156
|
+
`You are a helpful Hedera assistant. Your primary operator account is ${operatorId}. ` +
|
|
157
|
+
`You have tools to interact with the Hedera network. ` +
|
|
158
|
+
`When using any tool, provide all necessary parameters as defined by that tool's schema and description.`
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
if (userAccId) {
|
|
162
|
+
parts.push(
|
|
163
|
+
`The user you are assisting has a personal Hedera account ID: ${userAccId}. ` +
|
|
164
|
+
`IMPORTANT: When the user says things like "I want to send HBAR" or "transfer my tokens", you MUST use ${userAccId} as the sender/from account. ` +
|
|
165
|
+
`For example, if user says "I want to send 2 HBAR to 0.0.800", you must set up a transfer where ${userAccId} sends the HBAR, not your operator account.`
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const operationalMode =
|
|
170
|
+
this.config.execution?.operationalMode || 'returnBytes';
|
|
171
|
+
if (operationalMode === 'autonomous') {
|
|
172
|
+
parts.push(
|
|
173
|
+
`\nOPERATIONAL MODE: 'autonomous'. Your goal is to execute transactions directly using your tools. ` +
|
|
174
|
+
`Your account ${operatorId} will be the payer for these transactions. ` +
|
|
175
|
+
`Even if the user's account (${
|
|
176
|
+
userAccId || 'a specified account'
|
|
177
|
+
}) is the actor in the transaction body (e.g., sender of HBAR), ` +
|
|
178
|
+
`you (the agent with operator ${operatorId}) are still executing and paying. For HBAR transfers, ensure the amounts in the 'transfers' array sum to zero (as per tool schema), balancing with your operator account if necessary.`
|
|
179
|
+
);
|
|
180
|
+
} else {
|
|
181
|
+
if (
|
|
182
|
+
this.config.execution?.scheduleUserTransactionsInBytesMode &&
|
|
183
|
+
userAccId
|
|
184
|
+
) {
|
|
185
|
+
parts.push(
|
|
186
|
+
`\nOPERATIONAL MODE: 'returnBytes' with scheduled transactions for user actions. ` +
|
|
187
|
+
`When a user asks for a transaction to be prepared (e.g., creating a token, topic, transferring assets for them to sign, etc), ` +
|
|
188
|
+
`you MUST default to creating a Scheduled Transaction using the appropriate tool with the metaOption 'schedule: true'. ` +
|
|
189
|
+
`The user (with account ID ${userAccId}) will be the one to ultimately pay for and (if needed) sign the inner transaction. ` +
|
|
190
|
+
`Your operator account (${operatorId}) will pay for creating the schedule entity itself. ` +
|
|
191
|
+
`You MUST return the ScheduleId and details of the scheduled operation in a structured JSON format with these fields: success, op, schedule_id, description, payer_account_id_scheduled_tx, and scheduled_transaction_details.`
|
|
192
|
+
);
|
|
193
|
+
} else {
|
|
194
|
+
parts.push(
|
|
195
|
+
`\nOPERATIONAL MODE: 'returnBytes'. Your goal is to provide transaction bytes directly. ` +
|
|
196
|
+
`When a user asks for a transaction to be prepared (e.g., for them to sign, or for scheduling without the default scheduling flow), ` +
|
|
197
|
+
`you MUST call the appropriate tool. If you want raw bytes for the user to sign for their own account ${
|
|
198
|
+
userAccId || 'if specified'
|
|
199
|
+
}, ensure the tool constructs the transaction body accordingly and use metaOption 'returnBytes: true' if available, or ensure the builder is configured for the user.`
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (this.config.messaging?.conciseMode !== false) {
|
|
205
|
+
parts.push(
|
|
206
|
+
'\nAlways be concise. If the tool provides a JSON string as its primary output (especially in returnBytes mode), make your accompanying text brief. If the tool does not provide JSON output or an error occurs, your narrative becomes primary; if notes were generated by the tool in such cases, append them to your textual response.'
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (this.config.messaging?.systemPostamble) {
|
|
211
|
+
parts.push(this.config.messaging.systemPostamble);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return parts.join('\n');
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
isReady(): boolean {
|
|
218
|
+
return this.initialized;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export type { AIProvider, VercelAIProvider, BAMLProvider };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export const
|
|
1
|
+
export const getSystemMessage = (accountId: string): string => `You are a helpful assistant managing Hashgraph Online HCS-10 connections, messages, HCS-2 registries, and content inscription.
|
|
2
2
|
|
|
3
3
|
You have access to tools for:
|
|
4
4
|
- HCS-10: registering agents, finding registered agents, initiating connections, listing active connections, sending messages over connections, and checking for new messages
|