SPARC Graph Architecture - API Reference
Overview
This document provides comprehensive API reference for the SPARC Graph Architecture, including interfaces, classes, methods, and data structures used in the AI-assisted workflow orchestration system.
🏗️ Core Interfaces
WorkflowState Interface
interface WorkflowState {
// Core workflow information
issueId: string;
currentMode: 'design' | 'build' | 'debug';
currentPhase: string;
progress: number;
// Issue context
issueTitle: string;
issueDescription: string;
// Generated artifacts
artifacts: ArtifactMap;
// AI context
aiContext: AIContext;
// Memory and context
memory: MemoryContext;
// Workflow metadata
metadata: WorkflowMetadata;
// Timestamps
createdAt: Date;
updatedAt: Date;
}
interface ArtifactMap {
requirements?: string;
guidelines?: string;
pseudocode?: string;
architecture?: string;
implementation?: string;
tests?: string;
notes?: string;
}
interface AIContext {
currentAgent: string;
agentHistory: AgentAction[];
toolCalls: ToolCall[];
decisions: Decision[];
}
interface MemoryContext {
chatHistory: ChatMessage[];
context: ContextData;
retrievedContext: RetrievedContext[];
}
interface WorkflowMetadata {
transitions: Transition[];
errors: Error[];
performance: PerformanceMetrics;
}
GraphNode Interface
interface GraphNode {
id: string;
type: 'mode' | 'phase' | 'agent' | 'tool';
status: 'active' | 'completed' | 'pending' | 'blocked';
data: NodeData;
edges: Edge[];
metadata: NodeMetadata;
}
interface NodeData {
// Mode node data
mode?: 'design' | 'build' | 'debug';
currentPhase?: string;
progress?: number;
// Phase node data
phase?: 'specification' | 'pseudocode' | 'architecture' | 'refinement' | 'completion';
input?: string;
output?: string;
// Agent node data
agentType?: 'design' | 'build' | 'debug' | 'orchestration';
role?: string;
capabilities?: string[];
// Tool node data
toolName?: string;
toolType?: 'built-in' | 'mcp' | 'external' | 'custom';
parameters?: ParameterSchema;
}
interface Edge {
id: string;
from: string;
to: string;
type: 'transition' | 'tool_invocation' | 'context_flow';
condition?: TransitionCondition;
weight: number;
metadata: EdgeMetadata;
}
🤖 AI Agent Classes
BaseAgent Class
abstract class BaseAgent {
protected id: string;
protected type: AgentType;
protected capabilities: string[];
protected tools: ToolRegistry;
protected context: ContextManager;
constructor(
id: string,
type: AgentType,
capabilities: string[],
tools: ToolRegistry,
context: ContextManager
) {
this.id = id;
this.type = type;
this.capabilities = capabilities;
this.tools = tools;
this.context = context;
}
abstract execute(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult>;
protected validateInput(input: string): boolean {
// Input validation logic
}
protected logAction(action: AgentAction): void {
// Action logging logic
}
}
type AgentType = 'design' | 'build' | 'debug' | 'orchestration';
DesignAgent Class
class DesignAgent extends BaseAgent {
constructor(tools: ToolRegistry, context: ContextManager) {
super(
'design-agent',
'design',
[
'requirements_analysis',
'architecture_design',
'specification_generation',
'pseudocode_creation'
],
tools,
context
);
}
async execute(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
const phase = this.determinePhase(context);
switch (phase) {
case 'specification':
return await this.processSpecification(input, context, tools);
case 'pseudocode':
return await this.processPseudocode(input, context, tools);
case 'architecture':
return await this.processArchitecture(input, context, tools);
case 'refinement':
return await this.processRefinement(input, context, tools);
case 'completion':
return await this.processCompletion(input, context, tools);
default:
throw new Error(`Unknown phase: ${phase}`);
}
}
private async processSpecification(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
// Implementation for specification phase
}
private async processPseudocode(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
// Implementation for pseudocode phase
}
private async processArchitecture(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
// Implementation for architecture phase
}
private async processRefinement(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
// Implementation for refinement phase
}
private async processCompletion(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
// Implementation for completion phase
}
private determinePhase(context: WorkflowContext): string {
// Phase determination logic
}
}
BuildAgent Class
class BuildAgent extends BaseAgent {
constructor(tools: ToolRegistry, context: ContextManager) {
super(
'build-agent',
'build',
[
'code_generation',
'test_creation',
'documentation_generation',
'configuration_creation'
],
tools,
context
);
}
async execute(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
const phase = this.determinePhase(context);
switch (phase) {
case 'implementation':
return await this.processImplementation(input, context, tools);
case 'testing':
return await this.processTesting(input, context, tools);
case 'documentation':
return await this.processDocumentation(input, context, tools);
case 'configuration':
return await this.processConfiguration(input, context, tools);
default:
throw new Error(`Unknown phase: ${phase}`);
}
}
private async processImplementation(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
// Implementation for code generation
}
private async processTesting(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
// Implementation for test creation
}
private async processDocumentation(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
// Implementation for documentation generation
}
private async processConfiguration(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
// Implementation for configuration creation
}
}
DebugAgent Class
class DebugAgent extends BaseAgent {
constructor(tools: ToolRegistry, context: ContextManager) {
super(
'debug-agent',
'debug',
[
'code_analysis',
'issue_detection',
'fix_generation',
'performance_optimization'
],
tools,
context
);
}
async execute(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
const phase = this.determinePhase(context);
switch (phase) {
case 'analysis':
return await this.processAnalysis(input, context, tools);
case 'issue_detection':
return await this.processIssueDetection(input, context, tools);
case 'fix_generation':
return await this.processFixGeneration(input, context, tools);
case 'optimization':
return await this.processOptimization(input, context, tools);
default:
throw new Error(`Unknown phase: ${phase}`);
}
}
private async processAnalysis(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
// Implementation for code analysis
}
private async processIssueDetection(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
// Implementation for issue detection
}
private async processFixGeneration(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
// Implementation for fix generation
}
private async processOptimization(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
// Implementation for performance optimization
}
}
OrchestrationAgent Class
class OrchestrationAgent extends BaseAgent {
constructor(tools: ToolRegistry, context: ContextManager) {
super(
'orchestration-agent',
'orchestration',
[
'workflow_coordination',
'state_management',
'decision_making',
'conflict_resolution'
],
tools,
context
);
}
async execute(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
const action = this.determineAction(input, context);
switch (action) {
case 'coordinate_workflow':
return await this.coordinateWorkflow(input, context, tools);
case 'manage_transitions':
return await this.manageTransitions(input, context, tools);
case 'resolve_conflicts':
return await this.resolveConflicts(input, context, tools);
case 'optimize_workflow':
return await this.optimizeWorkflow(input, context, tools);
default:
throw new Error(`Unknown action: ${action}`);
}
}
private async coordinateWorkflow(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
// Implementation for workflow coordination
}
private async manageTransitions(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
// Implementation for transition management
}
private async resolveConflicts(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
// Implementation for conflict resolution
}
private async optimizeWorkflow(
input: string,
context: WorkflowContext,
tools: ToolRegistry
): Promise<AgentResult> {
// Implementation for workflow optimization
}
}
🔧 Tool Integration Classes
ToolRegistry Class
class ToolRegistry {
private tools: Map<string, Tool>;
private mcpServers: Map<string, MCPServer>;
private externalAPIs: Map<string, ExternalAPI>;
constructor() {
this.tools = new Map();
this.mcpServers = new Map();
this.externalAPIs = new Map();
}
registerTool(tool: Tool): void {
this.tools.set(tool.name, tool);
}
registerMCPServer(server: MCPServer): void {
this.mcpServers.set(server.name, server);
}
registerExternalAPI(api: ExternalAPI): void {
this.externalAPIs.set(api.name, api);
}
getTool(name: string): Tool | undefined {
return this.tools.get(name);
}
getMCPServer(name: string): MCPServer | undefined {
return this.mcpServers.get(name);
}
getExternalAPI(name: string): ExternalAPI | undefined {
return this.externalAPIs.get(name);
}
async invokeTool(
name: string,
parameters: ToolParameters,
context: ToolContext
): Promise<ToolResult> {
const tool = this.getTool(name);
if (!tool) {
throw new Error(`Tool ${name} not found`);
}
return await tool.execute(parameters, context);
}
async invokeMCPServer(
serverName: string,
toolName: string,
parameters: ToolParameters,
context: ToolContext
): Promise<ToolResult> {
const server = this.getMCPServer(serverName);
if (!server) {
throw new Error(`MCP server ${serverName} not found`);
}
return await server.invokeTool(toolName, parameters, context);
}
async invokeExternalAPI(
apiName: string,
endpoint: string,
parameters: ToolParameters,
context: ToolContext
): Promise<ToolResult> {
const api = this.getExternalAPI(apiName);
if (!api) {
throw new Error(`External API ${apiName} not found`);
}
return await api.invokeEndpoint(endpoint, parameters, context);
}
}
Tool Interface
interface Tool {
name: string;
description: string;
category: ToolCategory;
parameters: ParameterSchema;
capabilities: string[];
execute(
parameters: ToolParameters,
context: ToolContext
): Promise<ToolResult>;
validateParameters(parameters: ToolParameters): boolean;
getCapabilities(): string[];
}
type ToolCategory = 'filesystem' | 'terminal' | 'git' | 'analysis' | 'mcp' | 'external' | 'custom';
interface ParameterSchema {
[key: string]: {
type: 'string' | 'number' | 'boolean' | 'array' | 'object';
required?: boolean;
default?: any;
enum?: any[];
description?: string;
};
}
interface ToolParameters {
[key: string]: any;
}
interface ToolContext {
agentId: string;
sessionId: string;
issueId: string;
workspaceRoot: string;
permissions: string[];
}
interface ToolResult {
success: boolean;
data?: any;
error?: string;
metadata?: ToolMetadata;
}
interface ToolMetadata {
executionTime: number;
resourceUsage: ResourceUsage;
timestamp: Date;
}
MCPServer Class
class MCPServer {
name: string;
serverUrl: string;
serverType: 'http' | 'websocket' | 'stdio';
authentication: AuthConfig;
tools: MCPTool[];
constructor(
name: string,
serverUrl: string,
serverType: 'http' | 'websocket' | 'stdio',
authentication: AuthConfig,
tools: MCPTool[]
) {
this.name = name;
this.serverUrl = serverUrl;
this.serverType = serverType;
this.authentication = authentication;
this.tools = tools;
}
async invokeTool(
toolName: string,
parameters: ToolParameters,
context: ToolContext
): Promise<ToolResult> {
const tool = this.tools.find(t => t.name === toolName);
if (!tool) {
throw new Error(`Tool ${toolName} not found in MCP server ${this.name}`);
}
// Implementation for MCP server communication
return await this.communicateWithServer(tool, parameters, context);
}
private async communicateWithServer(
tool: MCPTool,
parameters: ToolParameters,
context: ToolContext
): Promise<ToolResult> {
// Implementation for server communication
}
}
interface MCPTool {
name: string;
description: string;
parameters: ParameterSchema;
capabilities: string[];
}
interface AuthConfig {
type: 'token' | 'api_key' | 'oauth' | 'none';
token?: string;
key?: string;
secret?: string;
scopes?: string[];
}
📊 LangGraph Integration Classes
LangGraphWorkflow Class
class LangGraphWorkflow {
private state: WorkflowState;
private nodes: Map<string, GraphNode>;
private edges: Map<string, Edge>;
private agentRegistry: AgentRegistry;
private toolRegistry: ToolRegistry;
private contextManager: ContextManager;
constructor(
agentRegistry: AgentRegistry,
toolRegistry: ToolRegistry,
contextManager: ContextManager
) {
this.agentRegistry = agentRegistry;
this.toolRegistry = toolRegistry;
this.contextManager = contextManager;
this.nodes = new Map();
this.edges = new Map();
}
async executeWorkflow(input: WorkflowInput): Promise<WorkflowOutput> {
// Initialize state
this.state = await this.initializeState(input);
// Execute graph traversal
while (!this.isComplete()) {
const currentNode = this.getCurrentNode();
const nextNode = await this.executeNode(currentNode);
await this.transitionTo(nextNode);
}
return this.generateOutput();
}
private async initializeState(input: WorkflowInput): Promise<WorkflowState> {
// State initialization logic
}
private async executeNode(node: GraphNode): Promise<GraphNode> {
// Get appropriate agent
const agent = this.agentRegistry.getAgent(node.data.agentType!);
// Execute agent with tools
const result = await agent.execute(
node.data.input || '',
this.state,
this.toolRegistry
);
// Update state
await this.updateState(result);
// Determine next node
return this.determineNextNode(node, result);
}
private async updateState(result: AgentResult): Promise<void> {
// State update logic
}
private async transitionTo(nextNode: GraphNode): Promise<void> {
// Transition logic
}
private isComplete(): boolean {
// Completion check logic
}
private getCurrentNode(): GraphNode {
// Current node retrieval logic
}
private determineNextNode(
currentNode: GraphNode,
result: AgentResult
): GraphNode {
// Next node determination logic
}
private generateOutput(): WorkflowOutput {
// Output generation logic
}
}
AgentRegistry Class
class AgentRegistry {
private agents: Map<AgentType, BaseAgent>;
constructor() {
this.agents = new Map();
}
registerAgent(agent: BaseAgent): void {
this.agents.set(agent.type, agent);
}
getAgent(type: AgentType): BaseAgent {
const agent = this.agents.get(type);
if (!agent) {
throw new Error(`Agent of type ${type} not found`);
}
return agent;
}
getAllAgents(): BaseAgent[] {
return Array.from(this.agents.values());
}
hasAgent(type: AgentType): boolean {
return this.agents.has(type);
}
}
ContextManager Class
class ContextManager {
private context: Map<string, ContextData>;
private memory: MemoryManager;
private vectorStore: VectorStore;
constructor(memory: MemoryManager, vectorStore: VectorStore) {
this.context = new Map();
this.memory = memory;
this.vectorStore = vectorStore;
}
async enrichContext(context: WorkflowContext): Promise<EnrichedContext> {
// Context enrichment logic
}
async updateContext(result: AgentResult): Promise<void> {
// Context update logic
}
async retrieveContext(query: string, limit: number = 10): Promise<RetrievedContext[]> {
// Context retrieval logic
}
async storeContext(context: ContextData): Promise<void> {
// Context storage logic
}
async getContext(key: string): Promise<ContextData | undefined> {
return this.context.get(key);
}
async setContext(key: string, data: ContextData): Promise<void> {
this.context.set(key, data);
}
}
🔄 Workflow Execution Classes
WorkflowExecutor Class
class WorkflowExecutor {
private langGraphWorkflow: LangGraphWorkflow;
private agentRegistry: AgentRegistry;
private toolRegistry: ToolRegistry;
private contextManager: ContextManager;
private persistenceLayer: PersistenceLayer;
constructor(
agentRegistry: AgentRegistry,
toolRegistry: ToolRegistry,
contextManager: ContextManager,
persistenceLayer: PersistenceLayer
) {
this.agentRegistry = agentRegistry;
this.toolRegistry = toolRegistry;
this.contextManager = contextManager;
this.persistenceLayer = persistenceLayer;
this.langGraphWorkflow = new LangGraphWorkflow(
agentRegistry,
toolRegistry,
contextManager
);
}
async executeWorkflow(
issueId: string,
mode: WorkflowMode,
userInput: string
): Promise<WorkflowResult> {
try {
// Load or create workflow state
const state = await this.loadOrCreateState(issueId, mode);
// Prepare workflow input
const input: WorkflowInput = {
issueId,
mode,
userInput,
state
};
// Execute workflow
const output = await this.langGraphWorkflow.executeWorkflow(input);
// Persist results
await this.persistenceLayer.saveWorkflowState(output.state);
await this.persistenceLayer.saveArtifacts(output.artifacts);
return output;
} catch (error) {
console.error('Workflow execution error:', error);
throw error;
}
}
private async loadOrCreateState(
issueId: string,
mode: WorkflowMode
): Promise<WorkflowState> {
// State loading/creation logic
}
}
AgentCoordinator Class
class AgentCoordinator {
private agentRegistry: AgentRegistry;
private toolRegistry: ToolRegistry;
private contextManager: ContextManager;
constructor(
agentRegistry: AgentRegistry,
toolRegistry: ToolRegistry,
contextManager: ContextManager
) {
this.agentRegistry = agentRegistry;
this.toolRegistry = toolRegistry;
this.contextManager = contextManager;
}
async coordinateWorkflow(
mode: WorkflowMode,
phase: WorkflowPhase,
input: string,
context: WorkflowContext
): Promise<AgentResult> {
// Select appropriate agent
const agent = this.selectAgent(mode, phase);
// Prepare context
const enrichedContext = await this.contextManager.enrichContext(context);
// Execute agent with tools
const result = await agent.execute(input, enrichedContext, this.toolRegistry);
// Update workflow state
await this.updateWorkflowState(result);
// Determine next steps
const nextSteps = await this.determineNextSteps(result);
return {
result,
nextSteps,
updatedState: this.getCurrentState()
};
}
private selectAgent(mode: WorkflowMode, phase: WorkflowPhase): BaseAgent {
// Agent selection logic
}
private async updateWorkflowState(result: AgentResult): Promise<void> {
// Workflow state update logic
}
private async determineNextSteps(result: AgentResult): Promise<NextSteps> {
// Next steps determination logic
}
private getCurrentState(): WorkflowState {
// Current state retrieval logic
}
}
📈 Performance and Monitoring Classes
PerformanceMonitor Class
class PerformanceMonitor {
private metrics: Map<string, PerformanceMetrics>;
private alerts: AlertManager;
constructor(alertManager: AlertManager) {
this.metrics = new Map();
this.alerts = alertManager;
}
async collectMetrics(workflowId: string): Promise<WorkflowMetrics> {
// Metrics collection logic
}
async monitorExecution(
workflowId: string,
execution: WorkflowExecution
): Promise<void> {
// Execution monitoring logic
}
async generateReport(workflowId: string): Promise<PerformanceReport> {
// Report generation logic
}
async setAlert(
workflowId: string,
condition: AlertCondition,
callback: AlertCallback
): Promise<void> {
// Alert setup logic
}
}
interface PerformanceMetrics {
executionTime: number;
nodeExecutionTimes: Map<string, number>;
toolUsageCounts: Map<string, number>;
errorRates: Map<string, number>;
successRate: number;
resourceUsage: ResourceUsage;
userSatisfaction: number;
}
interface ResourceUsage {
memory: number;
cpu: number;
storage: number;
network: number;
}
WorkflowOptimizer Class
class WorkflowOptimizer {
private performanceMonitor: PerformanceMonitor;
private cache: NodeCache;
private parallelExecutor: ParallelExecutor;
constructor(
performanceMonitor: PerformanceMonitor,
cache: NodeCache,
parallelExecutor: ParallelExecutor
) {
this.performanceMonitor = performanceMonitor;
this.cache = cache;
this.parallelExecutor = parallelExecutor;
}
async optimizeWorkflow(
workflow: LangGraphWorkflow,
metrics: WorkflowMetrics
): Promise<OptimizationResult> {
// Workflow optimization logic
}
async optimizeNodeExecution(node: GraphNode): Promise<OptimizationResult> {
// Node execution optimization logic
}
async optimizeToolUsage(tool: Tool): Promise<OptimizationResult> {
// Tool usage optimization logic
}
async optimizeResourceUsage(
workflow: LangGraphWorkflow
): Promise<OptimizationResult> {
// Resource usage optimization logic
}
}
interface OptimizationResult {
optimized: boolean;
improvements: Improvement[];
metrics: PerformanceMetrics;
recommendations: Recommendation[];
}
interface Improvement {
type: 'performance' | 'resource' | 'cost' | 'quality';
description: string;
impact: number;
effort: number;
}
interface Recommendation {
type: 'configuration' | 'architecture' | 'tool' | 'process';
description: string;
priority: 'high' | 'medium' | 'low';
implementation: string;
}
🔒 Security Classes
SecurityManager Class
class SecurityManager {
private authentication: AuthenticationManager;
private authorization: AuthorizationManager;
private encryption: EncryptionManager;
private audit: AuditManager;
constructor(
authentication: AuthenticationManager,
authorization: AuthorizationManager,
encryption: EncryptionManager,
audit: AuditManager
) {
this.authentication = authentication;
this.authorization = authorization;
this.encryption = encryption;
this.audit = audit;
}
async authenticateUser(credentials: UserCredentials): Promise<AuthResult> {
// User authentication logic
}
async authorizeAction(
userId: string,
action: string,
resource: string
): Promise<boolean> {
// Action authorization logic
}
async encryptData(data: any): Promise<EncryptedData> {
// Data encryption logic
}
async decryptData(encryptedData: EncryptedData): Promise<any> {
// Data decryption logic
}
async logAction(action: SecurityAction): Promise<void> {
// Action logging logic
}
}
interface UserCredentials {
username: string;
password: string;
token?: string;
}
interface AuthResult {
success: boolean;
token?: string;
permissions: string[];
expiresAt?: Date;
}
interface SecurityAction {
userId: string;
action: string;
resource: string;
timestamp: Date;
result: 'success' | 'failure';
details?: string;
}
AgentSandbox Class
class AgentSandbox {
private isolation: IsolationManager;
private resourceLimits: ResourceLimits;
private monitoring: SandboxMonitor;
constructor(
isolation: IsolationManager,
resourceLimits: ResourceLimits,
monitoring: SandboxMonitor
) {
this.isolation = isolation;
this.resourceLimits = resourceLimits;
this.monitoring = monitoring;
}
async create(): Promise<SandboxEnvironment> {
// Sandbox creation logic
}
async execute(
agent: BaseAgent,
input: string,
tools: ToolRegistry
): Promise<AgentResult> {
// Sandbox execution logic
}
async destroy(): Promise<void> {
// Sandbox cleanup logic
}
async monitor(): Promise<SandboxMetrics> {
// Sandbox monitoring logic
}
}
interface SandboxEnvironment {
id: string;
resources: ResourceLimits;
isolation: IsolationConfig;
monitoring: MonitoringConfig;
}
interface ResourceLimits {
memory: number;
cpu: number;
storage: number;
network: number;
executionTime: number;
}
interface IsolationConfig {
filesystem: boolean;
network: boolean;
process: boolean;
system: boolean;
}
This comprehensive API reference provides detailed interfaces, classes, and methods for implementing the SPARC Graph Architecture with AI-assisted workflow orchestration, LangGraph integration, and MCP server support.