(options = {})
| 2537 | } |
| 2538 | |
| 2539 | async function ensureLuckmailPurchaseForFlow(options = {}) { |
| 2540 | const { allowReuse = true } = options; |
| 2541 | const state = await getState(); |
| 2542 | const existingPurchase = getCurrentLuckmailPurchase(state); |
| 2543 | if (allowReuse && existingPurchase?.email_address && existingPurchase?.token) { |
| 2544 | if (state.email !== existingPurchase.email_address) { |
| 2545 | await setEmailState(existingPurchase.email_address); |
| 2546 | } |
| 2547 | return existingPurchase; |
| 2548 | } |
| 2549 | |
| 2550 | const config = getLuckmailSessionConfig(state); |
| 2551 | const client = createLuckmailClient(state); |
| 2552 | if (allowReuse) { |
| 2553 | const reusablePurchase = await findReusableLuckmailPurchaseForFlow(state, client); |
| 2554 | if (reusablePurchase) { |
| 2555 | return activateLuckmailPurchaseForFlow(state, client, reusablePurchase, { |
| 2556 | initializeCursor: true, |
| 2557 | logMessage: `LuckMail:已复用 openai 邮箱 ${reusablePurchase.email_address}`, |
| 2558 | }); |
| 2559 | } |
| 2560 | } |
| 2561 | |
| 2562 | const result = await client.user.purchaseEmails(DEFAULT_LUCKMAIL_PROJECT_CODE, 1, { |
| 2563 | emailType: config.emailType, |
| 2564 | domain: config.domain || undefined, |
| 2565 | }); |
| 2566 | const purchases = normalizeLuckmailPurchases(result); |
| 2567 | const purchase = purchases[0] || null; |
| 2568 | if (!purchase?.email_address || !purchase?.token) { |
| 2569 | throw new Error('LuckMail 购邮成功,但未返回可用邮箱或 token。'); |
| 2570 | } |
| 2571 | |
| 2572 | return activateLuckmailPurchaseForFlow(state, client, purchase, { |
| 2573 | initializeCursor: false, |
| 2574 | logMessage: `LuckMail:已购买邮箱 ${purchase.email_address}(类型:${config.emailType},项目:${DEFAULT_LUCKMAIL_PROJECT_CODE})`, |
| 2575 | }); |
| 2576 | } |
| 2577 | |
| 2578 | async function resolveLuckmailVerificationMail(client, token, filters = {}, tokenCodeResult = null) { |
| 2579 | const tokenCode = tokenCodeResult ? normalizeLuckmailTokenCode(tokenCodeResult) : null; |
no test coverage detected