Open History to see the last commit
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;
22}
23
24export class ThreadForkPlanError extends Schema.TaggedErrorClass<ThreadForkPlanError>()(
25 "ThreadForkPlanError",
26 {
27 sourceThreadId: ThreadId,
28 targetThreadId: ThreadId,
29 cause: Schema.optional(Schema.Defect()),
30 },
31) {}
32
33export interface ThreadForkServiceV2Shape {
34 readonly plan: (input: {
35 readonly sourceProjection: OrchestrationV2ThreadProjection;
36 readonly sourceRun: OrchestrationV2Run;
37 readonly sourceProviderThread: OrchestrationV2ProviderThread | undefined;
38 readonly canonicalSourcePoint: OrchestrationV2ContextSourcePoint;
39 readonly transferId: ContextTransferId;
40 readonly targetThreadId: ThreadId;
41 readonly title?: string;
42 readonly createdBy: OrchestrationV2Actor;
43 readonly creationSource: OrchestrationV2CreationSource;
44 readonly createdAt: DateTime.Utc;
45 }) => Effect.Effect<ThreadForkPlanV2, ThreadForkPlanError>;
46}
47
48export class ThreadForkServiceV2 extends Context.Service<
49 ThreadForkServiceV2,
50 ThreadForkServiceV2Shape
51>()("t3/orchestration-v2/ThreadForkService/ThreadForkServiceV2") {}
52
53export const layer: Layer.Layer<ThreadForkServiceV2> = Layer.succeed(
54 ThreadForkServiceV2,
55 ThreadForkServiceV2.of({
56 plan: (input) =>
57 Effect.gen(function* () {
58 if (input.sourceRun.status !== "completed") {
59 return yield* new ThreadForkPlanError({
60 sourceThreadId: input.sourceProjection.thread.id,
61 targetThreadId: input.targetThreadId,
62 cause: `Fork source run ${input.sourceRun.id} is ${input.sourceRun.status}.`,
63 });
64 }
65 const targetThread: OrchestrationV2AppThread = {
66 ...input.sourceProjection.thread,
67 createdBy: input.createdBy,
68 creationSource: input.creationSource,
69 id: input.targetThreadId,
70 title: input.title ?? `${input.sourceProjection.thread.title} fork`,
71 activeProviderThreadId: null,
72 lineage: {
73 parentThreadId: input.sourceProjection.thread.id,
74 relationshipToParent: "fork",
75 rootThreadId: input.sourceProjection.thread.lineage.rootThreadId,
76 },
77 forkedFrom: {
78 type: "run",
79 threadId: input.sourceProjection.thread.id,
80 runId: input.sourceRun.id,
81 },
82 createdAt: input.createdAt,
83 updatedAt: input.createdAt,
84 archivedAt: null,
85 settledOverride: null,
86 settledAt: null,
87 snoozedUntil: null,
88 snoozedAt: null,
89 lastVisitedAt: null,
90 deletedAt: null,
91 };
92 const transfer: OrchestrationV2ContextTransfer = {
93 id: input.transferId,
94 type: "fork",
95 sourceThreadId: input.sourceProjection.thread.id,
96 targetThreadId: input.targetThreadId,
97 sourcePoint: input.canonicalSourcePoint,
98 basePoint: null,
99 sourceProviderInstanceId: input.sourceRun.providerInstanceId,
100 targetProviderInstanceId: null,
101 targetRunId: null,
102 status: "pending",
103 resolution: null,
104 createdBy: input.createdBy,
105 error:
106 input.sourceProviderThread?.nativeThreadRef?.strength === "strong"
107 ? null
108 : "Source provider thread does not expose a strong native thread ref.",
109 createdAt: input.createdAt,
110 updatedAt: input.createdAt,
111 consumedAt: null,
112 };
113 return { targetThread, transfer };
114 }),
115 }),
116);