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

handoff.ts

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