Open History to see the last commit
1import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
2import * as Console from "effect/Console";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as References from "effect/References";
7import * as Runtime from "effect/Runtime";
8import * as Schema from "effect/Schema";
9import { Command, Flag, GlobalFlag } from "effect/cli";
10
11import * as ServerConfig from "../config.ts";
12import { readFeedbackReports } from "../feedback/FeedbackList.ts";
13import * as ManagedPolicy from "../policy/ManagedPolicy.ts";
14import * as ServerSettings from "../serverSettings.ts";
15
16import {
17 FEEDBACK_CLI_CLIENT_LABEL,
18 FEEDBACK_OFF_MESSAGE,
19 FeedbackInput,
20 FeedbackOffError,
21 FeedbackSubmitResult,
22} from "../feedback/FeedbackReport.ts";
23import {
24 authLocationFlags,
25 type CliAuthLocationFlags,
26 projectLocationFlags,
27 resolveCliAuthConfig,
28} from "./config.ts";
29import {
30 jsonFlag,
31 printJson,
32 reportThreadErrors,
33 ThreadToolFailedError,
34 withThreadTools,
35} from "./thread.ts";
36
37/** The server hides the feedback tools from every session while the setting is off. */
38const feedbackOff = ({ tool }: { readonly tool: string }) =>
39 Effect.fail(new ThreadToolFailedError({ tool, detail: FEEDBACK_OFF_MESSAGE }));
40
41class FeedbackListError extends Schema.TaggedError<FeedbackListError>()("FeedbackListError", {
42 detail: Schema.String,
43}) {
44 override readonly [Runtime.errorReported] = false;
45 override get message(): string {
46 return this.detail;
47 }
48}
49
50const firstLine = (error: { readonly _tag: string; readonly message?: unknown }) =>
51 (typeof error.message === "string" && error.message.trim() !== ""
52 ? error.message
53 : error._tag
54 ).split("\n")[0];
55
56/** Reads this machine's database and settings directly, whether or not its server runs. */
57const listReports = Effect.fn("cli.feedback.listReports")(function* (flags: CliAuthLocationFlags) {
58 const config = yield* resolveCliAuthConfig(flags, yield* GlobalFlag.LogLevel, {
59 quietLogs: true,
60 });
61 return yield* Effect.gen(function* () {
62 // Settings first, from the file alone, so Off never opens the database.
63 const { agentFeedback } = yield* ServerSettings.readServerSettingsFromDisk;
64 if (agentFeedback.mode === "off") return yield* new FeedbackOffError({});
65 const { dbPath } = yield* ServerConfig.ServerConfig;
66 const fs = yield* FileSystem.FileSystem;
67 if (!(yield* fs.exists(dbPath))) return [];
68 // Read-only: no setup, no migrations, no writes, whether or not a server runs.
69 return yield* readFeedbackReports.pipe(
70 Effect.provide(NodeSqliteClient.layer({ filename: dbPath, readonly: true })),
71 );
72 }).pipe(
73 Effect.provide(ManagedPolicy.layer.pipe(Layer.provideMerge(ServerConfig.layer(config)))),
74 Effect.provideService(References.MinimumLogLevel, "Error"),
75 );
76});
77
78const feedbackListCommand = Command.make("list", { ...authLocationFlags, json: jsonFlag }).pipe(
79 Command.withDescription("List recent feedback reports saved on this machine, newest first."),
80 Command.withHandler(
81 Effect.fn("cli.feedback.list")(
82 function* (flags) {
83 const reports = yield* listReports(flags);
84 if (flags.json) return yield* printJson(reports);
85 for (const report of reports) {
86 yield* Console.log(
87 [
88 report.createdAt,
89 report.threadId ?? "-",
90 report.kind,
91 report.subject || "-",
92 report.userRequested ? "approved" : "unprompted",
93 report.sent ? "sent" : "local",
94 ].join(" "),
95 );
96 yield* Console.log(` expected: ${report.expected}`);
97 yield* Console.log(` actual: ${report.actual}`);
98 if (report.workaround) yield* Console.log(` workaround: ${report.workaround}`);
99 if (report.trace) {
100 const { turns, textTurns, entries } = report.trace;
101 const turnLabel = turns === 1 ? "turn" : "turns";
102 const entryLabel = entries.length === 1 ? "entry" : "entries";
103 yield* Console.log(
104 ` trace: outline of ${turns} ${turnLabel} (${entries.length} ${entryLabel}), text of ${textTurns}`,
105 );
106 }
107 }
108 },
109 // Every failure is one line on stderr; the runtime does not log it again.
110 Effect.catch((error) => {
111 const detail =
112 error._tag === "FeedbackOffError"
113 ? error.message
114 : `Could not read this machine's feedback reports: ${firstLine(error)}`;
115 return Console.error(detail).pipe(
116 Effect.andThen(Effect.fail(new FeedbackListError({ detail }))),
117 );
118 }),
119 ),
120 ),
121);
122
123export const feedbackCommand = Command.make("feedback", {
124 ...projectLocationFlags,
125 kind: Flag.Literals("kind", FeedbackInput.fields.kind.literals).pipe(
126 Flag.withDescription("What went wrong."),
127 ),
128 subject: Flag.String("subject").pipe(
129 Flag.withDescription("The T3 tool name, t3 command such as `t3 thread send`, or `general`."),
130 ),
131 expected: Flag.String("expected").pipe(Flag.withDescription("What you expected.")),
132 actual: Flag.String("actual").pipe(Flag.withDescription("What happened instead.")),
133 workaround: Flag.String("workaround").pipe(
134 Flag.withDescription("What you did instead."),
135 Flag.withDefault(""),
136 ),
137}).pipe(
138 Command.withDescription(
139 "Send feedback about T3 Code to the T3 team. Needs Agent feedback set to Automatic in Settings > Labs; to approve a report first, ask your agent in a thread.",
140 ),
141 Command.withHandler(({ baseDir, kind, subject, expected, actual, workaround }) =>
142 withThreadTools(
143 { baseDir, json: true, clientLabel: FEEDBACK_CLI_CLIENT_LABEL },
144 Effect.fn("cli.feedback.submit")(function* (tools) {
145 const report = yield* tools.call(
146 "t3_feedback",
147 { kind, subject, expected, actual, workaround },
148 FeedbackSubmitResult,
149 );
150 yield* printJson(report);
151 }),
152 ).pipe(Effect.catchTags({ ThreadToolMissingError: feedbackOff }), reportThreadErrors),
153 ),
154 Command.withSubcommands([feedbackListCommand]),
155);