The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { isClearComposerCommand, parseSideChatCommand } from "@t3tools/shared/composerTrigger"; |
| 2 | import { isUsageLimitsCommand } from "@t3tools/shared/usageLimits"; |
| 3 | |
| 4 | import { parseCodexFeedbackCommand } from "./threadFeedback.ts"; |
| 5 | |
| 6 | export type ComposerDispatchMode = "auto" | "queue" | "steer" | "restart"; |
| 7 | export type ActiveTurnComposerAction = Exclude<ComposerDispatchMode, "auto">; |
| 8 | |
| 9 | export function resolveComposerDispatchMode(input: { |
| 10 | readonly running: boolean; |
| 11 | readonly alternateModifier: boolean; |
| 12 | readonly activeTurnDefault?: ActiveTurnComposerAction; |
| 13 | /** False while the active turn cannot take steering input, such as an explicit /compact. */ |
| 14 | readonly canSteer?: boolean; |
| 15 | }): ComposerDispatchMode { |
| 16 | if (!input.running) return "auto"; |
| 17 | if (input.canSteer === false) return "queue"; |
| 18 | const defaultAction = input.activeTurnDefault ?? "steer"; |
| 19 | if (input.alternateModifier) return defaultAction === "queue" ? "steer" : "queue"; |
| 20 | return defaultAction; |
| 21 | } |
| 22 | |
| 23 | // Automatic compaction takes steers only where the provider says a steer leaves it running. |
| 24 | export function compactionBlocksSteering(input: { |
| 25 | readonly compactCommandIsActive: boolean; |
| 26 | readonly compactionRunning: boolean; |
| 27 | readonly steersDuringCompaction: boolean; |
| 28 | readonly run: { readonly userMessageId: string } | undefined; |
| 29 | readonly messages: |
| 30 | | ReadonlyArray<{ |
| 31 | readonly id: string; |
| 32 | readonly text: string; |
| 33 | readonly attachments?: ReadonlyArray<unknown>; |
| 34 | }> |
| 35 | | undefined; |
| 36 | }): boolean { |
| 37 | if (input.compactCommandIsActive) return true; |
| 38 | if (!input.compactionRunning) return false; |
| 39 | if (!input.steersDuringCompaction) return true; |
| 40 | if (input.run === undefined) return false; |
| 41 | const message = input.messages?.find((message) => message.id === input.run?.userMessageId); |
| 42 | return ( |
| 43 | (message?.attachments?.length ?? 0) === 0 && |
| 44 | /^\/compact(?:\s|$)/i.test(message?.text.trim() ?? "") |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | export function alternateComposerDispatchAction( |
| 49 | activeTurnDefault?: ActiveTurnComposerAction, |
| 50 | ): ActiveTurnComposerAction { |
| 51 | return resolveComposerDispatchMode({ |
| 52 | running: true, |
| 53 | alternateModifier: true, |
| 54 | ...(activeTurnDefault === undefined ? {} : { activeTurnDefault }), |
| 55 | }) as ActiveTurnComposerAction; |
| 56 | } |
| 57 | |
| 58 | /** The composer runs these drafts itself, so they never queue for or steer a running turn. */ |
| 59 | export function composerRunsDraftLocally(input: { |
| 60 | readonly text: string; |
| 61 | readonly holdsOnlyText: boolean; |
| 62 | readonly sideChat: boolean; |
| 63 | readonly usageLimits: boolean; |
| 64 | readonly codexFeedback: boolean; |
| 65 | readonly freshSession: boolean; |
| 66 | }): boolean { |
| 67 | if (input.sideChat && parseSideChatCommand(input.text) !== null) return true; |
| 68 | if (!input.holdsOnlyText) return false; |
| 69 | return ( |
| 70 | (input.usageLimits && isUsageLimitsCommand(input.text)) || |
| 71 | (input.codexFeedback && parseCodexFeedbackCommand(input.text) !== null) || |
| 72 | (input.freshSession && isClearComposerCommand(input.text)) |
| 73 | ); |
| 74 | } |