mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 12:24:39 +00:00
feat(provider): refresh models from the public Pi catalog
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
"types": "./src/index.ts",
|
||||
"dependencies": {
|
||||
"@better-auth/mcp": "1.7.12",
|
||||
"@earendil-works/pi-durable": "1.5.0",
|
||||
"@earendil-works/pi-durable": "1.7.0",
|
||||
"@hono/zod-validator": "0.7.6",
|
||||
"@modelcontextprotocol/server": "2.0.0",
|
||||
"@shumai/core": "workspace:*",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { describe, expect, it, vi, beforeEach } from 'vitest'
|
||||
import { Hono, type Context, type Next } from 'hono'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import providerRoute from './provider'
|
||||
import { providerService } from '@shumai/core/src/provider/provider'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
@@ -34,6 +35,7 @@ describe('provider api', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
vi.clearAllMocks()
|
||||
vi.mocked(authzService.hasPermission).mockResolvedValue(undefined)
|
||||
})
|
||||
|
||||
@@ -320,78 +322,57 @@ describe('provider api', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('POST /teams/:teamId/providers/sync-check checks for updates', async () => {
|
||||
vi.mocked(providerService.checkUpdates).mockResolvedValue({
|
||||
providers: [
|
||||
{
|
||||
name: 'deepseek',
|
||||
isNewProvider: true,
|
||||
config: { api: 'openai-responses', apiKey: 'DEEPSEEK_API_KEY' },
|
||||
models: [],
|
||||
},
|
||||
],
|
||||
totalNewProviders: 1,
|
||||
totalNewModels: 0,
|
||||
})
|
||||
const modelsPath = '/teams/t1/providers/p1/models'
|
||||
const providerUpdatedAt = '2026-11-29T09:15:00.000Z'
|
||||
const selection = {
|
||||
providerUpdatedAt,
|
||||
modelIds: ['test-model'],
|
||||
}
|
||||
const importRequest = {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(selection),
|
||||
}
|
||||
|
||||
const res = await app.request('/teams/t1/providers/sync-check', {
|
||||
method: 'POST',
|
||||
it('previews refresh results for the requested provider as a team admin', async () => {
|
||||
vi.mocked(providerService.refreshModels).mockResolvedValue({
|
||||
status: 'ready',
|
||||
providerUpdatedAt,
|
||||
models: [],
|
||||
})
|
||||
|
||||
const res = await app.request(modelsPath + '/refresh', { method: 'POST' })
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
expect(data.totalNewProviders).toBe(1)
|
||||
expect(data.providers).toHaveLength(1)
|
||||
expect(providerService.checkUpdates).toHaveBeenCalledWith('t1')
|
||||
expect(res.headers.get('Cache-Control')).toBe('no-store')
|
||||
expect(await res.json()).toEqual({ status: 'ready', providerUpdatedAt, models: [] })
|
||||
expect(providerService.refreshModels).toHaveBeenCalledWith('t1', 'p1')
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: expect.anything(),
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.Team,
|
||||
id: 't1',
|
||||
})
|
||||
expect(auditLogService.logAction).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('POST /teams/:teamId/providers/sync-apply applies sync selection', async () => {
|
||||
vi.mocked(providerService.applySync).mockResolvedValue({
|
||||
addedProviders: 1,
|
||||
addedModels: 3,
|
||||
})
|
||||
|
||||
const payload = {
|
||||
providers: [
|
||||
{
|
||||
name: 'deepseek',
|
||||
isNewProvider: true,
|
||||
config: { api: 'openai-responses', apiKey: 'KEY' },
|
||||
models: [
|
||||
{
|
||||
modelId: 'deepseek-v4-flash',
|
||||
name: 'DeepSeek V4 Flash',
|
||||
config: {
|
||||
api: 'openai-responses',
|
||||
reasoning: true,
|
||||
input: ['text'],
|
||||
contextWindow: 128000,
|
||||
maxTokens: 4096,
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const res = await app.request('/teams/t1/providers/sync-apply', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
it('returns the server reason for an unavailable refresh', async () => {
|
||||
vi.mocked(providerService.refreshModels).mockResolvedValue({
|
||||
status: 'unavailable',
|
||||
reason: 'catalog_unavailable',
|
||||
})
|
||||
|
||||
const res = await app.request(modelsPath + '/refresh', { method: 'POST' })
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
expect(data.addedProviders).toBe(1)
|
||||
expect(data.addedModels).toBe(3)
|
||||
expect(providerService.applySync).toHaveBeenCalledWith('t1', payload)
|
||||
expect(await res.json()).toEqual({ status: 'unavailable', reason: 'catalog_unavailable' })
|
||||
})
|
||||
|
||||
it('imports the selection into the scoped provider and records its audit entry', async () => {
|
||||
vi.mocked(providerService.importModels).mockResolvedValue({ addedModels: 1 })
|
||||
|
||||
const res = await app.request(modelsPath + '/import', importRequest)
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.json()).toEqual({ addedModels: 1 })
|
||||
expect(providerService.importModels).toHaveBeenCalledWith('t1', 'p1', selection)
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: expect.anything(),
|
||||
permission: Permission.Admin,
|
||||
@@ -402,6 +383,36 @@ describe('provider api', () => {
|
||||
action: 'provider_update',
|
||||
teamId: 't1',
|
||||
userId: 'user1',
|
||||
itemId: 'p1',
|
||||
})
|
||||
})
|
||||
|
||||
it('skips the audit entry when the selected models already exist', async () => {
|
||||
vi.mocked(providerService.importModels).mockResolvedValue({ addedModels: 0 })
|
||||
const res = await app.request(modelsPath + '/import', importRequest)
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.json()).toEqual({ addedModels: 0 })
|
||||
expect(auditLogService.logAction).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects requests without team-admin permission before looking up the provider', async () => {
|
||||
vi.mocked(authzService.hasPermission).mockRejectedValue(new HTTPException(403))
|
||||
const refresh = await app.request(modelsPath + '/refresh', { method: 'POST' })
|
||||
const apply = await app.request(modelsPath + '/import', importRequest)
|
||||
expect(refresh.status).toBe(403)
|
||||
expect(apply.status).toBe(403)
|
||||
expect(providerService.refreshModels).not.toHaveBeenCalled()
|
||||
expect(providerService.importModels).not.toHaveBeenCalled()
|
||||
expect(auditLogService.logAction).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects an empty import selection before performing a write', async () => {
|
||||
const res = await app.request(modelsPath + '/import', {
|
||||
...importRequest,
|
||||
body: JSON.stringify({ providerUpdatedAt, modelIds: [] }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(400)
|
||||
expect(providerService.importModels).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -5,7 +5,7 @@ import { providerService } from '@shumai/core/src/provider/provider'
|
||||
import {
|
||||
createModelRequestSchema,
|
||||
createProviderRequestSchema,
|
||||
syncApplyRequestSchema,
|
||||
importProviderModelsRequestSchema,
|
||||
updateModelRequestSchema,
|
||||
updateProviderRequestSchema,
|
||||
AuditAction,
|
||||
@@ -31,8 +31,8 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
return c.json(providers)
|
||||
})
|
||||
|
||||
.post('/teams/:teamId/providers/sync-check', async (c) => {
|
||||
const teamId = c.req.param('teamId')
|
||||
.post('/teams/:teamId/providers/:providerId/models/refresh', async (c) => {
|
||||
const { teamId, providerId } = c.req.param()
|
||||
const user = c.get('user')
|
||||
|
||||
await authzService.hasPermission({
|
||||
@@ -42,15 +42,16 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
id: teamId,
|
||||
})
|
||||
|
||||
const result = await providerService.checkUpdates(teamId)
|
||||
const result = await providerService.refreshModels(teamId, providerId)
|
||||
c.header('Cache-Control', 'no-store')
|
||||
return c.json(result)
|
||||
})
|
||||
|
||||
.post(
|
||||
'/teams/:teamId/providers/sync-apply',
|
||||
zValidator('json', syncApplyRequestSchema),
|
||||
'/teams/:teamId/providers/:providerId/models/import',
|
||||
zValidator('json', importProviderModelsRequestSchema),
|
||||
async (c) => {
|
||||
const teamId = c.req.param('teamId')
|
||||
const { teamId, providerId } = c.req.param()
|
||||
const user = c.get('user')
|
||||
const body = c.req.valid('json')
|
||||
|
||||
@@ -61,12 +62,15 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
id: teamId,
|
||||
})
|
||||
|
||||
const result = await providerService.applySync(teamId, body)
|
||||
await auditLogService.logAction({
|
||||
action: AuditAction.provider_update,
|
||||
teamId,
|
||||
userId: user?.id,
|
||||
})
|
||||
const result = await providerService.importModels(teamId, providerId, body)
|
||||
if (result.addedModels > 0) {
|
||||
await auditLogService.logAction({
|
||||
action: AuditAction.provider_update,
|
||||
teamId,
|
||||
userId: user?.id,
|
||||
itemId: providerId,
|
||||
})
|
||||
}
|
||||
return c.json(result)
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user