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

threadWorkflows.ts

107 lines3.9 KB

1import type {
2 OrchestrationV2ProjectedTurnItem,
3 OrchestrationV2ProviderCapabilities,
4 OrchestrationV2ThreadProjection,
5} from "@t3tools/contracts";
6​
7type Projection = OrchestrationV2ThreadProjection;
8type Run = Projection["runs"][number];
9type ProviderSession = Projection["providerSessions"][number];
10​
11const ACTIVE_RUN_STATUSES = new Set<Run["status"]>(["preparing", "starting", "running", "waiting"]);
12​
13export interface QueuedThreadRun {
14 readonly run: Run;
15 readonly text: string;
17​
18export interface ThreadQueueWorkflowState {
19 readonly activeRun: Run | null;
20 readonly queuedRuns: ReadonlyArray<QueuedThreadRun>;
21 readonly canReorder: boolean;
22 readonly canPromoteToSteer: boolean;
24​
25export function resolveActiveThreadRun(projection: Projection): Run | null {
26 return projection.runs.findLast((run) => ACTIVE_RUN_STATUSES.has(run.status)) ?? null;
28​
29export function resolveThreadProviderSession(projection: Projection): ProviderSession | null {
30 const activeRun = resolveActiveThreadRun(projection);
31 const providerThreadId = activeRun?.providerThreadId ?? projection.thread.activeProviderThreadId;
32 const activeProviderThread =
33 providerThreadId === null
34 ? null
35 : (projection.providerThreads.find((thread) => thread.id === providerThreadId) ?? null);
36 const attachedProviderThread =
37 activeProviderThread ??
38 projection.providerThreads.find(
39 (thread) => thread.appThreadId === projection.thread.id && thread.providerSessionId !== null,
40 ) ??
41 null;
42 const sessionId = attachedProviderThread?.providerSessionId ?? null;
43 if (sessionId !== null) {
44 return projection.providerSessions.find((session) => session.id === sessionId) ?? null;
45 }
46 return (
47 projection.providerSessions.findLast(
48 (session) => session.status !== "stopped" && session.status !== "error",
49 ) ?? null
50 );
52​
53export function deriveThreadQueueWorkflowState(projection: Projection): ThreadQueueWorkflowState {
54 const activeRun = resolveActiveThreadRun(projection);
55 const session = resolveThreadProviderSession(projection);
56 const capabilities = session?.capabilities.turns;
57 const queuedRuns = projection.runs
58 .filter((run) => run.status === "queued")
59 .toSorted(
60 (left, right) =>
61 (left.queuePosition ?? left.ordinal) - (right.queuePosition ?? right.ordinal) ||
62 left.ordinal - right.ordinal,
63 )
64 .map((run) => ({
65 run,
66 text:
67 projection.messages.find((message) => message.id === run.userMessageId)?.text ??
68 "Queued message",
69 }));
70​
71 return {
72 activeRun,
73 queuedRuns,
74 canReorder: capabilities?.supportsQueuedMessages === true,
75 canPromoteToSteer:
76 activeRun !== null &&
77 (capabilities?.supportsActiveSteering === true ||
78 capabilities?.supportsSteeringByInterruptRestart === true),
79 };
81​
82export function canForkProjectedAssistantItem(input: {
83 readonly projectedItem: OrchestrationV2ProjectedTurnItem;
84 readonly capabilities?: OrchestrationV2ProviderCapabilities | undefined;
85}): boolean {
86 const item = input.projectedItem.item;
87 if (item.type !== "assistant_message" || item.runId === null || item.status !== "completed") {
88 return false;
89 }
90 if (input.capabilities === undefined) {
91 // Historical and inherited rows may outlive their provider-session record.
92 // Keep the portable server-side fallback available when capability evidence
93 // is absent; a known incapable provider is rejected below.
94 return true;
95 }
96 const capabilities = input.capabilities;
97 const canForkNatively =
98 capabilities.threads.canForkThread &&
99 capabilities.threads.canForkFromTurn &&
100 capabilities.identity.nativeThreadIds === "strong";
101 return canForkNatively || capabilities.context.supportsFullThreadHandoff;
103​
104export function canDetachThreadProviderSession(projection: Projection): boolean {
105 const session = resolveThreadProviderSession(projection);
106 return session !== null && session.status !== "stopped" && session.status !== "error";