@@ -4,25 +4,27 @@ import type {
44 OrchestrationV2TurnItem ,
55} from "@t3tools/contracts" ;
66import { modelSelectionsEqual } from "@t3tools/shared/model" ;
7+ import { runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError" ;
78
89/**
910 * The model or effort change a new run brings, measured from the latest run whose message is in
10- * the timeline, so consecutive dividers always chain. Callers skip starts with a context handoff;
11- * the handoff item already shows those.
11+ * the timeline, so consecutive dividers always chain. `baseline` stands in when there is none,
12+ * such as a fork's source run. Callers skip starts with a context handoff; the handoff item already
13+ * shows those.
1214 */
1315export function modelSwitchSince (
1416 projection : Pick < OrchestrationV2ThreadProjection , "runs" | "turnItems" > ,
1517 to : ModelSelection ,
18+ baseline ?: ModelSelection ,
1619) : Pick < Extract < OrchestrationV2TurnItem , { type : "user_message" } > , "modelSwitch" > {
1720 const shown = new Set (
1821 projection . turnItems . flatMap ( ( item ) => ( item . type === "user_message" ? [ item . messageId ] : [ ] ) ) ,
1922 ) ;
2023 let previous : OrchestrationV2ThreadProjection [ "runs" ] [ number ] | undefined ;
2124 for ( const run of projection . runs ) {
2225 if ( run . status === "rolled_back" || ! shown . has ( run . userMessageId ) ) continue ;
23- if ( previous === undefined || run . ordinal > previous . ordinal ) previous = run ;
26+ if ( previous === undefined || runRanAfter ( run , previous ) ) previous = run ;
2427 }
25- return previous === undefined || modelSelectionsEqual ( previous . modelSelection , to )
26- ? { }
27- : { modelSwitch : { from : previous . modelSelection , to } } ;
28+ const from = previous ?. modelSelection ?? baseline ;
29+ return from === undefined || modelSelectionsEqual ( from , to ) ? { } : { modelSwitch : { from, to } } ;
2830}
0 commit comments