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, 04:45 UTC
| 1 | import { msg } from "gt-react-native"; |
| 2 | import { |
| 3 | deriveAvailableInstanceId, |
| 4 | deriveInstanceId, |
| 5 | } from "@t3tools/client-runtime/provider-instance-ids"; |
| 6 | import { |
| 7 | defaultInstanceIdForDriver, |
| 8 | isManagedSettingPath, |
| 9 | ProviderDriverKind, |
| 10 | ProviderInstanceId, |
| 11 | type ManagedPolicySummary, |
| 12 | type ProviderInstanceConfig, |
| 13 | type ProviderInstanceMutation, |
| 14 | type ServerSettings, |
| 15 | } from "@t3tools/contracts"; |
| 16 | |
| 17 | import { m } from "../../i18n/messages"; |
| 18 | |
| 19 | export const CLAUDE_DRIVER = ProviderDriverKind.make("claudeAgent"); |
| 20 | export const DEFAULT_CLAUDE_INSTANCE_ID = defaultInstanceIdForDriver(CLAUDE_DRIVER); |
| 21 | |
| 22 | export interface ClaudeAccountDraft { |
| 23 | readonly name: string; |
| 24 | readonly configDir: string; |
| 25 | } |
| 26 | |
| 27 | function configRecord(instance: ProviderInstanceConfig | undefined): Record<string, unknown> { |
| 28 | const config = instance?.config; |
| 29 | return config !== null && typeof config === "object" ? (config as Record<string, unknown>) : {}; |
| 30 | } |
| 31 | |
| 32 | export function claudeConfigDir(instance: ProviderInstanceConfig | undefined): string { |
| 33 | const homePath = configRecord(instance).homePath; |
| 34 | return typeof homePath === "string" ? homePath : ""; |
| 35 | } |
| 36 | |
| 37 | /** Adds a Claude instance (`instanceId` null) or edits one, keeping every field the form doesn't show. */ |
| 38 | export function planClaudeAccountSave(input: { |
| 39 | readonly providerInstances: ServerSettings["providerInstances"]; |
| 40 | readonly policy: ManagedPolicySummary | undefined; |
| 41 | readonly instanceId: ProviderInstanceId | null; |
| 42 | readonly draft: ClaudeAccountDraft; |
| 43 | }): { readonly mutation: ProviderInstanceMutation } | { readonly error: string } { |
| 44 | const name = input.draft.name.trim(); |
| 45 | const configDir = input.draft.configDir.trim(); |
| 46 | const adding = input.instanceId === null; |
| 47 | if (adding && name.length === 0) return { error: m(msg("Enter a name for this account.")) }; |
| 48 | if (adding && configDir.length === 0) { |
| 49 | return { |
| 50 | error: m( |
| 51 | msg("Enter the {provider} config folder this account is signed in with.", { |
| 52 | provider: "Claude", |
| 53 | }), |
| 54 | ), |
| 55 | }; |
| 56 | } |
| 57 | const instanceId = |
| 58 | input.instanceId ?? |
| 59 | ProviderInstanceId.make( |
| 60 | deriveAvailableInstanceId( |
| 61 | (label) => deriveInstanceId(CLAUDE_DRIVER, label) || CLAUDE_DRIVER, |
| 62 | name, |
| 63 | new Set([DEFAULT_CLAUDE_INSTANCE_ID, ...Object.keys(input.providerInstances)]), |
| 64 | ), |
| 65 | ); |
| 66 | if (input.policy && isManagedSettingPath(input.policy, `providerInstances.${instanceId}`)) { |
| 67 | return { |
| 68 | error: m(msg("Managed by {organization}.", { organization: input.policy.organization })), |
| 69 | }; |
| 70 | } |
| 71 | const current: ProviderInstanceConfig = Object.hasOwn(input.providerInstances, instanceId) |
| 72 | ? input.providerInstances[instanceId]! |
| 73 | : { driver: CLAUDE_DRIVER }; |
| 74 | const { displayName: _displayName, config: _config, ...rest } = current; |
| 75 | const { homePath: _homePath, ...config } = configRecord(current); |
| 76 | const nextConfig = configDir ? { ...config, homePath: configDir } : config; |
| 77 | const instance: ProviderInstanceConfig = { |
| 78 | ...rest, |
| 79 | ...(adding ? { enabled: true } : {}), |
| 80 | ...(name ? { displayName: name } : {}), |
| 81 | ...(Object.keys(nextConfig).length > 0 ? { config: nextConfig } : {}), |
| 82 | }; |
| 83 | return { |
| 84 | mutation: { operation: adding ? "create" : "upsert", instanceId, instance }, |
| 85 | }; |
| 86 | } |