|
| 1 | +import { createOpenAI } from '@xsai-ext/providers/create' |
| 2 | +import { z } from 'zod' |
| 3 | + |
| 4 | +import { createOpenAICompatibleValidators } from '../../validators/openai-compatible' |
| 5 | +import { defineProvider } from '../registry' |
| 6 | + |
| 7 | +const n1nConfigSchema = z.object({ |
| 8 | + apiKey: z |
| 9 | + .string('API Key') |
| 10 | + .optional(), |
| 11 | + baseUrl: z |
| 12 | + .string('Base URL') |
| 13 | + .optional() |
| 14 | + .default('https://api.n1n.ai/v1'), |
| 15 | +}) |
| 16 | + |
| 17 | +type N1NConfig = z.input<typeof n1nConfigSchema> |
| 18 | + |
| 19 | +export const providerN1N = defineProvider<N1NConfig>({ |
| 20 | + id: 'n1n', |
| 21 | + order: 9, |
| 22 | + name: 'n1n', |
| 23 | + nameLocalize: ({ t }) => t('settings.pages.providers.provider.n1n.title'), |
| 24 | + description: 'n1n.ai - High-performance AI API provider.', |
| 25 | + descriptionLocalize: ({ t }) => t('settings.pages.providers.provider.n1n.description'), |
| 26 | + tasks: ['chat'], |
| 27 | + icon: 'i-lobe-icons:openai', |
| 28 | + |
| 29 | + createProviderConfig: ({ t }) => n1nConfigSchema.extend({ |
| 30 | + apiKey: n1nConfigSchema.shape.apiKey.meta({ |
| 31 | + labelLocalized: t('settings.pages.providers.catalog.edit.config.common.fields.field.api-key.label'), |
| 32 | + descriptionLocalized: t('settings.pages.providers.catalog.edit.config.common.fields.field.api-key.description'), |
| 33 | + placeholderLocalized: t('settings.pages.providers.catalog.edit.config.common.fields.field.api-key.placeholder'), |
| 34 | + type: 'password', |
| 35 | + }), |
| 36 | + baseUrl: n1nConfigSchema.shape.baseUrl.meta({ |
| 37 | + labelLocalized: t('settings.pages.providers.catalog.edit.config.common.fields.field.base-url.label'), |
| 38 | + descriptionLocalized: t('settings.pages.providers.catalog.edit.config.common.fields.field.base-url.description'), |
| 39 | + placeholderLocalized: t('settings.pages.providers.catalog.edit.config.common.fields.field.base-url.placeholder'), |
| 40 | + }), |
| 41 | + }), |
| 42 | + createProvider(config) { |
| 43 | + return createOpenAI(config.apiKey || '', config.baseUrl) |
| 44 | + }, |
| 45 | + |
| 46 | + validationRequiredWhen(config) { |
| 47 | + return !!config.apiKey?.trim() |
| 48 | + }, |
| 49 | + validators: { |
| 50 | + ...createOpenAICompatibleValidators({ |
| 51 | + checks: ['connectivity', 'model_list', 'chat_completions'], |
| 52 | + }), |
| 53 | + }, |
| 54 | +}) |
0 commit comments