{"version":3,"file":"cloudflare-auth.d.ts","sourceRoot":"","sources":["../../src/providers/cloudflare-auth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAiC,MAAM,kBAAkB,CAAC;AAqDlF,wBAAgB,uBAAuB,IAAI,UAAU,CAkBpD;AAED,wBAAgB,uBAAuB,IAAI,UAAU,CA6BpD","sourcesContent":["import type { ApiKeyAuth, ApiKeyCredential, AuthContext } from \"../auth/types.ts\";\nimport type { ProviderEnv } from \"../types.ts\";\n\nconst CLOUDFLARE_API_KEY = \"CLOUDFLARE_API_KEY\";\nconst CLOUDFLARE_ACCOUNT_ID = \"CLOUDFLARE_ACCOUNT_ID\";\nconst CLOUDFLARE_GATEWAY_ID = \"CLOUDFLARE_GATEWAY_ID\";\n\ntype CloudflareAuthKind = \"workers-ai\" | \"ai-gateway\";\n\nasync function resolveValue(\n\tname: string,\n\tctx: AuthContext,\n\tcredential: ApiKeyCredential | undefined,\n\tsignal: AbortSignal,\n): Promise<string | undefined> {\n\t// Per-field merge: prefer the credential value, fall back to ambient env.\n\t// A credential carrying only the API key must still pick up the account /\n\t// gateway id from the environment.\n\tconst fromCredential = credential\n\t\t? name === CLOUDFLARE_API_KEY\n\t\t\t? credential.key\n\t\t\t: credential.env?.[name]\n\t\t: undefined;\n\tif (fromCredential !== undefined) return fromCredential;\n\tsignal.throwIfAborted();\n\tconst value = await ctx.env(name);\n\tsignal.throwIfAborted();\n\treturn value;\n}\n\nasync function resolveCloudflareEnv(\n\tkind: CloudflareAuthKind,\n\tctx: AuthContext,\n\tcredential: ApiKeyCredential | undefined,\n\tsignal: AbortSignal,\n): Promise<{ apiKey: string; env: ProviderEnv; source: string } | undefined> {\n\tconst apiKey = await resolveValue(CLOUDFLARE_API_KEY, ctx, credential, signal);\n\tconst accountId = await resolveValue(CLOUDFLARE_ACCOUNT_ID, ctx, credential, signal);\n\tconst gatewayId =\n\t\tkind === \"ai-gateway\" ? await resolveValue(CLOUDFLARE_GATEWAY_ID, ctx, credential, signal) : undefined;\n\n\tif (!apiKey || !accountId || (kind === \"ai-gateway\" && !gatewayId)) return undefined;\n\n\treturn {\n\t\tapiKey,\n\t\tenv: {\n\t\t\tCLOUDFLARE_ACCOUNT_ID: accountId,\n\t\t\t...(gatewayId ? { CLOUDFLARE_GATEWAY_ID: gatewayId } : {}),\n\t\t},\n\t\tsource: credential ? \"stored credential\" : CLOUDFLARE_API_KEY,\n\t};\n}\n\nexport function cloudflareWorkersAIAuth(): ApiKeyAuth {\n\treturn {\n\t\tname: \"Cloudflare API key\",\n\t\tlogin: async (interaction) => {\n\t\t\tconst key = await interaction.prompt({ type: \"secret\", message: \"Enter Cloudflare API key\" });\n\t\t\tconst accountId = await interaction.prompt({ type: \"text\", message: \"Enter Cloudflare account ID\" });\n\t\t\treturn { type: \"api_key\", key, env: { CLOUDFLARE_ACCOUNT_ID: accountId } };\n\t\t},\n\t\tresolve: async ({ ctx, credential, signal }) => {\n\t\t\tconst resolved = await resolveCloudflareEnv(\"workers-ai\", ctx, credential, signal);\n\t\t\tif (!resolved) return undefined;\n\t\t\treturn {\n\t\t\t\tauth: { apiKey: resolved.apiKey },\n\t\t\t\tenv: resolved.env,\n\t\t\t\tsource: resolved.source,\n\t\t\t};\n\t\t},\n\t};\n}\n\nexport function cloudflareAIGatewayAuth(): ApiKeyAuth {\n\treturn {\n\t\tname: \"Cloudflare API key\",\n\t\tlogin: async (interaction) => {\n\t\t\tconst key = await interaction.prompt({ type: \"secret\", message: \"Enter Cloudflare API key\" });\n\t\t\tconst accountId = await interaction.prompt({ type: \"text\", message: \"Enter Cloudflare account ID\" });\n\t\t\tconst gatewayId = await interaction.prompt({ type: \"text\", message: \"Enter Cloudflare AI Gateway ID\" });\n\t\t\treturn {\n\t\t\t\ttype: \"api_key\",\n\t\t\t\tkey,\n\t\t\t\tenv: { CLOUDFLARE_ACCOUNT_ID: accountId, CLOUDFLARE_GATEWAY_ID: gatewayId },\n\t\t\t};\n\t\t},\n\t\tresolve: async ({ ctx, credential, signal }) => {\n\t\t\tconst resolved = await resolveCloudflareEnv(\"ai-gateway\", ctx, credential, signal);\n\t\t\tif (!resolved) return undefined;\n\t\t\treturn {\n\t\t\t\tauth: {\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"cf-aig-authorization\": `Bearer ${resolved.apiKey}`,\n\t\t\t\t\t\tAuthorization: null,\n\t\t\t\t\t\t\"x-api-key\": null,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\tenv: resolved.env,\n\t\t\t\tsource: resolved.source,\n\t\t\t};\n\t\t},\n\t};\n}\n"]}