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

secretRequest.ts

177 lines6.0 KB

1import {
2 SecretReference,
3 type OrchestrationV2TurnItem,
4 type SecretProviderId,
5 type SecretRequestAnswerInput,
6 type ServerConfig,
7 type ServerSettings,
8} from "@t3tools/contracts";
9import { builtInSecretProviderDefinitions } from "@t3tools/secret-provider-definitions/registry";
10import * as Option from "effect/Option";
11import * as Schema from "effect/Schema";
12​
13export type SecretRequestItem = Extract<
14 OrchestrationV2TurnItem,
15 { readonly type: "secret_request" }
16>;
17​
18/** A `file` request lets the agent's commands read the value, so its card must not promise more. */
19export const secretRequestPrivacyNote = (item: Pick<SecretRequestItem, "delivery">) =>
20 item.delivery === "file"
21 ? "Kept out of chat, in a private file this agent's commands can read for 24 hours"
22 : "Stored securely, never shown to the agent";
23export const SECRET_REQUEST_DEFAULT_PLACEHOLDER = "Paste the secret";
24​
25export type SecretRequestDisplay =
26 | { readonly kind: "pending" }
27 | { readonly kind: "pending-elsewhere"; readonly label: string }
28 | {
29 readonly kind: "answered";
30 readonly outcome: "saved" | "declined" | "ended";
31 readonly label: string;
32 };
33​
34const SAVED_DISPLAY: SecretRequestDisplay = {
35 kind: "answered",
36 outcome: "saved",
37 label: "Saved securely and kept private",
38};
39const DECLINED_DISPLAY: SecretRequestDisplay = {
40 kind: "answered",
41 outcome: "declined",
42 label: "Declined",
43};
44const ENDED_DISPLAY: SecretRequestDisplay = {
45 kind: "answered",
46 outcome: "ended",
47 label: "Request ended",
48};
49const PENDING_DISPLAY: SecretRequestDisplay = { kind: "pending" };
50const PENDING_ELSEWHERE_DISPLAY: SecretRequestDisplay = {
51 kind: "pending-elsewhere",
52 label: "Waiting for an answer in the original thread",
53};
54​
55/**
56 * `visibility` is the projected row's: a request inherited from another
57 * thread (a fork) can only be answered where it was asked.
58 */
59export function secretRequestDisplay(
60 item: Pick<SecretRequestItem, "secretStatus">,
61 visibility: "local" | "inherited" | "synthetic",
62): SecretRequestDisplay {
63 switch (item.secretStatus) {
64 case "pending":
65 return visibility === "local" ? PENDING_DISPLAY : PENDING_ELSEWHERE_DISPLAY;
66 case "saved":
67 return SAVED_DISPLAY;
68 case "declined":
69 return DECLINED_DISPLAY;
70 case "cancelled":
71 return ENDED_DISPLAY;
72 }
74​
75/** A configured secret provider a pending card can read its answer from. */
76export interface SecretRequestSource {
77 readonly id: SecretProviderId;
78 readonly label: string;
79 readonly isReference: (reference: string) => boolean;
80 readonly referencePlaceholder: string;
81 readonly invalidReferenceMessage: string;
83​
84const NO_SOURCES: ReadonlyArray<SecretRequestSource> = [];
85const decodeConfigByKind = new Map(
86 builtInSecretProviderDefinitions.definitions.map((definition) => [
87 definition.kind,
88 Schema.decodeUnknownOption(definition.configSchema),
89 ]),
90);
91​
92/** Empty unless the server accepts reference answers; providers this build cannot read are left out. */
93export function secretRequestSources(
94 config: Pick<ServerConfig, "secretRequestReferences"> | null | undefined,
95 providers: ServerSettings["secretProviders"],
96): ReadonlyArray<SecretRequestSource> {
97 if (config?.secretRequestReferences !== true) return NO_SOURCES;
98 return (Object.keys(providers) as Array<SecretProviderId>).flatMap((id) => {
99 const provider = providers[id];
100 const definition =
101 provider === undefined ? undefined : builtInSecretProviderDefinitions.find(provider.kind);
102 if (
103 provider === undefined ||
104 definition === undefined ||
105 decodeConfigByKind.get(definition.kind)?.(provider.config)._tag !== "Some"
106 ) {
107 return [];
108 }
109 return [
110 {
111 id,
112 label: provider.label,
113 isReference: definition.isReference,
114 referencePlaceholder: definition.referencePlaceholder,
115 invalidReferenceMessage: definition.invalidReferenceMessage,
116 },
117 ];
118 });
120​
121const decodeSecretReference = Schema.decodeUnknownOption(SecretReference);
122​
123/** The reference `text` names, or undefined when it is not `source`'s syntax. */
124export function secretRequestReference(
125 source: SecretRequestSource,
126 text: string,
127): SecretReference | undefined {
128 const decoded = decodeSecretReference({ provider: source.id, reference: text.trim() });
129 return Option.isSome(decoded) && source.isReference(decoded.value.reference)
130 ? decoded.value
131 : undefined;
133​
134export type SecretRequestReply =
135 | { readonly type: "save"; readonly secret: string }
136 | { readonly type: "reference"; readonly source: SecretRequestSource; readonly reference: string }
137 | { readonly type: "decline" };
138​
139export function secretRequestAnswerInput(
140 item: Pick<SecretRequestItem, "id" | "threadId">,
141 answer: SecretRequestReply,
142): SecretRequestAnswerInput | null {
143 if (answer.type === "decline") {
144 return { threadId: item.threadId, turnItemId: item.id, answer: { type: "decline" } };
145 }
146 if (answer.type === "reference") {
147 const reference = secretRequestReference(answer.source, answer.reference);
148 return reference === undefined
149 ? null
150 : { threadId: item.threadId, turnItemId: item.id, answer: { type: "reference", reference } };
151 }
152 const secret = answer.secret.trim();
153 if (secret.length === 0) return null;
154 return { threadId: item.threadId, turnItemId: item.id, answer: { type: "save", secret } };
156​
157const USER_FACING_FAILURE_TAGS = new Set(["SecretRequestError", "EnvironmentAuthorizationError"]);
158​
159/**
160 * Only known server errors pass their message through: anything else (transport or encoding
161 * failures) gets the generic copy, so the typed value can never surface in the UI.
162 */
163export function secretRequestFailureMessage(failure: unknown): string {
164 if (
165 typeof failure === "object" &&
166 failure !== null &&
167 "_tag" in failure &&
168 typeof failure._tag === "string" &&
169 USER_FACING_FAILURE_TAGS.has(failure._tag) &&
170 "message" in failure &&
171 typeof failure.message === "string" &&
172 failure.message.trim().length > 0
173 ) {
174 return failure.message;
175 }
176 return "Could not answer the request. Try again.";