Open History to see the last commit
1import { AgentFeedbackKind, AgentFeedbackTrace } from "@t3tools/contracts";
2import { resolveT3McpToolId } from "@t3tools/shared/t3McpToolPresentation";
3import * as Effect from "effect/Effect";
4import * as Schema from "effect/Schema";
5import * as Struct from "effect/Struct";
6
7// Code points, as the scrubber caps them, so a valid input stays a valid report after redaction.
8const FeedbackText = Schema.String.check(Schema.isMaxCodePoints(300));
9
10/** Turns the outline of a requested report covers at least. */
11export const FEEDBACK_TRACE_TURNS = 5;
12
13const reportFields = {
14 kind: AgentFeedbackKind,
15 subject: Schema.String.annotate({ description: "T3 tool name, `t3` command, or `general`." }),
16 expected: FeedbackText,
17 actual: FeedbackText,
18 workaround: FeedbackText.annotate({ description: "What you did instead, or empty." }),
19 // Annotate before the default: an annotation after it never reaches the JSON schema.
20 userRequested: Schema.Boolean.annotate({ description: "The user asked for this report." }).pipe(
21 Schema.withDecodingDefaultKey(Effect.succeed(false)),
22 ),
23};
24
25export const FeedbackInput = Schema.Struct({
26 ...reportFields,
27 turns: Schema.Int.check(Schema.isGreaterThanOrEqualTo(1))
28 .annotate({
29 description: "How many recent turns, counting this one, the problem spans.",
30 })
31 .pipe(Schema.withDecodingDefaultKey(Effect.succeed(1))),
32});
33
34export const FeedbackTrace = AgentFeedbackTrace;
35export type FeedbackTrace = AgentFeedbackTrace;
36
37const storedFields = {
38 id: Schema.String,
39 threadId: Schema.NullOr(Schema.String),
40 createdAt: Schema.String,
41 source: Schema.Literals(["mcp", "cli"]),
42 sent: Schema.Boolean,
43};
44
45export const FeedbackReport = Schema.Struct({
46 ...reportFields,
47 ...storedFields,
48 trace: Schema.NullOr(FeedbackTrace),
49});
50
51/** What `t3_feedback` answers: the report, with only the size of what its trace attached. */
52export const FeedbackSubmitResult = Schema.Struct({
53 ...reportFields,
54 ...storedFields,
55 trace: Schema.NullOr(FeedbackTrace.mapFields(Struct.omit(["entries"]))),
56});
57
58export const FEEDBACK_AWAITING_APPROVAL_NOTE =
59 "The user sees this report in a card and chooses there whether to send it and whether to include the conversation. Nothing is sent until they do, and you will not learn their choice. Do not wait for it or send it another way. Call again only if the user asks to change it; that shows a new card.";
60
61/** What `t3_feedback` answers for a requested report: shown to the user, not yet sent. */
62export const FeedbackAwaitingApproval = Schema.Struct({
63 status: Schema.Literal("awaiting_approval"),
64 note: Schema.String,
65});
66export type FeedbackAwaitingApproval = typeof FeedbackAwaitingApproval.Type;
67
68export const FEEDBACK_OFF_MESSAGE =
69 "Agent feedback is off. Turn it on in T3 Code under Settings > Labs, then try again.";
70
71export class FeedbackOffError extends Schema.TaggedError<FeedbackOffError>()(
72 "FeedbackOffError",
73 {},
74) {
75 override get message() {
76 return FEEDBACK_OFF_MESSAGE;
77 }
78}
79
80export type FeedbackSource = "mcp" | "cli";
81
82// Requested reports count when their card is shown, unprompted ones when stored.
83export const MAX_REPORTS_PER_TURN = 3;
84// Reports from no turn, such as `t3 feedback`, have no turn to count in.
85export const MAX_REPORTS_OUTSIDE_TURNS_PER_HOUR = 10;
86
87// One machine sends a problem (a subject and the failed call it follows) once in this long, so a
88// day's reports of a problem count machines. Repeats are kept here and not sent.
89export const REPEAT_REPORT_WINDOW_HOURS = 24;
90
91export class FeedbackLimitError extends Schema.TaggedError<FeedbackLimitError>()(
92 "FeedbackLimitError",
93 { scope: Schema.Literals(["turn", "hour", "cards"]) },
94) {
95 override get message() {
96 switch (this.scope) {
97 case "turn":
98 return `This turn already sent ${MAX_REPORTS_PER_TURN} agent feedback reports; nothing was sent.`;
99 case "cards":
100 return `This turn already showed the user ${MAX_REPORTS_PER_TURN} agent feedback reports to approve; nothing was shown.`;
101 case "hour":
102 return `This machine already sent ${MAX_REPORTS_OUTSIDE_TURNS_PER_HOUR} agent feedback reports outside a turn in the last hour; nothing was sent.`;
103 }
104 }
105}
106
107export class FeedbackNotRequestedError extends Schema.TaggedError<FeedbackNotRequestedError>()(
108 "FeedbackNotRequestedError",
109 { source: Schema.Literals(["mcp", "cli"]) },
110) {
111 override get message() {
112 return this.source === "cli"
113 ? "Agent feedback is set to When I ask: ask your agent in a thread to send it, so you can approve what is sent."
114 : "Agent feedback is set to When I ask: send a report only when the user asks for one.";
115 }
116}
117
118export class FeedbackStorageError extends Schema.TaggedError<FeedbackStorageError>()(
119 "FeedbackStorageError",
120 { cause: Schema.Defect() },
121) {
122 override get message() {
123 return "Could not save agent feedback.";
124 }
125}
126
127/** The MCP client label `t3 feedback` signs in with, so its reports count as `cli`. */
128export const FEEDBACK_CLI_CLIENT_LABEL = "t3 feedback cli";
129
130/** Listed `t3` command paths a report may name. A CLI test keeps this in step with binCli. */
131export const T3_CLI_COMMANDS: ReadonlyArray<string> = [
132 "t3",
133 "t3 help",
134 "t3 serve",
135 "t3 app",
136 "t3 pair",
137 "t3 auth",
138 "t3 auth connect-device",
139 "t3 auth connect-device approve",
140 "t3 auth connect-device list",
141 "t3 auth connect-device revoke",
142 "t3 auth pairing",
143 "t3 auth pairing create",
144 "t3 auth pairing list",
145 "t3 auth pairing revoke",
146 "t3 auth session",
147 "t3 auth session issue",
148 "t3 auth session list",
149 "t3 auth session revoke",
150 "t3 environment",
151 "t3 environment link",
152 "t3 environment list",
153 "t3 environment unlink",
154 "t3 browser",
155 "t3 browser setup",
156 "t3 project",
157 "t3 project add",
158 "t3 project remove",
159 "t3 project rename",
160 "t3 thread",
161 "t3 thread list",
162 "t3 thread start",
163 "t3 thread send",
164 "t3 thread show",
165 "t3 feedback",
166 "t3 feedback list",
167 "t3 workflow",
168 "t3 workflow start",
169 "t3 workflow list",
170 "t3 workflow show",
171 "t3 workflow resume",
172 "t3 workflow cancel",
173 "t3 workflow signal",
174 "t3 workflow withdraw",
175 "t3 workflow questions",
176 "t3 workflow hooks",
177 "t3 workflow store",
178 "t3 workflow store list",
179 "t3 workflow store set",
180 "t3 workflow store delete",
181 "t3 up",
182 "t3 status",
183 "t3 service",
184 "t3 service install",
185 "t3 service restart",
186 "t3 service uninstall",
187 "t3 service status",
188 "t3 service protect",
189 "t3 update",
190 "t3 install",
191 "t3 uninstall",
192 "t3 theme",
193 "t3 theme set",
194 "t3 theme clear",
195 "t3 theme show",
196 "t3 trace",
197 "t3 trace summary",
198 "t3 doctor",
199 "t3 policy",
200 "t3 policy show",
201 "t3 policy check",
202 "t3 policy templates",
203 "t3 policy settings",
204 "t3 triage",
205 "t3 connect",
206 "t3 connect link",
207 "t3 connect login",
208 "t3 connect logout",
209 "t3 connect publish",
210 "t3 connect status",
211 "t3 connect unlink",
212];
213
214export type FeedbackInput = typeof FeedbackInput.Type;
215export type FeedbackReport = typeof FeedbackReport.Type;
216
217// The shared resolver knows the presented tools; the prefix covers the rest, such as these.
218// Codex also registers `t3-code-direct` for T3 tools it keeps out of Code Mode.
219const T3_PREFIX = /^(?:mcp__t3[-_]?code(?:[-_]direct)?__|t3[-_]?code(?:[-_]direct)?[.:/])/iu;
220
221/** The bare name of a registered T3 tool however a provider prefixed it, else undefined. */
222export const resolveT3ToolName = (name: string, toolNames: ReadonlyArray<string>) => {
223 const tool = resolveT3McpToolId(name) ?? name.replace(T3_PREFIX, "");
224 if (toolNames.includes(tool)) return tool;
225 // OpenCode 2 prefixes tools with `t3-code-<thread>_`, underscores and all: the longest name wins.
226 if (!/^t3-code-/iu.test(name)) return undefined;
227 return toolNames
228 .filter((candidate) => name.endsWith(`_${candidate}`))
229 .toSorted((a, b) => b.length - a.length)[0];
230};
231
232/**
233 * The stored subject: a T3 tool, a `t3` command, or `general` for anything else. An agent names
234 * what it had trouble with, such as a bundled CLI; refusing that loses the report, and storing it
235 * would send its free text.
236 */
237export const resolveFeedbackSubject = (value: string, toolNames: ReadonlyArray<string>) => {
238 const subject = value.trim();
239 if (T3_CLI_COMMANDS.includes(subject)) return subject;
240 return resolveT3ToolName(subject, toolNames) ?? "general";
241};