feat: add ai model health recovery pool
This commit is contained in:
parent
1ed3184000
commit
3ebe7db37d
@ -863,6 +863,8 @@ type AiModel = {
|
|||||||
healthLastSuccessAt?: string | null;
|
healthLastSuccessAt?: string | null;
|
||||||
healthAutoDisabledAt?: string | null;
|
healthAutoDisabledAt?: string | null;
|
||||||
healthAutoDisabledReason?: string | null;
|
healthAutoDisabledReason?: string | null;
|
||||||
|
healthRecoverySuccessStreak?: number;
|
||||||
|
healthObservationStartedAt?: string | null;
|
||||||
isAgentModel?: boolean;
|
isAgentModel?: boolean;
|
||||||
agentCodingModelId?: string;
|
agentCodingModelId?: string;
|
||||||
isMultimodal?: boolean;
|
isMultimodal?: boolean;
|
||||||
@ -15677,6 +15679,19 @@ export function AdminSettingsClient() {
|
|||||||
supportsImageGen: model.supportsImageGen,
|
supportsImageGen: model.supportsImageGen,
|
||||||
});
|
});
|
||||||
const healthStatus = model.healthStatus || "healthy";
|
const healthStatus = model.healthStatus || "healthy";
|
||||||
|
const isObservationPool =
|
||||||
|
healthStatus === "degraded" &&
|
||||||
|
Boolean(model.healthObservationStartedAt);
|
||||||
|
const recoverySuccessStreak = Math.max(
|
||||||
|
0,
|
||||||
|
Number(model.healthRecoverySuccessStreak || 0)
|
||||||
|
);
|
||||||
|
const autoDisableCooldownUntil = model.healthAutoDisabledAt
|
||||||
|
? new Date(
|
||||||
|
new Date(model.healthAutoDisabledAt).getTime() +
|
||||||
|
2 * 60 * 60 * 1000
|
||||||
|
)
|
||||||
|
: null;
|
||||||
const modelHealthEvents =
|
const modelHealthEvents =
|
||||||
modelHealthEventsByModelId.get(model.id) || [];
|
modelHealthEventsByModelId.get(model.id) || [];
|
||||||
const latestHealthEvent = modelHealthEvents[0];
|
const latestHealthEvent = modelHealthEvents[0];
|
||||||
@ -15756,7 +15771,9 @@ export function AdminSettingsClient() {
|
|||||||
)}
|
)}
|
||||||
{healthStatus === "degraded" && (
|
{healthStatus === "degraded" && (
|
||||||
<span className="text-xs bg-orange-100 text-orange-700 px-2 py-0.5 rounded">
|
<span className="text-xs bg-orange-100 text-orange-700 px-2 py-0.5 rounded">
|
||||||
健康观察中
|
{isObservationPool
|
||||||
|
? `观察池 ${recoverySuccessStreak}/3`
|
||||||
|
: "健康观察中"}
|
||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
@ -15784,6 +15801,38 @@ export function AdminSettingsClient() {
|
|||||||
})}
|
})}
|
||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
|
{healthStatus === "auto_disabled" &&
|
||||||
|
autoDisableCooldownUntil && (
|
||||||
|
<span>
|
||||||
|
解冻时间:
|
||||||
|
{formatBeijingTime(autoDisableCooldownUntil, {
|
||||||
|
month: "2-digit",
|
||||||
|
day: "2-digit",
|
||||||
|
hour: "2-digit",
|
||||||
|
minute: "2-digit",
|
||||||
|
})}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{isObservationPool && (
|
||||||
|
<span>
|
||||||
|
观察成功:{recoverySuccessStreak}/3
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{isObservationPool &&
|
||||||
|
model.healthObservationStartedAt && (
|
||||||
|
<span>
|
||||||
|
观察开始:
|
||||||
|
{formatBeijingTime(
|
||||||
|
model.healthObservationStartedAt,
|
||||||
|
{
|
||||||
|
month: "2-digit",
|
||||||
|
day: "2-digit",
|
||||||
|
hour: "2-digit",
|
||||||
|
minute: "2-digit",
|
||||||
|
}
|
||||||
|
)}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
{(model.healthLastError || latestHealthEvent?.errorMessage) && (
|
{(model.healthLastError || latestHealthEvent?.errorMessage) && (
|
||||||
<div className="line-clamp-2 max-w-[760px]">
|
<div className="line-clamp-2 max-w-[760px]">
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import "server-only";
|
import "server-only";
|
||||||
|
|
||||||
import { and, desc, eq, sql } from "drizzle-orm";
|
import { and, desc, eq, inArray, lte, sql } from "drizzle-orm";
|
||||||
import { db } from "@/lib/db";
|
import { db } from "@/lib/db";
|
||||||
import {
|
import {
|
||||||
aiModel,
|
aiModel,
|
||||||
@ -20,6 +20,8 @@ export type AiModelHealthEventType =
|
|||||||
export type AiModelHealthSeverity = "info" | "warning" | "error" | "critical";
|
export type AiModelHealthSeverity = "info" | "warning" | "error" | "critical";
|
||||||
|
|
||||||
const AUTO_DISABLE_FAILURE_THRESHOLD = 3;
|
const AUTO_DISABLE_FAILURE_THRESHOLD = 3;
|
||||||
|
const AUTO_DISABLE_COOLDOWN_MS = 2 * 60 * 60 * 1000;
|
||||||
|
const OBSERVATION_RECOVERY_SUCCESS_THRESHOLD = 3;
|
||||||
const RECENT_EVENT_LIMIT = 80;
|
const RECENT_EVENT_LIMIT = 80;
|
||||||
|
|
||||||
function normalizeText(value: unknown) {
|
function normalizeText(value: unknown) {
|
||||||
@ -162,23 +164,157 @@ function buildMetadata(value?: Record<string, unknown>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isObservationPoolModel(
|
||||||
|
model:
|
||||||
|
| Pick<
|
||||||
|
AiModel,
|
||||||
|
| "healthStatus"
|
||||||
|
| "healthObservationStartedAt"
|
||||||
|
>
|
||||||
|
| null
|
||||||
|
| undefined
|
||||||
|
) {
|
||||||
|
return (
|
||||||
|
model?.healthStatus === "degraded" &&
|
||||||
|
Boolean(model.healthObservationStartedAt)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAutoDisableCooldownCutoff(now = new Date()) {
|
||||||
|
return new Date(now.getTime() - AUTO_DISABLE_COOLDOWN_MS);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function reviveAiModelsFromCooldown(params?: { modelId?: string }) {
|
||||||
|
const now = new Date();
|
||||||
|
const cooldownCutoff = getAutoDisableCooldownCutoff(now);
|
||||||
|
const conditions = [
|
||||||
|
eq(aiModel.healthStatus, "auto_disabled"),
|
||||||
|
eq(aiModel.isEnabled, false),
|
||||||
|
eq(aiModel.excludeFromFailover, false),
|
||||||
|
lte(aiModel.healthAutoDisabledAt, cooldownCutoff),
|
||||||
|
];
|
||||||
|
|
||||||
|
if (params?.modelId) {
|
||||||
|
conditions.push(eq(aiModel.id, normalizeText(params.modelId)));
|
||||||
|
}
|
||||||
|
|
||||||
|
const dueModels = await db
|
||||||
|
.select({
|
||||||
|
id: aiModel.id,
|
||||||
|
modelId: aiModel.modelId,
|
||||||
|
displayName: aiModel.displayName,
|
||||||
|
})
|
||||||
|
.from(aiModel)
|
||||||
|
.where(and(...conditions));
|
||||||
|
|
||||||
|
if (dueModels.length === 0) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const revived = await db
|
||||||
|
.update(aiModel)
|
||||||
|
.set({
|
||||||
|
isEnabled: true,
|
||||||
|
healthStatus: "degraded",
|
||||||
|
healthConsecutiveFailures: 0,
|
||||||
|
healthRecoverySuccessStreak: 0,
|
||||||
|
healthObservationStartedAt: now,
|
||||||
|
healthUpdatedAt: now,
|
||||||
|
updatedAt: now,
|
||||||
|
})
|
||||||
|
.where(
|
||||||
|
inArray(
|
||||||
|
aiModel.id,
|
||||||
|
dueModels.map((item) => item.id)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.returning();
|
||||||
|
|
||||||
|
if (revived.length > 0) {
|
||||||
|
console.info("[AI Model Health] Revived cooled-down models into observation pool:", {
|
||||||
|
count: revived.length,
|
||||||
|
models: revived.map((item) => ({
|
||||||
|
id: item.id,
|
||||||
|
modelId: item.modelId,
|
||||||
|
displayName: item.displayName,
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return revived;
|
||||||
|
}
|
||||||
|
|
||||||
export async function recordAiModelHealthSuccess(params: {
|
export async function recordAiModelHealthSuccess(params: {
|
||||||
model?: Pick<AiModel, "id" | "healthStatus"> | null;
|
model?:
|
||||||
|
| Pick<
|
||||||
|
AiModel,
|
||||||
|
| "id"
|
||||||
|
| "healthStatus"
|
||||||
|
| "healthRecoverySuccessStreak"
|
||||||
|
| "healthObservationStartedAt"
|
||||||
|
>
|
||||||
|
| null;
|
||||||
}) {
|
}) {
|
||||||
const modelId = normalizeText(params.model?.id);
|
const modelId = normalizeText(params.model?.id);
|
||||||
if (!modelId) return;
|
if (!modelId) return;
|
||||||
|
|
||||||
|
const [existing] = await db
|
||||||
|
.select({
|
||||||
|
id: aiModel.id,
|
||||||
|
healthStatus: aiModel.healthStatus,
|
||||||
|
healthRecoverySuccessStreak: aiModel.healthRecoverySuccessStreak,
|
||||||
|
healthObservationStartedAt: aiModel.healthObservationStartedAt,
|
||||||
|
})
|
||||||
|
.from(aiModel)
|
||||||
|
.where(eq(aiModel.id, modelId))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!existing) return;
|
||||||
|
|
||||||
|
const now = new Date();
|
||||||
|
const isObservation = isObservationPoolModel(existing);
|
||||||
|
const nextRecoverySuccessStreak = isObservation
|
||||||
|
? (existing.healthRecoverySuccessStreak || 0) + 1
|
||||||
|
: 0;
|
||||||
|
const recoveredToHealthy =
|
||||||
|
isObservation &&
|
||||||
|
nextRecoverySuccessStreak >= OBSERVATION_RECOVERY_SUCCESS_THRESHOLD;
|
||||||
|
|
||||||
|
const updateData: Partial<AiModel> = {
|
||||||
|
healthStatus:
|
||||||
|
existing.healthStatus === "auto_disabled"
|
||||||
|
? "auto_disabled"
|
||||||
|
: recoveredToHealthy
|
||||||
|
? "healthy"
|
||||||
|
: isObservation
|
||||||
|
? "degraded"
|
||||||
|
: "healthy",
|
||||||
|
healthConsecutiveFailures: 0,
|
||||||
|
healthLastEventType: "success",
|
||||||
|
healthLastError: null,
|
||||||
|
healthLastSuccessAt: now,
|
||||||
|
healthRecoverySuccessStreak: recoveredToHealthy
|
||||||
|
? 0
|
||||||
|
: isObservation
|
||||||
|
? nextRecoverySuccessStreak
|
||||||
|
: 0,
|
||||||
|
healthObservationStartedAt: recoveredToHealthy
|
||||||
|
? null
|
||||||
|
: isObservation
|
||||||
|
? existing.healthObservationStartedAt
|
||||||
|
: null,
|
||||||
|
healthUpdatedAt: now,
|
||||||
|
updatedAt: now,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (recoveredToHealthy) {
|
||||||
|
updateData.healthAutoDisabledAt = null;
|
||||||
|
updateData.healthAutoDisabledReason = null;
|
||||||
|
}
|
||||||
|
|
||||||
await db
|
await db
|
||||||
.update(aiModel)
|
.update(aiModel)
|
||||||
.set({
|
.set(updateData)
|
||||||
healthStatus:
|
|
||||||
params.model?.healthStatus === "auto_disabled" ? "auto_disabled" : "healthy",
|
|
||||||
healthConsecutiveFailures: 0,
|
|
||||||
healthLastEventType: "success",
|
|
||||||
healthLastError: null,
|
|
||||||
healthLastSuccessAt: new Date(),
|
|
||||||
healthUpdatedAt: new Date(),
|
|
||||||
})
|
|
||||||
.where(eq(aiModel.id, modelId));
|
.where(eq(aiModel.id, modelId));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,10 +341,15 @@ export async function recordAiModelHealthFailure(params: {
|
|||||||
const [existing] = await db
|
const [existing] = await db
|
||||||
.select({
|
.select({
|
||||||
id: aiModel.id,
|
id: aiModel.id,
|
||||||
|
modelId: aiModel.modelId,
|
||||||
|
displayName: aiModel.displayName,
|
||||||
isEnabled: aiModel.isEnabled,
|
isEnabled: aiModel.isEnabled,
|
||||||
excludeFromFailover: aiModel.excludeFromFailover,
|
excludeFromFailover: aiModel.excludeFromFailover,
|
||||||
healthStatus: aiModel.healthStatus,
|
healthStatus: aiModel.healthStatus,
|
||||||
healthConsecutiveFailures: aiModel.healthConsecutiveFailures,
|
healthConsecutiveFailures: aiModel.healthConsecutiveFailures,
|
||||||
|
healthRecoverySuccessStreak: aiModel.healthRecoverySuccessStreak,
|
||||||
|
healthObservationStartedAt: aiModel.healthObservationStartedAt,
|
||||||
|
healthAutoDisabledAt: aiModel.healthAutoDisabledAt,
|
||||||
})
|
})
|
||||||
.from(aiModel)
|
.from(aiModel)
|
||||||
.where(eq(aiModel.id, modelId))
|
.where(eq(aiModel.id, modelId))
|
||||||
@ -216,14 +357,19 @@ export async function recordAiModelHealthFailure(params: {
|
|||||||
|
|
||||||
if (!existing) return;
|
if (!existing) return;
|
||||||
|
|
||||||
|
const isObservation = isObservationPoolModel(existing);
|
||||||
const nextConsecutiveFailures = classification.counted
|
const nextConsecutiveFailures = classification.counted
|
||||||
? Math.max(1, (existing.healthConsecutiveFailures || 0) + 1)
|
? Math.max(
|
||||||
|
1,
|
||||||
|
(isObservation ? 0 : existing.healthConsecutiveFailures || 0) + 1
|
||||||
|
)
|
||||||
: existing.healthConsecutiveFailures || 0;
|
: existing.healthConsecutiveFailures || 0;
|
||||||
const shouldAutoDisable =
|
const shouldAutoDisable =
|
||||||
classification.counted &&
|
classification.counted &&
|
||||||
!existing.excludeFromFailover &&
|
!existing.excludeFromFailover &&
|
||||||
existing.healthStatus !== "auto_disabled" &&
|
((isObservation && existing.isEnabled) ||
|
||||||
nextConsecutiveFailures >= AUTO_DISABLE_FAILURE_THRESHOLD;
|
(existing.healthStatus !== "auto_disabled" &&
|
||||||
|
nextConsecutiveFailures >= AUTO_DISABLE_FAILURE_THRESHOLD));
|
||||||
const nextStatus = shouldAutoDisable
|
const nextStatus = shouldAutoDisable
|
||||||
? "auto_disabled"
|
? "auto_disabled"
|
||||||
: classification.counted
|
: classification.counted
|
||||||
@ -264,13 +410,23 @@ export async function recordAiModelHealthFailure(params: {
|
|||||||
healthLastEventType: classification.eventType,
|
healthLastEventType: classification.eventType,
|
||||||
healthLastError: errorMessage,
|
healthLastError: errorMessage,
|
||||||
healthLastFailureAt: now,
|
healthLastFailureAt: now,
|
||||||
|
healthRecoverySuccessStreak: shouldAutoDisable
|
||||||
|
? 0
|
||||||
|
: classification.counted
|
||||||
|
? 0
|
||||||
|
: existing.healthRecoverySuccessStreak || 0,
|
||||||
|
healthObservationStartedAt: shouldAutoDisable
|
||||||
|
? null
|
||||||
|
: isObservation
|
||||||
|
? existing.healthObservationStartedAt
|
||||||
|
: null,
|
||||||
healthUpdatedAt: now,
|
healthUpdatedAt: now,
|
||||||
updatedAt: now,
|
updatedAt: now,
|
||||||
};
|
};
|
||||||
if (shouldAutoDisable) {
|
if (shouldAutoDisable) {
|
||||||
updateData.healthAutoDisabledAt = now;
|
updateData.healthAutoDisabledAt = now;
|
||||||
updateData.healthAutoDisabledReason = autoDisabledReason || errorMessage;
|
updateData.healthAutoDisabledReason = autoDisabledReason || errorMessage;
|
||||||
} else if (existing.healthStatus !== "auto_disabled") {
|
} else if (existing.healthStatus !== "auto_disabled" && !isObservation) {
|
||||||
updateData.healthAutoDisabledReason = null;
|
updateData.healthAutoDisabledReason = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -304,6 +460,8 @@ export async function restoreAiModelHealth(modelId: string) {
|
|||||||
healthLastFailureAt: null,
|
healthLastFailureAt: null,
|
||||||
healthAutoDisabledAt: null,
|
healthAutoDisabledAt: null,
|
||||||
healthAutoDisabledReason: null,
|
healthAutoDisabledReason: null,
|
||||||
|
healthRecoverySuccessStreak: 0,
|
||||||
|
healthObservationStartedAt: null,
|
||||||
healthUpdatedAt: new Date(),
|
healthUpdatedAt: new Date(),
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
})
|
})
|
||||||
|
|||||||
@ -110,6 +110,14 @@ const runMigrate = async () => {
|
|||||||
ALTER TABLE "AiModel"
|
ALTER TABLE "AiModel"
|
||||||
ADD COLUMN IF NOT EXISTS "isDefaultTextEditModel" boolean NOT NULL DEFAULT false;
|
ADD COLUMN IF NOT EXISTS "isDefaultTextEditModel" boolean NOT NULL DEFAULT false;
|
||||||
`);
|
`);
|
||||||
|
await db.execute(sql`
|
||||||
|
ALTER TABLE "AiModel"
|
||||||
|
ADD COLUMN IF NOT EXISTS "healthRecoverySuccessStreak" integer NOT NULL DEFAULT 0;
|
||||||
|
`);
|
||||||
|
await db.execute(sql`
|
||||||
|
ALTER TABLE "AiModel"
|
||||||
|
ADD COLUMN IF NOT EXISTS "healthObservationStartedAt" timestamp;
|
||||||
|
`);
|
||||||
for (const item of providerBaseUrlDefaults) {
|
for (const item of providerBaseUrlDefaults) {
|
||||||
await db.execute(sql`
|
await db.execute(sql`
|
||||||
UPDATE "AiProvider"
|
UPDATE "AiProvider"
|
||||||
|
|||||||
5
lib/db/migrations/0080_add_ai_model_health_recovery.sql
Normal file
5
lib/db/migrations/0080_add_ai_model_health_recovery.sql
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
ALTER TABLE "AiModel"
|
||||||
|
ADD COLUMN IF NOT EXISTS "healthRecoverySuccessStreak" integer NOT NULL DEFAULT 0;
|
||||||
|
|
||||||
|
ALTER TABLE "AiModel"
|
||||||
|
ADD COLUMN IF NOT EXISTS "healthObservationStartedAt" timestamp;
|
||||||
@ -463,6 +463,13 @@
|
|||||||
"when": 1778252400000,
|
"when": 1778252400000,
|
||||||
"tag": "0079_add_ai_model_health",
|
"tag": "0079_add_ai_model_health",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 66,
|
||||||
|
"version": "7",
|
||||||
|
"when": 1778461200000,
|
||||||
|
"tag": "0080_add_ai_model_health_recovery",
|
||||||
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,6 +21,7 @@ import { drizzle } from "drizzle-orm/postgres-js";
|
|||||||
import postgres from "postgres";
|
import postgres from "postgres";
|
||||||
import type { ArtifactKind } from "@/components/artifact";
|
import type { ArtifactKind } from "@/components/artifact";
|
||||||
import type { VisibilityType } from "@/components/visibility-selector";
|
import type { VisibilityType } from "@/components/visibility-selector";
|
||||||
|
import { reviveAiModelsFromCooldown } from "@/lib/ai/runtime/model-health";
|
||||||
import { ChatSDKError } from "../errors";
|
import { ChatSDKError } from "../errors";
|
||||||
import type { AppUsage } from "../usage";
|
import type { AppUsage } from "../usage";
|
||||||
import { generateUUID } from "../utils";
|
import { generateUUID } from "../utils";
|
||||||
@ -3912,6 +3913,7 @@ export async function deleteAiProvider({ id }: { id: string }) {
|
|||||||
|
|
||||||
export async function getAllAiModels(): Promise<AiModel[]> {
|
export async function getAllAiModels(): Promise<AiModel[]> {
|
||||||
try {
|
try {
|
||||||
|
await reviveAiModelsFromCooldown();
|
||||||
// 暂时移除排序以避免 sortOrder 类型问题
|
// 暂时移除排序以避免 sortOrder 类型问题
|
||||||
return await db.select().from(aiModel);
|
return await db.select().from(aiModel);
|
||||||
} catch (_error) {
|
} catch (_error) {
|
||||||
@ -3925,6 +3927,7 @@ export async function getAllAiModels(): Promise<AiModel[]> {
|
|||||||
|
|
||||||
export async function getEnabledAiModels(): Promise<AiModel[]> {
|
export async function getEnabledAiModels(): Promise<AiModel[]> {
|
||||||
try {
|
try {
|
||||||
|
await reviveAiModelsFromCooldown();
|
||||||
return await db
|
return await db
|
||||||
.select()
|
.select()
|
||||||
.from(aiModel)
|
.from(aiModel)
|
||||||
@ -3943,6 +3946,7 @@ export async function getAiModelsByProviderId({
|
|||||||
providerId: string;
|
providerId: string;
|
||||||
}): Promise<AiModel[]> {
|
}): Promise<AiModel[]> {
|
||||||
try {
|
try {
|
||||||
|
await reviveAiModelsFromCooldown();
|
||||||
return await db
|
return await db
|
||||||
.select()
|
.select()
|
||||||
.from(aiModel)
|
.from(aiModel)
|
||||||
@ -3961,6 +3965,7 @@ export async function getAiModelById({
|
|||||||
id: string;
|
id: string;
|
||||||
}): Promise<AiModel | undefined> {
|
}): Promise<AiModel | undefined> {
|
||||||
try {
|
try {
|
||||||
|
await reviveAiModelsFromCooldown({ modelId: id });
|
||||||
const models = await db
|
const models = await db
|
||||||
.select()
|
.select()
|
||||||
.from(aiModel)
|
.from(aiModel)
|
||||||
@ -3977,6 +3982,7 @@ export async function getAiModelById({
|
|||||||
|
|
||||||
export async function getDefaultAiModel(): Promise<AiModel | undefined> {
|
export async function getDefaultAiModel(): Promise<AiModel | undefined> {
|
||||||
try {
|
try {
|
||||||
|
await reviveAiModelsFromCooldown();
|
||||||
const models = await db
|
const models = await db
|
||||||
.select()
|
.select()
|
||||||
.from(aiModel)
|
.from(aiModel)
|
||||||
@ -4194,6 +4200,8 @@ export async function updateAiModel({
|
|||||||
updateData.healthLastFailureAt = null;
|
updateData.healthLastFailureAt = null;
|
||||||
updateData.healthAutoDisabledAt = null;
|
updateData.healthAutoDisabledAt = null;
|
||||||
updateData.healthAutoDisabledReason = null;
|
updateData.healthAutoDisabledReason = null;
|
||||||
|
updateData.healthRecoverySuccessStreak = 0;
|
||||||
|
updateData.healthObservationStartedAt = null;
|
||||||
updateData.healthUpdatedAt = new Date();
|
updateData.healthUpdatedAt = new Date();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -4477,6 +4485,7 @@ export async function getEnabledAiModelsWithProvider(
|
|||||||
includeHidden: boolean = true
|
includeHidden: boolean = true
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
|
await reviveAiModelsFromCooldown();
|
||||||
const conditions = [
|
const conditions = [
|
||||||
eq(aiModel.isEnabled, true),
|
eq(aiModel.isEnabled, true),
|
||||||
eq(aiProvider.isEnabled, true),
|
eq(aiProvider.isEnabled, true),
|
||||||
@ -4499,7 +4508,23 @@ export async function getEnabledAiModelsWithProvider(
|
|||||||
.innerJoin(aiProvider, eq(aiModel.providerId, aiProvider.id))
|
.innerJoin(aiProvider, eq(aiModel.providerId, aiProvider.id))
|
||||||
.where(and(...conditions));
|
.where(and(...conditions));
|
||||||
|
|
||||||
return models;
|
const healthPriority = (status?: string | null) => {
|
||||||
|
if (status === "healthy") return 0;
|
||||||
|
if (status === "degraded") return 1;
|
||||||
|
if (status === "auto_disabled") return 2;
|
||||||
|
return 3;
|
||||||
|
};
|
||||||
|
|
||||||
|
return [...models].sort((left, right) => {
|
||||||
|
const byHealth =
|
||||||
|
healthPriority(left.model.healthStatus) -
|
||||||
|
healthPriority(right.model.healthStatus);
|
||||||
|
if (byHealth !== 0) return byHealth;
|
||||||
|
if (left.model.isDefault !== right.model.isDefault) {
|
||||||
|
return Number(right.model.isDefault) - Number(left.model.isDefault);
|
||||||
|
}
|
||||||
|
return (left.model.sortOrder ?? 0) - (right.model.sortOrder ?? 0);
|
||||||
|
});
|
||||||
} catch (_error) {
|
} catch (_error) {
|
||||||
throw new ChatSDKError(
|
throw new ChatSDKError(
|
||||||
"bad_request:database",
|
"bad_request:database",
|
||||||
@ -4528,6 +4553,7 @@ export async function getAgentAiModelWithProvider() {
|
|||||||
*/
|
*/
|
||||||
export async function getSubModelsByParentModelId(parentModelId: string) {
|
export async function getSubModelsByParentModelId(parentModelId: string) {
|
||||||
try {
|
try {
|
||||||
|
await reviveAiModelsFromCooldown();
|
||||||
const subModels = await db
|
const subModels = await db
|
||||||
.select({
|
.select({
|
||||||
model: aiModel,
|
model: aiModel,
|
||||||
|
|||||||
@ -349,6 +349,10 @@ export const aiModel = pgTable("AiModel", {
|
|||||||
healthLastSuccessAt: timestamp("healthLastSuccessAt"),
|
healthLastSuccessAt: timestamp("healthLastSuccessAt"),
|
||||||
healthAutoDisabledAt: timestamp("healthAutoDisabledAt"),
|
healthAutoDisabledAt: timestamp("healthAutoDisabledAt"),
|
||||||
healthAutoDisabledReason: text("healthAutoDisabledReason"),
|
healthAutoDisabledReason: text("healthAutoDisabledReason"),
|
||||||
|
healthRecoverySuccessStreak: integer("healthRecoverySuccessStreak")
|
||||||
|
.notNull()
|
||||||
|
.default(0),
|
||||||
|
healthObservationStartedAt: timestamp("healthObservationStartedAt"),
|
||||||
healthUpdatedAt: timestamp("healthUpdatedAt").notNull().defaultNow(),
|
healthUpdatedAt: timestamp("healthUpdatedAt").notNull().defaultNow(),
|
||||||
// 🆕 图片生成模型特性配置(用于阿里生图、Gemini等)
|
// 🆕 图片生成模型特性配置(用于阿里生图、Gemini等)
|
||||||
imageGenConfig: jsonb("imageGenConfig"), // { maxImages: 6, supportsSize: true, supportsPromptExtend: true, supportsNegativePrompt: true, enableLoadingPlaceholder: true }
|
imageGenConfig: jsonb("imageGenConfig"), // { maxImages: 6, supportsSize: true, supportsPromptExtend: true, supportsNegativePrompt: true, enableLoadingPlaceholder: true }
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user