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

secretRequest.ts

93 lines3.0 KB

1import type { OrchestrationV2TurnItem, SecretRequestAnswerInput } from "@t3tools/contracts";
2​
3export type SecretRequestItem = Extract<
4 OrchestrationV2TurnItem,
5 { readonly type: "secret_request" }
6>;
7​
8export const SECRET_REQUEST_PRIVACY_NOTE = "Stored securely, never shown to the agent";
9export const SECRET_REQUEST_DEFAULT_PLACEHOLDER = "Paste the secret";
10​
11export type SecretRequestDisplay =
12 | { readonly kind: "pending" }
13 | { readonly kind: "pending-elsewhere"; readonly label: string }
14 | {
15 readonly kind: "answered";
16 readonly outcome: "saved" | "declined" | "ended";
17 readonly label: string;
18 };
19​
20const SAVED_DISPLAY: SecretRequestDisplay = {
21 kind: "answered",
22 outcome: "saved",
23 label: "Saved securely and kept private",
24};
25const DECLINED_DISPLAY: SecretRequestDisplay = {
26 kind: "answered",
27 outcome: "declined",
28 label: "Declined",
29};
30const ENDED_DISPLAY: SecretRequestDisplay = {
31 kind: "answered",
32 outcome: "ended",
33 label: "Request ended",
34};
35const PENDING_DISPLAY: SecretRequestDisplay = { kind: "pending" };
36const PENDING_ELSEWHERE_DISPLAY: SecretRequestDisplay = {
37 kind: "pending-elsewhere",
38 label: "Waiting for an answer in the original thread",
39};
40​
41/**
42 * `visibility` is the projected row's: a request inherited from another
43 * thread (a fork) can only be answered where it was asked.
44 */
45export function secretRequestDisplay(
46 item: Pick<SecretRequestItem, "secretStatus">,
47 visibility: "local" | "inherited" | "synthetic",
48): SecretRequestDisplay {
49 switch (item.secretStatus) {
50 case "pending":
51 return visibility === "local" ? PENDING_DISPLAY : PENDING_ELSEWHERE_DISPLAY;
52 case "saved":
53 return SAVED_DISPLAY;
54 case "declined":
55 return DECLINED_DISPLAY;
56 case "cancelled":
57 return ENDED_DISPLAY;
58 }
60​
61export function secretRequestAnswerInput(
62 item: Pick<SecretRequestItem, "id" | "threadId">,
63 answer: { readonly type: "save"; readonly secret: string } | { readonly type: "decline" },
64): SecretRequestAnswerInput | null {
65 if (answer.type === "decline") {
66 return { threadId: item.threadId, turnItemId: item.id, answer: { type: "decline" } };
67 }
68 const secret = answer.secret.trim();
69 if (secret.length === 0) return null;
70 return { threadId: item.threadId, turnItemId: item.id, answer: { type: "save", secret } };
72​
73const USER_FACING_FAILURE_TAGS = new Set(["SecretRequestError", "EnvironmentAuthorizationError"]);
74​
75/**
76 * Only known server errors pass their message through: anything else (transport or encoding
77 * failures) gets the generic copy, so the typed value can never surface in the UI.
78 */
79export function secretRequestFailureMessage(failure: unknown): string {
80 if (
81 typeof failure === "object" &&
82 failure !== null &&
83 "_tag" in failure &&
84 typeof failure._tag === "string" &&
85 USER_FACING_FAILURE_TAGS.has(failure._tag) &&
86 "message" in failure &&
87 typeof failure.message === "string" &&
88 failure.message.trim().length > 0
89 ) {
90 return failure.message;
91 }
92 return "Could not answer the request. Try again.";