Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions apps/mobile/src/features/threads/thread-handoff-row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,14 @@ export function ThreadHandoffRow(props: {
const endpoints = useAtomValue(endpointsAtom);
if (item.type !== "handoff" || endpoints === null) return null;
const color = item.status === "failed" ? "#e11d48" : props.iconColor;
const label = endpoints.isRecoveredContext ? gt("Recovered context") : gt("Context handoff");
return (
<ThreadHandoffDivider
environmentId={props.environmentId}
endpoints={endpoints}
iconColor={color}
surfaceColor={props.surfaceColor}
label={gt("Context handoff")}
label={label}
failed={item.status === "failed"}
/>
);
Expand All @@ -116,6 +117,7 @@ export function ThreadHandoffDivider(props: {
}) {
const config = useAtomValue(serverEnvironment.configValueAtom(props.environmentId));
const { endpoints } = props;
const showFromEndpoints = !endpoints.isRecoveredContext && endpoints.from.length > 0;
const endpointProps = {
providers: (config?.providers ?? []).map((provider) => {
const rawConfig = config?.settings.providerInstances[provider.instanceId]?.config;
Expand All @@ -141,20 +143,22 @@ export function ThreadHandoffDivider(props: {
failed={props.failed}
>
<View className="flex-row flex-wrap items-center justify-center gap-1.5">
{endpoints.from.map((endpoint, index) => (
<View
key={`${endpoint.instanceId}:${endpoint.model ?? ""}`}
className="max-w-full flex-row items-center"
>
<HandoffEndpoint {...endpoint} {...endpointProps} />
{index < endpoints.from.length - 1 ? (
<Text accessible={false} className="text-xs text-foreground-muted">
,
</Text>
) : null}
</View>
))}
{endpoints.from.length > 0 ? (
{showFromEndpoints
? endpoints.from.map((endpoint, index) => (
<View
key={`${endpoint.instanceId}:${endpoint.model ?? ""}`}
className="max-w-full flex-row items-center"
>
<HandoffEndpoint {...endpoint} {...endpointProps} />
{index < endpoints.from.length - 1 ? (
<Text accessible={false} className="text-xs text-foreground-muted">
,
</Text>
) : null}
</View>
))
: null}
{showFromEndpoints ? (
<SymbolView name="arrow.right" size={12} tintColor={props.iconColor} />
) : null}
<HandoffEndpoint {...endpoints.to} {...endpointProps} />
Expand Down
47 changes: 28 additions & 19 deletions apps/web/src/components/chat/V2LifecycleRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -669,36 +669,45 @@ export function HandoffRow(props: {
readonly failed?: boolean;
}) {
const gt = useGT();
const { from: fromEndpoints, to } = props.endpoints;
const { from: fromEndpoints, to, isRecoveredContext } = props.endpoints;
const settings = useAtomValue(serverEnvironment.settingsValueAtom(props.environmentId));
const derived = deriveProviderInstanceEntries(props.providers);
const entries = settings ? applyProviderInstanceSettings(derived, settings, null) : derived;
const showFromEndpoints = !isRecoveredContext && fromEndpoints.length > 0;
return (
<TimelineSystemDivider
label={props.pending ? gt("Context handoff pending") : gt("Context handoff")}
label={
props.pending
? gt("Context handoff pending")
: isRecoveredContext
? gt("Recovered context")
: gt("Context handoff")
}
icon={ArrowRightLeftIcon}
showDetailSeparator={false}
tone={props.failed ? "danger" : "neutral"}
detail={
<span className="inline-flex min-w-0 flex-wrap items-center justify-center gap-1.5">
{fromEndpoints.map((endpoint, index) => (
<span
key={`${endpoint.instanceId}:${endpoint.model ?? ""}`}
className="inline-flex min-w-0 items-center"
>
<HandoffEndpoint
entries={entries}
instanceId={endpoint.instanceId}
model={endpoint.model}
/>
{index < fromEndpoints.length - 1 ? (
<span aria-hidden="true" className="ml-0.5">
,
{showFromEndpoints
? fromEndpoints.map((endpoint, index) => (
<span
key={`${endpoint.instanceId}:${endpoint.model ?? ""}`}
className="inline-flex min-w-0 items-center"
>
<HandoffEndpoint
entries={entries}
instanceId={endpoint.instanceId}
model={endpoint.model}
/>
{index < fromEndpoints.length - 1 ? (
<span aria-hidden="true" className="ml-0.5">
,
</span>
) : null}
</span>
) : null}
</span>
))}
{fromEndpoints.length > 0 ? (
))
: null}
{showFromEndpoints ? (
<ArrowRightIcon aria-hidden="true" className="size-3 shrink-0" />
) : null}
<HandoffEndpoint entries={entries} instanceId={to.instanceId} model={to.model} />
Expand Down
37 changes: 37 additions & 0 deletions packages/client-runtime/src/handoff.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ContextHandoffId,
ProviderInstanceId,
ProviderThreadId,
RunId,
type OrchestrationV2ThreadProjection,
type ServerProvider,
Expand Down Expand Up @@ -73,6 +74,42 @@ describe("handoff endpoints shared by web and mobile", () => {
resolveHandoffEndpoints(item, [run("target", 2, from, "wrong-model")]).to.model,
).toBeUndefined();
});

it("recognizes recovered context from same provider thread and omits self-handoff arrow", () => {
const threadId = ProviderThreadId.make("session-1");
const sameInstance = ProviderInstanceId.make("antigravity");
const recoveryItem = {
runId: RunId.make("target"),
title: "Recovered context",
fromProviderThreadIds: [threadId],
toProviderThreadId: threadId,
fromProviderInstanceIds: [sameInstance],
toProviderInstanceId: sameInstance,
fromModelSelections: [{ instanceId: sameInstance, model: "4 Neon" }],
toModel: "4 Neon",
};
expect(resolveHandoffEndpoints(recoveryItem, [])).toEqual({
from: [],
to: { instanceId: sameInstance, model: "4 Neon" },
isRecoveredContext: true,
});
});

it("treats same origin and destination model and provider as recovered context", () => {
const sameInstance = ProviderInstanceId.make("devReplay");
const selfHandoffItem = {
runId: RunId.make("target"),
fromProviderInstanceIds: [sameInstance],
toProviderInstanceId: sameInstance,
fromModelSelections: [{ instanceId: sameInstance, model: "Multi turn" }],
toModel: "Multi turn",
};
expect(resolveHandoffEndpoints(selfHandoffItem, [])).toEqual({
from: [],
to: { instanceId: sameInstance, model: "Multi turn" },
isRecoveredContext: true,
});
});
});

const projection = {
Expand Down
39 changes: 35 additions & 4 deletions packages/client-runtime/src/handoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
OrchestrationV2Run,
OrchestrationV2TurnItem,
ProviderInstanceId,
ProviderThreadId,
ServerProvider,
ServerProviderModel,
} from "@t3tools/contracts";
Expand All @@ -21,7 +22,17 @@ export type HandoffTimelineRun = Pick<
"id" | "ordinal" | "providerInstanceId" | "modelSelection"
>;

export type HandoffEndpoints = ReturnType<typeof resolveHandoffEndpoints>;
export type HandoffEndpoints = {
readonly from: ReadonlyArray<{
readonly instanceId: ProviderInstanceId;
readonly model?: string | undefined;
}>;
readonly to: {
readonly instanceId: ProviderInstanceId;
readonly model?: string | undefined;
};
readonly isRecoveredContext?: boolean | undefined;
};

export function resolvePendingHandoff(
projection: Pick<ThreadStructure, "runs" | "visibleTurnItems"> | null | undefined,
Expand Down Expand Up @@ -53,9 +64,13 @@ export function resolveHandoffEndpoints(
item: Pick<
Extract<OrchestrationV2TurnItem, { type: "handoff" }>,
"runId" | "fromModelSelections" | "fromProviderInstanceIds" | "toModel" | "toProviderInstanceId"
>,
> & {
readonly title?: string | null | undefined;
readonly fromProviderThreadIds?: ReadonlyArray<ProviderThreadId> | undefined;
readonly toProviderThreadId?: ProviderThreadId | undefined;
},
runs: ReadonlyArray<HandoffTimelineRun>,
) {
): HandoffEndpoints {
const handoffRun = item.runId === null ? undefined : runs.find((run) => run.id === item.runId);
const toModel =
item.toModel ??
Expand All @@ -72,7 +87,23 @@ export function resolveHandoffEndpoints(
instanceId,
model: latestRunModelBefore(runs, instanceId, handoffRun?.ordinal),
}));
return { from: fromEndpoints, to: { instanceId: item.toProviderInstanceId, model: toModel } };
const isRecoveredContext = Boolean(
item.title === "Recovered context" ||
(item.fromProviderThreadIds !== undefined &&
item.toProviderThreadId !== undefined &&
item.fromProviderThreadIds.length === 1 &&
item.fromProviderThreadIds[0] === item.toProviderThreadId) ||
(fromEndpoints.length === 1 &&
fromEndpoints[0]!.instanceId === item.toProviderInstanceId &&
(fromEndpoints[0]!.model === toModel ||
fromEndpoints[0]!.model === undefined ||
toModel === undefined)),
);
return {
from: isRecoveredContext ? [] : fromEndpoints,
to: { instanceId: item.toProviderInstanceId, model: toModel },
...(isRecoveredContext ? { isRecoveredContext: true } : {}),
};
}

function latestRunModelBefore(
Expand Down
Loading