feat: Complete API integration for Agents, Conversations, and Executions
Implement full CQRS API integration with type-safe endpoints for all core backend operations. ## What's New - **Agent Management**: 4 endpoints (create, get, update, delete) with 3 enums - **Conversations**: 2 endpoints (create, get) with message support - **Executions**: 3 endpoints (start, complete, get) with status tracking - **OpenAPI Schema**: Updated to backend v1.0.0-mvp (10 endpoints) ## Implementation Details - All endpoints follow CQRS pattern (commands/queries) - 100% strict typing (no dynamic, all explicit types) - Functional error handling with Result<T> pattern - 3,136+ lines of production code - 1,500+ lines of comprehensive documentation ## Files Added - lib/api/endpoints/agent_endpoint.dart (364 lines) - lib/api/endpoints/conversation_endpoint.dart (319 lines) - lib/api/endpoints/execution_endpoint.dart (434 lines) - lib/api/examples/agent_example.dart (212 lines) - docs/AGENT_API_INTEGRATION.md (431 lines) - docs/COMPLETE_API_INTEGRATION.md (555 lines) - docs/INTEGRATION_STATUS.md (339 lines) ## Quality Metrics - Flutter analyze: 0 errors ✅ - Type safety: 100% (0 dynamic types) ✅ - CQRS compliance: 100% ✅ - Backend compatibility: v1.0.0-mvp ✅ ## Backend Integration - Updated api-schema.json from backend openapi.json - Supports all MVP endpoints except list operations (deferred to Phase 3) - Ready for JWT authentication (infrastructure in place) ## Usage ```dart import 'package:console/api/api.dart'; final client = CqrsApiClient(config: ApiClientConfig.development); // Agent CRUD await client.createAgent(CreateAgentCommand(...)); await client.getAgent('uuid'); // Conversations await client.createConversation(CreateConversationCommand(...)); // Executions await client.startAgentExecution(StartAgentExecutionCommand(...)); ``` 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,212 @@
|
||||
/// Example usage of Agent API endpoints
|
||||
///
|
||||
/// This file demonstrates how to use the Agent CRUD operations
|
||||
/// with the CQRS API client.
|
||||
library;
|
||||
|
||||
import '../api.dart';
|
||||
|
||||
/// Example: Create and manage an AI agent
|
||||
Future<void> agentExample() async {
|
||||
// Initialize API client
|
||||
final CqrsApiClient client = CqrsApiClient(
|
||||
config: ApiClientConfig.development,
|
||||
);
|
||||
|
||||
try {
|
||||
// 1. Create a new agent
|
||||
print('Creating new agent...');
|
||||
final Result<void> createResult = await client.createAgent(
|
||||
CreateAgentCommand(
|
||||
name: 'Code Generator',
|
||||
description: 'AI agent for code generation tasks',
|
||||
type: AgentType.codeGenerator,
|
||||
modelProvider: 'ollama',
|
||||
modelName: 'phi',
|
||||
providerType: ModelProviderType.localEndpoint,
|
||||
modelEndpoint: 'http://localhost:11434',
|
||||
temperature: 0.7,
|
||||
maxTokens: 4000,
|
||||
systemPrompt: 'You are a helpful code generation assistant.',
|
||||
enableMemory: true,
|
||||
conversationWindowSize: 10,
|
||||
),
|
||||
);
|
||||
|
||||
createResult.when(
|
||||
success: (_) => print('✓ Agent created successfully'),
|
||||
error: (ApiErrorInfo error) =>
|
||||
print('✗ Failed to create agent: ${error.message}'),
|
||||
);
|
||||
|
||||
// 2. Get agent by ID
|
||||
print('\nFetching agent details...');
|
||||
final String agentId = 'your-agent-uuid-here'; // Replace with actual ID
|
||||
final Result<AgentDto> getResult = await client.getAgent(agentId);
|
||||
|
||||
getResult.when(
|
||||
success: (AgentDto agent) {
|
||||
print('✓ Agent found:');
|
||||
print(' Name: ${agent.name}');
|
||||
print(' Type: ${agent.type.value}');
|
||||
print(' Status: ${agent.status.value}');
|
||||
print(' Model: ${agent.modelProvider}/${agent.modelName}');
|
||||
print(' Created: ${agent.createdAt}');
|
||||
},
|
||||
error: (ApiErrorInfo error) =>
|
||||
print('✗ Failed to fetch agent: ${error.message}'),
|
||||
);
|
||||
|
||||
// 3. Update agent
|
||||
print('\nUpdating agent...');
|
||||
final Result<void> updateResult = await client.updateAgent(
|
||||
UpdateAgentCommand(
|
||||
id: agentId,
|
||||
name: 'Advanced Code Generator',
|
||||
temperature: 0.8,
|
||||
status: AgentStatus.active,
|
||||
),
|
||||
);
|
||||
|
||||
updateResult.when(
|
||||
success: (_) => print('✓ Agent updated successfully'),
|
||||
error: (ApiErrorInfo error) =>
|
||||
print('✗ Failed to update agent: ${error.message}'),
|
||||
);
|
||||
|
||||
// 4. Delete agent
|
||||
print('\nDeleting agent...');
|
||||
final Result<void> deleteResult = await client.deleteAgent(
|
||||
DeleteAgentCommand(id: agentId),
|
||||
);
|
||||
|
||||
deleteResult.when(
|
||||
success: (_) => print('✓ Agent deleted successfully'),
|
||||
error: (ApiErrorInfo error) =>
|
||||
print('✗ Failed to delete agent: ${error.message}'),
|
||||
);
|
||||
|
||||
// 5. Pattern matching example with switch expression
|
||||
final String message = switch (getResult) {
|
||||
ApiSuccess(value: final AgentDto agent) =>
|
||||
'Agent "${agent.name}" is ${agent.status.value}',
|
||||
ApiError(error: final ApiErrorInfo err) => 'Error: ${err.message}',
|
||||
};
|
||||
print('\nPattern match result: $message');
|
||||
} finally {
|
||||
// Always dispose client when done
|
||||
client.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// Example: Error handling patterns
|
||||
Future<void> errorHandlingExample() async {
|
||||
final CqrsApiClient client = CqrsApiClient(
|
||||
config: ApiClientConfig.development,
|
||||
);
|
||||
|
||||
try {
|
||||
final Result<AgentDto> result = await client.getAgent('invalid-uuid');
|
||||
|
||||
// Pattern 1: when() method
|
||||
result.when(
|
||||
success: (AgentDto agent) {
|
||||
print('Success: ${agent.name}');
|
||||
},
|
||||
error: (ApiErrorInfo error) {
|
||||
switch (error.type) {
|
||||
case ApiErrorType.network:
|
||||
print('No internet connection');
|
||||
case ApiErrorType.timeout:
|
||||
print('Request timed out');
|
||||
case ApiErrorType.http:
|
||||
if (error.statusCode == 404) {
|
||||
print('Agent not found');
|
||||
} else if (error.statusCode == 401) {
|
||||
print('Unauthorized - check API key');
|
||||
} else {
|
||||
print('HTTP error: ${error.statusCode}');
|
||||
}
|
||||
case ApiErrorType.validation:
|
||||
print('Validation error: ${error.details}');
|
||||
case ApiErrorType.serialization:
|
||||
print('JSON parsing failed');
|
||||
case ApiErrorType.unknown:
|
||||
print('Unexpected error: ${error.message}');
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// Pattern 2: Switch expression
|
||||
final String statusMessage = switch (result) {
|
||||
ApiSuccess() => 'Agent loaded successfully',
|
||||
ApiError(error: final ApiErrorInfo err) when err.statusCode == 404 =>
|
||||
'Agent not found',
|
||||
ApiError(error: final ApiErrorInfo err) =>
|
||||
'Error: ${err.type.name} - ${err.message}',
|
||||
};
|
||||
print(statusMessage);
|
||||
} finally {
|
||||
client.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// Example: Creating agents with different configurations
|
||||
Future<void> agentVariationsExample() async {
|
||||
final CqrsApiClient client = CqrsApiClient(
|
||||
config: ApiClientConfig.development,
|
||||
);
|
||||
|
||||
try {
|
||||
// Local Ollama model
|
||||
final CreateAgentCommand localAgent = CreateAgentCommand(
|
||||
name: 'Local Code Reviewer',
|
||||
description: 'Reviews code using local Ollama model',
|
||||
type: AgentType.codeReviewer,
|
||||
modelProvider: 'ollama',
|
||||
modelName: 'codellama:7b',
|
||||
providerType: ModelProviderType.localEndpoint,
|
||||
modelEndpoint: 'http://localhost:11434',
|
||||
systemPrompt: 'You are a code review expert.',
|
||||
temperature: 0.5,
|
||||
maxTokens: 2000,
|
||||
);
|
||||
|
||||
// Cloud API model (OpenAI)
|
||||
final CreateAgentCommand cloudAgent = CreateAgentCommand(
|
||||
name: 'Cloud Debugger',
|
||||
description: 'Advanced debugging assistant using GPT-4',
|
||||
type: AgentType.debugger,
|
||||
modelProvider: 'openai',
|
||||
modelName: 'gpt-4o',
|
||||
providerType: ModelProviderType.cloudApi,
|
||||
apiKey: 'your-openai-api-key-here',
|
||||
systemPrompt: 'You are an expert debugger.',
|
||||
temperature: 0.3,
|
||||
maxTokens: 8000,
|
||||
);
|
||||
|
||||
// Custom model
|
||||
final CreateAgentCommand customAgent = CreateAgentCommand(
|
||||
name: 'Documentation Writer',
|
||||
description: 'Generates comprehensive documentation',
|
||||
type: AgentType.documenter,
|
||||
modelProvider: 'custom',
|
||||
modelName: 'custom-model-v1',
|
||||
providerType: ModelProviderType.custom,
|
||||
modelEndpoint: 'https://api.example.com/v1/chat',
|
||||
apiKey: 'custom-api-key',
|
||||
systemPrompt: 'You are a technical documentation expert.',
|
||||
temperature: 0.6,
|
||||
maxTokens: 6000,
|
||||
conversationWindowSize: 20,
|
||||
);
|
||||
|
||||
// Create all agents
|
||||
await client.createAgent(localAgent);
|
||||
await client.createAgent(cloudAgent);
|
||||
await client.createAgent(customAgent);
|
||||
} finally {
|
||||
client.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user