Open History to see the last commit
1import { mFallback, msg } from "gt-i18n";
2import type { M } from "gt-i18n";
3import type {
4 AuthMcpClientAccess,
5 EnvironmentId,
6 OrchestrationV2TurnItem,
7} from "@t3tools/contracts";
8
9import type { LinkableMachine } from "./linkRequest.ts";
10
11export type HandoffRequestItem = Extract<
12 OrchestrationV2TurnItem,
13 { readonly type: "handoff_request" }
14>;
15
16export type HandoffRequestDisplay =
17 | { readonly kind: "pending" }
18 /** Inherited by a fork: only the thread that asked can move. */
19 | { readonly kind: "pending-elsewhere"; readonly label: string }
20 | { readonly kind: "answered"; readonly accepted: boolean; readonly label: string };
21
22export function handoffRequestDisplay(
23 item: Pick<HandoffRequestItem, "handoffStatus" | "acceptedLabel">,
24 visibility: "local" | "inherited" | "synthetic",
25 resolve: M = mFallback,
26): HandoffRequestDisplay {
27 switch (item.handoffStatus) {
28 case "pending":
29 return visibility === "local"
30 ? { kind: "pending" }
31 : { kind: "pending-elsewhere", label: resolve(msg("Waiting in the original thread")) };
32 case "accepted":
33 return {
34 kind: "answered",
35 accepted: true,
36 label:
37 item.acceptedLabel === undefined
38 ? resolve(msg("Accepted"))
39 : resolve(msg`Accepted for ${{ machine: item.acceptedLabel }}`),
40 };
41 case "declined":
42 return { kind: "answered", accepted: false, label: resolve(msg("Declined")) };
43 case "cancelled":
44 return { kind: "answered", accepted: false, label: resolve(msg("Closed")) };
45 }
46}
47
48/** What a link made from the move card lets agents here do there, until the user picks. */
49export const HANDOFF_LINK_DEFAULT_ACCESS: AuthMcpClientAccess = "approval-required";
50
51/**
52 * Machines the thread's environment has no link to yet, which this client is connected to and
53 * knows an address for: it can link them and then move the thread there. Whether its session
54 * there may create the link is the caller's check.
55 */
56export const handoffLinkCandidates = (
57 machines: ReadonlyArray<LinkableMachine>,
58): ReadonlyArray<LinkableMachine> =>
59 machines.filter((machine) => !machine.linked && machine.connected && machine.urls.length > 0);
60
61/**
62 * The machine the card starts on: the user's pick, else the agent's suggestion, while it can
63 * take the thread. With neither the choice starts blank, since the card may also offer machines
64 * to link.
65 */
66export const handoffRequestTarget = <
67 Option extends {
68 readonly environmentId: EnvironmentId;
69 readonly reason: string | null;
70 readonly projectSelectionRequired?: boolean;
71 },
72>(
73 options: ReadonlyArray<Option>,
74 wanted: EnvironmentId | null | undefined,
75): Option | undefined =>
76 options.find(
77 (option) =>
78 option.environmentId === wanted &&
79 (option.reason === null || option.projectSelectionRequired),
80 );