Skip to content

Commit

Permalink
update operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Germey committed Mar 2, 2024
1 parent b11b8f0 commit c93e229
Show file tree
Hide file tree
Showing 18 changed files with 220 additions and 66 deletions.
4 changes: 4 additions & 0 deletions src/layouts/Chat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ export default defineComponent({
};
},
async mounted() {
await this.onGetService();
await this.onGetApplication();
await this.onGetConversations();
},
methods: {
async onGetService() {
await this.$store.dispatch('chat/getService');
},
async onGetApplication() {
await this.$store.dispatch('chat/getApplication');
},
Expand Down
12 changes: 12 additions & 0 deletions src/layouts/Midjourney.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ export default defineComponent({
drawer: false,
drawer2: false
};
},
async mounted() {
await this.onGetService();
await this.onGetApplication();
},
methods: {
async onGetService() {
await this.$store.dispatch('midjourney/getService');
},
async onGetApplication() {
await this.$store.dispatch('midjourney/getApplication');
}
}
});
</script>
Expand Down
7 changes: 7 additions & 0 deletions src/models/midjourney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,15 @@ export enum MidjourneyImagineState {
FAILED = 'failed'
}

export enum MidjourneyImagineMode {
FAST = 'fast',
RELAX = 'relax',
TURBO = 'turbo'
}

