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

protocol.ts

228 lines8.4 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 /** Set when a workflow child asks; `turnId` is then the child's run, not a session turn. */
95 subagentOrigin: Schema.optional(Schema.Struct({ subagentId: NonEmptyString })),
96 currentRequirementId: Schema.Struct({ approvalId: NonEmptyString, sourceIndex: Schema.Int }),
97 availableChoices: Schema.Array(
98 Schema.Struct({
99 choiceId: NonEmptyString,
100 label: Schema.String,
101 decision: NonEmptyString,
102 scope: Schema.String,
103 }),
104 ),
105 subject: Schema.Struct({
106 kind: Schema.String,
107 access: Schema.optional(Schema.String),
108 command: Schema.optional(Schema.String),
109 path: Schema.optional(Schema.String),
110 host: Schema.optional(Schema.String),
111 target: Schema.optional(Schema.String),
112 }),
113});
114export type MuseApproval = typeof MuseApproval.Type;
115​
116export const MuseUserInput = Schema.Struct({
117 userInputId: NonEmptyString,
118 sessionId: NonEmptyString,
119 turnId: NonEmptyString,
120 questions: Schema.Array(
121 Schema.Struct({
122 id: NonEmptyString,
123 header: Schema.String,
124 question: Schema.String,
125 options: Schema.Array(
126 Schema.Struct({ label: Schema.String, description: Schema.optional(Schema.String) }),
127 ),
128 selection: Schema.Struct({
129 mode: Schema.Literals(["single", "multiple"]),
130 minSelections: Schema.optional(Schema.Int),
131 maxSelections: Schema.optional(Schema.Int),
132 }),
133 }),
134 ),
135});
136export type MuseUserInput = typeof MuseUserInput.Type;
137​
138export const MuseTokenUsageEvent = Schema.Struct({
139 turnId: NonEmptyString,
140 promptTokens: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
141 usage: MuseUsage,
142});
143export const MuseContextUsage = Schema.Struct({
144 usedTokens: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
145 windowTokens: Schema.optional(Schema.Int.check(Schema.isGreaterThan(0))),
146});
147export const MuseTodoList = Schema.Struct({
148 items: Schema.Array(Schema.Struct({ text: Schema.String, status: Schema.String })),
149});
150export const MuseTurnCompleted = Schema.Struct({
151 turnId: NonEmptyString,
152 terminal: NonEmptyString,
153 reason: Schema.optional(Schema.String),
154 error: Schema.optional(Schema.Struct({ message: Schema.String })),
155 usage: Schema.optional(MuseUsage),
156});
157export const MuseTurnRetryScheduled = Schema.Struct({
158 turnId: NonEmptyString,
159 nextAttempt: Schema.Int.check(Schema.isGreaterThan(0)),
160 maxAttempts: Schema.Int.check(Schema.isGreaterThan(0)),
161 reason: NonEmptyString,
162 retryDelayMs: Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)),
163});
164export const MuseCompactResult = Schema.Struct({
165 status: Schema.String,
166 reason: Schema.optional(Schema.String),
167});
168​
169export const MuseItemEvent = Schema.Struct({ item: MuseItem });
170export const MuseDelta = Schema.Struct({
171 itemId: NonEmptyString,
172 delta: Schema.String,
173 field: Schema.optional(Schema.String),
174});
175​
176/** The T3 decision for one native Muse choice, or none when T3 has no equivalent. */
177export function museApprovalDecision(
178 choice: Pick<MuseApproval["availableChoices"][number], "decision" | "scope">,
179): ProviderApprovalDecision | undefined {
180 switch (choice.decision) {
181 case "approved":
182 return "accept";
183 case "approvedForSession":
184 return "acceptForSession";
185 case "approvedPolicyAmendment":
186 return choice.scope === "session"
187 ? "acceptForSession"
188 : choice.scope === "localPersistent"
189 ? "acceptAlways"
190 : undefined;
191 case "denied":
192 case "deniedPolicyAmendment":
193 return "decline";
194 case "abort":
195 return "cancel";
196 default:
197 return undefined;
198 }
200​
201/**
202 * Maps each canonical decision to one native choice. Muse shell approvals offer
203 * no `denied` choice, only `abort` ("Reject"), so `decline` falls back to it.
204 */
205export function museApprovalChoices(approval: Pick<MuseApproval, "availableChoices">) {
206 const choices = new Map<ProviderApprovalDecision, MuseApproval["availableChoices"][number]>();
207 const rank = (choice: MuseApproval["availableChoices"][number]) =>
208 (choice.scope === "once" ? 0 : choice.scope === "session" ? 2 : 4) +
209 (choice.decision.endsWith("PolicyAmendment") ? 1 : 0);
210 for (const choice of approval.availableChoices) {
211 const decision = museApprovalDecision(choice);
212 if (!decision) continue;
213 const previous = choices.get(decision);
214 if (!previous || rank(choice) < rank(previous)) choices.set(decision, choice);
215 }
216 const abort = choices.get("cancel");
217 if (!choices.has("decline") && abort) choices.set("decline", abort);
218 return choices;
220​
221/** The approval buttons to show: one per native choice, preferring `decline` over `cancel`. */
222export function museApprovalOptions(approval: Pick<MuseApproval, "availableChoices">) {
223 const choices = museApprovalChoices(approval);
224 const declineChoiceId = choices.get("decline")?.choiceId;
225 return [...choices]
226 .filter(([decision, choice]) => decision !== "cancel" || choice.choiceId !== declineChoiceId)
227 .map(([decision, choice]) => ({ decision, label: choice.label }));