@@ -9,8 +9,19 @@ import {
99 type EnvironmentThreadStatus ,
1010 type ThreadHistoryMeta ,
1111} from "@t3tools/client-runtime/state/threads" ;
12+ import {
13+ arrayElementsEqual ,
14+ parseThreadKey ,
15+ threadKey ,
16+ } from "@t3tools/client-runtime/state/entities" ;
1217import type { ScopedProjectRef , ScopedThreadRef , ServerConfig } from "@t3tools/contracts" ;
13- import type { EnvironmentId , OrchestrationV2ProjectedTurnItem } from "@t3tools/contracts" ;
18+ import type {
19+ EnvironmentId ,
20+ OrchestrationV2ProjectedTurnItem ,
21+ OrchestrationV2ThreadProjection ,
22+ OrchestrationV2ThreadShell ,
23+ ThreadId ,
24+ } from "@t3tools/contracts" ;
1425import { Atom } from "effect/reactivity" ;
1526import { appAtomRegistry } from "../rpc/atomRegistry" ;
1627import { environmentProjects } from "./projects" ;
@@ -298,3 +309,70 @@ export function readEnvironmentThreadRefs(
298309export function readThreadShells ( ) : ReadonlyArray < EnvironmentThreadShell > {
299310 return appAtomRegistry . get ( environmentThreadShells . threadShellsAtom ) ;
300311}
312+
313+ function shellParentThreadId (
314+ thread : Pick < OrchestrationV2ThreadShell , "forkedFrom" | "lineage" > ,
315+ ) : ThreadId | null {
316+ return thread . forkedFrom ?. type === "run"
317+ ? thread . forkedFrom . threadId
318+ : thread . lineage . parentThreadId ;
319+ }
320+
321+ /**
322+ * The live shells the panel's immediate relationships can reach: the thread,
323+ * its parent, its children, and the threads its subagents, workflow members and
324+ * context transfers name. Keeps its array while those shells are unchanged, so
325+ * another thread streaming in the same environment does not re-render the panel.
326+ */
327+ /** Exported for tests. */
328+ export function selectThreadRelationshipShells (
329+ threadId : ThreadId ,
330+ projection : Pick <
331+ OrchestrationV2ThreadProjection ,
332+ "thread" | "subagents" | "contextTransfers"
333+ > | null ,
334+ threads : ReadonlyArray < OrchestrationV2ThreadShell > ,
335+ ) : ReadonlyArray < OrchestrationV2ThreadShell > {
336+ const named = new Set < string > ( [ threadId ] ) ;
337+ if ( projection !== null ) {
338+ const parentThreadId = shellParentThreadId ( projection . thread ) ;
339+ if ( parentThreadId !== null ) named . add ( parentThreadId ) ;
340+ for ( const subagent of projection . subagents ) {
341+ if ( subagent . childThreadId !== null ) named . add ( subagent . childThreadId ) ;
342+ for ( const agent of subagent . workflow ?. agents ?? [ ] ) {
343+ if ( agent . childThreadId !== undefined ) named . add ( agent . childThreadId ) ;
344+ }
345+ }
346+ for ( const transfer of projection . contextTransfers ) {
347+ named . add ( transfer . sourceThreadId ) ;
348+ named . add ( transfer . targetThreadId ) ;
349+ }
350+ }
351+ const current = threads . find ( ( thread ) => thread . id === threadId ) ;
352+ const currentParentId = current === undefined ? null : shellParentThreadId ( current ) ;
353+ if ( currentParentId !== null ) named . add ( currentParentId ) ;
354+ return threads . filter (
355+ ( thread ) => named . has ( thread . id ) || shellParentThreadId ( thread ) === threadId ,
356+ ) ;
357+ }
358+
359+ const relationshipShellsAtomFamily = Atom . family ( ( key : string ) => {
360+ const ref = parseThreadKey ( key ) ;
361+ let previous : ReadonlyArray < OrchestrationV2ThreadShell > = [ ] ;
362+ return Atom . make ( ( get ) => {
363+ const next = selectThreadRelationshipShells (
364+ ref . threadId ,
365+ get ( environmentThreadDetails . threadAtom ( ref ) ) ?. projection ?? null ,
366+ get ( environmentThreadShells . environmentThreadsAtom ( ref . environmentId ) ) ,
367+ ) ;
368+ if ( arrayElementsEqual ( previous , next ) ) return previous ;
369+ previous = next ;
370+ return next ;
371+ } ) . pipe ( Atom . withLabel ( `web-thread-relationship-shells:${ key } ` ) ) ;
372+ } ) ;
373+
374+ export function useThreadRelationshipShells (
375+ ref : ScopedThreadRef ,
376+ ) : ReadonlyArray < OrchestrationV2ThreadShell > {
377+ return useAtomValue ( relationshipShellsAtomFamily ( threadKey ( ref ) ) ) ;
378+ }
0 commit comments