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

secretRequest.ts

97 lines3.3 KB

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