Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadCommands.ts

685 lines23.7 KB

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