summaryrefslogtreecommitdiff
path: root/makima/frontend/src/components/mesh/TaskOutput.tsx
blob: d53429ddd7879aac33728ac35d9f32768572b02a (plain) (blame)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import { useRef, useEffect, useState, useCallback } from "react";
import { SimpleMarkdown } from "../SimpleMarkdown";
import type { TaskOutputEvent } from "../../hooks/useTaskSubscription";
import { sendTaskMessage } from "../../lib/api";

interface TaskOutputProps {
  /** Array of parsed output events from the backend */
  entries: TaskOutputEvent[];
  isStreaming: boolean;
  /** Name of subtask whose output is being viewed (null = parent task) */
  viewingSubtaskName?: string | null;
  /** Callback to return to parent task output */
  onClearSubtaskView?: () => void;
  onClear?: () => void;
  /** Task ID for sending input (if provided, shows input bar when streaming) */
  taskId?: string | null;
  /** Callback when user sends input (to show it immediately in output) */
  onUserInput?: (message: string) => void;
  /** Set of pending question IDs (for supervisor questions) */
  pendingQuestionIds?: Set<string>;
  /** Callback to answer a supervisor question */
  onAnswerQuestion?: (questionId: string, response: string) => Promise<void>;
}

export function TaskOutput({
  entries,
  isStreaming,
  viewingSubtaskName,
  onClearSubtaskView,
  onClear,
  taskId,
  onUserInput,
  pendingQuestionIds,
  onAnswerQuestion,
}: TaskOutputProps) {
  const containerRef = useRef<HTMLDivElement>(null);
  const [autoScroll, setAutoScroll] = useState(true);
  const [inputValue, setInputValue] = useState("");
  const [sendingInput, setSendingInput] = useState(false);
  const [inputError, setInputError] = useState<string | null>(null);
  const inputRef = useRef<HTMLInputElement>(null);

  // Handle scroll to check if user has scrolled up
  const handleScroll = useCallback(() => {
    if (!containerRef.current) return;
    const { scrollTop, scrollHeight, clientHeight } = containerRef.current;
    const isAtBottom = scrollHeight - scrollTop - clientHeight < 50;
    setAutoScroll(isAtBottom);
  }, []);

  // Auto-scroll when entries change
  useEffect(() => {
    if (autoScroll && containerRef.current) {
      containerRef.current.scrollTop = containerRef.current.scrollHeight;
    }
  }, [entries, autoScroll]);

  // Handle sending input to the task
  const handleSendInput = useCallback(async (e: React.FormEvent) => {
    e.preventDefault();
    if (!taskId || !inputValue.trim() || sendingInput) return;

    const message = inputValue.trim();
    setSendingInput(true);
    setInputError(null);

    // Show user input immediately in the output window
    onUserInput?.(message);

    try {
      await sendTaskMessage(taskId, message);
      setInputValue("");
      inputRef.current?.focus();
    } catch (err) {
      setInputError(err instanceof Error ? err.message : "Failed to send input");
    } finally {
      setSendingInput(false);
    }
  }, [taskId, inputValue, sendingInput, onUserInput]);

  // Show input bar when task is running and has a valid taskId
  const showInputBar = isStreaming && taskId;

  return (
    <div className="flex flex-col h-full">
      {/* Header */}
      <div className="flex items-center justify-between px-3 py-2 border-b border-dashed border-[rgba(117,170,252,0.35)] shrink-0">
        <div className="flex items-center gap-2">
          {viewingSubtaskName ? (
            <>
              <button
                onClick={onClearSubtaskView}
                className="font-mono text-xs text-[#75aafc] hover:text-[#9bc3ff] transition-colors"
              >
                &lt;
              </button>
              <span className="font-mono text-xs text-green-400 tracking-wide uppercase">
                Subtask: {viewingSubtaskName}
              </span>
            </>
          ) : (
            <span className="font-mono text-xs text-[#9bc3ff] tracking-wide uppercase">
              Output
            </span>
          )}
          {isStreaming && (
            <span className="flex items-center gap-1.5 px-2 py-0.5 bg-green-400/10 border border-green-400/30 text-green-400 font-mono text-[10px] uppercase">
              <span className="w-1.5 h-1.5 bg-green-400 rounded-full animate-pulse" />
              Live
            </span>
          )}
        </div>
        <div className="flex items-center gap-2">
          {!autoScroll && (
            <button
              onClick={() => {
                setAutoScroll(true);
                if (containerRef.current) {
                  containerRef.current.scrollTop =
                    containerRef.current.scrollHeight;
                }
              }}
              className="px-2 py-1 font-mono text-[10px] text-[#9bc3ff] border border-[rgba(117,170,252,0.25)] hover:border-[#3f6fb3] transition-colors uppercase"
            >
              Resume Scroll
            </button>
          )}
          {onClear && entries.length > 0 && (
            <button
              onClick={onClear}
              className="px-2 py-1 font-mono text-[10px] text-[#9bc3ff] border border-[rgba(117,170,252,0.25)] hover:border-[#3f6fb3] transition-colors uppercase"
            >
              Clear
            </button>
          )}
        </div>
      </div>

      {/* Output area */}
      <div
        ref={containerRef}
        onScroll={handleScroll}
        className="flex-1 overflow-auto bg-[#0a0f18] p-3 font-mono text-xs min-h-0"
      >
        {entries.length === 0 ? (
          <div className="text-[#555] italic">
            {isStreaming ? "Waiting for output..." : "No output yet"}
          </div>
        ) : (
          <div className="space-y-3">
            {entries.map((entry, idx) => (
              <OutputEntryRenderer
                key={idx}
                entry={entry}
                pendingQuestionIds={pendingQuestionIds}
                onAnswerQuestion={onAnswerQuestion}
              />
            ))}
            {isStreaming && (
              <span className="inline-block w-2 h-4 bg-[#9bc3ff] animate-pulse" />
            )}
          </div>
        )}
      </div>

      {/* Input bar for sending messages to running tasks */}
      {showInputBar && (
        <div className="shrink-0 border-t border-[rgba(117,170,252,0.35)] bg-[#0d1b2d]">
          {inputError && (
            <div className="px-3 py-1 bg-red-900/20 text-red-400 text-xs font-mono">
              {inputError}
            </div>
          )}
          <form onSubmit={handleSendInput} className="flex items-center gap-2 px-3 py-2">
            <span className="text-green-400 font-mono text-sm">&gt;</span>
            <input
              ref={inputRef}
              type="text"
              value={inputValue}
              onChange={(e) => setInputValue(e.target.value)}
              placeholder={sendingInput ? "Sending..." : "Send input to Claude..."}
              disabled={sendingInput}
              className="flex-1 bg-transparent border-none outline-none font-mono text-sm text-white placeholder-[#555]"
            />
            <button
              type="submit"
              disabled={sendingInput || !inputValue.trim()}
              className="px-3 py-1 font-mono text-xs text-green-400 border border-green-400/30 hover:border-green-400/50 hover:bg-green-400/10 disabled:opacity-50 disabled:cursor-not-allowed transition-colors uppercase"
            >
              {sendingInput ? "..." : "Send"}
            </button>
          </form>
        </div>
      )}
    </div>
  );
}

