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

museProtocol.ts

226 lines8.2 KB

1import type { ProviderApprovalDecision } from "@t3tools/contracts";
2import * as Schema from "effect/Schema";
3​
4const NonEmptyString = Schema.String.check(Schema.isMinLength(1));
5​
6const 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​
15export const MuseItem = Schema.Struct({
16 /** `workflow`: the launched entry's name. */
17 entryId: Schema.optional(Schema.String),
18 /** `workflow`: each child agent's latest attempt, re-sent whole on every change. */
19 children: Schema.optional(
20 Schema.Array(
21 Schema.Struct({
22 childId: NonEmptyString,
23 attempt: Schema.Int,
24 status: Schema.String,
25 label: Schema.optional(Schema.String),
26 terminal: Schema.optional(Schema.String),
27 durationMs: Schema.optional(Schema.Finite),
28 failureReason: Schema.optional(Schema.String),
29 }),
30 ),
31 ),
32 itemId: NonEmptyString,
33 kind: NonEmptyString,
34 revision: Schema.Int.check(Schema.isGreaterThan(0)),
35 status: NonEmptyString,
36 turnId: Schema.optional(Schema.NullOr(Schema.String)),
37 text: Schema.optional(Schema.String),
38 summary: Schema.optional(Schema.Array(Schema.String)),
39 tool: Schema.optional(Schema.String),
40 args: Schema.optional(Schema.String),
41 visibleOutput: Schema.optional(Schema.String),
42 truncated: Schema.optional(Schema.Boolean),
43 failureReason: Schema.optional(Schema.String),
44 fallbackText: Schema.optional(Schema.String),
45 commandText: Schema.optional(Schema.String),
46 exitCode: Schema.optional(Schema.Int),
47 durationMs: Schema.optional(Schema.Finite),
48 subagentId: Schema.optional(Schema.String),
49 childSessionId: Schema.optional(Schema.String),
50 objective: Schema.optional(Schema.String),
51 role: Schema.optional(Schema.String),
52 controlStatus: Schema.optional(Schema.String),
53 result: Schema.optional(
54 Schema.Struct({ summary: Schema.String, text: Schema.optional(Schema.String) }),
55 ),
56 usage: Schema.optional(MuseUsage),
57 outcome: Schema.optional(Schema.String),
58 trigger: Schema.optional(Schema.String),
59 reason: Schema.optional(Schema.String),
60});
61export type MuseItem = typeof MuseItem.Type;
62​
63export const MuseSessionResult = Schema.Struct({
64 session: Schema.Struct({
65 sessionId: NonEmptyString,
66 modelId: Schema.optional(Schema.NullOr(Schema.String)),
67 activeTurnId: Schema.optional(Schema.NullOr(Schema.String)),
68 }),
69 history: Schema.optional(
70 Schema.Struct({
71 items: Schema.NullOr(Schema.Array(MuseItem)),
72 snapshot: Schema.optional(
73 Schema.NullOr(
74 Schema.Struct({
75 schemaVersion: Schema.optional(Schema.Int),
76 state: Schema.Struct({ items: Schema.Array(MuseItem) }),
77 }),
78 ),
79 ),
80 }),
81 ),
82});
83export const MuseTurnStartResult = Schema.Struct({ turnId: NonEmptyString });
84​
85export const MuseApproval = Schema.Struct({
86 approvalId: NonEmptyString,
87 protectedWrite: Schema.optional(Schema.Boolean),
88 judgeEscalated: Schema.optional(Schema.Boolean),
89 sessionId: NonEmptyString,
90 turnId: Schema.optional(Schema.String),
91 itemId: Schema.optional(Schema.String),
92 toolName: Schema.optional(Schema.String),
93 rawArgs: Schema.optional(Schema.String),
94 currentRequirementId: Schema.Struct({ approvalId: NonEmptyString, sourceIndex: Schema.Int }),
95 availableChoices: Schema.Array(
96 Schema.Struct({
97 choiceId: NonEmptyString,
98 label: Schema.String,
99 decision: NonEmptyString,
100 scope: Schema.String,
101 }),
102 ),
103 subject: Schema.Struct({
104 kind: Schema.String,
105 access: Schema.optional(Schema.String),
106 command: Schema.optional(Schema.String),
107 path: Schema.optional(Schema.String),
108 host: Schema.optional(Schema.String),
109 target: Schema.optional(Schema.String),
110 }),
111});
112export type MuseApproval = typeof MuseApproval.Type;
113​
114export const MuseUserInput = Schema.Struct({
115 userInputId: NonEmptyString,
116 sessionId: NonEmptyString,
117 turnId: NonEmptyString,
118 questions: Schema.Array(
119 Schema.Struct({
120 id: NonEmptyString,
121 header: Schema.String,
122 question: Schema.String,
123 options: Schema.Array(
124 Schema.Struct({ label: Schema.String, description: Schema.optional(Schema.String) }),
125 ),
126 selection: Schema.Struct({
127 mode: Schema.Literals(["single", "multiple"]),
128 minSelections: Schema.optional(Schema.Int),
129 maxSelections: Schema.optional(Schema.Int),
130 }),
131 }),
132 ),
133});
134export type MuseUserInput = typeof MuseUserInput.Type;
135​
136export const MuseTokenUsageEvent = Schema.Struct({
137 turnId: NonEmptyString,
138 promptTokens: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
139 usage: MuseUsage,
140});
141export const MuseContextUsage = Schema.Struct({
142 usedTokens: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
143 windowTokens: Schema.optional(Schema.Int.check(Schema.isGreaterThan(0))),
144});
145export const MuseTodoList = Schema.Struct({
146 items: Schema.Array(Schema.Struct({ text: Schema.String, status: Schema.String })),
147});
148export const MuseTurnCompleted = Schema.Struct({
149 turnId: NonEmptyString,
150 terminal: NonEmptyString,
151 reason: Schema.optional(Schema.String),
152 error: Schema.optional(Schema.Struct({ message: Schema.String })),
153 usage: Schema.optional(MuseUsage),
154});
155export const MuseTurnRetryScheduled = Schema.Struct({
156 turnId: NonEmptyString,
157 nextAttempt: Schema.Int.check(Schema.isGreaterThan(0)),
158 maxAttempts: Schema.Int.check(Schema.isGreaterThan(0)),
159 reason: NonEmptyString,
160 retryDelayMs: Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)),
161});
162export const MuseCompactResult = Schema.Struct({
163 status: Schema.String,
164 reason: Schema.optional(Schema.String),
165});
166​
167export const MuseItemEvent = Schema.Struct({ item: MuseItem });
168export const MuseDelta = Schema.Struct({
169 itemId: NonEmptyString,
170 delta: Schema.String,
171 field: Schema.optional(Schema.String),
172});
173​
174/** The T3 decision for one native Muse choice, or none when T3 has no equivalent. */
175export function museApprovalDecision(
176 choice: Pick<MuseApproval["availableChoices"][number], "decision" | "scope">,
177): ProviderApprovalDecision | undefined {
178 switch (choice.decision) {
179 case "approved":
180 return "accept";
181 case "approvedForSession":
182 return "acceptForSession";
183 case "approvedPolicyAmendment":
184 return choice.scope === "session"
185 ? "acceptForSession"
186 : choice.scope === "localPersistent"
187 ? "acceptAlways"
188 : undefined;
189 case "denied":
190 case "deniedPolicyAmendment":
191 return "decline";
192 case "abort":
193 return "cancel";
194 default:
195 return undefined;
196 }
198​
199/**
200 * Maps each canonical decision to one native choice. Muse shell approvals offer
201 * no `denied` choice, only `abort` ("Reject"), so `decline` falls back to it.
202 */
203export function museApprovalChoices(approval: Pick<MuseApproval, "availableChoices">) {
204 const choices = new Map<ProviderApprovalDecision, MuseApproval["availableChoices"][number]>();
205 const rank = (choice: MuseApproval["availableChoices"][number]) =>
206 (choice.scope === "once" ? 0 : choice.scope === "session" ? 2 : 4) +
207 (choice.decision.endsWith("PolicyAmendment") ? 1 : 0);
208 for (const choice of approval.availableChoices) {
209 const decision = museApprovalDecision(choice);
210 if (!decision) continue;
211 const previous = choices.get(decision);
212 if (!previous || rank(choice) < rank(previous)) choices.set(decision, choice);
213 }
214 const abort = choices.get("cancel");
215 if (!choices.has("decline") && abort) choices.set("decline", abort);
216 return choices;
218​
219/** The approval buttons to show: one per native choice, preferring `decline` over `cancel`. */
220export function museApprovalOptions(approval: Pick<MuseApproval, "availableChoices">) {
221 const choices = museApprovalChoices(approval);
222 const declineChoiceId = choices.get("decline")?.choiceId;
223 return [...choices]
224 .filter(([decision, choice]) => decision !== "cancel" || choice.choiceId !== declineChoiceId)
225 .map(([decision, choice]) => ({ decision, label: choice.label }));