export interface IMidjourneyImagineRequest {
action?: MidjourneyImagineAction;
mode?: MidjourneyImagineMode;
prompt?: string;
image_id?: string;
translation?: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/operators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './order';
export * from './distribution';
export * from './chatdoc';
export * from './auth';
export * from './service';
45 changes: 22 additions & 23 deletions src/operators/midjourney.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import axios, { AxiosProgressEvent, AxiosResponse } from 'axios';
import axios, { AxiosResponse } from 'axios';
import { IMidjourneyImagineRequest, IMidjourneyImagineResponse, IMidjourneyImagineTask } from '@/models';
import { BASE_URL_API } from '@/constants';

class MidjourneyOperator {
async task(id: string): Promise<AxiosResponse<IMidjourneyImagineTask>> {
async task(id: string, options: { token: string }): Promise<AxiosResponse<IMidjourneyImagineTask>> {
return await axios.post(
`/midjourney/tasks`,
{
Expand All @@ -13,14 +13,18 @@ class MidjourneyOperator {
{
headers: {
accept: 'application/json',
'content-type': 'application/json'
'content-type': 'application/json',
authorization: `Bearer ${options.token}`
},
baseURL: BASE_URL_API
}
);
}

async tasks(filter: { ids?: string[]; applicationId?: string }): Promise<AxiosResponse<IMidjourneyImagineTask[]>> {
async tasks(
filter: { ids?: string[]; applicationId?: string; limit?: number; offset?: number },
options: { token: string }
): Promise<AxiosResponse<IMidjourneyImagineTask[]>> {
return await axios.post(
`/midjourney/tasks`,
{
Expand All @@ -34,12 +38,23 @@ class MidjourneyOperator {
? {
application_id: filter.applicationId
}
: {}),
...(filter.limit !== undefined
? {
limit: filter.limit
}
: {}),
...(filter.offset !== undefined
? {
offset: filter.offset
}
: {})
},
{
headers: {
accept: 'application/json',
'content-type': 'application/json'
'content-type': 'application/json',
authorization: `Bearer ${options.token}`
},
baseURL: BASE_URL_API
}
Expand All @@ -49,31 +64,15 @@ class MidjourneyOperator {
async imagine(
data: IMidjourneyImagineRequest,
options: {
stream?: (response: IMidjourneyImagineResponse) => void;
token: string;
endpoint: string;
path: string;
}
): Promise<AxiosResponse<IMidjourneyImagineResponse>> {
return await axios.post(options.path, data, {
return await axios.post('/midjourney/imagine', data, {
headers: {
authorization: `Bearer ${options.token}`,
accept: 'application/x-ndjson',
'content-type': 'application/json'
},
baseURL: options.endpoint,
responseType: 'stream',
onDownloadProgress: ({ event }: AxiosProgressEvent) => {
const response = event.target.response;
const lines = response.split('\r\n').filter((line: string) => !!line);
const lastLine = lines[lines.length - 1];
if (lastLine) {
const jsonData = JSON.parse(lastLine);
if (options?.stream) {
options?.stream(jsonData as IMidjourneyImagineResponse);
}
}
}
baseURL: BASE_URL_API
});
}
}
Expand Down
37 changes: 37 additions & 0 deletions src/operators/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { AxiosResponse } from 'axios';
import { httpClient } from './common';
import { IService, IServiceDetailResponse, IServiceListResponse } from '@/models';

export interface IServiceQuery {
limit?: number;
offset?: number;
ordering?: string;
}

class ServiceOperator {
key = 'services';

async getAll(query: IServiceQuery): Promise<AxiosResponse<IServiceListResponse>> {
return await httpClient.get(`/${this.key}/`, {
params: query
});
}

async get(id: string): Promise<AxiosResponse<IServiceDetailResponse>> {
return await httpClient.get(`/${this.key}/${id}`);
}

async create(data: IService): Promise<AxiosResponse<IServiceDetailResponse>> {
return await httpClient.post(`/${this.key}/`, data);
}

async update(id: string, data: IService): Promise<AxiosResponse<IServiceDetailResponse>> {
return await httpClient.put(`/${this.key}/${id}`, data);
}

async delete(id: string): Promise<AxiosResponse<null>> {
return await httpClient.delete(`/${this.key}/${id}`);
}
}

export const serviceOperator = new ServiceOperator();
11 changes: 7 additions & 4 deletions src/pages/chatdoc/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
{{ $t('chatdoc.message.introductionForRepository') }}
</div>
<div class="status">
<api-status
<application-status
:initializing="initializing"
:application="application"
:need-apply="needApply"
:api-id="apiId"
:service="service"
@refresh="$store.dispatch('chatdoc/getApplication')"
/>
</div>
Expand Down Expand Up @@ -67,7 +67,7 @@ import { IChatdocRepository } from '@/models';
import { ROUTE_CHATDOC_MANAGE } from '@/router';
import CreateRepository from '@/components/chatdoc/CreateRepository.vue';
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
import ApiStatus from '@/components/common/ApiStatus.vue';
import ApplicationStatus from '@/components/application/Status.vue';
import { Status } from '@/models';
export default defineComponent({
Expand All @@ -82,7 +82,7 @@ export default defineComponent({
ElDropdown,
ElDropdownItem,
ElDropdownMenu,
ApiStatus
ApplicationStatus
},
data() {
return {};
Expand All @@ -97,6 +97,9 @@ export default defineComponent({
application() {
return this.$store.state.chatdoc.application;
},
service() {
return this.$store.state.chatdoc.service;
},
initializing() {
return this.$store.state.chatdoc.getApplicationStatus === Status.Request;
}
Expand Down
11 changes: 7 additions & 4 deletions src/pages/chatdoc/Manage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
{{ $t('chatdoc.message.introductionForKnowledge') }}
</div>
<div class="status">
<api-status
<application-status
:initializing="initializing"
:application="application"
:need-apply="needApply"
:api-id="apiId"
:service="service"
@refresh="$store.dispatch('chatdoc/getApplication')"
/>
</div>
Expand Down Expand Up @@ -68,7 +68,7 @@ import { IApplication, IChatdocRepository } from '@/models';
import { ElButton, ElTag, ElTable, ElTableColumn, ElMessage } from 'element-plus';
import UploadDocument from '@/components/chatdoc/UploadDocument.vue';
import { Status } from '@/models';
import ApiStatus from '@/components/common/ApiStatus.vue';
import ApplicationStatus from '@/components/application/Status.vue';
export default defineComponent({
name: 'ChatdocKnowledge',
Expand All @@ -77,7 +77,7 @@ export default defineComponent({
ElButton,
ElTable,
ElTag,
ApiStatus,
ApplicationStatus,
ElTableColumn,
UploadDocument
},
Expand All @@ -97,6 +97,9 @@ export default defineComponent({
needApply() {
return this.$store.state.chatdoc.getApplicationStatus === Status.Success && !this.application;
},
service() {
return this.$store.state.chatdoc.service;
},
applications() {
return this.$store.state.chatdoc.applications;
},
Expand Down
21 changes: 8 additions & 13 deletions src/pages/midjourney/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,23 @@ export default defineComponent({
};
},
computed: {
service() {
return this.$store.state.midjourney.service;
},
mode() {
return this.$store.state.midjourney.mode;
},
preset() {
return this.$store.state.midjourney.preset;
},
initializing() {
return this.$store.state.midjourney.getApplicationStatus === Status.Request;
},
applications() {
return this.$store.state.midjourney.applications;
return this.$store.state.midjourney.status.getApplication === Status.Request;
},
needApply() {
return this.$store.state.midjourney.getApplicationStatus === Status.Success && !this.application;
return this.$store.state.midjourney.status.getApplication === Status.Success && !this.application;
},
application() {
if (this.applications && this.applications.length > 0) {
return this.applications.filter((item: IApplication) => item.api_id === this.channel.apiId)[0];
}
return undefined;
return this.$store.state.midjourney.application;
},
finalPrompt(): string {
let content = '';
Expand Down Expand Up @@ -170,10 +167,8 @@ export default defineComponent({
await this.$store.dispatch('midjourney/getApplication');
},
async onStartTask(request: IMidjourneyImagineRequest) {
const token = this.application?.credential?.token;
const endpoint = this.application?.api?.endpoint;
const path = this.application?.api?.path;
if (!token || !endpoint || !path) {
const token = this.application?.credentials?.[0]?.token;
if (!token) {
console.error('no token or endpoint or question');
return;
}
Expand Down
29 changes: 27 additions & 2 deletions src/store/chat/actions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { applicationOperator, chatOperator } from '@/operators';
import { applicationOperator, chatOperator, serviceOperator } from '@/operators';
import { IRootState } from '../common/models';
import { ActionContext } from 'vuex';
import { log } from '@/utils/log';
import { IChatState } from './models';
import { IApplication, IChatConversation, IChatModel, Status } from '@/models';
import { IApplication, IChatConversation, IChatModel, IService, Status } from '@/models';
import { CHAT_SERVICE_ID } from '@/constants';

export const resetAll = ({ commit }: ActionContext<IChatState, IRootState>): void => {
Expand All @@ -19,6 +19,11 @@ export const setApplication = async ({ commit }: any, payload: IApplication): Pr
commit('setApplication', payload);
};

export const setService = async ({ commit }: any, payload: IService): Promise<void> => {
log(setService, 'set service', payload);
commit('setService', payload);
};

export const setConversations = async ({ commit }: any, payload: IChatConversation[]): Promise<void> => {
log(setConversations, 'set conversations', payload);
commit('setConversations', payload);
Expand All @@ -37,6 +42,24 @@ export const setConversation = async ({ commit, state }: any, payload: IChatConv
log(setConversation, 'set conversation success', conversations);
};

export const getService = async ({ commit, state }: ActionContext<IChatState, IRootState>): Promise<IService> => {
return new Promise(async (resolve, reject) => {
log(getService, 'start to get service for chat');
state.status.getService = Status.Request;
serviceOperator
.get(CHAT_SERVICE_ID)
.then((response) => {
state.status.getService = Status.Success;
commit('setService', response.data);
resolve(response.data);
})
.catch((error) => {
state.status.getService = Status.Error;
reject(error);
});
});
};

export const getApplication = async ({
commit,
state,
Expand Down Expand Up @@ -98,6 +121,8 @@ export const getConversations = async ({
export default {
resetAll,
setModel,
getService,
setService,
setApplication,
getApplication,
setConversation,
Expand Down
1 change: 1 addition & 0 deletions src/store/chat/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface IChatState {
service: IService | undefined;
conversations: IChatConversation[] | undefined;
status: {
getService: Status;
getApplication: Status;
getConversations: Status;
};
Expand Down
Loading

0 comments on commit c93e229

Please sign in to comment.