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

DispatchModeLimit.ts

45 lines1.5 KB

1import type { ProviderInteractionMode, RuntimeMode, ThreadId } from "@t3tools/contracts";
2import * as Context from "effect/Context";
3import type * as Ref from "effect/Ref";
4​
5export interface DispatchModes {
6 readonly runtimeMode: RuntimeMode;
7 readonly interactionMode: ProviderInteractionMode;
8}
9​
10export interface DispatchModeRefusal extends DispatchModes {
11 readonly threadId: ThreadId;
12 readonly mode: "runtime" | "interaction";
14​
15/**
16 * A caller checks its target's modes before dispatching, but the target's user can raise them
17 * meanwhile; the orchestrator re-checks this limit inside the thread's command lock, where nothing
18 * else can change them.
19 */
20export interface DispatchModeLimitValue extends DispatchModes {
21 readonly refused?: Ref.Ref<DispatchModeRefusal | undefined>;
23​
24export const DispatchModeLimit = Context.Reference<DispatchModeLimitValue | undefined>(
25 "t3/orchestration-v2/DispatchModeLimit",
26 { defaultValue: () => undefined },
27);
28​
29const runtimeModeRank: Record<RuntimeMode, number> = {
30 "approval-required": 0,
31 "auto-accept-edits": 1,
32 auto: 2,
33 "full-access": 3,
34};
35const interactionModeRank: Record<ProviderInteractionMode, number> = { plan: 0, default: 1 };
36​
37export const exceededDispatchModeLimit = (
38 limit: DispatchModes,
39 modes: DispatchModes,
40): "runtime" | "interaction" | undefined =>
41 runtimeModeRank[modes.runtimeMode] > runtimeModeRank[limit.runtimeMode]
42 ? "runtime"
43 : interactionModeRank[modes.interactionMode] > interactionModeRank[limit.interactionMode]
44 ? "interaction"
45 : undefined;