Files
t3code/packages/client-runtime/src/state/threadCommands.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

471 lines
16 KiB
TypeScript

import type { ThreadId } from "@t3tools/contracts";
import * as Crypto from "effect/Crypto";
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
import { Atom } from "effect/unstable/reactivity";
import {
WS_METHODS,
type EnvironmentId,
type OrchestrationV2ShellSnapshot,
} from "@t3tools/contracts";
import { createOptimisticThreadLifecycle } from "./threadLifecycle.ts";
import * as DateTime from "effect/DateTime";
import {
createAtomCommandScheduler,
createEnvironmentCommand,
createEnvironmentRpcCommand,
} from "./runtime.ts";
import {
type ThreadCommandInput,
type ArchiveThreadInput,
type CancelQueuedRunInput,
type CreateThreadInput,
type DeleteThreadInput,
type EditQueuedRunInput,
type InterruptThreadTurnInput,
type MarkThreadUnreadInput,
type ForkThreadFromRunInput,
type MergeThreadBackInput,
type PromoteQueuedRunInput,
type ReorderQueuedRunInput,
type LinkThreadPullRequestInput,
type RespondToThreadApprovalInput,
type RespondToThreadUserInputInput,
type DismissThreadUserInputInput,
type RevertThreadCheckpointInput,
type SetThreadInteractionModeInput,
type SetThreadRuntimeModeInput,
type PinThreadInput,
type ReorderPinnedThreadInput,
type ReorderActiveThreadInput,
type SetThreadAutoSettleInput,
type SettleThreadInput,
type SnoozeThreadInput,
type StartThreadTurnInput,
type StopThreadSessionInput,
type UnarchiveThreadInput,
type UnlinkThreadPullRequestInput,
type UnpinThreadInput,
type UnsettleThreadInput,
type UnsnoozeThreadInput,
type UpdateThreadMetadataInput,
type VisitThreadInput,
archiveThread,
cancelQueuedRun,
createThread,
deleteThread,
editQueuedRun,
interruptThreadTurn,
forkThreadFromRun,
markThreadUnread,
mergeThreadBack,
promoteQueuedRun,
reorderQueuedRun,
resumeThreadQueue,
linkThreadPullRequest,
respondToThreadApproval,
respondToThreadUserInput,
dismissThreadUserInput,
revertThreadCheckpoint,
setThreadInteractionMode,
setThreadRuntimeMode,
pinThread,
reorderPinnedThread,
reorderActiveThread,
setThreadAutoSettle,
settleThread,
snoozeThread,
startThreadTurn,
stopThreadSession,
unarchiveThread,
unlinkThreadPullRequest,
unpinThread,
unsettleThread,
unsnoozeThread,
updateThreadMetadata,
visitThread,
} from "../operations/commands.ts";
import type { EnvironmentRegistry } from "../connection/registry.ts";
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
import * as ThreadHistoryController from "./threadHistoryController.ts";
export type LoadEarlierThreadHistoryInput = {
readonly threadId: ThreadId;
};
export type {
ArchiveThreadInput,
CancelQueuedRunInput,
CreateThreadInput,
DeleteThreadInput,
EditQueuedRunInput,
InterruptThreadTurnInput,
MarkThreadUnreadInput,
ForkThreadFromRunInput,
MergeThreadBackInput,
PromoteQueuedRunInput,
ReorderQueuedRunInput,
LinkThreadPullRequestInput,
RespondToThreadApprovalInput,
RespondToThreadUserInputInput,
DismissThreadUserInputInput,
RevertThreadCheckpointInput,
SetThreadInteractionModeInput,
SetThreadRuntimeModeInput,
PinThreadInput,
ReorderPinnedThreadInput,
ReorderActiveThreadInput,
SetThreadAutoSettleInput,
SettleThreadInput,
SnoozeThreadInput,
StartThreadTurnInput,
StopThreadSessionInput,
ThreadCommandInput,
UnarchiveThreadInput,
UnlinkThreadPullRequestInput,
UnpinThreadInput,
UnsettleThreadInput,
UnsnoozeThreadInput,
UpdateThreadMetadataInput,
VisitThreadInput,
} from "../operations/commands.ts";
export function createThreadEnvironmentAtoms<R, E>(
runtime: Atom.AtomRuntime<EnvironmentRegistry | Crypto.Crypto | R, E>,
snapshotAtom: (environmentId: EnvironmentId) => Atom.Atom<OrchestrationV2ShellSnapshot | null>,
) {
const scheduler = createAtomCommandScheduler();
const concurrency = {
mode: "serial" as const,
key: ({ environmentId, input }: { environmentId: string; input: { threadId: string } }) =>
JSON.stringify([environmentId, input.threadId]),
};
const commands = {
create: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:create",
execute: (input: CreateThreadInput) => createThread(input),
scheduler,
concurrency,
}),
delete: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:delete",
execute: (input: DeleteThreadInput) => deleteThread(input),
scheduler,
concurrency,
}),
archive: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:archive",
execute: (input: ArchiveThreadInput) => archiveThread(input),
scheduler,
concurrency,
}),
unarchive: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:unarchive",
execute: (input: UnarchiveThreadInput) => unarchiveThread(input),
scheduler,
concurrency,
}),
settle: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:settle",
execute: (input: SettleThreadInput) => settleThread(input),
scheduler,
concurrency,
}),
unsettle: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:unsettle",
execute: (input: UnsettleThreadInput) => unsettleThread(input),
scheduler,
concurrency,
}),
snooze: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:snooze",
execute: (input: SnoozeThreadInput) => snoozeThread(input),
scheduler,
concurrency,
}),
unsnooze: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:unsnooze",
execute: (input: UnsnoozeThreadInput) => unsnoozeThread(input),
scheduler,
concurrency,
}),
pin: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:pin",
execute: (input: PinThreadInput) => pinThread(input),
scheduler,
concurrency,
}),
unpin: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:unpin",
execute: (input: UnpinThreadInput) => unpinThread(input),
scheduler,
concurrency,
}),
reorderPin: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:reorder-pin",
execute: (input: ReorderPinnedThreadInput) => reorderPinnedThread(input),
scheduler,
concurrency,
}),
setAutoSettle: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:set-auto-settle",
execute: (input: SetThreadAutoSettleInput) => setThreadAutoSettle(input),
scheduler,
concurrency,
}),
reorderActive: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:reorder-active",
execute: (input: ReorderActiveThreadInput) => reorderActiveThread(input),
scheduler,
concurrency,
}),
visit: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:visit",
execute: (input: VisitThreadInput) => visitThread(input),
scheduler,
concurrency,
}),
markUnread: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:mark-unread",
execute: (input: MarkThreadUnreadInput) => markThreadUnread(input),
scheduler,
concurrency,
}),
updateMetadata: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:update-metadata",
execute: (input: UpdateThreadMetadataInput) => updateThreadMetadata(input),
scheduler,
concurrency,
}),
linkPullRequest: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:link-pull-request",
execute: (input: LinkThreadPullRequestInput) => linkThreadPullRequest(input),
scheduler,
concurrency,
}),
unlinkPullRequest: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:unlink-pull-request",
execute: (input: UnlinkThreadPullRequestInput) => unlinkThreadPullRequest(input),
scheduler,
concurrency,
}),
setRuntimeMode: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:set-runtime-mode",
execute: (input: SetThreadRuntimeModeInput) => setThreadRuntimeMode(input),
scheduler,
concurrency,
}),
setInteractionMode: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:set-interaction-mode",
execute: (input: SetThreadInteractionModeInput) => setThreadInteractionMode(input),
scheduler,
concurrency,
}),
startTurn: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:start-turn",
execute: (input: StartThreadTurnInput) => startThreadTurn(input),
scheduler,
concurrency,
}),
interruptTurn: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:interrupt-turn",
execute: (input: InterruptThreadTurnInput) => interruptThreadTurn(input),
scheduler,
concurrency,
}),
respondToApproval: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:respond-to-approval",
execute: (input: RespondToThreadApprovalInput) => respondToThreadApproval(input),
scheduler,
concurrency,
}),
respondToUserInput: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:respond-to-user-input",
execute: (input: RespondToThreadUserInputInput) => respondToThreadUserInput(input),
scheduler,
concurrency,
}),
dismissUserInput: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:dismiss-user-input",
execute: (input: DismissThreadUserInputInput) => dismissThreadUserInput(input),
scheduler,
concurrency,
}),
revertCheckpoint: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:revert-checkpoint",
execute: (input: RevertThreadCheckpointInput) => revertThreadCheckpoint(input),
scheduler,
concurrency,
}),
stopSession: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:stop-session",
execute: (input: StopThreadSessionInput) => stopThreadSession(input),
scheduler,
concurrency,
}),
forkFromRun: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:fork-from-run",
execute: (input: ForkThreadFromRunInput) => forkThreadFromRun(input),
scheduler,
concurrency: {
mode: "serial",
key: ({ environmentId, input }) => JSON.stringify([environmentId, input.sourceThreadId]),
},
}),
mergeBack: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:merge-back",
execute: (input: MergeThreadBackInput) => mergeThreadBack(input),
scheduler,
concurrency: {
mode: "serial",
key: ({ environmentId, input }) =>
JSON.stringify([environmentId, input.sourceThreadId, input.targetThreadId]),
},
}),
resumeThreadQueue: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:resume-queue",
execute: (input: ThreadCommandInput) => resumeThreadQueue(input),
scheduler,
concurrency,
}),
reorderQueuedRun: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:reorder-queued-run",
execute: (input: ReorderQueuedRunInput) => reorderQueuedRun(input),
scheduler,
concurrency,
}),
promoteQueuedRun: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:promote-queued-run",
execute: (input: PromoteQueuedRunInput) => promoteQueuedRun(input),
scheduler,
concurrency,
}),
cancelQueuedRun: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:cancel-queued-run",
execute: (input: CancelQueuedRunInput) => cancelQueuedRun(input),
scheduler,
concurrency,
}),
editQueuedRun: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:edit-queued-run",
execute: (input: EditQueuedRunInput) => editQueuedRun(input),
scheduler,
concurrency,
}),
loadEarlierHistory: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:load-earlier-history",
execute: (input: LoadEarlierThreadHistoryInput) =>
Effect.gen(function* () {
const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
const controller = yield* Effect.serviceOption(
ThreadHistoryController.ThreadHistoryController,
);
if (Option.isNone(controller)) {
return {
_tag: "noop",
} satisfies ThreadHistoryController.ThreadHistoryLoadEarlierResult;
}
return yield* controller.value.loadEarlier(
supervisor.target.environmentId,
input.threadId,
);
}),
scheduler,
concurrency: {
mode: "serial",
key: ({ environmentId, input }) => JSON.stringify([environmentId, input.threadId]),
},
}),
uploadFeedback: createEnvironmentRpcCommand(runtime, {
label: "environment-data:commands:thread:upload-feedback",
tag: WS_METHODS.providerUploadFeedback,
scheduler,
concurrency,
}),
};
const optimistic = createOptimisticThreadLifecycle(snapshotAtom);
return {
...commands,
snapshotAtom: optimistic.snapshotAtom,
settle: optimistic.wrap(commands.settle, (thread, _input, now, accepted) =>
!accepted &&
(thread.pendingRuntimeRequest !== null ||
["preparing", "queued", "starting", "running", "waiting"].includes(thread.status))
? thread
: {
...thread,
pendingRuntimeRequest: null,
settledOverride: "settled",
settledAt: thread.settledOverride === "settled" ? (thread.settledAt ?? now) : now,
unsettledAt: null,
activeOrderKey: null,
pinnedAt: null,
pinOrderKey: null,
snoozedAt: null,
snoozedUntil: null,
},
),
unsettle: optimistic.wrap(commands.unsettle, (thread, input, now) => ({
...thread,
settledOverride: input.reason === "user" ? "active" : null,
settledAt: null,
unsettledAt: thread.settledOverride === "active" ? (thread.unsettledAt ?? null) : now,
})),
snooze: optimistic.wrap(commands.snooze, (thread, input, now, accepted) =>
(!accepted &&
(thread.pendingRuntimeRequest !== null ||
["preparing", "queued", "starting"].includes(thread.status))) ||
!(Date.parse(input.snoozedUntil) > DateTime.toEpochMillis(now))
? thread
: {
...thread,
pendingRuntimeRequest: null,
snoozedUntil: DateTime.makeUnsafe(input.snoozedUntil),
snoozedAt:
thread.snoozedUntil != null &&
DateTime.formatIso(thread.snoozedUntil) === input.snoozedUntil
? (thread.snoozedAt ?? now)
: now,
},
),
unsnooze: optimistic.wrap(commands.unsnooze, (thread) => ({
...thread,
snoozedUntil: null,
snoozedAt: null,
})),
setAutoSettle: optimistic.wrap(commands.setAutoSettle, (thread, input, now) => ({
...thread,
autoSettleDisabledAt: input.enabled ? null : (thread.autoSettleDisabledAt ?? now),
})),
pin: optimistic.wrap(commands.pin, (thread, input, now) => ({
...thread,
pinnedAt: thread.pinnedAt ?? now,
pinOrderKey: thread.pinnedAt == null ? (input.orderKey ?? null) : thread.pinOrderKey,
...(thread.settledOverride === "settled"
? {
settledOverride: "active" as const,
settledAt: null,
unsettledAt: now,
}
: {}),
snoozedUntil: null,
snoozedAt: null,
})),
unpin: optimistic.wrap(commands.unpin, (thread) => ({
...thread,
pinnedAt: null,
pinOrderKey: null,
})),
reorderPin: optimistic.wrap(commands.reorderPin, (thread, input) => ({
...thread,
pinOrderKey: input.orderKey,
})),
reorderActive: optimistic.wrap(commands.reorderActive, (thread, input) => ({
...thread,
activeOrderKey: input.orderKey,
})),
};
}