interface OutputEntryRendererProps {
  entry: TaskOutputEvent;
  pendingQuestionIds?: Set<string>;
  onAnswerQuestion?: (questionId: string, response: string) => Promise<void>;
}

function OutputEntryRenderer({ entry, pendingQuestionIds, onAnswerQuestion }: OutputEntryRendererProps) {
  const [expanded, setExpanded] = useState(false);

  switch (entry.messageType) {
    case "user_input":
      return (
        <div className="pl-2 border-l-2 border-cyan-400/50">
          <div className="flex items-center gap-2">
            <span className="text-cyan-400 text-[10px] uppercase tracking-wide">You:</span>
          </div>
          <div className="text-cyan-300 mt-1">{entry.content}</div>
        </div>
      );

    case "system":
      return (
        <div className="text-[#555] text-[10px] uppercase tracking-wide">
          {entry.content}
        </div>
      );

    case "assistant":
      return (
        <div className="pl-2 border-l-2 border-[#3f6fb3]">
          <SimpleMarkdown content={entry.content} className="text-[#9bc3ff]" />
        </div>
      );

    case "tool_use":
      return (
        <div className="space-y-1">
          <div className="flex items-center gap-2">
            <span className="text-yellow-500">*</span>
            <span className="text-[#75aafc]">{entry.toolName || "unknown"}</span>
            {entry.toolInput && Object.keys(entry.toolInput).length > 0 && (
              <button
                onClick={() => setExpanded(!expanded)}
                className="text-[#555] hover:text-[#9bc3ff] text-[10px]"
              >
                {expanded ? "[-]" : "[+]"}
              </button>
            )}
          </div>
          {expanded && entry.toolInput && (
            <pre className="ml-4 text-[10px] text-[#555] bg-[#0a1525] p-2 overflow-x-auto">
              {JSON.stringify(entry.toolInput, null, 2)}
            </pre>
          )}
        </div>
      );

    case "tool_result":
      if (!entry.content) return null;
      return (
        <div className="ml-4 text-[10px]">
          <span className={entry.isError ? "text-red-400" : "text-green-500"}>
            {entry.isError ? "x" : "+"}
          </span>{" "}
          <span className="text-[#555]">
            {entry.content.split("\n")[0]}
            {entry.content.includes("\n") && "..."}
          </span>
        </div>
      );

    case "result":
      return (
        <div className="border-t border-[rgba(117,170,252,0.2)] pt-2 mt-2">
          <div className="text-green-500 font-semibold mb-1">Result:</div>
          <SimpleMarkdown content={entry.content} className="text-[#9bc3ff]" />
          {(entry.costUsd !== undefined || entry.durationMs !== undefined) && (
            <div className="text-[10px] text-[#555] mt-2">
              {entry.durationMs !== undefined && (
                <span>Duration: {(entry.durationMs / 1000).toFixed(1)}s</span>
              )}
              {entry.costUsd !== undefined && entry.durationMs !== undefined && " | "}
              {entry.costUsd !== undefined && (
                <span>Cost: ${entry.costUsd.toFixed(4)}</span>
              )}
            </div>
          )}
        </div>
      );

    case "error":
      return (
        <div className="text-red-400 pl-2 border-l-2 border-red-400/50">
          {entry.content}
        </div>
      );

    case "raw":
      return (
        <div className="text-[#555] text-[10px]">
          {entry.content}
        </div>
      );

    case "auth_required":
      return <AuthRequiredEntry entry={entry} />;

    case "supervisor_question":
      return (
        <SupervisorQuestionEntry
          entry={entry}
          pendingQuestionIds={pendingQuestionIds}
          onAnswerQuestion={onAnswerQuestion}
        />
      );

    default:
      return null;
  }
}

