Skip to content

Commit c54a281

Browse files
committed
feat(cli): add agent and agent invoke the tool call
1 parent a57e545 commit c54a281

File tree

7 files changed

+769
-74
lines changed

7 files changed

+769
-74
lines changed

packages/cli/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pak agent chat my-agent
5252
### 4. Execute Commands
5353

5454
```bash
55-
pak agent run my-agent "check my balance"
55+
pak agent run check-balance -a <your agent name>
5656
```
5757

5858
## Commands

packages/cli/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@
5757
"table": "^6.8.1",
5858
"boxen": "^7.1.1",
5959
"conf": "^11.0.2",
60-
"readline": "^1.3.0"
60+
"readline": "^1.3.0",
61+
"@langchain/core": "^0.3.40",
62+
"@langchain/ollama": "^0.2.2",
63+
"langchain": "^0.1.21"
6164
},
6265
"devDependencies": {
6366
"@babel/plugin-syntax-import-attributes": "^7.26.9",

packages/cli/src/commands/agent/chat.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import { configManager } from '../../core/config/manager.js';
77
import { logger } from '../../utils/logger.js';
88
import { AgentChatOptions, CLIError } from '../../types/commands.js';
99
import { AgentMetadata, ChatMessage } from '../../types/agent.js';
10+
import { PolkadotCLIAgent } from '../../core/agent/polkadotAgent.js';
1011

1112
export const chatCommand = new Command('chat')
1213
.description('Start an interactive chat session with an AI agent')
1314
.argument('<name>', 'Agent name')
15+
.option('-i, --interactive', 'Interactive mode', true)
1416
.option('--history', 'Show chat history', false)
1517
.option('--save', 'Save chat history', true)
1618
.option('--timeout <timeout>', 'Request timeout in seconds', '30')
@@ -150,7 +152,6 @@ async function runInteractiveChat(agent: AgentMetadata, options: AgentChatOption
150152
try {
151153
// Show typing indicator
152154
const typingInterval = showTypingIndicator();
153-
154155
// Send message to agent
155156
const response = await sendMessageToAgent(agent, message, options);
156157

@@ -241,20 +242,19 @@ function showTypingIndicator(): NodeJS.Timeout {
241242
}
242243

243244
async function sendMessageToAgent(agent: AgentMetadata, message: string, options: AgentChatOptions): Promise<string> {
244-
// This is a placeholder implementation
245-
// In a real implementation, this would integrate with the actual LLM providers
246-
247-
// Simulate API call delay
248-
await new Promise(resolve => setTimeout(resolve, 1000 + Math.random() * 2000));
249-
250-
// Mock response based on agent configuration
251-
const responses = [
252-
`I understand you want to ${message.toLowerCase()}. As a ${agent.provider} agent with ${agent.tools.join(', ')} tools, I can help you with Polkadot operations.`,
253-
`Let me process your request: "${message}". I have access to ${agent.tools.length} tools to assist you.`,
254-
`Based on your message about "${message}", I can help you with various Polkadot operations using my available tools.`,
255-
];
256-
257-
return responses[Math.floor(Math.random() * responses.length)] || 'I\'m sorry, I don\'t have the information to answer that question.';
245+
try {
246+
logger.info("Go to here again in send message to agent ");
247+
// Initialize the Polkadot CLI Agent
248+
const cliAgent = new PolkadotCLIAgent(agent);
249+
await cliAgent.initialize();
250+
// Send the message to the agent
251+
const response = await cliAgent.ask(message);
252+
253+
return response;
254+
} catch (error) {
255+
logger.error(`Agent communication failed: ${error instanceof Error ? error.message : String(error)}`);
256+
return `I encountered an error processing your request: ${error instanceof Error ? error.message : String(error)}. Please try again or check your configuration.`;
257+
}
258258
}
259259

260260
async function saveChatMessage(agentName: string, role: 'user' | 'assistant', content: string): Promise<void> {

packages/cli/src/commands/agent/create.ts

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { AgentCreateOptions, CLIError } from '../../types/commands.js';
99
import {
1010
AVAILABLE_TOOLS,
1111
TOOL_DESCRIPTIONS,
12-
DEFAULT_SYSTEM_PROMPTS,
12+
DEFAULT_SYSTEM_PROMPT,
1313
AgentMetadata,
1414
PolkadotAgentConfig
1515
} from '../../types/agent.js';
@@ -59,7 +59,6 @@ async function createAgent(name: string, options: AgentCreateOptions): Promise<v
5959
logger.success(chalk.green(`✅ Agent "${name}" created successfully!`));
6060
logger.info(chalk.cyan('\nNext steps:'));
6161
logger.info(chalk.white(` pak agent chat ${name}`));
62-
logger.info(chalk.white(` pak agent run ${name} "check my balance"`));
6362
}
6463

6564
function isValidAgentName(name: string): boolean {
@@ -89,7 +88,7 @@ async function getAgentConfig(name: string, options: AgentCreateOptions) {
8988

9089
function getDefaultModel(provider: string): string {
9190
const config = configManager.getConfig();
92-
91+
logger.debug("Go to getDefaultModel");
9392
if (provider === 'ollama') {
9493
return config.llm.ollama?.defaultModel || 'llama2';
9594
} else if (provider === 'openai') {
@@ -198,6 +197,7 @@ async function interactiveAgentSetup(name: string, options: AgentCreateOptions)
198197

199198
// Get available models for the selected provider
200199
const availableModels = await getAvailableModels(provider);
200+
logger.debug("Available Models:", availableModels);
201201

202202
const modelQuestions = [
203203
{
@@ -222,41 +222,20 @@ async function interactiveAgentSetup(name: string, options: AgentCreateOptions)
222222
}
223223
return true;
224224
}
225-
},
226-
{
227-
type: 'list',
228-
name: 'systemPromptType',
229-
message: 'Choose system prompt template:',
230-
choices: [
231-
{ name: 'General - All-purpose Polkadot assistant', value: 'general' },
232-
{ name: 'Trading - DeFi and trading focused', value: 'trading' },
233-
{ name: 'Staking - Staking and governance focused', value: 'staking' },
234-
{ name: 'Custom - I\'ll provide my own', value: 'custom' },
235-
],
236-
default: 'general'
237225
}
238226
];
239227

240228
const answers = await inquirer.prompt(modelQuestions);
241229

242-
let systemPrompt = DEFAULT_SYSTEM_PROMPTS[answers.systemPromptType as keyof typeof DEFAULT_SYSTEM_PROMPTS];
230+
let systemPrompt = DEFAULT_SYSTEM_PROMPT
243231

244-
if (answers.systemPromptType === 'custom') {
245-
const customPrompt = await inquirer.prompt([{
246-
type: 'editor',
247-
name: 'systemPrompt',
248-
message: 'Enter your custom system prompt:',
249-
default: DEFAULT_SYSTEM_PROMPTS.general
250-
}]);
251-
systemPrompt = customPrompt.systemPrompt;
252-
}
253232

254233
const finalQuestions = [
255234
{
256235
type: 'input',
257236
name: 'description',
258237
message: 'Agent description:',
259-
default: options.description || `${answers.systemPromptType} AI agent for Polkadot operations`
238+
default: options.description || `AI agent for Polkadot operations`
260239
},
261240
{
262241
type: 'number',

0 commit comments

Comments
 (0)