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

feedbackRequest.ts

148 lines5.6 KB

1import { mFallback, msg } from "gt-i18n";
2import type { MFunctionType } from "gt-i18n/types";
3import type { AgentFeedbackPreview, OrchestrationV2TurnItem } from "@t3tools/contracts";
4​
5export type FeedbackRequestItem = Extract<
6 OrchestrationV2TurnItem,
7 { readonly type: "feedback_request" }
8>;
9​
10export type FeedbackRequestDisplay =
11 | { readonly kind: "pending" }
12 /** Inherited by a fork: only the thread that asked can answer it. */
13 | { readonly kind: "pending-elsewhere"; readonly label: string }
14 | { readonly kind: "answered"; readonly outcome: "sent" | "not-sent"; readonly label: string };
15​
16export function feedbackRequestDisplay(
17 item: Pick<FeedbackRequestItem, "feedbackStatus">,
18 visibility: "local" | "inherited" | "synthetic",
19 resolve: MFunctionType = mFallback,
20): FeedbackRequestDisplay {
21 switch (item.feedbackStatus) {
22 case "pending":
23 return visibility === "local"
24 ? { kind: "pending" }
25 : { kind: "pending-elsewhere", label: resolve(msg("Waiting in the original thread")) };
26 case "sent":
27 return { kind: "answered", outcome: "sent", label: resolve(msg("Sent")) };
28 case "declined":
29 return { kind: "answered", outcome: "not-sent", label: resolve(msg("Not sent")) };
30 case "cancelled":
31 return {
32 kind: "answered",
33 outcome: "not-sent",
34 label: resolve(msg("Closed without sending")),
35 };
36 }
38​
39/** Exactly what Send sends: the outline, or with the conversation included, its text too. */
40export function feedbackCardPreview(
41 item: Pick<FeedbackRequestItem, "preview" | "conversationTrace">,
42 includeConversation: boolean,
43): AgentFeedbackPreview {
44 return includeConversation && item.conversationTrace !== undefined
45 ? { ...item.preview, trace: item.conversationTrace }
46 : item.preview;
48​
49const KIND_LABELS: Record<AgentFeedbackPreview["kind"], string> = {
50 bug: msg("Bug"),
51 confusing: msg("Confusing"),
52 missing_capability: msg("Missing capability"),
53 wrong_docs: msg("Wrong docs"),
54 other: msg("Other"),
55};
56​
57/** The report's fields in reading order, the empty workaround left out. */
58export function feedbackPreviewFields(
59 preview: AgentFeedbackPreview,
60 resolve: MFunctionType = mFallback,
61): ReadonlyArray<{ readonly label: string; readonly value: string }> {
62 return [
63 { label: resolve(msg("Kind")), value: resolve(KIND_LABELS[preview.kind]) },
64 { label: resolve(msg("About")), value: preview.subject },
65 { label: resolve(msg("Expected")), value: preview.expected },
66 { label: resolve(msg("Happened")), value: preview.actual },
67 { label: resolve(msg("Did instead")), value: preview.workaround },
68 ].filter((field) => field.value !== "");
70​
71const seconds = (ms: number) => `${(ms / 1_000).toFixed(ms < 10_000 ? 1 : 0)} s`;
72​
73/** One line per outline entry, plus the message text that goes with it. */
74export function feedbackTraceLines(
75 trace: NonNullable<AgentFeedbackPreview["trace"]>,
76 resolve: MFunctionType = mFallback,
77): ReadonlyArray<{ readonly key: string; readonly line: string; readonly text?: string }> {
78 return trace.entries.map((entry, index) => {
79 const what =
80 entry.type === "tool"
81 ? [
82 entry.tool ?? "tool",
83 entry.outcome,
84 entry.durationMs === undefined ? undefined : seconds(entry.durationMs),
85 ]
86 .filter((part) => part !== undefined)
87 .join(" · ")
88 : resolve(
89 msg(
90 "{count, plural, one {{speaker} · {count} character} other {{speaker} · {count} characters}}",
91 {
92 speaker: resolve(entry.type === "user" ? msg("You") : msg("Agent")),
93 count: entry.length ?? 0,
94 },
95 ),
96 );
97 return {
98 key: `${index}`,
99 line: resolve(msg("Turn {turn} · {what}", { turn: entry.turn, what })),
100 ...(entry.text === undefined ? {} : { text: entry.text }),
101 };
102 });
104​
105type Trace = NonNullable<AgentFeedbackPreview["trace"]>;
106​
107// Counted back to the oldest turn with text, so a newer turn without any is still inside it.
108const textSpan = (trace: Trace) => {
109 const withText = trace.entries.filter((entry) => entry.text !== undefined);
110 return withText.length === 0
111 ? 0
112 : trace.turns - Math.min(...withText.map((entry) => entry.turn)) + 1;
113};
114​
115/** What the outline holds, in one line. */
116export function feedbackTraceSummary(trace: Trace, resolve: MFunctionType = mFallback): string {
117 return resolve(
118 msg(
119 "Outline of {turns, plural, one {{turns} recent turn} other {{turns} recent turns}}, {textTurns, plural, =0 {no message text} one {message text of the last turn} other {message text of the last {textTurns} turns}}{truncated, select, yes {, shortened to fit} other {}}",
120 { turns: trace.turns, textTurns: textSpan(trace), truncated: trace.truncated ? "yes" : "no" },
121 ),
122 );
124​
125/** What the conversation switch adds: the text of the turns the agent said the problem spans. */
126export function feedbackConversationHint(trace: Trace, resolve: MFunctionType = mFallback): string {
127 return resolve(
128 msg(
129 "Adds the text of your messages and the agent's replies in the last {textTurns, plural, one {turn} other {{textTurns} turns}}.",
130 { textTurns: textSpan(trace) },
131 ),
132 );
134​
135/** The server's own message for a failed answer, else a generic one. */
136export function feedbackRequestFailureMessage(
137 failure: unknown,
138 resolve: MFunctionType = mFallback,
139): string {
140 return typeof failure === "object" &&
141 failure !== null &&
142 "_tag" in failure &&
143 failure._tag === "AgentFeedbackRequestError" &&
144 "message" in failure &&
145 typeof failure.message === "string"
146 ? failure.message
147 : resolve(msg("Could not send your answer. Try again."));