Open History to see the last commit
1import { mFallback, msg, plural, part as gtPart, select } from "gt-i18n";
2import type { M } from "gt-i18n";
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: M = 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 }
37}
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;
47}
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: M = mFallback,
61): ReadonlyArray<{ readonly label: string; readonly value: string }> {
62 return [
63 { label: resolve(msg("Kind")), value: resolve(KIND_LABELS[preview.kind]) },
64 {
65 label: resolve(msg({ context: "feedback report field: its subject" })`About`),
66 value: preview.subject,
67 },
68 { label: resolve(msg("Expected")), value: preview.expected },
69 { label: resolve(msg("Happened")), value: preview.actual },
70 { label: resolve(msg("Did instead")), value: preview.workaround },
71 ].filter((field) => field.value !== "");
72}
73
74const seconds = (ms: number) => `${(ms / 1_000).toFixed(ms < 10_000 ? 1 : 0)} s`;
75
76/** One line per outline entry, plus the message text that goes with it. */
77export function feedbackTraceLines(
78 trace: NonNullable<AgentFeedbackPreview["trace"]>,
79 resolve: M = mFallback,
80): ReadonlyArray<{ readonly key: string; readonly line: string; readonly text?: string }> {
81 return trace.entries.map((entry, index) => {
82 const what =
83 entry.type === "tool"
84 ? [
85 entry.tool ?? "tool",
86 entry.outcome,
87 entry.durationMs === undefined ? undefined : seconds(entry.durationMs),
88 ]
89 .filter((part) => part !== undefined)
90 .join(" · ")
91 : resolve(
92 msg`${plural(
93 { count: entry.length ?? 0 },
94 {
95 one: gtPart`${{ speaker: resolve(entry.type === "user" ? msg("You") : msg("Agent")) }} · ${{ count: entry.length ?? 0 }} character`,
96 other: gtPart`${{ speaker: resolve(entry.type === "user" ? msg("You") : msg("Agent")) }} · ${{ count: entry.length ?? 0 }} characters`,
97 },
98 )}`,
99 );
100 return {
101 key: `${index}`,
102 line: resolve(msg`Turn ${{ turn: entry.turn }} · ${{ what }}`),
103 ...(entry.text === undefined ? {} : { text: entry.text }),
104 };
105 });
106}
107
108type Trace = NonNullable<AgentFeedbackPreview["trace"]>;
109
110// Counted back to the oldest turn with text, so a newer turn without any is still inside it.
111const textSpan = (trace: Trace) => {
112 const withText = trace.entries.filter((entry) => entry.text !== undefined);
113 return withText.length === 0
114 ? 0
115 : trace.turns - Math.min(...withText.map((entry) => entry.turn)) + 1;
116};
117
118/** What the outline holds, in one line. */
119export function feedbackTraceSummary(trace: Trace, resolve: M = mFallback): string {
120 const { turns } = trace;
121 const textTurns = textSpan(trace);
122 return resolve(
123 msg`Outline of ${plural(
124 { turns },
125 { one: gtPart`${{ turns }} recent turn`, other: gtPart`${{ turns }} recent turns` },
126 )}, ${plural(
127 { textTurns },
128 {
129 "=0": "no message text",
130 one: "message text of the last turn",
131 other: gtPart`message text of the last ${{ textTurns }} turns`,
132 },
133 )}${select(
134 { truncated: trace.truncated ? "yes" : "no" },
135 {
136 yes: ", shortened to fit",
137 other: "",
138 },
139 )}`,
140 );
141}
142
143/** What the conversation switch adds: the text of the turns the agent said the problem spans. */
144export function feedbackConversationHint(trace: Trace, resolve: M = mFallback): string {
145 const textTurns = textSpan(trace);
146 return resolve(
147 msg`Adds the text of your messages and the agent's replies in the last ${plural(
148 { textTurns },
149 { one: "turn", other: gtPart`${{ textTurns }} turns` },
150 )}.`,
151 );
152}
153
154/** The server's own message for a failed answer, else a generic one. */
155export function feedbackRequestFailureMessage(failure: unknown, resolve: M = mFallback): string {
156 return typeof failure === "object" &&
157 failure !== null &&
158 "_tag" in failure &&
159 failure._tag === "AgentFeedbackRequestError" &&
160 "message" in failure &&
161 typeof failure.message === "string"
162 ? failure.message
163 : resolve(msg("Could not send your answer. Try again."));
164}