feat(api-ai): add provider registry and routing fallback

This commit is contained in:
2026-04-06 11:44:05 +08:00
parent 019436507e
commit 180f7a9baa
11 changed files with 1425 additions and 1 deletions
@@ -0,0 +1,28 @@
import { Injectable } from "@nestjs/common";
import { AiChannel } from "../../generated/prisma/client";
import { AstrbotProvider } from "./providers/astrbot.provider";
import { OpenAiCompatibleProvider } from "./providers/openai-compatible.provider";
import { AiChannelExecutor } from "./ai.types";
@Injectable()
export class AiProviderRegistryService {
private readonly executors = new Map<AiChannel, AiChannelExecutor>();
constructor(
openAiCompatibleProvider: OpenAiCompatibleProvider,
astrbotProvider: AstrbotProvider
) {
this.executors.set(AiChannel.USER_KEY, openAiCompatibleProvider);
this.executors.set(AiChannel.PUBLIC_POOL, openAiCompatibleProvider);
this.executors.set(AiChannel.ASTRBOT, astrbotProvider);
}
getExecutor(channel: AiChannel): AiChannelExecutor {
const executor = this.executors.get(channel);
if (!executor) {
throw new Error(`未找到 ${channel} 对应的 AI 通道执行器`);
}
return executor;
}
}
+41
View File
@@ -0,0 +1,41 @@
import { Body, Controller, Get, Headers, Post, UnauthorizedException } from "@nestjs/common";
import { AiChatDto } from "./dto/ai-chat.dto";
import { UpsertAiProviderBindingDto } from "./dto/upsert-ai-provider-binding.dto";
import { AiChatResponse, AiService, ListAiBindingsResponse } from "./ai.service";
@Controller("ai")
export class AiController {
constructor(private readonly aiService: AiService) {}
@Get("bindings")
async listBindings(
@Headers("x-user-id") userIdHeader: string | string[] | undefined
): Promise<ListAiBindingsResponse> {
return this.aiService.listBindings(this.resolveUserId(userIdHeader));
}
@Post("bindings")
async upsertBinding(
@Headers("x-user-id") userIdHeader: string | string[] | undefined,
@Body() body: UpsertAiProviderBindingDto
) {
return this.aiService.upsertBinding(this.resolveUserId(userIdHeader), body);
}
@Post("chat")
async chat(
@Headers("x-user-id") userIdHeader: string | string[] | undefined,
@Body() body: AiChatDto
): Promise<AiChatResponse> {
return this.aiService.chat(this.resolveUserId(userIdHeader), body);
}
private resolveUserId(userIdHeader: string | string[] | undefined): string {
const userId = Array.isArray(userIdHeader) ? userIdHeader[0] : userIdHeader;
if (!userId) {
throw new UnauthorizedException("缺少用户上下文");
}
return userId;
}
}
+14
View File
@@ -0,0 +1,14 @@
import { Module } from "@nestjs/common";
import { PrismaModule } from "../prisma/prisma.module";
import { AiController } from "./ai.controller";
import { AiProviderRegistryService } from "./ai-provider-registry.service";
import { AiService } from "./ai.service";
import { AstrbotProvider } from "./providers/astrbot.provider";
import { OpenAiCompatibleProvider } from "./providers/openai-compatible.provider";
@Module({
imports: [PrismaModule],
controllers: [AiController],
providers: [AiService, AiProviderRegistryService, OpenAiCompatibleProvider, AstrbotProvider]
})
export class AiModule {}
+430
View File
@@ -0,0 +1,430 @@
import {
BadGatewayException,
BadRequestException,
Injectable,
Logger,
NotFoundException
} from "@nestjs/common";
import {
AiChannel,
AiProviderBinding,
AiPublicPoolConfig,
Prisma
} from "../../generated/prisma/client";
import { PrismaService } from "../prisma/prisma.service";
import { AiProviderRegistryService } from "./ai-provider-registry.service";
import { AiChatDto } from "./dto/ai-chat.dto";
import { UpsertAiProviderBindingDto } from "./dto/upsert-ai-provider-binding.dto";
import { AiResolvedRouteCandidate, AiRouteAttempt, AiRouteFailureError } from "./ai.types";
type AiBindingSummary = {
id: string;
channel: AiChannel;
providerName: string;
model: string | null;
endpoint: string | null;
isDefault: boolean;
isEnabled: boolean;
hasApiKey: boolean;
maskedApiKey: string | null;
updatedAt: string;
};
type AiRoutePlanEntry =
| {
kind: "candidate";
candidate: AiResolvedRouteCandidate;
}
| {
kind: "skip";
attempt: AiRouteAttempt;
};
export type ListAiBindingsResponse = {
routeOrder: AiChannel[];
bindings: AiBindingSummary[];
publicPool: {
enabled: boolean;
providerName: string | null;
model: string | null;
endpoint: string | null;
hasApiKey: boolean;
} | null;
};
export type AiChatResponse = {
channel: AiChannel;
providerName: string;
model: string | null;
content: string;
sessionId: string | null;
attempts: AiRouteAttempt[];
};
@Injectable()
export class AiService {
private readonly logger = new Logger(AiService.name);
constructor(
private readonly prismaService: PrismaService,
private readonly aiProviderRegistryService: AiProviderRegistryService
) {}
async listBindings(userId: string): Promise<ListAiBindingsResponse> {
const [bindings, publicPool] = await Promise.all([
this.prismaService.aiProviderBinding.findMany({
where: {
userId
},
orderBy: [{ channel: "asc" }, { isDefault: "desc" }, { updatedAt: "desc" }]
}),
this.prismaService.aiPublicPoolConfig.findFirst({
orderBy: {
updatedAt: "desc"
}
})
]);
return {
routeOrder: [AiChannel.USER_KEY, AiChannel.ASTRBOT, AiChannel.PUBLIC_POOL],
bindings: bindings.map((binding) => this.serializeBinding(binding)),
publicPool: publicPool
? {
enabled: publicPool.enabled,
providerName: publicPool.providerName,
model: publicPool.model,
endpoint: publicPool.endpoint,
hasApiKey: Boolean(publicPool.encryptedApiKey)
}
: null
};
}
async upsertBinding(userId: string, dto: UpsertAiProviderBindingDto): Promise<AiBindingSummary> {
if (dto.channel === AiChannel.PUBLIC_POOL) {
throw new BadRequestException("公共 AI 通道只能由管理员配置");
}
const result = await this.prismaService.$transaction(async (tx) => {
if (dto.isDefault) {
const where: Prisma.AiProviderBindingWhereInput = {
userId,
channel: dto.channel
};
if (dto.id) {
where.id = {
not: dto.id
};
}
await tx.aiProviderBinding.updateMany({
where,
data: {
isDefault: false
}
});
}
if (!dto.id) {
return tx.aiProviderBinding.create({
data: {
userId,
channel: dto.channel,
providerName: dto.providerName.trim(),
model: this.normalizeOptionalString(dto.model),
endpoint: this.normalizeOptionalString(dto.endpoint),
encryptedApiKey: this.normalizeOptionalString(dto.apiKey),
isDefault: dto.isDefault ?? false,
isEnabled: dto.isEnabled ?? true
}
});
}
const existingBinding = await tx.aiProviderBinding.findFirst({
where: {
id: dto.id,
userId
}
});
if (!existingBinding) {
throw new NotFoundException("AI 通道配置不存在");
}
const updateData: Prisma.AiProviderBindingUpdateInput = {
channel: dto.channel,
providerName: dto.providerName.trim(),
model: this.normalizeOptionalString(dto.model),
isDefault: dto.isDefault ?? existingBinding.isDefault,
isEnabled: dto.isEnabled ?? existingBinding.isEnabled
};
if (dto.endpoint !== undefined) {
updateData.endpoint = this.normalizeOptionalString(dto.endpoint);
}
if (dto.apiKey !== undefined) {
updateData.encryptedApiKey = this.normalizeOptionalString(dto.apiKey);
}
return tx.aiProviderBinding.update({
where: {
id: dto.id
},
data: updateData
});
});
return this.serializeBinding(result);
}
async chat(userId: string, dto: AiChatDto): Promise<AiChatResponse> {
const attempts: AiRouteAttempt[] = [];
const plan = await this.buildRoutePlan(userId, dto.bindingId ?? null);
for (const entry of plan) {
if (entry.kind === "skip") {
attempts.push(entry.attempt);
continue;
}
const executor = this.aiProviderRegistryService.getExecutor(entry.candidate.channel);
try {
const result = await executor.execute(entry.candidate, {
userId,
message: dto.message,
sessionId: dto.sessionId ?? null
});
attempts.push({
channel: result.channel,
providerName: result.providerName,
model: result.model,
status: "success",
reasonCode: null,
reasonMessage: null
});
return {
channel: result.channel,
providerName: result.providerName,
model: result.model,
content: result.content,
sessionId: result.sessionId,
attempts
};
} catch (error) {
const failureAttempt = this.toFailureAttempt(entry.candidate, error);
attempts.push(failureAttempt);
this.logger.warn(
`AI 通道降级:channel=${failureAttempt.channel} provider=${failureAttempt.providerName ?? "unknown"} code=${failureAttempt.reasonCode ?? "UNKNOWN"} message=${failureAttempt.reasonMessage ?? "unknown"}`
);
}
}
throw new BadGatewayException({
message: "当前没有可用的 AI 通道,请稍后重试",
attempts
});
}
private async buildRoutePlan(
userId: string,
bindingId: string | null
): Promise<AiRoutePlanEntry[]> {
const plan: AiRoutePlanEntry[] = [];
const consumedChannels = new Set<AiChannel>();
if (bindingId) {
const pinnedBinding = await this.prismaService.aiProviderBinding.findFirst({
where: {
id: bindingId,
userId,
isEnabled: true
}
});
if (!pinnedBinding) {
throw new NotFoundException("指定的 AI 通道配置不存在或已禁用");
}
plan.push({
kind: "candidate",
candidate: this.toBindingCandidate(pinnedBinding)
});
consumedChannels.add(pinnedBinding.channel);
}
for (const channel of [AiChannel.USER_KEY, AiChannel.ASTRBOT]) {
if (consumedChannels.has(channel)) {
continue;
}
const binding = await this.findPreferredBinding(userId, channel);
if (binding) {
plan.push({
kind: "candidate",
candidate: this.toBindingCandidate(binding)
});
continue;
}
plan.push({
kind: "skip",
attempt: {
channel,
providerName: null,
model: null,
status: "skipped",
reasonCode: "CHANNEL_NOT_CONFIGURED",
reasonMessage:
channel === AiChannel.USER_KEY
? "当前用户未配置可用的自备 Key 通道"
: "当前用户未配置可用的 AstrBot 通道"
}
});
}
const publicPool = await this.findEnabledPublicPool();
if (publicPool) {
plan.push({
kind: "candidate",
candidate: this.toPublicPoolCandidate(publicPool)
});
} else {
plan.push({
kind: "skip",
attempt: {
channel: AiChannel.PUBLIC_POOL,
providerName: null,
model: null,
status: "skipped",
reasonCode: "PUBLIC_POOL_DISABLED",
reasonMessage: "公共 AI 通道未开启"
}
});
}
return plan;
}
private async findPreferredBinding(
userId: string,
channel: AiChannel
): Promise<AiProviderBinding | null> {
return this.prismaService.aiProviderBinding.findFirst({
where: {
userId,
channel,
isEnabled: true
},
orderBy: [{ isDefault: "desc" }, { updatedAt: "desc" }]
});
}
private async findEnabledPublicPool(): Promise<AiPublicPoolConfig | null> {
return this.prismaService.aiPublicPoolConfig.findFirst({
where: {
enabled: true
},
orderBy: {
updatedAt: "desc"
}
});
}
private toBindingCandidate(binding: AiProviderBinding): AiResolvedRouteCandidate {
return {
channel: binding.channel,
source: "binding",
sourceId: binding.id,
providerName: binding.providerName,
model: binding.model,
endpoint: binding.endpoint,
apiKey: binding.encryptedApiKey
};
}
private toPublicPoolCandidate(publicPool: AiPublicPoolConfig): AiResolvedRouteCandidate {
return {
channel: AiChannel.PUBLIC_POOL,
source: "public_pool",
sourceId: publicPool.id,
providerName: publicPool.providerName ?? "public-pool",
model: publicPool.model,
endpoint: publicPool.endpoint,
apiKey: publicPool.encryptedApiKey
};
}
private serializeBinding(binding: AiProviderBinding): AiBindingSummary {
return {
id: binding.id,
channel: binding.channel,
providerName: binding.providerName,
model: binding.model,
endpoint: binding.endpoint,
isDefault: binding.isDefault,
isEnabled: binding.isEnabled,
hasApiKey: Boolean(binding.encryptedApiKey),
maskedApiKey: this.maskSecret(binding.encryptedApiKey),
updatedAt: binding.updatedAt.toISOString()
};
}
private toFailureAttempt(candidate: AiResolvedRouteCandidate, error: unknown): AiRouteAttempt {
if (error instanceof AiRouteFailureError) {
return {
channel: error.channel,
providerName: error.providerName,
model: candidate.model,
status: "failed",
reasonCode: error.code,
reasonMessage: error.message
};
}
if (error instanceof Error) {
return {
channel: candidate.channel,
providerName: candidate.providerName,
model: candidate.model,
status: "failed",
reasonCode: "UNKNOWN_ERROR",
reasonMessage: error.message
};
}
return {
channel: candidate.channel,
providerName: candidate.providerName,
model: candidate.model,
status: "failed",
reasonCode: "UNKNOWN_ERROR",
reasonMessage: "未知错误"
};
}
private normalizeOptionalString(value: string | undefined): string | null {
if (value === undefined) {
return null;
}
const normalizedValue = value.trim();
return normalizedValue.length > 0 ? normalizedValue : null;
}
private maskSecret(secret: string | null): string | null {
if (!secret) {
return null;
}
if (secret.length <= 6) {
return "*".repeat(secret.length);
}
return `${secret.slice(0, 4)}***${secret.slice(-2)}`;
}
}
+52
View File
@@ -0,0 +1,52 @@
import { AiChannel } from "../../generated/prisma/client";
export type AiResolvedRouteCandidate = {
channel: AiChannel;
source: "binding" | "public_pool";
sourceId: string | null;
providerName: string;
model: string | null;
endpoint: string | null;
apiKey: string | null;
};
export type AiChatInput = {
userId: string;
message: string;
sessionId: string | null;
};
export type AiChatResult = {
channel: AiChannel;
providerName: string;
model: string | null;
content: string;
sessionId: string | null;
raw: unknown;
};
export type AiRouteAttempt = {
channel: AiChannel;
providerName: string | null;
model: string | null;
status: "skipped" | "failed" | "success";
reasonCode: string | null;
reasonMessage: string | null;
};
export class AiRouteFailureError extends Error {
constructor(
public readonly channel: AiChannel,
public readonly providerName: string,
public readonly code: string,
message: string
) {
super(message);
this.name = "AiRouteFailureError";
Object.setPrototypeOf(this, new.target.prototype);
}
}
export interface AiChannelExecutor {
execute(candidate: AiResolvedRouteCandidate, input: AiChatInput): Promise<AiChatResult>;
}
+17
View File
@@ -0,0 +1,17 @@
import { IsOptional, IsString, MinLength } from "class-validator";
export class AiChatDto {
@IsString()
@MinLength(1)
message!: string;
@IsOptional()
@IsString()
@MinLength(1)
sessionId?: string;
@IsOptional()
@IsString()
@MinLength(1)
bindingId?: string;
}
@@ -0,0 +1,45 @@
import { AiChannel } from "../../../generated/prisma/client";
import { IsBoolean, IsEnum, IsOptional, IsString, IsUrl, MinLength } from "class-validator";
export class UpsertAiProviderBindingDto {
@IsOptional()
@IsString()
@MinLength(1)
id?: string;
@IsEnum(AiChannel)
channel!: AiChannel;
@IsString()
@MinLength(1)
providerName!: string;
@IsOptional()
@IsString()
@MinLength(1)
model?: string;
@IsOptional()
@IsUrl(
{
require_tld: false
},
{
message: "endpoint 必须是合法的 URL"
}
)
endpoint?: string;
@IsOptional()
@IsString()
@MinLength(1)
apiKey?: string;
@IsOptional()
@IsBoolean()
isDefault?: boolean;
@IsOptional()
@IsBoolean()
isEnabled?: boolean;
}
@@ -0,0 +1,197 @@
import { Injectable } from "@nestjs/common";
import {
AiChannelExecutor,
AiChatInput,
AiChatResult,
AiResolvedRouteCandidate,
AiRouteFailureError
} from "../ai.types";
@Injectable()
export class AstrbotProvider implements AiChannelExecutor {
async execute(candidate: AiResolvedRouteCandidate, input: AiChatInput): Promise<AiChatResult> {
if (!candidate.endpoint) {
throw new AiRouteFailureError(
candidate.channel,
candidate.providerName,
"MISSING_ENDPOINT",
"缺少 AstrBot 服务地址配置"
);
}
if (!candidate.apiKey) {
throw new AiRouteFailureError(
candidate.channel,
candidate.providerName,
"MISSING_API_KEY",
"缺少 AstrBot API Key 配置"
);
}
const requestUrl = this.buildRequestUrl(candidate.endpoint);
let response: Response;
try {
response = await fetch(requestUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${candidate.apiKey}`
},
body: JSON.stringify({
username: input.userId,
session_id: input.sessionId ?? undefined,
message: input.message,
enable_streaming: false,
selected_provider: candidate.providerName || undefined,
selected_model: candidate.model ?? undefined
}),
signal: AbortSignal.timeout(30000)
});
} catch (error) {
throw new AiRouteFailureError(
candidate.channel,
candidate.providerName,
"UPSTREAM_UNREACHABLE",
this.toErrorMessage(error, "AstrBot 服务请求失败")
);
}
const rawText = await response.text();
if (!response.ok) {
throw new AiRouteFailureError(
candidate.channel,
candidate.providerName,
`UPSTREAM_HTTP_${response.status}`,
this.extractHttpErrorMessage(rawText, response.status)
);
}
const events = this.parseSseEvents(rawText);
let content = "";
let sessionId = input.sessionId;
for (const event of events) {
const type = this.readString(event["type"]);
if (type === "session_id") {
sessionId = this.readString(event["session_id"]) ?? sessionId;
continue;
}
if (type === "error") {
throw new AiRouteFailureError(
candidate.channel,
candidate.providerName,
this.readString(event["code"]) ?? "ASTRBOT_ERROR",
this.readString(event["data"]) ?? "AstrBot 返回错误"
);
}
if (type !== "plain") {
continue;
}
const chainType = this.readString(event["chain_type"]);
if (
chainType === "reasoning" ||
chainType === "tool_call" ||
chainType === "tool_call_result"
) {
continue;
}
const data = this.readString(event["data"]);
if (!data) {
continue;
}
if (event["streaming"] === true) {
content += data;
continue;
}
content = data;
}
if (!content.trim()) {
throw new AiRouteFailureError(
candidate.channel,
candidate.providerName,
"EMPTY_RESPONSE",
"AstrBot 没有返回有效内容"
);
}
return {
channel: candidate.channel,
providerName: candidate.providerName,
model: candidate.model,
content,
sessionId,
raw: events
};
}
private buildRequestUrl(endpoint: string): string {
const normalizedEndpoint = endpoint.replace(/\/+$/, "");
if (normalizedEndpoint.endsWith("/api/v1/chat")) {
return normalizedEndpoint;
}
if (normalizedEndpoint.endsWith("/api/v1")) {
return `${normalizedEndpoint}/chat`;
}
if (normalizedEndpoint.endsWith("/api")) {
return `${normalizedEndpoint}/v1/chat`;
}
return `${normalizedEndpoint}/api/v1/chat`;
}
private parseSseEvents(rawText: string): Array<Record<string, unknown>> {
return rawText
.split(/\r?\n\r?\n/)
.map((block) =>
block
.split(/\r?\n/)
.filter((line) => line.startsWith("data:"))
.map((line) => line.slice(5).trim())
.join("\n")
)
.filter((payload) => payload.length > 0)
.map((payload) => {
try {
return JSON.parse(payload) as Record<string, unknown>;
} catch {
return null;
}
})
.filter((item): item is Record<string, unknown> => item !== null);
}
private extractHttpErrorMessage(rawText: string, statusCode: number): string {
try {
const payload = JSON.parse(rawText) as Record<string, unknown>;
if (typeof payload["message"] === "string") {
return payload["message"];
}
if (typeof payload["data"] === "string") {
return payload["data"];
}
} catch {
return `AstrBot 服务调用失败,状态码 ${statusCode}`;
}
return `AstrBot 服务调用失败,状态码 ${statusCode}`;
}
private readString(value: unknown): string | null {
return typeof value === "string" ? value : null;
}
private toErrorMessage(error: unknown, fallback: string): string {
if (error instanceof Error && error.message) {
return error.message;
}
return fallback;
}
}
@@ -0,0 +1,203 @@
import { Injectable } from "@nestjs/common";
import {
AiChannelExecutor,
AiChatInput,
AiChatResult,
AiResolvedRouteCandidate,
AiRouteFailureError
} from "../ai.types";
@Injectable()
export class OpenAiCompatibleProvider implements AiChannelExecutor {
async execute(candidate: AiResolvedRouteCandidate, input: AiChatInput): Promise<AiChatResult> {
if (!candidate.endpoint) {
throw new AiRouteFailureError(
candidate.channel,
candidate.providerName,
"MISSING_ENDPOINT",
"缺少 AI 服务地址配置"
);
}
if (!candidate.apiKey) {
throw new AiRouteFailureError(
candidate.channel,
candidate.providerName,
"MISSING_API_KEY",
"缺少 AI 服务密钥配置"
);
}
if (!candidate.model) {
throw new AiRouteFailureError(
candidate.channel,
candidate.providerName,
"MISSING_MODEL",
"缺少 AI 模型配置"
);
}
const requestUrl = this.buildRequestUrl(candidate.endpoint);
let response: Response;
try {
response = await fetch(requestUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${candidate.apiKey}`
},
body: JSON.stringify({
model: candidate.model,
messages: [
{
role: "user",
content: input.message
}
],
stream: false
}),
signal: AbortSignal.timeout(30000)
});
} catch (error) {
throw new AiRouteFailureError(
candidate.channel,
candidate.providerName,
"UPSTREAM_UNREACHABLE",
this.toErrorMessage(error, "AI 服务请求失败")
);
}
let payload: unknown;
try {
payload = await response.json();
} catch (error) {
throw new AiRouteFailureError(
candidate.channel,
candidate.providerName,
"INVALID_RESPONSE",
this.toErrorMessage(error, "AI 服务返回了无法解析的数据")
);
}
if (!response.ok) {
throw new AiRouteFailureError(
candidate.channel,
candidate.providerName,
`UPSTREAM_HTTP_${response.status}`,
this.extractErrorMessage(payload, `AI 服务调用失败,状态码 ${response.status}`)
);
}
const content = this.extractAssistantText(payload);
if (!content.trim()) {
throw new AiRouteFailureError(
candidate.channel,
candidate.providerName,
"EMPTY_RESPONSE",
"AI 服务没有返回有效内容"
);
}
return {
channel: candidate.channel,
providerName: candidate.providerName,
model: this.extractModel(payload) ?? candidate.model,
content,
sessionId: input.sessionId,
raw: payload
};
}
private buildRequestUrl(endpoint: string): string {
const normalizedEndpoint = endpoint.replace(/\/+$/, "");
if (normalizedEndpoint.endsWith("/chat/completions")) {
return normalizedEndpoint;
}
if (normalizedEndpoint.endsWith("/v1")) {
return `${normalizedEndpoint}/chat/completions`;
}
return `${normalizedEndpoint}/v1/chat/completions`;
}
private extractAssistantText(payload: unknown): string {
if (!this.isRecord(payload)) {
return "";
}
const choices = payload["choices"];
if (!Array.isArray(choices) || choices.length === 0) {
return "";
}
const firstChoice = choices[0];
if (!this.isRecord(firstChoice)) {
return "";
}
const message = firstChoice["message"];
if (!this.isRecord(message)) {
return "";
}
return this.extractMessageContent(message["content"]);
}
private extractMessageContent(content: unknown): string {
if (typeof content === "string") {
return content;
}
if (!Array.isArray(content)) {
return "";
}
return content
.map((item) => {
if (!this.isRecord(item)) {
return "";
}
if (typeof item["text"] === "string") {
return item["text"];
}
return "";
})
.filter((item) => item.length > 0)
.join("\n");
}
private extractModel(payload: unknown): string | null {
if (!this.isRecord(payload) || typeof payload["model"] !== "string") {
return null;
}
return payload["model"];
}
private extractErrorMessage(payload: unknown, fallback: string): string {
if (!this.isRecord(payload)) {
return fallback;
}
const error = payload["error"];
if (!this.isRecord(error) || typeof error["message"] !== "string") {
return fallback;
}
return error["message"];
}
private isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null;
}
private toErrorMessage(error: unknown, fallback: string): string {
if (error instanceof Error && error.message) {
return error.message;
}
return fallback;
}
}
+3 -1
View File
@@ -1,5 +1,6 @@
import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { AiModule } from "./ai/ai.module";
import { AttachmentModule } from "./attachment/attachment.module";
import { AuthModule } from "./auth/auth.module";
import { PrismaModule } from "./prisma/prisma.module";
@@ -16,7 +17,8 @@ import { TaskModule } from "./task/task.module";
AuthModule,
TaskModule,
AttachmentModule,
SyncModule
SyncModule,
AiModule
]
})
export class AppModule {}