@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 256 lines 9.47 KB
Raw
1 import type { ProviderApprovalDecision } from "@t3tools/contracts";
2 import * as Schema from "effect/Schema";
3
4 const NonEmptyString = Schema.String.check(Schema.isMinLength(1));
5
6 const MuseUsage = Schema.Struct({
7 inputTokens: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
8 outputTokens: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
9 cachedTokens: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
10 reasoningTokens: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
11 cacheReadTokens: Schema.optional(Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))),
12 cacheWriteTokens: Schema.optional(Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))),
13 });
14
15 export const MuseItem = Schema.Struct({
16 entryId: Schema.optional(Schema.String),
17 children: Schema.optional(
18 Schema.Array(
19 Schema.Struct({
20 childId: NonEmptyString,
21 attempt: Schema.Int,
22 status: Schema.String,
23 label: Schema.optional(Schema.String),
24 terminal: Schema.optional(Schema.String),
25 durationMs: Schema.optional(Schema.Finite),
26 /** Present on a failed attempt, even one whose `terminal` is `completed`. */
27 failureKind: Schema.optional(Schema.String),
28 failureReason: Schema.optional(Schema.String),
29 }),
30 ),
31 ),
32 finalSummary: Schema.optional(Schema.String),
33 itemId: NonEmptyString,
34 kind: NonEmptyString,
35 revision: Schema.Int.check(Schema.isGreaterThan(0)),
36 status: NonEmptyString,
37 turnId: Schema.optional(Schema.NullOr(Schema.String)),
38 text: Schema.optional(Schema.String),
39 summary: Schema.optional(Schema.Array(Schema.String)),
40 tool: Schema.optional(Schema.String),
41 args: Schema.optional(Schema.String),
42 visibleOutput: Schema.optional(Schema.String),
43 truncated: Schema.optional(Schema.Boolean),
44 failureReason: Schema.optional(Schema.String),
45 fallbackText: Schema.optional(Schema.String),
46 commandText: Schema.optional(Schema.String),
47 exitCode: Schema.optional(Schema.Int),
48 durationMs: Schema.optional(Schema.Finite),
49 subagentId: Schema.optional(Schema.String),
50 childSessionId: Schema.optional(Schema.String),
51 objective: Schema.optional(Schema.String),
52 role: Schema.optional(Schema.String),
53 controlStatus: Schema.optional(Schema.String),
54 result: Schema.optional(
55 Schema.Struct({ summary: Schema.String, text: Schema.optional(Schema.String) }),
56 ),
57 usage: Schema.optional(MuseUsage),
58 outcome: Schema.optional(Schema.String),
59 trigger: Schema.optional(Schema.String),
60 reason: Schema.optional(Schema.String),
61 });
62 export type MuseItem = typeof MuseItem.Type;
63
64 export const MuseSessionResult = Schema.Struct({
65 session: Schema.Struct({
66 sessionId: NonEmptyString,
67 modelId: Schema.optional(Schema.NullOr(Schema.String)),
68 activeTurnId: Schema.optional(Schema.NullOr(Schema.String)),
69 }),
70 history: Schema.optional(
71 Schema.Struct({
72 items: Schema.NullOr(Schema.Array(MuseItem)),
73 noneReason: Schema.optional(Schema.String),
74 snapshot: Schema.optional(
75 Schema.NullOr(
76 Schema.Struct({
77 schemaVersion: Schema.optional(Schema.Int),
78 state: Schema.Struct({ items: Schema.Array(MuseItem) }),
79 }),
80 ),
81 ),
82 }),
83 ),
84 });
85 export const MuseTurnStartResult = Schema.Struct({ turnId: NonEmptyString });
86
87 export const MuseApproval = Schema.Struct({
88 approvalId: NonEmptyString,
89 protectedWrite: Schema.optional(Schema.Boolean),
90 judgeEscalated: Schema.optional(Schema.Boolean),
91 sessionId: NonEmptyString,
92 turnId: Schema.optional(Schema.String),
93 itemId: Schema.optional(Schema.String),
94 toolName: Schema.optional(Schema.String),
95 rawArgs: Schema.optional(Schema.String),
96 /** Set when a workflow child asks; `turnId` is then the child's run, not a session turn. */
97 subagentOrigin: Schema.optional(Schema.Struct({ subagentId: NonEmptyString })),
98 currentRequirementId: Schema.Struct({ approvalId: NonEmptyString, sourceIndex: Schema.Int }),
99 availableChoices: Schema.Array(
100 Schema.Struct({
101 choiceId: NonEmptyString,
102 label: Schema.String,
103 decision: NonEmptyString,
104 scope: Schema.String,
105 }),
106 ),
107 subject: Schema.Struct({
108 kind: Schema.String,
109 access: Schema.optional(Schema.String),
110 command: Schema.optional(Schema.String),
111 path: Schema.optional(Schema.String),
112 host: Schema.optional(Schema.String),
113 target: Schema.optional(Schema.String),
114 }),
115 });
116 export type MuseApproval = typeof MuseApproval.Type;
117
118 export const MuseUserInput = Schema.Struct({
119 userInputId: NonEmptyString,
120 sessionId: NonEmptyString,
121 turnId: NonEmptyString,
122 questions: Schema.Array(
123 Schema.Struct({
124 id: NonEmptyString,
125 header: Schema.String,
126 question: Schema.String,
127 options: Schema.Array(
128 Schema.Struct({ label: Schema.String, description: Schema.optional(Schema.String) }),
129 ),
130 selection: Schema.Struct({
131 mode: Schema.Literals(["single", "multiple"]),
132 minSelections: Schema.optional(Schema.Int),
133 maxSelections: Schema.optional(Schema.Int),
134 }),
135 }),
136 ),
137 });
138 export type MuseUserInput = typeof MuseUserInput.Type;
139
140 export const MuseTokenUsageEvent = Schema.Struct({
141 turnId: NonEmptyString,
142 promptTokens: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
143 usage: MuseUsage,
144 });
145 export const MuseContextUsage = Schema.Struct({
146 usedTokens: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
147 windowTokens: Schema.optional(Schema.Int.check(Schema.isGreaterThan(0))),
148 });
149 export const MuseTodoList = Schema.Struct({
150 items: Schema.Array(Schema.Struct({ text: Schema.String, status: Schema.String })),
151 });
152 export const MuseTurnCompleted = Schema.Struct({
153 turnId: NonEmptyString,
154 terminal: NonEmptyString,
155 reason: Schema.optional(Schema.String),
156 error: Schema.optional(
157 Schema.Struct({
158 message: Schema.String,
159 quota: Schema.optional(Schema.Struct({ resetsAtMs: Schema.Finite })),
160 }),
161 ),
162 usage: Schema.optional(MuseUsage),
163 });
164 /** The latest instant a JavaScript Date holds; formatting a later one throws. */
165 export const LATEST_DATE_MS = 8.64e15;
166 const DateMillis = Schema.Finite.check(
167 Schema.isGreaterThan(0),
168 Schema.isLessThanOrEqualTo(LATEST_DATE_MS),
169 );
170 /** Latest response subscription windows; verbatim Meta percentages may exceed 100. */
171 export const MuseSubscriptionUsage = Schema.Struct({
172 observedAtMs: DateMillis,
173 tier: Schema.String,
174 window: Schema.Struct({
175 usedPercent: Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)),
176 windowDurationMins: Schema.Finite.check(Schema.isGreaterThan(0)),
177 resetsAtMs: DateMillis,
178 }),
179 weekly: Schema.Struct({
180 usedPercent: Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)),
181 resetsAtMs: DateMillis,
182 }),
183 });
184 export type MuseSubscriptionUsage = typeof MuseSubscriptionUsage.Type;
185 /** `turn/retryScheduled` carries `reason`; `turn/modelRetryScheduled` carries the HTTP failure. */
186 export const MuseRetryScheduled = Schema.Struct({
187 turnId: NonEmptyString,
188 attempt: Schema.Int.check(Schema.isGreaterThan(0)),
189 nextAttempt: Schema.Int.check(Schema.isGreaterThan(0)),
190 maxAttempts: Schema.Int.check(Schema.isGreaterThan(0)),
191 reason: Schema.optional(NonEmptyString),
192 errorKind: Schema.optional(Schema.String),
193 httpStatus: Schema.optional(Schema.Int),
194 retryDelayMs: Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)),
195 sourceRange: Schema.Struct({ first: Schema.Struct({ id: NonEmptyString }) }),
196 });
197 export const MuseCompactResult = Schema.Struct({
198 status: Schema.String,
199 reason: Schema.optional(Schema.String),
200 });
201
202 export const MuseItemEvent = Schema.Struct({ item: MuseItem });
203 export const MuseDelta = Schema.Struct({
204 itemId: NonEmptyString,
205 delta: Schema.String,
206 field: Schema.optional(Schema.String),
207 });
208
209 export function museApprovalDecision(
210 choice: Pick<MuseApproval["availableChoices"][number], "decision" | "scope">,
211 ): ProviderApprovalDecision | undefined {
212 switch (choice.decision) {
213 case "approved":
214 return "accept";
215 case "approvedForSession":
216 return "acceptForSession";
217 case "approvedPolicyAmendment":
218 return choice.scope === "session"
219 ? "acceptForSession"
220 : choice.scope === "localPersistent"
221 ? "acceptAlways"
222 : undefined;
223 case "denied":
224 case "deniedPolicyAmendment":
225 return "decline";
226 case "abort":
227 return "cancel";
228 default:
229 return undefined;
230 }
231 }
232
233 /** Shell approvals offer abort instead of denied; decline falls back to abort. */
234 export function museApprovalChoices(approval: Pick<MuseApproval, "availableChoices">) {
235 const choices = new Map<ProviderApprovalDecision, MuseApproval["availableChoices"][number]>();
236 const rank = (choice: MuseApproval["availableChoices"][number]) =>
237 (choice.scope === "once" ? 0 : choice.scope === "session" ? 2 : 4) +
238 (choice.decision.endsWith("PolicyAmendment") ? 1 : 0);
239 for (const choice of approval.availableChoices) {
240 const decision = museApprovalDecision(choice);
241 if (!decision) continue;
242 const previous = choices.get(decision);
243 if (!previous || rank(choice) < rank(previous)) choices.set(decision, choice);
244 }
245 const abort = choices.get("cancel");
246 if (!choices.has("decline") && abort) choices.set("decline", abort);
247 return choices;
248 }
249
250 export function museApprovalOptions(approval: Pick<MuseApproval, "availableChoices">) {
251 const choices = museApprovalChoices(approval);
252 const declineChoiceId = choices.get("decline")?.choiceId;
253 return [...choices]
254 .filter(([decision, choice]) => decision !== "cancel" || choice.choiceId !== declineChoiceId)
255 .map(([decision, choice]) => ({ decision, label: choice.label }));
256 }