mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-03 06:24:30 +00:00
feat(provider): optimize provider and model configuration management (#377)
* feat(provider): optimize provider and model configuration management * style(provider): use default tabs variant in provider settings dialog * style(provider): make tabs list full width * feat(provider): make model card row clickable and remove pencil edit icon * feat(settings): persist active settings tab in url hash * feat(agent): add search box and alphabetical sorting for provider and model selectors * refactor(providers): remove frontend pagination and display all models in scrollable list * fix(providers): address code review findings for model editing, i18n, and api protocol propagation
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
import { prisma } from '@shumai/db'
|
||||
import { ProviderConfigSerializable, providerModelSchema } from '@shumai/dtos'
|
||||
import {
|
||||
CreateModelRequest,
|
||||
ProviderConfigSerializable,
|
||||
providerModelSchema,
|
||||
UpdateModelRequest,
|
||||
} from '@shumai/dtos'
|
||||
import { getBuiltinProvidersMap } from '@shumai/core/src/provider/builtin'
|
||||
import { z } from 'zod'
|
||||
|
||||
@@ -74,7 +79,7 @@ export class ProviderService {
|
||||
providerId,
|
||||
provider: { teamId },
|
||||
},
|
||||
orderBy: { id: 'asc' },
|
||||
orderBy: { id: 'desc' },
|
||||
})
|
||||
}
|
||||
|
||||
@@ -82,7 +87,7 @@ export class ProviderService {
|
||||
teamId: string,
|
||||
name: string,
|
||||
config: ProviderConfigSerializable,
|
||||
models: ProviderModel[],
|
||||
models: ProviderModel[] = [],
|
||||
) {
|
||||
return this.prismaClient.provider.create({
|
||||
data: {
|
||||
@@ -105,60 +110,177 @@ export class ProviderService {
|
||||
teamId: string,
|
||||
id: string,
|
||||
config: ProviderConfigSerializable,
|
||||
models: ProviderModel[],
|
||||
models?: ProviderModel[],
|
||||
name?: string,
|
||||
) {
|
||||
return this.prismaClient.$transaction(async (tx) => {
|
||||
const existingModels = await tx.model.findMany({
|
||||
where: { providerId: id },
|
||||
})
|
||||
|
||||
const existingByModelId = new Map(existingModels.map((m) => [m.modelId, m]))
|
||||
const incomingModelIds = new Set(models.map((m) => m.modelId))
|
||||
|
||||
// Delete models that are no longer in the incoming list
|
||||
const modelsToDelete = existingModels.filter((m) => !incomingModelIds.has(m.modelId))
|
||||
if (modelsToDelete.length > 0) {
|
||||
await tx.model.deleteMany({
|
||||
where: {
|
||||
id: { in: modelsToDelete.map((m) => m.id) },
|
||||
},
|
||||
if (models !== undefined) {
|
||||
const existingModels = await tx.model.findMany({
|
||||
where: { providerId: id },
|
||||
})
|
||||
}
|
||||
|
||||
// Update existing models or create new ones
|
||||
for (const m of models) {
|
||||
const existing = existingByModelId.get(m.modelId)
|
||||
if (existing) {
|
||||
await tx.model.update({
|
||||
where: { id: existing.id },
|
||||
data: {
|
||||
name: m.name,
|
||||
config: m.config,
|
||||
},
|
||||
})
|
||||
} else {
|
||||
await tx.model.create({
|
||||
data: {
|
||||
providerId: id,
|
||||
modelId: m.modelId,
|
||||
name: m.name,
|
||||
config: m.config,
|
||||
const existingByModelId = new Map(existingModels.map((m) => [m.modelId, m]))
|
||||
const incomingModelIds = new Set(models.map((m) => m.modelId))
|
||||
|
||||
// Delete models that are no longer in the incoming list
|
||||
const modelsToDelete = existingModels.filter((m) => !incomingModelIds.has(m.modelId))
|
||||
if (modelsToDelete.length > 0) {
|
||||
await tx.model.deleteMany({
|
||||
where: {
|
||||
id: { in: modelsToDelete.map((m) => m.id) },
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Update existing models or create new ones
|
||||
for (const m of models) {
|
||||
const existing = existingByModelId.get(m.modelId)
|
||||
const modelConfig = {
|
||||
...m.config,
|
||||
api: m.config?.api || config.api,
|
||||
}
|
||||
if (existing) {
|
||||
await tx.model.update({
|
||||
where: { id: existing.id },
|
||||
data: {
|
||||
name: m.name,
|
||||
config: modelConfig,
|
||||
},
|
||||
})
|
||||
} else {
|
||||
await tx.model.create({
|
||||
data: {
|
||||
providerId: id,
|
||||
modelId: m.modelId,
|
||||
name: m.name,
|
||||
config: modelConfig,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
} else if (config.api) {
|
||||
// Propagate provider API protocol change to all existing models
|
||||
const existingModels = await tx.model.findMany({
|
||||
where: { providerId: id },
|
||||
})
|
||||
for (const m of existingModels) {
|
||||
const currentConfig = m.config
|
||||
if (currentConfig.api !== config.api) {
|
||||
await tx.model.update({
|
||||
where: { id: m.id },
|
||||
data: {
|
||||
config: {
|
||||
...currentConfig,
|
||||
api: config.api,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update provider config
|
||||
// Update provider config and optional name
|
||||
return tx.provider.update({
|
||||
where: { id, teamId },
|
||||
data: {
|
||||
config,
|
||||
...(name ? { name } : {}),
|
||||
},
|
||||
include: { models: true },
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async createModel(teamId: string, providerId: string, data: CreateModelRequest) {
|
||||
const provider = await this.prismaClient.provider.findUnique({
|
||||
where: { id: providerId, teamId },
|
||||
})
|
||||
if (!provider) {
|
||||
throw new Error('Provider not found')
|
||||
}
|
||||
|
||||
const existing = await this.prismaClient.model.findFirst({
|
||||
where: {
|
||||
providerId,
|
||||
modelId: data.modelId,
|
||||
},
|
||||
})
|
||||
if (existing) {
|
||||
throw new Error(`Model with id "${data.modelId}" already exists for this provider`)
|
||||
}
|
||||
|
||||
return this.prismaClient.model.create({
|
||||
data: {
|
||||
providerId,
|
||||
modelId: data.modelId,
|
||||
name: data.name ?? '',
|
||||
config: data.config,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
async updateModel(
|
||||
teamId: string,
|
||||
providerId: string,
|
||||
modelDbId: string,
|
||||
data: UpdateModelRequest,
|
||||
) {
|
||||
const provider = await this.prismaClient.provider.findUnique({
|
||||
where: { id: providerId, teamId },
|
||||
})
|
||||
if (!provider) {
|
||||
throw new Error('Provider not found')
|
||||
}
|
||||
|
||||
const model = await this.prismaClient.model.findFirst({
|
||||
where: { id: modelDbId, providerId },
|
||||
})
|
||||
if (!model) {
|
||||
throw new Error('Model not found')
|
||||
}
|
||||
|
||||
if (data.modelId && data.modelId !== model.modelId) {
|
||||
const existingWithNewId = await this.prismaClient.model.findFirst({
|
||||
where: {
|
||||
providerId,
|
||||
modelId: data.modelId,
|
||||
},
|
||||
})
|
||||
if (existingWithNewId) {
|
||||
throw new Error(`Model with id "${data.modelId}" already exists for this provider`)
|
||||
}
|
||||
}
|
||||
|
||||
return this.prismaClient.model.update({
|
||||
where: { id: modelDbId },
|
||||
data: {
|
||||
...(data.modelId ? { modelId: data.modelId } : {}),
|
||||
...(data.name !== undefined ? { name: data.name } : {}),
|
||||
...(data.config ? { config: data.config } : {}),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
async deleteModel(teamId: string, providerId: string, modelDbId: string) {
|
||||
const provider = await this.prismaClient.provider.findUnique({
|
||||
where: { id: providerId, teamId },
|
||||
})
|
||||
if (!provider) {
|
||||
throw new Error('Provider not found')
|
||||
}
|
||||
|
||||
const model = await this.prismaClient.model.findFirst({
|
||||
where: { id: modelDbId, providerId },
|
||||
})
|
||||
if (!model) {
|
||||
throw new Error('Model not found')
|
||||
}
|
||||
|
||||
return this.prismaClient.model.delete({
|
||||
where: { id: modelDbId },
|
||||
})
|
||||
}
|
||||
|
||||
async delete(teamId: string, id: string) {
|
||||
const provider = await this.prismaClient.provider.findUnique({
|
||||
where: { id, teamId },
|
||||
|
||||
Reference in New Issue
Block a user