fix: 完善AI流式后台任务与模型配置
This commit is contained in:
1 parent
0b35a58f07
commit
cdb64c5499
30 files changed
+1732
-150
No files matched your search
@@ -60,6 +60,20 @@ async function request<T>(config: AxiosRequestConfig): Promise<T> {
|
||||
}
|
||||
|
||||
export const api = {
|
||||
stageAiAttachment: (
|
||||
taskId: string,
|
||||
attachmentId: string,
|
||||
file: File,
|
||||
signal?: AbortSignal,
|
||||
): Promise<null> =>
|
||||
request({
|
||||
method: 'PUT',
|
||||
url: `/ai/tasks/${encodeURIComponent(taskId)}/files/${encodeURIComponent(attachmentId)}`,
|
||||
data: file,
|
||||
headers: { 'content-type': 'application/octet-stream' },
|
||||
signal,
|
||||
timeout: 600000,
|
||||
}),
|
||||
executeAi: <T>(
|
||||
stepId: string,
|
||||
action: AiExecutionRequest,
|
||||
|
||||
@@ -3,6 +3,8 @@ import type {
|
||||
AiConversationRequest,
|
||||
AiConversationResult,
|
||||
AiPlanStep,
|
||||
AiModelInfo,
|
||||
AiTaskDto,
|
||||
} from '@classhelper/shared';
|
||||
import {
|
||||
API_PATHS,
|
||||
@@ -671,6 +673,23 @@ const AI_BASE = API_PATHS.ai;
|
||||
* 解释和附件预览只保存计划与审计;确认后由绑定的原业务接口执行写入。
|
||||
*/
|
||||
export const aiApi = {
|
||||
tasks: (): Promise<AiTaskDto[]> => api.get(`${AI_BASE}/tasks`),
|
||||
task: (id: string): Promise<AiTaskDto> => api.get(`${AI_BASE}/tasks/${id}`),
|
||||
createTask: (payload: AiConversationRequest): Promise<{ id: string }> =>
|
||||
api.post(`${AI_BASE}/tasks`, payload),
|
||||
startTask: (id: string): Promise<null> => api.post(`${AI_BASE}/tasks/${id}/start`),
|
||||
stopTask: (id: string): Promise<null> => api.post(`${AI_BASE}/tasks/${id}/stop`),
|
||||
executeTask: (
|
||||
id: string,
|
||||
body: {
|
||||
confirmed: true;
|
||||
extraConfirmed?: boolean;
|
||||
urgentConfirmed?: boolean;
|
||||
secrets?: Record<string, Record<string, string>>;
|
||||
},
|
||||
): Promise<null> => api.post(`${AI_BASE}/tasks/${id}/execute`, body),
|
||||
models: (payload: { baseUrl?: string; apiKey?: string }): Promise<AiModelInfo[]> =>
|
||||
api.post(`${AI_BASE}/models`, payload),
|
||||
/**
|
||||
* 读取配置。
|
||||
*
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import {
|
||||
EventStreamDecoder,
|
||||
STORAGE_KEYS,
|
||||
type AiAgentEvent,
|
||||
type AiConversationResult,
|
||||
} from '@classhelper/shared';
|
||||
import { API_BASE_URL } from '@/config';
|
||||
import { clearCalendarCache } from './calendarCache';
|
||||
|
||||
export async function observeTask(
|
||||
id: string,
|
||||
onEvent: (event: AiAgentEvent) => void,
|
||||
signal?: AbortSignal,
|
||||
): Promise<AiConversationResult> {
|
||||
const response = await fetch(`${API_BASE_URL}/ai/tasks/${encodeURIComponent(id)}/events`, {
|
||||
signal,
|
||||
headers: {
|
||||
authorization: `Bearer ${localStorage.getItem(STORAGE_KEYS.token) ?? ''}`,
|
||||
accept: 'text/event-stream',
|
||||
},
|
||||
});
|
||||
return consume(response, onEvent);
|
||||
}
|
||||
async function consume(
|
||||
response: Response,
|
||||
onEvent: (event: AiAgentEvent) => void,
|
||||
): Promise<AiConversationResult> {
|
||||
if (!response.ok) {
|
||||
const body = await response.json().catch(() => ({}));
|
||||
if (response.status === 401) {
|
||||
clearCalendarCache();
|
||||
localStorage.removeItem(STORAGE_KEYS.token);
|
||||
localStorage.removeItem(STORAGE_KEYS.user);
|
||||
window.location.assign('/login');
|
||||
}
|
||||
throw Object.assign(new Error(body.message || '暂时无法连接助手,任务已保留。'), { code: body.code });
|
||||
}
|
||||
if (!response.body) throw new Error('连接暂时中断,任务已保留。');
|
||||
const reader = response.body.getReader(),
|
||||
decoder = new TextDecoder(),
|
||||
events = new EventStreamDecoder(16 * 1024 * 1024);
|
||||
try {
|
||||
while (true) {
|
||||
const chunk = await reader.read();
|
||||
if (chunk.done) break;
|
||||
for (const frame of events.push(decoder.decode(chunk.value, { stream: true }))) {
|
||||
const event = JSON.parse(frame) as AiAgentEvent;
|
||||
onEvent(event);
|
||||
if (event.type === 'error') throw Object.assign(new Error(event.message), { code: event.code });
|
||||
if (event.type === 'result') return event.result;
|
||||
}
|
||||
}
|
||||
throw new Error('连接暂时中断,任务已保留。点击重试继续。');
|
||||
} finally {
|
||||
await reader.cancel().catch(() => undefined);
|
||||
reader.releaseLock();
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,8 @@ import {
|
||||
type AiSettingsDto,
|
||||
type SystemDiagnosticsDto,
|
||||
type UpdateAiSettingsRequest,
|
||||
type AiModelInfo,
|
||||
type AiReasoningEffort,
|
||||
} from '@classhelper/shared';
|
||||
import { aiApi, systemApi } from '@/api';
|
||||
import { useAuthStore } from '@/stores/auth';
|
||||
@@ -36,6 +38,7 @@ const form = reactive({
|
||||
provider: 'openai' as Provider,
|
||||
baseUrl: '',
|
||||
model: '',
|
||||
reasoningEffort: 'auto' as AiReasoningEffort,
|
||||
systemPromptExtra: '',
|
||||
});
|
||||
|
||||
@@ -44,11 +47,46 @@ const hasApiKey = ref(false);
|
||||
const keyHint = ref('');
|
||||
/** 本次要写入的新密钥;留空 = 保持原密钥不变 */
|
||||
const apiKeyInput = ref('');
|
||||
const models = ref<AiModelInfo[]>([]),
|
||||
modelsLoading = ref(false),
|
||||
modelsError = ref('');
|
||||
const effortLabels: Record<AiReasoningEffort, string> = {
|
||||
auto: '自动',
|
||||
none: '快速 · 不启用推理',
|
||||
low: '低',
|
||||
medium: '中',
|
||||
high: '高',
|
||||
max: '最高',
|
||||
};
|
||||
const effortOptions = computed(
|
||||
() =>
|
||||
models.value.find((item) => item.id === form.model)?.efforts ??
|
||||
(['auto', 'none', 'low', 'medium', 'high'] as AiReasoningEffort[]),
|
||||
);
|
||||
async function loadModels(): Promise<void> {
|
||||
if (!form.baseUrl.trim() || (!hasApiKey.value && !apiKeyInput.value.trim()) || modelsLoading.value) return;
|
||||
modelsLoading.value = true;
|
||||
modelsError.value = '';
|
||||
try {
|
||||
models.value = await aiApi.models({
|
||||
baseUrl: form.baseUrl.trim(),
|
||||
...(apiKeyInput.value.trim() ? { apiKey: apiKeyInput.value.trim() } : {}),
|
||||
});
|
||||
if (!form.model) form.model = models.value[0]?.id ?? '';
|
||||
} catch {
|
||||
modelsError.value = '获取失败,可重试或手动填写模型名。';
|
||||
} finally {
|
||||
modelsLoading.value = false;
|
||||
}
|
||||
}
|
||||
function modelChanged(): void {
|
||||
if (!effortOptions.value.includes(form.reasoningEffort)) form.reasoningEffort = 'auto';
|
||||
}
|
||||
|
||||
const providerHint = computed(() =>
|
||||
form.provider === 'mock'
|
||||
? '离线示例解析:不联网,只按关键词猜测,供演示与验收。'
|
||||
: 'OpenAI 兼容接口:请求 {服务地址}/chat/completions,适用于 OpenAI、通义、DeepSeek 等兼容服务。',
|
||||
: 'OpenAI 兼容接口,支持基础地址或完整 /chat/completions 地址。',
|
||||
);
|
||||
|
||||
function applySettings(settings: AiSettingsDto): void {
|
||||
@@ -56,6 +94,7 @@ function applySettings(settings: AiSettingsDto): void {
|
||||
form.provider = settings.provider;
|
||||
form.baseUrl = settings.baseUrl;
|
||||
form.model = settings.model;
|
||||
form.reasoningEffort = settings.reasoningEffort ?? 'auto';
|
||||
form.systemPromptExtra = settings.systemPromptExtra;
|
||||
hasApiKey.value = settings.hasApiKey;
|
||||
keyHint.value = settings.keyHint;
|
||||
@@ -66,6 +105,7 @@ async function loadSettings(): Promise<void> {
|
||||
loading.value = true;
|
||||
try {
|
||||
applySettings(await aiApi.settings());
|
||||
if (form.provider === 'openai') await loadModels();
|
||||
} catch {
|
||||
// 响应拦截器已经弹过服务端的中文提示,这里只保证页面不是一片空白
|
||||
} finally {
|
||||
@@ -81,6 +121,7 @@ async function save(): Promise<void> {
|
||||
provider: form.provider,
|
||||
baseUrl: form.baseUrl.trim(),
|
||||
model: form.model.trim(),
|
||||
reasoningEffort: form.reasoningEffort,
|
||||
systemPromptExtra: form.systemPromptExtra,
|
||||
};
|
||||
// 只有真的填了新密钥才带上 apiKey:不带 = 保持原值,null = 清除
|
||||
@@ -271,15 +312,40 @@ onMounted(async () => {
|
||||
v-model="form.baseUrl"
|
||||
placeholder="https://api.openai.com/v1"
|
||||
class="field-input"
|
||||
@change="loadModels"
|
||||
/>
|
||||
<p class="field-hint">
|
||||
DeepSeek 可填 https://api.deepseek.com/chat/completions,模型名按服务商填写。
|
||||
</p>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="模型名">
|
||||
<el-input
|
||||
<el-select
|
||||
v-model="form.model"
|
||||
placeholder="gpt-4o-mini(留空即用默认)"
|
||||
filterable
|
||||
allow-create
|
||||
default-first-option
|
||||
placeholder="选择或填写模型"
|
||||
class="field-input"
|
||||
/>
|
||||
:loading="modelsLoading"
|
||||
@change="modelChanged"
|
||||
>
|
||||
<el-option v-for="model in models" :key="model.id" :value="model.id" :label="model.name" />
|
||||
</el-select>
|
||||
<el-button :loading="modelsLoading" @click="loadModels">获取模型</el-button>
|
||||
<p v-if="modelsError" class="field-hint">{{ modelsError }}</p>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="推理强度">
|
||||
<el-select v-model="form.reasoningEffort" class="field-input">
|
||||
<el-option
|
||||
v-for="level in effortOptions"
|
||||
:key="level"
|
||||
:value="level"
|
||||
:label="effortLabels[level]"
|
||||
/>
|
||||
</el-select>
|
||||
<p class="field-hint">自动使用服务商默认值,其他档位以模型支持为准。</p>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="API 密钥">
|
||||
@@ -289,6 +355,7 @@ onMounted(async () => {
|
||||
type="password"
|
||||
show-password
|
||||
class="field-input"
|
||||
@change="loadModels"
|
||||
:placeholder="
|
||||
hasApiKey ? `已配置(尾号 ${keyHint}),留空表示不修改` : '粘贴密钥,保存后不再显示'
|
||||
"
|
||||
|
||||
@@ -2,12 +2,13 @@
|
||||
import { computed, nextTick, onBeforeUnmount, onMounted, reactive, ref } from 'vue';
|
||||
import axios from 'axios';
|
||||
import { ElMessage, ElMessageBox } from 'element-plus';
|
||||
import { PageHeader } from '@classhelper/ui';
|
||||
import {
|
||||
COURSEWARE_MAX_BYTES,
|
||||
type AiAttachment,
|
||||
type AiCapability,
|
||||
type AiConversationResult,
|
||||
type AiConversationRequest,
|
||||
type AiTaskDto,
|
||||
type AiPlanStep,
|
||||
type ClassDto,
|
||||
type ClassPeriod,
|
||||
@@ -19,10 +20,13 @@ import { hashAttachment, importFileBase64 } from '@/composables/assistantFiles';
|
||||
import { findClassInSession } from '@/composables/useClassInSession';
|
||||
import AssistantData from '@/components/AssistantData.vue';
|
||||
import UrgentClassWarning from '@/components/UrgentClassWarning.vue';
|
||||
import { observeTask } from '@/composables/assistantStream';
|
||||
interface ChatMessage {
|
||||
role: 'user' | 'assistant';
|
||||
content: string;
|
||||
result?: AiConversationResult;
|
||||
activity?: string[];
|
||||
state?: 'working' | 'done' | 'stopped';
|
||||
}
|
||||
const auth = useAuthStore();
|
||||
const text = ref(''),
|
||||
@@ -39,6 +43,7 @@ const busy = ref(false),
|
||||
executing = ref(false),
|
||||
progress = ref(0),
|
||||
error = ref('');
|
||||
const followScroll = ref(true);
|
||||
const disabled = ref(false),
|
||||
dragging = ref(false),
|
||||
active = ref<AiConversationResult>();
|
||||
@@ -47,6 +52,10 @@ const urgent = reactive({ visible: false, period: null as ClassPeriod | null, ti
|
||||
let resolveUrgent: ((confirmed: boolean) => void) | undefined;
|
||||
let controller: AbortController | undefined;
|
||||
let disposed = false;
|
||||
let observing = false;
|
||||
let lastRequest: AiConversationRequest | undefined;
|
||||
const taskId = ref(''),
|
||||
taskHistory = ref<AiTaskDto[]>([]);
|
||||
const pending = computed(
|
||||
() => active.value?.steps.some((step) => !['succeeded', 'blocked'].includes(step.status)) ?? false,
|
||||
);
|
||||
@@ -79,6 +88,7 @@ function clearCredentials(): void {
|
||||
}
|
||||
async function scroll(): Promise<void> {
|
||||
await nextTick();
|
||||
if (!followScroll.value) return;
|
||||
conversation.value?.scrollTo({ top: conversation.value.scrollHeight, behavior: 'smooth' });
|
||||
}
|
||||
async function selectFiles(selected: FileList | File[]): Promise<void> {
|
||||
@@ -133,41 +143,66 @@ async function prepare(result: AiConversationResult): Promise<void> {
|
||||
result.steps = await aiApi.prepare(result.planId, { [id]: file });
|
||||
}
|
||||
async function retryTask(): Promise<void> {
|
||||
const last = [...messages.value].reverse().find((message) => message.role === 'user');
|
||||
if (last) {
|
||||
text.value = last.content;
|
||||
await send();
|
||||
}
|
||||
if (lastRequest) await send(true);
|
||||
}
|
||||
async function send(): Promise<void> {
|
||||
if (!text.value.trim() || busy.value || executing.value || hashing.value) return;
|
||||
async function send(retrying = false): Promise<void> {
|
||||
if ((!retrying && !text.value.trim()) || busy.value || executing.value || hashing.value) return;
|
||||
busy.value = true;
|
||||
error.value = '';
|
||||
controller = new AbortController();
|
||||
let message: ChatMessage | undefined;
|
||||
const uploadFiles = attachments.value.map((attachment) => ({ attachment, file: files.get(attachment.id) }));
|
||||
try {
|
||||
if (active.value?.planId && pending.value) await aiApi.discard(active.value.planId);
|
||||
clearCredentials();
|
||||
active.value = undefined;
|
||||
const content = text.value.trim(),
|
||||
const content = retrying ? lastRequest!.text : text.value.trim(),
|
||||
history = messages.value.slice(-18).map(({ role, content }) => ({ role, content }));
|
||||
messages.value.push({ role: 'user', content });
|
||||
if (!retrying) {
|
||||
lastRequest = {
|
||||
text: content,
|
||||
classId: classId.value || undefined,
|
||||
messages: history,
|
||||
attachments: [...attachments.value],
|
||||
};
|
||||
messages.value.push({ role: 'user', content });
|
||||
}
|
||||
message = reactive<ChatMessage>({ role: 'assistant', content: '', activity: [], state: 'working' });
|
||||
messages.value.push(message);
|
||||
text.value = '';
|
||||
followScroll.value = true;
|
||||
await scroll();
|
||||
if (!retrying || !taskId.value) {
|
||||
taskId.value = (await aiApi.createTask(lastRequest!)).id;
|
||||
message.activity!.push(
|
||||
attachments.value.length ? '正在上传附件,完成后将转入后台' : '任务已提交到后台',
|
||||
);
|
||||
for (const { attachment, file } of uploadFiles) {
|
||||
if (!file) throw new Error('请重新选择原附件');
|
||||
await api.stageAiAttachment(taskId.value, attachment.id, file, controller.signal);
|
||||
}
|
||||
}
|
||||
controller.signal.throwIfAborted();
|
||||
await aiApi.startTask(taskId.value);
|
||||
if (disposed) return;
|
||||
observing = true;
|
||||
const result = reactive(
|
||||
await aiApi.interpret(
|
||||
{
|
||||
text: content,
|
||||
classId: classId.value || undefined,
|
||||
messages: history,
|
||||
attachments: attachments.value,
|
||||
await observeTask(
|
||||
taskId.value,
|
||||
(event) => {
|
||||
if (disposed || controller?.signal.aborted) return;
|
||||
if (event.type === 'status' && message!.activity?.at(-1) !== event.message)
|
||||
message!.activity = [...(message!.activity ?? []), event.message].slice(-60);
|
||||
if (event.type === 'message') message!.content = event.text;
|
||||
void scroll();
|
||||
},
|
||||
controller.signal,
|
||||
),
|
||||
);
|
||||
if (disposed) return;
|
||||
active.value = result;
|
||||
const message = reactive<ChatMessage>({ role: 'assistant', content: result.message, result });
|
||||
messages.value.push(message);
|
||||
message.content = result.message;
|
||||
message.result = result;
|
||||
try {
|
||||
await prepare(result);
|
||||
} finally {
|
||||
@@ -175,15 +210,49 @@ async function send(): Promise<void> {
|
||||
}
|
||||
for (const step of result.steps) credentials[step.id] = {};
|
||||
} catch (e) {
|
||||
if (!disposed && !axios.isCancel(e)) {
|
||||
if (controller.signal.aborted) {
|
||||
if (message) message.content = '已停止生成,任务已保留,可继续重试。';
|
||||
} else if (!disposed && !axios.isCancel(e)) {
|
||||
error.value = failure(e);
|
||||
if (axios.isAxiosError(e) && e.response?.data?.code === 'AI_NOT_CONFIGURED') disabled.value = true;
|
||||
if (message) message.content = '任务尚未完成,可继续重试。';
|
||||
if (typeof e === 'object' && e && 'code' in e && e.code === 'AI_NOT_CONFIGURED') disabled.value = true;
|
||||
}
|
||||
} finally {
|
||||
observing = false;
|
||||
if (message) message.state = controller.signal.aborted || error.value ? 'stopped' : 'done';
|
||||
busy.value = false;
|
||||
if (!disposed) taskHistory.value = await aiApi.tasks().catch(() => taskHistory.value);
|
||||
await scroll();
|
||||
}
|
||||
}
|
||||
function composerKey(event: KeyboardEvent): void {
|
||||
if (event.key === 'Enter' && !event.shiftKey && !event.isComposing) {
|
||||
event.preventDefault();
|
||||
void send();
|
||||
}
|
||||
}
|
||||
async function newConversation(): Promise<void> {
|
||||
if (busy.value || executing.value || hashing.value) return;
|
||||
if (pending.value && active.value?.planId) {
|
||||
try {
|
||||
await ElMessageBox.confirm('开始新对话将取消当前待确认计划。', '新对话', {
|
||||
confirmButtonText: '开始新对话',
|
||||
cancelButtonText: '保留当前对话',
|
||||
});
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
await aiApi.discard(active.value.planId);
|
||||
}
|
||||
clearCredentials();
|
||||
active.value = undefined;
|
||||
messages.value = [];
|
||||
attachments.value = [];
|
||||
files.clear();
|
||||
lastRequest = undefined;
|
||||
text.value = '';
|
||||
error.value = '';
|
||||
}
|
||||
async function urgentConfirmation(period: ClassPeriod | null, title: string): Promise<boolean> {
|
||||
urgent.period = period;
|
||||
urgent.title = title;
|
||||
@@ -256,46 +325,35 @@ async function execute(urgentConfirmed = false): Promise<void> {
|
||||
urgentConfirmed = true;
|
||||
}
|
||||
}
|
||||
result.steps = await aiApi.confirm(result.planId, {
|
||||
if (!taskId.value) throw new Error('请重新打开本次后台任务');
|
||||
await aiApi.executeTask(taskId.value, {
|
||||
confirmed: true,
|
||||
extraConfirmed: danger.length > 0,
|
||||
urgentConfirmed,
|
||||
secrets: { ...credentials },
|
||||
});
|
||||
clearCredentials();
|
||||
for (const step of result.steps) {
|
||||
if (step.status === 'succeeded') continue;
|
||||
if (!step.request) throw new Error('计划未确认,请刷新后重试');
|
||||
const file = step.request.attachmentId ? files.get(step.request.attachmentId) : undefined;
|
||||
if (step.request.attachmentId && !file) throw new Error('原附件已移除,请重新生成计划');
|
||||
step.status = 'running';
|
||||
try {
|
||||
step.result = await api.executeAi(step.id, step.request, file, controller.signal, (value) => {
|
||||
progress.value = value;
|
||||
});
|
||||
step.status = 'succeeded';
|
||||
} catch (e) {
|
||||
const refreshed = await refreshPlan(result)
|
||||
.then(() => true)
|
||||
.catch(() => false);
|
||||
if (!refreshed) {
|
||||
step.status = axios.isAxiosError(e) && e.response && e.response.status < 500 ? 'failed' : 'blocked';
|
||||
step.error = failure(e);
|
||||
}
|
||||
if (
|
||||
axios.isAxiosError(e) &&
|
||||
e.response?.data?.code === 'URGENT_DURING_CLASS' &&
|
||||
(await urgentConfirmation(
|
||||
e.response.data.details?.period ?? null,
|
||||
String(step.params?.title ?? ''),
|
||||
))
|
||||
) {
|
||||
executing.value = false;
|
||||
await execute(true);
|
||||
return;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
const activity = reactive<ChatMessage>({
|
||||
role: 'assistant',
|
||||
content: '',
|
||||
activity: [],
|
||||
state: 'working',
|
||||
});
|
||||
messages.value.push(activity);
|
||||
try {
|
||||
const completed = await observeTask(
|
||||
taskId.value,
|
||||
(event) => {
|
||||
if (event.type === 'status' && activity.activity!.at(-1) !== event.message)
|
||||
activity.activity!.push(event.message);
|
||||
void scroll();
|
||||
},
|
||||
controller.signal,
|
||||
);
|
||||
result.steps = completed.steps;
|
||||
activity.content = '所有步骤已完成。';
|
||||
} finally {
|
||||
activity.state = error.value ? 'stopped' : 'done';
|
||||
}
|
||||
await refreshPlan(result);
|
||||
messages.value.push({
|
||||
@@ -313,15 +371,72 @@ async function execute(urgentConfirmed = false): Promise<void> {
|
||||
}
|
||||
async function cancelPlan(): Promise<void> {
|
||||
if (executing.value) {
|
||||
if (taskId.value) await aiApi.stopTask(taskId.value);
|
||||
controller?.abort();
|
||||
return;
|
||||
}
|
||||
if (active.value?.planId) {
|
||||
await aiApi.discard(active.value.planId);
|
||||
if (taskId.value) await aiApi.stopTask(taskId.value);
|
||||
await refreshPlan(active.value);
|
||||
}
|
||||
clearCredentials();
|
||||
}
|
||||
async function stopGeneration(): Promise<void> {
|
||||
if (taskId.value) await aiApi.stopTask(taskId.value);
|
||||
controller?.abort();
|
||||
}
|
||||
async function resumeTask(id: string): Promise<void> {
|
||||
if (busy.value || executing.value) return;
|
||||
error.value = '';
|
||||
const saved = await aiApi.task(id);
|
||||
taskId.value = id;
|
||||
lastRequest = { text: saved.text };
|
||||
const message = reactive<ChatMessage>({
|
||||
role: 'assistant',
|
||||
content: saved.result?.message ?? '',
|
||||
activity: saved.events.filter((item) => item.type === 'status').map((item) => item.message),
|
||||
state: 'done',
|
||||
result: saved.result,
|
||||
});
|
||||
messages.value = [{ role: 'user', content: saved.text }, message];
|
||||
active.value = saved.result;
|
||||
clearCredentials();
|
||||
if (saved.error) error.value = saved.error;
|
||||
if (!['planning', 'executing'].includes(saved.state)) {
|
||||
await scroll();
|
||||
return;
|
||||
}
|
||||
busy.value = saved.state === 'planning';
|
||||
executing.value = saved.state === 'executing';
|
||||
message.state = 'working';
|
||||
controller = new AbortController();
|
||||
try {
|
||||
const result = reactive(
|
||||
await observeTask(
|
||||
id,
|
||||
(event) => {
|
||||
if (event.type === 'status' && message.activity!.at(-1) !== event.message)
|
||||
message.activity!.push(event.message);
|
||||
if (event.type === 'message') message.content = event.text;
|
||||
void scroll();
|
||||
},
|
||||
controller.signal,
|
||||
),
|
||||
);
|
||||
active.value = result;
|
||||
message.result = result;
|
||||
message.content = saved.state === 'executing' ? '任务已完成。' : result.message;
|
||||
clearCredentials();
|
||||
} catch (e) {
|
||||
if (!disposed && !controller.signal.aborted) error.value = failure(e);
|
||||
} finally {
|
||||
message.state = error.value ? 'stopped' : 'done';
|
||||
busy.value = false;
|
||||
executing.value = false;
|
||||
await scroll();
|
||||
}
|
||||
}
|
||||
function size(value: number): string {
|
||||
return value < 1024 * 1024 ? `${Math.ceil(value / 1024)} KB` : `${(value / 1024 / 1024).toFixed(1)} MB`;
|
||||
}
|
||||
@@ -331,13 +446,18 @@ onMounted(async () => {
|
||||
aiApi.capabilities(),
|
||||
classApi.list().catch(() => []),
|
||||
]);
|
||||
taskHistory.value = await aiApi.tasks();
|
||||
const latest =
|
||||
taskHistory.value.find((item) => ['planning', 'executing'].includes(item.state)) ??
|
||||
taskHistory.value[0];
|
||||
if (latest) await resumeTask(latest.id);
|
||||
} catch (e) {
|
||||
error.value = failure(e);
|
||||
}
|
||||
});
|
||||
onBeforeUnmount(() => {
|
||||
disposed = true;
|
||||
controller?.abort();
|
||||
if (observing || executing.value || hashing.value) controller?.abort();
|
||||
finishUrgent(false);
|
||||
clearCredentials();
|
||||
files.clear();
|
||||
@@ -352,15 +472,57 @@ onBeforeUnmount(() => {
|
||||
@dragleave.prevent="dragging = false"
|
||||
@drop.prevent="drop"
|
||||
>
|
||||
<PageHeader title="AI 助手">
|
||||
<el-select v-model="classId" clearable filterable placeholder="目标班级" :disabled="busy || executing">
|
||||
<el-option v-for="item in classes" :key="item.id" :value="item.id" :label="item.name" />
|
||||
</el-select>
|
||||
<el-button v-if="auth.can('ai:manage')" @click="$router.push('/ai-settings')">模型设置</el-button>
|
||||
</PageHeader>
|
||||
<div ref="conversation" class="assistant-conversation" aria-live="polite" :aria-busy="busy || executing">
|
||||
<header class="page-header agent-header">
|
||||
<div class="agent-brand">
|
||||
<span class="agent-mark">AI</span>
|
||||
<div>
|
||||
<h1>AI 助手</h1>
|
||||
<span class="agent-presence">{{ busy || executing ? '正在处理' : '班级工作助手' }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="agent-header-actions">
|
||||
<el-select
|
||||
v-if="taskHistory.length"
|
||||
:model-value="taskId"
|
||||
placeholder="最近任务"
|
||||
:disabled="busy || executing"
|
||||
@change="resumeTask"
|
||||
>
|
||||
<el-option
|
||||
v-for="item in taskHistory"
|
||||
:key="item.id"
|
||||
:value="item.id"
|
||||
:label="item.text.slice(0, 24)"
|
||||
/>
|
||||
</el-select>
|
||||
<el-button :disabled="busy || executing || hashing" @click="newConversation">新对话</el-button>
|
||||
<el-select
|
||||
v-model="classId"
|
||||
clearable
|
||||
filterable
|
||||
placeholder="目标班级"
|
||||
:disabled="busy || executing"
|
||||
>
|
||||
<el-option v-for="item in classes" :key="item.id" :value="item.id" :label="item.name" />
|
||||
</el-select>
|
||||
<el-button v-if="auth.can('ai:manage')" @click="$router.push('/ai-settings')">模型设置</el-button>
|
||||
</div>
|
||||
</header>
|
||||
<div
|
||||
ref="conversation"
|
||||
class="assistant-conversation"
|
||||
aria-live="polite"
|
||||
:aria-busy="busy || executing"
|
||||
@scroll="
|
||||
followScroll =
|
||||
!conversation ||
|
||||
conversation.scrollHeight - conversation.scrollTop - conversation.clientHeight < 100
|
||||
"
|
||||
>
|
||||
<div v-if="!messages.length" class="assistant-welcome">
|
||||
<h2>今天需要处理什么?</h2>
|
||||
<span class="agent-welcome-mark" aria-hidden="true">✦</span>
|
||||
<h2>有什么可以帮你?</h2>
|
||||
<p>查找信息、整理课表,或把文件交给我。</p>
|
||||
<div class="assistant-examples">
|
||||
<el-button @click="text = '查看本班今天的作业'">查看作业</el-button>
|
||||
<el-button @click="text = '将附件推送到所选班级'">推送文件</el-button>
|
||||
@@ -381,8 +543,27 @@ onBeforeUnmount(() => {
|
||||
class="assistant-message"
|
||||
:class="message.role"
|
||||
>
|
||||
<span class="assistant-role">{{ message.role === 'user' ? '你' : '助手' }}</span>
|
||||
<p>{{ message.content }}</p>
|
||||
<span class="assistant-role">{{ message.role === 'user' ? '你' : 'ClassHelper' }}</span>
|
||||
<details v-if="message.activity?.length" class="agent-activity" :open="message.state === 'working'">
|
||||
<summary>
|
||||
<span class="agent-indicator" :class="{ 'is-working': message.state === 'working' }" />
|
||||
{{
|
||||
message.state === 'working'
|
||||
? message.activity.at(-1)
|
||||
: message.state === 'stopped'
|
||||
? '处理已暂停'
|
||||
: '处理过程'
|
||||
}}
|
||||
</summary>
|
||||
<ol>
|
||||
<li v-for="(activity, n) in message.activity" :key="n">{{ activity }}</li>
|
||||
</ol>
|
||||
</details>
|
||||
<p v-if="message.content">{{ message.content }}</p>
|
||||
<span v-else-if="message.state === 'working'" class="agent-placeholder">
|
||||
正在准备回复
|
||||
<span class="agent-typing">…</span>
|
||||
</span>
|
||||
<el-card v-for="(item, n) in message.result?.results" :key="n" shadow="never">
|
||||
<template #header>{{ item.label }}</template>
|
||||
<AssistantData :value="item.data" />
|
||||
@@ -484,7 +665,7 @@ onBeforeUnmount(() => {
|
||||
<el-progress v-if="executing && progress > 0" :percentage="progress" />
|
||||
</section>
|
||||
</div>
|
||||
<el-alert v-if="error" :title="error" type="error" :closable="true" show-icon @close="error = ''" />
|
||||
<el-alert v-if="error" :title="error" type="warning" :closable="true" show-icon @close="error = ''" />
|
||||
<el-button v-if="error && !active?.steps.length && !busy && !executing" @click="retryTask">
|
||||
重试上次任务
|
||||
</el-button>
|
||||
@@ -506,20 +687,19 @@ onBeforeUnmount(() => {
|
||||
maxlength="8000"
|
||||
placeholder="描述任务,也可以拖入文件"
|
||||
:disabled="busy || executing || disabled"
|
||||
@keydown.ctrl.enter.prevent="send"
|
||||
@keydown="composerKey"
|
||||
/>
|
||||
<div class="assistant-composer-actions">
|
||||
<input ref="input" type="file" multiple hidden @change="choose" />
|
||||
<el-button :loading="hashing" :disabled="busy || executing || pending" @click="input?.click()">
|
||||
添加文件
|
||||
</el-button>
|
||||
<span class="text-muted">所有更改先确认</span>
|
||||
<el-button v-if="busy || hashing" @click="controller?.abort()">取消</el-button>
|
||||
<span class="text-muted">Enter 发送 · Shift + Enter 换行</span>
|
||||
<el-button v-if="busy || hashing" @click="stopGeneration">停止生成</el-button>
|
||||
<el-button
|
||||
type="primary"
|
||||
:loading="busy"
|
||||
:disabled="!text.trim() || hashing || executing || disabled"
|
||||
@click="send"
|
||||
:disabled="!text.trim() || busy || hashing || executing || disabled"
|
||||
@click="send()"
|
||||
>
|
||||
发送
|
||||
</el-button>
|
||||
@@ -537,27 +717,28 @@ onBeforeUnmount(() => {
|
||||
|
||||
<style scoped>
|
||||
.assistant-page {
|
||||
max-width: 1120px;
|
||||
margin: auto;
|
||||
max-width: none;
|
||||
padding: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: calc(100dvh - 110px);
|
||||
height: 100%;
|
||||
min-height: 0;
|
||||
overflow: hidden;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
.assistant-conversation {
|
||||
flex: 1;
|
||||
min-height: 240px;
|
||||
max-height: calc(100dvh - 380px);
|
||||
min-height: 0;
|
||||
overflow: auto;
|
||||
padding: 8px 2px 24px;
|
||||
padding: 32px max(20px, calc((100% - 800px) / 2)) 24px;
|
||||
scrollbar-gutter: stable;
|
||||
}
|
||||
.assistant-welcome {
|
||||
padding: clamp(20px, 4vw, 56px) 0;
|
||||
padding: clamp(24px, 7vh, 80px) 0 24px;
|
||||
text-align: center;
|
||||
}
|
||||
.assistant-welcome h2 {
|
||||
font-size: 28px;
|
||||
font-size: 2em;
|
||||
font-weight: 600;
|
||||
}
|
||||
.assistant-examples,
|
||||
@@ -572,20 +753,22 @@ onBeforeUnmount(() => {
|
||||
}
|
||||
.assistant-examples {
|
||||
margin: 24px 0;
|
||||
justify-content: center;
|
||||
}
|
||||
.assistant-capabilities {
|
||||
margin-top: 16px;
|
||||
}
|
||||
.assistant-message {
|
||||
margin: 0 0 24px;
|
||||
padding: 16px 20px;
|
||||
border: 1px solid var(--ch-ui-border);
|
||||
border-radius: var(--ch-ui-radius);
|
||||
background: var(--ch-ui-surface);
|
||||
padding: 8px 0;
|
||||
}
|
||||
.assistant-message.user {
|
||||
margin-left: 15%;
|
||||
background: var(--el-color-primary-light-9);
|
||||
margin-left: auto;
|
||||
width: fit-content;
|
||||
max-width: 85%;
|
||||
padding: 12px 20px;
|
||||
border-radius: 20px 20px 4px 20px;
|
||||
background: var(--el-fill-color);
|
||||
}
|
||||
.assistant-message p {
|
||||
white-space: pre-wrap;
|
||||
@@ -594,8 +777,9 @@ onBeforeUnmount(() => {
|
||||
line-height: 1.7;
|
||||
}
|
||||
.assistant-role {
|
||||
font-size: 12px;
|
||||
color: var(--ch-ui-muted);
|
||||
font-size: 0.85em;
|
||||
font-weight: 600;
|
||||
color: var(--ch-text-secondary);
|
||||
}
|
||||
.assistant-message .el-card {
|
||||
margin-top: 16px;
|
||||
@@ -626,11 +810,117 @@ onBeforeUnmount(() => {
|
||||
margin: 16px 0;
|
||||
}
|
||||
.assistant-composer {
|
||||
margin-top: 16px;
|
||||
width: min(880px, calc(100% - 32px));
|
||||
box-sizing: border-box;
|
||||
margin: 12px auto 8px;
|
||||
padding: 16px;
|
||||
border: 1px solid var(--ch-ui-border);
|
||||
border-radius: var(--ch-ui-radius);
|
||||
border-radius: 20px;
|
||||
background: var(--ch-ui-surface);
|
||||
box-shadow: 0 6px 24px #00000006;
|
||||
}
|
||||
.assistant-composer :deep(.el-textarea__inner) {
|
||||
box-shadow: none;
|
||||
background: transparent;
|
||||
padding: 4px;
|
||||
resize: none;
|
||||
}
|
||||
.assistant-composer:focus-within {
|
||||
border-color: var(--el-color-primary);
|
||||
}
|
||||
.agent-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
padding: 0 0 16px;
|
||||
border-bottom: 1px solid var(--ch-ui-border);
|
||||
}
|
||||
.agent-brand,
|
||||
.agent-header-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
}
|
||||
.agent-brand h1 {
|
||||
margin: 0;
|
||||
font-size: 1.15em;
|
||||
font-weight: 600;
|
||||
}
|
||||
.agent-mark {
|
||||
display: grid;
|
||||
place-items: center;
|
||||
width: 36px;
|
||||
height: 36px;
|
||||
border: 1px solid var(--ch-ui-border);
|
||||
border-radius: 11px;
|
||||
color: var(--el-color-primary);
|
||||
font-weight: 700;
|
||||
}
|
||||
.agent-presence {
|
||||
font-size: 0.8em;
|
||||
color: var(--ch-text-muted);
|
||||
}
|
||||
.agent-header-actions .el-select {
|
||||
width: 155px;
|
||||
}
|
||||
.agent-welcome-mark {
|
||||
font-size: 2.4em;
|
||||
color: var(--el-color-primary);
|
||||
}
|
||||
.assistant-welcome > p {
|
||||
color: var(--ch-text-muted);
|
||||
}
|
||||
.agent-activity {
|
||||
margin: 4px 0 12px;
|
||||
border-left: 2px solid var(--ch-ui-border);
|
||||
padding-left: 12px;
|
||||
}
|
||||
.agent-activity summary {
|
||||
list-style: none;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
.agent-activity ol {
|
||||
margin: 12px 0;
|
||||
padding-left: 20px;
|
||||
font-size: 0.85em;
|
||||
color: var(--ch-text-muted);
|
||||
line-height: 1.9;
|
||||
}
|
||||
.agent-indicator {
|
||||
width: 7px;
|
||||
height: 7px;
|
||||
border-radius: 50%;
|
||||
background: var(--el-color-success);
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.agent-indicator.is-working {
|
||||
background: var(--el-color-primary);
|
||||
animation: agent-pulse 1.3s ease-in-out infinite;
|
||||
}
|
||||
.agent-placeholder {
|
||||
color: var(--ch-text-muted);
|
||||
font-size: 0.9em;
|
||||
}
|
||||
.assistant-page > .el-alert {
|
||||
max-width: 880px;
|
||||
margin: 8px auto;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
.assistant-page > .el-button {
|
||||
align-self: center;
|
||||
}
|
||||
@keyframes agent-pulse {
|
||||
50% {
|
||||
opacity: 0.35;
|
||||
}
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.agent-indicator.is-working {
|
||||
animation: none;
|
||||
}
|
||||
}
|
||||
.assistant-composer-actions {
|
||||
margin-top: 12px;
|
||||
@@ -653,17 +943,29 @@ details > summary {
|
||||
}
|
||||
@media (max-width: 720px) {
|
||||
.assistant-page {
|
||||
height: auto;
|
||||
overflow: visible;
|
||||
height: 100%;
|
||||
}
|
||||
.agent-header {
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
.agent-header-actions {
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
}
|
||||
.agent-header-actions .el-select {
|
||||
width: 130px;
|
||||
}
|
||||
.assistant-composer {
|
||||
width: 100%;
|
||||
}
|
||||
.assistant-composer-actions .text-muted {
|
||||
display: none;
|
||||
}
|
||||
.assistant-diff {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
.assistant-message.user {
|
||||
margin-left: 8%;
|
||||
}
|
||||
.assistant-conversation {
|
||||
max-height: none;
|
||||
max-width: 92%;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in new issue
Block a user