71 lines
2.8 KiB
TypeScript
71 lines
2.8 KiB
TypeScript
import { hostApiFetch, createHostEventSource, ensureHostApiToken } from './host-api';
|
|
import type {
|
|
TeacherAvailability,
|
|
TeacherCheckInInput,
|
|
TeacherCheckInResult,
|
|
TeacherCatalog,
|
|
TeacherDefinition,
|
|
TeacherDiscussionAction,
|
|
TeacherSend,
|
|
TeacherTopic,
|
|
TeacherTopicList,
|
|
} from '../../shared/coding-teacher';
|
|
export function teacherTopicsPath(projectId: string) {
|
|
return projectId === 'preview'
|
|
? '/api/coding/teacher-preview/topics'
|
|
: `/api/coding/projects/${encodeURIComponent(projectId)}/agent-topics`;
|
|
}
|
|
/** Read old locally-generated friend histories without copying or rebinding them. */
|
|
export function legacyTopicBase(base: string, legacyFriend: boolean) {
|
|
return legacyFriend ? base.replace(/agent-topics$/, 'friend-topics') : base;
|
|
}
|
|
export const teacherApi = {
|
|
catalog: () => hostApiFetch<TeacherCatalog>('/api/coding/teacher/teachers'),
|
|
checkIn: (projectId: string, input: TeacherCheckInInput) =>
|
|
hostApiFetch<TeacherCheckInResult>(`/api/coding/projects/${encodeURIComponent(projectId)}/teacher-check-in`, {
|
|
method: 'POST',
|
|
body: JSON.stringify(input),
|
|
}),
|
|
config: () =>
|
|
hostApiFetch<TeacherAvailability & { definition: TeacherDefinition | null }>(
|
|
'/api/coding/teacher/config'
|
|
),
|
|
preview: (draftRevision: number) =>
|
|
hostApiFetch<{ payload: TeacherDefinition; draft_revision: number }>(
|
|
'/api/coding/teacher-preview?draftRevision=' + draftRevision
|
|
),
|
|
list: (base: string) => hostApiFetch<TeacherTopicList>(base),
|
|
create: (base: string, draftRevision?: number, sampleContext?: string, teacherVersion?: number) =>
|
|
hostApiFetch<TeacherTopic>(base, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ draftRevision, sampleContext, teacherVersion }),
|
|
}),
|
|
read: (base: string, id: string, select = true) =>
|
|
hostApiFetch<TeacherTopic>(base + '/' + encodeURIComponent(id) + (select ? '' : '?select=false')),
|
|
send: (base: string, id: string, input: TeacherSend) =>
|
|
hostApiFetch<TeacherTopic>(base + '/' + encodeURIComponent(id) + '/messages', {
|
|
method: 'POST',
|
|
body: JSON.stringify(input),
|
|
}),
|
|
updateDiscussion: (base: string, id: string, input: TeacherDiscussionAction) =>
|
|
hostApiFetch<TeacherTopic>(base + '/' + encodeURIComponent(id) + '/discussion', {
|
|
method: 'POST', body: JSON.stringify(input),
|
|
}),
|
|
cancel: (base: string, id: string, requestId: string) =>
|
|
hostApiFetch<TeacherTopic>(
|
|
base +
|
|
'/' +
|
|
encodeURIComponent(id) +
|
|
'/requests/' +
|
|
encodeURIComponent(requestId) +
|
|
'/cancel',
|
|
{ method: 'POST' }
|
|
),
|
|
save: (base: string, id: string) =>
|
|
hostApiFetch<TeacherTopic>(base + '/' + encodeURIComponent(id) + '/save', { method: 'POST' }),
|
|
async events(base: string, id: string) {
|
|
await ensureHostApiToken();
|
|
return createHostEventSource(base + '/' + encodeURIComponent(id) + '/events');
|
|
},
|
|
};
|