Open History to see the last commit
1import type {
2 ModelSelection,
3 OrchestrationV2ProviderCapabilities,
4 ProviderDriverKind,
5 ProviderInteractionMode,
6 RuntimeMode,
7} from "@t3tools/contracts";
8import { modelSelectionsEqual } from "@t3tools/shared/model";
9
10import type { ProviderContinuationIdentity } from "../provider/ProviderDriver.ts";
11import type { ProviderSelectionTransitionPlan } from "./ProviderSelectionTransition.ts";
12
13export type ProviderSessionTransition =
14 | { readonly type: "reuse" }
15 | { readonly type: "switch_model_in_session" }
16 | { readonly type: "restart_and_resume" }
17 | { readonly type: "create_with_handoff" }
18 | { readonly type: "reject"; readonly reason: string };
19
20export interface ProviderSessionTransitionState {
21 readonly driver: ProviderDriverKind;
22 readonly continuationIdentity: ProviderContinuationIdentity;
23 readonly modelSelection: ModelSelection;
24 readonly runtimeMode: RuntimeMode;
25 readonly interactionMode: ProviderInteractionMode;
26 readonly workspace: string;
27 readonly capabilities: OrchestrationV2ProviderCapabilities;
28}
29
30export interface ProviderSessionTransitionTarget extends ProviderSessionTransitionState {
31 readonly available: boolean;
32}
33
34export function decideProviderSessionTransition(input: {
35 readonly current: ProviderSessionTransitionState | null;
36 readonly target: ProviderSessionTransitionTarget;
37 readonly selectionTransition?: ProviderSelectionTransitionPlan;
38}): ProviderSessionTransition {
39 if (!input.target.available) {
40 return { type: "reject", reason: "The target provider instance is unavailable." };
41 }
42 if (input.current === null) {
43 return { type: "create_with_handoff" };
44 }
45
46 const current = input.current;
47 const target = input.target;
48 const continuationCompatible =
49 current.continuationIdentity.driverKind === target.continuationIdentity.driverKind &&
50 current.continuationIdentity.continuationKey === target.continuationIdentity.continuationKey;
51 if (!continuationCompatible || current.driver !== target.driver) {
52 return { type: "create_with_handoff" };
53 }
54
55 const instanceChanged = current.modelSelection.instanceId !== target.modelSelection.instanceId;
56 const runtimeChanged = current.runtimeMode !== target.runtimeMode;
57 const workspaceChanged = current.workspace !== target.workspace;
58 if (instanceChanged || runtimeChanged || workspaceChanged) {
59 return { type: "restart_and_resume" };
60 }
61
62 const selectionChanged = !modelSelectionsEqual(current.modelSelection, target.modelSelection);
63 if (selectionChanged) {
64 switch (input.selectionTransition?.type) {
65 case "apply_on_next_turn":
66 return { type: "switch_model_in_session" };
67 case "restart_session":
68 return { type: "restart_and_resume" };
69 case "create_with_handoff":
70 return { type: "create_with_handoff" };
71 case "reject":
72 return { type: "reject", reason: input.selectionTransition.reason };
73 case undefined:
74 return {
75 type: "reject",
76 reason: "The provider adapter did not classify the selection change.",
77 };
78 }
79 }
80
81 // Interaction mode is turn-scoped and is applied when the next turn starts.
82 return { type: "reuse" };
83}