feat(consultation): unify server-distributed agents without fixed roles
This commit is contained in:
1 parent
248776d855
commit
8a157be4f6
35 files changed
+625
-668
No files matched your search
@@ -83,9 +83,9 @@ export function useTeacherCompanion(options: Options) {
|
||||
setState((current) => ({ ...current, loading: false }));
|
||||
return () => { alive = false; };
|
||||
}
|
||||
const base = teacherTopicsPath(projectId, '', 'teacher');
|
||||
const base = teacherTopicsPath(projectId);
|
||||
const loadConfig = async () => {
|
||||
const config = await teacherApi.config('teacher');
|
||||
const config = await teacherApi.config();
|
||||
if (alive) setState((current) => ({ ...current, definition: config.definition, enabled: config.enabled, loading: false }));
|
||||
return config;
|
||||
};
|
||||
@@ -98,7 +98,7 @@ export function useTeacherCompanion(options: Options) {
|
||||
const topic = await teacherApi.read(base, topics.lastSelectedTopicId);
|
||||
if (alive && (!latest.current.state.topic || latest.current.state.topic.id === topic.id)) noteTopic(topic);
|
||||
} catch {
|
||||
if (alive) setState((current) => ({ ...current, loading: false, error: '老师暂时没连上,稍后再来看看。' }));
|
||||
if (alive) setState((current) => ({ ...current, loading: false, error: '智能体暂时没连上,稍后再来看看。' }));
|
||||
}
|
||||
})();
|
||||
// Keep the deadline across Renderer reloads/project switches, but allow a
|
||||
@@ -109,6 +109,7 @@ export function useTeacherCompanion(options: Options) {
|
||||
writeLocal(storageKey + ':last-attempt', lastAttempt);
|
||||
const check = async () => {
|
||||
const { options: current, state: currentState } = latest.current;
|
||||
if (currentState.topic?.role === 'friend') return;
|
||||
if (!alive || inFlight.current || document.visibilityState !== 'visible' || currentState.loading) return;
|
||||
// A dropped event stream must not leave a completed reply stuck as thinking.
|
||||
if (currentState.topic?.requests.some((request) => ['preparing', 'running'].includes(request.status))) {
|
||||
@@ -146,7 +147,7 @@ export function useTeacherCompanion(options: Options) {
|
||||
} catch (error) {
|
||||
if (!alive) return;
|
||||
if (error instanceof AppError && typeof error.details?.status === 'number' && error.details.status >= 400 && error.details.status < 500 && error.details.status !== 408) writeLocal(storageKey + ':pending', null);
|
||||
setState((value) => ({ ...value, error: '老师这次没连上,可以点头像找老师聊聊。' }));
|
||||
setState((value) => ({ ...value, error: '智能体这次没连上,可以点头像找智能体聊聊。' }));
|
||||
} finally {
|
||||
if (alive) inFlight.current = false;
|
||||
}
|
||||
@@ -167,10 +168,10 @@ export function useTeacherCompanion(options: Options) {
|
||||
const topicId = topic?.id;
|
||||
const topicProjectId = topic?.projectId;
|
||||
useEffect(() => {
|
||||
if (!topicId || !topicProjectId) return;
|
||||
if (!topicId || !topicProjectId || topic?.role === 'friend') return;
|
||||
let alive = true;
|
||||
let stream: EventSource | undefined;
|
||||
const base = teacherTopicsPath(topicProjectId, '', 'teacher');
|
||||
const base = teacherTopicsPath(topicProjectId);
|
||||
void teacherApi.events(base, topicId).then((source) => {
|
||||
if (!alive) { source.close(); return; }
|
||||
stream = source;
|
||||
@@ -180,18 +181,19 @@ export function useTeacherCompanion(options: Options) {
|
||||
});
|
||||
}).catch(() => { /* The consultation can still be opened and reloaded. */ });
|
||||
return () => { alive = false; stream?.close(); };
|
||||
}, [topicId, topicProjectId, noteTopic]);
|
||||
}, [topicId, topicProjectId, topic?.role, noteTopic]);
|
||||
|
||||
const last = topic?.requests.at(-1);
|
||||
const invitation = unreadInvitation({ topic, seen: state.seen });
|
||||
const visibleState = stateScope.current === scope ? state : { definition: null, enabled: false, loading: true, topic: null, seen: [], welcomeDismissed: true, error: '' };
|
||||
// This is the published greeting, not a fabricated project assessment.
|
||||
const welcome = !visibleState.loading && visibleState.enabled && !visibleState.welcomeDismissed
|
||||
? visibleState.definition?.welcome_message.trim() || null : null;
|
||||
const selectedDefinition = topic?.definition ?? visibleState.definition;
|
||||
const welcome = topic?.role !== 'friend' && !visibleState.loading && visibleState.enabled && !visibleState.welcomeDismissed
|
||||
? selectedDefinition?.welcome_message.trim() || null : null;
|
||||
return {
|
||||
...visibleState,
|
||||
error: last?.intent === 'check-in' && ['failed', 'interrupted'].includes(last.status) ? '老师这次没连上,可以点头像找老师聊聊。' : visibleState.error,
|
||||
definition: topic?.definition ?? visibleState.definition,
|
||||
error: last?.intent === 'check-in' && ['failed', 'interrupted'].includes(last.status) ? '智能体这次没连上,可以点头像找智能体聊聊。' : visibleState.error,
|
||||
definition: selectedDefinition,
|
||||
topic,
|
||||
invitation,
|
||||
welcome,
|
||||
|
||||
Reference in new issue
Block a user