The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { |
| 2 | ContextTransferId, |
| 3 | OrchestrationV2Actor, |
| 4 | OrchestrationV2AppThread, |
| 5 | OrchestrationV2ContextSourcePoint, |
| 6 | OrchestrationV2ContextTransfer, |
| 7 | OrchestrationV2CreationSource, |
| 8 | OrchestrationV2ProviderThread, |
| 9 | OrchestrationV2ProviderTurnTokenUsage, |
| 10 | OrchestrationV2Run, |
| 11 | OrchestrationV2ThreadProjection, |
| 12 | ThreadId, |
| 13 | } from "@t3tools/contracts"; |
| 14 | import * as Context from "effect/Context"; |
| 15 | import * as DateTime from "effect/DateTime"; |
| 16 | import * as Effect from "effect/Effect"; |
| 17 | import * as Layer from "effect/Layer"; |
| 18 | import * as Schema from "effect/Schema"; |
| 19 | |
| 20 | export interface ThreadForkPlanV2 { |
| 21 | readonly targetThread: OrchestrationV2AppThread; |
| 22 | readonly transfer: OrchestrationV2ContextTransfer; |
| 23 | } |
| 24 | |
| 25 | export class ThreadForkPlanError extends Schema.TaggedError<ThreadForkPlanError>()( |
| 26 | "ThreadForkPlanError", |
| 27 | { |
| 28 | sourceThreadId: ThreadId, |
| 29 | targetThreadId: ThreadId, |
| 30 | cause: Schema.optional(Schema.Defect()), |
| 31 | }, |
| 32 | ) {} |
| 33 | |
| 34 | /** waiting means provider-finished with checkpoint capture pending. */ |
| 35 | export function isForkableSourceRunStatus(status: OrchestrationV2Run["status"]): boolean { |
| 36 | return ( |
| 37 | status === "completed" || |
| 38 | status === "waiting" || |
| 39 | status === "failed" || |
| 40 | status === "interrupted" || |
| 41 | status === "cancelled" |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | export function forkableSourceRunStatusError( |
| 46 | run: Pick<OrchestrationV2Run, "id" | "status">, |
| 47 | ): string { |
| 48 | return `Fork source run ${run.id} is ${run.status}; in-progress and rolled-back runs cannot be forked.`; |
| 49 | } |
| 50 | |
| 51 | export interface ThreadForkServiceV2Shape { |
| 52 | readonly plan: (input: { |
| 53 | readonly sourceProjection: Pick<OrchestrationV2ThreadProjection, "thread">; |
| 54 | /** Null only for a side chat whose parent has no stable run yet. */ |
| 55 | readonly sourceRun: OrchestrationV2Run | null; |
| 56 | readonly sourceProviderThread: OrchestrationV2ProviderThread | undefined; |
| 57 | readonly sourceTokenUsage?: OrchestrationV2ProviderTurnTokenUsage | undefined; |
| 58 | readonly canonicalSourcePoint: OrchestrationV2ContextSourcePoint; |
| 59 | readonly transferId: ContextTransferId; |
| 60 | readonly targetThreadId: ThreadId; |
| 61 | readonly title?: string; |
| 62 | readonly sideChat?: boolean; |
| 63 | readonly createdBy: OrchestrationV2Actor; |
| 64 | readonly creationSource: OrchestrationV2CreationSource; |
| 65 | readonly createdAt: DateTime.Utc; |
| 66 | }) => Effect.Effect<ThreadForkPlanV2, ThreadForkPlanError>; |
| 67 | } |
| 68 | |
| 69 | export class ThreadForkServiceV2 extends Context.Service< |
| 70 | ThreadForkServiceV2, |
| 71 | ThreadForkServiceV2Shape |
| 72 | >()("t3/orchestration-v2/ThreadForkService/ThreadForkServiceV2") {} |
| 73 | |
| 74 | export const layer: Layer.Layer<ThreadForkServiceV2> = Layer.succeed( |
| 75 | ThreadForkServiceV2, |
| 76 | ThreadForkServiceV2.of({ |
| 77 | plan: Effect.fn("ThreadForkService.plan")(function* (input) { |
| 78 | const { sourceRun } = input; |
| 79 | const sourceLineage = input.sourceProjection.thread.lineage; |
| 80 | // A task a linked environment delegated here is a subagent of a remote thread. |
| 81 | if ( |
| 82 | input.sideChat && |
| 83 | (sourceLineage.sideChat || |
| 84 | sourceLineage.relationshipToParent === "subagent" || |
| 85 | input.sourceProjection.thread.delegatedFrom !== undefined) |
| 86 | ) { |
| 87 | return yield* new ThreadForkPlanError({ |
| 88 | sourceThreadId: input.sourceProjection.thread.id, |
| 89 | targetThreadId: input.targetThreadId, |
| 90 | cause: sourceLineage.sideChat |
| 91 | ? "A side chat cannot start another side chat." |
| 92 | : "A subagent cannot start a side chat.", |
| 93 | }); |
| 94 | } |
| 95 | if (sourceRun !== null && !isForkableSourceRunStatus(sourceRun.status)) { |
| 96 | return yield* new ThreadForkPlanError({ |
| 97 | sourceThreadId: input.sourceProjection.thread.id, |
| 98 | targetThreadId: input.targetThreadId, |
| 99 | cause: forkableSourceRunStatusError(sourceRun), |
| 100 | }); |
| 101 | } |
| 102 | // The remote parent delegated the source thread, not its forks. |
| 103 | const { delegatedFrom: _delegatedFrom, ...inherited } = input.sourceProjection.thread; |
| 104 | const targetThread: OrchestrationV2AppThread = { |
| 105 | ...inherited, |
| 106 | // PR context follows the conversation; watching remains the source thread's responsibility. |
| 107 | pullRequests: input.sourceProjection.thread.pullRequests?.map( |
| 108 | ({ watch: _watch, ...link }) => link, |
| 109 | ), |
| 110 | createdBy: input.createdBy, |
| 111 | creationSource: input.creationSource, |
| 112 | id: input.targetThreadId, |
| 113 | title: input.title ?? `${input.sourceProjection.thread.title} fork`, |
| 114 | // The source's title job and rename stay with the source thread. |
| 115 | titleRegeneration: null, |
| 116 | titleRenamed: false, |
| 117 | activeProviderThreadId: null, |
| 118 | lineage: { |
| 119 | parentThreadId: input.sourceProjection.thread.id, |
| 120 | relationshipToParent: "fork", |
| 121 | rootThreadId: input.sourceProjection.thread.lineage.rootThreadId, |
| 122 | ...(input.sideChat ? { sideChat: true } : {}), |
| 123 | }, |
| 124 | forkedFrom: |
| 125 | sourceRun === null |
| 126 | ? null |
| 127 | : { type: "run", threadId: input.sourceProjection.thread.id, runId: sourceRun.id }, |
| 128 | createdAt: input.createdAt, |
| 129 | updatedAt: input.createdAt, |
| 130 | archivedAt: null, |
| 131 | settledOverride: null, |
| 132 | settledAt: null, |
| 133 | snoozedUntil: null, |
| 134 | snoozedAt: null, |
| 135 | lastSnoozeWakeAt: null, |
| 136 | pinnedAt: null, |
| 137 | pinOrderKey: null, |
| 138 | activeOrderKey: null, |
| 139 | autoSettleDisabledAt: null, |
| 140 | // A guest link belongs to the thread it was made for. |
| 141 | guestShare: null, |
| 142 | lastVisitedAt: null, |
| 143 | // A fork is a new thread; it does not inherit how often the source was opened. |
| 144 | opens: null, |
| 145 | deletedAt: null, |
| 146 | }; |
| 147 | const transfer: OrchestrationV2ContextTransfer = { |
| 148 | id: input.transferId, |
| 149 | type: "fork", |
| 150 | sourceThreadId: input.sourceProjection.thread.id, |
| 151 | targetThreadId: input.targetThreadId, |
| 152 | sourcePoint: input.canonicalSourcePoint, |
| 153 | basePoint: null, |
| 154 | sourceProviderInstanceId: |
| 155 | sourceRun?.providerInstanceId ?? input.sourceProjection.thread.providerInstanceId, |
| 156 | // Only native forks retain this context; portable handoffs rebuild it. |
| 157 | sourceTokenUsage: |
| 158 | (sourceRun?.status === "completed" || sourceRun?.status === "waiting") && |
| 159 | input.sourceProviderThread?.nativeThreadRef?.strength === "strong" |
| 160 | ? input.sourceTokenUsage |
| 161 | : undefined, |
| 162 | targetProviderInstanceId: null, |
| 163 | targetRunId: null, |
| 164 | status: "pending", |
| 165 | resolution: null, |
| 166 | createdBy: input.createdBy, |
| 167 | error: |
| 168 | input.sourceProviderThread?.nativeThreadRef?.strength === "strong" |
| 169 | ? null |
| 170 | : "Source provider thread does not expose a strong native thread ref.", |
| 171 | createdAt: input.createdAt, |
| 172 | updatedAt: input.createdAt, |
| 173 | consumedAt: null, |
| 174 | }; |
| 175 | return { targetThread, transfer }; |
| 176 | }), |
| 177 | }), |
| 178 | ); |