dsh-ustc-campus 0.2.14 → 0.2.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/package.json +46 -10
- package/src/custom-feeds.mjs +516 -0
- package/src/feed.mjs +539 -89
- package/src/host.mjs +106 -2
- package/src/mcp-host.mjs +56 -0
- package/src/mcp.mjs +132 -23
- package/src/recommender.mjs +1279 -0
- package/src/state.mjs +105 -11
package/src/state.mjs
CHANGED
|
@@ -2,7 +2,9 @@ import { mkdirSync, readFileSync, renameSync, writeFileSync, unlinkSync } from '
|
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
import { createHash, randomUUID } from 'node:crypto';
|
|
4
4
|
import { DAY, shDate, localMidnight, parseCalendar, calendarEntries } from './calendar.mjs';
|
|
5
|
-
import { createFeed } from './feed.mjs';
|
|
5
|
+
import { createFeed, FEED_SOURCES } from './feed.mjs';
|
|
6
|
+
import { createCustomFeeds, CustomFeedError } from './custom-feeds.mjs';
|
|
7
|
+
import { createRecommender } from './recommender.mjs';
|
|
6
8
|
import { createMaterials } from './materials.mjs';
|
|
7
9
|
import { createLocalItems, LocalItemError } from './local-items.mjs';
|
|
8
10
|
import { sanitizeText } from './public-fetch.mjs';
|
|
@@ -10,8 +12,7 @@ import { sanitizeText } from './public-fetch.mjs';
|
|
|
10
12
|
export const MAX_ICS = 1024 * 1024;
|
|
11
13
|
const MAX_SCHOOL_ENTRIES = 2000;
|
|
12
14
|
const localKinds = new Set(['course', 'exam']);
|
|
13
|
-
const
|
|
14
|
-
const readLabels = { schedule: '课表', saved: '收藏', items: '日程' };
|
|
15
|
+
const readLabels = { schedule: '课表', saved: '收藏', items: '日程', recommender: '推荐偏好', 'custom-feeds': '自定义订阅' };
|
|
15
16
|
export class CampusInputError extends Error {}
|
|
16
17
|
function clockMinutes(value) {
|
|
17
18
|
if (typeof value !== 'string') return null;
|
|
@@ -39,6 +40,9 @@ export function openCampus(dir, { now = Date.now, fetch = globalThis.fetch } = {
|
|
|
39
40
|
catch (error) { try { unlinkSync(temp); } catch { /* retain primary error */ } throw error; }
|
|
40
41
|
};
|
|
41
42
|
const feed = createFeed({ read, write, now, fetch });
|
|
43
|
+
const customFeeds = createCustomFeeds({ read, write, now, fetch });
|
|
44
|
+
const recommender = createRecommender({ read, write, now });
|
|
45
|
+
const lastExtraItemsById = new Map();
|
|
42
46
|
const materials = createMaterials({ read, write, now, fetch });
|
|
43
47
|
const localItems = createLocalItems({ read, write, now });
|
|
44
48
|
let schedule = read('schedule') ?? {};
|
|
@@ -181,6 +185,16 @@ export function openCampus(dir, { now = Date.now, fetch = globalThis.fetch } = {
|
|
|
181
185
|
recommendationAgenda = undefined;
|
|
182
186
|
return { label: removed.label };
|
|
183
187
|
}
|
|
188
|
+
function findAnyFeedItem(lookupId) {
|
|
189
|
+
return feed.getItemById(lookupId) ?? customFeeds.getItemById(lookupId) ?? lastExtraItemsById.get(lookupId) ?? null;
|
|
190
|
+
}
|
|
191
|
+
function findAnySourceMeta(srcId) {
|
|
192
|
+
if (FEED_SOURCES[srcId]) return FEED_SOURCES[srcId];
|
|
193
|
+
if (srcId === 'youth-enrolling') {
|
|
194
|
+
return { id: 'youth-enrolling', label: '青春科大 · 报名中二课', category: 'activity', kind: 'event' };
|
|
195
|
+
}
|
|
196
|
+
return customFeeds.getSourceMeta(srcId) ?? null;
|
|
197
|
+
}
|
|
184
198
|
function queryFeed(options = {}) {
|
|
185
199
|
const from = shDate(now());
|
|
186
200
|
if (options.sort === 'recommended' && recommendationAgenda?.from !== from) {
|
|
@@ -188,10 +202,43 @@ export function openCampus(dir, { now = Date.now, fetch = globalThis.fetch } = {
|
|
|
188
202
|
}
|
|
189
203
|
const coverage = options.sort === 'recommended' ? recommendationAgenda : null;
|
|
190
204
|
const interests = Array.isArray(options.interests) ? options.interests.slice(0, 12).map(x => String(x).slice(0, 64)) : [];
|
|
191
|
-
const
|
|
192
|
-
|
|
205
|
+
const importedTitles = (coverage?.entries ?? [])
|
|
206
|
+
.filter(entry => entry.kind === 'course' || entry.kind === 'exam')
|
|
207
|
+
.map(entry => entry.title)
|
|
208
|
+
.filter(Boolean);
|
|
209
|
+
const hostCourseTitles = Array.isArray(options.courseTitles)
|
|
210
|
+
? options.courseTitles.slice(0, 40).map(x => sanitizeText(String(x ?? ''), 120)).filter(Boolean)
|
|
211
|
+
: [];
|
|
212
|
+
const courseTitles = [...new Set([...hostCourseTitles, ...importedTitles])].slice(0, 40);
|
|
213
|
+
const savedTitles = Object.values(saved).map(item => item.title).filter(Boolean).slice(0, 20);
|
|
214
|
+
const hostExtraItems = Array.isArray(options.extraItems) ? options.extraItems : [];
|
|
215
|
+
for (const item of hostExtraItems) {
|
|
216
|
+
if (item && typeof item === 'object' && item.id) {
|
|
217
|
+
if (lastExtraItemsById.size >= 400) lastExtraItemsById.clear();
|
|
218
|
+
lastExtraItemsById.set(item.id, item);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
const extraItems = [...customFeeds.getItems(), ...hostExtraItems];
|
|
222
|
+
const extraSources = [
|
|
223
|
+
...customFeeds.getSourceStatuses(),
|
|
224
|
+
...(Array.isArray(options.extraSources) ? options.extraSources : [])
|
|
225
|
+
];
|
|
226
|
+
const result = feed.query({
|
|
227
|
+
source: options.source,
|
|
228
|
+
category: options.category,
|
|
229
|
+
intent: options.intent,
|
|
230
|
+
keyword: options.keyword,
|
|
231
|
+
sort: options.sort,
|
|
232
|
+
interests,
|
|
233
|
+
extraItems,
|
|
234
|
+
extraSources,
|
|
235
|
+
recommender,
|
|
236
|
+
userContext: { courseTitles, savedTitles },
|
|
237
|
+
agenda: coverage?.entries,
|
|
238
|
+
agendaCoverage: coverage ? {
|
|
193
239
|
...range(coverage.from, coverage.through), complete: coverage.imported.length > 0 && !coverage.truncated && !coverage.warnings.length,
|
|
194
|
-
} : undefined
|
|
240
|
+
} : undefined
|
|
241
|
+
});
|
|
195
242
|
return { ...result, items: result.items.map(item => ({ ...item, saved: Object.hasOwn(saved, item.id) })) };
|
|
196
243
|
}
|
|
197
244
|
function saveEvent({ id, action = 'save' } = {}) {
|
|
@@ -204,23 +251,65 @@ export function openCampus(dir, { now = Date.now, fetch = globalThis.fetch } = {
|
|
|
204
251
|
return { status: 'removed', event };
|
|
205
252
|
}
|
|
206
253
|
if (Object.hasOwn(saved, id)) return { status: 'already_saved', event: saved[id] };
|
|
207
|
-
const
|
|
254
|
+
const extraItems = [...customFeeds.getItems(), ...lastExtraItemsById.values()];
|
|
255
|
+
const item = feed.query({ extraItems }).items.find(entry => entry.id === id);
|
|
208
256
|
if (!item) throw new CampusInputError('找不到该活动;请先查询资讯');
|
|
209
257
|
// Only an official activity with a proven clock time can become an agenda
|
|
210
258
|
// event. News publications and date-only rows are never auto-saved.
|
|
211
|
-
|
|
259
|
+
const itemKind = FEED_SOURCES[item.source]?.kind;
|
|
260
|
+
if (itemKind && itemKind !== 'event') {
|
|
261
|
+
throw new CampusInputError(itemKind === 'notice' ? '通知不是活动,不能保存为日程' : '新闻不是活动,不能保存为日程');
|
|
262
|
+
}
|
|
263
|
+
if (!itemKind && item.intent !== 'activity') {
|
|
264
|
+
throw new CampusInputError('通知或资讯不是活动,不能保存为日程');
|
|
265
|
+
}
|
|
212
266
|
if (!Number.isFinite(item.start) || typeof item.date !== 'string') {
|
|
213
267
|
throw new CampusInputError('只有官方活动列表中时间明确的场次可保存;仅有日期或时间未注明的信息不会保存');
|
|
214
268
|
}
|
|
215
269
|
localMidnight(item.date);
|
|
216
|
-
const event = { id, kind: 'event', source: item.source === 'lug' ? '科大 LUG' : '官方活动', activitySource: item.source,
|
|
270
|
+
const event = { id, kind: 'event', source: item.source === 'lug' ? '科大 LUG' : item.source === 'youth-enrolling' ? '青春科大 · 第二课堂' : '官方活动', activitySource: item.source,
|
|
217
271
|
title: item.title, date: item.date, time: item.time ?? null, start: item.start, end: item.end ?? item.start,
|
|
218
272
|
location: item.location ?? '', url: item.url ?? null };
|
|
219
273
|
const next = { ...saved, [id]: event };
|
|
220
274
|
write('saved', next); saved = next;
|
|
221
275
|
recommendationAgenda = undefined;
|
|
276
|
+
recommender.recordFeedback(
|
|
277
|
+
{ action: 'save', itemId: id, item },
|
|
278
|
+
{ getItem: findAnyFeedItem, getSourceMeta: findAnySourceMeta }
|
|
279
|
+
);
|
|
222
280
|
return { status: 'saved', event, note: '仅保存到本地日程,未报名' };
|
|
223
281
|
}
|
|
282
|
+
function recordFeedFeedback(data = {}) {
|
|
283
|
+
if (!data || typeof data !== 'object' || Array.isArray(data)) {
|
|
284
|
+
throw new CampusInputError('反馈参数无效');
|
|
285
|
+
}
|
|
286
|
+
return recommender.recordFeedback(data, {
|
|
287
|
+
getItem: findAnyFeedItem,
|
|
288
|
+
getSourceMeta: findAnySourceMeta
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
function feedProfile() {
|
|
292
|
+
return recommender.getProfile();
|
|
293
|
+
}
|
|
294
|
+
function listCustomFeeds() {
|
|
295
|
+
return customFeeds.list();
|
|
296
|
+
}
|
|
297
|
+
function addCustomFeed(data) {
|
|
298
|
+
try {
|
|
299
|
+
return customFeeds.add(data);
|
|
300
|
+
} catch (error) {
|
|
301
|
+
if (error instanceof CustomFeedError) throw new CampusInputError(error.message);
|
|
302
|
+
throw error;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
function removeCustomFeed(data) {
|
|
306
|
+
try {
|
|
307
|
+
return customFeeds.remove(data);
|
|
308
|
+
} catch (error) {
|
|
309
|
+
if (error instanceof CustomFeedError) throw new CampusInputError(error.message);
|
|
310
|
+
throw error;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
224
313
|
function mutateItem(data, trusted = {}) {
|
|
225
314
|
try {
|
|
226
315
|
const trustedEntries = trusted && typeof trusted === 'object' ? trusted.schoolEntries : undefined;
|
|
@@ -245,6 +334,11 @@ export function openCampus(dir, { now = Date.now, fetch = globalThis.fetch } = {
|
|
|
245
334
|
}
|
|
246
335
|
}
|
|
247
336
|
return { agenda, importCalendar, forgetCalendar, queryFeed, saveEvent, mutateItem, findItems,
|
|
248
|
-
|
|
249
|
-
|
|
337
|
+
recordFeedFeedback, feedProfile, listCustomFeeds, addCustomFeed, removeCustomFeed,
|
|
338
|
+
refresh: async options => {
|
|
339
|
+
const [feedResult] = await Promise.all([feed.refresh(options), customFeeds.refresh(options)]);
|
|
340
|
+
return feedResult;
|
|
341
|
+
},
|
|
342
|
+
lookup: query => materials.lookup(query),
|
|
343
|
+
close() { feed.close(); customFeeds.close(); materials.close(); } };
|
|
250
344
|
}
|