AI Agentic Workflow
Agentic workflow substrate on the medbase platform layer.
Custom-built agentic AI system — no LangChain, no CrewAI — plugin-driven agents with RAG, tool calling, and multi-provider LLM support
Overview
MedOS implements a custom agentic AI architecture without external frameworks (LangChain, CrewAI, AutoGen). The system spans:
- Visual Workflow Builder — ReactFlow-based DAG editor with 200+ node types including Agent and RAG nodes
- Clinical AI Services — OpenAI GPT-4o-mini for clinical note structuring and nurse documentation
- Migration AI Agents — Claude Sonnet 4.5 for schema analysis, field mapping, and transform generation
- Dify AI Workflows — External AI workflow platform for claim coding, diagnosis, and screening
- PRM AI Personas — Dify-hosted personas for clinical analysis and patient engagement
- RCM AI Assistant — Revenue cycle management AI panel
Architecture Diagram
┌──────────────────────────────────────────────────────────────────────────────┐
│ AI AGENTIC WORKFLOW SYSTEM │
│ │
│ ┌────────────────────────────────────────────────────────────────────────┐ │
│ │ LAYER 1: VISUAL WORKFLOW BUILDER │ │
│ │ main-flow-editor/ (ReactFlow v11 + @xyflow/react) │ │
│ │ │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
│ │ │ Start │→ │ LLM │→ │ Agent │→ │ End │ │ │
│ │ │ Node │ │ Node │ │ Node │ │ Node │ │ │
│ │ └──────────┘ └──────────┘ └────┬─────┘ └──────────┘ │ │
│ │ │ │ │
│ │ ┌─────┴──────┐ │ │
│ │ │ Strategy │ │ │
│ │ │ Provider │ │ │
│ │ │ Plugin │ │ │
│ │ └─────┬──────┘ │ │
│ │ │ │ │
│ │ ┌────────────────────────────┼────────────────────────┐ │ │
│ │ ▼ ▼ ▼ │ │
│ │ ┌──────────┐ ┌──────────────────────────┐ ┌──────────────────┐ │ │
│ │ │ Tools │ │ Knowledge Retrieval │ │ Control Flow │ │ │
│ │ │ ────── │ │ (RAG Node) │ │ ──────────── │ │ │
│ │ │ HTTP │ │ ────────────────── │ │ IfElse │ │ │
│ │ │ Code │ │ Multi-dataset │ │ Loop │ │ │
│ │ │ DB Query│ │ Metadata filters │ │ Iteration │ │ │
│ │ │ Custom │ │ Reranking model │ │ Variable Assign │ │ │
│ │ └──────────┘ │ Top-K results │ └──────────────────┘ │ │
│ │ └──────────────────────────┘ │ │
│ │ │ │
│ │ 200+ Node Types (BlockEnum): │ │
│ │ ├── Generic: Start, End, Answer, LLM, Code, HttpRequest, IfElse │ │
│ │ ├── Agent: agent (strategy-driven orchestration) │ │
│ │ ├── RAG: knowledge-retrieval (document retrieval) │ │
│ │ ├── NLP: question-classifier, parameter-extractor, document-extractor│ │
│ │ ├── Clinical: PatientAdmission, VitalSigns, DiagnosisEntry (50+) │ │
│ │ ├── Hospital: Triage, BedMgmt, Pharmacy, LabWork, Discharge │ │
│ │ ├── OPD: CheckIn, WaitDoctor, Consultation, Waiting states │ │
│ │ ├── E-MAR: MedReceived, Verification, Administration │ │
│ │ └── HL7: OR orders, Claims, Coverage, EMAR │ │
│ └────────────────────────────────────────────────────────────────────────┘ │
│ │
│ ┌────────────────────────────────────────────────────────────────────────┐ │
│ │ LAYER 2: AI SERVICE PROVIDERS │ │
│ │ │ │
│ │ ┌──────────────────┐ ┌──────────────────┐ ┌──────────────────┐ │ │
│ │ │ OpenAI │ │ Anthropic Claude │ │ Google Gemini │ │ │
│ │ │ ──────── │ │ ──────────────── │ │ ────────────── │ │ │
│ │ │ GPT-4o │ │ Sonnet 4.5 │ │ Vertex AI │ │ │
│ │ │ GPT-4o-mini │ │ (Migration AI) │ │ (configured) │ │ │
│ │ │ GPT-4-turbo │ │ │ │ │ │ │
│ │ │ GPT-3.5-turbo │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ Features: │ │ Features: │ │ │ │ │
│ │ │ • Assistants API │ │ • 8K max tokens │ │ │ │ │
│ │ │ • Tool calling │ │ • PHI masking │ │ │ │ │
│ │ │ • Thread mgmt │ │ • JSON output │ │ │ │ │
│ │ │ • Streaming │ │ │ │ │ │ │
│ │ └──────────────────┘ └──────────────────┘ └──────────────────┘ │ │
│ │ │ │
│ │ ┌──────────────────┐ ┌──────────────────┐ │ │
│ │ │ Azure OpenAI │ │ Custom Providers │ │ │
│ │ │ (enterprise) │ │ (plugin system) │ │ │
│ │ └──────────────────┘ └──────────────────┘ │ │
│ └────────────────────────────────────────────────────────────────────────┘ │
│ │
│ ┌────────────────────────────────────────────────────────────────────────┐ │
│ │ LAYER 3: DOMAIN-SPECIFIC AI AGENTS │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────────────────────┐ │ │
│ │ │ CLINICAL NOTE AI │ │ │
│ │ │ services/ai/clinical-note-ai.service.ts │ │ │
│ │ │ │ │ │
│ │ │ Input: Raw speech-to-text transcript or typed note │ │ │
│ │ │ Model: GPT-4o-mini (JSON response) | Fallback: local regex │ │ │
│ │ │ │ │ │
│ │ │ Output: │ │ │
│ │ │ ┌──────────────────┐ ┌──────────────────┐ ┌──────────────┐ │ │ │
│ │ │ │ SOAP Sections │ │ Extracted Data │ │ FHIR Tags │ │ │ │
│ │ │ │ • Subjective │ │ • Vitals (T,BP, │ │ • SNOMED CT │ │ │ │
│ │ │ │ • Objective │ │ HR,RR,SpO2, │ │ • LOINC │ │ │ │
│ │ │ │ • Assessment │ │ Pain) │ │ • Confidence │ │ │ │
│ │ │ │ • Plan │ │ • Diagnoses │ │ 0.85 (AI) │ │ │ │
│ │ │ │ • Focus │ │ • Medications │ │ 0.50 (local│ │ │ │
│ │ │ │ • Intervention │ │ • Procedures │ │ │ │ │ │
│ │ │ │ • Evaluation │ │ • Summary │ │ │ │ │ │
│ │ │ └──────────────────┘ └──────────────────┘ └──────────────┘ │ │ │
│ │ └─────────────────────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────────────────────┐ │ │
│ │ │ NURSE NOTE AI │ │ │
│ │ │ services/ai/nurse-note-ai.service.ts │ │ │
│ │ │ │ │ │
│ │ │ 5 Capabilities: │ │ │
│ │ │ 1. Extract from Speech → structured nursing assessment │ │ │
│ │ │ 2. Generate Draft → nurse note template from patient context │ │ │
│ │ │ 3. Rewrite/Polish → shorthand to formal documentation │ │ │
│ │ │ 4. Check Completeness → detect missing sections/contradictions │ │ │
│ │ │ 5. Shift Summary → previous notes to handoff summary │ │ │
│ │ │ │ │ │
│ │ │ Abbreviation expansion: pt→Patient, bp→blood pressure, │ │ │
│ │ │ hr→heart rate, spo2→oxygen saturation, npo→nothing by mouth, │ │ │
│ │ │ wnl→within normal limits, prn→as needed, c/o→complaining of │ │ │
│ │ └─────────────────────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────────────────────┐ │ │
│ │ │ MIGRATION AI AGENTS (Claude Sonnet 4.5) │ │ │
│ │ │ ever-api-migration/src/api/migration/claude/ │ │ │
│ │ │ │ │ │
│ │ │ ┌────────────────┐ ┌────────────────┐ ┌─────────────────┐ │ │ │
│ │ │ │ Schema Analyzer│──▶│ Field Mapper │──▶│ Transform Gen │ │ │ │
│ │ │ │ ──────────────│ │ ──────────────│ │ ───────────────│ │ │ │
│ │ │ │ Masked schema │ │ Table analysis│ │ Approved maps │ │ │ │
│ │ │ │ → entity types│ │ → field maps │ │ → TypeScript │ │ │ │
│ │ │ │ + confidence│ │ + traffic │ │ function code│ │ │ │
│ │ │ │ + order │ │ lights │ │ + test cases │ │ │ │
│ │ │ └────────────────┘ └────────────────┘ └─────────────────┘ │ │ │
│ │ │ │ │ │
│ │ │ PHI Masking: Thai, Swedish, Chinese, English │ │ │
│ │ │ Safety: Claude generates code → humans review → code executes │ │ │
│ │ └─────────────────────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────────────────────┐ │ │
│ │ │ DIFY AI WORKFLOWS (Edge Functions) │ │ │
│ │ │ supabase/functions/ → _shared/difyClient.ts │ │ │
│ │ │ │ │ │
│ │ │ ┌──────────────┐ ┌──────────────────┐ ┌──────────────────┐ │ │ │
│ │ │ │ Claim Coder │ │ Diagnosis Assist │ │ Patient Screener │ │ │ │
│ │ │ │ ─────────── │ │ ──────────────── │ │ ──────────────── │ │ │ │
│ │ │ │ Medical Rec │ │ Symptoms+History │ │ Intake form │ │ │ │
│ │ │ │ → ICD/CPT │ │ → Diff diagnosis │ │ → Risk score │ │ │ │
│ │ │ └──────────────┘ └──────────────────┘ └──────────────────┘ │ │ │
│ │ └─────────────────────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────────────────────┐ │ │
│ │ │ PRM AI PERSONAS (Dify-hosted) │ │ │
│ │ │ prm-sidecars/dify/protocol-scout-persona.yaml │ │ │
│ │ │ │ │ │
│ │ │ Protocol Scout ─── Clinical telemetry analysis │ │ │
│ │ │ Brand Concierge ── VIP patient outreach (Thai/EN WhatsApp) │ │ │
│ │ └─────────────────────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────────────────────┐ │ │
│ │ │ RCM AI ASSISTANT │ │ │
│ │ │ rcm-kit/src/financial/components/RcmAiAssistantPanel.tsx │ │ │
│ │ │ Revenue cycle management AI-assisted claim review │ │ │
│ │ └─────────────────────────────────────────────────────────────────┘ │ │
│ └────────────────────────────────────────────────────────────────────────┘ │
│ │
│ ┌────────────────────────────────────────────────────────────────────────┐ │
│ │ LAYER 4: WORKFLOW STATE MACHINE │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────────────────────┐ │ │
│ │ │ MASTER_WORKFLOW_NODES (Runtime State Machine) │ │ │
│ │ │ packages/medical-kit/src/medical-worklist/workflowState.ts │ │ │
│ │ │ │ │ │
│ │ │ Patient Journey Orchestration: │ │ │
│ │ │ Registration → Triage → Waiting → Consultation → Orders │ │ │
│ │ │ → Lab/Imaging → Results → Treatment → Discharge │ │ │
│ │ │ │ │ │
│ │ │ Backend Handlers: │ │ │
│ │ │ • workflowActionHandler.ts — Manifest transitions │ │ │
│ │ │ • workflowActionRouter.ts — API endpoint resolution │ │ │
│ │ │ • workflow-template.service.ts — Template CRUD │ │ │
│ │ │ • workflowSession.service.ts — Session state management │ │ │
│ │ │ • workflowSession.realtime.ts — Supabase realtime sync │ │ │
│ │ └─────────────────────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────────────────────┐ │ │
│ │ │ INTAKE WORKFLOW ENGINE (Configurable) │ │ │
│ │ │ services/intake-workflow/ │ │ │
│ │ │ │ │ │
│ │ │ workflowTemplate.service.ts — Template management │ │ │
│ │ │ workflowSession.service.ts — Session state │ │ │
│ │ │ workflowRuntime.map.ts — Runtime node mapping │ │ │
│ │ └─────────────────────────────────────────────────────────────────┘ │ │
│ └────────────────────────────────────────────────────────────────────────┘ │
│ │
│ ┌────────────────────────────────────────────────────────────────────────┐ │
│ │ LAYER 5: PLUGIN & EXECUTION INFRASTRUCTURE │ │
│ │ │ │
│ │ ┌──────────────────────┐ ┌──────────────────────────────────────┐ │ │
│ │ │ Plugin System │ │ Execution & Logging │ │ │
│ │ │ ────────────── │ │ ────────────────── │ │ │
│ │ │ • Agent strategies │ │ • Agent log viewer │ │ │
│ │ │ • Tool registration │ │ • Retry log tracking │ │ │
│ │ │ • Model configuration│ │ • Loop/iteration logging │ │ │
│ │ │ • Plugin versioning │ │ • Result panel display │ │ │
│ │ │ • Provider credentials│ │ • Status indicators │ │ │
│ │ └──────────────────────┘ └──────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌──────────────────────────────────────────────────────────────────┐ │ │
│ │ │ AI Administration │ │ │
│ │ │ system-administration/ai-system/pages/AIAgentManagementPage.tsx │ │ │
│ │ │ • System-wide agent configuration │ │ │
│ │ │ • Agent strategy management │ │ │
│ │ │ • Provider credentials management │ │ │
│ │ └──────────────────────────────────────────────────────────────────┘ │ │
│ └────────────────────────────────────────────────────────────────────────┘ │
└──────────────────────────────────────────────────────────────────────────────┘
Agent Node System
The Agent node is the core agentic component in the workflow builder.
Configuration
type AgentNodeType = {
agent_strategy_provider_name?: string // Plugin provider
agent_strategy_name?: string // Strategy identifier
agent_strategy_label?: string // Display name
agent_parameters?: ToolVarInputs // Configured parameters
output_schema: Record<string, any> // Expected outputs
plugin_unique_identifier?: string // Plugin version reference
memory?: Memory // Chat history config
}
Features
- Strategy-based execution — pluggable AI strategies via plugin system
- Multi-tool support — single and multi-tool selectors for function calling
- Model swapping — per-strategy model selection (OpenAI, Claude, Gemini, Azure)
- Memory —
AgentFeature.HISTORY_MESSAGESfor conversation persistence - Execution logging — detailed agent run logs with retry tracking
Agent UI Components
| Component | Path | Purpose |
|---|---|---|
agent/node.tsx |
Workflow canvas rendering | Shows strategy, models, tools |
agent/panel.tsx |
Configuration sidebar | Strategy selection, parameters |
agent/types.ts |
Type definitions | AgentNodeType interface |
agent/use-config.ts |
State hook | Configuration management |
agent-setting/ |
App config | Agent system settings |
agent-tools/ |
Tool management | Tool selection and config |
prompt-editor.tsx |
Prompt editing | Strategy prompt templates |
Knowledge Retrieval (RAG) Node
type KnowledgeRetrievalNodeType = {
query_variable_selector: VariableSelector // Input query
retrieval_mode: 'single' | 'multiple' // Dataset mode
single_retrieval_config?: {
model: ModelConfig // Embedding model
top_k: number // Result count
}
multiple_retrieval_config?: { ... } // Per-dataset configs
datasets: Dataset[] // Selected knowledge bases
metadata_filters?: MetadataFilterCondition[]
rerank_model?: ModelConfig // Optional reranking
}
Output Variables: results (documents), scores (relevance), metadata
OpenAI Assistants Integration
Location: shared-engine/ai-chat/components/SmartAISystem/
| Function | Purpose |
|---|---|
getAssistant() |
Retrieve configured assistant |
createThread() |
Create conversation thread |
addMessage(threadId, message) |
Add user message |
startRun(threadId, instructions) |
Execute assistant |
getRun(threadId, runId) |
Check run status |
submitOutputs(threadId, runId, outputs) |
Provide tool results |
chatCompletion({model, messages, tools}) |
Direct chat |
Features:
- Persistent thread management
- Function/tool calling with output submission
- HL7 order generation from natural language
- Temperature and max_token controls
Clinical AI Services
Clinical Note AI
| Aspect | Detail |
|---|---|
| File | services/ai/clinical-note-ai.service.ts |
| Model | GPT-4o-mini (JSON response format) |
| Fallback | Local regex-based extraction (confidence: 0.5) |
| Input | Raw STT transcript or typed clinical note |
| Output | SOAP sections, vitals, diagnoses, medications, procedures, FHIR tags |
| Coding | SNOMED CT (diagnoses), LOINC (observations/vitals) |
| Languages | Thai, English |
Nurse Note AI
| Aspect | Detail |
|---|---|
| File | services/ai/nurse-note-ai.service.ts |
| Capabilities | Extract, Generate Draft, Rewrite, Completeness Check, Shift Summary |
| Output | Focus, nursing dx, goals, assessments, interventions, evaluations, vitals |
| Abbreviations | 20+ nursing abbreviation expansions (pt, bp, hr, spo2, npo, prn, etc.) |
Migration AI Agents (Claude)
Three-phase pipeline with human-in-the-loop:
| Agent | Model | Input | Output |
|---|---|---|---|
| Schema Analyzer | claude-sonnet-4-5-20250514 | Masked schema | Entity type inference + confidence |
| Field Mapper | claude-sonnet-4-5-20250514 | Schema analysis + samples | Field mappings + traffic lights |
| Transform Generator | claude-sonnet-4-5-20250514 | Approved mappings | TypeScript functions + test cases |
Safety principle: Claude generates code → humans review → code executes deterministically. Claude never runs at migration runtime.
Dify AI Workflows
Externalized to Dify platform via Supabase Edge Functions:
| Workflow | Endpoint Env Var | Input | Output |
|---|---|---|---|
| Claim Coder | DIFY_CLAIM_CODER_ENDPOINT |
Medical record | ICD/CPT codes |
| Diagnosis Assistant | DIFY_DIAGNOSIS_ASSISTANT_ENDPOINT |
Symptoms, history, tests | Differential diagnoses |
| Patient Screener | DIFY_PATIENT_SCREENER_ENDPOINT |
Intake data | Risk score, actions |
Data Flow Patterns
Clinical Note Processing
Speech-to-Text → Raw Transcript
│
▼
Clinical Note AI Service
(GPT-4o-mini / local regex)
│
▼
┌───────────────────┐
│ Structured Output │
│ • SOAP sections │
│ • Extracted vitals│
│ • FHIR tags │
│ • Diagnoses │
└───────────────────┘
Agent Node Execution
Workflow DAG → Agent Node reached
│
▼
Load Strategy Plugin
│
▼
Select Model (OpenAI/Claude/Gemini)
│
▼
Execute with Tools + Memory
│
┌─────┴──────┐
│ Tool Calls │ (if needed)
│ → HTTP │
│ → Code │
│ → DB Query │
│ → Custom │
└─────┬──────┘
│
▼
Return Output Schema → Next Node
Migration AI Pipeline
Source DB Schema → PHI Masking → Schema Analyzer (Claude)
│
▼
Entity Types + Confidence
│
▼
Field Mapper (Claude)
│
▼
Field Mappings + Traffic Lights
│
┌─────┴─────┐
▼ ▼
GREEN (auto) YELLOW/RED
│ (human review)
│ │
└─────┬─────┘
▼
Transform Generator (Claude)
│
▼
TypeScript Functions + Tests
│
▼
Human Code Review → Approve
│
▼
Deterministic Execution (Phase 3)
Model Provider Configuration
| Provider | Models | Use Case |
|---|---|---|
| OpenAI | GPT-4o, GPT-4o-mini, GPT-4-turbo, GPT-3.5-turbo | Clinical notes, nurse notes, assistants, workflow agents |
| Anthropic Claude | claude-sonnet-4-5-20250514 | Migration AI agents (schema, mapping, transforms) |
| Google Gemini | Vertex AI | Configured but secondary |
| Azure OpenAI | Enterprise deployment | Enterprise clients |
| Dify Platform | Workflow-managed models | Claim coding, diagnosis, screening |
| Custom | Plugin system | Extensible via provider plugins |
Key Design Decisions
-
No LangChain/CrewAI — Custom implementation avoids framework lock-in and allows healthcare-specific optimizations (PHI masking, FHIR tagging, traffic light confidence).
-
Strategy-based agents — Agent behavior is driven by pluggable strategies, not hardcoded logic. New clinical workflows can be added without changing the agent infrastructure.
-
Dify for prompt management — Externalizes prompt engineering to Dify platform. Clinical teams can iterate on prompts without code deployments.
-
Local fallbacks — All AI services have regex-based fallback when APIs are unavailable. Critical for hospital environments with intermittent connectivity.
-
Human-in-the-loop for migration — Claude generates artifacts (mappings, code) that require human review before execution. No AI runs unsupervised on patient data.
-
FHIR-native tagging — Clinical note AI automatically assigns SNOMED CT and LOINC codes, enabling downstream interoperability without manual coding.
-
Workflow-as-DAG — Workflows stored as JSON DAGs in Redux + Supabase. Runtime execution via state machine in backend services. Visual editor provides low-code workflow creation.
-
Multi-provider flexibility — Agent nodes can swap LLM providers at runtime via strategy parameters. Avoids single-vendor lock-in.
File Index
| Component | Path |
|---|---|
| Agent Node | ever-medos-global-pure-react/src/common/components/medical/builder/main-flow-editor/components/workflow/nodes/agent/ |
| RAG Node | ever-medos-global-pure-react/src/common/components/medical/builder/main-flow-editor/components/workflow/nodes/knowledge-retrieval/ |
| Workflow Types | ever-medos-global-pure-react/src/common/components/medical/builder/main-flow-editor/components/workflow/types.ts |
| Clinical Note AI | ever-medos-global-pure-react/src/services/ai/clinical-note-ai.service.ts |
| Nurse Note AI | ever-medos-global-pure-react/src/services/ai/nurse-note-ai.service.ts |
| OpenAI Integration | ever-medos-global-pure-react/src/common/components/shared-engine/ai-chat/components/SmartAISystem/ |
| Migration Agents | his-vajira/apps/ever/microservices/ever-api-migration/src/api/migration/claude/ |
| Dify Client | ever-medos-global-pure-react/supabase/functions/_shared/difyClient.ts |
| PRM Personas | his-vajira/medbase/functions/prm-sidecars/dify/protocol-scout-persona.yaml |
| RCM AI Panel | ever-medos-global-pure-react/packages/rcm-kit/src/financial/components/revenue-settlement/components/RcmAiAssistantPanel.tsx |
| Plugin System | ever-medos-global-pure-react/src/common/components/medical/builder/main-flow-editor/components/plugins/ |
| AI Admin Page | ever-medos-global-pure-react/src/common/components/system-administration/ai-system/pages/AIAgentManagementPage.tsx |
| Workflow State | packages/medical-kit/src/medical-worklist/workflowState.ts |
| Intake Workflow | ever-medos-global-pure-react/src/services/intake-workflow/ |