import { hostApiFetch, createHostEventSource, ensureHostApiToken } from './host-api'; import type { TeacherModelCatalog, TeacherAvailability, TeacherCheckInInput, TeacherCheckInResult, TeacherCatalog, TeacherDefinition, TeacherSend, TeacherTopic, TeacherTopicList, TeacherConversationView, } from '../../shared/coding-teacher'; export interface TeacherCatalogState { config: TeacherAvailability & { definition: TeacherDefinition | null }; catalog: TeacherCatalog; } export function teacherTopicsPath(projectId: string) { return projectId === 'preview' ? '/api/coding/teacher-preview/topics' : `/api/coding/projects/${encodeURIComponent(projectId)}/agent-topics`; } export function teacherConversationsPath(projectId: string) { return `/api/coding/projects/${encodeURIComponent(projectId)}/agent-conversations`; } /** 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 = { models: (runtime: 'local' | 'yuxi') => hostApiFetch('/api/coding/teacher/models?runtime=' + runtime), legacyHistory: (projectId: string) => hostApiFetch(`/api/coding/projects/${encodeURIComponent(projectId)}/agent-history`), legacyRead: (projectId: string, item: TeacherTopicList['items'][number]) => hostApiFetch( `/api/coding/projects/${encodeURIComponent(projectId)}/agent-history/${encodeURIComponent(item.id)}?source=${encodeURIComponent(item.legacySourceId ?? 'project')}&friend=${item.legacyRole === 'friend'}`), seenConversation: (projectId: string, agentId: string, requestId: string) => hostApiFetch( teacherConversationsPath(projectId) + '/' + encodeURIComponent(agentId) + '/seen', { method: 'POST', body: JSON.stringify({ requestId }) }), conversation: (projectId: string, agentId: string, before?: string) => hostApiFetch( teacherConversationsPath(projectId) + '/' + encodeURIComponent(agentId) + (before ? '?before=' + encodeURIComponent(before) : '')), sendConversation: (projectId: string, agentId: string, input: TeacherSend) => hostApiFetch( teacherConversationsPath(projectId) + '/' + encodeURIComponent(agentId) + '/messages', { method: 'POST', body: JSON.stringify(input) }), catalog: () => hostApiFetch('/api/coding/teacher/teachers'), catalogRevision: () => hostApiFetch<{ revision: number }>('/api/coding/teacher/teachers/revision'), 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), }), 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'); }, };