+36
![github-actions[bot]](/assets/img/avatar_default.png)








Julius Marminge
maria-rcks
Bilal Bakr
shivam
Vitalii Yehorov
Jake Leventhal
Claude Opus 5
Alex Southwell
github-actions[bot]
Nicholas Wasmiller
PB
Exotic
Yash Singh
Devin
Theo Browne
Gabriel De Andrade
Dara Adedeji
scratchyone
Dominic Roy
chukfinley
Primož Ajdišek
benthecarman
NaveDanan
aaditagrawal
Aditya Garud
Nick Anisimov
MacKinley Smith
Yordis Prieto
t3-code[bot]
AKolenda
Guillermo Casanova
Otavio Salvador
Shirish Pothi
Ishaan Kothari
Bob Fowler
Anton Bezdenezhnykh
ValeraZSD
Ephraim
Ryan Ilano
Alex
maco
Cursor
Tristan Knight
PR Batch Tester
oliver
kamkm
de34391427
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Bilal Bakr <[email protected]> Co-authored-by: shivam <[email protected]> Co-authored-by: Vitalii Yehorov <[email protected]> Co-authored-by: Jake Leventhal <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Alex Southwell <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Nicholas Wasmiller <[email protected]> Co-authored-by: PB <[email protected]> Co-authored-by: Exotic <[email protected]> Co-authored-by: Yash Singh <[email protected]> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Theo Browne <[email protected]> Co-authored-by: Gabriel De Andrade <[email protected]> Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: scratchyone <[email protected]> Co-authored-by: Dominic Roy <[email protected]> Co-authored-by: chukfinley <[email protected]> Co-authored-by: Primož Ajdišek <[email protected]> Co-authored-by: benthecarman <[email protected]> Co-authored-by: NaveDanan <[email protected]> Co-authored-by: aaditagrawal <[email protected]> Co-authored-by: Aditya Garud <[email protected]> Co-authored-by: Nick Anisimov <[email protected]> Co-authored-by: MacKinley Smith <[email protected]> Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: AKolenda <[email protected]> Co-authored-by: Guillermo Casanova <[email protected]> Co-authored-by: Otavio Salvador <[email protected]> Co-authored-by: Shirish Pothi <[email protected]> Co-authored-by: Ishaan Kothari <[email protected]> Co-authored-by: Bob Fowler <[email protected]> Co-authored-by: Anton Bezdenezhnykh <[email protected]> Co-authored-by: ValeraZSD <[email protected]> Co-authored-by: Ephraim <[email protected]> Co-authored-by: Ryan Ilano <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: maco <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: Tristan Knight <[email protected]> Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: oliver <[email protected]> Co-authored-by: kamkm <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
241 lines
8.8 KiB
TypeScript
241 lines
8.8 KiB
TypeScript
import { useAtomValue } from "@effect/atom-react";
|
|
import { useRoute, type RouteProp } from "@react-navigation/native";
|
|
import { useMemo, useRef, useState } from "react";
|
|
import {
|
|
EnvironmentId,
|
|
ThreadId,
|
|
type OrchestrationV2ThreadProjection,
|
|
type OrchestrationV2ThreadShell,
|
|
type ScopedProjectRef,
|
|
type ScopedThreadRef,
|
|
} from "@t3tools/contracts";
|
|
import {
|
|
presentThreadShell,
|
|
type EnvironmentThreadShell,
|
|
} from "@t3tools/client-runtime/state/shell";
|
|
import {
|
|
deriveLatestThreadRun,
|
|
deriveThreadRuntime,
|
|
} from "@t3tools/client-runtime/state/thread-execution";
|
|
import * as Option from "effect/Option";
|
|
|
|
import { scopedThreadKey } from "../lib/scopedEntities";
|
|
import { useProject, useThreadShell } from "../state/entities";
|
|
import { useEnvironmentThread } from "../state/threads";
|
|
import {
|
|
resolvePendingThreadCreation,
|
|
pendingThreadCreationOutcomesAtom,
|
|
pendingThreadCreationShell,
|
|
type PendingThreadCreation,
|
|
} from "./pending-thread-creation";
|
|
import {
|
|
useRemoteEnvironmentRuntime,
|
|
useSavedRemoteConnection,
|
|
} from "./use-remote-environment-registry";
|
|
import { useThreadOutboxMessages } from "./use-thread-outbox";
|
|
type ThreadSelectionRouteParams = {
|
|
readonly environmentId?: string | string[];
|
|
readonly threadId?: string | string[];
|
|
};
|
|
|
|
function firstRouteParam(value: string | string[] | undefined): string | null {
|
|
if (Array.isArray(value)) {
|
|
return value[0] ?? null;
|
|
}
|
|
|
|
return value ?? null;
|
|
}
|
|
|
|
function latestUserMessageAt(
|
|
projection: OrchestrationV2ThreadProjection,
|
|
): OrchestrationV2ThreadShell["latestUserMessageAt"] {
|
|
for (let index = projection.messages.length - 1; index >= 0; index -= 1) {
|
|
const message = projection.messages[index];
|
|
if (message?.role === "user") {
|
|
return message.createdAt;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Builds an optimistic thread shell from the detail projection for the window
|
|
* where the shell list has not materialized the thread yet (e.g. a thread that
|
|
* was just created from this device).
|
|
*/
|
|
function threadDetailToShell(
|
|
environmentId: EnvironmentId,
|
|
projection: OrchestrationV2ThreadProjection,
|
|
): EnvironmentThreadShell {
|
|
const thread = projection.thread;
|
|
const latestRun = deriveLatestThreadRun(projection);
|
|
const runtime = deriveThreadRuntime(projection);
|
|
const pendingRequest =
|
|
projection.runtimeRequests.find((request) => request.status === "pending") ?? null;
|
|
return presentThreadShell(environmentId, {
|
|
id: thread.id,
|
|
projectId: thread.projectId,
|
|
title: thread.title,
|
|
providerInstanceId: thread.providerInstanceId,
|
|
modelSelection: thread.modelSelection,
|
|
runtimeMode: thread.runtimeMode,
|
|
interactionMode: thread.interactionMode,
|
|
branch: thread.branch,
|
|
worktreePath: thread.worktreePath,
|
|
linkedPullRequest: thread.linkedPullRequest ?? null,
|
|
pullRequests: thread.pullRequests,
|
|
branchPullRequest: thread.branchPullRequest ?? null,
|
|
activeProviderThreadId: thread.activeProviderThreadId,
|
|
lineage: thread.lineage,
|
|
forkedFrom: thread.forkedFrom,
|
|
createdBy: thread.createdBy,
|
|
creationSource: thread.creationSource,
|
|
latestRunId: latestRun?.runId ?? null,
|
|
activeRunId: runtime?.activeRunId ?? null,
|
|
status: runtime?.status ?? "idle",
|
|
pendingRuntimeRequest:
|
|
pendingRequest === null
|
|
? null
|
|
: { id: pendingRequest.id, kind: pendingRequest.kind, createdAt: pendingRequest.createdAt },
|
|
latestVisibleMessage: null,
|
|
latestUserMessageAt: latestUserMessageAt(projection),
|
|
hasActionableProposedPlan: false,
|
|
itemCount: projection.turnItems.length,
|
|
visibleItemCount: projection.visibleTurnItems.length,
|
|
createdAt: thread.createdAt,
|
|
updatedAt: thread.updatedAt,
|
|
archivedAt: thread.archivedAt,
|
|
settledOverride: thread.settledOverride,
|
|
settledAt: thread.settledAt,
|
|
unsettledAt: thread.unsettledAt,
|
|
activeOrderKey: thread.activeOrderKey,
|
|
autoSettleDisabledAt: thread.autoSettleDisabledAt,
|
|
pinnedAt: thread.pinnedAt,
|
|
pinOrderKey: thread.pinOrderKey,
|
|
snoozedUntil: thread.snoozedUntil ?? null,
|
|
snoozedAt: thread.snoozedAt ?? null,
|
|
deletedAt: thread.deletedAt,
|
|
});
|
|
}
|
|
|
|
function useResolvedThreadSelection(params: ThreadSelectionRouteParams | undefined) {
|
|
const routeParams = params ?? {};
|
|
const routeThreadRef = useMemo<ScopedThreadRef | null>(() => {
|
|
const environmentId = firstRouteParam(routeParams.environmentId);
|
|
const threadId = firstRouteParam(routeParams.threadId);
|
|
if (!environmentId || !threadId) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
environmentId: EnvironmentId.make(environmentId),
|
|
threadId: ThreadId.make(threadId),
|
|
};
|
|
}, [routeParams.environmentId, routeParams.threadId]);
|
|
const lastRouteThreadRef = useRef<ScopedThreadRef | null>(null);
|
|
if (routeThreadRef !== null) {
|
|
lastRouteThreadRef.current = routeThreadRef;
|
|
}
|
|
const selectedThreadRef = routeThreadRef ?? lastRouteThreadRef.current;
|
|
const selectedThreadShell = useThreadShell(selectedThreadRef);
|
|
const selectedThreadKey =
|
|
selectedThreadRef === null
|
|
? null
|
|
: scopedThreadKey(selectedThreadRef.environmentId, selectedThreadRef.threadId);
|
|
const queuedMessagesByThreadKey = useThreadOutboxMessages();
|
|
const creationOutcome = useAtomValue(pendingThreadCreationOutcomesAtom);
|
|
// A creation the outbox still holds or just delivered: the thread screen
|
|
// opened before the server made the thread, so present a stand-in shell.
|
|
const pendingCreation = useMemo<PendingThreadCreation | null>(() => {
|
|
if (selectedThreadKey === null) {
|
|
return null;
|
|
}
|
|
const queued = queuedMessagesByThreadKey[selectedThreadKey]?.find(
|
|
(message) => message.creation !== undefined,
|
|
);
|
|
const outcome = creationOutcome[selectedThreadKey] ?? null;
|
|
const message = queued ?? outcome?.message ?? null;
|
|
return message === null ? null : { message, outcome };
|
|
}, [creationOutcome, queuedMessagesByThreadKey, selectedThreadKey]);
|
|
// Until the creation is delivered the server has no thread to subscribe
|
|
// to; subscribing anyway would retry "not found" for the whole setup.
|
|
const selectedThreadDetailRef =
|
|
selectedThreadShell !== null ||
|
|
pendingCreation === null ||
|
|
pendingCreation.outcome?.kind === "delivered"
|
|
? selectedThreadRef
|
|
: null;
|
|
const [previousCreation, setPreviousCreation] = useState<PendingThreadCreation | null>(null);
|
|
// Normal selection is shell-only. Detail readers subscribe separately; only
|
|
// optimistic creation needs the projection here until its prompt arrives.
|
|
const needsDetail =
|
|
selectedThreadShell === null || pendingCreation !== null || previousCreation !== null;
|
|
const selectedThreadDetailState = useEnvironmentThread(
|
|
needsDetail ? (selectedThreadDetailRef?.environmentId ?? null) : null,
|
|
needsDetail ? (selectedThreadDetailRef?.threadId ?? null) : null,
|
|
);
|
|
const selectedThreadDetail = Option.getOrNull(selectedThreadDetailState.data);
|
|
const selectedThread = useMemo(
|
|
() =>
|
|
selectedThreadShell ??
|
|
(selectedThreadRef !== null && selectedThreadDetail !== null
|
|
? threadDetailToShell(selectedThreadRef.environmentId, selectedThreadDetail)
|
|
: pendingCreation !== null
|
|
? pendingThreadCreationShell(pendingCreation.message)
|
|
: null),
|
|
[pendingCreation, selectedThreadDetail, selectedThreadRef, selectedThreadShell],
|
|
);
|
|
const selectedThreadCreation = resolvePendingThreadCreation({
|
|
threadKey: selectedThreadKey,
|
|
pending: pendingCreation,
|
|
previous: previousCreation,
|
|
detail: selectedThreadDetail,
|
|
});
|
|
if (previousCreation !== selectedThreadCreation) {
|
|
setPreviousCreation(selectedThreadCreation);
|
|
}
|
|
const selectedProjectRef = useMemo<ScopedProjectRef | null>(
|
|
() =>
|
|
selectedThread === null
|
|
? null
|
|
: {
|
|
environmentId: selectedThread.environmentId,
|
|
projectId: selectedThread.projectId,
|
|
},
|
|
[selectedThread],
|
|
);
|
|
const selectedThreadProject = useProject(selectedProjectRef);
|
|
const selectedEnvironmentId = selectedThread?.environmentId ?? null;
|
|
const selectedEnvironmentConnection = useSavedRemoteConnection(selectedEnvironmentId);
|
|
const selectedEnvironmentRuntime = useRemoteEnvironmentRuntime(selectedEnvironmentId);
|
|
|
|
return useMemo(
|
|
() => ({
|
|
selectedThreadRef,
|
|
selectedThread,
|
|
selectedThreadCreation,
|
|
selectedThreadDetailRef,
|
|
selectedThreadProject,
|
|
selectedEnvironmentConnection,
|
|
selectedEnvironmentRuntime,
|
|
}),
|
|
[
|
|
selectedEnvironmentConnection,
|
|
selectedEnvironmentRuntime,
|
|
selectedThread,
|
|
selectedThreadCreation,
|
|
selectedThreadDetailRef,
|
|
selectedThreadProject,
|
|
selectedThreadRef,
|
|
],
|
|
);
|
|
}
|
|
|
|
type ThreadSelectionState = ReturnType<typeof useResolvedThreadSelection>;
|
|
|
|
export function useThreadSelection(): ThreadSelectionState {
|
|
const route = useRoute<RouteProp<Record<string, ThreadSelectionRouteParams | undefined>>>();
|
|
return useResolvedThreadSelection(route.params);
|
|
}
|