Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

handoff.ts

83 lines3.3 KB

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 ? from.model !== selection.model && target?.requiresNewThreadForModelChange === true
39 : source?.continuation?.groupKey === undefined ||
40 source.driver !== target?.driver ||
41 source.continuation.groupKey !== target.continuation?.groupKey;
42 return needsHandoff ? { from: [from], to: selection } : null;
44​
45export function resolveHandoffEndpoints(
46 item: Pick<
47 Extract<OrchestrationV2TurnItem, { type: "handoff" }>,
48 "runId" | "fromModelSelections" | "fromProviderInstanceIds" | "toModel" | "toProviderInstanceId"
49 >,
50 runs: ReadonlyArray<HandoffTimelineRun>,
51) {
52 const handoffRun = item.runId === null ? undefined : runs.find((run) => run.id === item.runId);
53 const toModel =
54 item.toModel ??
55 (handoffRun !== undefined && handoffRun.providerInstanceId === item.toProviderInstanceId
56 ? handoffRun.modelSelection.model
57 : undefined);
58 const fromEndpoints: ReadonlyArray<{
59 readonly instanceId: ProviderInstanceId;
60 readonly model?: string | undefined;
61 }> =
62 item.fromModelSelections !== undefined && item.fromModelSelections.length > 0
63 ? item.fromModelSelections
64 : item.fromProviderInstanceIds.map((instanceId) => ({
65 instanceId,
66 model: latestRunModelBefore(runs, instanceId, handoffRun?.ordinal),
67 }));
68 return { from: fromEndpoints, to: { instanceId: item.toProviderInstanceId, model: toModel } };
70​
71function latestRunModelBefore(
72 runs: ReadonlyArray<HandoffTimelineRun>,
73 instanceId: ProviderInstanceId,
74 beforeOrdinal: number | undefined,
75): string | undefined {
76 let latest: HandoffTimelineRun | undefined;
77 for (const run of runs) {
78 if (run.providerInstanceId !== instanceId) continue;
79 if (beforeOrdinal !== undefined && run.ordinal >= beforeOrdinal) continue;
80 if (latest === undefined || run.ordinal > latest.ordinal) latest = run;
81 }
82 return latest?.modelSelection.model;