File size: 3,226 Bytes
d4abe4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * WebSocket Message Types for ReAct Flow
 * Spec: BACKEND_WEBSOCKET_SPEC.md
 */

// Base message interface
export interface BaseMessage {
  type: string;
  timestamp: string; // ISO 8601 format
  session_id?: string;
}

// Thought message - AI reasoning
export interface ThoughtMessage extends BaseMessage {
  type: 'thought';
  content: string;
  variant?: 'initial' | 'intermediate' | 'final';
}

// Action start - Tool execution begins
export interface ActionStartMessage extends BaseMessage {
  type: 'action_start';
  tool_name: string;
  tool_display_name: string;
}

// Tool-specific result types
export interface CVPrediction {
  condition: string;
  confidence: number; // 0.0 to 1.0
}

export interface CVResults {
  predictions: CVPrediction[];
  visual_features?: string[];
  annotations?: Array<{
    bbox: [number, number, number, number];
    label: string;
    confidence: number;
  }>;
  model_info?: {
    name: string;
    accuracy: number;
    trained_on?: number;
  };
}

export interface RedFlag {
  symptom: string;
  risk: string;
  severity: 'low' | 'medium' | 'high' | 'critical';
}

export interface TriageResults {
  level: 'routine' | 'urgent' | 'emergency';
  reasoning: string;
  confidence: number;
  red_flags: RedFlag[];
  next_steps: string[];
}

export interface GuidelineResults {
  query: string;
  guidelines: Array<{
    title: string;
    content: string;
    source: string;
    relevance_score: number;
    url?: string;
  }>;
}

// Union type for all possible tool results
export type ToolResults = CVResults | TriageResults | GuidelineResults | Record<string, any>;

// Action complete - Tool execution finished
export interface ActionCompleteMessage extends BaseMessage {
  type: 'action_complete';
  tool_name: string;
  duration_ms: number;
  results: ToolResults;
}

// Action error - Tool execution failed
export interface ActionErrorMessage extends BaseMessage {
  type: 'action_error';
  tool_name: string;
  error_code: string;
  error_message: string;
  duration_ms: number;
}

// Observation - AI interprets tool results
export interface ObservationMessage extends BaseMessage {
  type: 'observation';
  tool_name: string;
  findings: any;
  confidence: number;
}

// Final answer - Agent completes
export interface FinalAnswerMessage extends BaseMessage {
  type: 'final_answer';
  result: {
    triage_level: string;
    recommendation: {
      action: string;
      timeframe: string;
      home_care_advice: string;
      warning_signs: string;
    };
    red_flags: RedFlag[];
  };
}

// Error message
export interface ErrorMessage extends BaseMessage {
  type: 'error';
  code: string;
  message: string;
}

// Union type for all message types
export type WebSocketMessage =
  | ThoughtMessage
  | ActionStartMessage
  | ActionCompleteMessage
  | ActionErrorMessage
  | ObservationMessage
  | FinalAnswerMessage
  | ErrorMessage;

// Tool display names mapping
export const TOOL_DISPLAY_NAMES: Record<string, string> = {
  derm_cv: 'Dermatology CV Analysis',
  eye_cv: 'Eye Condition Analysis',
  wound_cv: 'Wound Assessment',
  triage_rules: 'Triage Classification',
  guideline_retrieval: 'Medical Guidelines',
  rag_query: 'Medical Knowledge Retrieval',
};