feat(web): 完善 API 文档与异步任务说明
This commit is contained in:
@@ -1,5 +1,14 @@
|
||||
import type { GatewayTask } from '@easyai-ai-gateway/contracts';
|
||||
import { createCompatibleChatCompletion, createEmbedding, createImageEditTask, createImageGenerationTask, createRerank } from '../api';
|
||||
import {
|
||||
createCompatibleChatCompletion,
|
||||
createEmbedding,
|
||||
createImageEditTask,
|
||||
createImageGenerationTask,
|
||||
createRerank,
|
||||
createResponse,
|
||||
createVideoGenerationTask,
|
||||
getAPITask,
|
||||
} from '../api';
|
||||
import type { TaskForm } from '../types';
|
||||
|
||||
export interface RunTaskResponse {
|
||||
@@ -19,6 +28,19 @@ export async function runTask(token: string, task: TaskForm): Promise<RunTaskRes
|
||||
});
|
||||
return { localOnly: true, task: compatibleTask(task, result) };
|
||||
}
|
||||
if (task.kind === 'responses') {
|
||||
const result = await createResponse(token, {
|
||||
model: task.model,
|
||||
input: task.prompt,
|
||||
instructions: task.instructions,
|
||||
previous_response_id: task.previousResponseId,
|
||||
runMode: 'simulation',
|
||||
simulation: true,
|
||||
store: true,
|
||||
stream: false,
|
||||
});
|
||||
return { localOnly: true, task: compatibleTask(task, result) };
|
||||
}
|
||||
if (task.kind === 'embeddings') {
|
||||
const result = await createEmbedding(token, {
|
||||
model: task.model,
|
||||
@@ -60,6 +82,24 @@ export async function runTask(token: string, task: TaskForm): Promise<RunTaskRes
|
||||
simulation: true,
|
||||
});
|
||||
}
|
||||
if (task.kind === 'videos.generations') {
|
||||
return createVideoGenerationTask(token, {
|
||||
model: task.model,
|
||||
content: [{ type: 'text', text: task.prompt }],
|
||||
aspect_ratio: task.aspectRatio ?? '16:9',
|
||||
resolution: task.resolution ?? '720p',
|
||||
duration: task.duration ?? 5,
|
||||
audio: task.outputAudio ?? true,
|
||||
runMode: 'simulation',
|
||||
simulation: true,
|
||||
});
|
||||
}
|
||||
if (task.kind === 'tasks.retrieve') {
|
||||
const taskId = task.taskId?.trim();
|
||||
if (!taskId) throw new Error('请输入要取回的 Task ID');
|
||||
const result = await getAPITask(token, taskId);
|
||||
return { localOnly: true, task: compatibleTask(task, result as unknown as Record<string, unknown>) };
|
||||
}
|
||||
throw new Error(`Unsupported task kind: ${task.kind}`);
|
||||
}
|
||||
|
||||
@@ -83,6 +123,18 @@ function compatibleTask(task: TaskForm, result: Record<string, unknown>): Gatewa
|
||||
}
|
||||
|
||||
function requestSnapshot(task: TaskForm): Record<string, unknown> {
|
||||
if (task.kind === 'responses') {
|
||||
return {
|
||||
model: task.model,
|
||||
input: task.prompt,
|
||||
instructions: task.instructions,
|
||||
previous_response_id: task.previousResponseId,
|
||||
runMode: 'simulation',
|
||||
simulation: true,
|
||||
store: true,
|
||||
stream: false,
|
||||
};
|
||||
}
|
||||
if (task.kind === 'embeddings') {
|
||||
return {
|
||||
model: task.model,
|
||||
@@ -102,6 +154,19 @@ function requestSnapshot(task: TaskForm): Record<string, unknown> {
|
||||
simulation: true,
|
||||
};
|
||||
}
|
||||
if (task.kind === 'videos.generations') {
|
||||
return {
|
||||
model: task.model,
|
||||
content: [{ type: 'text', text: task.prompt }],
|
||||
aspect_ratio: task.aspectRatio ?? '16:9',
|
||||
resolution: task.resolution ?? '720p',
|
||||
duration: task.duration ?? 5,
|
||||
audio: task.outputAudio ?? true,
|
||||
runMode: 'simulation',
|
||||
simulation: true,
|
||||
};
|
||||
}
|
||||
if (task.kind === 'tasks.retrieve') return { taskId: task.taskId };
|
||||
return {
|
||||
model: task.model,
|
||||
messages: [{ role: 'user', content: task.prompt }],
|
||||
@@ -133,5 +198,7 @@ function modelTypeForKind(kind: TaskForm['kind']) {
|
||||
if (kind === 'reranks') return 'text_rerank';
|
||||
if (kind === 'images.generations') return 'image_generate';
|
||||
if (kind === 'images.edits') return 'image_edit';
|
||||
if (kind === 'videos.generations') return 'video_generate';
|
||||
if (kind === 'tasks.retrieve') return 'task';
|
||||
return 'text_generate';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user