- Add thread-panel presentation logic for inline vs popover layouts - Move thread details controls into the chat header/sidebar flow - Add shared wait-for-atom helper and workspace display name parsing Co-authored-by: codex <[email protected]>
283 lines
10 KiB
TypeScript
283 lines
10 KiB
TypeScript
import { useAtomValue } from "@effect/atom-react";
|
|
import type {
|
|
EnvironmentProject,
|
|
EnvironmentThread,
|
|
EnvironmentThreadShell,
|
|
ScopedThreadProjection,
|
|
ThreadConversationMessage,
|
|
ThreadProposedPlan,
|
|
ThreadRuntimeSummary,
|
|
ThreadWorkEntry,
|
|
} from "@t3tools/client-runtime/state/shell";
|
|
import { mergeEnvironmentThread } from "@t3tools/client-runtime/state/threads";
|
|
import type { ScopedProjectRef, ScopedThreadRef, ServerConfig } from "@t3tools/contracts";
|
|
import type { EnvironmentId, OrchestrationV2ProjectedTurnItem, ThreadId } from "@t3tools/contracts";
|
|
import { Atom } from "effect/unstable/reactivity";
|
|
import { useMemo } from "react";
|
|
import { appAtomRegistry } from "../rpc/atomRegistry";
|
|
import { environmentProjects } from "./projects";
|
|
import { environmentServerConfigsAtom } from "./server";
|
|
import { allEnvironmentShellsBootstrappedAtom } from "./shell";
|
|
import { environmentThreadDetails, environmentThreadShells } from "./threads";
|
|
import { waitForAtomValue } from "./waitForAtomValue";
|
|
|
|
const EMPTY_PROJECT_REFS: ReadonlyArray<ScopedProjectRef> = Object.freeze([]);
|
|
const EMPTY_THREAD_REFS: ReadonlyArray<ScopedThreadRef> = Object.freeze([]);
|
|
const EMPTY_MESSAGES: ReadonlyArray<ThreadConversationMessage> = Object.freeze([]);
|
|
const EMPTY_WORK_ENTRIES: ReadonlyArray<ThreadWorkEntry> = Object.freeze([]);
|
|
const EMPTY_PROPOSED_PLANS: ReadonlyArray<ThreadProposedPlan> = Object.freeze([]);
|
|
const EMPTY_VISIBLE_TURN_ITEMS: ReadonlyArray<OrchestrationV2ProjectedTurnItem> = Object.freeze([]);
|
|
|
|
const EMPTY_PROJECT_ATOM = Atom.make<EnvironmentProject | null>(null).pipe(
|
|
Atom.withLabel("web-project:empty"),
|
|
);
|
|
const EMPTY_PROJECT_REFS_ATOM = Atom.make(EMPTY_PROJECT_REFS).pipe(
|
|
Atom.withLabel("web-project-refs:empty"),
|
|
);
|
|
const EMPTY_THREAD_REFS_ATOM = Atom.make(EMPTY_THREAD_REFS).pipe(
|
|
Atom.withLabel("web-thread-refs:empty"),
|
|
);
|
|
const EMPTY_THREAD_SHELL_ATOM = Atom.make<EnvironmentThreadShell | null>(null).pipe(
|
|
Atom.withLabel("web-thread-shell:empty"),
|
|
);
|
|
const EMPTY_THREAD_DETAIL_ATOM = Atom.make<EnvironmentThread | null>(null).pipe(
|
|
Atom.withLabel("web-thread-detail:empty"),
|
|
);
|
|
const EMPTY_SCOPED_THREAD_PROJECTION_ATOM = Atom.make<ScopedThreadProjection | null>(null).pipe(
|
|
Atom.withLabel("web-scoped-thread-projection:empty"),
|
|
);
|
|
const EMPTY_MESSAGES_ATOM = Atom.make(EMPTY_MESSAGES).pipe(
|
|
Atom.withLabel("web-thread-messages:empty"),
|
|
);
|
|
const EMPTY_WORK_ENTRIES_ATOM = Atom.make(EMPTY_WORK_ENTRIES).pipe(
|
|
Atom.withLabel("web-thread-work-entries:empty"),
|
|
);
|
|
const EMPTY_PROPOSED_PLANS_ATOM = Atom.make(EMPTY_PROPOSED_PLANS).pipe(
|
|
Atom.withLabel("web-thread-proposed-plans:empty"),
|
|
);
|
|
const EMPTY_VISIBLE_TURN_ITEMS_ATOM = Atom.make(EMPTY_VISIBLE_TURN_ITEMS).pipe(
|
|
Atom.withLabel("web-thread-visible-turn-items:empty"),
|
|
);
|
|
const EMPTY_RUNTIME_ATOM = Atom.make<ThreadRuntimeSummary | null>(null).pipe(
|
|
Atom.withLabel("web-thread-runtime:empty"),
|
|
);
|
|
|
|
export const activeEnvironmentIdAtom = Atom.make<EnvironmentId | null>(null).pipe(
|
|
Atom.keepAlive,
|
|
Atom.withLabel("web-active-environment-id"),
|
|
);
|
|
|
|
export function useActiveEnvironmentId(): EnvironmentId | null {
|
|
return useAtomValue(activeEnvironmentIdAtom);
|
|
}
|
|
|
|
export function readActiveEnvironmentId(): EnvironmentId | null {
|
|
return appAtomRegistry.get(activeEnvironmentIdAtom);
|
|
}
|
|
|
|
export function setActiveEnvironmentId(environmentId: EnvironmentId | null): void {
|
|
appAtomRegistry.set(activeEnvironmentIdAtom, environmentId);
|
|
}
|
|
|
|
export function useProjectRefs(): ReadonlyArray<ScopedProjectRef> {
|
|
return useAtomValue(environmentProjects.projectRefsAtom);
|
|
}
|
|
|
|
export function useThreadRefs(): ReadonlyArray<ScopedThreadRef> {
|
|
return useAtomValue(environmentThreadShells.threadRefsAtom);
|
|
}
|
|
|
|
export function useEnvironmentProjectRefs(
|
|
environmentId: EnvironmentId | null,
|
|
): ReadonlyArray<ScopedProjectRef> {
|
|
return useAtomValue(
|
|
environmentId === null
|
|
? EMPTY_PROJECT_REFS_ATOM
|
|
: environmentProjects.environmentProjectRefsAtom(environmentId),
|
|
);
|
|
}
|
|
|
|
export function useEnvironmentThreadRefs(
|
|
environmentId: EnvironmentId | null,
|
|
): ReadonlyArray<ScopedThreadRef> {
|
|
return useAtomValue(
|
|
environmentId === null
|
|
? EMPTY_THREAD_REFS_ATOM
|
|
: environmentThreadShells.environmentThreadRefsAtom(environmentId),
|
|
);
|
|
}
|
|
|
|
export function useProjects(): ReadonlyArray<EnvironmentProject> {
|
|
return useAtomValue(environmentProjects.projectsAtom);
|
|
}
|
|
|
|
export function useServerConfigs(): ReadonlyMap<EnvironmentId, ServerConfig> {
|
|
return useAtomValue(environmentServerConfigsAtom);
|
|
}
|
|
|
|
export function useThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
|
|
return useAtomValue(environmentThreadShells.threadShellsAtom);
|
|
}
|
|
|
|
export function useAllEnvironmentShellsBootstrapped(): boolean {
|
|
return useAtomValue(allEnvironmentShellsBootstrappedAtom);
|
|
}
|
|
|
|
export function useThreadShellsForProjectRefs(
|
|
refs: ReadonlyArray<ScopedProjectRef>,
|
|
): ReadonlyArray<EnvironmentThreadShell> {
|
|
return useAtomValue(environmentThreadShells.threadShellsForProjectRefsAtom(refs));
|
|
}
|
|
|
|
export function useProject(ref: ScopedProjectRef | null): EnvironmentProject | null {
|
|
return useAtomValue(ref === null ? EMPTY_PROJECT_ATOM : environmentProjects.projectAtom(ref));
|
|
}
|
|
|
|
export function useThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null {
|
|
return useAtomValue(
|
|
ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.threadShellAtom(ref),
|
|
);
|
|
}
|
|
|
|
export function useThreadDetail(ref: ScopedThreadRef | null): EnvironmentThread | null {
|
|
return useAtomValue(
|
|
ref === null ? EMPTY_THREAD_DETAIL_ATOM : environmentThreadDetails.detailAtom(ref),
|
|
);
|
|
}
|
|
|
|
export function useThreadProjection(ref: ScopedThreadRef | null): ScopedThreadProjection | null {
|
|
return useAtomValue(
|
|
ref === null ? EMPTY_SCOPED_THREAD_PROJECTION_ATOM : environmentThreadDetails.threadAtom(ref),
|
|
);
|
|
}
|
|
|
|
/** Detail collections composed with shell-authoritative thread/workspace metadata. */
|
|
export function useThread(
|
|
ref: ScopedThreadRef | null,
|
|
options?: {
|
|
/**
|
|
* Client-reserved draft thread ids do not exist on the server until the
|
|
* first send. Waiting for the shell index avoids polling the detail
|
|
* endpoint for an intentionally missing thread during that window.
|
|
*/
|
|
waitForShell?: boolean;
|
|
},
|
|
): EnvironmentThread | null {
|
|
const shell = useThreadShell(ref);
|
|
const detail = useThreadDetail(
|
|
resolveThreadDetailRef(ref, {
|
|
shellExists: shell !== null,
|
|
waitForShell: options?.waitForShell === true,
|
|
}),
|
|
);
|
|
return useMemo(() => mergeEnvironmentThread(detail, shell), [detail, shell]);
|
|
}
|
|
|
|
export function useThreadMessages(
|
|
ref: ScopedThreadRef | null,
|
|
): ReadonlyArray<ThreadConversationMessage> {
|
|
return useAtomValue(
|
|
ref === null ? EMPTY_MESSAGES_ATOM : environmentThreadDetails.messagesAtom(ref),
|
|
);
|
|
}
|
|
|
|
export function useThreadWorkEntries(ref: ScopedThreadRef | null): ReadonlyArray<ThreadWorkEntry> {
|
|
return useAtomValue(
|
|
ref === null ? EMPTY_WORK_ENTRIES_ATOM : environmentThreadDetails.workEntriesAtom(ref),
|
|
);
|
|
}
|
|
|
|
export function useThreadProposedPlans(
|
|
ref: ScopedThreadRef | null,
|
|
): ReadonlyArray<ThreadProposedPlan> {
|
|
return useAtomValue(
|
|
ref === null ? EMPTY_PROPOSED_PLANS_ATOM : environmentThreadDetails.proposedPlansAtom(ref),
|
|
);
|
|
}
|
|
|
|
export function useThreadVisibleTurnItems(
|
|
ref: ScopedThreadRef | null,
|
|
): ReadonlyArray<OrchestrationV2ProjectedTurnItem> {
|
|
return useAtomValue(
|
|
ref === null
|
|
? EMPTY_VISIBLE_TURN_ITEMS_ATOM
|
|
: environmentThreadDetails.visibleTurnItemsAtom(ref),
|
|
);
|
|
}
|
|
|
|
export function useThreadRuntime(ref: ScopedThreadRef | null): ThreadRuntimeSummary | null {
|
|
return useAtomValue(
|
|
ref === null ? EMPTY_RUNTIME_ATOM : environmentThreadDetails.runtimeAtom(ref),
|
|
);
|
|
}
|
|
|
|
export function readProject(ref: ScopedProjectRef): EnvironmentProject | null {
|
|
return appAtomRegistry.get(environmentProjects.projectAtom(ref));
|
|
}
|
|
|
|
export function readThreadShell(ref: ScopedThreadRef): EnvironmentThreadShell | null {
|
|
return appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref));
|
|
}
|
|
|
|
export function waitForThreadShell(ref: ScopedThreadRef, timeoutMs = 5_000): Promise<boolean> {
|
|
return waitForAtomValue({
|
|
registry: appAtomRegistry,
|
|
atom: environmentThreadShells.threadShellAtom(ref),
|
|
predicate: (thread) => thread !== null,
|
|
timeoutMs,
|
|
});
|
|
}
|
|
|
|
/** Whether the environment's server understands thread.pin/unpin.
|
|
Same version-skew contract as settlement. */
|
|
export function readEnvironmentSupportsPinning(environmentId: EnvironmentId): boolean {
|
|
return (
|
|
appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
|
|
.threadPinning === true
|
|
);
|
|
}
|
|
|
|
/** Whether the environment's server understands thread title regeneration.
|
|
Same version-skew contract as settlement. */
|
|
export function readEnvironmentSupportsTitleRegeneration(environmentId: EnvironmentId): boolean {
|
|
return (
|
|
appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
|
|
.threadTitleRegeneration === true
|
|
);
|
|
}
|
|
|
|
/** Whether the environment's server understands thread.pin.reorder (and
|
|
orderKey on thread.pin). Same version-skew contract as settlement. */
|
|
export function readEnvironmentSupportsPinReorder(environmentId: EnvironmentId): boolean {
|
|
return (
|
|
appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
|
|
.threadPinReorder === true
|
|
);
|
|
}
|
|
|
|
export function readThreadDetail(ref: ScopedThreadRef): EnvironmentThread | null {
|
|
return appAtomRegistry.get(environmentThreadDetails.detailAtom(ref));
|
|
}
|
|
|
|
export function readEnvironmentThreadRefs(
|
|
environmentId: EnvironmentId,
|
|
): ReadonlyArray<ScopedThreadRef> {
|
|
return appAtomRegistry.get(environmentThreadShells.environmentThreadRefsAtom(environmentId));
|
|
}
|
|
|
|
export function readThreadRefs(): ReadonlyArray<ScopedThreadRef> {
|
|
return appAtomRegistry.get(environmentThreadShells.threadRefsAtom);
|
|
}
|
|
|
|
export function readThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
|
|
return appAtomRegistry.get(environmentThreadShells.threadShellsAtom);
|
|
}
|
|
|
|
export function findThreadRef(threadId: ThreadId): ScopedThreadRef | null {
|
|
return (
|
|
appAtomRegistry
|
|
.get(environmentThreadShells.threadRefsAtom)
|
|
.find((ref) => ref.threadId === threadId) ?? null
|
|
);
|
|
}
|