fix(server): unsplash path mapping (#9159)

This commit is contained in:
darkskygit
2024-12-16 09:35:18 +00:00
parent 9a66c97f52
commit 5cc40114fc
3 changed files with 22 additions and 1 deletions

View File

@@ -466,8 +466,8 @@ export class CopilotController {
} }
} }
@CallMetric('ai', 'unsplash')
@Get('/unsplash/photos') @Get('/unsplash/photos')
@CallMetric('ai', 'unsplash')
async unsplashPhotos( async unsplashPhotos(
@Req() req: Request, @Req() req: Request,
@Res() res: Response, @Res() res: Response,

View File

@@ -40,6 +40,7 @@ import {
MockCopilotTestProvider, MockCopilotTestProvider,
sse2array, sse2array,
textToEventStream, textToEventStream,
unsplashSearch,
} from './utils/copilot'; } from './utils/copilot';
const test = ava as TestFn<{ const test = ava as TestFn<{
@@ -62,6 +63,7 @@ test.beforeEach(async t => {
fal: { fal: {
apiKey: '1', apiKey: '1',
}, },
unsplashKey: process.env.UNSPLASH_ACCESS_KEY || '1',
}, },
}, },
}), }),
@@ -645,3 +647,10 @@ test('should reject request that user have not permission', async t => {
); );
} }
}); });
test('should be able to search image from unsplash', async t => {
const { app } = t.context;
const resp = await unsplashSearch(app, token);
t.not(resp.status, 404, 'route should be exists');
});

View File

@@ -286,6 +286,18 @@ export async function chatWithImages(
return chatWithText(app, userToken, sessionId, messageId, '/images'); return chatWithText(app, userToken, sessionId, messageId, '/images');
} }
export async function unsplashSearch(
app: INestApplication,
userToken: string,
params: Record<string, string> = {}
) {
const query = new URLSearchParams(params);
const res = await request(app.getHttpServer())
.get(`/api/copilot/unsplash/photos?${query}`)
.auth(userToken, { type: 'bearer' });
return res;
}
export function sse2array(eventSource: string) { export function sse2array(eventSource: string) {
const blocks = eventSource.replace(/^\n(.*?)\n$/, '$1').split(/\n\n+/); const blocks = eventSource.replace(/^\n(.*?)\n$/, '$1').split(/\n\n+/);
return blocks.map(block => return blocks.map(block =>