UNPKG

bc-webclient-mcp

Version:

Model Context Protocol (MCP) server for Microsoft Dynamics 365 Business Central via WebUI protocol. Enables AI assistants to interact with BC through the web client protocol, supporting Card, List, and Document pages with full line item support and server

176 lines 6.23 kB
/** * Workflow State Manager * * Tracks business workflow execution state across BC sessions. * This provides a higher-level abstraction on top of SessionStateManager * to track multi-step business processes like "create_sales_invoice" or "post_sales_order". * * Architecture: * - WorkflowStateManager: Manages workflow instances (this file) * - SessionStateManager: Manages BC sessions and pages * - WorkflowContext: Tracks current state, history, errors for a workflow * * NOTE: This is ephemeral (in-memory) and will reset when the process restarts. */ import type { ILogger } from '../core/interfaces.js'; /** * Record of a single operation executed within a workflow. */ export interface WorkflowOperation { readonly operationId: string; readonly tool: string; readonly timestamp: string; readonly parameters: Record<string, unknown>; readonly result: { readonly success: boolean; readonly data?: unknown; readonly error?: string; }; } /** * Workflow execution context. * * Tracks the state of a multi-step business process across BC sessions. * Updated by MCP tools as they execute operations. */ export interface WorkflowContext { readonly workflowId: string; readonly sessionId: string; readonly goal: string; readonly parameters: Record<string, unknown>; readonly status: 'active' | 'completed' | 'failed' | 'cancelled'; readonly createdAt: string; readonly updatedAt: string; readonly currentPageContextId?: string; readonly currentPageId?: string; readonly focusedRecordKeys?: Record<string, unknown>; readonly lastOperation?: WorkflowOperation; readonly operations: readonly WorkflowOperation[]; readonly unsavedChanges?: Record<string, unknown>; readonly errors: readonly string[]; readonly lastError?: string; } /** * Input for creating a new workflow. */ export interface CreateWorkflowInput { readonly sessionId: string; readonly goal: string; readonly parameters?: Record<string, unknown>; } /** * Input for updating workflow state. */ export interface UpdateWorkflowStateInput { readonly status?: 'active' | 'completed' | 'failed' | 'cancelled'; readonly currentPageContextId?: string; readonly currentPageId?: string; readonly focusedRecordKeys?: Record<string, unknown>; readonly unsavedChanges?: Record<string, unknown>; readonly appendError?: string; readonly clearErrors?: boolean; } /** * Snapshot of all workflows. */ export interface WorkflowStateSnapshot { readonly workflows: readonly WorkflowContext[]; } /** * WorkflowStateManager tracks business workflow execution state. * * This is a singleton to maintain consistent state across the application. * Provides higher-level workflow tracking on top of SessionStateManager. */ export declare class WorkflowStateManager { private readonly logger?; private static instance; /** * Gets the singleton instance. * @param logger - Optional logger for debug logging */ static getInstance(logger?: ILogger): WorkflowStateManager; /** * Resets the singleton instance (primarily for testing). */ static resetInstance(): void; private readonly workflows; private constructor(); /** * Creates a new workflow. * @param input - Workflow creation parameters * @returns The newly created workflow context */ createWorkflow(input: CreateWorkflowInput): WorkflowContext; /** * Gets a workflow by ID. * @param workflowId - The workflow ID * @returns The workflow context or undefined */ getWorkflow(workflowId: string): WorkflowContext | undefined; /** * Gets all workflows for a session. * @param sessionId - The session ID * @returns Array of workflows in the session */ getWorkflowsBySession(sessionId: string): WorkflowContext[]; /** * Gets all active workflows (status = 'active'). * @returns Array of active workflows */ getActiveWorkflows(): WorkflowContext[]; /** * Updates workflow state. * Creates a new immutable workflow context with updated fields. * @param workflowId - The workflow ID * @param update - State updates to apply * @returns The updated workflow context or undefined if not found */ updateWorkflowState(workflowId: string, update: UpdateWorkflowStateInput): WorkflowContext | undefined; /** * Records a completed operation in the workflow history. * @param workflowId - The workflow ID * @param operation - Operation details (without operationId and timestamp - will be auto-generated) * @returns The updated workflow context or undefined if not found */ recordOperation(workflowId: string, operation: Omit<WorkflowOperation, 'operationId' | 'timestamp'>): WorkflowContext | undefined; /** * Completes a workflow successfully. * @param workflowId - The workflow ID * @returns The updated workflow context or undefined if not found */ completeWorkflow(workflowId: string): WorkflowContext | undefined; /** * Fails a workflow with an error message. * @param workflowId - The workflow ID * @param error - Error message * @returns The updated workflow context or undefined if not found */ failWorkflow(workflowId: string, error: string): WorkflowContext | undefined; /** * Cancels a workflow. * @param workflowId - The workflow ID * @returns The updated workflow context or undefined if not found */ cancelWorkflow(workflowId: string): WorkflowContext | undefined; /** * Deletes a workflow. * @param workflowId - The workflow ID * @returns True if deleted, false if not found */ deleteWorkflow(workflowId: string): boolean; /** * Gets a snapshot of all workflows. * @returns Immutable snapshot of current workflow state */ getSnapshot(): WorkflowStateSnapshot; /** * Gets the number of workflows. */ getWorkflowCount(): number; /** * Clears all workflows (primarily for testing). */ clear(): void; } //# sourceMappingURL=workflow-state-manager.d.ts.map