function SupervisorQuestionEntry({
  entry,
  pendingQuestionIds,
  onAnswerQuestion,
}: {
  entry: TaskOutputEvent;
  pendingQuestionIds?: Set<string>;
  onAnswerQuestion?: (questionId: string, response: string) => Promise<void>;
}) {
  const questionId = entry.toolInput?.question_id as string;
  const choices = (entry.toolInput?.choices as string[]) || [];
  const context = entry.toolInput?.context as string | null;

  const [customInput, setCustomInput] = useState("");
  const [showOther, setShowOther] = useState(false);
  const [submitting, setSubmitting] = useState(false);

  const isPending = pendingQuestionIds?.has(questionId) ?? false;

  const handleChoiceSelect = async (choice: string) => {
    if (!onAnswerQuestion || submitting) return;
    setSubmitting(true);
    try {
      await onAnswerQuestion(questionId, choice);
    } finally {
      setSubmitting(false);
    }
  };

  const handleOtherSubmit = async () => {
    if (!onAnswerQuestion || !customInput.trim() || submitting) return;
    setSubmitting(true);
    try {
      await onAnswerQuestion(questionId, customInput.trim());
      setCustomInput("");
    } finally {
      setSubmitting(false);
    }
  };

  return (
    <div className="bg-amber-900/20 border border-amber-500/50 rounded p-3 my-2">
      <div className="flex items-center gap-2 text-amber-400 font-semibold mb-2">
        <span>?</span>
        <span>Question</span>
        {!isPending && (
          <span className="text-green-400 text-xs font-normal">(Answered)</span>
        )}
      </div>

      {context && (
        <p className="text-amber-200/60 text-xs mb-2 uppercase">{context}</p>
      )}

      <p className="text-amber-100 mb-3">{entry.content}</p>

      {isPending && (
        <div className="space-y-2">
          {choices.length > 0 && (
            <div className="flex flex-wrap gap-2">
              {choices.map((choice, idx) => (
                <button
                  key={idx}
                  onClick={() => handleChoiceSelect(choice)}
                  disabled={submitting}
                  className="px-3 py-1.5 text-sm font-mono bg-amber-500/20 border border-amber-500/50 hover:bg-amber-500/30 disabled:opacity-50 text-amber-100 transition-colors"
                >
                  {choice}
                </button>
              ))}
            </div>
          )}

          {/* Other option */}
          {!showOther ? (
            <button
              onClick={() => setShowOther(true)}
              className="text-xs text-amber-400 hover:text-amber-300 transition-colors"
            >
              + Other (custom response)
            </button>
          ) : (
            <div className="flex gap-2">
              <input
                type="text"
                value={customInput}
                onChange={(e) => setCustomInput(e.target.value)}
                placeholder="Type custom response..."
                disabled={submitting}
                className="flex-1 px-2 py-1 bg-[#0a1525] border border-amber-500/30 text-amber-100 text-sm rounded focus:outline-none focus:border-amber-400"
                onKeyDown={(e) => {
                  if (e.key === "Enter" && customInput.trim()) {
                    handleOtherSubmit();
                  }
                }}
              />
              <button
                onClick={handleOtherSubmit}
                disabled={submitting || !customInput.trim()}
                className="px-3 py-1 bg-amber-500 text-black text-sm font-medium rounded disabled:opacity-50 disabled:cursor-not-allowed transition-colors hover:bg-amber-400"
              >
                {submitting ? "..." : "Submit"}
              </button>
            </div>
          )}
        </div>
      )}
    </div>
  );
}

