diff --git a/apps/mobile/src/features/workflows/WorkflowRunRouteScreen.tsx b/apps/mobile/src/features/workflows/WorkflowRunRouteScreen.tsx index c267a6466ebdf..4b491558dc3af 100644 --- a/apps/mobile/src/features/workflows/WorkflowRunRouteScreen.tsx +++ b/apps/mobile/src/features/workflows/WorkflowRunRouteScreen.tsx @@ -10,7 +10,7 @@ import { type WorkflowBeat, type WorkflowRunNode, } from "@t3tools/client-runtime/workflows"; -import { EnvironmentId, ThreadId, type WorkflowQuestion } from "@t3tools/contracts"; +import { EnvironmentId, type WorkflowQuestion } from "@t3tools/contracts"; import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native"; import { T, Var, useGT, useMessages } from "gt-react-native"; import { useRef, useState } from "react"; @@ -33,7 +33,7 @@ import { Facts, plain, reportFailure, - useCanOpenThread, + useWorkflowThreadOpener, workflowRunFeedOf, } from "./WorkflowStepCard"; import { WORKFLOW_TONE_CLASS, WorkflowChip } from "./WorkflowsScreen"; @@ -183,14 +183,9 @@ function RunBody(props: { readonly environmentId: EnvironmentId; readonly node: ), ), ); - const canOpenThread = useCanOpenThread(environmentId); - const openThread = (thread: NonNullable) => - navigation.dispatch( - StackActions.push("Thread", { - environmentId: thread.environmentId ?? environmentId, - threadId: ThreadId.make(thread.threadId), - }), - ); + const threadOpener = useWorkflowThreadOpener(environmentId, (thread) => + navigation.dispatch(StackActions.push("Thread", thread)), + ); const openStep = (step: string) => navigation.dispatch( StackActions.push("WorkflowStep", { environmentId, runId: run.runId, step }), @@ -216,7 +211,7 @@ function RunBody(props: { readonly environmentId: EnvironmentId; readonly node: environmentId={environmentId} question={asked} canAnswer={canOperate} - onOpenThread={asked.thread !== null && canOpenThread(asked.thread) ? openThread : null} + onOpenThread={asked.thread === null ? null : threadOpener(asked.thread)} /> ); const pausedAt = feed === null ? null : workflowPausedBeat(feed, run.status); diff --git a/apps/mobile/src/features/workflows/WorkflowStepCard.tsx b/apps/mobile/src/features/workflows/WorkflowStepCard.tsx index 92f2a0f6a99b4..be10931440e72 100644 --- a/apps/mobile/src/features/workflows/WorkflowStepCard.tsx +++ b/apps/mobile/src/features/workflows/WorkflowStepCard.tsx @@ -6,7 +6,14 @@ import { type WorkflowBeat, type WorkflowRunNode, } from "@t3tools/client-runtime/workflows"; -import type { EnvironmentId, WorkflowStep } from "@t3tools/contracts"; +import { scopeThreadRef } from "@t3tools/client-runtime/environment"; +import { resolveLinkedThreadOpen } from "@t3tools/client-runtime/linked-thread"; +import { + EnvironmentId, + ThreadId, + type ScopedThreadRef, + type WorkflowStep, +} from "@t3tools/contracts"; import { isAtomCommandInterrupted, squashAtomCommandFailure, @@ -21,6 +28,8 @@ import { Button } from "../../components/ui/Button"; import * as Dialog from "../../components/ui/Dialog"; import { cn } from "../../lib/cn"; import { useEnvironments } from "../../state/environments"; +import { useLinkedEnvironments } from "../../state/peerLinks"; +import { useOpenLinkedThreadRead } from "../threads/useOpenLinkedThread"; export function reportFailure(title: string, result: AtomCommandResult) { if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return; @@ -63,12 +72,36 @@ export function Facts(props: { ); } -/** Whether this device can open a thread: only one in an environment it is paired with. */ -export function useCanOpenThread(environmentId: EnvironmentId) { - const { environments } = useEnvironments(); - return (thread: NonNullable) => { - const target = thread.environmentId ?? environmentId; - return environments.some((environment) => environment.environmentId === target); +/** + * How this device opens a step's thread from a run on `environmentId`: directly where it is paired, + * else read-only through the run's environment when that links to it. Null where it can do neither. + */ +export function useWorkflowThreadOpener( + environmentId: EnvironmentId, + openDirect: (thread: ScopedThreadRef) => void, + options?: { readonly replace?: boolean }, +) { + const { presentationById } = useEnvironments(); + const linkedEnvironments = useLinkedEnvironments(environmentId); + const openLinkedRead = useOpenLinkedThreadRead(); + return (thread: NonNullable): (() => void) | null => { + const ref = scopeThreadRef( + thread.environmentId === null ? environmentId : EnvironmentId.make(thread.environmentId), + ThreadId.make(thread.threadId), + ); + const own = presentationById.get(ref.environmentId); + const label = linkedEnvironments.get(ref.environmentId); + const open = resolveLinkedThreadOpen({ + environmentId: ref.environmentId, + viaEnvironmentId: environmentId, + own: own && { connected: own.connection.phase === "connected" }, + linked: label !== undefined, + }); + if (open === "unavailable") return null; + if (open === "linked" && label !== undefined) { + return () => openLinkedRead(ref, { viaEnvironmentId: environmentId, label }, null, options); + } + return () => openDirect(ref); }; } diff --git a/apps/mobile/src/features/workflows/WorkflowStepSheet.tsx b/apps/mobile/src/features/workflows/WorkflowStepSheet.tsx index f3e5afe9cbf3d..d761887f8f3a8 100644 --- a/apps/mobile/src/features/workflows/WorkflowStepSheet.tsx +++ b/apps/mobile/src/features/workflows/WorkflowStepSheet.tsx @@ -2,9 +2,8 @@ import { allWorkflowRunNodes, groupWorkflowRuns, workflowResultLines, - type WorkflowBeat, } from "@t3tools/client-runtime/workflows"; -import { EnvironmentId, ThreadId } from "@t3tools/contracts"; +import { EnvironmentId } from "@t3tools/contracts"; import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native"; import { useGT } from "gt-react-native"; import { ScrollView, View } from "react-native"; @@ -15,7 +14,13 @@ import { ScreenContent } from "../../components/ui/ScreenContent"; import { SheetHeader } from "../../components/ui/SheetHeader"; import { useEnvironmentQuery } from "../../state/query"; import { serverEnvironment } from "../../state/server"; -import { BeatCard, Card, Facts, useCanOpenThread, workflowRunFeedOf } from "./WorkflowStepCard"; +import { + BeatCard, + Card, + Facts, + useWorkflowThreadOpener, + workflowRunFeedOf, +} from "./WorkflowStepCard"; export type WorkflowStepParams = { readonly environmentId: string; @@ -50,14 +55,11 @@ export function WorkflowStepSheet({ route }: StaticScreenProps) => - navigation.dispatch( - StackActions.replace("Thread", { - environmentId: thread.environmentId ?? environmentId, - threadId: ThreadId.make(thread.threadId), - }), - ); - const canOpenThread = useCanOpenThread(environmentId); + const threadOpener = useWorkflowThreadOpener( + environmentId, + (thread) => navigation.dispatch(StackActions.replace("Thread", thread)), + { replace: true }, + ); const openRun = (runId: string) => navigation.dispatch(StackActions.replace("WorkflowRun", { environmentId, runId })); return ( @@ -108,7 +110,7 @@ export function WorkflowStepSheet({ route }: StaticScreenProps )} diff --git a/apps/web/src/components/workflows/WorkflowPane.tsx b/apps/web/src/components/workflows/WorkflowPane.tsx index e23d5de8a50c7..d43f2baf4f24d 100644 --- a/apps/web/src/components/workflows/WorkflowPane.tsx +++ b/apps/web/src/components/workflows/WorkflowPane.tsx @@ -24,6 +24,7 @@ import WorkflowThreadPane, { useOpenThreadPage, WorkflowThreadTabLabel, } from "./WorkflowThreadPane"; +import { useWorkflowThreadOpen } from "./useWorkflowThreadOpen"; /** * The pane beside a run: a tab per transcript or reference opened from it. Only the tab that @@ -59,11 +60,15 @@ export default function WorkflowPane({ (reference) => reference.kind === tab.kind && reference.key === tab.key, ) ?? null; const activeReference = active === undefined || "thread" in active ? null : referenceOf(active); + const activeThread = active !== undefined && "thread" in active ? threadOf(active) : null; + const activeThreadLinked = + useWorkflowThreadOpen(environmentId, activeThread?.environmentId ?? environmentId).open === + "linked"; const openFull = active === undefined ? null : "thread" in active - ? () => openThreadPage(threadOf(active)) + ? () => openThreadPage(threadOf(active), activeThreadLinked ? environmentId : undefined) : activeReference === null ? null : openReferencePage(activeReference); @@ -144,6 +149,7 @@ export default function WorkflowPane({ {active === undefined ? null : "thread" in active ? ( ) : activeReference !== null ? ( diff --git a/apps/web/src/components/workflows/WorkflowRunFeed.tsx b/apps/web/src/components/workflows/WorkflowRunFeed.tsx index d77b54d8dcc11..bbc51689827c3 100644 --- a/apps/web/src/components/workflows/WorkflowRunFeed.tsx +++ b/apps/web/src/components/workflows/WorkflowRunFeed.tsx @@ -40,7 +40,7 @@ import { requestConfirmDialog } from "../../confirmDialog"; import { useNowMinute } from "../../hooks/useNowMinute"; import { cn } from "../../lib/utils"; import { useQuietThreadShell } from "../../state/entities"; -import { useEnvironment, useEnvironmentProviders } from "../../state/environments"; +import { useEnvironmentProviders } from "../../state/environments"; import { type MessageResolver } from "../../i18n/messages"; import { useEnvironmentQuery } from "../../state/query"; import { serverEnvironment } from "../../state/server"; @@ -75,6 +75,7 @@ import { } from "@t3tools/client-runtime/workflows"; import type { WorkflowPaneTab } from "./workflowPane.logic"; import { WorkflowReferenceChip } from "./WorkflowReferences"; +import { useWorkflowThreadOpen } from "./useWorkflowThreadOpen"; /** An agent's thread, as a step names it: the linked environment it is in, or null for this one. */ export interface WorkflowThreadTarget { @@ -1629,8 +1630,7 @@ function WorkflowAgentCard({ beat.thread?.environmentId == null ? environmentId : EnvironmentId.make(beat.thread.environmentId); - // This client can only show a thread on an environment it is itself connected to. - const threadEnvironment = useEnvironment(threadEnvironmentId); + const threadOpen = useWorkflowThreadOpen(environmentId, threadEnvironmentId); const shell = useQuietThreadShell( beat.thread === null ? null @@ -1658,7 +1658,7 @@ function WorkflowAgentCard({ length, beat.attempts > 1 ? nthTry(beat.attempts, m) : null, elsewhere - ? gt("on {environment}", { environment: threadEnvironment?.label ?? threadEnvironmentId }) + ? gt("on {environment}", { environment: threadOpen.label ?? threadEnvironmentId }) : null, ] .filter((part) => part !== null) @@ -1684,7 +1684,7 @@ function WorkflowAgentCard({ name={beat.name} sub={sub} action={ - beat.thread === null ? null : threadEnvironment === null ? ( + beat.thread === null ? null : threadOpen.open === "unavailable" ? ( Thread on {threadEnvironmentId} @@ -1829,7 +1829,8 @@ function WorkflowQuestionCard({ question.thread?.environmentId == null ? environmentId : EnvironmentId.make(question.thread.environmentId); - const reachable = useEnvironment(threadEnvironmentId) !== null; + const reachable = + useWorkflowThreadOpen(environmentId, threadEnvironmentId).open !== "unavailable"; const choose = async (choice: string) => { if (busy) return; setBusy(true); diff --git a/apps/web/src/components/workflows/WorkflowRunsView.browser.test.tsx b/apps/web/src/components/workflows/WorkflowRunsView.browser.test.tsx index 386ba513e0e5b..63f6ad12e5193 100644 --- a/apps/web/src/components/workflows/WorkflowRunsView.browser.test.tsx +++ b/apps/web/src/components/workflows/WorkflowRunsView.browser.test.tsx @@ -60,6 +60,7 @@ const state = vi.hoisted(() => ({ commands: [] as Array, permitted: true, detailError: null as string | null, + linked: [] as ReadonlyArray, })); const canOperate = Atom.make(true); const cannotOperate = Atom.make(false); @@ -75,6 +76,7 @@ function setup( state.commands = []; state.permitted = true; state.detailError = null; + state.linked = []; } const environment = { environmentId, label: "Laptop" }; @@ -125,6 +127,15 @@ vi.mock(import("../../state/use-atom-command"), async (original) => ({ return Promise.resolve({ _tag: "Success", value: undefined }); }) as never, })); +vi.mock(import("../../state/peerLinks"), async (original) => ({ + ...(await original()), + useLinkedEnvironments: ((id: EnvironmentId | null) => + new Map( + id === environmentId + ? state.linked.map((link) => [EnvironmentId.make(link), "Build box"]) + : [], + )) as never, +})); vi.mock(import("../../state/environments"), async (original) => ({ ...(await original()), useEnvironment: ((id: EnvironmentId | null) => @@ -421,6 +432,41 @@ describe("WorkflowRunsView", () => { .not.toBeInTheDocument(); }); + it("opens an agent's thread on an environment this server links to", async () => { + setup( + { + runs: [run({ runId: "fix", title: "Fix", status: "completed", endedAt: at(12) })], + questions: [], + }, + { + fix: [ + step({ + name: "implement", + kind: "agent", + threadId: "thread-2", + environmentId: "build-box", + }), + ], + }, + ); + state.linked = ["build-box"]; + const thread = vi.fn(); + await using screen = await render(page("fix", { thread })); + + await screen + .getByRole("navigation", { name: "Steps so far" }) + .getByRole("button", { name: /implement/ }) + .click(); + const implement = screen.getByRole("region", { name: "implement" }); + await expect.element(implement).toMatchTextContent(/on Build box/); + await expect.element(implement).not.toMatchTextContent(/Thread on build-box/); + await implement.getByRole("button", { name: "Transcript" }).click(); + expect(thread).toHaveBeenCalledWith("fix", { + threadId: "thread-2", + environmentId: "build-box", + }); + }); + it("opens an invoked run from its parent's story", async () => { setup( { diff --git a/apps/web/src/components/workflows/WorkflowThreadPane.tsx b/apps/web/src/components/workflows/WorkflowThreadPane.tsx index 7e6432eaec393..15db55135a324 100644 --- a/apps/web/src/components/workflows/WorkflowThreadPane.tsx +++ b/apps/web/src/components/workflows/WorkflowThreadPane.tsx @@ -1,4 +1,4 @@ -import { type ScopedThreadRef } from "@t3tools/contracts"; +import { type EnvironmentId, type ScopedThreadRef } from "@t3tools/contracts"; import { useNavigate } from "@tanstack/react-router"; import { useGT } from "gt-react"; @@ -6,15 +6,26 @@ import { ComposerHandleContext } from "../../composerHandleContext"; import type { SideChatParentPanel } from "../../lib/sideChatPane"; import { useQuietThreadShell } from "../../state/entities"; import { useEnvironmentShellHasSnapshot } from "../../state/shell"; +import { linkedThreadRouteParams, linkedThreadSurface } from "../../linkedThreadSurface"; import { buildThreadRouteParams } from "../../threadRoutes"; import ChatView from "../ChatView"; import { BackgroundThreadContext } from "../chat/backgroundThread"; +import { LinkedThread } from "../chat/LinkedThreadPane"; +import { useWorkflowThreadOpen } from "./useWorkflowThreadOpen"; -/** Opens the thread as a full thread view. */ -export function useOpenThreadPage(): (threadRef: ScopedThreadRef) => void { +/** Opens the thread as a full thread view; `linked` reads it through `viaEnvironmentId`. */ +export function useOpenThreadPage(): ( + threadRef: ScopedThreadRef, + viaEnvironmentId?: EnvironmentId, +) => void { const navigate = useNavigate(); - return (threadRef) => - void navigate({ to: "/$environmentId/$threadId", params: buildThreadRouteParams(threadRef) }); + return (threadRef, viaEnvironmentId) => + void (viaEnvironmentId === undefined + ? navigate({ to: "/$environmentId/$threadId", params: buildThreadRouteParams(threadRef) }) + : navigate({ + to: "/linked/$viaEnvironmentId/$linkedEnvironmentId/$linkedThreadId", + params: linkedThreadRouteParams({ viaEnvironmentId, ...threadRef }), + })); } /** What a transcript's tab is called: the step its agent did, or the thread's own title. */ @@ -31,7 +42,28 @@ export function WorkflowThreadTabLabel({ threadRef }: { readonly threadRef: Scop * An agent step's thread beside its run: the same chat as anywhere, composer included, so a * person can read what the agent did and talk to it without leaving the run. */ -export default function WorkflowThreadPane({ threadRef }: { readonly threadRef: ScopedThreadRef }) { +export default function WorkflowThreadPane({ + viaEnvironmentId, + threadRef, +}: { + /** The environment running the workflow. */ + readonly viaEnvironmentId: EnvironmentId; + readonly threadRef: ScopedThreadRef; +}) { + const threadOpen = useWorkflowThreadOpen(viaEnvironmentId, threadRef.environmentId); + if (threadOpen.open === "linked" && threadOpen.label !== null) { + const surface = linkedThreadSurface({ + viaEnvironmentId, + ...threadRef, + label: threadOpen.label, + title: null, + }); + return ; + } + return ; +} + +function ConnectedWorkflowThreadPane({ threadRef }: { readonly threadRef: ScopedThreadRef }) { const gt = useGT(); const openThreadPage = useOpenThreadPage(); const shell = useQuietThreadShell(threadRef); diff --git a/apps/web/src/components/workflows/useWorkflowThreadOpen.ts b/apps/web/src/components/workflows/useWorkflowThreadOpen.ts new file mode 100644 index 0000000000000..9245ea7294f98 --- /dev/null +++ b/apps/web/src/components/workflows/useWorkflowThreadOpen.ts @@ -0,0 +1,30 @@ +import { + type LinkedThreadOpen, + resolveLinkedThreadOpen, +} from "@t3tools/client-runtime/linked-thread"; +import type { EnvironmentId } from "@t3tools/contracts"; + +import { useConnectedEnvironmentIds, useEnvironment } from "../../state/environments"; +import { useLinkedEnvironments } from "../../state/peerLinks"; + +/** + * How this client opens an agent's thread on `environmentId` from a run on `viaEnvironmentId`: + * its own connection, else through the run's server when that server links to it. + */ +export function useWorkflowThreadOpen( + viaEnvironmentId: EnvironmentId, + environmentId: EnvironmentId, +): { readonly open: LinkedThreadOpen; readonly label: string | null } { + const own = useEnvironment(environmentId); + const connected = useConnectedEnvironmentIds().includes(environmentId); + const linkedLabel = useLinkedEnvironments( + connected || environmentId === viaEnvironmentId ? null : viaEnvironmentId, + ).get(environmentId); + const open = resolveLinkedThreadOpen({ + environmentId, + viaEnvironmentId, + own: own === null ? undefined : { connected }, + linked: linkedLabel !== undefined, + }); + return { open, label: open === "linked" ? (linkedLabel ?? null) : (own?.label ?? null) }; +}