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
87export class FeedbackLimitError extends Schema.TaggedError<FeedbackLimitError>()(
88 "FeedbackLimitError",
89 { scope: Schema.Literals(["turn", "hour", "cards"]) },
90) {
91 override get message() {
92 switch (this.scope) {
93 case "turn":
94 return `This turn already sent ${MAX_REPORTS_PER_TURN} agent feedback reports; nothing was sent.`;
95 case "cards":
96 return `This turn already showed the user ${MAX_REPORTS_PER_TURN} agent feedback reports to approve; nothing was shown.`;
97 case "hour":
98 return `This machine already sent ${MAX_REPORTS_OUTSIDE_TURNS_PER_HOUR} agent feedback reports outside a turn in the last hour; nothing was sent.`;
99 }
100 }
101}
102
103export class FeedbackNotRequestedError extends Schema.TaggedError<FeedbackNotRequestedError>()(
104 "FeedbackNotRequestedError",
105 { source: Schema.Literals(["mcp", "cli"]) },
106) {
107 override get message() {
108 return this.source === "cli"
109 ? "Agent feedback is set to When I ask: ask your agent in a thread to send it, so you can approve what is sent."
110 : "Agent feedback is set to When I ask: send a report only when the user asks for one.";
111 }
112}
113
114export class FeedbackStorageError extends Schema.TaggedError<FeedbackStorageError>()(
115 "FeedbackStorageError",
116 { cause: Schema.Defect() },
117) {
118 override get message() {
119 return "Could not save agent feedback.";
120 }
121}
122
123/** The MCP client label `t3 feedback` signs in with, so its reports count as `cli`. */
124export const FEEDBACK_CLI_CLIENT_LABEL = "t3 feedback cli";
125
126/** Listed `t3` command paths a report may name. A CLI test keeps this in step with binCli. */
127export const T3_CLI_COMMANDS: ReadonlyArray<string> = [
128 "t3",
129 "t3 help",
130 "t3 serve",
131 "t3 app",
132 "t3 pair",
133 "t3 auth",
134 "t3 auth connect-device",
135 "t3 auth connect-device approve",
136 "t3 auth connect-device list",
137 "t3 auth connect-device revoke",
138 "t3 auth pairing",
139 "t3 auth pairing create",
140 "t3 auth pairing list",
141 "t3 auth pairing revoke",
142 "t3 auth session",
143 "t3 auth session issue",
144 "t3 auth session list",
145 "t3 auth session revoke",
146 "t3 environment",
147 "t3 environment link",
148 "t3 environment list",
149 "t3 environment unlink",
150 "t3 browser",
151 "t3 browser setup",
152 "t3 project",
153 "t3 project add",
154 "t3 project remove",
155 "t3 project rename",
156 "t3 thread",
157 "t3 thread list",
158 "t3 thread start",
159 "t3 thread send",
160 "t3 thread show",
161 "t3 feedback",
162 "t3 feedback list",
163 "t3 workflow",
164 "t3 workflow start",
165 "t3 workflow list",
166 "t3 workflow show",
167 "t3 workflow resume",
168 "t3 workflow cancel",
169 "t3 workflow signal",
170 "t3 workflow questions",
171 "t3 workflow hooks",
172 "t3 workflow store",
173 "t3 workflow store list",
174 "t3 workflow store set",
175 "t3 workflow store delete",
176 "t3 up",
177 "t3 status",
178 "t3 service",
179 "t3 service install",
180 "t3 service restart",
181 "t3 service uninstall",
182 "t3 service status",
183 "t3 service protect",
184 "t3 update",
185 "t3 install",
186 "t3 uninstall",
187 "t3 theme",
188 "t3 theme set",
189 "t3 theme clear",
190 "t3 theme show",
191 "t3 trace",
192 "t3 trace summary",
193 "t3 doctor",
194 "t3 policy",
195 "t3 policy show",
196 "t3 policy check",
197 "t3 policy templates",
198 "t3 policy settings",
199 "t3 triage",
200 "t3 connect",
201 "t3 connect link",
202 "t3 connect login",
203 "t3 connect logout",
204 "t3 connect publish",
205 "t3 connect status",
206 "t3 connect unlink",
207];
208
209export type FeedbackInput = typeof FeedbackInput.Type;
210export type FeedbackReport = typeof FeedbackReport.Type;
211
212export const FEEDBACK_SUBJECT_MESSAGE =
213 "Set subject to the exact T3 tool name, the t3 command, or `general`.";
214
215// The shared resolver knows the presented tools; the prefix covers the rest, such as these.
216// Codex also registers `t3-code-direct` for T3 tools it keeps out of Code Mode.
217const T3_PREFIX = /^(?:mcp__t3[-_]?code(?:[-_]direct)?__|t3[-_]?code(?:[-_]direct)?[.:/])/iu;
218
219/** The bare name of a registered T3 tool however a provider prefixed it, else undefined. */
220export const resolveT3ToolName = (name: string, toolNames: ReadonlyArray<string>) => {
221 const tool = resolveT3McpToolId(name) ?? name.replace(T3_PREFIX, "");
222 if (toolNames.includes(tool)) return tool;
223 // OpenCode 2 prefixes tools with `t3-code-<thread>_`, underscores and all: the longest name wins.
224 if (!/^t3-code-/iu.test(name)) return undefined;
225 return toolNames
226 .filter((candidate) => name.endsWith(`_${candidate}`))
227 .toSorted((a, b) => b.length - a.length)[0];
228};
229
230/** The stored subject, or undefined when the value names no T3 tool, `t3` command, or `general`. */
231export const resolveFeedbackSubject = (value: string, toolNames: ReadonlyArray<string>) => {
232 const subject = value.trim();
233 if (subject === "general" || T3_CLI_COMMANDS.includes(subject)) return subject;
234 return resolveT3ToolName(subject, toolNames);
235};