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

ThreadForkService.ts

135 lines4.6 KB

1import {
2 ContextTransferId,
3 OrchestrationV2Actor,
4 OrchestrationV2AppThread,
5 OrchestrationV2ContextSourcePoint,
6 OrchestrationV2ContextTransfer,
7 OrchestrationV2CreationSource,
8 OrchestrationV2ProviderThread,
9 OrchestrationV2Run,
10 OrchestrationV2ThreadProjection,
11 ThreadId,
12} from "@t3tools/contracts";
13import * as Context from "effect/Context";
14import * as DateTime from "effect/DateTime";
15import * as Effect from "effect/Effect";
16import * as Layer from "effect/Layer";
17import * as Schema from "effect/Schema";
18​
19export interface ThreadForkPlanV2 {
20 readonly targetThread: OrchestrationV2AppThread;
21 readonly transfer: OrchestrationV2ContextTransfer;
23​
24export class ThreadForkPlanError extends Schema.TaggedError<ThreadForkPlanError>()(
25 "ThreadForkPlanError",
26 {
27 sourceThreadId: ThreadId,
28 targetThreadId: ThreadId,
29 cause: Schema.optional(Schema.Defect()),
30 },
31) {}
32​
33/**
34 * `waiting` is provider-finished with checkpoint capture still pending.
35 */
36export function isForkableSourceRunStatus(status: OrchestrationV2Run["status"]): boolean {
37 return (
38 status === "completed" ||
39 status === "waiting" ||
40 status === "failed" ||
41 status === "interrupted" ||
42 status === "cancelled"
43 );
45​
46export function forkableSourceRunStatusError(
47 run: Pick<OrchestrationV2Run, "id" | "status">,
48): string {
49 return `Fork source run ${run.id} is ${run.status}; in-progress and rolled-back runs cannot be forked.`;
51​
52export interface ThreadForkServiceV2Shape {
53 readonly plan: (input: {
54 readonly sourceProjection: Pick<OrchestrationV2ThreadProjection, "thread">;
55 readonly sourceRun: OrchestrationV2Run;
56 readonly sourceProviderThread: OrchestrationV2ProviderThread | undefined;
57 readonly canonicalSourcePoint: OrchestrationV2ContextSourcePoint;
58 readonly transferId: ContextTransferId;
59 readonly targetThreadId: ThreadId;
60 readonly title?: string;
61 readonly createdBy: OrchestrationV2Actor;
62 readonly creationSource: OrchestrationV2CreationSource;
63 readonly createdAt: DateTime.Utc;
64 }) => Effect.Effect<ThreadForkPlanV2, ThreadForkPlanError>;
66​
67export class ThreadForkServiceV2 extends Context.Service<
68 ThreadForkServiceV2,
69 ThreadForkServiceV2Shape
70>()("t3/orchestration-v2/ThreadForkService/ThreadForkServiceV2") {}
71​
72export const layer: Layer.Layer<ThreadForkServiceV2> = Layer.succeed(
73 ThreadForkServiceV2,
74 ThreadForkServiceV2.of({
75 plan: (input) =>
76 Effect.gen(function* () {
77 if (!isForkableSourceRunStatus(input.sourceRun.status)) {
78 return yield* new ThreadForkPlanError({
79 sourceThreadId: input.sourceProjection.thread.id,
80 targetThreadId: input.targetThreadId,
81 cause: forkableSourceRunStatusError(input.sourceRun),
82 });
83 }
84 const targetThread: OrchestrationV2AppThread = {
85 ...input.sourceProjection.thread,
86 createdBy: input.createdBy,
87 creationSource: input.creationSource,
88 id: input.targetThreadId,
89 title: input.title ?? `${input.sourceProjection.thread.title} fork`,
90 activeProviderThreadId: null,
91 lineage: {
92 parentThreadId: input.sourceProjection.thread.id,
93 relationshipToParent: "fork",
94 rootThreadId: input.sourceProjection.thread.lineage.rootThreadId,
95 },
96 forkedFrom: {
97 type: "run",
98 threadId: input.sourceProjection.thread.id,
99 runId: input.sourceRun.id,
100 },
101 createdAt: input.createdAt,
102 updatedAt: input.createdAt,
103 archivedAt: null,
104 settledOverride: null,
105 settledAt: null,
106 snoozedUntil: null,
107 snoozedAt: null,
108 lastVisitedAt: null,
109 deletedAt: null,
110 };
111 const transfer: OrchestrationV2ContextTransfer = {
112 id: input.transferId,
113 type: "fork",
114 sourceThreadId: input.sourceProjection.thread.id,
115 targetThreadId: input.targetThreadId,
116 sourcePoint: input.canonicalSourcePoint,
117 basePoint: null,
118 sourceProviderInstanceId: input.sourceRun.providerInstanceId,
119 targetProviderInstanceId: null,
120 targetRunId: null,
121 status: "pending",
122 resolution: null,
123 createdBy: input.createdBy,
124 error:
125 input.sourceProviderThread?.nativeThreadRef?.strength === "strong"
126 ? null
127 : "Source provider thread does not expose a strong native thread ref.",
128 createdAt: input.createdAt,
129 updatedAt: input.createdAt,
130 consumedAt: null,
131 };
132 return { targetThread, transfer };
133 }),
134 }),
135);