-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathterminal.tsx
More file actions
184 lines (162 loc) · 5.49 KB
/
terminal.tsx
File metadata and controls
184 lines (162 loc) · 5.49 KB
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
"use client";
import { useRef, useCallback, useEffect, useMemo } from "react";
import { useRealtimeRunWithStreams } from "@trigger.dev/react-hooks";
import type { cursorAgentTask, STREAMS } from "@/trigger/cursor-agent";
import { parseCursorEvent } from "@/lib/cursor-events";
import { CursorEventRow } from "./cursor-event";
function getRunErrorMessage(output: unknown): string {
if (typeof output !== "object" || output === null) return "Task failed";
if ("message" in output && typeof output.message === "string") return output.message;
if ("error" in output && typeof output.error === "string") return output.error;
return "Task failed";
}
export function Terminal({
runId,
publicAccessToken,
onComplete,
}: {
runId: string;
publicAccessToken: string;
onComplete?: () => void;
}) {
const scrollRef = useRef<HTMLDivElement>(null);
const userScrolledUp = useRef(false);
const { run, streams, error } = useRealtimeRunWithStreams<typeof cursorAgentTask, STREAMS>(
runId,
{ accessToken: publicAccessToken }
);
const rawEvents = streams?.["cursor-events"] ?? [];
const events = useMemo(
() => rawEvents.map(parseCursorEvent).filter((e) => e !== null),
[rawEvents]
);
const workspaceCwd = useMemo(() => {
const systemEvent = events.find((e) => e.type === "system");
return systemEvent?.type === "system" ? systemEvent.cwd : undefined;
}, [events]);
const handleScroll = useCallback(() => {
const el = scrollRef.current;
if (!el) return;
const atBottom = el.scrollHeight - el.scrollTop - el.clientHeight < 40;
userScrolledUp.current = !atBottom;
}, []);
const status = run?.status;
const isRunning = status === "EXECUTING";
const isQueued = status === "PENDING_VERSION" || status === "DELAYED" || !status;
const isFailed = status === "FAILED" || status === "CRASHED" || status === "SYSTEM_FAILURE";
const isComplete = status === "COMPLETED";
const notified = useRef(false);
useEffect(() => {
if ((isComplete || isFailed) && !notified.current) {
notified.current = true;
onComplete?.();
}
}, [isComplete, isFailed, onComplete]);
useEffect(() => {
if (events.length > 0 && !userScrolledUp.current) {
requestAnimationFrame(() => {
scrollRef.current?.scrollTo({ top: scrollRef.current.scrollHeight, behavior: "smooth" });
});
}
}, [events.length]);
if (error) {
return (
<div className="rounded-xl border border-red-500/20 bg-red-500/5 p-4 font-mono text-sm text-red-400">
Connection error: {error.message}
</div>
);
}
const statusLabel = isQueued
? "Queued"
: isRunning
? "Running"
: isComplete
? "Complete"
: isFailed
? "Failed"
: "";
const statusDotClass = isQueued
? "bg-warning"
: isRunning
? "bg-accent"
: isComplete
? "bg-success"
: isFailed
? "bg-error"
: "bg-dim";
const statusTextClass = isQueued
? "text-warning"
: isRunning
? "text-accent"
: isComplete
? "text-success"
: isFailed
? "text-error"
: "text-dim";
return (
<div
className={`rounded-xl border overflow-hidden transition-colors duration-500 bg-terminal-bg ${
isRunning ? "animate-pulse-glow" : "border-border"
}`}
>
{/* Header */}
<div className="flex items-center justify-between px-5 py-3 border-b border-border bg-white/[0.012]">
<div className="flex items-center gap-2.5">
<span className="text-xs text-accent">◆</span>
<span className="text-xs font-mono text-muted">cursor-agent</span>
</div>
<div className="flex items-center gap-2">
<span
className={`w-1.5 h-1.5 rounded-full ${statusDotClass} ${isRunning || isQueued ? "animate-pulse" : ""}`}
/>
<span className={`text-[11px] font-mono ${statusTextClass}`}>
{statusLabel}
</span>
</div>
</div>
{/* Content */}
<div
ref={scrollRef}
onScroll={handleScroll}
className="p-5 font-mono text-sm overflow-y-auto max-h-[600px] min-h-[200px]"
>
{isQueued && (
<div className="flex items-center gap-2.5 text-dim text-xs">
<LoadingSpinner />
Waiting for worker...
</div>
)}
{events.length === 0 && isRunning && (
<div className="flex items-center gap-2.5 text-dim text-xs">
<LoadingSpinner />
Initializing cursor-agent...
</div>
)}
{events.map((event, i) => (
<CursorEventRow key={i} event={event} workspaceCwd={workspaceCwd} />
))}
{isRunning && <span className="inline-block w-[7px] h-[15px] bg-accent animate-cursor-blink ml-0.5 align-text-bottom rounded-[1px]" />}
{isFailed && (
<div className="mt-2 text-red-400 text-sm">{getRunErrorMessage(run?.output)}</div>
)}
{isComplete && events.length === 0 && (
<div className="text-dim text-xs">Completed with no output</div>
)}
</div>
</div>
);
}
function LoadingSpinner() {
return (
<svg className="animate-spin h-3 w-3 text-dim" viewBox="0 0 24 24" fill="none">
<circle className="opacity-20" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="3" />
<path
className="opacity-80"
d="M4 12a8 8 0 018-8"
stroke="currentColor"
strokeWidth="3"
strokeLinecap="round"
/>
</svg>
);
}