File size: 10,397 Bytes
054d73a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { useState, useEffect } from "react";

interface StepData {
  id: string;
  status: "pending" | "active" | "completed" | "error";
  detail?: string;
  timestamp?: string;
}

interface StatusResponse {
  current_step: string | null;
  steps: StepData[];
  started_at: string | null;
  completed_at: string | null;
}

interface WorkflowStep {
  id: string;
  label: string;
  icon: JSX.Element;
  status: "pending" | "active" | "completed" | "error";
  detail?: string;
}

interface WorkflowStatusProps {
  sessionId?: string;
}

const STEP_CONFIG: { id: string; label: string; icon: JSX.Element; defaultDetail: string }[] = [
  {
    id: "fetch",
    label: "Fetch Responses",
    icon: (
      <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4" />
      </svg>
    ),
    defaultDetail: "Getting data from Google Sheets",
  },
  {
    id: "normalize",
    label: "Parse Data",
    icon: (
      <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" />
      </svg>
    ),
    defaultDetail: "Organizing questions and answers",
  },
  {
    id: "grade",
    label: "Grade Answers",
    icon: (
      <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
      </svg>
    ),
    defaultDetail: "Comparing with correct answers",
  },
  {
    id: "explain",
    label: "Generate Explanations",
    icon: (
      <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z" />
      </svg>
    ),
    defaultDetail: "Explaining wrong answers",
  },
  {
    id: "group",
    label: "Create Peer Groups",
    icon: (
      <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0z" />
      </svg>
    ),
    defaultDetail: "Matching helpers with learners",
  },
  {
    id: "report",
    label: "Generate Report",
    icon: (
      <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 17v-2m3 2v-4m3 4v-6m2 10H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
      </svg>
    ),
    defaultDetail: "Creating comprehensive report",
  },
  {
    id: "email",
    label: "Send Email",
    icon: (
      <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" />
      </svg>
    ),
    defaultDetail: "Delivering report to inbox",
  },
];

