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

ThreadForkService.ts

139 lines4.8 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 // PR context follows the conversation; watching remains the source thread's responsibility.
87 pullRequests: input.sourceProjection.thread.pullRequests?.map(
88 ({ watch: _watch, ...link }) => link,
89 ),
90 createdBy: input.createdBy,
91 creationSource: input.creationSource,
92 id: input.targetThreadId,
93 title: input.title ?? `${input.sourceProjection.thread.title} fork`,
94 activeProviderThreadId: null,
95 lineage: {
96 parentThreadId: input.sourceProjection.thread.id,
97 relationshipToParent: "fork",
98 rootThreadId: input.sourceProjection.thread.lineage.rootThreadId,
99 },
100 forkedFrom: {
101 type: "run",
102 threadId: input.sourceProjection.thread.id,
103 runId: input.sourceRun.id,
104 },
105 createdAt: input.createdAt,
106 updatedAt: input.createdAt,
107 archivedAt: null,
108 settledOverride: null,
109 settledAt: null,
110 snoozedUntil: null,
111 snoozedAt: null,
112 lastVisitedAt: null,
113 deletedAt: null,
114 };
115 const transfer: OrchestrationV2ContextTransfer = {
116 id: input.transferId,
117 type: "fork",
118 sourceThreadId: input.sourceProjection.thread.id,
119 targetThreadId: input.targetThreadId,
120 sourcePoint: input.canonicalSourcePoint,
121 basePoint: null,
122 sourceProviderInstanceId: input.sourceRun.providerInstanceId,
123 targetProviderInstanceId: null,
124 targetRunId: null,
125 status: "pending",
126 resolution: null,
127 createdBy: input.createdBy,
128 error:
129 input.sourceProviderThread?.nativeThreadRef?.strength === "strong"
130 ? null
131 : "Source provider thread does not expose a strong native thread ref.",
132 createdAt: input.createdAt,
133 updatedAt: input.createdAt,
134 consumedAt: null,
135 };
136 return { targetThread, transfer };
137 }),
138 }),
139);