Repository navigation
feat(web,mobile): continue a thread on another machine from its menu #44775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
dd1d218
8e84c4e
6725973
4c62ff0
b65e44d
9ff9fb2
647fcdd
63ece3d
77b8430
1b0e5f5
b2a7cd6
7e8635d
daaba32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,293 @@ | ||||||||||||
| import { | ||||||||||||
| HANDOFF_LINK_DEFAULT_ACCESS, | ||||||||||||
| handoffLinkCandidates, | ||||||||||||
| handoffRequestDisplay, | ||||||||||||
| type HandoffRequestItem, | ||||||||||||
| handoffRequestTarget, | ||||||||||||
| } from "@t3tools/client-runtime/handoff-request"; | ||||||||||||
| import { | ||||||||||||
| linkRequestFailureMessage, | ||||||||||||
| type LinkableMachine, | ||||||||||||
| } from "@t3tools/client-runtime/link-request"; | ||||||||||||
| import { | ||||||||||||
| isAtomCommandInterrupted, | ||||||||||||
| squashAtomCommandFailure, | ||||||||||||
| } from "@t3tools/client-runtime/state/runtime"; | ||||||||||||
| import { | ||||||||||||
| AuthAccessWriteScope, | ||||||||||||
| type AuthMcpClientAccess, | ||||||||||||
| type EnvironmentId, | ||||||||||||
| type OrchestrationV2ProjectedTurnItem, | ||||||||||||
| } from "@t3tools/contracts"; | ||||||||||||
| import * as Result from "effect/Result"; | ||||||||||||
| import { T, msg, useGT, useMessages } from "gt-react-native"; | ||||||||||||
| import { useRef, useState } from "react"; | ||||||||||||
| import { View, type ColorValue } from "react-native"; | ||||||||||||
|
|
||||||||||||
| import { SymbolView, type AppSymbolName } from "../../components/ui/AppSymbol"; | ||||||||||||
| import { AppText as Text } from "../../components/ui/AppText"; | ||||||||||||
| import { Button } from "../../components/ui/Button"; | ||||||||||||
| import { ListChoiceRow } from "../../components/ui/ListChoiceRow"; | ||||||||||||
| import { scopedThreadKey } from "../../lib/scopedEntities"; | ||||||||||||
| import { SettingsSection } from "../settings/components/SettingsSection"; | ||||||||||||
| import { peerLinkEnvironment, useLinkableMachines } from "../../state/peerLinks"; | ||||||||||||
| import { mintPairingCodeOn } from "../../state/peerPairingCode"; | ||||||||||||
| import { useEnvironmentQuery } from "../../state/query"; | ||||||||||||
| import { useEnvironmentScope } from "../../state/session"; | ||||||||||||
| import { threadHandoffEnvironment, useCanMoveThreads } from "../../state/threadHandoff"; | ||||||||||||
| import { useAtomCommand } from "../../state/use-atom-command"; | ||||||||||||
| import { threadHasQueuedMessages, useQueuedThreadKeys } from "../../state/use-thread-outbox"; | ||||||||||||
| import { LINK_ACCESS_CHOICES } from "./LinkRequestCard"; | ||||||||||||
|
|
||||||||||||
| const TITLE = msg("Move this thread"); | ||||||||||||
| const LOCK_SYMBOL: AppSymbolName = { ios: "lock", android: "lock" }; | ||||||||||||
|
|
||||||||||||
| /** An agent's ask to move its thread: the user picks the machine and moves it, or declines. */ | ||||||||||||
| export function HandoffRequestCard(props: { | ||||||||||||
| readonly environmentId: EnvironmentId; | ||||||||||||
| readonly projectedItem: OrchestrationV2ProjectedTurnItem; | ||||||||||||
| readonly iconColor: ColorValue; | ||||||||||||
| }) { | ||||||||||||
| const m = useMessages(); | ||||||||||||
| const { item, visibility } = props.projectedItem; | ||||||||||||
| if (item.type !== "handoff_request") return null; | ||||||||||||
| const display = handoffRequestDisplay(item, visibility, m); | ||||||||||||
| if (display.kind === "pending") { | ||||||||||||
| return <PendingHandoffRequest environmentId={props.environmentId} item={item} />; | ||||||||||||
| } | ||||||||||||
| const icon: AppSymbolName = | ||||||||||||
| display.kind === "pending-elsewhere" ? LOCK_SYMBOL : display.accepted ? "checkmark" : "minus"; | ||||||||||||
| return ( | ||||||||||||
| <View className="mb-3 min-h-9 flex-row items-center gap-2 px-1"> | ||||||||||||
| <SymbolView name={icon} size={13} tintColor={props.iconColor} type="monochrome" /> | ||||||||||||
| <Text className="flex-1 font-sans text-sm text-foreground-muted" numberOfLines={2}> | ||||||||||||
| {m(TITLE)} · {display.label} | ||||||||||||
| </Text> | ||||||||||||
| </View> | ||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| function PendingHandoffRequest(props: { | ||||||||||||
| readonly environmentId: EnvironmentId; | ||||||||||||
| readonly item: HandoffRequestItem; | ||||||||||||
| }) { | ||||||||||||
| const gt = useGT(); | ||||||||||||
| const m = useMessages(); | ||||||||||||
| const { item } = props; | ||||||||||||
| const canMove = useCanMoveThreads(props.environmentId); | ||||||||||||
| const targets = useEnvironmentQuery( | ||||||||||||
| threadHandoffEnvironment.options({ | ||||||||||||
| environmentId: props.environmentId, | ||||||||||||
| input: { threadId: item.threadId }, | ||||||||||||
| }), | ||||||||||||
| ); | ||||||||||||
| const start = useAtomCommand(threadHandoffEnvironment.start, { reportFailure: false }); | ||||||||||||
| const cancel = useAtomCommand(threadHandoffEnvironment.cancel, { reportFailure: false }); | ||||||||||||
| const link = useAtomCommand(peerLinkEnvironment.link, { | ||||||||||||
| // The failure cause holds the request, and with it the code; keep it out of the logs. | ||||||||||||
| reportFailure: false, | ||||||||||||
| reportDefect: false, | ||||||||||||
| }); | ||||||||||||
| const [picked, setPicked] = useState<EnvironmentId | null>(null); | ||||||||||||
| const [access, setAccess] = useState<AuthMcpClientAccess>(HANDOFF_LINK_DEFAULT_ACCESS); | ||||||||||||
| const [busy, setBusy] = useState<"move" | "decline" | null>(null); | ||||||||||||
| const [error, setError] = useState<string | null>(null); | ||||||||||||
| // Two taps can land before a re-render; this guard is synchronous. | ||||||||||||
| const inFlight = useRef(false); | ||||||||||||
| // A message in the outbox exists only on this device, and a moved thread refuses it. | ||||||||||||
| const outboxWaiting = useQueuedThreadKeys().has( | ||||||||||||
| scopedThreadKey(props.environmentId, item.threadId), | ||||||||||||
| ); | ||||||||||||
| const options = targets.data?.options ?? []; | ||||||||||||
| // Machines this client is connected to that the thread's environment has no link to yet. | ||||||||||||
| const linkable = handoffLinkCandidates(useLinkableMachines(props.environmentId, true)).filter( | ||||||||||||
| (machine) => !options.some((option) => option.environmentId === machine.environmentId), | ||||||||||||
| ); | ||||||||||||
| const pickedLinkable = linkable.find((machine) => machine.environmentId === picked); | ||||||||||||
| // This device creates the link's code there, which its session must be allowed to. | ||||||||||||
| const mayLink = useEnvironmentScope(pickedLinkable?.environmentId ?? null, AuthAccessWriteScope); | ||||||||||||
| const toLink = mayLink ? pickedLinkable : undefined; | ||||||||||||
|
Comment on lines
+108
to
+109
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gate "Link and move" on access:write in this environment too. The card checks Require both permissions before the card offers the link path. Suggested fix
Committable suggestion
|
||||||||||||
| const chosen = | ||||||||||||
| pickedLinkable === undefined | ||||||||||||
| ? handoffRequestTarget(options, picked ?? item.environmentId) | ||||||||||||
| : undefined; | ||||||||||||
| const move = (environmentId: EnvironmentId) => | ||||||||||||
| start({ | ||||||||||||
| environmentId: props.environmentId, | ||||||||||||
| input: { threadId: item.threadId, environmentId, turnItemId: item.id }, | ||||||||||||
| }); | ||||||||||||
|
|
||||||||||||
| const answer = async ( | ||||||||||||
| key: "move" | "decline", | ||||||||||||
| send: () => ReturnType<typeof start> | ReturnType<typeof cancel>, | ||||||||||||
| ) => { | ||||||||||||
| if (inFlight.current) return; | ||||||||||||
| inFlight.current = true; | ||||||||||||
| setBusy(key); | ||||||||||||
| setError(null); | ||||||||||||
| const result = await send().finally(() => { | ||||||||||||
| inFlight.current = false; | ||||||||||||
| setBusy(null); | ||||||||||||
| }); | ||||||||||||
| if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return; | ||||||||||||
| const failure = squashAtomCommandFailure(result); | ||||||||||||
| setError(failure instanceof Error ? failure.message : gt("The thread could not be moved.")); | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| /** Links the thread's environment to `machine`; resolves to what went wrong, or null. */ | ||||||||||||
| const linkTo = async (machine: LinkableMachine): Promise<unknown> => { | ||||||||||||
| const [url, ...alternateUrls] = machine.urls; | ||||||||||||
| if (url === undefined) return "interrupted"; | ||||||||||||
| const code = await mintPairingCodeOn(machine.environmentId, access); | ||||||||||||
| if (Result.isFailure(code)) return code.failure; | ||||||||||||
| const linked = await link({ | ||||||||||||
| environmentId: props.environmentId, | ||||||||||||
| input: { | ||||||||||||
| url, | ||||||||||||
| ...(alternateUrls.length === 0 ? {} : { alternateUrls }), | ||||||||||||
| pairingCode: code.success, | ||||||||||||
| access, | ||||||||||||
| expectedEnvironmentId: machine.environmentId, | ||||||||||||
| expectedLabel: machine.label, | ||||||||||||
| }, | ||||||||||||
| }); | ||||||||||||
| if (linked._tag !== "Failure") return null; | ||||||||||||
| return isAtomCommandInterrupted(linked) ? "interrupted" : squashAtomCommandFailure(linked); | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| /** Links to a machine this client is connected to, then moves the thread there. */ | ||||||||||||
| const linkAndMove = async (machine: LinkableMachine) => { | ||||||||||||
| if (inFlight.current) return; | ||||||||||||
| inFlight.current = true; | ||||||||||||
| setBusy("move"); | ||||||||||||
| setError(null); | ||||||||||||
| const failure = await linkTo(machine); | ||||||||||||
| inFlight.current = false; | ||||||||||||
| setBusy(null); | ||||||||||||
| if (failure !== null) { | ||||||||||||
| if (failure !== "interrupted") setError(linkRequestFailureMessage(failure, m)); | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
| // Linking took a while; a message queued here meanwhile would be stranded by the move. | ||||||||||||
| if (threadHasQueuedMessages(scopedThreadKey(props.environmentId, item.threadId))) return; | ||||||||||||
| targets.refresh(); | ||||||||||||
| await answer("move", () => move(machine.environmentId)); | ||||||||||||
|
macroscopeapp[bot] marked this conversation as resolved.
Resolved
|
||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| return ( | ||||||||||||
| <View className="mb-3 gap-3 rounded-[20px] border border-border bg-card-alt p-4"> | ||||||||||||
| <View className="gap-1"> | ||||||||||||
| <Text className="font-t3-bold text-base text-foreground"> | ||||||||||||
| <T>Move this thread to another machine?</T> | ||||||||||||
| </Text> | ||||||||||||
| <Text className="font-sans text-xs text-foreground-muted"> | ||||||||||||
| {outboxWaiting | ||||||||||||
| ? gt("A message is still waiting to send from this device. Send or discard it first.") | ||||||||||||
| : targets.data === null | ||||||||||||
| ? (targets.error ?? gt("Asking linked machines…")) | ||||||||||||
| : options.length === 0 && linkable.length === 0 | ||||||||||||
| ? gt("This machine has no linked machines.") | ||||||||||||
| : gt( | ||||||||||||
| "It moves with its conversation and its code, uncommitted changes included. This copy becomes read-only.", | ||||||||||||
| )} | ||||||||||||
| </Text> | ||||||||||||
| </View> | ||||||||||||
| {error !== null ? ( | ||||||||||||
| <Text accessibilityRole="alert" className="font-sans text-sm text-danger-foreground"> | ||||||||||||
| {error} | ||||||||||||
| </Text> | ||||||||||||
| ) : !canMove ? ( | ||||||||||||
| <Text className="font-sans text-xs text-foreground-muted"> | ||||||||||||
| <T>Only someone who can run agents on this machine can move it or decline.</T> | ||||||||||||
| </Text> | ||||||||||||
| ) : null} | ||||||||||||
| {options.length > 0 || linkable.length > 0 ? ( | ||||||||||||
| <SettingsSection title={gt("Machine")}> | ||||||||||||
| {options.map((option, index) => ( | ||||||||||||
| <ListChoiceRow | ||||||||||||
| key={option.environmentId} | ||||||||||||
| label={option.label} | ||||||||||||
| {...(option.reason === null ? {} : { description: option.reason })} | ||||||||||||
| selected={chosen?.environmentId === option.environmentId} | ||||||||||||
| separated={index > 0} | ||||||||||||
| disabled={busy !== null || option.reason !== null || !canMove} | ||||||||||||
| onPress={() => setPicked(option.environmentId)} | ||||||||||||
| /> | ||||||||||||
| ))} | ||||||||||||
| {linkable.map((machine, index) => ( | ||||||||||||
| <ListChoiceRow | ||||||||||||
| key={machine.environmentId} | ||||||||||||
| label={machine.label} | ||||||||||||
| description={gt("Not linked yet")} | ||||||||||||
| selected={pickedLinkable?.environmentId === machine.environmentId} | ||||||||||||
| separated={options.length + index > 0} | ||||||||||||
| disabled={busy !== null || !canMove} | ||||||||||||
| onPress={() => setPicked(machine.environmentId)} | ||||||||||||
| /> | ||||||||||||
| ))} | ||||||||||||
| </SettingsSection> | ||||||||||||
| ) : null} | ||||||||||||
| {pickedLinkable !== undefined && !mayLink ? ( | ||||||||||||
| <Text className="font-sans text-xs text-foreground-muted"> | ||||||||||||
| {gt( | ||||||||||||
| "This device may not create a link on {machine}. Link it from Settings on a device that can.", | ||||||||||||
| { machine: pickedLinkable.label }, | ||||||||||||
| )} | ||||||||||||
| </Text> | ||||||||||||
| ) : null} | ||||||||||||
| {toLink !== undefined ? ( | ||||||||||||
| <> | ||||||||||||
| <Text className="font-sans text-xs text-foreground-muted"> | ||||||||||||
| {gt( | ||||||||||||
| "This machine is not linked to {machine} yet. Moving the thread links them first, with a code this device creates there. Choose what agents here may do there.", | ||||||||||||
| { machine: toLink.label }, | ||||||||||||
| )} | ||||||||||||
| </Text> | ||||||||||||
| <SettingsSection title={gt("What agents here may do there")}> | ||||||||||||
| {LINK_ACCESS_CHOICES.map((choice, index) => ( | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exclude read-only from the move card's link choices. The "What agents here may do there" list in the move card includes "Read only". A move cannot succeed with that level. The move calls Offer only levels that can start threads in this card. Keep the full list for ordinary linking. The default access must also be a level that can start threads. Suggested fix
Committable suggestion
|
||||||||||||
| <ListChoiceRow | ||||||||||||
| key={choice.access} | ||||||||||||
| label={m(choice.label)} | ||||||||||||
| description={m(choice.description)} | ||||||||||||
| selected={choice.access === access} | ||||||||||||
| separated={index > 0} | ||||||||||||
| disabled={busy !== null} | ||||||||||||
| onPress={() => setAccess(choice.access)} | ||||||||||||
| /> | ||||||||||||
| ))} | ||||||||||||
| </SettingsSection> | ||||||||||||
| </> | ||||||||||||
| ) : null} | ||||||||||||
| <Button | ||||||||||||
| label={toLink === undefined ? gt("Move") : gt("Link and move")} | ||||||||||||
| tone="primary" | ||||||||||||
| fullWidth | ||||||||||||
| loading={busy === "move"} | ||||||||||||
| disabled={ | ||||||||||||
| busy !== null || | ||||||||||||
| outboxWaiting || | ||||||||||||
| !canMove || | ||||||||||||
| (chosen === undefined && toLink === undefined) | ||||||||||||
| } | ||||||||||||
| onPress={() => { | ||||||||||||
| if (toLink !== undefined) void linkAndMove(toLink); | ||||||||||||
| else if (chosen !== undefined) void answer("move", () => move(chosen.environmentId)); | ||||||||||||
| }} | ||||||||||||
| /> | ||||||||||||
| <Button | ||||||||||||
| label={gt("Not now")} | ||||||||||||
| tone="text" | ||||||||||||
| loading={busy === "decline"} | ||||||||||||
| disabled={busy !== null || !canMove} | ||||||||||||
| onPress={() => | ||||||||||||
| void answer("decline", () => | ||||||||||||
| cancel({ | ||||||||||||
| environmentId: props.environmentId, | ||||||||||||
| input: { threadId: item.threadId, turnItemId: item.id }, | ||||||||||||
| }), | ||||||||||||
| ) | ||||||||||||
| } | ||||||||||||
| /> | ||||||||||||
| </View> | ||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium
threads/HandoffRequestCard.tsx:108A session with
orchestration:operateon the source but withoutaccess:writethere can select an unlinked machine and press “Link and move”; the app mints a pairing code, thenpeerLinkEnvironment.linkfails becausepeerLinksLinkrequiresaccess:writeon the source. Gate link availability onpeerLinkEnvironment.link.permissionAtom(props.environmentId)so this unusable action is not offered.🚀 Reply "fix it for me" or copy this AI Prompt for your agent: