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

feedback.ts

153 lines5.9 KB

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 }
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 yield* Console.log(
102 ` trace: outline of ${turns} turns (${entries.length} entries), text of ${textTurns}`,
103 );
104 }
105 }
106 },
107 // Every failure is one line on stderr; the runtime does not log it again.
108 Effect.catch((error) => {
109 const detail =
110 error._tag === "FeedbackOffError"
111 ? error.message
112 : `Could not read this machine's feedback reports: ${firstLine(error)}`;
113 return Console.error(detail).pipe(
114 Effect.andThen(Effect.fail(new FeedbackListError({ detail }))),
115 );
116 }),
117 ),
118 ),
119);
120​
121export const feedbackCommand = Command.make("feedback", {
122 ...projectLocationFlags,
123 kind: Flag.Literals("kind", FeedbackInput.fields.kind.literals).pipe(
124 Flag.withDescription("What went wrong."),
125 ),
126 subject: Flag.String("subject").pipe(
127 Flag.withDescription("The T3 tool name, t3 command such as `t3 thread send`, or `general`."),
128 ),
129 expected: Flag.String("expected").pipe(Flag.withDescription("What you expected.")),
130 actual: Flag.String("actual").pipe(Flag.withDescription("What happened instead.")),
131 workaround: Flag.String("workaround").pipe(
132 Flag.withDescription("What you did instead."),
133 Flag.withDefault(""),
134 ),
135}).pipe(
136 Command.withDescription(
137 "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.",
138 ),
139 Command.withHandler(({ baseDir, kind, subject, expected, actual, workaround }) =>
140 withThreadTools(
141 { baseDir, json: true, clientLabel: FEEDBACK_CLI_CLIENT_LABEL },
142 Effect.fn("cli.feedback.submit")(function* (tools) {
143 const report = yield* tools.call(
144 "t3_feedback",
145 { kind, subject, expected, actual, workaround },
146 FeedbackSubmitResult,
147 );
148 yield* printJson(report);
149 }),
150 ).pipe(Effect.catchTags({ ThreadToolMissingError: feedbackOff }), reportThreadErrors),
151 ),
152 Command.withSubcommands([feedbackListCommand]),
153);