export function WorkflowStatus({ sessionId = "default" }: WorkflowStatusProps) {
  const [steps, setSteps] = useState<WorkflowStep[]>(
    STEP_CONFIG.map(s => ({ ...s, status: "pending" as const, detail: s.defaultDetail }))
  );
  const [isConnected, setIsConnected] = useState(false);
  const [isAnalyzing, setIsAnalyzing] = useState(false);

  useEffect(() => {
    let eventSource: EventSource | null = null;
    let retryCount = 0;
    const maxRetries = 3;

    const connect = () => {
      eventSource = new EventSource(`/api/status/${sessionId}/stream`);

      eventSource.onopen = () => {
        setIsConnected(true);
        retryCount = 0;
      };

      eventSource.onmessage = (event) => {
        try {
          const data: StatusResponse = JSON.parse(event.data);
          
          // Check if there's any activity
          const hasActivity = data.steps && data.steps.length > 0;
          setIsAnalyzing(hasActivity && !data.completed_at);
          
          // Update steps based on server data
          setSteps(prevSteps => {
            return prevSteps.map(step => {
              const serverStep = data.steps?.find(s => s.id === step.id);
              if (serverStep) {
                return {
                  ...step,
                  status: serverStep.status as WorkflowStep["status"],
                  detail: serverStep.detail || step.detail,
                };
              }
              return step;
            });
          });
        } catch (e) {
          console.error("Error parsing status:", e);
        }
      };

      eventSource.onerror = () => {
        setIsConnected(false);
        eventSource?.close();
        
        // Retry connection
        if (retryCount < maxRetries) {
          retryCount++;
          setTimeout(connect, 2000 * retryCount);
        }
      };
    };

    connect();

    return () => {
      eventSource?.close();
    };
  }, [sessionId]);

  const getStatusColor = (status: WorkflowStep["status"]) => {
    switch (status) {
      case "completed": return "bg-[var(--color-success)] text-white";
      case "active": return "bg-[var(--color-accent)] text-white animate-pulse";
      case "error": return "bg-red-500 text-white";
      default: return "bg-[var(--color-border)] text-[var(--color-text-muted)]";
    }
  };

  const getLineColor = (status: WorkflowStep["status"]) => {
    switch (status) {
      case "completed": return "bg-[var(--color-success)]";
      case "active": return "bg-[var(--color-accent)]";
      default: return "bg-[var(--color-border)]";
    }
  };

  const activeStep = steps.find(s => s.status === "active");
  const completedCount = steps.filter(s => s.status === "completed").length;

  return (
    <div className="card p-4">
      {/* Header */}
      <div className="flex items-center justify-between mb-4">
        <div className="flex items-center gap-2">
          <div className={`w-2 h-2 rounded-full ${isAnalyzing ? 'bg-[var(--color-accent)] animate-pulse' : isConnected ? 'bg-[var(--color-success)]' : 'bg-[var(--color-border)]'}`} />
          <h3 className="font-display text-sm font-semibold text-[var(--color-text)]">
            {isAnalyzing ? "AI Agent Working..." : "Agent Status"}
          </h3>
        </div>
        {completedCount > 0 && (
          <span className="text-xs text-[var(--color-text-muted)]">
            {completedCount}/{steps.length}
          </span>
        )}
      </div>

      {/* Active step highlight */}
      {activeStep && (
        <div className="mb-4 p-3 rounded-lg bg-[var(--color-accent)]/10 border border-[var(--color-accent)]/20">
          <div className="flex items-center gap-2">
            <div className="w-5 h-5 rounded-full bg-[var(--color-accent)] flex items-center justify-center animate-pulse">
              <div className="w-2 h-2 bg-white rounded-full" />
            </div>
            <div>
              <div className="text-sm font-medium text-[var(--color-accent)]">
                {activeStep.label}
              </div>
              <div className="text-xs text-[var(--color-text-muted)]">
                {activeStep.detail}
              </div>
            </div>
          </div>
        </div>
      )}
      
      {/* Steps list */}
      <div className="space-y-1">
        {steps.map((step, index) => (
          <div key={step.id} className="relative">
            {/* Connector line */}
            {index < steps.length - 1 && (
              <div 
                className={`absolute left-[11px] top-[24px] w-0.5 h-4 transition-colors duration-300 ${getLineColor(step.status)}`}
              />
            )}
            
            <div className="flex items-center gap-3 py-1">
              {/* Icon circle */}
              <div 
                className={`flex-shrink-0 w-6 h-6 rounded-full flex items-center justify-center transition-all duration-300 ${getStatusColor(step.status)}`}
              >
                {step.status === "completed" ? (
                  <svg className="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={3} d="M5 13l4 4L19 7" />
                  </svg>
                ) : step.status === "active" ? (
                  <div className="w-2 h-2 bg-white rounded-full" />
                ) : step.status === "error" ? (
                  <svg className="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={3} d="M6 18L18 6M6 6l12 12" />
                  </svg>
                ) : (
                  <span className="text-[10px]">{index + 1}</span>
                )}
              </div>
              
              {/* Label */}
              <div className={`text-sm transition-colors ${
                step.status === "active" 
                  ? "font-medium text-[var(--color-accent)]" 
                  : step.status === "completed"
                  ? "text-[var(--color-success)]"
                  : step.status === "error"
                  ? "text-red-500"
                  : "text-[var(--color-text-muted)]"
              }`}>
                {step.label}
              </div>
            </div>
          </div>
        ))}
      </div>

      {/* Connection status */}
      <div className="mt-4 pt-3 border-t border-[var(--color-border)]">
        <div className="flex items-center gap-2 text-xs text-[var(--color-text-muted)]">
          <div className={`w-1.5 h-1.5 rounded-full ${isConnected ? 'bg-green-500' : 'bg-gray-400'}`} />
          {isConnected ? "Live updates" : "Connecting..."}
        </div>
      </div>
    </div>
  );
}