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, 04:45 UTC
| 1 | import { CommandId, type ModelSelection, type ThreadId } from "@t3tools/contracts"; |
| 2 | import * as SecureRandom from "effect/crypto/SecureRandom"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Option from "effect/Option"; |
| 5 | import { AsyncResult, Atom, type AtomRegistry } from "effect/reactivity"; |
| 6 | import { |
| 7 | WS_METHODS, |
| 8 | type EnvironmentId, |
| 9 | type OrchestrationV2ShellSnapshot, |
| 10 | } from "@t3tools/contracts"; |
| 11 | |
| 12 | import { createOptimisticThreadLifecycle } from "./threadLifecycle.ts"; |
| 13 | import * as DateTime from "effect/DateTime"; |
| 14 | |
| 15 | import { |
| 16 | type AtomCommand, |
| 17 | createAtomCommandScheduler, |
| 18 | createEnvironmentCommand, |
| 19 | createEnvironmentRpcCommand, |
| 20 | environmentConnectionAtom, |
| 21 | } from "./runtime.ts"; |
| 22 | import { waitForTitleRegenerationOutcome } from "./threadTitleRegeneration.ts"; |
| 23 | import { |
| 24 | type InterruptThreadTurnInput, |
| 25 | type UpdateThreadMetadataInput, |
| 26 | archiveThread, |
| 27 | closeGuestShare, |
| 28 | resolveGuestMessage, |
| 29 | cancelQueuedRun, |
| 30 | createThread, |
| 31 | deleteThread, |
| 32 | editQueuedRun, |
| 33 | interruptThreadTurn, |
| 34 | forkSideChat, |
| 35 | forkThreadFromRun, |
| 36 | markThreadUnread, |
| 37 | mergeThreadBack, |
| 38 | promoteQueuedRun, |
| 39 | resumeThreadQueue, |
| 40 | retryWorkspacePreparation, |
| 41 | linkThreadPullRequest, |
| 42 | linkThreadIssue, |
| 43 | respondToThreadApproval, |
| 44 | respondToThreadUserInput, |
| 45 | dismissThreadUserInput, |
| 46 | revertThreadCheckpoint, |
| 47 | setThreadInteractionMode, |
| 48 | setThreadRuntimeMode, |
| 49 | pinThread, |
| 50 | reorderPinnedThread, |
| 51 | reorderActiveThread, |
| 52 | setThreadAutoSettle, |
| 53 | settleThread, |
| 54 | snoozeThread, |
| 55 | startThreadTurn, |
| 56 | stopThreadSession, |
| 57 | unarchiveThread, |
| 58 | unlinkThreadPullRequest, |
| 59 | unlinkThreadIssue, |
| 60 | unpinThread, |
| 61 | unsettleThread, |
| 62 | unsnoozeThread, |
| 63 | updateThreadMetadata, |
| 64 | visitThread, |
| 65 | watchThreadPullRequest, |
| 66 | } from "../operations/commands.ts"; |
| 67 | import { connectionProjectionPhase } from "../connection/model.ts"; |
| 68 | import type { EnvironmentRegistry } from "../connection/registry.ts"; |
| 69 | import * as EnvironmentSupervisor from "../connection/supervisor.ts"; |
| 70 | import * as ThreadHistoryController from "./threadHistoryController.ts"; |
| 71 | |
| 72 | export type LoadEarlierThreadHistoryInput = { |
| 73 | readonly threadId: ThreadId; |
| 74 | readonly throughEntryId?: string; |
| 75 | readonly workAnchorId?: string; |
| 76 | }; |
| 77 | |
| 78 | export function createThreadEnvironmentAtoms<R, E>( |
| 79 | runtime: Atom.AtomRuntime<EnvironmentRegistry | SecureRandom.SecureRandom | R, E>, |
| 80 | snapshotAtom: (environmentId: EnvironmentId) => Atom.Atom<OrchestrationV2ShellSnapshot | null>, |
| 81 | ) { |
| 82 | const scheduler = createAtomCommandScheduler(); |
| 83 | const concurrency = { |
| 84 | mode: "serial" as const, |
| 85 | key: ({ environmentId, input }: { environmentId: string; input: { threadId: string } }) => |
| 86 | JSON.stringify([environmentId, input.threadId]), |
| 87 | }; |
| 88 | const loadEarlierHistory = Effect.fn("ThreadCommands.loadEarlierHistory")(function* ( |
| 89 | input: LoadEarlierThreadHistoryInput, |
| 90 | ) { |
| 91 | const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor; |
| 92 | const controller = yield* Effect.serviceOption(ThreadHistoryController.ThreadHistoryController); |
| 93 | if (Option.isNone(controller)) { |
| 94 | return { |
| 95 | _tag: "noop", |
| 96 | } satisfies ThreadHistoryController.ThreadHistoryLoadEarlierResult; |
| 97 | } |
| 98 | return yield* controller.value.loadEarlier( |
| 99 | supervisor.target.environmentId, |
| 100 | input.threadId, |
| 101 | input.throughEntryId, |
| 102 | input.workAnchorId, |
| 103 | ); |
| 104 | }); |
| 105 | |
| 106 | // Runs whether or not the server accepted the interrupt, so a stale cached item still |
| 107 | // settles when Stop had nothing to interrupt; the interrupt's own failure still surfaces. |
| 108 | // The repair is detached: its lookups must not hold the thread's serial command key, and |
| 109 | // closing the thread cancels the repair, not the Stop that triggered it. |
| 110 | const interruptTurn = Effect.fn("ThreadCommands.interruptTurn")(function* ( |
| 111 | input: InterruptThreadTurnInput, |
| 112 | ) { |
| 113 | const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor; |
| 114 | const controller = yield* Effect.serviceOption(ThreadHistoryController.ThreadHistoryController); |
| 115 | return yield* interruptThreadTurn(input).pipe( |
| 116 | Effect.ensuring( |
| 117 | Option.match(controller, { |
| 118 | onNone: () => Effect.void, |
| 119 | onSome: (value) => |
| 120 | value |
| 121 | .reconcilePendingWork(supervisor.target.environmentId, input.threadId) |
| 122 | .pipe(Effect.forkDetach, Effect.asVoid), |
| 123 | }), |
| 124 | ), |
| 125 | ); |
| 126 | }); |
| 127 | |
| 128 | const armTitleRegeneration = Effect.fn("ThreadCommands.armTitleRegeneration")(function* (input: { |
| 129 | readonly threadId: ThreadId; |
| 130 | }) { |
| 131 | const random = yield* SecureRandom.SecureRandom; |
| 132 | const requestId = CommandId.make(yield* random.uuidV4()); |
| 133 | const result = yield* updateThreadMetadata({ |
| 134 | threadId: input.threadId, |
| 135 | regenerateTitle: true, |
| 136 | commandId: requestId, |
| 137 | }); |
| 138 | return { requestId, sequence: result.sequence }; |
| 139 | }); |
| 140 | |
| 141 | // Counts the commands that move or remove a thread's row. A list that holds its rows while |
| 142 | // environments synchronize (`stepSteadyShells`) shows the user's own change at once. |
| 143 | const rowActionsAtom = Atom.family((_environmentId: EnvironmentId) => |
| 144 | Atom.make(0).pipe(Atom.keepAlive), |
| 145 | ); |
| 146 | const rowAction = <W extends { readonly environmentId: EnvironmentId }, A, E>( |
| 147 | command: AtomCommand<W, A, E>, |
| 148 | ): AtomCommand<W, A, E> => ({ |
| 149 | label: command.label, |
| 150 | run: (registry, target) => |
| 151 | Effect.suspend(() => { |
| 152 | registry.update(rowActionsAtom(target.environmentId), (count) => count + 1); |
| 153 | return command.run(registry, target); |
| 154 | }), |
| 155 | }); |
| 156 | |
| 157 | // The environment's time of the last sign-in to a server in a thread, until the app reloads. |
| 158 | const mcpSignedInAtFamily = Atom.family((_key: string) => |
| 159 | Atom.make<number | null>(null).pipe(Atom.keepAlive), |
| 160 | ); |
| 161 | const mcpSignInKey = ( |
| 162 | environmentId: EnvironmentId, |
| 163 | input: { readonly threadId: ThreadId; readonly server: string }, |
| 164 | ) => JSON.stringify([environmentId, input.threadId, input.server]); |
| 165 | const markMcpSignedIn = ( |
| 166 | registry: AtomRegistry.AtomRegistry, |
| 167 | environmentId: EnvironmentId, |
| 168 | input: { readonly threadId: ThreadId; readonly server: string }, |
| 169 | signedInAt: string, |
| 170 | ) => |
| 171 | registry.update(mcpSignedInAtFamily(mcpSignInKey(environmentId, input)), (current) => |
| 172 | Math.max(current ?? 0, Date.parse(signedInAt)), |
| 173 | ); |
| 174 | const mcpNoticeSignedInFamily = Atom.family((key: string) => { |
| 175 | const [signInKey, noticeAt] = JSON.parse(key) as [string, number]; |
| 176 | // A notice newer than the sign-in means the server needs one again. |
| 177 | return Atom.make((get) => { |
| 178 | const signedInAt = get(mcpSignedInAtFamily(signInKey)); |
| 179 | return signedInAt !== null && noticeAt <= signedInAt; |
| 180 | }); |
| 181 | }); |
| 182 | /** Whether a sign-in notice, by its environment time, came before the server was signed in. */ |
| 183 | const mcpSignedInAtom = ( |
| 184 | environmentId: EnvironmentId, |
| 185 | input: { readonly threadId: ThreadId; readonly server: string; readonly noticeAt: number }, |
| 186 | ) => |
| 187 | mcpNoticeSignedInFamily(JSON.stringify([mcpSignInKey(environmentId, input), input.noticeAt])); |
| 188 | const sent = { |
| 189 | armTitleRegeneration: createEnvironmentCommand(runtime, { |
| 190 | label: "environment-data:commands:thread:regenerate-title", |
| 191 | execute: armTitleRegeneration, |
| 192 | scheduler, |
| 193 | concurrency, |
| 194 | }), |
| 195 | |
| 196 | create: createEnvironmentCommand(runtime, { |
| 197 | label: "environment-data:commands:thread:create", |
| 198 | execute: createThread, |
| 199 | scheduler, |
| 200 | concurrency, |
| 201 | }), |
| 202 | delete: createEnvironmentCommand(runtime, { |
| 203 | label: "environment-data:commands:thread:delete", |
| 204 | execute: deleteThread, |
| 205 | scheduler, |
| 206 | concurrency, |
| 207 | }), |
| 208 | closeGuestShare: createEnvironmentCommand(runtime, { |
| 209 | label: "environment-data:commands:thread:close-guest-share", |
| 210 | execute: closeGuestShare, |
| 211 | scheduler, |
| 212 | concurrency, |
| 213 | }), |
| 214 | resolveGuestMessage: createEnvironmentCommand(runtime, { |
| 215 | label: "environment-data:commands:thread:resolve-guest-message", |
| 216 | execute: resolveGuestMessage, |
| 217 | scheduler, |
| 218 | concurrency, |
| 219 | }), |
| 220 | archive: createEnvironmentCommand(runtime, { |
| 221 | label: "environment-data:commands:thread:archive", |
| 222 | execute: archiveThread, |
| 223 | scheduler, |
| 224 | concurrency, |
| 225 | }), |
| 226 | unarchive: createEnvironmentCommand(runtime, { |
| 227 | label: "environment-data:commands:thread:unarchive", |
| 228 | execute: unarchiveThread, |
| 229 | scheduler, |
| 230 | concurrency, |
| 231 | }), |
| 232 | settle: createEnvironmentCommand(runtime, { |
| 233 | label: "environment-data:commands:thread:settle", |
| 234 | execute: settleThread, |
| 235 | scheduler, |
| 236 | concurrency, |
| 237 | }), |
| 238 | unsettle: createEnvironmentCommand(runtime, { |
| 239 | label: "environment-data:commands:thread:unsettle", |
| 240 | execute: unsettleThread, |
| 241 | scheduler, |
| 242 | concurrency, |
| 243 | }), |
| 244 | snooze: createEnvironmentCommand(runtime, { |
| 245 | label: "environment-data:commands:thread:snooze", |
| 246 | execute: snoozeThread, |
| 247 | scheduler, |
| 248 | concurrency, |
| 249 | }), |
| 250 | unsnooze: createEnvironmentCommand(runtime, { |
| 251 | label: "environment-data:commands:thread:unsnooze", |
| 252 | execute: unsnoozeThread, |
| 253 | scheduler, |
| 254 | concurrency, |
| 255 | }), |
| 256 | pin: createEnvironmentCommand(runtime, { |
| 257 | label: "environment-data:commands:thread:pin", |
| 258 | execute: pinThread, |
| 259 | scheduler, |
| 260 | concurrency, |
| 261 | }), |
| 262 | unpin: createEnvironmentCommand(runtime, { |
| 263 | label: "environment-data:commands:thread:unpin", |
| 264 | execute: unpinThread, |
| 265 | scheduler, |
| 266 | concurrency, |
| 267 | }), |
| 268 | reorderPin: createEnvironmentCommand(runtime, { |
| 269 | label: "environment-data:commands:thread:reorder-pin", |
| 270 | execute: reorderPinnedThread, |
| 271 | scheduler, |
| 272 | concurrency, |
| 273 | }), |
| 274 | setAutoSettle: createEnvironmentCommand(runtime, { |
| 275 | label: "environment-data:commands:thread:set-auto-settle", |
| 276 | execute: setThreadAutoSettle, |
| 277 | scheduler, |
| 278 | concurrency, |
| 279 | }), |
| 280 | reorderActive: createEnvironmentCommand(runtime, { |
| 281 | label: "environment-data:commands:thread:reorder-active", |
| 282 | execute: reorderActiveThread, |
| 283 | scheduler, |
| 284 | concurrency, |
| 285 | }), |
| 286 | visit: createEnvironmentCommand(runtime, { |
| 287 | label: "environment-data:commands:thread:visit", |
| 288 | execute: visitThread, |
| 289 | scheduler, |
| 290 | concurrency, |
| 291 | }), |
| 292 | markUnread: createEnvironmentCommand(runtime, { |
| 293 | label: "environment-data:commands:thread:mark-unread", |
| 294 | execute: markThreadUnread, |
| 295 | scheduler, |
| 296 | concurrency, |
| 297 | }), |
| 298 | updateMetadata: createEnvironmentCommand(runtime, { |
| 299 | label: "environment-data:commands:thread:update-metadata", |
| 300 | execute: updateThreadMetadata, |
| 301 | scheduler, |
| 302 | concurrency, |
| 303 | }), |
| 304 | linkPullRequest: createEnvironmentCommand(runtime, { |
| 305 | label: "environment-data:commands:thread:link-pull-request", |
| 306 | execute: linkThreadPullRequest, |
| 307 | scheduler, |
| 308 | concurrency, |
| 309 | }), |
| 310 | unlinkPullRequest: createEnvironmentCommand(runtime, { |
| 311 | label: "environment-data:commands:thread:unlink-pull-request", |
| 312 | execute: unlinkThreadPullRequest, |
| 313 | scheduler, |
| 314 | concurrency, |
| 315 | }), |
| 316 | watchPullRequest: createEnvironmentCommand(runtime, { |
| 317 | label: "environment-data:commands:thread:watch-pull-request", |
| 318 | execute: watchThreadPullRequest, |
| 319 | scheduler, |
| 320 | concurrency, |
| 321 | }), |
| 322 | setModelSelection: createEnvironmentCommand(runtime, { |
| 323 | label: "environment-data:commands:thread:set-model-selection", |
| 324 | execute: (input: UpdateThreadMetadataInput & { readonly modelSelection: ModelSelection }) => |
| 325 | updateThreadMetadata(input), |
| 326 | scheduler, |
| 327 | concurrency, |
| 328 | }), |
| 329 | linkIssue: createEnvironmentCommand(runtime, { |
| 330 | label: "environment-data:commands:thread:link-issue", |
| 331 | execute: linkThreadIssue, |
| 332 | scheduler, |
| 333 | concurrency, |
| 334 | }), |
| 335 | unlinkIssue: createEnvironmentCommand(runtime, { |
| 336 | label: "environment-data:commands:thread:unlink-issue", |
| 337 | execute: unlinkThreadIssue, |
| 338 | scheduler, |
| 339 | concurrency, |
| 340 | }), |
| 341 | setRuntimeMode: createEnvironmentCommand(runtime, { |
| 342 | label: "environment-data:commands:thread:set-runtime-mode", |
| 343 | execute: setThreadRuntimeMode, |
| 344 | scheduler, |
| 345 | concurrency, |
| 346 | }), |
| 347 | setInteractionMode: createEnvironmentCommand(runtime, { |
| 348 | label: "environment-data:commands:thread:set-interaction-mode", |
| 349 | execute: setThreadInteractionMode, |
| 350 | scheduler, |
| 351 | concurrency, |
| 352 | }), |
| 353 | startTurn: createEnvironmentCommand(runtime, { |
| 354 | label: "environment-data:commands:thread:start-turn", |
| 355 | execute: startThreadTurn, |
| 356 | scheduler, |
| 357 | concurrency, |
| 358 | }), |
| 359 | interruptTurn: createEnvironmentCommand(runtime, { |
| 360 | label: "environment-data:commands:thread:interrupt-turn", |
| 361 | execute: interruptTurn, |
| 362 | scheduler, |
| 363 | concurrency, |
| 364 | }), |
| 365 | respondToApproval: createEnvironmentCommand(runtime, { |
| 366 | label: "environment-data:commands:thread:respond-to-approval", |
| 367 | execute: respondToThreadApproval, |
| 368 | scheduler, |
| 369 | concurrency, |
| 370 | }), |
| 371 | respondToUserInput: createEnvironmentCommand(runtime, { |
| 372 | label: "environment-data:commands:thread:respond-to-user-input", |
| 373 | execute: respondToThreadUserInput, |
| 374 | scheduler, |
| 375 | concurrency, |
| 376 | }), |
| 377 | dismissUserInput: createEnvironmentCommand(runtime, { |
| 378 | label: "environment-data:commands:thread:dismiss-user-input", |
| 379 | execute: dismissThreadUserInput, |
| 380 | scheduler, |
| 381 | concurrency, |
| 382 | }), |
| 383 | revertCheckpoint: createEnvironmentCommand(runtime, { |
| 384 | label: "environment-data:commands:thread:revert-checkpoint", |
| 385 | execute: revertThreadCheckpoint, |
| 386 | scheduler, |
| 387 | concurrency, |
| 388 | }), |
| 389 | stopSession: createEnvironmentCommand(runtime, { |
| 390 | label: "environment-data:commands:thread:stop-session", |
| 391 | execute: stopThreadSession, |
| 392 | scheduler, |
| 393 | concurrency, |
| 394 | }), |
| 395 | forkFromRun: createEnvironmentCommand(runtime, { |
| 396 | label: "environment-data:commands:thread:fork-from-run", |
| 397 | execute: forkThreadFromRun, |
| 398 | scheduler, |
| 399 | concurrency: { |
| 400 | mode: "serial", |
| 401 | key: ({ environmentId, input }) => JSON.stringify([environmentId, input.sourceThreadId]), |
| 402 | }, |
| 403 | }), |
| 404 | forkSideChat: createEnvironmentCommand(runtime, { |
| 405 | label: "environment-data:commands:thread:fork-side-chat", |
| 406 | execute: forkSideChat, |
| 407 | scheduler, |
| 408 | concurrency: { |
| 409 | mode: "serial", |
| 410 | key: ({ environmentId, input }) => JSON.stringify([environmentId, input.sourceThreadId]), |
| 411 | }, |
| 412 | }), |
| 413 | mergeBack: createEnvironmentCommand(runtime, { |
| 414 | label: "environment-data:commands:thread:merge-back", |
| 415 | execute: mergeThreadBack, |
| 416 | scheduler, |
| 417 | concurrency: { |
| 418 | mode: "serial", |
| 419 | key: ({ environmentId, input }) => |
| 420 | JSON.stringify([environmentId, input.sourceThreadId, input.targetThreadId]), |
| 421 | }, |
| 422 | }), |
| 423 | resumeThreadQueue: createEnvironmentCommand(runtime, { |
| 424 | label: "environment-data:commands:thread:resume-queue", |
| 425 | execute: resumeThreadQueue, |
| 426 | scheduler, |
| 427 | concurrency, |
| 428 | }), |
| 429 | promoteQueuedRun: createEnvironmentCommand(runtime, { |
| 430 | label: "environment-data:commands:thread:promote-queued-run", |
| 431 | execute: promoteQueuedRun, |
| 432 | scheduler, |
| 433 | concurrency, |
| 434 | }), |
| 435 | cancelQueuedRun: createEnvironmentCommand(runtime, { |
| 436 | label: "environment-data:commands:thread:cancel-queued-run", |
| 437 | execute: cancelQueuedRun, |
| 438 | scheduler, |
| 439 | concurrency, |
| 440 | }), |
| 441 | retryWorkspacePreparation: createEnvironmentCommand(runtime, { |
| 442 | label: "environment-data:commands:thread:retry-workspace-preparation", |
| 443 | execute: retryWorkspacePreparation, |
| 444 | scheduler, |
| 445 | concurrency, |
| 446 | }), |
| 447 | editQueuedRun: createEnvironmentCommand(runtime, { |
| 448 | label: "environment-data:commands:thread:edit-queued-run", |
| 449 | execute: editQueuedRun, |
| 450 | scheduler, |
| 451 | concurrency, |
| 452 | }), |
| 453 | loadEarlierHistory: createEnvironmentCommand(runtime, { |
| 454 | label: "environment-data:commands:thread:load-earlier-history", |
| 455 | execute: loadEarlierHistory, |
| 456 | scheduler, |
| 457 | concurrency: { |
| 458 | mode: "serial", |
| 459 | key: ({ environmentId, input }) => |
| 460 | `history:${JSON.stringify([environmentId, input.threadId])}`, |
| 461 | }, |
| 462 | }), |
| 463 | uploadFeedback: createEnvironmentRpcCommand(runtime, { |
| 464 | label: "environment-data:commands:thread:upload-feedback", |
| 465 | tag: WS_METHODS.providerUploadFeedback, |
| 466 | scheduler, |
| 467 | concurrency, |
| 468 | }), |
| 469 | // Outside the thread's queue: finishing can wait on the agent, and messages must not. |
| 470 | mcpSignInStart: createEnvironmentRpcCommand(runtime, { |
| 471 | label: "environment-data:commands:thread:mcp-sign-in-start", |
| 472 | tag: WS_METHODS.providerMcpSignInStart, |
| 473 | onSuccess: ({ environmentId, input }, registry, result) => |
| 474 | Effect.sync(() => { |
| 475 | if (result.signedInAt !== null) { |
| 476 | markMcpSignedIn(registry, environmentId, input, result.signedInAt); |
| 477 | } |
| 478 | }), |
| 479 | }), |
| 480 | mcpSignInComplete: createEnvironmentRpcCommand(runtime, { |
| 481 | label: "environment-data:commands:thread:mcp-sign-in-complete", |
| 482 | tag: WS_METHODS.providerMcpSignInComplete, |
| 483 | onSuccess: ({ environmentId, input }, registry, result) => |
| 484 | Effect.sync(() => markMcpSignedIn(registry, environmentId, input, result.signedInAt)), |
| 485 | }), |
| 486 | }; |
| 487 | const commands = { |
| 488 | ...sent, |
| 489 | delete: rowAction(sent.delete), |
| 490 | archive: rowAction(sent.archive), |
| 491 | unarchive: rowAction(sent.unarchive), |
| 492 | settle: rowAction(sent.settle), |
| 493 | unsettle: rowAction(sent.unsettle), |
| 494 | snooze: rowAction(sent.snooze), |
| 495 | unsnooze: rowAction(sent.unsnooze), |
| 496 | pin: rowAction(sent.pin), |
| 497 | unpin: rowAction(sent.unpin), |
| 498 | }; |
| 499 | const optimistic = createOptimisticThreadLifecycle(snapshotAtom); |
| 500 | const disconnectedAtom = Atom.family((environmentId: EnvironmentId) => |
| 501 | Atom.make((get) => { |
| 502 | const connection = AsyncResult.getOrElse( |
| 503 | get(environmentConnectionAtom(runtime, environmentId)), |
| 504 | () => null, |
| 505 | ); |
| 506 | return connection !== null && connectionProjectionPhase(connection[0]) === "disconnected"; |
| 507 | }), |
| 508 | ); |
| 509 | const regenerateTitle = { |
| 510 | label: commands.armTitleRegeneration.label, |
| 511 | run: ( |
| 512 | registry: AtomRegistry.AtomRegistry, |
| 513 | target: { readonly environmentId: EnvironmentId; readonly input: { threadId: ThreadId } }, |
| 514 | ) => |
| 515 | commands.armTitleRegeneration.run(registry, target).pipe( |
| 516 | Effect.flatMap((armed) => |
| 517 | waitForTitleRegenerationOutcome( |
| 518 | registry, |
| 519 | { |
| 520 | snapshot: snapshotAtom(target.environmentId), |
| 521 | disconnected: disconnectedAtom(target.environmentId), |
| 522 | }, |
| 523 | { threadId: target.input.threadId, ...armed }, |
| 524 | ), |
| 525 | ), |
| 526 | ), |
| 527 | }; |
| 528 | return { |
| 529 | ...commands, |
| 530 | regenerateTitle, |
| 531 | rowActionsAtom, |
| 532 | mcpSignedInAtom, |
| 533 | snapshotAtom: optimistic.snapshotAtom, |
| 534 | pendingThreadIdsAtom: optimistic.pendingThreadIdsAtom, |
| 535 | settle: optimistic.wrap(commands.settle, (thread, _input, now, accepted) => |
| 536 | !accepted && |
| 537 | (thread.pendingRuntimeRequest !== null || |
| 538 | ["preparing", "queued", "starting", "running", "waiting"].includes(thread.status)) |
| 539 | ? thread |
| 540 | : { |
| 541 | ...thread, |
| 542 | pendingRuntimeRequest: null, |
| 543 | settledOverride: "settled", |
| 544 | settledAt: thread.settledOverride === "settled" ? (thread.settledAt ?? now) : now, |
| 545 | unsettledAt: null, |
| 546 | activeOrderKey: null, |
| 547 | pinnedAt: null, |
| 548 | pinOrderKey: null, |
| 549 | snoozedAt: null, |
| 550 | snoozedUntil: null, |
| 551 | }, |
| 552 | ), |
| 553 | unsettle: optimistic.wrap(commands.unsettle, (thread, input, now) => ({ |
| 554 | ...thread, |
| 555 | settledOverride: input.reason === "user" ? "active" : null, |
| 556 | settledAt: null, |
| 557 | unsettledAt: |
| 558 | thread.settledOverride === "active" |
| 559 | ? (thread.unsettledAt ?? null) |
| 560 | : input.restoreUnsettledAt === undefined |
| 561 | ? now |
| 562 | : input.restoreUnsettledAt === null |
| 563 | ? null |
| 564 | : DateTime.makeUnsafe(input.restoreUnsettledAt), |
| 565 | })), |
| 566 | snooze: optimistic.wrap(commands.snooze, (thread, input, now, accepted) => |
| 567 | (!accepted && |
| 568 | ((thread.pendingRuntimeRequest !== null && |
| 569 | thread.pendingRuntimeRequest.kind !== "user_input") || |
| 570 | ["preparing", "queued", "starting"].includes(thread.status))) || |
| 571 | !(Date.parse(input.snoozedUntil) > DateTime.toEpochMillis(now)) |
| 572 | ? thread |
| 573 | : { |
| 574 | ...thread, |
| 575 | snoozedUntil: DateTime.makeUnsafe(input.snoozedUntil), |
| 576 | snoozedAt: now, |
| 577 | }, |
| 578 | ), |
| 579 | unsnooze: optimistic.wrap(commands.unsnooze, (thread) => ({ |
| 580 | ...thread, |
| 581 | snoozedUntil: null, |
| 582 | snoozedAt: null, |
| 583 | })), |
| 584 | setAutoSettle: optimistic.wrap(commands.setAutoSettle, (thread, input, now) => ({ |
| 585 | ...thread, |
| 586 | autoSettleDisabledAt: input.enabled ? null : (thread.autoSettleDisabledAt ?? now), |
| 587 | })), |
| 588 | pin: optimistic.wrap(commands.pin, (thread, input, now) => ({ |
| 589 | ...thread, |
| 590 | pinnedAt: thread.pinnedAt ?? now, |
| 591 | pinOrderKey: thread.pinnedAt == null ? (input.orderKey ?? null) : thread.pinOrderKey, |
| 592 | ...(thread.settledOverride === "settled" |
| 593 | ? { |
| 594 | settledOverride: "active" as const, |
| 595 | settledAt: null, |
| 596 | unsettledAt: now, |
| 597 | } |
| 598 | : {}), |
| 599 | snoozedUntil: null, |
| 600 | snoozedAt: null, |
| 601 | })), |
| 602 | unpin: optimistic.wrap(commands.unpin, (thread) => ({ |
| 603 | ...thread, |
| 604 | pinnedAt: null, |
| 605 | pinOrderKey: null, |
| 606 | })), |
| 607 | reorderPin: optimistic.wrap(commands.reorderPin, (thread, input) => ({ |
| 608 | ...thread, |
| 609 | pinOrderKey: input.orderKey, |
| 610 | })), |
| 611 | reorderActive: optimistic.wrap(commands.reorderActive, (thread, input) => ({ |
| 612 | ...thread, |
| 613 | activeOrderKey: input.orderKey, |
| 614 | })), |
| 615 | // Shown at once, so a composer that releases its accepted pick keeps showing it. |
| 616 | setModelSelection: optimistic.wrap(commands.setModelSelection, (thread, input) => ({ |
| 617 | ...thread, |
| 618 | modelSelection: input.modelSelection, |
| 619 | })), |
| 620 | }; |
| 621 | } |