Skip to content

Commit

Permalink
fix: add log and fix 400 error in GET /image (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvqq authored May 16, 2023
1 parent 42b113e commit 9083617
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"markdown-it": "^13.0.1",
"markdown-it-highlightjs": "^4.0.1",
"markdown-it-kbd": "^2.2.2",
"midjourney-fetch": "0.1.2",
"midjourney-fetch": "0.1.3",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"replicate-fetch": "^0.1.1",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions src/modules/Content/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import { FC, useContext, useEffect, useState } from 'react';
import MessageBox from '@components/MessageBox';
import { Message, ReactSetState } from '@interfaces';
Expand Down Expand Up @@ -240,16 +241,19 @@ const Content: FC<ContentProps> = ({ setActiveSetting }) => {
await new Promise((resp) =>
setTimeout(resp, midjourneyConfigs.interval)
);
const message: MessageItem = await (
const message: MessageItem & { msg?: string } = await (
await fetch(
`/api/images?model=Midjourney&prompt=${content}&serverId=${configs.discordServerId}&channelId=${configs.discordChannelId}&token=${configs.discordToken}`
)
).json();
if (message && !isInProgress(message)) {
console.log(count, JSON.stringify(message));
// msg means error message
if (message && !message.msg && !isInProgress(message)) {
[image] = message.attachments;
break;
}
} catch {
} catch (e) {
console.log(count, e.message || e.stack || e);
continue;
}
}
Expand Down
33 changes: 21 additions & 12 deletions src/pages/api/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { APIRoute } from 'astro';
import { loadBalancer } from '@utils/server';
import { createOpenjourney } from 'replicate-fetch';
import { SupportedImageModels } from '@configs';
import { Midjourney, findMessageByPrompt } from 'midjourney-fetch';
import { Midjourney } from 'midjourney-fetch';
import {
apiKeyStrategy,
apiKeys,
Expand All @@ -22,9 +22,9 @@ export const get: APIRoute = async ({ request }) => {
const params = new URL(url).searchParams;

const model = params.get('model') as SupportedImageModels;
const serverId = params.get('serverId') ?? dicordServerId;
const channelId = params.get('channelId') ?? discordChannelId;
const token = params.get('token') ?? discordToken;
const serverId = params.get('serverId') || dicordServerId;
const channelId = params.get('channelId') || discordChannelId;
const token = params.get('token') || discordToken;
const prompt = params.get('prompt');

if (model === 'Midjourney') {
Expand Down Expand Up @@ -53,19 +53,28 @@ export const get: APIRoute = async ({ request }) => {
const midjourney = new Midjourney({
serverId,
channelId,
discordToken: token,
token,
});
midjourney.debugger = true;
try {
const message = await midjourney.getMessage(prompt);

const messgaes = await midjourney.getMessages();

const message = findMessageByPrompt(messgaes, prompt);
if (message) {
return new Response(JSON.stringify(message), { status: 200 });
if (message) {
return new Response(JSON.stringify(message), { status: 200 });
}
return new Response(JSON.stringify({ msg: 'No content found' }), {
status: 200,
});
} catch (e) {
return new Response(JSON.stringify({ msg: e.message || e.stack || e }), {
status: 500,
});
}
}

return new Response(JSON.stringify({ data: {} }), { status: 200 });
return new Response('{}', {
status: 200,
});
};

export const post: APIRoute = async ({ request }) => {
Expand Down Expand Up @@ -141,7 +150,7 @@ export const post: APIRoute = async ({ request }) => {
const midjourney = new Midjourney({
serverId,
channelId,
discordToken: token,
token,
});
midjourney.debugger = true;

Expand Down

0 comments on commit 9083617

Please sign in to comment.