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

secretRequest.ts

69 lines3.1 KB

1import * as Schema from "effect/Schema";
2​
3import { ThreadId, TrimmedNonEmptyString, TurnItemId } from "./baseSchemas.ts";
4import { SecretReference } from "./secretReference.ts";
5​
6/** Server stores the value under a one-use SecretRef; the thread learns only status. */
7export const SecretRequestAnswerInput = Schema.Struct({
8 threadId: ThreadId,
9 turnItemId: TurnItemId,
10 answer: Schema.Union([
11 Schema.Struct({ type: Schema.Literal("save"), secret: TrimmedNonEmptyString }),
12 Schema.Struct({ type: Schema.Literal("decline") }),
13 /** Read once through a configured secret provider; only servers advertising `secretRequestReferences` accept it. */
14 Schema.Struct({ type: Schema.Literal("reference"), reference: SecretReference }),
15 ]),
16});
17export type SecretRequestAnswerInput = typeof SecretRequestAnswerInput.Type;
18​
19/** What the agent reads when its card ends without a secret. */
20export const SECRET_REQUEST_UNSAVED_NOTE = {
21 declined:
22 "The user declined. Don't ask for it in chat or another way; say what's blocked and go on without it.",
23 cancelled:
24 "The card closed with the run. Ask again with a new clientRequestId only if the secret is still needed.",
25 timed_out:
26 "No answer in time. Don't ask in chat; ask again with a new clientRequestId only if it is still needed.",
27} as const;
28​
29const SECRET_REQUEST_FAILURE_MESSAGES = {
30 load_failed: "Could not load the secret request.",
31 not_found: "This secret request no longer exists.",
32 already_answered: "This secret request was already answered.",
33 agent_stopped: "The agent that asked has stopped, so this secret can't be used.",
34 store_failed: "Could not store the secret.",
35 reference_unreadable: "Could not read the secret from the secret provider.",
36 record_failed: "Saved the secret, but could not update the request.",
37 invalid_ref: "That secretRef is not valid.",
38 read_failed: "Could not read the secret.",
39 ref_unavailable:
40 "That secretRef was already used or does not exist. Ask the user again with request_secret.",
41 ref_expired: "That secretRef expired. Ask the user again with request_secret.",
42 consume_failed: "Could not use that secretRef. Try again.",
43 not_a_file_request:
44 'The user did not save this secret for shell use. Ask again with request_secret and delivery: "file".',
45} as const;
46​
47export const SecretRequestFailureReason = Schema.Literals(
48 Object.keys(SECRET_REQUEST_FAILURE_MESSAGES) as Array<
49 keyof typeof SECRET_REQUEST_FAILURE_MESSAGES
50 >,
51);
52export type SecretRequestFailureReason = typeof SecretRequestFailureReason.Type;
53​
54export class SecretRequestError extends Schema.TaggedError<SecretRequestError>()(
55 "SecretRequestError",
56 {
57 reason: SecretRequestFailureReason,
58 /** The secret provider a `reference_unreadable` answer named. */
59 provider: Schema.optionalKey(Schema.String),
60 cause: Schema.optionalKey(Schema.Defect()),
61 },
62) {
63 override get message(): string {
64 if (this.reason === "reference_unreadable" && this.provider !== undefined) {
65 return `Could not read the secret from the secret provider "${this.provider}". Unlock it and try again, or enter the value.`;
66 }
67 return SECRET_REQUEST_FAILURE_MESSAGES[this.reason];
68 }