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:
Yiling-J
2026-09-03 15:16:16 +08:00
committed by GitHub
parent e58dfaa71a
commit c3ef769f51
63 changed files with 3249 additions and 513 deletions
+155
View File
@@ -191,4 +191,159 @@ describe('ProviderService', () => {
const updatedAgent = updatedAgents.find((a) => a.id === agent!.id)
expect(updatedAgent?.modelId).toBe(initialModelId)
})
it('should support granular model CRUD operations and order by id desc', async () => {
const team = await teamService.ensureDefaultTeam()
const provider = await providerService.create(
team.id,
'test-granular-provider',
{ api: 'openai-completions', apiKey: 'key1' },
[],
)
// 1. createModel
const createdModel1 = await providerService.createModel(team.id, provider.id, {
modelId: 'custom-model-1',
name: 'Custom Model 1',
config: {
reasoning: true,
input: ['text', 'image'],
contextWindow: 200000,
maxTokens: 8192,
cost: { input: 1.5, output: 5, cacheRead: 0.1, cacheWrite: 0.5 },
},
})
expect(createdModel1.modelId).toBe('custom-model-1')
expect(createdModel1.name).toBe('Custom Model 1')
expect((createdModel1.config as { reasoning: boolean }).reasoning).toBe(true)
// Check duplicate modelId error
await expect(
providerService.createModel(team.id, provider.id, {
modelId: 'custom-model-1',
name: 'Duplicate',
config: {
reasoning: false,
input: ['text'],
contextWindow: 100000,
maxTokens: 4000,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
},
}),
).rejects.toThrow('already exists')
// 2. createModel 2
const createdModel2 = await providerService.createModel(team.id, provider.id, {
modelId: 'custom-model-2',
name: 'Custom Model 2',
config: {
reasoning: false,
input: ['text'],
contextWindow: 100000,
maxTokens: 4000,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
},
})
// 3. listModelsByProvider should return newest first (id desc)
const models = await providerService.listModelsByProvider(team.id, provider.id)
expect(models).toHaveLength(2)
expect(models[0].id).toBe(createdModel2.id)
expect(models[1].id).toBe(createdModel1.id)
// 4. updateModel
const updatedModel = await providerService.updateModel(team.id, provider.id, createdModel1.id, {
name: 'Updated Model 1',
config: {
reasoning: false,
input: ['text'],
contextWindow: 64000,
maxTokens: 2048,
cost: { input: 2, output: 8, cacheRead: 0.2, cacheWrite: 0.8 },
},
})
expect(updatedModel.name).toBe('Updated Model 1')
expect((updatedModel.config as { reasoning: boolean }).reasoning).toBe(false)
expect((updatedModel.config as { cost: { input: number } }).cost.input).toBe(2)
// 5. update provider config and name without touching models
const updatedProvider = await providerService.update(
team.id,
provider.id,
{ api: 'openai-completions', apiKey: 'new-key' },
undefined,
'renamed-provider',
)
expect(updatedProvider.name).toBe('renamed-provider')
expect(updatedProvider.config.apiKey).toBe('new-key')
const modelsAfterProviderUpdate = await providerService.listModelsByProvider(
team.id,
provider.id,
)
expect(modelsAfterProviderUpdate).toHaveLength(2)
// 6. deleteModel
await providerService.deleteModel(team.id, provider.id, createdModel2.id)
const modelsAfterDelete = await providerService.listModelsByProvider(team.id, provider.id)
expect(modelsAfterDelete).toHaveLength(1)
expect(modelsAfterDelete[0].id).toBe(createdModel1.id)
})
it('should propagate provider API protocol change to existing models', async () => {
const team = await teamService.ensureDefaultTeam()
// 1. Create provider with openai-responses
const provider = await providerService.create(
team.id,
'test-protocol-provider',
{ api: 'openai-responses', apiKey: 'key-1' },
[
{
modelId: 'm1',
name: 'Model 1',
config: {
api: 'openai-responses',
reasoning: false,
input: ['text'],
contextWindow: 128000,
maxTokens: 4096,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
},
},
{
modelId: 'm2',
name: 'Model 2',
config: {
api: 'openai-responses',
reasoning: true,
input: ['text'],
contextWindow: 64000,
maxTokens: 2048,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
},
},
],
)
const initialModels = await providerService.listModelsByProvider(team.id, provider.id)
expect(initialModels).toHaveLength(2)
expect((initialModels[0].config as { api: string }).api).toBe('openai-responses')
expect((initialModels[1].config as { api: string }).api).toBe('openai-responses')
// 2. Update provider global API protocol to anthropic-messages without passing models
const updatedProvider = await providerService.update(
team.id,
provider.id,
{ api: 'anthropic-messages', apiKey: 'key-2' },
undefined,
'test-protocol-provider',
)
expect(updatedProvider.config.api).toBe('anthropic-messages')
// 3. Verify that all existing models under this provider were updated
const modelsAfterUpdate = await providerService.listModelsByProvider(team.id, provider.id)
expect(modelsAfterUpdate).toHaveLength(2)
expect((modelsAfterUpdate[0].config as { api: string }).api).toBe('anthropic-messages')
expect((modelsAfterUpdate[1].config as { api: string }).api).toBe('anthropic-messages')
})
})
+160 -38
View File
@@ -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 },