Open History to see the last commit
1import type {
2 CanonicalItemType,
3 CanonicalRequestType,
4 ProviderApprovalDecision,
5} from "@t3tools/contracts";
6import * as Schema from "effect/Schema";
7
8const NonEmptyString = Schema.String.check(Schema.isMinLength(1));
9export const MuseResumeCursor = Schema.Struct({ sessionId: NonEmptyString });
10
11export const MuseUsage = Schema.Struct({
12 inputTokens: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
13 outputTokens: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
14 cachedTokens: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
15 reasoningTokens: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
16 cacheReadTokens: Schema.optional(Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))),
17 cacheWriteTokens: Schema.optional(Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))),
18});
19
20export const MuseItem = Schema.Struct({
21 itemId: NonEmptyString,
22 kind: NonEmptyString,
23 revision: Schema.Int.check(Schema.isGreaterThan(0)),
24 status: NonEmptyString,
25 turnId: Schema.optional(Schema.NullOr(Schema.String)),
26 text: Schema.optional(Schema.String),
27 summary: Schema.optional(Schema.Array(Schema.String)),
28 tool: Schema.optional(Schema.String),
29 args: Schema.optional(Schema.String),
30 visibleOutput: Schema.optional(Schema.String),
31 truncated: Schema.optional(Schema.Boolean),
32 failureReason: Schema.optional(Schema.String),
33 fallbackText: Schema.optional(Schema.String),
34 commandText: Schema.optional(Schema.String),
35 exitCode: Schema.optional(Schema.Int),
36 durationMs: Schema.optional(Schema.Finite),
37 subagentId: Schema.optional(Schema.String),
38 childSessionId: Schema.optional(Schema.String),
39 objective: Schema.optional(Schema.String),
40 role: Schema.optional(Schema.String),
41 controlStatus: Schema.optional(Schema.String),
42 result: Schema.optional(
43 Schema.Struct({ summary: Schema.String, text: Schema.optional(Schema.String) }),
44 ),
45 usage: Schema.optional(MuseUsage),
46 outcome: Schema.optional(Schema.String),
47 trigger: Schema.optional(Schema.String),
48 reason: Schema.optional(Schema.String),
49});
50export type MuseItem = typeof MuseItem.Type;
51
52export const MuseSessionResult = Schema.Struct({
53 session: Schema.Struct({
54 sessionId: NonEmptyString,
55 modelId: Schema.optional(Schema.NullOr(Schema.String)),
56 activeTurnId: Schema.optional(Schema.NullOr(Schema.String)),
57 }),
58 history: Schema.optional(
59 Schema.Struct({
60 items: Schema.NullOr(Schema.Array(MuseItem)),
61 snapshot: Schema.optional(
62 Schema.NullOr(
63 Schema.Struct({
64 schemaVersion: Schema.optional(Schema.Int),
65 state: Schema.Struct({ items: Schema.Array(MuseItem) }),
66 }),
67 ),
68 ),
69 }),
70 ),
71});
72export const MuseViewPage = Schema.Struct({
73 events: Schema.Array(
74 Schema.Struct({
75 method: Schema.String,
76 params: Schema.Record(Schema.String, Schema.Unknown),
77 }),
78 ),
79 nextCursor: Schema.NullOr(NonEmptyString),
80});
81export const MuseTurnStartResult = Schema.Struct({ turnId: NonEmptyString });
82
83export const MuseApproval = Schema.Struct({
84 approvalId: NonEmptyString,
85 protectedWrite: Schema.optional(Schema.Boolean),
86 judgeEscalated: Schema.optional(Schema.Boolean),
87 sessionId: NonEmptyString,
88 turnId: Schema.optional(Schema.String),
89 itemId: Schema.optional(Schema.String),
90 toolName: Schema.optional(Schema.String),
91 rawArgs: Schema.optional(Schema.String),
92 currentRequirementId: Schema.Struct({ approvalId: NonEmptyString, sourceIndex: Schema.Int }),
93 availableChoices: Schema.Array(
94 Schema.Struct({
95 choiceId: NonEmptyString,
96 label: Schema.String,
97 decision: NonEmptyString,
98 scope: Schema.String,
99 }),
100 ),
101 subject: Schema.Struct({
102 kind: Schema.String,
103 access: Schema.optional(Schema.String),
104 command: Schema.optional(Schema.String),
105 path: Schema.optional(Schema.String),
106 }),
107});
108export type MuseApproval = typeof MuseApproval.Type;
109
110export const MuseUserInput = Schema.Struct({
111 userInputId: NonEmptyString,
112 sessionId: NonEmptyString,
113 turnId: NonEmptyString,
114 questions: Schema.Array(
115 Schema.Struct({
116 id: NonEmptyString,
117 header: Schema.String,
118 question: Schema.String,
119 options: Schema.Array(
120 Schema.Struct({ label: Schema.String, description: Schema.optional(Schema.String) }),
121 ),
122 selection: Schema.Struct({
123 mode: Schema.Literals(["single", "multiple"]),
124 minSelections: Schema.optional(Schema.Int),
125 maxSelections: Schema.optional(Schema.Int),
126 }),
127 }),
128 ),
129});
130export type MuseUserInput = typeof MuseUserInput.Type;
131
132export const MuseUserInputSettled = Schema.Struct({
133 userInputId: NonEmptyString,
134 answers: Schema.Array(
135 Schema.Struct({
136 questionId: NonEmptyString,
137 selectedLabel: Schema.optional(Schema.String),
138 selectedLabels: Schema.optional(Schema.Array(Schema.String)),
139 freeText: Schema.optional(Schema.String),
140 note: Schema.optional(Schema.String),
141 }),
142 ),
143});
144
145export const MuseTokenUsageEvent = Schema.Struct({
146 turnId: NonEmptyString,
147 promptTokens: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
148 usage: MuseUsage,
149});
150export const MuseContextUsage = Schema.Struct({
151 usedTokens: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
152 windowTokens: Schema.optional(Schema.Int.check(Schema.isGreaterThan(0))),
153});
154export const MuseTodoList = Schema.Struct({
155 items: Schema.Array(Schema.Struct({ text: Schema.String, status: Schema.String })),
156});
157export const MuseTurnCompleted = Schema.Struct({
158 turnId: NonEmptyString,
159 terminal: NonEmptyString,
160 reason: Schema.optional(Schema.String),
161 error: Schema.optional(Schema.Struct({ message: Schema.String })),
162 usage: Schema.optional(MuseUsage),
163});
164export const MuseTurnRetryScheduled = Schema.Struct({
165 turnId: NonEmptyString,
166 nextAttempt: Schema.Int.check(Schema.isGreaterThan(0)),
167 maxAttempts: Schema.Int.check(Schema.isGreaterThan(0)),
168 reason: NonEmptyString,
169 retryDelayMs: Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)),
170});
171export const MuseCompactResult = Schema.Struct({
172 status: Schema.String,
173 reason: Schema.optional(Schema.String),
174});
175
176export const MuseItemEvent = Schema.Struct({ item: MuseItem });
177export const MuseDelta = Schema.Struct({
178 itemId: NonEmptyString,
179 delta: Schema.String,
180 field: Schema.optional(Schema.String),
181});
182
183export function museItemType(item: MuseItem): CanonicalItemType {
184 switch (item.kind) {
185 case "agentMessage":
186 return "assistant_message";
187 case "userMessage":
188 return "user_message";
189 case "reasoning":
190 return "reasoning";
191 case "compaction":
192 return "context_compaction";
193 case "userShell":
194 return "command_execution";
195 case "subagent":
196 return "collab_agent_tool_call";
197 case "toolCall": {
198 const tool = item.tool?.toLowerCase() ?? "";
199 if (/shell|bash|exec_command/.test(tool)) return "command_execution";
200 if (/write|edit|patch/.test(tool)) return "file_change";
201 if (/web|browse|fetch/.test(tool)) return "web_search";
202 if (/image/.test(tool)) return "image_view";
203 return "dynamic_tool_call";
204 }
205 default:
206 // Generic tool activity is rendered by every client; canonical "unknown"
207 // items are filtered out of the work log before they reach a client.
208 return "dynamic_tool_call";
209 }
210}
211
212export function museRequestType(approval: MuseApproval): CanonicalRequestType {
213 if (approval.subject.kind === "shell") return "command_execution_approval";
214 if (approval.subject.kind === "fileAccess") {
215 return approval.subject.access === "read" ? "file_read_approval" : "file_change_approval";
216 }
217 return "dynamic_tool_call";
218}
219
220export function museApprovalDecision(
221 choice: Pick<MuseApproval["availableChoices"][number], "decision" | "scope">,
222): ProviderApprovalDecision | undefined {
223 switch (choice.decision) {
224 case "approved":
225 return "accept";
226 case "approvedForSession":
227 return "acceptForSession";
228 case "approvedPolicyAmendment":
229 return choice.scope === "session"
230 ? "acceptForSession"
231 : choice.scope === "localPersistent"
232 ? "acceptAlways"
233 : undefined;
234 case "denied":
235 case "deniedPolicyAmendment":
236 return "decline";
237 case "abort":
238 return "cancel";
239 default:
240 return undefined;
241 }
242}
243
244/** The shared decision protocol exposes one native choice per canonical action. */
245export function museApprovalChoices(approval: MuseApproval) {
246 const choices = new Map<ProviderApprovalDecision, MuseApproval["availableChoices"][number]>();
247 const rank = (choice: MuseApproval["availableChoices"][number]) =>
248 (choice.scope === "once" ? 0 : choice.scope === "session" ? 2 : 4) +
249 (choice.decision.endsWith("PolicyAmendment") ? 1 : 0);
250 for (const choice of approval.availableChoices) {
251 const decision = museApprovalDecision(choice);
252 if (!decision) continue;
253 const previous = choices.get(decision);
254 if (!previous || rank(choice) < rank(previous)) choices.set(decision, choice);
255 }
256 return choices;
257}