Add kling (#200)

This commit is contained in:
HCode authored and GitHub committed 2025-03-12 23:08:22 +08:00
1 parent e12c9d96cc
commit 5f0ec79e83
43 files changed
+2718

No files matched your search

+2
View File
@@ -5,6 +5,7 @@ import { IChatdocState } from '../chatdoc/models';
import { IQrartState } from '../qrart/models';
import { ILumaState } from '../luma/models';
import { IPikaState } from '../pika/models';
import { IKlingState } from '../kling/models';
import { IFluxState } from '../flux/models';
import { IHailuoState } from '../hailuo/models';
import { IHeadshotsState } from '../headshots/models';
@@ -37,6 +38,7 @@ export interface IRootState extends ICommonState {
qrart: IQrartState;
luma: ILumaState;
pika: IPikaState;
kling: IKlingState;
flux: IFluxState;
hailuo: IHailuoState;
headshots: IHeadshotsState;
+8
View File
@@ -4,6 +4,10 @@ import midjourneyState from '../midjourney/state';
import chatdocState from '../chatdoc/state';
import qrartState from '../qrart/state';
import lumaState from '../luma/state';
import pikaState from '../pika/state';
import klingState from '../kling/state';
import fluxState from '../flux/state';
import hailuoState from '../hailuo/state';
import sunoState from '../suno/state';
import headshotsState from '../headshots/state';
@@ -29,6 +33,10 @@ export default (): IRootState => {
midjourney: midjourneyState(),
qrart: qrartState(),
luma: lumaState(),
pika: pikaState(),
kling: klingState(),
flux: fluxState(),
hailuo: hailuoState(),
suno: sunoState(),
headshots: headshotsState()
};
+4
View File
@@ -6,6 +6,7 @@ import chatdoc from './chatdoc';
import qrart from './qrart';
import luma from './luma';
import pika from './pika';
import kling from './kling';
import flux from './flux';
import hailuo from './hailuo';
import headshots from './headshots';
@@ -17,6 +18,7 @@ import persistChatdoc from './chatdoc/persist';
import persistQrart from './qrart/persist';
import persistLuma from './luma/persist';
import persistPika from './pika/persist';
import persistKling from './kling/persist';
import persistFlux from './flux/persist';
import persistHailuo from './hailuo/persist';
import persistHeadshots from './headshots/persist';
@@ -31,6 +33,7 @@ const store = createStore({
qrart: qrart,
luma: luma,
pika: pika,
kling: kling,
flux: flux,
hailuo: hailuo,
headshots: headshots,
@@ -47,6 +50,7 @@ const store = createStore({
...persistQrart,
...persistLuma,
...persistPika,
...persistKling,
...persistFlux,
...persistHailuo,
...persistHeadshots,
+196
View File
@@ -0,0 +1,196 @@
import { applicationOperator, credentialOperator, klingOperator, serviceOperator } from '@/operators';
import { IKlingState } from './models';
import { ActionContext } from 'vuex';
import { IRootState } from '../common/models';
import { IApplication, ICredential, IKlingConfig, IKlingTask, IService } from '@/models';
import { Status } from '@/models/common';
import { KLING_SERVICE_ID } from '@/constants';
import { mergeAndSortLists } from '@/utils/merge';
import { getFinalApplication } from '@/utils';
export const resetAll = ({ commit }: ActionContext<IKlingState, IRootState>): void => {
commit('resetAll');
};
export const setApplication = async ({ commit, dispatch }: any, payload: IApplication): Promise<void> => {
console.debug('set application', payload);
commit('setApplication', payload);
if (!payload) {
console.debug('application is null, return');
return;
}
const credential = payload?.credentials?.find((credential) => credential?.host === window.location.origin);
if (credential) {
console.debug('credential exists, set credential', credential);
commit('setCredential', credential);
} else {
console.debug('credential not exists, start to create credential for application', payload);
await dispatch('createCredential');
}
};
export const setApplications = async ({ commit }: any, payload: IApplication[]): Promise<void> => {
console.debug('set applications', payload);
commit('setApplications', payload);
};
export const setService = async ({ commit }: any, payload: IService): Promise<void> => {
console.debug('set service', payload);
commit('setService', payload);
};
export const setCredential = async ({ commit }: any, payload: ICredential): Promise<void> => {
console.debug('set credential', payload);
commit('setCredential', payload);
};
export const createCredential = async ({ commit, state }: any): Promise<ICredential | undefined> => {
const application = state.application;
console.debug('prepare to create credential for application', application);
if (!application) {
console.error('Application not found');
return undefined;
}
console.debug('creating create credential for application', application);
const { data: credential } = await credentialOperator.create({
application_id: application?.id,
host: window.location.origin
});
console.debug('created credential success', credential);
commit('setCredential', credential);
console.debug('end createCredential');
return credential;
};
export const getService = async ({
commit,
state
}: ActionContext<IKlingState, IRootState>): Promise<IService | undefined> => {
state.status.getService = Status.Request;
try {
const { data: service } = await serviceOperator.get(KLING_SERVICE_ID);
state.status.getService = Status.Success;
commit('setService', service);
return service;
} catch (error) {
state.status.getService = Status.Error;
commit('setService', undefined);
}
};
export const getApplications = async ({
commit,
state,
rootState
}: ActionContext<IKlingState, IRootState>): Promise<IApplication[] | undefined> => {
console.debug('start to get applications for chat');
state.status.getApplications = Status.Request;
const currentApplication = state.application;
console.debug('current application', currentApplication);
try {
const { data: applications } = await applicationOperator.getAll({
user_id: rootState?.user?.id,
service_id: KLING_SERVICE_ID
});
state.status.getApplications = Status.Success;
commit('setApplications', applications.items);
const finalApplication = getFinalApplication(applications.items, currentApplication);
if (finalApplication) {
console.debug('set final application', finalApplication, finalApplication?.type);
commit('setApplication', finalApplication);
} else {
console.debug('set application undefined', undefined);
commit('setApplication', undefined);
}
return applications.items;
} catch (error) {
console.error('get applications failed', error);
state.status.getApplications = Status.Error;
commit('setApplications', undefined);
commit('setApplication', undefined);
}
};
export const setConfig = ({ commit }: any, payload: IKlingConfig) => {
commit('setConfig', payload);
};
export const setTasks = ({ commit }: any, payload: any) => {
commit('setTasks', payload);
};
export const setTasksItems = ({ commit }: any, payload: IKlingTask[]) => {
commit('setTasksItems', payload);
};
export const setTasksTotal = ({ commit }: any, payload: number) => {
commit('setTasksTotal', payload);
};
export const setTasksActive = ({ commit }: any, payload: IKlingTask) => {
commit('setTasksActive', payload);
};
export const getTasks = async (
{ commit, state, rootState }: ActionContext<IKlingState, IRootState>,
{
offset,
limit,
createdAtMin,
createdAtMax
}: { offset?: number; limit?: number; createdAtMin?: number; createdAtMax?: number }
): Promise<IKlingTask[]> => {
return new Promise((resolve, reject) => {
console.debug('start to get tasks', offset, limit);
const credential = state.credential;
const token = credential?.token;
if (!token) {
return reject('no token');
}
klingOperator
.tasks(
{
userId: rootState?.user?.id,
createdAtMin,
createdAtMax,
type: 'videos'
},
{
token
}
)
.then((response) => {
console.debug('get videos tasks success', response.data.items);
// merge with existing tasks
const existingItems = state?.tasks?.items || [];
console.debug('existing items', existingItems);
const newItems = response.data.items || [];
console.debug('new items', newItems);
// sort and de-duplicate using created_at
const mergedItems = mergeAndSortLists(existingItems, newItems);
commit('setTasksItems', mergedItems);
commit('setTasksTotal', response.data.count);
resolve(response.data.items);
})
.catch((error) => {
return reject(error);
});
});
};
export default {
setService,
getService,
resetAll,
setCredential,
setConfig,
setApplication,
setApplications,
getApplications,
setTasks,
setTasksItems,
setTasksTotal,
setTasksActive,
getTasks,
createCredential
};
+14
View File
@@ -0,0 +1,14 @@
import { Module } from 'vuex';
import { IKlingState } from './models';
import actions from './actions';
import mutations from './mutations';
import state from './state';
export const kling: Module<IKlingState, any> = {
namespaced: true,
state,
mutations,
actions
};
export default kling;
+22
View File
@@ -0,0 +1,22 @@
import { IApplication, ICredential, IService, Status } from '@/models';
import { IKlingConfig, IKlingTask } from '@/models';
export interface IKlingState {
application: IApplication | undefined;
applications: IApplication[] | undefined;
service: IService | undefined;
credential: ICredential | undefined;
config: IKlingConfig | undefined;
tasks:
| {
items: IKlingTask[] | undefined;
total: number | undefined;
active: IKlingTask | undefined;
}
| undefined;
status: {
getService: Status;
getApplications: Status;
getTasks: Status;
};
}
+71
View File
@@ -0,0 +1,71 @@
import { IApplication, ICredential, IKlingConfig, IKlingTask, IService } from '@/models';
import { IKlingState } from './models';
export const resetAll = (state: IKlingState): void => {
state.service = undefined;
state.application = undefined;
state.config = undefined;
state.credential = undefined;
state.tasks = undefined;
};
export const setService = (state: IKlingState, payload: IService): void => {
state.service = payload;
};
export const setCredential = (state: IKlingState, payload: ICredential): void => {
state.credential = payload;
};
export const setApplication = (state: IKlingState, payload: IApplication): void => {
state.application = payload;
};
export const setApplications = (state: IKlingState, payload: IApplication[]): void => {
state.applications = payload;
};
export const setConfig = (state: IKlingState, payload: IKlingConfig): void => {
state.config = payload;
};
export const setTasksItems = (state: IKlingState, payload: IKlingTask[]): void => {
const newPayload = {
...state.tasks,
items: payload
} as typeof state.tasks;
state.tasks = newPayload;
};
export const setTasksTotal = (state: IKlingState, payload: number): void => {
const newPayload = {
...state.tasks,
total: payload
} as typeof state.tasks;
state.tasks = newPayload;
};
export const setTasksActive = (state: IKlingState, payload: IKlingTask): void => {
const newPayload = {
...state.tasks,
active: payload
} as typeof state.tasks;
state.tasks = newPayload;
};
export const setTasks = (state: IKlingState, payload: any): void => {
state.tasks = payload;
};
export default {
setTasks,
setApplication,
setApplications,
setConfig,
setCredential,
setService,
setTasksActive,
setTasksItems,
setTasksTotal,
resetAll
};
+1
View File
@@ -0,0 +1 @@
export default ['kling.credential', 'kling.application', 'kling.applications'];
+18
View File
@@ -0,0 +1,18 @@
import { IKlingState } from './models';
import { Status } from '@/models';
export default (): IKlingState => {
return {
service: undefined,
application: undefined,
applications: undefined,
tasks: undefined,
credential: undefined,
config: undefined,
status: {
getService: Status.None,
getApplications: Status.None,
getTasks: Status.None
}
};
};