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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
|
import { useState, useMemo, useEffect, useRef } from "react";
import type { DirectiveWithSteps, DirectiveStatus, MemoryCategory } from "../../lib/api";
import { DirectiveDAG } from "./DirectiveDAG";
import { DirectiveLogStream } from "./DirectiveLogStream";
import { useDirectiveMemories } from "../../hooks/useDirectiveMemories";
import { useMultiTaskSubscription } from "../../hooks/useMultiTaskSubscription";
const STATUS_BADGE: Record<DirectiveStatus, { color: string; label: string }> = {
draft: { color: "text-[#7788aa] border-[#2a3a5a]", label: "DRAFT" },
active: { color: "text-green-400 border-green-800", label: "ACTIVE" },
idle: { color: "text-yellow-400 border-yellow-800", label: "IDLE" },
paused: { color: "text-orange-400 border-orange-800", label: "PAUSED" },
archived: { color: "text-[#556677] border-[#2a3a5a]", label: "ARCHIVED" },
};
const CATEGORY_COLORS: Record<MemoryCategory, { text: string; border: string; bg: string; label: string }> = {
decision: { text: "text-amber-400", border: "border-amber-800", bg: "bg-amber-900/20", label: "Decision" },
context: { text: "text-cyan-400", border: "border-cyan-800", bg: "bg-cyan-900/20", label: "Context" },
preference: { text: "text-violet-400", border: "border-violet-800", bg: "bg-violet-900/20", label: "Preference" },
learning: { text: "text-emerald-400", border: "border-emerald-800", bg: "bg-emerald-900/20", label: "Learning" },
other: { text: "text-[#7788aa]", border: "border-[#2a3a5a]", bg: "bg-[#1a2540]", label: "Other" },
};
const ALL_CATEGORIES: MemoryCategory[] = ["decision", "context", "preference", "learning", "other"];
interface DirectiveDetailProps {
directive: DirectiveWithSteps;
onStart: () => void;
onPause: () => void;
onAdvance: () => void;
onCompleteStep: (stepId: string) => void;
onFailStep: (stepId: string) => void;
onSkipStep: (stepId: string) => void;
onUpdateGoal: (goal: string) => void;
onDelete: () => void;
onRefresh: () => void;
}
export function DirectiveDetail({
directive,
onStart,
onPause,
onAdvance,
onCompleteStep,
onFailStep,
onSkipStep,
onUpdateGoal,
onDelete,
onRefresh,
}: DirectiveDetailProps) {
const [editingGoal, setEditingGoal] = useState(false);
const [goalText, setGoalText] = useState(directive.goal);
const [visibleTaskIds, setVisibleTaskIds] = useState<Set<string> | null>(null);
const [searchQuery, setSearchQuery] = useState("");
const [isLogCollapsed, setIsLogCollapsed] = useState(true);
const prevHadRunningRef = useRef(false);
const badge = STATUS_BADGE[directive.status] || STATUS_BADGE.draft;
const completedSteps = directive.steps.filter((s) => s.status === "completed").length;
const totalSteps = directive.steps.length;
const progress = totalSteps > 0 ? Math.round((completedSteps / totalSteps) * 100) : 0;
// Memory panel state
const [memoryOpen, setMemoryOpen] = useState(false);
const [addingMemory, setAddingMemory] = useState(false);
const [newCategory, setNewCategory] = useState<MemoryCategory>("context");
const [newContent, setNewContent] = useState("");
const [newSource, setNewSource] = useState("");
const [confirmClear, setConfirmClear] = useState(false);
const {
grouped,
config: memoryConfig,
loading: memoryLoading,
error: memoryError,
toggleEnabled,
add: addMemory,
remove: removeMemory,
clearAll: clearMemories,
refresh: refreshMemories,
} = useDirectiveMemories(directive.id);
const memoryEnabled = memoryConfig?.enabled ?? false;
const totalMemories = Object.values(grouped).reduce((sum, arr) => sum + arr.length, 0);
// Build task map from directive steps and orchestrator
const taskMap = useMemo(() => {
const map = new Map<string, string>();
if (directive.orchestratorTaskId) {
map.set(directive.orchestratorTaskId, "Orchestrator");
}
for (const step of directive.steps) {
if (step.taskId) {
map.set(step.taskId, step.name);
}
}
return map;
}, [directive.orchestratorTaskId, directive.steps]);
// Subscribe to all task outputs
const { connected, entries, clearEntries } = useMultiTaskSubscription({
taskMap,
enabled: taskMap.size > 0,
});
// Auto-expand log panel when tasks start running
const hasRunningTasks = directive.steps.some((s) => s.status === "running") ||
!!directive.orchestratorTaskId;
useEffect(() => {
if (hasRunningTasks && !prevHadRunningRef.current) {
setIsLogCollapsed(false);
}
prevHadRunningRef.current = hasRunningTasks;
}, [hasRunningTasks]);
const handleGoalSave = () => {
if (goalText.trim() && goalText !== directive.goal) {
onUpdateGoal(goalText.trim());
}
setEditingGoal(false);
};
const handleAddMemory = async () => {
if (!newContent.trim()) return;
await addMemory({
category: newCategory,
content: newContent.trim(),
source: newSource.trim() || undefined,
});
setNewContent("");
setNewSource("");
setAddingMemory(false);
};
const handleClearAll = async () => {
await clearMemories();
setConfirmClear(false);
};
return (
<div className="flex flex-col h-full overflow-y-auto">
{/* Header */}
<div className="px-4 py-3 border-b border-dashed border-[rgba(117,170,252,0.2)]">
<div className="flex items-center justify-between mb-2">
<h2 className="text-[14px] font-mono text-white font-medium truncate pr-2">
{directive.title}
</h2>
<div className="flex items-center gap-2 shrink-0">
<span
className={`text-[10px] font-mono ${badge.color} border rounded px-2 py-0.5`}
>
{badge.label}
</span>
<button
type="button"
onClick={onRefresh}
className="text-[10px] font-mono text-[#7788aa] hover:text-white"
title="Refresh"
>
[refresh]
</button>
</div>
</div>
{/* Progress bar */}
{totalSteps > 0 && (
<div className="flex items-center gap-2 mb-2">
<div className="flex-1 h-1.5 bg-[#1a2540] rounded overflow-hidden">
<div
className="h-full bg-emerald-600 rounded transition-all"
style={{ width: `${progress}%` }}
/>
</div>
<span className="text-[10px] font-mono text-[#7788aa] shrink-0">
{completedSteps}/{totalSteps} steps
</span>
</div>
)}
{/* Repo info */}
{(directive.repositoryUrl || directive.localPath) && (
<div className="text-[10px] font-mono text-[#556677] mb-2 truncate">
{directive.repositoryUrl || directive.localPath}
{directive.baseBranch && ` @ ${directive.baseBranch}`}
</div>
)}
{/* Orchestrator planning indicator */}
{directive.orchestratorTaskId && (
<div className="flex items-center gap-2 mb-2 px-2 py-1.5 bg-[#1a1a30] border border-[rgba(117,170,252,0.2)] rounded">
<span className="inline-block w-2 h-2 rounded-full bg-[#75aafc] animate-pulse" />
<span className="text-[10px] font-mono text-[#75aafc]">
Planning in progress...
</span>
<a
href={`/mesh/${directive.orchestratorTaskId}`}
className="text-[9px] font-mono text-[#556677] hover:text-[#75aafc] underline ml-auto"
>
View task
</a>
</div>
)}
{/* PR link */}
{directive.prUrl && (
<div className="flex items-center gap-2 mb-2 px-2 py-1.5 bg-[#0a1a10] border border-emerald-900 rounded">
<span className="inline-block w-2 h-2 rounded-full bg-emerald-400" />
<span className="text-[10px] font-mono text-emerald-400">
PR created
</span>
<a
href={directive.prUrl}
target="_blank"
rel="noopener noreferrer"
className="text-[10px] font-mono text-emerald-400 hover:text-emerald-300 underline ml-auto truncate max-w-[200px]"
>
{directive.prUrl}
</a>
</div>
)}
{/* Completion task indicator */}
{directive.completionTaskId && !directive.prUrl && (
<div className="flex items-center gap-2 mb-2 px-2 py-1.5 bg-[#1a1a10] border border-yellow-900 rounded">
<span className="inline-block w-2 h-2 rounded-full bg-yellow-400 animate-pulse" />
<span className="text-[10px] font-mono text-yellow-400">
Creating PR...
</span>
<a
href={`/mesh/${directive.completionTaskId}`}
className="text-[9px] font-mono text-[#556677] hover:text-yellow-400 underline ml-auto"
>
View task
</a>
</div>
)}
{/* Controls */}
<div className="flex flex-wrap gap-2">
{(directive.status === "draft" || directive.status === "paused") && (
<button
type="button"
onClick={onStart}
className="text-[10px] font-mono text-green-400 hover:text-green-300 border border-green-800 rounded px-2 py-1"
>
Start
</button>
)}
{directive.status === "active" && (
<>
<button
type="button"
onClick={onPause}
className="text-[10px] font-mono text-orange-400 hover:text-orange-300 border border-orange-800 rounded px-2 py-1"
>
Pause
</button>
<button
type="button"
onClick={onAdvance}
className="text-[10px] font-mono text-[#75aafc] hover:text-white border border-[rgba(117,170,252,0.3)] rounded px-2 py-1"
>
Advance
</button>
</>
)}
{directive.status === "idle" && (
<div className="flex items-center gap-2">
<span className="text-[10px] font-mono text-yellow-400">
All steps done. Update goal to add new work.
</span>
<button
type="button"
onClick={() => setEditingGoal(true)}
className="text-[10px] font-mono text-[#75aafc] hover:text-white border border-[rgba(117,170,252,0.3)] rounded px-2 py-1"
>
Update Goal
</button>
</div>
)}
<button
type="button"
onClick={onDelete}
className="text-[10px] font-mono text-red-400 hover:text-red-300 border border-red-800 rounded px-2 py-1 ml-auto"
>
Delete
</button>
</div>
</div>
{/* Goal */}
<div className="px-4 py-3 border-b border-[rgba(117,170,252,0.1)]">
<div className="flex items-center justify-between mb-1">
<span className="text-[10px] font-mono text-[#9bc3ff] uppercase tracking-wide">
Goal
</span>
{!editingGoal && (
<button
type="button"
onClick={() => { setGoalText(directive.goal); setEditingGoal(true); }}
className="text-[9px] font-mono text-[#556677] hover:text-[#75aafc]"
>
[edit]
</button>
)}
</div>
{editingGoal ? (
<div className="flex flex-col gap-1.5">
<textarea
value={goalText}
onChange={(e) => setGoalText(e.target.value)}
className="w-full bg-[#0a1628] border border-[rgba(117,170,252,0.2)] rounded px-2 py-1.5 text-[11px] font-mono text-white resize-y min-h-[60px]"
rows={3}
/>
<div className="flex gap-1.5">
<button
type="button"
onClick={handleGoalSave}
className="text-[10px] font-mono text-emerald-400 hover:text-emerald-300 border border-emerald-800 rounded px-2 py-0.5"
>
Save
</button>
<button
type="button"
onClick={() => setEditingGoal(false)}
className="text-[10px] font-mono text-[#7788aa] hover:text-white border border-[#2a3a5a] rounded px-2 py-0.5"
>
Cancel
</button>
</div>
</div>
) : (
<p className="text-[11px] font-mono text-[#c0d0e0] whitespace-pre-wrap">
{directive.goal}
</p>
)}
</div>
{/* Memory Panel */}
<div className="px-4 py-3 border-b border-[rgba(117,170,252,0.1)]">
{/* Memory header — always visible */}
<div className="flex items-center justify-between">
<button
type="button"
onClick={() => setMemoryOpen((v) => !v)}
className="flex items-center gap-1.5 group"
>
<span className="text-[10px] font-mono text-[#556677] group-hover:text-[#9bc3ff] transition-colors">
{memoryOpen ? "\u25BC" : "\u25B6"}
</span>
<span className="text-[10px] font-mono text-[#9bc3ff] uppercase tracking-wide">
Memory
</span>
{totalMemories > 0 && (
<span className="text-[9px] font-mono text-[#556677] ml-1">
({totalMemories})
</span>
)}
</button>
<div className="flex items-center gap-2">
{/* Enable/disable toggle */}
<button
type="button"
onClick={() => toggleEnabled(!memoryEnabled)}
className={`text-[9px] font-mono border rounded px-1.5 py-0.5 transition-colors ${
memoryEnabled
? "text-emerald-400 border-emerald-800 hover:text-emerald-300"
: "text-[#556677] border-[#2a3a5a] hover:text-[#7788aa]"
}`}
title={memoryEnabled ? "Disable memory" : "Enable memory"}
>
{memoryEnabled ? "ON" : "OFF"}
</button>
</div>
</div>
{/* Collapsible content */}
{memoryOpen && (
<div className="mt-2">
{memoryError && (
<div className="text-[10px] font-mono text-red-400 mb-2 px-2 py-1 bg-red-900/10 border border-red-800/30 rounded">
{memoryError}
</div>
)}
{memoryLoading ? (
<div className="text-[10px] font-mono text-[#556677] py-2">Loading...</div>
) : totalMemories === 0 ? (
<div className="text-[10px] font-mono text-[#556677] py-2">
No memory entries yet.
{!memoryEnabled && " Enable memory to start capturing entries."}
</div>
) : (
/* Grouped entries */
<div className="flex flex-col gap-2">
{ALL_CATEGORIES.map((cat) => {
const entries = grouped[cat];
if (entries.length === 0) return null;
const style = CATEGORY_COLORS[cat];
return (
<div key={cat}>
<div className="flex items-center gap-1.5 mb-1">
<span className={`text-[9px] font-mono ${style.text} uppercase tracking-wider`}>
{style.label}
</span>
<span className="text-[9px] font-mono text-[#556677]">
({entries.length})
</span>
</div>
<div className="flex flex-col gap-1">
{entries.map((entry) => (
<div
key={entry.id}
className={`flex items-start gap-2 px-2 py-1.5 rounded border ${style.border} ${style.bg}`}
>
<div className="flex-1 min-w-0">
<p className="text-[10px] font-mono text-[#c0d0e0] whitespace-pre-wrap break-words">
{entry.content}
</p>
{entry.source && (
<span className="text-[9px] font-mono text-[#556677] mt-0.5 block">
src: {entry.source}
</span>
)}
</div>
<button
type="button"
onClick={() => removeMemory(entry.id)}
className="text-[9px] font-mono text-[#556677] hover:text-red-400 shrink-0 mt-0.5"
title="Delete entry"
>
x
</button>
</div>
))}
</div>
</div>
);
})}
</div>
)}
{/* Action bar: Add + Clear */}
<div className="flex items-center gap-2 mt-2 pt-2 border-t border-[rgba(117,170,252,0.1)]">
<button
type="button"
onClick={() => setAddingMemory((v) => !v)}
className="text-[10px] font-mono text-[#75aafc] hover:text-white border border-[rgba(117,170,252,0.2)] rounded px-2 py-0.5"
>
{addingMemory ? "Cancel" : "+ Add"}
</button>
{totalMemories > 0 && (
<>
{confirmClear ? (
<div className="flex items-center gap-1.5 ml-auto">
<span className="text-[9px] font-mono text-red-400">Clear all?</span>
<button
type="button"
onClick={handleClearAll}
className="text-[9px] font-mono text-red-400 hover:text-red-300 border border-red-800 rounded px-1.5 py-0.5"
>
Yes
</button>
<button
type="button"
onClick={() => setConfirmClear(false)}
className="text-[9px] font-mono text-[#7788aa] hover:text-white border border-[#2a3a5a] rounded px-1.5 py-0.5"
>
No
</button>
</div>
) : (
<button
type="button"
onClick={() => setConfirmClear(true)}
className="text-[10px] font-mono text-[#556677] hover:text-red-400 ml-auto"
>
Clear all
</button>
)}
</>
)}
<button
type="button"
onClick={refreshMemories}
className="text-[9px] font-mono text-[#556677] hover:text-[#75aafc]"
title="Refresh memories"
>
[refresh]
</button>
</div>
{/* Add form */}
{addingMemory && (
<div className="mt-2 p-2 bg-[#0a1628] border border-[rgba(117,170,252,0.15)] rounded flex flex-col gap-2">
<div className="flex items-center gap-2">
<label className="text-[9px] font-mono text-[#7788aa] shrink-0">Category</label>
<select
value={newCategory}
onChange={(e) => setNewCategory(e.target.value as MemoryCategory)}
className="bg-[#1a2540] border border-[rgba(117,170,252,0.2)] rounded px-1.5 py-0.5 text-[10px] font-mono text-white flex-1"
>
{ALL_CATEGORIES.map((c) => (
<option key={c} value={c}>
{CATEGORY_COLORS[c].label}
</option>
))}
</select>
</div>
<textarea
value={newContent}
onChange={(e) => setNewContent(e.target.value)}
placeholder="Memory content..."
className="w-full bg-[#1a2540] border border-[rgba(117,170,252,0.2)] rounded px-2 py-1.5 text-[10px] font-mono text-white resize-y min-h-[40px] placeholder:text-[#556677]"
rows={2}
/>
<input
type="text"
value={newSource}
onChange={(e) => setNewSource(e.target.value)}
placeholder="Source (optional)"
className="w-full bg-[#1a2540] border border-[rgba(117,170,252,0.2)] rounded px-2 py-1 text-[10px] font-mono text-white placeholder:text-[#556677]"
/>
<div className="flex gap-1.5">
<button
type="button"
onClick={handleAddMemory}
disabled={!newContent.trim()}
className="text-[10px] font-mono text-emerald-400 hover:text-emerald-300 border border-emerald-800 rounded px-2 py-0.5 disabled:opacity-40 disabled:cursor-not-allowed"
>
Save
</button>
<button
type="button"
onClick={() => { setAddingMemory(false); setNewContent(""); setNewSource(""); }}
className="text-[10px] font-mono text-[#7788aa] hover:text-white border border-[#2a3a5a] rounded px-2 py-0.5"
>
Cancel
</button>
</div>
</div>
)}
</div>
)}
</div>
{/* DAG */}
<div className="px-4 py-3 flex-1">
<span className="text-[10px] font-mono text-[#9bc3ff] uppercase tracking-wide block mb-2">
Steps ({totalSteps})
</span>
<DirectiveDAG
steps={directive.steps}
onComplete={onCompleteStep}
onFail={onFailStep}
onSkip={onSkipStep}
/>
</div>
{/* Log Stream */}
{taskMap.size > 0 && (
<div className="px-4 py-3 border-t border-[rgba(117,170,252,0.1)]">
<DirectiveLogStream
entries={entries}
taskMap={taskMap}
connected={connected}
visibleTaskIds={visibleTaskIds}
searchQuery={searchQuery}
isCollapsed={isLogCollapsed}
onToggleCollapse={() => setIsLogCollapsed((prev) => !prev)}
onSetVisibleTaskIds={setVisibleTaskIds}
onSetSearchQuery={setSearchQuery}
onClear={clearEntries}
/>
</div>
)}
</div>
);
}
|