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

handoff.ts

78 lines3.1 KB

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