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
| 1 | /** |
| 2 | * Provider-specific rules for migrating the released v0 profile store: one |
| 3 | * central `profiles.json` with each provider's saved credentials in a |
| 4 | * `credentials/<name>.json` sidecar. `ProfileStore` runs the migration once |
| 5 | * and moves the original files into a `.profiles-v0-*` backup. |
| 6 | * |
| 7 | * This is a frozen snapshot of the v0 layout, not provider configuration: |
| 8 | * the migration runs before any provider package is loaded and rewrites the |
| 9 | * whole store, so every provider's old filenames must be known here. Delete |
| 10 | * this module once v0 stores no longer need to roll forward. |
| 11 | */ |
| 12 | import * as Schema from "effect/Schema"; |
| 13 | import type { ProviderConfig } from "./Profile.ts"; |
| 14 | |
| 15 | /** The v0 central manifest. */ |
| 16 | export const V0ManifestSchema = Schema.StructWithRest( |
| 17 | Schema.Struct({ |
| 18 | version: Schema.Literal(0), |
| 19 | profiles: Schema.Record(Schema.String, Schema.Unknown), |
| 20 | }), |
| 21 | [Schema.Record(Schema.String, Schema.Unknown)], |
| 22 | ); |
| 23 | |
| 24 | /** Each provider's v0 sidecar name, by its `method` where it had several. */ |
| 25 | const SIDECARS: Record<string, string | Record<string, string>> = { |
| 26 | AWS: "aws-stored", |
| 27 | Axiom: "axiom-stored", |
| 28 | Cloudflare: "cloudflare-stored", |
| 29 | Fly: "fly-stored", |
| 30 | GitHub: "github-stored", |
| 31 | Hetzner: "hetzner-stored", |
| 32 | Neon: "neon-stored", |
| 33 | Planetscale: { |
| 34 | stored: "planetscale-stored", |
| 35 | oauth: "planetscale-oauth", |
| 36 | }, |
| 37 | Prisma: "prisma-stored", |
| 38 | Railway: { |
| 39 | stored: "railway-stored", |
| 40 | oauth: "railway-oauth", |
| 41 | }, |
| 42 | }; |
| 43 | |
| 44 | /** The earliest sidecars used shorter names. */ |
| 45 | const EARLIEST_SIDECARS: Record<string, string> = { |
| 46 | Cloudflare: "cf-stored", |
| 47 | GitHub: "gh-stored", |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * How to migrate one provider's v0 values: keep them as they are |
| 52 | * (`Inline`), or fold in the first sidecar found among `candidates` |
| 53 | * (sidecar names without `.json`), using `fallback` when none is readable. |
| 54 | */ |
| 55 | export type SidecarPlan = |
| 56 | | { readonly _tag: "Inline"; readonly values: ProviderConfig } |
| 57 | | { |
| 58 | readonly _tag: "Sidecar"; |
| 59 | readonly candidates: ReadonlyArray<string>; |
| 60 | readonly fallback: ProviderConfig; |
| 61 | }; |
| 62 | |
| 63 | export const planSidecar = (provider: string, values: ProviderConfig): SidecarPlan => { |
| 64 | // Cloudflare's released OAuth grant is obsolete, but its stored API token |
| 65 | // and global API key formats still map exactly to the new provider-owned |
| 66 | // values schema. |
| 67 | if (provider === "Cloudflare" && values.method !== "stored") { |
| 68 | return { _tag: "Inline", values: { method: "oauth" } }; |
| 69 | } |
| 70 | const configured = SIDECARS[provider]; |
| 71 | const sidecar = |
| 72 | typeof configured === "string" |
| 73 | ? configured |
| 74 | : values.method === undefined |
| 75 | ? undefined |
| 76 | : configured?.[values.method]; |
| 77 | if (sidecar === undefined) return { _tag: "Inline", values }; |
| 78 | const earliest = EARLIEST_SIDECARS[provider]; |
| 79 | return { |
| 80 | _tag: "Sidecar", |
| 81 | candidates: earliest === undefined ? [sidecar] : [sidecar, earliest], |
| 82 | // A Cloudflare entry is only usable with its sidecar's credentials. |
| 83 | fallback: provider === "Cloudflare" ? {} : values, |
| 84 | }; |
| 85 | }; |
| 86 | |
| 87 | /** Fold a decoded sidecar into the provider's v0 manifest values. */ |
| 88 | export const mergeSidecar = ( |
| 89 | provider: string, |
| 90 | values: ProviderConfig, |
| 91 | sidecar: ProviderConfig, |
| 92 | ): ProviderConfig => { |
| 93 | const { type: _type, ...inline } = sidecar; |
| 94 | const normalizedInline = |
| 95 | provider === "Axiom" && inline.apiToken !== undefined |
| 96 | ? (({ apiToken, ...rest }) => ({ ...rest, token: apiToken }))(inline) |
| 97 | : inline; |
| 98 | const { storageKey: _storageKey, ...manifestValues } = values; |
| 99 | const migrated: ProviderConfig = { |
| 100 | ...manifestValues, |
| 101 | ...normalizedInline, |
| 102 | }; |
| 103 | if (provider !== "Cloudflare") return migrated; |
| 104 | return migrated.credentialType === "apiKey" && |
| 105 | typeof migrated.apiKey === "string" && |
| 106 | typeof migrated.email === "string" && |
| 107 | typeof migrated.accountId === "string" |
| 108 | ? migrated |
| 109 | : migrated.credentialType === "apiToken" && |
| 110 | typeof migrated.apiToken === "string" && |
| 111 | typeof migrated.accountId === "string" |
| 112 | ? migrated |
| 113 | : {}; |
| 114 | }; |
| 115 | |
| 116 | /** |
| 117 | * Values to write for a provider whose v0 entry can't be decoded, or |
| 118 | * `undefined` to skip it (it stays in the v0 backup). Cloudflare gets an |
| 119 | * empty profile so `alchemy login` prompts for fresh credentials. |
| 120 | */ |
| 121 | export const undecodableValues = (provider: string): ProviderConfig | undefined => |
| 122 | provider === "Cloudflare" ? {} : undefined; |