forked from themanojdesai/python-a2a
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
599 lines (488 loc) · 20.3 KB
/
Copy pathcli.py
File metadata and controls
599 lines (488 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#!/usr/bin/env python
"""
Command Line Interface for the Agent Flow workflow system.
This module provides a CLI for creating, managing, and executing workflows.
"""
import os
import sys
import json
import argparse
import logging
from datetime import datetime
from typing import Dict, List, Optional, Any, Tuple, Union
from .models.workflow import (
Workflow, WorkflowNode, WorkflowEdge, NodeType, EdgeType
)
from .models.agent import AgentRegistry, AgentDefinition, AgentSource, AgentStatus
from .models.tool import ToolRegistry, ToolDefinition, ToolSource, ToolStatus
from .engine.executor import WorkflowExecutor
from .storage.workflow_storage import FileWorkflowStorage, SqliteWorkflowStorage
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger("AgentFlowCLI")
class AgentFlowCLI:
"""Command Line Interface for Agent Flow."""
def __init__(self, storage_dir: Optional[str] = None):
"""
Initialize the CLI.
Args:
storage_dir: Directory for storing data (defaults to ~/.agent_flow)
"""
# Set up storage directory
if not storage_dir:
home_dir = os.path.expanduser("~")
storage_dir = os.path.join(home_dir, ".agent_flow")
self.storage_dir = storage_dir
os.makedirs(storage_dir, exist_ok=True)
# Initialize registries
self.agent_registry = AgentRegistry()
self.tool_registry = ToolRegistry()
# Initialize storage
self.workflow_storage = FileWorkflowStorage(
os.path.join(storage_dir, "workflows")
)
# Initialize executor
self.workflow_executor = WorkflowExecutor(
self.agent_registry, self.tool_registry
)
def run(self, args=None):
"""
Run the CLI with the given arguments.
Args:
args: Command line arguments (defaults to sys.argv[1:])
"""
parser = self._create_parser()
parsed_args = parser.parse_args(args)
if not parsed_args.command:
parser.print_help()
return
# Run the appropriate command
command_func = parsed_args.func
command_func(parsed_args)
def _create_parser(self):
"""Create the argument parser for the CLI."""
parser = argparse.ArgumentParser(
description="Agent Flow - A2A-based Workflow System"
)
subparsers = parser.add_subparsers(dest="command")
# Agent commands
agent_parser = subparsers.add_parser(
"agent", help="Manage agent definitions"
)
agent_subparsers = agent_parser.add_subparsers(dest="agent_command")
# agent list
agent_list_parser = agent_subparsers.add_parser(
"list", help="List available agents"
)
agent_list_parser.set_defaults(func=self.agent_list)
# agent add
agent_add_parser = agent_subparsers.add_parser(
"add", help="Add an agent"
)
agent_add_parser.add_argument("name", help="Name of the agent")
agent_add_parser.add_argument("url", help="URL of the agent")
agent_add_parser.add_argument(
"--description", "-d",
help="Description of the agent"
)
agent_add_parser.add_argument(
"--type", "-t",
choices=["a2a", "llm", "custom"],
default="a2a",
help="Type of the agent"
)
agent_add_parser.set_defaults(func=self.agent_add)
# agent remove
agent_remove_parser = agent_subparsers.add_parser(
"remove", help="Remove an agent"
)
agent_remove_parser.add_argument("id", help="ID of the agent to remove")
agent_remove_parser.set_defaults(func=self.agent_remove)
# agent show
agent_show_parser = agent_subparsers.add_parser(
"show", help="Show agent details"
)
agent_show_parser.add_argument("id", help="ID of the agent to show")
agent_show_parser.set_defaults(func=self.agent_show)
# agent discover
agent_discover_parser = agent_subparsers.add_parser(
"discover", help="Discover agents"
)
agent_discover_parser.add_argument(
"--base-url", "-b",
default="http://localhost",
help="Base URL for discovery"
)
agent_discover_parser.add_argument(
"--port-min", "-p",
type=int,
default=8000,
help="Minimum port number"
)
agent_discover_parser.add_argument(
"--port-max", "-P",
type=int,
default=9000,
help="Maximum port number"
)
agent_discover_parser.set_defaults(func=self.agent_discover)
# Tool commands
tool_parser = subparsers.add_parser(
"tool", help="Manage tool definitions"
)
tool_subparsers = tool_parser.add_subparsers(dest="tool_command")
# tool list
tool_list_parser = tool_subparsers.add_parser(
"list", help="List available tools"
)
tool_list_parser.set_defaults(func=self.tool_list)
# tool discover
tool_discover_parser = tool_subparsers.add_parser(
"discover", help="Discover tools from an MCP server"
)
tool_discover_parser.add_argument(
"url", help="URL of the MCP server"
)
tool_discover_parser.set_defaults(func=self.tool_discover)
# Workflow commands
workflow_parser = subparsers.add_parser(
"workflow", help="Manage workflows"
)
workflow_subparsers = workflow_parser.add_subparsers(dest="workflow_command")
# workflow list
workflow_list_parser = workflow_subparsers.add_parser(
"list", help="List workflows"
)
workflow_list_parser.set_defaults(func=self.workflow_list)
# workflow show
workflow_show_parser = workflow_subparsers.add_parser(
"show", help="Show workflow details"
)
workflow_show_parser.add_argument("id", help="ID of the workflow to show")
workflow_show_parser.set_defaults(func=self.workflow_show)
# workflow create
workflow_create_parser = workflow_subparsers.add_parser(
"create", help="Create a new workflow"
)
workflow_create_parser.add_argument(
"--file", "-f",
help="JSON file containing workflow definition"
)
workflow_create_parser.add_argument(
"--name", "-n",
help="Name of the workflow"
)
workflow_create_parser.add_argument(
"--description", "-d",
help="Description of the workflow"
)
workflow_create_parser.set_defaults(func=self.workflow_create)
# workflow delete
workflow_delete_parser = workflow_subparsers.add_parser(
"delete", help="Delete a workflow"
)
workflow_delete_parser.add_argument("id", help="ID of the workflow to delete")
workflow_delete_parser.set_defaults(func=self.workflow_delete)
# workflow run
workflow_run_parser = workflow_subparsers.add_parser(
"run", help="Run a workflow"
)
workflow_run_parser.add_argument("id", help="ID of the workflow to run")
workflow_run_parser.add_argument(
"--input", "-i",
help="JSON file or string containing input data"
)
workflow_run_parser.add_argument(
"--async", "-a",
action="store_true",
dest="async_mode",
help="Run asynchronously and return execution ID"
)
workflow_run_parser.set_defaults(func=self.workflow_run)
# Server commands
server_parser = subparsers.add_parser(
"server", help="Start API server"
)
server_parser.add_argument(
"--host", "-H",
default="localhost",
help="Host to bind server to"
)
server_parser.add_argument(
"--port", "-p",
type=int,
default=8080,
help="Port to run server on"
)
server_parser.set_defaults(func=self.server_start)
return parser
# Agent commands
def agent_list(self, args):
"""List available agents."""
agents = self.agent_registry.list_agents()
if not agents:
print("No agents registered.")
return
print(f"Found {len(agents)} agents:")
for i, agent in enumerate(agents, 1):
status = "✓ Connected" if agent.status == AgentStatus.CONNECTED else "✗ Disconnected"
print(f"{i}. {agent.name} ({agent.id}) - {status}")
print(f" URL: {agent.url}")
if agent.description:
print(f" Description: {agent.description}")
print()
def agent_add(self, args):
"""Add an agent."""
# Create agent definition
agent = AgentDefinition(
name=args.name,
description=args.description or "",
url=args.url,
agent_source=AgentSource.REMOTE,
agent_type=args.type
)
# Try to connect to the agent
print(f"Connecting to agent at {args.url}...")
if agent.connect():
print(f"Successfully connected to agent: {agent.name}")
if agent.skills:
print(f"Agent has {len(agent.skills)} skills:")
for skill in agent.skills:
print(f"- {skill.name}: {skill.description}")
else:
print(f"Warning: Could not connect to agent: {agent.error_message}")
print("Adding agent anyway.")
# Register the agent
self.agent_registry.register(agent)
print(f"Agent registered with ID: {agent.id}")
return agent.id
def agent_remove(self, args):
"""Remove an agent."""
if self.agent_registry.unregister(args.id):
print(f"Agent {args.id} removed.")
else:
print(f"Agent {args.id} not found.")
def agent_show(self, args):
"""Show agent details."""
agent = self.agent_registry.get(args.id)
if not agent:
print(f"Agent {args.id} not found.")
return
print(f"Agent: {agent.name} ({agent.id})")
print(f"URL: {agent.url}")
print(f"Description: {agent.description}")
print(f"Type: {agent.agent_type}")
print(f"Source: {agent.agent_source.name}")
# Try to connect if not already connected
if agent.status != AgentStatus.CONNECTED:
print("Connecting to agent...")
agent.connect()
print(f"Status: {agent.status.name}")
if agent.skills:
print(f"\nSkills ({len(agent.skills)}):")
for skill in agent.skills:
print(f"- {skill.name}: {skill.description}")
if agent.error_message:
print(f"\nError: {agent.error_message}")
def agent_discover(self, args):
"""Discover agents on local ports."""
port_range = (args.port_min, args.port_max)
print(f"Discovering agents on ports {port_range[0]}-{port_range[1]}...")
agents = self.agent_registry.discover_agents(args.base_url, port_range)
if not agents:
print("No agents discovered.")
return
print(f"Discovered {len(agents)} agents:")
for i, agent in enumerate(agents, 1):
print(f"{i}. {agent.name} ({agent.id})")
print(f" URL: {agent.url}")
if agent.description:
print(f" Description: {agent.description}")
if agent.skills:
print(f" Skills: {len(agent.skills)}")
print()
# Tool commands
def tool_list(self, args):
"""List available tools."""
tools = self.tool_registry.list_tools()
if not tools:
print("No tools registered.")
return
print(f"Found {len(tools)} tools:")
for i, tool in enumerate(tools, 1):
status = "✓ Available" if tool.status == ToolStatus.AVAILABLE else "✗ Unavailable"
print(f"{i}. {tool.name} ({tool.id}) - {status}")
print(f" URL: {tool.url}")
if tool.description:
print(f" Description: {tool.description}")
if tool.parameters:
print(f" Parameters: {len(tool.parameters)}")
for param in tool.parameters:
required = " (required)" if param.required else ""
print(f" - {param.name}: {param.type_name}{required}")
print()
def tool_discover(self, args):
"""Discover tools from an MCP server."""
print(f"Discovering tools from MCP server at {args.url}...")
tools = self.tool_registry.discover_tools(args.url)
if not tools:
print("No tools discovered.")
return
print(f"Discovered {len(tools)} tools:")
for i, tool in enumerate(tools, 1):
print(f"{i}. {tool.name} ({tool.id})")
if tool.description:
print(f" Description: {tool.description}")
if tool.parameters:
print(f" Parameters: {len(tool.parameters)}")
for param in tool.parameters:
required = " (required)" if param.required else ""
print(f" - {param.name}: {param.type_name}{required}")
print()
# Workflow commands
def workflow_list(self, args):
"""List workflows."""
workflows = self.workflow_storage.list_workflows()
if not workflows:
print("No workflows found.")
return
print(f"Found {len(workflows)} workflows:")
for i, workflow in enumerate(workflows, 1):
print(f"{i}. {workflow['name']} ({workflow['id']})")
if workflow.get('description'):
print(f" Description: {workflow['description']}")
print(f" Created: {workflow['created_at']}")
print(f" Updated: {workflow['updated_at']}")
print(f" Version: {workflow['version']}")
print()
def workflow_show(self, args):
"""Show workflow details."""
workflow = self.workflow_storage.load_workflow(args.id)
if not workflow:
print(f"Workflow {args.id} not found.")
return
print(f"Workflow: {workflow.name} ({workflow.id})")
print(f"Description: {workflow.description}")
print(f"Created: {workflow.created_at.isoformat()}")
print(f"Updated: {workflow.updated_at.isoformat()}")
print(f"Version: {workflow.version}")
print(f"\nNodes ({len(workflow.nodes)}):")
for node_id, node in workflow.nodes.items():
print(f"- {node.name} ({node_id}) - Type: {node.node_type.name}")
print(f"\nEdges ({len(workflow.edges)}):")
for edge_id, edge in workflow.edges.items():
source_node = workflow.nodes.get(edge.source_node_id, None)
target_node = workflow.nodes.get(edge.target_node_id, None)
source_name = source_node.name if source_node else "<unknown>"
target_name = target_node.name if target_node else "<unknown>"
print(f"- {source_name} -> {target_name} ({edge.edge_type.name})")
def workflow_create(self, args):
"""Create a new workflow."""
if args.file:
# Load workflow from file
try:
with open(args.file, "r") as f:
workflow_data = json.load(f)
workflow = Workflow.from_dict(workflow_data)
# Override name and description if provided
if args.name:
workflow.name = args.name
if args.description:
workflow.description = args.description
except Exception as e:
print(f"Error loading workflow from file: {e}")
return
else:
# Create empty workflow
workflow = Workflow(
name=args.name or "New Workflow",
description=args.description or ""
)
# Validate the workflow
valid, errors = workflow.validate()
if not valid:
print("Warning: Workflow is not valid:")
for error in errors:
print(f"- {error}")
print("\nSaving workflow anyway.")
# Save the workflow
workflow_id = self.workflow_storage.save_workflow(workflow)
print(f"Workflow saved with ID: {workflow_id}")
return workflow_id
def workflow_delete(self, args):
"""Delete a workflow."""
if self.workflow_storage.delete_workflow(args.id):
print(f"Workflow {args.id} deleted.")
else:
print(f"Workflow {args.id} not found.")
def workflow_run(self, args):
"""Run a workflow."""
# Load the workflow
workflow = self.workflow_storage.load_workflow(args.id)
if not workflow:
print(f"Workflow {args.id} not found.")
return
# Parse input data if provided
input_data = None
if args.input:
try:
if os.path.isfile(args.input):
# Load from file
with open(args.input, "r") as f:
input_data = json.load(f)
else:
# Parse as JSON string
input_data = json.loads(args.input)
except Exception as e:
print(f"Error parsing input data: {e}")
return
# Run the workflow
print(f"Running workflow: {workflow.name} ({workflow.id})")
try:
if args.async_mode:
# Run asynchronously
execution_id = self.workflow_executor.execute_workflow(
workflow, input_data, wait=False
)
print(f"Workflow execution started. Execution ID: {execution_id}")
return execution_id
else:
# Run synchronously
print("Executing workflow (this may take a while)...")
results = self.workflow_executor.execute_workflow(
workflow, input_data, wait=True
)
print("\nWorkflow execution completed!")
print("\nResults:")
for key, value in results.items():
print(f"{key}: {value}")
return results
except Exception as e:
print(f"Error executing workflow: {e}")
# Server commands
def server_start(self, args):
"""Start the API server."""
try:
# Import here to avoid circular imports
from .server.api import create_app
print(f"Starting API server on {args.host}:{args.port}...")
# Create and run Flask app
app = create_app(
self.agent_registry,
self.tool_registry,
self.workflow_storage,
self.workflow_executor
)
app.run(host=args.host, port=args.port)
except ImportError:
print("Error: Flask is required to run the API server.")
print("Please install Flask with: pip install flask")
def main():
"""Main entry point for the CLI."""
cli = AgentFlowCLI()
cli.run()
if __name__ == "__main__":
main()