function AuthRequiredEntry({ entry }: { entry: TaskOutputEvent }) {
  const [authCode, setAuthCode] = useState("");
  const [submitting, setSubmitting] = useState(false);
  const [submitted, setSubmitted] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const loginUrl = entry.toolInput?.loginUrl as string | undefined;
  const hostname = entry.toolInput?.hostname as string | undefined;
  // Get taskId from entry or fallback to toolInput (for robustness)
  const taskId = entry.taskId || (entry.toolInput?.taskId as string | undefined);

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    if (!authCode.trim() || !taskId) return;

    setSubmitting(true);
    setError(null);

    try {
      // Send the auth code to the task via the message endpoint
      await sendTaskMessage(taskId, `AUTH_CODE:${authCode.trim()}`);
      setSubmitted(true);
    } catch (err) {
      setError(err instanceof Error ? err.message : "Failed to submit code");
    } finally {
      setSubmitting(false);
    }
  };

  if (submitted) {
    return (
      <div className="bg-green-900/30 border border-green-500/50 rounded p-3 my-2">
        <div className="flex items-center gap-2 text-green-400 font-semibold">
          <span>✓</span>
          <span>Authentication code submitted</span>
        </div>
        <p className="text-green-200/80 text-sm mt-1">
          Waiting for authentication to complete...
        </p>
      </div>
    );
  }

  return (
    <div className="bg-amber-900/30 border border-amber-500/50 rounded p-3 my-2">
      <div className="flex items-center gap-2 text-amber-400 font-semibold mb-2">
        <span>🔐</span>
        <span>Authentication Required{hostname ? ` (${hostname})` : ""}</span>
      </div>
      <p className="text-amber-200/80 text-sm mb-3">
        The daemon's OAuth token has expired. Click the button to login, then paste the code below:
      </p>

      <div className="flex flex-col gap-3">
        {loginUrl ? (
          <a
            href={loginUrl}
            target="_blank"
            rel="noopener noreferrer"
            className="inline-block bg-amber-500 hover:bg-amber-400 text-black font-medium px-4 py-2 rounded transition-colors text-center"
          >
            1. Login to Claude
          </a>
        ) : (
          <p className="text-red-400 text-sm">Login URL not available</p>
        )}

        <form onSubmit={handleSubmit} className="flex gap-2">
          <input
            type="text"
            value={authCode}
            onChange={(e) => setAuthCode(e.target.value)}
            placeholder="2. Paste authentication code here"
            className="flex-1 bg-[#0a1525] border border-amber-500/30 rounded px-3 py-2 text-amber-100 placeholder-amber-500/50 focus:outline-none focus:border-amber-400"
            disabled={submitting}
          />
          <button
            type="submit"
            disabled={submitting || !authCode.trim()}
            className="bg-amber-500 hover:bg-amber-400 disabled:bg-amber-700 disabled:cursor-not-allowed text-black font-medium px-4 py-2 rounded transition-colors"
          >
            {submitting ? "..." : "Submit"}
          </button>
        </form>

        {error && (
          <p className="text-red-400 text-sm">{error}</p>
        )}
      </div>
    </div>
  );
}