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

RestartContinuation.ts

104 lines4.2 KB

1import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
2import {
3 CommandId,
4 MessageId,
5 type OrchestrationV2Run,
6 type RunId,
7 type ThreadId,
8} from "@t3tools/contracts";
9import * as Effect from "effect/Effect";
10import type { ProjectionRuntimeRecoveryState } from "./ProjectionStore.ts";
11​
12import { ServerSettingsService } from "../serverSettings.ts";
13import { ThreadManagementService } from "./ThreadManagementService.ts";
14​
15export function restartContinuationRun(
16 projection: Pick<
17 ProjectionRuntimeRecoveryState,
18 "thread" | "runs" | "providerThreads" | "providerSessions" | "providerTurns"
19 >,
20): OrchestrationV2Run | undefined {
21 if (projection.thread.archivedAt !== null || projection.thread.deletedAt !== null) return;
22 const run = projection.runs.reduce<OrchestrationV2Run | undefined>(
23 (latest, candidate) => (!latest || candidate.ordinal > latest.ordinal ? candidate : latest),
24 undefined,
25 );
26 if (!run) return;
27 const preparedContinuation =
28 run.status === "starting" && run.restartContinuationOfRunId !== undefined;
29 if (run.status !== "running" && !preparedContinuation) return;
30 if (projection.thread.providerInstanceId !== run.providerInstanceId) return;
31 const providerThread = projection.providerThreads.find(
32 (thread) => thread.id === run.providerThreadId,
33 );
34 if (
35 !providerThread ||
36 providerThread.appThreadId !== projection.thread.id ||
37 providerThread.ownerNodeId !== null ||
38 providerThread.providerInstanceId !== run.providerInstanceId ||
39 providerThread.nativeThreadRef?.nativeId == null ||
40 providerThread.nativeThreadRef.strength !== "strong" ||
41 providerThread.nativeThreadRef.driver !== providerThread.driver ||
42 (!preparedContinuation && providerThread.status !== "active") ||
43 providerThread.status === "closed" ||
44 providerThread.status === "archived"
45 )
46 return;
47 const session = projection.providerSessions.find(
48 (candidate) => candidate.id === providerThread.providerSessionId,
49 );
50 if (
51 !session ||
52 session.providerInstanceId !== run.providerInstanceId ||
53 session.driver !== providerThread.driver ||
54 (!preparedContinuation && session.status !== "running")
55 )
56 return;
57 if (
58 !preparedContinuation &&
59 !projection.providerTurns.some(
60 (turn) =>
61 turn.providerThreadId === providerThread.id &&
62 turn.runAttemptId === run.activeAttemptId &&
63 turn.status === "running",
64 )
65 )
66 return;
67 return run;
69​
70export const continueRestartedRun = Effect.fn("RestartContinuation.continueRestartedRun")(
71 function* (input: { readonly threadId: ThreadId; readonly sourceRunId: RunId }) {
72 const settings = yield* ServerSettingsService;
73 const enabled = yield* settings.getSettings.pipe(Effect.orElseSucceed(() => null));
74 if (!enabled) return;
75 const threads = yield* ThreadManagementService;
76 const projection = yield* threads.getThreadProjection(input.threadId);
77 if (
78 !resolveProjectSettings(enabled, projection.thread.projectId).settings
79 .continueThreadsAfterServerUpdate
80 )
81 return;
82 if (projection.thread.archivedAt !== null || projection.thread.deletedAt !== null) return;
83 const messageId = MessageId.make(`message:restart-continuation:${input.sourceRunId}`);
84 if (projection.messages.some((message) => message.id === messageId)) return;
85 const source = projection.runs.find((run) => run.id === input.sourceRunId);
86 if (!source || source.status !== "cancelled") return;
87 // A user submission after reconciliation takes precedence over an automatic prompt.
88 if (projection.runs.some((run) => run.ordinal > source.ordinal)) return;
89 if (projection.thread.providerInstanceId !== source.providerInstanceId) return;
90 yield* threads.dispatch({
91 type: "message.dispatch",
92 commandId: CommandId.make(`command:restart-continuation:${input.sourceRunId}`),
93 threadId: input.threadId,
94 messageId,
95 text: "Continue where you left off.",
96 attachments: [],
97 modelSelection: source.modelSelection,
98 dispatchMode: { type: "start_immediately" },
99 createdBy: "agent",
100 creationSource: "server",
101 restartContinuationOfRunId: input.sourceRunId,
102 });
103 },
104);