Open History to see the last commit
1import type {
2 ModelSelection,
3 OrchestrationV2Run,
4 OrchestrationV2TurnItem,
5 ProviderInstanceId,
6 ServerProvider,
7} from "@t3tools/contracts";
8
9import type { ThreadStructure } from "./state/models.ts";
10
11export type HandoffTimelineRun = Pick<
12 OrchestrationV2Run,
13 "id" | "ordinal" | "providerInstanceId" | "modelSelection"
14>;
15
16export type HandoffEndpoints = ReturnType<typeof resolveHandoffEndpoints>;
17
18export function resolvePendingHandoff(
19 projection: ThreadStructure | null | undefined,
20 selection: ModelSelection | null | undefined,
21 providers: ReadonlyArray<ServerProvider>,
22): HandoffEndpoints | null {
23 // A thread with an empty timeline has nothing to hand off. Any loaded row
24 // counts: paging can leave a window of only tool rows.
25 if (!projection?.visibleTurnItems.length) return null;
26 // The handoff row is committed before the provider starts. Use its destination
27 // during startup so the footer does not duplicate the committed handoff.
28 const from = projection.runs.findLast(
29 (run) =>
30 run.status !== "rolled_back" &&
31 (run.startedAt !== null || (run.status === "starting" && run.contextHandoffId !== null)),
32 )?.modelSelection;
33 if (from === undefined || selection == null) return null;
34 const source = providers.find((provider) => provider.instanceId === from.instanceId);
35 const target = providers.find((provider) => provider.instanceId === selection.instanceId);
36 const needsHandoff =
37 from.instanceId !== selection.instanceId &&
38 (source?.continuation?.groupKey === undefined ||
39 source.driver !== target?.driver ||
40 source.continuation.groupKey !== target.continuation?.groupKey);
41 return needsHandoff ? { from: [from], to: selection } : null;
42}
43
44export function resolveHandoffEndpoints(
45 item: Pick<
46 Extract<OrchestrationV2TurnItem, { type: "handoff" }>,
47 "runId" | "fromModelSelections" | "fromProviderInstanceIds" | "toModel" | "toProviderInstanceId"
48 >,
49 runs: ReadonlyArray<HandoffTimelineRun>,
50) {
51 const handoffRun = item.runId === null ? undefined : runs.find((run) => run.id === item.runId);
52 const toModel =
53 item.toModel ??
54 (handoffRun !== undefined && handoffRun.providerInstanceId === item.toProviderInstanceId
55 ? handoffRun.modelSelection.model
56 : undefined);
57 const fromEndpoints: ReadonlyArray<{
58 readonly instanceId: ProviderInstanceId;
59 readonly model?: string | undefined;
60 }> =
61 item.fromModelSelections !== undefined && item.fromModelSelections.length > 0
62 ? item.fromModelSelections
63 : item.fromProviderInstanceIds.map((instanceId) => ({
64 instanceId,
65 model: latestRunModelBefore(runs, instanceId, handoffRun?.ordinal),
66 }));
67 return { from: fromEndpoints, to: { instanceId: item.toProviderInstanceId, model: toModel } };
68}
69
70function latestRunModelBefore(
71 runs: ReadonlyArray<HandoffTimelineRun>,
72 instanceId: ProviderInstanceId,
73 beforeOrdinal: number | undefined,
74): string | undefined {
75 let latest: HandoffTimelineRun | undefined;
76 for (const run of runs) {
77 if (run.providerInstanceId !== instanceId) continue;
78 if (beforeOrdinal !== undefined && run.ordinal >= beforeOrdinal) continue;
79 if (latest === undefined || run.ordinal > latest.ordinal) latest = run;
80 }
81 return latest?.modelSelection.model;
82}