mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 04:14:30 +00:00
feat(search): add image similarity search (#537)
* feat(search): add image similarity search * feat(search): support pasting image attachments
This commit is contained in:
@@ -60,6 +60,7 @@ describe('folder api', () => {
|
||||
vi.spyOn(assetService, 'restoreAssets').mockImplementation(vi.fn())
|
||||
vi.spyOn(assetService, 'listChildren').mockImplementation(vi.fn())
|
||||
vi.spyOn(searchService, 'search').mockImplementation(vi.fn())
|
||||
vi.spyOn(searchService, 'searchByImage').mockResolvedValue({ data: [], pageInfo: { total: 0 } })
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
@@ -329,6 +330,83 @@ describe('folder api', () => {
|
||||
)
|
||||
})
|
||||
|
||||
describe('POST /folders/:folderId/image-search', () => {
|
||||
function request(body: Record<string, unknown>) {
|
||||
const app = new Hono().use('*', authMiddleware).route('/', folderRoute)
|
||||
return app.request('/folders/scope-id/image-search', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
}
|
||||
|
||||
it('checks both permissions and passes a stored image search to Core', async () => {
|
||||
const res = await request({ sourceAssetId: 'image-id', mediaType: 'video', first: 20 })
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.json()).toEqual({ data: [], pageInfo: { total: 0 } })
|
||||
expect(vi.mocked(authzService.hasPermission).mock.calls.map(([input]) => input.id)).toEqual([
|
||||
'scope-id',
|
||||
'image-id',
|
||||
])
|
||||
expect(authzService.hasPermission).toHaveBeenLastCalledWith(
|
||||
expect.objectContaining({
|
||||
id: 'image-id',
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Asset,
|
||||
}),
|
||||
)
|
||||
expect(searchService.searchByImage).toHaveBeenCalledWith('scope-id', {
|
||||
sourceAssetId: 'image-id',
|
||||
mediaType: 'video',
|
||||
first: 20,
|
||||
operator: 'AND',
|
||||
conditions: [],
|
||||
})
|
||||
})
|
||||
|
||||
it('checks folder read access before passing image data to Core', async () => {
|
||||
const conditions = [{ field: 'status', operator: 'eq', value: 'approved' }]
|
||||
const res = await request({ image: 'aW1hZ2U=', conditions })
|
||||
expect(res.status).toBe(200)
|
||||
expect(authzService.hasPermission).toHaveBeenCalledTimes(1)
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
id: 'scope-id',
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Asset,
|
||||
}),
|
||||
)
|
||||
expect(searchService.searchByImage).toHaveBeenCalledWith('scope-id', {
|
||||
image: 'aW1hZ2U=',
|
||||
conditions,
|
||||
operator: 'AND',
|
||||
})
|
||||
})
|
||||
|
||||
it('rejects a denied folder or source before running the search', async () => {
|
||||
vi.mocked(authzService.hasPermission).mockRejectedValueOnce(new HTTPException(403))
|
||||
expect((await request({ image: 'aW1hZ2U=' })).status).toBe(403)
|
||||
vi.mocked(authzService.hasPermission)
|
||||
.mockResolvedValueOnce(undefined)
|
||||
.mockRejectedValueOnce(new HTTPException(403))
|
||||
expect((await request({ sourceAssetId: 'image-id' })).status).toBe(403)
|
||||
expect(searchService.searchByImage).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects incomplete, ambiguous, and invalid search inputs', async () => {
|
||||
for (const body of [
|
||||
{},
|
||||
{ image: 'aW1hZ2U=', sourceAssetId: 'both' },
|
||||
{ image: 'invalid!' },
|
||||
{ sourceAssetId: 'image-id', mediaType: 'audio' },
|
||||
]) {
|
||||
expect((await request(body)).status).toBe(400)
|
||||
}
|
||||
expect((await request({ image: 'A'.repeat(1_600_000) })).status).toBe(413)
|
||||
expect(searchService.searchByImage).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
it('PATCH /folders/:folderId/order', async () => {
|
||||
vi.spyOn(assetService, 'updateAssetOrder').mockResolvedValue({
|
||||
id: 'test-id',
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Hono } from 'hono'
|
||||
import { zValidator } from '@hono/zod-validator'
|
||||
import { bodyLimit } from 'hono/body-limit'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import { searchService } from '@shumai/core/src/search/search'
|
||||
@@ -11,7 +12,11 @@ import {
|
||||
updateAgentsMdRequestSchema,
|
||||
} from '@shumai/dtos'
|
||||
import { listChildrenRequestSchema, updateAssetOrderRequestSchema, AuditAction } from '@shumai/dtos'
|
||||
import { searchRequestSchema } from '@shumai/dtos'
|
||||
import {
|
||||
imageSearchRequestSchema,
|
||||
MAX_SEARCH_IMAGE_BASE64_LENGTH,
|
||||
searchRequestSchema,
|
||||
} from '@shumai/dtos'
|
||||
import type { Prisma } from '@shumai/db'
|
||||
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
||||
import {
|
||||
@@ -154,6 +159,33 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
const result = await searchService.search(folderId, req)
|
||||
return c.json(result)
|
||||
})
|
||||
.post(
|
||||
'/folders/:folderId/image-search',
|
||||
bodyLimit({ maxSize: MAX_SEARCH_IMAGE_BASE64_LENGTH + 64 * 1024 }),
|
||||
zValidator('json', imageSearchRequestSchema),
|
||||
async (c) => {
|
||||
const folderId = c.req.param('folderId')
|
||||
const user = c.get('user')
|
||||
const req = c.req.valid('json')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Asset,
|
||||
id: folderId,
|
||||
})
|
||||
if (req.sourceAssetId) {
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Asset,
|
||||
id: req.sourceAssetId,
|
||||
})
|
||||
}
|
||||
|
||||
return c.json(await searchService.searchByImage(folderId, req))
|
||||
},
|
||||
)
|
||||
.get('/folders/:folderId/agentsmd', async (c) => {
|
||||
const { content } = await runOperation(
|
||||
getFolderInstructionsOp,
|
||||
|
||||
Reference in New Issue
Block a user