test-fal-connection.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { fal } from './fal-client';
  2. // 类型定义
  3. interface TestResult {
  4. data?: {
  5. images?: Array<{ url: string }>;
  6. };
  7. }
  8. // 测试 fal AI 连接
  9. export async function testFalConnection(): Promise<{ success: boolean; message: string }> {
  10. try {
  11. // 检查API密钥是否已配置
  12. if (!process.env.FAL_KEY) {
  13. return {
  14. success: false,
  15. message: 'FAL_KEY 环境变量未配置。请在 .env.local 文件中设置您的 fal AI API 密钥。'
  16. };
  17. }
  18. // 尝试调用 Kontext Pro 模型来测试连接
  19. const result = await fal.subscribe("fal-ai/flux-pro/kontext", {
  20. input: {
  21. prompt: "test connection - make it slightly brighter",
  22. image_url: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==",
  23. guidance_scale: 3.5,
  24. sync_mode: true,
  25. num_images: 1,
  26. },
  27. logs: true,
  28. }) as TestResult;
  29. if (result && result.data && result.data.images && result.data.images.length > 0) {
  30. return {
  31. success: true,
  32. message: '✅ Fal AI Kontext Pro 连接成功!现在可以使用最新的前沿图像编辑模型。'
  33. };
  34. } else {
  35. return {
  36. success: false,
  37. message: 'API 调用成功但未返回预期结果。请检查您的 API 配额。'
  38. };
  39. }
  40. } catch (error) {
  41. console.error('Fal AI connection test failed:', error);
  42. if (error instanceof Error) {
  43. if (error.message.includes('401') || error.message.includes('Unauthorized')) {
  44. return {
  45. success: false,
  46. message: 'API 密钥无效。请检查您的 FAL_KEY 配置。访问 https://fal.ai 获取有效密钥。'
  47. };
  48. }
  49. if (error.message.includes('quota') || error.message.includes('limit')) {
  50. return {
  51. success: false,
  52. message: 'API 配额已用完。请检查您的 fal.ai 账户余额或升级套餐。'
  53. };
  54. }
  55. if (error.message.includes('403') || error.message.includes('Forbidden')) {
  56. return {
  57. success: false,
  58. message: '无权限访问 Kontext Pro 模型。请确保您的账户已开通相应权限。'
  59. };
  60. }
  61. return {
  62. success: false,
  63. message: `连接失败: ${error.message}`
  64. };
  65. }
  66. return {
  67. success: false,
  68. message: '未知错误,请稍后重试。'
  69. };
  70. }
  71. }
  72. // 获取模型信息
  73. export async function getFalAccountInfo() {
  74. try {
  75. return {
  76. success: true,
  77. message: '如需查看账户信息,请访问 https://fal.ai 控制台',
  78. models: {
  79. 'kontext-pro': 'FLUX.1 Kontext [pro] - 前沿图像编辑模型'
  80. },
  81. features: [
  82. '智能图像编辑',
  83. '精确细节调整',
  84. '创意风格变换',
  85. '背景移除'
  86. ],
  87. documentation: 'https://fal.ai/models/fal-ai/flux-pro/kontext/api'
  88. };
  89. } catch (error) {
  90. return {
  91. success: false,
  92. message: '无法获取模型信息'
  93. };
  94. }
  95. }