Open History to see the last commit
1// `t3 feedback list` reads the local table directly through a read-only connection, so the list
2// needs no agent tool and never writes. SQLite allows this read beside a running server.
3import * as Effect from "effect/Effect";
4import * as Schema from "effect/Schema";
5import * as SqlClient from "effect/sql/SqlClient";
6
7import * as Settings from "../serverSettings.ts";
8import {
9 FeedbackOffError,
10 type FeedbackReport,
11 FeedbackStorageError,
12 FeedbackTrace,
13} from "./FeedbackReport.ts";
14
15const LIST_LIMIT = 50;
16const decodeTrace = Schema.decodeUnknownEffect(Schema.NullOr(Schema.fromJsonString(FeedbackTrace)));
17
18/**
19 * The most recent reports in this database, newest first, exactly as stored. A database from
20 * before agent feedback has no table yet, and so no reports.
21 */
22export const readFeedbackReports = Effect.gen(function* () {
23 const sql = yield* SqlClient.SqlClient;
24 const [table] = yield* sql<{ readonly count: number }>`
25 SELECT COUNT(*) AS count FROM sqlite_schema WHERE type = 'table' AND name = 'agent_feedback'`.pipe(
26 Effect.mapError((cause) => new FeedbackStorageError({ cause })),
27 );
28 if ((table?.count ?? 0) === 0) return [];
29 const rows = yield* sql<
30 Omit<FeedbackReport, "sent" | "userRequested" | "trace"> & {
31 readonly sent: number;
32 readonly userRequested: number;
33 readonly trace: string | null;
34 }
35 >`
36 SELECT id, thread_id AS "threadId", created_at AS "createdAt", kind, subject,
37 expected, actual, workaround, user_requested AS "userRequested", trace, source, sent
38 FROM agent_feedback ORDER BY rowid DESC LIMIT ${LIST_LIMIT}`.pipe(
39 Effect.mapError((cause) => new FeedbackStorageError({ cause })),
40 );
41 return yield* Effect.forEach(rows, (row) =>
42 decodeTrace(row.trace).pipe(
43 Effect.mapError((cause) => new FeedbackStorageError({ cause })),
44 Effect.map((trace): FeedbackReport => ({
45 ...row,
46 trace,
47 userRequested: row.userRequested === 1,
48 sent: row.sent === 1,
49 })),
50 ),
51 );
52}).pipe(Effect.withSpan("FeedbackList.readFeedbackReports"));
53
54/** The reports, or the Labs line while agent feedback is off. */
55export const listFeedbackReports = Effect.gen(function* () {
56 const settings = yield* Settings.ServerSettingsService;
57 const { agentFeedback } = yield* settings.getSettings.pipe(
58 Effect.mapError((cause) => new FeedbackStorageError({ cause })),
59 );
60 if (agentFeedback.mode === "off") return yield* new FeedbackOffError({});
61 return yield* readFeedbackReports;
62});