Repository navigation
Expand file tree
/
Copy pathImportConversationDialog.tsx
More file actions
1024 lines (984 loc) · 38 KB
/
Copy pathImportConversationDialog.tsx
File metadata and controls
1024 lines (984 loc) · 38 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
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { useAtomValue } from "@effect/atom-react";
import {
scopedProjectKey,
scopeProjectRef,
scopeThreadRef,
} from "@t3tools/client-runtime/environment";
import type { EnvironmentProject } from "@t3tools/client-runtime/state/models";
import { isScratchProject } from "@t3tools/client-runtime/state/projects";
import {
isAtomCommandInterrupted,
squashAtomCommandFailure,
} from "@t3tools/client-runtime/state/runtime";
import {
ProviderDriverKind,
type AgentSessionSummary,
type ConductorAgent,
type ConductorWorkspaceSummary,
type EnvironmentId,
type ProjectId,
type ScopedProjectRef,
type ThreadId,
} from "@t3tools/contracts";
import { useNavigate } from "@tanstack/react-router";
import * as Option from "effect/Option";
import { AsyncResult, Atom } from "effect/reactivity";
import {
ArrowDownIcon,
ArrowRightIcon,
ArrowUpIcon,
DownloadIcon,
FolderGit2Icon,
LayersIcon,
ListFilterIcon,
} from "lucide-react";
import { useEffect, useRef, useState, type ElementType } from "react";
import { formatRelativeTimeLabel } from "~/timestampFormat";
import { appAtomRegistry } from "~/rpc/atomRegistry";
import {
agentSessionImport,
agentSessionList,
conductorWorkspaceImport,
conductorWorkspaceList,
} from "~/state/agentSessions";
import { useScratchProject } from "~/hooks/useScratchProject";
import { useProjects, useServerConfigs, waitForThreadShell } from "~/state/entities";
import { formatEnvironmentQueryError } from "~/state/query";
import { useAtomCommand } from "~/state/use-atom-command";
import { ProviderInstanceIcon } from "./chat/ProviderInstanceIcon";
import { ConductorIcon } from "./Icons";
import {
Dialog,
DialogDescription,
DialogHeader,
DialogPanel,
DialogPopup,
DialogTitle,
} from "./ui/dialog";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "./ui/table";
import { Input } from "./ui/input";
import { Spinner } from "./ui/spinner";
import {
PullRequestFilterRadioSubmenu,
type PullRequestFilterOption,
} from "./pullRequest/PullRequestListFilters";
import { Button } from "./ui/button";
import { Checkbox } from "./ui/checkbox";
import { Label } from "./ui/label";
import { Menu, MenuPopup, MenuTrigger } from "./ui/menu";
import { toastManager } from "./ui/toast";
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
import { Truncatable } from "./ui/truncatable";
/** `null` while closed; open, it holds the project the dialog starts on, if any. */
const importConversationAtom = Atom.make<{ readonly projectRef: ScopedProjectRef | null } | null>(
null,
).pipe(Atom.keepAlive, Atom.withLabel("import-conversation:open"));
/**
* Opens the picker of Claude Code and Codex conversations and Conductor workspaces,
* starting on `projectRef` when given. The dialog can switch to any other project.
*/
export function openImportConversationDialog(projectRef: ScopedProjectRef | null = null): void {
appAtomRegistry.set(importConversationAtom, { projectRef });
}
function closeImportConversationDialog() {
appAtomRegistry.set(importConversationAtom, null);
}
export function ImportConversationDialogHost() {
const open = useAtomValue(importConversationAtom);
if (open === null) return null;
return <ImportConversationDialog initialProjectRef={open.projectRef} />;
}
/** Projects whose server can list importable conversations, most recently updated first. */
function useImportableProjects() {
const projects = useProjects();
const serverConfigs = useServerConfigs();
const { scratchWorkspaceRootFor } = useScratchProject();
return projects
.filter(
(project) =>
serverConfigs.get(project.environmentId)?.environment.capabilities.agentSessionPicker ===
true && !isScratchProject(project, scratchWorkspaceRootFor(project.environmentId)),
)
.toSorted((left, right) => right.updatedAt.localeCompare(left.updatedAt));
}
function ImportConversationDialog({
initialProjectRef,
}: {
initialProjectRef: ScopedProjectRef | null;
}) {
const projects = useImportableProjects();
const [chosenKey, setChosenKey] = useState(
initialProjectRef === null ? ALL_PROJECTS : scopedProjectKey(initialProjectRef),
);
const chosen = projects.filter(
(candidate) =>
chosenKey === ALL_PROJECTS ||
scopedProjectKey(scopeProjectRef(candidate.environmentId, candidate.id)) === chosenKey,
);
// A project that went away falls back to all of them.
const selectedKey = chosen.length === 0 ? ALL_PROJECTS : chosenKey;
const selected = chosen.length === 0 ? projects : chosen;
const projectFilter: ImportFilter<string> = {
value: selectedKey,
onChange: setChosenKey,
options: [
{ value: ALL_PROJECTS, label: "All projects", Icon: LayersIcon },
...projects.map((candidate) => ({
value: scopedProjectKey(scopeProjectRef(candidate.environmentId, candidate.id)),
label: candidate.title,
Icon: FolderGit2Icon,
project: candidate,
})),
],
};
return (
<Dialog
open
onOpenChange={(open) => {
if (!open) closeImportConversationDialog();
}}
>
<DialogPopup className="sm:max-w-3xl">
<DialogHeader>
<DialogTitle>Import conversation</DialogTitle>
<DialogDescription>
Claude Code and Codex conversations from the last 30 days, and Conductor workspaces, for
your projects. An imported conversation continues the same session. Import all Conductor
can include archived workspaces, which become settled threads.
</DialogDescription>
</DialogHeader>
<DialogPanel>
{projects.length === 0 ? (
<div className="flex h-40 items-center justify-center px-6 text-center text-muted-foreground text-sm">
Add a project to import conversations into it.
</div>
) : (
<ImportConversationList
key={selectedKey}
projects={selected}
showProject={selectedKey === ALL_PROJECTS && projects.length > 1}
projectFilter={projectFilter}
/>
)}
</DialogPanel>
</DialogPopup>
</Dialog>
);
}
const PROVIDER_LABEL: Record<AgentSessionSummary["provider"], string> = {
pi: "Pi",
claudeAgent: "Claude Code",
codex: "Codex",
};
const SOURCES = ["all", "claudeAgent", "codex", "conductor"] as const;
type ImportSource = (typeof SOURCES)[number];
const SOURCE_LABEL: Record<ImportSource, string> = {
all: "All",
claudeAgent: "Claude Code",
codex: "Codex",
conductor: "Conductor",
};
type ImportRow =
| {
kind: "session";
project: EnvironmentProject;
session: AgentSessionSummary;
updatedAt: string;
}
| {
kind: "workspace";
project: EnvironmentProject;
workspace: ConductorWorkspaceSummary;
updatedAt: string;
};
const ALL_PROJECTS = "all-projects";
/**
* Both listings for each project, keyed by the projects' JSON so the dialog subscribes to one
* atom however many projects it shows.
*/
const importListingsAtom = Atom.family((targetsJson: string) =>
Atom.make((get) =>
(
JSON.parse(targetsJson) as ReadonlyArray<{
readonly environmentId: EnvironmentId;
readonly projectId: ProjectId;
}>
).map(({ environmentId, projectId }) => ({
sessions: get(agentSessionList({ environmentId, input: { projectId } })),
conductor: get(conductorWorkspaceList({ environmentId, input: { projectId } })),
})),
),
);
/** Whether a row's conversation ID, titles, prompt, folder, or branch match the search text. */
function matchesSearch(row: ImportRow, needle: string): boolean {
if (needle === "") return true;
const fields =
row.kind === "session"
? [row.session.providerSessionId, row.session.title, row.session.preview]
: [
row.workspace.title,
row.workspace.name,
row.workspace.branch ?? "",
...row.workspace.tabs.map((tab) => tab.title),
];
return fields.some((field) => field.toLowerCase().includes(needle));
}
/** The project and source rows of the Filters menu. */
interface ImportFilter<Value extends string> {
readonly value: Value;
readonly options: ReadonlyArray<PullRequestFilterOption<Value>>;
readonly onChange: (value: Value) => void;
}
const DEFAULT_SOURCE: ImportSource = "conductor";
function countPhrase(count: number, singular: string): string {
return `${count} ${count === 1 ? singular : `${singular}s`}`;
}
/** What the import-all run should tell the user, or nothing when it was stopped before any work. */
function conductorImportToast(input: {
readonly stopped: boolean;
readonly importedThreads: number;
readonly refreshedThreads: number;
readonly settledThreads: number;
readonly failed: number;
readonly failureDetail: string | null;
}): {
readonly type: "success" | "warning" | "error";
readonly title: string;
readonly description: string | undefined;
} | null {
if (
input.stopped &&
input.importedThreads === 0 &&
input.refreshedThreads === 0 &&
input.failed === 0
) {
return null;
}
const refreshed =
input.refreshedThreads > 0
? `Updated ${countPhrase(input.refreshedThreads, "earlier imported thread")}.`
: null;
const settled =
input.settledThreads > 0
? `${countPhrase(input.settledThreads, "archived thread")} ${input.settledThreads === 1 ? "is" : "are"} settled.`
: null;
const failed =
input.failed > 0 ? `${countPhrase(input.failed, "workspace")} could not be imported.` : null;
const description = [
refreshed,
settled,
failed,
input.importedThreads === 0 ? input.failureDetail : null,
]
.filter((part) => part !== null && part !== "")
.join(" ");
if (!input.stopped && input.failed === 0 && input.importedThreads === 0) {
return {
type: "success",
title:
input.refreshedThreads > 0
? `Updated ${countPhrase(input.refreshedThreads, "imported thread")}`
: "Conductor threads are already imported",
description: undefined,
};
}
return {
type:
input.failed > 0 && input.importedThreads === 0
? "error"
: input.failed > 0 || input.stopped
? "warning"
: "success",
title: input.stopped
? `Stopped after importing ${countPhrase(input.importedThreads, "thread")}`
: input.failed > 0 && input.importedThreads === 0
? "Could not import Conductor threads"
: `Imported ${countPhrase(input.importedThreads, "thread")}`,
description: description === "" ? undefined : description,
};
}
function providerIcon(driver: string, label: string) {
return function ProviderIcon({ className }: { className?: string }) {
return (
<ProviderInstanceIcon
driverKind={ProviderDriverKind.make(driver)}
displayName={label}
showBadge={false}
iconClassName={className ?? "size-3.5"}
/>
);
};
}
const SOURCE_ICON: Record<ImportSource, ElementType<{ className?: string }>> = {
all: LayersIcon,
claudeAgent: providerIcon("claudeAgent", "Claude Code"),
codex: providerIcon("codex", "Codex"),
conductor: ConductorIcon,
};
const CONDUCTOR_AGENT: Record<ConductorAgent, { driver: ProviderDriverKind; label: string }> = {
claude: { driver: ProviderDriverKind.make("claudeAgent"), label: "Claude Code" },
codex: { driver: ProviderDriverKind.make("codex"), label: "Codex" },
cursor: { driver: ProviderDriverKind.make("cursor"), label: "Cursor" },
};
/** "9 tabs", listing each tab's agent, title, and prompt count on hover. */
function TabCount({ workspace }: { workspace: ConductorWorkspaceSummary }) {
const tabs = workspace.tabs.length;
return (
<Tooltip>
<TooltipTrigger render={<span className="underline decoration-dotted underline-offset-2" />}>
{tabs} {tabs === 1 ? "tab" : "tabs"}
</TooltipTrigger>
<TooltipPopup align="start">
<ul className="flex max-w-80 flex-col gap-1">
{workspace.tabs.map((tab) => (
<li key={tab.sessionId} className="flex min-w-0 items-center gap-2">
<ProviderInstanceIcon
driverKind={CONDUCTOR_AGENT[tab.agent].driver}
displayName={CONDUCTOR_AGENT[tab.agent].label}
showBadge={false}
iconClassName="size-3"
/>
<span className="min-w-0 flex-1 truncate">{tab.title}</span>
<span className="shrink-0 text-muted-foreground">
{tab.messageCount} {tab.messageCount === 1 ? "prompt" : "prompts"}
</span>
</li>
))}
</ul>
</TooltipPopup>
</Tooltip>
);
}
const sessionKey = (session: AgentSessionSummary) =>
`${session.providerInstanceId}:${session.providerSessionId}`;
const rowKey = (row: ImportRow) =>
row.kind === "session" ? sessionKey(row.session) : `conductor:${row.workspace.workspaceId}`;
const rowTitle = (row: ImportRow) =>
row.kind === "session" ? row.session.title : row.workspace.title;
const rowSource = (row: ImportRow) =>
row.kind === "session" ? PROVIDER_LABEL[row.session.provider] : "Conductor";
type SortKey = "name" | "workspace" | "project" | "source" | "updated";
/** A Conductor workspace's folder; conversations have none and sort last. */
const rowWorkspace = (row: ImportRow) => (row.kind === "workspace" ? row.workspace.name : null);
interface Sort {
readonly key: SortKey;
readonly descending: boolean;
}
const DEFAULT_SORT: Sort = { key: "updated", descending: true };
function ariaSort(sort: Sort, column: SortKey) {
if (sort.key !== column) return "none" as const;
return sort.descending ? ("descending" as const) : ("ascending" as const);
}
function compareRows(left: ImportRow, right: ImportRow, key: SortKey): number {
switch (key) {
case "name":
return rowTitle(left).localeCompare(rowTitle(right));
case "workspace": {
const leftName = rowWorkspace(left);
const rightName = rowWorkspace(right);
if (leftName === null || rightName === null)
return Number(leftName === null) - Number(rightName === null);
return leftName.localeCompare(rightName);
}
case "project":
return left.project.title.localeCompare(right.project.title);
case "source":
return rowSource(left).localeCompare(rowSource(right));
case "updated":
return left.updatedAt.localeCompare(right.updatedAt);
}
}
/** A column header that sorts by its column; pressing the sorted one flips the direction. */
function SortHeader({
label,
column,
sort,
onSort,
alignEnd = false,
}: {
label: string;
column: SortKey;
/** Right-aligned columns lead with the arrow so the label lines up with the values. */
alignEnd?: boolean;
sort: Sort;
onSort: (sort: Sort) => void;
}) {
const active = sort.key === column;
const Arrow = sort.descending ? ArrowDownIcon : ArrowUpIcon;
return (
<button
type="button"
className="inline-flex items-center gap-1 text-muted-foreground hover:text-foreground"
onClick={() =>
onSort(
active
? { key: column, descending: !sort.descending }
: { key: column, descending: column === "updated" },
)
}
>
{alignEnd && active ? <Arrow aria-hidden className="size-3" /> : null}
{label}
{!alignEnd && active ? <Arrow aria-hidden className="size-3" /> : null}
</button>
);
}
function ImportConversationList({
projects,
showProject,
projectFilter,
}: {
projects: ReadonlyArray<EnvironmentProject>;
showProject: boolean;
projectFilter: ImportFilter<string>;
}) {
const navigate = useNavigate();
const listings = useAtomValue(
importListingsAtom(
JSON.stringify(
projects.map((project) => ({
environmentId: project.environmentId,
projectId: project.id,
})),
),
),
);
const importSession = useAtomCommand(agentSessionImport, { reportFailure: false });
const importWorkspace = useAtomCommand(conductorWorkspaceImport, { reportFailure: false });
const [pendingKey, setPendingKey] = useState<string | null>(null);
const [includeArchived, setIncludeArchived] = useState(false);
const [importProgress, setImportProgress] = useState<{
completed: number;
total: number;
} | null>(null);
const importRun = useRef<{ cancelled: boolean } | null>(null);
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- cancels the pending import when the dialog unmounts
useEffect(
() => () => {
if (importRun.current) importRun.current.cancelled = true;
},
[],
);
const [chosenSource, setSource] = useState<ImportSource | null>(null);
const [query, setQuery] = useState("");
const needle = query.trim().toLowerCase();
const [sort, setSort] = useState<Sort>(DEFAULT_SORT);
const openThread = (project: EnvironmentProject, threadId: ThreadId) => {
closeImportConversationDialog();
void navigate({
to: "/$environmentId/$threadId",
params: { environmentId: project.environmentId, threadId },
});
};
const choose = async (project: EnvironmentProject, session: AgentSessionSummary) => {
if (pendingKey !== null) return;
if (session.threadId !== null) {
openThread(project, session.threadId);
return;
}
const { environmentId } = project;
setPendingKey(sessionKey(session));
const result = await importSession({
environmentId,
input: {
projectId: project.id,
expectedWorkspaceRoot: project.workspaceRoot,
session: {
providerInstanceId: session.providerInstanceId,
providerSessionId: session.providerSessionId,
},
},
});
if (result._tag === "Success") {
const threadId = result.value.threadIds?.[0];
appAtomRegistry.refresh(
agentSessionList({ environmentId, input: { projectId: project.id } }),
);
if (threadId) {
// The route treats a thread the client has not heard of yet as missing.
await waitForThreadShell(scopeThreadRef(environmentId, threadId)).catch(() => null);
setPendingKey(null);
openThread(project, threadId);
return;
}
setPendingKey(null);
toastManager.add({
type: "error",
title: "Could not import conversation",
description: "It may have changed on disk, or an earlier import of it was deleted.",
});
return;
}
setPendingKey(null);
if (isAtomCommandInterrupted(result)) return;
const error = squashAtomCommandFailure(result);
toastManager.add({
type: "error",
title: "Could not import conversation",
description: error instanceof Error ? error.message : "An error occurred.",
});
};
const chooseWorkspace = async (
project: EnvironmentProject,
workspace: ConductorWorkspaceSummary,
) => {
if (pendingKey !== null) return;
if (workspace.threadId !== null) {
openThread(project, workspace.threadId);
return;
}
const { environmentId } = project;
setPendingKey(`conductor:${workspace.workspaceId}`);
const result = await importWorkspace({
environmentId,
input: { projectId: project.id, workspaceId: workspace.workspaceId },
});
if (result._tag === "Success" && result.value.threadIds[0] !== undefined) {
const threadId = result.value.threadIds[0];
appAtomRegistry.refresh(
conductorWorkspaceList({ environmentId, input: { projectId: project.id } }),
);
await waitForThreadShell(scopeThreadRef(environmentId, threadId)).catch(() => null);
setPendingKey(null);
openThread(project, threadId);
return;
}
setPendingKey(null);
if (result._tag !== "Success" && isAtomCommandInterrupted(result)) return;
const error = result._tag === "Success" ? null : squashAtomCommandFailure(result);
toastManager.add({
type: "error",
title: "Could not import Conductor workspace",
description: error instanceof Error ? error.message : "An error occurred.",
});
};
const loaded = listings.map((listing) => ({
sessions: Option.getOrNull(AsyncResult.value(listing.sessions)),
conductor: Option.getOrNull(AsyncResult.value(listing.conductor)),
}));
// Conductor leads while its listing loads; without Conductor on this machine, show everything.
const conductorMissing =
loaded.length > 0 && loaded.every((listing) => listing.conductor?.available === false);
const source: ImportSource =
chosenSource === null || (chosenSource === "conductor" && conductorMissing)
? conductorMissing
? "all"
: DEFAULT_SOURCE
: chosenSource;
const showSessions = source !== "conductor";
const showWorkspaces = source === "all" || source === "conductor";
// Two projects of one repository list the same workspaces; the first one keeps them.
const seen = new Set<string>();
const rows: Array<ImportRow> = [];
projects.forEach((project, index) => {
const listing = loaded[index];
if (showSessions) {
for (const session of listing?.sessions?.sessions ?? []) {
if (source !== "all" && session.provider !== source) continue;
if (seen.has(sessionKey(session))) continue;
seen.add(sessionKey(session));
rows.push({ kind: "session", project, session, updatedAt: session.updatedAt });
}
}
if (showWorkspaces) {
for (const workspace of listing?.conductor?.workspaces ?? []) {
const key = `conductor:${workspace.workspaceId}`;
if (seen.has(key)) continue;
seen.add(key);
rows.push({ kind: "workspace", project, workspace, updatedAt: workspace.updatedAt });
}
}
});
const visibleRows = rows
.filter((row) => matchesSearch(row, needle))
.toSorted((left, right) => {
const order = compareRows(left, right, sort.key) * (sort.descending ? -1 : 1);
return order !== 0 ? order : right.updatedAt.localeCompare(left.updatedAt);
});
const relevant = listings.flatMap((listing) => [
...(showSessions ? [listing.sessions] : []),
...(showWorkspaces ? [listing.conductor] : []),
]);
const pending = relevant.some(
(result) => result._tag === "Initial" || (result._tag !== "Success" && result.waiting),
);
const failure = relevant.find((result) => result._tag === "Failure");
const truncated = showSessions && loaded.some((listing) => listing.sessions?.truncated === true);
const sourceOptions = SOURCES.filter((option) => option !== "conductor" || !conductorMissing).map(
(option) => ({ value: option, label: SOURCE_LABEL[option], Icon: SOURCE_ICON[option] }),
);
const activeFilters =
Number(projectFilter.value !== ALL_PROJECTS) + Number(source !== DEFAULT_SOURCE);
const conductorKnown = loaded.length > 0 && loaded.every((listing) => listing.conductor !== null);
const showConductorImport = conductorKnown && !conductorMissing && showWorkspaces;
const archivedIds = new Set<string>();
for (const listing of loaded) {
for (const workspaceId of listing.conductor?.archivedWorkspaceIds ?? []) {
archivedIds.add(workspaceId);
}
}
const importTargets: Array<{
project: EnvironmentProject;
workspaceId: string;
}> = [];
if (showWorkspaces) {
const seenWorkspaceIds = new Set<string>();
projects.forEach((project, index) => {
const conductor = loaded[index]?.conductor;
if (conductor === null || conductor === undefined) return;
for (const workspace of conductor.workspaces) {
if (seenWorkspaceIds.has(workspace.workspaceId)) continue;
seenWorkspaceIds.add(workspace.workspaceId);
importTargets.push({ project, workspaceId: workspace.workspaceId });
}
if (!includeArchived) return;
for (const workspaceId of conductor.archivedWorkspaceIds) {
if (seenWorkspaceIds.has(workspaceId)) continue;
seenWorkspaceIds.add(workspaceId);
importTargets.push({ project, workspaceId });
}
});
}
const importingAll = importProgress !== null;
const importAll = async () => {
if (importRun.current !== null || pendingKey !== null || importTargets.length === 0) return;
const run = { cancelled: false };
importRun.current = run;
const targets = importTargets;
setPendingKey("import-all");
setImportProgress({ completed: 0, total: targets.length });
let importedThreads = 0;
let refreshedThreads = 0;
let settledThreads = 0;
let failed = 0;
let failureDetail: string | null = null;
const refreshProjects = new Map<string, EnvironmentProject>();
for (const [index, target] of targets.entries()) {
if (run.cancelled) break;
const result = await importWorkspace({
environmentId: target.project.environmentId,
input: { projectId: target.project.id, workspaceId: target.workspaceId },
});
refreshProjects.set(
scopedProjectKey(scopeProjectRef(target.project.environmentId, target.project.id)),
target.project,
);
if (result._tag === "Success") {
importedThreads += result.value.importedThreadCount;
refreshedThreads += result.value.refreshedThreadCount;
if (result.value.settled) settledThreads += result.value.importedThreadCount;
} else if (isAtomCommandInterrupted(result)) {
run.cancelled = true;
} else {
failed += 1;
const error = squashAtomCommandFailure(result);
failureDetail = error instanceof Error ? error.message : "An error occurred.";
}
setImportProgress({ completed: index + 1, total: targets.length });
if (run.cancelled) break;
}
if (importRun.current === run) importRun.current = null;
setPendingKey(null);
setImportProgress(null);
for (const project of refreshProjects.values()) {
appAtomRegistry.refresh(
conductorWorkspaceList({
environmentId: project.environmentId,
input: { projectId: project.id },
}),
);
}
const toast = conductorImportToast({
stopped: run.cancelled,
importedThreads,
refreshedThreads,
settledThreads,
failed,
failureDetail,
});
if (toast === null) return;
toastManager.add({ type: toast.type, title: toast.title, description: toast.description });
};
return (
<>
<div className="mb-2 flex items-center gap-2">
<Menu>
<MenuTrigger render={<Button type="button" variant="outline" size="sm" />}>
<ListFilterIcon />
<span>Filters</span>
{activeFilters > 0 ? (
<span className="rounded-full bg-muted px-1.5 text-xs text-muted-foreground tabular-nums">
{activeFilters}
</span>
) : null}
</MenuTrigger>
<MenuPopup align="start" side="bottom">
<PullRequestFilterRadioSubmenu
label="Project"
value={projectFilter.value}
options={projectFilter.options}
onChange={projectFilter.onChange}
/>
<PullRequestFilterRadioSubmenu
label="Source"
value={source}
options={sourceOptions}
optionsPending={listings.some(
(listing) => listing.conductor._tag === "Initial" || listing.conductor.waiting,
)}
onChange={setSource}
/>
</MenuPopup>
</Menu>
<div className="min-w-0 flex-1">
<Input
size="sm"
type="search"
value={query}
placeholder="Search by title, prompt, or conversation ID"
aria-label="Search conversations and workspaces"
onChange={(event) => setQuery(event.target.value)}
/>
</div>
</div>
{showConductorImport ? (
<div className="mb-2 flex items-center gap-2">
<Tooltip>
<TooltipTrigger render={<span className="inline-flex min-w-0" />}>
<Label>
<Checkbox
checked={includeArchived}
disabled={importingAll || archivedIds.size === 0}
onCheckedChange={(checked) => setIncludeArchived(checked === true)}
/>
Include archived
{archivedIds.size > 0 ? (
<span className="text-muted-foreground tabular-nums">{archivedIds.size}</span>
) : null}
</Label>
</TooltipTrigger>
<TooltipPopup>
Archived Conductor workspaces become settled threads. Their folders are usually
already gone, so the conversation comes across without a working copy.
</TooltipPopup>
</Tooltip>
<div className="ml-auto flex items-center gap-2">
{importingAll && importProgress !== null ? (
<span className="text-muted-foreground text-xs tabular-nums">
{importProgress.completed} of {importProgress.total}
</span>
) : null}
<Button
type="button"
variant="outline"
size="sm"
disabled={!importingAll && (importTargets.length === 0 || pending)}
onClick={() => {
if (importingAll) {
if (importRun.current) importRun.current.cancelled = true;
return;
}
void importAll();
}}
>
{importingAll ? "Stop" : "Import all Conductor"}
</Button>
</div>
</div>
) : null}
{rows.length === 0 && (pending || failure !== undefined) ? (
<div className="flex h-40 items-center justify-center px-6 text-center text-muted-foreground text-sm">
{pending ? (
<Spinner size="md" tone="muted" />
) : failure?._tag === "Failure" ? (
formatEnvironmentQueryError(failure.cause)
) : null}
</div>
) : visibleRows.length === 0 ? (
<div className="flex h-40 items-center justify-center px-6 text-center text-muted-foreground text-sm">
{needle !== ""
? `Nothing matches “${query.trim()}”.`
: source === "conductor"
? "No active Conductor workspaces found."
: source === "all"
? "Nothing to import."
: `No ${SOURCE_LABEL[source]} conversations found.`}
</div>
) : (
<div>
<div className="max-h-[28rem] overflow-y-auto rounded-xl border border-border/70">
<Table className="table-fixed" aria-label="Conversations and workspaces to import">
<colgroup>
<col />
<col className="w-28" />
{showProject ? <col className="w-28" /> : null}
<col className="w-36" />
<col className="w-24" />
<col className="w-12" />
</colgroup>
<TableHeader>
<TableRow>
<TableHead aria-sort={ariaSort(sort, "name")}>
<SortHeader label="Name" column="name" sort={sort} onSort={setSort} />
</TableHead>
<TableHead aria-sort={ariaSort(sort, "workspace")}>
<SortHeader label="Workspace" column="workspace" sort={sort} onSort={setSort} />
</TableHead>
{showProject ? (
<TableHead aria-sort={ariaSort(sort, "project")}>
<SortHeader label="Project" column="project" sort={sort} onSort={setSort} />
</TableHead>
) : null}
<TableHead aria-sort={ariaSort(sort, "source")}>
<SortHeader label="Source" column="source" sort={sort} onSort={setSort} />
</TableHead>
<TableHead aria-sort={ariaSort(sort, "updated")} className="text-right">
<SortHeader
label="Updated"
column="updated"
sort={sort}
onSort={setSort}
alignEnd
/>
</TableHead>
<TableHead>
<span className="sr-only">Action</span>
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{visibleRows.map((row) => {
const key = rowKey(row);
const imported =
(row.kind === "session" ? row.session.threadId : row.workspace.threadId) !==
null;
const run = () =>
void (row.kind === "session"
? choose(row.project, row.session)
: chooseWorkspace(row.project, row.workspace));
const agent =
row.kind === "session"
? {
driver: ProviderDriverKind.make(row.session.provider),
label: PROVIDER_LABEL[row.session.provider],
}
: CONDUCTOR_AGENT[row.workspace.tabs[0]!.agent];
const prompt =
row.kind === "session" && row.session.preview !== row.session.title
? row.session.preview
: null;
return (
<TableRow key={key} className="cursor-pointer" onClick={run}>
<TableCell>
<div className="flex min-w-0 items-center gap-2.5">
<ProviderInstanceIcon
driverKind={agent.driver}
displayName={agent.label}
showBadge={false}
iconClassName="size-4"
/>
{prompt === null ? (
<span className="min-w-0 font-medium text-foreground text-sm">
<Truncatable>{rowTitle(row)}</Truncatable>
</span>
) : (
<Tooltip>
<TooltipTrigger
render={
<span className="min-w-0 truncate font-medium text-foreground text-sm" />
}
>
{rowTitle(row)}
</TooltipTrigger>
<TooltipPopup align="start" className="max-w-96 whitespace-normal">
<div className="font-medium">{rowTitle(row)}</div>
<div className="text-muted-foreground">{prompt}</div>
</TooltipPopup>
</Tooltip>
)}
</div>
</TableCell>
<TableCell>
{row.kind === "workspace" ? (
<Tooltip>
<TooltipTrigger
render={<div className="truncate text-muted-foreground" />}
>
{row.workspace.name}
</TooltipTrigger>
<TooltipPopup>{row.workspace.branch ?? "No branch"}</TooltipPopup>
</Tooltip>
) : (
<div className="text-muted-foreground/50">None</div>
)}
</TableCell>
{showProject ? (
<TableCell>
<div className="text-muted-foreground">
<Truncatable>{row.project.title}</Truncatable>
</div>
</TableCell>
) : null}
<TableCell>
<div className="truncate text-muted-foreground">
{rowSource(row)} ·{" "}
{row.kind === "session" ? (
`${row.session.messageCount} msgs`
) : (
<TabCount workspace={row.workspace} />
)}
</div>
</TableCell>
<TableCell className="text-right">
<div className="truncate text-muted-foreground tabular-nums">
{formatRelativeTimeLabel(row.updatedAt)}
</div>
</TableCell>
<TableCell className="text-right">
<Tooltip>
<TooltipTrigger
render={
<Button
type="button"
size="icon-xs"
variant="ghost"
disabled={pendingKey !== null}
aria-label={`${imported ? "Open" : "Import"} ${rowTitle(row)}`}
onClick={(event) => {
event.stopPropagation();
run();
}}
/>
}
>
{pendingKey === key ? (
<Spinner size="xs" />
) : imported ? (
<ArrowRightIcon aria-hidden />
) : (
<DownloadIcon aria-hidden />
)}