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

secretRequest.ts

183 lines6.2 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 configuredSecretSources(providers);
100​
101export function configuredSecretSources(
102 providers: ServerSettings["secretProviders"],
103): ReadonlyArray<SecretRequestSource> {
104 return (Object.keys(providers) as Array<SecretProviderId>).flatMap((id) => {
105 const provider = providers[id];
106 const definition =
107 provider === undefined ? undefined : builtInSecretProviderDefinitions.find(provider.kind);
108 if (
109 provider === undefined ||
110 definition === undefined ||
111 decodeConfigByKind.get(definition.kind)?.(provider.config)._tag !== "Some"
112 ) {
113 return [];
114 }
115 return [
116 {
117 id,
118 label: provider.label,
119 isReference: definition.isReference,
120 referencePlaceholder: definition.referencePlaceholder,
121 invalidReferenceMessage: definition.invalidReferenceMessage,
122 },
123 ];
124 });
126​
127const decodeSecretReference = Schema.decodeUnknownOption(SecretReference);
128​
129/** The reference `text` names, or undefined when it is not `source`'s syntax. */
130export function secretRequestReference(
131 source: SecretRequestSource,
132 text: string,
133): SecretReference | undefined {
134 const decoded = decodeSecretReference({ provider: source.id, reference: text.trim() });
135 return Option.isSome(decoded) && source.isReference(decoded.value.reference)
136 ? decoded.value
137 : undefined;
139​
140export type SecretRequestReply =
141 | { readonly type: "save"; readonly secret: string }
142 | { readonly type: "reference"; readonly source: SecretRequestSource; readonly reference: string }
143 | { readonly type: "decline" };
144​
145export function secretRequestAnswerInput(
146 item: Pick<SecretRequestItem, "id" | "threadId">,
147 answer: SecretRequestReply,
148): SecretRequestAnswerInput | null {
149 if (answer.type === "decline") {
150 return { threadId: item.threadId, turnItemId: item.id, answer: { type: "decline" } };
151 }
152 if (answer.type === "reference") {
153 const reference = secretRequestReference(answer.source, answer.reference);
154 return reference === undefined
155 ? null
156 : { threadId: item.threadId, turnItemId: item.id, answer: { type: "reference", reference } };
157 }
158 const secret = answer.secret.trim();
159 if (secret.length === 0) return null;
160 return { threadId: item.threadId, turnItemId: item.id, answer: { type: "save", secret } };
162​
163const USER_FACING_FAILURE_TAGS = new Set(["SecretRequestError", "EnvironmentAuthorizationError"]);
164​
165/**
166 * Only known server errors pass their message through: anything else (transport or encoding
167 * failures) gets the generic copy, so the typed value can never surface in the UI.
168 */
169export function secretRequestFailureMessage(failure: unknown): string {
170 if (
171 typeof failure === "object" &&
172 failure !== null &&
173 "_tag" in failure &&
174 typeof failure._tag === "string" &&
175 USER_FACING_FAILURE_TAGS.has(failure._tag) &&
176 "message" in failure &&
177 typeof failure.message === "string" &&
178 failure.message.trim().length > 0
179 ) {
180 return failure.message;
181 }
182 return "Could not answer the request. Try again.";