Files
makelore/electron/coding-teacher/source-reader.ts

34 lines
1.3 KiB
TypeScript

import type { CodingProjectService } from '../coding-projects/project-service';
import type { CodingConversationRuntime } from '../coding-runtime/contracts';
import { readCodingConversationHistory } from '../coding-projects/conversation-history';
import { sourceContext } from './context';
import { TeacherError } from './config-client';
import type { TeacherSourceContext } from '../../shared/coding-teacher';
export async function readTeacherSource(
projects: CodingProjectService,
runtime: CodingConversationRuntime,
userDataDir: string,
projectId: string,
sourceId: string
): Promise<TeacherSourceContext> {
const project = await projects.getProject(projectId);
const conversation = await projects.conversationStore(project.path).get(sourceId);
if (!conversation) throw new TeacherError(404, 'teacher_source_not_found', '来源会话不存在。');
try {
const snapshot = await runtime.getSnapshot(sourceId);
if (snapshot.conversation.projectId === projectId) return sourceContext(snapshot);
} catch {
/* Cold history does not require a running worker. */
}
try {
return sourceContext(await readCodingConversationHistory(userDataDir, projectId, conversation));
} catch {
throw new TeacherError(
409,
'teacher_source_unreadable',
'暂时无法读取来源会话,请先打开该会话。'
);
}
}