Skip to content
Source
Sign inSign up

composerDispatch.ts

51 lines2.0 KB

1export type ComposerDispatchMode = "auto" | "queue" | "steer" | "restart";
2export type ActiveTurnComposerAction = Exclude<ComposerDispatchMode, "auto">;
3​
4export function resolveComposerDispatchMode(input: {
5 readonly running: boolean;
6 readonly alternateModifier: boolean;
7 readonly activeTurnDefault?: ActiveTurnComposerAction;
8 /** False while the active turn cannot take steering input, such as an explicit /compact. */
9 readonly canSteer?: boolean;
10}): ComposerDispatchMode {
11 if (!input.running) return "auto";
12 if (input.canSteer === false) return "queue";
13 const defaultAction = input.activeTurnDefault ?? "steer";
14 if (input.alternateModifier) return defaultAction === "queue" ? "steer" : "queue";
15 return defaultAction;
17​
18// Automatic compaction takes steers only where the provider says a steer leaves it running.
19export function compactionBlocksSteering(input: {
20 readonly compactCommandIsActive: boolean;
21 readonly compactionRunning: boolean;
22 readonly steersDuringCompaction: boolean;
23 readonly run: { readonly userMessageId: string } | undefined;
24 readonly messages:
25 | ReadonlyArray<{
26 readonly id: string;
27 readonly text: string;
28 readonly attachments?: ReadonlyArray<unknown>;
29 }>
30 | undefined;
31}): boolean {
32 if (input.compactCommandIsActive) return true;
33 if (!input.compactionRunning) return false;
34 if (!input.steersDuringCompaction) return true;
35 if (input.run === undefined) return false;
36 const message = input.messages?.find((message) => message.id === input.run?.userMessageId);
37 return (
38 (message?.attachments?.length ?? 0) === 0 &&
39 /^\/compact(?:\s|$)/i.test(message?.text.trim() ?? "")
40 );
42​
43export function alternateComposerDispatchAction(
44 activeTurnDefault?: ActiveTurnComposerAction,
45): ActiveTurnComposerAction {
46 return resolveComposerDispatchMode({
47 running: true,
48 alternateModifier: true,
49 ...(activeTurnDefault === undefined ? {} : { activeTurnDefault }),
50 }) as ActiveTurnComposerAction;