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

secretRequest.ts

51 lines1.9 KB

1import * as Schema from "effect/Schema";
2​
3import { ThreadId, TrimmedNonEmptyString, TurnItemId } from "./baseSchemas.ts";
4​
5/**
6 * A saved value is kept by the server under a one-use SecretRef; the thread
7 * learns only the status.
8 */
9export const SecretRequestAnswerInput = Schema.Struct({
10 threadId: ThreadId,
11 turnItemId: TurnItemId,
12 answer: Schema.Union([
13 Schema.Struct({ type: Schema.Literal("save"), secret: TrimmedNonEmptyString }),
14 Schema.Struct({ type: Schema.Literal("decline") }),
15 ]),
16});
17export type SecretRequestAnswerInput = typeof SecretRequestAnswerInput.Type;
18​
19const SECRET_REQUEST_FAILURE_MESSAGES = {
20 load_failed: "Could not load the secret request.",
21 not_found: "This secret request no longer exists.",
22 already_answered: "This secret request was already answered.",
23 agent_stopped: "The agent that asked has stopped, so this secret can't be used.",
24 store_failed: "Could not store the secret.",
25 record_failed: "Saved the secret, but could not update the request.",
26 invalid_ref: "That secretRef is not valid.",
27 read_failed: "Could not read the secret.",
28 ref_unavailable:
29 "That secretRef was already used or does not exist. Ask the user again with request_secret.",
30 ref_expired: "That secretRef expired. Ask the user again with request_secret.",
31 consume_failed: "Could not use that secretRef. Try again.",
32} as const;
33​
34export const SecretRequestFailureReason = Schema.Literals(
35 Object.keys(SECRET_REQUEST_FAILURE_MESSAGES) as Array<
36 keyof typeof SECRET_REQUEST_FAILURE_MESSAGES
37 >,
38);
39export type SecretRequestFailureReason = typeof SecretRequestFailureReason.Type;
40​
41export class SecretRequestError extends Schema.TaggedError<SecretRequestError>()(
42 "SecretRequestError",
43 {
44 reason: SecretRequestFailureReason,
45 cause: Schema.optionalKey(Schema.Defect()),
46 },
47) {
48 override get message(): string {
49 return SECRET_REQUEST_FAILURE_MESSAGES[this.reason];
50 }