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

feedbackRequest.ts

145 lines5.5 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("{speaker} · {count} characters", {
90 speaker: resolve(entry.type === "user" ? msg("You") : msg("Agent")),
91 count: entry.length ?? 0,
92 }),
93 );
94 return {
95 key: `${index}`,
96 line: resolve(msg("Turn {turn} · {what}", { turn: entry.turn, what })),
97 ...(entry.text === undefined ? {} : { text: entry.text }),
98 };
99 });
101​
102type Trace = NonNullable<AgentFeedbackPreview["trace"]>;
103​
104// Counted back to the oldest turn with text, so a newer turn without any is still inside it.
105const textSpan = (trace: Trace) => {
106 const withText = trace.entries.filter((entry) => entry.text !== undefined);
107 return withText.length === 0
108 ? 0
109 : trace.turns - Math.min(...withText.map((entry) => entry.turn)) + 1;
110};
111​
112/** What the outline holds, in one line. */
113export function feedbackTraceSummary(trace: Trace, resolve: MFunctionType = mFallback): string {
114 return resolve(
115 msg(
116 "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 {}}",
117 { turns: trace.turns, textTurns: textSpan(trace), truncated: trace.truncated ? "yes" : "no" },
118 ),
119 );
121​
122/** What the conversation switch adds: the text of the turns the agent said the problem spans. */
123export function feedbackConversationHint(trace: Trace, resolve: MFunctionType = mFallback): string {
124 return resolve(
125 msg(
126 "Adds the text of your messages and the agent's replies in the last {textTurns, plural, one {turn} other {{textTurns} turns}}.",
127 { textTurns: textSpan(trace) },
128 ),
129 );
131​
132/** The server's own message for a failed answer, else a generic one. */
133export function feedbackRequestFailureMessage(
134 failure: unknown,
135 resolve: MFunctionType = mFallback,
136): string {
137 return typeof failure === "object" &&
138 failure !== null &&
139 "_tag" in failure &&
140 failure._tag === "AgentFeedbackRequestError" &&
141 "message" in failure &&
142 typeof failure.message === "string"
143 ? failure.message
144 : resolve(msg("Could not send your answer. Try again."));