Open History to see the last commit
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;
68}
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 messageId = MessageId.make(`message:restart-continuation:${input.sourceRunId}`);
77 const projection = yield* threads.getThreadRecords(input.threadId, ["messages", "runs"], {
78 messageIds: [messageId],
79 });
80 if (
81 !resolveProjectSettings(enabled, projection.thread.projectId).settings
82 .continueThreadsAfterServerUpdate
83 )
84 return;
85 if (projection.thread.archivedAt !== null || projection.thread.deletedAt !== null) return;
86
87 if (projection.messages.some((message) => message.id === messageId)) return;
88 const source = projection.runs.find((run) => run.id === input.sourceRunId);
89 if (!source || source.status !== "cancelled") return;
90 // A user submission after reconciliation takes precedence over an automatic prompt.
91 if (projection.runs.some((run) => run.ordinal > source.ordinal)) return;
92 if (projection.thread.providerInstanceId !== source.providerInstanceId) return;
93 yield* threads.dispatch({
94 type: "message.dispatch",
95 commandId: CommandId.make(`command:restart-continuation:${input.sourceRunId}`),
96 threadId: input.threadId,
97 messageId,
98 text: "Continue where you left off.",
99 attachments: [],
100 modelSelection: source.modelSelection,
101 dispatchMode: { type: "start_immediately" },
102 createdBy: "agent",
103 creationSource: "server",
104 restartContinuationOfRunId: input.sourceRunId,
105 });
106 },
107);