@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 135 lines 3.92 KB
Raw
1 import { DEFAULT_API_BASE_URL } from "@distilled.cloud/axiom/Credentials";
2 import { AuthError } from "alchemy/Auth/AuthProvider";
3 import { getEnv, getEnvRedacted } from "alchemy/Auth/Env";
4 import {
5 makeStoredAuthProvider,
6 storedSecret,
7 storedValueText,
8 type StoredAuthConfig,
9 } from "alchemy/Auth/StoredAuthProvider";
10 import * as Effect from "effect/Effect";
11 import * as Redacted from "effect/Redacted";
12
13 export const AXIOM_AUTH_PROVIDER_NAME = "Axiom";
14
15 export type AxiomAuthConfig = StoredAuthConfig;
16
17 /**
18 * Resolved Axiom credentials. The provider values are a flat record
19 * (token + optional orgId/apiBaseUrl); the `apiToken`/`pat` distinction is
20 * derived at resolution time from orgId presence — an org id means the token
21 * is treated as a personal access token, mirroring how the CI environment
22 * resolution has always classified `AXIOM_ORG_ID`.
23 */
24 export type AxiomResolvedCredentials =
25 | {
26 type: "apiToken";
27 apiToken: Redacted.Redacted<string>;
28 apiBaseUrl: string;
29 orgId?: string;
30 source: { type: AxiomAuthConfig["method"] | "env"; details?: string };
31 }
32 | {
33 type: "pat";
34 apiToken: Redacted.Redacted<string>;
35 apiBaseUrl: string;
36 orgId: string;
37 source: { type: AxiomAuthConfig["method"] | "env"; details?: string };
38 };
39
40 const readEnvironment = Effect.gen(function* () {
41 const apiToken =
42 (yield* getEnvRedacted("AXIOM_TOKEN")) ?? (yield* getEnvRedacted("AXIOM_API_KEY"));
43 if (!apiToken) {
44 return yield* AuthError.make({
45 message: "Axiom CI credentials not found. Set AXIOM_TOKEN or AXIOM_API_KEY.",
46 });
47 }
48 const apiBaseUrl = (yield* getEnv("AXIOM_URL")) ?? DEFAULT_API_BASE_URL;
49 const orgId = yield* getEnv("AXIOM_ORG_ID");
50 return orgId
51 ? {
52 type: "pat" as const,
53 apiToken,
54 apiBaseUrl,
55 orgId,
56 source: { type: "env" as const },
57 }
58 : {
59 type: "apiToken" as const,
60 apiToken,
61 apiBaseUrl,
62 source: { type: "env" as const },
63 };
64 });
65
66 const axiomAuth = makeStoredAuthProvider<AxiomResolvedCredentials>({
67 provider: AXIOM_AUTH_PROVIDER_NAME,
68 fields: [
69 {
70 name: "token",
71 label: "Axiom API Token or Personal Access Token",
72 secret: true,
73 },
74 {
75 name: "orgId",
76 label: "Axiom Org ID (required for personal access tokens)",
77 optional: true,
78 },
79 {
80 name: "apiBaseUrl",
81 label: "Axiom API Base URL",
82 optional: true,
83 placeholder: DEFAULT_API_BASE_URL,
84 },
85 ],
86 toResolved: (values, source) => {
87 const apiToken = storedSecret(values.token) ?? Redacted.make("");
88 const apiBaseUrl = storedValueText(values.apiBaseUrl) ?? DEFAULT_API_BASE_URL;
89 return values.orgId !== undefined
90 ? {
91 type: "pat",
92 apiToken,
93 apiBaseUrl,
94 orgId: storedValueText(values.orgId) ?? "",
95 source: { type: source },
96 }
97 : {
98 type: "apiToken",
99 apiToken,
100 apiBaseUrl,
101 source: { type: source },
102 };
103 },
104 readEnvironment,
105 environment: [
106 {
107 name: "AXIOM_TOKEN",
108 required: true,
109 secret: true,
110 alternatives: ["AXIOM_API_KEY"],
111 description: "Personal access token or API token.",
112 },
113 {
114 name: "AXIOM_URL",
115 required: false,
116 description: "API base URL for self-hosted or regional deployments.",
117 },
118 {
119 name: "AXIOM_ORG_ID",
120 required: false,
121 description: "Organization id; required when the token is a personal access token.",
122 },
123 ],
124 });
125
126 /**
127 * Layer that registers the Axiom {@link AuthProvider} into the
128 * {@link AuthProviders} registry when built. Include this in the Axiom
129 * `providers()` layer so the alchemy CLI can discover it.
130 */
131 export const AxiomAuth = axiomAuth.layer;
132
133 /** Schema of Axiom's inline static-token values. */
134 export const AxiomStoredCredentials = axiomAuth.storedSchema;
135 export type AxiomStoredCredentials = typeof AxiomStoredCredentials.Type;