Implement V2 AI functions and enhance progress handling
- Added support for new V2 functions: `auto_cluster_v2` and `generate_ideas_v2`, including backend logic and API endpoints. - Updated model configuration to ensure V2 functions validate the presence of models before execution. - Enhanced progress modal to provide better feedback during asynchronous tasks, including task IDs for debugging. - Updated frontend components to integrate new V2 functionalities and improve user experience with clustering and idea generation.
This commit is contained in:
@@ -807,6 +807,140 @@ export async function autoGenerateIdeas(clusterIds: number[]): Promise<{ success
|
||||
}
|
||||
}
|
||||
|
||||
export async function autoClusterKeywordsV2(keywordIds: number[], sectorId?: number): Promise<{ success: boolean; task_id?: string; clusters_created?: number; keywords_updated?: number; message?: string; error?: string }> {
|
||||
const startTime = Date.now();
|
||||
const addLog = useAIRequestLogsStore.getState().addLog;
|
||||
|
||||
const endpoint = `/v1/planner/keywords/auto_cluster_v2/`;
|
||||
const requestBody = { ids: keywordIds, sector_id: sectorId };
|
||||
|
||||
const pendingLogId = addLog({
|
||||
function: 'autoClusterKeywordsV2',
|
||||
endpoint,
|
||||
request: {
|
||||
method: 'POST',
|
||||
body: requestBody,
|
||||
},
|
||||
status: 'pending',
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await fetchAPI(endpoint, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
const duration = Date.now() - startTime;
|
||||
const updateLog = useAIRequestLogsStore.getState().updateLog;
|
||||
|
||||
if (pendingLogId && response) {
|
||||
updateLog(pendingLogId, {
|
||||
response: {
|
||||
status: 200,
|
||||
data: response,
|
||||
},
|
||||
status: response.success === false ? 'error' : 'success',
|
||||
duration,
|
||||
});
|
||||
}
|
||||
|
||||
if (response && response.success === false) {
|
||||
return response;
|
||||
}
|
||||
|
||||
return response;
|
||||
} catch (error: any) {
|
||||
const duration = Date.now() - startTime;
|
||||
const updateLog = useAIRequestLogsStore.getState().updateLog;
|
||||
|
||||
let errorMessage = error.message || 'Unknown error';
|
||||
|
||||
if (pendingLogId) {
|
||||
updateLog(pendingLogId, {
|
||||
response: {
|
||||
status: 500,
|
||||
error: errorMessage,
|
||||
},
|
||||
status: 'error',
|
||||
duration,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
error: errorMessage,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function generateIdeasV2(clusterIds: number[]): Promise<{ success: boolean; task_id?: string; ideas_created?: number; message?: string; error?: string }> {
|
||||
const startTime = Date.now();
|
||||
const { useAIRequestLogsStore } = await import('../store/aiRequestLogsStore').catch(() => ({ useAIRequestLogsStore: null }));
|
||||
const addLog = useAIRequestLogsStore?.getState().addLog;
|
||||
|
||||
const endpoint = `/v1/planner/clusters/generate_ideas_v2/`;
|
||||
const requestBody = { ids: clusterIds };
|
||||
|
||||
addLog?.({
|
||||
function: 'generateIdeasV2',
|
||||
endpoint,
|
||||
request: {
|
||||
method: 'POST',
|
||||
body: requestBody,
|
||||
},
|
||||
status: 'pending',
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await fetchAPI(endpoint, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
const duration = Date.now() - startTime;
|
||||
addLog?.({
|
||||
function: 'generateIdeasV2',
|
||||
endpoint,
|
||||
request: {
|
||||
method: 'POST',
|
||||
body: requestBody,
|
||||
},
|
||||
response: {
|
||||
status: 200,
|
||||
data: response,
|
||||
},
|
||||
status: 'success',
|
||||
duration,
|
||||
});
|
||||
|
||||
return response;
|
||||
} catch (error: any) {
|
||||
const duration = Date.now() - startTime;
|
||||
|
||||
let errorMessage = error.message || 'Unknown error';
|
||||
|
||||
addLog?.({
|
||||
function: 'generateIdeasV2',
|
||||
endpoint,
|
||||
request: {
|
||||
method: 'POST',
|
||||
body: requestBody,
|
||||
},
|
||||
response: {
|
||||
status: 500,
|
||||
error: errorMessage,
|
||||
},
|
||||
status: 'error',
|
||||
duration,
|
||||
});
|
||||
|
||||
return {
|
||||
success: false,
|
||||
error: errorMessage,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function generateSingleIdea(ideaId: string | number, clusterId: number): Promise<{ success: boolean; task_id?: string; idea_created?: number; message?: string; error?: string }> {
|
||||
const startTime = Date.now();
|
||||
const { useAIRequestLogsStore } = await import('../store/aiRequestLogsStore').catch(() => ({ useAIRequestLogsStore: null }));
|
||||
|
||||
Reference in New Issue
Block a user