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