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 deletedAt: null,
86 };
87 const transfer: OrchestrationV2ContextTransfer = {
88 id: input.transferId,
89 type: "fork",
90 sourceThreadId: input.sourceProjection.thread.id,
91 targetThreadId: input.targetThreadId,
92 sourcePoint: input.canonicalSourcePoint,
93 basePoint: null,
94 sourceProviderInstanceId: input.sourceRun.providerInstanceId,
95 targetProviderInstanceId: null,
96 targetRunId: null,
97 status: "pending",
98 resolution: null,
99 createdBy: input.createdBy,
100 error:
101 input.sourceProviderThread?.nativeThreadRef?.strength === "strong"
102 ? null
103 : "Source provider thread does not expose a strong native thread ref.",
104 createdAt: input.createdAt,
105 updatedAt: input.createdAt,
106 consumedAt: null,
107 };
108 return { targetThread, transfer };
109 }),
110 }),
111);