Files
makelore/src/lib/coding-teacher.ts
brother7 efff238a75
Some checks failed
Electron E2E / Electron E2E (macos-latest) (push) Has been cancelled
Electron E2E / Electron E2E (ubuntu-latest) (push) Has been cancelled
Electron E2E / Electron E2E (windows-latest) (push) Has been cancelled
Merge remote main preserving project chats and reply recovery
2026-09-28 20:51:33 +08:00

83 lines
4.1 KiB
TypeScript

import { hostApiFetch, createHostEventSource, ensureHostApiToken } from './host-api';
import type {
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 = {
legacyHistory: (projectId: string) => hostApiFetch<TeacherTopicList>(`/api/coding/projects/${encodeURIComponent(projectId)}/agent-history`),
legacyRead: (projectId: string, item: TeacherTopicList['items'][number]) => hostApiFetch<TeacherTopic>(
`/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<TeacherTopic>(
teacherConversationsPath(projectId) + '/' + encodeURIComponent(agentId) + '/seen', { method: 'POST', body: JSON.stringify({ requestId }) }),
conversation: (projectId: string, agentId: string, before?: string) => hostApiFetch<TeacherConversationView>(
teacherConversationsPath(projectId) + '/' + encodeURIComponent(agentId) + (before ? '?before=' + encodeURIComponent(before) : '')),
sendConversation: (projectId: string, agentId: string, input: TeacherSend) => hostApiFetch<TeacherTopic>(
teacherConversationsPath(projectId) + '/' + encodeURIComponent(agentId) + '/messages', { method: 'POST', body: JSON.stringify(input) }),
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),
}),
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');
},
};