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('/api/coding/teacher/teachers'), checkIn: (projectId: string, input: TeacherCheckInInput) => hostApiFetch(`/api/coding/projects/${encodeURIComponent(projectId)}/teacher-check-in`, { method: 'POST', body: JSON.stringify(input), }), config: () => hostApiFetch( '/api/coding/teacher/config' ), preview: (draftRevision: number) => hostApiFetch<{ payload: TeacherDefinition; draft_revision: number }>( '/api/coding/teacher-preview?draftRevision=' + draftRevision ), list: (base: string) => hostApiFetch(base), create: (base: string, draftRevision?: number, sampleContext?: string, teacherVersion?: number) => hostApiFetch(base, { method: 'POST', body: JSON.stringify({ draftRevision, sampleContext, teacherVersion }), }), read: (base: string, id: string, select = true) => hostApiFetch(base + '/' + encodeURIComponent(id) + (select ? '' : '?select=false')), send: (base: string, id: string, input: TeacherSend) => hostApiFetch(base + '/' + encodeURIComponent(id) + '/messages', { method: 'POST', body: JSON.stringify(input), }), updateDiscussion: (base: string, id: string, input: TeacherDiscussionAction) => hostApiFetch(base + '/' + encodeURIComponent(id) + '/discussion', { method: 'POST', body: JSON.stringify(input), }), cancel: (base: string, id: string, requestId: string) => hostApiFetch( base + '/' + encodeURIComponent(id) + '/requests/' + encodeURIComponent(requestId) + '/cancel', { method: 'POST' } ), save: (base: string, id: string) => hostApiFetch(base + '/' + encodeURIComponent(id) + '/save', { method: 'POST' }), async events(base: string, id: string) { await ensureHostApiToken(); return createHostEventSource(base + '/' + encodeURIComponent(id) + '/events'); }, };